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
788 function First_Selector (Assoc : Node_Id) return Node_Id;
789 -- Obtain the first selector or choice from a given association
795 function First_Selector (Assoc : Node_Id) return Node_Id is
797 if Nkind (Assoc) = N_Component_Association then
798 return First (Choices (Assoc));
800 elsif Nkind (Assoc) = N_Discriminant_Association then
801 return (First (Selector_Names (Assoc)));
808 -- Local declarations
810 Assoc : Node_Id := Empty;
811 -- Assoc should perhaps be renamed and declared as a
812 -- Node_Or_Entity_Id since it encompasses not only component and
813 -- discriminant associations, but also discriminant components within
814 -- a type declaration or subtype indication ???
816 Assoc_Expr : Node_Id;
817 Assoc_Present : Boolean := False;
819 Unseen_Disc_Count : Nat := 0;
820 Seen_Discs : Elist_Id;
822 First_Disc : Entity_Id;
825 Return_Con : Node_Id;
828 -- Start of processing for Check_Return_Construct_Accessibility
831 -- Only perform checks on record types with access discriminants and
832 -- non-internally generated functions.
834 if not Is_Record_Type (R_Type)
835 or else not Has_Anonymous_Access_Discriminant (R_Type)
836 or else not Comes_From_Source (Return_Stmt)
841 -- We are only interested in return statements
843 if Nkind (Return_Stmt) not in
844 N_Extended_Return_Statement | N_Simple_Return_Statement
849 -- Fetch the object from the return statement, in the case of a
850 -- simple return statement the expression is part of the node.
852 if Nkind (Return_Stmt) = N_Extended_Return_Statement then
853 -- Obtain the object definition from the expanded extended return
855 Return_Con := First (Return_Object_Declarations (Return_Stmt));
856 while Present (Return_Con) loop
857 -- Inspect the original node to avoid object declarations
858 -- expanded into renamings.
860 if Nkind (Original_Node (Return_Con)) = N_Object_Declaration
861 and then Comes_From_Source (Original_Node (Return_Con))
866 Nlists.Next (Return_Con);
869 pragma Assert (Present (Return_Con));
871 -- Could be dealing with a renaming
873 Return_Con := Original_Node (Return_Con);
875 Return_Con := Expression (Return_Stmt);
878 -- Obtain the accessibility levels of the expressions associated
879 -- with all anonymous access discriminants, then generate a
880 -- dynamic check or static error when relevant.
882 Unqual := Unqualify (Original_Node (Return_Con));
884 -- Get the corresponding declaration based on the return object's
887 if Nkind (Unqual) = N_Identifier
888 and then Nkind (Parent (Entity (Unqual)))
889 in N_Object_Declaration
890 | N_Object_Renaming_Declaration
892 Obj_Decl := Original_Node (Parent (Entity (Unqual)));
894 -- We were passed the object declaration directly, so use it
896 elsif Nkind (Unqual) in N_Object_Declaration
897 | N_Object_Renaming_Declaration
901 -- Otherwise, we are looking at something else
908 -- Hop up object renamings when present
910 if Present (Obj_Decl)
911 and then Nkind (Obj_Decl) = N_Object_Renaming_Declaration
913 while Nkind (Obj_Decl) = N_Object_Renaming_Declaration loop
915 if Nkind (Name (Obj_Decl)) not in N_Entity then
916 -- We may be looking at the expansion of iterators or
917 -- some other internally generated construct, so it is safe
918 -- to ignore checks ???
920 if not Comes_From_Source (Obj_Decl) then
924 Obj_Decl := Original_Node
926 (Ultimate_Prefix (Name (Obj_Decl))));
928 -- Move up to the next declaration based on the object's name
931 Obj_Decl := Original_Node
932 (Declaration_Node (Name (Obj_Decl)));
937 -- Obtain the discriminant values from the return aggregate
939 -- Do we cover extension aggregates correctly ???
941 if Nkind (Unqual) = N_Aggregate then
942 if Present (Expressions (Unqual)) then
943 Assoc := First (Expressions (Unqual));
945 Assoc := First (Component_Associations (Unqual));
948 -- There is an object declaration for the return object
950 elsif Present (Obj_Decl) then
951 -- When a subtype indication is present in an object declaration
952 -- it must contain the object's discriminants.
954 if Nkind (Object_Definition (Obj_Decl)) = N_Subtype_Indication then
958 (Object_Definition (Obj_Decl))));
960 -- The object declaration contains an aggregate
962 elsif Present (Expression (Obj_Decl)) then
964 if Nkind (Unqualify (Expression (Obj_Decl))) = N_Aggregate then
965 -- Grab the first associated discriminant expresion
968 (Expressions (Unqualify (Expression (Obj_Decl))))
972 (Unqualify (Expression (Obj_Decl))));
975 (Component_Associations
976 (Unqualify (Expression (Obj_Decl))));
979 -- Otherwise, this is something else
985 -- There are no supplied discriminants in the object declaration,
986 -- so get them from the type definition since they must be default
989 -- Do we handle constrained subtypes correctly ???
991 elsif Nkind (Unqual) = N_Object_Declaration then
992 Assoc := First_Discriminant
993 (Etype (Object_Definition (Obj_Decl)));
996 Assoc := First_Discriminant (Etype (Unqual));
999 -- When we are not looking at an aggregate or an identifier, return
1000 -- since any other construct (like a function call) is not
1001 -- applicable since checks will be performed on the side of the
1008 -- Obtain the discriminants so we know the actual type in case the
1009 -- value of their associated expression gets implicitly converted.
1011 if No (Obj_Decl) then
1012 pragma Assert (Nkind (Unqual) = N_Aggregate);
1014 Disc := First_Discriminant (Etype (Unqual));
1017 Disc := First_Discriminant
1018 (Etype (Defining_Identifier (Obj_Decl)));
1021 -- Preserve the first discriminant for checking named associations
1025 -- Count the number of discriminants for processing an aggregate
1026 -- which includes an others.
1029 while Present (Disc) loop
1030 Unseen_Disc_Count := Unseen_Disc_Count + 1;
1032 Next_Discriminant (Disc);
1035 Seen_Discs := New_Elmt_List;
1037 -- Loop through each of the discriminants and check each expression
1038 -- associated with an anonymous access discriminant.
1040 -- When named associations occur in the return aggregate then
1041 -- discriminants can be in any order, so we need to ensure we do
1042 -- not continue to loop when all discriminants have been seen.
1045 while Present (Assoc)
1046 and then (Present (Disc) or else Assoc_Present)
1047 and then Unseen_Disc_Count > 0
1049 -- Handle named associations by searching through the names of
1050 -- the relevant discriminant components.
1053 in N_Component_Association | N_Discriminant_Association
1055 Assoc_Expr := Expression (Assoc);
1056 Assoc_Present := True;
1058 -- We currently don't handle box initialized discriminants,
1059 -- however, since default initialized anonymous access
1060 -- discriminants are a corner case, this is ok for now ???
1062 if Nkind (Assoc) = N_Component_Association
1063 and then Box_Present (Assoc)
1065 Assoc_Present := False;
1067 if Nkind (First_Selector (Assoc)) = N_Others_Choice then
1068 Unseen_Disc_Count := 0;
1071 -- When others is present we must identify a discriminant we
1072 -- haven't already seen so as to get the appropriate type for
1073 -- the static accessibility check.
1075 -- This works because all components within an others clause
1076 -- must have the same type.
1078 elsif Nkind (First_Selector (Assoc)) = N_Others_Choice then
1081 Outer : while Present (Disc) loop
1083 Current_Seen_Disc : Elmt_Id;
1085 -- Move through the list of identified discriminants
1087 Current_Seen_Disc := First_Elmt (Seen_Discs);
1088 while Present (Current_Seen_Disc) loop
1089 -- Exit the loop when we found a match
1092 Chars (Node (Current_Seen_Disc)) = Chars (Disc);
1094 Next_Elmt (Current_Seen_Disc);
1097 -- When we have exited the above loop without finding
1098 -- a match then we know that Disc has not been seen.
1100 exit Outer when No (Current_Seen_Disc);
1103 Next_Discriminant (Disc);
1106 -- If we got to an others clause with a non-zero
1107 -- discriminant count there must be a discriminant left to
1110 pragma Assert (Present (Disc));
1112 -- Set the unseen discriminant count to zero because we know
1113 -- an others clause sets all remaining components of an
1116 Unseen_Disc_Count := 0;
1118 -- Move through each of the selectors in the named association
1119 -- and obtain a discriminant for accessibility checking if one
1120 -- is referenced in the list. Also track which discriminants
1121 -- are referenced for the purpose of handling an others clause.
1125 Assoc_Choice : Node_Id;
1126 Curr_Disc : Node_Id;
1130 Curr_Disc := First_Disc;
1131 while Present (Curr_Disc) loop
1132 -- Check each of the choices in the associations for a
1133 -- match to the name of the current discriminant.
1135 Assoc_Choice := First_Selector (Assoc);
1136 while Present (Assoc_Choice) loop
1137 -- When the name matches we track that we have seen
1138 -- the discriminant, but instead of exiting the
1139 -- loop we continue iterating to make sure all the
1140 -- discriminants within the named association get
1143 if Chars (Assoc_Choice) = Chars (Curr_Disc) then
1144 Append_Elmt (Curr_Disc, Seen_Discs);
1147 Unseen_Disc_Count := Unseen_Disc_Count - 1;
1150 Next (Assoc_Choice);
1153 Next_Discriminant (Curr_Disc);
1158 -- Unwrap the associated expression if we are looking at a default
1159 -- initialized type declaration. In this case Assoc is not really
1160 -- an association, but a component declaration. Should Assoc be
1161 -- renamed in some way to be more clear ???
1163 -- This occurs when the return object does not initialize
1164 -- discriminant and instead relies on the type declaration for
1165 -- their supplied values.
1167 elsif Nkind (Assoc) in N_Entity
1168 and then Ekind (Assoc) = E_Discriminant
1170 Append_Elmt (Disc, Seen_Discs);
1172 Assoc_Expr := Discriminant_Default_Value (Assoc);
1173 Unseen_Disc_Count := Unseen_Disc_Count - 1;
1175 -- Otherwise, there is nothing to do because Assoc is an
1176 -- expression within the return aggregate itself.
1179 Append_Elmt (Disc, Seen_Discs);
1181 Assoc_Expr := Assoc;
1182 Unseen_Disc_Count := Unseen_Disc_Count - 1;
1185 -- Check the accessibility level of the expression when the
1186 -- discriminant is of an anonymous access type.
1188 if Present (Assoc_Expr)
1189 and then Present (Disc)
1190 and then Ekind (Etype (Disc)) = E_Anonymous_Access_Type
1192 -- Perform a static check first, if possible
1194 if Static_Accessibility_Level
1195 (Expr => Assoc_Expr,
1196 Level => Zero_On_Dynamic_Level,
1197 In_Return_Context => True)
1198 > Scope_Depth (Scope (Scope_Id))
1201 ("access discriminant in return object would be a dangling"
1202 & " reference", Return_Stmt);
1207 -- Otherwise, generate a dynamic check based on the extra
1208 -- accessibility of the result.
1210 if Present (Extra_Accessibility_Of_Result (Scope_Id)) then
1211 Insert_Before_And_Analyze (Return_Stmt,
1212 Make_Raise_Program_Error (Loc,
1215 Left_Opnd => Accessibility_Level
1216 (Expr => Assoc_Expr,
1217 Level => Dynamic_Level,
1218 In_Return_Context => True),
1219 Right_Opnd => Extra_Accessibility_Of_Result
1221 Reason => PE_Accessibility_Check_Failed));
1225 -- Iterate over the discriminants, except when we have encountered
1226 -- a named association since the discriminant order becomes
1227 -- irrelevant in that case.
1229 if not Assoc_Present then
1230 Next_Discriminant (Disc);
1233 -- Iterate over associations
1235 if not Is_List_Member (Assoc) then
1238 Nlists.Next (Assoc);
1241 end Check_Return_Construct_Accessibility;
1243 -------------------------------------
1244 -- Check_Return_Subtype_Indication --
1245 -------------------------------------
1247 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
1248 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
1250 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
1251 -- Subtype given in the extended return statement (must match R_Type)
1253 Subtype_Ind : constant Node_Id :=
1254 Object_Definition (Original_Node (Obj_Decl));
1256 procedure Error_No_Match (N : Node_Id);
1257 -- Output error messages for case where types do not statically
1258 -- match. N is the location for the messages.
1260 --------------------
1261 -- Error_No_Match --
1262 --------------------
1264 procedure Error_No_Match (N : Node_Id) is
1267 ("subtype must statically match function result subtype", N);
1269 if not Predicates_Match (R_Stm_Type, R_Type) then
1270 Error_Msg_Node_2 := R_Type;
1272 ("\predicate of& does not match predicate of&",
1277 -- Start of processing for Check_Return_Subtype_Indication
1280 -- First, avoid cascaded errors
1282 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
1286 -- "return access T" case; check that the return statement also has
1287 -- "access T", and that the subtypes statically match:
1288 -- if this is an access to subprogram the signatures must match.
1290 if Is_Anonymous_Access_Type (R_Type) then
1291 if Is_Anonymous_Access_Type (R_Stm_Type) then
1292 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
1294 if Base_Type (Designated_Type (R_Stm_Type)) /=
1295 Base_Type (Designated_Type (R_Type))
1296 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
1298 Error_No_Match (Subtype_Mark (Subtype_Ind));
1302 -- For two anonymous access to subprogram types, the types
1303 -- themselves must be type conformant.
1305 if not Conforming_Types
1306 (R_Stm_Type, R_Type, Fully_Conformant)
1308 Error_No_Match (Subtype_Ind);
1313 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
1316 -- If the return object is of an anonymous access type, then report
1317 -- an error if the function's result type is not also anonymous.
1319 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
1320 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
1322 ("anonymous access not allowed for function with named access "
1323 & "result", Subtype_Ind);
1325 -- Subtype indication case: check that the return object's type is
1326 -- covered by the result type, and that the subtypes statically match
1327 -- when the result subtype is constrained. Also handle record types
1328 -- with unknown discriminants for which we have built the underlying
1329 -- record view. Coverage is needed to allow specific-type return
1330 -- objects when the result type is class-wide (see AI05-32).
1332 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
1333 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
1336 (Base_Type (R_Type),
1337 Underlying_Record_View (Base_Type (R_Stm_Type))))
1339 -- A null exclusion may be present on the return type, on the
1340 -- function specification, on the object declaration or on the
1343 if Is_Access_Type (R_Type)
1345 (Can_Never_Be_Null (R_Type)
1346 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1347 Can_Never_Be_Null (R_Stm_Type)
1349 Error_No_Match (Subtype_Ind);
1352 -- AI05-103: for elementary types, subtypes must statically match
1354 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1355 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1356 Error_No_Match (Subtype_Ind);
1360 -- All remaining cases are illegal
1362 -- Note: previous versions of this subprogram allowed the return
1363 -- value to be the ancestor of the return type if the return type
1364 -- was a null extension. This was plainly incorrect.
1368 ("wrong type for return_subtype_indication", Subtype_Ind);
1370 end Check_Return_Subtype_Indication;
1372 ---------------------
1373 -- Local Variables --
1374 ---------------------
1377 Obj_Decl : Node_Id := Empty;
1379 -- Start of processing for Analyze_Function_Return
1382 Set_Return_Present (Scope_Id);
1384 if Nkind (N) = N_Simple_Return_Statement then
1385 Expr := Expression (N);
1387 -- Guard against a malformed expression. The parser may have tried to
1388 -- recover but the node is not analyzable.
1390 if Nkind (Expr) = N_Error then
1391 Set_Etype (Expr, Any_Type);
1392 Expander_Mode_Save_And_Set (False);
1396 -- The resolution of a controlled [extension] aggregate associated
1397 -- with a return statement creates a temporary which needs to be
1398 -- finalized on function exit. Wrap the return statement inside a
1399 -- block so that the finalization machinery can detect this case.
1400 -- This early expansion is done only when the return statement is
1401 -- not part of a handled sequence of statements.
1403 if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
1404 and then Needs_Finalization (R_Type)
1405 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1408 Make_Block_Statement (Loc,
1409 Handled_Statement_Sequence =>
1410 Make_Handled_Sequence_Of_Statements (Loc,
1411 Statements => New_List (Relocate_Node (N)))));
1419 -- Ada 2005 (AI-251): If the type of the returned object is
1420 -- an access to an interface type then we add an implicit type
1421 -- conversion to force the displacement of the "this" pointer to
1422 -- reference the secondary dispatch table. We cannot delay the
1423 -- generation of this implicit conversion until the expansion
1424 -- because in this case the type resolution changes the decoration
1425 -- of the expression node to match R_Type; by contrast, if the
1426 -- returned object is a class-wide interface type then it is too
1427 -- early to generate here the implicit conversion since the return
1428 -- statement may be rewritten by the expander into an extended
1429 -- return statement whose expansion takes care of adding the
1430 -- implicit type conversion to displace the pointer to the object.
1433 and then Serious_Errors_Detected = 0
1434 and then Is_Access_Type (R_Type)
1435 and then Nkind (Expr) not in N_Null | N_Raise_Expression
1436 and then Is_Interface (Designated_Type (R_Type))
1437 and then Is_Progenitor (Designated_Type (R_Type),
1438 Designated_Type (Etype (Expr)))
1440 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1444 Resolve (Expr, R_Type);
1445 Check_Limited_Return (N, Expr, R_Type);
1447 Check_Return_Construct_Accessibility (N);
1449 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1450 -- nonreturning function shall be a simple_return_statement with
1451 -- an expression that is a raise_expression, or else a call on a
1452 -- nonreturning function, or else a parenthesized expression of
1455 if Ada_Version >= Ada_2020
1456 and then No_Return (Scope_Id)
1457 and then Comes_From_Source (N)
1459 Check_No_Return_Expression (Original_Node (Expr));
1463 Obj_Decl := Last (Return_Object_Declarations (N));
1465 -- Analyze parts specific to extended_return_statement:
1468 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1469 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1472 Expr := Expression (Obj_Decl);
1474 -- Note: The check for OK_For_Limited_Init will happen in
1475 -- Analyze_Object_Declaration; we treat it as a normal
1476 -- object declaration.
1478 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1480 -- Returning a build-in-place unconstrained array type we defer
1481 -- the full analysis of the returned object to avoid generating
1482 -- the corresponding constrained subtype; otherwise the bounds
1483 -- would be created in the stack and a dangling reference would
1484 -- be returned pointing to the bounds. We perform its preanalysis
1485 -- to report errors on the initializing aggregate now (if any);
1486 -- we also ensure its activation chain and Master variable are
1487 -- defined (if tasks are being declared) since they are generated
1488 -- as part of the analysis and expansion of the object declaration
1491 if Is_Array_Type (R_Type)
1492 and then not Is_Constrained (R_Type)
1493 and then Is_Build_In_Place_Function (Scope_Id)
1494 and then Needs_BIP_Alloc_Form (Scope_Id)
1495 and then Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
1497 Preanalyze (Obj_Decl);
1499 if Expander_Active then
1500 Ensure_Activation_Chain_And_Master (Obj_Decl);
1507 Check_Return_Subtype_Indication (Obj_Decl);
1509 if Present (HSS) then
1512 if Present (Exception_Handlers (HSS)) then
1514 -- ???Has_Nested_Block_With_Handler needs to be set.
1515 -- Probably by creating an actual N_Block_Statement.
1516 -- Probably in Expand.
1522 -- Mark the return object as referenced, since the return is an
1523 -- implicit reference of the object.
1525 Set_Referenced (Defining_Identifier (Obj_Decl));
1527 Check_References (Stm_Entity);
1529 Check_Return_Construct_Accessibility (N);
1531 -- Check RM 6.5 (5.9/3)
1534 if Ada_Version < Ada_2012 then
1536 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1537 -- Can it really happen (extended return???)
1540 ("aliased only allowed for limited return objects "
1541 & "in Ada 2012??", N);
1543 elsif not Is_Limited_View (R_Type) then
1545 ("aliased only allowed for limited return objects", N);
1549 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1550 -- nonreturning function shall be a simple_return_statement.
1552 if Ada_Version >= Ada_2020
1553 and then No_Return (Scope_Id)
1554 and then Comes_From_Source (N)
1557 ("extended RETURN statement not allowed in No_Return "
1563 -- Case of Expr present
1565 if Present (Expr) then
1567 -- Defend against previous errors
1569 if Nkind (Expr) = N_Empty
1570 or else No (Etype (Expr))
1575 -- Apply constraint check. Note that this is done before the implicit
1576 -- conversion of the expression done for anonymous access types to
1577 -- ensure correct generation of the null-excluding check associated
1578 -- with null-excluding expressions found in return statements. We
1579 -- don't need a check if the subtype of the return object is the
1580 -- same as the result subtype of the function.
1582 if Nkind (N) /= N_Extended_Return_Statement
1583 or else Nkind (Obj_Decl) /= N_Object_Declaration
1584 or else Nkind (Object_Definition (Obj_Decl)) not in N_Has_Entity
1585 or else Entity (Object_Definition (Obj_Decl)) /= R_Type
1587 Apply_Constraint_Check (Expr, R_Type);
1590 -- The return value is converted to the return type of the function,
1591 -- which implies a predicate check if the return type is predicated.
1592 -- We do not apply the check for an extended return statement because
1593 -- Analyze_Object_Declaration has already done it on Obj_Decl above.
1594 -- We do not apply the check to a case expression because it will
1595 -- be expanded into a series of return statements, each of which
1596 -- will receive a predicate check.
1598 if Nkind (N) /= N_Extended_Return_Statement
1599 and then Nkind (Expr) /= N_Case_Expression
1601 Apply_Predicate_Check (Expr, R_Type);
1604 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1605 -- type, apply an implicit conversion of the expression to that type
1606 -- to force appropriate static and run-time accessibility checks.
1607 -- But we want to apply the checks to an extended return statement
1608 -- only once, i.e. not to the simple return statement generated at
1609 -- the end of its expansion because, prior to leaving the function,
1610 -- the accessibility level of the return object changes to be a level
1611 -- determined by the point of call (RM 3.10.2(10.8/3)).
1613 if Ada_Version >= Ada_2005
1614 and then Ekind (R_Type) = E_Anonymous_Access_Type
1615 and then (Nkind (N) = N_Extended_Return_Statement
1616 or else not Comes_From_Extended_Return_Statement (N))
1618 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1619 Analyze_And_Resolve (Expr, R_Type);
1621 -- If this is a local anonymous access to subprogram, the
1622 -- accessibility check can be applied statically. The return is
1623 -- illegal if the access type of the return expression is declared
1624 -- inside of the subprogram (except if it is the subtype indication
1625 -- of an extended return statement).
1627 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1628 if not Comes_From_Source (Current_Scope)
1629 or else Ekind (Current_Scope) = E_Return_Statement
1634 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1636 Error_Msg_N ("cannot return local access to subprogram", N);
1639 -- The expression cannot be of a formal incomplete type
1641 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1642 and then Is_Generic_Type (Etype (Expr))
1645 ("cannot return expression of a formal incomplete type", N);
1648 -- If the result type is class-wide, then check that the return
1649 -- expression's type is not declared at a deeper level than the
1650 -- function (RM05-6.5(5.6/2)).
1652 if Ada_Version >= Ada_2005
1653 and then Is_Class_Wide_Type (R_Type)
1655 if Type_Access_Level (Etype (Expr)) >
1656 Subprogram_Access_Level (Scope_Id)
1659 ("level of return expression type is deeper than "
1660 & "class-wide function!", Expr);
1664 -- Check incorrect use of dynamically tagged expression
1666 if Is_Tagged_Type (R_Type) then
1667 Check_Dynamically_Tagged_Expression
1673 -- ??? A real run-time accessibility check is needed in cases
1674 -- involving dereferences of access parameters. For now we just
1675 -- check the static cases.
1677 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1678 and then Is_Limited_View (Etype (Scope_Id))
1679 and then Static_Accessibility_Level (Expr, Zero_On_Dynamic_Level)
1680 > Subprogram_Access_Level (Scope_Id)
1682 -- Suppress the message in a generic, where the rewriting
1685 if Inside_A_Generic then
1690 Make_Raise_Program_Error (Loc,
1691 Reason => PE_Accessibility_Check_Failed));
1694 Error_Msg_Warn := SPARK_Mode /= On;
1695 Error_Msg_N ("cannot return a local value by reference<<", N);
1696 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1700 if Known_Null (Expr)
1701 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1702 and then Null_Exclusion_Present (Parent (Scope_Id))
1704 Apply_Compile_Time_Constraint_Error
1706 Msg => "(Ada 2005) null not allowed for "
1707 & "null-excluding return??",
1708 Reason => CE_Null_Not_Allowed);
1711 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1712 -- has no initializing expression.
1714 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1715 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1716 Subprogram_Access_Level (Scope_Id)
1719 ("level of return expression type is deeper than "
1720 & "class-wide function!", Obj_Decl);
1723 end Analyze_Function_Return;
1725 -------------------------------------
1726 -- Analyze_Generic_Subprogram_Body --
1727 -------------------------------------
1729 procedure Analyze_Generic_Subprogram_Body
1733 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1734 Kind : constant Entity_Kind := Ekind (Gen_Id);
1735 Body_Id : Entity_Id;
1740 -- Copy body and disable expansion while analyzing the generic For a
1741 -- stub, do not copy the stub (which would load the proper body), this
1742 -- will be done when the proper body is analyzed.
1744 if Nkind (N) /= N_Subprogram_Body_Stub then
1745 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1748 -- Once the contents of the generic copy and the template are
1749 -- swapped, do the same for their respective aspect specifications.
1751 Exchange_Aspects (N, New_N);
1753 -- Collect all contract-related source pragmas found within the
1754 -- template and attach them to the contract of the subprogram body.
1755 -- This contract is used in the capture of global references within
1758 Create_Generic_Contract (N);
1763 Spec := Specification (N);
1765 -- Within the body of the generic, the subprogram is callable, and
1766 -- behaves like the corresponding non-generic unit.
1768 Body_Id := Defining_Entity (Spec);
1770 if Kind = E_Generic_Procedure
1771 and then Nkind (Spec) /= N_Procedure_Specification
1773 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1776 elsif Kind = E_Generic_Function
1777 and then Nkind (Spec) /= N_Function_Specification
1779 Error_Msg_N ("invalid body for generic function ", Body_Id);
1783 Set_Corresponding_Body (Gen_Decl, Body_Id);
1785 if Has_Completion (Gen_Id)
1786 and then Nkind (Parent (N)) /= N_Subunit
1788 Error_Msg_N ("duplicate generic body", N);
1791 Set_Has_Completion (Gen_Id);
1794 if Nkind (N) = N_Subprogram_Body_Stub then
1795 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1797 Set_Corresponding_Spec (N, Gen_Id);
1800 if Nkind (Parent (N)) = N_Compilation_Unit then
1801 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1804 -- Make generic parameters immediately visible in the body. They are
1805 -- needed to process the formals declarations. Then make the formals
1806 -- visible in a separate step.
1808 Push_Scope (Gen_Id);
1812 First_Ent : Entity_Id;
1815 First_Ent := First_Entity (Gen_Id);
1818 while Present (E) and then not Is_Formal (E) loop
1823 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1825 -- Now generic formals are visible, and the specification can be
1826 -- analyzed, for subsequent conformance check.
1828 Body_Id := Analyze_Subprogram_Specification (Spec);
1830 -- Make formal parameters visible
1834 -- E is the first formal parameter, we loop through the formals
1835 -- installing them so that they will be visible.
1837 Set_First_Entity (Gen_Id, E);
1838 while Present (E) loop
1844 -- Visible generic entity is callable within its own body
1846 Set_Ekind (Gen_Id, Ekind (Body_Id));
1847 Set_Ekind (Body_Id, E_Subprogram_Body);
1848 Set_Convention (Body_Id, Convention (Gen_Id));
1849 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1850 Set_Scope (Body_Id, Scope (Gen_Id));
1852 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1854 if Nkind (N) = N_Subprogram_Body_Stub then
1856 -- No body to analyze, so restore state of generic unit
1858 Set_Ekind (Gen_Id, Kind);
1859 Set_Ekind (Body_Id, Kind);
1861 if Present (First_Ent) then
1862 Set_First_Entity (Gen_Id, First_Ent);
1869 -- If this is a compilation unit, it must be made visible explicitly,
1870 -- because the compilation of the declaration, unlike other library
1871 -- unit declarations, does not. If it is not a unit, the following
1872 -- is redundant but harmless.
1874 Set_Is_Immediately_Visible (Gen_Id);
1875 Reference_Body_Formals (Gen_Id, Body_Id);
1877 if Is_Child_Unit (Gen_Id) then
1878 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1881 Set_Actual_Subtypes (N, Current_Scope);
1883 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1884 Set_SPARK_Pragma_Inherited (Body_Id);
1886 -- Analyze any aspect specifications that appear on the generic
1889 if Has_Aspects (N) then
1890 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
1893 Analyze_Declarations (Declarations (N));
1896 -- Process the contract of the subprogram body after all declarations
1897 -- have been analyzed. This ensures that any contract-related pragmas
1898 -- are available through the N_Contract node of the body.
1900 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1902 Analyze (Handled_Statement_Sequence (N));
1903 Save_Global_References (Original_Node (N));
1905 -- Prior to exiting the scope, include generic formals again (if any
1906 -- are present) in the set of local entities.
1908 if Present (First_Ent) then
1909 Set_First_Entity (Gen_Id, First_Ent);
1912 Check_References (Gen_Id);
1915 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1916 Update_Use_Clause_Chain;
1917 Validate_Categorization_Dependency (N, Gen_Id);
1919 Check_Subprogram_Order (N);
1921 -- Outside of its body, unit is generic again
1923 Set_Ekind (Gen_Id, Kind);
1924 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1927 Style.Check_Identifier (Body_Id, Gen_Id);
1931 end Analyze_Generic_Subprogram_Body;
1933 ----------------------------
1934 -- Analyze_Null_Procedure --
1935 ----------------------------
1937 -- WARNING: This routine manages Ghost regions. Return statements must be
1938 -- replaced by gotos that jump to the end of the routine and restore the
1941 procedure Analyze_Null_Procedure
1943 Is_Completion : out Boolean)
1945 Loc : constant Source_Ptr := Sloc (N);
1946 Spec : constant Node_Id := Specification (N);
1948 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1949 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1950 Saved_ISMP : constant Boolean :=
1951 Ignore_SPARK_Mode_Pragmas_In_Instance;
1952 -- Save the Ghost and SPARK mode-related data to restore on exit
1954 Designator : Entity_Id;
1956 Null_Body : Node_Id := Empty;
1957 Null_Stmt : Node_Id := Null_Statement (Spec);
1961 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1963 -- A null procedure is Ghost when it is stand-alone and is subject to
1964 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1965 -- now, to ensure that any nodes generated during analysis and expansion
1966 -- are properly marked as Ghost.
1968 if Present (Prev) then
1969 Mark_And_Set_Ghost_Body (N, Prev);
1972 -- Capture the profile of the null procedure before analysis, for
1973 -- expansion at the freeze point and at each point of call. The body is
1974 -- used if the procedure has preconditions, or if it is a completion. In
1975 -- the first case the body is analyzed at the freeze point, in the other
1976 -- it replaces the null procedure declaration.
1978 -- For a null procedure that comes from source, a NULL statement is
1979 -- provided by the parser, which carries the source location of the
1980 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1981 -- from expansion, create one now.
1983 if No (Null_Stmt) then
1984 Null_Stmt := Make_Null_Statement (Loc);
1988 Make_Subprogram_Body (Loc,
1989 Specification => New_Copy_Tree (Spec),
1990 Declarations => New_List,
1991 Handled_Statement_Sequence =>
1992 Make_Handled_Sequence_Of_Statements (Loc,
1993 Statements => New_List (Null_Stmt)));
1995 -- Create new entities for body and formals
1997 Set_Defining_Unit_Name (Specification (Null_Body),
1998 Make_Defining_Identifier
1999 (Sloc (Defining_Entity (N)),
2000 Chars (Defining_Entity (N))));
2002 Form := First (Parameter_Specifications (Specification (Null_Body)));
2003 while Present (Form) loop
2004 Set_Defining_Identifier (Form,
2005 Make_Defining_Identifier
2006 (Sloc (Defining_Identifier (Form)),
2007 Chars (Defining_Identifier (Form))));
2011 -- Determine whether the null procedure may be a completion of a generic
2012 -- suprogram, in which case we use the new null body as the completion
2013 -- and set minimal semantic information on the original declaration,
2014 -- which is rewritten as a null statement.
2016 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
2017 Insert_Before (N, Null_Body);
2018 Set_Ekind (Defining_Entity (N), Ekind (Prev));
2020 Rewrite (N, Make_Null_Statement (Loc));
2021 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
2022 Is_Completion := True;
2027 -- Resolve the types of the formals now, because the freeze point may
2028 -- appear in a different context, e.g. an instantiation.
2030 Form := First (Parameter_Specifications (Specification (Null_Body)));
2031 while Present (Form) loop
2032 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
2033 Find_Type (Parameter_Type (Form));
2035 elsif No (Access_To_Subprogram_Definition
2036 (Parameter_Type (Form)))
2038 Find_Type (Subtype_Mark (Parameter_Type (Form)));
2040 -- The case of a null procedure with a formal that is an
2041 -- access-to-subprogram type, and that is used as an actual
2042 -- in an instantiation is left to the enthusiastic reader.
2052 -- If there are previous overloadable entities with the same name, check
2053 -- whether any of them is completed by the null procedure.
2055 if Present (Prev) and then Is_Overloadable (Prev) then
2056 Designator := Analyze_Subprogram_Specification (Spec);
2057 Prev := Find_Corresponding_Spec (N);
2060 if No (Prev) or else not Comes_From_Source (Prev) then
2061 Designator := Analyze_Subprogram_Specification (Spec);
2062 Set_Has_Completion (Designator);
2064 -- Signal to caller that this is a procedure declaration
2066 Is_Completion := False;
2068 -- Null procedures are always inlined, but generic formal subprograms
2069 -- which appear as such in the internal instance of formal packages,
2070 -- need no completion and are not marked Inline.
2073 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
2075 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2076 Set_Body_To_Inline (N, Null_Body);
2077 Set_Is_Inlined (Designator);
2081 -- The null procedure is a completion. We unconditionally rewrite
2082 -- this as a null body (even if expansion is not active), because
2083 -- there are various error checks that are applied on this body
2084 -- when it is analyzed (e.g. correct aspect placement).
2086 if Has_Completion (Prev) then
2087 Error_Msg_Sloc := Sloc (Prev);
2088 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
2091 Check_Previous_Null_Procedure (N, Prev);
2093 Is_Completion := True;
2094 Rewrite (N, Null_Body);
2099 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
2100 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2101 end Analyze_Null_Procedure;
2103 -----------------------------
2104 -- Analyze_Operator_Symbol --
2105 -----------------------------
2107 -- An operator symbol such as "+" or "and" may appear in context where the
2108 -- literal denotes an entity name, such as "+"(x, y) or in context when it
2109 -- is just a string, as in (conjunction = "or"). In these cases the parser
2110 -- generates this node, and the semantics does the disambiguation. Other
2111 -- such case are actuals in an instantiation, the generic unit in an
2112 -- instantiation, pragma arguments, and aspect specifications.
2114 procedure Analyze_Operator_Symbol (N : Node_Id) is
2115 Par : constant Node_Id := Parent (N);
2117 Maybe_Aspect_Spec : Node_Id := Par;
2119 if Nkind (Maybe_Aspect_Spec) /= N_Aspect_Specification then
2120 -- deal with N_Aggregate nodes
2121 Maybe_Aspect_Spec := Parent (Maybe_Aspect_Spec);
2124 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
2125 or else Nkind (Par) = N_Function_Instantiation
2126 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
2127 or else (Nkind (Par) = N_Pragma_Argument_Association
2128 and then not Is_Pragma_String_Literal (Par))
2129 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
2130 or else (Nkind (Par) = N_Attribute_Reference
2131 and then Attribute_Name (Par) /= Name_Value)
2132 or else (Nkind (Maybe_Aspect_Spec) = N_Aspect_Specification
2133 and then Get_Aspect_Id (Maybe_Aspect_Spec)
2134 -- include other aspects here ???
2135 in Aspect_Stable_Properties | Aspect_Aggregate)
2137 Find_Direct_Name (N);
2140 Change_Operator_Symbol_To_String_Literal (N);
2143 end Analyze_Operator_Symbol;
2145 -----------------------------------
2146 -- Analyze_Parameter_Association --
2147 -----------------------------------
2149 procedure Analyze_Parameter_Association (N : Node_Id) is
2151 Analyze (Explicit_Actual_Parameter (N));
2152 end Analyze_Parameter_Association;
2154 ----------------------------
2155 -- Analyze_Procedure_Call --
2156 ----------------------------
2158 -- WARNING: This routine manages Ghost regions. Return statements must be
2159 -- replaced by gotos that jump to the end of the routine and restore the
2162 procedure Analyze_Procedure_Call (N : Node_Id) is
2163 procedure Analyze_Call_And_Resolve;
2164 -- Do Analyze and Resolve calls for procedure call. At the end, check
2165 -- for illegal order dependence.
2166 -- ??? where is the check for illegal order dependencies?
2168 ------------------------------
2169 -- Analyze_Call_And_Resolve --
2170 ------------------------------
2172 procedure Analyze_Call_And_Resolve is
2174 if Nkind (N) = N_Procedure_Call_Statement then
2176 Resolve (N, Standard_Void_Type);
2180 end Analyze_Call_And_Resolve;
2184 Actuals : constant List_Id := Parameter_Associations (N);
2185 Loc : constant Source_Ptr := Sloc (N);
2186 P : constant Node_Id := Name (N);
2188 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
2189 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
2190 -- Save the Ghost-related attributes to restore on exit
2195 -- Start of processing for Analyze_Procedure_Call
2198 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
2199 -- a procedure call or an entry call. The prefix may denote an access
2200 -- to subprogram type, in which case an implicit dereference applies.
2201 -- If the prefix is an indexed component (without implicit dereference)
2202 -- then the construct denotes a call to a member of an entire family.
2203 -- If the prefix is a simple name, it may still denote a call to a
2204 -- parameterless member of an entry family. Resolution of these various
2205 -- interpretations is delicate.
2207 -- Do not analyze machine code statements to avoid rejecting them in
2210 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
2211 Set_Etype (P, Standard_Void_Type);
2216 -- If this is a call of the form Obj.Op, the call may have been analyzed
2217 -- and possibly rewritten into a block, in which case we are done.
2219 if Analyzed (N) then
2222 -- If there is an error analyzing the name (which may have been
2223 -- rewritten if the original call was in prefix notation) then error
2224 -- has been emitted already, mark node and return.
2226 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
2227 Set_Etype (N, Any_Type);
2231 -- A procedure call is Ghost when its name denotes a Ghost procedure.
2232 -- Set the mode now to ensure that any nodes generated during analysis
2233 -- and expansion are properly marked as Ghost.
2235 Mark_And_Set_Ghost_Procedure_Call (N);
2237 -- Otherwise analyze the parameters
2239 if Present (Actuals) then
2240 Actual := First (Actuals);
2242 while Present (Actual) loop
2244 Check_Parameterless_Call (Actual);
2249 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
2251 if Nkind (P) = N_Attribute_Reference
2252 and then Attribute_Name (P) in Name_Elab_Spec
2254 | Name_Elab_Subp_Body
2256 if Present (Actuals) then
2258 ("no parameters allowed for this call", First (Actuals));
2262 Set_Etype (N, Standard_Void_Type);
2265 elsif Is_Entity_Name (P)
2266 and then Is_Record_Type (Etype (Entity (P)))
2267 and then Remote_AST_I_Dereference (P)
2271 elsif Is_Entity_Name (P)
2272 and then Ekind (Entity (P)) /= E_Entry_Family
2274 if Is_Access_Type (Etype (P))
2275 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
2276 and then No (Actuals)
2277 and then Comes_From_Source (N)
2279 Error_Msg_N ("missing explicit dereference in call", N);
2281 elsif Ekind (Entity (P)) = E_Operator then
2282 Error_Msg_Name_1 := Chars (P);
2283 Error_Msg_N ("operator % cannot be used as a procedure", N);
2286 Analyze_Call_And_Resolve;
2288 -- If the prefix is the simple name of an entry family, this is a
2289 -- parameterless call from within the task body itself.
2291 elsif Is_Entity_Name (P)
2292 and then Nkind (P) = N_Identifier
2293 and then Ekind (Entity (P)) = E_Entry_Family
2294 and then Present (Actuals)
2295 and then No (Next (First (Actuals)))
2297 -- Can be call to parameterless entry family. What appears to be the
2298 -- sole argument is in fact the entry index. Rewrite prefix of node
2299 -- accordingly. Source representation is unchanged by this
2303 Make_Indexed_Component (Loc,
2305 Make_Selected_Component (Loc,
2306 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
2307 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
2308 Expressions => Actuals);
2309 Set_Name (N, New_N);
2310 Set_Etype (New_N, Standard_Void_Type);
2311 Set_Parameter_Associations (N, No_List);
2312 Analyze_Call_And_Resolve;
2314 elsif Nkind (P) = N_Explicit_Dereference then
2315 if Ekind (Etype (P)) = E_Subprogram_Type then
2316 Analyze_Call_And_Resolve;
2318 Error_Msg_N ("expect access to procedure in call", P);
2321 -- The name can be a selected component or an indexed component that
2322 -- yields an access to subprogram. Such a prefix is legal if the call
2323 -- has parameter associations.
2325 elsif Is_Access_Type (Etype (P))
2326 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
2328 if Present (Actuals) then
2329 Analyze_Call_And_Resolve;
2331 Error_Msg_N ("missing explicit dereference in call ", N);
2334 -- If not an access to subprogram, then the prefix must resolve to the
2335 -- name of an entry, entry family, or protected operation.
2337 -- For the case of a simple entry call, P is a selected component where
2338 -- the prefix is the task and the selector name is the entry. A call to
2339 -- a protected procedure will have the same syntax. If the protected
2340 -- object contains overloaded operations, the entity may appear as a
2341 -- function, the context will select the operation whose type is Void.
2343 elsif Nkind (P) = N_Selected_Component
2344 and then Ekind (Entity (Selector_Name (P)))
2345 in E_Entry | E_Function | E_Procedure
2347 -- When front-end inlining is enabled, as with SPARK_Mode, a call
2348 -- in prefix notation may still be missing its controlling argument,
2349 -- so perform the transformation now.
2351 if SPARK_Mode = On and then In_Inlined_Body then
2353 Subp : constant Entity_Id := Entity (Selector_Name (P));
2354 Typ : constant Entity_Id := Etype (Prefix (P));
2357 if Is_Tagged_Type (Typ)
2358 and then Present (First_Formal (Subp))
2359 and then (Etype (First_Formal (Subp)) = Typ
2361 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
2362 and then Try_Object_Operation (P)
2367 Analyze_Call_And_Resolve;
2372 Analyze_Call_And_Resolve;
2375 elsif Nkind (P) = N_Selected_Component
2376 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
2377 and then Present (Actuals)
2378 and then No (Next (First (Actuals)))
2380 -- Can be call to parameterless entry family. What appears to be the
2381 -- sole argument is in fact the entry index. Rewrite prefix of node
2382 -- accordingly. Source representation is unchanged by this
2386 Make_Indexed_Component (Loc,
2387 Prefix => New_Copy (P),
2388 Expressions => Actuals);
2389 Set_Name (N, New_N);
2390 Set_Etype (New_N, Standard_Void_Type);
2391 Set_Parameter_Associations (N, No_List);
2392 Analyze_Call_And_Resolve;
2394 -- For the case of a reference to an element of an entry family, P is
2395 -- an indexed component whose prefix is a selected component (task and
2396 -- entry family), and whose index is the entry family index.
2398 elsif Nkind (P) = N_Indexed_Component
2399 and then Nkind (Prefix (P)) = N_Selected_Component
2400 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
2402 Analyze_Call_And_Resolve;
2404 -- If the prefix is the name of an entry family, it is a call from
2405 -- within the task body itself.
2407 elsif Nkind (P) = N_Indexed_Component
2408 and then Nkind (Prefix (P)) = N_Identifier
2409 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
2412 Make_Selected_Component (Loc,
2414 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
2415 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
2416 Rewrite (Prefix (P), New_N);
2418 Analyze_Call_And_Resolve;
2420 -- In Ada 2012. a qualified expression is a name, but it cannot be a
2421 -- procedure name, so the construct can only be a qualified expression.
2423 elsif Nkind (P) = N_Qualified_Expression
2424 and then Ada_Version >= Ada_2012
2426 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
2429 -- Anything else is an error
2432 Error_Msg_N ("invalid procedure or entry call", N);
2434 -- Specialize the error message in the case where both a primitive
2435 -- operation and a record component are visible at the same time.
2437 if Nkind (P) = N_Selected_Component
2438 and then Is_Entity_Name (Selector_Name (P))
2441 Sel : constant Entity_Id := Entity (Selector_Name (P));
2443 if Ekind (Sel) = E_Component
2444 and then Present (Homonym (Sel))
2445 and then Ekind (Homonym (Sel)) = E_Procedure
2447 Error_Msg_NE ("\component & conflicts with"
2448 & " homonym procedure (RM 4.1.3 (9.2/3))",
2449 Selector_Name (P), Sel);
2456 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2457 end Analyze_Procedure_Call;
2459 ------------------------------
2460 -- Analyze_Return_Statement --
2461 ------------------------------
2463 procedure Analyze_Return_Statement (N : Node_Id) is
2465 (Nkind (N) in N_Extended_Return_Statement | N_Simple_Return_Statement);
2467 Returns_Object : constant Boolean :=
2468 Nkind (N) = N_Extended_Return_Statement
2470 (Nkind (N) = N_Simple_Return_Statement
2471 and then Present (Expression (N)));
2472 -- True if we're returning something; that is, "return <expression>;"
2473 -- or "return Result : T [:= ...]". False for "return;". Used for error
2474 -- checking: If Returns_Object is True, N should apply to a function
2475 -- body; otherwise N should apply to a procedure body, entry body,
2476 -- accept statement, or extended return statement.
2478 function Find_What_It_Applies_To return Entity_Id;
2479 -- Find the entity representing the innermost enclosing body, accept
2480 -- statement, or extended return statement. If the result is a callable
2481 -- construct or extended return statement, then this will be the value
2482 -- of the Return_Applies_To attribute. Otherwise, the program is
2483 -- illegal. See RM-6.5(4/2).
2485 -----------------------------
2486 -- Find_What_It_Applies_To --
2487 -----------------------------
2489 function Find_What_It_Applies_To return Entity_Id is
2490 Result : Entity_Id := Empty;
2493 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2494 -- and postconditions.
2496 for J in reverse 0 .. Scope_Stack.Last loop
2497 Result := Scope_Stack.Table (J).Entity;
2498 exit when Ekind (Result) not in E_Block | E_Loop
2499 and then Chars (Result) /= Name_uPostconditions;
2502 pragma Assert (Present (Result));
2504 end Find_What_It_Applies_To;
2506 -- Local declarations
2508 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2509 Kind : constant Entity_Kind := Ekind (Scope_Id);
2510 Loc : constant Source_Ptr := Sloc (N);
2511 Stm_Entity : constant Entity_Id :=
2513 (E_Return_Statement, Current_Scope, Loc, 'R');
2515 -- Start of processing for Analyze_Return_Statement
2518 Set_Return_Statement_Entity (N, Stm_Entity);
2520 Set_Etype (Stm_Entity, Standard_Void_Type);
2521 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2523 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2524 -- (4/2): an inner return statement will apply to this extended return.
2526 if Nkind (N) = N_Extended_Return_Statement then
2527 Push_Scope (Stm_Entity);
2530 -- Check that pragma No_Return is obeyed. Don't complain about the
2531 -- implicitly-generated return that is placed at the end.
2533 if No_Return (Scope_Id)
2534 and then Kind in E_Procedure | E_Generic_Procedure
2535 and then Comes_From_Source (N)
2538 ("RETURN statement not allowed in No_Return procedure", N);
2541 -- Warn on any unassigned OUT parameters if in procedure
2543 if Ekind (Scope_Id) = E_Procedure then
2544 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2547 -- Check that functions return objects, and other things do not
2549 if Kind in E_Function | E_Generic_Function then
2550 if not Returns_Object then
2551 Error_Msg_N ("missing expression in return from function", N);
2554 elsif Kind in E_Procedure | E_Generic_Procedure then
2555 if Returns_Object then
2556 Error_Msg_N ("procedure cannot return value (use function)", N);
2559 elsif Kind in E_Entry | E_Entry_Family then
2560 if Returns_Object then
2561 if Is_Protected_Type (Scope (Scope_Id)) then
2562 Error_Msg_N ("entry body cannot return value", N);
2564 Error_Msg_N ("accept statement cannot return value", N);
2568 elsif Kind = E_Return_Statement then
2570 -- We are nested within another return statement, which must be an
2571 -- extended_return_statement.
2573 if Returns_Object then
2574 if Nkind (N) = N_Extended_Return_Statement then
2576 ("extended return statement cannot be nested (use `RETURN;`)",
2579 -- Case of a simple return statement with a value inside extended
2580 -- return statement.
2584 ("return nested in extended return statement cannot return "
2585 & "value (use `RETURN;`)", N);
2590 Error_Msg_N ("illegal context for return statement", N);
2593 if Kind in E_Function | E_Generic_Function then
2594 Analyze_Function_Return (N);
2596 elsif Kind in E_Procedure | E_Generic_Procedure then
2597 Set_Return_Present (Scope_Id);
2600 if Nkind (N) = N_Extended_Return_Statement then
2604 Kill_Current_Values (Last_Assignment_Only => True);
2605 Check_Unreachable_Code (N);
2607 Analyze_Dimension (N);
2608 end Analyze_Return_Statement;
2610 -------------------------------------
2611 -- Analyze_Simple_Return_Statement --
2612 -------------------------------------
2614 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2616 if Present (Expression (N)) then
2617 Mark_Coextensions (N, Expression (N));
2620 Analyze_Return_Statement (N);
2621 end Analyze_Simple_Return_Statement;
2623 -------------------------
2624 -- Analyze_Return_Type --
2625 -------------------------
2627 procedure Analyze_Return_Type (N : Node_Id) is
2628 Designator : constant Entity_Id := Defining_Entity (N);
2629 Typ : Entity_Id := Empty;
2632 -- Normal case where result definition does not indicate an error
2634 if Result_Definition (N) /= Error then
2635 if Nkind (Result_Definition (N)) = N_Access_Definition then
2637 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2640 AD : constant Node_Id :=
2641 Access_To_Subprogram_Definition (Result_Definition (N));
2643 if Present (AD) and then Protected_Present (AD) then
2644 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2646 Typ := Access_Definition (N, Result_Definition (N));
2650 Set_Parent (Typ, Result_Definition (N));
2651 Set_Is_Local_Anonymous_Access (Typ);
2652 Set_Etype (Designator, Typ);
2654 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2656 Null_Exclusion_Static_Checks (N);
2658 -- Subtype_Mark case
2661 Find_Type (Result_Definition (N));
2662 Typ := Entity (Result_Definition (N));
2663 Set_Etype (Designator, Typ);
2665 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2667 Null_Exclusion_Static_Checks (N);
2669 -- If a null exclusion is imposed on the result type, then create
2670 -- a null-excluding itype (an access subtype) and use it as the
2671 -- function's Etype. Note that the null exclusion checks are done
2672 -- right before this, because they don't get applied to types that
2673 -- do not come from source.
2675 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2676 Set_Etype (Designator,
2677 Create_Null_Excluding_Itype
2680 Scope_Id => Scope (Current_Scope)));
2682 -- The new subtype must be elaborated before use because
2683 -- it is visible outside of the function. However its base
2684 -- type may not be frozen yet, so the reference that will
2685 -- force elaboration must be attached to the freezing of
2688 -- If the return specification appears on a proper body,
2689 -- the subtype will have been created already on the spec.
2691 if Is_Frozen (Typ) then
2692 if Nkind (Parent (N)) = N_Subprogram_Body
2693 and then Nkind (Parent (Parent (N))) = N_Subunit
2697 Build_Itype_Reference (Etype (Designator), Parent (N));
2701 Ensure_Freeze_Node (Typ);
2704 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2706 Set_Itype (IR, Etype (Designator));
2707 Append_Freeze_Actions (Typ, New_List (IR));
2712 Set_Etype (Designator, Typ);
2715 if Ekind (Typ) = E_Incomplete_Type
2716 or else (Is_Class_Wide_Type (Typ)
2717 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2719 -- AI05-0151: Tagged incomplete types are allowed in all formal
2720 -- parts. Untagged incomplete types are not allowed in bodies.
2721 -- As a consequence, limited views cannot appear in a basic
2722 -- declaration that is itself within a body, because there is
2723 -- no point at which the non-limited view will become visible.
2725 if Ada_Version >= Ada_2012 then
2726 if From_Limited_With (Typ) and then In_Package_Body then
2728 ("invalid use of incomplete type&",
2729 Result_Definition (N), Typ);
2731 -- The return type of a subprogram body cannot be of a
2732 -- formal incomplete type.
2734 elsif Is_Generic_Type (Typ)
2735 and then Nkind (Parent (N)) = N_Subprogram_Body
2738 ("return type cannot be a formal incomplete type",
2739 Result_Definition (N));
2741 elsif Is_Class_Wide_Type (Typ)
2742 and then Is_Generic_Type (Root_Type (Typ))
2743 and then Nkind (Parent (N)) = N_Subprogram_Body
2746 ("return type cannot be a formal incomplete type",
2747 Result_Definition (N));
2749 elsif Is_Tagged_Type (Typ) then
2752 -- Use is legal in a thunk generated for an operation
2753 -- inherited from a progenitor.
2755 elsif Is_Thunk (Designator)
2756 and then Present (Non_Limited_View (Typ))
2760 elsif Nkind (Parent (N)) = N_Subprogram_Body
2761 or else Nkind (Parent (Parent (N))) in
2762 N_Accept_Statement | N_Entry_Body
2765 ("invalid use of untagged incomplete type&",
2769 -- The type must be completed in the current package. This
2770 -- is checked at the end of the package declaration when
2771 -- Taft-amendment types are identified. If the return type
2772 -- is class-wide, there is no required check, the type can
2773 -- be a bona fide TAT.
2775 if Ekind (Scope (Current_Scope)) = E_Package
2776 and then In_Private_Part (Scope (Current_Scope))
2777 and then not Is_Class_Wide_Type (Typ)
2779 Append_Elmt (Designator, Private_Dependents (Typ));
2784 ("invalid use of incomplete type&", Designator, Typ);
2789 -- Case where result definition does indicate an error
2792 Set_Etype (Designator, Any_Type);
2794 end Analyze_Return_Type;
2796 -----------------------------
2797 -- Analyze_Subprogram_Body --
2798 -----------------------------
2800 procedure Analyze_Subprogram_Body (N : Node_Id) is
2801 Loc : constant Source_Ptr := Sloc (N);
2802 Body_Spec : constant Node_Id := Specification (N);
2803 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2806 if Debug_Flag_C then
2807 Write_Str ("==> subprogram body ");
2808 Write_Name (Chars (Body_Id));
2809 Write_Str (" from ");
2810 Write_Location (Loc);
2815 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2817 -- The real work is split out into the helper, so it can do "return;"
2818 -- without skipping the debug output:
2820 Analyze_Subprogram_Body_Helper (N);
2822 if Debug_Flag_C then
2824 Write_Str ("<== subprogram body ");
2825 Write_Name (Chars (Body_Id));
2826 Write_Str (" from ");
2827 Write_Location (Loc);
2830 end Analyze_Subprogram_Body;
2832 ------------------------------------
2833 -- Analyze_Subprogram_Body_Helper --
2834 ------------------------------------
2836 -- This procedure is called for regular subprogram bodies, generic bodies,
2837 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2838 -- specification matters, and is used to create a proper declaration for
2839 -- the subprogram, or to perform conformance checks.
2841 -- WARNING: This routine manages Ghost regions. Return statements must be
2842 -- replaced by gotos that jump to the end of the routine and restore the
2845 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2846 Body_Spec : Node_Id := Specification (N);
2847 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2848 Loc : constant Source_Ptr := Sloc (N);
2849 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2851 Body_Nod : Node_Id := Empty;
2852 Minimum_Acc_Objs : List_Id := No_List;
2854 Conformant : Boolean;
2855 Desig_View : Entity_Id := Empty;
2856 Exch_Views : Elist_Id := No_Elist;
2858 Mask_Types : Elist_Id := No_Elist;
2859 Prot_Typ : Entity_Id := Empty;
2860 Spec_Decl : Node_Id := Empty;
2861 Spec_Id : Entity_Id;
2863 Last_Real_Spec_Entity : Entity_Id := Empty;
2864 -- When we analyze a separate spec, the entity chain ends up containing
2865 -- the formals, as well as any itypes generated during analysis of the
2866 -- default expressions for parameters, or the arguments of associated
2867 -- precondition/postcondition pragmas (which are analyzed in the context
2868 -- of the spec since they have visibility on formals).
2870 -- These entities belong with the spec and not the body. However we do
2871 -- the analysis of the body in the context of the spec (again to obtain
2872 -- visibility to the formals), and all the entities generated during
2873 -- this analysis end up also chained to the entity chain of the spec.
2874 -- But they really belong to the body, and there is circuitry to move
2875 -- them from the spec to the body.
2877 -- However, when we do this move, we don't want to move the real spec
2878 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2879 -- variable points to the last real spec entity, so we only move those
2880 -- chained beyond that point. It is initialized to Empty to deal with
2881 -- the case where there is no separate spec.
2883 function Body_Has_Contract return Boolean;
2884 -- Check whether unanalyzed body has an aspect or pragma that may
2885 -- generate a SPARK contract.
2887 function Body_Has_SPARK_Mode_On return Boolean;
2888 -- Check whether SPARK_Mode On applies to the subprogram body, either
2889 -- because it is specified directly on the body, or because it is
2890 -- inherited from the enclosing subprogram or package.
2892 function Build_Internal_Protected_Declaration
2893 (N : Node_Id) return Entity_Id;
2894 -- A subprogram body without a previous spec that appears in a protected
2895 -- body must be expanded separately to create a subprogram declaration
2896 -- for it, in order to resolve internal calls to it from other protected
2899 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2901 procedure Build_Subprogram_Declaration;
2902 -- Create a matching subprogram declaration for subprogram body N
2904 procedure Check_Anonymous_Return;
2905 -- Ada 2005: if a function returns an access type that denotes a task,
2906 -- or a type that contains tasks, we must create a master entity for
2907 -- the anonymous type, which typically will be used in an allocator
2908 -- in the body of the function.
2910 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2911 -- Look ahead to recognize a pragma that may appear after the body.
2912 -- If there is a previous spec, check that it appears in the same
2913 -- declarative part. If the pragma is Inline_Always, perform inlining
2914 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2915 -- If the body acts as a spec, and inlining is required, we create a
2916 -- subprogram declaration for it, in order to attach the body to inline.
2917 -- If pragma does not appear after the body, check whether there is
2918 -- an inline pragma before any local declarations.
2920 procedure Check_Missing_Return;
2921 -- Checks for a function with a no return statements, and also performs
2922 -- the warning checks implemented by Check_Returns. In formal mode, also
2923 -- verify that a function ends with a RETURN and that a procedure does
2924 -- not contain any RETURN.
2926 function Disambiguate_Spec return Entity_Id;
2927 -- When a primitive is declared between the private view and the full
2928 -- view of a concurrent type which implements an interface, a special
2929 -- mechanism is used to find the corresponding spec of the primitive
2932 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2933 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2934 -- incomplete types coming from a limited context and replace their
2935 -- limited views with the non-limited ones. Return the list of changes
2936 -- to be used to undo the transformation.
2938 procedure Generate_Minimum_Accessibility
2939 (Extra_Access : Entity_Id;
2940 Related_Form : Entity_Id := Empty);
2941 -- Generate a minimum accessibility object for a given extra
2942 -- accessibility formal (Extra_Access) and its related formal if it
2945 function Is_Private_Concurrent_Primitive
2946 (Subp_Id : Entity_Id) return Boolean;
2947 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2948 -- type that implements an interface and has a private view.
2950 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2951 -- N is the body generated for an expression function that is not a
2952 -- completion and Spec_Id the defining entity of its spec. Mark all
2953 -- the not-yet-frozen types referenced by the simple return statement
2954 -- of the function as formally frozen.
2956 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2957 -- Find all suitable source pragmas at the top of subprogram body
2958 -- From's declarations and move them after arbitrary node To.
2959 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2960 -- as it applies to the body too.
2962 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2963 -- Undo the transformation done by Exchange_Limited_Views.
2965 procedure Set_Trivial_Subprogram (N : Node_Id);
2966 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2967 -- subprogram whose body is being analyzed. N is the statement node
2968 -- causing the flag to be set, if the following statement is a return
2969 -- of an entity, we mark the entity as set in source to suppress any
2970 -- warning on the stylized use of function stubs with a dummy return.
2972 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2973 -- Undo the transformation done by Mask_Unfrozen_Types
2975 procedure Verify_Overriding_Indicator;
2976 -- If there was a previous spec, the entity has been entered in the
2977 -- current scope previously. If the body itself carries an overriding
2978 -- indicator, check that it is consistent with the known status of the
2981 -----------------------
2982 -- Body_Has_Contract --
2983 -----------------------
2985 function Body_Has_Contract return Boolean is
2986 Decls : constant List_Id := Declarations (N);
2990 -- Check for aspects that may generate a contract
2992 if Present (Aspect_Specifications (N)) then
2993 Item := First (Aspect_Specifications (N));
2994 while Present (Item) loop
2995 if Is_Subprogram_Contract_Annotation (Item) then
3003 -- Check for pragmas that may generate a contract
3005 if Present (Decls) then
3006 Item := First (Decls);
3007 while Present (Item) loop
3008 if Nkind (Item) = N_Pragma
3009 and then Is_Subprogram_Contract_Annotation (Item)
3019 end Body_Has_Contract;
3021 ----------------------------
3022 -- Body_Has_SPARK_Mode_On --
3023 ----------------------------
3025 function Body_Has_SPARK_Mode_On return Boolean is
3026 Decls : constant List_Id := Declarations (N);
3030 -- Check for SPARK_Mode aspect
3032 if Present (Aspect_Specifications (N)) then
3033 Item := First (Aspect_Specifications (N));
3034 while Present (Item) loop
3035 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
3036 return Get_SPARK_Mode_From_Annotation (Item) = On;
3043 -- Check for SPARK_Mode pragma
3045 if Present (Decls) then
3046 Item := First (Decls);
3047 while Present (Item) loop
3049 -- Pragmas that apply to a subprogram body are usually grouped
3050 -- together. Look for a potential pragma SPARK_Mode among them.
3052 if Nkind (Item) = N_Pragma then
3053 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
3054 return Get_SPARK_Mode_From_Annotation (Item) = On;
3057 -- Otherwise the first non-pragma declarative item terminates
3058 -- the region where pragma SPARK_Mode may appear.
3068 -- Otherwise, the applicable SPARK_Mode is inherited from the
3069 -- enclosing subprogram or package.
3071 return SPARK_Mode = On;
3072 end Body_Has_SPARK_Mode_On;
3074 ------------------------------------------
3075 -- Build_Internal_Protected_Declaration --
3076 ------------------------------------------
3078 function Build_Internal_Protected_Declaration
3079 (N : Node_Id) return Entity_Id
3081 procedure Analyze_Pragmas (From : Node_Id);
3082 -- Analyze all pragmas which follow arbitrary node From
3084 ---------------------
3085 -- Analyze_Pragmas --
3086 ---------------------
3088 procedure Analyze_Pragmas (From : Node_Id) is
3092 Decl := Next (From);
3093 while Present (Decl) loop
3094 if Nkind (Decl) = N_Pragma then
3095 Analyze_Pragma (Decl);
3097 -- No candidate pragmas are available for analysis
3105 end Analyze_Pragmas;
3109 Body_Id : constant Entity_Id := Defining_Entity (N);
3110 Loc : constant Source_Ptr := Sloc (N);
3115 Spec_Id : Entity_Id;
3117 -- Start of processing for Build_Internal_Protected_Declaration
3120 Formal := First_Formal (Body_Id);
3122 -- The protected operation always has at least one formal, namely the
3123 -- object itself, but it is only placed in the parameter list if
3124 -- expansion is enabled.
3126 if Present (Formal) or else Expander_Active then
3127 Formals := Copy_Parameter_List (Body_Id);
3133 Make_Defining_Identifier (Sloc (Body_Id),
3134 Chars => Chars (Body_Id));
3136 -- Indicate that the entity comes from source, to ensure that cross-
3137 -- reference information is properly generated. The body itself is
3138 -- rewritten during expansion, and the body entity will not appear in
3139 -- calls to the operation.
3141 Set_Comes_From_Source (Spec_Id, True);
3143 if Nkind (Specification (N)) = N_Procedure_Specification then
3145 Make_Procedure_Specification (Loc,
3146 Defining_Unit_Name => Spec_Id,
3147 Parameter_Specifications => Formals);
3150 Make_Function_Specification (Loc,
3151 Defining_Unit_Name => Spec_Id,
3152 Parameter_Specifications => Formals,
3153 Result_Definition =>
3154 New_Occurrence_Of (Etype (Body_Id), Loc));
3157 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
3158 Set_Corresponding_Body (Decl, Body_Id);
3159 Set_Corresponding_Spec (N, Spec_Id);
3161 Insert_Before (N, Decl);
3163 -- Associate all aspects and pragmas of the body with the spec. This
3164 -- ensures that these annotations apply to the initial declaration of
3165 -- the subprogram body.
3167 Move_Aspects (From => N, To => Decl);
3168 Move_Pragmas (From => N, To => Decl);
3172 -- The analysis of the spec may generate pragmas which require manual
3173 -- analysis. Since the generation of the spec and the relocation of
3174 -- the annotations is driven by the expansion of the stand-alone
3175 -- body, the pragmas will not be analyzed in a timely manner. Do this
3178 Analyze_Pragmas (Decl);
3180 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
3181 -- ensuring in particular that 'Access is illegal.
3183 Set_Convention (Spec_Id, Convention_Intrinsic);
3184 Set_Has_Completion (Spec_Id);
3187 end Build_Internal_Protected_Declaration;
3189 ----------------------------------
3190 -- Build_Subprogram_Declaration --
3191 ----------------------------------
3193 procedure Build_Subprogram_Declaration is
3195 Subp_Decl : Node_Id;
3198 -- Create a matching subprogram spec using the profile of the body.
3199 -- The structure of the tree is identical, but has new entities for
3200 -- the defining unit name and formal parameters.
3203 Make_Subprogram_Declaration (Loc,
3204 Specification => Copy_Subprogram_Spec (Body_Spec));
3205 Set_Comes_From_Source (Subp_Decl, True);
3207 -- Also mark parameters as coming from source
3209 if Present (Parameter_Specifications (Specification (Subp_Decl))) then
3214 First (Parameter_Specifications (Specification (Subp_Decl)));
3216 while Present (Form) loop
3217 Set_Comes_From_Source (Defining_Identifier (Form), True);
3223 -- Relocate the aspects and relevant pragmas from the subprogram body
3224 -- to the generated spec because it acts as the initial declaration.
3226 Insert_Before (N, Subp_Decl);
3227 Move_Aspects (N, To => Subp_Decl);
3228 Move_Pragmas (N, To => Subp_Decl);
3230 -- Ensure that the generated corresponding spec and original body
3231 -- share the same SPARK_Mode pragma or aspect. As a result, both have
3232 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
3233 -- correctly set for local subprograms.
3235 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
3237 Analyze (Subp_Decl);
3239 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
3240 -- the body since the expander may generate calls using that entity.
3241 -- Required to ensure that Expand_Call rewrites calls to this
3242 -- function by calls to the built procedure.
3244 if Transform_Function_Array
3245 and then Nkind (Body_Spec) = N_Function_Specification
3247 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
3249 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
3250 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
3251 Corresponding_Procedure
3252 (Defining_Entity (Specification (Subp_Decl))));
3255 -- Analyze any relocated source pragmas or pragmas created for aspect
3258 Decl := Next (Subp_Decl);
3259 while Present (Decl) loop
3261 -- Stop the search for pragmas once the body has been reached as
3262 -- this terminates the region where pragmas may appear.
3267 elsif Nkind (Decl) = N_Pragma then
3274 Spec_Id := Defining_Entity (Subp_Decl);
3275 Set_Corresponding_Spec (N, Spec_Id);
3277 -- Mark the generated spec as a source construct to ensure that all
3278 -- calls to it are properly registered in ALI files for GNATprove.
3280 Set_Comes_From_Source (Spec_Id, True);
3282 -- Ensure that the specs of the subprogram declaration and its body
3283 -- are identical, otherwise they will appear non-conformant due to
3284 -- rewritings in the default values of formal parameters.
3286 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
3287 Set_Specification (N, Body_Spec);
3288 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3289 end Build_Subprogram_Declaration;
3291 ----------------------------
3292 -- Check_Anonymous_Return --
3293 ----------------------------
3295 procedure Check_Anonymous_Return is
3301 if Present (Spec_Id) then
3307 if Ekind (Scop) = E_Function
3308 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
3309 and then not Is_Thunk (Scop)
3311 -- Skip internally built functions which handle the case of
3312 -- a null access (see Expand_Interface_Conversion)
3314 and then not (Is_Interface (Designated_Type (Etype (Scop)))
3315 and then not Comes_From_Source (Parent (Scop)))
3317 and then (Has_Task (Designated_Type (Etype (Scop)))
3319 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
3321 Is_Limited_Record (Designated_Type (Etype (Scop)))))
3322 and then Expander_Active
3324 Decl := Build_Master_Declaration (Loc);
3326 if Present (Declarations (N)) then
3327 Prepend (Decl, Declarations (N));
3329 Set_Declarations (N, New_List (Decl));
3332 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
3333 Set_Has_Master_Entity (Scop);
3335 -- Now mark the containing scope as a task master
3338 while Nkind (Par) /= N_Compilation_Unit loop
3339 Par := Parent (Par);
3340 pragma Assert (Present (Par));
3342 -- If we fall off the top, we are at the outer level, and
3343 -- the environment task is our effective master, so nothing
3347 in N_Task_Body | N_Block_Statement | N_Subprogram_Body
3349 Set_Is_Task_Master (Par, True);
3354 end Check_Anonymous_Return;
3356 -------------------------
3357 -- Check_Inline_Pragma --
3358 -------------------------
3360 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
3364 function Is_Inline_Pragma (N : Node_Id) return Boolean;
3365 -- True when N is a pragma Inline or Inline_Always that applies
3366 -- to this subprogram.
3368 -----------------------
3369 -- Is_Inline_Pragma --
3370 -----------------------
3372 function Is_Inline_Pragma (N : Node_Id) return Boolean is
3374 if Nkind (N) = N_Pragma
3376 (Pragma_Name_Unmapped (N) = Name_Inline_Always
3377 or else (Pragma_Name_Unmapped (N) = Name_Inline
3379 (Front_End_Inlining or else Optimization_Level > 0)))
3380 and then Present (Pragma_Argument_Associations (N))
3383 Pragma_Arg : Node_Id :=
3384 Expression (First (Pragma_Argument_Associations (N)));
3386 if Nkind (Pragma_Arg) = N_Selected_Component then
3387 Pragma_Arg := Selector_Name (Pragma_Arg);
3390 return Chars (Pragma_Arg) = Chars (Body_Id);
3396 end Is_Inline_Pragma;
3398 -- Start of processing for Check_Inline_Pragma
3401 if not Expander_Active then
3405 if Is_List_Member (N)
3406 and then Present (Next (N))
3407 and then Is_Inline_Pragma (Next (N))
3411 elsif Nkind (N) /= N_Subprogram_Body_Stub
3412 and then Present (Declarations (N))
3413 and then Is_Inline_Pragma (First (Declarations (N)))
3415 Prag := First (Declarations (N));
3421 if Present (Prag) then
3422 if Present (Spec_Id) then
3423 if Is_List_Member (N)
3424 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
3425 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
3431 -- Create a subprogram declaration, to make treatment uniform.
3432 -- Make the sloc of the subprogram name that of the entity in
3433 -- the body, so that style checks find identical strings.
3436 Subp : constant Entity_Id :=
3437 Make_Defining_Identifier
3438 (Sloc (Body_Id), Chars (Body_Id));
3439 Decl : constant Node_Id :=
3440 Make_Subprogram_Declaration (Loc,
3442 New_Copy_Tree (Specification (N)));
3445 -- Link the body and the generated spec
3447 Set_Corresponding_Body (Decl, Body_Id);
3448 Set_Corresponding_Spec (N, Subp);
3450 Set_Defining_Unit_Name (Specification (Decl), Subp);
3452 -- To ensure proper coverage when body is inlined, indicate
3453 -- whether the subprogram comes from source.
3455 Preserve_Comes_From_Source (Subp, N);
3457 if Present (First_Formal (Body_Id)) then
3458 Plist := Copy_Parameter_List (Body_Id);
3459 Set_Parameter_Specifications
3460 (Specification (Decl), Plist);
3463 -- Move aspects to the new spec
3465 if Has_Aspects (N) then
3466 Move_Aspects (N, To => Decl);
3469 Insert_Before (N, Decl);
3472 Set_Has_Pragma_Inline (Subp);
3474 if Pragma_Name (Prag) = Name_Inline_Always then
3475 Set_Is_Inlined (Subp);
3476 Set_Has_Pragma_Inline_Always (Subp);
3479 -- Prior to copying the subprogram body to create a template
3480 -- for it for subsequent inlining, remove the pragma from
3481 -- the current body so that the copy that will produce the
3482 -- new body will start from a completely unanalyzed tree.
3484 if Nkind (Parent (Prag)) = N_Subprogram_Body then
3485 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
3492 end Check_Inline_Pragma;
3494 --------------------------
3495 -- Check_Missing_Return --
3496 --------------------------
3498 procedure Check_Missing_Return is
3500 Missing_Ret : Boolean;
3503 if Nkind (Body_Spec) = N_Function_Specification then
3504 if Present (Spec_Id) then
3510 if Return_Present (Id) then
3511 Check_Returns (HSS, 'F', Missing_Ret);
3514 Set_Has_Missing_Return (Id);
3517 -- Within a premature instantiation of a package with no body, we
3518 -- build completions of the functions therein, with a Raise
3519 -- statement. No point in complaining about a missing return in
3522 elsif Ekind (Id) = E_Function
3523 and then In_Instance
3524 and then Present (Statements (HSS))
3525 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
3529 elsif Is_Generic_Subprogram (Id)
3530 or else not Is_Machine_Code_Subprogram (Id)
3532 Error_Msg_N ("missing RETURN statement in function body", N);
3535 -- If procedure with No_Return, check returns
3537 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3538 if Present (Spec_Id) then
3544 if No_Return (Id) then
3545 Check_Returns (HSS, 'P', Missing_Ret, Id);
3548 end Check_Missing_Return;
3550 -----------------------
3551 -- Disambiguate_Spec --
3552 -----------------------
3554 function Disambiguate_Spec return Entity_Id is
3555 Priv_Spec : Entity_Id;
3558 procedure Replace_Types (To_Corresponding : Boolean);
3559 -- Depending on the flag, replace the type of formal parameters of
3560 -- Body_Id if it is a concurrent type implementing interfaces with
3561 -- the corresponding record type or the other way around.
3563 procedure Replace_Types (To_Corresponding : Boolean) is
3565 Formal_Typ : Entity_Id;
3568 Formal := First_Formal (Body_Id);
3569 while Present (Formal) loop
3570 Formal_Typ := Etype (Formal);
3572 if Is_Class_Wide_Type (Formal_Typ) then
3573 Formal_Typ := Root_Type (Formal_Typ);
3576 -- From concurrent type to corresponding record
3578 if To_Corresponding then
3579 if Is_Concurrent_Type (Formal_Typ)
3580 and then Present (Corresponding_Record_Type (Formal_Typ))
3583 (Corresponding_Record_Type (Formal_Typ)))
3586 Corresponding_Record_Type (Formal_Typ));
3589 -- From corresponding record to concurrent type
3592 if Is_Concurrent_Record_Type (Formal_Typ)
3593 and then Present (Interfaces (Formal_Typ))
3596 Corresponding_Concurrent_Type (Formal_Typ));
3600 Next_Formal (Formal);
3604 -- Start of processing for Disambiguate_Spec
3607 -- Try to retrieve the specification of the body as is. All error
3608 -- messages are suppressed because the body may not have a spec in
3609 -- its current state.
3611 Spec_N := Find_Corresponding_Spec (N, False);
3613 -- It is possible that this is the body of a primitive declared
3614 -- between a private and a full view of a concurrent type. The
3615 -- controlling parameter of the spec carries the concurrent type,
3616 -- not the corresponding record type as transformed by Analyze_
3617 -- Subprogram_Specification. In such cases, we undo the change
3618 -- made by the analysis of the specification and try to find the
3621 -- Note that wrappers already have their corresponding specs and
3622 -- bodies set during their creation, so if the candidate spec is
3623 -- a wrapper, then we definitely need to swap all types to their
3624 -- original concurrent status.
3627 or else Is_Primitive_Wrapper (Spec_N)
3629 -- Restore all references of corresponding record types to the
3630 -- original concurrent types.
3632 Replace_Types (To_Corresponding => False);
3633 Priv_Spec := Find_Corresponding_Spec (N, False);
3635 -- The current body truly belongs to a primitive declared between
3636 -- a private and a full view. We leave the modified body as is,
3637 -- and return the true spec.
3639 if Present (Priv_Spec)
3640 and then Is_Private_Primitive (Priv_Spec)
3645 -- In case that this is some sort of error, restore the original
3646 -- state of the body.
3648 Replace_Types (To_Corresponding => True);
3652 end Disambiguate_Spec;
3654 ----------------------------
3655 -- Exchange_Limited_Views --
3656 ----------------------------
3658 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3659 Result : Elist_Id := No_Elist;
3661 procedure Detect_And_Exchange (Id : Entity_Id);
3662 -- Determine whether Id's type denotes an incomplete type associated
3663 -- with a limited with clause and exchange the limited view with the
3664 -- non-limited one when available. Note that the non-limited view
3665 -- may exist because of a with_clause in another unit in the context,
3666 -- but cannot be used because the current view of the enclosing unit
3667 -- is still a limited view.
3669 -------------------------
3670 -- Detect_And_Exchange --
3671 -------------------------
3673 procedure Detect_And_Exchange (Id : Entity_Id) is
3674 Typ : constant Entity_Id := Etype (Id);
3676 if From_Limited_With (Typ)
3677 and then Has_Non_Limited_View (Typ)
3678 and then not From_Limited_With (Scope (Typ))
3681 Result := New_Elmt_List;
3684 Prepend_Elmt (Typ, Result);
3685 Prepend_Elmt (Id, Result);
3686 Set_Etype (Id, Non_Limited_View (Typ));
3688 end Detect_And_Exchange;
3694 -- Start of processing for Exchange_Limited_Views
3697 -- Do not process subprogram bodies as they already use the non-
3698 -- limited view of types.
3700 if Ekind (Subp_Id) not in E_Function | E_Procedure then
3704 -- Examine all formals and swap views when applicable
3706 Formal := First_Formal (Subp_Id);
3707 while Present (Formal) loop
3708 Detect_And_Exchange (Formal);
3710 Next_Formal (Formal);
3713 -- Process the return type of a function
3715 if Ekind (Subp_Id) = E_Function then
3716 Detect_And_Exchange (Subp_Id);
3720 end Exchange_Limited_Views;
3722 ------------------------------------
3723 -- Generate_Minimum_Accessibility --
3724 ------------------------------------
3726 procedure Generate_Minimum_Accessibility
3727 (Extra_Access : Entity_Id;
3728 Related_Form : Entity_Id := Empty)
3730 Loc : constant Source_Ptr := Sloc (Body_Nod);
3734 -- When no related formal exists then we are dealing with an
3735 -- extra accessibility formal for a function result.
3737 if No (Related_Form) then
3738 Form := Extra_Access;
3740 Form := Related_Form;
3743 -- Create the minimum accessibility object
3746 Make_Object_Declaration (Loc,
3747 Defining_Identifier =>
3749 (Loc, 'A', Extra_Access),
3750 Object_Definition => New_Occurrence_Of
3751 (Standard_Natural, Loc),
3753 Make_Attribute_Reference (Loc,
3754 Prefix => New_Occurrence_Of
3755 (Standard_Natural, Loc),
3756 Attribute_Name => Name_Min,
3757 Expressions => New_List (
3758 Make_Integer_Literal (Loc,
3759 Scope_Depth (Body_Id)),
3761 (Extra_Access, Loc))));
3763 -- Add the new local object to the Minimum_Acc_Obj to
3764 -- be later prepended to the subprogram's list of
3765 -- declarations after we are sure all expansion is
3768 if Present (Minimum_Acc_Objs) then
3769 Prepend (Obj_Node, Minimum_Acc_Objs);
3771 Minimum_Acc_Objs := New_List (Obj_Node);
3774 -- Register the object and analyze it
3776 Set_Minimum_Accessibility
3777 (Form, Defining_Identifier (Obj_Node));
3780 end Generate_Minimum_Accessibility;
3782 -------------------------------------
3783 -- Is_Private_Concurrent_Primitive --
3784 -------------------------------------
3786 function Is_Private_Concurrent_Primitive
3787 (Subp_Id : Entity_Id) return Boolean
3789 Formal_Typ : Entity_Id;
3792 if Present (First_Formal (Subp_Id)) then
3793 Formal_Typ := Etype (First_Formal (Subp_Id));
3795 if Is_Concurrent_Record_Type (Formal_Typ) then
3796 if Is_Class_Wide_Type (Formal_Typ) then
3797 Formal_Typ := Root_Type (Formal_Typ);
3800 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3803 -- The type of the first formal is a concurrent tagged type with
3807 Is_Concurrent_Type (Formal_Typ)
3808 and then Is_Tagged_Type (Formal_Typ)
3809 and then Has_Private_Declaration (Formal_Typ);
3813 end Is_Private_Concurrent_Primitive;
3815 -------------------------
3816 -- Mask_Unfrozen_Types --
3817 -------------------------
3819 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3820 Result : Elist_Id := No_Elist;
3822 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3823 -- Mask all types referenced in the subtree rooted at Node
3825 --------------------
3826 -- Mask_Type_Refs --
3827 --------------------
3829 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3830 procedure Mask_Type (Typ : Entity_Id);
3831 -- ??? what does this do?
3837 procedure Mask_Type (Typ : Entity_Id) is
3839 -- Skip Itypes created by the preanalysis
3842 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3847 if not Is_Frozen (Typ) then
3848 if Scope (Typ) /= Current_Scope then
3849 Set_Is_Frozen (Typ);
3850 Append_New_Elmt (Typ, Result);
3852 Freeze_Before (N, Typ);
3857 -- Start of processing for Mask_Type_Refs
3860 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3861 Mask_Type (Etype (Entity (Node)));
3863 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
3864 Mask_Type (Scope (Entity (Node)));
3867 elsif Nkind (Node) in N_Aggregate | N_Null | N_Type_Conversion
3868 and then Present (Etype (Node))
3870 Mask_Type (Etype (Node));
3876 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3880 Return_Stmt : constant Node_Id :=
3881 First (Statements (Handled_Statement_Sequence (N)));
3883 -- Start of processing for Mask_Unfrozen_Types
3886 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3888 Mask_References (Expression (Return_Stmt));
3891 end Mask_Unfrozen_Types;
3897 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
3899 Insert_Nod : Node_Id;
3900 Next_Decl : Node_Id;
3903 pragma Assert (Nkind (From) = N_Subprogram_Body);
3905 -- The pragmas are moved in an order-preserving fashion
3909 -- Inspect the declarations of the subprogram body and relocate all
3910 -- candidate pragmas.
3912 Decl := First (Declarations (From));
3913 while Present (Decl) loop
3915 -- Preserve the following declaration for iteration purposes, due
3916 -- to possible relocation of a pragma.
3918 Next_Decl := Next (Decl);
3920 if Nkind (Decl) = N_Pragma then
3921 -- Copy pragma SPARK_Mode if present in the declarative list
3922 -- of subprogram body From and insert it after node To. This
3923 -- pragma should not be moved, as it applies to the body too.
3925 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
3926 Insert_After (Insert_Nod, New_Copy_Tree (Decl));
3928 -- Move relevant pragmas to the spec
3930 elsif Pragma_Name_Unmapped (Decl) in Name_Depends
3936 | Name_Refined_Depends
3937 | Name_Refined_Global
3940 | Name_Pure_Function
3941 | Name_Volatile_Function
3944 Insert_After (Insert_Nod, Decl);
3948 -- Skip internally generated code
3950 elsif not Comes_From_Source (Decl) then
3953 -- No candidate pragmas are available for relocation
3963 ---------------------------
3964 -- Restore_Limited_Views --
3965 ---------------------------
3967 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3968 Elmt : Elmt_Id := First_Elmt (Restore_List);
3972 while Present (Elmt) loop
3975 Set_Etype (Id, Node (Elmt));
3978 end Restore_Limited_Views;
3980 ----------------------------
3981 -- Set_Trivial_Subprogram --
3982 ----------------------------
3984 procedure Set_Trivial_Subprogram (N : Node_Id) is
3985 Nxt : constant Node_Id := Next (N);
3988 Set_Is_Trivial_Subprogram (Body_Id);
3990 if Present (Spec_Id) then
3991 Set_Is_Trivial_Subprogram (Spec_Id);
3995 and then Nkind (Nxt) = N_Simple_Return_Statement
3996 and then No (Next (Nxt))
3997 and then Present (Expression (Nxt))
3998 and then Is_Entity_Name (Expression (Nxt))
4000 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
4002 end Set_Trivial_Subprogram;
4004 ---------------------------
4005 -- Unmask_Unfrozen_Types --
4006 ---------------------------
4008 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
4009 Elmt : Elmt_Id := First_Elmt (Unmask_List);
4012 while Present (Elmt) loop
4013 Set_Is_Frozen (Node (Elmt), False);
4016 end Unmask_Unfrozen_Types;
4018 ---------------------------------
4019 -- Verify_Overriding_Indicator --
4020 ---------------------------------
4022 procedure Verify_Overriding_Indicator is
4024 if Must_Override (Body_Spec) then
4025 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
4026 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
4030 -- Overridden controlled primitives may have had their
4031 -- Overridden_Operation field cleared according to the setting of
4032 -- the Is_Hidden flag. An issue arises, however, when analyzing
4033 -- an instance that may have manipulated the flag during
4034 -- expansion. As a result, we add an exception for this case.
4036 elsif not Present (Overridden_Operation (Spec_Id))
4037 and then not (Chars (Spec_Id) in Name_Adjust
4040 and then In_Instance)
4043 ("subprogram& is not overriding", Body_Spec, Spec_Id);
4045 -- Overriding indicators aren't allowed for protected subprogram
4046 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
4047 -- this to a warning if -gnatd.E is enabled.
4049 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
4050 Error_Msg_Warn := Error_To_Warning;
4052 ("<<overriding indicator not allowed for protected "
4053 & "subprogram body", Body_Spec);
4056 elsif Must_Not_Override (Body_Spec) then
4057 if Present (Overridden_Operation (Spec_Id)) then
4059 ("subprogram& overrides inherited operation",
4060 Body_Spec, Spec_Id);
4062 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
4063 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
4066 ("subprogram& overrides predefined operator ",
4067 Body_Spec, Spec_Id);
4069 -- Overriding indicators aren't allowed for protected subprogram
4070 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
4071 -- this to a warning if -gnatd.E is enabled.
4073 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
4074 Error_Msg_Warn := Error_To_Warning;
4077 ("<<overriding indicator not allowed "
4078 & "for protected subprogram body", Body_Spec);
4080 -- If this is not a primitive operation, then the overriding
4081 -- indicator is altogether illegal.
4083 elsif not Is_Primitive (Spec_Id) then
4085 ("overriding indicator only allowed "
4086 & "if subprogram is primitive", Body_Spec);
4089 -- If checking the style rule and the operation overrides, then
4090 -- issue a warning about a missing overriding_indicator. Protected
4091 -- subprogram bodies are excluded from this style checking, since
4092 -- they aren't primitives (even though their declarations can
4093 -- override) and aren't allowed to have an overriding_indicator.
4096 and then Present (Overridden_Operation (Spec_Id))
4097 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
4099 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
4100 Style.Missing_Overriding (N, Body_Id);
4103 and then Can_Override_Operator (Spec_Id)
4104 and then not In_Predefined_Unit (Spec_Id)
4106 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
4107 Style.Missing_Overriding (N, Body_Id);
4109 end Verify_Overriding_Indicator;
4113 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4114 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4115 Saved_EA : constant Boolean := Expander_Active;
4116 Saved_ISMP : constant Boolean :=
4117 Ignore_SPARK_Mode_Pragmas_In_Instance;
4118 -- Save the Ghost and SPARK mode-related data to restore on exit
4120 -- Start of processing for Analyze_Subprogram_Body_Helper
4123 -- A [generic] subprogram body freezes the contract of the nearest
4124 -- enclosing package body and all other contracts encountered in the
4125 -- same declarative part up to and excluding the subprogram body:
4127 -- package body Nearest_Enclosing_Package
4128 -- with Refined_State => (State => Constit)
4132 -- procedure Freezes_Enclosing_Package_Body
4133 -- with Refined_Depends => (Input => Constit) ...
4135 -- This ensures that any annotations referenced by the contract of the
4136 -- [generic] subprogram body are available. This form of freezing is
4137 -- decoupled from the usual Freeze_xxx mechanism because it must also
4138 -- work in the context of generics where normal freezing is disabled.
4140 -- Only bodies coming from source should cause this type of freezing.
4141 -- Expression functions that act as bodies and complete an initial
4142 -- declaration must be included in this category, hence the use of
4145 if Comes_From_Source (Original_Node (N)) then
4146 Freeze_Previous_Contracts (N);
4149 -- Generic subprograms are handled separately. They always have a
4150 -- generic specification. Determine whether current scope has a
4151 -- previous declaration.
4153 -- If the subprogram body is defined within an instance of the same
4154 -- name, the instance appears as a package renaming, and will be hidden
4155 -- within the subprogram.
4157 if Present (Prev_Id)
4158 and then not Is_Overloadable (Prev_Id)
4159 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
4160 or else Comes_From_Source (Prev_Id))
4162 if Is_Generic_Subprogram (Prev_Id) then
4165 -- A subprogram body is Ghost when it is stand-alone and subject
4166 -- to pragma Ghost or when the corresponding spec is Ghost. Set
4167 -- the mode now to ensure that any nodes generated during analysis
4168 -- and expansion are properly marked as Ghost.
4170 Mark_And_Set_Ghost_Body (N, Spec_Id);
4172 -- If the body completes the initial declaration of a compilation
4173 -- unit which is subject to pragma Elaboration_Checks, set the
4174 -- model specified by the pragma because it applies to all parts
4177 Install_Elaboration_Model (Spec_Id);
4179 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4180 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4182 Analyze_Generic_Subprogram_Body (N, Spec_Id);
4184 if Nkind (N) = N_Subprogram_Body then
4185 HSS := Handled_Statement_Sequence (N);
4186 Check_Missing_Return;
4191 -- Otherwise a previous entity conflicts with the subprogram name.
4192 -- Attempting to enter name will post error.
4195 Enter_Name (Body_Id);
4199 -- Non-generic case, find the subprogram declaration, if one was seen,
4200 -- or enter new overloaded entity in the current scope. If the
4201 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
4202 -- part of the context of one of its subunits. No need to redo the
4205 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
4209 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
4211 if Nkind (N) = N_Subprogram_Body_Stub
4212 or else No (Corresponding_Spec (N))
4214 if Is_Private_Concurrent_Primitive (Body_Id) then
4215 Spec_Id := Disambiguate_Spec;
4217 -- A subprogram body is Ghost when it is stand-alone and
4218 -- subject to pragma Ghost or when the corresponding spec is
4219 -- Ghost. Set the mode now to ensure that any nodes generated
4220 -- during analysis and expansion are properly marked as Ghost.
4222 Mark_And_Set_Ghost_Body (N, Spec_Id);
4224 -- If the body completes a compilation unit which is subject
4225 -- to pragma Elaboration_Checks, set the model specified by
4226 -- the pragma because it applies to all parts of the unit.
4228 Install_Elaboration_Model (Spec_Id);
4231 Spec_Id := Find_Corresponding_Spec (N);
4233 -- A subprogram body is Ghost when it is stand-alone and
4234 -- subject to pragma Ghost or when the corresponding spec is
4235 -- Ghost. Set the mode now to ensure that any nodes generated
4236 -- during analysis and expansion are properly marked as Ghost.
4238 Mark_And_Set_Ghost_Body (N, Spec_Id);
4240 -- If the body completes a compilation unit which is subject
4241 -- to pragma Elaboration_Checks, set the model specified by
4242 -- the pragma because it applies to all parts of the unit.
4244 Install_Elaboration_Model (Spec_Id);
4246 -- In GNATprove mode, if the body has no previous spec, create
4247 -- one so that the inlining machinery can operate properly.
4248 -- Transfer aspects, if any, to the new spec, so that they
4249 -- are legal and can be processed ahead of the body.
4250 -- We make two copies of the given spec, one for the new
4251 -- declaration, and one for the body.
4252 -- ??? This should be conditioned on front-end inlining rather
4253 -- than GNATprove_Mode.
4255 if No (Spec_Id) and then GNATprove_Mode
4257 -- Inlining does not apply during preanalysis of code
4259 and then Full_Analysis
4261 -- Inlining only applies to full bodies, not stubs
4263 and then Nkind (N) /= N_Subprogram_Body_Stub
4265 -- Inlining only applies to bodies in the source code, not to
4266 -- those generated by the compiler. In particular, expression
4267 -- functions, whose body is generated by the compiler, are
4268 -- treated specially by GNATprove.
4270 and then Comes_From_Source (Body_Id)
4272 -- This cannot be done for a compilation unit, which is not
4273 -- in a context where we can insert a new spec.
4275 and then Is_List_Member (N)
4277 -- Inlining only applies to subprograms without contracts,
4278 -- as a contract is a sign that GNATprove should perform a
4279 -- modular analysis of the subprogram instead of a contextual
4280 -- analysis at each call site. The same test is performed in
4281 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
4282 -- here in another form (because the contract has not been
4283 -- attached to the body) to avoid front-end errors in case
4284 -- pragmas are used instead of aspects, because the
4285 -- corresponding pragmas in the body would not be transferred
4286 -- to the spec, leading to legality errors.
4288 and then not Body_Has_Contract
4289 and then not Inside_A_Generic
4291 Build_Subprogram_Declaration;
4293 -- If this is a function that returns a constrained array, and
4294 -- Transform_Function_Array is set, create subprogram
4295 -- declaration to simplify e.g. subsequent C generation.
4298 and then Transform_Function_Array
4299 and then Nkind (Body_Spec) = N_Function_Specification
4300 and then Is_Array_Type (Etype (Body_Id))
4301 and then Is_Constrained (Etype (Body_Id))
4303 Build_Subprogram_Declaration;
4307 -- If this is a duplicate body, no point in analyzing it
4309 if Error_Posted (N) then
4313 -- A subprogram body should cause freezing of its own declaration,
4314 -- but if there was no previous explicit declaration, then the
4315 -- subprogram will get frozen too late (there may be code within
4316 -- the body that depends on the subprogram having been frozen,
4317 -- such as uses of extra formals), so we force it to be frozen
4318 -- here. Same holds if the body and spec are compilation units.
4319 -- Finally, if the return type is an anonymous access to protected
4320 -- subprogram, it must be frozen before the body because its
4321 -- expansion has generated an equivalent type that is used when
4322 -- elaborating the body.
4324 -- An exception in the case of Ada 2012, AI05-177: The bodies
4325 -- created for expression functions do not freeze.
4328 and then Nkind (Original_Node (N)) /= N_Expression_Function
4330 Freeze_Before (N, Body_Id);
4332 elsif Nkind (Parent (N)) = N_Compilation_Unit then
4333 Freeze_Before (N, Spec_Id);
4335 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
4336 Freeze_Before (N, Etype (Body_Id));
4340 Spec_Id := Corresponding_Spec (N);
4342 -- A subprogram body is Ghost when it is stand-alone and subject
4343 -- to pragma Ghost or when the corresponding spec is Ghost. Set
4344 -- the mode now to ensure that any nodes generated during analysis
4345 -- and expansion are properly marked as Ghost.
4347 Mark_And_Set_Ghost_Body (N, Spec_Id);
4349 -- If the body completes the initial declaration of a compilation
4350 -- unit which is subject to pragma Elaboration_Checks, set the
4351 -- model specified by the pragma because it applies to all parts
4354 Install_Elaboration_Model (Spec_Id);
4358 -- Deactivate expansion inside the body of ignored Ghost entities,
4359 -- as this code will ultimately be ignored. This avoids requiring the
4360 -- presence of run-time units which are not needed. Only do this for
4361 -- user entities, as internally generated entitities might still need
4362 -- to be expanded (e.g. those generated for types).
4364 if Present (Ignored_Ghost_Region)
4365 and then Comes_From_Source (Body_Id)
4367 Expander_Active := False;
4370 -- Previously we scanned the body to look for nested subprograms, and
4371 -- rejected an inline directive if nested subprograms were present,
4372 -- because the back-end would generate conflicting symbols for the
4373 -- nested bodies. This is now unnecessary.
4375 -- Look ahead to recognize a pragma Inline that appears after the body
4377 Check_Inline_Pragma (Spec_Id);
4379 -- Deal with special case of a fully private operation in the body of
4380 -- the protected type. We must create a declaration for the subprogram,
4381 -- in order to attach the subprogram that will be used in internal
4382 -- calls. We exclude compiler generated bodies from the expander since
4383 -- the issue does not arise for those cases.
4386 and then Comes_From_Source (N)
4387 and then Is_Protected_Type (Current_Scope)
4389 Spec_Id := Build_Internal_Protected_Declaration (N);
4392 -- If Transform_Function_Array is set and this is a function returning a
4393 -- constrained array type for which we must create a procedure with an
4394 -- extra out parameter, build and analyze the body now. The procedure
4395 -- declaration has already been created. We reuse the source body of the
4396 -- function, because in an instance it may contain global references
4397 -- that cannot be reanalyzed. The source function itself is not used any
4398 -- further, so we mark it as having a completion. If the subprogram is a
4399 -- stub the transformation is done later, when the proper body is
4403 and then Transform_Function_Array
4404 and then Present (Spec_Id)
4405 and then Ekind (Spec_Id) = E_Function
4406 and then Nkind (N) /= N_Subprogram_Body_Stub
4407 and then Rewritten_For_C (Spec_Id)
4409 Set_Has_Completion (Spec_Id);
4411 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
4414 -- The entity for the created procedure must remain invisible, so it
4415 -- does not participate in resolution of subsequent references to the
4418 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
4422 -- If a separate spec is present, then deal with freezing issues
4424 if Present (Spec_Id) then
4425 Spec_Decl := Unit_Declaration_Node (Spec_Id);
4426 Verify_Overriding_Indicator;
4428 -- In general, the spec will be frozen when we start analyzing the
4429 -- body. However, for internally generated operations, such as
4430 -- wrapper functions for inherited operations with controlling
4431 -- results, the spec may not have been frozen by the time we expand
4432 -- the freeze actions that include the bodies. In particular, extra
4433 -- formals for accessibility or for return-in-place may need to be
4434 -- generated. Freeze nodes, if any, are inserted before the current
4435 -- body. These freeze actions are also needed in Compile_Only mode to
4436 -- enable the proper back-end type annotations.
4437 -- They are necessary in any case to ensure proper elaboration order
4440 if Nkind (N) = N_Subprogram_Body
4441 and then Was_Expression_Function (N)
4442 and then not Has_Completion (Spec_Id)
4443 and then Serious_Errors_Detected = 0
4444 and then (Expander_Active
4445 or else Operating_Mode = Check_Semantics
4446 or else Is_Ignored_Ghost_Entity (Spec_Id))
4448 -- The body generated for an expression function that is not a
4449 -- completion is a freeze point neither for the profile nor for
4450 -- anything else. That's why, in order to prevent any freezing
4451 -- during analysis, we need to mask types declared outside the
4452 -- expression (and in an outer scope) that are not yet frozen.
4453 -- This also needs to be done in the case of an ignored Ghost
4454 -- expression function, where the expander isn't active.
4456 Set_Is_Frozen (Spec_Id);
4457 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
4459 elsif not Is_Frozen (Spec_Id)
4460 and then Serious_Errors_Detected = 0
4462 Set_Has_Delayed_Freeze (Spec_Id);
4463 Freeze_Before (N, Spec_Id);
4467 -- If the subprogram has a class-wide clone, build its body as a copy
4468 -- of the original body, and rewrite body of original subprogram as a
4469 -- wrapper that calls the clone. If N is a stub, this construction will
4470 -- take place when the proper body is analyzed. No action needed if this
4471 -- subprogram has been eliminated.
4473 if Present (Spec_Id)
4474 and then Present (Class_Wide_Clone (Spec_Id))
4475 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
4476 and then Nkind (N) /= N_Subprogram_Body_Stub
4477 and then not (Expander_Active and then Is_Eliminated (Spec_Id))
4479 Build_Class_Wide_Clone_Body (Spec_Id, N);
4481 -- This is the new body for the existing primitive operation
4483 Rewrite (N, Build_Class_Wide_Clone_Call
4484 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
4485 Set_Has_Completion (Spec_Id, False);
4490 -- Place subprogram on scope stack, and make formals visible. If there
4491 -- is a spec, the visible entity remains that of the spec.
4493 if Present (Spec_Id) then
4494 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
4496 if Is_Child_Unit (Spec_Id) then
4497 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
4501 Style.Check_Identifier (Body_Id, Spec_Id);
4504 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4505 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4507 if Is_Abstract_Subprogram (Spec_Id) then
4508 Error_Msg_N ("an abstract subprogram cannot have a body", N);
4512 Set_Convention (Body_Id, Convention (Spec_Id));
4513 Set_Has_Completion (Spec_Id);
4515 if Is_Protected_Type (Scope (Spec_Id)) then
4516 Prot_Typ := Scope (Spec_Id);
4519 -- If this is a body generated for a renaming, do not check for
4520 -- full conformance. The check is redundant, because the spec of
4521 -- the body is a copy of the spec in the renaming declaration,
4522 -- and the test can lead to spurious errors on nested defaults.
4524 if Present (Spec_Decl)
4525 and then not Comes_From_Source (N)
4527 (Nkind (Original_Node (Spec_Decl)) =
4528 N_Subprogram_Renaming_Declaration
4529 or else (Present (Corresponding_Body (Spec_Decl))
4531 Nkind (Unit_Declaration_Node
4532 (Corresponding_Body (Spec_Decl))) =
4533 N_Subprogram_Renaming_Declaration))
4537 -- Conversely, the spec may have been generated for specless body
4538 -- with an inline pragma. The entity comes from source, which is
4539 -- both semantically correct and necessary for proper inlining.
4540 -- The subprogram declaration itself is not in the source.
4542 elsif Comes_From_Source (N)
4543 and then Present (Spec_Decl)
4544 and then not Comes_From_Source (Spec_Decl)
4545 and then Has_Pragma_Inline (Spec_Id)
4552 Fully_Conformant, True, Conformant, Body_Id);
4555 -- If the body is not fully conformant, we have to decide if we
4556 -- should analyze it or not. If it has a really messed up profile
4557 -- then we probably should not analyze it, since we will get too
4558 -- many bogus messages.
4560 -- Our decision is to go ahead in the non-fully conformant case
4561 -- only if it is at least mode conformant with the spec. Note
4562 -- that the call to Check_Fully_Conformant has issued the proper
4563 -- error messages to complain about the lack of conformance.
4566 and then not Mode_Conformant (Body_Id, Spec_Id)
4572 -- In the case we are dealing with an expression function we check
4573 -- the formals attached to the spec instead of the body - so we don't
4574 -- reference body formals.
4576 if Spec_Id /= Body_Id
4577 and then not Is_Expression_Function (Spec_Id)
4579 Reference_Body_Formals (Spec_Id, Body_Id);
4582 Set_Ekind (Body_Id, E_Subprogram_Body);
4584 if Nkind (N) = N_Subprogram_Body_Stub then
4585 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4590 Set_Corresponding_Spec (N, Spec_Id);
4592 -- Ada 2005 (AI-345): If the operation is a primitive operation
4593 -- of a concurrent type, the type of the first parameter has been
4594 -- replaced with the corresponding record, which is the proper
4595 -- run-time structure to use. However, within the body there may
4596 -- be uses of the formals that depend on primitive operations
4597 -- of the type (in particular calls in prefixed form) for which
4598 -- we need the original concurrent type. The operation may have
4599 -- several controlling formals, so the replacement must be done
4602 if Comes_From_Source (Spec_Id)
4603 and then Present (First_Entity (Spec_Id))
4604 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4605 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4606 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4607 and then Present (Corresponding_Concurrent_Type
4608 (Etype (First_Entity (Spec_Id))))
4611 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4615 Form := First_Formal (Spec_Id);
4616 while Present (Form) loop
4617 if Etype (Form) = Typ then
4618 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4626 -- Make the formals visible, and place subprogram on scope stack.
4627 -- This is also the point at which we set Last_Real_Spec_Entity
4628 -- to mark the entities which will not be moved to the body.
4630 Install_Formals (Spec_Id);
4631 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4633 -- Within an instance, add local renaming declarations so that
4634 -- gdb can retrieve the values of actuals more easily. This is
4635 -- only relevant if generating code.
4637 if Is_Generic_Instance (Spec_Id)
4638 and then Is_Wrapper_Package (Current_Scope)
4639 and then Expander_Active
4641 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4644 Push_Scope (Spec_Id);
4646 -- Make sure that the subprogram is immediately visible. For
4647 -- child units that have no separate spec this is indispensable.
4648 -- Otherwise it is safe albeit redundant.
4650 Set_Is_Immediately_Visible (Spec_Id);
4653 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4654 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4655 Set_Scope (Body_Id, Scope (Spec_Id));
4657 -- Case of subprogram body with no previous spec
4660 -- Check for style warning required
4664 -- Only apply check for source level subprograms for which checks
4665 -- have not been suppressed.
4667 and then Comes_From_Source (Body_Id)
4668 and then not Suppress_Style_Checks (Body_Id)
4670 -- No warnings within an instance
4672 and then not In_Instance
4674 -- No warnings for expression functions
4676 and then Nkind (Original_Node (N)) /= N_Expression_Function
4678 Style.Body_With_No_Spec (N);
4681 New_Overloaded_Entity (Body_Id);
4683 if Nkind (N) /= N_Subprogram_Body_Stub then
4684 Set_Acts_As_Spec (N);
4685 Generate_Definition (Body_Id);
4687 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4689 -- If the body is an entry wrapper created for an entry with
4690 -- preconditions, it must be compiled in the context of the
4691 -- enclosing synchronized object, because it may mention other
4692 -- operations of the type.
4694 if Is_Entry_Wrapper (Body_Id) then
4696 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4699 Install_Declarations (Prot);
4703 Install_Formals (Body_Id);
4705 Push_Scope (Body_Id);
4708 -- For stubs and bodies with no previous spec, generate references to
4711 Generate_Reference_To_Formals (Body_Id);
4714 -- Entry barrier functions are generated outside the protected type and
4715 -- should not carry the SPARK_Mode of the enclosing context.
4717 if Nkind (N) = N_Subprogram_Body
4718 and then Is_Entry_Barrier_Function (N)
4722 -- The body is generated as part of expression function expansion. When
4723 -- the expression function appears in the visible declarations of a
4724 -- package, the body is added to the private declarations. Since both
4725 -- declarative lists may be subject to a different SPARK_Mode, inherit
4726 -- the mode of the spec.
4728 -- package P with SPARK_Mode is
4729 -- function Expr_Func ... is (...); -- original
4730 -- [function Expr_Func ...;] -- generated spec
4733 -- pragma SPARK_Mode (Off);
4734 -- [function Expr_Func ... is return ...;] -- generated body
4735 -- end P; -- mode is ON
4737 elsif not Comes_From_Source (N)
4738 and then Present (Spec_Id)
4739 and then Is_Expression_Function (Spec_Id)
4741 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4742 Set_SPARK_Pragma_Inherited
4743 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4745 -- Set the SPARK_Mode from the current context (may be overwritten later
4746 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4747 -- initially on a stand-alone subprogram body, but is then relocated to
4748 -- a generated corresponding spec. In this scenario the mode is shared
4749 -- between the spec and body.
4751 elsif No (SPARK_Pragma (Body_Id)) then
4752 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4753 Set_SPARK_Pragma_Inherited (Body_Id);
4756 -- A subprogram body may be instantiated or inlined at a later pass.
4757 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4758 -- applied to the initial declaration of the body.
4760 if Present (Spec_Id) then
4761 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4762 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4766 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4767 -- case the body is instantiated or inlined later and out of context.
4768 -- The body uses this attribute to restore the value of the global
4771 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4772 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4774 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4775 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4779 -- Preserve relevant elaboration-related attributes of the context which
4780 -- are no longer available or very expensive to recompute once analysis,
4781 -- resolution, and expansion are over.
4783 if No (Spec_Id) then
4784 Mark_Elaboration_Attributes
4790 -- If this is the proper body of a stub, we must verify that the stub
4791 -- conforms to the body, and to the previous spec if one was present.
4792 -- We know already that the body conforms to that spec. This test is
4793 -- only required for subprograms that come from source.
4795 if Nkind (Parent (N)) = N_Subunit
4796 and then Comes_From_Source (N)
4797 and then not Error_Posted (Body_Id)
4798 and then Nkind (Corresponding_Stub (Parent (N))) =
4799 N_Subprogram_Body_Stub
4802 Old_Id : constant Entity_Id :=
4804 (Specification (Corresponding_Stub (Parent (N))));
4806 Conformant : Boolean := False;
4809 if No (Spec_Id) then
4810 Check_Fully_Conformant (Body_Id, Old_Id);
4814 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4816 if not Conformant then
4818 -- The stub was taken to be a new declaration. Indicate that
4821 Set_Has_Completion (Old_Id, False);
4827 Set_Has_Completion (Body_Id);
4828 Check_Eliminated (Body_Id);
4830 -- Analyze any aspect specifications that appear on the subprogram body
4831 -- stub. Stop the analysis now as the stub does not have a declarative
4832 -- or a statement part, and it cannot be inlined.
4834 if Nkind (N) = N_Subprogram_Body_Stub then
4835 if Has_Aspects (N) then
4836 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4845 and then Serious_Errors_Detected = 0
4846 and then Present (Spec_Id)
4847 and then Has_Pragma_Inline (Spec_Id)
4849 -- Legacy implementation (relying on front-end inlining)
4851 if not Back_End_Inlining then
4852 if Has_Pragma_Inline_Always (Spec_Id)
4853 or else (Front_End_Inlining
4854 and then not Opt.Disable_FE_Inline)
4856 Build_Body_To_Inline (N, Spec_Id);
4859 -- New implementation (relying on back-end inlining)
4862 if Has_Pragma_Inline_Always (Spec_Id)
4863 or else Optimization_Level > 0
4865 -- Handle function returning an unconstrained type
4867 if Comes_From_Source (Body_Id)
4868 and then Ekind (Spec_Id) = E_Function
4869 and then Returns_Unconstrained_Type (Spec_Id)
4871 -- If function builds in place, i.e. returns a limited type,
4872 -- inlining cannot be done.
4874 and then not Is_Limited_Type (Etype (Spec_Id))
4876 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4880 Subp_Body : constant Node_Id :=
4881 Unit_Declaration_Node (Body_Id);
4882 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4885 -- Do not pass inlining to the backend if the subprogram
4886 -- has declarations or statements which cannot be inlined
4887 -- by the backend. This check is done here to emit an
4888 -- error instead of the generic warning message reported
4889 -- by the GCC backend (ie. "function might not be
4892 if Present (Subp_Decl)
4893 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4897 elsif Has_Excluded_Statement
4900 (Handled_Statement_Sequence (Subp_Body)))
4904 -- If the backend inlining is available then at this
4905 -- stage we only have to mark the subprogram as inlined.
4906 -- The expander will take care of registering it in the
4907 -- table of subprograms inlined by the backend a part of
4908 -- processing calls to it (cf. Expand_Call)
4911 Set_Is_Inlined (Spec_Id);
4918 -- In GNATprove mode, inline only when there is a separate subprogram
4919 -- declaration for now, as inlining of subprogram bodies acting as
4920 -- declarations, or subprogram stubs, are not supported by front-end
4921 -- inlining. This inlining should occur after analysis of the body, so
4922 -- that it is known whether the value of SPARK_Mode, which can be
4923 -- defined by a pragma inside the body, is applicable to the body.
4924 -- Inlining can be disabled with switch -gnatdm
4926 elsif GNATprove_Mode
4927 and then Full_Analysis
4928 and then not Inside_A_Generic
4929 and then Present (Spec_Id)
4931 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4932 and then Body_Has_SPARK_Mode_On
4933 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4934 and then not Body_Has_Contract
4935 and then not Debug_Flag_M
4937 Build_Body_To_Inline (N, Spec_Id);
4940 -- When generating code, inherited pre/postconditions are handled when
4941 -- expanding the corresponding contract.
4943 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4944 -- of the specification we have to install the private withed units.
4945 -- This holds for child units as well.
4947 if Is_Compilation_Unit (Body_Id)
4948 or else Nkind (Parent (N)) = N_Compilation_Unit
4950 Install_Private_With_Clauses (Body_Id);
4953 Check_Anonymous_Return;
4955 -- Set the Protected_Formal field of each extra formal of the protected
4956 -- subprogram to reference the corresponding extra formal of the
4957 -- subprogram that implements it. For regular formals this occurs when
4958 -- the protected subprogram's declaration is expanded, but the extra
4959 -- formals don't get created until the subprogram is frozen. We need to
4960 -- do this before analyzing the protected subprogram's body so that any
4961 -- references to the original subprogram's extra formals will be changed
4962 -- refer to the implementing subprogram's formals (see Expand_Formal).
4964 if Present (Spec_Id)
4965 and then Is_Protected_Type (Scope (Spec_Id))
4966 and then Present (Protected_Body_Subprogram (Spec_Id))
4969 Impl_Subp : constant Entity_Id :=
4970 Protected_Body_Subprogram (Spec_Id);
4971 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4972 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4975 while Present (Prot_Ext_Formal) loop
4976 pragma Assert (Present (Impl_Ext_Formal));
4977 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4978 Next_Formal_With_Extras (Prot_Ext_Formal);
4979 Next_Formal_With_Extras (Impl_Ext_Formal);
4984 -- Generate minimum accessibility local objects to correspond with
4985 -- any extra formal added for anonymous access types. This new local
4986 -- object can then be used instead of the formal in case it is used
4987 -- in an actual to a call to a nested subprogram.
4989 -- This method is used to supplement our "small integer model" for
4990 -- accessibility-check generation (for more information see
4991 -- Accessibility_Level).
4993 -- Because we allow accessibility values greater than our expected value
4994 -- passing along the same extra accessibility formal as an actual
4995 -- to a nested subprogram becomes a problem because high values mean
4996 -- different things to the callee even though they are the same to the
4997 -- caller. So, as described in the first section, we create a local
4998 -- object representing the minimum of the accessibility level value that
4999 -- is passed in and the accessibility level of the callee's parameter
5000 -- and locals and use it in the case of a call to a nested subprogram.
5001 -- This generated object is refered to as a "minimum accessiblity
5004 if Present (Spec_Id) or else Present (Body_Id) then
5005 Body_Nod := Unit_Declaration_Node (Body_Id);
5010 -- Grab the appropriate formal depending on whether there exists
5011 -- an actual spec for the subprogram or whether we are dealing
5012 -- with a protected subprogram.
5014 if Present (Spec_Id) then
5015 if Present (Protected_Body_Subprogram (Spec_Id)) then
5016 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
5018 Form := First_Formal (Spec_Id);
5021 Form := First_Formal (Body_Id);
5024 -- Loop through formals if the subprogram is capable of accepting
5025 -- a generated local object. If it is not then it is also not
5026 -- capable of having local subprograms meaning it would not need
5027 -- a minimum accessibility level object anyway.
5029 if Present (Body_Nod)
5030 and then Has_Declarations (Body_Nod)
5031 and then Nkind (Body_Nod) /= N_Package_Specification
5033 while Present (Form) loop
5035 if Present (Extra_Accessibility (Form))
5036 and then No (Minimum_Accessibility (Form))
5038 -- Generate the minimum accessibility level object
5040 -- A60b : constant natural := natural'min(1, paramL);
5042 Generate_Minimum_Accessibility
5043 (Extra_Accessibility (Form), Form);
5049 -- Generate the minimum accessibility level object for the
5050 -- function's Extra_Accessibility_Of_Result.
5052 -- A31b : constant natural := natural'min (2, funcL);
5054 if Ekind (Body_Id) = E_Function
5055 and then Present (Extra_Accessibility_Of_Result (Body_Id))
5057 Generate_Minimum_Accessibility
5058 (Extra_Accessibility_Of_Result (Body_Id));
5060 -- Replace the Extra_Accessibility_Of_Result with the new
5061 -- minimum accessibility object.
5063 Set_Extra_Accessibility_Of_Result
5064 (Body_Id, Minimum_Accessibility
5065 (Extra_Accessibility_Of_Result (Body_Id)));
5071 -- Now we can go on to analyze the body
5073 HSS := Handled_Statement_Sequence (N);
5074 Set_Actual_Subtypes (N, Current_Scope);
5076 -- Add a declaration for the Protection object, renaming declarations
5077 -- for discriminals and privals and finally a declaration for the entry
5078 -- family index (if applicable). This form of early expansion is done
5079 -- when the Expander is active because Install_Private_Data_Declarations
5080 -- references entities which were created during regular expansion. The
5081 -- subprogram entity must come from source, and not be an internally
5082 -- generated subprogram.
5085 and then Present (Prot_Typ)
5086 and then Present (Spec_Id)
5087 and then Comes_From_Source (Spec_Id)
5088 and then not Is_Eliminated (Spec_Id)
5090 Install_Private_Data_Declarations
5091 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
5094 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
5095 -- may now appear in parameter and result profiles. Since the analysis
5096 -- of a subprogram body may use the parameter and result profile of the
5097 -- spec, swap any limited views with their non-limited counterpart.
5099 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
5100 Exch_Views := Exchange_Limited_Views (Spec_Id);
5103 -- If the return type is an anonymous access type whose designated type
5104 -- is the limited view of a class-wide type and the non-limited view is
5105 -- available, update the return type accordingly.
5107 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
5113 Rtyp := Etype (Spec_Id);
5115 if Ekind (Rtyp) = E_Anonymous_Access_Type then
5116 Etyp := Directly_Designated_Type (Rtyp);
5118 if Is_Class_Wide_Type (Etyp)
5119 and then From_Limited_With (Etyp)
5122 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
5128 -- Analyze any aspect specifications that appear on the subprogram body
5130 if Has_Aspects (N) then
5131 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
5134 Analyze_Declarations (Declarations (N));
5136 -- Verify that the SPARK_Mode of the body agrees with that of its spec
5138 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
5139 if Present (SPARK_Pragma (Spec_Id)) then
5140 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
5142 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
5144 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
5145 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
5146 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
5148 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
5151 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
5154 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
5155 -- as specified in source code, or because SPARK_Mode On is ignored
5156 -- in an instance where the context is SPARK_Mode Off/Auto.
5158 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
5159 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
5164 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
5165 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
5166 Error_Msg_Sloc := Sloc (Spec_Id);
5168 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
5172 -- A subprogram body freezes its own contract. Analyze the contract
5173 -- after the declarations of the body have been processed as pragmas
5174 -- are now chained on the contract of the subprogram body.
5176 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
5178 -- Check completion, and analyze the statements
5181 Inspect_Deferred_Constant_Completion (Declarations (N));
5184 -- Add the generated minimum accessibility objects to the subprogram
5185 -- body's list of declarations after analysis of the statements and
5188 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
5189 if Present (Declarations (Body_Nod)) then
5190 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
5193 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
5197 -- Deal with end of scope processing for the body
5199 Process_End_Label (HSS, 't', Current_Scope);
5200 Update_Use_Clause_Chain;
5203 -- If we are compiling an entry wrapper, remove the enclosing
5204 -- synchronized object from the stack.
5206 if Is_Entry_Wrapper (Body_Id) then
5210 Check_Subprogram_Order (N);
5211 Set_Analyzed (Body_Id);
5213 -- If we have a separate spec, then the analysis of the declarations
5214 -- caused the entities in the body to be chained to the spec id, but
5215 -- we want them chained to the body id. Only the formal parameters
5216 -- end up chained to the spec id in this case.
5218 if Present (Spec_Id) then
5220 -- We must conform to the categorization of our spec
5222 Validate_Categorization_Dependency (N, Spec_Id);
5224 -- And if this is a child unit, the parent units must conform
5226 if Is_Child_Unit (Spec_Id) then
5227 Validate_Categorization_Dependency
5228 (Unit_Declaration_Node (Spec_Id), Spec_Id);
5231 -- Here is where we move entities from the spec to the body
5233 -- Case where there are entities that stay with the spec
5235 if Present (Last_Real_Spec_Entity) then
5237 -- No body entities (happens when the only real spec entities come
5238 -- from precondition and postcondition pragmas).
5240 if No (Last_Entity (Body_Id)) then
5241 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
5243 -- Body entities present (formals), so chain stuff past them
5247 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
5250 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
5251 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5252 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
5254 -- Case where there are no spec entities, in this case there can be
5255 -- no body entities either, so just move everything.
5257 -- If the body is generated for an expression function, it may have
5258 -- been preanalyzed already, if 'access was applied to it.
5261 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
5262 N_Expression_Function
5264 pragma Assert (No (Last_Entity (Body_Id)));
5268 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
5269 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5270 Set_First_Entity (Spec_Id, Empty);
5271 Set_Last_Entity (Spec_Id, Empty);
5274 -- Otherwise the body does not complete a previous declaration. Check
5275 -- the categorization of the body against the units it withs.
5278 Validate_Categorization_Dependency (N, Body_Id);
5281 Check_Missing_Return;
5283 -- Now we are going to check for variables that are never modified in
5284 -- the body of the procedure. But first we deal with a special case
5285 -- where we want to modify this check. If the body of the subprogram
5286 -- starts with a raise statement or its equivalent, or if the body
5287 -- consists entirely of a null statement, then it is pretty obvious that
5288 -- it is OK to not reference the parameters. For example, this might be
5289 -- the following common idiom for a stubbed function: statement of the
5290 -- procedure raises an exception. In particular this deals with the
5291 -- common idiom of a stubbed function, which appears something like:
5293 -- function F (A : Integer) return Some_Type;
5296 -- raise Program_Error;
5300 -- Here the purpose of X is simply to satisfy the annoying requirement
5301 -- in Ada that there be at least one return, and we certainly do not
5302 -- want to go posting warnings on X that it is not initialized. On
5303 -- the other hand, if X is entirely unreferenced that should still
5306 -- What we do is to detect these cases, and if we find them, flag the
5307 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
5308 -- suppress unwanted warnings. For the case of the function stub above
5309 -- we have a special test to set X as apparently assigned to suppress
5316 -- Skip call markers installed by the ABE mechanism, labels, and
5317 -- Push_xxx_Error_Label to find the first real statement.
5319 Stm := First (Statements (HSS));
5320 while Nkind (Stm) in N_Call_Marker | N_Label | N_Push_xxx_Label loop
5324 -- Do the test on the original statement before expansion
5327 Ostm : constant Node_Id := Original_Node (Stm);
5330 -- If explicit raise statement, turn on flag
5332 if Nkind (Ostm) = N_Raise_Statement then
5333 Set_Trivial_Subprogram (Stm);
5335 -- If null statement, and no following statements, turn on flag
5337 elsif Nkind (Stm) = N_Null_Statement
5338 and then Comes_From_Source (Stm)
5339 and then No (Next (Stm))
5341 Set_Trivial_Subprogram (Stm);
5343 -- Check for explicit call cases which likely raise an exception
5345 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
5346 if Is_Entity_Name (Name (Ostm)) then
5348 Ent : constant Entity_Id := Entity (Name (Ostm));
5351 -- If the procedure is marked No_Return, then likely it
5352 -- raises an exception, but in any case it is not coming
5353 -- back here, so turn on the flag.
5356 and then Ekind (Ent) = E_Procedure
5357 and then No_Return (Ent)
5359 Set_Trivial_Subprogram (Stm);
5367 -- Check for variables that are never modified
5374 -- If there is a separate spec, then transfer Never_Set_In_Source
5375 -- flags from out parameters to the corresponding entities in the
5376 -- body. The reason we do that is we want to post error flags on
5377 -- the body entities, not the spec entities.
5379 if Present (Spec_Id) then
5380 E1 := First_Entity (Spec_Id);
5381 while Present (E1) loop
5382 if Ekind (E1) = E_Out_Parameter then
5383 E2 := First_Entity (Body_Id);
5384 while Present (E2) loop
5385 exit when Chars (E1) = Chars (E2);
5389 if Present (E2) then
5390 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
5398 -- Check references of the subprogram spec when we are dealing with
5399 -- an expression function due to it having a generated body.
5400 -- Otherwise, we simply check the formals of the subprogram body.
5402 if Present (Spec_Id)
5403 and then Is_Expression_Function (Spec_Id)
5405 Check_References (Spec_Id);
5407 Check_References (Body_Id);
5411 -- Check for nested subprogram, and mark outer level subprogram if so
5417 if Present (Spec_Id) then
5424 Ent := Enclosing_Subprogram (Ent);
5425 exit when No (Ent) or else Is_Subprogram (Ent);
5428 if Present (Ent) then
5429 Set_Has_Nested_Subprogram (Ent);
5433 -- Restore the limited views in the spec, if any, to let the back end
5434 -- process it without running into circularities.
5436 if Exch_Views /= No_Elist then
5437 Restore_Limited_Views (Exch_Views);
5440 if Mask_Types /= No_Elist then
5441 Unmask_Unfrozen_Types (Mask_Types);
5444 if Present (Desig_View) then
5445 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
5449 if Present (Ignored_Ghost_Region) then
5450 Expander_Active := Saved_EA;
5453 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5454 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5455 end Analyze_Subprogram_Body_Helper;
5457 ------------------------------------
5458 -- Analyze_Subprogram_Declaration --
5459 ------------------------------------
5461 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
5462 Scop : constant Entity_Id := Current_Scope;
5463 Designator : Entity_Id;
5465 Is_Completion : Boolean;
5466 -- Indicates whether a null procedure declaration is a completion
5469 -- Null procedures are not allowed in SPARK
5471 if Nkind (Specification (N)) = N_Procedure_Specification
5472 and then Null_Present (Specification (N))
5474 -- Null procedures are allowed in protected types, following the
5475 -- recent AI12-0147.
5477 if Is_Protected_Type (Current_Scope)
5478 and then Ada_Version < Ada_2012
5480 Error_Msg_N ("protected operation cannot be a null procedure", N);
5483 Analyze_Null_Procedure (N, Is_Completion);
5485 -- The null procedure acts as a body, nothing further is needed
5487 if Is_Completion then
5492 Designator := Analyze_Subprogram_Specification (Specification (N));
5494 -- A reference may already have been generated for the unit name, in
5495 -- which case the following call is redundant. However it is needed for
5496 -- declarations that are the rewriting of an expression function.
5498 Generate_Definition (Designator);
5500 -- Set the SPARK mode from the current context (may be overwritten later
5501 -- with explicit pragma). This is not done for entry barrier functions
5502 -- because they are generated outside the protected type and should not
5503 -- carry the mode of the enclosing context.
5505 if Nkind (N) = N_Subprogram_Declaration
5506 and then Is_Entry_Barrier_Function (N)
5511 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5512 Set_SPARK_Pragma_Inherited (Designator);
5515 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5516 -- the body of this subprogram is instantiated or inlined later and out
5517 -- of context. The body uses this attribute to restore the value of the
5520 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5521 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5524 -- Preserve relevant elaboration-related attributes of the context which
5525 -- are no longer available or very expensive to recompute once analysis,
5526 -- resolution, and expansion are over.
5528 Mark_Elaboration_Attributes
5529 (N_Id => Designator,
5533 if Debug_Flag_C then
5534 Write_Str ("==> subprogram spec ");
5535 Write_Name (Chars (Designator));
5536 Write_Str (" from ");
5537 Write_Location (Sloc (N));
5542 Validate_RCI_Subprogram_Declaration (N);
5543 New_Overloaded_Entity (Designator);
5544 Check_Delayed_Subprogram (Designator);
5546 -- If the type of the first formal of the current subprogram is a non-
5547 -- generic tagged private type, mark the subprogram as being a private
5548 -- primitive. Ditto if this is a function with controlling result, and
5549 -- the return type is currently private. In both cases, the type of the
5550 -- controlling argument or result must be in the current scope for the
5551 -- operation to be primitive.
5553 if Has_Controlling_Result (Designator)
5554 and then Is_Private_Type (Etype (Designator))
5555 and then Scope (Etype (Designator)) = Current_Scope
5556 and then not Is_Generic_Actual_Type (Etype (Designator))
5558 Set_Is_Private_Primitive (Designator);
5560 elsif Present (First_Formal (Designator)) then
5562 Formal_Typ : constant Entity_Id :=
5563 Etype (First_Formal (Designator));
5565 Set_Is_Private_Primitive (Designator,
5566 Is_Tagged_Type (Formal_Typ)
5567 and then Scope (Formal_Typ) = Current_Scope
5568 and then Is_Private_Type (Formal_Typ)
5569 and then not Is_Generic_Actual_Type (Formal_Typ));
5573 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5576 if Ada_Version >= Ada_2005
5577 and then Comes_From_Source (N)
5578 and then Is_Dispatching_Operation (Designator)
5585 if Has_Controlling_Result (Designator) then
5586 Etyp := Etype (Designator);
5589 E := First_Entity (Designator);
5591 and then Is_Formal (E)
5592 and then not Is_Controlling_Formal (E)
5600 if Is_Access_Type (Etyp) then
5601 Etyp := Directly_Designated_Type (Etyp);
5604 if Is_Interface (Etyp)
5605 and then not Is_Abstract_Subprogram (Designator)
5606 and then not (Ekind (Designator) = E_Procedure
5607 and then Null_Present (Specification (N)))
5609 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5611 -- Specialize error message based on procedures vs. functions,
5612 -- since functions can't be null subprograms.
5614 if Ekind (Designator) = E_Procedure then
5616 ("interface procedure % must be abstract or null", N);
5619 ("interface function % must be abstract", N);
5625 -- What is the following code for, it used to be
5627 -- ??? Set_Suppress_Elaboration_Checks
5628 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
5630 -- The following seems equivalent, but a bit dubious
5632 if Elaboration_Checks_Suppressed (Designator) then
5633 Set_Kill_Elaboration_Checks (Designator);
5636 -- For a compilation unit, set body required. This flag will only be
5637 -- reset if a valid Import or Interface pragma is processed later on.
5639 if Nkind (Parent (N)) = N_Compilation_Unit then
5640 Set_Body_Required (Parent (N), True);
5642 if Ada_Version >= Ada_2005
5643 and then Nkind (Specification (N)) = N_Procedure_Specification
5644 and then Null_Present (Specification (N))
5647 ("null procedure cannot be declared at library level", N);
5651 Generate_Reference_To_Formals (Designator);
5652 Check_Eliminated (Designator);
5654 if Debug_Flag_C then
5656 Write_Str ("<== subprogram spec ");
5657 Write_Name (Chars (Designator));
5658 Write_Str (" from ");
5659 Write_Location (Sloc (N));
5663 -- Indicate that this is a protected operation, because it may be used
5664 -- in subsequent declarations within the protected type.
5666 if Is_Protected_Type (Current_Scope) then
5667 Set_Convention (Designator, Convention_Protected);
5670 List_Inherited_Pre_Post_Aspects (Designator);
5672 -- Process the aspects before establishing the proper categorization in
5673 -- case the subprogram is a compilation unit and one of its aspects is
5674 -- converted into a categorization pragma.
5676 if Has_Aspects (N) then
5677 Analyze_Aspect_Specifications (N, Designator);
5680 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5681 Set_Categorization_From_Scope (Designator, Scop);
5683 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5684 -- proper categorization of the unit based on its pragmas.
5687 Push_Scope (Designator);
5688 Set_Categorization_From_Pragmas (N);
5689 Validate_Categorization_Dependency (N, Designator);
5692 end Analyze_Subprogram_Declaration;
5694 --------------------------------------
5695 -- Analyze_Subprogram_Specification --
5696 --------------------------------------
5698 -- Reminder: N here really is a subprogram specification (not a subprogram
5699 -- declaration). This procedure is called to analyze the specification in
5700 -- both subprogram bodies and subprogram declarations (specs).
5702 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5703 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5704 -- Determine whether entity E denotes the spec or body of an invariant
5707 ------------------------------------
5708 -- Is_Invariant_Procedure_Or_Body --
5709 ------------------------------------
5711 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5712 Decl : constant Node_Id := Unit_Declaration_Node (E);
5716 if Nkind (Decl) = N_Subprogram_Body then
5717 Spec := Corresponding_Spec (Decl);
5724 and then Ekind (Spec) = E_Procedure
5725 and then (Is_Partial_Invariant_Procedure (Spec)
5726 or else Is_Invariant_Procedure (Spec));
5727 end Is_Invariant_Procedure_Or_Body;
5731 Designator : constant Entity_Id := Defining_Entity (N);
5732 Formals : constant List_Id := Parameter_Specifications (N);
5734 -- Start of processing for Analyze_Subprogram_Specification
5737 -- Proceed with analysis. Do not emit a cross-reference entry if the
5738 -- specification comes from an expression function, because it may be
5739 -- the completion of a previous declaration. If it is not, the cross-
5740 -- reference entry will be emitted for the new subprogram declaration.
5742 if Nkind (Parent (N)) /= N_Expression_Function then
5743 Generate_Definition (Designator);
5746 if Nkind (N) = N_Function_Specification then
5747 Set_Ekind (Designator, E_Function);
5748 Set_Mechanism (Designator, Default_Mechanism);
5750 Set_Ekind (Designator, E_Procedure);
5751 Set_Etype (Designator, Standard_Void_Type);
5754 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5755 -- those subprograms which could be inlined in GNATprove mode (because
5756 -- Body_To_Inline is non-Empty) but should not be inlined.
5758 if GNATprove_Mode then
5759 Set_Is_Inlined_Always (Designator);
5762 -- Introduce new scope for analysis of the formals and the return type
5764 Set_Scope (Designator, Current_Scope);
5766 if Present (Formals) then
5767 Push_Scope (Designator);
5768 Process_Formals (Formals, N);
5770 -- Check dimensions in N for formals with default expression
5772 Analyze_Dimension_Formals (N, Formals);
5774 -- Ada 2005 (AI-345): If this is an overriding operation of an
5775 -- inherited interface operation, and the controlling type is
5776 -- a synchronized type, replace the type with its corresponding
5777 -- record, to match the proper signature of an overriding operation.
5778 -- Same processing for an access parameter whose designated type is
5779 -- derived from a synchronized interface.
5781 -- This modification is not done for invariant procedures because
5782 -- the corresponding record may not necessarely be visible when the
5783 -- concurrent type acts as the full view of a private type.
5786 -- type Prot is private with Type_Invariant => ...;
5787 -- procedure ConcInvariant (Obj : Prot);
5789 -- protected type Prot is ...;
5790 -- type Concurrent_Record_Prot is record ...;
5791 -- procedure ConcInvariant (Obj : Prot) is
5793 -- end ConcInvariant;
5796 -- In the example above, both the spec and body of the invariant
5797 -- procedure must utilize the private type as the controlling type.
5799 if Ada_Version >= Ada_2005
5800 and then not Is_Invariant_Procedure_Or_Body (Designator)
5804 Formal_Typ : Entity_Id;
5805 Rec_Typ : Entity_Id;
5806 Desig_Typ : Entity_Id;
5809 Formal := First_Formal (Designator);
5810 while Present (Formal) loop
5811 Formal_Typ := Etype (Formal);
5813 if Is_Concurrent_Type (Formal_Typ)
5814 and then Present (Corresponding_Record_Type (Formal_Typ))
5816 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5818 if Present (Interfaces (Rec_Typ)) then
5819 Set_Etype (Formal, Rec_Typ);
5822 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5823 Desig_Typ := Designated_Type (Formal_Typ);
5825 if Is_Concurrent_Type (Desig_Typ)
5826 and then Present (Corresponding_Record_Type (Desig_Typ))
5828 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5830 if Present (Interfaces (Rec_Typ)) then
5831 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5836 Next_Formal (Formal);
5843 -- The subprogram scope is pushed and popped around the processing of
5844 -- the return type for consistency with call above to Process_Formals
5845 -- (which itself can call Analyze_Return_Type), and to ensure that any
5846 -- itype created for the return type will be associated with the proper
5849 elsif Nkind (N) = N_Function_Specification then
5850 Push_Scope (Designator);
5851 Analyze_Return_Type (N);
5857 if Nkind (N) = N_Function_Specification then
5859 -- Deal with operator symbol case
5861 if Nkind (Designator) = N_Defining_Operator_Symbol then
5862 Valid_Operator_Definition (Designator);
5865 May_Need_Actuals (Designator);
5867 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5868 -- the subprogram is abstract also. This does not apply to renaming
5869 -- declarations, where abstractness is inherited, and to subprogram
5870 -- bodies generated for stream operations, which become renamings as
5873 -- In case of primitives associated with abstract interface types
5874 -- the check is applied later (see Analyze_Subprogram_Declaration).
5876 if Nkind (Original_Node (Parent (N))) not in
5877 N_Abstract_Subprogram_Declaration |
5878 N_Formal_Abstract_Subprogram_Declaration |
5879 N_Subprogram_Renaming_Declaration
5881 if Is_Abstract_Type (Etype (Designator)) then
5883 ("function that returns abstract type must be abstract", N);
5885 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5886 -- access result whose designated type is abstract.
5888 elsif Ada_Version >= Ada_2012
5889 and then Nkind (Result_Definition (N)) = N_Access_Definition
5891 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5892 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5895 ("function whose access result designates abstract type "
5896 & "must be abstract", N);
5902 end Analyze_Subprogram_Specification;
5904 -----------------------
5905 -- Check_Conformance --
5906 -----------------------
5908 procedure Check_Conformance
5909 (New_Id : Entity_Id;
5911 Ctype : Conformance_Type;
5913 Conforms : out Boolean;
5914 Err_Loc : Node_Id := Empty;
5915 Get_Inst : Boolean := False;
5916 Skip_Controlling_Formals : Boolean := False)
5918 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5919 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5920 -- If Errmsg is True, then processing continues to post an error message
5921 -- for conformance error on given node. Two messages are output. The
5922 -- first message points to the previous declaration with a general "no
5923 -- conformance" message. The second is the detailed reason, supplied as
5924 -- Msg. The parameter N provide information for a possible & insertion
5925 -- in the message, and also provides the location for posting the
5926 -- message in the absence of a specified Err_Loc location.
5928 function Conventions_Match (Id1, Id2 : Entity_Id) return Boolean;
5929 -- True if the conventions of entities Id1 and Id2 match.
5931 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean;
5932 -- True if the null exclusions of two formals of anonymous access type
5935 -----------------------
5936 -- Conformance_Error --
5937 -----------------------
5939 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5946 if No (Err_Loc) then
5952 Error_Msg_Sloc := Sloc (Old_Id);
5955 when Type_Conformant =>
5956 Error_Msg_N -- CODEFIX
5957 ("not type conformant with declaration#!", Enode);
5959 when Mode_Conformant =>
5960 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5962 ("not mode conformant with operation inherited#!",
5966 ("not mode conformant with declaration#!", Enode);
5969 when Subtype_Conformant =>
5970 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5972 ("not subtype conformant with operation inherited#!",
5976 ("not subtype conformant with declaration#!", Enode);
5979 when Fully_Conformant =>
5980 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5981 Error_Msg_N -- CODEFIX
5982 ("not fully conformant with operation inherited#!",
5985 Error_Msg_N -- CODEFIX
5986 ("not fully conformant with declaration#!", Enode);
5990 Error_Msg_NE (Msg, Enode, N);
5992 end Conformance_Error;
5994 -----------------------
5995 -- Conventions_Match --
5996 -----------------------
5998 function Conventions_Match
6000 Id2 : Entity_Id) return Boolean
6003 -- Ignore the conventions of anonymous access-to-subprogram types
6004 -- and subprogram types because these are internally generated and
6005 -- the only way these may receive a convention is if they inherit
6006 -- the convention of a related subprogram.
6008 if Ekind (Id1) in E_Anonymous_Access_Subprogram_Type
6011 Ekind (Id2) in E_Anonymous_Access_Subprogram_Type
6016 -- Otherwise compare the conventions directly
6019 return Convention (Id1) = Convention (Id2);
6021 end Conventions_Match;
6023 ---------------------------
6024 -- Null_Exclusions_Match --
6025 ---------------------------
6027 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean is
6029 if not Is_Anonymous_Access_Type (Etype (F1))
6030 or else not Is_Anonymous_Access_Type (Etype (F2))
6035 -- AI12-0289-1: Case of controlling access parameter; False if the
6036 -- partial view is untagged, the full view is tagged, and no explicit
6037 -- "not null". Note that at this point, we're processing the package
6038 -- body, so private/full types have been swapped. The Sloc test below
6039 -- is to detect the (legal) case where F1 comes after the full type
6040 -- declaration. This part is disabled pre-2005, because "not null" is
6041 -- not allowed on those language versions.
6043 if Ada_Version >= Ada_2005
6044 and then Is_Controlling_Formal (F1)
6045 and then not Null_Exclusion_Present (Parent (F1))
6046 and then not Null_Exclusion_Present (Parent (F2))
6049 D : constant Entity_Id := Directly_Designated_Type (Etype (F1));
6050 Partial_View_Of_Desig : constant Entity_Id :=
6051 Incomplete_Or_Partial_View (D);
6053 return No (Partial_View_Of_Desig)
6054 or else Is_Tagged_Type (Partial_View_Of_Desig)
6055 or else Sloc (D) < Sloc (F1);
6058 -- Not a controlling parameter, or one or both views have an explicit
6062 return Null_Exclusion_Present (Parent (F1)) =
6063 Null_Exclusion_Present (Parent (F2));
6065 end Null_Exclusions_Match;
6069 Old_Type : constant Entity_Id := Etype (Old_Id);
6070 New_Type : constant Entity_Id := Etype (New_Id);
6071 Old_Formal : Entity_Id;
6072 New_Formal : Entity_Id;
6073 Old_Formal_Base : Entity_Id;
6074 New_Formal_Base : Entity_Id;
6076 -- Start of processing for Check_Conformance
6081 -- We need a special case for operators, since they don't appear
6084 if Ctype = Type_Conformant then
6085 if Ekind (New_Id) = E_Operator
6086 and then Operator_Matches_Spec (New_Id, Old_Id)
6092 -- If both are functions/operators, check return types conform
6094 if Old_Type /= Standard_Void_Type
6096 New_Type /= Standard_Void_Type
6098 -- If we are checking interface conformance we omit controlling
6099 -- arguments and result, because we are only checking the conformance
6100 -- of the remaining parameters.
6102 if Has_Controlling_Result (Old_Id)
6103 and then Has_Controlling_Result (New_Id)
6104 and then Skip_Controlling_Formals
6108 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
6109 if Ctype >= Subtype_Conformant
6110 and then not Predicates_Match (Old_Type, New_Type)
6113 ("\predicate of return type does not match!", New_Id);
6116 ("\return type does not match!", New_Id);
6122 -- Ada 2005 (AI-231): In case of anonymous access types check the
6123 -- null-exclusion and access-to-constant attributes match.
6125 if Ada_Version >= Ada_2005
6126 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
6128 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
6129 or else Is_Access_Constant (Etype (Old_Type)) /=
6130 Is_Access_Constant (Etype (New_Type)))
6132 Conformance_Error ("\return type does not match!", New_Id);
6136 -- If either is a function/operator and the other isn't, error
6138 elsif Old_Type /= Standard_Void_Type
6139 or else New_Type /= Standard_Void_Type
6141 Conformance_Error ("\functions can only match functions!", New_Id);
6145 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
6146 -- If this is a renaming as body, refine error message to indicate that
6147 -- the conflict is with the original declaration. If the entity is not
6148 -- frozen, the conventions don't have to match, the one of the renamed
6149 -- entity is inherited.
6151 if Ctype >= Subtype_Conformant then
6152 if not Conventions_Match (Old_Id, New_Id) then
6153 if not Is_Frozen (New_Id) then
6156 elsif Present (Err_Loc)
6157 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
6158 and then Present (Corresponding_Spec (Err_Loc))
6160 Error_Msg_Name_1 := Chars (New_Id);
6162 Name_Ada + Convention_Id'Pos (Convention (New_Id));
6163 Conformance_Error ("\prior declaration for% has convention %!");
6167 Conformance_Error ("\calling conventions do not match!");
6171 Check_Formal_Subprogram_Conformance
6172 (New_Id, Old_Id, Err_Loc, Errmsg, Conforms);
6174 if not Conforms then
6180 -- Deal with parameters
6182 -- Note: we use the entity information, rather than going directly
6183 -- to the specification in the tree. This is not only simpler, but
6184 -- absolutely necessary for some cases of conformance tests between
6185 -- operators, where the declaration tree simply does not exist.
6187 Old_Formal := First_Formal (Old_Id);
6188 New_Formal := First_Formal (New_Id);
6189 while Present (Old_Formal) and then Present (New_Formal) loop
6190 if Is_Controlling_Formal (Old_Formal)
6191 and then Is_Controlling_Formal (New_Formal)
6192 and then Skip_Controlling_Formals
6194 -- The controlling formals will have different types when
6195 -- comparing an interface operation with its match, but both
6196 -- or neither must be access parameters.
6198 if Is_Access_Type (Etype (Old_Formal))
6200 Is_Access_Type (Etype (New_Formal))
6202 goto Skip_Controlling_Formal;
6205 ("\access parameter does not match!", New_Formal);
6209 -- Ada 2012: Mode conformance also requires that formal parameters
6210 -- be both aliased, or neither.
6212 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
6213 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
6215 ("\aliased parameter mismatch!", New_Formal);
6219 if Ctype = Fully_Conformant then
6221 -- Names must match. Error message is more accurate if we do
6222 -- this before checking that the types of the formals match.
6224 if Chars (Old_Formal) /= Chars (New_Formal) then
6225 Conformance_Error ("\name& does not match!", New_Formal);
6227 -- Set error posted flag on new formal as well to stop
6228 -- junk cascaded messages in some cases.
6230 Set_Error_Posted (New_Formal);
6234 -- Null exclusion must match
6236 if not Null_Exclusions_Match (Old_Formal, New_Formal) then
6238 ("\null exclusion for& does not match", New_Formal);
6240 -- Mark error posted on the new formal to avoid duplicated
6241 -- complaint about types not matching.
6243 Set_Error_Posted (New_Formal);
6247 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6248 -- case occurs whenever a subprogram is being renamed and one of its
6249 -- parameters imposes a null exclusion. For example:
6251 -- type T is null record;
6252 -- type Acc_T is access T;
6253 -- subtype Acc_T_Sub is Acc_T;
6255 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6256 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6259 Old_Formal_Base := Etype (Old_Formal);
6260 New_Formal_Base := Etype (New_Formal);
6263 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
6264 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
6267 -- Types must always match. In the visible part of an instance,
6268 -- usual overloading rules for dispatching operations apply, and
6269 -- we check base types (not the actual subtypes).
6271 if In_Instance_Visible_Part
6272 and then Is_Dispatching_Operation (New_Id)
6274 if not Conforming_Types
6275 (T1 => Base_Type (Etype (Old_Formal)),
6276 T2 => Base_Type (Etype (New_Formal)),
6278 Get_Inst => Get_Inst)
6280 Conformance_Error ("\type of & does not match!", New_Formal);
6284 elsif not Conforming_Types
6285 (T1 => Old_Formal_Base,
6286 T2 => New_Formal_Base,
6288 Get_Inst => Get_Inst)
6290 -- Don't give error message if old type is Any_Type. This test
6291 -- avoids some cascaded errors, e.g. in case of a bad spec.
6293 if Errmsg and then Old_Formal_Base = Any_Type then
6296 if Ctype >= Subtype_Conformant
6298 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
6301 ("\predicate of & does not match!", New_Formal);
6304 ("\type of & does not match!", New_Formal);
6306 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
6308 Error_Msg_N ("\dimensions mismatch!", New_Formal);
6316 -- For mode conformance, mode must match
6318 if Ctype >= Mode_Conformant then
6319 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6320 if Ekind (New_Id) not in E_Function | E_Procedure
6321 or else not Is_Primitive_Wrapper (New_Id)
6323 Conformance_Error ("\mode of & does not match!", New_Formal);
6327 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6329 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
6334 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
6338 ("\mode of & does not match!", New_Formal);
6345 elsif Is_Access_Type (Old_Formal_Base)
6346 and then Is_Access_Type (New_Formal_Base)
6347 and then Is_Access_Constant (Old_Formal_Base) /=
6348 Is_Access_Constant (New_Formal_Base)
6351 ("\constant modifier does not match!", New_Formal);
6356 if Ctype >= Subtype_Conformant then
6358 -- Ada 2005 (AI-231): In case of anonymous access types check
6359 -- the null-exclusion and access-to-constant attributes must
6360 -- match. For null exclusion, we test the types rather than the
6361 -- formals themselves, since the attribute is only set reliably
6362 -- on the formals in the Ada 95 case, and we exclude the case
6363 -- where Old_Formal is marked as controlling, to avoid errors
6364 -- when matching completing bodies with dispatching declarations
6365 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6367 if Ada_Version >= Ada_2005
6368 and then Is_Anonymous_Access_Type (Etype (Old_Formal))
6369 and then Is_Anonymous_Access_Type (Etype (New_Formal))
6371 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6372 Can_Never_Be_Null (Etype (New_Formal))
6374 not Is_Controlling_Formal (Old_Formal))
6376 Is_Access_Constant (Etype (Old_Formal)) /=
6377 Is_Access_Constant (Etype (New_Formal)))
6379 -- Do not complain if error already posted on New_Formal. This
6380 -- avoids some redundant error messages.
6382 and then not Error_Posted (New_Formal)
6384 -- It is allowed to omit the null-exclusion in case of stream
6385 -- attribute subprograms. We recognize stream subprograms
6386 -- through their TSS-generated suffix.
6389 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6392 if TSS_Name /= TSS_Stream_Read
6393 and then TSS_Name /= TSS_Stream_Write
6394 and then TSS_Name /= TSS_Stream_Input
6395 and then TSS_Name /= TSS_Stream_Output
6397 -- Here we have a definite conformance error. It is worth
6398 -- special casing the error message for the case of a
6399 -- controlling formal (which excludes null).
6401 if Is_Controlling_Formal (New_Formal) then
6402 Error_Msg_Node_2 := Scope (New_Formal);
6404 ("\controlling formal & of & excludes null, "
6405 & "declaration must exclude null as well",
6408 -- Normal case (couldn't we give more detail here???)
6412 ("\type of & does not match!", New_Formal);
6421 -- Full conformance checks
6423 if Ctype = Fully_Conformant then
6425 -- We have checked already that names match
6427 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6429 -- Check default expressions for in parameters
6432 NewD : constant Boolean :=
6433 Present (Default_Value (New_Formal));
6434 OldD : constant Boolean :=
6435 Present (Default_Value (Old_Formal));
6437 if NewD or OldD then
6439 -- The old default value has been analyzed because the
6440 -- current full declaration will have frozen everything
6441 -- before. The new default value has not been analyzed,
6442 -- so analyze it now before we check for conformance.
6445 Push_Scope (New_Id);
6446 Preanalyze_Spec_Expression
6447 (Default_Value (New_Formal), Etype (New_Formal));
6451 if not (NewD and OldD)
6452 or else not Fully_Conformant_Expressions
6453 (Default_Value (Old_Formal),
6454 Default_Value (New_Formal))
6457 ("\default expression for & does not match!",
6466 -- A couple of special checks for Ada 83 mode. These checks are
6467 -- skipped if either entity is an operator in package Standard,
6468 -- or if either old or new instance is not from the source program.
6470 if Ada_Version = Ada_83
6471 and then Sloc (Old_Id) > Standard_Location
6472 and then Sloc (New_Id) > Standard_Location
6473 and then Comes_From_Source (Old_Id)
6474 and then Comes_From_Source (New_Id)
6477 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6478 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6481 -- Explicit IN must be present or absent in both cases. This
6482 -- test is required only in the full conformance case.
6484 if In_Present (Old_Param) /= In_Present (New_Param)
6485 and then Ctype = Fully_Conformant
6488 ("\(Ada 83) IN must appear in both declarations",
6493 -- Grouping (use of comma in param lists) must be the same
6494 -- This is where we catch a misconformance like:
6497 -- A : Integer; B : Integer
6499 -- which are represented identically in the tree except
6500 -- for the setting of the flags More_Ids and Prev_Ids.
6502 if More_Ids (Old_Param) /= More_Ids (New_Param)
6503 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6506 ("\grouping of & does not match!", New_Formal);
6512 -- This label is required when skipping controlling formals
6514 <<Skip_Controlling_Formal>>
6516 Next_Formal (Old_Formal);
6517 Next_Formal (New_Formal);
6520 if Present (Old_Formal) then
6521 Conformance_Error ("\too few parameters!");
6524 elsif Present (New_Formal) then
6525 Conformance_Error ("\too many parameters!", New_Formal);
6528 end Check_Conformance;
6530 -----------------------
6531 -- Check_Conventions --
6532 -----------------------
6534 procedure Check_Conventions (Typ : Entity_Id) is
6535 Ifaces_List : Elist_Id;
6537 procedure Check_Convention (Op : Entity_Id);
6538 -- Verify that the convention of inherited dispatching operation Op is
6539 -- consistent among all subprograms it overrides. In order to minimize
6540 -- the search, Search_From is utilized to designate a specific point in
6541 -- the list rather than iterating over the whole list once more.
6543 ----------------------
6544 -- Check_Convention --
6545 ----------------------
6547 procedure Check_Convention (Op : Entity_Id) is
6548 Op_Conv : constant Convention_Id := Convention (Op);
6549 Iface_Conv : Convention_Id;
6550 Iface_Elmt : Elmt_Id;
6551 Iface_Prim_Elmt : Elmt_Id;
6552 Iface_Prim : Entity_Id;
6555 Iface_Elmt := First_Elmt (Ifaces_List);
6556 while Present (Iface_Elmt) loop
6558 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6559 while Present (Iface_Prim_Elmt) loop
6560 Iface_Prim := Node (Iface_Prim_Elmt);
6561 Iface_Conv := Convention (Iface_Prim);
6563 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6564 and then Iface_Conv /= Op_Conv
6567 ("inconsistent conventions in primitive operations", Typ);
6569 Error_Msg_Name_1 := Chars (Op);
6570 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6571 Error_Msg_Sloc := Sloc (Op);
6573 if Comes_From_Source (Op) or else No (Alias (Op)) then
6574 if not Present (Overridden_Operation (Op)) then
6575 Error_Msg_N ("\\primitive % defined #", Typ);
6578 ("\\overriding operation % with "
6579 & "convention % defined #", Typ);
6582 else pragma Assert (Present (Alias (Op)));
6583 Error_Msg_Sloc := Sloc (Alias (Op));
6584 Error_Msg_N ("\\inherited operation % with "
6585 & "convention % defined #", Typ);
6588 Error_Msg_Name_1 := Chars (Op);
6589 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6590 Error_Msg_Sloc := Sloc (Iface_Prim);
6591 Error_Msg_N ("\\overridden operation % with "
6592 & "convention % defined #", Typ);
6594 -- Avoid cascading errors
6599 Next_Elmt (Iface_Prim_Elmt);
6602 Next_Elmt (Iface_Elmt);
6604 end Check_Convention;
6608 Prim_Op : Entity_Id;
6609 Prim_Op_Elmt : Elmt_Id;
6611 -- Start of processing for Check_Conventions
6614 if not Has_Interfaces (Typ) then
6618 Collect_Interfaces (Typ, Ifaces_List);
6620 -- The algorithm checks every overriding dispatching operation against
6621 -- all the corresponding overridden dispatching operations, detecting
6622 -- differences in conventions.
6624 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6625 while Present (Prim_Op_Elmt) loop
6626 Prim_Op := Node (Prim_Op_Elmt);
6628 -- A small optimization: skip the predefined dispatching operations
6629 -- since they always have the same convention.
6631 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6632 Check_Convention (Prim_Op);
6635 Next_Elmt (Prim_Op_Elmt);
6637 end Check_Conventions;
6639 ------------------------------
6640 -- Check_Delayed_Subprogram --
6641 ------------------------------
6643 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6644 procedure Possible_Freeze (T : Entity_Id);
6645 -- T is the type of either a formal parameter or of the return type. If
6646 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6647 -- itself must be delayed.
6649 ---------------------
6650 -- Possible_Freeze --
6651 ---------------------
6653 procedure Possible_Freeze (T : Entity_Id) is
6654 Scop : constant Entity_Id := Scope (Designator);
6657 -- If the subprogram appears within a package instance (which may be
6658 -- the wrapper package of a subprogram instance) the freeze node for
6659 -- that package will freeze the subprogram at the proper place, so
6660 -- do not emit a freeze node for the subprogram, given that it may
6661 -- appear in the wrong scope.
6663 if Ekind (Scop) = E_Package
6664 and then not Comes_From_Source (Scop)
6665 and then Is_Generic_Instance (Scop)
6669 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6670 Set_Has_Delayed_Freeze (Designator);
6672 elsif Is_Access_Type (T)
6673 and then Has_Delayed_Freeze (Designated_Type (T))
6674 and then not Is_Frozen (Designated_Type (T))
6676 Set_Has_Delayed_Freeze (Designator);
6678 end Possible_Freeze;
6684 -- Start of processing for Check_Delayed_Subprogram
6687 -- All subprograms, including abstract subprograms, may need a freeze
6688 -- node if some formal type or the return type needs one.
6690 Possible_Freeze (Etype (Designator));
6691 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6693 -- Need delayed freeze if any of the formal types themselves need a
6694 -- delayed freeze and are not yet frozen.
6696 F := First_Formal (Designator);
6697 while Present (F) loop
6698 Possible_Freeze (Etype (F));
6699 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6703 -- Mark functions that return by reference. Note that it cannot be done
6704 -- for delayed_freeze subprograms because the underlying returned type
6705 -- may not be known yet (for private types).
6707 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6709 Typ : constant Entity_Id := Etype (Designator);
6710 Utyp : constant Entity_Id := Underlying_Type (Typ);
6713 if Is_Limited_View (Typ) then
6714 Set_Returns_By_Ref (Designator);
6716 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6717 Set_Returns_By_Ref (Designator);
6721 end Check_Delayed_Subprogram;
6723 ------------------------------------
6724 -- Check_Discriminant_Conformance --
6725 ------------------------------------
6727 procedure Check_Discriminant_Conformance
6732 Old_Discr : Entity_Id := First_Discriminant (Prev);
6733 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6734 New_Discr_Id : Entity_Id;
6735 New_Discr_Type : Entity_Id;
6737 procedure Conformance_Error (Msg : String; N : Node_Id);
6738 -- Post error message for conformance error on given node. Two messages
6739 -- are output. The first points to the previous declaration with a
6740 -- general "no conformance" message. The second is the detailed reason,
6741 -- supplied as Msg. The parameter N provide information for a possible
6742 -- & insertion in the message.
6744 -----------------------
6745 -- Conformance_Error --
6746 -----------------------
6748 procedure Conformance_Error (Msg : String; N : Node_Id) is
6750 Error_Msg_Sloc := Sloc (Prev_Loc);
6751 Error_Msg_N -- CODEFIX
6752 ("not fully conformant with declaration#!", N);
6753 Error_Msg_NE (Msg, N, N);
6754 end Conformance_Error;
6756 -- Start of processing for Check_Discriminant_Conformance
6759 while Present (Old_Discr) and then Present (New_Discr) loop
6760 New_Discr_Id := Defining_Identifier (New_Discr);
6762 -- The subtype mark of the discriminant on the full type has not
6763 -- been analyzed so we do it here. For an access discriminant a new
6766 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6768 Access_Definition (N, Discriminant_Type (New_Discr));
6771 Find_Type (Discriminant_Type (New_Discr));
6772 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6774 -- Ada 2005: if the discriminant definition carries a null
6775 -- exclusion, create an itype to check properly for consistency
6776 -- with partial declaration.
6778 if Is_Access_Type (New_Discr_Type)
6779 and then Null_Exclusion_Present (New_Discr)
6782 Create_Null_Excluding_Itype
6783 (T => New_Discr_Type,
6784 Related_Nod => New_Discr,
6785 Scope_Id => Current_Scope);
6789 if not Conforming_Types
6790 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6792 Conformance_Error ("type of & does not match!", New_Discr_Id);
6795 -- Treat the new discriminant as an occurrence of the old one,
6796 -- for navigation purposes, and fill in some semantic
6797 -- information, for completeness.
6799 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6800 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6801 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6806 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6807 Conformance_Error ("name & does not match!", New_Discr_Id);
6811 -- Default expressions must match
6814 NewD : constant Boolean :=
6815 Present (Expression (New_Discr));
6816 OldD : constant Boolean :=
6817 Present (Expression (Parent (Old_Discr)));
6820 if NewD or OldD then
6822 -- The old default value has been analyzed and expanded,
6823 -- because the current full declaration will have frozen
6824 -- everything before. The new default values have not been
6825 -- expanded, so expand now to check conformance.
6828 Preanalyze_Spec_Expression
6829 (Expression (New_Discr), New_Discr_Type);
6832 if not (NewD and OldD)
6833 or else not Fully_Conformant_Expressions
6834 (Expression (Parent (Old_Discr)),
6835 Expression (New_Discr))
6839 ("default expression for & does not match!",
6846 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6848 if Ada_Version = Ada_83 then
6850 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6853 -- Grouping (use of comma in param lists) must be the same
6854 -- This is where we catch a misconformance like:
6857 -- A : Integer; B : Integer
6859 -- which are represented identically in the tree except
6860 -- for the setting of the flags More_Ids and Prev_Ids.
6862 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6863 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6866 ("grouping of & does not match!", New_Discr_Id);
6872 Next_Discriminant (Old_Discr);
6876 if Present (Old_Discr) then
6877 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6880 elsif Present (New_Discr) then
6882 ("too many discriminants!", Defining_Identifier (New_Discr));
6885 end Check_Discriminant_Conformance;
6887 -----------------------------------------
6888 -- Check_Formal_Subprogram_Conformance --
6889 -----------------------------------------
6891 procedure Check_Formal_Subprogram_Conformance
6892 (New_Id : Entity_Id;
6896 Conforms : out Boolean)
6902 if Is_Formal_Subprogram (Old_Id)
6903 or else Is_Formal_Subprogram (New_Id)
6904 or else (Is_Subprogram (New_Id)
6905 and then Present (Alias (New_Id))
6906 and then Is_Formal_Subprogram (Alias (New_Id)))
6908 if Present (Err_Loc) then
6917 Error_Msg_Sloc := Sloc (Old_Id);
6918 Error_Msg_N ("not subtype conformant with declaration#!", N);
6920 ("\formal subprograms are not subtype conformant "
6921 & "(RM 6.3.1 (17/3))", N, New_Id);
6924 end Check_Formal_Subprogram_Conformance;
6926 procedure Check_Formal_Subprogram_Conformance
6927 (New_Id : Entity_Id;
6929 Err_Loc : Node_Id := Empty)
6933 Check_Formal_Subprogram_Conformance
6934 (New_Id, Old_Id, Err_Loc, True, Ignore);
6935 end Check_Formal_Subprogram_Conformance;
6937 ----------------------------
6938 -- Check_Fully_Conformant --
6939 ----------------------------
6941 procedure Check_Fully_Conformant
6942 (New_Id : Entity_Id;
6944 Err_Loc : Node_Id := Empty)
6947 pragma Warnings (Off, Result);
6950 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6951 end Check_Fully_Conformant;
6953 --------------------------
6954 -- Check_Limited_Return --
6955 --------------------------
6957 procedure Check_Limited_Return
6963 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6964 -- replaced by anonymous access results. This is an incompatibility with
6965 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6966 -- controllable with special switch. ???
6968 -- A limited interface that is not immutably limited is OK
6970 if Is_Limited_Interface (R_Type)
6972 not (Is_Task_Interface (R_Type)
6973 or else Is_Protected_Interface (R_Type)
6974 or else Is_Synchronized_Interface (R_Type))
6978 elsif Is_Limited_Type (R_Type)
6979 and then not Is_Interface (R_Type)
6980 and then Comes_From_Source (N)
6981 and then not In_Instance_Body
6982 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6984 -- Error in Ada 2005
6986 if Ada_Version >= Ada_2005
6987 and then not Debug_Flag_Dot_L
6988 and then not GNAT_Mode
6991 ("(Ada 2005) cannot copy object of a limited type "
6992 & "(RM-2005 6.5(5.5/2))", Expr);
6994 if Is_Limited_View (R_Type) then
6996 ("\return by reference not permitted in Ada 2005", Expr);
6999 -- Warn in Ada 95 mode, to give folks a heads up about this
7002 -- In GNAT mode, this is just a warning, to allow it to be evilly
7003 -- turned off. Otherwise it is a real error.
7005 -- In a generic context, simplify the warning because it makes no
7006 -- sense to discuss pass-by-reference or copy.
7008 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
7009 if Inside_A_Generic then
7011 ("return of limited object not permitted in Ada 2005 "
7012 & "(RM-2005 6.5(5.5/2))?y?", Expr);
7014 elsif Is_Limited_View (R_Type) then
7016 ("return by reference not permitted in Ada 2005 "
7017 & "(RM-2005 6.5(5.5/2))?y?", Expr);
7020 ("cannot copy object of a limited type in Ada 2005 "
7021 & "(RM-2005 6.5(5.5/2))?y?", Expr);
7024 -- Ada 95 mode, and compatibility warnings disabled
7027 pragma Assert (Ada_Version <= Ada_95);
7028 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
7029 return; -- skip continuation messages below
7032 if not Inside_A_Generic then
7034 ("\consider switching to return of access type", Expr);
7035 Explain_Limited_Type (R_Type, Expr);
7038 end Check_Limited_Return;
7040 ---------------------------
7041 -- Check_Mode_Conformant --
7042 ---------------------------
7044 procedure Check_Mode_Conformant
7045 (New_Id : Entity_Id;
7047 Err_Loc : Node_Id := Empty;
7048 Get_Inst : Boolean := False)
7051 pragma Warnings (Off, Result);
7054 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
7055 end Check_Mode_Conformant;
7057 --------------------------------
7058 -- Check_Overriding_Indicator --
7059 --------------------------------
7061 procedure Check_Overriding_Indicator
7063 Overridden_Subp : Entity_Id;
7064 Is_Primitive : Boolean)
7070 -- No overriding indicator for literals
7072 if Ekind (Subp) = E_Enumeration_Literal then
7075 elsif Ekind (Subp) = E_Entry then
7076 Decl := Parent (Subp);
7078 -- No point in analyzing a malformed operator
7080 elsif Nkind (Subp) = N_Defining_Operator_Symbol
7081 and then Error_Posted (Subp)
7086 Decl := Unit_Declaration_Node (Subp);
7089 if Nkind (Decl) in N_Subprogram_Body
7090 | N_Subprogram_Body_Stub
7091 | N_Subprogram_Declaration
7092 | N_Abstract_Subprogram_Declaration
7093 | N_Subprogram_Renaming_Declaration
7095 Spec := Specification (Decl);
7097 elsif Nkind (Decl) = N_Entry_Declaration then
7104 -- An overriding indication is illegal on a subprogram declared
7105 -- in a protected body, where there is no operation to override.
7107 if (Must_Override (Spec) or else Must_Not_Override (Spec))
7108 and then Is_List_Member (Decl)
7109 and then Present (Parent (List_Containing (Decl)))
7110 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
7113 ("illegal overriding indication in protected body", Decl);
7117 -- The overriding operation is type conformant with the overridden one,
7118 -- but the names of the formals are not required to match. If the names
7119 -- appear permuted in the overriding operation, this is a possible
7120 -- source of confusion that is worth diagnosing. Controlling formals
7121 -- often carry names that reflect the type, and it is not worthwhile
7122 -- requiring that their names match.
7124 if Present (Overridden_Subp)
7125 and then Nkind (Subp) /= N_Defining_Operator_Symbol
7132 Form1 := First_Formal (Subp);
7133 Form2 := First_Formal (Overridden_Subp);
7135 -- If the overriding operation is a synchronized operation, skip
7136 -- the first parameter of the overridden operation, which is
7137 -- implicit in the new one. If the operation is declared in the
7138 -- body it is not primitive and all formals must match.
7140 if Is_Concurrent_Type (Scope (Subp))
7141 and then Is_Tagged_Type (Scope (Subp))
7142 and then not Has_Completion (Scope (Subp))
7144 Form2 := Next_Formal (Form2);
7147 if Present (Form1) then
7148 Form1 := Next_Formal (Form1);
7149 Form2 := Next_Formal (Form2);
7152 while Present (Form1) loop
7153 if not Is_Controlling_Formal (Form1)
7154 and then Present (Next_Formal (Form2))
7155 and then Chars (Form1) = Chars (Next_Formal (Form2))
7157 Error_Msg_Node_2 := Alias (Overridden_Subp);
7158 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7160 ("& does not match corresponding formal of&#",
7165 Next_Formal (Form1);
7166 Next_Formal (Form2);
7171 -- If there is an overridden subprogram, then check that there is no
7172 -- "not overriding" indicator, and mark the subprogram as overriding.
7174 -- This is not done if the overridden subprogram is marked as hidden,
7175 -- which can occur for the case of inherited controlled operations
7176 -- (see Derive_Subprogram), unless the inherited subprogram's parent
7177 -- subprogram is not itself hidden or we are within a generic instance,
7178 -- in which case the hidden flag may have been modified for the
7179 -- expansion of the instance.
7181 -- (Note: This condition could probably be simplified, leaving out the
7182 -- testing for the specific controlled cases, but it seems safer and
7183 -- clearer this way, and echoes similar special-case tests of this
7184 -- kind in other places.)
7186 if Present (Overridden_Subp)
7187 and then (not Is_Hidden (Overridden_Subp)
7189 (Chars (Overridden_Subp) in Name_Initialize
7192 and then Present (Alias (Overridden_Subp))
7193 and then (not Is_Hidden (Alias (Overridden_Subp))
7194 or else In_Instance)))
7196 if Must_Not_Override (Spec) then
7197 Error_Msg_Sloc := Sloc (Overridden_Subp);
7199 if Ekind (Subp) = E_Entry then
7201 ("entry & overrides inherited operation #", Spec, Subp);
7204 ("subprogram & overrides inherited operation #", Spec, Subp);
7207 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
7208 -- as an extension of Root_Controlled, and thus has a useless Adjust
7209 -- operation. This operation should not be inherited by other limited
7210 -- controlled types. An explicit Adjust for them is not overriding.
7212 elsif Must_Override (Spec)
7213 and then Chars (Overridden_Subp) = Name_Adjust
7214 and then Is_Limited_Type (Etype (First_Formal (Subp)))
7215 and then Present (Alias (Overridden_Subp))
7216 and then In_Predefined_Unit (Alias (Overridden_Subp))
7219 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
7220 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7222 elsif Is_Subprogram (Subp) then
7223 if Is_Init_Proc (Subp) then
7226 elsif No (Overridden_Operation (Subp)) then
7228 -- For entities generated by Derive_Subprograms the overridden
7229 -- operation is the inherited primitive (which is available
7230 -- through the attribute alias)
7232 if (Is_Dispatching_Operation (Subp)
7233 or else Is_Dispatching_Operation (Overridden_Subp))
7234 and then not Comes_From_Source (Overridden_Subp)
7235 and then Find_Dispatching_Type (Overridden_Subp) =
7236 Find_Dispatching_Type (Subp)
7237 and then Present (Alias (Overridden_Subp))
7238 and then Comes_From_Source (Alias (Overridden_Subp))
7240 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
7241 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
7244 Set_Overridden_Operation (Subp, Overridden_Subp);
7245 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
7250 -- If primitive flag is set or this is a protected operation, then
7251 -- the operation is overriding at the point of its declaration, so
7252 -- warn if necessary. Otherwise it may have been declared before the
7253 -- operation it overrides and no check is required.
7256 and then not Must_Override (Spec)
7257 and then (Is_Primitive
7258 or else Ekind (Scope (Subp)) = E_Protected_Type)
7260 Style.Missing_Overriding (Decl, Subp);
7263 -- If Subp is an operator, it may override a predefined operation, if
7264 -- it is defined in the same scope as the type to which it applies.
7265 -- In that case Overridden_Subp is empty because of our implicit
7266 -- representation for predefined operators. We have to check whether the
7267 -- signature of Subp matches that of a predefined operator. Note that
7268 -- first argument provides the name of the operator, and the second
7269 -- argument the signature that may match that of a standard operation.
7270 -- If the indicator is overriding, then the operator must match a
7271 -- predefined signature, because we know already that there is no
7272 -- explicit overridden operation.
7274 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
7275 if Must_Not_Override (Spec) then
7277 -- If this is not a primitive or a protected subprogram, then
7278 -- "not overriding" is illegal.
7281 and then Ekind (Scope (Subp)) /= E_Protected_Type
7283 Error_Msg_N ("overriding indicator only allowed "
7284 & "if subprogram is primitive", Subp);
7286 elsif Can_Override_Operator (Subp) then
7288 ("subprogram& overrides predefined operator ", Spec, Subp);
7291 elsif Must_Override (Spec) then
7292 if No (Overridden_Operation (Subp))
7293 and then not Can_Override_Operator (Subp)
7295 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7298 elsif not Error_Posted (Subp)
7299 and then Style_Check
7300 and then Can_Override_Operator (Subp)
7301 and then not In_Predefined_Unit (Subp)
7303 -- If style checks are enabled, indicate that the indicator is
7304 -- missing. However, at the point of declaration, the type of
7305 -- which this is a primitive operation may be private, in which
7306 -- case the indicator would be premature.
7308 if Has_Private_Declaration (Etype (Subp))
7309 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
7313 Style.Missing_Overriding (Decl, Subp);
7317 elsif Must_Override (Spec) then
7318 if Ekind (Subp) = E_Entry then
7319 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
7321 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7324 -- If the operation is marked "not overriding" and it's not primitive
7325 -- then an error is issued, unless this is an operation of a task or
7326 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7327 -- has been specified have already been checked above.
7329 elsif Must_Not_Override (Spec)
7330 and then not Is_Primitive
7331 and then Ekind (Subp) /= E_Entry
7332 and then Ekind (Scope (Subp)) /= E_Protected_Type
7335 ("overriding indicator only allowed if subprogram is primitive",
7339 end Check_Overriding_Indicator;
7345 -- Note: this procedure needs to know far too much about how the expander
7346 -- messes with exceptions. The use of the flag Exception_Junk and the
7347 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7348 -- works, but is not very clean. It would be better if the expansion
7349 -- routines would leave Original_Node working nicely, and we could use
7350 -- Original_Node here to ignore all the peculiar expander messing ???
7352 procedure Check_Returns
7356 Proc : Entity_Id := Empty)
7360 procedure Check_Statement_Sequence (L : List_Id);
7361 -- Internal recursive procedure to check a list of statements for proper
7362 -- termination by a return statement (or a transfer of control or a
7363 -- compound statement that is itself internally properly terminated).
7365 ------------------------------
7366 -- Check_Statement_Sequence --
7367 ------------------------------
7369 procedure Check_Statement_Sequence (L : List_Id) is
7374 function Assert_False return Boolean;
7375 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7376 -- rewritten as a null statement when assertions are off. The assert
7377 -- is not active, but it is still enough to kill the warning.
7383 function Assert_False return Boolean is
7384 Orig : constant Node_Id := Original_Node (Last_Stm);
7387 if Nkind (Orig) = N_Pragma
7388 and then Pragma_Name (Orig) = Name_Assert
7389 and then not Error_Posted (Orig)
7392 Arg : constant Node_Id :=
7393 First (Pragma_Argument_Associations (Orig));
7394 Exp : constant Node_Id := Expression (Arg);
7396 return Nkind (Exp) = N_Identifier
7397 and then Chars (Exp) = Name_False;
7407 Raise_Exception_Call : Boolean;
7408 -- Set True if statement sequence terminated by Raise_Exception call
7409 -- or a Reraise_Occurrence call.
7411 -- Start of processing for Check_Statement_Sequence
7414 Raise_Exception_Call := False;
7416 -- Get last real statement
7418 Last_Stm := Last (L);
7420 -- Deal with digging out exception handler statement sequences that
7421 -- have been transformed by the local raise to goto optimization.
7422 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7423 -- optimization has occurred, we are looking at something like:
7426 -- original stmts in block
7430 -- goto L1; | omitted if No_Exception_Propagation
7435 -- goto L3; -- skip handler when exception not raised
7437 -- <<L1>> -- target label for local exception
7451 -- and what we have to do is to dig out the estmts1 and estmts2
7452 -- sequences (which were the original sequences of statements in
7453 -- the exception handlers) and check them.
7455 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7460 exit when Nkind (Stm) /= N_Block_Statement;
7461 exit when not Exception_Junk (Stm);
7464 exit when Nkind (Stm) /= N_Label;
7465 exit when not Exception_Junk (Stm);
7466 Check_Statement_Sequence
7467 (Statements (Handled_Statement_Sequence (Next (Stm))));
7472 exit when Nkind (Stm) /= N_Goto_Statement;
7473 exit when not Exception_Junk (Stm);
7477 -- Don't count pragmas
7479 while Nkind (Last_Stm) = N_Pragma
7481 -- Don't count call to SS_Release (can happen after Raise_Exception)
7484 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7486 Nkind (Name (Last_Stm)) = N_Identifier
7488 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7490 -- Don't count exception junk
7493 (Nkind (Last_Stm) in
7494 N_Goto_Statement | N_Label | N_Object_Declaration
7495 and then Exception_Junk (Last_Stm))
7496 or else Nkind (Last_Stm) in N_Push_xxx_Label | N_Pop_xxx_Label
7498 -- Inserted code, such as finalization calls, is irrelevant: we only
7499 -- need to check original source.
7501 or else Is_Rewrite_Insertion (Last_Stm)
7506 -- Here we have the "real" last statement
7508 Kind := Nkind (Last_Stm);
7510 -- Transfer of control, OK. Note that in the No_Return procedure
7511 -- case, we already diagnosed any explicit return statements, so
7512 -- we can treat them as OK in this context.
7514 if Is_Transfer (Last_Stm) then
7517 -- Check cases of explicit non-indirect procedure calls
7519 elsif Kind = N_Procedure_Call_Statement
7520 and then Is_Entity_Name (Name (Last_Stm))
7522 -- Check call to Raise_Exception procedure which is treated
7523 -- specially, as is a call to Reraise_Occurrence.
7525 -- We suppress the warning in these cases since it is likely that
7526 -- the programmer really does not expect to deal with the case
7527 -- of Null_Occurrence, and thus would find a warning about a
7528 -- missing return curious, and raising Program_Error does not
7529 -- seem such a bad behavior if this does occur.
7531 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7532 -- behavior will be to raise Constraint_Error (see AI-329).
7534 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7536 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7538 Raise_Exception_Call := True;
7540 -- For Raise_Exception call, test first argument, if it is
7541 -- an attribute reference for a 'Identity call, then we know
7542 -- that the call cannot possibly return.
7545 Arg : constant Node_Id :=
7546 Original_Node (First_Actual (Last_Stm));
7548 if Nkind (Arg) = N_Attribute_Reference
7549 and then Attribute_Name (Arg) = Name_Identity
7556 -- If statement, need to look inside if there is an else and check
7557 -- each constituent statement sequence for proper termination.
7559 elsif Kind = N_If_Statement
7560 and then Present (Else_Statements (Last_Stm))
7562 Check_Statement_Sequence (Then_Statements (Last_Stm));
7563 Check_Statement_Sequence (Else_Statements (Last_Stm));
7565 if Present (Elsif_Parts (Last_Stm)) then
7567 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7570 while Present (Elsif_Part) loop
7571 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7579 -- Case statement, check each case for proper termination
7581 elsif Kind = N_Case_Statement then
7585 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7586 while Present (Case_Alt) loop
7587 Check_Statement_Sequence (Statements (Case_Alt));
7588 Next_Non_Pragma (Case_Alt);
7594 -- Block statement, check its handled sequence of statements
7596 elsif Kind = N_Block_Statement then
7602 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7611 -- Loop statement. If there is an iteration scheme, we can definitely
7612 -- fall out of the loop. Similarly if there is an exit statement, we
7613 -- can fall out. In either case we need a following return.
7615 elsif Kind = N_Loop_Statement then
7616 if Present (Iteration_Scheme (Last_Stm))
7617 or else Has_Exit (Entity (Identifier (Last_Stm)))
7621 -- A loop with no exit statement or iteration scheme is either
7622 -- an infinite loop, or it has some other exit (raise/return).
7623 -- In either case, no warning is required.
7629 -- Timed entry call, check entry call and delay alternatives
7631 -- Note: in expanded code, the timed entry call has been converted
7632 -- to a set of expanded statements on which the check will work
7633 -- correctly in any case.
7635 elsif Kind = N_Timed_Entry_Call then
7637 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7638 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7641 -- If statement sequence of entry call alternative is missing,
7642 -- then we can definitely fall through, and we post the error
7643 -- message on the entry call alternative itself.
7645 if No (Statements (ECA)) then
7648 -- If statement sequence of delay alternative is missing, then
7649 -- we can definitely fall through, and we post the error
7650 -- message on the delay alternative itself.
7652 -- Note: if both ECA and DCA are missing the return, then we
7653 -- post only one message, should be enough to fix the bugs.
7654 -- If not we will get a message next time on the DCA when the
7657 elsif No (Statements (DCA)) then
7660 -- Else check both statement sequences
7663 Check_Statement_Sequence (Statements (ECA));
7664 Check_Statement_Sequence (Statements (DCA));
7669 -- Conditional entry call, check entry call and else part
7671 -- Note: in expanded code, the conditional entry call has been
7672 -- converted to a set of expanded statements on which the check
7673 -- will work correctly in any case.
7675 elsif Kind = N_Conditional_Entry_Call then
7677 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7680 -- If statement sequence of entry call alternative is missing,
7681 -- then we can definitely fall through, and we post the error
7682 -- message on the entry call alternative itself.
7684 if No (Statements (ECA)) then
7687 -- Else check statement sequence and else part
7690 Check_Statement_Sequence (Statements (ECA));
7691 Check_Statement_Sequence (Else_Statements (Last_Stm));
7697 -- If we fall through, issue appropriate message
7701 -- Kill warning if last statement is a raise exception call,
7702 -- or a pragma Assert (False). Note that with assertions enabled,
7703 -- such a pragma has been converted into a raise exception call
7704 -- already, so the Assert_False is for the assertions off case.
7706 if not Raise_Exception_Call and then not Assert_False then
7708 -- In GNATprove mode, it is an error to have a missing return
7710 Error_Msg_Warn := SPARK_Mode /= On;
7712 -- Issue error message or warning
7715 ("RETURN statement missing following this statement<<!",
7718 ("\Program_Error ]<<!", Last_Stm);
7721 -- Note: we set Err even though we have not issued a warning
7722 -- because we still have a case of a missing return. This is
7723 -- an extremely marginal case, probably will never be noticed
7724 -- but we might as well get it right.
7728 -- Otherwise we have the case of a procedure marked No_Return
7731 if not Raise_Exception_Call then
7732 if GNATprove_Mode then
7734 ("implied return after this statement would have raised "
7735 & "Program_Error", Last_Stm);
7737 -- In normal compilation mode, do not warn on a generated call
7738 -- (e.g. in the body of a renaming as completion).
7740 elsif Comes_From_Source (Last_Stm) then
7742 ("implied return after this statement will raise "
7743 & "Program_Error??", Last_Stm);
7746 Error_Msg_Warn := SPARK_Mode /= On;
7748 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7752 RE : constant Node_Id :=
7753 Make_Raise_Program_Error (Sloc (Last_Stm),
7754 Reason => PE_Implicit_Return);
7756 Insert_After (Last_Stm, RE);
7760 end Check_Statement_Sequence;
7762 -- Start of processing for Check_Returns
7766 Check_Statement_Sequence (Statements (HSS));
7768 if Present (Exception_Handlers (HSS)) then
7769 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7770 while Present (Handler) loop
7771 Check_Statement_Sequence (Statements (Handler));
7772 Next_Non_Pragma (Handler);
7777 ----------------------------
7778 -- Check_Subprogram_Order --
7779 ----------------------------
7781 procedure Check_Subprogram_Order (N : Node_Id) is
7783 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7784 -- This is used to check if S1 > S2 in the sense required by this test,
7785 -- for example nameab < namec, but name2 < name10.
7787 -----------------------------
7788 -- Subprogram_Name_Greater --
7789 -----------------------------
7791 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7796 -- Deal with special case where names are identical except for a
7797 -- numerical suffix. These are handled specially, taking the numeric
7798 -- ordering from the suffix into account.
7801 while S1 (L1) in '0' .. '9' loop
7806 while S2 (L2) in '0' .. '9' loop
7810 -- If non-numeric parts non-equal, do straight compare
7812 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7815 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7816 -- that a missing suffix is treated as numeric zero in this test.
7820 while L1 < S1'Last loop
7822 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7826 while L2 < S2'Last loop
7828 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7833 end Subprogram_Name_Greater;
7835 -- Start of processing for Check_Subprogram_Order
7838 -- Check body in alpha order if this is option
7841 and then Style_Check_Order_Subprograms
7842 and then Nkind (N) = N_Subprogram_Body
7843 and then Comes_From_Source (N)
7844 and then In_Extended_Main_Source_Unit (N)
7848 renames Scope_Stack.Table
7849 (Scope_Stack.Last).Last_Subprogram_Name;
7851 Body_Id : constant Entity_Id :=
7852 Defining_Entity (Specification (N));
7855 Get_Decoded_Name_String (Chars (Body_Id));
7858 if Subprogram_Name_Greater
7859 (LSN.all, Name_Buffer (1 .. Name_Len))
7861 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7867 LSN := new String'(Name_Buffer (1 .. Name_Len));
7870 end Check_Subprogram_Order;
7872 ------------------------------
7873 -- Check_Subtype_Conformant --
7874 ------------------------------
7876 procedure Check_Subtype_Conformant
7877 (New_Id : Entity_Id;
7879 Err_Loc : Node_Id := Empty;
7880 Skip_Controlling_Formals : Boolean := False;
7881 Get_Inst : Boolean := False)
7884 pragma Warnings (Off, Result);
7887 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7888 Skip_Controlling_Formals => Skip_Controlling_Formals,
7889 Get_Inst => Get_Inst);
7890 end Check_Subtype_Conformant;
7892 -----------------------------------
7893 -- Check_Synchronized_Overriding --
7894 -----------------------------------
7896 procedure Check_Synchronized_Overriding
7897 (Def_Id : Entity_Id;
7898 Overridden_Subp : out Entity_Id)
7900 Ifaces_List : Elist_Id;
7904 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7905 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7906 -- subprogram is implemented by a protected procedure or entry,
7907 -- its first parameter must be out, in out, or access-to-variable.
7909 function Matches_Prefixed_View_Profile
7910 (Prim_Params : List_Id;
7911 Iface_Params : List_Id) return Boolean;
7912 -- Determine whether a subprogram's parameter profile Prim_Params
7913 -- matches that of a potentially overridden interface subprogram
7914 -- Iface_Params. Also determine if the type of first parameter of
7915 -- Iface_Params is an implemented interface.
7917 ----------------------
7918 -- Is_Valid_Formal --
7919 ----------------------
7921 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7924 Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
7926 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7927 and then not Constant_Present (Parameter_Type (Parent (F))));
7928 end Is_Valid_Formal;
7930 -----------------------------------
7931 -- Matches_Prefixed_View_Profile --
7932 -----------------------------------
7934 function Matches_Prefixed_View_Profile
7935 (Prim_Params : List_Id;
7936 Iface_Params : List_Id) return Boolean
7938 function Is_Implemented
7939 (Ifaces_List : Elist_Id;
7940 Iface : Entity_Id) return Boolean;
7941 -- Determine if Iface is implemented by the current task or
7944 --------------------
7945 -- Is_Implemented --
7946 --------------------
7948 function Is_Implemented
7949 (Ifaces_List : Elist_Id;
7950 Iface : Entity_Id) return Boolean
7952 Iface_Elmt : Elmt_Id;
7955 Iface_Elmt := First_Elmt (Ifaces_List);
7956 while Present (Iface_Elmt) loop
7957 if Node (Iface_Elmt) = Iface then
7961 Next_Elmt (Iface_Elmt);
7969 Iface_Id : Entity_Id;
7970 Iface_Param : Node_Id;
7971 Iface_Typ : Entity_Id;
7972 Prim_Id : Entity_Id;
7973 Prim_Param : Node_Id;
7974 Prim_Typ : Entity_Id;
7976 -- Start of processing for Matches_Prefixed_View_Profile
7979 Iface_Param := First (Iface_Params);
7980 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7982 if Is_Access_Type (Iface_Typ) then
7983 Iface_Typ := Designated_Type (Iface_Typ);
7986 Prim_Param := First (Prim_Params);
7988 -- The first parameter of the potentially overridden subprogram must
7989 -- be an interface implemented by Prim.
7991 if not Is_Interface (Iface_Typ)
7992 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7997 -- The checks on the object parameters are done, so move on to the
7998 -- rest of the parameters.
8000 if not In_Scope then
8005 while Present (Iface_Param) and then Present (Prim_Param) loop
8006 Iface_Id := Defining_Identifier (Iface_Param);
8007 Iface_Typ := Find_Parameter_Type (Iface_Param);
8009 Prim_Id := Defining_Identifier (Prim_Param);
8010 Prim_Typ := Find_Parameter_Type (Prim_Param);
8012 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
8013 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
8014 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
8016 Iface_Typ := Designated_Type (Iface_Typ);
8017 Prim_Typ := Designated_Type (Prim_Typ);
8020 -- Case of multiple interface types inside a parameter profile
8022 -- (Obj_Param : in out Iface; ...; Param : Iface)
8024 -- If the interface type is implemented, then the matching type in
8025 -- the primitive should be the implementing record type.
8027 if Ekind (Iface_Typ) = E_Record_Type
8028 and then Is_Interface (Iface_Typ)
8029 and then Is_Implemented (Ifaces_List, Iface_Typ)
8031 if Prim_Typ /= Typ then
8035 -- The two parameters must be both mode and subtype conformant
8037 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
8039 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
8048 -- One of the two lists contains more parameters than the other
8050 if Present (Iface_Param) or else Present (Prim_Param) then
8055 end Matches_Prefixed_View_Profile;
8057 -- Start of processing for Check_Synchronized_Overriding
8060 Overridden_Subp := Empty;
8062 -- Def_Id must be an entry or a subprogram. We should skip predefined
8063 -- primitives internally generated by the front end; however at this
8064 -- stage predefined primitives are still not fully decorated. As a
8065 -- minor optimization we skip here internally generated subprograms.
8067 if (Ekind (Def_Id) /= E_Entry
8068 and then Ekind (Def_Id) /= E_Function
8069 and then Ekind (Def_Id) /= E_Procedure)
8070 or else not Comes_From_Source (Def_Id)
8075 -- Search for the concurrent declaration since it contains the list of
8076 -- all implemented interfaces. In this case, the subprogram is declared
8077 -- within the scope of a protected or a task type.
8079 if Present (Scope (Def_Id))
8080 and then Is_Concurrent_Type (Scope (Def_Id))
8081 and then not Is_Generic_Actual_Type (Scope (Def_Id))
8083 Typ := Scope (Def_Id);
8086 -- The enclosing scope is not a synchronized type and the subprogram
8089 elsif No (First_Formal (Def_Id)) then
8092 -- The subprogram has formals and hence it may be a primitive of a
8096 Typ := Etype (First_Formal (Def_Id));
8098 if Is_Access_Type (Typ) then
8099 Typ := Directly_Designated_Type (Typ);
8102 if Is_Concurrent_Type (Typ)
8103 and then not Is_Generic_Actual_Type (Typ)
8107 -- This case occurs when the concurrent type is declared within a
8108 -- generic unit. As a result the corresponding record has been built
8109 -- and used as the type of the first formal, we just have to retrieve
8110 -- the corresponding concurrent type.
8112 elsif Is_Concurrent_Record_Type (Typ)
8113 and then not Is_Class_Wide_Type (Typ)
8114 and then Present (Corresponding_Concurrent_Type (Typ))
8116 Typ := Corresponding_Concurrent_Type (Typ);
8124 -- There is no overriding to check if this is an inherited operation in
8125 -- a type derivation for a generic actual.
8127 Collect_Interfaces (Typ, Ifaces_List);
8129 if Is_Empty_Elmt_List (Ifaces_List) then
8133 -- Determine whether entry or subprogram Def_Id overrides a primitive
8134 -- operation that belongs to one of the interfaces in Ifaces_List.
8137 Candidate : Entity_Id := Empty;
8138 Hom : Entity_Id := Empty;
8139 Subp : Entity_Id := Empty;
8142 -- Traverse the homonym chain, looking for a potentially overridden
8143 -- subprogram that belongs to an implemented interface.
8145 Hom := Current_Entity_In_Scope (Def_Id);
8146 while Present (Hom) loop
8150 or else not Is_Overloadable (Subp)
8151 or else not Is_Primitive (Subp)
8152 or else not Is_Dispatching_Operation (Subp)
8153 or else not Present (Find_Dispatching_Type (Subp))
8154 or else not Is_Interface (Find_Dispatching_Type (Subp))
8158 -- Entries and procedures can override abstract or null interface
8161 elsif Ekind (Def_Id) in E_Entry | E_Procedure
8162 and then Ekind (Subp) = E_Procedure
8163 and then Matches_Prefixed_View_Profile
8164 (Parameter_Specifications (Parent (Def_Id)),
8165 Parameter_Specifications (Parent (Subp)))
8169 -- For an overridden subprogram Subp, check whether the mode
8170 -- of its first parameter is correct depending on the kind of
8171 -- synchronized type.
8174 Formal : constant Node_Id := First_Formal (Candidate);
8177 -- In order for an entry or a protected procedure to
8178 -- override, the first parameter of the overridden routine
8179 -- must be of mode "out", "in out", or access-to-variable.
8181 if Ekind (Candidate) in E_Entry | E_Procedure
8182 and then Is_Protected_Type (Typ)
8183 and then not Is_Valid_Formal (Formal)
8187 -- All other cases are OK since a task entry or routine does
8188 -- not have a restriction on the mode of the first parameter
8189 -- of the overridden interface routine.
8192 Overridden_Subp := Candidate;
8197 -- Functions can override abstract interface functions. Return
8198 -- types must be subtype conformant.
8200 elsif Ekind (Def_Id) = E_Function
8201 and then Ekind (Subp) = E_Function
8202 and then Matches_Prefixed_View_Profile
8203 (Parameter_Specifications (Parent (Def_Id)),
8204 Parameter_Specifications (Parent (Subp)))
8205 and then Conforming_Types
8206 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
8210 -- If an inherited subprogram is implemented by a protected
8211 -- function, then the first parameter of the inherited
8212 -- subprogram shall be of mode in, but not an access-to-
8213 -- variable parameter (RM 9.4(11/9)).
8215 if Present (First_Formal (Subp))
8216 and then Ekind (First_Formal (Subp)) = E_In_Parameter
8218 (not Is_Access_Type (Etype (First_Formal (Subp)))
8220 Is_Access_Constant (Etype (First_Formal (Subp))))
8222 Overridden_Subp := Subp;
8227 Hom := Homonym (Hom);
8230 -- After examining all candidates for overriding, we are left with
8231 -- the best match, which is a mode-incompatible interface routine.
8233 if In_Scope and then Present (Candidate) then
8234 Error_Msg_PT (Def_Id, Candidate);
8237 Overridden_Subp := Candidate;
8240 end Check_Synchronized_Overriding;
8242 ---------------------------
8243 -- Check_Type_Conformant --
8244 ---------------------------
8246 procedure Check_Type_Conformant
8247 (New_Id : Entity_Id;
8249 Err_Loc : Node_Id := Empty)
8252 pragma Warnings (Off, Result);
8255 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
8256 end Check_Type_Conformant;
8258 ---------------------------
8259 -- Can_Override_Operator --
8260 ---------------------------
8262 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
8266 if Nkind (Subp) /= N_Defining_Operator_Symbol then
8270 Typ := Base_Type (Etype (First_Formal (Subp)));
8272 -- Check explicitly that the operation is a primitive of the type
8274 return Operator_Matches_Spec (Subp, Subp)
8275 and then not Is_Generic_Type (Typ)
8276 and then Scope (Subp) = Scope (Typ)
8277 and then not Is_Class_Wide_Type (Typ);
8279 end Can_Override_Operator;
8281 ----------------------
8282 -- Conforming_Types --
8283 ----------------------
8285 function Conforming_Types
8288 Ctype : Conformance_Type;
8289 Get_Inst : Boolean := False) return Boolean
8291 function Base_Types_Match
8293 Typ_2 : Entity_Id) return Boolean;
8294 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
8295 -- in different scopes (e.g. parent and child instances), then verify
8296 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
8297 -- the same subtype chain. The whole purpose of this procedure is to
8298 -- prevent spurious ambiguities in an instantiation that may arise if
8299 -- two distinct generic types are instantiated with the same actual.
8301 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
8302 -- An access parameter can designate an incomplete type. If the
8303 -- incomplete type is the limited view of a type from a limited_
8304 -- with_clause, check whether the non-limited view is available.
8305 -- If it is a (non-limited) incomplete type, get the full view.
8307 function Matches_Limited_With_View
8309 Typ_2 : Entity_Id) return Boolean;
8310 -- Returns True if and only if either Typ_1 denotes a limited view of
8311 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
8312 -- the limited with view of a type is used in a subprogram declaration
8313 -- and the subprogram body is in the scope of a regular with clause for
8314 -- the same unit. In such a case, the two type entities are considered
8315 -- identical for purposes of conformance checking.
8317 ----------------------
8318 -- Base_Types_Match --
8319 ----------------------
8321 function Base_Types_Match
8323 Typ_2 : Entity_Id) return Boolean
8325 Base_1 : constant Entity_Id := Base_Type (Typ_1);
8326 Base_2 : constant Entity_Id := Base_Type (Typ_2);
8329 if Typ_1 = Typ_2 then
8332 elsif Base_1 = Base_2 then
8334 -- The following is too permissive. A more precise test should
8335 -- check that the generic actual is an ancestor subtype of the
8338 -- See code in Find_Corresponding_Spec that applies an additional
8339 -- filter to handle accidental amiguities in instances.
8342 not Is_Generic_Actual_Type (Typ_1)
8343 or else not Is_Generic_Actual_Type (Typ_2)
8344 or else Scope (Typ_1) /= Scope (Typ_2);
8346 -- If Typ_2 is a generic actual type it is declared as the subtype of
8347 -- the actual. If that actual is itself a subtype we need to use its
8348 -- own base type to check for compatibility.
8350 elsif Ekind (Base_2) = Ekind (Typ_2)
8351 and then Base_1 = Base_Type (Base_2)
8355 elsif Ekind (Base_1) = Ekind (Typ_1)
8356 and then Base_2 = Base_Type (Base_1)
8363 end Base_Types_Match;
8365 --------------------------
8366 -- Find_Designated_Type --
8367 --------------------------
8369 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
8373 Desig := Directly_Designated_Type (Typ);
8375 if Ekind (Desig) = E_Incomplete_Type then
8377 -- If regular incomplete type, get full view if available
8379 if Present (Full_View (Desig)) then
8380 Desig := Full_View (Desig);
8382 -- If limited view of a type, get non-limited view if available,
8383 -- and check again for a regular incomplete type.
8385 elsif Present (Non_Limited_View (Desig)) then
8386 Desig := Get_Full_View (Non_Limited_View (Desig));
8391 end Find_Designated_Type;
8393 -------------------------------
8394 -- Matches_Limited_With_View --
8395 -------------------------------
8397 function Matches_Limited_With_View
8399 Typ_2 : Entity_Id) return Boolean
8401 function Is_Matching_Limited_View
8403 View : Entity_Id) return Boolean;
8404 -- Determine whether non-limited view View denotes type Typ in some
8405 -- conformant fashion.
8407 ------------------------------
8408 -- Is_Matching_Limited_View --
8409 ------------------------------
8411 function Is_Matching_Limited_View
8413 View : Entity_Id) return Boolean
8415 Root_Typ : Entity_Id;
8416 Root_View : Entity_Id;
8419 -- The non-limited view directly denotes the type
8424 -- The type is a subtype of the non-limited view
8426 elsif Is_Subtype_Of (Typ, View) then
8429 -- Both the non-limited view and the type denote class-wide types
8431 elsif Is_Class_Wide_Type (Typ)
8432 and then Is_Class_Wide_Type (View)
8434 Root_Typ := Root_Type (Typ);
8435 Root_View := Root_Type (View);
8437 if Root_Typ = Root_View then
8440 -- An incomplete tagged type and its full view may receive two
8441 -- distinct class-wide types when the related package has not
8442 -- been analyzed yet.
8445 -- type T is tagged; -- CW_1
8446 -- type T is tagged null record; -- CW_2
8449 -- This is because the package lacks any semantic information
8450 -- that may eventually link both views of T. As a consequence,
8451 -- a client of the limited view of Pack will see CW_2 while a
8452 -- client of the non-limited view of Pack will see CW_1.
8454 elsif Is_Incomplete_Type (Root_Typ)
8455 and then Present (Full_View (Root_Typ))
8456 and then Full_View (Root_Typ) = Root_View
8460 elsif Is_Incomplete_Type (Root_View)
8461 and then Present (Full_View (Root_View))
8462 and then Full_View (Root_View) = Root_Typ
8469 end Is_Matching_Limited_View;
8471 -- Start of processing for Matches_Limited_With_View
8474 -- In some cases a type imported through a limited_with clause, and
8475 -- its non-limited view are both visible, for example in an anonymous
8476 -- access-to-class-wide type in a formal, or when building the body
8477 -- for a subprogram renaming after the subprogram has been frozen.
8478 -- In these cases both entities designate the same type. In addition,
8479 -- if one of them is an actual in an instance, it may be a subtype of
8480 -- the non-limited view of the other.
8482 if From_Limited_With (Typ_1)
8483 and then From_Limited_With (Typ_2)
8484 and then Available_View (Typ_1) = Available_View (Typ_2)
8488 elsif From_Limited_With (Typ_1) then
8489 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8491 elsif From_Limited_With (Typ_2) then
8492 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8497 end Matches_Limited_With_View;
8501 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8503 Type_1 : Entity_Id := T1;
8504 Type_2 : Entity_Id := T2;
8506 -- Start of processing for Conforming_Types
8509 -- The context is an instance association for a formal access-to-
8510 -- subprogram type; the formal parameter types require mapping because
8511 -- they may denote other formal parameters of the generic unit.
8514 Type_1 := Get_Instance_Of (T1);
8515 Type_2 := Get_Instance_Of (T2);
8518 -- If one of the types is a view of the other introduced by a limited
8519 -- with clause, treat these as conforming for all purposes.
8521 if Matches_Limited_With_View (T1, T2) then
8524 elsif Base_Types_Match (Type_1, Type_2) then
8525 if Ctype <= Mode_Conformant then
8530 Subtypes_Statically_Match (Type_1, Type_2)
8531 and then Dimensions_Match (Type_1, Type_2);
8534 elsif Is_Incomplete_Or_Private_Type (Type_1)
8535 and then Present (Full_View (Type_1))
8536 and then Base_Types_Match (Full_View (Type_1), Type_2)
8539 Ctype <= Mode_Conformant
8540 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8542 elsif Ekind (Type_2) = E_Incomplete_Type
8543 and then Present (Full_View (Type_2))
8544 and then Base_Types_Match (Type_1, Full_View (Type_2))
8547 Ctype <= Mode_Conformant
8548 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8550 elsif Is_Private_Type (Type_2)
8551 and then In_Instance
8552 and then Present (Full_View (Type_2))
8553 and then Base_Types_Match (Type_1, Full_View (Type_2))
8556 Ctype <= Mode_Conformant
8557 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8559 -- Another confusion between views in a nested instance with an
8560 -- actual private type whose full view is not in scope.
8562 elsif Ekind (Type_2) = E_Private_Subtype
8563 and then In_Instance
8564 and then Etype (Type_2) = Type_1
8568 -- In Ada 2012, incomplete types (including limited views) can appear
8569 -- as actuals in instantiations, where they are conformant to the
8570 -- corresponding incomplete formal.
8572 elsif Is_Incomplete_Type (Type_1)
8573 and then Is_Incomplete_Type (Type_2)
8574 and then In_Instance
8575 and then (Used_As_Generic_Actual (Type_1)
8576 or else Used_As_Generic_Actual (Type_2))
8581 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8582 -- treated recursively because they carry a signature. As far as
8583 -- conformance is concerned, convention plays no role, and either
8584 -- or both could be access to protected subprograms.
8586 Are_Anonymous_Access_To_Subprogram_Types :=
8587 Ekind (Type_1) in E_Anonymous_Access_Subprogram_Type
8588 | E_Anonymous_Access_Protected_Subprogram_Type
8590 Ekind (Type_2) in E_Anonymous_Access_Subprogram_Type
8591 | E_Anonymous_Access_Protected_Subprogram_Type;
8593 -- Test anonymous access type case. For this case, static subtype
8594 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8595 -- the base types because we may have built internal subtype entities
8596 -- to handle null-excluding types (see Process_Formals).
8598 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8600 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8602 -- Ada 2005 (AI-254)
8604 or else Are_Anonymous_Access_To_Subprogram_Types
8607 Desig_1 : Entity_Id;
8608 Desig_2 : Entity_Id;
8611 -- In Ada 2005, access constant indicators must match for
8612 -- subtype conformance.
8614 if Ada_Version >= Ada_2005
8615 and then Ctype >= Subtype_Conformant
8617 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8622 Desig_1 := Find_Designated_Type (Type_1);
8623 Desig_2 := Find_Designated_Type (Type_2);
8625 -- If the context is an instance association for a formal
8626 -- access-to-subprogram type; formal access parameter designated
8627 -- types require mapping because they may denote other formal
8628 -- parameters of the generic unit.
8631 Desig_1 := Get_Instance_Of (Desig_1);
8632 Desig_2 := Get_Instance_Of (Desig_2);
8635 -- It is possible for a Class_Wide_Type to be introduced for an
8636 -- incomplete type, in which case there is a separate class_ wide
8637 -- type for the full view. The types conform if their Etypes
8638 -- conform, i.e. one may be the full view of the other. This can
8639 -- only happen in the context of an access parameter, other uses
8640 -- of an incomplete Class_Wide_Type are illegal.
8642 if Is_Class_Wide_Type (Desig_1)
8644 Is_Class_Wide_Type (Desig_2)
8648 (Etype (Base_Type (Desig_1)),
8649 Etype (Base_Type (Desig_2)), Ctype);
8651 elsif Are_Anonymous_Access_To_Subprogram_Types then
8652 if Ada_Version < Ada_2005 then
8654 Ctype = Type_Conformant
8655 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8657 -- We must check the conformance of the signatures themselves
8661 Conformant : Boolean;
8664 (Desig_1, Desig_2, Ctype, False, Conformant);
8669 -- A limited view of an actual matches the corresponding
8670 -- incomplete formal.
8672 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8673 and then From_Limited_With (Desig_2)
8674 and then Used_As_Generic_Actual (Etype (Desig_2))
8679 return Base_Type (Desig_1) = Base_Type (Desig_2)
8680 and then (Ctype = Type_Conformant
8682 Subtypes_Statically_Match (Desig_1, Desig_2));
8686 -- Otherwise definitely no match
8689 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8690 and then Is_Access_Type (Type_2))
8691 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8692 and then Is_Access_Type (Type_1)))
8695 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8697 May_Hide_Profile := True;
8702 end Conforming_Types;
8704 --------------------------
8705 -- Create_Extra_Formals --
8706 --------------------------
8708 procedure Create_Extra_Formals (E : Entity_Id) is
8709 First_Extra : Entity_Id := Empty;
8711 Last_Extra : Entity_Id := Empty;
8713 function Add_Extra_Formal
8714 (Assoc_Entity : Entity_Id;
8717 Suffix : String) return Entity_Id;
8718 -- Add an extra formal to the current list of formals and extra formals.
8719 -- The extra formal is added to the end of the list of extra formals,
8720 -- and also returned as the result. These formals are always of mode IN.
8721 -- The new formal has the type Typ, is declared in Scope, and its name
8722 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8723 -- The following suffixes are currently used. They should not be changed
8724 -- without coordinating with CodePeer, which makes use of these to
8725 -- provide better messages.
8727 -- O denotes the Constrained bit.
8728 -- L denotes the accessibility level.
8729 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8730 -- the full list in exp_ch6.BIP_Formal_Kind.
8732 ----------------------
8733 -- Add_Extra_Formal --
8734 ----------------------
8736 function Add_Extra_Formal
8737 (Assoc_Entity : Entity_Id;
8740 Suffix : String) return Entity_Id
8742 EF : constant Entity_Id :=
8743 Make_Defining_Identifier (Sloc (Assoc_Entity),
8744 Chars => New_External_Name (Chars (Assoc_Entity),
8748 -- A little optimization. Never generate an extra formal for the
8749 -- _init operand of an initialization procedure, since it could
8752 if Chars (Formal) = Name_uInit then
8756 Set_Ekind (EF, E_In_Parameter);
8757 Set_Actual_Subtype (EF, Typ);
8758 Set_Etype (EF, Typ);
8759 Set_Scope (EF, Scope);
8760 Set_Mechanism (EF, Default_Mechanism);
8761 Set_Formal_Validity (EF);
8763 if No (First_Extra) then
8765 Set_Extra_Formals (Scope, EF);
8768 if Present (Last_Extra) then
8769 Set_Extra_Formal (Last_Extra, EF);
8775 end Add_Extra_Formal;
8779 Formal_Type : Entity_Id;
8780 P_Formal : Entity_Id := Empty;
8782 -- Start of processing for Create_Extra_Formals
8785 -- We never generate extra formals if expansion is not active because we
8786 -- don't need them unless we are generating code.
8788 if not Expander_Active then
8792 -- No need to generate extra formals in interface thunks whose target
8793 -- primitive has no extra formals.
8795 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8799 -- If this is a derived subprogram then the subtypes of the parent
8800 -- subprogram's formal parameters will be used to determine the need
8801 -- for extra formals.
8803 if Is_Overloadable (E) and then Present (Alias (E)) then
8804 P_Formal := First_Formal (Alias (E));
8807 Formal := First_Formal (E);
8808 while Present (Formal) loop
8809 Last_Extra := Formal;
8810 Next_Formal (Formal);
8813 -- If Extra_Formals were already created, don't do it again. This
8814 -- situation may arise for subprogram types created as part of
8815 -- dispatching calls (see Expand_Dispatching_Call).
8817 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8821 -- If the subprogram is a predefined dispatching subprogram then don't
8822 -- generate any extra constrained or accessibility level formals. In
8823 -- general we suppress these for internal subprograms (by not calling
8824 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8825 -- generated stream attributes do get passed through because extra
8826 -- build-in-place formals are needed in some cases (limited 'Input).
8828 if Is_Predefined_Internal_Operation (E) then
8829 goto Test_For_Func_Result_Extras;
8832 Formal := First_Formal (E);
8833 while Present (Formal) loop
8835 -- Create extra formal for supporting the attribute 'Constrained.
8836 -- The case of a private type view without discriminants also
8837 -- requires the extra formal if the underlying type has defaulted
8840 if Ekind (Formal) /= E_In_Parameter then
8841 if Present (P_Formal) then
8842 Formal_Type := Etype (P_Formal);
8844 Formal_Type := Etype (Formal);
8847 -- Do not produce extra formals for Unchecked_Union parameters.
8848 -- Jump directly to the end of the loop.
8850 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8851 goto Skip_Extra_Formal_Generation;
8854 if not Has_Discriminants (Formal_Type)
8855 and then Ekind (Formal_Type) in Private_Kind
8856 and then Present (Underlying_Type (Formal_Type))
8858 Formal_Type := Underlying_Type (Formal_Type);
8861 -- Suppress the extra formal if formal's subtype is constrained or
8862 -- indefinite, or we're compiling for Ada 2012 and the underlying
8863 -- type is tagged and limited. In Ada 2012, a limited tagged type
8864 -- can have defaulted discriminants, but 'Constrained is required
8865 -- to return True, so the formal is never needed (see AI05-0214).
8866 -- Note that this ensures consistency of calling sequences for
8867 -- dispatching operations when some types in a class have defaults
8868 -- on discriminants and others do not (and requiring the extra
8869 -- formal would introduce distributed overhead).
8871 -- If the type does not have a completion yet, treat as prior to
8872 -- Ada 2012 for consistency.
8874 if Has_Discriminants (Formal_Type)
8875 and then not Is_Constrained (Formal_Type)
8876 and then Is_Definite_Subtype (Formal_Type)
8877 and then (Ada_Version < Ada_2012
8878 or else No (Underlying_Type (Formal_Type))
8880 (Is_Limited_Type (Formal_Type)
8883 (Underlying_Type (Formal_Type)))))
8885 Set_Extra_Constrained
8886 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8890 -- Create extra formal for supporting accessibility checking. This
8891 -- is done for both anonymous access formals and formals of named
8892 -- access types that are marked as controlling formals. The latter
8893 -- case can occur when Expand_Dispatching_Call creates a subprogram
8894 -- type and substitutes the types of access-to-class-wide actuals
8895 -- for the anonymous access-to-specific-type of controlling formals.
8896 -- Base_Type is applied because in cases where there is a null
8897 -- exclusion the formal may have an access subtype.
8899 -- This is suppressed if we specifically suppress accessibility
8900 -- checks at the package level for either the subprogram, or the
8901 -- package in which it resides. However, we do not suppress it
8902 -- simply if the scope has accessibility checks suppressed, since
8903 -- this could cause trouble when clients are compiled with a
8904 -- different suppression setting. The explicit checks at the
8905 -- package level are safe from this point of view.
8907 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8908 or else (Is_Controlling_Formal (Formal)
8909 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8911 (Explicit_Suppress (E, Accessibility_Check)
8913 Explicit_Suppress (Scope (E), Accessibility_Check))
8916 or else Present (Extra_Accessibility (P_Formal)))
8918 Set_Extra_Accessibility
8919 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8922 -- This label is required when skipping extra formal generation for
8923 -- Unchecked_Union parameters.
8925 <<Skip_Extra_Formal_Generation>>
8927 if Present (P_Formal) then
8928 Next_Formal (P_Formal);
8931 Next_Formal (Formal);
8934 <<Test_For_Func_Result_Extras>>
8936 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8937 -- function call is ... determined by the point of call ...".
8939 if Needs_Result_Accessibility_Level (E) then
8940 Set_Extra_Accessibility_Of_Result
8941 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8944 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8945 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8947 if Is_Build_In_Place_Function (E) then
8949 Result_Subt : constant Entity_Id := Etype (E);
8950 Formal_Typ : Entity_Id;
8951 Subp_Decl : Node_Id;
8952 Discard : Entity_Id;
8955 -- In the case of functions with unconstrained result subtypes,
8956 -- add a 4-state formal indicating whether the return object is
8957 -- allocated by the caller (1), or should be allocated by the
8958 -- callee on the secondary stack (2), in the global heap (3), or
8959 -- in a user-defined storage pool (4). For the moment we just use
8960 -- Natural for the type of this formal. Note that this formal
8961 -- isn't usually needed in the case where the result subtype is
8962 -- constrained, but it is needed when the function has a tagged
8963 -- result, because generally such functions can be called in a
8964 -- dispatching context and such calls must be handled like calls
8965 -- to a class-wide function.
8967 if Needs_BIP_Alloc_Form (E) then
8970 (E, Standard_Natural,
8971 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8973 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8974 -- use a user-defined pool. This formal is not added on
8975 -- ZFP as those targets do not support pools.
8977 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8980 (E, RTE (RE_Root_Storage_Pool_Ptr),
8981 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8985 -- In the case of functions whose result type needs finalization,
8986 -- add an extra formal which represents the finalization master.
8988 if Needs_BIP_Finalization_Master (E) then
8991 (E, RTE (RE_Finalization_Master_Ptr),
8992 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8995 -- When the result type contains tasks, add two extra formals: the
8996 -- master of the tasks to be created, and the caller's activation
8999 if Needs_BIP_Task_Actuals (E) then
9002 (E, RTE (RE_Master_Id),
9003 E, BIP_Formal_Suffix (BIP_Task_Master));
9005 Set_Has_Master_Entity (E);
9009 (E, RTE (RE_Activation_Chain_Access),
9010 E, BIP_Formal_Suffix (BIP_Activation_Chain));
9013 -- All build-in-place functions get an extra formal that will be
9014 -- passed the address of the return object within the caller.
9017 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
9019 -- Incomplete_View_From_Limited_With is needed here because
9020 -- gigi gets confused if the designated type is the full view
9021 -- coming from a limited-with'ed package. In the normal case,
9022 -- (no limited with) Incomplete_View_From_Limited_With
9023 -- returns Result_Subt.
9025 Set_Directly_Designated_Type
9026 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
9027 Set_Etype (Formal_Typ, Formal_Typ);
9028 Set_Depends_On_Private
9029 (Formal_Typ, Has_Private_Component (Formal_Typ));
9030 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
9031 Set_Is_Access_Constant (Formal_Typ, False);
9033 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
9034 -- the designated type comes from the limited view (for back-end
9037 Set_From_Limited_With
9038 (Formal_Typ, From_Limited_With (Result_Subt));
9040 Layout_Type (Formal_Typ);
9042 -- Force the definition of the Itype in case of internal function
9043 -- calls within the same or nested scope.
9045 if Is_Subprogram_Or_Generic_Subprogram (E) then
9046 Subp_Decl := Parent (E);
9048 -- The insertion point for an Itype reference should be after
9049 -- the unit declaration node of the subprogram. An exception
9050 -- to this are inherited operations from a parent type in which
9051 -- case the derived type acts as their parent.
9053 if Nkind (Subp_Decl) in N_Function_Specification
9054 | N_Procedure_Specification
9056 Subp_Decl := Parent (Subp_Decl);
9059 Build_Itype_Reference (Formal_Typ, Subp_Decl);
9064 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
9068 -- If this is an instance of a generic, we need to have extra formals
9071 if Is_Generic_Instance (E) and then Present (Alias (E)) then
9072 Set_Extra_Formals (Alias (E), Extra_Formals (E));
9074 end Create_Extra_Formals;
9076 -----------------------------
9077 -- Enter_Overloaded_Entity --
9078 -----------------------------
9080 procedure Enter_Overloaded_Entity (S : Entity_Id) is
9081 function Matches_Predefined_Op return Boolean;
9082 -- This returns an approximation of whether S matches a predefined
9083 -- operator, based on the operator symbol, and the parameter and result
9084 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
9086 ---------------------------
9087 -- Matches_Predefined_Op --
9088 ---------------------------
9090 function Matches_Predefined_Op return Boolean is
9091 Formal_1 : constant Entity_Id := First_Formal (S);
9092 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
9093 Op : constant Name_Id := Chars (S);
9094 Result_Type : constant Entity_Id := Base_Type (Etype (S));
9095 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
9100 if Present (Formal_2) then
9102 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
9105 -- All but "&" and "**" have same-types parameters
9114 if Type_1 /= Type_2 then
9119 -- Check parameter and result types
9127 Is_Boolean_Type (Result_Type)
9128 and then Result_Type = Type_1;
9134 Is_Integer_Type (Result_Type)
9135 and then Result_Type = Type_1;
9143 Is_Numeric_Type (Result_Type)
9144 and then Result_Type = Type_1;
9150 Is_Boolean_Type (Result_Type)
9151 and then not Is_Limited_Type (Type_1);
9159 Is_Boolean_Type (Result_Type)
9160 and then (Is_Array_Type (Type_1)
9161 or else Is_Scalar_Type (Type_1));
9163 when Name_Op_Concat =>
9164 return Is_Array_Type (Result_Type);
9166 when Name_Op_Expon =>
9168 (Is_Integer_Type (Result_Type)
9169 or else Is_Floating_Point_Type (Result_Type))
9170 and then Result_Type = Type_1
9171 and then Type_2 = Standard_Integer;
9174 raise Program_Error;
9187 Is_Numeric_Type (Result_Type)
9188 and then Result_Type = Type_1;
9192 Is_Boolean_Type (Result_Type)
9193 and then Result_Type = Type_1;
9196 raise Program_Error;
9199 end Matches_Predefined_Op;
9203 E : Entity_Id := Current_Entity_In_Scope (S);
9204 C_E : Entity_Id := Current_Entity (S);
9206 -- Start of processing for Enter_Overloaded_Entity
9210 Set_Has_Homonym (E);
9211 Set_Has_Homonym (S);
9214 Set_Is_Immediately_Visible (S);
9215 Set_Scope (S, Current_Scope);
9217 -- Chain new entity if front of homonym in current scope, so that
9218 -- homonyms are contiguous.
9220 if Present (E) and then E /= C_E then
9221 while Homonym (C_E) /= E loop
9222 C_E := Homonym (C_E);
9225 Set_Homonym (C_E, S);
9229 Set_Current_Entity (S);
9234 if Is_Inherited_Operation (S) then
9235 Append_Inherited_Subprogram (S);
9237 Append_Entity (S, Current_Scope);
9240 Set_Public_Status (S);
9242 if Debug_Flag_E then
9243 Write_Str ("New overloaded entity chain: ");
9244 Write_Name (Chars (S));
9247 while Present (E) loop
9248 Write_Str (" "); Write_Int (Int (E));
9255 -- Generate warning for hiding
9258 and then Comes_From_Source (S)
9259 and then In_Extended_Main_Source_Unit (S)
9266 -- Warn unless genuine overloading. Do not emit warning on
9267 -- hiding predefined operators in Standard (these are either an
9268 -- artifact of our implicit declarations, or simple noise) but
9269 -- keep warning on a operator defined on a local subtype, because
9270 -- of the real danger that different operators may be applied in
9271 -- various parts of the program.
9273 -- Note that if E and S have the same scope, there is never any
9274 -- hiding. Either the two conflict, and the program is illegal,
9275 -- or S is overriding an implicit inherited subprogram.
9277 if Scope (E) /= Scope (S)
9278 and then (not Is_Overloadable (E)
9279 or else Subtype_Conformant (E, S))
9280 and then (Is_Immediately_Visible (E)
9281 or else Is_Potentially_Use_Visible (S))
9283 if Scope (E) = Standard_Standard then
9284 if Nkind (S) = N_Defining_Operator_Symbol
9285 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
9287 and then Matches_Predefined_Op
9290 ("declaration of & hides predefined operator?h?", S);
9293 -- E not immediately within Standard
9296 Error_Msg_Sloc := Sloc (E);
9297 Error_Msg_N ("declaration of & hides one #?h?", S);
9302 end Enter_Overloaded_Entity;
9304 -----------------------------
9305 -- Check_Untagged_Equality --
9306 -----------------------------
9308 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
9309 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
9310 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
9314 -- This check applies only if we have a subprogram declaration with an
9315 -- untagged record type that is conformant to the predefined op.
9317 if Nkind (Decl) /= N_Subprogram_Declaration
9318 or else not Is_Record_Type (Typ)
9319 or else Is_Tagged_Type (Typ)
9320 or else Etype (Next_Formal (First_Formal (Eq_Op))) /= Typ
9325 -- In Ada 2012 case, we will output errors or warnings depending on
9326 -- the setting of debug flag -gnatd.E.
9328 if Ada_Version >= Ada_2012 then
9329 Error_Msg_Warn := Debug_Flag_Dot_EE;
9331 -- In earlier versions of Ada, nothing to do unless we are warning on
9332 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
9335 if not Warn_On_Ada_2012_Compatibility then
9340 -- Cases where the type has already been frozen
9342 if Is_Frozen (Typ) then
9344 -- The check applies to a primitive operation, so check that type
9345 -- and equality operation are in the same scope.
9347 if Scope (Typ) /= Current_Scope then
9350 -- If the type is a generic actual (sub)type, the operation is not
9351 -- primitive either because the base type is declared elsewhere.
9353 elsif Is_Generic_Actual_Type (Typ) then
9356 -- Here we have a definite error of declaration after freezing
9359 if Ada_Version >= Ada_2012 then
9361 ("equality operator must be declared before type & is "
9362 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
9364 -- In Ada 2012 mode with error turned to warning, output one
9365 -- more warning to warn that the equality operation may not
9366 -- compose. This is the consequence of ignoring the error.
9368 if Error_Msg_Warn then
9369 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
9374 ("equality operator must be declared before type& is "
9375 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
9378 -- If we are in the package body, we could just move the
9379 -- declaration to the package spec, so add a message saying that.
9381 if In_Package_Body (Scope (Typ)) then
9382 if Ada_Version >= Ada_2012 then
9384 ("\move declaration to package spec<<", Eq_Op);
9387 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
9390 -- Otherwise try to find the freezing point for better message.
9393 Obj_Decl := Next (Parent (Typ));
9394 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9395 if Nkind (Obj_Decl) = N_Object_Declaration
9396 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9398 -- Freezing point, output warnings
9400 if Ada_Version >= Ada_2012 then
9402 ("type& is frozen by declaration??", Obj_Decl, Typ);
9404 ("\an equality operator cannot be declared after "
9409 ("type& is frozen by declaration (Ada 2012)?y?",
9412 ("\an equality operator cannot be declared after "
9413 & "this point (Ada 2012)?y?",
9419 -- If we reach generated code for subprogram declaration
9420 -- or body, it is the body that froze the type and the
9421 -- declaration is legal.
9423 elsif Sloc (Obj_Decl) = Sloc (Decl) then
9432 -- Here if type is not frozen yet. It is illegal to have a primitive
9433 -- equality declared in the private part if the type is visible.
9435 elsif not In_Same_List (Parent (Typ), Decl)
9436 and then not Is_Limited_Type (Typ)
9438 -- Shouldn't we give an RM reference here???
9440 if Ada_Version >= Ada_2012 then
9442 ("equality operator appears too late<<", Eq_Op);
9445 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
9448 -- Finally check for AI12-0352: declaration of a user-defined primitive
9449 -- equality operation for a record type T is illegal if it occurs after
9450 -- a type has been derived from T.
9453 Obj_Decl := Next (Parent (Typ));
9455 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9456 if Nkind (Obj_Decl) = N_Full_Type_Declaration
9457 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9460 ("equality operator cannot appear after derivation", Eq_Op);
9462 ("an equality operator for& cannot be declared after "
9470 end Check_Untagged_Equality;
9472 -----------------------------
9473 -- Find_Corresponding_Spec --
9474 -----------------------------
9476 function Find_Corresponding_Spec
9478 Post_Error : Boolean := True) return Entity_Id
9480 Spec : constant Node_Id := Specification (N);
9481 Designator : constant Entity_Id := Defining_Entity (Spec);
9485 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9486 -- Even if fully conformant, a body may depend on a generic actual when
9487 -- the spec does not, or vice versa, in which case they were distinct
9488 -- entities in the generic.
9490 -------------------------------
9491 -- Different_Generic_Profile --
9492 -------------------------------
9494 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9497 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9498 -- Check that the types of corresponding formals have the same
9499 -- generic actual if any. We have to account for subtypes of a
9500 -- generic formal, declared between a spec and a body, which may
9501 -- appear distinct in an instance but matched in the generic, and
9502 -- the subtype may be used either in the spec or the body of the
9503 -- subprogram being checked.
9505 -------------------------
9506 -- Same_Generic_Actual --
9507 -------------------------
9509 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9511 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9512 -- Predicate to check whether S1 is a subtype of S2 in the source
9515 -------------------------
9516 -- Is_Declared_Subtype --
9517 -------------------------
9519 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9521 return Comes_From_Source (Parent (S1))
9522 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9523 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9524 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9525 end Is_Declared_Subtype;
9527 -- Start of processing for Same_Generic_Actual
9530 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9531 or else Is_Declared_Subtype (T1, T2)
9532 or else Is_Declared_Subtype (T2, T1);
9533 end Same_Generic_Actual;
9535 -- Start of processing for Different_Generic_Profile
9538 if not In_Instance then
9541 elsif Ekind (E) = E_Function
9542 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9547 F1 := First_Formal (Designator);
9548 F2 := First_Formal (E);
9549 while Present (F1) loop
9550 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9559 end Different_Generic_Profile;
9561 -- Start of processing for Find_Corresponding_Spec
9564 E := Current_Entity (Designator);
9565 while Present (E) loop
9567 -- We are looking for a matching spec. It must have the same scope,
9568 -- and the same name, and either be type conformant, or be the case
9569 -- of a library procedure spec and its body (which belong to one
9570 -- another regardless of whether they are type conformant or not).
9572 if Scope (E) = Current_Scope then
9573 if Current_Scope = Standard_Standard
9574 or else (Ekind (E) = Ekind (Designator)
9575 and then Type_Conformant (E, Designator))
9577 -- Within an instantiation, we know that spec and body are
9578 -- subtype conformant, because they were subtype conformant in
9579 -- the generic. We choose the subtype-conformant entity here as
9580 -- well, to resolve spurious ambiguities in the instance that
9581 -- were not present in the generic (i.e. when two different
9582 -- types are given the same actual). If we are looking for a
9583 -- spec to match a body, full conformance is expected.
9587 -- Inherit the convention and "ghostness" of the matching
9588 -- spec to ensure proper full and subtype conformance.
9590 Set_Convention (Designator, Convention (E));
9592 -- Skip past subprogram bodies and subprogram renamings that
9593 -- may appear to have a matching spec, but that aren't fully
9594 -- conformant with it. That can occur in cases where an
9595 -- actual type causes unrelated homographs in the instance.
9597 if Nkind (N) in N_Subprogram_Body
9598 | N_Subprogram_Renaming_Declaration
9599 and then Present (Homonym (E))
9600 and then not Fully_Conformant (Designator, E)
9604 elsif not Subtype_Conformant (Designator, E) then
9607 elsif Different_Generic_Profile (E) then
9612 -- Ada 2012 (AI05-0165): For internally generated bodies of
9613 -- null procedures locate the internally generated spec. We
9614 -- enforce mode conformance since a tagged type may inherit
9615 -- from interfaces several null primitives which differ only
9616 -- in the mode of the formals.
9618 if not (Comes_From_Source (E))
9619 and then Is_Null_Procedure (E)
9620 and then not Mode_Conformant (Designator, E)
9624 -- For null procedures coming from source that are completions,
9625 -- analysis of the generated body will establish the link.
9627 elsif Comes_From_Source (E)
9628 and then Nkind (Spec) = N_Procedure_Specification
9629 and then Null_Present (Spec)
9633 -- Expression functions can be completions, but cannot be
9634 -- completed by an explicit body.
9636 elsif Comes_From_Source (E)
9637 and then Comes_From_Source (N)
9638 and then Nkind (N) = N_Subprogram_Body
9639 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9640 N_Expression_Function
9642 Error_Msg_Sloc := Sloc (E);
9643 Error_Msg_N ("body conflicts with expression function#", N);
9646 elsif not Has_Completion (E) then
9647 if Nkind (N) /= N_Subprogram_Body_Stub then
9648 Set_Corresponding_Spec (N, E);
9651 Set_Has_Completion (E);
9654 elsif Nkind (Parent (N)) = N_Subunit then
9656 -- If this is the proper body of a subunit, the completion
9657 -- flag is set when analyzing the stub.
9661 -- If E is an internal function with a controlling result that
9662 -- was created for an operation inherited by a null extension,
9663 -- it may be overridden by a body without a previous spec (one
9664 -- more reason why these should be shunned). In that case we
9665 -- remove the generated body if present, because the current
9666 -- one is the explicit overriding.
9668 elsif Ekind (E) = E_Function
9669 and then Ada_Version >= Ada_2005
9670 and then not Comes_From_Source (E)
9671 and then Has_Controlling_Result (E)
9672 and then Is_Null_Extension (Etype (E))
9673 and then Comes_From_Source (Spec)
9675 Set_Has_Completion (E, False);
9678 and then Nkind (Parent (E)) = N_Function_Specification
9681 (Unit_Declaration_Node
9682 (Corresponding_Body (Unit_Declaration_Node (E))));
9686 -- If expansion is disabled, or if the wrapper function has
9687 -- not been generated yet, this a late body overriding an
9688 -- inherited operation, or it is an overriding by some other
9689 -- declaration before the controlling result is frozen. In
9690 -- either case this is a declaration of a new entity.
9696 -- If the body already exists, then this is an error unless
9697 -- the previous declaration is the implicit declaration of a
9698 -- derived subprogram. It is also legal for an instance to
9699 -- contain type conformant overloadable declarations (but the
9700 -- generic declaration may not), per 8.3(26/2).
9702 elsif No (Alias (E))
9703 and then not Is_Intrinsic_Subprogram (E)
9704 and then not In_Instance
9707 Error_Msg_Sloc := Sloc (E);
9709 if Is_Imported (E) then
9711 ("body not allowed for imported subprogram & declared#",
9714 Error_Msg_NE ("duplicate body for & declared#", N, E);
9718 -- Child units cannot be overloaded, so a conformance mismatch
9719 -- between body and a previous spec is an error.
9721 elsif Is_Child_Unit (E)
9723 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9725 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9730 ("body of child unit does not match previous declaration", N);
9738 -- On exit, we know that no previous declaration of subprogram exists
9741 end Find_Corresponding_Spec;
9743 ----------------------
9744 -- Fully_Conformant --
9745 ----------------------
9747 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9750 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9752 end Fully_Conformant;
9754 ----------------------------------
9755 -- Fully_Conformant_Expressions --
9756 ----------------------------------
9758 function Fully_Conformant_Expressions
9759 (Given_E1 : Node_Id;
9761 Report : Boolean := False) return Boolean
9763 E1 : constant Node_Id := Original_Node (Given_E1);
9764 E2 : constant Node_Id := Original_Node (Given_E2);
9765 -- We always test conformance on original nodes, since it is possible
9766 -- for analysis and/or expansion to make things look as though they
9767 -- conform when they do not, e.g. by converting 1+2 into 3.
9769 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9772 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9773 -- Compare elements of two lists for conformance. Elements have to be
9774 -- conformant, and actuals inserted as default parameters do not match
9775 -- explicit actuals with the same value.
9777 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9778 -- Compare an operator node with a function call
9784 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
9786 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
9793 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
9798 if L1 = No_List then
9804 if L2 = No_List then
9810 -- Compare two lists, skipping rewrite insertions (we want to compare
9811 -- the original trees, not the expanded versions).
9814 if Is_Rewrite_Insertion (N1) then
9816 elsif Is_Rewrite_Insertion (N2) then
9822 elsif not FCE (N1, N2) then
9835 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
9836 Actuals : constant List_Id := Parameter_Associations (Call_Node);
9841 or else Entity (Op_Node) /= Entity (Name (Call_Node))
9846 Act := First (Actuals);
9848 if Nkind (Op_Node) in N_Binary_Op then
9849 if not FCE (Left_Opnd (Op_Node), Act) then
9856 return Present (Act)
9857 and then FCE (Right_Opnd (Op_Node), Act)
9858 and then No (Next (Act));
9862 function User_Defined_Numeric_Literal_Mismatch return Boolean;
9863 -- Usually literals with the same value like 12345 and 12_345
9864 -- or 123.0 and 123.00 conform, but not if they are
9865 -- user-defined literals.
9867 -------------------------------------------
9868 -- User_Defined_Numeric_Literal_Mismatch --
9869 -------------------------------------------
9871 function User_Defined_Numeric_Literal_Mismatch return Boolean is
9872 E1_Is_User_Defined : constant Boolean :=
9873 Nkind (Given_E1) not in N_Integer_Literal | N_Real_Literal;
9874 E2_Is_User_Defined : constant Boolean :=
9875 Nkind (Given_E2) not in N_Integer_Literal | N_Real_Literal;
9878 pragma Assert (E1_Is_User_Defined = E2_Is_User_Defined);
9880 return E1_Is_User_Defined and then
9881 not String_Equal (String_From_Numeric_Literal (E1),
9882 String_From_Numeric_Literal (E2));
9883 end User_Defined_Numeric_Literal_Mismatch;
9889 -- Start of processing for Fully_Conformant_Expressions
9894 -- Nonconformant if paren count does not match. Note: if some idiot
9895 -- complains that we don't do this right for more than 3 levels of
9896 -- parentheses, they will be treated with the respect they deserve.
9898 if Paren_Count (E1) /= Paren_Count (E2) then
9901 -- If same entities are referenced, then they are conformant even if
9902 -- they have different forms (RM 8.3.1(19-20)).
9904 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9905 if Present (Entity (E1)) then
9906 Result := Entity (E1) = Entity (E2)
9908 -- One may be a discriminant that has been replaced by the
9909 -- corresponding discriminal.
9912 (Chars (Entity (E1)) = Chars (Entity (E2))
9913 and then Ekind (Entity (E1)) = E_Discriminant
9914 and then Ekind (Entity (E2)) = E_In_Parameter)
9916 -- The discriminant of a protected type is transformed into
9917 -- a local constant and then into a parameter of a protected
9921 (Ekind (Entity (E1)) = E_Constant
9922 and then Ekind (Entity (E2)) = E_In_Parameter
9923 and then Present (Discriminal_Link (Entity (E1)))
9924 and then Discriminal_Link (Entity (E1)) =
9925 Discriminal_Link (Entity (E2)))
9927 -- AI12-050: The loop variables of quantified expressions match
9928 -- if they have the same identifier, even though they may have
9929 -- different entities.
9932 (Chars (Entity (E1)) = Chars (Entity (E2))
9933 and then Ekind (Entity (E1)) = E_Loop_Parameter
9934 and then Ekind (Entity (E2)) = E_Loop_Parameter)
9936 -- A call to an instantiation of Unchecked_Conversion is
9937 -- rewritten with the name of the generated function created for
9938 -- the instance, and this must be special-cased.
9941 (Ekind (Entity (E1)) = E_Function
9942 and then Is_Intrinsic_Subprogram (Entity (E1))
9943 and then Is_Generic_Instance (Entity (E1))
9944 and then Entity (E2) = Alias (Entity (E1)));
9945 if Report and not Result then
9947 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
9949 ("Meaning of& differs because of declaration#", E1, E2);
9954 elsif Nkind (E1) = N_Expanded_Name
9955 and then Nkind (E2) = N_Expanded_Name
9956 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9957 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9959 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9962 -- Identifiers in component associations don't always have
9963 -- entities, but their names must conform.
9965 return Nkind (E1) = N_Identifier
9966 and then Nkind (E2) = N_Identifier
9967 and then Chars (E1) = Chars (E2);
9970 elsif Nkind (E1) = N_Character_Literal
9971 and then Nkind (E2) = N_Expanded_Name
9973 return Nkind (Selector_Name (E2)) = N_Character_Literal
9974 and then Chars (E1) = Chars (Selector_Name (E2));
9976 elsif Nkind (E2) = N_Character_Literal
9977 and then Nkind (E1) = N_Expanded_Name
9979 return Nkind (Selector_Name (E1)) = N_Character_Literal
9980 and then Chars (E2) = Chars (Selector_Name (E1));
9982 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9983 return FCO (E1, E2);
9985 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9986 return FCO (E2, E1);
9988 -- Otherwise we must have the same syntactic entity
9990 elsif Nkind (E1) /= Nkind (E2) then
9993 -- At this point, we specialize by node type
9999 FCL (Expressions (E1), Expressions (E2))
10001 FCL (Component_Associations (E1),
10002 Component_Associations (E2));
10004 when N_Allocator =>
10005 if Nkind (Expression (E1)) = N_Qualified_Expression
10007 Nkind (Expression (E2)) = N_Qualified_Expression
10009 return FCE (Expression (E1), Expression (E2));
10011 -- Check that the subtype marks and any constraints
10016 Indic1 : constant Node_Id := Expression (E1);
10017 Indic2 : constant Node_Id := Expression (E2);
10022 if Nkind (Indic1) /= N_Subtype_Indication then
10024 Nkind (Indic2) /= N_Subtype_Indication
10025 and then Entity (Indic1) = Entity (Indic2);
10027 elsif Nkind (Indic2) /= N_Subtype_Indication then
10029 Nkind (Indic1) /= N_Subtype_Indication
10030 and then Entity (Indic1) = Entity (Indic2);
10033 if Entity (Subtype_Mark (Indic1)) /=
10034 Entity (Subtype_Mark (Indic2))
10039 Elt1 := First (Constraints (Constraint (Indic1)));
10040 Elt2 := First (Constraints (Constraint (Indic2)));
10041 while Present (Elt1) and then Present (Elt2) loop
10042 if not FCE (Elt1, Elt2) then
10055 when N_Attribute_Reference =>
10057 Attribute_Name (E1) = Attribute_Name (E2)
10058 and then FCL (Expressions (E1), Expressions (E2));
10060 when N_Binary_Op =>
10062 Entity (E1) = Entity (E2)
10063 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
10064 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
10066 when N_Membership_Test
10070 FCE (Left_Opnd (E1), Left_Opnd (E2))
10072 FCE (Right_Opnd (E1), Right_Opnd (E2));
10074 when N_Case_Expression =>
10080 if not FCE (Expression (E1), Expression (E2)) then
10084 Alt1 := First (Alternatives (E1));
10085 Alt2 := First (Alternatives (E2));
10087 if Present (Alt1) /= Present (Alt2) then
10089 elsif No (Alt1) then
10093 if not FCE (Expression (Alt1), Expression (Alt2))
10094 or else not FCL (Discrete_Choices (Alt1),
10095 Discrete_Choices (Alt2))
10106 when N_Character_Literal =>
10108 Char_Literal_Value (E1) = Char_Literal_Value (E2);
10110 when N_Component_Association =>
10112 FCL (Choices (E1), Choices (E2))
10114 FCE (Expression (E1), Expression (E2));
10116 when N_Explicit_Dereference =>
10118 FCE (Prefix (E1), Prefix (E2));
10120 when N_Extension_Aggregate =>
10122 FCL (Expressions (E1), Expressions (E2))
10123 and then Null_Record_Present (E1) =
10124 Null_Record_Present (E2)
10125 and then FCL (Component_Associations (E1),
10126 Component_Associations (E2));
10128 when N_Function_Call =>
10130 FCE (Name (E1), Name (E2))
10132 FCL (Parameter_Associations (E1),
10133 Parameter_Associations (E2));
10135 when N_If_Expression =>
10137 FCL (Expressions (E1), Expressions (E2));
10139 when N_Indexed_Component =>
10141 FCE (Prefix (E1), Prefix (E2))
10143 FCL (Expressions (E1), Expressions (E2));
10145 when N_Integer_Literal =>
10146 return (Intval (E1) = Intval (E2))
10147 and then not User_Defined_Numeric_Literal_Mismatch;
10152 when N_Operator_Symbol =>
10154 Chars (E1) = Chars (E2);
10156 when N_Others_Choice =>
10159 when N_Parameter_Association =>
10161 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
10162 and then FCE (Explicit_Actual_Parameter (E1),
10163 Explicit_Actual_Parameter (E2));
10165 when N_Qualified_Expression
10166 | N_Type_Conversion
10167 | N_Unchecked_Type_Conversion
10170 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
10172 FCE (Expression (E1), Expression (E2));
10174 when N_Quantified_Expression =>
10175 if not FCE (Condition (E1), Condition (E2)) then
10179 if Present (Loop_Parameter_Specification (E1))
10180 and then Present (Loop_Parameter_Specification (E2))
10183 L1 : constant Node_Id :=
10184 Loop_Parameter_Specification (E1);
10185 L2 : constant Node_Id :=
10186 Loop_Parameter_Specification (E2);
10190 Reverse_Present (L1) = Reverse_Present (L2)
10192 FCE (Defining_Identifier (L1),
10193 Defining_Identifier (L2))
10195 FCE (Discrete_Subtype_Definition (L1),
10196 Discrete_Subtype_Definition (L2));
10199 elsif Present (Iterator_Specification (E1))
10200 and then Present (Iterator_Specification (E2))
10203 I1 : constant Node_Id := Iterator_Specification (E1);
10204 I2 : constant Node_Id := Iterator_Specification (E2);
10208 FCE (Defining_Identifier (I1),
10209 Defining_Identifier (I2))
10211 Of_Present (I1) = Of_Present (I2)
10213 Reverse_Present (I1) = Reverse_Present (I2)
10214 and then FCE (Name (I1), Name (I2))
10215 and then FCE (Subtype_Indication (I1),
10216 Subtype_Indication (I2));
10219 -- The quantified expressions used different specifications to
10220 -- walk their respective ranges.
10228 FCE (Low_Bound (E1), Low_Bound (E2))
10230 FCE (High_Bound (E1), High_Bound (E2));
10232 when N_Real_Literal =>
10233 return (Realval (E1) = Realval (E2))
10234 and then not User_Defined_Numeric_Literal_Mismatch;
10236 when N_Selected_Component =>
10238 FCE (Prefix (E1), Prefix (E2))
10240 FCE (Selector_Name (E1), Selector_Name (E2));
10244 FCE (Prefix (E1), Prefix (E2))
10246 FCE (Discrete_Range (E1), Discrete_Range (E2));
10248 when N_String_Literal =>
10250 S1 : constant String_Id := Strval (E1);
10251 S2 : constant String_Id := Strval (E2);
10252 L1 : constant Nat := String_Length (S1);
10253 L2 : constant Nat := String_Length (S2);
10260 for J in 1 .. L1 loop
10261 if Get_String_Char (S1, J) /=
10262 Get_String_Char (S2, J)
10274 Entity (E1) = Entity (E2)
10276 FCE (Right_Opnd (E1), Right_Opnd (E2));
10278 -- All other node types cannot appear in this context. Strictly
10279 -- we should raise a fatal internal error. Instead we just ignore
10280 -- the nodes. This means that if anyone makes a mistake in the
10281 -- expander and mucks an expression tree irretrievably, the result
10282 -- will be a failure to detect a (probably very obscure) case
10283 -- of non-conformance, which is better than bombing on some
10284 -- case where two expressions do in fact conform.
10290 end Fully_Conformant_Expressions;
10292 ----------------------------------------
10293 -- Fully_Conformant_Discrete_Subtypes --
10294 ----------------------------------------
10296 function Fully_Conformant_Discrete_Subtypes
10297 (Given_S1 : Node_Id;
10298 Given_S2 : Node_Id) return Boolean
10300 S1 : constant Node_Id := Original_Node (Given_S1);
10301 S2 : constant Node_Id := Original_Node (Given_S2);
10303 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
10304 -- Special-case for a bound given by a discriminant, which in the body
10305 -- is replaced with the discriminal of the enclosing type.
10307 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
10308 -- Check both bounds
10310 -----------------------
10311 -- Conforming_Bounds --
10312 -----------------------
10314 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
10316 if Is_Entity_Name (B1)
10317 and then Is_Entity_Name (B2)
10318 and then Ekind (Entity (B1)) = E_Discriminant
10320 return Chars (B1) = Chars (B2);
10323 return Fully_Conformant_Expressions (B1, B2);
10325 end Conforming_Bounds;
10327 -----------------------
10328 -- Conforming_Ranges --
10329 -----------------------
10331 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
10334 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
10336 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
10337 end Conforming_Ranges;
10339 -- Start of processing for Fully_Conformant_Discrete_Subtypes
10342 if Nkind (S1) /= Nkind (S2) then
10345 elsif Is_Entity_Name (S1) then
10346 return Entity (S1) = Entity (S2);
10348 elsif Nkind (S1) = N_Range then
10349 return Conforming_Ranges (S1, S2);
10351 elsif Nkind (S1) = N_Subtype_Indication then
10353 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
10356 (Range_Expression (Constraint (S1)),
10357 Range_Expression (Constraint (S2)));
10361 end Fully_Conformant_Discrete_Subtypes;
10363 --------------------
10364 -- Install_Entity --
10365 --------------------
10367 procedure Install_Entity (E : Entity_Id) is
10368 Prev : constant Entity_Id := Current_Entity (E);
10370 Set_Is_Immediately_Visible (E);
10371 Set_Current_Entity (E);
10372 Set_Homonym (E, Prev);
10373 end Install_Entity;
10375 ---------------------
10376 -- Install_Formals --
10377 ---------------------
10379 procedure Install_Formals (Id : Entity_Id) is
10382 F := First_Formal (Id);
10383 while Present (F) loop
10384 Install_Entity (F);
10387 end Install_Formals;
10389 -----------------------------
10390 -- Is_Interface_Conformant --
10391 -----------------------------
10393 function Is_Interface_Conformant
10394 (Tagged_Type : Entity_Id;
10395 Iface_Prim : Entity_Id;
10396 Prim : Entity_Id) return Boolean
10398 -- The operation may in fact be an inherited (implicit) operation
10399 -- rather than the original interface primitive, so retrieve the
10400 -- ultimate ancestor.
10402 Iface : constant Entity_Id :=
10403 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
10404 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
10406 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
10407 -- Return the controlling formal of Prim
10409 ------------------------
10410 -- Controlling_Formal --
10411 ------------------------
10413 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
10417 E := First_Entity (Prim);
10418 while Present (E) loop
10419 if Is_Formal (E) and then Is_Controlling_Formal (E) then
10427 end Controlling_Formal;
10431 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
10432 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
10434 -- Start of processing for Is_Interface_Conformant
10437 pragma Assert (Is_Subprogram (Iface_Prim)
10438 and then Is_Subprogram (Prim)
10439 and then Is_Dispatching_Operation (Iface_Prim)
10440 and then Is_Dispatching_Operation (Prim));
10442 pragma Assert (Is_Interface (Iface)
10443 or else (Present (Alias (Iface_Prim))
10446 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
10448 if Prim = Iface_Prim
10449 or else not Is_Subprogram (Prim)
10450 or else Ekind (Prim) /= Ekind (Iface_Prim)
10451 or else not Is_Dispatching_Operation (Prim)
10452 or else Scope (Prim) /= Scope (Tagged_Type)
10454 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
10455 or else not Primitive_Names_Match (Iface_Prim, Prim)
10459 -- The mode of the controlling formals must match
10461 elsif Present (Iface_Ctrl_F)
10462 and then Present (Prim_Ctrl_F)
10463 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
10467 -- Case of a procedure, or a function whose result type matches the
10468 -- result type of the interface primitive, or a function that has no
10469 -- controlling result (I or access I).
10471 elsif Ekind (Iface_Prim) = E_Procedure
10472 or else Etype (Prim) = Etype (Iface_Prim)
10473 or else not Has_Controlling_Result (Prim)
10475 return Type_Conformant
10476 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
10478 -- Case of a function returning an interface, or an access to one. Check
10479 -- that the return types correspond.
10481 elsif Implements_Interface (Typ, Iface) then
10482 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10484 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10489 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10490 Skip_Controlling_Formals => True);
10496 end Is_Interface_Conformant;
10498 ---------------------------------
10499 -- Is_Non_Overriding_Operation --
10500 ---------------------------------
10502 function Is_Non_Overriding_Operation
10503 (Prev_E : Entity_Id;
10504 New_E : Entity_Id) return Boolean
10506 Formal : Entity_Id;
10508 G_Typ : Entity_Id := Empty;
10510 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10511 -- If F_Type is a derived type associated with a generic actual subtype,
10512 -- then return its Generic_Parent_Type attribute, else return Empty.
10514 function Types_Correspond
10515 (P_Type : Entity_Id;
10516 N_Type : Entity_Id) return Boolean;
10517 -- Returns true if and only if the types (or designated types in the
10518 -- case of anonymous access types) are the same or N_Type is derived
10519 -- directly or indirectly from P_Type.
10521 -----------------------------
10522 -- Get_Generic_Parent_Type --
10523 -----------------------------
10525 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10531 if Is_Derived_Type (F_Typ)
10532 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10534 -- The tree must be traversed to determine the parent subtype in
10535 -- the generic unit, which unfortunately isn't always available
10536 -- via semantic attributes. ??? (Note: The use of Original_Node
10537 -- is needed for cases where a full derived type has been
10540 -- If the parent type is a scalar type, the derivation creates
10541 -- an anonymous base type for it, and the source type is its
10544 if Is_Scalar_Type (F_Typ)
10545 and then not Comes_From_Source (F_Typ)
10549 (Original_Node (Parent (First_Subtype (F_Typ))));
10551 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10553 if Nkind (Defn) = N_Derived_Type_Definition then
10554 Indic := Subtype_Indication (Defn);
10556 if Nkind (Indic) = N_Subtype_Indication then
10557 G_Typ := Entity (Subtype_Mark (Indic));
10559 G_Typ := Entity (Indic);
10562 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10563 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10565 return Generic_Parent_Type (Parent (G_Typ));
10571 end Get_Generic_Parent_Type;
10573 ----------------------
10574 -- Types_Correspond --
10575 ----------------------
10577 function Types_Correspond
10578 (P_Type : Entity_Id;
10579 N_Type : Entity_Id) return Boolean
10581 Prev_Type : Entity_Id := Base_Type (P_Type);
10582 New_Type : Entity_Id := Base_Type (N_Type);
10585 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10586 Prev_Type := Designated_Type (Prev_Type);
10589 if Ekind (New_Type) = E_Anonymous_Access_Type then
10590 New_Type := Designated_Type (New_Type);
10593 if Prev_Type = New_Type then
10596 elsif not Is_Class_Wide_Type (New_Type) then
10597 while Etype (New_Type) /= New_Type loop
10598 New_Type := Etype (New_Type);
10600 if New_Type = Prev_Type then
10606 end Types_Correspond;
10608 -- Start of processing for Is_Non_Overriding_Operation
10611 -- In the case where both operations are implicit derived subprograms
10612 -- then neither overrides the other. This can only occur in certain
10613 -- obscure cases (e.g., derivation from homographs created in a generic
10616 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10619 elsif Ekind (Current_Scope) = E_Package
10620 and then Is_Generic_Instance (Current_Scope)
10621 and then In_Private_Part (Current_Scope)
10622 and then Comes_From_Source (New_E)
10624 -- We examine the formals and result type of the inherited operation,
10625 -- to determine whether their type is derived from (the instance of)
10626 -- a generic type. The first such formal or result type is the one
10629 Formal := First_Formal (Prev_E);
10631 while Present (Formal) loop
10632 F_Typ := Base_Type (Etype (Formal));
10634 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10635 F_Typ := Designated_Type (F_Typ);
10638 G_Typ := Get_Generic_Parent_Type (F_Typ);
10639 exit when Present (G_Typ);
10641 Next_Formal (Formal);
10644 -- If the function dispatches on result check the result type
10646 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10647 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10654 -- If the generic type is a private type, then the original operation
10655 -- was not overriding in the generic, because there was no primitive
10656 -- operation to override.
10658 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10659 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10660 N_Formal_Private_Type_Definition
10664 -- The generic parent type is the ancestor of a formal derived
10665 -- type declaration. We need to check whether it has a primitive
10666 -- operation that should be overridden by New_E in the generic.
10670 P_Formal : Entity_Id;
10671 N_Formal : Entity_Id;
10674 P_Prim : Entity_Id;
10675 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10678 while Present (Prim_Elt) loop
10679 P_Prim := Node (Prim_Elt);
10681 if Chars (P_Prim) = Chars (New_E)
10682 and then Ekind (P_Prim) = Ekind (New_E)
10684 P_Formal := First_Formal (P_Prim);
10685 N_Formal := First_Formal (New_E);
10686 while Present (P_Formal) and then Present (N_Formal) loop
10687 P_Typ := Etype (P_Formal);
10688 N_Typ := Etype (N_Formal);
10690 if not Types_Correspond (P_Typ, N_Typ) then
10694 Next_Entity (P_Formal);
10695 Next_Entity (N_Formal);
10698 -- Found a matching primitive operation belonging to the
10699 -- formal ancestor type, so the new subprogram is
10703 and then No (N_Formal)
10704 and then (Ekind (New_E) /= E_Function
10707 (Etype (P_Prim), Etype (New_E)))
10713 Next_Elmt (Prim_Elt);
10716 -- If no match found, then the new subprogram does not override
10717 -- in the generic (nor in the instance).
10719 -- If the type in question is not abstract, and the subprogram
10720 -- is, this will be an error if the new operation is in the
10721 -- private part of the instance. Emit a warning now, which will
10722 -- make the subsequent error message easier to understand.
10724 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
10725 and then Is_Abstract_Subprogram (Prev_E)
10726 and then In_Private_Part (Current_Scope)
10728 Error_Msg_Node_2 := F_Typ;
10730 ("private operation& in generic unit does not override "
10731 & "any primitive operation of& (RM 12.3 (18))??",
10741 end Is_Non_Overriding_Operation;
10743 -------------------------------------
10744 -- List_Inherited_Pre_Post_Aspects --
10745 -------------------------------------
10747 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
10749 if Opt.List_Inherited_Aspects
10750 and then Is_Subprogram_Or_Generic_Subprogram (E)
10753 Subps : constant Subprogram_List := Inherited_Subprograms (E);
10758 for Index in Subps'Range loop
10759 Items := Contract (Subps (Index));
10761 if Present (Items) then
10762 Prag := Pre_Post_Conditions (Items);
10763 while Present (Prag) loop
10764 Error_Msg_Sloc := Sloc (Prag);
10766 if Class_Present (Prag)
10767 and then not Split_PPC (Prag)
10769 if Pragma_Name (Prag) = Name_Precondition then
10771 ("info: & inherits `Pre''Class` aspect from "
10775 ("info: & inherits `Post''Class` aspect from "
10780 Prag := Next_Pragma (Prag);
10786 end List_Inherited_Pre_Post_Aspects;
10788 ------------------------------
10789 -- Make_Inequality_Operator --
10790 ------------------------------
10792 -- S is the defining identifier of an equality operator. We build a
10793 -- subprogram declaration with the right signature. This operation is
10794 -- intrinsic, because it is always expanded as the negation of the
10795 -- call to the equality function.
10797 procedure Make_Inequality_Operator (S : Entity_Id) is
10798 Loc : constant Source_Ptr := Sloc (S);
10801 Op_Name : Entity_Id;
10803 FF : constant Entity_Id := First_Formal (S);
10804 NF : constant Entity_Id := Next_Formal (FF);
10807 -- Check that equality was properly defined, ignore call if not
10814 A : constant Entity_Id :=
10815 Make_Defining_Identifier (Sloc (FF),
10816 Chars => Chars (FF));
10818 B : constant Entity_Id :=
10819 Make_Defining_Identifier (Sloc (NF),
10820 Chars => Chars (NF));
10823 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
10825 Formals := New_List (
10826 Make_Parameter_Specification (Loc,
10827 Defining_Identifier => A,
10829 New_Occurrence_Of (Etype (First_Formal (S)),
10830 Sloc (Etype (First_Formal (S))))),
10832 Make_Parameter_Specification (Loc,
10833 Defining_Identifier => B,
10835 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
10836 Sloc (Etype (Next_Formal (First_Formal (S)))))));
10839 Make_Subprogram_Declaration (Loc,
10841 Make_Function_Specification (Loc,
10842 Defining_Unit_Name => Op_Name,
10843 Parameter_Specifications => Formals,
10844 Result_Definition =>
10845 New_Occurrence_Of (Standard_Boolean, Loc)));
10847 -- Insert inequality right after equality if it is explicit or after
10848 -- the derived type when implicit. These entities are created only
10849 -- for visibility purposes, and eventually replaced in the course
10850 -- of expansion, so they do not need to be attached to the tree and
10851 -- seen by the back-end. Keeping them internal also avoids spurious
10852 -- freezing problems. The declaration is inserted in the tree for
10853 -- analysis, and removed afterwards. If the equality operator comes
10854 -- from an explicit declaration, attach the inequality immediately
10855 -- after. Else the equality is inherited from a derived type
10856 -- declaration, so insert inequality after that declaration.
10858 if No (Alias (S)) then
10859 Insert_After (Unit_Declaration_Node (S), Decl);
10860 elsif Is_List_Member (Parent (S)) then
10861 Insert_After (Parent (S), Decl);
10863 Insert_After (Parent (Etype (First_Formal (S))), Decl);
10866 Mark_Rewrite_Insertion (Decl);
10867 Set_Is_Intrinsic_Subprogram (Op_Name);
10870 Set_Has_Completion (Op_Name);
10871 Set_Corresponding_Equality (Op_Name, S);
10872 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
10874 end Make_Inequality_Operator;
10876 ----------------------
10877 -- May_Need_Actuals --
10878 ----------------------
10880 procedure May_Need_Actuals (Fun : Entity_Id) is
10885 F := First_Formal (Fun);
10887 while Present (F) loop
10888 if No (Default_Value (F)) then
10896 Set_Needs_No_Actuals (Fun, B);
10897 end May_Need_Actuals;
10899 ---------------------
10900 -- Mode_Conformant --
10901 ---------------------
10903 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10906 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
10908 end Mode_Conformant;
10910 ---------------------------
10911 -- New_Overloaded_Entity --
10912 ---------------------------
10914 procedure New_Overloaded_Entity
10916 Derived_Type : Entity_Id := Empty)
10918 Overridden_Subp : Entity_Id := Empty;
10919 -- Set if the current scope has an operation that is type-conformant
10920 -- with S, and becomes hidden by S.
10922 Is_Primitive_Subp : Boolean;
10923 -- Set to True if the new subprogram is primitive
10926 -- Entity that S overrides
10928 procedure Check_For_Primitive_Subprogram
10929 (Is_Primitive : out Boolean;
10930 Is_Overriding : Boolean := False);
10931 -- If the subprogram being analyzed is a primitive operation of the type
10932 -- of a formal or result, set the Has_Primitive_Operations flag on the
10933 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10934 -- corresponding flag on the entity itself for later use.
10936 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10937 -- True if a) E is a subprogram whose first formal is a concurrent type
10938 -- defined in the scope of E that has some entry or subprogram whose
10939 -- profile matches E, or b) E is an internally built dispatching
10940 -- subprogram of a protected type and there is a matching subprogram
10941 -- defined in the enclosing scope of the protected type, or c) E is
10942 -- an entry of a synchronized type and a matching procedure has been
10943 -- previously defined in the enclosing scope of the synchronized type.
10945 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10946 -- Check that E is declared in the private part of the current package,
10947 -- or in the package body, where it may hide a previous declaration.
10948 -- We can't use In_Private_Part by itself because this flag is also
10949 -- set when freezing entities, so we must examine the place of the
10950 -- declaration in the tree, and recognize wrapper packages as well.
10952 function Is_Overriding_Alias
10953 (Old_E : Entity_Id;
10954 New_E : Entity_Id) return Boolean;
10955 -- Check whether new subprogram and old subprogram are both inherited
10956 -- from subprograms that have distinct dispatch table entries. This can
10957 -- occur with derivations from instances with accidental homonyms. The
10958 -- function is conservative given that the converse is only true within
10959 -- instances that contain accidental overloadings.
10961 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10962 -- Report conflict between entities S and E
10964 ------------------------------------
10965 -- Check_For_Primitive_Subprogram --
10966 ------------------------------------
10968 procedure Check_For_Primitive_Subprogram
10969 (Is_Primitive : out Boolean;
10970 Is_Overriding : Boolean := False)
10972 Formal : Entity_Id;
10976 function Visible_Part_Type (T : Entity_Id) return Boolean;
10977 -- Returns true if T is declared in the visible part of the current
10978 -- package scope; otherwise returns false. Assumes that T is declared
10981 procedure Check_Private_Overriding (T : Entity_Id);
10982 -- Checks that if a primitive abstract subprogram of a visible
10983 -- abstract type is declared in a private part, then it must override
10984 -- an abstract subprogram declared in the visible part. Also checks
10985 -- that if a primitive function with a controlling result is declared
10986 -- in a private part, then it must override a function declared in
10987 -- the visible part.
10989 ------------------------------
10990 -- Check_Private_Overriding --
10991 ------------------------------
10993 procedure Check_Private_Overriding (T : Entity_Id) is
10994 function Overrides_Private_Part_Op return Boolean;
10995 -- This detects the special case where the overriding subprogram
10996 -- is overriding a subprogram that was declared in the same
10997 -- private part. That case is illegal by 3.9.3(10).
10999 function Overrides_Visible_Function
11000 (Partial_View : Entity_Id) return Boolean;
11001 -- True if S overrides a function in the visible part. The
11002 -- overridden function could be explicitly or implicitly declared.
11004 -------------------------------
11005 -- Overrides_Private_Part_Op --
11006 -------------------------------
11008 function Overrides_Private_Part_Op return Boolean is
11009 Over_Decl : constant Node_Id :=
11010 Unit_Declaration_Node (Overridden_Operation (S));
11011 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
11014 pragma Assert (Is_Overriding);
11016 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
11018 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
11020 return In_Same_List (Over_Decl, Subp_Decl);
11021 end Overrides_Private_Part_Op;
11023 --------------------------------
11024 -- Overrides_Visible_Function --
11025 --------------------------------
11027 function Overrides_Visible_Function
11028 (Partial_View : Entity_Id) return Boolean
11031 if not Is_Overriding or else not Has_Homonym (S) then
11035 if not Present (Partial_View) then
11039 -- Search through all the homonyms H of S in the current
11040 -- package spec, and return True if we find one that matches.
11041 -- Note that Parent (H) will be the declaration of the
11042 -- partial view of T for a match.
11045 H : Entity_Id := S;
11049 exit when not Present (H) or else Scope (H) /= Scope (S);
11051 if Nkind (Parent (H)) in
11052 N_Private_Extension_Declaration |
11053 N_Private_Type_Declaration
11054 and then Defining_Identifier (Parent (H)) = Partial_View
11062 end Overrides_Visible_Function;
11064 -- Start of processing for Check_Private_Overriding
11067 if Is_Package_Or_Generic_Package (Current_Scope)
11068 and then In_Private_Part (Current_Scope)
11069 and then Visible_Part_Type (T)
11070 and then not In_Instance
11072 if Is_Abstract_Type (T)
11073 and then Is_Abstract_Subprogram (S)
11074 and then (not Is_Overriding
11075 or else not Is_Abstract_Subprogram (E)
11076 or else Overrides_Private_Part_Op)
11079 ("abstract subprograms must be visible (RM 3.9.3(10))!",
11082 elsif Ekind (S) = E_Function then
11084 Partial_View : constant Entity_Id :=
11085 Incomplete_Or_Partial_View (T);
11088 if not Overrides_Visible_Function (Partial_View) then
11090 -- Here, S is "function ... return T;" declared in
11091 -- the private part, not overriding some visible
11092 -- operation. That's illegal in the tagged case
11093 -- (but not if the private type is untagged).
11095 if ((Present (Partial_View)
11096 and then Is_Tagged_Type (Partial_View))
11097 or else (not Present (Partial_View)
11098 and then Is_Tagged_Type (T)))
11099 and then T = Base_Type (Etype (S))
11102 ("private function with tagged result must"
11103 & " override visible-part function", S);
11105 ("\move subprogram to the visible part"
11106 & " (RM 3.9.3(10))", S);
11108 -- Ada 2012 (AI05-0073): Extend this check to the case
11109 -- of a function whose result subtype is defined by an
11110 -- access_definition designating specific tagged type.
11112 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
11113 and then Is_Tagged_Type (Designated_Type (Etype (S)))
11115 not Is_Class_Wide_Type
11116 (Designated_Type (Etype (S)))
11117 and then Ada_Version >= Ada_2012
11120 ("private function with controlling access "
11121 & "result must override visible-part function",
11124 ("\move subprogram to the visible part"
11125 & " (RM 3.9.3(10))", S);
11131 end Check_Private_Overriding;
11133 -----------------------
11134 -- Visible_Part_Type --
11135 -----------------------
11137 function Visible_Part_Type (T : Entity_Id) return Boolean is
11138 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
11141 -- If the entity is a private type, then it must be declared in a
11144 if Ekind (T) in Private_Kind then
11147 elsif Is_Type (T) and then Has_Private_Declaration (T) then
11150 elsif Is_List_Member (Declaration_Node (T))
11151 and then List_Containing (Declaration_Node (T)) =
11152 Visible_Declarations (Specification (P))
11159 end Visible_Part_Type;
11161 -- Start of processing for Check_For_Primitive_Subprogram
11164 Is_Primitive := False;
11166 if not Comes_From_Source (S) then
11169 -- If subprogram is at library level, it is not primitive operation
11171 elsif Current_Scope = Standard_Standard then
11174 elsif (Is_Package_Or_Generic_Package (Current_Scope)
11175 and then not In_Package_Body (Current_Scope))
11176 or else Is_Overriding
11178 -- For function, check return type
11180 if Ekind (S) = E_Function then
11181 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
11182 F_Typ := Designated_Type (Etype (S));
11184 F_Typ := Etype (S);
11187 B_Typ := Base_Type (F_Typ);
11189 if Scope (B_Typ) = Current_Scope
11190 and then not Is_Class_Wide_Type (B_Typ)
11191 and then not Is_Generic_Type (B_Typ)
11193 Is_Primitive := True;
11194 Set_Has_Primitive_Operations (B_Typ);
11195 Set_Is_Primitive (S);
11196 Check_Private_Overriding (B_Typ);
11198 -- The Ghost policy in effect at the point of declaration
11199 -- or a tagged type and a primitive operation must match
11200 -- (SPARK RM 6.9(16)).
11202 Check_Ghost_Primitive (S, B_Typ);
11206 -- For all subprograms, check formals
11208 Formal := First_Formal (S);
11209 while Present (Formal) loop
11210 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
11211 F_Typ := Designated_Type (Etype (Formal));
11213 F_Typ := Etype (Formal);
11216 B_Typ := Base_Type (F_Typ);
11218 if Ekind (B_Typ) = E_Access_Subtype then
11219 B_Typ := Base_Type (B_Typ);
11222 if Scope (B_Typ) = Current_Scope
11223 and then not Is_Class_Wide_Type (B_Typ)
11224 and then not Is_Generic_Type (B_Typ)
11226 Is_Primitive := True;
11227 Set_Is_Primitive (S);
11228 Set_Has_Primitive_Operations (B_Typ);
11229 Check_Private_Overriding (B_Typ);
11231 -- The Ghost policy in effect at the point of declaration
11232 -- of a tagged type and a primitive operation must match
11233 -- (SPARK RM 6.9(16)).
11235 Check_Ghost_Primitive (S, B_Typ);
11238 Next_Formal (Formal);
11241 -- Special case: An equality function can be redefined for a type
11242 -- occurring in a declarative part, and won't otherwise be treated as
11243 -- a primitive because it doesn't occur in a package spec and doesn't
11244 -- override an inherited subprogram. It's important that we mark it
11245 -- primitive so it can be returned by Collect_Primitive_Operations
11246 -- and be used in composing the equality operation of later types
11247 -- that have a component of the type.
11249 elsif Chars (S) = Name_Op_Eq
11250 and then Etype (S) = Standard_Boolean
11252 B_Typ := Base_Type (Etype (First_Formal (S)));
11254 if Scope (B_Typ) = Current_Scope
11256 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
11257 and then not Is_Limited_Type (B_Typ)
11259 Is_Primitive := True;
11260 Set_Is_Primitive (S);
11261 Set_Has_Primitive_Operations (B_Typ);
11262 Check_Private_Overriding (B_Typ);
11264 -- The Ghost policy in effect at the point of declaration of a
11265 -- tagged type and a primitive operation must match
11266 -- (SPARK RM 6.9(16)).
11268 Check_Ghost_Primitive (S, B_Typ);
11271 end Check_For_Primitive_Subprogram;
11273 --------------------------------------
11274 -- Has_Matching_Entry_Or_Subprogram --
11275 --------------------------------------
11277 function Has_Matching_Entry_Or_Subprogram
11278 (E : Entity_Id) return Boolean
11280 function Check_Conforming_Parameters
11281 (E1_Param : Node_Id;
11282 E2_Param : Node_Id;
11283 Ctype : Conformance_Type) return Boolean;
11284 -- Starting from the given parameters, check that all the parameters
11285 -- of two entries or subprograms are conformant. Used to skip
11286 -- the check on the controlling argument.
11288 function Matching_Entry_Or_Subprogram
11289 (Conc_Typ : Entity_Id;
11290 Subp : Entity_Id) return Entity_Id;
11291 -- Return the first entry or subprogram of the given concurrent type
11292 -- whose name matches the name of Subp and has a profile conformant
11293 -- with Subp; return Empty if not found.
11295 function Matching_Dispatching_Subprogram
11296 (Conc_Typ : Entity_Id;
11297 Ent : Entity_Id) return Entity_Id;
11298 -- Return the first dispatching primitive of Conc_Type defined in the
11299 -- enclosing scope of Conc_Type (i.e. before the full definition of
11300 -- this concurrent type) whose name matches the entry Ent and has a
11301 -- profile conformant with the profile of the corresponding (not yet
11302 -- built) dispatching primitive of Ent; return Empty if not found.
11304 function Matching_Original_Protected_Subprogram
11305 (Prot_Typ : Entity_Id;
11306 Subp : Entity_Id) return Entity_Id;
11307 -- Return the first subprogram defined in the enclosing scope of
11308 -- Prot_Typ (before the full definition of this protected type)
11309 -- whose name matches the original name of Subp and has a profile
11310 -- conformant with the profile of Subp; return Empty if not found.
11312 function Normalized_First_Parameter_Type
11313 (E : Entity_Id) return Entity_Id;
11314 -- Return the type of the first parameter unless that type
11315 -- is an anonymous access type, in which case return the
11316 -- designated type. Used to treat anonymous-access-to-synchronized
11317 -- the same as synchronized for purposes of checking for
11318 -- prefixed view profile conflicts.
11320 ---------------------------------
11321 -- Check_Conforming_Parameters --
11322 ---------------------------------
11324 function Check_Conforming_Parameters
11325 (E1_Param : Node_Id;
11326 E2_Param : Node_Id;
11327 Ctype : Conformance_Type) return Boolean
11329 Param_E1 : Node_Id := E1_Param;
11330 Param_E2 : Node_Id := E2_Param;
11333 while Present (Param_E1) and then Present (Param_E2) loop
11334 if (Ctype >= Mode_Conformant) and then
11335 Ekind (Defining_Identifier (Param_E1)) /=
11336 Ekind (Defining_Identifier (Param_E2))
11341 (Find_Parameter_Type (Param_E1),
11342 Find_Parameter_Type (Param_E2),
11352 -- The candidate is not valid if one of the two lists contains
11353 -- more parameters than the other
11355 return No (Param_E1) and then No (Param_E2);
11356 end Check_Conforming_Parameters;
11358 ----------------------------------
11359 -- Matching_Entry_Or_Subprogram --
11360 ----------------------------------
11362 function Matching_Entry_Or_Subprogram
11363 (Conc_Typ : Entity_Id;
11364 Subp : Entity_Id) return Entity_Id
11369 E := First_Entity (Conc_Typ);
11370 while Present (E) loop
11371 if Chars (Subp) = Chars (E)
11372 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
11374 Check_Conforming_Parameters
11375 (First (Parameter_Specifications (Parent (E))),
11376 Next (First (Parameter_Specifications (Parent (Subp)))),
11386 end Matching_Entry_Or_Subprogram;
11388 -------------------------------------
11389 -- Matching_Dispatching_Subprogram --
11390 -------------------------------------
11392 function Matching_Dispatching_Subprogram
11393 (Conc_Typ : Entity_Id;
11394 Ent : Entity_Id) return Entity_Id
11399 -- Search for entities in the enclosing scope of this synchonized
11402 pragma Assert (Is_Concurrent_Type (Conc_Typ));
11403 Push_Scope (Scope (Conc_Typ));
11404 E := Current_Entity_In_Scope (Ent);
11407 while Present (E) loop
11408 if Scope (E) = Scope (Conc_Typ)
11409 and then Comes_From_Source (E)
11410 and then Ekind (E) = E_Procedure
11411 and then Present (First_Entity (E))
11412 and then Is_Controlling_Formal (First_Entity (E))
11413 and then Etype (First_Entity (E)) = Conc_Typ
11415 Check_Conforming_Parameters
11416 (First (Parameter_Specifications (Parent (Ent))),
11417 Next (First (Parameter_Specifications (Parent (E)))),
11418 Subtype_Conformant)
11427 end Matching_Dispatching_Subprogram;
11429 --------------------------------------------
11430 -- Matching_Original_Protected_Subprogram --
11431 --------------------------------------------
11433 function Matching_Original_Protected_Subprogram
11434 (Prot_Typ : Entity_Id;
11435 Subp : Entity_Id) return Entity_Id
11437 ICF : constant Boolean :=
11438 Is_Controlling_Formal (First_Entity (Subp));
11442 -- Temporarily decorate the first parameter of Subp as controlling
11443 -- formal, required to invoke Subtype_Conformant.
11445 Set_Is_Controlling_Formal (First_Entity (Subp));
11448 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
11450 while Present (E) loop
11451 if Scope (E) = Scope (Prot_Typ)
11452 and then Comes_From_Source (E)
11453 and then Ekind (Subp) = Ekind (E)
11454 and then Present (First_Entity (E))
11455 and then Is_Controlling_Formal (First_Entity (E))
11456 and then Etype (First_Entity (E)) = Prot_Typ
11457 and then Subtype_Conformant (Subp, E,
11458 Skip_Controlling_Formals => True)
11460 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11467 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11470 end Matching_Original_Protected_Subprogram;
11472 -------------------------------------
11473 -- Normalized_First_Parameter_Type --
11474 -------------------------------------
11476 function Normalized_First_Parameter_Type
11477 (E : Entity_Id) return Entity_Id
11479 Result : Entity_Id := Etype (First_Entity (E));
11481 if Ekind (Result) = E_Anonymous_Access_Type then
11482 Result := Designated_Type (Result);
11485 end Normalized_First_Parameter_Type;
11487 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11490 -- Case 1: E is a subprogram whose first formal is a concurrent type
11491 -- defined in the scope of E that has an entry or subprogram whose
11492 -- profile matches E.
11494 if Comes_From_Source (E)
11495 and then Is_Subprogram (E)
11496 and then Present (First_Entity (E))
11497 and then Is_Concurrent_Record_Type
11498 (Normalized_First_Parameter_Type (E))
11501 Scope (Corresponding_Concurrent_Type
11502 (Normalized_First_Parameter_Type (E)))
11505 (Matching_Entry_Or_Subprogram
11506 (Corresponding_Concurrent_Type
11507 (Normalized_First_Parameter_Type (E)),
11510 Report_Conflict (E,
11511 Matching_Entry_Or_Subprogram
11512 (Corresponding_Concurrent_Type
11513 (Normalized_First_Parameter_Type (E)),
11518 -- Case 2: E is an internally built dispatching subprogram of a
11519 -- protected type and there is a subprogram defined in the enclosing
11520 -- scope of the protected type that has the original name of E and
11521 -- its profile is conformant with the profile of E. We check the
11522 -- name of the original protected subprogram associated with E since
11523 -- the expander builds dispatching primitives of protected functions
11524 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11526 elsif not Comes_From_Source (E)
11527 and then Is_Subprogram (E)
11528 and then Present (First_Entity (E))
11529 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11530 and then Present (Original_Protected_Subprogram (E))
11533 (Matching_Original_Protected_Subprogram
11534 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11537 Report_Conflict (E,
11538 Matching_Original_Protected_Subprogram
11539 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11543 -- Case 3: E is an entry of a synchronized type and a matching
11544 -- procedure has been previously defined in the enclosing scope
11545 -- of the synchronized type.
11547 elsif Comes_From_Source (E)
11548 and then Ekind (E) = E_Entry
11550 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11552 Report_Conflict (E,
11553 Matching_Dispatching_Subprogram (Current_Scope, E));
11558 end Has_Matching_Entry_Or_Subprogram;
11560 ----------------------------
11561 -- Is_Private_Declaration --
11562 ----------------------------
11564 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11565 Decl : constant Node_Id := Unit_Declaration_Node (E);
11566 Priv_Decls : List_Id;
11569 if Is_Package_Or_Generic_Package (Current_Scope)
11570 and then In_Private_Part (Current_Scope)
11573 Private_Declarations (Package_Specification (Current_Scope));
11575 return In_Package_Body (Current_Scope)
11577 (Is_List_Member (Decl)
11578 and then List_Containing (Decl) = Priv_Decls)
11579 or else (Nkind (Parent (Decl)) = N_Package_Specification
11581 Is_Compilation_Unit
11582 (Defining_Entity (Parent (Decl)))
11583 and then List_Containing (Parent (Parent (Decl))) =
11588 end Is_Private_Declaration;
11590 --------------------------
11591 -- Is_Overriding_Alias --
11592 --------------------------
11594 function Is_Overriding_Alias
11595 (Old_E : Entity_Id;
11596 New_E : Entity_Id) return Boolean
11598 AO : constant Entity_Id := Alias (Old_E);
11599 AN : constant Entity_Id := Alias (New_E);
11602 return Scope (AO) /= Scope (AN)
11603 or else No (DTC_Entity (AO))
11604 or else No (DTC_Entity (AN))
11605 or else DT_Position (AO) = DT_Position (AN);
11606 end Is_Overriding_Alias;
11608 ---------------------
11609 -- Report_Conflict --
11610 ---------------------
11612 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
11614 Error_Msg_Sloc := Sloc (E);
11616 -- Generate message, with useful additional warning if in generic
11618 if Is_Generic_Unit (E) then
11619 Error_Msg_N ("previous generic unit cannot be overloaded", S);
11620 Error_Msg_N ("\& conflicts with declaration#", S);
11622 Error_Msg_N ("& conflicts with declaration#", S);
11624 end Report_Conflict;
11626 -- Start of processing for New_Overloaded_Entity
11629 -- We need to look for an entity that S may override. This must be a
11630 -- homonym in the current scope, so we look for the first homonym of
11631 -- S in the current scope as the starting point for the search.
11633 E := Current_Entity_In_Scope (S);
11635 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
11636 -- They are directly added to the list of primitive operations of
11637 -- Derived_Type, unless this is a rederivation in the private part
11638 -- of an operation that was already derived in the visible part of
11639 -- the current package.
11641 if Ada_Version >= Ada_2005
11642 and then Present (Derived_Type)
11643 and then Present (Alias (S))
11644 and then Is_Dispatching_Operation (Alias (S))
11645 and then Present (Find_Dispatching_Type (Alias (S)))
11646 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
11648 -- For private types, when the full-view is processed we propagate to
11649 -- the full view the non-overridden entities whose attribute "alias"
11650 -- references an interface primitive. These entities were added by
11651 -- Derive_Subprograms to ensure that interface primitives are
11654 -- Inside_Freeze_Actions is non zero when S corresponds with an
11655 -- internal entity that links an interface primitive with its
11656 -- covering primitive through attribute Interface_Alias (see
11657 -- Add_Internal_Interface_Entities).
11659 if Inside_Freezing_Actions = 0
11660 and then Is_Package_Or_Generic_Package (Current_Scope)
11661 and then In_Private_Part (Current_Scope)
11662 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11663 and then Nkind (Parent (S)) = N_Full_Type_Declaration
11664 and then Full_View (Defining_Identifier (Parent (E)))
11665 = Defining_Identifier (Parent (S))
11666 and then Alias (E) = Alias (S)
11668 Check_Operation_From_Private_View (S, E);
11669 Set_Is_Dispatching_Operation (S);
11674 Enter_Overloaded_Entity (S);
11675 Check_Dispatching_Operation (S, Empty);
11676 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11682 -- For synchronized types check conflicts of this entity with previously
11683 -- defined entities.
11685 if Ada_Version >= Ada_2005
11686 and then Has_Matching_Entry_Or_Subprogram (S)
11691 -- If there is no homonym then this is definitely not overriding
11694 Enter_Overloaded_Entity (S);
11695 Check_Dispatching_Operation (S, Empty);
11696 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11698 -- If subprogram has an explicit declaration, check whether it has an
11699 -- overriding indicator.
11701 if Comes_From_Source (S) then
11702 Check_Synchronized_Overriding (S, Overridden_Subp);
11704 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
11705 -- it may have overridden some hidden inherited primitive. Update
11706 -- Overridden_Subp to avoid spurious errors when checking the
11707 -- overriding indicator.
11709 if Ada_Version >= Ada_2012
11710 and then No (Overridden_Subp)
11711 and then Is_Dispatching_Operation (S)
11712 and then Present (Overridden_Operation (S))
11714 Overridden_Subp := Overridden_Operation (S);
11717 Check_Overriding_Indicator
11718 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11720 -- The Ghost policy in effect at the point of declaration of a
11721 -- parent subprogram and an overriding subprogram must match
11722 -- (SPARK RM 6.9(17)).
11724 Check_Ghost_Overriding (S, Overridden_Subp);
11727 -- If there is a homonym that is not overloadable, then we have an
11728 -- error, except for the special cases checked explicitly below.
11730 elsif not Is_Overloadable (E) then
11732 -- Check for spurious conflict produced by a subprogram that has the
11733 -- same name as that of the enclosing generic package. The conflict
11734 -- occurs within an instance, between the subprogram and the renaming
11735 -- declaration for the package. After the subprogram, the package
11736 -- renaming declaration becomes hidden.
11738 if Ekind (E) = E_Package
11739 and then Present (Renamed_Object (E))
11740 and then Renamed_Object (E) = Current_Scope
11741 and then Nkind (Parent (Renamed_Object (E))) =
11742 N_Package_Specification
11743 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
11746 Set_Is_Immediately_Visible (E, False);
11747 Enter_Overloaded_Entity (S);
11748 Set_Homonym (S, Homonym (E));
11749 Check_Dispatching_Operation (S, Empty);
11750 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
11752 -- If the subprogram is implicit it is hidden by the previous
11753 -- declaration. However if it is dispatching, it must appear in the
11754 -- dispatch table anyway, because it can be dispatched to even if it
11755 -- cannot be called directly.
11757 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
11758 Set_Scope (S, Current_Scope);
11760 if Is_Dispatching_Operation (Alias (S)) then
11761 Check_Dispatching_Operation (S, Empty);
11767 Report_Conflict (S, E);
11771 -- E exists and is overloadable
11774 Check_Synchronized_Overriding (S, Overridden_Subp);
11776 -- Loop through E and its homonyms to determine if any of them is
11777 -- the candidate for overriding by S.
11779 while Present (E) loop
11781 -- Definitely not interesting if not in the current scope
11783 if Scope (E) /= Current_Scope then
11786 -- A function can overload the name of an abstract state. The
11787 -- state can be viewed as a function with a profile that cannot
11788 -- be matched by anything.
11790 elsif Ekind (S) = E_Function
11791 and then Ekind (E) = E_Abstract_State
11793 Enter_Overloaded_Entity (S);
11796 -- Ada 2012 (AI05-0165): For internally generated bodies of null
11797 -- procedures locate the internally generated spec. We enforce
11798 -- mode conformance since a tagged type may inherit from
11799 -- interfaces several null primitives which differ only in
11800 -- the mode of the formals.
11802 elsif not Comes_From_Source (S)
11803 and then Is_Null_Procedure (S)
11804 and then not Mode_Conformant (E, S)
11808 -- Check if we have type conformance
11810 elsif Type_Conformant (E, S) then
11812 -- If the old and new entities have the same profile and one
11813 -- is not the body of the other, then this is an error, unless
11814 -- one of them is implicitly declared.
11816 -- There are some cases when both can be implicit, for example
11817 -- when both a literal and a function that overrides it are
11818 -- inherited in a derivation, or when an inherited operation
11819 -- of a tagged full type overrides the inherited operation of
11820 -- a private extension. Ada 83 had a special rule for the
11821 -- literal case. In Ada 95, the later implicit operation hides
11822 -- the former, and the literal is always the former. In the
11823 -- odd case where both are derived operations declared at the
11824 -- same point, both operations should be declared, and in that
11825 -- case we bypass the following test and proceed to the next
11826 -- part. This can only occur for certain obscure cases in
11827 -- instances, when an operation on a type derived from a formal
11828 -- private type does not override a homograph inherited from
11829 -- the actual. In subsequent derivations of such a type, the
11830 -- DT positions of these operations remain distinct, if they
11833 if Present (Alias (S))
11834 and then (No (Alias (E))
11835 or else Comes_From_Source (E)
11836 or else Is_Abstract_Subprogram (S)
11838 (Is_Dispatching_Operation (E)
11839 and then Is_Overriding_Alias (E, S)))
11840 and then Ekind (E) /= E_Enumeration_Literal
11842 -- When an derived operation is overloaded it may be due to
11843 -- the fact that the full view of a private extension
11844 -- re-inherits. It has to be dealt with.
11846 if Is_Package_Or_Generic_Package (Current_Scope)
11847 and then In_Private_Part (Current_Scope)
11849 Check_Operation_From_Private_View (S, E);
11852 -- In any case the implicit operation remains hidden by the
11853 -- existing declaration, which is overriding. Indicate that
11854 -- E overrides the operation from which S is inherited.
11856 if Present (Alias (S)) then
11857 Set_Overridden_Operation (E, Alias (S));
11858 Inherit_Subprogram_Contract (E, Alias (S));
11861 Set_Overridden_Operation (E, S);
11862 Inherit_Subprogram_Contract (E, S);
11865 -- When a dispatching operation overrides an inherited
11866 -- subprogram, it shall be subtype conformant with the
11867 -- inherited subprogram (RM 3.9.2 (10.2)).
11869 if Comes_From_Source (E)
11870 and then Is_Dispatching_Operation (E)
11871 and then Find_Dispatching_Type (S)
11872 = Find_Dispatching_Type (E)
11874 Check_Subtype_Conformant (E, S);
11877 if Comes_From_Source (E) then
11878 Check_Overriding_Indicator (E, S, Is_Primitive => False);
11880 -- The Ghost policy in effect at the point of declaration
11881 -- of a parent subprogram and an overriding subprogram
11882 -- must match (SPARK RM 6.9(17)).
11884 Check_Ghost_Overriding (E, S);
11889 -- Within an instance, the renaming declarations for actual
11890 -- subprograms may become ambiguous, but they do not hide each
11893 elsif Ekind (E) /= E_Entry
11894 and then not Comes_From_Source (E)
11895 and then not Is_Generic_Instance (E)
11896 and then (Present (Alias (E))
11897 or else Is_Intrinsic_Subprogram (E))
11898 and then (not In_Instance
11899 or else No (Parent (E))
11900 or else Nkind (Unit_Declaration_Node (E)) /=
11901 N_Subprogram_Renaming_Declaration)
11903 -- A subprogram child unit is not allowed to override an
11904 -- inherited subprogram (10.1.1(20)).
11906 if Is_Child_Unit (S) then
11908 ("child unit overrides inherited subprogram in parent",
11913 if Is_Non_Overriding_Operation (E, S) then
11914 Enter_Overloaded_Entity (S);
11916 if No (Derived_Type)
11917 or else Is_Tagged_Type (Derived_Type)
11919 Check_Dispatching_Operation (S, Empty);
11925 -- E is a derived operation or an internal operator which
11926 -- is being overridden. Remove E from further visibility.
11927 -- Furthermore, if E is a dispatching operation, it must be
11928 -- replaced in the list of primitive operations of its type
11929 -- (see Override_Dispatching_Operation).
11931 Overridden_Subp := E;
11933 -- It is possible for E to be in the current scope and
11934 -- yet not in the entity chain. This can only occur in a
11935 -- generic context where E is an implicit concatenation
11936 -- in the formal part, because in a generic body the
11937 -- entity chain starts with the formals.
11939 -- In GNATprove mode, a wrapper for an operation with
11940 -- axiomatization may be a homonym of another declaration
11941 -- for an actual subprogram (needs refinement ???).
11943 if No (Prev_Entity (E)) then
11945 and then GNATprove_Mode
11947 Nkind (Original_Node (Unit_Declaration_Node (S))) =
11948 N_Subprogram_Renaming_Declaration
11952 pragma Assert (Chars (E) = Name_Op_Concat);
11957 -- E must be removed both from the entity_list of the
11958 -- current scope, and from the visibility chain.
11960 if Debug_Flag_E then
11961 Write_Str ("Override implicit operation ");
11962 Write_Int (Int (E));
11966 -- If E is a predefined concatenation, it stands for four
11967 -- different operations. As a result, a single explicit
11968 -- declaration does not hide it. In a possible ambiguous
11969 -- situation, Disambiguate chooses the user-defined op,
11970 -- so it is correct to retain the previous internal one.
11972 if Chars (E) /= Name_Op_Concat
11973 or else Ekind (E) /= E_Operator
11975 -- For nondispatching derived operations that are
11976 -- overridden by a subprogram declared in the private
11977 -- part of a package, we retain the derived subprogram
11978 -- but mark it as not immediately visible. If the
11979 -- derived operation was declared in the visible part
11980 -- then this ensures that it will still be visible
11981 -- outside the package with the proper signature
11982 -- (calls from outside must also be directed to this
11983 -- version rather than the overriding one, unlike the
11984 -- dispatching case). Calls from inside the package
11985 -- will still resolve to the overriding subprogram
11986 -- since the derived one is marked as not visible
11987 -- within the package.
11989 -- If the private operation is dispatching, we achieve
11990 -- the overriding by keeping the implicit operation
11991 -- but setting its alias to be the overriding one. In
11992 -- this fashion the proper body is executed in all
11993 -- cases, but the original signature is used outside
11996 -- If the overriding is not in the private part, we
11997 -- remove the implicit operation altogether.
11999 if Is_Private_Declaration (S) then
12000 if not Is_Dispatching_Operation (E) then
12001 Set_Is_Immediately_Visible (E, False);
12003 -- Work done in Override_Dispatching_Operation, so
12004 -- nothing else needs to be done here.
12010 Remove_Entity_And_Homonym (E);
12014 Enter_Overloaded_Entity (S);
12016 -- For entities generated by Derive_Subprograms the
12017 -- overridden operation is the inherited primitive
12018 -- (which is available through the attribute alias).
12020 if not (Comes_From_Source (E))
12021 and then Is_Dispatching_Operation (E)
12022 and then Find_Dispatching_Type (E) =
12023 Find_Dispatching_Type (S)
12024 and then Present (Alias (E))
12025 and then Comes_From_Source (Alias (E))
12027 Set_Overridden_Operation (S, Alias (E));
12028 Inherit_Subprogram_Contract (S, Alias (E));
12030 -- Normal case of setting entity as overridden
12032 -- Note: Static_Initialization and Overridden_Operation
12033 -- attributes use the same field in subprogram entities.
12034 -- Static_Initialization is only defined for internal
12035 -- initialization procedures, where Overridden_Operation
12036 -- is irrelevant. Therefore the setting of this attribute
12037 -- must check whether the target is an init_proc.
12039 elsif not Is_Init_Proc (S) then
12040 Set_Overridden_Operation (S, E);
12041 Inherit_Subprogram_Contract (S, E);
12044 Check_Overriding_Indicator (S, E, Is_Primitive => True);
12046 -- The Ghost policy in effect at the point of declaration
12047 -- of a parent subprogram and an overriding subprogram
12048 -- must match (SPARK RM 6.9(17)).
12050 Check_Ghost_Overriding (S, E);
12052 -- If S is a user-defined subprogram or a null procedure
12053 -- expanded to override an inherited null procedure, or a
12054 -- predefined dispatching primitive then indicate that E
12055 -- overrides the operation from which S is inherited.
12057 if Comes_From_Source (S)
12059 (Present (Parent (S))
12060 and then Nkind (Parent (S)) = N_Procedure_Specification
12061 and then Null_Present (Parent (S)))
12063 (Present (Alias (E))
12065 Is_Predefined_Dispatching_Operation (Alias (E)))
12067 if Present (Alias (E)) then
12068 Set_Overridden_Operation (S, Alias (E));
12069 Inherit_Subprogram_Contract (S, Alias (E));
12073 if Is_Dispatching_Operation (E) then
12075 -- An overriding dispatching subprogram inherits the
12076 -- convention of the overridden subprogram (AI-117).
12078 Set_Convention (S, Convention (E));
12079 Check_Dispatching_Operation (S, E);
12082 Check_Dispatching_Operation (S, Empty);
12085 Check_For_Primitive_Subprogram
12086 (Is_Primitive_Subp, Is_Overriding => True);
12087 goto Check_Inequality;
12089 -- Apparent redeclarations in instances can occur when two
12090 -- formal types get the same actual type. The subprograms in
12091 -- in the instance are legal, even if not callable from the
12092 -- outside. Calls from within are disambiguated elsewhere.
12093 -- For dispatching operations in the visible part, the usual
12094 -- rules apply, and operations with the same profile are not
12095 -- legal (B830001).
12097 elsif (In_Instance_Visible_Part
12098 and then not Is_Dispatching_Operation (E))
12099 or else In_Instance_Not_Visible
12103 -- Here we have a real error (identical profile)
12106 Error_Msg_Sloc := Sloc (E);
12108 -- Avoid cascaded errors if the entity appears in
12109 -- subsequent calls.
12111 Set_Scope (S, Current_Scope);
12113 -- Generate error, with extra useful warning for the case
12114 -- of a generic instance with no completion.
12116 if Is_Generic_Instance (S)
12117 and then not Has_Completion (E)
12120 ("instantiation cannot provide body for&", S);
12121 Error_Msg_N ("\& conflicts with declaration#", S);
12123 Error_Msg_N ("& conflicts with declaration#", S);
12130 -- If one subprogram has an access parameter and the other
12131 -- a parameter of an access type, calls to either might be
12132 -- ambiguous. Verify that parameters match except for the
12133 -- access parameter.
12135 if May_Hide_Profile then
12141 F1 := First_Formal (S);
12142 F2 := First_Formal (E);
12143 while Present (F1) and then Present (F2) loop
12144 if Is_Access_Type (Etype (F1)) then
12145 if not Is_Access_Type (Etype (F2))
12146 or else not Conforming_Types
12147 (Designated_Type (Etype (F1)),
12148 Designated_Type (Etype (F2)),
12151 May_Hide_Profile := False;
12155 not Conforming_Types
12156 (Etype (F1), Etype (F2), Type_Conformant)
12158 May_Hide_Profile := False;
12165 if May_Hide_Profile
12169 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
12178 -- On exit, we know that S is a new entity
12180 Enter_Overloaded_Entity (S);
12181 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
12182 Check_Overriding_Indicator
12183 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
12185 -- The Ghost policy in effect at the point of declaration of a parent
12186 -- subprogram and an overriding subprogram must match
12187 -- (SPARK RM 6.9(17)).
12189 Check_Ghost_Overriding (S, Overridden_Subp);
12191 -- If S is a derived operation for an untagged type then by
12192 -- definition it's not a dispatching operation (even if the parent
12193 -- operation was dispatching), so Check_Dispatching_Operation is not
12194 -- called in that case.
12196 if No (Derived_Type)
12197 or else Is_Tagged_Type (Derived_Type)
12199 Check_Dispatching_Operation (S, Empty);
12203 -- If this is a user-defined equality operator that is not a derived
12204 -- subprogram, create the corresponding inequality. If the operation is
12205 -- dispatching, the expansion is done elsewhere, and we do not create
12206 -- an explicit inequality operation.
12208 <<Check_Inequality>>
12209 if Chars (S) = Name_Op_Eq
12210 and then Etype (S) = Standard_Boolean
12211 and then Present (Parent (S))
12212 and then not Is_Dispatching_Operation (S)
12214 Make_Inequality_Operator (S);
12215 Check_Untagged_Equality (S);
12217 end New_Overloaded_Entity;
12219 ----------------------------------
12220 -- Preanalyze_Formal_Expression --
12221 ----------------------------------
12223 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
12224 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
12226 In_Spec_Expression := True;
12227 Preanalyze_With_Freezing_And_Resolve (N, T);
12228 In_Spec_Expression := Save_In_Spec_Expression;
12229 end Preanalyze_Formal_Expression;
12231 ---------------------
12232 -- Process_Formals --
12233 ---------------------
12235 procedure Process_Formals
12237 Related_Nod : Node_Id)
12239 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
12240 -- Determine whether an access type designates a type coming from a
12243 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
12244 -- Check whether the default has a class-wide type. After analysis the
12245 -- default has the type of the formal, so we must also check explicitly
12246 -- for an access attribute.
12248 ----------------------------------
12249 -- Designates_From_Limited_With --
12250 ----------------------------------
12252 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
12253 Desig : Entity_Id := Typ;
12256 if Is_Access_Type (Desig) then
12257 Desig := Directly_Designated_Type (Desig);
12260 if Is_Class_Wide_Type (Desig) then
12261 Desig := Root_Type (Desig);
12265 Ekind (Desig) = E_Incomplete_Type
12266 and then From_Limited_With (Desig);
12267 end Designates_From_Limited_With;
12269 ---------------------------
12270 -- Is_Class_Wide_Default --
12271 ---------------------------
12273 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
12275 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
12276 or else (Nkind (D) = N_Attribute_Reference
12277 and then Attribute_Name (D) = Name_Access
12278 and then Is_Class_Wide_Type (Etype (Prefix (D))));
12279 end Is_Class_Wide_Default;
12283 Context : constant Node_Id := Parent (Parent (T));
12285 Formal : Entity_Id;
12286 Formal_Type : Entity_Id;
12287 Param_Spec : Node_Id;
12290 Num_Out_Params : Nat := 0;
12291 First_Out_Param : Entity_Id := Empty;
12292 -- Used for setting Is_Only_Out_Parameter
12294 -- Start of processing for Process_Formals
12297 -- In order to prevent premature use of the formals in the same formal
12298 -- part, the Ekind is left undefined until all default expressions are
12299 -- analyzed. The Ekind is established in a separate loop at the end.
12301 Param_Spec := First (T);
12302 while Present (Param_Spec) loop
12303 Formal := Defining_Identifier (Param_Spec);
12304 Set_Never_Set_In_Source (Formal, True);
12305 Enter_Name (Formal);
12307 -- Case of ordinary parameters
12309 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
12310 Find_Type (Parameter_Type (Param_Spec));
12311 Ptype := Parameter_Type (Param_Spec);
12313 if Ptype = Error then
12317 -- Protect against malformed parameter types
12319 if Nkind (Ptype) not in N_Has_Entity then
12320 Formal_Type := Any_Type;
12322 Formal_Type := Entity (Ptype);
12325 if Is_Incomplete_Type (Formal_Type)
12327 (Is_Class_Wide_Type (Formal_Type)
12328 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
12330 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
12331 -- primitive operations, as long as their completion is
12332 -- in the same declarative part. If in the private part
12333 -- this means that the type cannot be a Taft-amendment type.
12334 -- Check is done on package exit. For access to subprograms,
12335 -- the use is legal for Taft-amendment types.
12337 -- Ada 2012: tagged incomplete types are allowed as generic
12338 -- formal types. They do not introduce dependencies and the
12339 -- corresponding generic subprogram does not have a delayed
12340 -- freeze, because it does not need a freeze node. However,
12341 -- it is still the case that untagged incomplete types cannot
12342 -- be Taft-amendment types and must be completed in private
12343 -- part, so the subprogram must appear in the list of private
12344 -- dependents of the type.
12346 if Is_Tagged_Type (Formal_Type)
12347 or else (Ada_Version >= Ada_2012
12348 and then not From_Limited_With (Formal_Type)
12349 and then not Is_Generic_Type (Formal_Type))
12351 if Ekind (Scope (Current_Scope)) = E_Package
12352 and then not Is_Generic_Type (Formal_Type)
12353 and then not Is_Class_Wide_Type (Formal_Type)
12355 if Nkind (Parent (T)) not in
12356 N_Access_Function_Definition |
12357 N_Access_Procedure_Definition
12359 Append_Elmt (Current_Scope,
12360 Private_Dependents (Base_Type (Formal_Type)));
12362 -- Freezing is delayed to ensure that Register_Prim
12363 -- will get called for this operation, which is needed
12364 -- in cases where static dispatch tables aren't built.
12365 -- (Note that the same is done for controlling access
12366 -- parameter cases in function Access_Definition.)
12368 if not Is_Thunk (Current_Scope) then
12369 Set_Has_Delayed_Freeze (Current_Scope);
12374 elsif Nkind (Parent (T)) not in N_Access_Function_Definition
12375 | N_Access_Procedure_Definition
12377 -- AI05-0151: Tagged incomplete types are allowed in all
12378 -- formal parts. Untagged incomplete types are not allowed
12379 -- in bodies. Limited views of either kind are not allowed
12380 -- if there is no place at which the non-limited view can
12381 -- become available.
12383 -- Incomplete formal untagged types are not allowed in
12384 -- subprogram bodies (but are legal in their declarations).
12385 -- This excludes bodies created for null procedures, which
12386 -- are basic declarations.
12388 if Is_Generic_Type (Formal_Type)
12389 and then not Is_Tagged_Type (Formal_Type)
12390 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
12393 ("invalid use of formal incomplete type", Param_Spec);
12395 elsif Ada_Version >= Ada_2012 then
12396 if Is_Tagged_Type (Formal_Type)
12397 and then (not From_Limited_With (Formal_Type)
12398 or else not In_Package_Body)
12402 elsif Nkind (Context) in N_Accept_Statement
12403 | N_Accept_Alternative
12405 or else (Nkind (Context) = N_Subprogram_Body
12406 and then Comes_From_Source (Context))
12409 ("invalid use of untagged incomplete type &",
12410 Ptype, Formal_Type);
12415 ("invalid use of incomplete type&",
12416 Param_Spec, Formal_Type);
12418 -- Further checks on the legality of incomplete types
12419 -- in formal parts are delayed until the freeze point
12420 -- of the enclosing subprogram or access to subprogram.
12424 elsif Ekind (Formal_Type) = E_Void then
12426 ("premature use of&",
12427 Parameter_Type (Param_Spec), Formal_Type);
12430 -- Ada 2012 (AI-142): Handle aliased parameters
12432 if Ada_Version >= Ada_2012
12433 and then Aliased_Present (Param_Spec)
12435 Set_Is_Aliased (Formal);
12437 -- AI12-001: All aliased objects are considered to be specified
12438 -- as independently addressable (RM C.6(8.1/4)).
12440 Set_Is_Independent (Formal);
12443 -- Ada 2005 (AI-231): Create and decorate an internal subtype
12444 -- declaration corresponding to the null-excluding type of the
12445 -- formal in the enclosing scope. Finally, replace the parameter
12446 -- type of the formal with the internal subtype.
12448 if Ada_Version >= Ada_2005
12449 and then Null_Exclusion_Present (Param_Spec)
12451 if not Is_Access_Type (Formal_Type) then
12453 ("`NOT NULL` allowed only for an access type", Param_Spec);
12456 if Can_Never_Be_Null (Formal_Type)
12457 and then Comes_From_Source (Related_Nod)
12460 ("`NOT NULL` not allowed (& already excludes null)",
12461 Param_Spec, Formal_Type);
12465 Create_Null_Excluding_Itype
12467 Related_Nod => Related_Nod,
12468 Scope_Id => Scope (Current_Scope));
12470 -- If the designated type of the itype is an itype that is
12471 -- not frozen yet, we set the Has_Delayed_Freeze attribute
12472 -- on the access subtype, to prevent order-of-elaboration
12473 -- issues in the backend.
12476 -- type T is access procedure;
12477 -- procedure Op (O : not null T);
12479 if Is_Itype (Directly_Designated_Type (Formal_Type))
12481 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12483 Set_Has_Delayed_Freeze (Formal_Type);
12488 -- An access formal type
12492 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12494 -- No need to continue if we already notified errors
12496 if not Present (Formal_Type) then
12500 -- Ada 2005 (AI-254)
12503 AD : constant Node_Id :=
12504 Access_To_Subprogram_Definition
12505 (Parameter_Type (Param_Spec));
12507 if Present (AD) and then Protected_Present (AD) then
12509 Replace_Anonymous_Access_To_Protected_Subprogram
12515 Set_Etype (Formal, Formal_Type);
12517 -- Deal with default expression if present
12519 Default := Expression (Param_Spec);
12521 if Present (Default) then
12522 if Out_Present (Param_Spec) then
12524 ("default initialization only allowed for IN parameters",
12528 -- Do the special preanalysis of the expression (see section on
12529 -- "Handling of Default Expressions" in the spec of package Sem).
12531 Preanalyze_Formal_Expression (Default, Formal_Type);
12533 -- An access to constant cannot be the default for
12534 -- an access parameter that is an access to variable.
12536 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12537 and then not Is_Access_Constant (Formal_Type)
12538 and then Is_Access_Type (Etype (Default))
12539 and then Is_Access_Constant (Etype (Default))
12542 ("formal that is access to variable cannot be initialized "
12543 & "with an access-to-constant expression", Default);
12546 -- Check that the designated type of an access parameter's default
12547 -- is not a class-wide type unless the parameter's designated type
12548 -- is also class-wide.
12550 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12551 and then not Designates_From_Limited_With (Formal_Type)
12552 and then Is_Class_Wide_Default (Default)
12553 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
12556 ("access to class-wide expression not allowed here", Default);
12559 -- Check incorrect use of dynamically tagged expressions
12561 if Is_Tagged_Type (Formal_Type) then
12562 Check_Dynamically_Tagged_Expression
12564 Typ => Formal_Type,
12565 Related_Nod => Default);
12569 -- Ada 2005 (AI-231): Static checks
12571 if Ada_Version >= Ada_2005
12572 and then Is_Access_Type (Etype (Formal))
12573 and then Can_Never_Be_Null (Etype (Formal))
12575 Null_Exclusion_Static_Checks (Param_Spec);
12578 -- The following checks are relevant only when SPARK_Mode is on as
12579 -- these are not standard Ada legality rules.
12581 if SPARK_Mode = On then
12582 if Ekind (Scope (Formal)) in E_Function | E_Generic_Function then
12584 -- A function cannot have a parameter of mode IN OUT or OUT
12587 if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then
12589 ("function cannot have parameter of mode `OUT` or "
12590 & "`IN OUT`", Formal);
12593 -- A procedure cannot have an effectively volatile formal
12594 -- parameter of mode IN because it behaves as a constant
12595 -- (SPARK RM 7.1.3(4)).
12597 elsif Ekind (Scope (Formal)) = E_Procedure
12598 and then Ekind (Formal) = E_In_Parameter
12599 and then Is_Effectively_Volatile (Formal)
12602 ("formal parameter of mode `IN` cannot be volatile", Formal);
12606 -- Deal with aspects on formal parameters. Only Unreferenced is
12607 -- supported for the time being.
12609 if Has_Aspects (Param_Spec) then
12611 Aspect : Node_Id := First (Aspect_Specifications (Param_Spec));
12613 while Present (Aspect) loop
12614 if Chars (Identifier (Aspect)) = Name_Unreferenced then
12615 Set_Has_Pragma_Unreferenced (Formal);
12618 ("unsupported aspect& on parameter",
12619 Aspect, Identifier (Aspect));
12631 -- If this is the formal part of a function specification, analyze the
12632 -- subtype mark in the context where the formals are visible but not
12633 -- yet usable, and may hide outer homographs.
12635 if Nkind (Related_Nod) = N_Function_Specification then
12636 Analyze_Return_Type (Related_Nod);
12639 -- Now set the kind (mode) of each formal
12641 Param_Spec := First (T);
12642 while Present (Param_Spec) loop
12643 Formal := Defining_Identifier (Param_Spec);
12644 Set_Formal_Mode (Formal);
12646 if Ekind (Formal) = E_In_Parameter then
12647 Set_Default_Value (Formal, Expression (Param_Spec));
12649 if Present (Expression (Param_Spec)) then
12650 Default := Expression (Param_Spec);
12652 if Is_Scalar_Type (Etype (Default)) then
12653 if Nkind (Parameter_Type (Param_Spec)) /=
12654 N_Access_Definition
12656 Formal_Type := Entity (Parameter_Type (Param_Spec));
12660 (Related_Nod, Parameter_Type (Param_Spec));
12663 Apply_Scalar_Range_Check (Default, Formal_Type);
12667 elsif Ekind (Formal) = E_Out_Parameter then
12668 Num_Out_Params := Num_Out_Params + 1;
12670 if Num_Out_Params = 1 then
12671 First_Out_Param := Formal;
12674 elsif Ekind (Formal) = E_In_Out_Parameter then
12675 Num_Out_Params := Num_Out_Params + 1;
12678 -- Skip remaining processing if formal type was in error
12680 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
12681 goto Next_Parameter;
12684 -- Force call by reference if aliased
12687 Conv : constant Convention_Id := Convention (Etype (Formal));
12689 if Is_Aliased (Formal) then
12690 Set_Mechanism (Formal, By_Reference);
12692 -- Warn if user asked this to be passed by copy
12694 if Conv = Convention_Ada_Pass_By_Copy then
12696 ("cannot pass aliased parameter & by copy??", Formal);
12699 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
12701 elsif Conv = Convention_Ada_Pass_By_Copy then
12702 Set_Mechanism (Formal, By_Copy);
12704 elsif Conv = Convention_Ada_Pass_By_Reference then
12705 Set_Mechanism (Formal, By_Reference);
12713 if Present (First_Out_Param) and then Num_Out_Params = 1 then
12714 Set_Is_Only_Out_Parameter (First_Out_Param);
12716 end Process_Formals;
12718 ----------------------------
12719 -- Reference_Body_Formals --
12720 ----------------------------
12722 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12727 if Error_Posted (Spec) then
12731 -- Iterate over both lists. They may be of different lengths if the two
12732 -- specs are not conformant.
12734 Fs := First_Formal (Spec);
12735 Fb := First_Formal (Bod);
12736 while Present (Fs) and then Present (Fb) loop
12737 Generate_Reference (Fs, Fb, 'b');
12739 if Style_Check then
12740 Style.Check_Identifier (Fb, Fs);
12743 Set_Spec_Entity (Fb, Fs);
12744 Set_Referenced (Fs, False);
12748 end Reference_Body_Formals;
12750 -------------------------
12751 -- Set_Actual_Subtypes --
12752 -------------------------
12754 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12756 Formal : Entity_Id;
12758 First_Stmt : Node_Id := Empty;
12759 AS_Needed : Boolean;
12762 -- If this is an empty initialization procedure, no need to create
12763 -- actual subtypes (small optimization).
12765 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12768 -- Within a predicate function we do not want to generate local
12769 -- subtypes that may generate nested predicate functions.
12771 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
12775 -- The subtype declarations may freeze the formals. The body generated
12776 -- for an expression function is not a freeze point, so do not emit
12777 -- these declarations (small loss of efficiency in rare cases).
12779 if Nkind (N) = N_Subprogram_Body
12780 and then Was_Expression_Function (N)
12785 Formal := First_Formal (Subp);
12786 while Present (Formal) loop
12787 T := Etype (Formal);
12789 -- We never need an actual subtype for a constrained formal
12791 if Is_Constrained (T) then
12792 AS_Needed := False;
12794 -- If we have unknown discriminants, then we do not need an actual
12795 -- subtype, or more accurately we cannot figure it out. Note that
12796 -- all class-wide types have unknown discriminants.
12798 elsif Has_Unknown_Discriminants (T) then
12799 AS_Needed := False;
12801 -- At this stage we have an unconstrained type that may need an
12802 -- actual subtype. For sure the actual subtype is needed if we have
12803 -- an unconstrained array type. However, in an instance, the type
12804 -- may appear as a subtype of the full view, while the actual is
12805 -- in fact private (in which case no actual subtype is needed) so
12806 -- check the kind of the base type.
12808 elsif Is_Array_Type (Base_Type (T)) then
12811 -- The only other case needing an actual subtype is an unconstrained
12812 -- record type which is an IN parameter (we cannot generate actual
12813 -- subtypes for the OUT or IN OUT case, since an assignment can
12814 -- change the discriminant values. However we exclude the case of
12815 -- initialization procedures, since discriminants are handled very
12816 -- specially in this context, see the section entitled "Handling of
12817 -- Discriminants" in Einfo.
12819 -- We also exclude the case of Discrim_SO_Functions (functions used
12820 -- in front-end layout mode for size/offset values), since in such
12821 -- functions only discriminants are referenced, and not only are such
12822 -- subtypes not needed, but they cannot always be generated, because
12823 -- of order of elaboration issues.
12825 elsif Is_Record_Type (T)
12826 and then Ekind (Formal) = E_In_Parameter
12827 and then Chars (Formal) /= Name_uInit
12828 and then not Is_Unchecked_Union (T)
12829 and then not Is_Discrim_SO_Function (Subp)
12833 -- All other cases do not need an actual subtype
12836 AS_Needed := False;
12839 -- Generate actual subtypes for unconstrained arrays and
12840 -- unconstrained discriminated records.
12843 if Nkind (N) = N_Accept_Statement then
12845 -- If expansion is active, the formal is replaced by a local
12846 -- variable that renames the corresponding entry of the
12847 -- parameter block, and it is this local variable that may
12848 -- require an actual subtype.
12850 if Expander_Active then
12851 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12853 Decl := Build_Actual_Subtype (T, Formal);
12856 if Present (Handled_Statement_Sequence (N)) then
12858 First (Statements (Handled_Statement_Sequence (N)));
12859 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12860 Mark_Rewrite_Insertion (Decl);
12862 -- If the accept statement has no body, there will be no
12863 -- reference to the actuals, so no need to compute actual
12870 Decl := Build_Actual_Subtype (T, Formal);
12871 Prepend (Decl, Declarations (N));
12872 Mark_Rewrite_Insertion (Decl);
12875 -- The declaration uses the bounds of an existing object, and
12876 -- therefore needs no constraint checks.
12878 Analyze (Decl, Suppress => All_Checks);
12879 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
12881 -- We need to freeze manually the generated type when it is
12882 -- inserted anywhere else than in a declarative part.
12884 if Present (First_Stmt) then
12885 Insert_List_Before_And_Analyze (First_Stmt,
12886 Freeze_Entity (Defining_Identifier (Decl), N));
12888 -- Ditto if the type has a dynamic predicate, because the
12889 -- generated function will mention the actual subtype. The
12890 -- predicate may come from an explicit aspect of be inherited.
12892 elsif Has_Predicates (T) then
12893 Insert_List_After_And_Analyze (Decl,
12894 Freeze_Entity (Defining_Identifier (Decl), N));
12897 if Nkind (N) = N_Accept_Statement
12898 and then Expander_Active
12900 Set_Actual_Subtype (Renamed_Object (Formal),
12901 Defining_Identifier (Decl));
12903 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12907 Next_Formal (Formal);
12909 end Set_Actual_Subtypes;
12911 ---------------------
12912 -- Set_Formal_Mode --
12913 ---------------------
12915 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12916 Spec : constant Node_Id := Parent (Formal_Id);
12917 Id : constant Entity_Id := Scope (Formal_Id);
12920 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12921 -- since we ensure that corresponding actuals are always valid at the
12922 -- point of the call.
12924 if Out_Present (Spec) then
12926 or else Is_Subprogram_Or_Generic_Subprogram (Id)
12928 Set_Has_Out_Or_In_Out_Parameter (Id, True);
12931 if Ekind (Id) in E_Function | E_Generic_Function then
12933 -- [IN] OUT parameters allowed for functions in Ada 2012
12935 if Ada_Version >= Ada_2012 then
12937 -- Even in Ada 2012 operators can only have IN parameters
12939 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12940 Error_Msg_N ("operators can only have IN parameters", Spec);
12943 if In_Present (Spec) then
12944 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12946 Set_Ekind (Formal_Id, E_Out_Parameter);
12949 -- But not in earlier versions of Ada
12952 Error_Msg_N ("functions can only have IN parameters", Spec);
12953 Set_Ekind (Formal_Id, E_In_Parameter);
12956 elsif In_Present (Spec) then
12957 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12960 Set_Ekind (Formal_Id, E_Out_Parameter);
12961 Set_Never_Set_In_Source (Formal_Id, True);
12962 Set_Is_True_Constant (Formal_Id, False);
12963 Set_Current_Value (Formal_Id, Empty);
12967 Set_Ekind (Formal_Id, E_In_Parameter);
12970 -- Set Is_Known_Non_Null for access parameters since the language
12971 -- guarantees that access parameters are always non-null. We also set
12972 -- Can_Never_Be_Null, since there is no way to change the value.
12974 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12976 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12977 -- null; In Ada 2005, only if then null_exclusion is explicit.
12979 if Ada_Version < Ada_2005
12980 or else Can_Never_Be_Null (Etype (Formal_Id))
12982 Set_Is_Known_Non_Null (Formal_Id);
12983 Set_Can_Never_Be_Null (Formal_Id);
12986 -- Ada 2005 (AI-231): Null-exclusion access subtype
12988 elsif Is_Access_Type (Etype (Formal_Id))
12989 and then Can_Never_Be_Null (Etype (Formal_Id))
12991 Set_Is_Known_Non_Null (Formal_Id);
12993 -- We can also set Can_Never_Be_Null (thus preventing some junk
12994 -- access checks) for the case of an IN parameter, which cannot
12995 -- be changed, or for an IN OUT parameter, which can be changed but
12996 -- not to a null value. But for an OUT parameter, the initial value
12997 -- passed in can be null, so we can't set this flag in that case.
12999 if Ekind (Formal_Id) /= E_Out_Parameter then
13000 Set_Can_Never_Be_Null (Formal_Id);
13004 Set_Mechanism (Formal_Id, Default_Mechanism);
13005 Set_Formal_Validity (Formal_Id);
13006 end Set_Formal_Mode;
13008 -------------------------
13009 -- Set_Formal_Validity --
13010 -------------------------
13012 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
13014 -- If no validity checking, then we cannot assume anything about the
13015 -- validity of parameters, since we do not know there is any checking
13016 -- of the validity on the call side.
13018 if not Validity_Checks_On then
13021 -- If validity checking for parameters is enabled, this means we are
13022 -- not supposed to make any assumptions about argument values.
13024 elsif Validity_Check_Parameters then
13027 -- If we are checking in parameters, we will assume that the caller is
13028 -- also checking parameters, so we can assume the parameter is valid.
13030 elsif Ekind (Formal_Id) = E_In_Parameter
13031 and then Validity_Check_In_Params
13033 Set_Is_Known_Valid (Formal_Id, True);
13035 -- Similar treatment for IN OUT parameters
13037 elsif Ekind (Formal_Id) = E_In_Out_Parameter
13038 and then Validity_Check_In_Out_Params
13040 Set_Is_Known_Valid (Formal_Id, True);
13042 end Set_Formal_Validity;
13044 ------------------------
13045 -- Subtype_Conformant --
13046 ------------------------
13048 function Subtype_Conformant
13049 (New_Id : Entity_Id;
13050 Old_Id : Entity_Id;
13051 Skip_Controlling_Formals : Boolean := False) return Boolean
13055 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
13056 Skip_Controlling_Formals => Skip_Controlling_Formals);
13058 end Subtype_Conformant;
13060 ---------------------
13061 -- Type_Conformant --
13062 ---------------------
13064 function Type_Conformant
13065 (New_Id : Entity_Id;
13066 Old_Id : Entity_Id;
13067 Skip_Controlling_Formals : Boolean := False) return Boolean
13071 May_Hide_Profile := False;
13073 (New_Id, Old_Id, Type_Conformant, False, Result,
13074 Skip_Controlling_Formals => Skip_Controlling_Formals);
13076 end Type_Conformant;
13078 -------------------------------
13079 -- Valid_Operator_Definition --
13080 -------------------------------
13082 procedure Valid_Operator_Definition (Designator : Entity_Id) is
13085 Id : constant Name_Id := Chars (Designator);
13089 F := First_Formal (Designator);
13090 while Present (F) loop
13093 if Present (Default_Value (F)) then
13095 ("default values not allowed for operator parameters",
13098 -- For function instantiations that are operators, we must check
13099 -- separately that the corresponding generic only has in-parameters.
13100 -- For subprogram declarations this is done in Set_Formal_Mode. Such
13101 -- an error could not arise in earlier versions of the language.
13103 elsif Ekind (F) /= E_In_Parameter then
13104 Error_Msg_N ("operators can only have IN parameters", F);
13110 -- Verify that user-defined operators have proper number of arguments
13111 -- First case of operators which can only be unary
13113 if Id in Name_Op_Not | Name_Op_Abs then
13116 -- Case of operators which can be unary or binary
13118 elsif Id in Name_Op_Add | Name_Op_Subtract then
13119 N_OK := (N in 1 .. 2);
13121 -- All other operators can only be binary
13129 ("incorrect number of arguments for operator", Designator);
13133 and then Base_Type (Etype (Designator)) = Standard_Boolean
13134 and then not Is_Intrinsic_Subprogram (Designator)
13137 ("explicit definition of inequality not allowed", Designator);
13139 end Valid_Operator_Definition;