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_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Inline; use Inline;
44 with Itypes; use Itypes;
45 with Lib.Xref; use Lib.Xref;
46 with Layout; use Layout;
47 with Namet; use Namet;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
52 with Output; use Output;
53 with Restrict; use Restrict;
54 with Rtsfind; use Rtsfind;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch4; use Sem_Ch4;
60 with Sem_Ch5; use Sem_Ch5;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch9; use Sem_Ch9;
63 with Sem_Ch10; use Sem_Ch10;
64 with Sem_Ch12; use Sem_Ch12;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Elim; use Sem_Elim;
70 with Sem_Eval; use Sem_Eval;
71 with Sem_Mech; use Sem_Mech;
72 with Sem_Prag; use Sem_Prag;
73 with Sem_Res; use Sem_Res;
74 with Sem_Util; use Sem_Util;
75 with Sem_Type; use Sem_Type;
76 with Sem_Warn; use Sem_Warn;
77 with Sinput; use Sinput;
78 with Stand; use Stand;
79 with Sinfo; use Sinfo;
80 with Sinfo.CN; use Sinfo.CN;
81 with Snames; use Snames;
82 with Stringt; use Stringt;
84 with Stylesw; use Stylesw;
85 with Tbuild; use Tbuild;
86 with Uintp; use Uintp;
87 with Urealp; use Urealp;
88 with Validsw; use Validsw;
90 package body Sem_Ch6 is
92 May_Hide_Profile : Boolean := False;
93 -- This flag is used to indicate that two formals in two subprograms being
94 -- checked for conformance differ only in that one is an access parameter
95 -- while the other is of a general access type with the same designated
96 -- type. In this case, if the rest of the signatures match, a call to
97 -- either subprogram may be ambiguous, which is worth a warning. The flag
98 -- is set in Compatible_Types, and the warning emitted in
99 -- New_Overloaded_Entity.
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
105 procedure Analyze_Function_Return (N : Node_Id);
106 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
107 -- applies to a [generic] function.
109 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
110 -- Analyze a generic subprogram body. N is the body to be analyzed, and
111 -- Gen_Id is the defining entity Id for the corresponding spec.
113 procedure Analyze_Null_Procedure
115 Is_Completion : out Boolean);
116 -- A null procedure can be a declaration or (Ada 2012) a completion
118 procedure Analyze_Return_Statement (N : Node_Id);
119 -- Common processing for simple and extended return statements
121 procedure Analyze_Return_Type (N : Node_Id);
122 -- Subsidiary to Process_Formals: analyze subtype mark in function
123 -- specification in a context where the formals are visible and hide
126 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
127 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
128 -- that we can use RETURN but not skip the debug output at the end.
130 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
131 -- Returns true if Subp can override a predefined operator.
133 procedure Check_Conformance
136 Ctype : Conformance_Type;
138 Conforms : out Boolean;
139 Err_Loc : Node_Id := Empty;
140 Get_Inst : Boolean := False;
141 Skip_Controlling_Formals : Boolean := False);
142 -- Given two entities, this procedure checks that the profiles associated
143 -- with these entities meet the conformance criterion given by the third
144 -- parameter. If they conform, Conforms is set True and control returns
145 -- to the caller. If they do not conform, Conforms is set to False, and
146 -- in addition, if Errmsg is True on the call, proper messages are output
147 -- to complain about the conformance failure. If Err_Loc is non_Empty
148 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
149 -- error messages are placed on the appropriate part of the construct
150 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
151 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 procedure Check_Limited_Return
158 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
159 -- types. Used only for simple return statements. Expr is the expression
162 procedure Check_Subprogram_Order (N : Node_Id);
163 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
164 -- the alpha ordering rule for N if this ordering requirement applicable.
166 procedure Check_Returns
170 Proc : Entity_Id := Empty);
171 -- Called to check for missing return statements in a function body, or for
172 -- returns present in a procedure body which has No_Return set. HSS is the
173 -- handled statement sequence for the subprogram body. This procedure
174 -- checks all flow paths to make sure they either have return (Mode = 'F',
175 -- used for functions) or do not have a return (Mode = 'P', used for
176 -- No_Return procedures). The flag Err is set if there are any control
177 -- paths not explicitly terminated by a return in the function case, and is
178 -- True otherwise. Proc is the entity for the procedure case and is used
179 -- in posting the warning message.
181 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
182 -- In Ada 2012, a primitive equality operator on an untagged record type
183 -- must appear before the type is frozen, and have the same visibility as
184 -- that of the type. This procedure checks that this rule is met, and
185 -- otherwise emits an error on the subprogram declaration and a warning
186 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
187 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
188 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
189 -- is set, otherwise the call has no effect.
191 procedure Enter_Overloaded_Entity (S : Entity_Id);
192 -- This procedure makes S, a new overloaded entity, into the first visible
193 -- entity with that name.
195 function Is_Non_Overriding_Operation
197 New_E : Entity_Id) return Boolean;
198 -- Enforce the rule given in 12.3(18): a private operation in an instance
199 -- overrides an inherited operation only if the corresponding operation
200 -- was overriding in the generic. This needs to be checked for primitive
201 -- operations of types derived (in the generic unit) from formal private
202 -- or formal derived types.
204 procedure Make_Inequality_Operator (S : Entity_Id);
205 -- Create the declaration for an inequality operator that is implicitly
206 -- created by a user-defined equality operator that yields a boolean.
208 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id);
209 -- Preanalysis of default expressions of subprogram formals. N is the
210 -- expression to be analyzed and T is the expected type.
212 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
213 -- Formal_Id is an formal parameter entity. This procedure deals with
214 -- setting the proper validity status for this entity, which depends on
215 -- the kind of parameter and the validity checking mode.
217 ---------------------------------------------
218 -- Analyze_Abstract_Subprogram_Declaration --
219 ---------------------------------------------
221 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
222 Scop : constant Entity_Id := Current_Scope;
223 Subp_Id : constant Entity_Id :=
224 Analyze_Subprogram_Specification (Specification (N));
227 Generate_Definition (Subp_Id);
229 -- Set the SPARK mode from the current context (may be overwritten later
230 -- with explicit pragma).
232 Set_SPARK_Pragma (Subp_Id, SPARK_Mode_Pragma);
233 Set_SPARK_Pragma_Inherited (Subp_Id);
235 -- Preserve relevant elaboration-related attributes of the context which
236 -- are no longer available or very expensive to recompute once analysis,
237 -- resolution, and expansion are over.
239 Mark_Elaboration_Attributes
244 Set_Is_Abstract_Subprogram (Subp_Id);
245 New_Overloaded_Entity (Subp_Id);
246 Check_Delayed_Subprogram (Subp_Id);
248 Set_Categorization_From_Scope (Subp_Id, Scop);
250 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
251 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
253 -- Issue a warning if the abstract subprogram is neither a dispatching
254 -- operation nor an operation that overrides an inherited subprogram or
255 -- predefined operator, since this most likely indicates a mistake.
257 elsif Warn_On_Redundant_Constructs
258 and then not Is_Dispatching_Operation (Subp_Id)
259 and then not Present (Overridden_Operation (Subp_Id))
260 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
261 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
264 ("abstract subprogram is not dispatching or overriding?r?", N);
267 Generate_Reference_To_Formals (Subp_Id);
268 Check_Eliminated (Subp_Id);
270 if Has_Aspects (N) then
271 Analyze_Aspect_Specifications (N, Subp_Id);
273 end Analyze_Abstract_Subprogram_Declaration;
275 ---------------------------------
276 -- Analyze_Expression_Function --
277 ---------------------------------
279 procedure Analyze_Expression_Function (N : Node_Id) is
280 Expr : constant Node_Id := Expression (N);
281 Loc : constant Source_Ptr := Sloc (N);
282 LocX : constant Source_Ptr := Sloc (Expr);
283 Spec : constant Node_Id := Specification (N);
293 Def_Id : Entity_Id := Empty;
295 -- If the expression is a completion, Prev is the entity whose
296 -- declaration is completed. Def_Id is needed to analyze the spec.
298 -- Start of processing for Analyze_Expression_Function
301 -- This is one of the occasions on which we transform the tree during
302 -- semantic analysis. If this is a completion, transform the expression
303 -- function into an equivalent subprogram body, and analyze it.
305 -- Expression functions are inlined unconditionally. The back-end will
306 -- determine whether this is possible.
308 Inline_Processing_Required := True;
310 -- Create a specification for the generated body. This must be done
311 -- prior to the analysis of the initial declaration.
313 New_Spec := Copy_Subprogram_Spec (Spec);
314 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
316 -- If there are previous overloadable entities with the same name,
317 -- check whether any of them is completed by the expression function.
318 -- In a generic context a formal subprogram has no completion.
321 and then Is_Overloadable (Prev)
322 and then not Is_Formal_Subprogram (Prev)
324 Def_Id := Analyze_Subprogram_Specification (Spec);
325 Prev := Find_Corresponding_Spec (N);
327 -- The previous entity may be an expression function as well, in
328 -- which case the redeclaration is illegal.
331 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
332 N_Expression_Function
334 Error_Msg_Sloc := Sloc (Prev);
335 Error_Msg_N ("& conflicts with declaration#", Def_Id);
340 Ret := Make_Simple_Return_Statement (LocX, Expr);
343 Make_Subprogram_Body (Loc,
344 Specification => New_Spec,
345 Declarations => Empty_List,
346 Handled_Statement_Sequence =>
347 Make_Handled_Sequence_Of_Statements (LocX,
348 Statements => New_List (Ret)));
349 Set_Was_Expression_Function (New_Body);
351 -- If the expression completes a generic subprogram, we must create a
352 -- separate node for the body, because at instantiation the original
353 -- node of the generic copy must be a generic subprogram body, and
354 -- cannot be a expression function. Otherwise we just rewrite the
355 -- expression with the non-generic body.
357 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
358 Insert_After (N, New_Body);
360 -- Propagate any aspects or pragmas that apply to the expression
361 -- function to the proper body when the expression function acts
364 if Has_Aspects (N) then
365 Move_Aspects (N, To => New_Body);
368 Relocate_Pragmas_To_Body (New_Body);
370 Rewrite (N, Make_Null_Statement (Loc));
371 Set_Has_Completion (Prev, False);
374 Set_Is_Inlined (Prev);
376 -- If the expression function is a completion, the previous declaration
377 -- must come from source. We know already that it appears in the current
378 -- scope. The entity itself may be internally created if within a body
382 and then Is_Overloadable (Prev)
383 and then not Is_Formal_Subprogram (Prev)
384 and then Comes_From_Source (Parent (Prev))
386 Set_Has_Completion (Prev, False);
387 Set_Is_Inlined (Prev);
389 -- AI12-0103: Expression functions that are a completion freeze their
390 -- expression but don't freeze anything else (unlike regular bodies).
392 -- Note that we cannot defer this freezing to the analysis of the
393 -- expression itself, because a freeze node might appear in a nested
394 -- scope, leading to an elaboration order issue in gigi.
395 -- As elsewhere, we do not emit freeze nodes within a generic unit.
397 if not Inside_A_Generic then
400 Typ => Etype (Def_Id),
405 -- For navigation purposes, indicate that the function is a body
407 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
408 Rewrite (N, New_Body);
410 -- Remove any existing aspects from the original node because the act
411 -- of rewriting causes the list to be shared between the two nodes.
413 Orig_N := Original_Node (N);
414 Remove_Aspects (Orig_N);
416 -- Propagate any pragmas that apply to expression function to the
417 -- proper body when the expression function acts as a completion.
418 -- Aspects are automatically transfered because of node rewriting.
420 Relocate_Pragmas_To_Body (N);
423 -- Prev is the previous entity with the same name, but it is can
424 -- be an unrelated spec that is not completed by the expression
425 -- function. In that case the relevant entity is the one in the body.
426 -- Not clear that the backend can inline it in this case ???
428 if Has_Completion (Prev) then
430 -- The formals of the expression function are body formals,
431 -- and do not appear in the ali file, which will only contain
432 -- references to the formals of the original subprogram spec.
439 F1 := First_Formal (Def_Id);
440 F2 := First_Formal (Prev);
442 while Present (F1) loop
443 Set_Spec_Entity (F1, F2);
450 Set_Is_Inlined (Defining_Entity (New_Body));
453 -- If this is not a completion, create both a declaration and a body, so
454 -- that the expression can be inlined whenever possible.
457 -- An expression function that is not a completion is not a
458 -- subprogram declaration, and thus cannot appear in a protected
461 if Nkind (Parent (N)) = N_Protected_Definition then
463 ("an expression function is not a legal protected operation", N);
466 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
468 -- Remove any existing aspects from the original node because the act
469 -- of rewriting causes the list to be shared between the two nodes.
471 Orig_N := Original_Node (N);
472 Remove_Aspects (Orig_N);
476 -- If aspect SPARK_Mode was specified on the body, it needs to be
477 -- repeated both on the generated spec and the body.
479 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
481 if Present (Asp) then
482 Asp := New_Copy_Tree (Asp);
483 Set_Analyzed (Asp, False);
484 Set_Aspect_Specifications (New_Body, New_List (Asp));
487 Def_Id := Defining_Entity (N);
488 Set_Is_Inlined (Def_Id);
490 -- Establish the linkages between the spec and the body. These are
491 -- used when the expression function acts as the prefix of attribute
492 -- 'Access in order to freeze the original expression which has been
493 -- moved to the generated body.
495 Set_Corresponding_Body (N, Defining_Entity (New_Body));
496 Set_Corresponding_Spec (New_Body, Def_Id);
498 -- Within a generic preanalyze the original expression for name
499 -- capture. The body is also generated but plays no role in
500 -- this because it is not part of the original source.
502 if Inside_A_Generic then
503 Set_Has_Completion (Def_Id);
505 Install_Formals (Def_Id);
506 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
510 -- To prevent premature freeze action, insert the new body at the end
511 -- of the current declarations, or at the end of the package spec.
512 -- However, resolve usage names now, to prevent spurious visibility
513 -- on later entities. Note that the function can now be called in
514 -- the current declarative part, which will appear to be prior to
515 -- the presence of the body in the code. There are nevertheless no
516 -- order of elaboration issues because all name resolution has taken
517 -- place at the point of declaration.
520 Decls : List_Id := List_Containing (N);
521 Expr : constant Node_Id := Expression (Ret);
522 Par : constant Node_Id := Parent (Decls);
523 Typ : constant Entity_Id := Etype (Def_Id);
526 -- If this is a wrapper created for in an instance for a formal
527 -- subprogram, insert body after declaration, to be analyzed when
528 -- the enclosing instance is analyzed.
531 and then Is_Generic_Actual_Subprogram (Def_Id)
533 Insert_After (N, New_Body);
536 if Nkind (Par) = N_Package_Specification
537 and then Decls = Visible_Declarations (Par)
538 and then Present (Private_Declarations (Par))
539 and then not Is_Empty_List (Private_Declarations (Par))
541 Decls := Private_Declarations (Par);
544 Insert_After (Last (Decls), New_Body);
546 -- Preanalyze the expression if not already done above
548 if not Inside_A_Generic then
550 Install_Formals (Def_Id);
551 Preanalyze_Formal_Expression (Expr, Typ);
552 Check_Limited_Return (Original_Node (N), Expr, Typ);
559 -- Check incorrect use of dynamically tagged expression. This doesn't
560 -- fall out automatically when analyzing the generated function body,
561 -- because Check_Dynamically_Tagged_Expression deliberately ignores
562 -- nodes that don't come from source.
565 and then Nkind (Def_Id) in N_Has_Etype
566 and then Is_Tagged_Type (Etype (Def_Id))
568 Check_Dynamically_Tagged_Expression
570 Typ => Etype (Def_Id),
571 Related_Nod => Original_Node (N));
574 -- We must enforce checks for unreferenced formals in our newly
575 -- generated function, so we propagate the referenced flag from the
576 -- original spec to the new spec as well as setting Comes_From_Source.
578 if Present (Parameter_Specifications (New_Spec)) then
580 Form_New_Def : Entity_Id;
581 Form_New_Spec : Entity_Id;
582 Form_Old_Def : Entity_Id;
583 Form_Old_Spec : Entity_Id;
586 Form_New_Spec := First (Parameter_Specifications (New_Spec));
587 Form_Old_Spec := First (Parameter_Specifications (Spec));
589 while Present (Form_New_Spec) and then Present (Form_Old_Spec) loop
590 Form_New_Def := Defining_Identifier (Form_New_Spec);
591 Form_Old_Def := Defining_Identifier (Form_Old_Spec);
593 Set_Comes_From_Source (Form_New_Def, True);
595 -- Because of the usefulness of unreferenced controlling
596 -- formals we exempt them from unreferenced warnings by marking
597 -- them as always referenced.
599 Set_Referenced (Form_Old_Def,
600 (Is_Formal (Form_Old_Def)
601 and then Is_Controlling_Formal (Form_Old_Def))
602 or else Referenced (Form_Old_Def));
604 Next (Form_New_Spec);
605 Next (Form_Old_Spec);
609 end Analyze_Expression_Function;
611 ----------------------------------------
612 -- Analyze_Extended_Return_Statement --
613 ----------------------------------------
615 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
617 Check_Compiler_Unit ("extended return statement", N);
618 Analyze_Return_Statement (N);
619 end Analyze_Extended_Return_Statement;
621 ----------------------------
622 -- Analyze_Function_Call --
623 ----------------------------
625 procedure Analyze_Function_Call (N : Node_Id) is
626 Actuals : constant List_Id := Parameter_Associations (N);
627 Func_Nam : constant Node_Id := Name (N);
633 -- A call of the form A.B (X) may be an Ada 2005 call, which is
634 -- rewritten as B (A, X). If the rewriting is successful, the call
635 -- has been analyzed and we just return.
637 if Nkind (Func_Nam) = N_Selected_Component
638 and then Name (N) /= Func_Nam
639 and then Is_Rewrite_Substitution (N)
640 and then Present (Etype (N))
645 -- If error analyzing name, then set Any_Type as result type and return
647 if Etype (Func_Nam) = Any_Type then
648 Set_Etype (N, Any_Type);
652 -- Otherwise analyze the parameters
654 if Present (Actuals) then
655 Actual := First (Actuals);
656 while Present (Actual) loop
658 Check_Parameterless_Call (Actual);
664 end Analyze_Function_Call;
666 -----------------------------
667 -- Analyze_Function_Return --
668 -----------------------------
670 procedure Analyze_Function_Return (N : Node_Id) is
671 Loc : constant Source_Ptr := Sloc (N);
672 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
673 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
675 R_Type : constant Entity_Id := Etype (Scope_Id);
676 -- Function result subtype
678 procedure Check_No_Return_Expression (Return_Expr : Node_Id);
679 -- Ada 2020: Check that the return expression in a No_Return function
680 -- meets the conditions specified by RM 6.5.1(5.1/5).
682 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id);
683 -- Apply legality rule of 6.5 (5.9) to the access discriminants of an
684 -- aggregate in a return statement.
686 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
687 -- Check that the return_subtype_indication properly matches the result
688 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
690 --------------------------------
691 -- Check_No_Return_Expression --
692 --------------------------------
694 procedure Check_No_Return_Expression (Return_Expr : Node_Id) is
695 Kind : constant Node_Kind := Nkind (Return_Expr);
698 if Kind = N_Raise_Expression then
701 elsif Kind = N_Function_Call
702 and then Is_Entity_Name (Name (Return_Expr))
703 and then Ekind_In (Entity (Name (Return_Expr)), E_Function,
705 and then No_Return (Entity (Name (Return_Expr)))
711 ("illegal expression in RETURN statement of No_Return function",
714 ("\must be raise expression or call to No_Return (RM 6.5.1(5.1/5))",
716 end Check_No_Return_Expression;
718 ------------------------------------------
719 -- Check_Return_Construct_Accessibility --
720 ------------------------------------------
722 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id) is
724 Agg : Node_Id := Empty;
728 Process_Exprs : Boolean := False;
729 Return_Con : Node_Id;
732 -- Only perform checks on record types with access discriminants and
733 -- non-internally generated functions.
735 if not Is_Record_Type (R_Type)
736 or else not Has_Discriminants (R_Type)
737 or else not Comes_From_Source (Return_Stmt)
742 -- We are only interested in return statements
744 if not Nkind_In (Return_Stmt, N_Extended_Return_Statement,
745 N_Simple_Return_Statement)
750 -- Fetch the object from the return statement, in the case of a
751 -- simple return statement the expression is part of the node.
753 if Nkind (Return_Stmt) = N_Extended_Return_Statement then
754 -- Obtain the object definition from the expanded extended return
756 Return_Con := First (Return_Object_Declarations (Return_Stmt));
757 while Present (Return_Con) loop
758 -- Inspect the original node to avoid object declarations
759 -- expanded into renamings.
761 if Nkind (Original_Node (Return_Con)) = N_Object_Declaration
762 and then Comes_From_Source (Original_Node (Return_Con))
767 Nlists.Next (Return_Con);
770 pragma Assert (Present (Return_Con));
772 -- Could be dealing with a renaming
774 Return_Con := Original_Node (Return_Con);
776 Return_Con := Return_Stmt;
779 -- We may need to check an aggregate or a subtype indication
780 -- depending on how the discriminants were specified and whether
781 -- we are looking at an extended return statement.
783 if Nkind (Return_Con) = N_Object_Declaration
784 and then Nkind (Object_Definition (Return_Con))
785 = N_Subtype_Indication
787 Assoc := Original_Node
790 (Constraint (Object_Definition (Return_Con)))));
792 -- Qualified expressions may be nested
794 Agg := Original_Node (Expression (Return_Con));
795 while Nkind (Agg) = N_Qualified_Expression loop
796 Agg := Original_Node (Expression (Agg));
799 -- If we are looking at an aggregate instead of a function call we
800 -- can continue checking accessibility for the supplied
801 -- discriminant associations.
803 if Nkind (Agg) = N_Aggregate then
804 if Present (Expressions (Agg)) then
805 Assoc := First (Expressions (Agg));
806 Process_Exprs := True;
808 Assoc := First (Component_Associations (Agg));
811 -- Otherwise the expression is not of interest ???
818 -- Move through the discriminants checking the accessibility level
819 -- of each co-extension's associated expression.
821 Discr := First_Discriminant (R_Type);
822 while Present (Discr) loop
823 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
825 if Nkind (Assoc) = N_Attribute_Reference then
827 elsif Nkind_In (Assoc, N_Component_Association,
828 N_Discriminant_Association)
830 Expr := Expression (Assoc);
835 -- This anonymous access discriminant has an associated
836 -- expression which needs checking.
839 and then Nkind (Expr) = N_Attribute_Reference
840 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
842 -- Obtain the object to perform static checks on by moving
843 -- up the prefixes in the expression taking into account
844 -- named access types and renamed objects within the
847 Obj := Original_Node (Prefix (Expr));
849 while Nkind_In (Obj, N_Explicit_Dereference,
851 N_Selected_Component)
853 -- When we encounter a named access type then we can
854 -- ignore accessibility checks on the dereference.
856 if Ekind (Etype (Original_Node (Prefix (Obj))))
858 E_Access_Protected_Subprogram_Type
860 if Nkind (Obj) = N_Selected_Component then
861 Obj := Selector_Name (Obj);
863 Obj := Original_Node (Prefix (Obj));
868 Obj := Original_Node (Prefix (Obj));
871 if Nkind (Obj) = N_Selected_Component then
872 Obj := Selector_Name (Obj);
875 -- Check for renamings
877 pragma Assert (Is_Entity_Name (Obj));
879 if Present (Renamed_Object (Entity (Obj))) then
880 Obj := Renamed_Object (Entity (Obj));
886 -- Do not check aliased formals or function calls. A
887 -- run-time check may still be needed ???
889 if Is_Formal (Entity (Obj))
890 and then Is_Aliased (Entity (Obj))
894 elsif Object_Access_Level (Obj) >
895 Scope_Depth (Scope (Scope_Id))
898 ("access discriminant in return aggregate would "
899 & "be a dangling reference", Obj);
904 Next_Discriminant (Discr);
906 if not Is_List_Member (Assoc) then
912 -- After aggregate expressions, examine component associations if
917 and then Process_Exprs
918 and then Present (Component_Associations (Agg))
920 Assoc := First (Component_Associations (Agg));
921 Process_Exprs := False;
927 end Check_Return_Construct_Accessibility;
929 -------------------------------------
930 -- Check_Return_Subtype_Indication --
931 -------------------------------------
933 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
934 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
936 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
937 -- Subtype given in the extended return statement (must match R_Type)
939 Subtype_Ind : constant Node_Id :=
940 Object_Definition (Original_Node (Obj_Decl));
942 procedure Error_No_Match (N : Node_Id);
943 -- Output error messages for case where types do not statically
944 -- match. N is the location for the messages.
950 procedure Error_No_Match (N : Node_Id) is
953 ("subtype must statically match function result subtype", N);
955 if not Predicates_Match (R_Stm_Type, R_Type) then
956 Error_Msg_Node_2 := R_Type;
958 ("\predicate of& does not match predicate of&",
963 -- Start of processing for Check_Return_Subtype_Indication
966 -- First, avoid cascaded errors
968 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
972 -- "return access T" case; check that the return statement also has
973 -- "access T", and that the subtypes statically match:
974 -- if this is an access to subprogram the signatures must match.
976 if Is_Anonymous_Access_Type (R_Type) then
977 if Is_Anonymous_Access_Type (R_Stm_Type) then
978 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
980 if Base_Type (Designated_Type (R_Stm_Type)) /=
981 Base_Type (Designated_Type (R_Type))
982 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
984 Error_No_Match (Subtype_Mark (Subtype_Ind));
988 -- For two anonymous access to subprogram types, the types
989 -- themselves must be type conformant.
991 if not Conforming_Types
992 (R_Stm_Type, R_Type, Fully_Conformant)
994 Error_No_Match (Subtype_Ind);
999 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
1002 -- If the return object is of an anonymous access type, then report
1003 -- an error if the function's result type is not also anonymous.
1005 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
1006 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
1008 ("anonymous access not allowed for function with named access "
1009 & "result", Subtype_Ind);
1011 -- Subtype indication case: check that the return object's type is
1012 -- covered by the result type, and that the subtypes statically match
1013 -- when the result subtype is constrained. Also handle record types
1014 -- with unknown discriminants for which we have built the underlying
1015 -- record view. Coverage is needed to allow specific-type return
1016 -- objects when the result type is class-wide (see AI05-32).
1018 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
1019 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
1022 (Base_Type (R_Type),
1023 Underlying_Record_View (Base_Type (R_Stm_Type))))
1025 -- A null exclusion may be present on the return type, on the
1026 -- function specification, on the object declaration or on the
1029 if Is_Access_Type (R_Type)
1031 (Can_Never_Be_Null (R_Type)
1032 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1033 Can_Never_Be_Null (R_Stm_Type)
1035 Error_No_Match (Subtype_Ind);
1038 -- AI05-103: for elementary types, subtypes must statically match
1040 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1041 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1042 Error_No_Match (Subtype_Ind);
1046 -- All remaining cases are illegal
1048 -- Note: previous versions of this subprogram allowed the return
1049 -- value to be the ancestor of the return type if the return type
1050 -- was a null extension. This was plainly incorrect.
1054 ("wrong type for return_subtype_indication", Subtype_Ind);
1056 end Check_Return_Subtype_Indication;
1058 ---------------------
1059 -- Local Variables --
1060 ---------------------
1063 Obj_Decl : Node_Id := Empty;
1065 -- Start of processing for Analyze_Function_Return
1068 Set_Return_Present (Scope_Id);
1070 if Nkind (N) = N_Simple_Return_Statement then
1071 Expr := Expression (N);
1073 -- Guard against a malformed expression. The parser may have tried to
1074 -- recover but the node is not analyzable.
1076 if Nkind (Expr) = N_Error then
1077 Set_Etype (Expr, Any_Type);
1078 Expander_Mode_Save_And_Set (False);
1082 -- The resolution of a controlled [extension] aggregate associated
1083 -- with a return statement creates a temporary which needs to be
1084 -- finalized on function exit. Wrap the return statement inside a
1085 -- block so that the finalization machinery can detect this case.
1086 -- This early expansion is done only when the return statement is
1087 -- not part of a handled sequence of statements.
1089 if Nkind_In (Expr, N_Aggregate,
1090 N_Extension_Aggregate)
1091 and then Needs_Finalization (R_Type)
1092 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1095 Make_Block_Statement (Loc,
1096 Handled_Statement_Sequence =>
1097 Make_Handled_Sequence_Of_Statements (Loc,
1098 Statements => New_List (Relocate_Node (N)))));
1106 -- Ada 2005 (AI-251): If the type of the returned object is
1107 -- an access to an interface type then we add an implicit type
1108 -- conversion to force the displacement of the "this" pointer to
1109 -- reference the secondary dispatch table. We cannot delay the
1110 -- generation of this implicit conversion until the expansion
1111 -- because in this case the type resolution changes the decoration
1112 -- of the expression node to match R_Type; by contrast, if the
1113 -- returned object is a class-wide interface type then it is too
1114 -- early to generate here the implicit conversion since the return
1115 -- statement may be rewritten by the expander into an extended
1116 -- return statement whose expansion takes care of adding the
1117 -- implicit type conversion to displace the pointer to the object.
1120 and then Serious_Errors_Detected = 0
1121 and then Is_Access_Type (R_Type)
1122 and then not Nkind_In (Expr, N_Null, N_Raise_Expression)
1123 and then Is_Interface (Designated_Type (R_Type))
1124 and then Is_Progenitor (Designated_Type (R_Type),
1125 Designated_Type (Etype (Expr)))
1127 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1131 Resolve (Expr, R_Type);
1132 Check_Limited_Return (N, Expr, R_Type);
1134 Check_Return_Construct_Accessibility (N);
1136 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1137 -- nonreturning function shall be a simple_return_statement with
1138 -- an expression that is a raise_expression, or else a call on a
1139 -- nonreturning function, or else a parenthesized expression of
1142 if Ada_Version >= Ada_2020
1143 and then No_Return (Scope_Id)
1144 and then Comes_From_Source (N)
1146 Check_No_Return_Expression (Original_Node (Expr));
1150 Obj_Decl := Last (Return_Object_Declarations (N));
1152 -- Analyze parts specific to extended_return_statement:
1155 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1156 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1159 Expr := Expression (Obj_Decl);
1161 -- Note: The check for OK_For_Limited_Init will happen in
1162 -- Analyze_Object_Declaration; we treat it as a normal
1163 -- object declaration.
1165 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1168 Check_Return_Subtype_Indication (Obj_Decl);
1170 if Present (HSS) then
1173 if Present (Exception_Handlers (HSS)) then
1175 -- ???Has_Nested_Block_With_Handler needs to be set.
1176 -- Probably by creating an actual N_Block_Statement.
1177 -- Probably in Expand.
1183 -- Mark the return object as referenced, since the return is an
1184 -- implicit reference of the object.
1186 Set_Referenced (Defining_Identifier (Obj_Decl));
1188 Check_References (Stm_Entity);
1190 Check_Return_Construct_Accessibility (N);
1192 -- Check RM 6.5 (5.9/3)
1195 if Ada_Version < Ada_2012 then
1197 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1198 -- Can it really happen (extended return???)
1201 ("aliased only allowed for limited return objects "
1202 & "in Ada 2012??", N);
1204 elsif not Is_Limited_View (R_Type) then
1206 ("aliased only allowed for limited return objects", N);
1210 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1211 -- nonreturning function shall be a simple_return_statement.
1213 if Ada_Version >= Ada_2020
1214 and then No_Return (Scope_Id)
1215 and then Comes_From_Source (N)
1218 ("extended RETURN statement not allowed in No_Return "
1224 -- Case of Expr present
1226 if Present (Expr) then
1228 -- Defend against previous errors
1230 if Nkind (Expr) = N_Empty
1231 or else No (Etype (Expr))
1236 -- Apply constraint check. Note that this is done before the implicit
1237 -- conversion of the expression done for anonymous access types to
1238 -- ensure correct generation of the null-excluding check associated
1239 -- with null-excluding expressions found in return statements. We
1240 -- don't need a check if the subtype of the return object is the
1241 -- same as the result subtype of the function.
1243 if Nkind (N) /= N_Extended_Return_Statement
1244 or else Nkind (Obj_Decl) /= N_Object_Declaration
1245 or else Nkind (Object_Definition (Obj_Decl)) not in N_Has_Entity
1246 or else Entity (Object_Definition (Obj_Decl)) /= R_Type
1248 Apply_Constraint_Check (Expr, R_Type);
1251 -- The return value is converted to the return type of the function,
1252 -- which implies a predicate check if the return type is predicated.
1253 -- We do not apply the check for an extended return statement because
1254 -- Analyze_Object_Declaration has already done it on Obj_Decl above.
1255 -- We do not apply the check to a case expression because it will
1256 -- be expanded into a series of return statements, each of which
1257 -- will receive a predicate check.
1259 if Nkind (N) /= N_Extended_Return_Statement
1260 and then Nkind (Expr) /= N_Case_Expression
1262 Apply_Predicate_Check (Expr, R_Type);
1265 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1266 -- type, apply an implicit conversion of the expression to that type
1267 -- to force appropriate static and run-time accessibility checks.
1268 -- But we want to apply the checks to an extended return statement
1269 -- only once, i.e. not to the simple return statement generated at
1270 -- the end of its expansion because, prior to leaving the function,
1271 -- the accessibility level of the return object changes to be a level
1272 -- determined by the point of call (RM 3.10.2(10.8/3).
1274 if Ada_Version >= Ada_2005
1275 and then Ekind (R_Type) = E_Anonymous_Access_Type
1276 and then (Nkind (N) = N_Extended_Return_Statement
1277 or else not Comes_From_Extended_Return_Statement (N))
1279 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1280 Analyze_And_Resolve (Expr, R_Type);
1282 -- If this is a local anonymous access to subprogram, the
1283 -- accessibility check can be applied statically. The return is
1284 -- illegal if the access type of the return expression is declared
1285 -- inside of the subprogram (except if it is the subtype indication
1286 -- of an extended return statement).
1288 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1289 if not Comes_From_Source (Current_Scope)
1290 or else Ekind (Current_Scope) = E_Return_Statement
1295 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1297 Error_Msg_N ("cannot return local access to subprogram", N);
1300 -- The expression cannot be of a formal incomplete type
1302 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1303 and then Is_Generic_Type (Etype (Expr))
1306 ("cannot return expression of a formal incomplete type", N);
1309 -- If the result type is class-wide, then check that the return
1310 -- expression's type is not declared at a deeper level than the
1311 -- function (RM05-6.5(5.6/2)).
1313 if Ada_Version >= Ada_2005
1314 and then Is_Class_Wide_Type (R_Type)
1316 if Type_Access_Level (Etype (Expr)) >
1317 Subprogram_Access_Level (Scope_Id)
1320 ("level of return expression type is deeper than "
1321 & "class-wide function!", Expr);
1325 -- Check incorrect use of dynamically tagged expression
1327 if Is_Tagged_Type (R_Type) then
1328 Check_Dynamically_Tagged_Expression
1334 -- ??? A real run-time accessibility check is needed in cases
1335 -- involving dereferences of access parameters. For now we just
1336 -- check the static cases.
1338 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1339 and then Is_Limited_View (Etype (Scope_Id))
1340 and then Object_Access_Level (Expr) >
1341 Subprogram_Access_Level (Scope_Id)
1343 -- Suppress the message in a generic, where the rewriting
1346 if Inside_A_Generic then
1351 Make_Raise_Program_Error (Loc,
1352 Reason => PE_Accessibility_Check_Failed));
1355 Error_Msg_Warn := SPARK_Mode /= On;
1356 Error_Msg_N ("cannot return a local value by reference<<", N);
1357 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1361 if Known_Null (Expr)
1362 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1363 and then Null_Exclusion_Present (Parent (Scope_Id))
1365 Apply_Compile_Time_Constraint_Error
1367 Msg => "(Ada 2005) null not allowed for "
1368 & "null-excluding return??",
1369 Reason => CE_Null_Not_Allowed);
1372 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1373 -- has no initializing expression.
1375 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1376 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1377 Subprogram_Access_Level (Scope_Id)
1380 ("level of return expression type is deeper than "
1381 & "class-wide function!", Obj_Decl);
1384 end Analyze_Function_Return;
1386 -------------------------------------
1387 -- Analyze_Generic_Subprogram_Body --
1388 -------------------------------------
1390 procedure Analyze_Generic_Subprogram_Body
1394 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1395 Kind : constant Entity_Kind := Ekind (Gen_Id);
1396 Body_Id : Entity_Id;
1401 -- Copy body and disable expansion while analyzing the generic For a
1402 -- stub, do not copy the stub (which would load the proper body), this
1403 -- will be done when the proper body is analyzed.
1405 if Nkind (N) /= N_Subprogram_Body_Stub then
1406 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1409 -- Once the contents of the generic copy and the template are
1410 -- swapped, do the same for their respective aspect specifications.
1412 Exchange_Aspects (N, New_N);
1414 -- Collect all contract-related source pragmas found within the
1415 -- template and attach them to the contract of the subprogram body.
1416 -- This contract is used in the capture of global references within
1419 Create_Generic_Contract (N);
1424 Spec := Specification (N);
1426 -- Within the body of the generic, the subprogram is callable, and
1427 -- behaves like the corresponding non-generic unit.
1429 Body_Id := Defining_Entity (Spec);
1431 if Kind = E_Generic_Procedure
1432 and then Nkind (Spec) /= N_Procedure_Specification
1434 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1437 elsif Kind = E_Generic_Function
1438 and then Nkind (Spec) /= N_Function_Specification
1440 Error_Msg_N ("invalid body for generic function ", Body_Id);
1444 Set_Corresponding_Body (Gen_Decl, Body_Id);
1446 if Has_Completion (Gen_Id)
1447 and then Nkind (Parent (N)) /= N_Subunit
1449 Error_Msg_N ("duplicate generic body", N);
1452 Set_Has_Completion (Gen_Id);
1455 if Nkind (N) = N_Subprogram_Body_Stub then
1456 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1458 Set_Corresponding_Spec (N, Gen_Id);
1461 if Nkind (Parent (N)) = N_Compilation_Unit then
1462 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1465 -- Make generic parameters immediately visible in the body. They are
1466 -- needed to process the formals declarations. Then make the formals
1467 -- visible in a separate step.
1469 Push_Scope (Gen_Id);
1473 First_Ent : Entity_Id;
1476 First_Ent := First_Entity (Gen_Id);
1479 while Present (E) and then not Is_Formal (E) loop
1484 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1486 -- Now generic formals are visible, and the specification can be
1487 -- analyzed, for subsequent conformance check.
1489 Body_Id := Analyze_Subprogram_Specification (Spec);
1491 -- Make formal parameters visible
1495 -- E is the first formal parameter, we loop through the formals
1496 -- installing them so that they will be visible.
1498 Set_First_Entity (Gen_Id, E);
1499 while Present (E) loop
1505 -- Visible generic entity is callable within its own body
1507 Set_Ekind (Gen_Id, Ekind (Body_Id));
1508 Set_Ekind (Body_Id, E_Subprogram_Body);
1509 Set_Convention (Body_Id, Convention (Gen_Id));
1510 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1511 Set_Scope (Body_Id, Scope (Gen_Id));
1513 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1515 if Nkind (N) = N_Subprogram_Body_Stub then
1517 -- No body to analyze, so restore state of generic unit
1519 Set_Ekind (Gen_Id, Kind);
1520 Set_Ekind (Body_Id, Kind);
1522 if Present (First_Ent) then
1523 Set_First_Entity (Gen_Id, First_Ent);
1530 -- If this is a compilation unit, it must be made visible explicitly,
1531 -- because the compilation of the declaration, unlike other library
1532 -- unit declarations, does not. If it is not a unit, the following
1533 -- is redundant but harmless.
1535 Set_Is_Immediately_Visible (Gen_Id);
1536 Reference_Body_Formals (Gen_Id, Body_Id);
1538 if Is_Child_Unit (Gen_Id) then
1539 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1542 Set_Actual_Subtypes (N, Current_Scope);
1544 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1545 Set_SPARK_Pragma_Inherited (Body_Id);
1547 -- Analyze any aspect specifications that appear on the generic
1550 if Has_Aspects (N) then
1551 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
1554 Analyze_Declarations (Declarations (N));
1557 -- Process the contract of the subprogram body after all declarations
1558 -- have been analyzed. This ensures that any contract-related pragmas
1559 -- are available through the N_Contract node of the body.
1561 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1563 Analyze (Handled_Statement_Sequence (N));
1564 Save_Global_References (Original_Node (N));
1566 -- Prior to exiting the scope, include generic formals again (if any
1567 -- are present) in the set of local entities.
1569 if Present (First_Ent) then
1570 Set_First_Entity (Gen_Id, First_Ent);
1573 Check_References (Gen_Id);
1576 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1577 Update_Use_Clause_Chain;
1578 Validate_Categorization_Dependency (N, Gen_Id);
1580 Check_Subprogram_Order (N);
1582 -- Outside of its body, unit is generic again
1584 Set_Ekind (Gen_Id, Kind);
1585 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1588 Style.Check_Identifier (Body_Id, Gen_Id);
1592 end Analyze_Generic_Subprogram_Body;
1594 ----------------------------
1595 -- Analyze_Null_Procedure --
1596 ----------------------------
1598 -- WARNING: This routine manages Ghost regions. Return statements must be
1599 -- replaced by gotos that jump to the end of the routine and restore the
1602 procedure Analyze_Null_Procedure
1604 Is_Completion : out Boolean)
1606 Loc : constant Source_Ptr := Sloc (N);
1607 Spec : constant Node_Id := Specification (N);
1609 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1610 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1611 Saved_ISMP : constant Boolean :=
1612 Ignore_SPARK_Mode_Pragmas_In_Instance;
1613 -- Save the Ghost and SPARK mode-related data to restore on exit
1615 Designator : Entity_Id;
1617 Null_Body : Node_Id := Empty;
1618 Null_Stmt : Node_Id := Null_Statement (Spec);
1622 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1624 -- A null procedure is Ghost when it is stand-alone and is subject to
1625 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1626 -- now, to ensure that any nodes generated during analysis and expansion
1627 -- are properly marked as Ghost.
1629 if Present (Prev) then
1630 Mark_And_Set_Ghost_Body (N, Prev);
1633 -- Capture the profile of the null procedure before analysis, for
1634 -- expansion at the freeze point and at each point of call. The body is
1635 -- used if the procedure has preconditions, or if it is a completion. In
1636 -- the first case the body is analyzed at the freeze point, in the other
1637 -- it replaces the null procedure declaration.
1639 -- For a null procedure that comes from source, a NULL statement is
1640 -- provided by the parser, which carries the source location of the
1641 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1642 -- from expansion, create one now.
1644 if No (Null_Stmt) then
1645 Null_Stmt := Make_Null_Statement (Loc);
1649 Make_Subprogram_Body (Loc,
1650 Specification => New_Copy_Tree (Spec),
1651 Declarations => New_List,
1652 Handled_Statement_Sequence =>
1653 Make_Handled_Sequence_Of_Statements (Loc,
1654 Statements => New_List (Null_Stmt)));
1656 -- Create new entities for body and formals
1658 Set_Defining_Unit_Name (Specification (Null_Body),
1659 Make_Defining_Identifier
1660 (Sloc (Defining_Entity (N)),
1661 Chars (Defining_Entity (N))));
1663 Form := First (Parameter_Specifications (Specification (Null_Body)));
1664 while Present (Form) loop
1665 Set_Defining_Identifier (Form,
1666 Make_Defining_Identifier
1667 (Sloc (Defining_Identifier (Form)),
1668 Chars (Defining_Identifier (Form))));
1672 -- Determine whether the null procedure may be a completion of a generic
1673 -- suprogram, in which case we use the new null body as the completion
1674 -- and set minimal semantic information on the original declaration,
1675 -- which is rewritten as a null statement.
1677 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1678 Insert_Before (N, Null_Body);
1679 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1681 Rewrite (N, Make_Null_Statement (Loc));
1682 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1683 Is_Completion := True;
1688 -- Resolve the types of the formals now, because the freeze point may
1689 -- appear in a different context, e.g. an instantiation.
1691 Form := First (Parameter_Specifications (Specification (Null_Body)));
1692 while Present (Form) loop
1693 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1694 Find_Type (Parameter_Type (Form));
1696 elsif No (Access_To_Subprogram_Definition
1697 (Parameter_Type (Form)))
1699 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1701 -- The case of a null procedure with a formal that is an
1702 -- access-to-subprogram type, and that is used as an actual
1703 -- in an instantiation is left to the enthusiastic reader.
1713 -- If there are previous overloadable entities with the same name, check
1714 -- whether any of them is completed by the null procedure.
1716 if Present (Prev) and then Is_Overloadable (Prev) then
1717 Designator := Analyze_Subprogram_Specification (Spec);
1718 Prev := Find_Corresponding_Spec (N);
1721 if No (Prev) or else not Comes_From_Source (Prev) then
1722 Designator := Analyze_Subprogram_Specification (Spec);
1723 Set_Has_Completion (Designator);
1725 -- Signal to caller that this is a procedure declaration
1727 Is_Completion := False;
1729 -- Null procedures are always inlined, but generic formal subprograms
1730 -- which appear as such in the internal instance of formal packages,
1731 -- need no completion and are not marked Inline.
1734 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1736 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1737 Set_Body_To_Inline (N, Null_Body);
1738 Set_Is_Inlined (Designator);
1742 -- The null procedure is a completion. We unconditionally rewrite
1743 -- this as a null body (even if expansion is not active), because
1744 -- there are various error checks that are applied on this body
1745 -- when it is analyzed (e.g. correct aspect placement).
1747 if Has_Completion (Prev) then
1748 Error_Msg_Sloc := Sloc (Prev);
1749 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1752 Check_Previous_Null_Procedure (N, Prev);
1754 Is_Completion := True;
1755 Rewrite (N, Null_Body);
1760 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1761 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1762 end Analyze_Null_Procedure;
1764 -----------------------------
1765 -- Analyze_Operator_Symbol --
1766 -----------------------------
1768 -- An operator symbol such as "+" or "and" may appear in context where the
1769 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1770 -- is just a string, as in (conjunction = "or"). In these cases the parser
1771 -- generates this node, and the semantics does the disambiguation. Other
1772 -- such case are actuals in an instantiation, the generic unit in an
1773 -- instantiation, and pragma arguments.
1775 procedure Analyze_Operator_Symbol (N : Node_Id) is
1776 Par : constant Node_Id := Parent (N);
1779 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1780 or else Nkind (Par) = N_Function_Instantiation
1781 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1782 or else (Nkind (Par) = N_Pragma_Argument_Association
1783 and then not Is_Pragma_String_Literal (Par))
1784 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1785 or else (Nkind (Par) = N_Attribute_Reference
1786 and then Attribute_Name (Par) /= Name_Value)
1788 Find_Direct_Name (N);
1791 Change_Operator_Symbol_To_String_Literal (N);
1794 end Analyze_Operator_Symbol;
1796 -----------------------------------
1797 -- Analyze_Parameter_Association --
1798 -----------------------------------
1800 procedure Analyze_Parameter_Association (N : Node_Id) is
1802 Analyze (Explicit_Actual_Parameter (N));
1803 end Analyze_Parameter_Association;
1805 ----------------------------
1806 -- Analyze_Procedure_Call --
1807 ----------------------------
1809 -- WARNING: This routine manages Ghost regions. Return statements must be
1810 -- replaced by gotos that jump to the end of the routine and restore the
1813 procedure Analyze_Procedure_Call (N : Node_Id) is
1814 procedure Analyze_Call_And_Resolve;
1815 -- Do Analyze and Resolve calls for procedure call. At the end, check
1816 -- for illegal order dependence.
1817 -- ??? where is the check for illegal order dependencies?
1819 ------------------------------
1820 -- Analyze_Call_And_Resolve --
1821 ------------------------------
1823 procedure Analyze_Call_And_Resolve is
1825 if Nkind (N) = N_Procedure_Call_Statement then
1827 Resolve (N, Standard_Void_Type);
1831 end Analyze_Call_And_Resolve;
1835 Actuals : constant List_Id := Parameter_Associations (N);
1836 Loc : constant Source_Ptr := Sloc (N);
1837 P : constant Node_Id := Name (N);
1839 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1840 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1841 -- Save the Ghost-related attributes to restore on exit
1846 -- Start of processing for Analyze_Procedure_Call
1849 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1850 -- a procedure call or an entry call. The prefix may denote an access
1851 -- to subprogram type, in which case an implicit dereference applies.
1852 -- If the prefix is an indexed component (without implicit dereference)
1853 -- then the construct denotes a call to a member of an entire family.
1854 -- If the prefix is a simple name, it may still denote a call to a
1855 -- parameterless member of an entry family. Resolution of these various
1856 -- interpretations is delicate.
1858 -- Do not analyze machine code statements to avoid rejecting them in
1861 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1862 Set_Etype (P, Standard_Void_Type);
1867 -- If this is a call of the form Obj.Op, the call may have been analyzed
1868 -- and possibly rewritten into a block, in which case we are done.
1870 if Analyzed (N) then
1873 -- If there is an error analyzing the name (which may have been
1874 -- rewritten if the original call was in prefix notation) then error
1875 -- has been emitted already, mark node and return.
1877 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1878 Set_Etype (N, Any_Type);
1882 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1883 -- Set the mode now to ensure that any nodes generated during analysis
1884 -- and expansion are properly marked as Ghost.
1886 Mark_And_Set_Ghost_Procedure_Call (N);
1888 -- Otherwise analyze the parameters
1890 if Present (Actuals) then
1891 Actual := First (Actuals);
1893 while Present (Actual) loop
1895 Check_Parameterless_Call (Actual);
1900 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1902 if Nkind (P) = N_Attribute_Reference
1903 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1905 Name_Elab_Subp_Body)
1907 if Present (Actuals) then
1909 ("no parameters allowed for this call", First (Actuals));
1913 Set_Etype (N, Standard_Void_Type);
1916 elsif Is_Entity_Name (P)
1917 and then Is_Record_Type (Etype (Entity (P)))
1918 and then Remote_AST_I_Dereference (P)
1922 elsif Is_Entity_Name (P)
1923 and then Ekind (Entity (P)) /= E_Entry_Family
1925 if Is_Access_Type (Etype (P))
1926 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1927 and then No (Actuals)
1928 and then Comes_From_Source (N)
1930 Error_Msg_N ("missing explicit dereference in call", N);
1933 Analyze_Call_And_Resolve;
1935 -- If the prefix is the simple name of an entry family, this is a
1936 -- parameterless call from within the task body itself.
1938 elsif Is_Entity_Name (P)
1939 and then Nkind (P) = N_Identifier
1940 and then Ekind (Entity (P)) = E_Entry_Family
1941 and then Present (Actuals)
1942 and then No (Next (First (Actuals)))
1944 -- Can be call to parameterless entry family. What appears to be the
1945 -- sole argument is in fact the entry index. Rewrite prefix of node
1946 -- accordingly. Source representation is unchanged by this
1950 Make_Indexed_Component (Loc,
1952 Make_Selected_Component (Loc,
1953 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1954 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1955 Expressions => Actuals);
1956 Set_Name (N, New_N);
1957 Set_Etype (New_N, Standard_Void_Type);
1958 Set_Parameter_Associations (N, No_List);
1959 Analyze_Call_And_Resolve;
1961 elsif Nkind (P) = N_Explicit_Dereference then
1962 if Ekind (Etype (P)) = E_Subprogram_Type then
1963 Analyze_Call_And_Resolve;
1965 Error_Msg_N ("expect access to procedure in call", P);
1968 -- The name can be a selected component or an indexed component that
1969 -- yields an access to subprogram. Such a prefix is legal if the call
1970 -- has parameter associations.
1972 elsif Is_Access_Type (Etype (P))
1973 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1975 if Present (Actuals) then
1976 Analyze_Call_And_Resolve;
1978 Error_Msg_N ("missing explicit dereference in call ", N);
1981 -- If not an access to subprogram, then the prefix must resolve to the
1982 -- name of an entry, entry family, or protected operation.
1984 -- For the case of a simple entry call, P is a selected component where
1985 -- the prefix is the task and the selector name is the entry. A call to
1986 -- a protected procedure will have the same syntax. If the protected
1987 -- object contains overloaded operations, the entity may appear as a
1988 -- function, the context will select the operation whose type is Void.
1990 elsif Nkind (P) = N_Selected_Component
1991 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1995 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1996 -- in prefix notation may still be missing its controlling argument,
1997 -- so perform the transformation now.
1999 if SPARK_Mode = On and then In_Inlined_Body then
2001 Subp : constant Entity_Id := Entity (Selector_Name (P));
2002 Typ : constant Entity_Id := Etype (Prefix (P));
2005 if Is_Tagged_Type (Typ)
2006 and then Present (First_Formal (Subp))
2007 and then (Etype (First_Formal (Subp)) = Typ
2009 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
2010 and then Try_Object_Operation (P)
2015 Analyze_Call_And_Resolve;
2020 Analyze_Call_And_Resolve;
2023 elsif Nkind (P) = N_Selected_Component
2024 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
2025 and then Present (Actuals)
2026 and then No (Next (First (Actuals)))
2028 -- Can be call to parameterless entry family. What appears to be the
2029 -- sole argument is in fact the entry index. Rewrite prefix of node
2030 -- accordingly. Source representation is unchanged by this
2034 Make_Indexed_Component (Loc,
2035 Prefix => New_Copy (P),
2036 Expressions => Actuals);
2037 Set_Name (N, New_N);
2038 Set_Etype (New_N, Standard_Void_Type);
2039 Set_Parameter_Associations (N, No_List);
2040 Analyze_Call_And_Resolve;
2042 -- For the case of a reference to an element of an entry family, P is
2043 -- an indexed component whose prefix is a selected component (task and
2044 -- entry family), and whose index is the entry family index.
2046 elsif Nkind (P) = N_Indexed_Component
2047 and then Nkind (Prefix (P)) = N_Selected_Component
2048 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
2050 Analyze_Call_And_Resolve;
2052 -- If the prefix is the name of an entry family, it is a call from
2053 -- within the task body itself.
2055 elsif Nkind (P) = N_Indexed_Component
2056 and then Nkind (Prefix (P)) = N_Identifier
2057 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
2060 Make_Selected_Component (Loc,
2062 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
2063 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
2064 Rewrite (Prefix (P), New_N);
2066 Analyze_Call_And_Resolve;
2068 -- In Ada 2012. a qualified expression is a name, but it cannot be a
2069 -- procedure name, so the construct can only be a qualified expression.
2071 elsif Nkind (P) = N_Qualified_Expression
2072 and then Ada_Version >= Ada_2012
2074 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
2077 -- Anything else is an error
2080 Error_Msg_N ("invalid procedure or entry call", N);
2084 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2085 end Analyze_Procedure_Call;
2087 ------------------------------
2088 -- Analyze_Return_Statement --
2089 ------------------------------
2091 procedure Analyze_Return_Statement (N : Node_Id) is
2092 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
2093 N_Simple_Return_Statement));
2095 Returns_Object : constant Boolean :=
2096 Nkind (N) = N_Extended_Return_Statement
2098 (Nkind (N) = N_Simple_Return_Statement
2099 and then Present (Expression (N)));
2100 -- True if we're returning something; that is, "return <expression>;"
2101 -- or "return Result : T [:= ...]". False for "return;". Used for error
2102 -- checking: If Returns_Object is True, N should apply to a function
2103 -- body; otherwise N should apply to a procedure body, entry body,
2104 -- accept statement, or extended return statement.
2106 function Find_What_It_Applies_To return Entity_Id;
2107 -- Find the entity representing the innermost enclosing body, accept
2108 -- statement, or extended return statement. If the result is a callable
2109 -- construct or extended return statement, then this will be the value
2110 -- of the Return_Applies_To attribute. Otherwise, the program is
2111 -- illegal. See RM-6.5(4/2).
2113 -----------------------------
2114 -- Find_What_It_Applies_To --
2115 -----------------------------
2117 function Find_What_It_Applies_To return Entity_Id is
2118 Result : Entity_Id := Empty;
2121 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2122 -- and postconditions.
2124 for J in reverse 0 .. Scope_Stack.Last loop
2125 Result := Scope_Stack.Table (J).Entity;
2126 exit when not Ekind_In (Result, E_Block, E_Loop)
2127 and then Chars (Result) /= Name_uPostconditions;
2130 pragma Assert (Present (Result));
2132 end Find_What_It_Applies_To;
2134 -- Local declarations
2136 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2137 Kind : constant Entity_Kind := Ekind (Scope_Id);
2138 Loc : constant Source_Ptr := Sloc (N);
2139 Stm_Entity : constant Entity_Id :=
2141 (E_Return_Statement, Current_Scope, Loc, 'R');
2143 -- Start of processing for Analyze_Return_Statement
2146 Set_Return_Statement_Entity (N, Stm_Entity);
2148 Set_Etype (Stm_Entity, Standard_Void_Type);
2149 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2151 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2152 -- (4/2): an inner return statement will apply to this extended return.
2154 if Nkind (N) = N_Extended_Return_Statement then
2155 Push_Scope (Stm_Entity);
2158 -- Check that pragma No_Return is obeyed. Don't complain about the
2159 -- implicitly-generated return that is placed at the end.
2161 if No_Return (Scope_Id)
2162 and then Ekind_In (Kind, E_Procedure, E_Generic_Procedure)
2163 and then Comes_From_Source (N)
2166 ("RETURN statement not allowed in No_Return procedure", N);
2169 -- Warn on any unassigned OUT parameters if in procedure
2171 if Ekind (Scope_Id) = E_Procedure then
2172 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2175 -- Check that functions return objects, and other things do not
2177 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2178 if not Returns_Object then
2179 Error_Msg_N ("missing expression in return from function", N);
2182 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2183 if Returns_Object then
2184 Error_Msg_N ("procedure cannot return value (use function)", N);
2187 elsif Ekind_In (Kind, E_Entry, E_Entry_Family) then
2188 if Returns_Object then
2189 if Is_Protected_Type (Scope (Scope_Id)) then
2190 Error_Msg_N ("entry body cannot return value", N);
2192 Error_Msg_N ("accept statement cannot return value", N);
2196 elsif Kind = E_Return_Statement then
2198 -- We are nested within another return statement, which must be an
2199 -- extended_return_statement.
2201 if Returns_Object then
2202 if Nkind (N) = N_Extended_Return_Statement then
2204 ("extended return statement cannot be nested (use `RETURN;`)",
2207 -- Case of a simple return statement with a value inside extended
2208 -- return statement.
2212 ("return nested in extended return statement cannot return "
2213 & "value (use `RETURN;`)", N);
2218 Error_Msg_N ("illegal context for return statement", N);
2221 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2222 Analyze_Function_Return (N);
2224 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2225 Set_Return_Present (Scope_Id);
2228 if Nkind (N) = N_Extended_Return_Statement then
2232 Kill_Current_Values (Last_Assignment_Only => True);
2233 Check_Unreachable_Code (N);
2235 Analyze_Dimension (N);
2236 end Analyze_Return_Statement;
2238 -------------------------------------
2239 -- Analyze_Simple_Return_Statement --
2240 -------------------------------------
2242 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2244 if Present (Expression (N)) then
2245 Mark_Coextensions (N, Expression (N));
2248 Analyze_Return_Statement (N);
2249 end Analyze_Simple_Return_Statement;
2251 -------------------------
2252 -- Analyze_Return_Type --
2253 -------------------------
2255 procedure Analyze_Return_Type (N : Node_Id) is
2256 Designator : constant Entity_Id := Defining_Entity (N);
2257 Typ : Entity_Id := Empty;
2260 -- Normal case where result definition does not indicate an error
2262 if Result_Definition (N) /= Error then
2263 if Nkind (Result_Definition (N)) = N_Access_Definition then
2265 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2268 AD : constant Node_Id :=
2269 Access_To_Subprogram_Definition (Result_Definition (N));
2271 if Present (AD) and then Protected_Present (AD) then
2272 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2274 Typ := Access_Definition (N, Result_Definition (N));
2278 Set_Parent (Typ, Result_Definition (N));
2279 Set_Is_Local_Anonymous_Access (Typ);
2280 Set_Etype (Designator, Typ);
2282 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2284 Null_Exclusion_Static_Checks (N);
2286 -- Subtype_Mark case
2289 Find_Type (Result_Definition (N));
2290 Typ := Entity (Result_Definition (N));
2291 Set_Etype (Designator, Typ);
2293 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2295 Null_Exclusion_Static_Checks (N);
2297 -- If a null exclusion is imposed on the result type, then create
2298 -- a null-excluding itype (an access subtype) and use it as the
2299 -- function's Etype. Note that the null exclusion checks are done
2300 -- right before this, because they don't get applied to types that
2301 -- do not come from source.
2303 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2304 Set_Etype (Designator,
2305 Create_Null_Excluding_Itype
2308 Scope_Id => Scope (Current_Scope)));
2310 -- The new subtype must be elaborated before use because
2311 -- it is visible outside of the function. However its base
2312 -- type may not be frozen yet, so the reference that will
2313 -- force elaboration must be attached to the freezing of
2316 -- If the return specification appears on a proper body,
2317 -- the subtype will have been created already on the spec.
2319 if Is_Frozen (Typ) then
2320 if Nkind (Parent (N)) = N_Subprogram_Body
2321 and then Nkind (Parent (Parent (N))) = N_Subunit
2325 Build_Itype_Reference (Etype (Designator), Parent (N));
2329 Ensure_Freeze_Node (Typ);
2332 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2334 Set_Itype (IR, Etype (Designator));
2335 Append_Freeze_Actions (Typ, New_List (IR));
2340 Set_Etype (Designator, Typ);
2343 if Ekind (Typ) = E_Incomplete_Type
2344 or else (Is_Class_Wide_Type (Typ)
2345 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2347 -- AI05-0151: Tagged incomplete types are allowed in all formal
2348 -- parts. Untagged incomplete types are not allowed in bodies.
2349 -- As a consequence, limited views cannot appear in a basic
2350 -- declaration that is itself within a body, because there is
2351 -- no point at which the non-limited view will become visible.
2353 if Ada_Version >= Ada_2012 then
2354 if From_Limited_With (Typ) and then In_Package_Body then
2356 ("invalid use of incomplete type&",
2357 Result_Definition (N), Typ);
2359 -- The return type of a subprogram body cannot be of a
2360 -- formal incomplete type.
2362 elsif Is_Generic_Type (Typ)
2363 and then Nkind (Parent (N)) = N_Subprogram_Body
2366 ("return type cannot be a formal incomplete type",
2367 Result_Definition (N));
2369 elsif Is_Class_Wide_Type (Typ)
2370 and then Is_Generic_Type (Root_Type (Typ))
2371 and then Nkind (Parent (N)) = N_Subprogram_Body
2374 ("return type cannot be a formal incomplete type",
2375 Result_Definition (N));
2377 elsif Is_Tagged_Type (Typ) then
2380 -- Use is legal in a thunk generated for an operation
2381 -- inherited from a progenitor.
2383 elsif Is_Thunk (Designator)
2384 and then Present (Non_Limited_View (Typ))
2388 elsif Nkind (Parent (N)) = N_Subprogram_Body
2389 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2393 ("invalid use of untagged incomplete type&",
2397 -- The type must be completed in the current package. This
2398 -- is checked at the end of the package declaration when
2399 -- Taft-amendment types are identified. If the return type
2400 -- is class-wide, there is no required check, the type can
2401 -- be a bona fide TAT.
2403 if Ekind (Scope (Current_Scope)) = E_Package
2404 and then In_Private_Part (Scope (Current_Scope))
2405 and then not Is_Class_Wide_Type (Typ)
2407 Append_Elmt (Designator, Private_Dependents (Typ));
2412 ("invalid use of incomplete type&", Designator, Typ);
2417 -- Case where result definition does indicate an error
2420 Set_Etype (Designator, Any_Type);
2422 end Analyze_Return_Type;
2424 -----------------------------
2425 -- Analyze_Subprogram_Body --
2426 -----------------------------
2428 procedure Analyze_Subprogram_Body (N : Node_Id) is
2429 Loc : constant Source_Ptr := Sloc (N);
2430 Body_Spec : constant Node_Id := Specification (N);
2431 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2434 if Debug_Flag_C then
2435 Write_Str ("==> subprogram body ");
2436 Write_Name (Chars (Body_Id));
2437 Write_Str (" from ");
2438 Write_Location (Loc);
2443 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2445 -- The real work is split out into the helper, so it can do "return;"
2446 -- without skipping the debug output:
2448 Analyze_Subprogram_Body_Helper (N);
2450 if Debug_Flag_C then
2452 Write_Str ("<== subprogram body ");
2453 Write_Name (Chars (Body_Id));
2454 Write_Str (" from ");
2455 Write_Location (Loc);
2458 end Analyze_Subprogram_Body;
2460 ------------------------------------
2461 -- Analyze_Subprogram_Body_Helper --
2462 ------------------------------------
2464 -- This procedure is called for regular subprogram bodies, generic bodies,
2465 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2466 -- specification matters, and is used to create a proper declaration for
2467 -- the subprogram, or to perform conformance checks.
2469 -- WARNING: This routine manages Ghost regions. Return statements must be
2470 -- replaced by gotos that jump to the end of the routine and restore the
2473 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2474 Body_Spec : Node_Id := Specification (N);
2475 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2476 Loc : constant Source_Ptr := Sloc (N);
2477 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2479 Conformant : Boolean;
2480 Desig_View : Entity_Id := Empty;
2481 Exch_Views : Elist_Id := No_Elist;
2483 Mask_Types : Elist_Id := No_Elist;
2484 Prot_Typ : Entity_Id := Empty;
2485 Spec_Decl : Node_Id := Empty;
2486 Spec_Id : Entity_Id;
2488 Last_Real_Spec_Entity : Entity_Id := Empty;
2489 -- When we analyze a separate spec, the entity chain ends up containing
2490 -- the formals, as well as any itypes generated during analysis of the
2491 -- default expressions for parameters, or the arguments of associated
2492 -- precondition/postcondition pragmas (which are analyzed in the context
2493 -- of the spec since they have visibility on formals).
2495 -- These entities belong with the spec and not the body. However we do
2496 -- the analysis of the body in the context of the spec (again to obtain
2497 -- visibility to the formals), and all the entities generated during
2498 -- this analysis end up also chained to the entity chain of the spec.
2499 -- But they really belong to the body, and there is circuitry to move
2500 -- them from the spec to the body.
2502 -- However, when we do this move, we don't want to move the real spec
2503 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2504 -- variable points to the last real spec entity, so we only move those
2505 -- chained beyond that point. It is initialized to Empty to deal with
2506 -- the case where there is no separate spec.
2508 function Body_Has_Contract return Boolean;
2509 -- Check whether unanalyzed body has an aspect or pragma that may
2510 -- generate a SPARK contract.
2512 function Body_Has_SPARK_Mode_On return Boolean;
2513 -- Check whether SPARK_Mode On applies to the subprogram body, either
2514 -- because it is specified directly on the body, or because it is
2515 -- inherited from the enclosing subprogram or package.
2517 function Build_Internal_Protected_Declaration
2518 (N : Node_Id) return Entity_Id;
2519 -- A subprogram body without a previous spec that appears in a protected
2520 -- body must be expanded separately to create a subprogram declaration
2521 -- for it, in order to resolve internal calls to it from other protected
2524 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2526 procedure Build_Subprogram_Declaration;
2527 -- Create a matching subprogram declaration for subprogram body N
2529 procedure Check_Anonymous_Return;
2530 -- Ada 2005: if a function returns an access type that denotes a task,
2531 -- or a type that contains tasks, we must create a master entity for
2532 -- the anonymous type, which typically will be used in an allocator
2533 -- in the body of the function.
2535 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2536 -- Look ahead to recognize a pragma that may appear after the body.
2537 -- If there is a previous spec, check that it appears in the same
2538 -- declarative part. If the pragma is Inline_Always, perform inlining
2539 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2540 -- If the body acts as a spec, and inlining is required, we create a
2541 -- subprogram declaration for it, in order to attach the body to inline.
2542 -- If pragma does not appear after the body, check whether there is
2543 -- an inline pragma before any local declarations.
2545 procedure Check_Missing_Return;
2546 -- Checks for a function with a no return statements, and also performs
2547 -- the warning checks implemented by Check_Returns. In formal mode, also
2548 -- verify that a function ends with a RETURN and that a procedure does
2549 -- not contain any RETURN.
2551 function Disambiguate_Spec return Entity_Id;
2552 -- When a primitive is declared between the private view and the full
2553 -- view of a concurrent type which implements an interface, a special
2554 -- mechanism is used to find the corresponding spec of the primitive
2557 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2558 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2559 -- incomplete types coming from a limited context and replace their
2560 -- limited views with the non-limited ones. Return the list of changes
2561 -- to be used to undo the transformation.
2563 function Is_Private_Concurrent_Primitive
2564 (Subp_Id : Entity_Id) return Boolean;
2565 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2566 -- type that implements an interface and has a private view.
2568 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2569 -- N is the body generated for an expression function that is not a
2570 -- completion and Spec_Id the defining entity of its spec. Mark all
2571 -- the not-yet-frozen types referenced by the simple return statement
2572 -- of the function as formally frozen.
2574 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2575 -- Find all suitable source pragmas at the top of subprogram body
2576 -- From's declarations and move them after arbitrary node To.
2577 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2578 -- as it applies to the body too.
2580 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2581 -- Undo the transformation done by Exchange_Limited_Views.
2583 procedure Set_Trivial_Subprogram (N : Node_Id);
2584 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2585 -- subprogram whose body is being analyzed. N is the statement node
2586 -- causing the flag to be set, if the following statement is a return
2587 -- of an entity, we mark the entity as set in source to suppress any
2588 -- warning on the stylized use of function stubs with a dummy return.
2590 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2591 -- Undo the transformation done by Mask_Unfrozen_Types
2593 procedure Verify_Overriding_Indicator;
2594 -- If there was a previous spec, the entity has been entered in the
2595 -- current scope previously. If the body itself carries an overriding
2596 -- indicator, check that it is consistent with the known status of the
2599 -----------------------
2600 -- Body_Has_Contract --
2601 -----------------------
2603 function Body_Has_Contract return Boolean is
2604 Decls : constant List_Id := Declarations (N);
2608 -- Check for aspects that may generate a contract
2610 if Present (Aspect_Specifications (N)) then
2611 Item := First (Aspect_Specifications (N));
2612 while Present (Item) loop
2613 if Is_Subprogram_Contract_Annotation (Item) then
2621 -- Check for pragmas that may generate a contract
2623 if Present (Decls) then
2624 Item := First (Decls);
2625 while Present (Item) loop
2626 if Nkind (Item) = N_Pragma
2627 and then Is_Subprogram_Contract_Annotation (Item)
2637 end Body_Has_Contract;
2639 ----------------------------
2640 -- Body_Has_SPARK_Mode_On --
2641 ----------------------------
2643 function Body_Has_SPARK_Mode_On return Boolean is
2644 Decls : constant List_Id := Declarations (N);
2648 -- Check for SPARK_Mode aspect
2650 if Present (Aspect_Specifications (N)) then
2651 Item := First (Aspect_Specifications (N));
2652 while Present (Item) loop
2653 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2654 return Get_SPARK_Mode_From_Annotation (Item) = On;
2661 -- Check for SPARK_Mode pragma
2663 if Present (Decls) then
2664 Item := First (Decls);
2665 while Present (Item) loop
2667 -- Pragmas that apply to a subprogram body are usually grouped
2668 -- together. Look for a potential pragma SPARK_Mode among them.
2670 if Nkind (Item) = N_Pragma then
2671 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2672 return Get_SPARK_Mode_From_Annotation (Item) = On;
2675 -- Otherwise the first non-pragma declarative item terminates
2676 -- the region where pragma SPARK_Mode may appear.
2686 -- Otherwise, the applicable SPARK_Mode is inherited from the
2687 -- enclosing subprogram or package.
2689 return SPARK_Mode = On;
2690 end Body_Has_SPARK_Mode_On;
2692 ------------------------------------------
2693 -- Build_Internal_Protected_Declaration --
2694 ------------------------------------------
2696 function Build_Internal_Protected_Declaration
2697 (N : Node_Id) return Entity_Id
2699 procedure Analyze_Pragmas (From : Node_Id);
2700 -- Analyze all pragmas which follow arbitrary node From
2702 ---------------------
2703 -- Analyze_Pragmas --
2704 ---------------------
2706 procedure Analyze_Pragmas (From : Node_Id) is
2710 Decl := Next (From);
2711 while Present (Decl) loop
2712 if Nkind (Decl) = N_Pragma then
2713 Analyze_Pragma (Decl);
2715 -- No candidate pragmas are available for analysis
2723 end Analyze_Pragmas;
2727 Body_Id : constant Entity_Id := Defining_Entity (N);
2728 Loc : constant Source_Ptr := Sloc (N);
2733 Spec_Id : Entity_Id;
2735 -- Start of processing for Build_Internal_Protected_Declaration
2738 Formal := First_Formal (Body_Id);
2740 -- The protected operation always has at least one formal, namely the
2741 -- object itself, but it is only placed in the parameter list if
2742 -- expansion is enabled.
2744 if Present (Formal) or else Expander_Active then
2745 Formals := Copy_Parameter_List (Body_Id);
2751 Make_Defining_Identifier (Sloc (Body_Id),
2752 Chars => Chars (Body_Id));
2754 -- Indicate that the entity comes from source, to ensure that cross-
2755 -- reference information is properly generated. The body itself is
2756 -- rewritten during expansion, and the body entity will not appear in
2757 -- calls to the operation.
2759 Set_Comes_From_Source (Spec_Id, True);
2761 if Nkind (Specification (N)) = N_Procedure_Specification then
2763 Make_Procedure_Specification (Loc,
2764 Defining_Unit_Name => Spec_Id,
2765 Parameter_Specifications => Formals);
2768 Make_Function_Specification (Loc,
2769 Defining_Unit_Name => Spec_Id,
2770 Parameter_Specifications => Formals,
2771 Result_Definition =>
2772 New_Occurrence_Of (Etype (Body_Id), Loc));
2775 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2776 Set_Corresponding_Body (Decl, Body_Id);
2777 Set_Corresponding_Spec (N, Spec_Id);
2779 Insert_Before (N, Decl);
2781 -- Associate all aspects and pragmas of the body with the spec. This
2782 -- ensures that these annotations apply to the initial declaration of
2783 -- the subprogram body.
2785 Move_Aspects (From => N, To => Decl);
2786 Move_Pragmas (From => N, To => Decl);
2790 -- The analysis of the spec may generate pragmas which require manual
2791 -- analysis. Since the generation of the spec and the relocation of
2792 -- the annotations is driven by the expansion of the stand-alone
2793 -- body, the pragmas will not be analyzed in a timely manner. Do this
2796 Analyze_Pragmas (Decl);
2798 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
2799 -- ensuring in particular that 'Access is illegal.
2801 Set_Convention (Spec_Id, Convention_Intrinsic);
2802 Set_Has_Completion (Spec_Id);
2805 end Build_Internal_Protected_Declaration;
2807 ----------------------------------
2808 -- Build_Subprogram_Declaration --
2809 ----------------------------------
2811 procedure Build_Subprogram_Declaration is
2813 Subp_Decl : Node_Id;
2816 -- Create a matching subprogram spec using the profile of the body.
2817 -- The structure of the tree is identical, but has new entities for
2818 -- the defining unit name and formal parameters.
2821 Make_Subprogram_Declaration (Loc,
2822 Specification => Copy_Subprogram_Spec (Body_Spec));
2823 Set_Comes_From_Source (Subp_Decl, True);
2825 -- Also mark parameters as coming from source
2827 if Present (Parameter_Specifications (Specification (Subp_Decl))) then
2832 First (Parameter_Specifications (Specification (Subp_Decl)));
2834 while Present (Form) loop
2835 Set_Comes_From_Source (Defining_Identifier (Form), True);
2841 -- Relocate the aspects and relevant pragmas from the subprogram body
2842 -- to the generated spec because it acts as the initial declaration.
2844 Insert_Before (N, Subp_Decl);
2845 Move_Aspects (N, To => Subp_Decl);
2846 Move_Pragmas (N, To => Subp_Decl);
2848 -- Ensure that the generated corresponding spec and original body
2849 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2850 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2851 -- correctly set for local subprograms.
2853 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2855 Analyze (Subp_Decl);
2857 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2858 -- the body since the expander may generate calls using that entity.
2859 -- Required to ensure that Expand_Call rewrites calls to this
2860 -- function by calls to the built procedure.
2862 if Modify_Tree_For_C
2863 and then Nkind (Body_Spec) = N_Function_Specification
2865 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2867 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2868 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2869 Corresponding_Procedure
2870 (Defining_Entity (Specification (Subp_Decl))));
2873 -- Analyze any relocated source pragmas or pragmas created for aspect
2876 Decl := Next (Subp_Decl);
2877 while Present (Decl) loop
2879 -- Stop the search for pragmas once the body has been reached as
2880 -- this terminates the region where pragmas may appear.
2885 elsif Nkind (Decl) = N_Pragma then
2892 Spec_Id := Defining_Entity (Subp_Decl);
2893 Set_Corresponding_Spec (N, Spec_Id);
2895 -- Mark the generated spec as a source construct to ensure that all
2896 -- calls to it are properly registered in ALI files for GNATprove.
2898 Set_Comes_From_Source (Spec_Id, True);
2900 -- Ensure that the specs of the subprogram declaration and its body
2901 -- are identical, otherwise they will appear non-conformant due to
2902 -- rewritings in the default values of formal parameters.
2904 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2905 Set_Specification (N, Body_Spec);
2906 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2907 end Build_Subprogram_Declaration;
2909 ----------------------------
2910 -- Check_Anonymous_Return --
2911 ----------------------------
2913 procedure Check_Anonymous_Return is
2919 if Present (Spec_Id) then
2925 if Ekind (Scop) = E_Function
2926 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2927 and then not Is_Thunk (Scop)
2929 -- Skip internally built functions which handle the case of
2930 -- a null access (see Expand_Interface_Conversion)
2932 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2933 and then not Comes_From_Source (Parent (Scop)))
2935 and then (Has_Task (Designated_Type (Etype (Scop)))
2937 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2939 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2940 and then Expander_Active
2942 Decl := Build_Master_Declaration (Loc);
2944 if Present (Declarations (N)) then
2945 Prepend (Decl, Declarations (N));
2947 Set_Declarations (N, New_List (Decl));
2950 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2951 Set_Has_Master_Entity (Scop);
2953 -- Now mark the containing scope as a task master
2956 while Nkind (Par) /= N_Compilation_Unit loop
2957 Par := Parent (Par);
2958 pragma Assert (Present (Par));
2960 -- If we fall off the top, we are at the outer level, and
2961 -- the environment task is our effective master, so nothing
2965 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2967 Set_Is_Task_Master (Par, True);
2972 end Check_Anonymous_Return;
2974 -------------------------
2975 -- Check_Inline_Pragma --
2976 -------------------------
2978 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2982 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2983 -- True when N is a pragma Inline or Inline_Always that applies
2984 -- to this subprogram.
2986 -----------------------
2987 -- Is_Inline_Pragma --
2988 -----------------------
2990 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2992 if Nkind (N) = N_Pragma
2994 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2995 or else (Pragma_Name_Unmapped (N) = Name_Inline
2997 (Front_End_Inlining or else Optimization_Level > 0)))
2998 and then Present (Pragma_Argument_Associations (N))
3001 Pragma_Arg : Node_Id :=
3002 Expression (First (Pragma_Argument_Associations (N)));
3004 if Nkind (Pragma_Arg) = N_Selected_Component then
3005 Pragma_Arg := Selector_Name (Pragma_Arg);
3008 return Chars (Pragma_Arg) = Chars (Body_Id);
3014 end Is_Inline_Pragma;
3016 -- Start of processing for Check_Inline_Pragma
3019 if not Expander_Active then
3023 if Is_List_Member (N)
3024 and then Present (Next (N))
3025 and then Is_Inline_Pragma (Next (N))
3029 elsif Nkind (N) /= N_Subprogram_Body_Stub
3030 and then Present (Declarations (N))
3031 and then Is_Inline_Pragma (First (Declarations (N)))
3033 Prag := First (Declarations (N));
3039 if Present (Prag) then
3040 if Present (Spec_Id) then
3041 if Is_List_Member (N)
3042 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
3043 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
3049 -- Create a subprogram declaration, to make treatment uniform.
3050 -- Make the sloc of the subprogram name that of the entity in
3051 -- the body, so that style checks find identical strings.
3054 Subp : constant Entity_Id :=
3055 Make_Defining_Identifier
3056 (Sloc (Body_Id), Chars (Body_Id));
3057 Decl : constant Node_Id :=
3058 Make_Subprogram_Declaration (Loc,
3060 New_Copy_Tree (Specification (N)));
3063 -- Link the body and the generated spec
3065 Set_Corresponding_Body (Decl, Body_Id);
3066 Set_Corresponding_Spec (N, Subp);
3068 Set_Defining_Unit_Name (Specification (Decl), Subp);
3070 -- To ensure proper coverage when body is inlined, indicate
3071 -- whether the subprogram comes from source.
3073 Set_Comes_From_Source (Subp, Comes_From_Source (N));
3075 if Present (First_Formal (Body_Id)) then
3076 Plist := Copy_Parameter_List (Body_Id);
3077 Set_Parameter_Specifications
3078 (Specification (Decl), Plist);
3081 -- Move aspects to the new spec
3083 if Has_Aspects (N) then
3084 Move_Aspects (N, To => Decl);
3087 Insert_Before (N, Decl);
3090 Set_Has_Pragma_Inline (Subp);
3092 if Pragma_Name (Prag) = Name_Inline_Always then
3093 Set_Is_Inlined (Subp);
3094 Set_Has_Pragma_Inline_Always (Subp);
3097 -- Prior to copying the subprogram body to create a template
3098 -- for it for subsequent inlining, remove the pragma from
3099 -- the current body so that the copy that will produce the
3100 -- new body will start from a completely unanalyzed tree.
3102 if Nkind (Parent (Prag)) = N_Subprogram_Body then
3103 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
3110 end Check_Inline_Pragma;
3112 --------------------------
3113 -- Check_Missing_Return --
3114 --------------------------
3116 procedure Check_Missing_Return is
3118 Missing_Ret : Boolean;
3121 if Nkind (Body_Spec) = N_Function_Specification then
3122 if Present (Spec_Id) then
3128 if Return_Present (Id) then
3129 Check_Returns (HSS, 'F', Missing_Ret);
3132 Set_Has_Missing_Return (Id);
3135 -- Within a premature instantiation of a package with no body, we
3136 -- build completions of the functions therein, with a Raise
3137 -- statement. No point in complaining about a missing return in
3140 elsif Ekind (Id) = E_Function
3141 and then In_Instance
3142 and then Present (Statements (HSS))
3143 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
3147 elsif Is_Generic_Subprogram (Id)
3148 or else not Is_Machine_Code_Subprogram (Id)
3150 Error_Msg_N ("missing RETURN statement in function body", N);
3153 -- If procedure with No_Return, check returns
3155 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3156 if Present (Spec_Id) then
3162 if No_Return (Id) then
3163 Check_Returns (HSS, 'P', Missing_Ret, Id);
3166 end Check_Missing_Return;
3168 -----------------------
3169 -- Disambiguate_Spec --
3170 -----------------------
3172 function Disambiguate_Spec return Entity_Id is
3173 Priv_Spec : Entity_Id;
3176 procedure Replace_Types (To_Corresponding : Boolean);
3177 -- Depending on the flag, replace the type of formal parameters of
3178 -- Body_Id if it is a concurrent type implementing interfaces with
3179 -- the corresponding record type or the other way around.
3181 procedure Replace_Types (To_Corresponding : Boolean) is
3183 Formal_Typ : Entity_Id;
3186 Formal := First_Formal (Body_Id);
3187 while Present (Formal) loop
3188 Formal_Typ := Etype (Formal);
3190 if Is_Class_Wide_Type (Formal_Typ) then
3191 Formal_Typ := Root_Type (Formal_Typ);
3194 -- From concurrent type to corresponding record
3196 if To_Corresponding then
3197 if Is_Concurrent_Type (Formal_Typ)
3198 and then Present (Corresponding_Record_Type (Formal_Typ))
3201 (Corresponding_Record_Type (Formal_Typ)))
3204 Corresponding_Record_Type (Formal_Typ));
3207 -- From corresponding record to concurrent type
3210 if Is_Concurrent_Record_Type (Formal_Typ)
3211 and then Present (Interfaces (Formal_Typ))
3214 Corresponding_Concurrent_Type (Formal_Typ));
3218 Next_Formal (Formal);
3222 -- Start of processing for Disambiguate_Spec
3225 -- Try to retrieve the specification of the body as is. All error
3226 -- messages are suppressed because the body may not have a spec in
3227 -- its current state.
3229 Spec_N := Find_Corresponding_Spec (N, False);
3231 -- It is possible that this is the body of a primitive declared
3232 -- between a private and a full view of a concurrent type. The
3233 -- controlling parameter of the spec carries the concurrent type,
3234 -- not the corresponding record type as transformed by Analyze_
3235 -- Subprogram_Specification. In such cases, we undo the change
3236 -- made by the analysis of the specification and try to find the
3239 -- Note that wrappers already have their corresponding specs and
3240 -- bodies set during their creation, so if the candidate spec is
3241 -- a wrapper, then we definitely need to swap all types to their
3242 -- original concurrent status.
3245 or else Is_Primitive_Wrapper (Spec_N)
3247 -- Restore all references of corresponding record types to the
3248 -- original concurrent types.
3250 Replace_Types (To_Corresponding => False);
3251 Priv_Spec := Find_Corresponding_Spec (N, False);
3253 -- The current body truly belongs to a primitive declared between
3254 -- a private and a full view. We leave the modified body as is,
3255 -- and return the true spec.
3257 if Present (Priv_Spec)
3258 and then Is_Private_Primitive (Priv_Spec)
3263 -- In case that this is some sort of error, restore the original
3264 -- state of the body.
3266 Replace_Types (To_Corresponding => True);
3270 end Disambiguate_Spec;
3272 ----------------------------
3273 -- Exchange_Limited_Views --
3274 ----------------------------
3276 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3277 Result : Elist_Id := No_Elist;
3279 procedure Detect_And_Exchange (Id : Entity_Id);
3280 -- Determine whether Id's type denotes an incomplete type associated
3281 -- with a limited with clause and exchange the limited view with the
3282 -- non-limited one when available. Note that the non-limited view
3283 -- may exist because of a with_clause in another unit in the context,
3284 -- but cannot be used because the current view of the enclosing unit
3285 -- is still a limited view.
3287 -------------------------
3288 -- Detect_And_Exchange --
3289 -------------------------
3291 procedure Detect_And_Exchange (Id : Entity_Id) is
3292 Typ : constant Entity_Id := Etype (Id);
3294 if From_Limited_With (Typ)
3295 and then Has_Non_Limited_View (Typ)
3296 and then not From_Limited_With (Scope (Typ))
3299 Result := New_Elmt_List;
3302 Prepend_Elmt (Typ, Result);
3303 Prepend_Elmt (Id, Result);
3304 Set_Etype (Id, Non_Limited_View (Typ));
3306 end Detect_And_Exchange;
3312 -- Start of processing for Exchange_Limited_Views
3315 -- Do not process subprogram bodies as they already use the non-
3316 -- limited view of types.
3318 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
3322 -- Examine all formals and swap views when applicable
3324 Formal := First_Formal (Subp_Id);
3325 while Present (Formal) loop
3326 Detect_And_Exchange (Formal);
3328 Next_Formal (Formal);
3331 -- Process the return type of a function
3333 if Ekind (Subp_Id) = E_Function then
3334 Detect_And_Exchange (Subp_Id);
3338 end Exchange_Limited_Views;
3340 -------------------------------------
3341 -- Is_Private_Concurrent_Primitive --
3342 -------------------------------------
3344 function Is_Private_Concurrent_Primitive
3345 (Subp_Id : Entity_Id) return Boolean
3347 Formal_Typ : Entity_Id;
3350 if Present (First_Formal (Subp_Id)) then
3351 Formal_Typ := Etype (First_Formal (Subp_Id));
3353 if Is_Concurrent_Record_Type (Formal_Typ) then
3354 if Is_Class_Wide_Type (Formal_Typ) then
3355 Formal_Typ := Root_Type (Formal_Typ);
3358 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3361 -- The type of the first formal is a concurrent tagged type with
3365 Is_Concurrent_Type (Formal_Typ)
3366 and then Is_Tagged_Type (Formal_Typ)
3367 and then Has_Private_Declaration (Formal_Typ);
3371 end Is_Private_Concurrent_Primitive;
3373 -------------------------
3374 -- Mask_Unfrozen_Types --
3375 -------------------------
3377 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3378 Result : Elist_Id := No_Elist;
3380 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3381 -- Mask all types referenced in the subtree rooted at Node
3383 --------------------
3384 -- Mask_Type_Refs --
3385 --------------------
3387 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3388 procedure Mask_Type (Typ : Entity_Id);
3389 -- ??? what does this do?
3395 procedure Mask_Type (Typ : Entity_Id) is
3397 -- Skip Itypes created by the preanalysis
3400 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3405 if not Is_Frozen (Typ) then
3406 if Scope (Typ) /= Current_Scope then
3407 Set_Is_Frozen (Typ);
3408 Append_New_Elmt (Typ, Result);
3410 Freeze_Before (N, Typ);
3415 -- Start of processing for Mask_Type_Refs
3418 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3419 Mask_Type (Etype (Entity (Node)));
3421 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
3422 Mask_Type (Scope (Entity (Node)));
3425 elsif Nkind_In (Node, N_Aggregate, N_Null, N_Type_Conversion)
3426 and then Present (Etype (Node))
3428 Mask_Type (Etype (Node));
3434 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3438 Return_Stmt : constant Node_Id :=
3439 First (Statements (Handled_Statement_Sequence (N)));
3441 -- Start of processing for Mask_Unfrozen_Types
3444 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3446 Mask_References (Expression (Return_Stmt));
3449 end Mask_Unfrozen_Types;
3455 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
3457 Insert_Nod : Node_Id;
3458 Next_Decl : Node_Id;
3461 pragma Assert (Nkind (From) = N_Subprogram_Body);
3463 -- The pragmas are moved in an order-preserving fashion
3467 -- Inspect the declarations of the subprogram body and relocate all
3468 -- candidate pragmas.
3470 Decl := First (Declarations (From));
3471 while Present (Decl) loop
3473 -- Preserve the following declaration for iteration purposes, due
3474 -- to possible relocation of a pragma.
3476 Next_Decl := Next (Decl);
3478 if Nkind (Decl) = N_Pragma then
3479 -- Copy pragma SPARK_Mode if present in the declarative list
3480 -- of subprogram body From and insert it after node To. This
3481 -- pragma should not be moved, as it applies to the body too.
3483 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
3484 Insert_After (Insert_Nod, New_Copy_Tree (Decl));
3486 -- Move relevant pragmas to the spec
3488 elsif Nam_In (Pragma_Name_Unmapped (Decl),
3495 Name_Refined_Depends,
3496 Name_Refined_Global,
3500 Name_Volatile_Function)
3503 Insert_After (Insert_Nod, Decl);
3507 -- Skip internally generated code
3509 elsif not Comes_From_Source (Decl) then
3512 -- No candidate pragmas are available for relocation
3522 ---------------------------
3523 -- Restore_Limited_Views --
3524 ---------------------------
3526 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3527 Elmt : Elmt_Id := First_Elmt (Restore_List);
3531 while Present (Elmt) loop
3534 Set_Etype (Id, Node (Elmt));
3537 end Restore_Limited_Views;
3539 ----------------------------
3540 -- Set_Trivial_Subprogram --
3541 ----------------------------
3543 procedure Set_Trivial_Subprogram (N : Node_Id) is
3544 Nxt : constant Node_Id := Next (N);
3547 Set_Is_Trivial_Subprogram (Body_Id);
3549 if Present (Spec_Id) then
3550 Set_Is_Trivial_Subprogram (Spec_Id);
3554 and then Nkind (Nxt) = N_Simple_Return_Statement
3555 and then No (Next (Nxt))
3556 and then Present (Expression (Nxt))
3557 and then Is_Entity_Name (Expression (Nxt))
3559 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3561 end Set_Trivial_Subprogram;
3563 ---------------------------
3564 -- Unmask_Unfrozen_Types --
3565 ---------------------------
3567 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3568 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3571 while Present (Elmt) loop
3572 Set_Is_Frozen (Node (Elmt), False);
3575 end Unmask_Unfrozen_Types;
3577 ---------------------------------
3578 -- Verify_Overriding_Indicator --
3579 ---------------------------------
3581 procedure Verify_Overriding_Indicator is
3583 if Must_Override (Body_Spec) then
3584 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3585 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3589 -- Overridden controlled primitives may have had their
3590 -- Overridden_Operation field cleared according to the setting of
3591 -- the Is_Hidden flag. An issue arises, however, when analyzing
3592 -- an instance that may have manipulated the flag during
3593 -- expansion. As a result, we add an exception for this case.
3595 elsif not Present (Overridden_Operation (Spec_Id))
3596 and then not (Nam_In (Chars (Spec_Id), Name_Adjust,
3599 and then In_Instance)
3602 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3604 -- Overriding indicators aren't allowed for protected subprogram
3605 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3606 -- this to a warning if -gnatd.E is enabled.
3608 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3609 Error_Msg_Warn := Error_To_Warning;
3611 ("<<overriding indicator not allowed for protected "
3612 & "subprogram body", Body_Spec);
3615 elsif Must_Not_Override (Body_Spec) then
3616 if Present (Overridden_Operation (Spec_Id)) then
3618 ("subprogram& overrides inherited operation",
3619 Body_Spec, Spec_Id);
3621 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3622 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3625 ("subprogram& overrides predefined operator ",
3626 Body_Spec, Spec_Id);
3628 -- Overriding indicators aren't allowed for protected subprogram
3629 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3630 -- this to a warning if -gnatd.E is enabled.
3632 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3633 Error_Msg_Warn := Error_To_Warning;
3636 ("<<overriding indicator not allowed "
3637 & "for protected subprogram body", Body_Spec);
3639 -- If this is not a primitive operation, then the overriding
3640 -- indicator is altogether illegal.
3642 elsif not Is_Primitive (Spec_Id) then
3644 ("overriding indicator only allowed "
3645 & "if subprogram is primitive", Body_Spec);
3648 -- If checking the style rule and the operation overrides, then
3649 -- issue a warning about a missing overriding_indicator. Protected
3650 -- subprogram bodies are excluded from this style checking, since
3651 -- they aren't primitives (even though their declarations can
3652 -- override) and aren't allowed to have an overriding_indicator.
3655 and then Present (Overridden_Operation (Spec_Id))
3656 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3658 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3659 Style.Missing_Overriding (N, Body_Id);
3662 and then Can_Override_Operator (Spec_Id)
3663 and then not In_Predefined_Unit (Spec_Id)
3665 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3666 Style.Missing_Overriding (N, Body_Id);
3668 end Verify_Overriding_Indicator;
3672 Body_Nod : Node_Id := Empty;
3673 Minimum_Acc_Objs : List_Id := No_List;
3675 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3676 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3677 Saved_EA : constant Boolean := Expander_Active;
3678 Saved_ISMP : constant Boolean :=
3679 Ignore_SPARK_Mode_Pragmas_In_Instance;
3680 -- Save the Ghost and SPARK mode-related data to restore on exit
3682 -- Start of processing for Analyze_Subprogram_Body_Helper
3685 -- A [generic] subprogram body freezes the contract of the nearest
3686 -- enclosing package body and all other contracts encountered in the
3687 -- same declarative part up to and excluding the subprogram body:
3689 -- package body Nearest_Enclosing_Package
3690 -- with Refined_State => (State => Constit)
3694 -- procedure Freezes_Enclosing_Package_Body
3695 -- with Refined_Depends => (Input => Constit) ...
3697 -- This ensures that any annotations referenced by the contract of the
3698 -- [generic] subprogram body are available. This form of freezing is
3699 -- decoupled from the usual Freeze_xxx mechanism because it must also
3700 -- work in the context of generics where normal freezing is disabled.
3702 -- Only bodies coming from source should cause this type of freezing.
3703 -- Expression functions that act as bodies and complete an initial
3704 -- declaration must be included in this category, hence the use of
3707 if Comes_From_Source (Original_Node (N)) then
3708 Freeze_Previous_Contracts (N);
3711 -- Generic subprograms are handled separately. They always have a
3712 -- generic specification. Determine whether current scope has a
3713 -- previous declaration.
3715 -- If the subprogram body is defined within an instance of the same
3716 -- name, the instance appears as a package renaming, and will be hidden
3717 -- within the subprogram.
3719 if Present (Prev_Id)
3720 and then not Is_Overloadable (Prev_Id)
3721 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3722 or else Comes_From_Source (Prev_Id))
3724 if Is_Generic_Subprogram (Prev_Id) then
3727 -- A subprogram body is Ghost when it is stand-alone and subject
3728 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3729 -- the mode now to ensure that any nodes generated during analysis
3730 -- and expansion are properly marked as Ghost.
3732 Mark_And_Set_Ghost_Body (N, Spec_Id);
3734 -- If the body completes the initial declaration of a compilation
3735 -- unit which is subject to pragma Elaboration_Checks, set the
3736 -- model specified by the pragma because it applies to all parts
3739 Install_Elaboration_Model (Spec_Id);
3741 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3742 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3744 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3746 if Nkind (N) = N_Subprogram_Body then
3747 HSS := Handled_Statement_Sequence (N);
3748 Check_Missing_Return;
3753 -- Otherwise a previous entity conflicts with the subprogram name.
3754 -- Attempting to enter name will post error.
3757 Enter_Name (Body_Id);
3761 -- Non-generic case, find the subprogram declaration, if one was seen,
3762 -- or enter new overloaded entity in the current scope. If the
3763 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3764 -- part of the context of one of its subunits. No need to redo the
3767 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3771 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3773 if Nkind (N) = N_Subprogram_Body_Stub
3774 or else No (Corresponding_Spec (N))
3776 if Is_Private_Concurrent_Primitive (Body_Id) then
3777 Spec_Id := Disambiguate_Spec;
3779 -- A subprogram body is Ghost when it is stand-alone and
3780 -- subject to pragma Ghost or when the corresponding spec is
3781 -- Ghost. Set the mode now to ensure that any nodes generated
3782 -- during analysis and expansion are properly marked as Ghost.
3784 Mark_And_Set_Ghost_Body (N, Spec_Id);
3786 -- If the body completes a compilation unit which is subject
3787 -- to pragma Elaboration_Checks, set the model specified by
3788 -- the pragma because it applies to all parts of the unit.
3790 Install_Elaboration_Model (Spec_Id);
3793 Spec_Id := Find_Corresponding_Spec (N);
3795 -- A subprogram body is Ghost when it is stand-alone and
3796 -- subject to pragma Ghost or when the corresponding spec is
3797 -- Ghost. Set the mode now to ensure that any nodes generated
3798 -- during analysis and expansion are properly marked as Ghost.
3800 Mark_And_Set_Ghost_Body (N, Spec_Id);
3802 -- If the body completes a compilation unit which is subject
3803 -- to pragma Elaboration_Checks, set the model specified by
3804 -- the pragma because it applies to all parts of the unit.
3806 Install_Elaboration_Model (Spec_Id);
3808 -- In GNATprove mode, if the body has no previous spec, create
3809 -- one so that the inlining machinery can operate properly.
3810 -- Transfer aspects, if any, to the new spec, so that they
3811 -- are legal and can be processed ahead of the body.
3812 -- We make two copies of the given spec, one for the new
3813 -- declaration, and one for the body.
3814 -- ??? This should be conditioned on front-end inlining rather
3815 -- than GNATprove_Mode.
3817 if No (Spec_Id) and then GNATprove_Mode
3819 -- Inlining does not apply during preanalysis of code
3821 and then Full_Analysis
3823 -- Inlining only applies to full bodies, not stubs
3825 and then Nkind (N) /= N_Subprogram_Body_Stub
3827 -- Inlining only applies to bodies in the source code, not to
3828 -- those generated by the compiler. In particular, expression
3829 -- functions, whose body is generated by the compiler, are
3830 -- treated specially by GNATprove.
3832 and then Comes_From_Source (Body_Id)
3834 -- This cannot be done for a compilation unit, which is not
3835 -- in a context where we can insert a new spec.
3837 and then Is_List_Member (N)
3839 -- Inlining only applies to subprograms without contracts,
3840 -- as a contract is a sign that GNATprove should perform a
3841 -- modular analysis of the subprogram instead of a contextual
3842 -- analysis at each call site. The same test is performed in
3843 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3844 -- here in another form (because the contract has not been
3845 -- attached to the body) to avoid front-end errors in case
3846 -- pragmas are used instead of aspects, because the
3847 -- corresponding pragmas in the body would not be transferred
3848 -- to the spec, leading to legality errors.
3850 and then not Body_Has_Contract
3851 and then not Inside_A_Generic
3853 Build_Subprogram_Declaration;
3855 -- If this is a function that returns a constrained array, and
3856 -- we are generating C code, create subprogram declaration
3857 -- to simplify subsequent C generation.
3860 and then Modify_Tree_For_C
3861 and then Nkind (Body_Spec) = N_Function_Specification
3862 and then Is_Array_Type (Etype (Body_Id))
3863 and then Is_Constrained (Etype (Body_Id))
3865 Build_Subprogram_Declaration;
3869 -- If this is a duplicate body, no point in analyzing it
3871 if Error_Posted (N) then
3875 -- A subprogram body should cause freezing of its own declaration,
3876 -- but if there was no previous explicit declaration, then the
3877 -- subprogram will get frozen too late (there may be code within
3878 -- the body that depends on the subprogram having been frozen,
3879 -- such as uses of extra formals), so we force it to be frozen
3880 -- here. Same holds if the body and spec are compilation units.
3881 -- Finally, if the return type is an anonymous access to protected
3882 -- subprogram, it must be frozen before the body because its
3883 -- expansion has generated an equivalent type that is used when
3884 -- elaborating the body.
3886 -- An exception in the case of Ada 2012, AI05-177: The bodies
3887 -- created for expression functions do not freeze.
3890 and then Nkind (Original_Node (N)) /= N_Expression_Function
3892 Freeze_Before (N, Body_Id);
3894 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3895 Freeze_Before (N, Spec_Id);
3897 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3898 Freeze_Before (N, Etype (Body_Id));
3902 Spec_Id := Corresponding_Spec (N);
3904 -- A subprogram body is Ghost when it is stand-alone and subject
3905 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3906 -- the mode now to ensure that any nodes generated during analysis
3907 -- and expansion are properly marked as Ghost.
3909 Mark_And_Set_Ghost_Body (N, Spec_Id);
3911 -- If the body completes the initial declaration of a compilation
3912 -- unit which is subject to pragma Elaboration_Checks, set the
3913 -- model specified by the pragma because it applies to all parts
3916 Install_Elaboration_Model (Spec_Id);
3920 -- Deactivate expansion inside the body of ignored Ghost entities,
3921 -- as this code will ultimately be ignored. This avoids requiring the
3922 -- presence of run-time units which are not needed. Only do this for
3923 -- user entities, as internally generated entitities might still need
3924 -- to be expanded (e.g. those generated for types).
3926 if Present (Ignored_Ghost_Region)
3927 and then Comes_From_Source (Body_Id)
3929 Expander_Active := False;
3932 -- Previously we scanned the body to look for nested subprograms, and
3933 -- rejected an inline directive if nested subprograms were present,
3934 -- because the back-end would generate conflicting symbols for the
3935 -- nested bodies. This is now unnecessary.
3937 -- Look ahead to recognize a pragma Inline that appears after the body
3939 Check_Inline_Pragma (Spec_Id);
3941 -- Deal with special case of a fully private operation in the body of
3942 -- the protected type. We must create a declaration for the subprogram,
3943 -- in order to attach the subprogram that will be used in internal
3944 -- calls. We exclude compiler generated bodies from the expander since
3945 -- the issue does not arise for those cases.
3948 and then Comes_From_Source (N)
3949 and then Is_Protected_Type (Current_Scope)
3951 Spec_Id := Build_Internal_Protected_Declaration (N);
3954 -- If we are generating C and this is a function returning a constrained
3955 -- array type for which we must create a procedure with an extra out
3956 -- parameter, build and analyze the body now. The procedure declaration
3957 -- has already been created. We reuse the source body of the function,
3958 -- because in an instance it may contain global references that cannot
3959 -- be reanalyzed. The source function itself is not used any further,
3960 -- so we mark it as having a completion. If the subprogram is a stub the
3961 -- transformation is done later, when the proper body is analyzed.
3964 and then Modify_Tree_For_C
3965 and then Present (Spec_Id)
3966 and then Ekind (Spec_Id) = E_Function
3967 and then Nkind (N) /= N_Subprogram_Body_Stub
3968 and then Rewritten_For_C (Spec_Id)
3970 Set_Has_Completion (Spec_Id);
3972 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3975 -- The entity for the created procedure must remain invisible, so it
3976 -- does not participate in resolution of subsequent references to the
3979 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3983 -- If a separate spec is present, then deal with freezing issues
3985 if Present (Spec_Id) then
3986 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3987 Verify_Overriding_Indicator;
3989 -- In general, the spec will be frozen when we start analyzing the
3990 -- body. However, for internally generated operations, such as
3991 -- wrapper functions for inherited operations with controlling
3992 -- results, the spec may not have been frozen by the time we expand
3993 -- the freeze actions that include the bodies. In particular, extra
3994 -- formals for accessibility or for return-in-place may need to be
3995 -- generated. Freeze nodes, if any, are inserted before the current
3996 -- body. These freeze actions are also needed in Compile_Only mode to
3997 -- enable the proper back-end type annotations.
3998 -- They are necessary in any case to ensure proper elaboration order
4001 if Nkind (N) = N_Subprogram_Body
4002 and then Was_Expression_Function (N)
4003 and then not Has_Completion (Spec_Id)
4004 and then Serious_Errors_Detected = 0
4005 and then (Expander_Active
4006 or else Operating_Mode = Check_Semantics
4007 or else Is_Ignored_Ghost_Entity (Spec_Id))
4009 -- The body generated for an expression function that is not a
4010 -- completion is a freeze point neither for the profile nor for
4011 -- anything else. That's why, in order to prevent any freezing
4012 -- during analysis, we need to mask types declared outside the
4013 -- expression (and in an outer scope) that are not yet frozen.
4014 -- This also needs to be done in the case of an ignored Ghost
4015 -- expression function, where the expander isn't active.
4017 Set_Is_Frozen (Spec_Id);
4018 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
4020 elsif not Is_Frozen (Spec_Id)
4021 and then Serious_Errors_Detected = 0
4023 Set_Has_Delayed_Freeze (Spec_Id);
4024 Freeze_Before (N, Spec_Id);
4028 -- If the subprogram has a class-wide clone, build its body as a copy
4029 -- of the original body, and rewrite body of original subprogram as a
4030 -- wrapper that calls the clone. If N is a stub, this construction will
4031 -- take place when the proper body is analyzed. No action needed if this
4032 -- subprogram has been eliminated.
4034 if Present (Spec_Id)
4035 and then Present (Class_Wide_Clone (Spec_Id))
4036 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
4037 and then Nkind (N) /= N_Subprogram_Body_Stub
4038 and then not (Expander_Active and then Is_Eliminated (Spec_Id))
4040 Build_Class_Wide_Clone_Body (Spec_Id, N);
4042 -- This is the new body for the existing primitive operation
4044 Rewrite (N, Build_Class_Wide_Clone_Call
4045 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
4046 Set_Has_Completion (Spec_Id, False);
4051 -- Place subprogram on scope stack, and make formals visible. If there
4052 -- is a spec, the visible entity remains that of the spec.
4054 if Present (Spec_Id) then
4055 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
4057 if Is_Child_Unit (Spec_Id) then
4058 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
4062 Style.Check_Identifier (Body_Id, Spec_Id);
4065 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4066 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4068 if Is_Abstract_Subprogram (Spec_Id) then
4069 Error_Msg_N ("an abstract subprogram cannot have a body", N);
4073 Set_Convention (Body_Id, Convention (Spec_Id));
4074 Set_Has_Completion (Spec_Id);
4076 if Is_Protected_Type (Scope (Spec_Id)) then
4077 Prot_Typ := Scope (Spec_Id);
4080 -- If this is a body generated for a renaming, do not check for
4081 -- full conformance. The check is redundant, because the spec of
4082 -- the body is a copy of the spec in the renaming declaration,
4083 -- and the test can lead to spurious errors on nested defaults.
4085 if Present (Spec_Decl)
4086 and then not Comes_From_Source (N)
4088 (Nkind (Original_Node (Spec_Decl)) =
4089 N_Subprogram_Renaming_Declaration
4090 or else (Present (Corresponding_Body (Spec_Decl))
4092 Nkind (Unit_Declaration_Node
4093 (Corresponding_Body (Spec_Decl))) =
4094 N_Subprogram_Renaming_Declaration))
4098 -- Conversely, the spec may have been generated for specless body
4099 -- with an inline pragma. The entity comes from source, which is
4100 -- both semantically correct and necessary for proper inlining.
4101 -- The subprogram declaration itself is not in the source.
4103 elsif Comes_From_Source (N)
4104 and then Present (Spec_Decl)
4105 and then not Comes_From_Source (Spec_Decl)
4106 and then Has_Pragma_Inline (Spec_Id)
4113 Fully_Conformant, True, Conformant, Body_Id);
4116 -- If the body is not fully conformant, we have to decide if we
4117 -- should analyze it or not. If it has a really messed up profile
4118 -- then we probably should not analyze it, since we will get too
4119 -- many bogus messages.
4121 -- Our decision is to go ahead in the non-fully conformant case
4122 -- only if it is at least mode conformant with the spec. Note
4123 -- that the call to Check_Fully_Conformant has issued the proper
4124 -- error messages to complain about the lack of conformance.
4127 and then not Mode_Conformant (Body_Id, Spec_Id)
4133 -- In the case we are dealing with an expression function we check
4134 -- the formals attached to the spec instead of the body - so we don't
4135 -- reference body formals.
4137 if Spec_Id /= Body_Id
4138 and then not Is_Expression_Function (Spec_Id)
4140 Reference_Body_Formals (Spec_Id, Body_Id);
4143 Set_Ekind (Body_Id, E_Subprogram_Body);
4145 if Nkind (N) = N_Subprogram_Body_Stub then
4146 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4151 Set_Corresponding_Spec (N, Spec_Id);
4153 -- Ada 2005 (AI-345): If the operation is a primitive operation
4154 -- of a concurrent type, the type of the first parameter has been
4155 -- replaced with the corresponding record, which is the proper
4156 -- run-time structure to use. However, within the body there may
4157 -- be uses of the formals that depend on primitive operations
4158 -- of the type (in particular calls in prefixed form) for which
4159 -- we need the original concurrent type. The operation may have
4160 -- several controlling formals, so the replacement must be done
4163 if Comes_From_Source (Spec_Id)
4164 and then Present (First_Entity (Spec_Id))
4165 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4166 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4167 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4168 and then Present (Corresponding_Concurrent_Type
4169 (Etype (First_Entity (Spec_Id))))
4172 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4176 Form := First_Formal (Spec_Id);
4177 while Present (Form) loop
4178 if Etype (Form) = Typ then
4179 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4187 -- Make the formals visible, and place subprogram on scope stack.
4188 -- This is also the point at which we set Last_Real_Spec_Entity
4189 -- to mark the entities which will not be moved to the body.
4191 Install_Formals (Spec_Id);
4192 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4194 -- Within an instance, add local renaming declarations so that
4195 -- gdb can retrieve the values of actuals more easily. This is
4196 -- only relevant if generating code.
4198 if Is_Generic_Instance (Spec_Id)
4199 and then Is_Wrapper_Package (Current_Scope)
4200 and then Expander_Active
4202 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4205 Push_Scope (Spec_Id);
4207 -- Make sure that the subprogram is immediately visible. For
4208 -- child units that have no separate spec this is indispensable.
4209 -- Otherwise it is safe albeit redundant.
4211 Set_Is_Immediately_Visible (Spec_Id);
4214 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4215 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4216 Set_Scope (Body_Id, Scope (Spec_Id));
4218 -- Case of subprogram body with no previous spec
4221 -- Check for style warning required
4225 -- Only apply check for source level subprograms for which checks
4226 -- have not been suppressed.
4228 and then Comes_From_Source (Body_Id)
4229 and then not Suppress_Style_Checks (Body_Id)
4231 -- No warnings within an instance
4233 and then not In_Instance
4235 -- No warnings for expression functions
4237 and then Nkind (Original_Node (N)) /= N_Expression_Function
4239 Style.Body_With_No_Spec (N);
4242 New_Overloaded_Entity (Body_Id);
4244 if Nkind (N) /= N_Subprogram_Body_Stub then
4245 Set_Acts_As_Spec (N);
4246 Generate_Definition (Body_Id);
4248 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4250 -- If the body is an entry wrapper created for an entry with
4251 -- preconditions, it must be compiled in the context of the
4252 -- enclosing synchronized object, because it may mention other
4253 -- operations of the type.
4255 if Is_Entry_Wrapper (Body_Id) then
4257 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4260 Install_Declarations (Prot);
4264 Install_Formals (Body_Id);
4266 Push_Scope (Body_Id);
4269 -- For stubs and bodies with no previous spec, generate references to
4272 Generate_Reference_To_Formals (Body_Id);
4275 -- Entry barrier functions are generated outside the protected type and
4276 -- should not carry the SPARK_Mode of the enclosing context.
4278 if Nkind (N) = N_Subprogram_Body
4279 and then Is_Entry_Barrier_Function (N)
4283 -- The body is generated as part of expression function expansion. When
4284 -- the expression function appears in the visible declarations of a
4285 -- package, the body is added to the private declarations. Since both
4286 -- declarative lists may be subject to a different SPARK_Mode, inherit
4287 -- the mode of the spec.
4289 -- package P with SPARK_Mode is
4290 -- function Expr_Func ... is (...); -- original
4291 -- [function Expr_Func ...;] -- generated spec
4294 -- pragma SPARK_Mode (Off);
4295 -- [function Expr_Func ... is return ...;] -- generated body
4296 -- end P; -- mode is ON
4298 elsif not Comes_From_Source (N)
4299 and then Present (Spec_Id)
4300 and then Is_Expression_Function (Spec_Id)
4302 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4303 Set_SPARK_Pragma_Inherited
4304 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4306 -- Set the SPARK_Mode from the current context (may be overwritten later
4307 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4308 -- initially on a stand-alone subprogram body, but is then relocated to
4309 -- a generated corresponding spec. In this scenario the mode is shared
4310 -- between the spec and body.
4312 elsif No (SPARK_Pragma (Body_Id)) then
4313 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4314 Set_SPARK_Pragma_Inherited (Body_Id);
4317 -- A subprogram body may be instantiated or inlined at a later pass.
4318 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4319 -- applied to the initial declaration of the body.
4321 if Present (Spec_Id) then
4322 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4323 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4327 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4328 -- case the body is instantiated or inlined later and out of context.
4329 -- The body uses this attribute to restore the value of the global
4332 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4333 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4335 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4336 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4340 -- Preserve relevant elaboration-related attributes of the context which
4341 -- are no longer available or very expensive to recompute once analysis,
4342 -- resolution, and expansion are over.
4344 if No (Spec_Id) then
4345 Mark_Elaboration_Attributes
4351 -- If this is the proper body of a stub, we must verify that the stub
4352 -- conforms to the body, and to the previous spec if one was present.
4353 -- We know already that the body conforms to that spec. This test is
4354 -- only required for subprograms that come from source.
4356 if Nkind (Parent (N)) = N_Subunit
4357 and then Comes_From_Source (N)
4358 and then not Error_Posted (Body_Id)
4359 and then Nkind (Corresponding_Stub (Parent (N))) =
4360 N_Subprogram_Body_Stub
4363 Old_Id : constant Entity_Id :=
4365 (Specification (Corresponding_Stub (Parent (N))));
4367 Conformant : Boolean := False;
4370 if No (Spec_Id) then
4371 Check_Fully_Conformant (Body_Id, Old_Id);
4375 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4377 if not Conformant then
4379 -- The stub was taken to be a new declaration. Indicate that
4382 Set_Has_Completion (Old_Id, False);
4388 Set_Has_Completion (Body_Id);
4389 Check_Eliminated (Body_Id);
4391 -- Analyze any aspect specifications that appear on the subprogram body
4392 -- stub. Stop the analysis now as the stub does not have a declarative
4393 -- or a statement part, and it cannot be inlined.
4395 if Nkind (N) = N_Subprogram_Body_Stub then
4396 if Has_Aspects (N) then
4397 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4405 -- Note: Normally we don't do any inlining if expansion is off, since
4406 -- we won't generate code in any case. An exception arises in GNATprove
4407 -- mode where we want to expand some calls in place, even with expansion
4408 -- disabled, since the inlining eases formal verification.
4410 if not GNATprove_Mode
4411 and then Expander_Active
4412 and then Serious_Errors_Detected = 0
4413 and then Present (Spec_Id)
4414 and then Has_Pragma_Inline (Spec_Id)
4416 -- Legacy implementation (relying on front-end inlining)
4418 if not Back_End_Inlining then
4419 if Has_Pragma_Inline_Always (Spec_Id)
4420 or else (Front_End_Inlining
4421 and then not Opt.Disable_FE_Inline)
4423 Build_Body_To_Inline (N, Spec_Id);
4426 -- New implementation (relying on back-end inlining)
4429 if Has_Pragma_Inline_Always (Spec_Id)
4430 or else Optimization_Level > 0
4432 -- Handle function returning an unconstrained type
4434 if Comes_From_Source (Body_Id)
4435 and then Ekind (Spec_Id) = E_Function
4436 and then Returns_Unconstrained_Type (Spec_Id)
4438 -- If function builds in place, i.e. returns a limited type,
4439 -- inlining cannot be done.
4441 and then not Is_Limited_Type (Etype (Spec_Id))
4443 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4447 Subp_Body : constant Node_Id :=
4448 Unit_Declaration_Node (Body_Id);
4449 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4452 -- Do not pass inlining to the backend if the subprogram
4453 -- has declarations or statements which cannot be inlined
4454 -- by the backend. This check is done here to emit an
4455 -- error instead of the generic warning message reported
4456 -- by the GCC backend (ie. "function might not be
4459 if Present (Subp_Decl)
4460 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4464 elsif Has_Excluded_Statement
4467 (Handled_Statement_Sequence (Subp_Body)))
4471 -- If the backend inlining is available then at this
4472 -- stage we only have to mark the subprogram as inlined.
4473 -- The expander will take care of registering it in the
4474 -- table of subprograms inlined by the backend a part of
4475 -- processing calls to it (cf. Expand_Call)
4478 Set_Is_Inlined (Spec_Id);
4485 -- In GNATprove mode, inline only when there is a separate subprogram
4486 -- declaration for now, as inlining of subprogram bodies acting as
4487 -- declarations, or subprogram stubs, are not supported by front-end
4488 -- inlining. This inlining should occur after analysis of the body, so
4489 -- that it is known whether the value of SPARK_Mode, which can be
4490 -- defined by a pragma inside the body, is applicable to the body.
4491 -- Inlining can be disabled with switch -gnatdm
4493 elsif GNATprove_Mode
4494 and then Full_Analysis
4495 and then not Inside_A_Generic
4496 and then Present (Spec_Id)
4498 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4499 and then Body_Has_SPARK_Mode_On
4500 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4501 and then not Body_Has_Contract
4502 and then not Debug_Flag_M
4504 Build_Body_To_Inline (N, Spec_Id);
4507 -- When generating code, inherited pre/postconditions are handled when
4508 -- expanding the corresponding contract.
4510 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4511 -- of the specification we have to install the private withed units.
4512 -- This holds for child units as well.
4514 if Is_Compilation_Unit (Body_Id)
4515 or else Nkind (Parent (N)) = N_Compilation_Unit
4517 Install_Private_With_Clauses (Body_Id);
4520 Check_Anonymous_Return;
4522 -- Set the Protected_Formal field of each extra formal of the protected
4523 -- subprogram to reference the corresponding extra formal of the
4524 -- subprogram that implements it. For regular formals this occurs when
4525 -- the protected subprogram's declaration is expanded, but the extra
4526 -- formals don't get created until the subprogram is frozen. We need to
4527 -- do this before analyzing the protected subprogram's body so that any
4528 -- references to the original subprogram's extra formals will be changed
4529 -- refer to the implementing subprogram's formals (see Expand_Formal).
4531 if Present (Spec_Id)
4532 and then Is_Protected_Type (Scope (Spec_Id))
4533 and then Present (Protected_Body_Subprogram (Spec_Id))
4536 Impl_Subp : constant Entity_Id :=
4537 Protected_Body_Subprogram (Spec_Id);
4538 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4539 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4542 while Present (Prot_Ext_Formal) loop
4543 pragma Assert (Present (Impl_Ext_Formal));
4544 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4545 Next_Formal_With_Extras (Prot_Ext_Formal);
4546 Next_Formal_With_Extras (Impl_Ext_Formal);
4551 -- Generate minimum accessibility local objects to correspond with
4552 -- any extra formal added for anonymous access types. This new local
4553 -- object can then be used instead of the formal in case it is used
4554 -- in an actual to a call to a nested subprogram.
4556 -- This method is used to supplement our "small integer model" for
4557 -- accessibility-check generation (for more information see
4558 -- Dynamic_Accessibility_Level).
4560 -- Because we allow accessibility values greater than our expected value
4561 -- passing along the same extra accessibility formal as an actual
4562 -- to a nested subprogram becomes a problem because high values mean
4563 -- different things to the callee even though they are the same to the
4564 -- caller. So, as described in the first section, we create a local
4565 -- object representing the minimum of the accessibility level value that
4566 -- is passed in and the accessibility level of the callee's parameter
4567 -- and locals and use it in the case of a call to a nested subprogram.
4568 -- This generated object is refered to as a "minimum accessiblity
4571 if Present (Spec_Id) or else Present (Body_Id) then
4572 Body_Nod := Unit_Declaration_Node (Body_Id);
4577 -- Grab the appropriate formal depending on whether there exists
4578 -- an actual spec for the subprogram or whether we are dealing
4579 -- with a protected subprogram.
4581 if Present (Spec_Id) then
4582 if Present (Protected_Body_Subprogram (Spec_Id)) then
4583 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4585 Form := First_Formal (Spec_Id);
4588 Form := First_Formal (Body_Id);
4591 -- Loop through formals if the subprogram is capable of accepting
4592 -- a generated local object. If it is not then it is also not
4593 -- capable of having local subprograms meaning it would not need
4594 -- a minimum accessibility level object anyway.
4596 if Present (Body_Nod)
4597 and then Has_Declarations (Body_Nod)
4598 and then Nkind (Body_Nod) /= N_Package_Specification
4600 while Present (Form) loop
4602 if Present (Extra_Accessibility (Form))
4603 and then No (Minimum_Accessibility (Form))
4605 -- Generate the minimum accessibility level object
4607 -- A60b : integer := integer'min(2, paramL);
4610 Loc : constant Source_Ptr := Sloc (Body_Nod);
4611 Obj_Node : constant Node_Id :=
4612 Make_Object_Declaration (Loc,
4613 Defining_Identifier =>
4615 (Loc, 'A', Extra_Accessibility (Form)),
4616 Object_Definition => New_Occurrence_Of
4617 (Standard_Integer, Loc),
4619 Make_Attribute_Reference (Loc,
4620 Prefix => New_Occurrence_Of
4621 (Standard_Integer, Loc),
4622 Attribute_Name => Name_Min,
4623 Expressions => New_List (
4624 Make_Integer_Literal (Loc,
4625 Object_Access_Level (Form)),
4627 (Extra_Accessibility (Form), Loc))));
4629 -- Add the new local object to the Minimum_Acc_Obj to
4630 -- be later prepended to the subprogram's list of
4631 -- declarations after we are sure all expansion is
4634 if Present (Minimum_Acc_Objs) then
4635 Prepend (Obj_Node, Minimum_Acc_Objs);
4637 Minimum_Acc_Objs := New_List (Obj_Node);
4640 -- Register the object and analyze it
4642 Set_Minimum_Accessibility
4643 (Form, Defining_Identifier (Obj_Node));
4655 -- Now we can go on to analyze the body
4657 HSS := Handled_Statement_Sequence (N);
4658 Set_Actual_Subtypes (N, Current_Scope);
4660 -- Add a declaration for the Protection object, renaming declarations
4661 -- for discriminals and privals and finally a declaration for the entry
4662 -- family index (if applicable). This form of early expansion is done
4663 -- when the Expander is active because Install_Private_Data_Declarations
4664 -- references entities which were created during regular expansion. The
4665 -- subprogram entity must come from source, and not be an internally
4666 -- generated subprogram.
4669 and then Present (Prot_Typ)
4670 and then Present (Spec_Id)
4671 and then Comes_From_Source (Spec_Id)
4672 and then not Is_Eliminated (Spec_Id)
4674 Install_Private_Data_Declarations
4675 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4678 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4679 -- may now appear in parameter and result profiles. Since the analysis
4680 -- of a subprogram body may use the parameter and result profile of the
4681 -- spec, swap any limited views with their non-limited counterpart.
4683 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4684 Exch_Views := Exchange_Limited_Views (Spec_Id);
4687 -- If the return type is an anonymous access type whose designated type
4688 -- is the limited view of a class-wide type and the non-limited view is
4689 -- available, update the return type accordingly.
4691 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4697 Rtyp := Etype (Spec_Id);
4699 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4700 Etyp := Directly_Designated_Type (Rtyp);
4702 if Is_Class_Wide_Type (Etyp)
4703 and then From_Limited_With (Etyp)
4706 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4712 -- Analyze any aspect specifications that appear on the subprogram body
4714 if Has_Aspects (N) then
4715 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4718 Analyze_Declarations (Declarations (N));
4720 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4722 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4723 if Present (SPARK_Pragma (Spec_Id)) then
4724 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4726 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4728 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4729 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4730 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4732 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4735 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4738 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4739 -- as specified in source code, or because SPARK_Mode On is ignored
4740 -- in an instance where the context is SPARK_Mode Off/Auto.
4742 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4743 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4748 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4749 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4750 Error_Msg_Sloc := Sloc (Spec_Id);
4752 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4756 -- A subprogram body freezes its own contract. Analyze the contract
4757 -- after the declarations of the body have been processed as pragmas
4758 -- are now chained on the contract of the subprogram body.
4760 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4762 -- Check completion, and analyze the statements
4765 Inspect_Deferred_Constant_Completion (Declarations (N));
4768 -- Add the generated minimum accessibility objects to the subprogram
4769 -- body's list of declarations after analysis of the statements and
4772 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4773 if Present (Declarations (Body_Nod)) then
4774 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4777 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4781 -- Deal with end of scope processing for the body
4783 Process_End_Label (HSS, 't', Current_Scope);
4784 Update_Use_Clause_Chain;
4787 -- If we are compiling an entry wrapper, remove the enclosing
4788 -- synchronized object from the stack.
4790 if Is_Entry_Wrapper (Body_Id) then
4794 Check_Subprogram_Order (N);
4795 Set_Analyzed (Body_Id);
4797 -- If we have a separate spec, then the analysis of the declarations
4798 -- caused the entities in the body to be chained to the spec id, but
4799 -- we want them chained to the body id. Only the formal parameters
4800 -- end up chained to the spec id in this case.
4802 if Present (Spec_Id) then
4804 -- We must conform to the categorization of our spec
4806 Validate_Categorization_Dependency (N, Spec_Id);
4808 -- And if this is a child unit, the parent units must conform
4810 if Is_Child_Unit (Spec_Id) then
4811 Validate_Categorization_Dependency
4812 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4815 -- Here is where we move entities from the spec to the body
4817 -- Case where there are entities that stay with the spec
4819 if Present (Last_Real_Spec_Entity) then
4821 -- No body entities (happens when the only real spec entities come
4822 -- from precondition and postcondition pragmas).
4824 if No (Last_Entity (Body_Id)) then
4825 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4827 -- Body entities present (formals), so chain stuff past them
4831 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4834 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4835 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4836 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4838 -- Case where there are no spec entities, in this case there can be
4839 -- no body entities either, so just move everything.
4841 -- If the body is generated for an expression function, it may have
4842 -- been preanalyzed already, if 'access was applied to it.
4845 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4846 N_Expression_Function
4848 pragma Assert (No (Last_Entity (Body_Id)));
4852 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4853 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4854 Set_First_Entity (Spec_Id, Empty);
4855 Set_Last_Entity (Spec_Id, Empty);
4858 -- Otherwise the body does not complete a previous declaration. Check
4859 -- the categorization of the body against the units it withs.
4862 Validate_Categorization_Dependency (N, Body_Id);
4865 Check_Missing_Return;
4867 -- Now we are going to check for variables that are never modified in
4868 -- the body of the procedure. But first we deal with a special case
4869 -- where we want to modify this check. If the body of the subprogram
4870 -- starts with a raise statement or its equivalent, or if the body
4871 -- consists entirely of a null statement, then it is pretty obvious that
4872 -- it is OK to not reference the parameters. For example, this might be
4873 -- the following common idiom for a stubbed function: statement of the
4874 -- procedure raises an exception. In particular this deals with the
4875 -- common idiom of a stubbed function, which appears something like:
4877 -- function F (A : Integer) return Some_Type;
4880 -- raise Program_Error;
4884 -- Here the purpose of X is simply to satisfy the annoying requirement
4885 -- in Ada that there be at least one return, and we certainly do not
4886 -- want to go posting warnings on X that it is not initialized. On
4887 -- the other hand, if X is entirely unreferenced that should still
4890 -- What we do is to detect these cases, and if we find them, flag the
4891 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4892 -- suppress unwanted warnings. For the case of the function stub above
4893 -- we have a special test to set X as apparently assigned to suppress
4900 -- Skip call markers installed by the ABE mechanism, labels, and
4901 -- Push_xxx_Error_Label to find the first real statement.
4903 Stm := First (Statements (HSS));
4904 while Nkind_In (Stm, N_Call_Marker, N_Label)
4905 or else Nkind (Stm) in N_Push_xxx_Label
4910 -- Do the test on the original statement before expansion
4913 Ostm : constant Node_Id := Original_Node (Stm);
4916 -- If explicit raise statement, turn on flag
4918 if Nkind (Ostm) = N_Raise_Statement then
4919 Set_Trivial_Subprogram (Stm);
4921 -- If null statement, and no following statements, turn on flag
4923 elsif Nkind (Stm) = N_Null_Statement
4924 and then Comes_From_Source (Stm)
4925 and then No (Next (Stm))
4927 Set_Trivial_Subprogram (Stm);
4929 -- Check for explicit call cases which likely raise an exception
4931 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4932 if Is_Entity_Name (Name (Ostm)) then
4934 Ent : constant Entity_Id := Entity (Name (Ostm));
4937 -- If the procedure is marked No_Return, then likely it
4938 -- raises an exception, but in any case it is not coming
4939 -- back here, so turn on the flag.
4942 and then Ekind (Ent) = E_Procedure
4943 and then No_Return (Ent)
4945 Set_Trivial_Subprogram (Stm);
4953 -- Check for variables that are never modified
4960 -- If there is a separate spec, then transfer Never_Set_In_Source
4961 -- flags from out parameters to the corresponding entities in the
4962 -- body. The reason we do that is we want to post error flags on
4963 -- the body entities, not the spec entities.
4965 if Present (Spec_Id) then
4966 E1 := First_Entity (Spec_Id);
4967 while Present (E1) loop
4968 if Ekind (E1) = E_Out_Parameter then
4969 E2 := First_Entity (Body_Id);
4970 while Present (E2) loop
4971 exit when Chars (E1) = Chars (E2);
4975 if Present (E2) then
4976 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4984 -- Check references of the subprogram spec when we are dealing with
4985 -- an expression function due to it having a generated body.
4986 -- Otherwise, we simply check the formals of the subprogram body.
4988 if Present (Spec_Id)
4989 and then Is_Expression_Function (Spec_Id)
4991 Check_References (Spec_Id);
4993 Check_References (Body_Id);
4997 -- Check for nested subprogram, and mark outer level subprogram if so
5003 if Present (Spec_Id) then
5010 Ent := Enclosing_Subprogram (Ent);
5011 exit when No (Ent) or else Is_Subprogram (Ent);
5014 if Present (Ent) then
5015 Set_Has_Nested_Subprogram (Ent);
5019 -- Restore the limited views in the spec, if any, to let the back end
5020 -- process it without running into circularities.
5022 if Exch_Views /= No_Elist then
5023 Restore_Limited_Views (Exch_Views);
5026 if Mask_Types /= No_Elist then
5027 Unmask_Unfrozen_Types (Mask_Types);
5030 if Present (Desig_View) then
5031 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
5035 if Present (Ignored_Ghost_Region) then
5036 Expander_Active := Saved_EA;
5039 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5040 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5041 end Analyze_Subprogram_Body_Helper;
5043 ------------------------------------
5044 -- Analyze_Subprogram_Declaration --
5045 ------------------------------------
5047 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
5048 Scop : constant Entity_Id := Current_Scope;
5049 Designator : Entity_Id;
5051 Is_Completion : Boolean;
5052 -- Indicates whether a null procedure declaration is a completion
5055 -- Null procedures are not allowed in SPARK
5057 if Nkind (Specification (N)) = N_Procedure_Specification
5058 and then Null_Present (Specification (N))
5060 -- Null procedures are allowed in protected types, following the
5061 -- recent AI12-0147.
5063 if Is_Protected_Type (Current_Scope)
5064 and then Ada_Version < Ada_2012
5066 Error_Msg_N ("protected operation cannot be a null procedure", N);
5069 Analyze_Null_Procedure (N, Is_Completion);
5071 -- The null procedure acts as a body, nothing further is needed
5073 if Is_Completion then
5078 Designator := Analyze_Subprogram_Specification (Specification (N));
5080 -- A reference may already have been generated for the unit name, in
5081 -- which case the following call is redundant. However it is needed for
5082 -- declarations that are the rewriting of an expression function.
5084 Generate_Definition (Designator);
5086 -- Set the SPARK mode from the current context (may be overwritten later
5087 -- with explicit pragma). This is not done for entry barrier functions
5088 -- because they are generated outside the protected type and should not
5089 -- carry the mode of the enclosing context.
5091 if Nkind (N) = N_Subprogram_Declaration
5092 and then Is_Entry_Barrier_Function (N)
5097 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5098 Set_SPARK_Pragma_Inherited (Designator);
5101 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5102 -- the body of this subprogram is instantiated or inlined later and out
5103 -- of context. The body uses this attribute to restore the value of the
5106 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5107 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5110 -- Preserve relevant elaboration-related attributes of the context which
5111 -- are no longer available or very expensive to recompute once analysis,
5112 -- resolution, and expansion are over.
5114 Mark_Elaboration_Attributes
5115 (N_Id => Designator,
5119 if Debug_Flag_C then
5120 Write_Str ("==> subprogram spec ");
5121 Write_Name (Chars (Designator));
5122 Write_Str (" from ");
5123 Write_Location (Sloc (N));
5128 Validate_RCI_Subprogram_Declaration (N);
5129 New_Overloaded_Entity (Designator);
5130 Check_Delayed_Subprogram (Designator);
5132 -- If the type of the first formal of the current subprogram is a non-
5133 -- generic tagged private type, mark the subprogram as being a private
5134 -- primitive. Ditto if this is a function with controlling result, and
5135 -- the return type is currently private. In both cases, the type of the
5136 -- controlling argument or result must be in the current scope for the
5137 -- operation to be primitive.
5139 if Has_Controlling_Result (Designator)
5140 and then Is_Private_Type (Etype (Designator))
5141 and then Scope (Etype (Designator)) = Current_Scope
5142 and then not Is_Generic_Actual_Type (Etype (Designator))
5144 Set_Is_Private_Primitive (Designator);
5146 elsif Present (First_Formal (Designator)) then
5148 Formal_Typ : constant Entity_Id :=
5149 Etype (First_Formal (Designator));
5151 Set_Is_Private_Primitive (Designator,
5152 Is_Tagged_Type (Formal_Typ)
5153 and then Scope (Formal_Typ) = Current_Scope
5154 and then Is_Private_Type (Formal_Typ)
5155 and then not Is_Generic_Actual_Type (Formal_Typ));
5159 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5162 if Ada_Version >= Ada_2005
5163 and then Comes_From_Source (N)
5164 and then Is_Dispatching_Operation (Designator)
5171 if Has_Controlling_Result (Designator) then
5172 Etyp := Etype (Designator);
5175 E := First_Entity (Designator);
5177 and then Is_Formal (E)
5178 and then not Is_Controlling_Formal (E)
5186 if Is_Access_Type (Etyp) then
5187 Etyp := Directly_Designated_Type (Etyp);
5190 if Is_Interface (Etyp)
5191 and then not Is_Abstract_Subprogram (Designator)
5192 and then not (Ekind (Designator) = E_Procedure
5193 and then Null_Present (Specification (N)))
5195 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5197 -- Specialize error message based on procedures vs. functions,
5198 -- since functions can't be null subprograms.
5200 if Ekind (Designator) = E_Procedure then
5202 ("interface procedure % must be abstract or null", N);
5205 ("interface function % must be abstract", N);
5211 -- What is the following code for, it used to be
5213 -- ??? Set_Suppress_Elaboration_Checks
5214 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
5216 -- The following seems equivalent, but a bit dubious
5218 if Elaboration_Checks_Suppressed (Designator) then
5219 Set_Kill_Elaboration_Checks (Designator);
5222 -- For a compilation unit, set body required. This flag will only be
5223 -- reset if a valid Import or Interface pragma is processed later on.
5225 if Nkind (Parent (N)) = N_Compilation_Unit then
5226 Set_Body_Required (Parent (N), True);
5228 if Ada_Version >= Ada_2005
5229 and then Nkind (Specification (N)) = N_Procedure_Specification
5230 and then Null_Present (Specification (N))
5233 ("null procedure cannot be declared at library level", N);
5237 Generate_Reference_To_Formals (Designator);
5238 Check_Eliminated (Designator);
5240 if Debug_Flag_C then
5242 Write_Str ("<== subprogram spec ");
5243 Write_Name (Chars (Designator));
5244 Write_Str (" from ");
5245 Write_Location (Sloc (N));
5249 -- Indicate that this is a protected operation, because it may be used
5250 -- in subsequent declarations within the protected type.
5252 if Is_Protected_Type (Current_Scope) then
5253 Set_Convention (Designator, Convention_Protected);
5256 List_Inherited_Pre_Post_Aspects (Designator);
5258 -- Process the aspects before establishing the proper categorization in
5259 -- case the subprogram is a compilation unit and one of its aspects is
5260 -- converted into a categorization pragma.
5262 if Has_Aspects (N) then
5263 Analyze_Aspect_Specifications (N, Designator);
5266 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5267 Set_Categorization_From_Scope (Designator, Scop);
5269 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5270 -- proper categorization of the unit based on its pragmas.
5273 Push_Scope (Designator);
5274 Set_Categorization_From_Pragmas (N);
5275 Validate_Categorization_Dependency (N, Designator);
5278 end Analyze_Subprogram_Declaration;
5280 --------------------------------------
5281 -- Analyze_Subprogram_Specification --
5282 --------------------------------------
5284 -- Reminder: N here really is a subprogram specification (not a subprogram
5285 -- declaration). This procedure is called to analyze the specification in
5286 -- both subprogram bodies and subprogram declarations (specs).
5288 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5289 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5290 -- Determine whether entity E denotes the spec or body of an invariant
5293 ------------------------------------
5294 -- Is_Invariant_Procedure_Or_Body --
5295 ------------------------------------
5297 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5298 Decl : constant Node_Id := Unit_Declaration_Node (E);
5302 if Nkind (Decl) = N_Subprogram_Body then
5303 Spec := Corresponding_Spec (Decl);
5310 and then Ekind (Spec) = E_Procedure
5311 and then (Is_Partial_Invariant_Procedure (Spec)
5312 or else Is_Invariant_Procedure (Spec));
5313 end Is_Invariant_Procedure_Or_Body;
5317 Designator : constant Entity_Id := Defining_Entity (N);
5318 Formals : constant List_Id := Parameter_Specifications (N);
5320 -- Start of processing for Analyze_Subprogram_Specification
5323 -- Proceed with analysis. Do not emit a cross-reference entry if the
5324 -- specification comes from an expression function, because it may be
5325 -- the completion of a previous declaration. If it is not, the cross-
5326 -- reference entry will be emitted for the new subprogram declaration.
5328 if Nkind (Parent (N)) /= N_Expression_Function then
5329 Generate_Definition (Designator);
5332 if Nkind (N) = N_Function_Specification then
5333 Set_Ekind (Designator, E_Function);
5334 Set_Mechanism (Designator, Default_Mechanism);
5336 Set_Ekind (Designator, E_Procedure);
5337 Set_Etype (Designator, Standard_Void_Type);
5340 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5341 -- those subprograms which could be inlined in GNATprove mode (because
5342 -- Body_To_Inline is non-Empty) but should not be inlined.
5344 if GNATprove_Mode then
5345 Set_Is_Inlined_Always (Designator);
5348 -- Introduce new scope for analysis of the formals and the return type
5350 Set_Scope (Designator, Current_Scope);
5352 if Present (Formals) then
5353 Push_Scope (Designator);
5354 Process_Formals (Formals, N);
5356 -- Check dimensions in N for formals with default expression
5358 Analyze_Dimension_Formals (N, Formals);
5360 -- Ada 2005 (AI-345): If this is an overriding operation of an
5361 -- inherited interface operation, and the controlling type is
5362 -- a synchronized type, replace the type with its corresponding
5363 -- record, to match the proper signature of an overriding operation.
5364 -- Same processing for an access parameter whose designated type is
5365 -- derived from a synchronized interface.
5367 -- This modification is not done for invariant procedures because
5368 -- the corresponding record may not necessarely be visible when the
5369 -- concurrent type acts as the full view of a private type.
5372 -- type Prot is private with Type_Invariant => ...;
5373 -- procedure ConcInvariant (Obj : Prot);
5375 -- protected type Prot is ...;
5376 -- type Concurrent_Record_Prot is record ...;
5377 -- procedure ConcInvariant (Obj : Prot) is
5379 -- end ConcInvariant;
5382 -- In the example above, both the spec and body of the invariant
5383 -- procedure must utilize the private type as the controlling type.
5385 if Ada_Version >= Ada_2005
5386 and then not Is_Invariant_Procedure_Or_Body (Designator)
5390 Formal_Typ : Entity_Id;
5391 Rec_Typ : Entity_Id;
5392 Desig_Typ : Entity_Id;
5395 Formal := First_Formal (Designator);
5396 while Present (Formal) loop
5397 Formal_Typ := Etype (Formal);
5399 if Is_Concurrent_Type (Formal_Typ)
5400 and then Present (Corresponding_Record_Type (Formal_Typ))
5402 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5404 if Present (Interfaces (Rec_Typ)) then
5405 Set_Etype (Formal, Rec_Typ);
5408 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5409 Desig_Typ := Designated_Type (Formal_Typ);
5411 if Is_Concurrent_Type (Desig_Typ)
5412 and then Present (Corresponding_Record_Type (Desig_Typ))
5414 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5416 if Present (Interfaces (Rec_Typ)) then
5417 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5422 Next_Formal (Formal);
5429 -- The subprogram scope is pushed and popped around the processing of
5430 -- the return type for consistency with call above to Process_Formals
5431 -- (which itself can call Analyze_Return_Type), and to ensure that any
5432 -- itype created for the return type will be associated with the proper
5435 elsif Nkind (N) = N_Function_Specification then
5436 Push_Scope (Designator);
5437 Analyze_Return_Type (N);
5443 if Nkind (N) = N_Function_Specification then
5445 -- Deal with operator symbol case
5447 if Nkind (Designator) = N_Defining_Operator_Symbol then
5448 Valid_Operator_Definition (Designator);
5451 May_Need_Actuals (Designator);
5453 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5454 -- the subprogram is abstract also. This does not apply to renaming
5455 -- declarations, where abstractness is inherited, and to subprogram
5456 -- bodies generated for stream operations, which become renamings as
5459 -- In case of primitives associated with abstract interface types
5460 -- the check is applied later (see Analyze_Subprogram_Declaration).
5462 if not Nkind_In (Original_Node (Parent (N)),
5463 N_Abstract_Subprogram_Declaration,
5464 N_Formal_Abstract_Subprogram_Declaration,
5465 N_Subprogram_Renaming_Declaration)
5467 if Is_Abstract_Type (Etype (Designator))
5468 and then not Is_Interface (Etype (Designator))
5471 ("function that returns abstract type must be abstract", N);
5473 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5474 -- access result whose designated type is abstract.
5476 elsif Ada_Version >= Ada_2012
5477 and then Nkind (Result_Definition (N)) = N_Access_Definition
5479 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5480 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5483 ("function whose access result designates abstract type "
5484 & "must be abstract", N);
5490 end Analyze_Subprogram_Specification;
5492 -----------------------
5493 -- Check_Conformance --
5494 -----------------------
5496 procedure Check_Conformance
5497 (New_Id : Entity_Id;
5499 Ctype : Conformance_Type;
5501 Conforms : out Boolean;
5502 Err_Loc : Node_Id := Empty;
5503 Get_Inst : Boolean := False;
5504 Skip_Controlling_Formals : Boolean := False)
5506 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5507 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5508 -- If Errmsg is True, then processing continues to post an error message
5509 -- for conformance error on given node. Two messages are output. The
5510 -- first message points to the previous declaration with a general "no
5511 -- conformance" message. The second is the detailed reason, supplied as
5512 -- Msg. The parameter N provide information for a possible & insertion
5513 -- in the message, and also provides the location for posting the
5514 -- message in the absence of a specified Err_Loc location.
5516 function Conventions_Match
5518 Id2 : Entity_Id) return Boolean;
5519 -- Determine whether the conventions of arbitrary entities Id1 and Id2
5522 -----------------------
5523 -- Conformance_Error --
5524 -----------------------
5526 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5533 if No (Err_Loc) then
5539 Error_Msg_Sloc := Sloc (Old_Id);
5542 when Type_Conformant =>
5543 Error_Msg_N -- CODEFIX
5544 ("not type conformant with declaration#!", Enode);
5546 when Mode_Conformant =>
5547 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5549 ("not mode conformant with operation inherited#!",
5553 ("not mode conformant with declaration#!", Enode);
5556 when Subtype_Conformant =>
5557 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5559 ("not subtype conformant with operation inherited#!",
5563 ("not subtype conformant with declaration#!", Enode);
5566 when Fully_Conformant =>
5567 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5568 Error_Msg_N -- CODEFIX
5569 ("not fully conformant with operation inherited#!",
5572 Error_Msg_N -- CODEFIX
5573 ("not fully conformant with declaration#!", Enode);
5577 Error_Msg_NE (Msg, Enode, N);
5579 end Conformance_Error;
5581 -----------------------
5582 -- Conventions_Match --
5583 -----------------------
5585 function Conventions_Match
5587 Id2 : Entity_Id) return Boolean
5590 -- Ignore the conventions of anonymous access-to-subprogram types
5591 -- and subprogram types because these are internally generated and
5592 -- the only way these may receive a convention is if they inherit
5593 -- the convention of a related subprogram.
5595 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5598 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5603 -- Otherwise compare the conventions directly
5606 return Convention (Id1) = Convention (Id2);
5608 end Conventions_Match;
5612 Old_Type : constant Entity_Id := Etype (Old_Id);
5613 New_Type : constant Entity_Id := Etype (New_Id);
5614 Old_Formal : Entity_Id;
5615 New_Formal : Entity_Id;
5616 Access_Types_Match : Boolean;
5617 Old_Formal_Base : Entity_Id;
5618 New_Formal_Base : Entity_Id;
5620 -- Start of processing for Check_Conformance
5625 -- We need a special case for operators, since they don't appear
5628 if Ctype = Type_Conformant then
5629 if Ekind (New_Id) = E_Operator
5630 and then Operator_Matches_Spec (New_Id, Old_Id)
5636 -- If both are functions/operators, check return types conform
5638 if Old_Type /= Standard_Void_Type
5640 New_Type /= Standard_Void_Type
5642 -- If we are checking interface conformance we omit controlling
5643 -- arguments and result, because we are only checking the conformance
5644 -- of the remaining parameters.
5646 if Has_Controlling_Result (Old_Id)
5647 and then Has_Controlling_Result (New_Id)
5648 and then Skip_Controlling_Formals
5652 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5653 if Ctype >= Subtype_Conformant
5654 and then not Predicates_Match (Old_Type, New_Type)
5657 ("\predicate of return type does not match!", New_Id);
5660 ("\return type does not match!", New_Id);
5666 -- Ada 2005 (AI-231): In case of anonymous access types check the
5667 -- null-exclusion and access-to-constant attributes match.
5669 if Ada_Version >= Ada_2005
5670 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5672 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5673 or else Is_Access_Constant (Etype (Old_Type)) /=
5674 Is_Access_Constant (Etype (New_Type)))
5676 Conformance_Error ("\return type does not match!", New_Id);
5680 -- If either is a function/operator and the other isn't, error
5682 elsif Old_Type /= Standard_Void_Type
5683 or else New_Type /= Standard_Void_Type
5685 Conformance_Error ("\functions can only match functions!", New_Id);
5689 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5690 -- If this is a renaming as body, refine error message to indicate that
5691 -- the conflict is with the original declaration. If the entity is not
5692 -- frozen, the conventions don't have to match, the one of the renamed
5693 -- entity is inherited.
5695 if Ctype >= Subtype_Conformant then
5696 if not Conventions_Match (Old_Id, New_Id) then
5697 if not Is_Frozen (New_Id) then
5700 elsif Present (Err_Loc)
5701 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5702 and then Present (Corresponding_Spec (Err_Loc))
5704 Error_Msg_Name_1 := Chars (New_Id);
5706 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5707 Conformance_Error ("\prior declaration for% has convention %!");
5710 Conformance_Error ("\calling conventions do not match!");
5715 elsif Is_Formal_Subprogram (Old_Id)
5716 or else Is_Formal_Subprogram (New_Id)
5717 or else (Is_Subprogram (New_Id)
5718 and then Present (Alias (New_Id))
5719 and then Is_Formal_Subprogram (Alias (New_Id)))
5722 ("\formal subprograms are not subtype conformant "
5723 & "(RM 6.3.1 (17/3))");
5727 -- Deal with parameters
5729 -- Note: we use the entity information, rather than going directly
5730 -- to the specification in the tree. This is not only simpler, but
5731 -- absolutely necessary for some cases of conformance tests between
5732 -- operators, where the declaration tree simply does not exist.
5734 Old_Formal := First_Formal (Old_Id);
5735 New_Formal := First_Formal (New_Id);
5736 while Present (Old_Formal) and then Present (New_Formal) loop
5737 if Is_Controlling_Formal (Old_Formal)
5738 and then Is_Controlling_Formal (New_Formal)
5739 and then Skip_Controlling_Formals
5741 -- The controlling formals will have different types when
5742 -- comparing an interface operation with its match, but both
5743 -- or neither must be access parameters.
5745 if Is_Access_Type (Etype (Old_Formal))
5747 Is_Access_Type (Etype (New_Formal))
5749 goto Skip_Controlling_Formal;
5752 ("\access parameter does not match!", New_Formal);
5756 -- Ada 2012: Mode conformance also requires that formal parameters
5757 -- be both aliased, or neither.
5759 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5760 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5762 ("\aliased parameter mismatch!", New_Formal);
5766 if Ctype = Fully_Conformant then
5768 -- Names must match. Error message is more accurate if we do
5769 -- this before checking that the types of the formals match.
5771 if Chars (Old_Formal) /= Chars (New_Formal) then
5772 Conformance_Error ("\name& does not match!", New_Formal);
5774 -- Set error posted flag on new formal as well to stop
5775 -- junk cascaded messages in some cases.
5777 Set_Error_Posted (New_Formal);
5781 -- Null exclusion must match
5783 if Null_Exclusion_Present (Parent (Old_Formal))
5785 Null_Exclusion_Present (Parent (New_Formal))
5787 -- Only give error if both come from source. This should be
5788 -- investigated some time, since it should not be needed ???
5790 if Comes_From_Source (Old_Formal)
5792 Comes_From_Source (New_Formal)
5795 ("\null exclusion for& does not match", New_Formal);
5797 -- Mark error posted on the new formal to avoid duplicated
5798 -- complaint about types not matching.
5800 Set_Error_Posted (New_Formal);
5805 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5806 -- case occurs whenever a subprogram is being renamed and one of its
5807 -- parameters imposes a null exclusion. For example:
5809 -- type T is null record;
5810 -- type Acc_T is access T;
5811 -- subtype Acc_T_Sub is Acc_T;
5813 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5814 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5817 Old_Formal_Base := Etype (Old_Formal);
5818 New_Formal_Base := Etype (New_Formal);
5821 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5822 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5825 Access_Types_Match := Ada_Version >= Ada_2005
5827 -- Ensure that this rule is only applied when New_Id is a
5828 -- renaming of Old_Id.
5830 and then Nkind (Parent (Parent (New_Id))) =
5831 N_Subprogram_Renaming_Declaration
5832 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5833 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5834 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5836 -- Now handle the allowed access-type case
5838 and then Is_Access_Type (Old_Formal_Base)
5839 and then Is_Access_Type (New_Formal_Base)
5841 -- The type kinds must match. The only exception occurs with
5842 -- multiple generics of the form:
5845 -- type F is private; type A is private;
5846 -- type F_Ptr is access F; type A_Ptr is access A;
5847 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5848 -- package F_Pack is ... package A_Pack is
5849 -- package F_Inst is
5850 -- new F_Pack (A, A_Ptr, A_P);
5852 -- When checking for conformance between the parameters of A_P
5853 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5854 -- because the compiler has transformed A_Ptr into a subtype of
5855 -- F_Ptr. We catch this case in the code below.
5857 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5859 (Is_Generic_Type (Old_Formal_Base)
5860 and then Is_Generic_Type (New_Formal_Base)
5861 and then Is_Internal (New_Formal_Base)
5862 and then Etype (Etype (New_Formal_Base)) =
5864 and then Directly_Designated_Type (Old_Formal_Base) =
5865 Directly_Designated_Type (New_Formal_Base)
5866 and then ((Is_Itype (Old_Formal_Base)
5867 and then (Can_Never_Be_Null (Old_Formal_Base)
5868 or else Is_Access_Constant
5871 (Is_Itype (New_Formal_Base)
5872 and then (Can_Never_Be_Null (New_Formal_Base)
5873 or else Is_Access_Constant
5874 (New_Formal_Base))));
5876 -- Types must always match. In the visible part of an instance,
5877 -- usual overloading rules for dispatching operations apply, and
5878 -- we check base types (not the actual subtypes).
5880 if In_Instance_Visible_Part
5881 and then Is_Dispatching_Operation (New_Id)
5883 if not Conforming_Types
5884 (T1 => Base_Type (Etype (Old_Formal)),
5885 T2 => Base_Type (Etype (New_Formal)),
5887 Get_Inst => Get_Inst)
5888 and then not Access_Types_Match
5890 Conformance_Error ("\type of & does not match!", New_Formal);
5894 elsif not Conforming_Types
5895 (T1 => Old_Formal_Base,
5896 T2 => New_Formal_Base,
5898 Get_Inst => Get_Inst)
5899 and then not Access_Types_Match
5901 -- Don't give error message if old type is Any_Type. This test
5902 -- avoids some cascaded errors, e.g. in case of a bad spec.
5904 if Errmsg and then Old_Formal_Base = Any_Type then
5907 if Ctype >= Subtype_Conformant
5909 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5912 ("\predicate of & does not match!", New_Formal);
5915 ("\type of & does not match!", New_Formal);
5917 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5919 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5927 -- For mode conformance, mode must match
5929 if Ctype >= Mode_Conformant then
5930 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5931 if not Ekind_In (New_Id, E_Function, E_Procedure)
5932 or else not Is_Primitive_Wrapper (New_Id)
5934 Conformance_Error ("\mode of & does not match!", New_Formal);
5938 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5940 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5942 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5945 ("\mode of & does not match!", New_Formal);
5952 -- Part of mode conformance for access types is having the same
5953 -- constant modifier.
5955 elsif Access_Types_Match
5956 and then Is_Access_Constant (Old_Formal_Base) /=
5957 Is_Access_Constant (New_Formal_Base)
5960 ("\constant modifier does not match!", New_Formal);
5965 if Ctype >= Subtype_Conformant then
5967 -- Ada 2005 (AI-231): In case of anonymous access types check
5968 -- the null-exclusion and access-to-constant attributes must
5969 -- match. For null exclusion, we test the types rather than the
5970 -- formals themselves, since the attribute is only set reliably
5971 -- on the formals in the Ada 95 case, and we exclude the case
5972 -- where Old_Formal is marked as controlling, to avoid errors
5973 -- when matching completing bodies with dispatching declarations
5974 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5976 if Ada_Version >= Ada_2005
5977 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5978 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5980 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5981 Can_Never_Be_Null (Etype (New_Formal))
5983 not Is_Controlling_Formal (Old_Formal))
5985 Is_Access_Constant (Etype (Old_Formal)) /=
5986 Is_Access_Constant (Etype (New_Formal)))
5988 -- Do not complain if error already posted on New_Formal. This
5989 -- avoids some redundant error messages.
5991 and then not Error_Posted (New_Formal)
5993 -- It is allowed to omit the null-exclusion in case of stream
5994 -- attribute subprograms. We recognize stream subprograms
5995 -- through their TSS-generated suffix.
5998 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6001 if TSS_Name /= TSS_Stream_Read
6002 and then TSS_Name /= TSS_Stream_Write
6003 and then TSS_Name /= TSS_Stream_Input
6004 and then TSS_Name /= TSS_Stream_Output
6006 -- Here we have a definite conformance error. It is worth
6007 -- special casing the error message for the case of a
6008 -- controlling formal (which excludes null).
6010 if Is_Controlling_Formal (New_Formal) then
6011 Error_Msg_Node_2 := Scope (New_Formal);
6013 ("\controlling formal & of & excludes null, "
6014 & "declaration must exclude null as well",
6017 -- Normal case (couldn't we give more detail here???)
6021 ("\type of & does not match!", New_Formal);
6030 -- Full conformance checks
6032 if Ctype = Fully_Conformant then
6034 -- We have checked already that names match
6036 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6038 -- Check default expressions for in parameters
6041 NewD : constant Boolean :=
6042 Present (Default_Value (New_Formal));
6043 OldD : constant Boolean :=
6044 Present (Default_Value (Old_Formal));
6046 if NewD or OldD then
6048 -- The old default value has been analyzed because the
6049 -- current full declaration will have frozen everything
6050 -- before. The new default value has not been analyzed,
6051 -- so analyze it now before we check for conformance.
6054 Push_Scope (New_Id);
6055 Preanalyze_Spec_Expression
6056 (Default_Value (New_Formal), Etype (New_Formal));
6060 if not (NewD and OldD)
6061 or else not Fully_Conformant_Expressions
6062 (Default_Value (Old_Formal),
6063 Default_Value (New_Formal))
6066 ("\default expression for & does not match!",
6075 -- A couple of special checks for Ada 83 mode. These checks are
6076 -- skipped if either entity is an operator in package Standard,
6077 -- or if either old or new instance is not from the source program.
6079 if Ada_Version = Ada_83
6080 and then Sloc (Old_Id) > Standard_Location
6081 and then Sloc (New_Id) > Standard_Location
6082 and then Comes_From_Source (Old_Id)
6083 and then Comes_From_Source (New_Id)
6086 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6087 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6090 -- Explicit IN must be present or absent in both cases. This
6091 -- test is required only in the full conformance case.
6093 if In_Present (Old_Param) /= In_Present (New_Param)
6094 and then Ctype = Fully_Conformant
6097 ("\(Ada 83) IN must appear in both declarations",
6102 -- Grouping (use of comma in param lists) must be the same
6103 -- This is where we catch a misconformance like:
6106 -- A : Integer; B : Integer
6108 -- which are represented identically in the tree except
6109 -- for the setting of the flags More_Ids and Prev_Ids.
6111 if More_Ids (Old_Param) /= More_Ids (New_Param)
6112 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6115 ("\grouping of & does not match!", New_Formal);
6121 -- This label is required when skipping controlling formals
6123 <<Skip_Controlling_Formal>>
6125 Next_Formal (Old_Formal);
6126 Next_Formal (New_Formal);
6129 if Present (Old_Formal) then
6130 Conformance_Error ("\too few parameters!");
6133 elsif Present (New_Formal) then
6134 Conformance_Error ("\too many parameters!", New_Formal);
6137 end Check_Conformance;
6139 -----------------------
6140 -- Check_Conventions --
6141 -----------------------
6143 procedure Check_Conventions (Typ : Entity_Id) is
6144 Ifaces_List : Elist_Id;
6146 procedure Check_Convention (Op : Entity_Id);
6147 -- Verify that the convention of inherited dispatching operation Op is
6148 -- consistent among all subprograms it overrides. In order to minimize
6149 -- the search, Search_From is utilized to designate a specific point in
6150 -- the list rather than iterating over the whole list once more.
6152 ----------------------
6153 -- Check_Convention --
6154 ----------------------
6156 procedure Check_Convention (Op : Entity_Id) is
6157 Op_Conv : constant Convention_Id := Convention (Op);
6158 Iface_Conv : Convention_Id;
6159 Iface_Elmt : Elmt_Id;
6160 Iface_Prim_Elmt : Elmt_Id;
6161 Iface_Prim : Entity_Id;
6164 Iface_Elmt := First_Elmt (Ifaces_List);
6165 while Present (Iface_Elmt) loop
6167 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6168 while Present (Iface_Prim_Elmt) loop
6169 Iface_Prim := Node (Iface_Prim_Elmt);
6170 Iface_Conv := Convention (Iface_Prim);
6172 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6173 and then Iface_Conv /= Op_Conv
6176 ("inconsistent conventions in primitive operations", Typ);
6178 Error_Msg_Name_1 := Chars (Op);
6179 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6180 Error_Msg_Sloc := Sloc (Op);
6182 if Comes_From_Source (Op) or else No (Alias (Op)) then
6183 if not Present (Overridden_Operation (Op)) then
6184 Error_Msg_N ("\\primitive % defined #", Typ);
6187 ("\\overriding operation % with "
6188 & "convention % defined #", Typ);
6191 else pragma Assert (Present (Alias (Op)));
6192 Error_Msg_Sloc := Sloc (Alias (Op));
6193 Error_Msg_N ("\\inherited operation % with "
6194 & "convention % defined #", Typ);
6197 Error_Msg_Name_1 := Chars (Op);
6198 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6199 Error_Msg_Sloc := Sloc (Iface_Prim);
6200 Error_Msg_N ("\\overridden operation % with "
6201 & "convention % defined #", Typ);
6203 -- Avoid cascading errors
6208 Next_Elmt (Iface_Prim_Elmt);
6211 Next_Elmt (Iface_Elmt);
6213 end Check_Convention;
6217 Prim_Op : Entity_Id;
6218 Prim_Op_Elmt : Elmt_Id;
6220 -- Start of processing for Check_Conventions
6223 if not Has_Interfaces (Typ) then
6227 Collect_Interfaces (Typ, Ifaces_List);
6229 -- The algorithm checks every overriding dispatching operation against
6230 -- all the corresponding overridden dispatching operations, detecting
6231 -- differences in conventions.
6233 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6234 while Present (Prim_Op_Elmt) loop
6235 Prim_Op := Node (Prim_Op_Elmt);
6237 -- A small optimization: skip the predefined dispatching operations
6238 -- since they always have the same convention.
6240 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6241 Check_Convention (Prim_Op);
6244 Next_Elmt (Prim_Op_Elmt);
6246 end Check_Conventions;
6248 ------------------------------
6249 -- Check_Delayed_Subprogram --
6250 ------------------------------
6252 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6253 procedure Possible_Freeze (T : Entity_Id);
6254 -- T is the type of either a formal parameter or of the return type. If
6255 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6256 -- itself must be delayed.
6258 ---------------------
6259 -- Possible_Freeze --
6260 ---------------------
6262 procedure Possible_Freeze (T : Entity_Id) is
6263 Scop : constant Entity_Id := Scope (Designator);
6266 -- If the subprogram appears within a package instance (which may be
6267 -- the wrapper package of a subprogram instance) the freeze node for
6268 -- that package will freeze the subprogram at the proper place, so
6269 -- do not emit a freeze node for the subprogram, given that it may
6270 -- appear in the wrong scope.
6272 if Ekind (Scop) = E_Package
6273 and then not Comes_From_Source (Scop)
6274 and then Is_Generic_Instance (Scop)
6278 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6279 Set_Has_Delayed_Freeze (Designator);
6281 elsif Is_Access_Type (T)
6282 and then Has_Delayed_Freeze (Designated_Type (T))
6283 and then not Is_Frozen (Designated_Type (T))
6285 Set_Has_Delayed_Freeze (Designator);
6287 end Possible_Freeze;
6293 -- Start of processing for Check_Delayed_Subprogram
6296 -- All subprograms, including abstract subprograms, may need a freeze
6297 -- node if some formal type or the return type needs one.
6299 Possible_Freeze (Etype (Designator));
6300 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6302 -- Need delayed freeze if any of the formal types themselves need a
6303 -- delayed freeze and are not yet frozen.
6305 F := First_Formal (Designator);
6306 while Present (F) loop
6307 Possible_Freeze (Etype (F));
6308 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6312 -- Mark functions that return by reference. Note that it cannot be done
6313 -- for delayed_freeze subprograms because the underlying returned type
6314 -- may not be known yet (for private types).
6316 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6318 Typ : constant Entity_Id := Etype (Designator);
6319 Utyp : constant Entity_Id := Underlying_Type (Typ);
6322 if Is_Limited_View (Typ) then
6323 Set_Returns_By_Ref (Designator);
6325 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6326 Set_Returns_By_Ref (Designator);
6330 end Check_Delayed_Subprogram;
6332 ------------------------------------
6333 -- Check_Discriminant_Conformance --
6334 ------------------------------------
6336 procedure Check_Discriminant_Conformance
6341 Old_Discr : Entity_Id := First_Discriminant (Prev);
6342 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6343 New_Discr_Id : Entity_Id;
6344 New_Discr_Type : Entity_Id;
6346 procedure Conformance_Error (Msg : String; N : Node_Id);
6347 -- Post error message for conformance error on given node. Two messages
6348 -- are output. The first points to the previous declaration with a
6349 -- general "no conformance" message. The second is the detailed reason,
6350 -- supplied as Msg. The parameter N provide information for a possible
6351 -- & insertion in the message.
6353 -----------------------
6354 -- Conformance_Error --
6355 -----------------------
6357 procedure Conformance_Error (Msg : String; N : Node_Id) is
6359 Error_Msg_Sloc := Sloc (Prev_Loc);
6360 Error_Msg_N -- CODEFIX
6361 ("not fully conformant with declaration#!", N);
6362 Error_Msg_NE (Msg, N, N);
6363 end Conformance_Error;
6365 -- Start of processing for Check_Discriminant_Conformance
6368 while Present (Old_Discr) and then Present (New_Discr) loop
6369 New_Discr_Id := Defining_Identifier (New_Discr);
6371 -- The subtype mark of the discriminant on the full type has not
6372 -- been analyzed so we do it here. For an access discriminant a new
6375 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6377 Access_Definition (N, Discriminant_Type (New_Discr));
6380 Find_Type (Discriminant_Type (New_Discr));
6381 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6383 -- Ada 2005: if the discriminant definition carries a null
6384 -- exclusion, create an itype to check properly for consistency
6385 -- with partial declaration.
6387 if Is_Access_Type (New_Discr_Type)
6388 and then Null_Exclusion_Present (New_Discr)
6391 Create_Null_Excluding_Itype
6392 (T => New_Discr_Type,
6393 Related_Nod => New_Discr,
6394 Scope_Id => Current_Scope);
6398 if not Conforming_Types
6399 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6401 Conformance_Error ("type of & does not match!", New_Discr_Id);
6404 -- Treat the new discriminant as an occurrence of the old one,
6405 -- for navigation purposes, and fill in some semantic
6406 -- information, for completeness.
6408 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6409 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6410 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6415 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6416 Conformance_Error ("name & does not match!", New_Discr_Id);
6420 -- Default expressions must match
6423 NewD : constant Boolean :=
6424 Present (Expression (New_Discr));
6425 OldD : constant Boolean :=
6426 Present (Expression (Parent (Old_Discr)));
6429 if NewD or OldD then
6431 -- The old default value has been analyzed and expanded,
6432 -- because the current full declaration will have frozen
6433 -- everything before. The new default values have not been
6434 -- expanded, so expand now to check conformance.
6437 Preanalyze_Spec_Expression
6438 (Expression (New_Discr), New_Discr_Type);
6441 if not (NewD and OldD)
6442 or else not Fully_Conformant_Expressions
6443 (Expression (Parent (Old_Discr)),
6444 Expression (New_Discr))
6448 ("default expression for & does not match!",
6455 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6457 if Ada_Version = Ada_83 then
6459 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6462 -- Grouping (use of comma in param lists) must be the same
6463 -- This is where we catch a misconformance like:
6466 -- A : Integer; B : Integer
6468 -- which are represented identically in the tree except
6469 -- for the setting of the flags More_Ids and Prev_Ids.
6471 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6472 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6475 ("grouping of & does not match!", New_Discr_Id);
6481 Next_Discriminant (Old_Discr);
6485 if Present (Old_Discr) then
6486 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6489 elsif Present (New_Discr) then
6491 ("too many discriminants!", Defining_Identifier (New_Discr));
6494 end Check_Discriminant_Conformance;
6496 ----------------------------
6497 -- Check_Fully_Conformant --
6498 ----------------------------
6500 procedure Check_Fully_Conformant
6501 (New_Id : Entity_Id;
6503 Err_Loc : Node_Id := Empty)
6506 pragma Warnings (Off, Result);
6509 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6510 end Check_Fully_Conformant;
6512 --------------------------
6513 -- Check_Limited_Return --
6514 --------------------------
6516 procedure Check_Limited_Return
6522 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6523 -- replaced by anonymous access results. This is an incompatibility with
6524 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6525 -- controllable with special switch. ???
6527 -- A limited interface that is not immutably limited is OK
6529 if Is_Limited_Interface (R_Type)
6531 not (Is_Task_Interface (R_Type)
6532 or else Is_Protected_Interface (R_Type)
6533 or else Is_Synchronized_Interface (R_Type))
6537 elsif Is_Limited_Type (R_Type)
6538 and then not Is_Interface (R_Type)
6539 and then Comes_From_Source (N)
6540 and then not In_Instance_Body
6541 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6543 -- Error in Ada 2005
6545 if Ada_Version >= Ada_2005
6546 and then not Debug_Flag_Dot_L
6547 and then not GNAT_Mode
6550 ("(Ada 2005) cannot copy object of a limited type "
6551 & "(RM-2005 6.5(5.5/2))", Expr);
6553 if Is_Limited_View (R_Type) then
6555 ("\return by reference not permitted in Ada 2005", Expr);
6558 -- Warn in Ada 95 mode, to give folks a heads up about this
6561 -- In GNAT mode, this is just a warning, to allow it to be evilly
6562 -- turned off. Otherwise it is a real error.
6564 -- In a generic context, simplify the warning because it makes no
6565 -- sense to discuss pass-by-reference or copy.
6567 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6568 if Inside_A_Generic then
6570 ("return of limited object not permitted in Ada 2005 "
6571 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6573 elsif Is_Limited_View (R_Type) then
6575 ("return by reference not permitted in Ada 2005 "
6576 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6579 ("cannot copy object of a limited type in Ada 2005 "
6580 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6583 -- Ada 95 mode, and compatibility warnings disabled
6586 pragma Assert (Ada_Version <= Ada_95);
6587 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6588 return; -- skip continuation messages below
6591 if not Inside_A_Generic then
6593 ("\consider switching to return of access type", Expr);
6594 Explain_Limited_Type (R_Type, Expr);
6597 end Check_Limited_Return;
6599 ---------------------------
6600 -- Check_Mode_Conformant --
6601 ---------------------------
6603 procedure Check_Mode_Conformant
6604 (New_Id : Entity_Id;
6606 Err_Loc : Node_Id := Empty;
6607 Get_Inst : Boolean := False)
6610 pragma Warnings (Off, Result);
6613 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6614 end Check_Mode_Conformant;
6616 --------------------------------
6617 -- Check_Overriding_Indicator --
6618 --------------------------------
6620 procedure Check_Overriding_Indicator
6622 Overridden_Subp : Entity_Id;
6623 Is_Primitive : Boolean)
6629 -- No overriding indicator for literals
6631 if Ekind (Subp) = E_Enumeration_Literal then
6634 elsif Ekind (Subp) = E_Entry then
6635 Decl := Parent (Subp);
6637 -- No point in analyzing a malformed operator
6639 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6640 and then Error_Posted (Subp)
6645 Decl := Unit_Declaration_Node (Subp);
6648 if Nkind_In (Decl, N_Subprogram_Body,
6649 N_Subprogram_Body_Stub,
6650 N_Subprogram_Declaration,
6651 N_Abstract_Subprogram_Declaration,
6652 N_Subprogram_Renaming_Declaration)
6654 Spec := Specification (Decl);
6656 elsif Nkind (Decl) = N_Entry_Declaration then
6663 -- An overriding indication is illegal on a subprogram declared
6664 -- in a protected body, where there is no operation to override.
6666 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6667 and then Is_List_Member (Decl)
6668 and then Present (Parent (List_Containing (Decl)))
6669 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6672 ("illegal overriding indication in protected body", Decl);
6676 -- The overriding operation is type conformant with the overridden one,
6677 -- but the names of the formals are not required to match. If the names
6678 -- appear permuted in the overriding operation, this is a possible
6679 -- source of confusion that is worth diagnosing. Controlling formals
6680 -- often carry names that reflect the type, and it is not worthwhile
6681 -- requiring that their names match.
6683 if Present (Overridden_Subp)
6684 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6691 Form1 := First_Formal (Subp);
6692 Form2 := First_Formal (Overridden_Subp);
6694 -- If the overriding operation is a synchronized operation, skip
6695 -- the first parameter of the overridden operation, which is
6696 -- implicit in the new one. If the operation is declared in the
6697 -- body it is not primitive and all formals must match.
6699 if Is_Concurrent_Type (Scope (Subp))
6700 and then Is_Tagged_Type (Scope (Subp))
6701 and then not Has_Completion (Scope (Subp))
6703 Form2 := Next_Formal (Form2);
6706 if Present (Form1) then
6707 Form1 := Next_Formal (Form1);
6708 Form2 := Next_Formal (Form2);
6711 while Present (Form1) loop
6712 if not Is_Controlling_Formal (Form1)
6713 and then Present (Next_Formal (Form2))
6714 and then Chars (Form1) = Chars (Next_Formal (Form2))
6716 Error_Msg_Node_2 := Alias (Overridden_Subp);
6717 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6719 ("& does not match corresponding formal of&#",
6724 Next_Formal (Form1);
6725 Next_Formal (Form2);
6730 -- If there is an overridden subprogram, then check that there is no
6731 -- "not overriding" indicator, and mark the subprogram as overriding.
6733 -- This is not done if the overridden subprogram is marked as hidden,
6734 -- which can occur for the case of inherited controlled operations
6735 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6736 -- subprogram is not itself hidden or we are within a generic instance,
6737 -- in which case the hidden flag may have been modified for the
6738 -- expansion of the instance.
6740 -- (Note: This condition could probably be simplified, leaving out the
6741 -- testing for the specific controlled cases, but it seems safer and
6742 -- clearer this way, and echoes similar special-case tests of this
6743 -- kind in other places.)
6745 if Present (Overridden_Subp)
6746 and then (not Is_Hidden (Overridden_Subp)
6748 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6751 and then Present (Alias (Overridden_Subp))
6752 and then (not Is_Hidden (Alias (Overridden_Subp))
6753 or else In_Instance)))
6755 if Must_Not_Override (Spec) then
6756 Error_Msg_Sloc := Sloc (Overridden_Subp);
6758 if Ekind (Subp) = E_Entry then
6760 ("entry & overrides inherited operation #", Spec, Subp);
6763 ("subprogram & overrides inherited operation #", Spec, Subp);
6766 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6767 -- as an extension of Root_Controlled, and thus has a useless Adjust
6768 -- operation. This operation should not be inherited by other limited
6769 -- controlled types. An explicit Adjust for them is not overriding.
6771 elsif Must_Override (Spec)
6772 and then Chars (Overridden_Subp) = Name_Adjust
6773 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6774 and then Present (Alias (Overridden_Subp))
6775 and then In_Predefined_Unit (Alias (Overridden_Subp))
6778 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6779 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6781 elsif Is_Subprogram (Subp) then
6782 if Is_Init_Proc (Subp) then
6785 elsif No (Overridden_Operation (Subp)) then
6787 -- For entities generated by Derive_Subprograms the overridden
6788 -- operation is the inherited primitive (which is available
6789 -- through the attribute alias)
6791 if (Is_Dispatching_Operation (Subp)
6792 or else Is_Dispatching_Operation (Overridden_Subp))
6793 and then not Comes_From_Source (Overridden_Subp)
6794 and then Find_Dispatching_Type (Overridden_Subp) =
6795 Find_Dispatching_Type (Subp)
6796 and then Present (Alias (Overridden_Subp))
6797 and then Comes_From_Source (Alias (Overridden_Subp))
6799 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6800 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6803 Set_Overridden_Operation (Subp, Overridden_Subp);
6804 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6809 -- If primitive flag is set or this is a protected operation, then
6810 -- the operation is overriding at the point of its declaration, so
6811 -- warn if necessary. Otherwise it may have been declared before the
6812 -- operation it overrides and no check is required.
6815 and then not Must_Override (Spec)
6816 and then (Is_Primitive
6817 or else Ekind (Scope (Subp)) = E_Protected_Type)
6819 Style.Missing_Overriding (Decl, Subp);
6822 -- If Subp is an operator, it may override a predefined operation, if
6823 -- it is defined in the same scope as the type to which it applies.
6824 -- In that case Overridden_Subp is empty because of our implicit
6825 -- representation for predefined operators. We have to check whether the
6826 -- signature of Subp matches that of a predefined operator. Note that
6827 -- first argument provides the name of the operator, and the second
6828 -- argument the signature that may match that of a standard operation.
6829 -- If the indicator is overriding, then the operator must match a
6830 -- predefined signature, because we know already that there is no
6831 -- explicit overridden operation.
6833 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6834 if Must_Not_Override (Spec) then
6836 -- If this is not a primitive or a protected subprogram, then
6837 -- "not overriding" is illegal.
6840 and then Ekind (Scope (Subp)) /= E_Protected_Type
6842 Error_Msg_N ("overriding indicator only allowed "
6843 & "if subprogram is primitive", Subp);
6845 elsif Can_Override_Operator (Subp) then
6847 ("subprogram& overrides predefined operator ", Spec, Subp);
6850 elsif Must_Override (Spec) then
6851 if No (Overridden_Operation (Subp))
6852 and then not Can_Override_Operator (Subp)
6854 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6857 elsif not Error_Posted (Subp)
6858 and then Style_Check
6859 and then Can_Override_Operator (Subp)
6860 and then not In_Predefined_Unit (Subp)
6862 -- If style checks are enabled, indicate that the indicator is
6863 -- missing. However, at the point of declaration, the type of
6864 -- which this is a primitive operation may be private, in which
6865 -- case the indicator would be premature.
6867 if Has_Private_Declaration (Etype (Subp))
6868 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6872 Style.Missing_Overriding (Decl, Subp);
6876 elsif Must_Override (Spec) then
6877 if Ekind (Subp) = E_Entry then
6878 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6880 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6883 -- If the operation is marked "not overriding" and it's not primitive
6884 -- then an error is issued, unless this is an operation of a task or
6885 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6886 -- has been specified have already been checked above.
6888 elsif Must_Not_Override (Spec)
6889 and then not Is_Primitive
6890 and then Ekind (Subp) /= E_Entry
6891 and then Ekind (Scope (Subp)) /= E_Protected_Type
6894 ("overriding indicator only allowed if subprogram is primitive",
6898 end Check_Overriding_Indicator;
6904 -- Note: this procedure needs to know far too much about how the expander
6905 -- messes with exceptions. The use of the flag Exception_Junk and the
6906 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6907 -- works, but is not very clean. It would be better if the expansion
6908 -- routines would leave Original_Node working nicely, and we could use
6909 -- Original_Node here to ignore all the peculiar expander messing ???
6911 procedure Check_Returns
6915 Proc : Entity_Id := Empty)
6919 procedure Check_Statement_Sequence (L : List_Id);
6920 -- Internal recursive procedure to check a list of statements for proper
6921 -- termination by a return statement (or a transfer of control or a
6922 -- compound statement that is itself internally properly terminated).
6924 ------------------------------
6925 -- Check_Statement_Sequence --
6926 ------------------------------
6928 procedure Check_Statement_Sequence (L : List_Id) is
6933 function Assert_False return Boolean;
6934 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6935 -- rewritten as a null statement when assertions are off. The assert
6936 -- is not active, but it is still enough to kill the warning.
6942 function Assert_False return Boolean is
6943 Orig : constant Node_Id := Original_Node (Last_Stm);
6946 if Nkind (Orig) = N_Pragma
6947 and then Pragma_Name (Orig) = Name_Assert
6948 and then not Error_Posted (Orig)
6951 Arg : constant Node_Id :=
6952 First (Pragma_Argument_Associations (Orig));
6953 Exp : constant Node_Id := Expression (Arg);
6955 return Nkind (Exp) = N_Identifier
6956 and then Chars (Exp) = Name_False;
6966 Raise_Exception_Call : Boolean;
6967 -- Set True if statement sequence terminated by Raise_Exception call
6968 -- or a Reraise_Occurrence call.
6970 -- Start of processing for Check_Statement_Sequence
6973 Raise_Exception_Call := False;
6975 -- Get last real statement
6977 Last_Stm := Last (L);
6979 -- Deal with digging out exception handler statement sequences that
6980 -- have been transformed by the local raise to goto optimization.
6981 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6982 -- optimization has occurred, we are looking at something like:
6985 -- original stmts in block
6989 -- goto L1; | omitted if No_Exception_Propagation
6994 -- goto L3; -- skip handler when exception not raised
6996 -- <<L1>> -- target label for local exception
7010 -- and what we have to do is to dig out the estmts1 and estmts2
7011 -- sequences (which were the original sequences of statements in
7012 -- the exception handlers) and check them.
7014 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7019 exit when Nkind (Stm) /= N_Block_Statement;
7020 exit when not Exception_Junk (Stm);
7023 exit when Nkind (Stm) /= N_Label;
7024 exit when not Exception_Junk (Stm);
7025 Check_Statement_Sequence
7026 (Statements (Handled_Statement_Sequence (Next (Stm))));
7031 exit when Nkind (Stm) /= N_Goto_Statement;
7032 exit when not Exception_Junk (Stm);
7036 -- Don't count pragmas
7038 while Nkind (Last_Stm) = N_Pragma
7040 -- Don't count call to SS_Release (can happen after Raise_Exception)
7043 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7045 Nkind (Name (Last_Stm)) = N_Identifier
7047 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7049 -- Don't count exception junk
7052 (Nkind_In (Last_Stm, N_Goto_Statement,
7054 N_Object_Declaration)
7055 and then Exception_Junk (Last_Stm))
7056 or else Nkind (Last_Stm) in N_Push_xxx_Label
7057 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7059 -- Inserted code, such as finalization calls, is irrelevant: we only
7060 -- need to check original source.
7062 or else Is_Rewrite_Insertion (Last_Stm)
7067 -- Here we have the "real" last statement
7069 Kind := Nkind (Last_Stm);
7071 -- Transfer of control, OK. Note that in the No_Return procedure
7072 -- case, we already diagnosed any explicit return statements, so
7073 -- we can treat them as OK in this context.
7075 if Is_Transfer (Last_Stm) then
7078 -- Check cases of explicit non-indirect procedure calls
7080 elsif Kind = N_Procedure_Call_Statement
7081 and then Is_Entity_Name (Name (Last_Stm))
7083 -- Check call to Raise_Exception procedure which is treated
7084 -- specially, as is a call to Reraise_Occurrence.
7086 -- We suppress the warning in these cases since it is likely that
7087 -- the programmer really does not expect to deal with the case
7088 -- of Null_Occurrence, and thus would find a warning about a
7089 -- missing return curious, and raising Program_Error does not
7090 -- seem such a bad behavior if this does occur.
7092 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7093 -- behavior will be to raise Constraint_Error (see AI-329).
7095 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7097 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7099 Raise_Exception_Call := True;
7101 -- For Raise_Exception call, test first argument, if it is
7102 -- an attribute reference for a 'Identity call, then we know
7103 -- that the call cannot possibly return.
7106 Arg : constant Node_Id :=
7107 Original_Node (First_Actual (Last_Stm));
7109 if Nkind (Arg) = N_Attribute_Reference
7110 and then Attribute_Name (Arg) = Name_Identity
7117 -- If statement, need to look inside if there is an else and check
7118 -- each constituent statement sequence for proper termination.
7120 elsif Kind = N_If_Statement
7121 and then Present (Else_Statements (Last_Stm))
7123 Check_Statement_Sequence (Then_Statements (Last_Stm));
7124 Check_Statement_Sequence (Else_Statements (Last_Stm));
7126 if Present (Elsif_Parts (Last_Stm)) then
7128 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7131 while Present (Elsif_Part) loop
7132 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7140 -- Case statement, check each case for proper termination
7142 elsif Kind = N_Case_Statement then
7146 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7147 while Present (Case_Alt) loop
7148 Check_Statement_Sequence (Statements (Case_Alt));
7149 Next_Non_Pragma (Case_Alt);
7155 -- Block statement, check its handled sequence of statements
7157 elsif Kind = N_Block_Statement then
7163 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7172 -- Loop statement. If there is an iteration scheme, we can definitely
7173 -- fall out of the loop. Similarly if there is an exit statement, we
7174 -- can fall out. In either case we need a following return.
7176 elsif Kind = N_Loop_Statement then
7177 if Present (Iteration_Scheme (Last_Stm))
7178 or else Has_Exit (Entity (Identifier (Last_Stm)))
7182 -- A loop with no exit statement or iteration scheme is either
7183 -- an infinite loop, or it has some other exit (raise/return).
7184 -- In either case, no warning is required.
7190 -- Timed entry call, check entry call and delay alternatives
7192 -- Note: in expanded code, the timed entry call has been converted
7193 -- to a set of expanded statements on which the check will work
7194 -- correctly in any case.
7196 elsif Kind = N_Timed_Entry_Call then
7198 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7199 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7202 -- If statement sequence of entry call alternative is missing,
7203 -- then we can definitely fall through, and we post the error
7204 -- message on the entry call alternative itself.
7206 if No (Statements (ECA)) then
7209 -- If statement sequence of delay alternative is missing, then
7210 -- we can definitely fall through, and we post the error
7211 -- message on the delay alternative itself.
7213 -- Note: if both ECA and DCA are missing the return, then we
7214 -- post only one message, should be enough to fix the bugs.
7215 -- If not we will get a message next time on the DCA when the
7218 elsif No (Statements (DCA)) then
7221 -- Else check both statement sequences
7224 Check_Statement_Sequence (Statements (ECA));
7225 Check_Statement_Sequence (Statements (DCA));
7230 -- Conditional entry call, check entry call and else part
7232 -- Note: in expanded code, the conditional entry call has been
7233 -- converted to a set of expanded statements on which the check
7234 -- will work correctly in any case.
7236 elsif Kind = N_Conditional_Entry_Call then
7238 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7241 -- If statement sequence of entry call alternative is missing,
7242 -- then we can definitely fall through, and we post the error
7243 -- message on the entry call alternative itself.
7245 if No (Statements (ECA)) then
7248 -- Else check statement sequence and else part
7251 Check_Statement_Sequence (Statements (ECA));
7252 Check_Statement_Sequence (Else_Statements (Last_Stm));
7258 -- If we fall through, issue appropriate message
7262 -- Kill warning if last statement is a raise exception call,
7263 -- or a pragma Assert (False). Note that with assertions enabled,
7264 -- such a pragma has been converted into a raise exception call
7265 -- already, so the Assert_False is for the assertions off case.
7267 if not Raise_Exception_Call and then not Assert_False then
7269 -- In GNATprove mode, it is an error to have a missing return
7271 Error_Msg_Warn := SPARK_Mode /= On;
7273 -- Issue error message or warning
7276 ("RETURN statement missing following this statement<<!",
7279 ("\Program_Error ]<<!", Last_Stm);
7282 -- Note: we set Err even though we have not issued a warning
7283 -- because we still have a case of a missing return. This is
7284 -- an extremely marginal case, probably will never be noticed
7285 -- but we might as well get it right.
7289 -- Otherwise we have the case of a procedure marked No_Return
7292 if not Raise_Exception_Call then
7293 if GNATprove_Mode then
7295 ("implied return after this statement would have raised "
7296 & "Program_Error", Last_Stm);
7298 -- In normal compilation mode, do not warn on a generated call
7299 -- (e.g. in the body of a renaming as completion).
7301 elsif Comes_From_Source (Last_Stm) then
7303 ("implied return after this statement will raise "
7304 & "Program_Error??", Last_Stm);
7307 Error_Msg_Warn := SPARK_Mode /= On;
7309 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7313 RE : constant Node_Id :=
7314 Make_Raise_Program_Error (Sloc (Last_Stm),
7315 Reason => PE_Implicit_Return);
7317 Insert_After (Last_Stm, RE);
7321 end Check_Statement_Sequence;
7323 -- Start of processing for Check_Returns
7327 Check_Statement_Sequence (Statements (HSS));
7329 if Present (Exception_Handlers (HSS)) then
7330 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7331 while Present (Handler) loop
7332 Check_Statement_Sequence (Statements (Handler));
7333 Next_Non_Pragma (Handler);
7338 ----------------------------
7339 -- Check_Subprogram_Order --
7340 ----------------------------
7342 procedure Check_Subprogram_Order (N : Node_Id) is
7344 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7345 -- This is used to check if S1 > S2 in the sense required by this test,
7346 -- for example nameab < namec, but name2 < name10.
7348 -----------------------------
7349 -- Subprogram_Name_Greater --
7350 -----------------------------
7352 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7357 -- Deal with special case where names are identical except for a
7358 -- numerical suffix. These are handled specially, taking the numeric
7359 -- ordering from the suffix into account.
7362 while S1 (L1) in '0' .. '9' loop
7367 while S2 (L2) in '0' .. '9' loop
7371 -- If non-numeric parts non-equal, do straight compare
7373 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7376 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7377 -- that a missing suffix is treated as numeric zero in this test.
7381 while L1 < S1'Last loop
7383 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7387 while L2 < S2'Last loop
7389 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7394 end Subprogram_Name_Greater;
7396 -- Start of processing for Check_Subprogram_Order
7399 -- Check body in alpha order if this is option
7402 and then Style_Check_Order_Subprograms
7403 and then Nkind (N) = N_Subprogram_Body
7404 and then Comes_From_Source (N)
7405 and then In_Extended_Main_Source_Unit (N)
7409 renames Scope_Stack.Table
7410 (Scope_Stack.Last).Last_Subprogram_Name;
7412 Body_Id : constant Entity_Id :=
7413 Defining_Entity (Specification (N));
7416 Get_Decoded_Name_String (Chars (Body_Id));
7419 if Subprogram_Name_Greater
7420 (LSN.all, Name_Buffer (1 .. Name_Len))
7422 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7428 LSN := new String'(Name_Buffer (1 .. Name_Len));
7431 end Check_Subprogram_Order;
7433 ------------------------------
7434 -- Check_Subtype_Conformant --
7435 ------------------------------
7437 procedure Check_Subtype_Conformant
7438 (New_Id : Entity_Id;
7440 Err_Loc : Node_Id := Empty;
7441 Skip_Controlling_Formals : Boolean := False;
7442 Get_Inst : Boolean := False)
7445 pragma Warnings (Off, Result);
7448 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7449 Skip_Controlling_Formals => Skip_Controlling_Formals,
7450 Get_Inst => Get_Inst);
7451 end Check_Subtype_Conformant;
7453 -----------------------------------
7454 -- Check_Synchronized_Overriding --
7455 -----------------------------------
7457 procedure Check_Synchronized_Overriding
7458 (Def_Id : Entity_Id;
7459 Overridden_Subp : out Entity_Id)
7461 Ifaces_List : Elist_Id;
7465 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7466 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7467 -- subprogram is implemented by a protected procedure or entry,
7468 -- its first parameter must be out, in out, or access-to-variable.
7470 function Matches_Prefixed_View_Profile
7471 (Prim_Params : List_Id;
7472 Iface_Params : List_Id) return Boolean;
7473 -- Determine whether a subprogram's parameter profile Prim_Params
7474 -- matches that of a potentially overridden interface subprogram
7475 -- Iface_Params. Also determine if the type of first parameter of
7476 -- Iface_Params is an implemented interface.
7478 ----------------------
7479 -- Is_Valid_Formal --
7480 ----------------------
7482 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7485 Ekind_In (F, E_In_Out_Parameter, E_Out_Parameter)
7487 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7488 and then not Constant_Present (Parameter_Type (Parent (F))));
7489 end Is_Valid_Formal;
7491 -----------------------------------
7492 -- Matches_Prefixed_View_Profile --
7493 -----------------------------------
7495 function Matches_Prefixed_View_Profile
7496 (Prim_Params : List_Id;
7497 Iface_Params : List_Id) return Boolean
7499 function Is_Implemented
7500 (Ifaces_List : Elist_Id;
7501 Iface : Entity_Id) return Boolean;
7502 -- Determine if Iface is implemented by the current task or
7505 --------------------
7506 -- Is_Implemented --
7507 --------------------
7509 function Is_Implemented
7510 (Ifaces_List : Elist_Id;
7511 Iface : Entity_Id) return Boolean
7513 Iface_Elmt : Elmt_Id;
7516 Iface_Elmt := First_Elmt (Ifaces_List);
7517 while Present (Iface_Elmt) loop
7518 if Node (Iface_Elmt) = Iface then
7522 Next_Elmt (Iface_Elmt);
7530 Iface_Id : Entity_Id;
7531 Iface_Param : Node_Id;
7532 Iface_Typ : Entity_Id;
7533 Prim_Id : Entity_Id;
7534 Prim_Param : Node_Id;
7535 Prim_Typ : Entity_Id;
7537 -- Start of processing for Matches_Prefixed_View_Profile
7540 Iface_Param := First (Iface_Params);
7541 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7543 if Is_Access_Type (Iface_Typ) then
7544 Iface_Typ := Designated_Type (Iface_Typ);
7547 Prim_Param := First (Prim_Params);
7549 -- The first parameter of the potentially overridden subprogram must
7550 -- be an interface implemented by Prim.
7552 if not Is_Interface (Iface_Typ)
7553 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7558 -- The checks on the object parameters are done, so move on to the
7559 -- rest of the parameters.
7561 if not In_Scope then
7566 while Present (Iface_Param) and then Present (Prim_Param) loop
7567 Iface_Id := Defining_Identifier (Iface_Param);
7568 Iface_Typ := Find_Parameter_Type (Iface_Param);
7570 Prim_Id := Defining_Identifier (Prim_Param);
7571 Prim_Typ := Find_Parameter_Type (Prim_Param);
7573 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7574 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7575 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7577 Iface_Typ := Designated_Type (Iface_Typ);
7578 Prim_Typ := Designated_Type (Prim_Typ);
7581 -- Case of multiple interface types inside a parameter profile
7583 -- (Obj_Param : in out Iface; ...; Param : Iface)
7585 -- If the interface type is implemented, then the matching type in
7586 -- the primitive should be the implementing record type.
7588 if Ekind (Iface_Typ) = E_Record_Type
7589 and then Is_Interface (Iface_Typ)
7590 and then Is_Implemented (Ifaces_List, Iface_Typ)
7592 if Prim_Typ /= Typ then
7596 -- The two parameters must be both mode and subtype conformant
7598 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7600 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7609 -- One of the two lists contains more parameters than the other
7611 if Present (Iface_Param) or else Present (Prim_Param) then
7616 end Matches_Prefixed_View_Profile;
7618 -- Start of processing for Check_Synchronized_Overriding
7621 Overridden_Subp := Empty;
7623 -- Def_Id must be an entry or a subprogram. We should skip predefined
7624 -- primitives internally generated by the front end; however at this
7625 -- stage predefined primitives are still not fully decorated. As a
7626 -- minor optimization we skip here internally generated subprograms.
7628 if (Ekind (Def_Id) /= E_Entry
7629 and then Ekind (Def_Id) /= E_Function
7630 and then Ekind (Def_Id) /= E_Procedure)
7631 or else not Comes_From_Source (Def_Id)
7636 -- Search for the concurrent declaration since it contains the list of
7637 -- all implemented interfaces. In this case, the subprogram is declared
7638 -- within the scope of a protected or a task type.
7640 if Present (Scope (Def_Id))
7641 and then Is_Concurrent_Type (Scope (Def_Id))
7642 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7644 Typ := Scope (Def_Id);
7647 -- The enclosing scope is not a synchronized type and the subprogram
7650 elsif No (First_Formal (Def_Id)) then
7653 -- The subprogram has formals and hence it may be a primitive of a
7657 Typ := Etype (First_Formal (Def_Id));
7659 if Is_Access_Type (Typ) then
7660 Typ := Directly_Designated_Type (Typ);
7663 if Is_Concurrent_Type (Typ)
7664 and then not Is_Generic_Actual_Type (Typ)
7668 -- This case occurs when the concurrent type is declared within a
7669 -- generic unit. As a result the corresponding record has been built
7670 -- and used as the type of the first formal, we just have to retrieve
7671 -- the corresponding concurrent type.
7673 elsif Is_Concurrent_Record_Type (Typ)
7674 and then not Is_Class_Wide_Type (Typ)
7675 and then Present (Corresponding_Concurrent_Type (Typ))
7677 Typ := Corresponding_Concurrent_Type (Typ);
7685 -- There is no overriding to check if this is an inherited operation in
7686 -- a type derivation for a generic actual.
7688 Collect_Interfaces (Typ, Ifaces_List);
7690 if Is_Empty_Elmt_List (Ifaces_List) then
7694 -- Determine whether entry or subprogram Def_Id overrides a primitive
7695 -- operation that belongs to one of the interfaces in Ifaces_List.
7698 Candidate : Entity_Id := Empty;
7699 Hom : Entity_Id := Empty;
7700 Subp : Entity_Id := Empty;
7703 -- Traverse the homonym chain, looking for a potentially overridden
7704 -- subprogram that belongs to an implemented interface.
7706 Hom := Current_Entity_In_Scope (Def_Id);
7707 while Present (Hom) loop
7711 or else not Is_Overloadable (Subp)
7712 or else not Is_Primitive (Subp)
7713 or else not Is_Dispatching_Operation (Subp)
7714 or else not Present (Find_Dispatching_Type (Subp))
7715 or else not Is_Interface (Find_Dispatching_Type (Subp))
7719 -- Entries and procedures can override abstract or null interface
7722 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7723 and then Ekind (Subp) = E_Procedure
7724 and then Matches_Prefixed_View_Profile
7725 (Parameter_Specifications (Parent (Def_Id)),
7726 Parameter_Specifications (Parent (Subp)))
7730 -- For an overridden subprogram Subp, check whether the mode
7731 -- of its first parameter is correct depending on the kind of
7732 -- synchronized type.
7735 Formal : constant Node_Id := First_Formal (Candidate);
7738 -- In order for an entry or a protected procedure to
7739 -- override, the first parameter of the overridden routine
7740 -- must be of mode "out", "in out", or access-to-variable.
7742 if Ekind_In (Candidate, E_Entry, E_Procedure)
7743 and then Is_Protected_Type (Typ)
7744 and then not Is_Valid_Formal (Formal)
7748 -- All other cases are OK since a task entry or routine does
7749 -- not have a restriction on the mode of the first parameter
7750 -- of the overridden interface routine.
7753 Overridden_Subp := Candidate;
7758 -- Functions can override abstract interface functions. Return
7759 -- types must be subtype conformant.
7761 elsif Ekind (Def_Id) = E_Function
7762 and then Ekind (Subp) = E_Function
7763 and then Matches_Prefixed_View_Profile
7764 (Parameter_Specifications (Parent (Def_Id)),
7765 Parameter_Specifications (Parent (Subp)))
7766 and then Conforming_Types
7767 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7771 -- If an inherited subprogram is implemented by a protected
7772 -- function, then the first parameter of the inherited
7773 -- subprogram shall be of mode in, but not an access-to-
7774 -- variable parameter (RM 9.4(11/9)).
7776 if Present (First_Formal (Subp))
7777 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7779 (not Is_Access_Type (Etype (First_Formal (Subp)))
7781 Is_Access_Constant (Etype (First_Formal (Subp))))
7783 Overridden_Subp := Subp;
7788 Hom := Homonym (Hom);
7791 -- After examining all candidates for overriding, we are left with
7792 -- the best match, which is a mode-incompatible interface routine.
7794 if In_Scope and then Present (Candidate) then
7795 Error_Msg_PT (Def_Id, Candidate);
7798 Overridden_Subp := Candidate;
7801 end Check_Synchronized_Overriding;
7803 ---------------------------
7804 -- Check_Type_Conformant --
7805 ---------------------------
7807 procedure Check_Type_Conformant
7808 (New_Id : Entity_Id;
7810 Err_Loc : Node_Id := Empty)
7813 pragma Warnings (Off, Result);
7816 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7817 end Check_Type_Conformant;
7819 ---------------------------
7820 -- Can_Override_Operator --
7821 ---------------------------
7823 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7827 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7831 Typ := Base_Type (Etype (First_Formal (Subp)));
7833 -- Check explicitly that the operation is a primitive of the type
7835 return Operator_Matches_Spec (Subp, Subp)
7836 and then not Is_Generic_Type (Typ)
7837 and then Scope (Subp) = Scope (Typ)
7838 and then not Is_Class_Wide_Type (Typ);
7840 end Can_Override_Operator;
7842 ----------------------
7843 -- Conforming_Types --
7844 ----------------------
7846 function Conforming_Types
7849 Ctype : Conformance_Type;
7850 Get_Inst : Boolean := False) return Boolean
7852 function Base_Types_Match
7854 Typ_2 : Entity_Id) return Boolean;
7855 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7856 -- in different scopes (e.g. parent and child instances), then verify
7857 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7858 -- the same subtype chain. The whole purpose of this procedure is to
7859 -- prevent spurious ambiguities in an instantiation that may arise if
7860 -- two distinct generic types are instantiated with the same actual.
7862 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7863 -- An access parameter can designate an incomplete type. If the
7864 -- incomplete type is the limited view of a type from a limited_
7865 -- with_clause, check whether the non-limited view is available.
7866 -- If it is a (non-limited) incomplete type, get the full view.
7868 function Matches_Limited_With_View
7870 Typ_2 : Entity_Id) return Boolean;
7871 -- Returns True if and only if either Typ_1 denotes a limited view of
7872 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7873 -- the limited with view of a type is used in a subprogram declaration
7874 -- and the subprogram body is in the scope of a regular with clause for
7875 -- the same unit. In such a case, the two type entities are considered
7876 -- identical for purposes of conformance checking.
7878 ----------------------
7879 -- Base_Types_Match --
7880 ----------------------
7882 function Base_Types_Match
7884 Typ_2 : Entity_Id) return Boolean
7886 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7887 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7890 if Typ_1 = Typ_2 then
7893 elsif Base_1 = Base_2 then
7895 -- The following is too permissive. A more precise test should
7896 -- check that the generic actual is an ancestor subtype of the
7899 -- See code in Find_Corresponding_Spec that applies an additional
7900 -- filter to handle accidental amiguities in instances.
7903 not Is_Generic_Actual_Type (Typ_1)
7904 or else not Is_Generic_Actual_Type (Typ_2)
7905 or else Scope (Typ_1) /= Scope (Typ_2);
7907 -- If Typ_2 is a generic actual type it is declared as the subtype of
7908 -- the actual. If that actual is itself a subtype we need to use its
7909 -- own base type to check for compatibility.
7911 elsif Ekind (Base_2) = Ekind (Typ_2)
7912 and then Base_1 = Base_Type (Base_2)
7916 elsif Ekind (Base_1) = Ekind (Typ_1)
7917 and then Base_2 = Base_Type (Base_1)
7924 end Base_Types_Match;
7926 --------------------------
7927 -- Find_Designated_Type --
7928 --------------------------
7930 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7934 Desig := Directly_Designated_Type (Typ);
7936 if Ekind (Desig) = E_Incomplete_Type then
7938 -- If regular incomplete type, get full view if available
7940 if Present (Full_View (Desig)) then
7941 Desig := Full_View (Desig);
7943 -- If limited view of a type, get non-limited view if available,
7944 -- and check again for a regular incomplete type.
7946 elsif Present (Non_Limited_View (Desig)) then
7947 Desig := Get_Full_View (Non_Limited_View (Desig));
7952 end Find_Designated_Type;
7954 -------------------------------
7955 -- Matches_Limited_With_View --
7956 -------------------------------
7958 function Matches_Limited_With_View
7960 Typ_2 : Entity_Id) return Boolean
7962 function Is_Matching_Limited_View
7964 View : Entity_Id) return Boolean;
7965 -- Determine whether non-limited view View denotes type Typ in some
7966 -- conformant fashion.
7968 ------------------------------
7969 -- Is_Matching_Limited_View --
7970 ------------------------------
7972 function Is_Matching_Limited_View
7974 View : Entity_Id) return Boolean
7976 Root_Typ : Entity_Id;
7977 Root_View : Entity_Id;
7980 -- The non-limited view directly denotes the type
7985 -- The type is a subtype of the non-limited view
7987 elsif Is_Subtype_Of (Typ, View) then
7990 -- Both the non-limited view and the type denote class-wide types
7992 elsif Is_Class_Wide_Type (Typ)
7993 and then Is_Class_Wide_Type (View)
7995 Root_Typ := Root_Type (Typ);
7996 Root_View := Root_Type (View);
7998 if Root_Typ = Root_View then
8001 -- An incomplete tagged type and its full view may receive two
8002 -- distinct class-wide types when the related package has not
8003 -- been analyzed yet.
8006 -- type T is tagged; -- CW_1
8007 -- type T is tagged null record; -- CW_2
8010 -- This is because the package lacks any semantic information
8011 -- that may eventually link both views of T. As a consequence,
8012 -- a client of the limited view of Pack will see CW_2 while a
8013 -- client of the non-limited view of Pack will see CW_1.
8015 elsif Is_Incomplete_Type (Root_Typ)
8016 and then Present (Full_View (Root_Typ))
8017 and then Full_View (Root_Typ) = Root_View
8021 elsif Is_Incomplete_Type (Root_View)
8022 and then Present (Full_View (Root_View))
8023 and then Full_View (Root_View) = Root_Typ
8030 end Is_Matching_Limited_View;
8032 -- Start of processing for Matches_Limited_With_View
8035 -- In some cases a type imported through a limited_with clause, and
8036 -- its non-limited view are both visible, for example in an anonymous
8037 -- access-to-class-wide type in a formal, or when building the body
8038 -- for a subprogram renaming after the subprogram has been frozen.
8039 -- In these cases both entities designate the same type. In addition,
8040 -- if one of them is an actual in an instance, it may be a subtype of
8041 -- the non-limited view of the other.
8043 if From_Limited_With (Typ_1)
8044 and then From_Limited_With (Typ_2)
8045 and then Available_View (Typ_1) = Available_View (Typ_2)
8049 elsif From_Limited_With (Typ_1) then
8050 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8052 elsif From_Limited_With (Typ_2) then
8053 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8058 end Matches_Limited_With_View;
8062 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8064 Type_1 : Entity_Id := T1;
8065 Type_2 : Entity_Id := T2;
8067 -- Start of processing for Conforming_Types
8070 -- The context is an instance association for a formal access-to-
8071 -- subprogram type; the formal parameter types require mapping because
8072 -- they may denote other formal parameters of the generic unit.
8075 Type_1 := Get_Instance_Of (T1);
8076 Type_2 := Get_Instance_Of (T2);
8079 -- If one of the types is a view of the other introduced by a limited
8080 -- with clause, treat these as conforming for all purposes.
8082 if Matches_Limited_With_View (T1, T2) then
8085 elsif Base_Types_Match (Type_1, Type_2) then
8086 if Ctype <= Mode_Conformant then
8091 Subtypes_Statically_Match (Type_1, Type_2)
8092 and then Dimensions_Match (Type_1, Type_2);
8095 elsif Is_Incomplete_Or_Private_Type (Type_1)
8096 and then Present (Full_View (Type_1))
8097 and then Base_Types_Match (Full_View (Type_1), Type_2)
8100 Ctype <= Mode_Conformant
8101 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8103 elsif Ekind (Type_2) = E_Incomplete_Type
8104 and then Present (Full_View (Type_2))
8105 and then Base_Types_Match (Type_1, Full_View (Type_2))
8108 Ctype <= Mode_Conformant
8109 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8111 elsif Is_Private_Type (Type_2)
8112 and then In_Instance
8113 and then Present (Full_View (Type_2))
8114 and then Base_Types_Match (Type_1, Full_View (Type_2))
8117 Ctype <= Mode_Conformant
8118 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8120 -- Another confusion between views in a nested instance with an
8121 -- actual private type whose full view is not in scope.
8123 elsif Ekind (Type_2) = E_Private_Subtype
8124 and then In_Instance
8125 and then Etype (Type_2) = Type_1
8129 -- In Ada 2012, incomplete types (including limited views) can appear
8130 -- as actuals in instantiations, where they are conformant to the
8131 -- corresponding incomplete formal.
8133 elsif Is_Incomplete_Type (Type_1)
8134 and then Is_Incomplete_Type (Type_2)
8135 and then In_Instance
8136 and then (Used_As_Generic_Actual (Type_1)
8137 or else Used_As_Generic_Actual (Type_2))
8142 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8143 -- treated recursively because they carry a signature. As far as
8144 -- conformance is concerned, convention plays no role, and either
8145 -- or both could be access to protected subprograms.
8147 Are_Anonymous_Access_To_Subprogram_Types :=
8148 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
8149 E_Anonymous_Access_Protected_Subprogram_Type)
8151 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
8152 E_Anonymous_Access_Protected_Subprogram_Type);
8154 -- Test anonymous access type case. For this case, static subtype
8155 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8156 -- the base types because we may have built internal subtype entities
8157 -- to handle null-excluding types (see Process_Formals).
8159 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8161 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8163 -- Ada 2005 (AI-254)
8165 or else Are_Anonymous_Access_To_Subprogram_Types
8168 Desig_1 : Entity_Id;
8169 Desig_2 : Entity_Id;
8172 -- In Ada 2005, access constant indicators must match for
8173 -- subtype conformance.
8175 if Ada_Version >= Ada_2005
8176 and then Ctype >= Subtype_Conformant
8178 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8183 Desig_1 := Find_Designated_Type (Type_1);
8184 Desig_2 := Find_Designated_Type (Type_2);
8186 -- If the context is an instance association for a formal
8187 -- access-to-subprogram type; formal access parameter designated
8188 -- types require mapping because they may denote other formal
8189 -- parameters of the generic unit.
8192 Desig_1 := Get_Instance_Of (Desig_1);
8193 Desig_2 := Get_Instance_Of (Desig_2);
8196 -- It is possible for a Class_Wide_Type to be introduced for an
8197 -- incomplete type, in which case there is a separate class_ wide
8198 -- type for the full view. The types conform if their Etypes
8199 -- conform, i.e. one may be the full view of the other. This can
8200 -- only happen in the context of an access parameter, other uses
8201 -- of an incomplete Class_Wide_Type are illegal.
8203 if Is_Class_Wide_Type (Desig_1)
8205 Is_Class_Wide_Type (Desig_2)
8209 (Etype (Base_Type (Desig_1)),
8210 Etype (Base_Type (Desig_2)), Ctype);
8212 elsif Are_Anonymous_Access_To_Subprogram_Types then
8213 if Ada_Version < Ada_2005 then
8215 Ctype = Type_Conformant
8216 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8218 -- We must check the conformance of the signatures themselves
8222 Conformant : Boolean;
8225 (Desig_1, Desig_2, Ctype, False, Conformant);
8230 -- A limited view of an actual matches the corresponding
8231 -- incomplete formal.
8233 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8234 and then From_Limited_With (Desig_2)
8235 and then Used_As_Generic_Actual (Etype (Desig_2))
8240 return Base_Type (Desig_1) = Base_Type (Desig_2)
8241 and then (Ctype = Type_Conformant
8243 Subtypes_Statically_Match (Desig_1, Desig_2));
8247 -- Otherwise definitely no match
8250 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8251 and then Is_Access_Type (Type_2))
8252 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8253 and then Is_Access_Type (Type_1)))
8256 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8258 May_Hide_Profile := True;
8263 end Conforming_Types;
8265 --------------------------
8266 -- Create_Extra_Formals --
8267 --------------------------
8269 procedure Create_Extra_Formals (E : Entity_Id) is
8270 First_Extra : Entity_Id := Empty;
8272 Last_Extra : Entity_Id := Empty;
8274 function Add_Extra_Formal
8275 (Assoc_Entity : Entity_Id;
8278 Suffix : String) return Entity_Id;
8279 -- Add an extra formal to the current list of formals and extra formals.
8280 -- The extra formal is added to the end of the list of extra formals,
8281 -- and also returned as the result. These formals are always of mode IN.
8282 -- The new formal has the type Typ, is declared in Scope, and its name
8283 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8284 -- The following suffixes are currently used. They should not be changed
8285 -- without coordinating with CodePeer, which makes use of these to
8286 -- provide better messages.
8288 -- O denotes the Constrained bit.
8289 -- L denotes the accessibility level.
8290 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8291 -- the full list in exp_ch6.BIP_Formal_Kind.
8293 ----------------------
8294 -- Add_Extra_Formal --
8295 ----------------------
8297 function Add_Extra_Formal
8298 (Assoc_Entity : Entity_Id;
8301 Suffix : String) return Entity_Id
8303 EF : constant Entity_Id :=
8304 Make_Defining_Identifier (Sloc (Assoc_Entity),
8305 Chars => New_External_Name (Chars (Assoc_Entity),
8309 -- A little optimization. Never generate an extra formal for the
8310 -- _init operand of an initialization procedure, since it could
8313 if Chars (Formal) = Name_uInit then
8317 Set_Ekind (EF, E_In_Parameter);
8318 Set_Actual_Subtype (EF, Typ);
8319 Set_Etype (EF, Typ);
8320 Set_Scope (EF, Scope);
8321 Set_Mechanism (EF, Default_Mechanism);
8322 Set_Formal_Validity (EF);
8324 if No (First_Extra) then
8326 Set_Extra_Formals (Scope, EF);
8329 if Present (Last_Extra) then
8330 Set_Extra_Formal (Last_Extra, EF);
8336 end Add_Extra_Formal;
8340 Formal_Type : Entity_Id;
8341 P_Formal : Entity_Id := Empty;
8343 -- Start of processing for Create_Extra_Formals
8346 -- We never generate extra formals if expansion is not active because we
8347 -- don't need them unless we are generating code.
8349 if not Expander_Active then
8353 -- No need to generate extra formals in interface thunks whose target
8354 -- primitive has no extra formals.
8356 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8360 -- If this is a derived subprogram then the subtypes of the parent
8361 -- subprogram's formal parameters will be used to determine the need
8362 -- for extra formals.
8364 if Is_Overloadable (E) and then Present (Alias (E)) then
8365 P_Formal := First_Formal (Alias (E));
8368 Formal := First_Formal (E);
8369 while Present (Formal) loop
8370 Last_Extra := Formal;
8371 Next_Formal (Formal);
8374 -- If Extra_Formals were already created, don't do it again. This
8375 -- situation may arise for subprogram types created as part of
8376 -- dispatching calls (see Expand_Dispatching_Call).
8378 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8382 -- If the subprogram is a predefined dispatching subprogram then don't
8383 -- generate any extra constrained or accessibility level formals. In
8384 -- general we suppress these for internal subprograms (by not calling
8385 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8386 -- generated stream attributes do get passed through because extra
8387 -- build-in-place formals are needed in some cases (limited 'Input).
8389 if Is_Predefined_Internal_Operation (E) then
8390 goto Test_For_Func_Result_Extras;
8393 Formal := First_Formal (E);
8394 while Present (Formal) loop
8396 -- Create extra formal for supporting the attribute 'Constrained.
8397 -- The case of a private type view without discriminants also
8398 -- requires the extra formal if the underlying type has defaulted
8401 if Ekind (Formal) /= E_In_Parameter then
8402 if Present (P_Formal) then
8403 Formal_Type := Etype (P_Formal);
8405 Formal_Type := Etype (Formal);
8408 -- Do not produce extra formals for Unchecked_Union parameters.
8409 -- Jump directly to the end of the loop.
8411 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8412 goto Skip_Extra_Formal_Generation;
8415 if not Has_Discriminants (Formal_Type)
8416 and then Ekind (Formal_Type) in Private_Kind
8417 and then Present (Underlying_Type (Formal_Type))
8419 Formal_Type := Underlying_Type (Formal_Type);
8422 -- Suppress the extra formal if formal's subtype is constrained or
8423 -- indefinite, or we're compiling for Ada 2012 and the underlying
8424 -- type is tagged and limited. In Ada 2012, a limited tagged type
8425 -- can have defaulted discriminants, but 'Constrained is required
8426 -- to return True, so the formal is never needed (see AI05-0214).
8427 -- Note that this ensures consistency of calling sequences for
8428 -- dispatching operations when some types in a class have defaults
8429 -- on discriminants and others do not (and requiring the extra
8430 -- formal would introduce distributed overhead).
8432 -- If the type does not have a completion yet, treat as prior to
8433 -- Ada 2012 for consistency.
8435 if Has_Discriminants (Formal_Type)
8436 and then not Is_Constrained (Formal_Type)
8437 and then Is_Definite_Subtype (Formal_Type)
8438 and then (Ada_Version < Ada_2012
8439 or else No (Underlying_Type (Formal_Type))
8441 (Is_Limited_Type (Formal_Type)
8444 (Underlying_Type (Formal_Type)))))
8446 Set_Extra_Constrained
8447 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8451 -- Create extra formal for supporting accessibility checking. This
8452 -- is done for both anonymous access formals and formals of named
8453 -- access types that are marked as controlling formals. The latter
8454 -- case can occur when Expand_Dispatching_Call creates a subprogram
8455 -- type and substitutes the types of access-to-class-wide actuals
8456 -- for the anonymous access-to-specific-type of controlling formals.
8457 -- Base_Type is applied because in cases where there is a null
8458 -- exclusion the formal may have an access subtype.
8460 -- This is suppressed if we specifically suppress accessibility
8461 -- checks at the package level for either the subprogram, or the
8462 -- package in which it resides. However, we do not suppress it
8463 -- simply if the scope has accessibility checks suppressed, since
8464 -- this could cause trouble when clients are compiled with a
8465 -- different suppression setting. The explicit checks at the
8466 -- package level are safe from this point of view.
8468 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8469 or else (Is_Controlling_Formal (Formal)
8470 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8472 (Explicit_Suppress (E, Accessibility_Check)
8474 Explicit_Suppress (Scope (E), Accessibility_Check))
8477 or else Present (Extra_Accessibility (P_Formal)))
8479 Set_Extra_Accessibility
8480 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8483 -- This label is required when skipping extra formal generation for
8484 -- Unchecked_Union parameters.
8486 <<Skip_Extra_Formal_Generation>>
8488 if Present (P_Formal) then
8489 Next_Formal (P_Formal);
8492 Next_Formal (Formal);
8495 <<Test_For_Func_Result_Extras>>
8497 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8498 -- function call is ... determined by the point of call ...".
8500 if Needs_Result_Accessibility_Level (E) then
8501 Set_Extra_Accessibility_Of_Result
8502 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8505 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8506 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8508 if Is_Build_In_Place_Function (E) then
8510 Result_Subt : constant Entity_Id := Etype (E);
8511 Formal_Typ : Entity_Id;
8512 Subp_Decl : Node_Id;
8513 Discard : Entity_Id;
8516 -- In the case of functions with unconstrained result subtypes,
8517 -- add a 4-state formal indicating whether the return object is
8518 -- allocated by the caller (1), or should be allocated by the
8519 -- callee on the secondary stack (2), in the global heap (3), or
8520 -- in a user-defined storage pool (4). For the moment we just use
8521 -- Natural for the type of this formal. Note that this formal
8522 -- isn't usually needed in the case where the result subtype is
8523 -- constrained, but it is needed when the function has a tagged
8524 -- result, because generally such functions can be called in a
8525 -- dispatching context and such calls must be handled like calls
8526 -- to a class-wide function.
8528 if Needs_BIP_Alloc_Form (E) then
8531 (E, Standard_Natural,
8532 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8534 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8535 -- use a user-defined pool. This formal is not added on
8536 -- ZFP as those targets do not support pools.
8538 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8541 (E, RTE (RE_Root_Storage_Pool_Ptr),
8542 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8546 -- In the case of functions whose result type needs finalization,
8547 -- add an extra formal which represents the finalization master.
8549 if Needs_BIP_Finalization_Master (E) then
8552 (E, RTE (RE_Finalization_Master_Ptr),
8553 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8556 -- When the result type contains tasks, add two extra formals: the
8557 -- master of the tasks to be created, and the caller's activation
8560 if Needs_BIP_Task_Actuals (E) then
8563 (E, RTE (RE_Master_Id),
8564 E, BIP_Formal_Suffix (BIP_Task_Master));
8566 Set_Has_Master_Entity (E);
8570 (E, RTE (RE_Activation_Chain_Access),
8571 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8574 -- All build-in-place functions get an extra formal that will be
8575 -- passed the address of the return object within the caller.
8578 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8580 -- Incomplete_View_From_Limited_With is needed here because
8581 -- gigi gets confused if the designated type is the full view
8582 -- coming from a limited-with'ed package. In the normal case,
8583 -- (no limited with) Incomplete_View_From_Limited_With
8584 -- returns Result_Subt.
8586 Set_Directly_Designated_Type
8587 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
8588 Set_Etype (Formal_Typ, Formal_Typ);
8589 Set_Depends_On_Private
8590 (Formal_Typ, Has_Private_Component (Formal_Typ));
8591 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8592 Set_Is_Access_Constant (Formal_Typ, False);
8594 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8595 -- the designated type comes from the limited view (for back-end
8598 Set_From_Limited_With
8599 (Formal_Typ, From_Limited_With (Result_Subt));
8601 Layout_Type (Formal_Typ);
8603 -- Force the definition of the Itype in case of internal function
8604 -- calls within the same or nested scope.
8606 if Is_Subprogram_Or_Generic_Subprogram (E) then
8607 Subp_Decl := Parent (E);
8609 -- The insertion point for an Itype reference should be after
8610 -- the unit declaration node of the subprogram. An exception
8611 -- to this are inherited operations from a parent type in which
8612 -- case the derived type acts as their parent.
8614 if Nkind_In (Subp_Decl, N_Function_Specification,
8615 N_Procedure_Specification)
8617 Subp_Decl := Parent (Subp_Decl);
8620 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8625 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8629 -- If this is an instance of a generic, we need to have extra formals
8632 if Is_Generic_Instance (E) and then Present (Alias (E)) then
8633 Set_Extra_Formals (Alias (E), Extra_Formals (E));
8635 end Create_Extra_Formals;
8637 -----------------------------
8638 -- Enter_Overloaded_Entity --
8639 -----------------------------
8641 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8642 function Matches_Predefined_Op return Boolean;
8643 -- This returns an approximation of whether S matches a predefined
8644 -- operator, based on the operator symbol, and the parameter and result
8645 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8647 ---------------------------
8648 -- Matches_Predefined_Op --
8649 ---------------------------
8651 function Matches_Predefined_Op return Boolean is
8652 Formal_1 : constant Entity_Id := First_Formal (S);
8653 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8654 Op : constant Name_Id := Chars (S);
8655 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8656 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8661 if Present (Formal_2) then
8663 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8666 -- All but "&" and "**" have same-types parameters
8675 if Type_1 /= Type_2 then
8680 -- Check parameter and result types
8688 Is_Boolean_Type (Result_Type)
8689 and then Result_Type = Type_1;
8695 Is_Integer_Type (Result_Type)
8696 and then Result_Type = Type_1;
8704 Is_Numeric_Type (Result_Type)
8705 and then Result_Type = Type_1;
8711 Is_Boolean_Type (Result_Type)
8712 and then not Is_Limited_Type (Type_1);
8720 Is_Boolean_Type (Result_Type)
8721 and then (Is_Array_Type (Type_1)
8722 or else Is_Scalar_Type (Type_1));
8724 when Name_Op_Concat =>
8725 return Is_Array_Type (Result_Type);
8727 when Name_Op_Expon =>
8729 (Is_Integer_Type (Result_Type)
8730 or else Is_Floating_Point_Type (Result_Type))
8731 and then Result_Type = Type_1
8732 and then Type_2 = Standard_Integer;
8735 raise Program_Error;
8748 Is_Numeric_Type (Result_Type)
8749 and then Result_Type = Type_1;
8753 Is_Boolean_Type (Result_Type)
8754 and then Result_Type = Type_1;
8757 raise Program_Error;
8760 end Matches_Predefined_Op;
8764 E : Entity_Id := Current_Entity_In_Scope (S);
8765 C_E : Entity_Id := Current_Entity (S);
8767 -- Start of processing for Enter_Overloaded_Entity
8771 Set_Has_Homonym (E);
8772 Set_Has_Homonym (S);
8775 Set_Is_Immediately_Visible (S);
8776 Set_Scope (S, Current_Scope);
8778 -- Chain new entity if front of homonym in current scope, so that
8779 -- homonyms are contiguous.
8781 if Present (E) and then E /= C_E then
8782 while Homonym (C_E) /= E loop
8783 C_E := Homonym (C_E);
8786 Set_Homonym (C_E, S);
8790 Set_Current_Entity (S);
8795 if Is_Inherited_Operation (S) then
8796 Append_Inherited_Subprogram (S);
8798 Append_Entity (S, Current_Scope);
8801 Set_Public_Status (S);
8803 if Debug_Flag_E then
8804 Write_Str ("New overloaded entity chain: ");
8805 Write_Name (Chars (S));
8808 while Present (E) loop
8809 Write_Str (" "); Write_Int (Int (E));
8816 -- Generate warning for hiding
8819 and then Comes_From_Source (S)
8820 and then In_Extended_Main_Source_Unit (S)
8827 -- Warn unless genuine overloading. Do not emit warning on
8828 -- hiding predefined operators in Standard (these are either an
8829 -- (artifact of our implicit declarations, or simple noise) but
8830 -- keep warning on a operator defined on a local subtype, because
8831 -- of the real danger that different operators may be applied in
8832 -- various parts of the program.
8834 -- Note that if E and S have the same scope, there is never any
8835 -- hiding. Either the two conflict, and the program is illegal,
8836 -- or S is overriding an implicit inherited subprogram.
8838 if Scope (E) /= Scope (S)
8839 and then (not Is_Overloadable (E)
8840 or else Subtype_Conformant (E, S))
8841 and then (Is_Immediately_Visible (E)
8842 or else Is_Potentially_Use_Visible (S))
8844 if Scope (E) = Standard_Standard then
8845 if Nkind (S) = N_Defining_Operator_Symbol
8846 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8848 and then Matches_Predefined_Op
8851 ("declaration of & hides predefined operator?h?", S);
8854 -- E not immediately within Standard
8857 Error_Msg_Sloc := Sloc (E);
8858 Error_Msg_N ("declaration of & hides one #?h?", S);
8863 end Enter_Overloaded_Entity;
8865 -----------------------------
8866 -- Check_Untagged_Equality --
8867 -----------------------------
8869 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8870 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8871 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8875 -- This check applies only if we have a subprogram declaration with an
8876 -- untagged record type that is conformant to the predefined op.
8878 if Nkind (Decl) /= N_Subprogram_Declaration
8879 or else not Is_Record_Type (Typ)
8880 or else Is_Tagged_Type (Typ)
8881 or else Etype (Next_Formal (First_Formal (Eq_Op))) /= Typ
8886 -- In Ada 2012 case, we will output errors or warnings depending on
8887 -- the setting of debug flag -gnatd.E.
8889 if Ada_Version >= Ada_2012 then
8890 Error_Msg_Warn := Debug_Flag_Dot_EE;
8892 -- In earlier versions of Ada, nothing to do unless we are warning on
8893 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8896 if not Warn_On_Ada_2012_Compatibility then
8901 -- Cases where the type has already been frozen
8903 if Is_Frozen (Typ) then
8905 -- The check applies to a primitive operation, so check that type
8906 -- and equality operation are in the same scope.
8908 if Scope (Typ) /= Current_Scope then
8911 -- If the type is a generic actual (sub)type, the operation is not
8912 -- primitive either because the base type is declared elsewhere.
8914 elsif Is_Generic_Actual_Type (Typ) then
8917 -- Here we have a definite error of declaration after freezing
8920 if Ada_Version >= Ada_2012 then
8922 ("equality operator must be declared before type & is "
8923 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8925 -- In Ada 2012 mode with error turned to warning, output one
8926 -- more warning to warn that the equality operation may not
8927 -- compose. This is the consequence of ignoring the error.
8929 if Error_Msg_Warn then
8930 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8935 ("equality operator must be declared before type& is "
8936 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8939 -- If we are in the package body, we could just move the
8940 -- declaration to the package spec, so add a message saying that.
8942 if In_Package_Body (Scope (Typ)) then
8943 if Ada_Version >= Ada_2012 then
8945 ("\move declaration to package spec<<", Eq_Op);
8948 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8951 -- Otherwise try to find the freezing point for better message.
8954 Obj_Decl := Next (Parent (Typ));
8955 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8956 if Nkind (Obj_Decl) = N_Object_Declaration
8957 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8959 -- Freezing point, output warnings
8961 if Ada_Version >= Ada_2012 then
8963 ("type& is frozen by declaration??", Obj_Decl, Typ);
8965 ("\an equality operator cannot be declared after "
8970 ("type& is frozen by declaration (Ada 2012)?y?",
8973 ("\an equality operator cannot be declared after "
8974 & "this point (Ada 2012)?y?",
8980 -- If we reach generated code for subprogram declaration
8981 -- or body, it is the body that froze the type and the
8982 -- declaration is legal.
8984 elsif Sloc (Obj_Decl) = Sloc (Decl) then
8993 -- Here if type is not frozen yet. It is illegal to have a primitive
8994 -- equality declared in the private part if the type is visible.
8996 elsif not In_Same_List (Parent (Typ), Decl)
8997 and then not Is_Limited_Type (Typ)
8999 -- Shouldn't we give an RM reference here???
9001 if Ada_Version >= Ada_2012 then
9003 ("equality operator appears too late<<", Eq_Op);
9006 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
9009 -- No error detected
9014 end Check_Untagged_Equality;
9016 -----------------------------
9017 -- Find_Corresponding_Spec --
9018 -----------------------------
9020 function Find_Corresponding_Spec
9022 Post_Error : Boolean := True) return Entity_Id
9024 Spec : constant Node_Id := Specification (N);
9025 Designator : constant Entity_Id := Defining_Entity (Spec);
9029 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9030 -- Even if fully conformant, a body may depend on a generic actual when
9031 -- the spec does not, or vice versa, in which case they were distinct
9032 -- entities in the generic.
9034 -------------------------------
9035 -- Different_Generic_Profile --
9036 -------------------------------
9038 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9041 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9042 -- Check that the types of corresponding formals have the same
9043 -- generic actual if any. We have to account for subtypes of a
9044 -- generic formal, declared between a spec and a body, which may
9045 -- appear distinct in an instance but matched in the generic, and
9046 -- the subtype may be used either in the spec or the body of the
9047 -- subprogram being checked.
9049 -------------------------
9050 -- Same_Generic_Actual --
9051 -------------------------
9053 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9055 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9056 -- Predicate to check whether S1 is a subtype of S2 in the source
9059 -------------------------
9060 -- Is_Declared_Subtype --
9061 -------------------------
9063 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9065 return Comes_From_Source (Parent (S1))
9066 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9067 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9068 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9069 end Is_Declared_Subtype;
9071 -- Start of processing for Same_Generic_Actual
9074 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9075 or else Is_Declared_Subtype (T1, T2)
9076 or else Is_Declared_Subtype (T2, T1);
9077 end Same_Generic_Actual;
9079 -- Start of processing for Different_Generic_Profile
9082 if not In_Instance then
9085 elsif Ekind (E) = E_Function
9086 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9091 F1 := First_Formal (Designator);
9092 F2 := First_Formal (E);
9093 while Present (F1) loop
9094 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9103 end Different_Generic_Profile;
9105 -- Start of processing for Find_Corresponding_Spec
9108 E := Current_Entity (Designator);
9109 while Present (E) loop
9111 -- We are looking for a matching spec. It must have the same scope,
9112 -- and the same name, and either be type conformant, or be the case
9113 -- of a library procedure spec and its body (which belong to one
9114 -- another regardless of whether they are type conformant or not).
9116 if Scope (E) = Current_Scope then
9117 if Current_Scope = Standard_Standard
9118 or else (Ekind (E) = Ekind (Designator)
9119 and then Type_Conformant (E, Designator))
9121 -- Within an instantiation, we know that spec and body are
9122 -- subtype conformant, because they were subtype conformant in
9123 -- the generic. We choose the subtype-conformant entity here as
9124 -- well, to resolve spurious ambiguities in the instance that
9125 -- were not present in the generic (i.e. when two different
9126 -- types are given the same actual). If we are looking for a
9127 -- spec to match a body, full conformance is expected.
9131 -- Inherit the convention and "ghostness" of the matching
9132 -- spec to ensure proper full and subtype conformance.
9134 Set_Convention (Designator, Convention (E));
9136 -- Skip past subprogram bodies and subprogram renamings that
9137 -- may appear to have a matching spec, but that aren't fully
9138 -- conformant with it. That can occur in cases where an
9139 -- actual type causes unrelated homographs in the instance.
9141 if Nkind_In (N, N_Subprogram_Body,
9142 N_Subprogram_Renaming_Declaration)
9143 and then Present (Homonym (E))
9144 and then not Fully_Conformant (Designator, E)
9148 elsif not Subtype_Conformant (Designator, E) then
9151 elsif Different_Generic_Profile (E) then
9156 -- Ada 2012 (AI05-0165): For internally generated bodies of
9157 -- null procedures locate the internally generated spec. We
9158 -- enforce mode conformance since a tagged type may inherit
9159 -- from interfaces several null primitives which differ only
9160 -- in the mode of the formals.
9162 if not (Comes_From_Source (E))
9163 and then Is_Null_Procedure (E)
9164 and then not Mode_Conformant (Designator, E)
9168 -- For null procedures coming from source that are completions,
9169 -- analysis of the generated body will establish the link.
9171 elsif Comes_From_Source (E)
9172 and then Nkind (Spec) = N_Procedure_Specification
9173 and then Null_Present (Spec)
9177 -- Expression functions can be completions, but cannot be
9178 -- completed by an explicit body.
9180 elsif Comes_From_Source (E)
9181 and then Comes_From_Source (N)
9182 and then Nkind (N) = N_Subprogram_Body
9183 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9184 N_Expression_Function
9186 Error_Msg_Sloc := Sloc (E);
9187 Error_Msg_N ("body conflicts with expression function#", N);
9190 elsif not Has_Completion (E) then
9191 if Nkind (N) /= N_Subprogram_Body_Stub then
9192 Set_Corresponding_Spec (N, E);
9195 Set_Has_Completion (E);
9198 elsif Nkind (Parent (N)) = N_Subunit then
9200 -- If this is the proper body of a subunit, the completion
9201 -- flag is set when analyzing the stub.
9205 -- If E is an internal function with a controlling result that
9206 -- was created for an operation inherited by a null extension,
9207 -- it may be overridden by a body without a previous spec (one
9208 -- more reason why these should be shunned). In that case we
9209 -- remove the generated body if present, because the current
9210 -- one is the explicit overriding.
9212 elsif Ekind (E) = E_Function
9213 and then Ada_Version >= Ada_2005
9214 and then not Comes_From_Source (E)
9215 and then Has_Controlling_Result (E)
9216 and then Is_Null_Extension (Etype (E))
9217 and then Comes_From_Source (Spec)
9219 Set_Has_Completion (E, False);
9222 and then Nkind (Parent (E)) = N_Function_Specification
9225 (Unit_Declaration_Node
9226 (Corresponding_Body (Unit_Declaration_Node (E))));
9230 -- If expansion is disabled, or if the wrapper function has
9231 -- not been generated yet, this a late body overriding an
9232 -- inherited operation, or it is an overriding by some other
9233 -- declaration before the controlling result is frozen. In
9234 -- either case this is a declaration of a new entity.
9240 -- If the body already exists, then this is an error unless
9241 -- the previous declaration is the implicit declaration of a
9242 -- derived subprogram. It is also legal for an instance to
9243 -- contain type conformant overloadable declarations (but the
9244 -- generic declaration may not), per 8.3(26/2).
9246 elsif No (Alias (E))
9247 and then not Is_Intrinsic_Subprogram (E)
9248 and then not In_Instance
9251 Error_Msg_Sloc := Sloc (E);
9253 if Is_Imported (E) then
9255 ("body not allowed for imported subprogram & declared#",
9258 Error_Msg_NE ("duplicate body for & declared#", N, E);
9262 -- Child units cannot be overloaded, so a conformance mismatch
9263 -- between body and a previous spec is an error.
9265 elsif Is_Child_Unit (E)
9267 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9269 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9274 ("body of child unit does not match previous declaration", N);
9282 -- On exit, we know that no previous declaration of subprogram exists
9285 end Find_Corresponding_Spec;
9287 ----------------------
9288 -- Fully_Conformant --
9289 ----------------------
9291 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9294 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9296 end Fully_Conformant;
9298 ----------------------------------
9299 -- Fully_Conformant_Expressions --
9300 ----------------------------------
9302 function Fully_Conformant_Expressions
9303 (Given_E1 : Node_Id;
9305 Report : Boolean := False) return Boolean
9307 E1 : constant Node_Id := Original_Node (Given_E1);
9308 E2 : constant Node_Id := Original_Node (Given_E2);
9309 -- We always test conformance on original nodes, since it is possible
9310 -- for analysis and/or expansion to make things look as though they
9311 -- conform when they do not, e.g. by converting 1+2 into 3.
9313 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9316 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9317 -- Compare elements of two lists for conformance. Elements have to be
9318 -- conformant, and actuals inserted as default parameters do not match
9319 -- explicit actuals with the same value.
9321 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9322 -- Compare an operator node with a function call
9328 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
9330 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
9337 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
9342 if L1 = No_List then
9348 if L2 = No_List then
9354 -- Compare two lists, skipping rewrite insertions (we want to compare
9355 -- the original trees, not the expanded versions).
9358 if Is_Rewrite_Insertion (N1) then
9360 elsif Is_Rewrite_Insertion (N2) then
9366 elsif not FCE (N1, N2) then
9379 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
9380 Actuals : constant List_Id := Parameter_Associations (Call_Node);
9385 or else Entity (Op_Node) /= Entity (Name (Call_Node))
9390 Act := First (Actuals);
9392 if Nkind (Op_Node) in N_Binary_Op then
9393 if not FCE (Left_Opnd (Op_Node), Act) then
9400 return Present (Act)
9401 and then FCE (Right_Opnd (Op_Node), Act)
9402 and then No (Next (Act));
9410 -- Start of processing for Fully_Conformant_Expressions
9415 -- Nonconformant if paren count does not match. Note: if some idiot
9416 -- complains that we don't do this right for more than 3 levels of
9417 -- parentheses, they will be treated with the respect they deserve.
9419 if Paren_Count (E1) /= Paren_Count (E2) then
9422 -- If same entities are referenced, then they are conformant even if
9423 -- they have different forms (RM 8.3.1(19-20)).
9425 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9426 if Present (Entity (E1)) then
9427 Result := Entity (E1) = Entity (E2)
9429 -- One may be a discriminant that has been replaced by the
9430 -- corresponding discriminal.
9433 (Chars (Entity (E1)) = Chars (Entity (E2))
9434 and then Ekind (Entity (E1)) = E_Discriminant
9435 and then Ekind (Entity (E2)) = E_In_Parameter)
9437 -- The discriminant of a protected type is transformed into
9438 -- a local constant and then into a parameter of a protected
9442 (Ekind (Entity (E1)) = E_Constant
9443 and then Ekind (Entity (E2)) = E_In_Parameter
9444 and then Present (Discriminal_Link (Entity (E1)))
9445 and then Discriminal_Link (Entity (E1)) =
9446 Discriminal_Link (Entity (E2)))
9448 -- AI12-050: The loop variables of quantified expressions match
9449 -- if they have the same identifier, even though they may have
9450 -- different entities.
9453 (Chars (Entity (E1)) = Chars (Entity (E2))
9454 and then Ekind (Entity (E1)) = E_Loop_Parameter
9455 and then Ekind (Entity (E2)) = E_Loop_Parameter)
9457 -- A call to an instantiation of Unchecked_Conversion is
9458 -- rewritten with the name of the generated function created for
9459 -- the instance, and this must be special-cased.
9462 (Ekind (Entity (E1)) = E_Function
9463 and then Is_Intrinsic_Subprogram (Entity (E1))
9464 and then Is_Generic_Instance (Entity (E1))
9465 and then Entity (E2) = Alias (Entity (E1)));
9466 if Report and not Result then
9468 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
9470 ("Meaning of& differs because of declaration#", E1, E2);
9475 elsif Nkind (E1) = N_Expanded_Name
9476 and then Nkind (E2) = N_Expanded_Name
9477 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9478 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9480 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9483 -- Identifiers in component associations don't always have
9484 -- entities, but their names must conform.
9486 return Nkind (E1) = N_Identifier
9487 and then Nkind (E2) = N_Identifier
9488 and then Chars (E1) = Chars (E2);
9491 elsif Nkind (E1) = N_Character_Literal
9492 and then Nkind (E2) = N_Expanded_Name
9494 return Nkind (Selector_Name (E2)) = N_Character_Literal
9495 and then Chars (E1) = Chars (Selector_Name (E2));
9497 elsif Nkind (E2) = N_Character_Literal
9498 and then Nkind (E1) = N_Expanded_Name
9500 return Nkind (Selector_Name (E1)) = N_Character_Literal
9501 and then Chars (E2) = Chars (Selector_Name (E1));
9503 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9504 return FCO (E1, E2);
9506 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9507 return FCO (E2, E1);
9509 -- Otherwise we must have the same syntactic entity
9511 elsif Nkind (E1) /= Nkind (E2) then
9514 -- At this point, we specialize by node type
9520 FCL (Expressions (E1), Expressions (E2))
9522 FCL (Component_Associations (E1),
9523 Component_Associations (E2));
9526 if Nkind (Expression (E1)) = N_Qualified_Expression
9528 Nkind (Expression (E2)) = N_Qualified_Expression
9530 return FCE (Expression (E1), Expression (E2));
9532 -- Check that the subtype marks and any constraints
9537 Indic1 : constant Node_Id := Expression (E1);
9538 Indic2 : constant Node_Id := Expression (E2);
9543 if Nkind (Indic1) /= N_Subtype_Indication then
9545 Nkind (Indic2) /= N_Subtype_Indication
9546 and then Entity (Indic1) = Entity (Indic2);
9548 elsif Nkind (Indic2) /= N_Subtype_Indication then
9550 Nkind (Indic1) /= N_Subtype_Indication
9551 and then Entity (Indic1) = Entity (Indic2);
9554 if Entity (Subtype_Mark (Indic1)) /=
9555 Entity (Subtype_Mark (Indic2))
9560 Elt1 := First (Constraints (Constraint (Indic1)));
9561 Elt2 := First (Constraints (Constraint (Indic2)));
9562 while Present (Elt1) and then Present (Elt2) loop
9563 if not FCE (Elt1, Elt2) then
9576 when N_Attribute_Reference =>
9578 Attribute_Name (E1) = Attribute_Name (E2)
9579 and then FCL (Expressions (E1), Expressions (E2));
9583 Entity (E1) = Entity (E2)
9584 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9585 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9587 when N_Membership_Test
9591 FCE (Left_Opnd (E1), Left_Opnd (E2))
9593 FCE (Right_Opnd (E1), Right_Opnd (E2));
9595 when N_Case_Expression =>
9601 if not FCE (Expression (E1), Expression (E2)) then
9605 Alt1 := First (Alternatives (E1));
9606 Alt2 := First (Alternatives (E2));
9608 if Present (Alt1) /= Present (Alt2) then
9610 elsif No (Alt1) then
9614 if not FCE (Expression (Alt1), Expression (Alt2))
9615 or else not FCL (Discrete_Choices (Alt1),
9616 Discrete_Choices (Alt2))
9627 when N_Character_Literal =>
9629 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9631 when N_Component_Association =>
9633 FCL (Choices (E1), Choices (E2))
9635 FCE (Expression (E1), Expression (E2));
9637 when N_Explicit_Dereference =>
9639 FCE (Prefix (E1), Prefix (E2));
9641 when N_Extension_Aggregate =>
9643 FCL (Expressions (E1), Expressions (E2))
9644 and then Null_Record_Present (E1) =
9645 Null_Record_Present (E2)
9646 and then FCL (Component_Associations (E1),
9647 Component_Associations (E2));
9649 when N_Function_Call =>
9651 FCE (Name (E1), Name (E2))
9653 FCL (Parameter_Associations (E1),
9654 Parameter_Associations (E2));
9656 when N_If_Expression =>
9658 FCL (Expressions (E1), Expressions (E2));
9660 when N_Indexed_Component =>
9662 FCE (Prefix (E1), Prefix (E2))
9664 FCL (Expressions (E1), Expressions (E2));
9666 when N_Integer_Literal =>
9667 return (Intval (E1) = Intval (E2));
9672 when N_Operator_Symbol =>
9674 Chars (E1) = Chars (E2);
9676 when N_Others_Choice =>
9679 when N_Parameter_Association =>
9681 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9682 and then FCE (Explicit_Actual_Parameter (E1),
9683 Explicit_Actual_Parameter (E2));
9685 when N_Qualified_Expression
9687 | N_Unchecked_Type_Conversion
9690 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9692 FCE (Expression (E1), Expression (E2));
9694 when N_Quantified_Expression =>
9695 if not FCE (Condition (E1), Condition (E2)) then
9699 if Present (Loop_Parameter_Specification (E1))
9700 and then Present (Loop_Parameter_Specification (E2))
9703 L1 : constant Node_Id :=
9704 Loop_Parameter_Specification (E1);
9705 L2 : constant Node_Id :=
9706 Loop_Parameter_Specification (E2);
9710 Reverse_Present (L1) = Reverse_Present (L2)
9712 FCE (Defining_Identifier (L1),
9713 Defining_Identifier (L2))
9715 FCE (Discrete_Subtype_Definition (L1),
9716 Discrete_Subtype_Definition (L2));
9719 elsif Present (Iterator_Specification (E1))
9720 and then Present (Iterator_Specification (E2))
9723 I1 : constant Node_Id := Iterator_Specification (E1);
9724 I2 : constant Node_Id := Iterator_Specification (E2);
9728 FCE (Defining_Identifier (I1),
9729 Defining_Identifier (I2))
9731 Of_Present (I1) = Of_Present (I2)
9733 Reverse_Present (I1) = Reverse_Present (I2)
9734 and then FCE (Name (I1), Name (I2))
9735 and then FCE (Subtype_Indication (I1),
9736 Subtype_Indication (I2));
9739 -- The quantified expressions used different specifications to
9740 -- walk their respective ranges.
9748 FCE (Low_Bound (E1), Low_Bound (E2))
9750 FCE (High_Bound (E1), High_Bound (E2));
9752 when N_Real_Literal =>
9753 return (Realval (E1) = Realval (E2));
9755 when N_Selected_Component =>
9757 FCE (Prefix (E1), Prefix (E2))
9759 FCE (Selector_Name (E1), Selector_Name (E2));
9763 FCE (Prefix (E1), Prefix (E2))
9765 FCE (Discrete_Range (E1), Discrete_Range (E2));
9767 when N_String_Literal =>
9769 S1 : constant String_Id := Strval (E1);
9770 S2 : constant String_Id := Strval (E2);
9771 L1 : constant Nat := String_Length (S1);
9772 L2 : constant Nat := String_Length (S2);
9779 for J in 1 .. L1 loop
9780 if Get_String_Char (S1, J) /=
9781 Get_String_Char (S2, J)
9793 Entity (E1) = Entity (E2)
9795 FCE (Right_Opnd (E1), Right_Opnd (E2));
9797 -- All other node types cannot appear in this context. Strictly
9798 -- we should raise a fatal internal error. Instead we just ignore
9799 -- the nodes. This means that if anyone makes a mistake in the
9800 -- expander and mucks an expression tree irretrievably, the result
9801 -- will be a failure to detect a (probably very obscure) case
9802 -- of non-conformance, which is better than bombing on some
9803 -- case where two expressions do in fact conform.
9809 end Fully_Conformant_Expressions;
9811 ----------------------------------------
9812 -- Fully_Conformant_Discrete_Subtypes --
9813 ----------------------------------------
9815 function Fully_Conformant_Discrete_Subtypes
9816 (Given_S1 : Node_Id;
9817 Given_S2 : Node_Id) return Boolean
9819 S1 : constant Node_Id := Original_Node (Given_S1);
9820 S2 : constant Node_Id := Original_Node (Given_S2);
9822 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9823 -- Special-case for a bound given by a discriminant, which in the body
9824 -- is replaced with the discriminal of the enclosing type.
9826 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9827 -- Check both bounds
9829 -----------------------
9830 -- Conforming_Bounds --
9831 -----------------------
9833 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9835 if Is_Entity_Name (B1)
9836 and then Is_Entity_Name (B2)
9837 and then Ekind (Entity (B1)) = E_Discriminant
9839 return Chars (B1) = Chars (B2);
9842 return Fully_Conformant_Expressions (B1, B2);
9844 end Conforming_Bounds;
9846 -----------------------
9847 -- Conforming_Ranges --
9848 -----------------------
9850 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9853 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9855 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9856 end Conforming_Ranges;
9858 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9861 if Nkind (S1) /= Nkind (S2) then
9864 elsif Is_Entity_Name (S1) then
9865 return Entity (S1) = Entity (S2);
9867 elsif Nkind (S1) = N_Range then
9868 return Conforming_Ranges (S1, S2);
9870 elsif Nkind (S1) = N_Subtype_Indication then
9872 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9875 (Range_Expression (Constraint (S1)),
9876 Range_Expression (Constraint (S2)));
9880 end Fully_Conformant_Discrete_Subtypes;
9882 --------------------
9883 -- Install_Entity --
9884 --------------------
9886 procedure Install_Entity (E : Entity_Id) is
9887 Prev : constant Entity_Id := Current_Entity (E);
9889 Set_Is_Immediately_Visible (E);
9890 Set_Current_Entity (E);
9891 Set_Homonym (E, Prev);
9894 ---------------------
9895 -- Install_Formals --
9896 ---------------------
9898 procedure Install_Formals (Id : Entity_Id) is
9901 F := First_Formal (Id);
9902 while Present (F) loop
9906 end Install_Formals;
9908 -----------------------------
9909 -- Is_Interface_Conformant --
9910 -----------------------------
9912 function Is_Interface_Conformant
9913 (Tagged_Type : Entity_Id;
9914 Iface_Prim : Entity_Id;
9915 Prim : Entity_Id) return Boolean
9917 -- The operation may in fact be an inherited (implicit) operation
9918 -- rather than the original interface primitive, so retrieve the
9919 -- ultimate ancestor.
9921 Iface : constant Entity_Id :=
9922 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9923 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9925 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9926 -- Return the controlling formal of Prim
9928 ------------------------
9929 -- Controlling_Formal --
9930 ------------------------
9932 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9936 E := First_Entity (Prim);
9937 while Present (E) loop
9938 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9946 end Controlling_Formal;
9950 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9951 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9953 -- Start of processing for Is_Interface_Conformant
9956 pragma Assert (Is_Subprogram (Iface_Prim)
9957 and then Is_Subprogram (Prim)
9958 and then Is_Dispatching_Operation (Iface_Prim)
9959 and then Is_Dispatching_Operation (Prim));
9961 pragma Assert (Is_Interface (Iface)
9962 or else (Present (Alias (Iface_Prim))
9965 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9967 if Prim = Iface_Prim
9968 or else not Is_Subprogram (Prim)
9969 or else Ekind (Prim) /= Ekind (Iface_Prim)
9970 or else not Is_Dispatching_Operation (Prim)
9971 or else Scope (Prim) /= Scope (Tagged_Type)
9973 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9974 or else not Primitive_Names_Match (Iface_Prim, Prim)
9978 -- The mode of the controlling formals must match
9980 elsif Present (Iface_Ctrl_F)
9981 and then Present (Prim_Ctrl_F)
9982 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9986 -- Case of a procedure, or a function whose result type matches the
9987 -- result type of the interface primitive, or a function that has no
9988 -- controlling result (I or access I).
9990 elsif Ekind (Iface_Prim) = E_Procedure
9991 or else Etype (Prim) = Etype (Iface_Prim)
9992 or else not Has_Controlling_Result (Prim)
9994 return Type_Conformant
9995 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9997 -- Case of a function returning an interface, or an access to one. Check
9998 -- that the return types correspond.
10000 elsif Implements_Interface (Typ, Iface) then
10001 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10003 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10008 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10009 Skip_Controlling_Formals => True);
10015 end Is_Interface_Conformant;
10017 ---------------------------------
10018 -- Is_Non_Overriding_Operation --
10019 ---------------------------------
10021 function Is_Non_Overriding_Operation
10022 (Prev_E : Entity_Id;
10023 New_E : Entity_Id) return Boolean
10025 Formal : Entity_Id;
10027 G_Typ : Entity_Id := Empty;
10029 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10030 -- If F_Type is a derived type associated with a generic actual subtype,
10031 -- then return its Generic_Parent_Type attribute, else return Empty.
10033 function Types_Correspond
10034 (P_Type : Entity_Id;
10035 N_Type : Entity_Id) return Boolean;
10036 -- Returns true if and only if the types (or designated types in the
10037 -- case of anonymous access types) are the same or N_Type is derived
10038 -- directly or indirectly from P_Type.
10040 -----------------------------
10041 -- Get_Generic_Parent_Type --
10042 -----------------------------
10044 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10050 if Is_Derived_Type (F_Typ)
10051 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10053 -- The tree must be traversed to determine the parent subtype in
10054 -- the generic unit, which unfortunately isn't always available
10055 -- via semantic attributes. ??? (Note: The use of Original_Node
10056 -- is needed for cases where a full derived type has been
10059 -- If the parent type is a scalar type, the derivation creates
10060 -- an anonymous base type for it, and the source type is its
10063 if Is_Scalar_Type (F_Typ)
10064 and then not Comes_From_Source (F_Typ)
10068 (Original_Node (Parent (First_Subtype (F_Typ))));
10070 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10072 if Nkind (Defn) = N_Derived_Type_Definition then
10073 Indic := Subtype_Indication (Defn);
10075 if Nkind (Indic) = N_Subtype_Indication then
10076 G_Typ := Entity (Subtype_Mark (Indic));
10078 G_Typ := Entity (Indic);
10081 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10082 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10084 return Generic_Parent_Type (Parent (G_Typ));
10090 end Get_Generic_Parent_Type;
10092 ----------------------
10093 -- Types_Correspond --
10094 ----------------------
10096 function Types_Correspond
10097 (P_Type : Entity_Id;
10098 N_Type : Entity_Id) return Boolean
10100 Prev_Type : Entity_Id := Base_Type (P_Type);
10101 New_Type : Entity_Id := Base_Type (N_Type);
10104 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10105 Prev_Type := Designated_Type (Prev_Type);
10108 if Ekind (New_Type) = E_Anonymous_Access_Type then
10109 New_Type := Designated_Type (New_Type);
10112 if Prev_Type = New_Type then
10115 elsif not Is_Class_Wide_Type (New_Type) then
10116 while Etype (New_Type) /= New_Type loop
10117 New_Type := Etype (New_Type);
10119 if New_Type = Prev_Type then
10125 end Types_Correspond;
10127 -- Start of processing for Is_Non_Overriding_Operation
10130 -- In the case where both operations are implicit derived subprograms
10131 -- then neither overrides the other. This can only occur in certain
10132 -- obscure cases (e.g., derivation from homographs created in a generic
10135 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10138 elsif Ekind (Current_Scope) = E_Package
10139 and then Is_Generic_Instance (Current_Scope)
10140 and then In_Private_Part (Current_Scope)
10141 and then Comes_From_Source (New_E)
10143 -- We examine the formals and result type of the inherited operation,
10144 -- to determine whether their type is derived from (the instance of)
10145 -- a generic type. The first such formal or result type is the one
10148 Formal := First_Formal (Prev_E);
10150 while Present (Formal) loop
10151 F_Typ := Base_Type (Etype (Formal));
10153 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10154 F_Typ := Designated_Type (F_Typ);
10157 G_Typ := Get_Generic_Parent_Type (F_Typ);
10158 exit when Present (G_Typ);
10160 Next_Formal (Formal);
10163 -- If the function dispatches on result check the result type
10165 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10166 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10173 -- If the generic type is a private type, then the original operation
10174 -- was not overriding in the generic, because there was no primitive
10175 -- operation to override.
10177 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10178 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10179 N_Formal_Private_Type_Definition
10183 -- The generic parent type is the ancestor of a formal derived
10184 -- type declaration. We need to check whether it has a primitive
10185 -- operation that should be overridden by New_E in the generic.
10189 P_Formal : Entity_Id;
10190 N_Formal : Entity_Id;
10193 P_Prim : Entity_Id;
10194 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10197 while Present (Prim_Elt) loop
10198 P_Prim := Node (Prim_Elt);
10200 if Chars (P_Prim) = Chars (New_E)
10201 and then Ekind (P_Prim) = Ekind (New_E)
10203 P_Formal := First_Formal (P_Prim);
10204 N_Formal := First_Formal (New_E);
10205 while Present (P_Formal) and then Present (N_Formal) loop
10206 P_Typ := Etype (P_Formal);
10207 N_Typ := Etype (N_Formal);
10209 if not Types_Correspond (P_Typ, N_Typ) then
10213 Next_Entity (P_Formal);
10214 Next_Entity (N_Formal);
10217 -- Found a matching primitive operation belonging to the
10218 -- formal ancestor type, so the new subprogram is
10222 and then No (N_Formal)
10223 and then (Ekind (New_E) /= E_Function
10226 (Etype (P_Prim), Etype (New_E)))
10232 Next_Elmt (Prim_Elt);
10235 -- If no match found, then the new subprogram does not override
10236 -- in the generic (nor in the instance).
10238 -- If the type in question is not abstract, and the subprogram
10239 -- is, this will be an error if the new operation is in the
10240 -- private part of the instance. Emit a warning now, which will
10241 -- make the subsequent error message easier to understand.
10243 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
10244 and then Is_Abstract_Subprogram (Prev_E)
10245 and then In_Private_Part (Current_Scope)
10247 Error_Msg_Node_2 := F_Typ;
10249 ("private operation& in generic unit does not override "
10250 & "any primitive operation of& (RM 12.3 (18))??",
10260 end Is_Non_Overriding_Operation;
10262 -------------------------------------
10263 -- List_Inherited_Pre_Post_Aspects --
10264 -------------------------------------
10266 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
10268 if Opt.List_Inherited_Aspects
10269 and then Is_Subprogram_Or_Generic_Subprogram (E)
10272 Subps : constant Subprogram_List := Inherited_Subprograms (E);
10277 for Index in Subps'Range loop
10278 Items := Contract (Subps (Index));
10280 if Present (Items) then
10281 Prag := Pre_Post_Conditions (Items);
10282 while Present (Prag) loop
10283 Error_Msg_Sloc := Sloc (Prag);
10285 if Class_Present (Prag)
10286 and then not Split_PPC (Prag)
10288 if Pragma_Name (Prag) = Name_Precondition then
10290 ("info: & inherits `Pre''Class` aspect from "
10294 ("info: & inherits `Post''Class` aspect from "
10299 Prag := Next_Pragma (Prag);
10305 end List_Inherited_Pre_Post_Aspects;
10307 ------------------------------
10308 -- Make_Inequality_Operator --
10309 ------------------------------
10311 -- S is the defining identifier of an equality operator. We build a
10312 -- subprogram declaration with the right signature. This operation is
10313 -- intrinsic, because it is always expanded as the negation of the
10314 -- call to the equality function.
10316 procedure Make_Inequality_Operator (S : Entity_Id) is
10317 Loc : constant Source_Ptr := Sloc (S);
10320 Op_Name : Entity_Id;
10322 FF : constant Entity_Id := First_Formal (S);
10323 NF : constant Entity_Id := Next_Formal (FF);
10326 -- Check that equality was properly defined, ignore call if not
10333 A : constant Entity_Id :=
10334 Make_Defining_Identifier (Sloc (FF),
10335 Chars => Chars (FF));
10337 B : constant Entity_Id :=
10338 Make_Defining_Identifier (Sloc (NF),
10339 Chars => Chars (NF));
10342 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
10344 Formals := New_List (
10345 Make_Parameter_Specification (Loc,
10346 Defining_Identifier => A,
10348 New_Occurrence_Of (Etype (First_Formal (S)),
10349 Sloc (Etype (First_Formal (S))))),
10351 Make_Parameter_Specification (Loc,
10352 Defining_Identifier => B,
10354 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
10355 Sloc (Etype (Next_Formal (First_Formal (S)))))));
10358 Make_Subprogram_Declaration (Loc,
10360 Make_Function_Specification (Loc,
10361 Defining_Unit_Name => Op_Name,
10362 Parameter_Specifications => Formals,
10363 Result_Definition =>
10364 New_Occurrence_Of (Standard_Boolean, Loc)));
10366 -- Insert inequality right after equality if it is explicit or after
10367 -- the derived type when implicit. These entities are created only
10368 -- for visibility purposes, and eventually replaced in the course
10369 -- of expansion, so they do not need to be attached to the tree and
10370 -- seen by the back-end. Keeping them internal also avoids spurious
10371 -- freezing problems. The declaration is inserted in the tree for
10372 -- analysis, and removed afterwards. If the equality operator comes
10373 -- from an explicit declaration, attach the inequality immediately
10374 -- after. Else the equality is inherited from a derived type
10375 -- declaration, so insert inequality after that declaration.
10377 if No (Alias (S)) then
10378 Insert_After (Unit_Declaration_Node (S), Decl);
10379 elsif Is_List_Member (Parent (S)) then
10380 Insert_After (Parent (S), Decl);
10382 Insert_After (Parent (Etype (First_Formal (S))), Decl);
10385 Mark_Rewrite_Insertion (Decl);
10386 Set_Is_Intrinsic_Subprogram (Op_Name);
10389 Set_Has_Completion (Op_Name);
10390 Set_Corresponding_Equality (Op_Name, S);
10391 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
10393 end Make_Inequality_Operator;
10395 ----------------------
10396 -- May_Need_Actuals --
10397 ----------------------
10399 procedure May_Need_Actuals (Fun : Entity_Id) is
10404 F := First_Formal (Fun);
10406 while Present (F) loop
10407 if No (Default_Value (F)) then
10415 Set_Needs_No_Actuals (Fun, B);
10416 end May_Need_Actuals;
10418 ---------------------
10419 -- Mode_Conformant --
10420 ---------------------
10422 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10425 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
10427 end Mode_Conformant;
10429 ---------------------------
10430 -- New_Overloaded_Entity --
10431 ---------------------------
10433 procedure New_Overloaded_Entity
10435 Derived_Type : Entity_Id := Empty)
10437 Overridden_Subp : Entity_Id := Empty;
10438 -- Set if the current scope has an operation that is type-conformant
10439 -- with S, and becomes hidden by S.
10441 Is_Primitive_Subp : Boolean;
10442 -- Set to True if the new subprogram is primitive
10445 -- Entity that S overrides
10447 procedure Check_For_Primitive_Subprogram
10448 (Is_Primitive : out Boolean;
10449 Is_Overriding : Boolean := False);
10450 -- If the subprogram being analyzed is a primitive operation of the type
10451 -- of a formal or result, set the Has_Primitive_Operations flag on the
10452 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10453 -- corresponding flag on the entity itself for later use.
10455 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10456 -- True if a) E is a subprogram whose first formal is a concurrent type
10457 -- defined in the scope of E that has some entry or subprogram whose
10458 -- profile matches E, or b) E is an internally built dispatching
10459 -- subprogram of a protected type and there is a matching subprogram
10460 -- defined in the enclosing scope of the protected type, or c) E is
10461 -- an entry of a synchronized type and a matching procedure has been
10462 -- previously defined in the enclosing scope of the synchronized type.
10464 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10465 -- Check that E is declared in the private part of the current package,
10466 -- or in the package body, where it may hide a previous declaration.
10467 -- We can't use In_Private_Part by itself because this flag is also
10468 -- set when freezing entities, so we must examine the place of the
10469 -- declaration in the tree, and recognize wrapper packages as well.
10471 function Is_Overriding_Alias
10472 (Old_E : Entity_Id;
10473 New_E : Entity_Id) return Boolean;
10474 -- Check whether new subprogram and old subprogram are both inherited
10475 -- from subprograms that have distinct dispatch table entries. This can
10476 -- occur with derivations from instances with accidental homonyms. The
10477 -- function is conservative given that the converse is only true within
10478 -- instances that contain accidental overloadings.
10480 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10481 -- Report conflict between entities S and E
10483 ------------------------------------
10484 -- Check_For_Primitive_Subprogram --
10485 ------------------------------------
10487 procedure Check_For_Primitive_Subprogram
10488 (Is_Primitive : out Boolean;
10489 Is_Overriding : Boolean := False)
10491 Formal : Entity_Id;
10495 function Visible_Part_Type (T : Entity_Id) return Boolean;
10496 -- Returns true if T is declared in the visible part of the current
10497 -- package scope; otherwise returns false. Assumes that T is declared
10500 procedure Check_Private_Overriding (T : Entity_Id);
10501 -- Checks that if a primitive abstract subprogram of a visible
10502 -- abstract type is declared in a private part, then it must override
10503 -- an abstract subprogram declared in the visible part. Also checks
10504 -- that if a primitive function with a controlling result is declared
10505 -- in a private part, then it must override a function declared in
10506 -- the visible part.
10508 ------------------------------
10509 -- Check_Private_Overriding --
10510 ------------------------------
10512 procedure Check_Private_Overriding (T : Entity_Id) is
10513 function Overrides_Private_Part_Op return Boolean;
10514 -- This detects the special case where the overriding subprogram
10515 -- is overriding a subprogram that was declared in the same
10516 -- private part. That case is illegal by 3.9.3(10).
10518 function Overrides_Visible_Function
10519 (Partial_View : Entity_Id) return Boolean;
10520 -- True if S overrides a function in the visible part. The
10521 -- overridden function could be explicitly or implicitly declared.
10523 -------------------------------
10524 -- Overrides_Private_Part_Op --
10525 -------------------------------
10527 function Overrides_Private_Part_Op return Boolean is
10528 Over_Decl : constant Node_Id :=
10529 Unit_Declaration_Node (Overridden_Operation (S));
10530 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
10533 pragma Assert (Is_Overriding);
10535 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
10537 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
10539 return In_Same_List (Over_Decl, Subp_Decl);
10540 end Overrides_Private_Part_Op;
10542 --------------------------------
10543 -- Overrides_Visible_Function --
10544 --------------------------------
10546 function Overrides_Visible_Function
10547 (Partial_View : Entity_Id) return Boolean
10550 if not Is_Overriding or else not Has_Homonym (S) then
10554 if not Present (Partial_View) then
10558 -- Search through all the homonyms H of S in the current
10559 -- package spec, and return True if we find one that matches.
10560 -- Note that Parent (H) will be the declaration of the
10561 -- partial view of T for a match.
10564 H : Entity_Id := S;
10568 exit when not Present (H) or else Scope (H) /= Scope (S);
10572 N_Private_Extension_Declaration,
10573 N_Private_Type_Declaration)
10574 and then Defining_Identifier (Parent (H)) = Partial_View
10582 end Overrides_Visible_Function;
10584 -- Start of processing for Check_Private_Overriding
10587 if Is_Package_Or_Generic_Package (Current_Scope)
10588 and then In_Private_Part (Current_Scope)
10589 and then Visible_Part_Type (T)
10590 and then not In_Instance
10592 if Is_Abstract_Type (T)
10593 and then Is_Abstract_Subprogram (S)
10594 and then (not Is_Overriding
10595 or else not Is_Abstract_Subprogram (E)
10596 or else Overrides_Private_Part_Op)
10599 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10602 elsif Ekind (S) = E_Function then
10604 Partial_View : constant Entity_Id :=
10605 Incomplete_Or_Partial_View (T);
10608 if not Overrides_Visible_Function (Partial_View) then
10610 -- Here, S is "function ... return T;" declared in
10611 -- the private part, not overriding some visible
10612 -- operation. That's illegal in the tagged case
10613 -- (but not if the private type is untagged).
10615 if ((Present (Partial_View)
10616 and then Is_Tagged_Type (Partial_View))
10617 or else (not Present (Partial_View)
10618 and then Is_Tagged_Type (T)))
10619 and then T = Base_Type (Etype (S))
10622 ("private function with tagged result must"
10623 & " override visible-part function", S);
10625 ("\move subprogram to the visible part"
10626 & " (RM 3.9.3(10))", S);
10628 -- Ada 2012 (AI05-0073): Extend this check to the case
10629 -- of a function whose result subtype is defined by an
10630 -- access_definition designating specific tagged type.
10632 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10633 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10635 not Is_Class_Wide_Type
10636 (Designated_Type (Etype (S)))
10637 and then Ada_Version >= Ada_2012
10640 ("private function with controlling access "
10641 & "result must override visible-part function",
10644 ("\move subprogram to the visible part"
10645 & " (RM 3.9.3(10))", S);
10651 end Check_Private_Overriding;
10653 -----------------------
10654 -- Visible_Part_Type --
10655 -----------------------
10657 function Visible_Part_Type (T : Entity_Id) return Boolean is
10658 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10661 -- If the entity is a private type, then it must be declared in a
10664 if Ekind (T) in Private_Kind then
10667 elsif Is_Type (T) and then Has_Private_Declaration (T) then
10670 elsif Is_List_Member (Declaration_Node (T))
10671 and then List_Containing (Declaration_Node (T)) =
10672 Visible_Declarations (Specification (P))
10679 end Visible_Part_Type;
10681 -- Start of processing for Check_For_Primitive_Subprogram
10684 Is_Primitive := False;
10686 if not Comes_From_Source (S) then
10689 -- If subprogram is at library level, it is not primitive operation
10691 elsif Current_Scope = Standard_Standard then
10694 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10695 and then not In_Package_Body (Current_Scope))
10696 or else Is_Overriding
10698 -- For function, check return type
10700 if Ekind (S) = E_Function then
10701 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10702 F_Typ := Designated_Type (Etype (S));
10704 F_Typ := Etype (S);
10707 B_Typ := Base_Type (F_Typ);
10709 if Scope (B_Typ) = Current_Scope
10710 and then not Is_Class_Wide_Type (B_Typ)
10711 and then not Is_Generic_Type (B_Typ)
10713 Is_Primitive := True;
10714 Set_Has_Primitive_Operations (B_Typ);
10715 Set_Is_Primitive (S);
10716 Check_Private_Overriding (B_Typ);
10718 -- The Ghost policy in effect at the point of declaration
10719 -- or a tagged type and a primitive operation must match
10720 -- (SPARK RM 6.9(16)).
10722 Check_Ghost_Primitive (S, B_Typ);
10726 -- For all subprograms, check formals
10728 Formal := First_Formal (S);
10729 while Present (Formal) loop
10730 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10731 F_Typ := Designated_Type (Etype (Formal));
10733 F_Typ := Etype (Formal);
10736 B_Typ := Base_Type (F_Typ);
10738 if Ekind (B_Typ) = E_Access_Subtype then
10739 B_Typ := Base_Type (B_Typ);
10742 if Scope (B_Typ) = Current_Scope
10743 and then not Is_Class_Wide_Type (B_Typ)
10744 and then not Is_Generic_Type (B_Typ)
10746 Is_Primitive := True;
10747 Set_Is_Primitive (S);
10748 Set_Has_Primitive_Operations (B_Typ);
10749 Check_Private_Overriding (B_Typ);
10751 -- The Ghost policy in effect at the point of declaration
10752 -- of a tagged type and a primitive operation must match
10753 -- (SPARK RM 6.9(16)).
10755 Check_Ghost_Primitive (S, B_Typ);
10758 Next_Formal (Formal);
10761 -- Special case: An equality function can be redefined for a type
10762 -- occurring in a declarative part, and won't otherwise be treated as
10763 -- a primitive because it doesn't occur in a package spec and doesn't
10764 -- override an inherited subprogram. It's important that we mark it
10765 -- primitive so it can be returned by Collect_Primitive_Operations
10766 -- and be used in composing the equality operation of later types
10767 -- that have a component of the type.
10769 elsif Chars (S) = Name_Op_Eq
10770 and then Etype (S) = Standard_Boolean
10772 B_Typ := Base_Type (Etype (First_Formal (S)));
10774 if Scope (B_Typ) = Current_Scope
10776 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10777 and then not Is_Limited_Type (B_Typ)
10779 Is_Primitive := True;
10780 Set_Is_Primitive (S);
10781 Set_Has_Primitive_Operations (B_Typ);
10782 Check_Private_Overriding (B_Typ);
10784 -- The Ghost policy in effect at the point of declaration of a
10785 -- tagged type and a primitive operation must match
10786 -- (SPARK RM 6.9(16)).
10788 Check_Ghost_Primitive (S, B_Typ);
10791 end Check_For_Primitive_Subprogram;
10793 --------------------------------------
10794 -- Has_Matching_Entry_Or_Subprogram --
10795 --------------------------------------
10797 function Has_Matching_Entry_Or_Subprogram
10798 (E : Entity_Id) return Boolean
10800 function Check_Conforming_Parameters
10801 (E1_Param : Node_Id;
10802 E2_Param : Node_Id;
10803 Ctype : Conformance_Type) return Boolean;
10804 -- Starting from the given parameters, check that all the parameters
10805 -- of two entries or subprograms are conformant. Used to skip
10806 -- the check on the controlling argument.
10808 function Matching_Entry_Or_Subprogram
10809 (Conc_Typ : Entity_Id;
10810 Subp : Entity_Id) return Entity_Id;
10811 -- Return the first entry or subprogram of the given concurrent type
10812 -- whose name matches the name of Subp and has a profile conformant
10813 -- with Subp; return Empty if not found.
10815 function Matching_Dispatching_Subprogram
10816 (Conc_Typ : Entity_Id;
10817 Ent : Entity_Id) return Entity_Id;
10818 -- Return the first dispatching primitive of Conc_Type defined in the
10819 -- enclosing scope of Conc_Type (i.e. before the full definition of
10820 -- this concurrent type) whose name matches the entry Ent and has a
10821 -- profile conformant with the profile of the corresponding (not yet
10822 -- built) dispatching primitive of Ent; return Empty if not found.
10824 function Matching_Original_Protected_Subprogram
10825 (Prot_Typ : Entity_Id;
10826 Subp : Entity_Id) return Entity_Id;
10827 -- Return the first subprogram defined in the enclosing scope of
10828 -- Prot_Typ (before the full definition of this protected type)
10829 -- whose name matches the original name of Subp and has a profile
10830 -- conformant with the profile of Subp; return Empty if not found.
10832 function Normalized_First_Parameter_Type
10833 (E : Entity_Id) return Entity_Id;
10834 -- Return the type of the first parameter unless that type
10835 -- is an anonymous access type, in which case return the
10836 -- designated type. Used to treat anonymous-access-to-synchronized
10837 -- the same as synchronized for purposes of checking for
10838 -- prefixed view profile conflicts.
10840 ---------------------------------
10841 -- Check_Conforming_Parameters --
10842 ---------------------------------
10844 function Check_Conforming_Parameters
10845 (E1_Param : Node_Id;
10846 E2_Param : Node_Id;
10847 Ctype : Conformance_Type) return Boolean
10849 Param_E1 : Node_Id := E1_Param;
10850 Param_E2 : Node_Id := E2_Param;
10853 while Present (Param_E1) and then Present (Param_E2) loop
10854 if (Ctype >= Mode_Conformant) and then
10855 Ekind (Defining_Identifier (Param_E1)) /=
10856 Ekind (Defining_Identifier (Param_E2))
10861 (Find_Parameter_Type (Param_E1),
10862 Find_Parameter_Type (Param_E2),
10872 -- The candidate is not valid if one of the two lists contains
10873 -- more parameters than the other
10875 return No (Param_E1) and then No (Param_E2);
10876 end Check_Conforming_Parameters;
10878 ----------------------------------
10879 -- Matching_Entry_Or_Subprogram --
10880 ----------------------------------
10882 function Matching_Entry_Or_Subprogram
10883 (Conc_Typ : Entity_Id;
10884 Subp : Entity_Id) return Entity_Id
10889 E := First_Entity (Conc_Typ);
10890 while Present (E) loop
10891 if Chars (Subp) = Chars (E)
10892 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10894 Check_Conforming_Parameters
10895 (First (Parameter_Specifications (Parent (E))),
10896 Next (First (Parameter_Specifications (Parent (Subp)))),
10906 end Matching_Entry_Or_Subprogram;
10908 -------------------------------------
10909 -- Matching_Dispatching_Subprogram --
10910 -------------------------------------
10912 function Matching_Dispatching_Subprogram
10913 (Conc_Typ : Entity_Id;
10914 Ent : Entity_Id) return Entity_Id
10919 -- Search for entities in the enclosing scope of this synchonized
10922 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10923 Push_Scope (Scope (Conc_Typ));
10924 E := Current_Entity_In_Scope (Ent);
10927 while Present (E) loop
10928 if Scope (E) = Scope (Conc_Typ)
10929 and then Comes_From_Source (E)
10930 and then Ekind (E) = E_Procedure
10931 and then Present (First_Entity (E))
10932 and then Is_Controlling_Formal (First_Entity (E))
10933 and then Etype (First_Entity (E)) = Conc_Typ
10935 Check_Conforming_Parameters
10936 (First (Parameter_Specifications (Parent (Ent))),
10937 Next (First (Parameter_Specifications (Parent (E)))),
10938 Subtype_Conformant)
10947 end Matching_Dispatching_Subprogram;
10949 --------------------------------------------
10950 -- Matching_Original_Protected_Subprogram --
10951 --------------------------------------------
10953 function Matching_Original_Protected_Subprogram
10954 (Prot_Typ : Entity_Id;
10955 Subp : Entity_Id) return Entity_Id
10957 ICF : constant Boolean :=
10958 Is_Controlling_Formal (First_Entity (Subp));
10962 -- Temporarily decorate the first parameter of Subp as controlling
10963 -- formal, required to invoke Subtype_Conformant.
10965 Set_Is_Controlling_Formal (First_Entity (Subp));
10968 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10970 while Present (E) loop
10971 if Scope (E) = Scope (Prot_Typ)
10972 and then Comes_From_Source (E)
10973 and then Ekind (Subp) = Ekind (E)
10974 and then Present (First_Entity (E))
10975 and then Is_Controlling_Formal (First_Entity (E))
10976 and then Etype (First_Entity (E)) = Prot_Typ
10977 and then Subtype_Conformant (Subp, E,
10978 Skip_Controlling_Formals => True)
10980 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10987 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10990 end Matching_Original_Protected_Subprogram;
10992 -------------------------------------
10993 -- Normalized_First_Parameter_Type --
10994 -------------------------------------
10996 function Normalized_First_Parameter_Type
10997 (E : Entity_Id) return Entity_Id
10999 Result : Entity_Id := Etype (First_Entity (E));
11001 if Ekind (Result) = E_Anonymous_Access_Type then
11002 Result := Designated_Type (Result);
11005 end Normalized_First_Parameter_Type;
11007 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11010 -- Case 1: E is a subprogram whose first formal is a concurrent type
11011 -- defined in the scope of E that has an entry or subprogram whose
11012 -- profile matches E.
11014 if Comes_From_Source (E)
11015 and then Is_Subprogram (E)
11016 and then Present (First_Entity (E))
11017 and then Is_Concurrent_Record_Type
11018 (Normalized_First_Parameter_Type (E))
11021 Scope (Corresponding_Concurrent_Type
11022 (Normalized_First_Parameter_Type (E)))
11025 (Matching_Entry_Or_Subprogram
11026 (Corresponding_Concurrent_Type
11027 (Normalized_First_Parameter_Type (E)),
11030 Report_Conflict (E,
11031 Matching_Entry_Or_Subprogram
11032 (Corresponding_Concurrent_Type
11033 (Normalized_First_Parameter_Type (E)),
11038 -- Case 2: E is an internally built dispatching subprogram of a
11039 -- protected type and there is a subprogram defined in the enclosing
11040 -- scope of the protected type that has the original name of E and
11041 -- its profile is conformant with the profile of E. We check the
11042 -- name of the original protected subprogram associated with E since
11043 -- the expander builds dispatching primitives of protected functions
11044 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11046 elsif not Comes_From_Source (E)
11047 and then Is_Subprogram (E)
11048 and then Present (First_Entity (E))
11049 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11050 and then Present (Original_Protected_Subprogram (E))
11053 (Matching_Original_Protected_Subprogram
11054 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11057 Report_Conflict (E,
11058 Matching_Original_Protected_Subprogram
11059 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11063 -- Case 3: E is an entry of a synchronized type and a matching
11064 -- procedure has been previously defined in the enclosing scope
11065 -- of the synchronized type.
11067 elsif Comes_From_Source (E)
11068 and then Ekind (E) = E_Entry
11070 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11072 Report_Conflict (E,
11073 Matching_Dispatching_Subprogram (Current_Scope, E));
11078 end Has_Matching_Entry_Or_Subprogram;
11080 ----------------------------
11081 -- Is_Private_Declaration --
11082 ----------------------------
11084 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11085 Decl : constant Node_Id := Unit_Declaration_Node (E);
11086 Priv_Decls : List_Id;
11089 if Is_Package_Or_Generic_Package (Current_Scope)
11090 and then In_Private_Part (Current_Scope)
11093 Private_Declarations (Package_Specification (Current_Scope));
11095 return In_Package_Body (Current_Scope)
11097 (Is_List_Member (Decl)
11098 and then List_Containing (Decl) = Priv_Decls)
11099 or else (Nkind (Parent (Decl)) = N_Package_Specification
11101 Is_Compilation_Unit
11102 (Defining_Entity (Parent (Decl)))
11103 and then List_Containing (Parent (Parent (Decl))) =
11108 end Is_Private_Declaration;
11110 --------------------------
11111 -- Is_Overriding_Alias --
11112 --------------------------
11114 function Is_Overriding_Alias
11115 (Old_E : Entity_Id;
11116 New_E : Entity_Id) return Boolean
11118 AO : constant Entity_Id := Alias (Old_E);
11119 AN : constant Entity_Id := Alias (New_E);
11122 return Scope (AO) /= Scope (AN)
11123 or else No (DTC_Entity (AO))
11124 or else No (DTC_Entity (AN))
11125 or else DT_Position (AO) = DT_Position (AN);
11126 end Is_Overriding_Alias;
11128 ---------------------
11129 -- Report_Conflict --
11130 ---------------------
11132 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
11134 Error_Msg_Sloc := Sloc (E);
11136 -- Generate message, with useful additional warning if in generic
11138 if Is_Generic_Unit (E) then
11139 Error_Msg_N ("previous generic unit cannot be overloaded", S);
11140 Error_Msg_N ("\& conflicts with declaration#", S);
11142 Error_Msg_N ("& conflicts with declaration#", S);
11144 end Report_Conflict;
11146 -- Start of processing for New_Overloaded_Entity
11149 -- We need to look for an entity that S may override. This must be a
11150 -- homonym in the current scope, so we look for the first homonym of
11151 -- S in the current scope as the starting point for the search.
11153 E := Current_Entity_In_Scope (S);
11155 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
11156 -- They are directly added to the list of primitive operations of
11157 -- Derived_Type, unless this is a rederivation in the private part
11158 -- of an operation that was already derived in the visible part of
11159 -- the current package.
11161 if Ada_Version >= Ada_2005
11162 and then Present (Derived_Type)
11163 and then Present (Alias (S))
11164 and then Is_Dispatching_Operation (Alias (S))
11165 and then Present (Find_Dispatching_Type (Alias (S)))
11166 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
11168 -- For private types, when the full-view is processed we propagate to
11169 -- the full view the non-overridden entities whose attribute "alias"
11170 -- references an interface primitive. These entities were added by
11171 -- Derive_Subprograms to ensure that interface primitives are
11174 -- Inside_Freeze_Actions is non zero when S corresponds with an
11175 -- internal entity that links an interface primitive with its
11176 -- covering primitive through attribute Interface_Alias (see
11177 -- Add_Internal_Interface_Entities).
11179 if Inside_Freezing_Actions = 0
11180 and then Is_Package_Or_Generic_Package (Current_Scope)
11181 and then In_Private_Part (Current_Scope)
11182 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11183 and then Nkind (Parent (S)) = N_Full_Type_Declaration
11184 and then Full_View (Defining_Identifier (Parent (E)))
11185 = Defining_Identifier (Parent (S))
11186 and then Alias (E) = Alias (S)
11188 Check_Operation_From_Private_View (S, E);
11189 Set_Is_Dispatching_Operation (S);
11194 Enter_Overloaded_Entity (S);
11195 Check_Dispatching_Operation (S, Empty);
11196 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11202 -- For synchronized types check conflicts of this entity with previously
11203 -- defined entities.
11205 if Ada_Version >= Ada_2005
11206 and then Has_Matching_Entry_Or_Subprogram (S)
11211 -- If there is no homonym then this is definitely not overriding
11214 Enter_Overloaded_Entity (S);
11215 Check_Dispatching_Operation (S, Empty);
11216 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11218 -- If subprogram has an explicit declaration, check whether it has an
11219 -- overriding indicator.
11221 if Comes_From_Source (S) then
11222 Check_Synchronized_Overriding (S, Overridden_Subp);
11224 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
11225 -- it may have overridden some hidden inherited primitive. Update
11226 -- Overridden_Subp to avoid spurious errors when checking the
11227 -- overriding indicator.
11229 if Ada_Version >= Ada_2012
11230 and then No (Overridden_Subp)
11231 and then Is_Dispatching_Operation (S)
11232 and then Present (Overridden_Operation (S))
11234 Overridden_Subp := Overridden_Operation (S);
11237 Check_Overriding_Indicator
11238 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11240 -- The Ghost policy in effect at the point of declaration of a
11241 -- parent subprogram and an overriding subprogram must match
11242 -- (SPARK RM 6.9(17)).
11244 Check_Ghost_Overriding (S, Overridden_Subp);
11247 -- If there is a homonym that is not overloadable, then we have an
11248 -- error, except for the special cases checked explicitly below.
11250 elsif not Is_Overloadable (E) then
11252 -- Check for spurious conflict produced by a subprogram that has the
11253 -- same name as that of the enclosing generic package. The conflict
11254 -- occurs within an instance, between the subprogram and the renaming
11255 -- declaration for the package. After the subprogram, the package
11256 -- renaming declaration becomes hidden.
11258 if Ekind (E) = E_Package
11259 and then Present (Renamed_Object (E))
11260 and then Renamed_Object (E) = Current_Scope
11261 and then Nkind (Parent (Renamed_Object (E))) =
11262 N_Package_Specification
11263 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
11266 Set_Is_Immediately_Visible (E, False);
11267 Enter_Overloaded_Entity (S);
11268 Set_Homonym (S, Homonym (E));
11269 Check_Dispatching_Operation (S, Empty);
11270 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
11272 -- If the subprogram is implicit it is hidden by the previous
11273 -- declaration. However if it is dispatching, it must appear in the
11274 -- dispatch table anyway, because it can be dispatched to even if it
11275 -- cannot be called directly.
11277 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
11278 Set_Scope (S, Current_Scope);
11280 if Is_Dispatching_Operation (Alias (S)) then
11281 Check_Dispatching_Operation (S, Empty);
11287 Report_Conflict (S, E);
11291 -- E exists and is overloadable
11294 Check_Synchronized_Overriding (S, Overridden_Subp);
11296 -- Loop through E and its homonyms to determine if any of them is
11297 -- the candidate for overriding by S.
11299 while Present (E) loop
11301 -- Definitely not interesting if not in the current scope
11303 if Scope (E) /= Current_Scope then
11306 -- A function can overload the name of an abstract state. The
11307 -- state can be viewed as a function with a profile that cannot
11308 -- be matched by anything.
11310 elsif Ekind (S) = E_Function
11311 and then Ekind (E) = E_Abstract_State
11313 Enter_Overloaded_Entity (S);
11316 -- Ada 2012 (AI05-0165): For internally generated bodies of null
11317 -- procedures locate the internally generated spec. We enforce
11318 -- mode conformance since a tagged type may inherit from
11319 -- interfaces several null primitives which differ only in
11320 -- the mode of the formals.
11322 elsif not Comes_From_Source (S)
11323 and then Is_Null_Procedure (S)
11324 and then not Mode_Conformant (E, S)
11328 -- Check if we have type conformance
11330 elsif Type_Conformant (E, S) then
11332 -- If the old and new entities have the same profile and one
11333 -- is not the body of the other, then this is an error, unless
11334 -- one of them is implicitly declared.
11336 -- There are some cases when both can be implicit, for example
11337 -- when both a literal and a function that overrides it are
11338 -- inherited in a derivation, or when an inherited operation
11339 -- of a tagged full type overrides the inherited operation of
11340 -- a private extension. Ada 83 had a special rule for the
11341 -- literal case. In Ada 95, the later implicit operation hides
11342 -- the former, and the literal is always the former. In the
11343 -- odd case where both are derived operations declared at the
11344 -- same point, both operations should be declared, and in that
11345 -- case we bypass the following test and proceed to the next
11346 -- part. This can only occur for certain obscure cases in
11347 -- instances, when an operation on a type derived from a formal
11348 -- private type does not override a homograph inherited from
11349 -- the actual. In subsequent derivations of such a type, the
11350 -- DT positions of these operations remain distinct, if they
11353 if Present (Alias (S))
11354 and then (No (Alias (E))
11355 or else Comes_From_Source (E)
11356 or else Is_Abstract_Subprogram (S)
11358 (Is_Dispatching_Operation (E)
11359 and then Is_Overriding_Alias (E, S)))
11360 and then Ekind (E) /= E_Enumeration_Literal
11362 -- When an derived operation is overloaded it may be due to
11363 -- the fact that the full view of a private extension
11364 -- re-inherits. It has to be dealt with.
11366 if Is_Package_Or_Generic_Package (Current_Scope)
11367 and then In_Private_Part (Current_Scope)
11369 Check_Operation_From_Private_View (S, E);
11372 -- In any case the implicit operation remains hidden by the
11373 -- existing declaration, which is overriding. Indicate that
11374 -- E overrides the operation from which S is inherited.
11376 if Present (Alias (S)) then
11377 Set_Overridden_Operation (E, Alias (S));
11378 Inherit_Subprogram_Contract (E, Alias (S));
11381 Set_Overridden_Operation (E, S);
11382 Inherit_Subprogram_Contract (E, S);
11385 -- When a dispatching operation overrides an inherited
11386 -- subprogram, it shall be subtype conformant with the
11387 -- inherited subprogram (RM 3.9.2 (10.2)).
11389 if Comes_From_Source (E)
11390 and then Is_Dispatching_Operation (E)
11391 and then Find_Dispatching_Type (S)
11392 = Find_Dispatching_Type (E)
11394 Check_Subtype_Conformant (E, S);
11397 if Comes_From_Source (E) then
11398 Check_Overriding_Indicator (E, S, Is_Primitive => False);
11400 -- The Ghost policy in effect at the point of declaration
11401 -- of a parent subprogram and an overriding subprogram
11402 -- must match (SPARK RM 6.9(17)).
11404 Check_Ghost_Overriding (E, S);
11409 -- Within an instance, the renaming declarations for actual
11410 -- subprograms may become ambiguous, but they do not hide each
11413 elsif Ekind (E) /= E_Entry
11414 and then not Comes_From_Source (E)
11415 and then not Is_Generic_Instance (E)
11416 and then (Present (Alias (E))
11417 or else Is_Intrinsic_Subprogram (E))
11418 and then (not In_Instance
11419 or else No (Parent (E))
11420 or else Nkind (Unit_Declaration_Node (E)) /=
11421 N_Subprogram_Renaming_Declaration)
11423 -- A subprogram child unit is not allowed to override an
11424 -- inherited subprogram (10.1.1(20)).
11426 if Is_Child_Unit (S) then
11428 ("child unit overrides inherited subprogram in parent",
11433 if Is_Non_Overriding_Operation (E, S) then
11434 Enter_Overloaded_Entity (S);
11436 if No (Derived_Type)
11437 or else Is_Tagged_Type (Derived_Type)
11439 Check_Dispatching_Operation (S, Empty);
11445 -- E is a derived operation or an internal operator which
11446 -- is being overridden. Remove E from further visibility.
11447 -- Furthermore, if E is a dispatching operation, it must be
11448 -- replaced in the list of primitive operations of its type
11449 -- (see Override_Dispatching_Operation).
11451 Overridden_Subp := E;
11453 -- It is possible for E to be in the current scope and
11454 -- yet not in the entity chain. This can only occur in a
11455 -- generic context where E is an implicit concatenation
11456 -- in the formal part, because in a generic body the
11457 -- entity chain starts with the formals.
11459 -- In GNATprove mode, a wrapper for an operation with
11460 -- axiomatization may be a homonym of another declaration
11461 -- for an actual subprogram (needs refinement ???).
11463 if No (Prev_Entity (E)) then
11465 and then GNATprove_Mode
11467 Nkind (Original_Node (Unit_Declaration_Node (S))) =
11468 N_Subprogram_Renaming_Declaration
11472 pragma Assert (Chars (E) = Name_Op_Concat);
11477 -- E must be removed both from the entity_list of the
11478 -- current scope, and from the visibility chain.
11480 if Debug_Flag_E then
11481 Write_Str ("Override implicit operation ");
11482 Write_Int (Int (E));
11486 -- If E is a predefined concatenation, it stands for four
11487 -- different operations. As a result, a single explicit
11488 -- declaration does not hide it. In a possible ambiguous
11489 -- situation, Disambiguate chooses the user-defined op,
11490 -- so it is correct to retain the previous internal one.
11492 if Chars (E) /= Name_Op_Concat
11493 or else Ekind (E) /= E_Operator
11495 -- For nondispatching derived operations that are
11496 -- overridden by a subprogram declared in the private
11497 -- part of a package, we retain the derived subprogram
11498 -- but mark it as not immediately visible. If the
11499 -- derived operation was declared in the visible part
11500 -- then this ensures that it will still be visible
11501 -- outside the package with the proper signature
11502 -- (calls from outside must also be directed to this
11503 -- version rather than the overriding one, unlike the
11504 -- dispatching case). Calls from inside the package
11505 -- will still resolve to the overriding subprogram
11506 -- since the derived one is marked as not visible
11507 -- within the package.
11509 -- If the private operation is dispatching, we achieve
11510 -- the overriding by keeping the implicit operation
11511 -- but setting its alias to be the overriding one. In
11512 -- this fashion the proper body is executed in all
11513 -- cases, but the original signature is used outside
11516 -- If the overriding is not in the private part, we
11517 -- remove the implicit operation altogether.
11519 if Is_Private_Declaration (S) then
11520 if not Is_Dispatching_Operation (E) then
11521 Set_Is_Immediately_Visible (E, False);
11523 -- Work done in Override_Dispatching_Operation, so
11524 -- nothing else needs to be done here.
11530 Remove_Entity_And_Homonym (E);
11534 Enter_Overloaded_Entity (S);
11536 -- For entities generated by Derive_Subprograms the
11537 -- overridden operation is the inherited primitive
11538 -- (which is available through the attribute alias).
11540 if not (Comes_From_Source (E))
11541 and then Is_Dispatching_Operation (E)
11542 and then Find_Dispatching_Type (E) =
11543 Find_Dispatching_Type (S)
11544 and then Present (Alias (E))
11545 and then Comes_From_Source (Alias (E))
11547 Set_Overridden_Operation (S, Alias (E));
11548 Inherit_Subprogram_Contract (S, Alias (E));
11550 -- Normal case of setting entity as overridden
11552 -- Note: Static_Initialization and Overridden_Operation
11553 -- attributes use the same field in subprogram entities.
11554 -- Static_Initialization is only defined for internal
11555 -- initialization procedures, where Overridden_Operation
11556 -- is irrelevant. Therefore the setting of this attribute
11557 -- must check whether the target is an init_proc.
11559 elsif not Is_Init_Proc (S) then
11560 Set_Overridden_Operation (S, E);
11561 Inherit_Subprogram_Contract (S, E);
11564 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11566 -- The Ghost policy in effect at the point of declaration
11567 -- of a parent subprogram and an overriding subprogram
11568 -- must match (SPARK RM 6.9(17)).
11570 Check_Ghost_Overriding (S, E);
11572 -- If S is a user-defined subprogram or a null procedure
11573 -- expanded to override an inherited null procedure, or a
11574 -- predefined dispatching primitive then indicate that E
11575 -- overrides the operation from which S is inherited.
11577 if Comes_From_Source (S)
11579 (Present (Parent (S))
11580 and then Nkind (Parent (S)) = N_Procedure_Specification
11581 and then Null_Present (Parent (S)))
11583 (Present (Alias (E))
11585 Is_Predefined_Dispatching_Operation (Alias (E)))
11587 if Present (Alias (E)) then
11588 Set_Overridden_Operation (S, Alias (E));
11589 Inherit_Subprogram_Contract (S, Alias (E));
11593 if Is_Dispatching_Operation (E) then
11595 -- An overriding dispatching subprogram inherits the
11596 -- convention of the overridden subprogram (AI-117).
11598 Set_Convention (S, Convention (E));
11599 Check_Dispatching_Operation (S, E);
11602 Check_Dispatching_Operation (S, Empty);
11605 Check_For_Primitive_Subprogram
11606 (Is_Primitive_Subp, Is_Overriding => True);
11607 goto Check_Inequality;
11609 -- Apparent redeclarations in instances can occur when two
11610 -- formal types get the same actual type. The subprograms in
11611 -- in the instance are legal, even if not callable from the
11612 -- outside. Calls from within are disambiguated elsewhere.
11613 -- For dispatching operations in the visible part, the usual
11614 -- rules apply, and operations with the same profile are not
11615 -- legal (B830001).
11617 elsif (In_Instance_Visible_Part
11618 and then not Is_Dispatching_Operation (E))
11619 or else In_Instance_Not_Visible
11623 -- Here we have a real error (identical profile)
11626 Error_Msg_Sloc := Sloc (E);
11628 -- Avoid cascaded errors if the entity appears in
11629 -- subsequent calls.
11631 Set_Scope (S, Current_Scope);
11633 -- Generate error, with extra useful warning for the case
11634 -- of a generic instance with no completion.
11636 if Is_Generic_Instance (S)
11637 and then not Has_Completion (E)
11640 ("instantiation cannot provide body for&", S);
11641 Error_Msg_N ("\& conflicts with declaration#", S);
11643 Error_Msg_N ("& conflicts with declaration#", S);
11650 -- If one subprogram has an access parameter and the other
11651 -- a parameter of an access type, calls to either might be
11652 -- ambiguous. Verify that parameters match except for the
11653 -- access parameter.
11655 if May_Hide_Profile then
11661 F1 := First_Formal (S);
11662 F2 := First_Formal (E);
11663 while Present (F1) and then Present (F2) loop
11664 if Is_Access_Type (Etype (F1)) then
11665 if not Is_Access_Type (Etype (F2))
11666 or else not Conforming_Types
11667 (Designated_Type (Etype (F1)),
11668 Designated_Type (Etype (F2)),
11671 May_Hide_Profile := False;
11675 not Conforming_Types
11676 (Etype (F1), Etype (F2), Type_Conformant)
11678 May_Hide_Profile := False;
11685 if May_Hide_Profile
11689 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11698 -- On exit, we know that S is a new entity
11700 Enter_Overloaded_Entity (S);
11701 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11702 Check_Overriding_Indicator
11703 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11705 -- The Ghost policy in effect at the point of declaration of a parent
11706 -- subprogram and an overriding subprogram must match
11707 -- (SPARK RM 6.9(17)).
11709 Check_Ghost_Overriding (S, Overridden_Subp);
11711 -- If S is a derived operation for an untagged type then by
11712 -- definition it's not a dispatching operation (even if the parent
11713 -- operation was dispatching), so Check_Dispatching_Operation is not
11714 -- called in that case.
11716 if No (Derived_Type)
11717 or else Is_Tagged_Type (Derived_Type)
11719 Check_Dispatching_Operation (S, Empty);
11723 -- If this is a user-defined equality operator that is not a derived
11724 -- subprogram, create the corresponding inequality. If the operation is
11725 -- dispatching, the expansion is done elsewhere, and we do not create
11726 -- an explicit inequality operation.
11728 <<Check_Inequality>>
11729 if Chars (S) = Name_Op_Eq
11730 and then Etype (S) = Standard_Boolean
11731 and then Present (Parent (S))
11732 and then not Is_Dispatching_Operation (S)
11734 Make_Inequality_Operator (S);
11735 Check_Untagged_Equality (S);
11737 end New_Overloaded_Entity;
11739 ----------------------------------
11740 -- Preanalyze_Formal_Expression --
11741 ----------------------------------
11743 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
11744 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
11746 In_Spec_Expression := True;
11747 Preanalyze_With_Freezing_And_Resolve (N, T);
11748 In_Spec_Expression := Save_In_Spec_Expression;
11749 end Preanalyze_Formal_Expression;
11751 ---------------------
11752 -- Process_Formals --
11753 ---------------------
11755 procedure Process_Formals
11757 Related_Nod : Node_Id)
11759 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11760 -- Determine whether an access type designates a type coming from a
11763 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11764 -- Check whether the default has a class-wide type. After analysis the
11765 -- default has the type of the formal, so we must also check explicitly
11766 -- for an access attribute.
11768 ----------------------------------
11769 -- Designates_From_Limited_With --
11770 ----------------------------------
11772 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11773 Desig : Entity_Id := Typ;
11776 if Is_Access_Type (Desig) then
11777 Desig := Directly_Designated_Type (Desig);
11780 if Is_Class_Wide_Type (Desig) then
11781 Desig := Root_Type (Desig);
11785 Ekind (Desig) = E_Incomplete_Type
11786 and then From_Limited_With (Desig);
11787 end Designates_From_Limited_With;
11789 ---------------------------
11790 -- Is_Class_Wide_Default --
11791 ---------------------------
11793 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11795 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11796 or else (Nkind (D) = N_Attribute_Reference
11797 and then Attribute_Name (D) = Name_Access
11798 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11799 end Is_Class_Wide_Default;
11803 Context : constant Node_Id := Parent (Parent (T));
11805 Formal : Entity_Id;
11806 Formal_Type : Entity_Id;
11807 Param_Spec : Node_Id;
11810 Num_Out_Params : Nat := 0;
11811 First_Out_Param : Entity_Id := Empty;
11812 -- Used for setting Is_Only_Out_Parameter
11814 -- Start of processing for Process_Formals
11817 -- In order to prevent premature use of the formals in the same formal
11818 -- part, the Ekind is left undefined until all default expressions are
11819 -- analyzed. The Ekind is established in a separate loop at the end.
11821 Param_Spec := First (T);
11822 while Present (Param_Spec) loop
11823 Formal := Defining_Identifier (Param_Spec);
11824 Set_Never_Set_In_Source (Formal, True);
11825 Enter_Name (Formal);
11827 -- Case of ordinary parameters
11829 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11830 Find_Type (Parameter_Type (Param_Spec));
11831 Ptype := Parameter_Type (Param_Spec);
11833 if Ptype = Error then
11837 -- Protect against malformed parameter types
11839 if Nkind (Ptype) not in N_Has_Entity then
11840 Formal_Type := Any_Type;
11842 Formal_Type := Entity (Ptype);
11845 if Is_Incomplete_Type (Formal_Type)
11847 (Is_Class_Wide_Type (Formal_Type)
11848 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11850 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11851 -- primitive operations, as long as their completion is
11852 -- in the same declarative part. If in the private part
11853 -- this means that the type cannot be a Taft-amendment type.
11854 -- Check is done on package exit. For access to subprograms,
11855 -- the use is legal for Taft-amendment types.
11857 -- Ada 2012: tagged incomplete types are allowed as generic
11858 -- formal types. They do not introduce dependencies and the
11859 -- corresponding generic subprogram does not have a delayed
11860 -- freeze, because it does not need a freeze node. However,
11861 -- it is still the case that untagged incomplete types cannot
11862 -- be Taft-amendment types and must be completed in private
11863 -- part, so the subprogram must appear in the list of private
11864 -- dependents of the type.
11866 if Is_Tagged_Type (Formal_Type)
11867 or else (Ada_Version >= Ada_2012
11868 and then not From_Limited_With (Formal_Type)
11869 and then not Is_Generic_Type (Formal_Type))
11871 if Ekind (Scope (Current_Scope)) = E_Package
11872 and then not Is_Generic_Type (Formal_Type)
11873 and then not Is_Class_Wide_Type (Formal_Type)
11876 (Parent (T), N_Access_Function_Definition,
11877 N_Access_Procedure_Definition)
11879 Append_Elmt (Current_Scope,
11880 Private_Dependents (Base_Type (Formal_Type)));
11882 -- Freezing is delayed to ensure that Register_Prim
11883 -- will get called for this operation, which is needed
11884 -- in cases where static dispatch tables aren't built.
11885 -- (Note that the same is done for controlling access
11886 -- parameter cases in function Access_Definition.)
11888 if not Is_Thunk (Current_Scope) then
11889 Set_Has_Delayed_Freeze (Current_Scope);
11894 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11895 N_Access_Procedure_Definition)
11897 -- AI05-0151: Tagged incomplete types are allowed in all
11898 -- formal parts. Untagged incomplete types are not allowed
11899 -- in bodies. Limited views of either kind are not allowed
11900 -- if there is no place at which the non-limited view can
11901 -- become available.
11903 -- Incomplete formal untagged types are not allowed in
11904 -- subprogram bodies (but are legal in their declarations).
11905 -- This excludes bodies created for null procedures, which
11906 -- are basic declarations.
11908 if Is_Generic_Type (Formal_Type)
11909 and then not Is_Tagged_Type (Formal_Type)
11910 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11913 ("invalid use of formal incomplete type", Param_Spec);
11915 elsif Ada_Version >= Ada_2012 then
11916 if Is_Tagged_Type (Formal_Type)
11917 and then (not From_Limited_With (Formal_Type)
11918 or else not In_Package_Body)
11922 elsif Nkind_In (Context, N_Accept_Statement,
11923 N_Accept_Alternative,
11925 or else (Nkind (Context) = N_Subprogram_Body
11926 and then Comes_From_Source (Context))
11929 ("invalid use of untagged incomplete type &",
11930 Ptype, Formal_Type);
11935 ("invalid use of incomplete type&",
11936 Param_Spec, Formal_Type);
11938 -- Further checks on the legality of incomplete types
11939 -- in formal parts are delayed until the freeze point
11940 -- of the enclosing subprogram or access to subprogram.
11944 elsif Ekind (Formal_Type) = E_Void then
11946 ("premature use of&",
11947 Parameter_Type (Param_Spec), Formal_Type);
11950 -- Ada 2012 (AI-142): Handle aliased parameters
11952 if Ada_Version >= Ada_2012
11953 and then Aliased_Present (Param_Spec)
11955 Set_Is_Aliased (Formal);
11957 -- AI12-001: All aliased objects are considered to be specified
11958 -- as independently addressable (RM C.6(8.1/4)).
11960 Set_Is_Independent (Formal);
11963 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11964 -- declaration corresponding to the null-excluding type of the
11965 -- formal in the enclosing scope. Finally, replace the parameter
11966 -- type of the formal with the internal subtype.
11968 if Ada_Version >= Ada_2005
11969 and then Null_Exclusion_Present (Param_Spec)
11971 if not Is_Access_Type (Formal_Type) then
11973 ("`NOT NULL` allowed only for an access type", Param_Spec);
11976 if Can_Never_Be_Null (Formal_Type)
11977 and then Comes_From_Source (Related_Nod)
11980 ("`NOT NULL` not allowed (& already excludes null)",
11981 Param_Spec, Formal_Type);
11985 Create_Null_Excluding_Itype
11987 Related_Nod => Related_Nod,
11988 Scope_Id => Scope (Current_Scope));
11990 -- If the designated type of the itype is an itype that is
11991 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11992 -- on the access subtype, to prevent order-of-elaboration
11993 -- issues in the backend.
11996 -- type T is access procedure;
11997 -- procedure Op (O : not null T);
11999 if Is_Itype (Directly_Designated_Type (Formal_Type))
12001 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12003 Set_Has_Delayed_Freeze (Formal_Type);
12008 -- An access formal type
12012 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12014 -- No need to continue if we already notified errors
12016 if not Present (Formal_Type) then
12020 -- Ada 2005 (AI-254)
12023 AD : constant Node_Id :=
12024 Access_To_Subprogram_Definition
12025 (Parameter_Type (Param_Spec));
12027 if Present (AD) and then Protected_Present (AD) then
12029 Replace_Anonymous_Access_To_Protected_Subprogram
12035 Set_Etype (Formal, Formal_Type);
12037 -- Deal with default expression if present
12039 Default := Expression (Param_Spec);
12041 if Present (Default) then
12042 if Out_Present (Param_Spec) then
12044 ("default initialization only allowed for IN parameters",
12048 -- Do the special preanalysis of the expression (see section on
12049 -- "Handling of Default Expressions" in the spec of package Sem).
12051 Preanalyze_Formal_Expression (Default, Formal_Type);
12053 -- An access to constant cannot be the default for
12054 -- an access parameter that is an access to variable.
12056 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12057 and then not Is_Access_Constant (Formal_Type)
12058 and then Is_Access_Type (Etype (Default))
12059 and then Is_Access_Constant (Etype (Default))
12062 ("formal that is access to variable cannot be initialized "
12063 & "with an access-to-constant expression", Default);
12066 -- Check that the designated type of an access parameter's default
12067 -- is not a class-wide type unless the parameter's designated type
12068 -- is also class-wide.
12070 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12071 and then not Designates_From_Limited_With (Formal_Type)
12072 and then Is_Class_Wide_Default (Default)
12073 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
12076 ("access to class-wide expression not allowed here", Default);
12079 -- Check incorrect use of dynamically tagged expressions
12081 if Is_Tagged_Type (Formal_Type) then
12082 Check_Dynamically_Tagged_Expression
12084 Typ => Formal_Type,
12085 Related_Nod => Default);
12089 -- Ada 2005 (AI-231): Static checks
12091 if Ada_Version >= Ada_2005
12092 and then Is_Access_Type (Etype (Formal))
12093 and then Can_Never_Be_Null (Etype (Formal))
12095 Null_Exclusion_Static_Checks (Param_Spec);
12098 -- The following checks are relevant only when SPARK_Mode is on as
12099 -- these are not standard Ada legality rules.
12101 if SPARK_Mode = On then
12102 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
12104 -- A function cannot have a parameter of mode IN OUT or OUT
12107 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
12109 ("function cannot have parameter of mode `OUT` or "
12110 & "`IN OUT`", Formal);
12113 -- A procedure cannot have an effectively volatile formal
12114 -- parameter of mode IN because it behaves as a constant
12115 -- (SPARK RM 7.1.3(4)).
12117 elsif Ekind (Scope (Formal)) = E_Procedure
12118 and then Ekind (Formal) = E_In_Parameter
12119 and then Is_Effectively_Volatile (Formal)
12122 ("formal parameter of mode `IN` cannot be volatile", Formal);
12130 -- If this is the formal part of a function specification, analyze the
12131 -- subtype mark in the context where the formals are visible but not
12132 -- yet usable, and may hide outer homographs.
12134 if Nkind (Related_Nod) = N_Function_Specification then
12135 Analyze_Return_Type (Related_Nod);
12138 -- Now set the kind (mode) of each formal
12140 Param_Spec := First (T);
12141 while Present (Param_Spec) loop
12142 Formal := Defining_Identifier (Param_Spec);
12143 Set_Formal_Mode (Formal);
12145 if Ekind (Formal) = E_In_Parameter then
12146 Set_Default_Value (Formal, Expression (Param_Spec));
12148 if Present (Expression (Param_Spec)) then
12149 Default := Expression (Param_Spec);
12151 if Is_Scalar_Type (Etype (Default)) then
12152 if Nkind (Parameter_Type (Param_Spec)) /=
12153 N_Access_Definition
12155 Formal_Type := Entity (Parameter_Type (Param_Spec));
12159 (Related_Nod, Parameter_Type (Param_Spec));
12162 Apply_Scalar_Range_Check (Default, Formal_Type);
12166 elsif Ekind (Formal) = E_Out_Parameter then
12167 Num_Out_Params := Num_Out_Params + 1;
12169 if Num_Out_Params = 1 then
12170 First_Out_Param := Formal;
12173 elsif Ekind (Formal) = E_In_Out_Parameter then
12174 Num_Out_Params := Num_Out_Params + 1;
12177 -- Skip remaining processing if formal type was in error
12179 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
12180 goto Next_Parameter;
12183 -- Force call by reference if aliased
12186 Conv : constant Convention_Id := Convention (Etype (Formal));
12188 if Is_Aliased (Formal) then
12189 Set_Mechanism (Formal, By_Reference);
12191 -- Warn if user asked this to be passed by copy
12193 if Conv = Convention_Ada_Pass_By_Copy then
12195 ("cannot pass aliased parameter & by copy??", Formal);
12198 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
12200 elsif Conv = Convention_Ada_Pass_By_Copy then
12201 Set_Mechanism (Formal, By_Copy);
12203 elsif Conv = Convention_Ada_Pass_By_Reference then
12204 Set_Mechanism (Formal, By_Reference);
12212 if Present (First_Out_Param) and then Num_Out_Params = 1 then
12213 Set_Is_Only_Out_Parameter (First_Out_Param);
12215 end Process_Formals;
12217 ----------------------------
12218 -- Reference_Body_Formals --
12219 ----------------------------
12221 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12226 if Error_Posted (Spec) then
12230 -- Iterate over both lists. They may be of different lengths if the two
12231 -- specs are not conformant.
12233 Fs := First_Formal (Spec);
12234 Fb := First_Formal (Bod);
12235 while Present (Fs) and then Present (Fb) loop
12236 Generate_Reference (Fs, Fb, 'b');
12238 if Style_Check then
12239 Style.Check_Identifier (Fb, Fs);
12242 Set_Spec_Entity (Fb, Fs);
12243 Set_Referenced (Fs, False);
12247 end Reference_Body_Formals;
12249 -------------------------
12250 -- Set_Actual_Subtypes --
12251 -------------------------
12253 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12255 Formal : Entity_Id;
12257 First_Stmt : Node_Id := Empty;
12258 AS_Needed : Boolean;
12261 -- If this is an empty initialization procedure, no need to create
12262 -- actual subtypes (small optimization).
12264 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12267 -- Within a predicate function we do not want to generate local
12268 -- subtypes that may generate nested predicate functions.
12270 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
12274 -- The subtype declarations may freeze the formals. The body generated
12275 -- for an expression function is not a freeze point, so do not emit
12276 -- these declarations (small loss of efficiency in rare cases).
12278 if Nkind (N) = N_Subprogram_Body
12279 and then Was_Expression_Function (N)
12284 Formal := First_Formal (Subp);
12285 while Present (Formal) loop
12286 T := Etype (Formal);
12288 -- We never need an actual subtype for a constrained formal
12290 if Is_Constrained (T) then
12291 AS_Needed := False;
12293 -- If we have unknown discriminants, then we do not need an actual
12294 -- subtype, or more accurately we cannot figure it out. Note that
12295 -- all class-wide types have unknown discriminants.
12297 elsif Has_Unknown_Discriminants (T) then
12298 AS_Needed := False;
12300 -- At this stage we have an unconstrained type that may need an
12301 -- actual subtype. For sure the actual subtype is needed if we have
12302 -- an unconstrained array type. However, in an instance, the type
12303 -- may appear as a subtype of the full view, while the actual is
12304 -- in fact private (in which case no actual subtype is needed) so
12305 -- check the kind of the base type.
12307 elsif Is_Array_Type (Base_Type (T)) then
12310 -- The only other case needing an actual subtype is an unconstrained
12311 -- record type which is an IN parameter (we cannot generate actual
12312 -- subtypes for the OUT or IN OUT case, since an assignment can
12313 -- change the discriminant values. However we exclude the case of
12314 -- initialization procedures, since discriminants are handled very
12315 -- specially in this context, see the section entitled "Handling of
12316 -- Discriminants" in Einfo.
12318 -- We also exclude the case of Discrim_SO_Functions (functions used
12319 -- in front-end layout mode for size/offset values), since in such
12320 -- functions only discriminants are referenced, and not only are such
12321 -- subtypes not needed, but they cannot always be generated, because
12322 -- of order of elaboration issues.
12324 elsif Is_Record_Type (T)
12325 and then Ekind (Formal) = E_In_Parameter
12326 and then Chars (Formal) /= Name_uInit
12327 and then not Is_Unchecked_Union (T)
12328 and then not Is_Discrim_SO_Function (Subp)
12332 -- All other cases do not need an actual subtype
12335 AS_Needed := False;
12338 -- Generate actual subtypes for unconstrained arrays and
12339 -- unconstrained discriminated records.
12342 if Nkind (N) = N_Accept_Statement then
12344 -- If expansion is active, the formal is replaced by a local
12345 -- variable that renames the corresponding entry of the
12346 -- parameter block, and it is this local variable that may
12347 -- require an actual subtype.
12349 if Expander_Active then
12350 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12352 Decl := Build_Actual_Subtype (T, Formal);
12355 if Present (Handled_Statement_Sequence (N)) then
12357 First (Statements (Handled_Statement_Sequence (N)));
12358 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12359 Mark_Rewrite_Insertion (Decl);
12361 -- If the accept statement has no body, there will be no
12362 -- reference to the actuals, so no need to compute actual
12369 Decl := Build_Actual_Subtype (T, Formal);
12370 Prepend (Decl, Declarations (N));
12371 Mark_Rewrite_Insertion (Decl);
12374 -- The declaration uses the bounds of an existing object, and
12375 -- therefore needs no constraint checks.
12377 Analyze (Decl, Suppress => All_Checks);
12378 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
12380 -- We need to freeze manually the generated type when it is
12381 -- inserted anywhere else than in a declarative part.
12383 if Present (First_Stmt) then
12384 Insert_List_Before_And_Analyze (First_Stmt,
12385 Freeze_Entity (Defining_Identifier (Decl), N));
12387 -- Ditto if the type has a dynamic predicate, because the
12388 -- generated function will mention the actual subtype. The
12389 -- predicate may come from an explicit aspect of be inherited.
12391 elsif Has_Predicates (T) then
12392 Insert_List_After_And_Analyze (Decl,
12393 Freeze_Entity (Defining_Identifier (Decl), N));
12396 if Nkind (N) = N_Accept_Statement
12397 and then Expander_Active
12399 Set_Actual_Subtype (Renamed_Object (Formal),
12400 Defining_Identifier (Decl));
12402 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12406 Next_Formal (Formal);
12408 end Set_Actual_Subtypes;
12410 ---------------------
12411 -- Set_Formal_Mode --
12412 ---------------------
12414 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12415 Spec : constant Node_Id := Parent (Formal_Id);
12416 Id : constant Entity_Id := Scope (Formal_Id);
12419 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12420 -- since we ensure that corresponding actuals are always valid at the
12421 -- point of the call.
12423 if Out_Present (Spec) then
12425 or else Is_Subprogram_Or_Generic_Subprogram (Id)
12427 Set_Has_Out_Or_In_Out_Parameter (Id, True);
12430 if Ekind_In (Id, E_Function, E_Generic_Function) then
12432 -- [IN] OUT parameters allowed for functions in Ada 2012
12434 if Ada_Version >= Ada_2012 then
12436 -- Even in Ada 2012 operators can only have IN parameters
12438 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12439 Error_Msg_N ("operators can only have IN parameters", Spec);
12442 if In_Present (Spec) then
12443 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12445 Set_Ekind (Formal_Id, E_Out_Parameter);
12448 -- But not in earlier versions of Ada
12451 Error_Msg_N ("functions can only have IN parameters", Spec);
12452 Set_Ekind (Formal_Id, E_In_Parameter);
12455 elsif In_Present (Spec) then
12456 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12459 Set_Ekind (Formal_Id, E_Out_Parameter);
12460 Set_Never_Set_In_Source (Formal_Id, True);
12461 Set_Is_True_Constant (Formal_Id, False);
12462 Set_Current_Value (Formal_Id, Empty);
12466 Set_Ekind (Formal_Id, E_In_Parameter);
12469 -- Set Is_Known_Non_Null for access parameters since the language
12470 -- guarantees that access parameters are always non-null. We also set
12471 -- Can_Never_Be_Null, since there is no way to change the value.
12473 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12475 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12476 -- null; In Ada 2005, only if then null_exclusion is explicit.
12478 if Ada_Version < Ada_2005
12479 or else Can_Never_Be_Null (Etype (Formal_Id))
12481 Set_Is_Known_Non_Null (Formal_Id);
12482 Set_Can_Never_Be_Null (Formal_Id);
12485 -- Ada 2005 (AI-231): Null-exclusion access subtype
12487 elsif Is_Access_Type (Etype (Formal_Id))
12488 and then Can_Never_Be_Null (Etype (Formal_Id))
12490 Set_Is_Known_Non_Null (Formal_Id);
12492 -- We can also set Can_Never_Be_Null (thus preventing some junk
12493 -- access checks) for the case of an IN parameter, which cannot
12494 -- be changed, or for an IN OUT parameter, which can be changed but
12495 -- not to a null value. But for an OUT parameter, the initial value
12496 -- passed in can be null, so we can't set this flag in that case.
12498 if Ekind (Formal_Id) /= E_Out_Parameter then
12499 Set_Can_Never_Be_Null (Formal_Id);
12503 Set_Mechanism (Formal_Id, Default_Mechanism);
12504 Set_Formal_Validity (Formal_Id);
12505 end Set_Formal_Mode;
12507 -------------------------
12508 -- Set_Formal_Validity --
12509 -------------------------
12511 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12513 -- If no validity checking, then we cannot assume anything about the
12514 -- validity of parameters, since we do not know there is any checking
12515 -- of the validity on the call side.
12517 if not Validity_Checks_On then
12520 -- If validity checking for parameters is enabled, this means we are
12521 -- not supposed to make any assumptions about argument values.
12523 elsif Validity_Check_Parameters then
12526 -- If we are checking in parameters, we will assume that the caller is
12527 -- also checking parameters, so we can assume the parameter is valid.
12529 elsif Ekind (Formal_Id) = E_In_Parameter
12530 and then Validity_Check_In_Params
12532 Set_Is_Known_Valid (Formal_Id, True);
12534 -- Similar treatment for IN OUT parameters
12536 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12537 and then Validity_Check_In_Out_Params
12539 Set_Is_Known_Valid (Formal_Id, True);
12541 end Set_Formal_Validity;
12543 ------------------------
12544 -- Subtype_Conformant --
12545 ------------------------
12547 function Subtype_Conformant
12548 (New_Id : Entity_Id;
12549 Old_Id : Entity_Id;
12550 Skip_Controlling_Formals : Boolean := False) return Boolean
12554 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12555 Skip_Controlling_Formals => Skip_Controlling_Formals);
12557 end Subtype_Conformant;
12559 ---------------------
12560 -- Type_Conformant --
12561 ---------------------
12563 function Type_Conformant
12564 (New_Id : Entity_Id;
12565 Old_Id : Entity_Id;
12566 Skip_Controlling_Formals : Boolean := False) return Boolean
12570 May_Hide_Profile := False;
12572 (New_Id, Old_Id, Type_Conformant, False, Result,
12573 Skip_Controlling_Formals => Skip_Controlling_Formals);
12575 end Type_Conformant;
12577 -------------------------------
12578 -- Valid_Operator_Definition --
12579 -------------------------------
12581 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12584 Id : constant Name_Id := Chars (Designator);
12588 F := First_Formal (Designator);
12589 while Present (F) loop
12592 if Present (Default_Value (F)) then
12594 ("default values not allowed for operator parameters",
12597 -- For function instantiations that are operators, we must check
12598 -- separately that the corresponding generic only has in-parameters.
12599 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12600 -- an error could not arise in earlier versions of the language.
12602 elsif Ekind (F) /= E_In_Parameter then
12603 Error_Msg_N ("operators can only have IN parameters", F);
12609 -- Verify that user-defined operators have proper number of arguments
12610 -- First case of operators which can only be unary
12612 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12615 -- Case of operators which can be unary or binary
12617 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12618 N_OK := (N in 1 .. 2);
12620 -- All other operators can only be binary
12628 ("incorrect number of arguments for operator", Designator);
12632 and then Base_Type (Etype (Designator)) = Standard_Boolean
12633 and then not Is_Intrinsic_Subprogram (Designator)
12636 ("explicit definition of inequality not allowed", Designator);
12638 end Valid_Operator_Definition;