1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2014, 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 Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Dbug; use Exp_Dbug;
38 with Exp_Disp; use Exp_Disp;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Itypes; use Itypes;
44 with Lib.Xref; use Lib.Xref;
45 with Layout; use Layout;
46 with Namet; use Namet;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
51 with Output; use Output;
52 with Restrict; use Restrict;
53 with Rident; use Rident;
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_Ch10; use Sem_Ch10;
63 with Sem_Ch12; use Sem_Ch12;
64 with Sem_Ch13; use Sem_Ch13;
65 with Sem_Dim; use Sem_Dim;
66 with Sem_Disp; use Sem_Disp;
67 with Sem_Dist; use Sem_Dist;
68 with Sem_Elim; use Sem_Elim;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Prag; use Sem_Prag;
72 with Sem_Res; use Sem_Res;
73 with Sem_Util; use Sem_Util;
74 with Sem_Type; use Sem_Type;
75 with Sem_Warn; use Sem_Warn;
76 with Sinput; use Sinput;
77 with Stand; use Stand;
78 with Sinfo; use Sinfo;
79 with Sinfo.CN; use Sinfo.CN;
80 with Snames; use Snames;
81 with Stringt; use Stringt;
83 with Stylesw; use Stylesw;
84 with Targparm; use Targparm;
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_Null_Procedure
107 Is_Completion : out Boolean);
108 -- A null procedure can be a declaration or (Ada 2012) a completion.
110 procedure Analyze_Return_Statement (N : Node_Id);
111 -- Common processing for simple and extended return statements
113 procedure Analyze_Function_Return (N : Node_Id);
114 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
115 -- applies to a [generic] function.
117 procedure Analyze_Return_Type (N : Node_Id);
118 -- Subsidiary to Process_Formals: analyze subtype mark in function
119 -- specification in a context where the formals are visible and hide
122 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
123 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
124 -- that we can use RETURN but not skip the debug output at the end.
126 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
127 -- Analyze a generic subprogram body. N is the body to be analyzed, and
128 -- Gen_Id is the defining entity Id for the corresponding spec.
130 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
131 -- If a subprogram has pragma Inline and inlining is active, use generic
132 -- machinery to build an unexpanded body for the subprogram. This body is
133 -- subsequently used for inline expansions at call sites. If subprogram can
134 -- be inlined (depending on size and nature of local declarations) this
135 -- function returns true. Otherwise subprogram body is treated normally.
136 -- If proper warnings are enabled and the subprogram contains a construct
137 -- that cannot be inlined, the offending construct is flagged accordingly.
139 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
140 -- Returns true if Subp can override a predefined operator.
142 procedure Check_And_Build_Body_To_Inline
145 Body_Id : Entity_Id);
146 -- Spec_Id and Body_Id are the entities of the specification and body of
147 -- the subprogram body N. If N can be inlined by the frontend (supported
148 -- cases documented in Check_Body_To_Inline) then build the body-to-inline
149 -- associated with N and attach it to the declaration node of Spec_Id.
151 procedure Check_Conformance
154 Ctype : Conformance_Type;
156 Conforms : out Boolean;
157 Err_Loc : Node_Id := Empty;
158 Get_Inst : Boolean := False;
159 Skip_Controlling_Formals : Boolean := False);
160 -- Given two entities, this procedure checks that the profiles associated
161 -- with these entities meet the conformance criterion given by the third
162 -- parameter. If they conform, Conforms is set True and control returns
163 -- to the caller. If they do not conform, Conforms is set to False, and
164 -- in addition, if Errmsg is True on the call, proper messages are output
165 -- to complain about the conformance failure. If Err_Loc is non_Empty
166 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
167 -- error messages are placed on the appropriate part of the construct
168 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
169 -- against a formal access-to-subprogram type so Get_Instance_Of must
172 procedure Check_Subprogram_Order (N : Node_Id);
173 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
174 -- the alpha ordering rule for N if this ordering requirement applicable.
176 procedure Check_Returns
180 Proc : Entity_Id := Empty);
181 -- Called to check for missing return statements in a function body, or for
182 -- returns present in a procedure body which has No_Return set. HSS is the
183 -- handled statement sequence for the subprogram body. This procedure
184 -- checks all flow paths to make sure they either have return (Mode = 'F',
185 -- used for functions) or do not have a return (Mode = 'P', used for
186 -- No_Return procedures). The flag Err is set if there are any control
187 -- paths not explicitly terminated by a return in the function case, and is
188 -- True otherwise. Proc is the entity for the procedure case and is used
189 -- in posting the warning message.
191 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
192 -- In Ada 2012, a primitive equality operator on an untagged record type
193 -- must appear before the type is frozen, and have the same visibility as
194 -- that of the type. This procedure checks that this rule is met, and
195 -- otherwise emits an error on the subprogram declaration and a warning
196 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
197 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
198 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
199 -- is set, otherwise the call has no effect.
201 procedure Enter_Overloaded_Entity (S : Entity_Id);
202 -- This procedure makes S, a new overloaded entity, into the first visible
203 -- entity with that name.
205 function Is_Non_Overriding_Operation
207 New_E : Entity_Id) return Boolean;
208 -- Enforce the rule given in 12.3(18): a private operation in an instance
209 -- overrides an inherited operation only if the corresponding operation
210 -- was overriding in the generic. This needs to be checked for primitive
211 -- operations of types derived (in the generic unit) from formal private
212 -- or formal derived types.
214 procedure Make_Inequality_Operator (S : Entity_Id);
215 -- Create the declaration for an inequality operator that is implicitly
216 -- created by a user-defined equality operator that yields a boolean.
218 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
219 -- Formal_Id is an formal parameter entity. This procedure deals with
220 -- setting the proper validity status for this entity, which depends on
221 -- the kind of parameter and the validity checking mode.
223 ---------------------------------------------
224 -- Analyze_Abstract_Subprogram_Declaration --
225 ---------------------------------------------
227 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
228 Designator : constant Entity_Id :=
229 Analyze_Subprogram_Specification (Specification (N));
230 Scop : constant Entity_Id := Current_Scope;
233 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
235 Generate_Definition (Designator);
236 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
237 Set_Is_Abstract_Subprogram (Designator);
238 New_Overloaded_Entity (Designator);
239 Check_Delayed_Subprogram (Designator);
241 Set_Categorization_From_Scope (Designator, Scop);
243 if Ekind (Scope (Designator)) = E_Protected_Type then
245 ("abstract subprogram not allowed in protected type", N);
247 -- Issue a warning if the abstract subprogram is neither a dispatching
248 -- operation nor an operation that overrides an inherited subprogram or
249 -- predefined operator, since this most likely indicates a mistake.
251 elsif Warn_On_Redundant_Constructs
252 and then not Is_Dispatching_Operation (Designator)
253 and then not Present (Overridden_Operation (Designator))
254 and then (not Is_Operator_Symbol_Name (Chars (Designator))
255 or else Scop /= Scope (Etype (First_Formal (Designator))))
258 ("abstract subprogram is not dispatching or overriding?r?", N);
261 Generate_Reference_To_Formals (Designator);
262 Check_Eliminated (Designator);
264 if Has_Aspects (N) then
265 Analyze_Aspect_Specifications (N, Designator);
267 end Analyze_Abstract_Subprogram_Declaration;
269 ---------------------------------
270 -- Analyze_Expression_Function --
271 ---------------------------------
273 procedure Analyze_Expression_Function (N : Node_Id) is
274 Loc : constant Source_Ptr := Sloc (N);
275 LocX : constant Source_Ptr := Sloc (Expression (N));
276 Expr : constant Node_Id := Expression (N);
277 Spec : constant Node_Id := Specification (N);
282 -- If the expression is a completion, Prev is the entity whose
283 -- declaration is completed. Def_Id is needed to analyze the spec.
291 -- This is one of the occasions on which we transform the tree during
292 -- semantic analysis. If this is a completion, transform the expression
293 -- function into an equivalent subprogram body, and analyze it.
295 -- Expression functions are inlined unconditionally. The back-end will
296 -- determine whether this is possible.
298 Inline_Processing_Required := True;
300 -- Create a specification for the generated body. Types and defauts in
301 -- the profile are copies of the spec, but new entities must be created
302 -- for the unit name and the formals.
304 New_Spec := New_Copy_Tree (Spec);
305 Set_Defining_Unit_Name (New_Spec,
306 Make_Defining_Identifier (Sloc (Defining_Unit_Name (Spec)),
307 Chars (Defining_Unit_Name (Spec))));
309 if Present (Parameter_Specifications (New_Spec)) then
311 Formal_Spec : Node_Id;
315 Formal_Spec := First (Parameter_Specifications (New_Spec));
317 -- Create a new formal parameter at the same source position
319 while Present (Formal_Spec) loop
320 Def := Defining_Identifier (Formal_Spec);
321 Set_Defining_Identifier (Formal_Spec,
322 Make_Defining_Identifier (Sloc (Def),
323 Chars => Chars (Def)));
329 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
331 -- If there are previous overloadable entities with the same name,
332 -- check whether any of them is completed by the expression function.
334 if Present (Prev) and then Is_Overloadable (Prev) then
335 Def_Id := Analyze_Subprogram_Specification (Spec);
336 Prev := Find_Corresponding_Spec (N);
339 Ret := Make_Simple_Return_Statement (LocX, Expression (N));
342 Make_Subprogram_Body (Loc,
343 Specification => New_Spec,
344 Declarations => Empty_List,
345 Handled_Statement_Sequence =>
346 Make_Handled_Sequence_Of_Statements (LocX,
347 Statements => New_List (Ret)));
349 -- If the expression completes a generic subprogram, we must create a
350 -- separate node for the body, because at instantiation the original
351 -- node of the generic copy must be a generic subprogram body, and
352 -- cannot be a expression function. Otherwise we just rewrite the
353 -- expression with the non-generic body.
355 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
356 Insert_After (N, New_Body);
358 -- Propagate any aspects or pragmas that apply to the expression
359 -- function to the proper body when the expression function acts
362 if Has_Aspects (N) then
363 Move_Aspects (N, To => New_Body);
366 Relocate_Pragmas_To_Body (New_Body);
368 Rewrite (N, Make_Null_Statement (Loc));
369 Set_Has_Completion (Prev, False);
372 Set_Is_Inlined (Prev);
374 elsif Present (Prev) and then Comes_From_Source (Prev) then
375 Set_Has_Completion (Prev, False);
377 -- An expression function that is a completion freezes the
378 -- expression. This means freezing the return type, and if it is
379 -- an access type, freezing its designated type as well.
381 -- Note that we cannot defer this freezing to the analysis of the
382 -- expression itself, because a freeze node might appear in a nested
383 -- scope, leading to an elaboration order issue in gigi.
385 Freeze_Before (N, Etype (Prev));
387 if Is_Access_Type (Etype (Prev)) then
388 Freeze_Before (N, Designated_Type (Etype (Prev)));
391 -- For navigation purposes, indicate that the function is a body
393 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
394 Rewrite (N, New_Body);
396 -- Correct the parent pointer of the aspect specification list to
397 -- reference the rewritten node.
399 if Has_Aspects (N) then
400 Set_Parent (Aspect_Specifications (N), N);
403 -- Propagate any pragmas that apply to the expression function to the
404 -- proper body when the expression function acts as a completion.
405 -- Aspects are automatically transfered because of node rewriting.
407 Relocate_Pragmas_To_Body (N);
410 -- Prev is the previous entity with the same name, but it is can
411 -- be an unrelated spec that is not completed by the expression
412 -- function. In that case the relevant entity is the one in the body.
413 -- Not clear that the backend can inline it in this case ???
415 if Has_Completion (Prev) then
416 Set_Is_Inlined (Prev);
418 -- The formals of the expression function are body formals,
419 -- and do not appear in the ali file, which will only contain
420 -- references to the formals of the original subprogram spec.
427 F1 := First_Formal (Def_Id);
428 F2 := First_Formal (Prev);
430 while Present (F1) loop
431 Set_Spec_Entity (F1, F2);
438 Set_Is_Inlined (Defining_Entity (New_Body));
441 -- If this is not a completion, create both a declaration and a body, so
442 -- that the expression can be inlined whenever possible.
445 -- An expression function that is not a completion is not a
446 -- subprogram declaration, and thus cannot appear in a protected
449 if Nkind (Parent (N)) = N_Protected_Definition then
451 ("an expression function is not a legal protected operation", N);
455 Make_Subprogram_Declaration (Loc, Specification => Spec);
457 Rewrite (N, New_Decl);
459 -- Correct the parent pointer of the aspect specification list to
460 -- reference the rewritten node.
462 if Has_Aspects (N) then
463 Set_Parent (Aspect_Specifications (N), N);
467 Set_Is_Inlined (Defining_Entity (New_Decl));
469 -- To prevent premature freeze action, insert the new body at the end
470 -- of the current declarations, or at the end of the package spec.
471 -- However, resolve usage names now, to prevent spurious visibility
472 -- on later entities. Note that the function can now be called in
473 -- the current declarative part, which will appear to be prior to
474 -- the presence of the body in the code. There are nevertheless no
475 -- order of elaboration issues because all name resolution has taken
476 -- place at the point of declaration.
479 Decls : List_Id := List_Containing (N);
480 Par : constant Node_Id := Parent (Decls);
481 Id : constant Entity_Id := Defining_Entity (New_Decl);
484 if Nkind (Par) = N_Package_Specification
485 and then Decls = Visible_Declarations (Par)
486 and then Present (Private_Declarations (Par))
487 and then not Is_Empty_List (Private_Declarations (Par))
489 Decls := Private_Declarations (Par);
492 Insert_After (Last (Decls), New_Body);
494 Install_Formals (Id);
496 -- Preanalyze the expression for name capture, except in an
497 -- instance, where this has been done during generic analysis,
498 -- and will be redone when analyzing the body.
501 Expr : constant Node_Id := Expression (Ret);
504 Set_Parent (Expr, Ret);
506 if not In_Instance then
507 Preanalyze_Spec_Expression (Expr, Etype (Id));
515 -- If the return expression is a static constant, we suppress warning
516 -- messages on unused formals, which in most cases will be noise.
518 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
519 Is_OK_Static_Expression (Expr));
520 end Analyze_Expression_Function;
522 ----------------------------------------
523 -- Analyze_Extended_Return_Statement --
524 ----------------------------------------
526 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
528 Analyze_Return_Statement (N);
529 end Analyze_Extended_Return_Statement;
531 ----------------------------
532 -- Analyze_Function_Call --
533 ----------------------------
535 procedure Analyze_Function_Call (N : Node_Id) is
536 Actuals : constant List_Id := Parameter_Associations (N);
537 Func_Nam : constant Node_Id := Name (N);
543 -- A call of the form A.B (X) may be an Ada 2005 call, which is
544 -- rewritten as B (A, X). If the rewriting is successful, the call
545 -- has been analyzed and we just return.
547 if Nkind (Func_Nam) = N_Selected_Component
548 and then Name (N) /= Func_Nam
549 and then Is_Rewrite_Substitution (N)
550 and then Present (Etype (N))
555 -- If error analyzing name, then set Any_Type as result type and return
557 if Etype (Func_Nam) = Any_Type then
558 Set_Etype (N, Any_Type);
562 -- Otherwise analyze the parameters
564 if Present (Actuals) then
565 Actual := First (Actuals);
566 while Present (Actual) loop
568 Check_Parameterless_Call (Actual);
574 end Analyze_Function_Call;
576 -----------------------------
577 -- Analyze_Function_Return --
578 -----------------------------
580 procedure Analyze_Function_Return (N : Node_Id) is
581 Loc : constant Source_Ptr := Sloc (N);
582 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
583 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
585 R_Type : constant Entity_Id := Etype (Scope_Id);
586 -- Function result subtype
588 procedure Check_Limited_Return (Expr : Node_Id);
589 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
590 -- limited types. Used only for simple return statements.
591 -- Expr is the expression returned.
593 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
594 -- Check that the return_subtype_indication properly matches the result
595 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
597 --------------------------
598 -- Check_Limited_Return --
599 --------------------------
601 procedure Check_Limited_Return (Expr : Node_Id) is
603 -- Ada 2005 (AI-318-02): Return-by-reference types have been
604 -- removed and replaced by anonymous access results. This is an
605 -- incompatibility with Ada 95. Not clear whether this should be
606 -- enforced yet or perhaps controllable with special switch. ???
608 -- A limited interface that is not immutably limited is OK.
610 if Is_Limited_Interface (R_Type)
612 not (Is_Task_Interface (R_Type)
613 or else Is_Protected_Interface (R_Type)
614 or else Is_Synchronized_Interface (R_Type))
618 elsif Is_Limited_Type (R_Type)
619 and then not Is_Interface (R_Type)
620 and then Comes_From_Source (N)
621 and then not In_Instance_Body
622 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
626 if Ada_Version >= Ada_2005
627 and then not Debug_Flag_Dot_L
628 and then not GNAT_Mode
631 ("(Ada 2005) cannot copy object of a limited type " &
632 "(RM-2005 6.5(5.5/2))", Expr);
634 if Is_Limited_View (R_Type) then
636 ("\return by reference not permitted in Ada 2005", Expr);
639 -- Warn in Ada 95 mode, to give folks a heads up about this
642 -- In GNAT mode, this is just a warning, to allow it to be
643 -- evilly turned off. Otherwise it is a real error.
645 -- In a generic context, simplify the warning because it makes
646 -- no sense to discuss pass-by-reference or copy.
648 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
649 if Inside_A_Generic then
651 ("return of limited object not permitted in Ada 2005 "
652 & "(RM-2005 6.5(5.5/2))?y?", Expr);
654 elsif Is_Limited_View (R_Type) then
656 ("return by reference not permitted in Ada 2005 "
657 & "(RM-2005 6.5(5.5/2))?y?", Expr);
660 ("cannot copy object of a limited type in Ada 2005 "
661 & "(RM-2005 6.5(5.5/2))?y?", Expr);
664 -- Ada 95 mode, compatibility warnings disabled
667 return; -- skip continuation messages below
670 if not Inside_A_Generic then
672 ("\consider switching to return of access type", Expr);
673 Explain_Limited_Type (R_Type, Expr);
676 end Check_Limited_Return;
678 -------------------------------------
679 -- Check_Return_Subtype_Indication --
680 -------------------------------------
682 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
683 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
685 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
686 -- Subtype given in the extended return statement (must match R_Type)
688 Subtype_Ind : constant Node_Id :=
689 Object_Definition (Original_Node (Obj_Decl));
691 R_Type_Is_Anon_Access : constant Boolean :=
693 E_Anonymous_Access_Subprogram_Type,
694 E_Anonymous_Access_Protected_Subprogram_Type,
695 E_Anonymous_Access_Type);
696 -- True if return type of the function is an anonymous access type
697 -- Can't we make Is_Anonymous_Access_Type in einfo ???
699 R_Stm_Type_Is_Anon_Access : constant Boolean :=
700 Ekind_In (R_Stm_Type,
701 E_Anonymous_Access_Subprogram_Type,
702 E_Anonymous_Access_Protected_Subprogram_Type,
703 E_Anonymous_Access_Type);
704 -- True if type of the return object is an anonymous access type
706 procedure Error_No_Match (N : Node_Id);
707 -- Output error messages for case where types do not statically
708 -- match. N is the location for the messages.
714 procedure Error_No_Match (N : Node_Id) is
717 ("subtype must statically match function result subtype", N);
719 if not Predicates_Match (R_Stm_Type, R_Type) then
720 Error_Msg_Node_2 := R_Type;
722 ("\predicate of & does not match predicate of &",
727 -- Start of processing for Check_Return_Subtype_Indication
730 -- First, avoid cascaded errors
732 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
736 -- "return access T" case; check that the return statement also has
737 -- "access T", and that the subtypes statically match:
738 -- if this is an access to subprogram the signatures must match.
740 if R_Type_Is_Anon_Access then
741 if R_Stm_Type_Is_Anon_Access then
743 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
745 if Base_Type (Designated_Type (R_Stm_Type)) /=
746 Base_Type (Designated_Type (R_Type))
747 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
749 Error_No_Match (Subtype_Mark (Subtype_Ind));
753 -- For two anonymous access to subprogram types, the
754 -- types themselves must be type conformant.
756 if not Conforming_Types
757 (R_Stm_Type, R_Type, Fully_Conformant)
759 Error_No_Match (Subtype_Ind);
764 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
767 -- If the return object is of an anonymous access type, then report
768 -- an error if the function's result type is not also anonymous.
770 elsif R_Stm_Type_Is_Anon_Access
771 and then not R_Type_Is_Anon_Access
773 Error_Msg_N ("anonymous access not allowed for function with " &
774 "named access result", Subtype_Ind);
776 -- Subtype indication case: check that the return object's type is
777 -- covered by the result type, and that the subtypes statically match
778 -- when the result subtype is constrained. Also handle record types
779 -- with unknown discriminants for which we have built the underlying
780 -- record view. Coverage is needed to allow specific-type return
781 -- objects when the result type is class-wide (see AI05-32).
783 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
784 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
788 Underlying_Record_View (Base_Type (R_Stm_Type))))
790 -- A null exclusion may be present on the return type, on the
791 -- function specification, on the object declaration or on the
794 if Is_Access_Type (R_Type)
796 (Can_Never_Be_Null (R_Type)
797 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
798 Can_Never_Be_Null (R_Stm_Type)
800 Error_No_Match (Subtype_Ind);
803 -- AI05-103: for elementary types, subtypes must statically match
805 if Is_Constrained (R_Type)
806 or else Is_Access_Type (R_Type)
808 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
809 Error_No_Match (Subtype_Ind);
813 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
814 and then Is_Null_Extension (Base_Type (R_Type))
820 ("wrong type for return_subtype_indication", Subtype_Ind);
822 end Check_Return_Subtype_Indication;
824 ---------------------
825 -- Local Variables --
826 ---------------------
830 -- Start of processing for Analyze_Function_Return
833 Set_Return_Present (Scope_Id);
835 if Nkind (N) = N_Simple_Return_Statement then
836 Expr := Expression (N);
838 -- Guard against a malformed expression. The parser may have tried to
839 -- recover but the node is not analyzable.
841 if Nkind (Expr) = N_Error then
842 Set_Etype (Expr, Any_Type);
843 Expander_Mode_Save_And_Set (False);
847 -- The resolution of a controlled [extension] aggregate associated
848 -- with a return statement creates a temporary which needs to be
849 -- finalized on function exit. Wrap the return statement inside a
850 -- block so that the finalization machinery can detect this case.
851 -- This early expansion is done only when the return statement is
852 -- not part of a handled sequence of statements.
854 if Nkind_In (Expr, N_Aggregate,
855 N_Extension_Aggregate)
856 and then Needs_Finalization (R_Type)
857 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
860 Make_Block_Statement (Loc,
861 Handled_Statement_Sequence =>
862 Make_Handled_Sequence_Of_Statements (Loc,
863 Statements => New_List (Relocate_Node (N)))));
869 Analyze_And_Resolve (Expr, R_Type);
870 Check_Limited_Return (Expr);
873 -- RETURN only allowed in SPARK as the last statement in function
875 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
877 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
878 or else Present (Next (N)))
880 Check_SPARK_Restriction
881 ("RETURN should be the last statement in function", N);
885 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
887 -- Analyze parts specific to extended_return_statement:
890 Obj_Decl : constant Node_Id :=
891 Last (Return_Object_Declarations (N));
892 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
893 HSS : constant Node_Id := Handled_Statement_Sequence (N);
896 Expr := Expression (Obj_Decl);
898 -- Note: The check for OK_For_Limited_Init will happen in
899 -- Analyze_Object_Declaration; we treat it as a normal
900 -- object declaration.
902 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
905 Check_Return_Subtype_Indication (Obj_Decl);
907 if Present (HSS) then
910 if Present (Exception_Handlers (HSS)) then
912 -- ???Has_Nested_Block_With_Handler needs to be set.
913 -- Probably by creating an actual N_Block_Statement.
914 -- Probably in Expand.
920 -- Mark the return object as referenced, since the return is an
921 -- implicit reference of the object.
923 Set_Referenced (Defining_Identifier (Obj_Decl));
925 Check_References (Stm_Entity);
927 -- Check RM 6.5 (5.9/3)
930 if Ada_Version < Ada_2012 then
932 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
933 -- Can it really happen (extended return???)
936 ("aliased only allowed for limited return objects "
937 & "in Ada 2012??", N);
939 elsif not Is_Limited_View (R_Type) then
940 Error_Msg_N ("aliased only allowed for limited"
941 & " return objects", N);
947 -- Case of Expr present
951 -- Defend against previous errors
953 and then Nkind (Expr) /= N_Empty
954 and then Present (Etype (Expr))
956 -- Apply constraint check. Note that this is done before the implicit
957 -- conversion of the expression done for anonymous access types to
958 -- ensure correct generation of the null-excluding check associated
959 -- with null-excluding expressions found in return statements.
961 Apply_Constraint_Check (Expr, R_Type);
963 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
964 -- type, apply an implicit conversion of the expression to that type
965 -- to force appropriate static and run-time accessibility checks.
967 if Ada_Version >= Ada_2005
968 and then Ekind (R_Type) = E_Anonymous_Access_Type
970 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
971 Analyze_And_Resolve (Expr, R_Type);
973 -- If this is a local anonymous access to subprogram, the
974 -- accessibility check can be applied statically. The return is
975 -- illegal if the access type of the return expression is declared
976 -- inside of the subprogram (except if it is the subtype indication
977 -- of an extended return statement).
979 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
980 if not Comes_From_Source (Current_Scope)
981 or else Ekind (Current_Scope) = E_Return_Statement
986 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
988 Error_Msg_N ("cannot return local access to subprogram", N);
992 -- If the result type is class-wide, then check that the return
993 -- expression's type is not declared at a deeper level than the
994 -- function (RM05-6.5(5.6/2)).
996 if Ada_Version >= Ada_2005
997 and then Is_Class_Wide_Type (R_Type)
999 if Type_Access_Level (Etype (Expr)) >
1000 Subprogram_Access_Level (Scope_Id)
1003 ("level of return expression type is deeper than " &
1004 "class-wide function!", Expr);
1008 -- Check incorrect use of dynamically tagged expression
1010 if Is_Tagged_Type (R_Type) then
1011 Check_Dynamically_Tagged_Expression
1017 -- ??? A real run-time accessibility check is needed in cases
1018 -- involving dereferences of access parameters. For now we just
1019 -- check the static cases.
1021 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1022 and then Is_Limited_View (Etype (Scope_Id))
1023 and then Object_Access_Level (Expr) >
1024 Subprogram_Access_Level (Scope_Id)
1026 -- Suppress the message in a generic, where the rewriting
1029 if Inside_A_Generic then
1034 Make_Raise_Program_Error (Loc,
1035 Reason => PE_Accessibility_Check_Failed));
1038 Error_Msg_Warn := SPARK_Mode /= On;
1039 Error_Msg_N ("cannot return a local value by reference<<", N);
1040 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1044 if Known_Null (Expr)
1045 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1046 and then Null_Exclusion_Present (Parent (Scope_Id))
1048 Apply_Compile_Time_Constraint_Error
1050 Msg => "(Ada 2005) null not allowed for "
1051 & "null-excluding return??",
1052 Reason => CE_Null_Not_Allowed);
1055 end Analyze_Function_Return;
1057 -------------------------------------
1058 -- Analyze_Generic_Subprogram_Body --
1059 -------------------------------------
1061 procedure Analyze_Generic_Subprogram_Body
1065 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1066 Kind : constant Entity_Kind := Ekind (Gen_Id);
1067 Body_Id : Entity_Id;
1072 -- Copy body and disable expansion while analyzing the generic For a
1073 -- stub, do not copy the stub (which would load the proper body), this
1074 -- will be done when the proper body is analyzed.
1076 if Nkind (N) /= N_Subprogram_Body_Stub then
1077 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1082 Spec := Specification (N);
1084 -- Within the body of the generic, the subprogram is callable, and
1085 -- behaves like the corresponding non-generic unit.
1087 Body_Id := Defining_Entity (Spec);
1089 if Kind = E_Generic_Procedure
1090 and then Nkind (Spec) /= N_Procedure_Specification
1092 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1095 elsif Kind = E_Generic_Function
1096 and then Nkind (Spec) /= N_Function_Specification
1098 Error_Msg_N ("invalid body for generic function ", Body_Id);
1102 Set_Corresponding_Body (Gen_Decl, Body_Id);
1104 if Has_Completion (Gen_Id)
1105 and then Nkind (Parent (N)) /= N_Subunit
1107 Error_Msg_N ("duplicate generic body", N);
1110 Set_Has_Completion (Gen_Id);
1113 if Nkind (N) = N_Subprogram_Body_Stub then
1114 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1116 Set_Corresponding_Spec (N, Gen_Id);
1119 if Nkind (Parent (N)) = N_Compilation_Unit then
1120 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1123 -- Make generic parameters immediately visible in the body. They are
1124 -- needed to process the formals declarations. Then make the formals
1125 -- visible in a separate step.
1127 Push_Scope (Gen_Id);
1131 First_Ent : Entity_Id;
1134 First_Ent := First_Entity (Gen_Id);
1137 while Present (E) and then not Is_Formal (E) loop
1142 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1144 -- Now generic formals are visible, and the specification can be
1145 -- analyzed, for subsequent conformance check.
1147 Body_Id := Analyze_Subprogram_Specification (Spec);
1149 -- Make formal parameters visible
1153 -- E is the first formal parameter, we loop through the formals
1154 -- installing them so that they will be visible.
1156 Set_First_Entity (Gen_Id, E);
1157 while Present (E) loop
1163 -- Visible generic entity is callable within its own body
1165 Set_Ekind (Gen_Id, Ekind (Body_Id));
1166 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
1167 Set_Ekind (Body_Id, E_Subprogram_Body);
1168 Set_Convention (Body_Id, Convention (Gen_Id));
1169 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1170 Set_Scope (Body_Id, Scope (Gen_Id));
1171 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1173 if Nkind (N) = N_Subprogram_Body_Stub then
1175 -- No body to analyze, so restore state of generic unit
1177 Set_Ekind (Gen_Id, Kind);
1178 Set_Ekind (Body_Id, Kind);
1180 if Present (First_Ent) then
1181 Set_First_Entity (Gen_Id, First_Ent);
1188 -- If this is a compilation unit, it must be made visible explicitly,
1189 -- because the compilation of the declaration, unlike other library
1190 -- unit declarations, does not. If it is not a unit, the following
1191 -- is redundant but harmless.
1193 Set_Is_Immediately_Visible (Gen_Id);
1194 Reference_Body_Formals (Gen_Id, Body_Id);
1196 if Is_Child_Unit (Gen_Id) then
1197 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1200 Set_Actual_Subtypes (N, Current_Scope);
1202 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
1203 -- invariants and predicates associated with the body and its spec.
1204 -- Note that this is not pure expansion as Expand_Subprogram_Contract
1205 -- prepares the contract assertions for generic subprograms or for
1206 -- ASIS. Do not generate contract checks in SPARK mode.
1208 if not GNATprove_Mode then
1209 Expand_Subprogram_Contract (N, Gen_Id, Body_Id);
1212 -- If the generic unit carries pre- or post-conditions, copy them
1213 -- to the original generic tree, so that they are properly added
1214 -- to any instantiation.
1217 Orig : constant Node_Id := Original_Node (N);
1221 Cond := First (Declarations (N));
1222 while Present (Cond) loop
1223 if Nkind (Cond) = N_Pragma
1224 and then Pragma_Name (Cond) = Name_Check
1226 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1228 elsif Nkind (Cond) = N_Pragma
1229 and then Pragma_Name (Cond) = Name_Postcondition
1231 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1232 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1241 Check_SPARK_Mode_In_Generic (N);
1243 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1244 Set_SPARK_Pragma_Inherited (Body_Id, True);
1246 Analyze_Declarations (Declarations (N));
1248 Analyze (Handled_Statement_Sequence (N));
1250 Save_Global_References (Original_Node (N));
1252 -- Prior to exiting the scope, include generic formals again (if any
1253 -- are present) in the set of local entities.
1255 if Present (First_Ent) then
1256 Set_First_Entity (Gen_Id, First_Ent);
1259 Check_References (Gen_Id);
1262 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1264 Check_Subprogram_Order (N);
1266 -- Outside of its body, unit is generic again
1268 Set_Ekind (Gen_Id, Kind);
1269 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1272 Style.Check_Identifier (Body_Id, Gen_Id);
1276 end Analyze_Generic_Subprogram_Body;
1278 ----------------------------
1279 -- Analyze_Null_Procedure --
1280 ----------------------------
1282 procedure Analyze_Null_Procedure
1284 Is_Completion : out Boolean)
1286 Loc : constant Source_Ptr := Sloc (N);
1287 Spec : constant Node_Id := Specification (N);
1288 Designator : Entity_Id;
1290 Null_Body : Node_Id := Empty;
1294 -- Capture the profile of the null procedure before analysis, for
1295 -- expansion at the freeze point and at each point of call. The body is
1296 -- used if the procedure has preconditions, or if it is a completion. In
1297 -- the first case the body is analyzed at the freeze point, in the other
1298 -- it replaces the null procedure declaration.
1301 Make_Subprogram_Body (Loc,
1302 Specification => New_Copy_Tree (Spec),
1303 Declarations => New_List,
1304 Handled_Statement_Sequence =>
1305 Make_Handled_Sequence_Of_Statements (Loc,
1306 Statements => New_List (Make_Null_Statement (Loc))));
1308 -- Create new entities for body and formals
1310 Set_Defining_Unit_Name (Specification (Null_Body),
1311 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
1313 Form := First (Parameter_Specifications (Specification (Null_Body)));
1314 while Present (Form) loop
1315 Set_Defining_Identifier (Form,
1316 Make_Defining_Identifier (Loc, Chars (Defining_Identifier (Form))));
1320 -- Determine whether the null procedure may be a completion of a generic
1321 -- suprogram, in which case we use the new null body as the completion
1322 -- and set minimal semantic information on the original declaration,
1323 -- which is rewritten as a null statement.
1325 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1327 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1328 Insert_Before (N, Null_Body);
1329 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1330 Set_Contract (Defining_Entity (N), Make_Contract (Loc));
1332 Rewrite (N, Make_Null_Statement (Loc));
1333 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1334 Is_Completion := True;
1338 -- Resolve the types of the formals now, because the freeze point
1339 -- may appear in a different context, e.g. an instantiation.
1341 Form := First (Parameter_Specifications (Specification (Null_Body)));
1342 while Present (Form) loop
1343 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1344 Find_Type (Parameter_Type (Form));
1347 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
1349 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1352 -- The case of a null procedure with a formal that is an
1353 -- access_to_subprogram type, and that is used as an actual
1354 -- in an instantiation is left to the enthusiastic reader.
1363 -- If there are previous overloadable entities with the same name,
1364 -- check whether any of them is completed by the null procedure.
1366 if Present (Prev) and then Is_Overloadable (Prev) then
1367 Designator := Analyze_Subprogram_Specification (Spec);
1368 Prev := Find_Corresponding_Spec (N);
1371 if No (Prev) or else not Comes_From_Source (Prev) then
1372 Designator := Analyze_Subprogram_Specification (Spec);
1373 Set_Has_Completion (Designator);
1375 -- Signal to caller that this is a procedure declaration
1377 Is_Completion := False;
1379 -- Null procedures are always inlined, but generic formal subprograms
1380 -- which appear as such in the internal instance of formal packages,
1381 -- need no completion and are not marked Inline.
1384 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1386 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1387 Set_Body_To_Inline (N, Null_Body);
1388 Set_Is_Inlined (Designator);
1392 -- The null procedure is a completion
1394 Is_Completion := True;
1396 if Expander_Active then
1397 Rewrite (N, Null_Body);
1401 Designator := Analyze_Subprogram_Specification (Spec);
1402 Set_Has_Completion (Designator);
1403 Set_Has_Completion (Prev);
1406 end Analyze_Null_Procedure;
1408 -----------------------------
1409 -- Analyze_Operator_Symbol --
1410 -----------------------------
1412 -- An operator symbol such as "+" or "and" may appear in context where the
1413 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1414 -- is just a string, as in (conjunction = "or"). In these cases the parser
1415 -- generates this node, and the semantics does the disambiguation. Other
1416 -- such case are actuals in an instantiation, the generic unit in an
1417 -- instantiation, and pragma arguments.
1419 procedure Analyze_Operator_Symbol (N : Node_Id) is
1420 Par : constant Node_Id := Parent (N);
1423 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1424 or else Nkind (Par) = N_Function_Instantiation
1425 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1426 or else (Nkind (Par) = N_Pragma_Argument_Association
1427 and then not Is_Pragma_String_Literal (Par))
1428 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1429 or else (Nkind (Par) = N_Attribute_Reference
1430 and then Attribute_Name (Par) /= Name_Value)
1432 Find_Direct_Name (N);
1435 Change_Operator_Symbol_To_String_Literal (N);
1438 end Analyze_Operator_Symbol;
1440 -----------------------------------
1441 -- Analyze_Parameter_Association --
1442 -----------------------------------
1444 procedure Analyze_Parameter_Association (N : Node_Id) is
1446 Analyze (Explicit_Actual_Parameter (N));
1447 end Analyze_Parameter_Association;
1449 ----------------------------
1450 -- Analyze_Procedure_Call --
1451 ----------------------------
1453 procedure Analyze_Procedure_Call (N : Node_Id) is
1454 Loc : constant Source_Ptr := Sloc (N);
1455 P : constant Node_Id := Name (N);
1456 Actuals : constant List_Id := Parameter_Associations (N);
1460 procedure Analyze_Call_And_Resolve;
1461 -- Do Analyze and Resolve calls for procedure call
1462 -- At end, check illegal order dependence.
1464 ------------------------------
1465 -- Analyze_Call_And_Resolve --
1466 ------------------------------
1468 procedure Analyze_Call_And_Resolve is
1470 if Nkind (N) = N_Procedure_Call_Statement then
1472 Resolve (N, Standard_Void_Type);
1476 end Analyze_Call_And_Resolve;
1478 -- Start of processing for Analyze_Procedure_Call
1481 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1482 -- a procedure call or an entry call. The prefix may denote an access
1483 -- to subprogram type, in which case an implicit dereference applies.
1484 -- If the prefix is an indexed component (without implicit dereference)
1485 -- then the construct denotes a call to a member of an entire family.
1486 -- If the prefix is a simple name, it may still denote a call to a
1487 -- parameterless member of an entry family. Resolution of these various
1488 -- interpretations is delicate.
1492 -- If this is a call of the form Obj.Op, the call may have been
1493 -- analyzed and possibly rewritten into a block, in which case
1496 if Analyzed (N) then
1500 -- If there is an error analyzing the name (which may have been
1501 -- rewritten if the original call was in prefix notation) then error
1502 -- has been emitted already, mark node and return.
1504 if Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1505 Set_Etype (N, Any_Type);
1509 -- Otherwise analyze the parameters
1511 if Present (Actuals) then
1512 Actual := First (Actuals);
1514 while Present (Actual) loop
1516 Check_Parameterless_Call (Actual);
1521 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1523 if Nkind (P) = N_Attribute_Reference
1524 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1526 Name_Elab_Subp_Body)
1528 if Present (Actuals) then
1530 ("no parameters allowed for this call", First (Actuals));
1534 Set_Etype (N, Standard_Void_Type);
1537 elsif Is_Entity_Name (P)
1538 and then Is_Record_Type (Etype (Entity (P)))
1539 and then Remote_AST_I_Dereference (P)
1543 elsif Is_Entity_Name (P)
1544 and then Ekind (Entity (P)) /= E_Entry_Family
1546 if Is_Access_Type (Etype (P))
1547 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1548 and then No (Actuals)
1549 and then Comes_From_Source (N)
1551 Error_Msg_N ("missing explicit dereference in call", N);
1554 Analyze_Call_And_Resolve;
1556 -- If the prefix is the simple name of an entry family, this is
1557 -- a parameterless call from within the task body itself.
1559 elsif Is_Entity_Name (P)
1560 and then Nkind (P) = N_Identifier
1561 and then Ekind (Entity (P)) = E_Entry_Family
1562 and then Present (Actuals)
1563 and then No (Next (First (Actuals)))
1565 -- Can be call to parameterless entry family. What appears to be the
1566 -- sole argument is in fact the entry index. Rewrite prefix of node
1567 -- accordingly. Source representation is unchanged by this
1571 Make_Indexed_Component (Loc,
1573 Make_Selected_Component (Loc,
1574 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1575 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1576 Expressions => Actuals);
1577 Set_Name (N, New_N);
1578 Set_Etype (New_N, Standard_Void_Type);
1579 Set_Parameter_Associations (N, No_List);
1580 Analyze_Call_And_Resolve;
1582 elsif Nkind (P) = N_Explicit_Dereference then
1583 if Ekind (Etype (P)) = E_Subprogram_Type then
1584 Analyze_Call_And_Resolve;
1586 Error_Msg_N ("expect access to procedure in call", P);
1589 -- The name can be a selected component or an indexed component that
1590 -- yields an access to subprogram. Such a prefix is legal if the call
1591 -- has parameter associations.
1593 elsif Is_Access_Type (Etype (P))
1594 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1596 if Present (Actuals) then
1597 Analyze_Call_And_Resolve;
1599 Error_Msg_N ("missing explicit dereference in call ", N);
1602 -- If not an access to subprogram, then the prefix must resolve to the
1603 -- name of an entry, entry family, or protected operation.
1605 -- For the case of a simple entry call, P is a selected component where
1606 -- the prefix is the task and the selector name is the entry. A call to
1607 -- a protected procedure will have the same syntax. If the protected
1608 -- object contains overloaded operations, the entity may appear as a
1609 -- function, the context will select the operation whose type is Void.
1611 elsif Nkind (P) = N_Selected_Component
1612 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1616 Analyze_Call_And_Resolve;
1618 elsif Nkind (P) = N_Selected_Component
1619 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1620 and then Present (Actuals)
1621 and then No (Next (First (Actuals)))
1623 -- Can be call to parameterless entry family. What appears to be the
1624 -- sole argument is in fact the entry index. Rewrite prefix of node
1625 -- accordingly. Source representation is unchanged by this
1629 Make_Indexed_Component (Loc,
1630 Prefix => New_Copy (P),
1631 Expressions => Actuals);
1632 Set_Name (N, New_N);
1633 Set_Etype (New_N, Standard_Void_Type);
1634 Set_Parameter_Associations (N, No_List);
1635 Analyze_Call_And_Resolve;
1637 -- For the case of a reference to an element of an entry family, P is
1638 -- an indexed component whose prefix is a selected component (task and
1639 -- entry family), and whose index is the entry family index.
1641 elsif Nkind (P) = N_Indexed_Component
1642 and then Nkind (Prefix (P)) = N_Selected_Component
1643 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1645 Analyze_Call_And_Resolve;
1647 -- If the prefix is the name of an entry family, it is a call from
1648 -- within the task body itself.
1650 elsif Nkind (P) = N_Indexed_Component
1651 and then Nkind (Prefix (P)) = N_Identifier
1652 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1655 Make_Selected_Component (Loc,
1656 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1657 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1658 Rewrite (Prefix (P), New_N);
1660 Analyze_Call_And_Resolve;
1662 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1663 -- procedure name, so the construct can only be a qualified expression.
1665 elsif Nkind (P) = N_Qualified_Expression
1666 and then Ada_Version >= Ada_2012
1668 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1671 -- Anything else is an error
1674 Error_Msg_N ("invalid procedure or entry call", N);
1676 end Analyze_Procedure_Call;
1678 ------------------------------
1679 -- Analyze_Return_Statement --
1680 ------------------------------
1682 procedure Analyze_Return_Statement (N : Node_Id) is
1684 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1685 N_Extended_Return_Statement));
1687 Returns_Object : constant Boolean :=
1688 Nkind (N) = N_Extended_Return_Statement
1690 (Nkind (N) = N_Simple_Return_Statement
1691 and then Present (Expression (N)));
1692 -- True if we're returning something; that is, "return <expression>;"
1693 -- or "return Result : T [:= ...]". False for "return;". Used for error
1694 -- checking: If Returns_Object is True, N should apply to a function
1695 -- body; otherwise N should apply to a procedure body, entry body,
1696 -- accept statement, or extended return statement.
1698 function Find_What_It_Applies_To return Entity_Id;
1699 -- Find the entity representing the innermost enclosing body, accept
1700 -- statement, or extended return statement. If the result is a callable
1701 -- construct or extended return statement, then this will be the value
1702 -- of the Return_Applies_To attribute. Otherwise, the program is
1703 -- illegal. See RM-6.5(4/2).
1705 -----------------------------
1706 -- Find_What_It_Applies_To --
1707 -----------------------------
1709 function Find_What_It_Applies_To return Entity_Id is
1710 Result : Entity_Id := Empty;
1713 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1714 -- and postconditions.
1716 for J in reverse 0 .. Scope_Stack.Last loop
1717 Result := Scope_Stack.Table (J).Entity;
1718 exit when not Ekind_In (Result, E_Block, E_Loop)
1719 and then Chars (Result) /= Name_uPostconditions;
1722 pragma Assert (Present (Result));
1724 end Find_What_It_Applies_To;
1726 -- Local declarations
1728 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1729 Kind : constant Entity_Kind := Ekind (Scope_Id);
1730 Loc : constant Source_Ptr := Sloc (N);
1731 Stm_Entity : constant Entity_Id :=
1733 (E_Return_Statement, Current_Scope, Loc, 'R');
1735 -- Start of processing for Analyze_Return_Statement
1738 Set_Return_Statement_Entity (N, Stm_Entity);
1740 Set_Etype (Stm_Entity, Standard_Void_Type);
1741 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1743 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1744 -- (4/2): an inner return statement will apply to this extended return.
1746 if Nkind (N) = N_Extended_Return_Statement then
1747 Push_Scope (Stm_Entity);
1750 -- Check that pragma No_Return is obeyed. Don't complain about the
1751 -- implicitly-generated return that is placed at the end.
1753 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1754 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1757 -- Warn on any unassigned OUT parameters if in procedure
1759 if Ekind (Scope_Id) = E_Procedure then
1760 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1763 -- Check that functions return objects, and other things do not
1765 if Kind = E_Function or else Kind = E_Generic_Function then
1766 if not Returns_Object then
1767 Error_Msg_N ("missing expression in return from function", N);
1770 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1771 if Returns_Object then
1772 Error_Msg_N ("procedure cannot return value (use function)", N);
1775 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1776 if Returns_Object then
1777 if Is_Protected_Type (Scope (Scope_Id)) then
1778 Error_Msg_N ("entry body cannot return value", N);
1780 Error_Msg_N ("accept statement cannot return value", N);
1784 elsif Kind = E_Return_Statement then
1786 -- We are nested within another return statement, which must be an
1787 -- extended_return_statement.
1789 if Returns_Object then
1790 if Nkind (N) = N_Extended_Return_Statement then
1792 ("extended return statement cannot be nested (use `RETURN;`)",
1795 -- Case of a simple return statement with a value inside extended
1796 -- return statement.
1800 ("return nested in extended return statement cannot return " &
1801 "value (use `RETURN;`)", N);
1806 Error_Msg_N ("illegal context for return statement", N);
1809 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1810 Analyze_Function_Return (N);
1812 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1813 Set_Return_Present (Scope_Id);
1816 if Nkind (N) = N_Extended_Return_Statement then
1820 Kill_Current_Values (Last_Assignment_Only => True);
1821 Check_Unreachable_Code (N);
1823 Analyze_Dimension (N);
1824 end Analyze_Return_Statement;
1826 -------------------------------------
1827 -- Analyze_Simple_Return_Statement --
1828 -------------------------------------
1830 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1832 if Present (Expression (N)) then
1833 Mark_Coextensions (N, Expression (N));
1836 Analyze_Return_Statement (N);
1837 end Analyze_Simple_Return_Statement;
1839 -------------------------
1840 -- Analyze_Return_Type --
1841 -------------------------
1843 procedure Analyze_Return_Type (N : Node_Id) is
1844 Designator : constant Entity_Id := Defining_Entity (N);
1845 Typ : Entity_Id := Empty;
1848 -- Normal case where result definition does not indicate an error
1850 if Result_Definition (N) /= Error then
1851 if Nkind (Result_Definition (N)) = N_Access_Definition then
1852 Check_SPARK_Restriction
1853 ("access result is not allowed", Result_Definition (N));
1855 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1858 AD : constant Node_Id :=
1859 Access_To_Subprogram_Definition (Result_Definition (N));
1861 if Present (AD) and then Protected_Present (AD) then
1862 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1864 Typ := Access_Definition (N, Result_Definition (N));
1868 Set_Parent (Typ, Result_Definition (N));
1869 Set_Is_Local_Anonymous_Access (Typ);
1870 Set_Etype (Designator, Typ);
1872 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1874 Null_Exclusion_Static_Checks (N);
1876 -- Subtype_Mark case
1879 Find_Type (Result_Definition (N));
1880 Typ := Entity (Result_Definition (N));
1881 Set_Etype (Designator, Typ);
1883 -- Unconstrained array as result is not allowed in SPARK
1885 if Is_Array_Type (Typ) and then not Is_Constrained (Typ) then
1886 Check_SPARK_Restriction
1887 ("returning an unconstrained array is not allowed",
1888 Result_Definition (N));
1891 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1893 Null_Exclusion_Static_Checks (N);
1895 -- If a null exclusion is imposed on the result type, then create
1896 -- a null-excluding itype (an access subtype) and use it as the
1897 -- function's Etype. Note that the null exclusion checks are done
1898 -- right before this, because they don't get applied to types that
1899 -- do not come from source.
1901 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
1902 Set_Etype (Designator,
1903 Create_Null_Excluding_Itype
1906 Scope_Id => Scope (Current_Scope)));
1908 -- The new subtype must be elaborated before use because
1909 -- it is visible outside of the function. However its base
1910 -- type may not be frozen yet, so the reference that will
1911 -- force elaboration must be attached to the freezing of
1914 -- If the return specification appears on a proper body,
1915 -- the subtype will have been created already on the spec.
1917 if Is_Frozen (Typ) then
1918 if Nkind (Parent (N)) = N_Subprogram_Body
1919 and then Nkind (Parent (Parent (N))) = N_Subunit
1923 Build_Itype_Reference (Etype (Designator), Parent (N));
1927 Ensure_Freeze_Node (Typ);
1930 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1932 Set_Itype (IR, Etype (Designator));
1933 Append_Freeze_Actions (Typ, New_List (IR));
1938 Set_Etype (Designator, Typ);
1941 if Ekind (Typ) = E_Incomplete_Type
1942 and then Is_Value_Type (Typ)
1946 elsif Ekind (Typ) = E_Incomplete_Type
1947 or else (Is_Class_Wide_Type (Typ)
1948 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1950 -- AI05-0151: Tagged incomplete types are allowed in all formal
1951 -- parts. Untagged incomplete types are not allowed in bodies.
1953 if Ada_Version >= Ada_2012 then
1954 if Is_Tagged_Type (Typ) then
1957 elsif Nkind (Parent (N)) = N_Subprogram_Body
1958 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
1962 ("invalid use of untagged incomplete type&",
1966 -- The type must be completed in the current package. This
1967 -- is checked at the end of the package declaration when
1968 -- Taft-amendment types are identified. If the return type
1969 -- is class-wide, there is no required check, the type can
1970 -- be a bona fide TAT.
1972 if Ekind (Scope (Current_Scope)) = E_Package
1973 and then In_Private_Part (Scope (Current_Scope))
1974 and then not Is_Class_Wide_Type (Typ)
1976 Append_Elmt (Designator, Private_Dependents (Typ));
1981 ("invalid use of incomplete type&", Designator, Typ);
1986 -- Case where result definition does indicate an error
1989 Set_Etype (Designator, Any_Type);
1991 end Analyze_Return_Type;
1993 -----------------------------
1994 -- Analyze_Subprogram_Body --
1995 -----------------------------
1997 procedure Analyze_Subprogram_Body (N : Node_Id) is
1998 Loc : constant Source_Ptr := Sloc (N);
1999 Body_Spec : constant Node_Id := Specification (N);
2000 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2003 if Debug_Flag_C then
2004 Write_Str ("==> subprogram body ");
2005 Write_Name (Chars (Body_Id));
2006 Write_Str (" from ");
2007 Write_Location (Loc);
2012 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2014 -- The real work is split out into the helper, so it can do "return;"
2015 -- without skipping the debug output:
2017 Analyze_Subprogram_Body_Helper (N);
2019 if Debug_Flag_C then
2021 Write_Str ("<== subprogram body ");
2022 Write_Name (Chars (Body_Id));
2023 Write_Str (" from ");
2024 Write_Location (Loc);
2027 end Analyze_Subprogram_Body;
2029 --------------------------------------
2030 -- Analyze_Subprogram_Body_Contract --
2031 --------------------------------------
2033 procedure Analyze_Subprogram_Body_Contract (Body_Id : Entity_Id) is
2034 Body_Decl : constant Node_Id := Parent (Parent (Body_Id));
2036 Ref_Depends : Node_Id := Empty;
2037 Ref_Global : Node_Id := Empty;
2038 Spec_Id : Entity_Id;
2041 -- When a subprogram body declaration is illegal, its defining entity is
2042 -- left unanalyzed. There is nothing left to do in this case because the
2043 -- body lacks a contract, or even a proper Ekind.
2045 if Ekind (Body_Id) = E_Void then
2049 if Nkind (Body_Decl) = N_Subprogram_Body_Stub then
2050 Spec_Id := Corresponding_Spec_Of_Stub (Body_Decl);
2052 Spec_Id := Corresponding_Spec (Body_Decl);
2055 -- Locate and store pragmas Refined_Depends and Refined_Global since
2056 -- their order of analysis matters.
2058 Prag := Classifications (Contract (Body_Id));
2059 while Present (Prag) loop
2060 if Pragma_Name (Prag) = Name_Refined_Depends then
2061 Ref_Depends := Prag;
2062 elsif Pragma_Name (Prag) = Name_Refined_Global then
2066 Prag := Next_Pragma (Prag);
2069 -- Analyze Refined_Global first as Refined_Depends may mention items
2070 -- classified in the global refinement.
2072 if Present (Ref_Global) then
2073 Analyze_Refined_Global_In_Decl_Part (Ref_Global);
2075 -- When the corresponding Global aspect/pragma references a state with
2076 -- visible refinement, the body requires Refined_Global. Refinement is
2077 -- not required when SPARK checks are suppressed.
2079 elsif Present (Spec_Id) then
2080 Prag := Get_Pragma (Spec_Id, Pragma_Global);
2082 if SPARK_Mode /= Off
2083 and then Present (Prag)
2084 and then Contains_Refined_State (Prag)
2087 ("body of subprogram & requires global refinement",
2088 Body_Decl, Spec_Id);
2092 -- Refined_Depends must be analyzed after Refined_Global in order to see
2093 -- the modes of all global refinements.
2095 if Present (Ref_Depends) then
2096 Analyze_Refined_Depends_In_Decl_Part (Ref_Depends);
2098 -- When the corresponding Depends aspect/pragma references a state with
2099 -- visible refinement, the body requires Refined_Depends. Refinement is
2100 -- not required when SPARK checks are suppressed.
2102 elsif Present (Spec_Id) then
2103 Prag := Get_Pragma (Spec_Id, Pragma_Depends);
2105 if SPARK_Mode /= Off
2106 and then Present (Prag)
2107 and then Contains_Refined_State (Prag)
2110 ("body of subprogram & requires dependance refinement",
2111 Body_Decl, Spec_Id);
2114 end Analyze_Subprogram_Body_Contract;
2116 ------------------------------------
2117 -- Analyze_Subprogram_Body_Helper --
2118 ------------------------------------
2120 -- This procedure is called for regular subprogram bodies, generic bodies,
2121 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2122 -- specification matters, and is used to create a proper declaration for
2123 -- the subprogram, or to perform conformance checks.
2125 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2126 Loc : constant Source_Ptr := Sloc (N);
2127 Body_Spec : constant Node_Id := Specification (N);
2128 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2129 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2130 Conformant : Boolean;
2132 Prot_Typ : Entity_Id := Empty;
2133 Spec_Id : Entity_Id;
2134 Spec_Decl : Node_Id := Empty;
2136 Last_Real_Spec_Entity : Entity_Id := Empty;
2137 -- When we analyze a separate spec, the entity chain ends up containing
2138 -- the formals, as well as any itypes generated during analysis of the
2139 -- default expressions for parameters, or the arguments of associated
2140 -- precondition/postcondition pragmas (which are analyzed in the context
2141 -- of the spec since they have visibility on formals).
2143 -- These entities belong with the spec and not the body. However we do
2144 -- the analysis of the body in the context of the spec (again to obtain
2145 -- visibility to the formals), and all the entities generated during
2146 -- this analysis end up also chained to the entity chain of the spec.
2147 -- But they really belong to the body, and there is circuitry to move
2148 -- them from the spec to the body.
2150 -- However, when we do this move, we don't want to move the real spec
2151 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2152 -- variable points to the last real spec entity, so we only move those
2153 -- chained beyond that point. It is initialized to Empty to deal with
2154 -- the case where there is no separate spec.
2156 procedure Analyze_Aspects_On_Body_Or_Stub;
2157 -- Analyze the aspect specifications of a subprogram body [stub]. It is
2158 -- assumed that N has aspects.
2160 procedure Check_Anonymous_Return;
2161 -- Ada 2005: if a function returns an access type that denotes a task,
2162 -- or a type that contains tasks, we must create a master entity for
2163 -- the anonymous type, which typically will be used in an allocator
2164 -- in the body of the function.
2166 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2167 -- Look ahead to recognize a pragma that may appear after the body.
2168 -- If there is a previous spec, check that it appears in the same
2169 -- declarative part. If the pragma is Inline_Always, perform inlining
2170 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2171 -- If the body acts as a spec, and inlining is required, we create a
2172 -- subprogram declaration for it, in order to attach the body to inline.
2173 -- If pragma does not appear after the body, check whether there is
2174 -- an inline pragma before any local declarations.
2176 procedure Check_Missing_Return;
2177 -- Checks for a function with a no return statements, and also performs
2178 -- the warning checks implemented by Check_Returns. In formal mode, also
2179 -- verify that a function ends with a RETURN and that a procedure does
2180 -- not contain any RETURN.
2182 function Disambiguate_Spec return Entity_Id;
2183 -- When a primitive is declared between the private view and the full
2184 -- view of a concurrent type which implements an interface, a special
2185 -- mechanism is used to find the corresponding spec of the primitive
2188 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
2189 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2190 -- incomplete types coming from a limited context and swap their limited
2191 -- views with the non-limited ones.
2193 function Is_Private_Concurrent_Primitive
2194 (Subp_Id : Entity_Id) return Boolean;
2195 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2196 -- type that implements an interface and has a private view.
2198 procedure Set_Trivial_Subprogram (N : Node_Id);
2199 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2200 -- subprogram whose body is being analyzed. N is the statement node
2201 -- causing the flag to be set, if the following statement is a return
2202 -- of an entity, we mark the entity as set in source to suppress any
2203 -- warning on the stylized use of function stubs with a dummy return.
2205 procedure Verify_Overriding_Indicator;
2206 -- If there was a previous spec, the entity has been entered in the
2207 -- current scope previously. If the body itself carries an overriding
2208 -- indicator, check that it is consistent with the known status of the
2211 -------------------------------------
2212 -- Analyze_Aspects_On_Body_Or_Stub --
2213 -------------------------------------
2215 procedure Analyze_Aspects_On_Body_Or_Stub is
2216 procedure Diagnose_Misplaced_Aspects;
2217 -- Subprogram body [stub] N has aspects, but they are not properly
2218 -- placed. Provide precise diagnostics depending on the aspects
2221 --------------------------------
2222 -- Diagnose_Misplaced_Aspects --
2223 --------------------------------
2225 procedure Diagnose_Misplaced_Aspects is
2229 -- The current aspect along with its name and id
2231 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id);
2232 -- Emit an error message concerning SPARK aspect Asp. Ref_Nam is
2233 -- the name of the refined version of the aspect.
2235 ------------------------
2236 -- SPARK_Aspect_Error --
2237 ------------------------
2239 procedure SPARK_Aspect_Error (Ref_Nam : Name_Id) is
2241 -- The corresponding spec already contains the aspect in
2242 -- question and the one appearing on the body must be the
2245 -- procedure P with Global ...;
2246 -- procedure P with Global ... is ... end P;
2250 if Has_Aspect (Spec_Id, Asp_Id) then
2251 Error_Msg_Name_1 := Asp_Nam;
2253 -- Subunits cannot carry aspects that apply to a subprogram
2256 if Nkind (Parent (N)) = N_Subunit then
2257 Error_Msg_N ("aspect % cannot apply to a subunit", Asp);
2260 Error_Msg_Name_2 := Ref_Nam;
2261 Error_Msg_N ("aspect % should be %", Asp);
2264 -- Otherwise the aspect must appear in the spec, not in the
2268 -- procedure P with Global ... is ... end P;
2272 ("aspect specification must appear in subprogram "
2273 & "declaration", Asp);
2275 end SPARK_Aspect_Error;
2277 -- Start of processing for Diagnose_Misplaced_Aspects
2280 -- Iterate over the aspect specifications and emit specific errors
2281 -- where applicable.
2283 Asp := First (Aspect_Specifications (N));
2284 while Present (Asp) loop
2285 Asp_Nam := Chars (Identifier (Asp));
2286 Asp_Id := Get_Aspect_Id (Asp_Nam);
2288 -- Do not emit errors on aspects that can appear on a
2289 -- subprogram body. This scenario occurs when the aspect
2290 -- specification list contains both misplaced and properly
2293 if Aspect_On_Body_Or_Stub_OK (Asp_Id) then
2296 -- Special diagnostics for SPARK aspects
2298 elsif Asp_Nam = Name_Depends then
2299 SPARK_Aspect_Error (Name_Refined_Depends);
2301 elsif Asp_Nam = Name_Global then
2302 SPARK_Aspect_Error (Name_Refined_Global);
2304 elsif Asp_Nam = Name_Post then
2305 SPARK_Aspect_Error (Name_Refined_Post);
2309 ("aspect specification must appear in subprogram "
2310 & "declaration", Asp);
2315 end Diagnose_Misplaced_Aspects;
2317 -- Start of processing for Analyze_Aspects_On_Body_Or_Stub
2320 -- Language-defined aspects cannot be associated with a subprogram
2321 -- body [stub] if the subprogram has a spec. Certain implementation
2322 -- defined aspects are allowed to break this rule (for list, see
2323 -- table Aspect_On_Body_Or_Stub_OK).
2325 if Present (Spec_Id) and then not Aspects_On_Body_Or_Stub_OK (N) then
2326 Diagnose_Misplaced_Aspects;
2328 Analyze_Aspect_Specifications (N, Body_Id);
2330 end Analyze_Aspects_On_Body_Or_Stub;
2332 ----------------------------
2333 -- Check_Anonymous_Return --
2334 ----------------------------
2336 procedure Check_Anonymous_Return is
2342 if Present (Spec_Id) then
2348 if Ekind (Scop) = E_Function
2349 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2350 and then not Is_Thunk (Scop)
2351 and then (Has_Task (Designated_Type (Etype (Scop)))
2353 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2355 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2356 and then Expander_Active
2358 -- Avoid cases with no tasking support
2360 and then RTE_Available (RE_Current_Master)
2361 and then not Restriction_Active (No_Task_Hierarchy)
2364 Make_Object_Declaration (Loc,
2365 Defining_Identifier =>
2366 Make_Defining_Identifier (Loc, Name_uMaster),
2367 Constant_Present => True,
2368 Object_Definition =>
2369 New_Occurrence_Of (RTE (RE_Master_Id), Loc),
2371 Make_Explicit_Dereference (Loc,
2372 New_Occurrence_Of (RTE (RE_Current_Master), Loc)));
2374 if Present (Declarations (N)) then
2375 Prepend (Decl, Declarations (N));
2377 Set_Declarations (N, New_List (Decl));
2380 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2381 Set_Has_Master_Entity (Scop);
2383 -- Now mark the containing scope as a task master
2386 while Nkind (Par) /= N_Compilation_Unit loop
2387 Par := Parent (Par);
2388 pragma Assert (Present (Par));
2390 -- If we fall off the top, we are at the outer level, and
2391 -- the environment task is our effective master, so nothing
2395 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2397 Set_Is_Task_Master (Par, True);
2402 end Check_Anonymous_Return;
2404 -------------------------
2405 -- Check_Inline_Pragma --
2406 -------------------------
2408 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2412 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2413 -- True when N is a pragma Inline or Inline_Always that applies
2414 -- to this subprogram.
2416 -----------------------
2417 -- Is_Inline_Pragma --
2418 -----------------------
2420 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2423 Nkind (N) = N_Pragma
2425 (Pragma_Name (N) = Name_Inline_Always
2428 and then Pragma_Name (N) = Name_Inline))
2431 (Expression (First (Pragma_Argument_Associations (N)))) =
2433 end Is_Inline_Pragma;
2435 -- Start of processing for Check_Inline_Pragma
2438 if not Expander_Active then
2442 if Is_List_Member (N)
2443 and then Present (Next (N))
2444 and then Is_Inline_Pragma (Next (N))
2448 elsif Nkind (N) /= N_Subprogram_Body_Stub
2449 and then Present (Declarations (N))
2450 and then Is_Inline_Pragma (First (Declarations (N)))
2452 Prag := First (Declarations (N));
2458 if Present (Prag) then
2459 if Present (Spec_Id) then
2460 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
2465 -- Create a subprogram declaration, to make treatment uniform
2468 Subp : constant Entity_Id :=
2469 Make_Defining_Identifier (Loc, Chars (Body_Id));
2470 Decl : constant Node_Id :=
2471 Make_Subprogram_Declaration (Loc,
2473 New_Copy_Tree (Specification (N)));
2476 Set_Defining_Unit_Name (Specification (Decl), Subp);
2478 if Present (First_Formal (Body_Id)) then
2479 Plist := Copy_Parameter_List (Body_Id);
2480 Set_Parameter_Specifications
2481 (Specification (Decl), Plist);
2484 Insert_Before (N, Decl);
2487 Set_Has_Pragma_Inline (Subp);
2489 if Pragma_Name (Prag) = Name_Inline_Always then
2490 Set_Is_Inlined (Subp);
2491 Set_Has_Pragma_Inline_Always (Subp);
2494 -- Prior to copying the subprogram body to create a template
2495 -- for it for subsequent inlining, remove the pragma from
2496 -- the current body so that the copy that will produce the
2497 -- new body will start from a completely unanalyzed tree.
2499 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2500 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2507 end Check_Inline_Pragma;
2509 --------------------------
2510 -- Check_Missing_Return --
2511 --------------------------
2513 procedure Check_Missing_Return is
2515 Missing_Ret : Boolean;
2518 if Nkind (Body_Spec) = N_Function_Specification then
2519 if Present (Spec_Id) then
2525 if Return_Present (Id) then
2526 Check_Returns (HSS, 'F', Missing_Ret);
2529 Set_Has_Missing_Return (Id);
2532 elsif Is_Generic_Subprogram (Id)
2533 or else not Is_Machine_Code_Subprogram (Id)
2535 Error_Msg_N ("missing RETURN statement in function body", N);
2538 -- If procedure with No_Return, check returns
2540 elsif Nkind (Body_Spec) = N_Procedure_Specification
2541 and then Present (Spec_Id)
2542 and then No_Return (Spec_Id)
2544 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2547 -- Special checks in SPARK mode
2549 if Nkind (Body_Spec) = N_Function_Specification then
2551 -- In SPARK mode, last statement of a function should be a return
2554 Stat : constant Node_Id := Last_Source_Statement (HSS);
2557 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2558 N_Extended_Return_Statement)
2560 Check_SPARK_Restriction
2561 ("last statement in function should be RETURN", Stat);
2565 -- In SPARK mode, verify that a procedure has no return
2567 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2568 if Present (Spec_Id) then
2574 -- Would be nice to point to return statement here, can we
2575 -- borrow the Check_Returns procedure here ???
2577 if Return_Present (Id) then
2578 Check_SPARK_Restriction
2579 ("procedure should not have RETURN", N);
2582 end Check_Missing_Return;
2584 -----------------------
2585 -- Disambiguate_Spec --
2586 -----------------------
2588 function Disambiguate_Spec return Entity_Id is
2589 Priv_Spec : Entity_Id;
2592 procedure Replace_Types (To_Corresponding : Boolean);
2593 -- Depending on the flag, replace the type of formal parameters of
2594 -- Body_Id if it is a concurrent type implementing interfaces with
2595 -- the corresponding record type or the other way around.
2597 procedure Replace_Types (To_Corresponding : Boolean) is
2599 Formal_Typ : Entity_Id;
2602 Formal := First_Formal (Body_Id);
2603 while Present (Formal) loop
2604 Formal_Typ := Etype (Formal);
2606 if Is_Class_Wide_Type (Formal_Typ) then
2607 Formal_Typ := Root_Type (Formal_Typ);
2610 -- From concurrent type to corresponding record
2612 if To_Corresponding then
2613 if Is_Concurrent_Type (Formal_Typ)
2614 and then Present (Corresponding_Record_Type (Formal_Typ))
2615 and then Present (Interfaces (
2616 Corresponding_Record_Type (Formal_Typ)))
2619 Corresponding_Record_Type (Formal_Typ));
2622 -- From corresponding record to concurrent type
2625 if Is_Concurrent_Record_Type (Formal_Typ)
2626 and then Present (Interfaces (Formal_Typ))
2629 Corresponding_Concurrent_Type (Formal_Typ));
2633 Next_Formal (Formal);
2637 -- Start of processing for Disambiguate_Spec
2640 -- Try to retrieve the specification of the body as is. All error
2641 -- messages are suppressed because the body may not have a spec in
2642 -- its current state.
2644 Spec_N := Find_Corresponding_Spec (N, False);
2646 -- It is possible that this is the body of a primitive declared
2647 -- between a private and a full view of a concurrent type. The
2648 -- controlling parameter of the spec carries the concurrent type,
2649 -- not the corresponding record type as transformed by Analyze_
2650 -- Subprogram_Specification. In such cases, we undo the change
2651 -- made by the analysis of the specification and try to find the
2654 -- Note that wrappers already have their corresponding specs and
2655 -- bodies set during their creation, so if the candidate spec is
2656 -- a wrapper, then we definitely need to swap all types to their
2657 -- original concurrent status.
2660 or else Is_Primitive_Wrapper (Spec_N)
2662 -- Restore all references of corresponding record types to the
2663 -- original concurrent types.
2665 Replace_Types (To_Corresponding => False);
2666 Priv_Spec := Find_Corresponding_Spec (N, False);
2668 -- The current body truly belongs to a primitive declared between
2669 -- a private and a full view. We leave the modified body as is,
2670 -- and return the true spec.
2672 if Present (Priv_Spec)
2673 and then Is_Private_Primitive (Priv_Spec)
2678 -- In case that this is some sort of error, restore the original
2679 -- state of the body.
2681 Replace_Types (To_Corresponding => True);
2685 end Disambiguate_Spec;
2687 ----------------------------
2688 -- Exchange_Limited_Views --
2689 ----------------------------
2691 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2692 procedure Detect_And_Exchange (Id : Entity_Id);
2693 -- Determine whether Id's type denotes an incomplete type associated
2694 -- with a limited with clause and exchange the limited view with the
2697 -------------------------
2698 -- Detect_And_Exchange --
2699 -------------------------
2701 procedure Detect_And_Exchange (Id : Entity_Id) is
2702 Typ : constant Entity_Id := Etype (Id);
2705 if Ekind (Typ) = E_Incomplete_Type
2706 and then From_Limited_With (Typ)
2707 and then Present (Non_Limited_View (Typ))
2709 Set_Etype (Id, Non_Limited_View (Typ));
2711 end Detect_And_Exchange;
2717 -- Start of processing for Exchange_Limited_Views
2720 if No (Subp_Id) then
2723 -- Do not process subprogram bodies as they already use the non-
2724 -- limited view of types.
2726 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2730 -- Examine all formals and swap views when applicable
2732 Formal := First_Formal (Subp_Id);
2733 while Present (Formal) loop
2734 Detect_And_Exchange (Formal);
2736 Next_Formal (Formal);
2739 -- Process the return type of a function
2741 if Ekind (Subp_Id) = E_Function then
2742 Detect_And_Exchange (Subp_Id);
2744 end Exchange_Limited_Views;
2746 -------------------------------------
2747 -- Is_Private_Concurrent_Primitive --
2748 -------------------------------------
2750 function Is_Private_Concurrent_Primitive
2751 (Subp_Id : Entity_Id) return Boolean
2753 Formal_Typ : Entity_Id;
2756 if Present (First_Formal (Subp_Id)) then
2757 Formal_Typ := Etype (First_Formal (Subp_Id));
2759 if Is_Concurrent_Record_Type (Formal_Typ) then
2760 if Is_Class_Wide_Type (Formal_Typ) then
2761 Formal_Typ := Root_Type (Formal_Typ);
2764 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2767 -- The type of the first formal is a concurrent tagged type with
2771 Is_Concurrent_Type (Formal_Typ)
2772 and then Is_Tagged_Type (Formal_Typ)
2773 and then Has_Private_Declaration (Formal_Typ);
2777 end Is_Private_Concurrent_Primitive;
2779 ----------------------------
2780 -- Set_Trivial_Subprogram --
2781 ----------------------------
2783 procedure Set_Trivial_Subprogram (N : Node_Id) is
2784 Nxt : constant Node_Id := Next (N);
2787 Set_Is_Trivial_Subprogram (Body_Id);
2789 if Present (Spec_Id) then
2790 Set_Is_Trivial_Subprogram (Spec_Id);
2794 and then Nkind (Nxt) = N_Simple_Return_Statement
2795 and then No (Next (Nxt))
2796 and then Present (Expression (Nxt))
2797 and then Is_Entity_Name (Expression (Nxt))
2799 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2801 end Set_Trivial_Subprogram;
2803 ---------------------------------
2804 -- Verify_Overriding_Indicator --
2805 ---------------------------------
2807 procedure Verify_Overriding_Indicator is
2809 if Must_Override (Body_Spec) then
2810 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2811 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2815 elsif not Present (Overridden_Operation (Spec_Id)) then
2817 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2819 -- Overriding indicators aren't allowed for protected subprogram
2820 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2821 -- this to a warning if -gnatd.E is enabled.
2823 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2824 Error_Msg_Warn := Error_To_Warning;
2826 ("<<overriding indicator not allowed for protected "
2827 & "subprogram body", Body_Spec);
2830 elsif Must_Not_Override (Body_Spec) then
2831 if Present (Overridden_Operation (Spec_Id)) then
2833 ("subprogram& overrides inherited operation",
2834 Body_Spec, Spec_Id);
2836 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2837 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2840 ("subprogram & overrides predefined operator ",
2841 Body_Spec, Spec_Id);
2843 -- Overriding indicators aren't allowed for protected subprogram
2844 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
2845 -- this to a warning if -gnatd.E is enabled.
2847 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
2848 Error_Msg_Warn := Error_To_Warning;
2851 ("<<overriding indicator not allowed " &
2852 "for protected subprogram body",
2855 -- If this is not a primitive operation, then the overriding
2856 -- indicator is altogether illegal.
2858 elsif not Is_Primitive (Spec_Id) then
2860 ("overriding indicator only allowed " &
2861 "if subprogram is primitive",
2865 -- If checking the style rule and the operation overrides, then
2866 -- issue a warning about a missing overriding_indicator. Protected
2867 -- subprogram bodies are excluded from this style checking, since
2868 -- they aren't primitives (even though their declarations can
2869 -- override) and aren't allowed to have an overriding_indicator.
2872 and then Present (Overridden_Operation (Spec_Id))
2873 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2875 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2876 Style.Missing_Overriding (N, Body_Id);
2879 and then Can_Override_Operator (Spec_Id)
2880 and then not Is_Predefined_File_Name
2881 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2883 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2884 Style.Missing_Overriding (N, Body_Id);
2886 end Verify_Overriding_Indicator;
2888 -- Start of processing for Analyze_Subprogram_Body_Helper
2891 -- Generic subprograms are handled separately. They always have a
2892 -- generic specification. Determine whether current scope has a
2893 -- previous declaration.
2895 -- If the subprogram body is defined within an instance of the same
2896 -- name, the instance appears as a package renaming, and will be hidden
2897 -- within the subprogram.
2899 if Present (Prev_Id)
2900 and then not Is_Overloadable (Prev_Id)
2901 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2902 or else Comes_From_Source (Prev_Id))
2904 if Is_Generic_Subprogram (Prev_Id) then
2906 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2907 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2909 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2911 if Nkind (N) = N_Subprogram_Body then
2912 HSS := Handled_Statement_Sequence (N);
2913 Check_Missing_Return;
2919 -- Previous entity conflicts with subprogram name. Attempting to
2920 -- enter name will post error.
2922 Enter_Name (Body_Id);
2926 -- Non-generic case, find the subprogram declaration, if one was seen,
2927 -- or enter new overloaded entity in the current scope. If the
2928 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2929 -- part of the context of one of its subunits. No need to redo the
2932 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
2936 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2938 if Nkind (N) = N_Subprogram_Body_Stub
2939 or else No (Corresponding_Spec (N))
2941 if Is_Private_Concurrent_Primitive (Body_Id) then
2942 Spec_Id := Disambiguate_Spec;
2944 Spec_Id := Find_Corresponding_Spec (N);
2947 -- If this is a duplicate body, no point in analyzing it
2949 if Error_Posted (N) then
2953 -- A subprogram body should cause freezing of its own declaration,
2954 -- but if there was no previous explicit declaration, then the
2955 -- subprogram will get frozen too late (there may be code within
2956 -- the body that depends on the subprogram having been frozen,
2957 -- such as uses of extra formals), so we force it to be frozen
2958 -- here. Same holds if the body and spec are compilation units.
2959 -- Finally, if the return type is an anonymous access to protected
2960 -- subprogram, it must be frozen before the body because its
2961 -- expansion has generated an equivalent type that is used when
2962 -- elaborating the body.
2964 -- An exception in the case of Ada 2012, AI05-177: The bodies
2965 -- created for expression functions do not freeze.
2968 and then Nkind (Original_Node (N)) /= N_Expression_Function
2970 Freeze_Before (N, Body_Id);
2972 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2973 Freeze_Before (N, Spec_Id);
2975 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2976 Freeze_Before (N, Etype (Body_Id));
2980 Spec_Id := Corresponding_Spec (N);
2984 -- Previously we scanned the body to look for nested subprograms, and
2985 -- rejected an inline directive if nested subprograms were present,
2986 -- because the back-end would generate conflicting symbols for the
2987 -- nested bodies. This is now unnecessary.
2989 -- Look ahead to recognize a pragma Inline that appears after the body
2991 Check_Inline_Pragma (Spec_Id);
2993 -- Deal with special case of a fully private operation in the body of
2994 -- the protected type. We must create a declaration for the subprogram,
2995 -- in order to attach the protected subprogram that will be used in
2996 -- internal calls. We exclude compiler generated bodies from the
2997 -- expander since the issue does not arise for those cases.
3000 and then Comes_From_Source (N)
3001 and then Is_Protected_Type (Current_Scope)
3003 Spec_Id := Build_Private_Protected_Declaration (N);
3006 -- If a separate spec is present, then deal with freezing issues
3008 if Present (Spec_Id) then
3009 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3010 Verify_Overriding_Indicator;
3012 -- In general, the spec will be frozen when we start analyzing the
3013 -- body. However, for internally generated operations, such as
3014 -- wrapper functions for inherited operations with controlling
3015 -- results, the spec may not have been frozen by the time we expand
3016 -- the freeze actions that include the bodies. In particular, extra
3017 -- formals for accessibility or for return-in-place may need to be
3018 -- generated. Freeze nodes, if any, are inserted before the current
3019 -- body. These freeze actions are also needed in ASIS mode to enable
3020 -- the proper back-annotations.
3022 if not Is_Frozen (Spec_Id)
3023 and then (Expander_Active or ASIS_Mode)
3025 -- Force the generation of its freezing node to ensure proper
3026 -- management of access types in the backend.
3028 -- This is definitely needed for some cases, but it is not clear
3029 -- why, to be investigated further???
3031 Set_Has_Delayed_Freeze (Spec_Id);
3032 Freeze_Before (N, Spec_Id);
3036 -- Mark presence of postcondition procedure in current scope and mark
3037 -- the procedure itself as needing debug info. The latter is important
3038 -- when analyzing decision coverage (for example, for MC/DC coverage).
3040 if Chars (Body_Id) = Name_uPostconditions then
3041 Set_Has_Postconditions (Current_Scope);
3042 Set_Debug_Info_Needed (Body_Id);
3045 -- Place subprogram on scope stack, and make formals visible. If there
3046 -- is a spec, the visible entity remains that of the spec.
3048 if Present (Spec_Id) then
3049 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3051 if Is_Child_Unit (Spec_Id) then
3052 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3056 Style.Check_Identifier (Body_Id, Spec_Id);
3059 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3060 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3062 if Is_Abstract_Subprogram (Spec_Id) then
3063 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3067 Set_Convention (Body_Id, Convention (Spec_Id));
3068 Set_Has_Completion (Spec_Id);
3070 if Is_Protected_Type (Scope (Spec_Id)) then
3071 Prot_Typ := Scope (Spec_Id);
3074 -- If this is a body generated for a renaming, do not check for
3075 -- full conformance. The check is redundant, because the spec of
3076 -- the body is a copy of the spec in the renaming declaration,
3077 -- and the test can lead to spurious errors on nested defaults.
3079 if Present (Spec_Decl)
3080 and then not Comes_From_Source (N)
3082 (Nkind (Original_Node (Spec_Decl)) =
3083 N_Subprogram_Renaming_Declaration
3084 or else (Present (Corresponding_Body (Spec_Decl))
3086 Nkind (Unit_Declaration_Node
3087 (Corresponding_Body (Spec_Decl))) =
3088 N_Subprogram_Renaming_Declaration))
3092 -- Conversely, the spec may have been generated for specless body
3093 -- with an inline pragma.
3095 elsif Comes_From_Source (N)
3096 and then not Comes_From_Source (Spec_Id)
3097 and then Has_Pragma_Inline (Spec_Id)
3104 Fully_Conformant, True, Conformant, Body_Id);
3107 -- If the body is not fully conformant, we have to decide if we
3108 -- should analyze it or not. If it has a really messed up profile
3109 -- then we probably should not analyze it, since we will get too
3110 -- many bogus messages.
3112 -- Our decision is to go ahead in the non-fully conformant case
3113 -- only if it is at least mode conformant with the spec. Note
3114 -- that the call to Check_Fully_Conformant has issued the proper
3115 -- error messages to complain about the lack of conformance.
3118 and then not Mode_Conformant (Body_Id, Spec_Id)
3124 if Spec_Id /= Body_Id then
3125 Reference_Body_Formals (Spec_Id, Body_Id);
3128 Set_Ekind (Body_Id, E_Subprogram_Body);
3130 if Nkind (N) = N_Subprogram_Body_Stub then
3131 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
3136 Set_Corresponding_Spec (N, Spec_Id);
3138 -- Ada 2005 (AI-345): If the operation is a primitive operation
3139 -- of a concurrent type, the type of the first parameter has been
3140 -- replaced with the corresponding record, which is the proper
3141 -- run-time structure to use. However, within the body there may
3142 -- be uses of the formals that depend on primitive operations
3143 -- of the type (in particular calls in prefixed form) for which
3144 -- we need the original concurrent type. The operation may have
3145 -- several controlling formals, so the replacement must be done
3148 if Comes_From_Source (Spec_Id)
3149 and then Present (First_Entity (Spec_Id))
3150 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
3151 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
3152 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
3153 and then Present (Corresponding_Concurrent_Type
3154 (Etype (First_Entity (Spec_Id))))
3157 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
3161 Form := First_Formal (Spec_Id);
3162 while Present (Form) loop
3163 if Etype (Form) = Typ then
3164 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
3172 -- Make the formals visible, and place subprogram on scope stack.
3173 -- This is also the point at which we set Last_Real_Spec_Entity
3174 -- to mark the entities which will not be moved to the body.
3176 Install_Formals (Spec_Id);
3177 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
3179 -- Within an instance, add local renaming declarations so that
3180 -- gdb can retrieve the values of actuals more easily. This is
3181 -- only relevant if generating code (and indeed we definitely
3182 -- do not want these definitions -gnatc mode, because that would
3185 if Is_Generic_Instance (Spec_Id)
3186 and then Is_Wrapper_Package (Current_Scope)
3187 and then Expander_Active
3189 Build_Subprogram_Instance_Renamings (N, Current_Scope);
3192 Push_Scope (Spec_Id);
3194 -- Make sure that the subprogram is immediately visible. For
3195 -- child units that have no separate spec this is indispensable.
3196 -- Otherwise it is safe albeit redundant.
3198 Set_Is_Immediately_Visible (Spec_Id);
3201 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
3202 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3203 Set_Scope (Body_Id, Scope (Spec_Id));
3204 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
3206 -- Case of subprogram body with no previous spec
3209 -- Check for style warning required
3213 -- Only apply check for source level subprograms for which checks
3214 -- have not been suppressed.
3216 and then Comes_From_Source (Body_Id)
3217 and then not Suppress_Style_Checks (Body_Id)
3219 -- No warnings within an instance
3221 and then not In_Instance
3223 -- No warnings for expression functions
3225 and then Nkind (Original_Node (N)) /= N_Expression_Function
3227 Style.Body_With_No_Spec (N);
3230 New_Overloaded_Entity (Body_Id);
3232 if Nkind (N) /= N_Subprogram_Body_Stub then
3233 Set_Acts_As_Spec (N);
3234 Generate_Definition (Body_Id);
3235 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
3237 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
3238 Install_Formals (Body_Id);
3240 Push_Scope (Body_Id);
3243 -- For stubs and bodies with no previous spec, generate references to
3246 Generate_Reference_To_Formals (Body_Id);
3249 -- Set SPARK_Mode from context
3251 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
3252 Set_SPARK_Pragma_Inherited (Body_Id, True);
3254 -- If the return type is an anonymous access type whose designated type
3255 -- is the limited view of a class-wide type and the non-limited view is
3256 -- available, update the return type accordingly.
3258 if Ada_Version >= Ada_2005 and then Comes_From_Source (N) then
3264 Rtyp := Etype (Current_Scope);
3266 if Ekind (Rtyp) = E_Anonymous_Access_Type then
3267 Etyp := Directly_Designated_Type (Rtyp);
3269 if Is_Class_Wide_Type (Etyp)
3270 and then From_Limited_With (Etyp)
3272 Set_Directly_Designated_Type
3273 (Etype (Current_Scope), Available_View (Etyp));
3279 -- If this is the proper body of a stub, we must verify that the stub
3280 -- conforms to the body, and to the previous spec if one was present.
3281 -- We know already that the body conforms to that spec. This test is
3282 -- only required for subprograms that come from source.
3284 if Nkind (Parent (N)) = N_Subunit
3285 and then Comes_From_Source (N)
3286 and then not Error_Posted (Body_Id)
3287 and then Nkind (Corresponding_Stub (Parent (N))) =
3288 N_Subprogram_Body_Stub
3291 Old_Id : constant Entity_Id :=
3293 (Specification (Corresponding_Stub (Parent (N))));
3295 Conformant : Boolean := False;
3298 if No (Spec_Id) then
3299 Check_Fully_Conformant (Body_Id, Old_Id);
3303 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
3305 if not Conformant then
3307 -- The stub was taken to be a new declaration. Indicate that
3310 Set_Has_Completion (Old_Id, False);
3316 Set_Has_Completion (Body_Id);
3317 Check_Eliminated (Body_Id);
3319 if Nkind (N) = N_Subprogram_Body_Stub then
3321 -- Analyze any aspect specifications that appear on the subprogram
3324 if Has_Aspects (N) then
3325 Analyze_Aspects_On_Body_Or_Stub;
3328 -- Stop the analysis now as the stub cannot be inlined, plus it does
3329 -- not have declarative or statement lists.
3334 -- Handle frontend inlining. There is no need to prepare us for inlining
3335 -- if we will not generate the code.
3339 if not Debug_Flag_Dot_K then
3340 if Present (Spec_Id)
3341 and then Expander_Active
3343 (Has_Pragma_Inline_Always (Spec_Id)
3344 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
3346 Build_Body_To_Inline (N, Spec_Id);
3351 elsif Expander_Active
3352 and then Serious_Errors_Detected = 0
3353 and then Present (Spec_Id)
3354 and then Has_Pragma_Inline (Spec_Id)
3356 Check_And_Build_Body_To_Inline (N, Spec_Id, Body_Id);
3359 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
3360 -- of the specification we have to install the private withed units.
3361 -- This holds for child units as well.
3363 if Is_Compilation_Unit (Body_Id)
3364 or else Nkind (Parent (N)) = N_Compilation_Unit
3366 Install_Private_With_Clauses (Body_Id);
3369 Check_Anonymous_Return;
3371 -- Set the Protected_Formal field of each extra formal of the protected
3372 -- subprogram to reference the corresponding extra formal of the
3373 -- subprogram that implements it. For regular formals this occurs when
3374 -- the protected subprogram's declaration is expanded, but the extra
3375 -- formals don't get created until the subprogram is frozen. We need to
3376 -- do this before analyzing the protected subprogram's body so that any
3377 -- references to the original subprogram's extra formals will be changed
3378 -- refer to the implementing subprogram's formals (see Expand_Formal).
3380 if Present (Spec_Id)
3381 and then Is_Protected_Type (Scope (Spec_Id))
3382 and then Present (Protected_Body_Subprogram (Spec_Id))
3385 Impl_Subp : constant Entity_Id :=
3386 Protected_Body_Subprogram (Spec_Id);
3387 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
3388 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
3390 while Present (Prot_Ext_Formal) loop
3391 pragma Assert (Present (Impl_Ext_Formal));
3392 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
3393 Next_Formal_With_Extras (Prot_Ext_Formal);
3394 Next_Formal_With_Extras (Impl_Ext_Formal);
3399 -- Now we can go on to analyze the body
3401 HSS := Handled_Statement_Sequence (N);
3402 Set_Actual_Subtypes (N, Current_Scope);
3404 -- Add a declaration for the Protection object, renaming declarations
3405 -- for discriminals and privals and finally a declaration for the entry
3406 -- family index (if applicable). This form of early expansion is done
3407 -- when the Expander is active because Install_Private_Data_Declarations
3408 -- references entities which were created during regular expansion. The
3409 -- subprogram entity must come from source, and not be an internally
3410 -- generated subprogram.
3413 and then Present (Prot_Typ)
3414 and then Present (Spec_Id)
3415 and then Comes_From_Source (Spec_Id)
3416 and then not Is_Eliminated (Spec_Id)
3418 Install_Private_Data_Declarations
3419 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
3422 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
3423 -- may now appear in parameter and result profiles. Since the analysis
3424 -- of a subprogram body may use the parameter and result profile of the
3425 -- spec, swap any limited views with their non-limited counterpart.
3427 if Ada_Version >= Ada_2012 then
3428 Exchange_Limited_Views (Spec_Id);
3431 -- Analyze any aspect specifications that appear on the subprogram body
3433 if Has_Aspects (N) then
3434 Analyze_Aspects_On_Body_Or_Stub;
3437 -- Deal with [refined] preconditions, postconditions, Contract_Cases,
3438 -- invariants and predicates associated with the body and its spec.
3439 -- Note that this is not pure expansion as Expand_Subprogram_Contract
3440 -- prepares the contract assertions for generic subprograms or for ASIS.
3441 -- Do not generate contract checks in SPARK mode.
3443 if not GNATprove_Mode then
3444 Expand_Subprogram_Contract (N, Spec_Id, Body_Id);
3447 -- Analyze the declarations (this call will analyze the precondition
3448 -- Check pragmas we prepended to the list, as well as the declaration
3449 -- of the _Postconditions procedure).
3451 Analyze_Declarations (Declarations (N));
3453 -- After declarations have been analyzed, the body has been set
3454 -- its final value of SPARK_Mode. Check that SPARK_Mode for body
3455 -- is consistent with SPARK_Mode for spec.
3457 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
3458 if Present (SPARK_Pragma (Spec_Id)) then
3459 if Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Spec_Id)) = Off
3461 Get_SPARK_Mode_From_Pragma (SPARK_Pragma (Body_Id)) = On
3463 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3464 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3465 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
3467 ("\value Off was set for SPARK_Mode on&#", N, Spec_Id);
3470 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
3474 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
3475 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
3476 Error_Msg_Sloc := Sloc (Spec_Id);
3477 Error_Msg_NE ("\no value was set for SPARK_Mode on&#", N, Spec_Id);
3481 -- Check completion, and analyze the statements
3484 Inspect_Deferred_Constant_Completion (Declarations (N));
3487 -- Deal with end of scope processing for the body
3489 Process_End_Label (HSS, 't', Current_Scope);
3491 Check_Subprogram_Order (N);
3492 Set_Analyzed (Body_Id);
3494 -- If we have a separate spec, then the analysis of the declarations
3495 -- caused the entities in the body to be chained to the spec id, but
3496 -- we want them chained to the body id. Only the formal parameters
3497 -- end up chained to the spec id in this case.
3499 if Present (Spec_Id) then
3501 -- We must conform to the categorization of our spec
3503 Validate_Categorization_Dependency (N, Spec_Id);
3505 -- And if this is a child unit, the parent units must conform
3507 if Is_Child_Unit (Spec_Id) then
3508 Validate_Categorization_Dependency
3509 (Unit_Declaration_Node (Spec_Id), Spec_Id);
3512 -- Here is where we move entities from the spec to the body
3514 -- Case where there are entities that stay with the spec
3516 if Present (Last_Real_Spec_Entity) then
3518 -- No body entities (happens when the only real spec entities come
3519 -- from precondition and postcondition pragmas).
3521 if No (Last_Entity (Body_Id)) then
3523 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
3525 -- Body entities present (formals), so chain stuff past them
3529 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
3532 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
3533 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3534 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
3536 -- Case where there are no spec entities, in this case there can be
3537 -- no body entities either, so just move everything.
3540 pragma Assert (No (Last_Entity (Body_Id)));
3541 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
3542 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
3543 Set_First_Entity (Spec_Id, Empty);
3544 Set_Last_Entity (Spec_Id, Empty);
3548 Check_Missing_Return;
3550 -- Now we are going to check for variables that are never modified in
3551 -- the body of the procedure. But first we deal with a special case
3552 -- where we want to modify this check. If the body of the subprogram
3553 -- starts with a raise statement or its equivalent, or if the body
3554 -- consists entirely of a null statement, then it is pretty obvious that
3555 -- it is OK to not reference the parameters. For example, this might be
3556 -- the following common idiom for a stubbed function: statement of the
3557 -- procedure raises an exception. In particular this deals with the
3558 -- common idiom of a stubbed function, which appears something like:
3560 -- function F (A : Integer) return Some_Type;
3563 -- raise Program_Error;
3567 -- Here the purpose of X is simply to satisfy the annoying requirement
3568 -- in Ada that there be at least one return, and we certainly do not
3569 -- want to go posting warnings on X that it is not initialized. On
3570 -- the other hand, if X is entirely unreferenced that should still
3573 -- What we do is to detect these cases, and if we find them, flag the
3574 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
3575 -- suppress unwanted warnings. For the case of the function stub above
3576 -- we have a special test to set X as apparently assigned to suppress
3583 -- Skip initial labels (for one thing this occurs when we are in
3584 -- front end ZCX mode, but in any case it is irrelevant), and also
3585 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
3587 Stm := First (Statements (HSS));
3588 while Nkind (Stm) = N_Label
3589 or else Nkind (Stm) in N_Push_xxx_Label
3594 -- Do the test on the original statement before expansion
3597 Ostm : constant Node_Id := Original_Node (Stm);
3600 -- If explicit raise statement, turn on flag
3602 if Nkind (Ostm) = N_Raise_Statement then
3603 Set_Trivial_Subprogram (Stm);
3605 -- If null statement, and no following statements, turn on flag
3607 elsif Nkind (Stm) = N_Null_Statement
3608 and then Comes_From_Source (Stm)
3609 and then No (Next (Stm))
3611 Set_Trivial_Subprogram (Stm);
3613 -- Check for explicit call cases which likely raise an exception
3615 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3616 if Is_Entity_Name (Name (Ostm)) then
3618 Ent : constant Entity_Id := Entity (Name (Ostm));
3621 -- If the procedure is marked No_Return, then likely it
3622 -- raises an exception, but in any case it is not coming
3623 -- back here, so turn on the flag.
3626 and then Ekind (Ent) = E_Procedure
3627 and then No_Return (Ent)
3629 Set_Trivial_Subprogram (Stm);
3637 -- Check for variables that are never modified
3643 -- If there is a separate spec, then transfer Never_Set_In_Source
3644 -- flags from out parameters to the corresponding entities in the
3645 -- body. The reason we do that is we want to post error flags on
3646 -- the body entities, not the spec entities.
3648 if Present (Spec_Id) then
3649 E1 := First_Entity (Spec_Id);
3650 while Present (E1) loop
3651 if Ekind (E1) = E_Out_Parameter then
3652 E2 := First_Entity (Body_Id);
3653 while Present (E2) loop
3654 exit when Chars (E1) = Chars (E2);
3658 if Present (E2) then
3659 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3667 -- Check references in body
3669 Check_References (Body_Id);
3671 end Analyze_Subprogram_Body_Helper;
3673 ---------------------------------
3674 -- Analyze_Subprogram_Contract --
3675 ---------------------------------
3677 procedure Analyze_Subprogram_Contract (Subp : Entity_Id) is
3678 Items : constant Node_Id := Contract (Subp);
3679 Case_Prag : Node_Id := Empty;
3680 Depends : Node_Id := Empty;
3681 Global : Node_Id := Empty;
3683 Post_Prag : Node_Id := Empty;
3685 Seen_In_Case : Boolean := False;
3686 Seen_In_Post : Boolean := False;
3689 if Present (Items) then
3691 -- Analyze pre- and postconditions
3693 Prag := Pre_Post_Conditions (Items);
3694 while Present (Prag) loop
3695 Analyze_Pre_Post_Condition_In_Decl_Part (Prag, Subp);
3697 -- Verify whether a postcondition mentions attribute 'Result and
3698 -- its expression introduces a post-state.
3700 if Warn_On_Suspicious_Contract
3701 and then Pragma_Name (Prag) = Name_Postcondition
3704 Check_Result_And_Post_State (Prag, Seen_In_Post);
3707 Prag := Next_Pragma (Prag);
3710 -- Analyze contract-cases and test-cases
3712 Prag := Contract_Test_Cases (Items);
3713 while Present (Prag) loop
3714 Nam := Pragma_Name (Prag);
3716 if Nam = Name_Contract_Cases then
3717 Analyze_Contract_Cases_In_Decl_Part (Prag);
3719 -- Verify whether contract-cases mention attribute 'Result and
3720 -- its expression introduces a post-state. Perform the check
3721 -- only when the pragma is legal.
3723 if Warn_On_Suspicious_Contract
3724 and then not Error_Posted (Prag)
3727 Check_Result_And_Post_State (Prag, Seen_In_Case);
3731 pragma Assert (Nam = Name_Test_Case);
3732 Analyze_Test_Case_In_Decl_Part (Prag, Subp);
3735 Prag := Next_Pragma (Prag);
3738 -- Analyze classification pragmas
3740 Prag := Classifications (Items);
3741 while Present (Prag) loop
3742 Nam := Pragma_Name (Prag);
3744 if Nam = Name_Depends then
3746 else pragma Assert (Nam = Name_Global);
3750 Prag := Next_Pragma (Prag);
3753 -- Analyze Global first as Depends may mention items classified in
3754 -- the global categorization.
3756 if Present (Global) then
3757 Analyze_Global_In_Decl_Part (Global);
3760 -- Depends must be analyzed after Global in order to see the modes of
3761 -- all global items.
3763 if Present (Depends) then
3764 Analyze_Depends_In_Decl_Part (Depends);
3768 -- Emit an error when neither the postconditions nor the contract-cases
3769 -- mention attribute 'Result in the context of a function.
3771 if Warn_On_Suspicious_Contract
3772 and then Ekind_In (Subp, E_Function, E_Generic_Function)
3774 if Present (Case_Prag)
3775 and then not Seen_In_Case
3776 and then Present (Post_Prag)
3777 and then not Seen_In_Post
3780 ("neither function postcondition nor contract cases mention "
3781 & "result?T?", Post_Prag);
3783 elsif Present (Case_Prag) and then not Seen_In_Case then
3785 ("contract cases do not mention result?T?", Case_Prag);
3787 -- OK if we have at least one IN OUT parameter
3789 elsif Present (Post_Prag) and then not Seen_In_Post then
3793 F := First_Formal (Subp);
3794 while Present (F) loop
3795 if Ekind (F) = E_In_Out_Parameter then
3803 -- If no in-out parameters and no mention of Result, the contract
3804 -- is certainly suspicious.
3807 ("function postcondition does not mention result?T?", Post_Prag);
3810 end Analyze_Subprogram_Contract;
3812 ------------------------------------
3813 -- Analyze_Subprogram_Declaration --
3814 ------------------------------------
3816 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3817 Scop : constant Entity_Id := Current_Scope;
3818 Designator : Entity_Id;
3820 Is_Completion : Boolean;
3821 -- Indicates whether a null procedure declaration is a completion
3824 -- Null procedures are not allowed in SPARK
3826 if Nkind (Specification (N)) = N_Procedure_Specification
3827 and then Null_Present (Specification (N))
3829 Check_SPARK_Restriction ("null procedure is not allowed", N);
3831 if Is_Protected_Type (Current_Scope) then
3832 Error_Msg_N ("protected operation cannot be a null procedure", N);
3835 Analyze_Null_Procedure (N, Is_Completion);
3837 if Is_Completion then
3839 -- The null procedure acts as a body, nothing further is needed.
3845 Designator := Analyze_Subprogram_Specification (Specification (N));
3847 -- A reference may already have been generated for the unit name, in
3848 -- which case the following call is redundant. However it is needed for
3849 -- declarations that are the rewriting of an expression function.
3851 Generate_Definition (Designator);
3853 -- Set SPARK mode from current context (may be overwritten later with
3854 -- explicit pragma).
3856 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
3857 Set_SPARK_Pragma_Inherited (Designator, True);
3859 if Debug_Flag_C then
3860 Write_Str ("==> subprogram spec ");
3861 Write_Name (Chars (Designator));
3862 Write_Str (" from ");
3863 Write_Location (Sloc (N));
3868 Validate_RCI_Subprogram_Declaration (N);
3869 New_Overloaded_Entity (Designator);
3870 Check_Delayed_Subprogram (Designator);
3872 -- If the type of the first formal of the current subprogram is a non-
3873 -- generic tagged private type, mark the subprogram as being a private
3874 -- primitive. Ditto if this is a function with controlling result, and
3875 -- the return type is currently private. In both cases, the type of the
3876 -- controlling argument or result must be in the current scope for the
3877 -- operation to be primitive.
3879 if Has_Controlling_Result (Designator)
3880 and then Is_Private_Type (Etype (Designator))
3881 and then Scope (Etype (Designator)) = Current_Scope
3882 and then not Is_Generic_Actual_Type (Etype (Designator))
3884 Set_Is_Private_Primitive (Designator);
3886 elsif Present (First_Formal (Designator)) then
3888 Formal_Typ : constant Entity_Id :=
3889 Etype (First_Formal (Designator));
3891 Set_Is_Private_Primitive (Designator,
3892 Is_Tagged_Type (Formal_Typ)
3893 and then Scope (Formal_Typ) = Current_Scope
3894 and then Is_Private_Type (Formal_Typ)
3895 and then not Is_Generic_Actual_Type (Formal_Typ));
3899 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3902 if Ada_Version >= Ada_2005
3903 and then Comes_From_Source (N)
3904 and then Is_Dispatching_Operation (Designator)
3911 if Has_Controlling_Result (Designator) then
3912 Etyp := Etype (Designator);
3915 E := First_Entity (Designator);
3917 and then Is_Formal (E)
3918 and then not Is_Controlling_Formal (E)
3926 if Is_Access_Type (Etyp) then
3927 Etyp := Directly_Designated_Type (Etyp);
3930 if Is_Interface (Etyp)
3931 and then not Is_Abstract_Subprogram (Designator)
3932 and then not (Ekind (Designator) = E_Procedure
3933 and then Null_Present (Specification (N)))
3935 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3937 -- Specialize error message based on procedures vs. functions,
3938 -- since functions can't be null subprograms.
3940 if Ekind (Designator) = E_Procedure then
3942 ("interface procedure % must be abstract or null", N);
3944 Error_Msg_N ("interface function % must be abstract", N);
3950 -- What is the following code for, it used to be
3952 -- ??? Set_Suppress_Elaboration_Checks
3953 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3955 -- The following seems equivalent, but a bit dubious
3957 if Elaboration_Checks_Suppressed (Designator) then
3958 Set_Kill_Elaboration_Checks (Designator);
3961 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
3962 Set_Categorization_From_Scope (Designator, Scop);
3965 -- For a compilation unit, check for library-unit pragmas
3967 Push_Scope (Designator);
3968 Set_Categorization_From_Pragmas (N);
3969 Validate_Categorization_Dependency (N, Designator);
3973 -- For a compilation unit, set body required. This flag will only be
3974 -- reset if a valid Import or Interface pragma is processed later on.
3976 if Nkind (Parent (N)) = N_Compilation_Unit then
3977 Set_Body_Required (Parent (N), True);
3979 if Ada_Version >= Ada_2005
3980 and then Nkind (Specification (N)) = N_Procedure_Specification
3981 and then Null_Present (Specification (N))
3984 ("null procedure cannot be declared at library level", N);
3988 Generate_Reference_To_Formals (Designator);
3989 Check_Eliminated (Designator);
3991 if Debug_Flag_C then
3993 Write_Str ("<== subprogram spec ");
3994 Write_Name (Chars (Designator));
3995 Write_Str (" from ");
3996 Write_Location (Sloc (N));
4000 if Is_Protected_Type (Current_Scope) then
4002 -- Indicate that this is a protected operation, because it may be
4003 -- used in subsequent declarations within the protected type.
4005 Set_Convention (Designator, Convention_Protected);
4008 List_Inherited_Pre_Post_Aspects (Designator);
4010 if Has_Aspects (N) then
4011 Analyze_Aspect_Specifications (N, Designator);
4013 end Analyze_Subprogram_Declaration;
4015 --------------------------------------
4016 -- Analyze_Subprogram_Specification --
4017 --------------------------------------
4019 -- Reminder: N here really is a subprogram specification (not a subprogram
4020 -- declaration). This procedure is called to analyze the specification in
4021 -- both subprogram bodies and subprogram declarations (specs).
4023 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
4024 Designator : constant Entity_Id := Defining_Entity (N);
4025 Formals : constant List_Id := Parameter_Specifications (N);
4027 -- Start of processing for Analyze_Subprogram_Specification
4030 -- User-defined operator is not allowed in SPARK, except as a renaming
4032 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
4033 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
4035 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
4038 -- Proceed with analysis. Do not emit a cross-reference entry if the
4039 -- specification comes from an expression function, because it may be
4040 -- the completion of a previous declaration. It is is not, the cross-
4041 -- reference entry will be emitted for the new subprogram declaration.
4043 if Nkind (Parent (N)) /= N_Expression_Function then
4044 Generate_Definition (Designator);
4047 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
4049 if Nkind (N) = N_Function_Specification then
4050 Set_Ekind (Designator, E_Function);
4051 Set_Mechanism (Designator, Default_Mechanism);
4053 Set_Ekind (Designator, E_Procedure);
4054 Set_Etype (Designator, Standard_Void_Type);
4057 -- Introduce new scope for analysis of the formals and the return type
4059 Set_Scope (Designator, Current_Scope);
4061 if Present (Formals) then
4062 Push_Scope (Designator);
4063 Process_Formals (Formals, N);
4065 -- Check dimensions in N for formals with default expression
4067 Analyze_Dimension_Formals (N, Formals);
4069 -- Ada 2005 (AI-345): If this is an overriding operation of an
4070 -- inherited interface operation, and the controlling type is
4071 -- a synchronized type, replace the type with its corresponding
4072 -- record, to match the proper signature of an overriding operation.
4073 -- Same processing for an access parameter whose designated type is
4074 -- derived from a synchronized interface.
4076 if Ada_Version >= Ada_2005 then
4079 Formal_Typ : Entity_Id;
4080 Rec_Typ : Entity_Id;
4081 Desig_Typ : Entity_Id;
4084 Formal := First_Formal (Designator);
4085 while Present (Formal) loop
4086 Formal_Typ := Etype (Formal);
4088 if Is_Concurrent_Type (Formal_Typ)
4089 and then Present (Corresponding_Record_Type (Formal_Typ))
4091 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
4093 if Present (Interfaces (Rec_Typ)) then
4094 Set_Etype (Formal, Rec_Typ);
4097 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
4098 Desig_Typ := Designated_Type (Formal_Typ);
4100 if Is_Concurrent_Type (Desig_Typ)
4101 and then Present (Corresponding_Record_Type (Desig_Typ))
4103 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
4105 if Present (Interfaces (Rec_Typ)) then
4106 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
4111 Next_Formal (Formal);
4118 -- The subprogram scope is pushed and popped around the processing of
4119 -- the return type for consistency with call above to Process_Formals
4120 -- (which itself can call Analyze_Return_Type), and to ensure that any
4121 -- itype created for the return type will be associated with the proper
4124 elsif Nkind (N) = N_Function_Specification then
4125 Push_Scope (Designator);
4126 Analyze_Return_Type (N);
4132 if Nkind (N) = N_Function_Specification then
4134 -- Deal with operator symbol case
4136 if Nkind (Designator) = N_Defining_Operator_Symbol then
4137 Valid_Operator_Definition (Designator);
4140 May_Need_Actuals (Designator);
4142 -- Ada 2005 (AI-251): If the return type is abstract, verify that
4143 -- the subprogram is abstract also. This does not apply to renaming
4144 -- declarations, where abstractness is inherited, and to subprogram
4145 -- bodies generated for stream operations, which become renamings as
4148 -- In case of primitives associated with abstract interface types
4149 -- the check is applied later (see Analyze_Subprogram_Declaration).
4151 if not Nkind_In (Original_Node (Parent (N)),
4152 N_Subprogram_Renaming_Declaration,
4153 N_Abstract_Subprogram_Declaration,
4154 N_Formal_Abstract_Subprogram_Declaration)
4156 if Is_Abstract_Type (Etype (Designator))
4157 and then not Is_Interface (Etype (Designator))
4160 ("function that returns abstract type must be abstract", N);
4162 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
4163 -- access result whose designated type is abstract.
4165 elsif Nkind (Result_Definition (N)) = N_Access_Definition
4167 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
4168 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
4169 and then Ada_Version >= Ada_2012
4171 Error_Msg_N ("function whose access result designates "
4172 & "abstract type must be abstract", N);
4178 end Analyze_Subprogram_Specification;
4180 --------------------------
4181 -- Build_Body_To_Inline --
4182 --------------------------
4184 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
4185 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
4186 Original_Body : Node_Id;
4187 Body_To_Analyze : Node_Id;
4188 Max_Size : constant := 10;
4189 Stat_Count : Integer := 0;
4191 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4192 -- Check for declarations that make inlining not worthwhile
4194 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4195 -- Check for statements that make inlining not worthwhile: any tasking
4196 -- statement, nested at any level. Keep track of total number of
4197 -- elementary statements, as a measure of acceptable size.
4199 function Has_Pending_Instantiation return Boolean;
4200 -- If some enclosing body contains instantiations that appear before the
4201 -- corresponding generic body, the enclosing body has a freeze node so
4202 -- that it can be elaborated after the generic itself. This might
4203 -- conflict with subsequent inlinings, so that it is unsafe to try to
4204 -- inline in such a case.
4206 function Has_Single_Return return Boolean;
4207 -- In general we cannot inline functions that return unconstrained type.
4208 -- However, we can handle such functions if all return statements return
4209 -- a local variable that is the only declaration in the body of the
4210 -- function. In that case the call can be replaced by that local
4211 -- variable as is done for other inlined calls.
4213 procedure Remove_Pragmas;
4214 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
4215 -- parameter has no meaning when the body is inlined and the formals
4216 -- are rewritten. Remove it from body to inline. The analysis of the
4217 -- non-inlined body will handle the pragma properly.
4219 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
4220 -- If the body of the subprogram includes a call that returns an
4221 -- unconstrained type, the secondary stack is involved, and it
4222 -- is not worth inlining.
4224 ------------------------------
4225 -- Has_Excluded_Declaration --
4226 ------------------------------
4228 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4231 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4232 -- Nested subprograms make a given body ineligible for inlining, but
4233 -- we make an exception for instantiations of unchecked conversion.
4234 -- The body has not been analyzed yet, so check the name, and verify
4235 -- that the visible entity with that name is the predefined unit.
4237 -----------------------------
4238 -- Is_Unchecked_Conversion --
4239 -----------------------------
4241 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
4242 Id : constant Node_Id := Name (D);
4246 if Nkind (Id) = N_Identifier
4247 and then Chars (Id) = Name_Unchecked_Conversion
4249 Conv := Current_Entity (Id);
4251 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
4252 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
4254 Conv := Current_Entity (Selector_Name (Id));
4259 return Present (Conv)
4260 and then Is_Predefined_File_Name
4261 (Unit_File_Name (Get_Source_Unit (Conv)))
4262 and then Is_Intrinsic_Subprogram (Conv);
4263 end Is_Unchecked_Conversion;
4265 -- Start of processing for Has_Excluded_Declaration
4269 while Present (D) loop
4270 if (Nkind (D) = N_Function_Instantiation
4271 and then not Is_Unchecked_Conversion (D))
4272 or else Nkind_In (D, N_Protected_Type_Declaration,
4273 N_Package_Declaration,
4274 N_Package_Instantiation,
4276 N_Procedure_Instantiation,
4277 N_Task_Type_Declaration)
4280 ("cannot inline & (non-allowed declaration)?", D, Subp);
4288 end Has_Excluded_Declaration;
4290 ----------------------------
4291 -- Has_Excluded_Statement --
4292 ----------------------------
4294 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
4300 while Present (S) loop
4301 Stat_Count := Stat_Count + 1;
4303 if Nkind_In (S, N_Abort_Statement,
4304 N_Asynchronous_Select,
4305 N_Conditional_Entry_Call,
4306 N_Delay_Relative_Statement,
4307 N_Delay_Until_Statement,
4312 ("cannot inline & (non-allowed statement)?", S, Subp);
4315 elsif Nkind (S) = N_Block_Statement then
4316 if Present (Declarations (S))
4317 and then Has_Excluded_Declaration (Declarations (S))
4321 elsif Present (Handled_Statement_Sequence (S))
4324 (Exception_Handlers (Handled_Statement_Sequence (S)))
4326 Has_Excluded_Statement
4327 (Statements (Handled_Statement_Sequence (S))))
4332 elsif Nkind (S) = N_Case_Statement then
4333 E := First (Alternatives (S));
4334 while Present (E) loop
4335 if Has_Excluded_Statement (Statements (E)) then
4342 elsif Nkind (S) = N_If_Statement then
4343 if Has_Excluded_Statement (Then_Statements (S)) then
4347 if Present (Elsif_Parts (S)) then
4348 E := First (Elsif_Parts (S));
4349 while Present (E) loop
4350 if Has_Excluded_Statement (Then_Statements (E)) then
4358 if Present (Else_Statements (S))
4359 and then Has_Excluded_Statement (Else_Statements (S))
4364 elsif Nkind (S) = N_Loop_Statement
4365 and then Has_Excluded_Statement (Statements (S))
4369 elsif Nkind (S) = N_Extended_Return_Statement then
4370 if Has_Excluded_Statement
4371 (Statements (Handled_Statement_Sequence (S)))
4373 (Exception_Handlers (Handled_Statement_Sequence (S)))
4383 end Has_Excluded_Statement;
4385 -------------------------------
4386 -- Has_Pending_Instantiation --
4387 -------------------------------
4389 function Has_Pending_Instantiation return Boolean is
4394 while Present (S) loop
4395 if Is_Compilation_Unit (S)
4396 or else Is_Child_Unit (S)
4400 elsif Ekind (S) = E_Package
4401 and then Has_Forward_Instantiation (S)
4410 end Has_Pending_Instantiation;
4412 ------------------------
4413 -- Has_Single_Return --
4414 ------------------------
4416 function Has_Single_Return return Boolean is
4417 Return_Statement : Node_Id := Empty;
4419 function Check_Return (N : Node_Id) return Traverse_Result;
4425 function Check_Return (N : Node_Id) return Traverse_Result is
4427 if Nkind (N) = N_Simple_Return_Statement then
4428 if Present (Expression (N))
4429 and then Is_Entity_Name (Expression (N))
4431 if No (Return_Statement) then
4432 Return_Statement := N;
4435 elsif Chars (Expression (N)) =
4436 Chars (Expression (Return_Statement))
4444 -- A return statement within an extended return is a noop
4447 elsif No (Expression (N))
4448 and then Nkind (Parent (Parent (N))) =
4449 N_Extended_Return_Statement
4454 -- Expression has wrong form
4459 -- We can only inline a build-in-place function if
4460 -- it has a single extended return.
4462 elsif Nkind (N) = N_Extended_Return_Statement then
4463 if No (Return_Statement) then
4464 Return_Statement := N;
4476 function Check_All_Returns is new Traverse_Func (Check_Return);
4478 -- Start of processing for Has_Single_Return
4481 if Check_All_Returns (N) /= OK then
4484 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
4488 return Present (Declarations (N))
4489 and then Present (First (Declarations (N)))
4490 and then Chars (Expression (Return_Statement)) =
4491 Chars (Defining_Identifier (First (Declarations (N))));
4493 end Has_Single_Return;
4495 --------------------
4496 -- Remove_Pragmas --
4497 --------------------
4499 procedure Remove_Pragmas is
4504 Decl := First (Declarations (Body_To_Analyze));
4505 while Present (Decl) loop
4508 if Nkind (Decl) = N_Pragma
4509 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
4519 --------------------------
4520 -- Uses_Secondary_Stack --
4521 --------------------------
4523 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
4524 function Check_Call (N : Node_Id) return Traverse_Result;
4525 -- Look for function calls that return an unconstrained type
4531 function Check_Call (N : Node_Id) return Traverse_Result is
4533 if Nkind (N) = N_Function_Call
4534 and then Is_Entity_Name (Name (N))
4535 and then Is_Composite_Type (Etype (Entity (Name (N))))
4536 and then not Is_Constrained (Etype (Entity (Name (N))))
4539 ("cannot inline & (call returns unconstrained type)?",
4547 function Check_Calls is new Traverse_Func (Check_Call);
4550 return Check_Calls (Bod) = Abandon;
4551 end Uses_Secondary_Stack;
4553 -- Start of processing for Build_Body_To_Inline
4556 -- Return immediately if done already
4558 if Nkind (Decl) = N_Subprogram_Declaration
4559 and then Present (Body_To_Inline (Decl))
4563 -- Functions that return unconstrained composite types require
4564 -- secondary stack handling, and cannot currently be inlined, unless
4565 -- all return statements return a local variable that is the first
4566 -- local declaration in the body.
4568 elsif Ekind (Subp) = E_Function
4569 and then not Is_Scalar_Type (Etype (Subp))
4570 and then not Is_Access_Type (Etype (Subp))
4571 and then not Is_Constrained (Etype (Subp))
4573 if not Has_Single_Return then
4575 ("cannot inline & (unconstrained return type)?", N, Subp);
4579 -- Ditto for functions that return controlled types, where controlled
4580 -- actions interfere in complex ways with inlining.
4582 elsif Ekind (Subp) = E_Function
4583 and then Needs_Finalization (Etype (Subp))
4586 ("cannot inline & (controlled return type)?", N, Subp);
4590 if Present (Declarations (N))
4591 and then Has_Excluded_Declaration (Declarations (N))
4596 if Present (Handled_Statement_Sequence (N)) then
4597 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
4599 ("cannot inline& (exception handler)?",
4600 First (Exception_Handlers (Handled_Statement_Sequence (N))),
4604 Has_Excluded_Statement
4605 (Statements (Handled_Statement_Sequence (N)))
4611 -- We do not inline a subprogram that is too large, unless it is
4612 -- marked Inline_Always. This pragma does not suppress the other
4613 -- checks on inlining (forbidden declarations, handlers, etc).
4615 if Stat_Count > Max_Size
4616 and then not Has_Pragma_Inline_Always (Subp)
4618 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
4622 if Has_Pending_Instantiation then
4624 ("cannot inline& (forward instance within enclosing body)?",
4629 -- Within an instance, the body to inline must be treated as a nested
4630 -- generic, so that the proper global references are preserved.
4632 -- Note that we do not do this at the library level, because it is not
4633 -- needed, and furthermore this causes trouble if front end inlining
4634 -- is activated (-gnatN).
4636 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4637 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4638 Original_Body := Copy_Generic_Node (N, Empty, True);
4640 Original_Body := Copy_Separate_Tree (N);
4643 -- We need to capture references to the formals in order to substitute
4644 -- the actuals at the point of inlining, i.e. instantiation. To treat
4645 -- the formals as globals to the body to inline, we nest it within
4646 -- a dummy parameterless subprogram, declared within the real one.
4647 -- To avoid generating an internal name (which is never public, and
4648 -- which affects serial numbers of other generated names), we use
4649 -- an internal symbol that cannot conflict with user declarations.
4651 Set_Parameter_Specifications (Specification (Original_Body), No_List);
4652 Set_Defining_Unit_Name
4653 (Specification (Original_Body),
4654 Make_Defining_Identifier (Sloc (N), Name_uParent));
4655 Set_Corresponding_Spec (Original_Body, Empty);
4657 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4659 -- Set return type of function, which is also global and does not need
4662 if Ekind (Subp) = E_Function then
4663 Set_Result_Definition (Specification (Body_To_Analyze),
4664 New_Occurrence_Of (Etype (Subp), Sloc (N)));
4667 if No (Declarations (N)) then
4668 Set_Declarations (N, New_List (Body_To_Analyze));
4670 Append (Body_To_Analyze, Declarations (N));
4673 Expander_Mode_Save_And_Set (False);
4676 Analyze (Body_To_Analyze);
4677 Push_Scope (Defining_Entity (Body_To_Analyze));
4678 Save_Global_References (Original_Body);
4680 Remove (Body_To_Analyze);
4682 Expander_Mode_Restore;
4684 -- Restore environment if previously saved
4686 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
4690 -- If secondary stk used there is no point in inlining. We have
4691 -- already issued the warning in this case, so nothing to do.
4693 if Uses_Secondary_Stack (Body_To_Analyze) then
4697 Set_Body_To_Inline (Decl, Original_Body);
4698 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4699 Set_Is_Inlined (Subp);
4700 end Build_Body_To_Inline;
4706 procedure Cannot_Inline
4710 Is_Serious : Boolean := False)
4713 pragma Assert (Msg (Msg'Last) = '?');
4717 if not Debug_Flag_Dot_K then
4719 -- Do not emit warning if this is a predefined unit which is not
4720 -- the main unit. With validity checks enabled, some predefined
4721 -- subprograms may contain nested subprograms and become ineligible
4724 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4725 and then not In_Extended_Main_Source_Unit (Subp)
4729 elsif Has_Pragma_Inline_Always (Subp) then
4731 -- Remove last character (question mark) to make this into an
4732 -- error, because the Inline_Always pragma cannot be obeyed.
4734 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4736 elsif Ineffective_Inline_Warnings then
4737 Error_Msg_NE (Msg & "p?", N, Subp);
4744 elsif Is_Serious then
4746 -- Remove last character (question mark) to make this into an error.
4748 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4750 elsif Optimization_Level = 0 then
4752 -- Do not emit warning if this is a predefined unit which is not
4753 -- the main unit. This behavior is currently provided for backward
4754 -- compatibility but it will be removed when we enforce the
4755 -- strictness of the new rules.
4757 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4758 and then not In_Extended_Main_Source_Unit (Subp)
4762 elsif Has_Pragma_Inline_Always (Subp) then
4764 -- Emit a warning if this is a call to a runtime subprogram
4765 -- which is located inside a generic. Previously this call
4766 -- was silently skipped.
4768 if Is_Generic_Instance (Subp) then
4770 Gen_P : constant Entity_Id := Generic_Parent (Parent (Subp));
4772 if Is_Predefined_File_Name
4773 (Unit_File_Name (Get_Source_Unit (Gen_P)))
4775 Set_Is_Inlined (Subp, False);
4776 Error_Msg_NE (Msg & "p?", N, Subp);
4782 -- Remove last character (question mark) to make this into an
4783 -- error, because the Inline_Always pragma cannot be obeyed.
4785 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4787 else pragma Assert (Front_End_Inlining);
4788 Set_Is_Inlined (Subp, False);
4790 -- When inlining cannot take place we must issue an error.
4791 -- For backward compatibility we still report a warning.
4793 if Ineffective_Inline_Warnings then
4794 Error_Msg_NE (Msg & "p?", N, Subp);
4798 -- Compiling with optimizations enabled it is too early to report
4799 -- problems since the backend may still perform inlining. In order
4800 -- to report unhandled inlinings the program must be compiled with
4801 -- -Winline and the error is reported by the backend.
4808 ------------------------------------
4809 -- Check_And_Build_Body_To_Inline --
4810 ------------------------------------
4812 procedure Check_And_Build_Body_To_Inline
4814 Spec_Id : Entity_Id;
4815 Body_Id : Entity_Id)
4817 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id);
4818 -- Use generic machinery to build an unexpanded body for the subprogram.
4819 -- This body is subsequently used for inline expansions at call sites.
4821 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean;
4822 -- Return true if we generate code for the function body N, the function
4823 -- body N has no local declarations and its unique statement is a single
4824 -- extended return statement with a handled statements sequence.
4826 function Check_Body_To_Inline
4828 Subp : Entity_Id) return Boolean;
4829 -- N is the N_Subprogram_Body of Subp. Return true if Subp can be
4830 -- inlined by the frontend. These are the rules:
4831 -- * At -O0 use fe inlining when inline_always is specified except if
4832 -- the function returns a controlled type.
4833 -- * At other optimization levels use the fe inlining for both inline
4834 -- and inline_always in the following cases:
4835 -- - function returning a known at compile time constant
4836 -- - function returning a call to an intrinsic function
4837 -- - function returning an unconstrained type (see Can_Split
4838 -- Unconstrained_Function).
4839 -- - function returning a call to a frontend-inlined function
4840 -- Use the back-end mechanism otherwise
4842 -- In addition, in the following cases the function cannot be inlined by
4844 -- - functions that uses the secondary stack
4845 -- - functions that have declarations of:
4846 -- - Concurrent types
4850 -- - functions that have some of the following statements:
4852 -- - asynchronous-select
4853 -- - conditional-entry-call
4856 -- - selective-accept
4857 -- - timed-entry-call
4858 -- - functions that have exception handlers
4859 -- - functions that have some enclosing body containing instantiations
4860 -- that appear before the corresponding generic body.
4862 procedure Generate_Body_To_Inline
4864 Body_To_Inline : out Node_Id);
4865 -- Generate a parameterless duplicate of subprogram body N. Occurrences
4866 -- of pragmas referencing the formals are removed since they have no
4867 -- meaning when the body is inlined and the formals are rewritten (the
4868 -- analysis of the non-inlined body will handle these pragmas properly).
4869 -- A new internal name is associated with Body_To_Inline.
4871 procedure Split_Unconstrained_Function
4873 Spec_Id : Entity_Id);
4874 -- N is an inlined function body that returns an unconstrained type and
4875 -- has a single extended return statement. Split N in two subprograms:
4876 -- a procedure P' and a function F'. The formals of P' duplicate the
4877 -- formals of N plus an extra formal which is used return a value;
4878 -- its body is composed by the declarations and list of statements
4879 -- of the extended return statement of N.
4881 --------------------------
4882 -- Build_Body_To_Inline --
4883 --------------------------
4885 procedure Build_Body_To_Inline (N : Node_Id; Spec_Id : Entity_Id) is
4886 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
4887 Original_Body : Node_Id;
4888 Body_To_Analyze : Node_Id;
4891 pragma Assert (Current_Scope = Spec_Id);
4893 -- Within an instance, the body to inline must be treated as a nested
4894 -- generic, so that the proper global references are preserved. We
4895 -- do not do this at the library level, because it is not needed, and
4896 -- furthermore this causes trouble if front end inlining is activated
4900 and then Scope (Current_Scope) /= Standard_Standard
4902 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
4905 -- We need to capture references to the formals in order
4906 -- to substitute the actuals at the point of inlining, i.e.
4907 -- instantiation. To treat the formals as globals to the body to
4908 -- inline, we nest it within a dummy parameterless subprogram,
4909 -- declared within the real one.
4911 Generate_Body_To_Inline (N, Original_Body);
4912 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
4914 -- Set return type of function, which is also global and does not
4915 -- need to be resolved.
4917 if Ekind (Spec_Id) = E_Function then
4918 Set_Result_Definition (Specification (Body_To_Analyze),
4919 New_Occurrence_Of (Etype (Spec_Id), Sloc (N)));
4922 if No (Declarations (N)) then
4923 Set_Declarations (N, New_List (Body_To_Analyze));
4925 Append_To (Declarations (N), Body_To_Analyze);
4928 Preanalyze (Body_To_Analyze);
4930 Push_Scope (Defining_Entity (Body_To_Analyze));
4931 Save_Global_References (Original_Body);
4933 Remove (Body_To_Analyze);
4935 -- Restore environment if previously saved
4938 and then Scope (Current_Scope) /= Standard_Standard
4943 pragma Assert (No (Body_To_Inline (Decl)));
4944 Set_Body_To_Inline (Decl, Original_Body);
4945 Set_Ekind (Defining_Entity (Original_Body), Ekind (Spec_Id));
4946 end Build_Body_To_Inline;
4948 --------------------------
4949 -- Check_Body_To_Inline --
4950 --------------------------
4952 function Check_Body_To_Inline
4954 Subp : Entity_Id) return Boolean
4956 Max_Size : constant := 10;
4957 Stat_Count : Integer := 0;
4959 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
4960 -- Check for declarations that make inlining not worthwhile
4962 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
4963 -- Check for statements that make inlining not worthwhile: any
4964 -- tasking statement, nested at any level. Keep track of total
4965 -- number of elementary statements, as a measure of acceptable size.
4967 function Has_Pending_Instantiation return Boolean;
4968 -- Return True if some enclosing body contains instantiations that
4969 -- appear before the corresponding generic body.
4971 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean;
4972 -- Return True if all the return statements of the function body N
4973 -- are simple return statements and return a compile time constant
4975 function Returns_Intrinsic_Function_Call (N : Node_Id) return Boolean;
4976 -- Return True if all the return statements of the function body N
4977 -- are simple return statements and return an intrinsic function call
4979 function Uses_Secondary_Stack (N : Node_Id) return Boolean;
4980 -- If the body of the subprogram includes a call that returns an
4981 -- unconstrained type, the secondary stack is involved, and it
4982 -- is not worth inlining.
4984 ------------------------------
4985 -- Has_Excluded_Declaration --
4986 ------------------------------
4988 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
4991 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
4992 -- Nested subprograms make a given body ineligible for inlining,
4993 -- but we make an exception for instantiations of unchecked
4994 -- conversion. The body has not been analyzed yet, so check the
4995 -- name, and verify that the visible entity with that name is the
4998 -----------------------------
4999 -- Is_Unchecked_Conversion --
5000 -----------------------------
5002 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
5003 Id : constant Node_Id := Name (D);
5007 if Nkind (Id) = N_Identifier
5008 and then Chars (Id) = Name_Unchecked_Conversion
5010 Conv := Current_Entity (Id);
5012 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
5014 Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
5016 Conv := Current_Entity (Selector_Name (Id));
5021 return Present (Conv)
5022 and then Is_Predefined_File_Name
5023 (Unit_File_Name (Get_Source_Unit (Conv)))
5024 and then Is_Intrinsic_Subprogram (Conv);
5025 end Is_Unchecked_Conversion;
5027 -- Start of processing for Has_Excluded_Declaration
5031 while Present (D) loop
5032 if (Nkind (D) = N_Function_Instantiation
5033 and then not Is_Unchecked_Conversion (D))
5034 or else Nkind_In (D, N_Protected_Type_Declaration,
5035 N_Package_Declaration,
5036 N_Package_Instantiation,
5038 N_Procedure_Instantiation,
5039 N_Task_Type_Declaration)
5042 ("cannot inline & (non-allowed declaration)?", D, Subp);
5051 end Has_Excluded_Declaration;
5053 ----------------------------
5054 -- Has_Excluded_Statement --
5055 ----------------------------
5057 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
5063 while Present (S) loop
5064 Stat_Count := Stat_Count + 1;
5066 if Nkind_In (S, N_Abort_Statement,
5067 N_Asynchronous_Select,
5068 N_Conditional_Entry_Call,
5069 N_Delay_Relative_Statement,
5070 N_Delay_Until_Statement,
5075 ("cannot inline & (non-allowed statement)?", S, Subp);
5078 elsif Nkind (S) = N_Block_Statement then
5079 if Present (Declarations (S))
5080 and then Has_Excluded_Declaration (Declarations (S))
5084 elsif Present (Handled_Statement_Sequence (S)) then
5086 (Exception_Handlers (Handled_Statement_Sequence (S)))
5089 ("cannot inline& (exception handler)?",
5090 First (Exception_Handlers
5091 (Handled_Statement_Sequence (S))),
5095 elsif Has_Excluded_Statement
5096 (Statements (Handled_Statement_Sequence (S)))
5102 elsif Nkind (S) = N_Case_Statement then
5103 E := First (Alternatives (S));
5104 while Present (E) loop
5105 if Has_Excluded_Statement (Statements (E)) then
5112 elsif Nkind (S) = N_If_Statement then
5113 if Has_Excluded_Statement (Then_Statements (S)) then
5117 if Present (Elsif_Parts (S)) then
5118 E := First (Elsif_Parts (S));
5119 while Present (E) loop
5120 if Has_Excluded_Statement (Then_Statements (E)) then
5127 if Present (Else_Statements (S))
5128 and then Has_Excluded_Statement (Else_Statements (S))
5133 elsif Nkind (S) = N_Loop_Statement
5134 and then Has_Excluded_Statement (Statements (S))
5138 elsif Nkind (S) = N_Extended_Return_Statement then
5139 if Present (Handled_Statement_Sequence (S))
5141 Has_Excluded_Statement
5142 (Statements (Handled_Statement_Sequence (S)))
5146 elsif Present (Handled_Statement_Sequence (S))
5148 Present (Exception_Handlers
5149 (Handled_Statement_Sequence (S)))
5152 ("cannot inline& (exception handler)?",
5153 First (Exception_Handlers
5154 (Handled_Statement_Sequence (S))),
5164 end Has_Excluded_Statement;
5166 -------------------------------
5167 -- Has_Pending_Instantiation --
5168 -------------------------------
5170 function Has_Pending_Instantiation return Boolean is
5175 while Present (S) loop
5176 if Is_Compilation_Unit (S)
5177 or else Is_Child_Unit (S)
5181 elsif Ekind (S) = E_Package
5182 and then Has_Forward_Instantiation (S)
5191 end Has_Pending_Instantiation;
5193 ------------------------------------
5194 -- Returns_Compile_Time_Constant --
5195 ------------------------------------
5197 function Returns_Compile_Time_Constant (N : Node_Id) return Boolean is
5199 function Check_Return (N : Node_Id) return Traverse_Result;
5205 function Check_Return (N : Node_Id) return Traverse_Result is
5207 if Nkind (N) = N_Extended_Return_Statement then
5210 elsif Nkind (N) = N_Simple_Return_Statement then
5211 if Present (Expression (N)) then
5213 Orig_Expr : constant Node_Id :=
5214 Original_Node (Expression (N));
5217 if Nkind_In (Orig_Expr, N_Integer_Literal,
5219 N_Character_Literal)
5223 elsif Is_Entity_Name (Orig_Expr)
5224 and then Ekind (Entity (Orig_Expr)) = E_Constant
5225 and then Is_Static_Expression (Orig_Expr)
5233 -- Expression has wrong form
5239 -- Continue analyzing statements
5246 function Check_All_Returns is new Traverse_Func (Check_Return);
5248 -- Start of processing for Returns_Compile_Time_Constant
5251 return Check_All_Returns (N) = OK;
5252 end Returns_Compile_Time_Constant;
5254 --------------------------------------
5255 -- Returns_Intrinsic_Function_Call --
5256 --------------------------------------
5258 function Returns_Intrinsic_Function_Call
5259 (N : Node_Id) return Boolean
5261 function Check_Return (N : Node_Id) return Traverse_Result;
5267 function Check_Return (N : Node_Id) return Traverse_Result is
5269 if Nkind (N) = N_Extended_Return_Statement then
5272 elsif Nkind (N) = N_Simple_Return_Statement then
5273 if Present (Expression (N)) then
5275 Orig_Expr : constant Node_Id :=
5276 Original_Node (Expression (N));
5279 if Nkind (Orig_Expr) in N_Op
5280 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5284 elsif Nkind (Orig_Expr) in N_Has_Entity
5285 and then Present (Entity (Orig_Expr))
5286 and then Ekind (Entity (Orig_Expr)) = E_Function
5287 and then Is_Inlined (Entity (Orig_Expr))
5291 elsif Nkind (Orig_Expr) in N_Has_Entity
5292 and then Present (Entity (Orig_Expr))
5293 and then Is_Intrinsic_Subprogram (Entity (Orig_Expr))
5302 -- Expression has wrong form
5308 -- Continue analyzing statements
5315 function Check_All_Returns is new Traverse_Func (Check_Return);
5317 -- Start of processing for Returns_Intrinsic_Function_Call
5320 return Check_All_Returns (N) = OK;
5321 end Returns_Intrinsic_Function_Call;
5323 --------------------------
5324 -- Uses_Secondary_Stack --
5325 --------------------------
5327 function Uses_Secondary_Stack (N : Node_Id) return Boolean is
5329 function Check_Call (N : Node_Id) return Traverse_Result;
5330 -- Look for function calls that return an unconstrained type
5336 function Check_Call (N : Node_Id) return Traverse_Result is
5338 if Nkind (N) = N_Function_Call
5339 and then Is_Entity_Name (Name (N))
5340 and then Is_Composite_Type (Etype (Entity (Name (N))))
5341 and then not Is_Constrained (Etype (Entity (Name (N))))
5344 ("cannot inline & (call returns unconstrained type)?",
5353 function Check_Calls is new Traverse_Func (Check_Call);
5355 -- Start of processing for Uses_Secondary_Stack
5358 return Check_Calls (N) = Abandon;
5359 end Uses_Secondary_Stack;
5363 Decl : constant Node_Id := Unit_Declaration_Node (Spec_Id);
5364 May_Inline : constant Boolean :=
5365 Has_Pragma_Inline_Always (Spec_Id)
5366 or else (Has_Pragma_Inline (Spec_Id)
5367 and then ((Optimization_Level > 0
5368 and then Ekind (Spec_Id)
5370 or else Front_End_Inlining));
5371 Body_To_Analyze : Node_Id;
5373 -- Start of processing for Check_Body_To_Inline
5376 -- No action needed in stubs since the attribute Body_To_Inline
5379 if Nkind (Decl) = N_Subprogram_Body_Stub then
5382 -- Cannot build the body to inline if the attribute is already set.
5383 -- This attribute may have been set if this is a subprogram renaming
5384 -- declarations (see Freeze.Build_Renamed_Body).
5386 elsif Present (Body_To_Inline (Decl)) then
5389 -- No action needed if the subprogram does not fulfill the minimum
5390 -- conditions to be inlined by the frontend
5392 elsif not May_Inline then
5396 -- Check excluded declarations
5398 if Present (Declarations (N))
5399 and then Has_Excluded_Declaration (Declarations (N))
5404 -- Check excluded statements
5406 if Present (Handled_Statement_Sequence (N)) then
5408 (Exception_Handlers (Handled_Statement_Sequence (N)))
5411 ("cannot inline& (exception handler)?",
5413 (Exception_Handlers (Handled_Statement_Sequence (N))),
5418 elsif Has_Excluded_Statement
5419 (Statements (Handled_Statement_Sequence (N)))
5425 -- For backward compatibility, compiling under -gnatN we do not
5426 -- inline a subprogram that is too large, unless it is marked
5427 -- Inline_Always. This pragma does not suppress the other checks
5428 -- on inlining (forbidden declarations, handlers, etc).
5430 if Front_End_Inlining
5431 and then not Has_Pragma_Inline_Always (Subp)
5432 and then Stat_Count > Max_Size
5434 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
5438 -- If some enclosing body contains instantiations that appear before
5439 -- the corresponding generic body, the enclosing body has a freeze
5440 -- node so that it can be elaborated after the generic itself. This
5441 -- might conflict with subsequent inlinings, so that it is unsafe to
5442 -- try to inline in such a case.
5444 if Has_Pending_Instantiation then
5446 ("cannot inline& (forward instance within enclosing body)?",
5452 -- Generate and preanalyze the body to inline (needed to perform
5453 -- the rest of the checks)
5455 Generate_Body_To_Inline (N, Body_To_Analyze);
5457 if Ekind (Subp) = E_Function then
5458 Set_Result_Definition (Specification (Body_To_Analyze),
5459 New_Occurrence_Of (Etype (Subp), Sloc (N)));
5462 -- Nest the body to analyze within the real one
5464 if No (Declarations (N)) then
5465 Set_Declarations (N, New_List (Body_To_Analyze));
5467 Append_To (Declarations (N), Body_To_Analyze);
5470 Preanalyze (Body_To_Analyze);
5471 Remove (Body_To_Analyze);
5473 -- Keep separate checks needed when compiling without optimizations
5475 if Optimization_Level = 0
5477 -- AAMP and VM targets have no support for inlining in the backend
5478 -- and hence we use frontend inlining at all optimization levels.
5480 or else AAMP_On_Target
5481 or else VM_Target /= No_VM
5483 -- Cannot inline functions whose body has a call that returns an
5484 -- unconstrained type since the secondary stack is involved, and
5485 -- it is not worth inlining.
5487 if Uses_Secondary_Stack (Body_To_Analyze) then
5490 -- Cannot inline functions that return controlled types since
5491 -- controlled actions interfere in complex ways with inlining.
5493 elsif Ekind (Subp) = E_Function
5494 and then Needs_Finalization (Etype (Subp))
5497 ("cannot inline & (controlled return type)?", N, Subp);
5500 elsif Returns_Unconstrained_Type (Subp) then
5502 ("cannot inline & (unconstrained return type)?", N, Subp);
5506 -- Compiling with optimizations enabled
5509 -- Procedures are never frontend inlined in this case
5511 if Ekind (Subp) /= E_Function then
5514 -- Functions returning unconstrained types are tested
5515 -- separately (see Can_Split_Unconstrained_Function).
5517 elsif Returns_Unconstrained_Type (Subp) then
5520 -- Check supported cases
5522 elsif not Returns_Compile_Time_Constant (Body_To_Analyze)
5523 and then Convention (Subp) /= Convention_Intrinsic
5524 and then not Returns_Intrinsic_Function_Call (Body_To_Analyze)
5531 end Check_Body_To_Inline;
5533 --------------------------------------
5534 -- Can_Split_Unconstrained_Function --
5535 --------------------------------------
5537 function Can_Split_Unconstrained_Function (N : Node_Id) return Boolean
5539 Ret_Node : constant Node_Id :=
5540 First (Statements (Handled_Statement_Sequence (N)));
5544 -- No user defined declarations allowed in the function except inside
5545 -- the unique return statement; implicit labels are the only allowed
5548 if not Is_Empty_List (Declarations (N)) then
5549 D := First (Declarations (N));
5550 while Present (D) loop
5551 if Nkind (D) /= N_Implicit_Label_Declaration then
5559 -- We only split the inlined function when we are generating the code
5560 -- of its body; otherwise we leave duplicated split subprograms in
5561 -- the tree which (if referenced) generate wrong references at link
5564 return In_Extended_Main_Code_Unit (N)
5565 and then Present (Ret_Node)
5566 and then Nkind (Ret_Node) = N_Extended_Return_Statement
5567 and then No (Next (Ret_Node))
5568 and then Present (Handled_Statement_Sequence (Ret_Node));
5569 end Can_Split_Unconstrained_Function;
5571 -----------------------------
5572 -- Generate_Body_To_Inline --
5573 -----------------------------
5575 procedure Generate_Body_To_Inline
5577 Body_To_Inline : out Node_Id)
5579 procedure Remove_Pragmas (N : Node_Id);
5580 -- Remove occurrences of pragmas that may reference the formals of
5581 -- N. The analysis of the non-inlined body will handle these pragmas
5584 --------------------
5585 -- Remove_Pragmas --
5586 --------------------
5588 procedure Remove_Pragmas (N : Node_Id) is
5593 Decl := First (Declarations (N));
5594 while Present (Decl) loop
5597 if Nkind (Decl) = N_Pragma
5598 and then Nam_In (Pragma_Name (Decl), Name_Unreferenced,
5608 -- Start of processing for Generate_Body_To_Inline
5611 -- Within an instance, the body to inline must be treated as a nested
5612 -- generic, so that the proper global references are preserved.
5614 -- Note that we do not do this at the library level, because it
5615 -- is not needed, and furthermore this causes trouble if front
5616 -- end inlining is activated (-gnatN).
5619 and then Scope (Current_Scope) /= Standard_Standard
5621 Body_To_Inline := Copy_Generic_Node (N, Empty, True);
5623 Body_To_Inline := Copy_Separate_Tree (N);
5626 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
5627 -- parameter has no meaning when the body is inlined and the formals
5628 -- are rewritten. Remove it from body to inline. The analysis of the
5629 -- non-inlined body will handle the pragma properly.
5631 Remove_Pragmas (Body_To_Inline);
5633 -- We need to capture references to the formals in order
5634 -- to substitute the actuals at the point of inlining, i.e.
5635 -- instantiation. To treat the formals as globals to the body to
5636 -- inline, we nest it within a dummy parameterless subprogram,
5637 -- declared within the real one.
5639 Set_Parameter_Specifications
5640 (Specification (Body_To_Inline), No_List);
5642 -- A new internal name is associated with Body_To_Inline to avoid
5643 -- conflicts when the non-inlined body N is analyzed.
5645 Set_Defining_Unit_Name (Specification (Body_To_Inline),
5646 Make_Defining_Identifier (Sloc (N), New_Internal_Name ('P')));
5647 Set_Corresponding_Spec (Body_To_Inline, Empty);
5648 end Generate_Body_To_Inline;
5650 ----------------------------------
5651 -- Split_Unconstrained_Function --
5652 ----------------------------------
5654 procedure Split_Unconstrained_Function
5656 Spec_Id : Entity_Id)
5658 Loc : constant Source_Ptr := Sloc (N);
5659 Ret_Node : constant Node_Id :=
5660 First (Statements (Handled_Statement_Sequence (N)));
5661 Ret_Obj : constant Node_Id :=
5662 First (Return_Object_Declarations (Ret_Node));
5664 procedure Build_Procedure
5665 (Proc_Id : out Entity_Id;
5666 Decl_List : out List_Id);
5667 -- Build a procedure containing the statements found in the extended
5668 -- return statement of the unconstrained function body N.
5670 procedure Build_Procedure
5671 (Proc_Id : out Entity_Id;
5672 Decl_List : out List_Id)
5675 Formal_List : constant List_Id := New_List;
5676 Proc_Spec : Node_Id;
5677 Proc_Body : Node_Id;
5678 Subp_Name : constant Name_Id := New_Internal_Name ('F');
5679 Body_Decl_List : List_Id := No_List;
5680 Param_Type : Node_Id;
5683 if Nkind (Object_Definition (Ret_Obj)) = N_Identifier then
5684 Param_Type := New_Copy (Object_Definition (Ret_Obj));
5687 New_Copy (Subtype_Mark (Object_Definition (Ret_Obj)));
5690 Append_To (Formal_List,
5691 Make_Parameter_Specification (Loc,
5692 Defining_Identifier =>
5693 Make_Defining_Identifier (Loc,
5694 Chars => Chars (Defining_Identifier (Ret_Obj))),
5695 In_Present => False,
5696 Out_Present => True,
5697 Null_Exclusion_Present => False,
5698 Parameter_Type => Param_Type));
5700 Formal := First_Formal (Spec_Id);
5701 while Present (Formal) loop
5702 Append_To (Formal_List,
5703 Make_Parameter_Specification (Loc,
5704 Defining_Identifier =>
5705 Make_Defining_Identifier (Sloc (Formal),
5706 Chars => Chars (Formal)),
5707 In_Present => In_Present (Parent (Formal)),
5708 Out_Present => Out_Present (Parent (Formal)),
5709 Null_Exclusion_Present =>
5710 Null_Exclusion_Present (Parent (Formal)),
5712 New_Occurrence_Of (Etype (Formal), Loc),
5714 Copy_Separate_Tree (Expression (Parent (Formal)))));
5716 Next_Formal (Formal);
5720 Make_Defining_Identifier (Loc, Chars => Subp_Name);
5723 Make_Procedure_Specification (Loc,
5724 Defining_Unit_Name => Proc_Id,
5725 Parameter_Specifications => Formal_List);
5727 Decl_List := New_List;
5729 Append_To (Decl_List,
5730 Make_Subprogram_Declaration (Loc, Proc_Spec));
5732 -- Can_Convert_Unconstrained_Function checked that the function
5733 -- has no local declarations except implicit label declarations.
5734 -- Copy these declarations to the built procedure.
5736 if Present (Declarations (N)) then
5737 Body_Decl_List := New_List;
5744 D := First (Declarations (N));
5745 while Present (D) loop
5746 pragma Assert (Nkind (D) = N_Implicit_Label_Declaration);
5749 Make_Implicit_Label_Declaration (Loc,
5750 Make_Defining_Identifier (Loc,
5751 Chars => Chars (Defining_Identifier (D))),
5752 Label_Construct => Empty);
5753 Append_To (Body_Decl_List, New_D);
5760 pragma Assert (Present (Handled_Statement_Sequence (Ret_Node)));
5763 Make_Subprogram_Body (Loc,
5764 Specification => Copy_Separate_Tree (Proc_Spec),
5765 Declarations => Body_Decl_List,
5766 Handled_Statement_Sequence =>
5767 Copy_Separate_Tree (Handled_Statement_Sequence (Ret_Node)));
5769 Set_Defining_Unit_Name (Specification (Proc_Body),
5770 Make_Defining_Identifier (Loc, Subp_Name));
5772 Append_To (Decl_List, Proc_Body);
5773 end Build_Procedure;
5777 New_Obj : constant Node_Id := Copy_Separate_Tree (Ret_Obj);
5779 Proc_Id : Entity_Id;
5780 Proc_Call : Node_Id;
5782 -- Start of processing for Split_Unconstrained_Function
5785 -- Build the associated procedure, analyze it and insert it before
5786 -- the function body N
5789 Scope : constant Entity_Id := Current_Scope;
5790 Decl_List : List_Id;
5793 Build_Procedure (Proc_Id, Decl_List);
5794 Insert_Actions (N, Decl_List);
5798 -- Build the call to the generated procedure
5801 Actual_List : constant List_Id := New_List;
5805 Append_To (Actual_List,
5806 New_Occurrence_Of (Defining_Identifier (New_Obj), Loc));
5808 Formal := First_Formal (Spec_Id);
5809 while Present (Formal) loop
5810 Append_To (Actual_List, New_Occurrence_Of (Formal, Loc));
5812 -- Avoid spurious warning on unreferenced formals
5814 Set_Referenced (Formal);
5815 Next_Formal (Formal);
5819 Make_Procedure_Call_Statement (Loc,
5820 Name => New_Occurrence_Of (Proc_Id, Loc),
5821 Parameter_Associations => Actual_List);
5829 -- main_1__F1b (New_Obj, ...);
5834 Make_Block_Statement (Loc,
5835 Declarations => New_List (New_Obj),
5836 Handled_Statement_Sequence =>
5837 Make_Handled_Sequence_Of_Statements (Loc,
5838 Statements => New_List (
5842 Make_Simple_Return_Statement (Loc,
5845 (Defining_Identifier (New_Obj), Loc)))));
5847 Rewrite (Ret_Node, Blk_Stmt);
5848 end Split_Unconstrained_Function;
5850 -- Start of processing for Check_And_Build_Body_To_Inline
5853 -- Do not inline any subprogram that contains nested subprograms, since
5854 -- the backend inlining circuit seems to generate uninitialized
5855 -- references in this case. We know this happens in the case of front
5856 -- end ZCX support, but it also appears it can happen in other cases as
5857 -- well. The backend often rejects attempts to inline in the case of
5858 -- nested procedures anyway, so little if anything is lost by this.
5859 -- Note that this is test is for the benefit of the back-end. There is
5860 -- a separate test for front-end inlining that also rejects nested
5863 -- Do not do this test if errors have been detected, because in some
5864 -- error cases, this code blows up, and we don't need it anyway if
5865 -- there have been errors, since we won't get to the linker anyway.
5867 if Comes_From_Source (Body_Id)
5868 and then (Has_Pragma_Inline_Always (Spec_Id)
5869 or else Optimization_Level > 0)
5870 and then Serious_Errors_Detected = 0
5878 P_Ent := Scope (P_Ent);
5879 exit when No (P_Ent) or else P_Ent = Standard_Standard;
5881 if Is_Subprogram (P_Ent) then
5882 Set_Is_Inlined (P_Ent, False);
5884 if Comes_From_Source (P_Ent)
5885 and then Has_Pragma_Inline (P_Ent)
5888 ("cannot inline& (nested subprogram)?", N, P_Ent,
5889 Is_Serious => True);
5896 -- Build the body to inline only if really needed
5898 if Check_Body_To_Inline (N, Spec_Id)
5899 and then Serious_Errors_Detected = 0
5901 if Returns_Unconstrained_Type (Spec_Id) then
5902 if Can_Split_Unconstrained_Function (N) then
5903 Split_Unconstrained_Function (N, Spec_Id);
5904 Build_Body_To_Inline (N, Spec_Id);
5905 Set_Is_Inlined (Spec_Id);
5908 Build_Body_To_Inline (N, Spec_Id);
5909 Set_Is_Inlined (Spec_Id);
5912 end Check_And_Build_Body_To_Inline;
5914 -----------------------
5915 -- Check_Conformance --
5916 -----------------------
5918 procedure Check_Conformance
5919 (New_Id : Entity_Id;
5921 Ctype : Conformance_Type;
5923 Conforms : out Boolean;
5924 Err_Loc : Node_Id := Empty;
5925 Get_Inst : Boolean := False;
5926 Skip_Controlling_Formals : Boolean := False)
5928 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5929 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5930 -- If Errmsg is True, then processing continues to post an error message
5931 -- for conformance error on given node. Two messages are output. The
5932 -- first message points to the previous declaration with a general "no
5933 -- conformance" message. The second is the detailed reason, supplied as
5934 -- Msg. The parameter N provide information for a possible & insertion
5935 -- in the message, and also provides the location for posting the
5936 -- message in the absence of a specified Err_Loc location.
5938 -----------------------
5939 -- Conformance_Error --
5940 -----------------------
5942 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5949 if No (Err_Loc) then
5955 Error_Msg_Sloc := Sloc (Old_Id);
5958 when Type_Conformant =>
5959 Error_Msg_N -- CODEFIX
5960 ("not type conformant with declaration#!", Enode);
5962 when Mode_Conformant =>
5963 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5965 ("not mode conformant with operation inherited#!",
5969 ("not mode conformant with declaration#!", Enode);
5972 when Subtype_Conformant =>
5973 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5975 ("not subtype conformant with operation inherited#!",
5979 ("not subtype conformant with declaration#!", Enode);
5982 when Fully_Conformant =>
5983 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5984 Error_Msg_N -- CODEFIX
5985 ("not fully conformant with operation inherited#!",
5988 Error_Msg_N -- CODEFIX
5989 ("not fully conformant with declaration#!", Enode);
5993 Error_Msg_NE (Msg, Enode, N);
5995 end Conformance_Error;
5999 Old_Type : constant Entity_Id := Etype (Old_Id);
6000 New_Type : constant Entity_Id := Etype (New_Id);
6001 Old_Formal : Entity_Id;
6002 New_Formal : Entity_Id;
6003 Access_Types_Match : Boolean;
6004 Old_Formal_Base : Entity_Id;
6005 New_Formal_Base : Entity_Id;
6007 -- Start of processing for Check_Conformance
6012 -- We need a special case for operators, since they don't appear
6015 if Ctype = Type_Conformant then
6016 if Ekind (New_Id) = E_Operator
6017 and then Operator_Matches_Spec (New_Id, Old_Id)
6023 -- If both are functions/operators, check return types conform
6025 if Old_Type /= Standard_Void_Type
6026 and then New_Type /= Standard_Void_Type
6029 -- If we are checking interface conformance we omit controlling
6030 -- arguments and result, because we are only checking the conformance
6031 -- of the remaining parameters.
6033 if Has_Controlling_Result (Old_Id)
6034 and then Has_Controlling_Result (New_Id)
6035 and then Skip_Controlling_Formals
6039 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
6040 if Ctype >= Subtype_Conformant
6041 and then not Predicates_Match (Old_Type, New_Type)
6044 ("\predicate of return type does not match!", New_Id);
6047 ("\return type does not match!", New_Id);
6053 -- Ada 2005 (AI-231): In case of anonymous access types check the
6054 -- null-exclusion and access-to-constant attributes match.
6056 if Ada_Version >= Ada_2005
6057 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
6059 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
6060 or else Is_Access_Constant (Etype (Old_Type)) /=
6061 Is_Access_Constant (Etype (New_Type)))
6063 Conformance_Error ("\return type does not match!", New_Id);
6067 -- If either is a function/operator and the other isn't, error
6069 elsif Old_Type /= Standard_Void_Type
6070 or else New_Type /= Standard_Void_Type
6072 Conformance_Error ("\functions can only match functions!", New_Id);
6076 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
6077 -- If this is a renaming as body, refine error message to indicate that
6078 -- the conflict is with the original declaration. If the entity is not
6079 -- frozen, the conventions don't have to match, the one of the renamed
6080 -- entity is inherited.
6082 if Ctype >= Subtype_Conformant then
6083 if Convention (Old_Id) /= Convention (New_Id) then
6084 if not Is_Frozen (New_Id) then
6087 elsif Present (Err_Loc)
6088 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
6089 and then Present (Corresponding_Spec (Err_Loc))
6091 Error_Msg_Name_1 := Chars (New_Id);
6093 Name_Ada + Convention_Id'Pos (Convention (New_Id));
6094 Conformance_Error ("\prior declaration for% has convention %!");
6097 Conformance_Error ("\calling conventions do not match!");
6102 elsif Is_Formal_Subprogram (Old_Id)
6103 or else Is_Formal_Subprogram (New_Id)
6105 Conformance_Error ("\formal subprograms not allowed!");
6110 -- Deal with parameters
6112 -- Note: we use the entity information, rather than going directly
6113 -- to the specification in the tree. This is not only simpler, but
6114 -- absolutely necessary for some cases of conformance tests between
6115 -- operators, where the declaration tree simply does not exist.
6117 Old_Formal := First_Formal (Old_Id);
6118 New_Formal := First_Formal (New_Id);
6119 while Present (Old_Formal) and then Present (New_Formal) loop
6120 if Is_Controlling_Formal (Old_Formal)
6121 and then Is_Controlling_Formal (New_Formal)
6122 and then Skip_Controlling_Formals
6124 -- The controlling formals will have different types when
6125 -- comparing an interface operation with its match, but both
6126 -- or neither must be access parameters.
6128 if Is_Access_Type (Etype (Old_Formal))
6130 Is_Access_Type (Etype (New_Formal))
6132 goto Skip_Controlling_Formal;
6135 ("\access parameter does not match!", New_Formal);
6139 -- Ada 2012: Mode conformance also requires that formal parameters
6140 -- be both aliased, or neither.
6142 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
6143 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
6145 ("\aliased parameter mismatch!", New_Formal);
6149 if Ctype = Fully_Conformant then
6151 -- Names must match. Error message is more accurate if we do
6152 -- this before checking that the types of the formals match.
6154 if Chars (Old_Formal) /= Chars (New_Formal) then
6155 Conformance_Error ("\name & does not match!", New_Formal);
6157 -- Set error posted flag on new formal as well to stop
6158 -- junk cascaded messages in some cases.
6160 Set_Error_Posted (New_Formal);
6164 -- Null exclusion must match
6166 if Null_Exclusion_Present (Parent (Old_Formal))
6168 Null_Exclusion_Present (Parent (New_Formal))
6170 -- Only give error if both come from source. This should be
6171 -- investigated some time, since it should not be needed ???
6173 if Comes_From_Source (Old_Formal)
6175 Comes_From_Source (New_Formal)
6178 ("\null exclusion for & does not match", New_Formal);
6180 -- Mark error posted on the new formal to avoid duplicated
6181 -- complaint about types not matching.
6183 Set_Error_Posted (New_Formal);
6188 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6189 -- case occurs whenever a subprogram is being renamed and one of its
6190 -- parameters imposes a null exclusion. For example:
6192 -- type T is null record;
6193 -- type Acc_T is access T;
6194 -- subtype Acc_T_Sub is Acc_T;
6196 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6197 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6200 Old_Formal_Base := Etype (Old_Formal);
6201 New_Formal_Base := Etype (New_Formal);
6204 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
6205 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
6208 Access_Types_Match := Ada_Version >= Ada_2005
6210 -- Ensure that this rule is only applied when New_Id is a
6211 -- renaming of Old_Id.
6213 and then Nkind (Parent (Parent (New_Id))) =
6214 N_Subprogram_Renaming_Declaration
6215 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
6216 and then Present (Entity (Name (Parent (Parent (New_Id)))))
6217 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
6219 -- Now handle the allowed access-type case
6221 and then Is_Access_Type (Old_Formal_Base)
6222 and then Is_Access_Type (New_Formal_Base)
6224 -- The type kinds must match. The only exception occurs with
6225 -- multiple generics of the form:
6228 -- type F is private; type A is private;
6229 -- type F_Ptr is access F; type A_Ptr is access A;
6230 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
6231 -- package F_Pack is ... package A_Pack is
6232 -- package F_Inst is
6233 -- new F_Pack (A, A_Ptr, A_P);
6235 -- When checking for conformance between the parameters of A_P
6236 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
6237 -- because the compiler has transformed A_Ptr into a subtype of
6238 -- F_Ptr. We catch this case in the code below.
6240 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
6242 (Is_Generic_Type (Old_Formal_Base)
6243 and then Is_Generic_Type (New_Formal_Base)
6244 and then Is_Internal (New_Formal_Base)
6245 and then Etype (Etype (New_Formal_Base)) =
6247 and then Directly_Designated_Type (Old_Formal_Base) =
6248 Directly_Designated_Type (New_Formal_Base)
6249 and then ((Is_Itype (Old_Formal_Base)
6250 and then Can_Never_Be_Null (Old_Formal_Base))
6252 (Is_Itype (New_Formal_Base)
6253 and then Can_Never_Be_Null (New_Formal_Base)));
6255 -- Types must always match. In the visible part of an instance,
6256 -- usual overloading rules for dispatching operations apply, and
6257 -- we check base types (not the actual subtypes).
6259 if In_Instance_Visible_Part
6260 and then Is_Dispatching_Operation (New_Id)
6262 if not Conforming_Types
6263 (T1 => Base_Type (Etype (Old_Formal)),
6264 T2 => Base_Type (Etype (New_Formal)),
6266 Get_Inst => Get_Inst)
6267 and then not Access_Types_Match
6269 Conformance_Error ("\type of & does not match!", New_Formal);
6273 elsif not Conforming_Types
6274 (T1 => Old_Formal_Base,
6275 T2 => New_Formal_Base,
6277 Get_Inst => Get_Inst)
6278 and then not Access_Types_Match
6280 -- Don't give error message if old type is Any_Type. This test
6281 -- avoids some cascaded errors, e.g. in case of a bad spec.
6283 if Errmsg and then Old_Formal_Base = Any_Type then
6286 if Ctype >= Subtype_Conformant
6288 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
6291 ("\predicate of & does not match!", New_Formal);
6294 ("\type of & does not match!", New_Formal);
6301 -- For mode conformance, mode must match
6303 if Ctype >= Mode_Conformant then
6304 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6305 if not Ekind_In (New_Id, E_Function, E_Procedure)
6306 or else not Is_Primitive_Wrapper (New_Id)
6308 Conformance_Error ("\mode of & does not match!", New_Formal);
6312 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6314 if Is_Protected_Type
6315 (Corresponding_Concurrent_Type (T))
6317 Error_Msg_PT (T, New_Id);
6320 ("\mode of & does not match!", New_Formal);
6327 -- Part of mode conformance for access types is having the same
6328 -- constant modifier.
6330 elsif Access_Types_Match
6331 and then Is_Access_Constant (Old_Formal_Base) /=
6332 Is_Access_Constant (New_Formal_Base)
6335 ("\constant modifier does not match!", New_Formal);
6340 if Ctype >= Subtype_Conformant then
6342 -- Ada 2005 (AI-231): In case of anonymous access types check
6343 -- the null-exclusion and access-to-constant attributes must
6344 -- match. For null exclusion, we test the types rather than the
6345 -- formals themselves, since the attribute is only set reliably
6346 -- on the formals in the Ada 95 case, and we exclude the case
6347 -- where Old_Formal is marked as controlling, to avoid errors
6348 -- when matching completing bodies with dispatching declarations
6349 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6351 if Ada_Version >= Ada_2005
6352 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
6353 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
6355 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6356 Can_Never_Be_Null (Etype (New_Formal))
6358 not Is_Controlling_Formal (Old_Formal))
6360 Is_Access_Constant (Etype (Old_Formal)) /=
6361 Is_Access_Constant (Etype (New_Formal)))
6363 -- Do not complain if error already posted on New_Formal. This
6364 -- avoids some redundant error messages.
6366 and then not Error_Posted (New_Formal)
6368 -- It is allowed to omit the null-exclusion in case of stream
6369 -- attribute subprograms. We recognize stream subprograms
6370 -- through their TSS-generated suffix.
6373 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6376 if TSS_Name /= TSS_Stream_Read
6377 and then TSS_Name /= TSS_Stream_Write
6378 and then TSS_Name /= TSS_Stream_Input
6379 and then TSS_Name /= TSS_Stream_Output
6381 -- Here we have a definite conformance error. It is worth
6382 -- special casing the error message for the case of a
6383 -- controlling formal (which excludes null).
6385 if Is_Controlling_Formal (New_Formal) then
6386 Error_Msg_Node_2 := Scope (New_Formal);
6388 ("\controlling formal& of& excludes null, "
6389 & "declaration must exclude null as well",
6392 -- Normal case (couldn't we give more detail here???)
6396 ("\type of & does not match!", New_Formal);
6405 -- Full conformance checks
6407 if Ctype = Fully_Conformant then
6409 -- We have checked already that names match
6411 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6413 -- Check default expressions for in parameters
6416 NewD : constant Boolean :=
6417 Present (Default_Value (New_Formal));
6418 OldD : constant Boolean :=
6419 Present (Default_Value (Old_Formal));
6421 if NewD or OldD then
6423 -- The old default value has been analyzed because the
6424 -- current full declaration will have frozen everything
6425 -- before. The new default value has not been analyzed,
6426 -- so analyze it now before we check for conformance.
6429 Push_Scope (New_Id);
6430 Preanalyze_Spec_Expression
6431 (Default_Value (New_Formal), Etype (New_Formal));
6435 if not (NewD and OldD)
6436 or else not Fully_Conformant_Expressions
6437 (Default_Value (Old_Formal),
6438 Default_Value (New_Formal))
6441 ("\default expression for & does not match!",
6450 -- A couple of special checks for Ada 83 mode. These checks are
6451 -- skipped if either entity is an operator in package Standard,
6452 -- or if either old or new instance is not from the source program.
6454 if Ada_Version = Ada_83
6455 and then Sloc (Old_Id) > Standard_Location
6456 and then Sloc (New_Id) > Standard_Location
6457 and then Comes_From_Source (Old_Id)
6458 and then Comes_From_Source (New_Id)
6461 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6462 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6465 -- Explicit IN must be present or absent in both cases. This
6466 -- test is required only in the full conformance case.
6468 if In_Present (Old_Param) /= In_Present (New_Param)
6469 and then Ctype = Fully_Conformant
6472 ("\(Ada 83) IN must appear in both declarations",
6477 -- Grouping (use of comma in param lists) must be the same
6478 -- This is where we catch a misconformance like:
6481 -- A : Integer; B : Integer
6483 -- which are represented identically in the tree except
6484 -- for the setting of the flags More_Ids and Prev_Ids.
6486 if More_Ids (Old_Param) /= More_Ids (New_Param)
6487 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6490 ("\grouping of & does not match!", New_Formal);
6496 -- This label is required when skipping controlling formals
6498 <<Skip_Controlling_Formal>>
6500 Next_Formal (Old_Formal);
6501 Next_Formal (New_Formal);
6504 if Present (Old_Formal) then
6505 Conformance_Error ("\too few parameters!");
6508 elsif Present (New_Formal) then
6509 Conformance_Error ("\too many parameters!", New_Formal);
6512 end Check_Conformance;
6514 -----------------------
6515 -- Check_Conventions --
6516 -----------------------
6518 procedure Check_Conventions (Typ : Entity_Id) is
6519 Ifaces_List : Elist_Id;
6521 procedure Check_Convention (Op : Entity_Id);
6522 -- Verify that the convention of inherited dispatching operation Op is
6523 -- consistent among all subprograms it overrides. In order to minimize
6524 -- the search, Search_From is utilized to designate a specific point in
6525 -- the list rather than iterating over the whole list once more.
6527 ----------------------
6528 -- Check_Convention --
6529 ----------------------
6531 procedure Check_Convention (Op : Entity_Id) is
6532 function Convention_Of (Id : Entity_Id) return Convention_Id;
6533 -- Given an entity, return its convention. The function treats Ghost
6534 -- as convention Ada because the two have the same dynamic semantics.
6540 function Convention_Of (Id : Entity_Id) return Convention_Id is
6541 Conv : constant Convention_Id := Convention (Id);
6543 if Conv = Convention_Ghost then
6544 return Convention_Ada;
6552 Op_Conv : constant Convention_Id := Convention_Of (Op);
6553 Iface_Conv : Convention_Id;
6554 Iface_Elmt : Elmt_Id;
6555 Iface_Prim_Elmt : Elmt_Id;
6556 Iface_Prim : Entity_Id;
6558 -- Start of processing for Check_Convention
6561 Iface_Elmt := First_Elmt (Ifaces_List);
6562 while Present (Iface_Elmt) loop
6564 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6565 while Present (Iface_Prim_Elmt) loop
6566 Iface_Prim := Node (Iface_Prim_Elmt);
6567 Iface_Conv := Convention_Of (Iface_Prim);
6569 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6570 and then Iface_Conv /= Op_Conv
6573 ("inconsistent conventions in primitive operations", Typ);
6575 Error_Msg_Name_1 := Chars (Op);
6576 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6577 Error_Msg_Sloc := Sloc (Op);
6579 if Comes_From_Source (Op) or else No (Alias (Op)) then
6580 if not Present (Overridden_Operation (Op)) then
6581 Error_Msg_N ("\\primitive % defined #", Typ);
6584 ("\\overriding operation % with " &
6585 "convention % defined #", Typ);
6588 else pragma Assert (Present (Alias (Op)));
6589 Error_Msg_Sloc := Sloc (Alias (Op));
6591 ("\\inherited operation % with " &
6592 "convention % defined #", Typ);
6595 Error_Msg_Name_1 := Chars (Op);
6596 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6597 Error_Msg_Sloc := Sloc (Iface_Prim);
6599 ("\\overridden operation % with " &
6600 "convention % defined #", Typ);
6602 -- Avoid cascading errors
6607 Next_Elmt (Iface_Prim_Elmt);
6610 Next_Elmt (Iface_Elmt);
6612 end Check_Convention;
6616 Prim_Op : Entity_Id;
6617 Prim_Op_Elmt : Elmt_Id;
6619 -- Start of processing for Check_Conventions
6622 if not Has_Interfaces (Typ) then
6626 Collect_Interfaces (Typ, Ifaces_List);
6628 -- The algorithm checks every overriding dispatching operation against
6629 -- all the corresponding overridden dispatching operations, detecting
6630 -- differences in conventions.
6632 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6633 while Present (Prim_Op_Elmt) loop
6634 Prim_Op := Node (Prim_Op_Elmt);
6636 -- A small optimization: skip the predefined dispatching operations
6637 -- since they always have the same convention.
6639 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6640 Check_Convention (Prim_Op);
6643 Next_Elmt (Prim_Op_Elmt);
6645 end Check_Conventions;
6647 ------------------------------
6648 -- Check_Delayed_Subprogram --
6649 ------------------------------
6651 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6654 procedure Possible_Freeze (T : Entity_Id);
6655 -- T is the type of either a formal parameter or of the return type.
6656 -- If T is not yet frozen and needs a delayed freeze, then the
6657 -- subprogram itself must be delayed. If T is the limited view of an
6658 -- incomplete type the subprogram must be frozen as well, because
6659 -- T may depend on local types that have not been frozen yet.
6661 ---------------------
6662 -- Possible_Freeze --
6663 ---------------------
6665 procedure Possible_Freeze (T : Entity_Id) is
6667 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6668 Set_Has_Delayed_Freeze (Designator);
6670 elsif Is_Access_Type (T)
6671 and then Has_Delayed_Freeze (Designated_Type (T))
6672 and then not Is_Frozen (Designated_Type (T))
6674 Set_Has_Delayed_Freeze (Designator);
6676 elsif Ekind (T) = E_Incomplete_Type
6677 and then From_Limited_With (T)
6679 Set_Has_Delayed_Freeze (Designator);
6681 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
6682 -- of a subprogram or entry declaration.
6684 elsif Ekind (T) = E_Incomplete_Type
6685 and then Ada_Version >= Ada_2012
6687 Set_Has_Delayed_Freeze (Designator);
6690 end Possible_Freeze;
6692 -- Start of processing for Check_Delayed_Subprogram
6695 -- All subprograms, including abstract subprograms, may need a freeze
6696 -- node if some formal type or the return type needs one.
6698 Possible_Freeze (Etype (Designator));
6699 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6701 -- Need delayed freeze if any of the formal types themselves need
6702 -- a delayed freeze and are not yet frozen.
6704 F := First_Formal (Designator);
6705 while Present (F) loop
6706 Possible_Freeze (Etype (F));
6707 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6711 -- Mark functions that return by reference. Note that it cannot be
6712 -- done for delayed_freeze subprograms because the underlying
6713 -- returned type may not be known yet (for private types)
6715 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6717 Typ : constant Entity_Id := Etype (Designator);
6718 Utyp : constant Entity_Id := Underlying_Type (Typ);
6720 if Is_Limited_View (Typ) then
6721 Set_Returns_By_Ref (Designator);
6722 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6723 Set_Returns_By_Ref (Designator);
6727 end Check_Delayed_Subprogram;
6729 ------------------------------------
6730 -- Check_Discriminant_Conformance --
6731 ------------------------------------
6733 procedure Check_Discriminant_Conformance
6738 Old_Discr : Entity_Id := First_Discriminant (Prev);
6739 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6740 New_Discr_Id : Entity_Id;
6741 New_Discr_Type : Entity_Id;
6743 procedure Conformance_Error (Msg : String; N : Node_Id);
6744 -- Post error message for conformance error on given node. Two messages
6745 -- are output. The first points to the previous declaration with a
6746 -- general "no conformance" message. The second is the detailed reason,
6747 -- supplied as Msg. The parameter N provide information for a possible
6748 -- & insertion in the message.
6750 -----------------------
6751 -- Conformance_Error --
6752 -----------------------
6754 procedure Conformance_Error (Msg : String; N : Node_Id) is
6756 Error_Msg_Sloc := Sloc (Prev_Loc);
6757 Error_Msg_N -- CODEFIX
6758 ("not fully conformant with declaration#!", N);
6759 Error_Msg_NE (Msg, N, N);
6760 end Conformance_Error;
6762 -- Start of processing for Check_Discriminant_Conformance
6765 while Present (Old_Discr) and then Present (New_Discr) loop
6766 New_Discr_Id := Defining_Identifier (New_Discr);
6768 -- The subtype mark of the discriminant on the full type has not
6769 -- been analyzed so we do it here. For an access discriminant a new
6772 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6774 Access_Definition (N, Discriminant_Type (New_Discr));
6777 Analyze (Discriminant_Type (New_Discr));
6778 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6780 -- Ada 2005: if the discriminant definition carries a null
6781 -- exclusion, create an itype to check properly for consistency
6782 -- with partial declaration.
6784 if Is_Access_Type (New_Discr_Type)
6785 and then Null_Exclusion_Present (New_Discr)
6788 Create_Null_Excluding_Itype
6789 (T => New_Discr_Type,
6790 Related_Nod => New_Discr,
6791 Scope_Id => Current_Scope);
6795 if not Conforming_Types
6796 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6798 Conformance_Error ("type of & does not match!", New_Discr_Id);
6801 -- Treat the new discriminant as an occurrence of the old one,
6802 -- for navigation purposes, and fill in some semantic
6803 -- information, for completeness.
6805 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6806 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6807 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6812 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6813 Conformance_Error ("name & does not match!", New_Discr_Id);
6817 -- Default expressions must match
6820 NewD : constant Boolean :=
6821 Present (Expression (New_Discr));
6822 OldD : constant Boolean :=
6823 Present (Expression (Parent (Old_Discr)));
6826 if NewD or OldD then
6828 -- The old default value has been analyzed and expanded,
6829 -- because the current full declaration will have frozen
6830 -- everything before. The new default values have not been
6831 -- expanded, so expand now to check conformance.
6834 Preanalyze_Spec_Expression
6835 (Expression (New_Discr), New_Discr_Type);
6838 if not (NewD and OldD)
6839 or else not Fully_Conformant_Expressions
6840 (Expression (Parent (Old_Discr)),
6841 Expression (New_Discr))
6845 ("default expression for & does not match!",
6852 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6854 if Ada_Version = Ada_83 then
6856 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6859 -- Grouping (use of comma in param lists) must be the same
6860 -- This is where we catch a misconformance like:
6863 -- A : Integer; B : Integer
6865 -- which are represented identically in the tree except
6866 -- for the setting of the flags More_Ids and Prev_Ids.
6868 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6869 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6872 ("grouping of & does not match!", New_Discr_Id);
6878 Next_Discriminant (Old_Discr);
6882 if Present (Old_Discr) then
6883 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6886 elsif Present (New_Discr) then
6888 ("too many discriminants!", Defining_Identifier (New_Discr));
6891 end Check_Discriminant_Conformance;
6893 ----------------------------
6894 -- Check_Fully_Conformant --
6895 ----------------------------
6897 procedure Check_Fully_Conformant
6898 (New_Id : Entity_Id;
6900 Err_Loc : Node_Id := Empty)
6903 pragma Warnings (Off, Result);
6906 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6907 end Check_Fully_Conformant;
6909 ---------------------------
6910 -- Check_Mode_Conformant --
6911 ---------------------------
6913 procedure Check_Mode_Conformant
6914 (New_Id : Entity_Id;
6916 Err_Loc : Node_Id := Empty;
6917 Get_Inst : Boolean := False)
6920 pragma Warnings (Off, Result);
6923 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6924 end Check_Mode_Conformant;
6926 --------------------------------
6927 -- Check_Overriding_Indicator --
6928 --------------------------------
6930 procedure Check_Overriding_Indicator
6932 Overridden_Subp : Entity_Id;
6933 Is_Primitive : Boolean)
6939 -- No overriding indicator for literals
6941 if Ekind (Subp) = E_Enumeration_Literal then
6944 elsif Ekind (Subp) = E_Entry then
6945 Decl := Parent (Subp);
6947 -- No point in analyzing a malformed operator
6949 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6950 and then Error_Posted (Subp)
6955 Decl := Unit_Declaration_Node (Subp);
6958 if Nkind_In (Decl, N_Subprogram_Body,
6959 N_Subprogram_Body_Stub,
6960 N_Subprogram_Declaration,
6961 N_Abstract_Subprogram_Declaration,
6962 N_Subprogram_Renaming_Declaration)
6964 Spec := Specification (Decl);
6966 elsif Nkind (Decl) = N_Entry_Declaration then
6973 -- The overriding operation is type conformant with the overridden one,
6974 -- but the names of the formals are not required to match. If the names
6975 -- appear permuted in the overriding operation, this is a possible
6976 -- source of confusion that is worth diagnosing. Controlling formals
6977 -- often carry names that reflect the type, and it is not worthwhile
6978 -- requiring that their names match.
6980 if Present (Overridden_Subp)
6981 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6988 Form1 := First_Formal (Subp);
6989 Form2 := First_Formal (Overridden_Subp);
6991 -- If the overriding operation is a synchronized operation, skip
6992 -- the first parameter of the overridden operation, which is
6993 -- implicit in the new one. If the operation is declared in the
6994 -- body it is not primitive and all formals must match.
6996 if Is_Concurrent_Type (Scope (Subp))
6997 and then Is_Tagged_Type (Scope (Subp))
6998 and then not Has_Completion (Scope (Subp))
7000 Form2 := Next_Formal (Form2);
7003 if Present (Form1) then
7004 Form1 := Next_Formal (Form1);
7005 Form2 := Next_Formal (Form2);
7008 while Present (Form1) loop
7009 if not Is_Controlling_Formal (Form1)
7010 and then Present (Next_Formal (Form2))
7011 and then Chars (Form1) = Chars (Next_Formal (Form2))
7013 Error_Msg_Node_2 := Alias (Overridden_Subp);
7014 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
7016 ("& does not match corresponding formal of&#",
7021 Next_Formal (Form1);
7022 Next_Formal (Form2);
7027 -- If there is an overridden subprogram, then check that there is no
7028 -- "not overriding" indicator, and mark the subprogram as overriding.
7029 -- This is not done if the overridden subprogram is marked as hidden,
7030 -- which can occur for the case of inherited controlled operations
7031 -- (see Derive_Subprogram), unless the inherited subprogram's parent
7032 -- subprogram is not itself hidden. (Note: This condition could probably
7033 -- be simplified, leaving out the testing for the specific controlled
7034 -- cases, but it seems safer and clearer this way, and echoes similar
7035 -- special-case tests of this kind in other places.)
7037 if Present (Overridden_Subp)
7038 and then (not Is_Hidden (Overridden_Subp)
7040 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
7043 and then Present (Alias (Overridden_Subp))
7044 and then not Is_Hidden (Alias (Overridden_Subp))))
7046 if Must_Not_Override (Spec) then
7047 Error_Msg_Sloc := Sloc (Overridden_Subp);
7049 if Ekind (Subp) = E_Entry then
7051 ("entry & overrides inherited operation #", Spec, Subp);
7054 ("subprogram & overrides inherited operation #", Spec, Subp);
7057 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
7058 -- as an extension of Root_Controlled, and thus has a useless Adjust
7059 -- operation. This operation should not be inherited by other limited
7060 -- controlled types. An explicit Adjust for them is not overriding.
7062 elsif Must_Override (Spec)
7063 and then Chars (Overridden_Subp) = Name_Adjust
7064 and then Is_Limited_Type (Etype (First_Formal (Subp)))
7065 and then Present (Alias (Overridden_Subp))
7067 Is_Predefined_File_Name
7068 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
7070 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7072 elsif Is_Subprogram (Subp) then
7073 if Is_Init_Proc (Subp) then
7076 elsif No (Overridden_Operation (Subp)) then
7078 -- For entities generated by Derive_Subprograms the overridden
7079 -- operation is the inherited primitive (which is available
7080 -- through the attribute alias)
7082 if (Is_Dispatching_Operation (Subp)
7083 or else Is_Dispatching_Operation (Overridden_Subp))
7084 and then not Comes_From_Source (Overridden_Subp)
7085 and then Find_Dispatching_Type (Overridden_Subp) =
7086 Find_Dispatching_Type (Subp)
7087 and then Present (Alias (Overridden_Subp))
7088 and then Comes_From_Source (Alias (Overridden_Subp))
7090 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
7093 Set_Overridden_Operation (Subp, Overridden_Subp);
7098 -- If primitive flag is set or this is a protected operation, then
7099 -- the operation is overriding at the point of its declaration, so
7100 -- warn if necessary. Otherwise it may have been declared before the
7101 -- operation it overrides and no check is required.
7104 and then not Must_Override (Spec)
7105 and then (Is_Primitive
7106 or else Ekind (Scope (Subp)) = E_Protected_Type)
7108 Style.Missing_Overriding (Decl, Subp);
7111 -- If Subp is an operator, it may override a predefined operation, if
7112 -- it is defined in the same scope as the type to which it applies.
7113 -- In that case Overridden_Subp is empty because of our implicit
7114 -- representation for predefined operators. We have to check whether the
7115 -- signature of Subp matches that of a predefined operator. Note that
7116 -- first argument provides the name of the operator, and the second
7117 -- argument the signature that may match that of a standard operation.
7118 -- If the indicator is overriding, then the operator must match a
7119 -- predefined signature, because we know already that there is no
7120 -- explicit overridden operation.
7122 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
7123 if Must_Not_Override (Spec) then
7125 -- If this is not a primitive or a protected subprogram, then
7126 -- "not overriding" is illegal.
7129 and then Ekind (Scope (Subp)) /= E_Protected_Type
7132 ("overriding indicator only allowed "
7133 & "if subprogram is primitive", Subp);
7135 elsif Can_Override_Operator (Subp) then
7137 ("subprogram& overrides predefined operator ", Spec, Subp);
7140 elsif Must_Override (Spec) then
7141 if No (Overridden_Operation (Subp))
7142 and then not Can_Override_Operator (Subp)
7144 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7147 elsif not Error_Posted (Subp)
7148 and then Style_Check
7149 and then Can_Override_Operator (Subp)
7151 not Is_Predefined_File_Name
7152 (Unit_File_Name (Get_Source_Unit (Subp)))
7154 -- If style checks are enabled, indicate that the indicator is
7155 -- missing. However, at the point of declaration, the type of
7156 -- which this is a primitive operation may be private, in which
7157 -- case the indicator would be premature.
7159 if Has_Private_Declaration (Etype (Subp))
7160 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
7164 Style.Missing_Overriding (Decl, Subp);
7168 elsif Must_Override (Spec) then
7169 if Ekind (Subp) = E_Entry then
7170 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
7172 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7175 -- If the operation is marked "not overriding" and it's not primitive
7176 -- then an error is issued, unless this is an operation of a task or
7177 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7178 -- has been specified have already been checked above.
7180 elsif Must_Not_Override (Spec)
7181 and then not Is_Primitive
7182 and then Ekind (Subp) /= E_Entry
7183 and then Ekind (Scope (Subp)) /= E_Protected_Type
7186 ("overriding indicator only allowed if subprogram is primitive",
7190 end Check_Overriding_Indicator;
7196 -- Note: this procedure needs to know far too much about how the expander
7197 -- messes with exceptions. The use of the flag Exception_Junk and the
7198 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7199 -- works, but is not very clean. It would be better if the expansion
7200 -- routines would leave Original_Node working nicely, and we could use
7201 -- Original_Node here to ignore all the peculiar expander messing ???
7203 procedure Check_Returns
7207 Proc : Entity_Id := Empty)
7211 procedure Check_Statement_Sequence (L : List_Id);
7212 -- Internal recursive procedure to check a list of statements for proper
7213 -- termination by a return statement (or a transfer of control or a
7214 -- compound statement that is itself internally properly terminated).
7216 ------------------------------
7217 -- Check_Statement_Sequence --
7218 ------------------------------
7220 procedure Check_Statement_Sequence (L : List_Id) is
7225 function Assert_False return Boolean;
7226 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7227 -- rewritten as a null statement when assertions are off. The assert
7228 -- is not active, but it is still enough to kill the warning.
7234 function Assert_False return Boolean is
7235 Orig : constant Node_Id := Original_Node (Last_Stm);
7238 if Nkind (Orig) = N_Pragma
7239 and then Pragma_Name (Orig) = Name_Assert
7240 and then not Error_Posted (Orig)
7243 Arg : constant Node_Id :=
7244 First (Pragma_Argument_Associations (Orig));
7245 Exp : constant Node_Id := Expression (Arg);
7247 return Nkind (Exp) = N_Identifier
7248 and then Chars (Exp) = Name_False;
7258 Raise_Exception_Call : Boolean;
7259 -- Set True if statement sequence terminated by Raise_Exception call
7260 -- or a Reraise_Occurrence call.
7262 -- Start of processing for Check_Statement_Sequence
7265 Raise_Exception_Call := False;
7267 -- Get last real statement
7269 Last_Stm := Last (L);
7271 -- Deal with digging out exception handler statement sequences that
7272 -- have been transformed by the local raise to goto optimization.
7273 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7274 -- optimization has occurred, we are looking at something like:
7277 -- original stmts in block
7281 -- goto L1; | omitted if No_Exception_Propagation
7286 -- goto L3; -- skip handler when exception not raised
7288 -- <<L1>> -- target label for local exception
7302 -- and what we have to do is to dig out the estmts1 and estmts2
7303 -- sequences (which were the original sequences of statements in
7304 -- the exception handlers) and check them.
7306 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7311 exit when Nkind (Stm) /= N_Block_Statement;
7312 exit when not Exception_Junk (Stm);
7315 exit when Nkind (Stm) /= N_Label;
7316 exit when not Exception_Junk (Stm);
7317 Check_Statement_Sequence
7318 (Statements (Handled_Statement_Sequence (Next (Stm))));
7323 exit when Nkind (Stm) /= N_Goto_Statement;
7324 exit when not Exception_Junk (Stm);
7328 -- Don't count pragmas
7330 while Nkind (Last_Stm) = N_Pragma
7332 -- Don't count call to SS_Release (can happen after Raise_Exception)
7335 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7337 Nkind (Name (Last_Stm)) = N_Identifier
7339 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7341 -- Don't count exception junk
7344 (Nkind_In (Last_Stm, N_Goto_Statement,
7346 N_Object_Declaration)
7347 and then Exception_Junk (Last_Stm))
7348 or else Nkind (Last_Stm) in N_Push_xxx_Label
7349 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7351 -- Inserted code, such as finalization calls, is irrelevant: we only
7352 -- need to check original source.
7354 or else Is_Rewrite_Insertion (Last_Stm)
7359 -- Here we have the "real" last statement
7361 Kind := Nkind (Last_Stm);
7363 -- Transfer of control, OK. Note that in the No_Return procedure
7364 -- case, we already diagnosed any explicit return statements, so
7365 -- we can treat them as OK in this context.
7367 if Is_Transfer (Last_Stm) then
7370 -- Check cases of explicit non-indirect procedure calls
7372 elsif Kind = N_Procedure_Call_Statement
7373 and then Is_Entity_Name (Name (Last_Stm))
7375 -- Check call to Raise_Exception procedure which is treated
7376 -- specially, as is a call to Reraise_Occurrence.
7378 -- We suppress the warning in these cases since it is likely that
7379 -- the programmer really does not expect to deal with the case
7380 -- of Null_Occurrence, and thus would find a warning about a
7381 -- missing return curious, and raising Program_Error does not
7382 -- seem such a bad behavior if this does occur.
7384 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7385 -- behavior will be to raise Constraint_Error (see AI-329).
7387 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7389 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7391 Raise_Exception_Call := True;
7393 -- For Raise_Exception call, test first argument, if it is
7394 -- an attribute reference for a 'Identity call, then we know
7395 -- that the call cannot possibly return.
7398 Arg : constant Node_Id :=
7399 Original_Node (First_Actual (Last_Stm));
7401 if Nkind (Arg) = N_Attribute_Reference
7402 and then Attribute_Name (Arg) = Name_Identity
7409 -- If statement, need to look inside if there is an else and check
7410 -- each constituent statement sequence for proper termination.
7412 elsif Kind = N_If_Statement
7413 and then Present (Else_Statements (Last_Stm))
7415 Check_Statement_Sequence (Then_Statements (Last_Stm));
7416 Check_Statement_Sequence (Else_Statements (Last_Stm));
7418 if Present (Elsif_Parts (Last_Stm)) then
7420 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7423 while Present (Elsif_Part) loop
7424 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7432 -- Case statement, check each case for proper termination
7434 elsif Kind = N_Case_Statement then
7438 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7439 while Present (Case_Alt) loop
7440 Check_Statement_Sequence (Statements (Case_Alt));
7441 Next_Non_Pragma (Case_Alt);
7447 -- Block statement, check its handled sequence of statements
7449 elsif Kind = N_Block_Statement then
7455 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7464 -- Loop statement. If there is an iteration scheme, we can definitely
7465 -- fall out of the loop. Similarly if there is an exit statement, we
7466 -- can fall out. In either case we need a following return.
7468 elsif Kind = N_Loop_Statement then
7469 if Present (Iteration_Scheme (Last_Stm))
7470 or else Has_Exit (Entity (Identifier (Last_Stm)))
7474 -- A loop with no exit statement or iteration scheme is either
7475 -- an infinite loop, or it has some other exit (raise/return).
7476 -- In either case, no warning is required.
7482 -- Timed entry call, check entry call and delay alternatives
7484 -- Note: in expanded code, the timed entry call has been converted
7485 -- to a set of expanded statements on which the check will work
7486 -- correctly in any case.
7488 elsif Kind = N_Timed_Entry_Call then
7490 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7491 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7494 -- If statement sequence of entry call alternative is missing,
7495 -- then we can definitely fall through, and we post the error
7496 -- message on the entry call alternative itself.
7498 if No (Statements (ECA)) then
7501 -- If statement sequence of delay alternative is missing, then
7502 -- we can definitely fall through, and we post the error
7503 -- message on the delay alternative itself.
7505 -- Note: if both ECA and DCA are missing the return, then we
7506 -- post only one message, should be enough to fix the bugs.
7507 -- If not we will get a message next time on the DCA when the
7510 elsif No (Statements (DCA)) then
7513 -- Else check both statement sequences
7516 Check_Statement_Sequence (Statements (ECA));
7517 Check_Statement_Sequence (Statements (DCA));
7522 -- Conditional entry call, check entry call and else part
7524 -- Note: in expanded code, the conditional entry call has been
7525 -- converted to a set of expanded statements on which the check
7526 -- will work correctly in any case.
7528 elsif Kind = N_Conditional_Entry_Call then
7530 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7533 -- If statement sequence of entry call alternative is missing,
7534 -- then we can definitely fall through, and we post the error
7535 -- message on the entry call alternative itself.
7537 if No (Statements (ECA)) then
7540 -- Else check statement sequence and else part
7543 Check_Statement_Sequence (Statements (ECA));
7544 Check_Statement_Sequence (Else_Statements (Last_Stm));
7550 -- If we fall through, issue appropriate message
7554 -- Kill warning if last statement is a raise exception call,
7555 -- or a pragma Assert (False). Note that with assertions enabled,
7556 -- such a pragma has been converted into a raise exception call
7557 -- already, so the Assert_False is for the assertions off case.
7559 if not Raise_Exception_Call and then not Assert_False then
7561 -- In GNATprove mode, it is an error to have a missing return
7563 Error_Msg_Warn := SPARK_Mode /= On;
7565 -- Issue error message or warning
7568 ("RETURN statement missing following this statement<<!",
7571 ("\Program_Error ]<<!", Last_Stm);
7574 -- Note: we set Err even though we have not issued a warning
7575 -- because we still have a case of a missing return. This is
7576 -- an extremely marginal case, probably will never be noticed
7577 -- but we might as well get it right.
7581 -- Otherwise we have the case of a procedure marked No_Return
7584 if not Raise_Exception_Call then
7585 if GNATprove_Mode then
7587 ("implied return after this statement "
7588 & "would have raised Program_Error", Last_Stm);
7591 ("implied return after this statement "
7592 & "will raise Program_Error??", Last_Stm);
7595 Error_Msg_Warn := SPARK_Mode /= On;
7597 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7601 RE : constant Node_Id :=
7602 Make_Raise_Program_Error (Sloc (Last_Stm),
7603 Reason => PE_Implicit_Return);
7605 Insert_After (Last_Stm, RE);
7609 end Check_Statement_Sequence;
7611 -- Start of processing for Check_Returns
7615 Check_Statement_Sequence (Statements (HSS));
7617 if Present (Exception_Handlers (HSS)) then
7618 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7619 while Present (Handler) loop
7620 Check_Statement_Sequence (Statements (Handler));
7621 Next_Non_Pragma (Handler);
7626 ----------------------------
7627 -- Check_Subprogram_Order --
7628 ----------------------------
7630 procedure Check_Subprogram_Order (N : Node_Id) is
7632 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7633 -- This is used to check if S1 > S2 in the sense required by this test,
7634 -- for example nameab < namec, but name2 < name10.
7636 -----------------------------
7637 -- Subprogram_Name_Greater --
7638 -----------------------------
7640 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7645 -- Deal with special case where names are identical except for a
7646 -- numerical suffix. These are handled specially, taking the numeric
7647 -- ordering from the suffix into account.
7650 while S1 (L1) in '0' .. '9' loop
7655 while S2 (L2) in '0' .. '9' loop
7659 -- If non-numeric parts non-equal, do straight compare
7661 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7664 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7665 -- that a missing suffix is treated as numeric zero in this test.
7669 while L1 < S1'Last loop
7671 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7675 while L2 < S2'Last loop
7677 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7682 end Subprogram_Name_Greater;
7684 -- Start of processing for Check_Subprogram_Order
7687 -- Check body in alpha order if this is option
7690 and then Style_Check_Order_Subprograms
7691 and then Nkind (N) = N_Subprogram_Body
7692 and then Comes_From_Source (N)
7693 and then In_Extended_Main_Source_Unit (N)
7697 renames Scope_Stack.Table
7698 (Scope_Stack.Last).Last_Subprogram_Name;
7700 Body_Id : constant Entity_Id :=
7701 Defining_Entity (Specification (N));
7704 Get_Decoded_Name_String (Chars (Body_Id));
7707 if Subprogram_Name_Greater
7708 (LSN.all, Name_Buffer (1 .. Name_Len))
7710 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7716 LSN := new String'(Name_Buffer (1 .. Name_Len));
7719 end Check_Subprogram_Order;
7721 ------------------------------
7722 -- Check_Subtype_Conformant --
7723 ------------------------------
7725 procedure Check_Subtype_Conformant
7726 (New_Id : Entity_Id;
7728 Err_Loc : Node_Id := Empty;
7729 Skip_Controlling_Formals : Boolean := False;
7730 Get_Inst : Boolean := False)
7733 pragma Warnings (Off, Result);
7736 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7737 Skip_Controlling_Formals => Skip_Controlling_Formals,
7738 Get_Inst => Get_Inst);
7739 end Check_Subtype_Conformant;
7741 ---------------------------
7742 -- Check_Type_Conformant --
7743 ---------------------------
7745 procedure Check_Type_Conformant
7746 (New_Id : Entity_Id;
7748 Err_Loc : Node_Id := Empty)
7751 pragma Warnings (Off, Result);
7754 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7755 end Check_Type_Conformant;
7757 ---------------------------
7758 -- Can_Override_Operator --
7759 ---------------------------
7761 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7765 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7769 Typ := Base_Type (Etype (First_Formal (Subp)));
7771 -- Check explicitly that the operation is a primitive of the type
7773 return Operator_Matches_Spec (Subp, Subp)
7774 and then not Is_Generic_Type (Typ)
7775 and then Scope (Subp) = Scope (Typ)
7776 and then not Is_Class_Wide_Type (Typ);
7778 end Can_Override_Operator;
7780 ----------------------
7781 -- Conforming_Types --
7782 ----------------------
7784 function Conforming_Types
7787 Ctype : Conformance_Type;
7788 Get_Inst : Boolean := False) return Boolean
7790 Type_1 : Entity_Id := T1;
7791 Type_2 : Entity_Id := T2;
7792 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7794 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7795 -- If neither T1 nor T2 are generic actual types, or if they are in
7796 -- different scopes (e.g. parent and child instances), then verify that
7797 -- the base types are equal. Otherwise T1 and T2 must be on the same
7798 -- subtype chain. The whole purpose of this procedure is to prevent
7799 -- spurious ambiguities in an instantiation that may arise if two
7800 -- distinct generic types are instantiated with the same actual.
7802 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7803 -- An access parameter can designate an incomplete type. If the
7804 -- incomplete type is the limited view of a type from a limited_
7805 -- with_clause, check whether the non-limited view is available. If
7806 -- it is a (non-limited) incomplete type, get the full view.
7808 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7809 -- Returns True if and only if either T1 denotes a limited view of T2
7810 -- or T2 denotes a limited view of T1. This can arise when the limited
7811 -- with view of a type is used in a subprogram declaration and the
7812 -- subprogram body is in the scope of a regular with clause for the
7813 -- same unit. In such a case, the two type entities can be considered
7814 -- identical for purposes of conformance checking.
7816 ----------------------
7817 -- Base_Types_Match --
7818 ----------------------
7820 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7821 BT1 : constant Entity_Id := Base_Type (T1);
7822 BT2 : constant Entity_Id := Base_Type (T2);
7828 elsif BT1 = BT2 then
7830 -- The following is too permissive. A more precise test should
7831 -- check that the generic actual is an ancestor subtype of the
7834 -- See code in Find_Corresponding_Spec that applies an additional
7835 -- filter to handle accidental amiguities in instances.
7837 return not Is_Generic_Actual_Type (T1)
7838 or else not Is_Generic_Actual_Type (T2)
7839 or else Scope (T1) /= Scope (T2);
7841 -- If T2 is a generic actual type it is declared as the subtype of
7842 -- the actual. If that actual is itself a subtype we need to use its
7843 -- own base type to check for compatibility.
7845 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7848 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7854 end Base_Types_Match;
7856 --------------------------
7857 -- Find_Designated_Type --
7858 --------------------------
7860 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7864 Desig := Directly_Designated_Type (T);
7866 if Ekind (Desig) = E_Incomplete_Type then
7868 -- If regular incomplete type, get full view if available
7870 if Present (Full_View (Desig)) then
7871 Desig := Full_View (Desig);
7873 -- If limited view of a type, get non-limited view if available,
7874 -- and check again for a regular incomplete type.
7876 elsif Present (Non_Limited_View (Desig)) then
7877 Desig := Get_Full_View (Non_Limited_View (Desig));
7882 end Find_Designated_Type;
7884 -------------------------------
7885 -- Matches_Limited_With_View --
7886 -------------------------------
7888 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7890 -- In some cases a type imported through a limited_with clause, and
7891 -- its nonlimited view are both visible, for example in an anonymous
7892 -- access-to-class-wide type in a formal. Both entities designate the
7895 if From_Limited_With (T1) and then T2 = Available_View (T1) then
7898 elsif From_Limited_With (T2) and then T1 = Available_View (T2) then
7901 elsif From_Limited_With (T1)
7902 and then From_Limited_With (T2)
7903 and then Available_View (T1) = Available_View (T2)
7910 end Matches_Limited_With_View;
7912 -- Start of processing for Conforming_Types
7915 -- The context is an instance association for a formal access-to-
7916 -- subprogram type; the formal parameter types require mapping because
7917 -- they may denote other formal parameters of the generic unit.
7920 Type_1 := Get_Instance_Of (T1);
7921 Type_2 := Get_Instance_Of (T2);
7924 -- If one of the types is a view of the other introduced by a limited
7925 -- with clause, treat these as conforming for all purposes.
7927 if Matches_Limited_With_View (T1, T2) then
7930 elsif Base_Types_Match (Type_1, Type_2) then
7931 return Ctype <= Mode_Conformant
7932 or else Subtypes_Statically_Match (Type_1, Type_2);
7934 elsif Is_Incomplete_Or_Private_Type (Type_1)
7935 and then Present (Full_View (Type_1))
7936 and then Base_Types_Match (Full_View (Type_1), Type_2)
7938 return Ctype <= Mode_Conformant
7939 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7941 elsif Ekind (Type_2) = E_Incomplete_Type
7942 and then Present (Full_View (Type_2))
7943 and then Base_Types_Match (Type_1, Full_View (Type_2))
7945 return Ctype <= Mode_Conformant
7946 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7948 elsif Is_Private_Type (Type_2)
7949 and then In_Instance
7950 and then Present (Full_View (Type_2))
7951 and then Base_Types_Match (Type_1, Full_View (Type_2))
7953 return Ctype <= Mode_Conformant
7954 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7957 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7958 -- treated recursively because they carry a signature. As far as
7959 -- conformance is concerned, convention plays no role, and either
7960 -- or both could be access to protected subprograms.
7962 Are_Anonymous_Access_To_Subprogram_Types :=
7963 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7964 E_Anonymous_Access_Protected_Subprogram_Type)
7966 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
7967 E_Anonymous_Access_Protected_Subprogram_Type);
7969 -- Test anonymous access type case. For this case, static subtype
7970 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7971 -- the base types because we may have built internal subtype entities
7972 -- to handle null-excluding types (see Process_Formals).
7974 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7976 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7978 -- Ada 2005 (AI-254)
7980 or else Are_Anonymous_Access_To_Subprogram_Types
7983 Desig_1 : Entity_Id;
7984 Desig_2 : Entity_Id;
7987 -- In Ada 2005, access constant indicators must match for
7988 -- subtype conformance.
7990 if Ada_Version >= Ada_2005
7991 and then Ctype >= Subtype_Conformant
7993 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7998 Desig_1 := Find_Designated_Type (Type_1);
7999 Desig_2 := Find_Designated_Type (Type_2);
8001 -- If the context is an instance association for a formal
8002 -- access-to-subprogram type; formal access parameter designated
8003 -- types require mapping because they may denote other formal
8004 -- parameters of the generic unit.
8007 Desig_1 := Get_Instance_Of (Desig_1);
8008 Desig_2 := Get_Instance_Of (Desig_2);
8011 -- It is possible for a Class_Wide_Type to be introduced for an
8012 -- incomplete type, in which case there is a separate class_ wide
8013 -- type for the full view. The types conform if their Etypes
8014 -- conform, i.e. one may be the full view of the other. This can
8015 -- only happen in the context of an access parameter, other uses
8016 -- of an incomplete Class_Wide_Type are illegal.
8018 if Is_Class_Wide_Type (Desig_1)
8020 Is_Class_Wide_Type (Desig_2)
8024 (Etype (Base_Type (Desig_1)),
8025 Etype (Base_Type (Desig_2)), Ctype);
8027 elsif Are_Anonymous_Access_To_Subprogram_Types then
8028 if Ada_Version < Ada_2005 then
8029 return Ctype = Type_Conformant
8031 Subtypes_Statically_Match (Desig_1, Desig_2);
8033 -- We must check the conformance of the signatures themselves
8037 Conformant : Boolean;
8040 (Desig_1, Desig_2, Ctype, False, Conformant);
8046 return Base_Type (Desig_1) = Base_Type (Desig_2)
8047 and then (Ctype = Type_Conformant
8049 Subtypes_Statically_Match (Desig_1, Desig_2));
8053 -- Otherwise definitely no match
8056 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8057 and then Is_Access_Type (Type_2))
8058 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8059 and then Is_Access_Type (Type_1)))
8062 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8064 May_Hide_Profile := True;
8069 end Conforming_Types;
8071 --------------------------
8072 -- Create_Extra_Formals --
8073 --------------------------
8075 procedure Create_Extra_Formals (E : Entity_Id) is
8077 First_Extra : Entity_Id := Empty;
8078 Last_Extra : Entity_Id;
8079 Formal_Type : Entity_Id;
8080 P_Formal : Entity_Id := Empty;
8082 function Add_Extra_Formal
8083 (Assoc_Entity : Entity_Id;
8086 Suffix : String) return Entity_Id;
8087 -- Add an extra formal to the current list of formals and extra formals.
8088 -- The extra formal is added to the end of the list of extra formals,
8089 -- and also returned as the result. These formals are always of mode IN.
8090 -- The new formal has the type Typ, is declared in Scope, and its name
8091 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8092 -- The following suffixes are currently used. They should not be changed
8093 -- without coordinating with CodePeer, which makes use of these to
8094 -- provide better messages.
8096 -- O denotes the Constrained bit.
8097 -- L denotes the accessibility level.
8098 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8099 -- the full list in exp_ch6.BIP_Formal_Kind.
8101 ----------------------
8102 -- Add_Extra_Formal --
8103 ----------------------
8105 function Add_Extra_Formal
8106 (Assoc_Entity : Entity_Id;
8109 Suffix : String) return Entity_Id
8111 EF : constant Entity_Id :=
8112 Make_Defining_Identifier (Sloc (Assoc_Entity),
8113 Chars => New_External_Name (Chars (Assoc_Entity),
8117 -- A little optimization. Never generate an extra formal for the
8118 -- _init operand of an initialization procedure, since it could
8121 if Chars (Formal) = Name_uInit then
8125 Set_Ekind (EF, E_In_Parameter);
8126 Set_Actual_Subtype (EF, Typ);
8127 Set_Etype (EF, Typ);
8128 Set_Scope (EF, Scope);
8129 Set_Mechanism (EF, Default_Mechanism);
8130 Set_Formal_Validity (EF);
8132 if No (First_Extra) then
8134 Set_Extra_Formals (Scope, First_Extra);
8137 if Present (Last_Extra) then
8138 Set_Extra_Formal (Last_Extra, EF);
8144 end Add_Extra_Formal;
8146 -- Start of processing for Create_Extra_Formals
8149 -- We never generate extra formals if expansion is not active because we
8150 -- don't need them unless we are generating code.
8152 if not Expander_Active then
8156 -- No need to generate extra formals in interface thunks whose target
8157 -- primitive has no extra formals.
8159 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8163 -- If this is a derived subprogram then the subtypes of the parent
8164 -- subprogram's formal parameters will be used to determine the need
8165 -- for extra formals.
8167 if Is_Overloadable (E) and then Present (Alias (E)) then
8168 P_Formal := First_Formal (Alias (E));
8171 Last_Extra := Empty;
8172 Formal := First_Formal (E);
8173 while Present (Formal) loop
8174 Last_Extra := Formal;
8175 Next_Formal (Formal);
8178 -- If Extra_formals were already created, don't do it again. This
8179 -- situation may arise for subprogram types created as part of
8180 -- dispatching calls (see Expand_Dispatching_Call)
8182 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8186 -- If the subprogram is a predefined dispatching subprogram then don't
8187 -- generate any extra constrained or accessibility level formals. In
8188 -- general we suppress these for internal subprograms (by not calling
8189 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8190 -- generated stream attributes do get passed through because extra
8191 -- build-in-place formals are needed in some cases (limited 'Input).
8193 if Is_Predefined_Internal_Operation (E) then
8194 goto Test_For_Func_Result_Extras;
8197 Formal := First_Formal (E);
8198 while Present (Formal) loop
8200 -- Create extra formal for supporting the attribute 'Constrained.
8201 -- The case of a private type view without discriminants also
8202 -- requires the extra formal if the underlying type has defaulted
8205 if Ekind (Formal) /= E_In_Parameter then
8206 if Present (P_Formal) then
8207 Formal_Type := Etype (P_Formal);
8209 Formal_Type := Etype (Formal);
8212 -- Do not produce extra formals for Unchecked_Union parameters.
8213 -- Jump directly to the end of the loop.
8215 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8216 goto Skip_Extra_Formal_Generation;
8219 if not Has_Discriminants (Formal_Type)
8220 and then Ekind (Formal_Type) in Private_Kind
8221 and then Present (Underlying_Type (Formal_Type))
8223 Formal_Type := Underlying_Type (Formal_Type);
8226 -- Suppress the extra formal if formal's subtype is constrained or
8227 -- indefinite, or we're compiling for Ada 2012 and the underlying
8228 -- type is tagged and limited. In Ada 2012, a limited tagged type
8229 -- can have defaulted discriminants, but 'Constrained is required
8230 -- to return True, so the formal is never needed (see AI05-0214).
8231 -- Note that this ensures consistency of calling sequences for
8232 -- dispatching operations when some types in a class have defaults
8233 -- on discriminants and others do not (and requiring the extra
8234 -- formal would introduce distributed overhead).
8236 -- If the type does not have a completion yet, treat as prior to
8237 -- Ada 2012 for consistency.
8239 if Has_Discriminants (Formal_Type)
8240 and then not Is_Constrained (Formal_Type)
8241 and then not Is_Indefinite_Subtype (Formal_Type)
8242 and then (Ada_Version < Ada_2012
8243 or else No (Underlying_Type (Formal_Type))
8245 (Is_Limited_Type (Formal_Type)
8248 (Underlying_Type (Formal_Type)))))
8250 Set_Extra_Constrained
8251 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8255 -- Create extra formal for supporting accessibility checking. This
8256 -- is done for both anonymous access formals and formals of named
8257 -- access types that are marked as controlling formals. The latter
8258 -- case can occur when Expand_Dispatching_Call creates a subprogram
8259 -- type and substitutes the types of access-to-class-wide actuals
8260 -- for the anonymous access-to-specific-type of controlling formals.
8261 -- Base_Type is applied because in cases where there is a null
8262 -- exclusion the formal may have an access subtype.
8264 -- This is suppressed if we specifically suppress accessibility
8265 -- checks at the package level for either the subprogram, or the
8266 -- package in which it resides. However, we do not suppress it
8267 -- simply if the scope has accessibility checks suppressed, since
8268 -- this could cause trouble when clients are compiled with a
8269 -- different suppression setting. The explicit checks at the
8270 -- package level are safe from this point of view.
8272 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8273 or else (Is_Controlling_Formal (Formal)
8274 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8276 (Explicit_Suppress (E, Accessibility_Check)
8278 Explicit_Suppress (Scope (E), Accessibility_Check))
8281 or else Present (Extra_Accessibility (P_Formal)))
8283 Set_Extra_Accessibility
8284 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8287 -- This label is required when skipping extra formal generation for
8288 -- Unchecked_Union parameters.
8290 <<Skip_Extra_Formal_Generation>>
8292 if Present (P_Formal) then
8293 Next_Formal (P_Formal);
8296 Next_Formal (Formal);
8299 <<Test_For_Func_Result_Extras>>
8301 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8302 -- function call is ... determined by the point of call ...".
8304 if Needs_Result_Accessibility_Level (E) then
8305 Set_Extra_Accessibility_Of_Result
8306 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8309 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8310 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8312 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8314 Result_Subt : constant Entity_Id := Etype (E);
8315 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8316 Formal_Typ : Entity_Id;
8318 Discard : Entity_Id;
8319 pragma Warnings (Off, Discard);
8322 -- In the case of functions with unconstrained result subtypes,
8323 -- add a 4-state formal indicating whether the return object is
8324 -- allocated by the caller (1), or should be allocated by the
8325 -- callee on the secondary stack (2), in the global heap (3), or
8326 -- in a user-defined storage pool (4). For the moment we just use
8327 -- Natural for the type of this formal. Note that this formal
8328 -- isn't usually needed in the case where the result subtype is
8329 -- constrained, but it is needed when the function has a tagged
8330 -- result, because generally such functions can be called in a
8331 -- dispatching context and such calls must be handled like calls
8332 -- to a class-wide function.
8334 if Needs_BIP_Alloc_Form (E) then
8337 (E, Standard_Natural,
8338 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8340 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8341 -- use a user-defined pool. This formal is not added on
8342 -- .NET/JVM/ZFP as those targets do not support pools.
8344 if VM_Target = No_VM
8345 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8349 (E, RTE (RE_Root_Storage_Pool_Ptr),
8350 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8354 -- In the case of functions whose result type needs finalization,
8355 -- add an extra formal which represents the finalization master.
8357 if Needs_BIP_Finalization_Master (E) then
8360 (E, RTE (RE_Finalization_Master_Ptr),
8361 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8364 -- When the result type contains tasks, add two extra formals: the
8365 -- master of the tasks to be created, and the caller's activation
8368 if Has_Task (Full_Subt) then
8371 (E, RTE (RE_Master_Id),
8372 E, BIP_Formal_Suffix (BIP_Task_Master));
8375 (E, RTE (RE_Activation_Chain_Access),
8376 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8379 -- All build-in-place functions get an extra formal that will be
8380 -- passed the address of the return object within the caller.
8383 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8385 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8386 Set_Etype (Formal_Typ, Formal_Typ);
8387 Set_Depends_On_Private
8388 (Formal_Typ, Has_Private_Component (Formal_Typ));
8389 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8390 Set_Is_Access_Constant (Formal_Typ, False);
8392 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8393 -- the designated type comes from the limited view (for back-end
8396 Set_From_Limited_With
8397 (Formal_Typ, From_Limited_With (Result_Subt));
8399 Layout_Type (Formal_Typ);
8403 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8406 end Create_Extra_Formals;
8408 -----------------------------
8409 -- Enter_Overloaded_Entity --
8410 -----------------------------
8412 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8413 E : Entity_Id := Current_Entity_In_Scope (S);
8414 C_E : Entity_Id := Current_Entity (S);
8418 Set_Has_Homonym (E);
8419 Set_Has_Homonym (S);
8422 Set_Is_Immediately_Visible (S);
8423 Set_Scope (S, Current_Scope);
8425 -- Chain new entity if front of homonym in current scope, so that
8426 -- homonyms are contiguous.
8428 if Present (E) and then E /= C_E then
8429 while Homonym (C_E) /= E loop
8430 C_E := Homonym (C_E);
8433 Set_Homonym (C_E, S);
8437 Set_Current_Entity (S);
8442 if Is_Inherited_Operation (S) then
8443 Append_Inherited_Subprogram (S);
8445 Append_Entity (S, Current_Scope);
8448 Set_Public_Status (S);
8450 if Debug_Flag_E then
8451 Write_Str ("New overloaded entity chain: ");
8452 Write_Name (Chars (S));
8455 while Present (E) loop
8456 Write_Str (" "); Write_Int (Int (E));
8463 -- Generate warning for hiding
8466 and then Comes_From_Source (S)
8467 and then In_Extended_Main_Source_Unit (S)
8474 -- Warn unless genuine overloading. Do not emit warning on
8475 -- hiding predefined operators in Standard (these are either an
8476 -- (artifact of our implicit declarations, or simple noise) but
8477 -- keep warning on a operator defined on a local subtype, because
8478 -- of the real danger that different operators may be applied in
8479 -- various parts of the program.
8481 -- Note that if E and S have the same scope, there is never any
8482 -- hiding. Either the two conflict, and the program is illegal,
8483 -- or S is overriding an implicit inherited subprogram.
8485 if Scope (E) /= Scope (S)
8486 and then (not Is_Overloadable (E)
8487 or else Subtype_Conformant (E, S))
8488 and then (Is_Immediately_Visible (E)
8490 Is_Potentially_Use_Visible (S))
8492 if Scope (E) /= Standard_Standard then
8493 Error_Msg_Sloc := Sloc (E);
8494 Error_Msg_N ("declaration of & hides one#?h?", S);
8496 elsif Nkind (S) = N_Defining_Operator_Symbol
8498 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8501 ("declaration of & hides predefined operator?h?", S);
8506 end Enter_Overloaded_Entity;
8508 -----------------------------
8509 -- Check_Untagged_Equality --
8510 -----------------------------
8512 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8513 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8514 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8518 -- This check applies only if we have a subprogram declaration with a
8519 -- non-tagged record type.
8521 if Nkind (Decl) /= N_Subprogram_Declaration
8522 or else not Is_Record_Type (Typ)
8523 or else Is_Tagged_Type (Typ)
8528 -- In Ada 2012 case, we will output errors or warnings depending on
8529 -- the setting of debug flag -gnatd.E.
8531 if Ada_Version >= Ada_2012 then
8532 Error_Msg_Warn := Debug_Flag_Dot_EE;
8534 -- In earlier versions of Ada, nothing to do unless we are warning on
8535 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8538 if not Warn_On_Ada_2012_Compatibility then
8543 -- Cases where the type has already been frozen
8545 if Is_Frozen (Typ) then
8547 -- If the type is not declared in a package, or if we are in the body
8548 -- of the package or in some other scope, the new operation is not
8549 -- primitive, and therefore legal, though suspicious. Should we
8550 -- generate a warning in this case ???
8552 if Ekind (Scope (Typ)) /= E_Package
8553 or else Scope (Typ) /= Current_Scope
8557 -- If the type is a generic actual (sub)type, the operation is not
8558 -- primitive either because the base type is declared elsewhere.
8560 elsif Is_Generic_Actual_Type (Typ) then
8563 -- Here we have a definite error of declaration after freezing
8566 if Ada_Version >= Ada_2012 then
8568 ("equality operator must be declared before type& is "
8569 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8571 -- In Ada 2012 mode with error turned to warning, output one
8572 -- more warning to warn that the equality operation may not
8573 -- compose. This is the consequence of ignoring the error.
8575 if Error_Msg_Warn then
8576 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8581 ("equality operator must be declared before type& is "
8582 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8585 -- If we are in the package body, we could just move the
8586 -- declaration to the package spec, so add a message saying that.
8588 if In_Package_Body (Scope (Typ)) then
8589 if Ada_Version >= Ada_2012 then
8591 ("\move declaration to package spec<<", Eq_Op);
8594 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8597 -- Otherwise try to find the freezing point
8600 Obj_Decl := Next (Parent (Typ));
8601 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8602 if Nkind (Obj_Decl) = N_Object_Declaration
8603 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8605 -- Freezing point, output warnings
8607 if Ada_Version >= Ada_2012 then
8609 ("type& is frozen by declaration??", Obj_Decl, Typ);
8611 ("\an equality operator cannot be declared after "
8616 ("type& is frozen by declaration (Ada 2012)?y?",
8619 ("\an equality operator cannot be declared after "
8620 & "this point (Ada 2012)?y?",
8632 -- Here if type is not frozen yet. It is illegal to have a primitive
8633 -- equality declared in the private part if the type is visible.
8635 elsif not In_Same_List (Parent (Typ), Decl)
8636 and then not Is_Limited_Type (Typ)
8638 -- Shouldn't we give an RM reference here???
8640 if Ada_Version >= Ada_2012 then
8642 ("equality operator appears too late<<", Eq_Op);
8645 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
8648 -- No error detected
8653 end Check_Untagged_Equality;
8655 -----------------------------
8656 -- Find_Corresponding_Spec --
8657 -----------------------------
8659 function Find_Corresponding_Spec
8661 Post_Error : Boolean := True) return Entity_Id
8663 Spec : constant Node_Id := Specification (N);
8664 Designator : constant Entity_Id := Defining_Entity (Spec);
8668 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8669 -- Even if fully conformant, a body may depend on a generic actual when
8670 -- the spec does not, or vice versa, in which case they were distinct
8671 -- entities in the generic.
8673 -------------------------------
8674 -- Different_Generic_Profile --
8675 -------------------------------
8677 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8680 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
8681 -- Check that the types of corresponding formals have the same
8682 -- generic actual if any. We have to account for subtypes of a
8683 -- generic formal, declared between a spec and a body, which may
8684 -- appear distinct in an instance but matched in the generic.
8686 -------------------------
8687 -- Same_Generic_Actual --
8688 -------------------------
8690 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
8692 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
8694 (Present (Parent (T1))
8695 and then Comes_From_Source (Parent (T1))
8696 and then Nkind (Parent (T1)) = N_Subtype_Declaration
8697 and then Is_Entity_Name (Subtype_Indication (Parent (T1)))
8698 and then Entity (Subtype_Indication (Parent (T1))) = T2);
8699 end Same_Generic_Actual;
8701 -- Start of processing for Different_Generic_Profile
8704 if not In_Instance then
8707 elsif Ekind (E) = E_Function
8708 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
8713 F1 := First_Formal (Designator);
8714 F2 := First_Formal (E);
8715 while Present (F1) loop
8716 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
8725 end Different_Generic_Profile;
8727 -- Start of processing for Find_Corresponding_Spec
8730 E := Current_Entity (Designator);
8731 while Present (E) loop
8733 -- We are looking for a matching spec. It must have the same scope,
8734 -- and the same name, and either be type conformant, or be the case
8735 -- of a library procedure spec and its body (which belong to one
8736 -- another regardless of whether they are type conformant or not).
8738 if Scope (E) = Current_Scope then
8739 if Current_Scope = Standard_Standard
8740 or else (Ekind (E) = Ekind (Designator)
8741 and then Type_Conformant (E, Designator))
8743 -- Within an instantiation, we know that spec and body are
8744 -- subtype conformant, because they were subtype conformant in
8745 -- the generic. We choose the subtype-conformant entity here as
8746 -- well, to resolve spurious ambiguities in the instance that
8747 -- were not present in the generic (i.e. when two different
8748 -- types are given the same actual). If we are looking for a
8749 -- spec to match a body, full conformance is expected.
8752 Set_Convention (Designator, Convention (E));
8754 -- Skip past subprogram bodies and subprogram renamings that
8755 -- may appear to have a matching spec, but that aren't fully
8756 -- conformant with it. That can occur in cases where an
8757 -- actual type causes unrelated homographs in the instance.
8759 if Nkind_In (N, N_Subprogram_Body,
8760 N_Subprogram_Renaming_Declaration)
8761 and then Present (Homonym (E))
8762 and then not Fully_Conformant (Designator, E)
8766 elsif not Subtype_Conformant (Designator, E) then
8769 elsif Different_Generic_Profile (E) then
8774 -- Ada 2012 (AI05-0165): For internally generated bodies of
8775 -- null procedures locate the internally generated spec. We
8776 -- enforce mode conformance since a tagged type may inherit
8777 -- from interfaces several null primitives which differ only
8778 -- in the mode of the formals.
8780 if not (Comes_From_Source (E))
8781 and then Is_Null_Procedure (E)
8782 and then not Mode_Conformant (Designator, E)
8786 -- For null procedures coming from source that are completions,
8787 -- analysis of the generated body will establish the link.
8789 elsif Comes_From_Source (E)
8790 and then Nkind (Spec) = N_Procedure_Specification
8791 and then Null_Present (Spec)
8795 elsif not Has_Completion (E) then
8796 if Nkind (N) /= N_Subprogram_Body_Stub then
8797 Set_Corresponding_Spec (N, E);
8800 Set_Has_Completion (E);
8803 elsif Nkind (Parent (N)) = N_Subunit then
8805 -- If this is the proper body of a subunit, the completion
8806 -- flag is set when analyzing the stub.
8810 -- If E is an internal function with a controlling result that
8811 -- was created for an operation inherited by a null extension,
8812 -- it may be overridden by a body without a previous spec (one
8813 -- more reason why these should be shunned). In that case we
8814 -- remove the generated body if present, because the current
8815 -- one is the explicit overriding.
8817 elsif Ekind (E) = E_Function
8818 and then Ada_Version >= Ada_2005
8819 and then not Comes_From_Source (E)
8820 and then Has_Controlling_Result (E)
8821 and then Is_Null_Extension (Etype (E))
8822 and then Comes_From_Source (Spec)
8824 Set_Has_Completion (E, False);
8827 and then Nkind (Parent (E)) = N_Function_Specification
8830 (Unit_Declaration_Node
8831 (Corresponding_Body (Unit_Declaration_Node (E))));
8835 -- If expansion is disabled, or if the wrapper function has
8836 -- not been generated yet, this a late body overriding an
8837 -- inherited operation, or it is an overriding by some other
8838 -- declaration before the controlling result is frozen. In
8839 -- either case this is a declaration of a new entity.
8845 -- If the body already exists, then this is an error unless
8846 -- the previous declaration is the implicit declaration of a
8847 -- derived subprogram. It is also legal for an instance to
8848 -- contain type conformant overloadable declarations (but the
8849 -- generic declaration may not), per 8.3(26/2).
8851 elsif No (Alias (E))
8852 and then not Is_Intrinsic_Subprogram (E)
8853 and then not In_Instance
8856 Error_Msg_Sloc := Sloc (E);
8858 if Is_Imported (E) then
8860 ("body not allowed for imported subprogram & declared#",
8863 Error_Msg_NE ("duplicate body for & declared#", N, E);
8867 -- Child units cannot be overloaded, so a conformance mismatch
8868 -- between body and a previous spec is an error.
8870 elsif Is_Child_Unit (E)
8872 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8874 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8879 ("body of child unit does not match previous declaration", N);
8887 -- On exit, we know that no previous declaration of subprogram exists
8890 end Find_Corresponding_Spec;
8892 ----------------------
8893 -- Fully_Conformant --
8894 ----------------------
8896 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8899 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8901 end Fully_Conformant;
8903 ----------------------------------
8904 -- Fully_Conformant_Expressions --
8905 ----------------------------------
8907 function Fully_Conformant_Expressions
8908 (Given_E1 : Node_Id;
8909 Given_E2 : Node_Id) return Boolean
8911 E1 : constant Node_Id := Original_Node (Given_E1);
8912 E2 : constant Node_Id := Original_Node (Given_E2);
8913 -- We always test conformance on original nodes, since it is possible
8914 -- for analysis and/or expansion to make things look as though they
8915 -- conform when they do not, e.g. by converting 1+2 into 3.
8917 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8918 renames Fully_Conformant_Expressions;
8920 function FCL (L1, L2 : List_Id) return Boolean;
8921 -- Compare elements of two lists for conformance. Elements have to be
8922 -- conformant, and actuals inserted as default parameters do not match
8923 -- explicit actuals with the same value.
8925 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8926 -- Compare an operator node with a function call
8932 function FCL (L1, L2 : List_Id) return Boolean is
8936 if L1 = No_List then
8942 if L2 = No_List then
8948 -- Compare two lists, skipping rewrite insertions (we want to compare
8949 -- the original trees, not the expanded versions).
8952 if Is_Rewrite_Insertion (N1) then
8954 elsif Is_Rewrite_Insertion (N2) then
8960 elsif not FCE (N1, N2) then
8973 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8974 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8979 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8984 Act := First (Actuals);
8986 if Nkind (Op_Node) in N_Binary_Op then
8987 if not FCE (Left_Opnd (Op_Node), Act) then
8994 return Present (Act)
8995 and then FCE (Right_Opnd (Op_Node), Act)
8996 and then No (Next (Act));
9000 -- Start of processing for Fully_Conformant_Expressions
9003 -- Non-conformant if paren count does not match. Note: if some idiot
9004 -- complains that we don't do this right for more than 3 levels of
9005 -- parentheses, they will be treated with the respect they deserve.
9007 if Paren_Count (E1) /= Paren_Count (E2) then
9010 -- If same entities are referenced, then they are conformant even if
9011 -- they have different forms (RM 8.3.1(19-20)).
9013 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9014 if Present (Entity (E1)) then
9015 return Entity (E1) = Entity (E2)
9016 or else (Chars (Entity (E1)) = Chars (Entity (E2))
9017 and then Ekind (Entity (E1)) = E_Discriminant
9018 and then Ekind (Entity (E2)) = E_In_Parameter);
9020 elsif Nkind (E1) = N_Expanded_Name
9021 and then Nkind (E2) = N_Expanded_Name
9022 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9023 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9025 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9028 -- Identifiers in component associations don't always have
9029 -- entities, but their names must conform.
9031 return Nkind (E1) = N_Identifier
9032 and then Nkind (E2) = N_Identifier
9033 and then Chars (E1) = Chars (E2);
9036 elsif Nkind (E1) = N_Character_Literal
9037 and then Nkind (E2) = N_Expanded_Name
9039 return Nkind (Selector_Name (E2)) = N_Character_Literal
9040 and then Chars (E1) = Chars (Selector_Name (E2));
9042 elsif Nkind (E2) = N_Character_Literal
9043 and then Nkind (E1) = N_Expanded_Name
9045 return Nkind (Selector_Name (E1)) = N_Character_Literal
9046 and then Chars (E2) = Chars (Selector_Name (E1));
9048 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9049 return FCO (E1, E2);
9051 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9052 return FCO (E2, E1);
9054 -- Otherwise we must have the same syntactic entity
9056 elsif Nkind (E1) /= Nkind (E2) then
9059 -- At this point, we specialize by node type
9066 FCL (Expressions (E1), Expressions (E2))
9068 FCL (Component_Associations (E1),
9069 Component_Associations (E2));
9072 if Nkind (Expression (E1)) = N_Qualified_Expression
9074 Nkind (Expression (E2)) = N_Qualified_Expression
9076 return FCE (Expression (E1), Expression (E2));
9078 -- Check that the subtype marks and any constraints
9083 Indic1 : constant Node_Id := Expression (E1);
9084 Indic2 : constant Node_Id := Expression (E2);
9089 if Nkind (Indic1) /= N_Subtype_Indication then
9091 Nkind (Indic2) /= N_Subtype_Indication
9092 and then Entity (Indic1) = Entity (Indic2);
9094 elsif Nkind (Indic2) /= N_Subtype_Indication then
9096 Nkind (Indic1) /= N_Subtype_Indication
9097 and then Entity (Indic1) = Entity (Indic2);
9100 if Entity (Subtype_Mark (Indic1)) /=
9101 Entity (Subtype_Mark (Indic2))
9106 Elt1 := First (Constraints (Constraint (Indic1)));
9107 Elt2 := First (Constraints (Constraint (Indic2)));
9108 while Present (Elt1) and then Present (Elt2) loop
9109 if not FCE (Elt1, Elt2) then
9122 when N_Attribute_Reference =>
9124 Attribute_Name (E1) = Attribute_Name (E2)
9125 and then FCL (Expressions (E1), Expressions (E2));
9129 Entity (E1) = Entity (E2)
9130 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9131 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9133 when N_Short_Circuit | N_Membership_Test =>
9135 FCE (Left_Opnd (E1), Left_Opnd (E2))
9137 FCE (Right_Opnd (E1), Right_Opnd (E2));
9139 when N_Case_Expression =>
9145 if not FCE (Expression (E1), Expression (E2)) then
9149 Alt1 := First (Alternatives (E1));
9150 Alt2 := First (Alternatives (E2));
9152 if Present (Alt1) /= Present (Alt2) then
9154 elsif No (Alt1) then
9158 if not FCE (Expression (Alt1), Expression (Alt2))
9159 or else not FCL (Discrete_Choices (Alt1),
9160 Discrete_Choices (Alt2))
9171 when N_Character_Literal =>
9173 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9175 when N_Component_Association =>
9177 FCL (Choices (E1), Choices (E2))
9179 FCE (Expression (E1), Expression (E2));
9181 when N_Explicit_Dereference =>
9183 FCE (Prefix (E1), Prefix (E2));
9185 when N_Extension_Aggregate =>
9187 FCL (Expressions (E1), Expressions (E2))
9188 and then Null_Record_Present (E1) =
9189 Null_Record_Present (E2)
9190 and then FCL (Component_Associations (E1),
9191 Component_Associations (E2));
9193 when N_Function_Call =>
9195 FCE (Name (E1), Name (E2))
9197 FCL (Parameter_Associations (E1),
9198 Parameter_Associations (E2));
9200 when N_If_Expression =>
9202 FCL (Expressions (E1), Expressions (E2));
9204 when N_Indexed_Component =>
9206 FCE (Prefix (E1), Prefix (E2))
9208 FCL (Expressions (E1), Expressions (E2));
9210 when N_Integer_Literal =>
9211 return (Intval (E1) = Intval (E2));
9216 when N_Operator_Symbol =>
9218 Chars (E1) = Chars (E2);
9220 when N_Others_Choice =>
9223 when N_Parameter_Association =>
9225 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9226 and then FCE (Explicit_Actual_Parameter (E1),
9227 Explicit_Actual_Parameter (E2));
9229 when N_Qualified_Expression =>
9231 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9233 FCE (Expression (E1), Expression (E2));
9235 when N_Quantified_Expression =>
9236 if not FCE (Condition (E1), Condition (E2)) then
9240 if Present (Loop_Parameter_Specification (E1))
9241 and then Present (Loop_Parameter_Specification (E2))
9244 L1 : constant Node_Id :=
9245 Loop_Parameter_Specification (E1);
9246 L2 : constant Node_Id :=
9247 Loop_Parameter_Specification (E2);
9251 Reverse_Present (L1) = Reverse_Present (L2)
9253 FCE (Defining_Identifier (L1),
9254 Defining_Identifier (L2))
9256 FCE (Discrete_Subtype_Definition (L1),
9257 Discrete_Subtype_Definition (L2));
9260 elsif Present (Iterator_Specification (E1))
9261 and then Present (Iterator_Specification (E2))
9264 I1 : constant Node_Id := Iterator_Specification (E1);
9265 I2 : constant Node_Id := Iterator_Specification (E2);
9269 FCE (Defining_Identifier (I1),
9270 Defining_Identifier (I2))
9272 Of_Present (I1) = Of_Present (I2)
9274 Reverse_Present (I1) = Reverse_Present (I2)
9275 and then FCE (Name (I1), Name (I2))
9276 and then FCE (Subtype_Indication (I1),
9277 Subtype_Indication (I2));
9280 -- The quantified expressions used different specifications to
9281 -- walk their respective ranges.
9289 FCE (Low_Bound (E1), Low_Bound (E2))
9291 FCE (High_Bound (E1), High_Bound (E2));
9293 when N_Real_Literal =>
9294 return (Realval (E1) = Realval (E2));
9296 when N_Selected_Component =>
9298 FCE (Prefix (E1), Prefix (E2))
9300 FCE (Selector_Name (E1), Selector_Name (E2));
9304 FCE (Prefix (E1), Prefix (E2))
9306 FCE (Discrete_Range (E1), Discrete_Range (E2));
9308 when N_String_Literal =>
9310 S1 : constant String_Id := Strval (E1);
9311 S2 : constant String_Id := Strval (E2);
9312 L1 : constant Nat := String_Length (S1);
9313 L2 : constant Nat := String_Length (S2);
9320 for J in 1 .. L1 loop
9321 if Get_String_Char (S1, J) /=
9322 Get_String_Char (S2, J)
9332 when N_Type_Conversion =>
9334 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9336 FCE (Expression (E1), Expression (E2));
9340 Entity (E1) = Entity (E2)
9342 FCE (Right_Opnd (E1), Right_Opnd (E2));
9344 when N_Unchecked_Type_Conversion =>
9346 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9348 FCE (Expression (E1), Expression (E2));
9350 -- All other node types cannot appear in this context. Strictly
9351 -- we should raise a fatal internal error. Instead we just ignore
9352 -- the nodes. This means that if anyone makes a mistake in the
9353 -- expander and mucks an expression tree irretrievably, the result
9354 -- will be a failure to detect a (probably very obscure) case
9355 -- of non-conformance, which is better than bombing on some
9356 -- case where two expressions do in fact conform.
9363 end Fully_Conformant_Expressions;
9365 ----------------------------------------
9366 -- Fully_Conformant_Discrete_Subtypes --
9367 ----------------------------------------
9369 function Fully_Conformant_Discrete_Subtypes
9370 (Given_S1 : Node_Id;
9371 Given_S2 : Node_Id) return Boolean
9373 S1 : constant Node_Id := Original_Node (Given_S1);
9374 S2 : constant Node_Id := Original_Node (Given_S2);
9376 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9377 -- Special-case for a bound given by a discriminant, which in the body
9378 -- is replaced with the discriminal of the enclosing type.
9380 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9381 -- Check both bounds
9383 -----------------------
9384 -- Conforming_Bounds --
9385 -----------------------
9387 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9389 if Is_Entity_Name (B1)
9390 and then Is_Entity_Name (B2)
9391 and then Ekind (Entity (B1)) = E_Discriminant
9393 return Chars (B1) = Chars (B2);
9396 return Fully_Conformant_Expressions (B1, B2);
9398 end Conforming_Bounds;
9400 -----------------------
9401 -- Conforming_Ranges --
9402 -----------------------
9404 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9407 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9409 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9410 end Conforming_Ranges;
9412 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9415 if Nkind (S1) /= Nkind (S2) then
9418 elsif Is_Entity_Name (S1) then
9419 return Entity (S1) = Entity (S2);
9421 elsif Nkind (S1) = N_Range then
9422 return Conforming_Ranges (S1, S2);
9424 elsif Nkind (S1) = N_Subtype_Indication then
9426 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9429 (Range_Expression (Constraint (S1)),
9430 Range_Expression (Constraint (S2)));
9434 end Fully_Conformant_Discrete_Subtypes;
9436 --------------------
9437 -- Install_Entity --
9438 --------------------
9440 procedure Install_Entity (E : Entity_Id) is
9441 Prev : constant Entity_Id := Current_Entity (E);
9443 Set_Is_Immediately_Visible (E);
9444 Set_Current_Entity (E);
9445 Set_Homonym (E, Prev);
9448 ---------------------
9449 -- Install_Formals --
9450 ---------------------
9452 procedure Install_Formals (Id : Entity_Id) is
9455 F := First_Formal (Id);
9456 while Present (F) loop
9460 end Install_Formals;
9462 -----------------------------
9463 -- Is_Interface_Conformant --
9464 -----------------------------
9466 function Is_Interface_Conformant
9467 (Tagged_Type : Entity_Id;
9468 Iface_Prim : Entity_Id;
9469 Prim : Entity_Id) return Boolean
9471 -- The operation may in fact be an inherited (implicit) operation
9472 -- rather than the original interface primitive, so retrieve the
9473 -- ultimate ancestor.
9475 Iface : constant Entity_Id :=
9476 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9477 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9479 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9480 -- Return the controlling formal of Prim
9482 ------------------------
9483 -- Controlling_Formal --
9484 ------------------------
9486 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9490 E := First_Entity (Prim);
9491 while Present (E) loop
9492 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9500 end Controlling_Formal;
9504 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9505 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9507 -- Start of processing for Is_Interface_Conformant
9510 pragma Assert (Is_Subprogram (Iface_Prim)
9511 and then Is_Subprogram (Prim)
9512 and then Is_Dispatching_Operation (Iface_Prim)
9513 and then Is_Dispatching_Operation (Prim));
9515 pragma Assert (Is_Interface (Iface)
9516 or else (Present (Alias (Iface_Prim))
9519 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9521 if Prim = Iface_Prim
9522 or else not Is_Subprogram (Prim)
9523 or else Ekind (Prim) /= Ekind (Iface_Prim)
9524 or else not Is_Dispatching_Operation (Prim)
9525 or else Scope (Prim) /= Scope (Tagged_Type)
9527 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9528 or else not Primitive_Names_Match (Iface_Prim, Prim)
9532 -- The mode of the controlling formals must match
9534 elsif Present (Iface_Ctrl_F)
9535 and then Present (Prim_Ctrl_F)
9536 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9540 -- Case of a procedure, or a function whose result type matches the
9541 -- result type of the interface primitive, or a function that has no
9542 -- controlling result (I or access I).
9544 elsif Ekind (Iface_Prim) = E_Procedure
9545 or else Etype (Prim) = Etype (Iface_Prim)
9546 or else not Has_Controlling_Result (Prim)
9548 return Type_Conformant
9549 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9551 -- Case of a function returning an interface, or an access to one. Check
9552 -- that the return types correspond.
9554 elsif Implements_Interface (Typ, Iface) then
9555 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9557 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9562 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
9563 Skip_Controlling_Formals => True);
9569 end Is_Interface_Conformant;
9571 ---------------------------------
9572 -- Is_Non_Overriding_Operation --
9573 ---------------------------------
9575 function Is_Non_Overriding_Operation
9576 (Prev_E : Entity_Id;
9577 New_E : Entity_Id) return Boolean
9581 G_Typ : Entity_Id := Empty;
9583 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9584 -- If F_Type is a derived type associated with a generic actual subtype,
9585 -- then return its Generic_Parent_Type attribute, else return Empty.
9587 function Types_Correspond
9588 (P_Type : Entity_Id;
9589 N_Type : Entity_Id) return Boolean;
9590 -- Returns true if and only if the types (or designated types in the
9591 -- case of anonymous access types) are the same or N_Type is derived
9592 -- directly or indirectly from P_Type.
9594 -----------------------------
9595 -- Get_Generic_Parent_Type --
9596 -----------------------------
9598 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9604 if Is_Derived_Type (F_Typ)
9605 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9607 -- The tree must be traversed to determine the parent subtype in
9608 -- the generic unit, which unfortunately isn't always available
9609 -- via semantic attributes. ??? (Note: The use of Original_Node
9610 -- is needed for cases where a full derived type has been
9613 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9614 if Nkind (Defn) = N_Derived_Type_Definition then
9615 Indic := Subtype_Indication (Defn);
9617 if Nkind (Indic) = N_Subtype_Indication then
9618 G_Typ := Entity (Subtype_Mark (Indic));
9620 G_Typ := Entity (Indic);
9623 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9624 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9626 return Generic_Parent_Type (Parent (G_Typ));
9632 end Get_Generic_Parent_Type;
9634 ----------------------
9635 -- Types_Correspond --
9636 ----------------------
9638 function Types_Correspond
9639 (P_Type : Entity_Id;
9640 N_Type : Entity_Id) return Boolean
9642 Prev_Type : Entity_Id := Base_Type (P_Type);
9643 New_Type : Entity_Id := Base_Type (N_Type);
9646 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9647 Prev_Type := Designated_Type (Prev_Type);
9650 if Ekind (New_Type) = E_Anonymous_Access_Type then
9651 New_Type := Designated_Type (New_Type);
9654 if Prev_Type = New_Type then
9657 elsif not Is_Class_Wide_Type (New_Type) then
9658 while Etype (New_Type) /= New_Type loop
9659 New_Type := Etype (New_Type);
9660 if New_Type = Prev_Type then
9666 end Types_Correspond;
9668 -- Start of processing for Is_Non_Overriding_Operation
9671 -- In the case where both operations are implicit derived subprograms
9672 -- then neither overrides the other. This can only occur in certain
9673 -- obscure cases (e.g., derivation from homographs created in a generic
9676 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9679 elsif Ekind (Current_Scope) = E_Package
9680 and then Is_Generic_Instance (Current_Scope)
9681 and then In_Private_Part (Current_Scope)
9682 and then Comes_From_Source (New_E)
9684 -- We examine the formals and result type of the inherited operation,
9685 -- to determine whether their type is derived from (the instance of)
9686 -- a generic type. The first such formal or result type is the one
9689 Formal := First_Formal (Prev_E);
9690 while Present (Formal) loop
9691 F_Typ := Base_Type (Etype (Formal));
9693 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9694 F_Typ := Designated_Type (F_Typ);
9697 G_Typ := Get_Generic_Parent_Type (F_Typ);
9698 exit when Present (G_Typ);
9700 Next_Formal (Formal);
9703 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9704 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9711 -- If the generic type is a private type, then the original operation
9712 -- was not overriding in the generic, because there was no primitive
9713 -- operation to override.
9715 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9716 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9717 N_Formal_Private_Type_Definition
9721 -- The generic parent type is the ancestor of a formal derived
9722 -- type declaration. We need to check whether it has a primitive
9723 -- operation that should be overridden by New_E in the generic.
9727 P_Formal : Entity_Id;
9728 N_Formal : Entity_Id;
9732 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9735 while Present (Prim_Elt) loop
9736 P_Prim := Node (Prim_Elt);
9738 if Chars (P_Prim) = Chars (New_E)
9739 and then Ekind (P_Prim) = Ekind (New_E)
9741 P_Formal := First_Formal (P_Prim);
9742 N_Formal := First_Formal (New_E);
9743 while Present (P_Formal) and then Present (N_Formal) loop
9744 P_Typ := Etype (P_Formal);
9745 N_Typ := Etype (N_Formal);
9747 if not Types_Correspond (P_Typ, N_Typ) then
9751 Next_Entity (P_Formal);
9752 Next_Entity (N_Formal);
9755 -- Found a matching primitive operation belonging to the
9756 -- formal ancestor type, so the new subprogram is
9760 and then No (N_Formal)
9761 and then (Ekind (New_E) /= E_Function
9764 (Etype (P_Prim), Etype (New_E)))
9770 Next_Elmt (Prim_Elt);
9773 -- If no match found, then the new subprogram does not override
9774 -- in the generic (nor in the instance).
9776 -- If the type in question is not abstract, and the subprogram
9777 -- is, this will be an error if the new operation is in the
9778 -- private part of the instance. Emit a warning now, which will
9779 -- make the subsequent error message easier to understand.
9781 if not Is_Abstract_Type (F_Typ)
9782 and then Is_Abstract_Subprogram (Prev_E)
9783 and then In_Private_Part (Current_Scope)
9785 Error_Msg_Node_2 := F_Typ;
9787 ("private operation& in generic unit does not override " &
9788 "any primitive operation of& (RM 12.3 (18))??",
9798 end Is_Non_Overriding_Operation;
9800 -------------------------------------
9801 -- List_Inherited_Pre_Post_Aspects --
9802 -------------------------------------
9804 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9806 if Opt.List_Inherited_Aspects
9807 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9810 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9814 for J in Inherited'Range loop
9815 P := Pre_Post_Conditions (Contract (Inherited (J)));
9816 while Present (P) loop
9817 Error_Msg_Sloc := Sloc (P);
9819 if Class_Present (P) and then not Split_PPC (P) then
9820 if Pragma_Name (P) = Name_Precondition then
9822 ("info: & inherits `Pre''Class` aspect from #?L?",
9826 ("info: & inherits `Post''Class` aspect from #?L?",
9831 P := Next_Pragma (P);
9836 end List_Inherited_Pre_Post_Aspects;
9838 ------------------------------
9839 -- Make_Inequality_Operator --
9840 ------------------------------
9842 -- S is the defining identifier of an equality operator. We build a
9843 -- subprogram declaration with the right signature. This operation is
9844 -- intrinsic, because it is always expanded as the negation of the
9845 -- call to the equality function.
9847 procedure Make_Inequality_Operator (S : Entity_Id) is
9848 Loc : constant Source_Ptr := Sloc (S);
9851 Op_Name : Entity_Id;
9853 FF : constant Entity_Id := First_Formal (S);
9854 NF : constant Entity_Id := Next_Formal (FF);
9857 -- Check that equality was properly defined, ignore call if not
9864 A : constant Entity_Id :=
9865 Make_Defining_Identifier (Sloc (FF),
9866 Chars => Chars (FF));
9868 B : constant Entity_Id :=
9869 Make_Defining_Identifier (Sloc (NF),
9870 Chars => Chars (NF));
9873 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9875 Formals := New_List (
9876 Make_Parameter_Specification (Loc,
9877 Defining_Identifier => A,
9879 New_Occurrence_Of (Etype (First_Formal (S)),
9880 Sloc (Etype (First_Formal (S))))),
9882 Make_Parameter_Specification (Loc,
9883 Defining_Identifier => B,
9885 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
9886 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9889 Make_Subprogram_Declaration (Loc,
9891 Make_Function_Specification (Loc,
9892 Defining_Unit_Name => Op_Name,
9893 Parameter_Specifications => Formals,
9894 Result_Definition =>
9895 New_Occurrence_Of (Standard_Boolean, Loc)));
9897 -- Insert inequality right after equality if it is explicit or after
9898 -- the derived type when implicit. These entities are created only
9899 -- for visibility purposes, and eventually replaced in the course
9900 -- of expansion, so they do not need to be attached to the tree and
9901 -- seen by the back-end. Keeping them internal also avoids spurious
9902 -- freezing problems. The declaration is inserted in the tree for
9903 -- analysis, and removed afterwards. If the equality operator comes
9904 -- from an explicit declaration, attach the inequality immediately
9905 -- after. Else the equality is inherited from a derived type
9906 -- declaration, so insert inequality after that declaration.
9908 if No (Alias (S)) then
9909 Insert_After (Unit_Declaration_Node (S), Decl);
9910 elsif Is_List_Member (Parent (S)) then
9911 Insert_After (Parent (S), Decl);
9913 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9916 Mark_Rewrite_Insertion (Decl);
9917 Set_Is_Intrinsic_Subprogram (Op_Name);
9920 Set_Has_Completion (Op_Name);
9921 Set_Corresponding_Equality (Op_Name, S);
9922 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9924 end Make_Inequality_Operator;
9926 ----------------------
9927 -- May_Need_Actuals --
9928 ----------------------
9930 procedure May_Need_Actuals (Fun : Entity_Id) is
9935 F := First_Formal (Fun);
9937 while Present (F) loop
9938 if No (Default_Value (F)) then
9946 Set_Needs_No_Actuals (Fun, B);
9947 end May_Need_Actuals;
9949 ---------------------
9950 -- Mode_Conformant --
9951 ---------------------
9953 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9956 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9958 end Mode_Conformant;
9960 ---------------------------
9961 -- New_Overloaded_Entity --
9962 ---------------------------
9964 procedure New_Overloaded_Entity
9966 Derived_Type : Entity_Id := Empty)
9968 Overridden_Subp : Entity_Id := Empty;
9969 -- Set if the current scope has an operation that is type-conformant
9970 -- with S, and becomes hidden by S.
9972 Is_Primitive_Subp : Boolean;
9973 -- Set to True if the new subprogram is primitive
9976 -- Entity that S overrides
9978 Prev_Vis : Entity_Id := Empty;
9979 -- Predecessor of E in Homonym chain
9981 procedure Check_For_Primitive_Subprogram
9982 (Is_Primitive : out Boolean;
9983 Is_Overriding : Boolean := False);
9984 -- If the subprogram being analyzed is a primitive operation of the type
9985 -- of a formal or result, set the Has_Primitive_Operations flag on the
9986 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9987 -- corresponding flag on the entity itself for later use.
9989 procedure Check_Synchronized_Overriding
9990 (Def_Id : Entity_Id;
9991 Overridden_Subp : out Entity_Id);
9992 -- First determine if Def_Id is an entry or a subprogram either defined
9993 -- in the scope of a task or protected type, or is a primitive of such
9994 -- a type. Check whether Def_Id overrides a subprogram of an interface
9995 -- implemented by the synchronized type, return the overridden entity
9998 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9999 -- Check that E is declared in the private part of the current package,
10000 -- or in the package body, where it may hide a previous declaration.
10001 -- We can't use In_Private_Part by itself because this flag is also
10002 -- set when freezing entities, so we must examine the place of the
10003 -- declaration in the tree, and recognize wrapper packages as well.
10005 function Is_Overriding_Alias
10006 (Old_E : Entity_Id;
10007 New_E : Entity_Id) return Boolean;
10008 -- Check whether new subprogram and old subprogram are both inherited
10009 -- from subprograms that have distinct dispatch table entries. This can
10010 -- occur with derivations from instances with accidental homonyms. The
10011 -- function is conservative given that the converse is only true within
10012 -- instances that contain accidental overloadings.
10014 ------------------------------------
10015 -- Check_For_Primitive_Subprogram --
10016 ------------------------------------
10018 procedure Check_For_Primitive_Subprogram
10019 (Is_Primitive : out Boolean;
10020 Is_Overriding : Boolean := False)
10022 Formal : Entity_Id;
10026 function Visible_Part_Type (T : Entity_Id) return Boolean;
10027 -- Returns true if T is declared in the visible part of the current
10028 -- package scope; otherwise returns false. Assumes that T is declared
10031 procedure Check_Private_Overriding (T : Entity_Id);
10032 -- Checks that if a primitive abstract subprogram of a visible
10033 -- abstract type is declared in a private part, then it must override
10034 -- an abstract subprogram declared in the visible part. Also checks
10035 -- that if a primitive function with a controlling result is declared
10036 -- in a private part, then it must override a function declared in
10037 -- the visible part.
10039 ------------------------------
10040 -- Check_Private_Overriding --
10041 ------------------------------
10043 procedure Check_Private_Overriding (T : Entity_Id) is
10045 if Is_Package_Or_Generic_Package (Current_Scope)
10046 and then In_Private_Part (Current_Scope)
10047 and then Visible_Part_Type (T)
10048 and then not In_Instance
10050 if Is_Abstract_Type (T)
10051 and then Is_Abstract_Subprogram (S)
10052 and then (not Is_Overriding
10053 or else not Is_Abstract_Subprogram (E))
10056 ("abstract subprograms must be visible "
10057 & "(RM 3.9.3(10))!", S);
10059 elsif Ekind (S) = E_Function and then not Is_Overriding then
10060 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
10062 ("private function with tagged result must"
10063 & " override visible-part function", S);
10065 ("\move subprogram to the visible part"
10066 & " (RM 3.9.3(10))", S);
10068 -- AI05-0073: extend this test to the case of a function
10069 -- with a controlling access result.
10071 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10072 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10074 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
10075 and then Ada_Version >= Ada_2012
10078 ("private function with controlling access result "
10079 & "must override visible-part function", S);
10081 ("\move subprogram to the visible part"
10082 & " (RM 3.9.3(10))", S);
10086 end Check_Private_Overriding;
10088 -----------------------
10089 -- Visible_Part_Type --
10090 -----------------------
10092 function Visible_Part_Type (T : Entity_Id) return Boolean is
10093 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10097 -- If the entity is a private type, then it must be declared in a
10100 if Ekind (T) in Private_Kind then
10104 -- Otherwise, we traverse the visible part looking for its
10105 -- corresponding declaration. We cannot use the declaration
10106 -- node directly because in the private part the entity of a
10107 -- private type is the one in the full view, which does not
10108 -- indicate that it is the completion of something visible.
10110 N := First (Visible_Declarations (Specification (P)));
10111 while Present (N) loop
10112 if Nkind (N) = N_Full_Type_Declaration
10113 and then Present (Defining_Identifier (N))
10114 and then T = Defining_Identifier (N)
10118 elsif Nkind_In (N, N_Private_Type_Declaration,
10119 N_Private_Extension_Declaration)
10120 and then Present (Defining_Identifier (N))
10121 and then T = Full_View (Defining_Identifier (N))
10130 end Visible_Part_Type;
10132 -- Start of processing for Check_For_Primitive_Subprogram
10135 Is_Primitive := False;
10137 if not Comes_From_Source (S) then
10140 -- If subprogram is at library level, it is not primitive operation
10142 elsif Current_Scope = Standard_Standard then
10145 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10146 and then not In_Package_Body (Current_Scope))
10147 or else Is_Overriding
10149 -- For function, check return type
10151 if Ekind (S) = E_Function then
10152 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10153 F_Typ := Designated_Type (Etype (S));
10155 F_Typ := Etype (S);
10158 B_Typ := Base_Type (F_Typ);
10160 if Scope (B_Typ) = Current_Scope
10161 and then not Is_Class_Wide_Type (B_Typ)
10162 and then not Is_Generic_Type (B_Typ)
10164 Is_Primitive := True;
10165 Set_Has_Primitive_Operations (B_Typ);
10166 Set_Is_Primitive (S);
10167 Check_Private_Overriding (B_Typ);
10171 -- For all subprograms, check formals
10173 Formal := First_Formal (S);
10174 while Present (Formal) loop
10175 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10176 F_Typ := Designated_Type (Etype (Formal));
10178 F_Typ := Etype (Formal);
10181 B_Typ := Base_Type (F_Typ);
10183 if Ekind (B_Typ) = E_Access_Subtype then
10184 B_Typ := Base_Type (B_Typ);
10187 if Scope (B_Typ) = Current_Scope
10188 and then not Is_Class_Wide_Type (B_Typ)
10189 and then not Is_Generic_Type (B_Typ)
10191 Is_Primitive := True;
10192 Set_Is_Primitive (S);
10193 Set_Has_Primitive_Operations (B_Typ);
10194 Check_Private_Overriding (B_Typ);
10197 Next_Formal (Formal);
10200 -- Special case: An equality function can be redefined for a type
10201 -- occurring in a declarative part, and won't otherwise be treated as
10202 -- a primitive because it doesn't occur in a package spec and doesn't
10203 -- override an inherited subprogram. It's important that we mark it
10204 -- primitive so it can be returned by Collect_Primitive_Operations
10205 -- and be used in composing the equality operation of later types
10206 -- that have a component of the type.
10208 elsif Chars (S) = Name_Op_Eq
10209 and then Etype (S) = Standard_Boolean
10211 B_Typ := Base_Type (Etype (First_Formal (S)));
10213 if Scope (B_Typ) = Current_Scope
10215 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10216 and then not Is_Limited_Type (B_Typ)
10218 Is_Primitive := True;
10219 Set_Is_Primitive (S);
10220 Set_Has_Primitive_Operations (B_Typ);
10221 Check_Private_Overriding (B_Typ);
10224 end Check_For_Primitive_Subprogram;
10226 -----------------------------------
10227 -- Check_Synchronized_Overriding --
10228 -----------------------------------
10230 procedure Check_Synchronized_Overriding
10231 (Def_Id : Entity_Id;
10232 Overridden_Subp : out Entity_Id)
10234 Ifaces_List : Elist_Id;
10235 In_Scope : Boolean;
10238 function Matches_Prefixed_View_Profile
10239 (Prim_Params : List_Id;
10240 Iface_Params : List_Id) return Boolean;
10241 -- Determine whether a subprogram's parameter profile Prim_Params
10242 -- matches that of a potentially overridden interface subprogram
10243 -- Iface_Params. Also determine if the type of first parameter of
10244 -- Iface_Params is an implemented interface.
10246 -----------------------------------
10247 -- Matches_Prefixed_View_Profile --
10248 -----------------------------------
10250 function Matches_Prefixed_View_Profile
10251 (Prim_Params : List_Id;
10252 Iface_Params : List_Id) return Boolean
10254 Iface_Id : Entity_Id;
10255 Iface_Param : Node_Id;
10256 Iface_Typ : Entity_Id;
10257 Prim_Id : Entity_Id;
10258 Prim_Param : Node_Id;
10259 Prim_Typ : Entity_Id;
10261 function Is_Implemented
10262 (Ifaces_List : Elist_Id;
10263 Iface : Entity_Id) return Boolean;
10264 -- Determine if Iface is implemented by the current task or
10267 --------------------
10268 -- Is_Implemented --
10269 --------------------
10271 function Is_Implemented
10272 (Ifaces_List : Elist_Id;
10273 Iface : Entity_Id) return Boolean
10275 Iface_Elmt : Elmt_Id;
10278 Iface_Elmt := First_Elmt (Ifaces_List);
10279 while Present (Iface_Elmt) loop
10280 if Node (Iface_Elmt) = Iface then
10284 Next_Elmt (Iface_Elmt);
10288 end Is_Implemented;
10290 -- Start of processing for Matches_Prefixed_View_Profile
10293 Iface_Param := First (Iface_Params);
10294 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
10296 if Is_Access_Type (Iface_Typ) then
10297 Iface_Typ := Designated_Type (Iface_Typ);
10300 Prim_Param := First (Prim_Params);
10302 -- The first parameter of the potentially overridden subprogram
10303 -- must be an interface implemented by Prim.
10305 if not Is_Interface (Iface_Typ)
10306 or else not Is_Implemented (Ifaces_List, Iface_Typ)
10311 -- The checks on the object parameters are done, move onto the
10312 -- rest of the parameters.
10314 if not In_Scope then
10315 Prim_Param := Next (Prim_Param);
10318 Iface_Param := Next (Iface_Param);
10319 while Present (Iface_Param) and then Present (Prim_Param) loop
10320 Iface_Id := Defining_Identifier (Iface_Param);
10321 Iface_Typ := Find_Parameter_Type (Iface_Param);
10323 Prim_Id := Defining_Identifier (Prim_Param);
10324 Prim_Typ := Find_Parameter_Type (Prim_Param);
10326 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
10327 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
10328 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
10330 Iface_Typ := Designated_Type (Iface_Typ);
10331 Prim_Typ := Designated_Type (Prim_Typ);
10334 -- Case of multiple interface types inside a parameter profile
10336 -- (Obj_Param : in out Iface; ...; Param : Iface)
10338 -- If the interface type is implemented, then the matching type
10339 -- in the primitive should be the implementing record type.
10341 if Ekind (Iface_Typ) = E_Record_Type
10342 and then Is_Interface (Iface_Typ)
10343 and then Is_Implemented (Ifaces_List, Iface_Typ)
10345 if Prim_Typ /= Typ then
10349 -- The two parameters must be both mode and subtype conformant
10351 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
10353 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
10358 Next (Iface_Param);
10362 -- One of the two lists contains more parameters than the other
10364 if Present (Iface_Param) or else Present (Prim_Param) then
10369 end Matches_Prefixed_View_Profile;
10371 -- Start of processing for Check_Synchronized_Overriding
10374 Overridden_Subp := Empty;
10376 -- Def_Id must be an entry or a subprogram. We should skip predefined
10377 -- primitives internally generated by the frontend; however at this
10378 -- stage predefined primitives are still not fully decorated. As a
10379 -- minor optimization we skip here internally generated subprograms.
10381 if (Ekind (Def_Id) /= E_Entry
10382 and then Ekind (Def_Id) /= E_Function
10383 and then Ekind (Def_Id) /= E_Procedure)
10384 or else not Comes_From_Source (Def_Id)
10389 -- Search for the concurrent declaration since it contains the list
10390 -- of all implemented interfaces. In this case, the subprogram is
10391 -- declared within the scope of a protected or a task type.
10393 if Present (Scope (Def_Id))
10394 and then Is_Concurrent_Type (Scope (Def_Id))
10395 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10397 Typ := Scope (Def_Id);
10400 -- The enclosing scope is not a synchronized type and the subprogram
10403 elsif No (First_Formal (Def_Id)) then
10406 -- The subprogram has formals and hence it may be a primitive of a
10407 -- concurrent type.
10410 Typ := Etype (First_Formal (Def_Id));
10412 if Is_Access_Type (Typ) then
10413 Typ := Directly_Designated_Type (Typ);
10416 if Is_Concurrent_Type (Typ)
10417 and then not Is_Generic_Actual_Type (Typ)
10421 -- This case occurs when the concurrent type is declared within
10422 -- a generic unit. As a result the corresponding record has been
10423 -- built and used as the type of the first formal, we just have
10424 -- to retrieve the corresponding concurrent type.
10426 elsif Is_Concurrent_Record_Type (Typ)
10427 and then not Is_Class_Wide_Type (Typ)
10428 and then Present (Corresponding_Concurrent_Type (Typ))
10430 Typ := Corresponding_Concurrent_Type (Typ);
10438 -- There is no overriding to check if is an inherited operation in a
10439 -- type derivation on for a generic actual.
10441 Collect_Interfaces (Typ, Ifaces_List);
10443 if Is_Empty_Elmt_List (Ifaces_List) then
10447 -- Determine whether entry or subprogram Def_Id overrides a primitive
10448 -- operation that belongs to one of the interfaces in Ifaces_List.
10451 Candidate : Entity_Id := Empty;
10452 Hom : Entity_Id := Empty;
10453 Iface_Typ : Entity_Id;
10454 Subp : Entity_Id := Empty;
10457 -- Traverse the homonym chain, looking for a potentially
10458 -- overridden subprogram that belongs to an implemented
10461 Hom := Current_Entity_In_Scope (Def_Id);
10462 while Present (Hom) loop
10466 or else not Is_Overloadable (Subp)
10467 or else not Is_Primitive (Subp)
10468 or else not Is_Dispatching_Operation (Subp)
10469 or else not Present (Find_Dispatching_Type (Subp))
10470 or else not Is_Interface (Find_Dispatching_Type (Subp))
10474 -- Entries and procedures can override abstract or null
10475 -- interface procedures.
10477 elsif (Ekind (Def_Id) = E_Procedure
10478 or else Ekind (Def_Id) = E_Entry)
10479 and then Ekind (Subp) = E_Procedure
10480 and then Matches_Prefixed_View_Profile
10481 (Parameter_Specifications (Parent (Def_Id)),
10482 Parameter_Specifications (Parent (Subp)))
10486 -- For an overridden subprogram Subp, check whether the mode
10487 -- of its first parameter is correct depending on the kind
10488 -- of synchronized type.
10491 Formal : constant Node_Id := First_Formal (Candidate);
10494 -- In order for an entry or a protected procedure to
10495 -- override, the first parameter of the overridden
10496 -- routine must be of mode "out", "in out" or
10497 -- access-to-variable.
10499 if Ekind_In (Candidate, E_Entry, E_Procedure)
10500 and then Is_Protected_Type (Typ)
10501 and then Ekind (Formal) /= E_In_Out_Parameter
10502 and then Ekind (Formal) /= E_Out_Parameter
10503 and then Nkind (Parameter_Type (Parent (Formal))) /=
10504 N_Access_Definition
10508 -- All other cases are OK since a task entry or routine
10509 -- does not have a restriction on the mode of the first
10510 -- parameter of the overridden interface routine.
10513 Overridden_Subp := Candidate;
10518 -- Functions can override abstract interface functions
10520 elsif Ekind (Def_Id) = E_Function
10521 and then Ekind (Subp) = E_Function
10522 and then Matches_Prefixed_View_Profile
10523 (Parameter_Specifications (Parent (Def_Id)),
10524 Parameter_Specifications (Parent (Subp)))
10525 and then Etype (Result_Definition (Parent (Def_Id))) =
10526 Etype (Result_Definition (Parent (Subp)))
10528 Overridden_Subp := Subp;
10532 Hom := Homonym (Hom);
10535 -- After examining all candidates for overriding, we are left with
10536 -- the best match which is a mode incompatible interface routine.
10537 -- Do not emit an error if the Expander is active since this error
10538 -- will be detected later on after all concurrent types are
10539 -- expanded and all wrappers are built. This check is meant for
10540 -- spec-only compilations.
10542 if Present (Candidate) and then not Expander_Active then
10544 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10546 -- Def_Id is primitive of a protected type, declared inside the
10547 -- type, and the candidate is primitive of a limited or
10548 -- synchronized interface.
10551 and then Is_Protected_Type (Typ)
10553 (Is_Limited_Interface (Iface_Typ)
10554 or else Is_Protected_Interface (Iface_Typ)
10555 or else Is_Synchronized_Interface (Iface_Typ)
10556 or else Is_Task_Interface (Iface_Typ))
10558 Error_Msg_PT (Parent (Typ), Candidate);
10562 Overridden_Subp := Candidate;
10565 end Check_Synchronized_Overriding;
10567 ----------------------------
10568 -- Is_Private_Declaration --
10569 ----------------------------
10571 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10572 Priv_Decls : List_Id;
10573 Decl : constant Node_Id := Unit_Declaration_Node (E);
10576 if Is_Package_Or_Generic_Package (Current_Scope)
10577 and then In_Private_Part (Current_Scope)
10580 Private_Declarations (Package_Specification (Current_Scope));
10582 return In_Package_Body (Current_Scope)
10584 (Is_List_Member (Decl)
10585 and then List_Containing (Decl) = Priv_Decls)
10586 or else (Nkind (Parent (Decl)) = N_Package_Specification
10588 Is_Compilation_Unit
10589 (Defining_Entity (Parent (Decl)))
10590 and then List_Containing (Parent (Parent (Decl))) =
10595 end Is_Private_Declaration;
10597 --------------------------
10598 -- Is_Overriding_Alias --
10599 --------------------------
10601 function Is_Overriding_Alias
10602 (Old_E : Entity_Id;
10603 New_E : Entity_Id) return Boolean
10605 AO : constant Entity_Id := Alias (Old_E);
10606 AN : constant Entity_Id := Alias (New_E);
10609 return Scope (AO) /= Scope (AN)
10610 or else No (DTC_Entity (AO))
10611 or else No (DTC_Entity (AN))
10612 or else DT_Position (AO) = DT_Position (AN);
10613 end Is_Overriding_Alias;
10615 -- Start of processing for New_Overloaded_Entity
10618 -- We need to look for an entity that S may override. This must be a
10619 -- homonym in the current scope, so we look for the first homonym of
10620 -- S in the current scope as the starting point for the search.
10622 E := Current_Entity_In_Scope (S);
10624 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10625 -- They are directly added to the list of primitive operations of
10626 -- Derived_Type, unless this is a rederivation in the private part
10627 -- of an operation that was already derived in the visible part of
10628 -- the current package.
10630 if Ada_Version >= Ada_2005
10631 and then Present (Derived_Type)
10632 and then Present (Alias (S))
10633 and then Is_Dispatching_Operation (Alias (S))
10634 and then Present (Find_Dispatching_Type (Alias (S)))
10635 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10637 -- For private types, when the full-view is processed we propagate to
10638 -- the full view the non-overridden entities whose attribute "alias"
10639 -- references an interface primitive. These entities were added by
10640 -- Derive_Subprograms to ensure that interface primitives are
10643 -- Inside_Freeze_Actions is non zero when S corresponds with an
10644 -- internal entity that links an interface primitive with its
10645 -- covering primitive through attribute Interface_Alias (see
10646 -- Add_Internal_Interface_Entities).
10648 if Inside_Freezing_Actions = 0
10649 and then Is_Package_Or_Generic_Package (Current_Scope)
10650 and then In_Private_Part (Current_Scope)
10651 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10652 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10653 and then Full_View (Defining_Identifier (Parent (E)))
10654 = Defining_Identifier (Parent (S))
10655 and then Alias (E) = Alias (S)
10657 Check_Operation_From_Private_View (S, E);
10658 Set_Is_Dispatching_Operation (S);
10663 Enter_Overloaded_Entity (S);
10664 Check_Dispatching_Operation (S, Empty);
10665 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10671 -- If there is no homonym then this is definitely not overriding
10674 Enter_Overloaded_Entity (S);
10675 Check_Dispatching_Operation (S, Empty);
10676 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10678 -- If subprogram has an explicit declaration, check whether it has an
10679 -- overriding indicator.
10681 if Comes_From_Source (S) then
10682 Check_Synchronized_Overriding (S, Overridden_Subp);
10684 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10685 -- it may have overridden some hidden inherited primitive. Update
10686 -- Overridden_Subp to avoid spurious errors when checking the
10687 -- overriding indicator.
10689 if Ada_Version >= Ada_2012
10690 and then No (Overridden_Subp)
10691 and then Is_Dispatching_Operation (S)
10692 and then Present (Overridden_Operation (S))
10694 Overridden_Subp := Overridden_Operation (S);
10697 Check_Overriding_Indicator
10698 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10701 -- If there is a homonym that is not overloadable, then we have an
10702 -- error, except for the special cases checked explicitly below.
10704 elsif not Is_Overloadable (E) then
10706 -- Check for spurious conflict produced by a subprogram that has the
10707 -- same name as that of the enclosing generic package. The conflict
10708 -- occurs within an instance, between the subprogram and the renaming
10709 -- declaration for the package. After the subprogram, the package
10710 -- renaming declaration becomes hidden.
10712 if Ekind (E) = E_Package
10713 and then Present (Renamed_Object (E))
10714 and then Renamed_Object (E) = Current_Scope
10715 and then Nkind (Parent (Renamed_Object (E))) =
10716 N_Package_Specification
10717 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10720 Set_Is_Immediately_Visible (E, False);
10721 Enter_Overloaded_Entity (S);
10722 Set_Homonym (S, Homonym (E));
10723 Check_Dispatching_Operation (S, Empty);
10724 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10726 -- If the subprogram is implicit it is hidden by the previous
10727 -- declaration. However if it is dispatching, it must appear in the
10728 -- dispatch table anyway, because it can be dispatched to even if it
10729 -- cannot be called directly.
10731 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10732 Set_Scope (S, Current_Scope);
10734 if Is_Dispatching_Operation (Alias (S)) then
10735 Check_Dispatching_Operation (S, Empty);
10741 Error_Msg_Sloc := Sloc (E);
10743 -- Generate message, with useful additional warning if in generic
10745 if Is_Generic_Unit (E) then
10746 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10747 Error_Msg_N ("\& conflicts with declaration#", S);
10749 Error_Msg_N ("& conflicts with declaration#", S);
10755 -- E exists and is overloadable
10758 Check_Synchronized_Overriding (S, Overridden_Subp);
10760 -- Loop through E and its homonyms to determine if any of them is
10761 -- the candidate for overriding by S.
10763 while Present (E) loop
10765 -- Definitely not interesting if not in the current scope
10767 if Scope (E) /= Current_Scope then
10770 -- A function can overload the name of an abstract state. The
10771 -- state can be viewed as a function with a profile that cannot
10772 -- be matched by anything.
10774 elsif Ekind (S) = E_Function
10775 and then Ekind (E) = E_Abstract_State
10777 Enter_Overloaded_Entity (S);
10780 -- Ada 2012 (AI05-0165): For internally generated bodies of null
10781 -- procedures locate the internally generated spec. We enforce
10782 -- mode conformance since a tagged type may inherit from
10783 -- interfaces several null primitives which differ only in
10784 -- the mode of the formals.
10786 elsif not Comes_From_Source (S)
10787 and then Is_Null_Procedure (S)
10788 and then not Mode_Conformant (E, S)
10792 -- Check if we have type conformance
10794 elsif Type_Conformant (E, S) then
10796 -- If the old and new entities have the same profile and one
10797 -- is not the body of the other, then this is an error, unless
10798 -- one of them is implicitly declared.
10800 -- There are some cases when both can be implicit, for example
10801 -- when both a literal and a function that overrides it are
10802 -- inherited in a derivation, or when an inherited operation
10803 -- of a tagged full type overrides the inherited operation of
10804 -- a private extension. Ada 83 had a special rule for the
10805 -- literal case. In Ada 95, the later implicit operation hides
10806 -- the former, and the literal is always the former. In the
10807 -- odd case where both are derived operations declared at the
10808 -- same point, both operations should be declared, and in that
10809 -- case we bypass the following test and proceed to the next
10810 -- part. This can only occur for certain obscure cases in
10811 -- instances, when an operation on a type derived from a formal
10812 -- private type does not override a homograph inherited from
10813 -- the actual. In subsequent derivations of such a type, the
10814 -- DT positions of these operations remain distinct, if they
10817 if Present (Alias (S))
10818 and then (No (Alias (E))
10819 or else Comes_From_Source (E)
10820 or else Is_Abstract_Subprogram (S)
10822 (Is_Dispatching_Operation (E)
10823 and then Is_Overriding_Alias (E, S)))
10824 and then Ekind (E) /= E_Enumeration_Literal
10826 -- When an derived operation is overloaded it may be due to
10827 -- the fact that the full view of a private extension
10828 -- re-inherits. It has to be dealt with.
10830 if Is_Package_Or_Generic_Package (Current_Scope)
10831 and then In_Private_Part (Current_Scope)
10833 Check_Operation_From_Private_View (S, E);
10836 -- In any case the implicit operation remains hidden by the
10837 -- existing declaration, which is overriding. Indicate that
10838 -- E overrides the operation from which S is inherited.
10840 if Present (Alias (S)) then
10841 Set_Overridden_Operation (E, Alias (S));
10843 Set_Overridden_Operation (E, S);
10846 if Comes_From_Source (E) then
10847 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10852 -- Within an instance, the renaming declarations for actual
10853 -- subprograms may become ambiguous, but they do not hide each
10856 elsif Ekind (E) /= E_Entry
10857 and then not Comes_From_Source (E)
10858 and then not Is_Generic_Instance (E)
10859 and then (Present (Alias (E))
10860 or else Is_Intrinsic_Subprogram (E))
10861 and then (not In_Instance
10862 or else No (Parent (E))
10863 or else Nkind (Unit_Declaration_Node (E)) /=
10864 N_Subprogram_Renaming_Declaration)
10866 -- A subprogram child unit is not allowed to override an
10867 -- inherited subprogram (10.1.1(20)).
10869 if Is_Child_Unit (S) then
10871 ("child unit overrides inherited subprogram in parent",
10876 if Is_Non_Overriding_Operation (E, S) then
10877 Enter_Overloaded_Entity (S);
10879 if No (Derived_Type)
10880 or else Is_Tagged_Type (Derived_Type)
10882 Check_Dispatching_Operation (S, Empty);
10888 -- E is a derived operation or an internal operator which
10889 -- is being overridden. Remove E from further visibility.
10890 -- Furthermore, if E is a dispatching operation, it must be
10891 -- replaced in the list of primitive operations of its type
10892 -- (see Override_Dispatching_Operation).
10894 Overridden_Subp := E;
10900 Prev := First_Entity (Current_Scope);
10901 while Present (Prev) and then Next_Entity (Prev) /= E loop
10902 Next_Entity (Prev);
10905 -- It is possible for E to be in the current scope and
10906 -- yet not in the entity chain. This can only occur in a
10907 -- generic context where E is an implicit concatenation
10908 -- in the formal part, because in a generic body the
10909 -- entity chain starts with the formals.
10912 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10914 -- E must be removed both from the entity_list of the
10915 -- current scope, and from the visibility chain
10917 if Debug_Flag_E then
10918 Write_Str ("Override implicit operation ");
10919 Write_Int (Int (E));
10923 -- If E is a predefined concatenation, it stands for four
10924 -- different operations. As a result, a single explicit
10925 -- declaration does not hide it. In a possible ambiguous
10926 -- situation, Disambiguate chooses the user-defined op,
10927 -- so it is correct to retain the previous internal one.
10929 if Chars (E) /= Name_Op_Concat
10930 or else Ekind (E) /= E_Operator
10932 -- For nondispatching derived operations that are
10933 -- overridden by a subprogram declared in the private
10934 -- part of a package, we retain the derived subprogram
10935 -- but mark it as not immediately visible. If the
10936 -- derived operation was declared in the visible part
10937 -- then this ensures that it will still be visible
10938 -- outside the package with the proper signature
10939 -- (calls from outside must also be directed to this
10940 -- version rather than the overriding one, unlike the
10941 -- dispatching case). Calls from inside the package
10942 -- will still resolve to the overriding subprogram
10943 -- since the derived one is marked as not visible
10944 -- within the package.
10946 -- If the private operation is dispatching, we achieve
10947 -- the overriding by keeping the implicit operation
10948 -- but setting its alias to be the overriding one. In
10949 -- this fashion the proper body is executed in all
10950 -- cases, but the original signature is used outside
10953 -- If the overriding is not in the private part, we
10954 -- remove the implicit operation altogether.
10956 if Is_Private_Declaration (S) then
10957 if not Is_Dispatching_Operation (E) then
10958 Set_Is_Immediately_Visible (E, False);
10960 -- Work done in Override_Dispatching_Operation,
10961 -- so nothing else needs to be done here.
10967 -- Find predecessor of E in Homonym chain
10969 if E = Current_Entity (E) then
10972 Prev_Vis := Current_Entity (E);
10973 while Homonym (Prev_Vis) /= E loop
10974 Prev_Vis := Homonym (Prev_Vis);
10978 if Prev_Vis /= Empty then
10980 -- Skip E in the visibility chain
10982 Set_Homonym (Prev_Vis, Homonym (E));
10985 Set_Name_Entity_Id (Chars (E), Homonym (E));
10988 Set_Next_Entity (Prev, Next_Entity (E));
10990 if No (Next_Entity (Prev)) then
10991 Set_Last_Entity (Current_Scope, Prev);
10996 Enter_Overloaded_Entity (S);
10998 -- For entities generated by Derive_Subprograms the
10999 -- overridden operation is the inherited primitive
11000 -- (which is available through the attribute alias).
11002 if not (Comes_From_Source (E))
11003 and then Is_Dispatching_Operation (E)
11004 and then Find_Dispatching_Type (E) =
11005 Find_Dispatching_Type (S)
11006 and then Present (Alias (E))
11007 and then Comes_From_Source (Alias (E))
11009 Set_Overridden_Operation (S, Alias (E));
11011 -- Normal case of setting entity as overridden
11013 -- Note: Static_Initialization and Overridden_Operation
11014 -- attributes use the same field in subprogram entities.
11015 -- Static_Initialization is only defined for internal
11016 -- initialization procedures, where Overridden_Operation
11017 -- is irrelevant. Therefore the setting of this attribute
11018 -- must check whether the target is an init_proc.
11020 elsif not Is_Init_Proc (S) then
11021 Set_Overridden_Operation (S, E);
11024 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11026 -- If S is a user-defined subprogram or a null procedure
11027 -- expanded to override an inherited null procedure, or a
11028 -- predefined dispatching primitive then indicate that E
11029 -- overrides the operation from which S is inherited.
11031 if Comes_From_Source (S)
11033 (Present (Parent (S))
11035 Nkind (Parent (S)) = N_Procedure_Specification
11037 Null_Present (Parent (S)))
11039 (Present (Alias (E))
11041 Is_Predefined_Dispatching_Operation (Alias (E)))
11043 if Present (Alias (E)) then
11044 Set_Overridden_Operation (S, Alias (E));
11048 if Is_Dispatching_Operation (E) then
11050 -- An overriding dispatching subprogram inherits the
11051 -- convention of the overridden subprogram (AI-117).
11053 Set_Convention (S, Convention (E));
11054 Check_Dispatching_Operation (S, E);
11057 Check_Dispatching_Operation (S, Empty);
11060 Check_For_Primitive_Subprogram
11061 (Is_Primitive_Subp, Is_Overriding => True);
11062 goto Check_Inequality;
11065 -- Apparent redeclarations in instances can occur when two
11066 -- formal types get the same actual type. The subprograms in
11067 -- in the instance are legal, even if not callable from the
11068 -- outside. Calls from within are disambiguated elsewhere.
11069 -- For dispatching operations in the visible part, the usual
11070 -- rules apply, and operations with the same profile are not
11071 -- legal (B830001).
11073 elsif (In_Instance_Visible_Part
11074 and then not Is_Dispatching_Operation (E))
11075 or else In_Instance_Not_Visible
11079 -- Here we have a real error (identical profile)
11082 Error_Msg_Sloc := Sloc (E);
11084 -- Avoid cascaded errors if the entity appears in
11085 -- subsequent calls.
11087 Set_Scope (S, Current_Scope);
11089 -- Generate error, with extra useful warning for the case
11090 -- of a generic instance with no completion.
11092 if Is_Generic_Instance (S)
11093 and then not Has_Completion (E)
11096 ("instantiation cannot provide body for&", S);
11097 Error_Msg_N ("\& conflicts with declaration#", S);
11099 Error_Msg_N ("& conflicts with declaration#", S);
11106 -- If one subprogram has an access parameter and the other
11107 -- a parameter of an access type, calls to either might be
11108 -- ambiguous. Verify that parameters match except for the
11109 -- access parameter.
11111 if May_Hide_Profile then
11117 F1 := First_Formal (S);
11118 F2 := First_Formal (E);
11119 while Present (F1) and then Present (F2) loop
11120 if Is_Access_Type (Etype (F1)) then
11121 if not Is_Access_Type (Etype (F2))
11122 or else not Conforming_Types
11123 (Designated_Type (Etype (F1)),
11124 Designated_Type (Etype (F2)),
11127 May_Hide_Profile := False;
11131 not Conforming_Types
11132 (Etype (F1), Etype (F2), Type_Conformant)
11134 May_Hide_Profile := False;
11141 if May_Hide_Profile
11145 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11154 -- On exit, we know that S is a new entity
11156 Enter_Overloaded_Entity (S);
11157 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11158 Check_Overriding_Indicator
11159 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11161 -- Overloading is not allowed in SPARK, except for operators
11163 if Nkind (S) /= N_Defining_Operator_Symbol then
11164 Error_Msg_Sloc := Sloc (Homonym (S));
11165 Check_SPARK_Restriction
11166 ("overloading not allowed with entity#", S);
11169 -- If S is a derived operation for an untagged type then by
11170 -- definition it's not a dispatching operation (even if the parent
11171 -- operation was dispatching), so Check_Dispatching_Operation is not
11172 -- called in that case.
11174 if No (Derived_Type)
11175 or else Is_Tagged_Type (Derived_Type)
11177 Check_Dispatching_Operation (S, Empty);
11181 -- If this is a user-defined equality operator that is not a derived
11182 -- subprogram, create the corresponding inequality. If the operation is
11183 -- dispatching, the expansion is done elsewhere, and we do not create
11184 -- an explicit inequality operation.
11186 <<Check_Inequality>>
11187 if Chars (S) = Name_Op_Eq
11188 and then Etype (S) = Standard_Boolean
11189 and then Present (Parent (S))
11190 and then not Is_Dispatching_Operation (S)
11192 Make_Inequality_Operator (S);
11193 Check_Untagged_Equality (S);
11195 end New_Overloaded_Entity;
11197 ---------------------
11198 -- Process_Formals --
11199 ---------------------
11201 procedure Process_Formals
11203 Related_Nod : Node_Id)
11205 Param_Spec : Node_Id;
11206 Formal : Entity_Id;
11207 Formal_Type : Entity_Id;
11211 Num_Out_Params : Nat := 0;
11212 First_Out_Param : Entity_Id := Empty;
11213 -- Used for setting Is_Only_Out_Parameter
11215 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11216 -- Determine whether an access type designates a type coming from a
11219 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11220 -- Check whether the default has a class-wide type. After analysis the
11221 -- default has the type of the formal, so we must also check explicitly
11222 -- for an access attribute.
11224 ----------------------------------
11225 -- Designates_From_Limited_With --
11226 ----------------------------------
11228 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11229 Desig : Entity_Id := Typ;
11232 if Is_Access_Type (Desig) then
11233 Desig := Directly_Designated_Type (Desig);
11236 if Is_Class_Wide_Type (Desig) then
11237 Desig := Root_Type (Desig);
11241 Ekind (Desig) = E_Incomplete_Type
11242 and then From_Limited_With (Desig);
11243 end Designates_From_Limited_With;
11245 ---------------------------
11246 -- Is_Class_Wide_Default --
11247 ---------------------------
11249 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11251 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11252 or else (Nkind (D) = N_Attribute_Reference
11253 and then Attribute_Name (D) = Name_Access
11254 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11255 end Is_Class_Wide_Default;
11257 -- Start of processing for Process_Formals
11260 -- In order to prevent premature use of the formals in the same formal
11261 -- part, the Ekind is left undefined until all default expressions are
11262 -- analyzed. The Ekind is established in a separate loop at the end.
11264 Param_Spec := First (T);
11265 while Present (Param_Spec) loop
11266 Formal := Defining_Identifier (Param_Spec);
11267 Set_Never_Set_In_Source (Formal, True);
11268 Enter_Name (Formal);
11270 -- Case of ordinary parameters
11272 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11273 Find_Type (Parameter_Type (Param_Spec));
11274 Ptype := Parameter_Type (Param_Spec);
11276 if Ptype = Error then
11280 Formal_Type := Entity (Ptype);
11282 if Is_Incomplete_Type (Formal_Type)
11284 (Is_Class_Wide_Type (Formal_Type)
11285 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11287 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11288 -- primitive operations, as long as their completion is
11289 -- in the same declarative part. If in the private part
11290 -- this means that the type cannot be a Taft-amendment type.
11291 -- Check is done on package exit. For access to subprograms,
11292 -- the use is legal for Taft-amendment types.
11294 -- Ada 2012: tagged incomplete types are allowed as generic
11295 -- formal types. They do not introduce dependencies and the
11296 -- corresponding generic subprogram does not have a delayed
11297 -- freeze, because it does not need a freeze node. However,
11298 -- it is still the case that untagged incomplete types cannot
11299 -- be Taft-amendment types and must be completed in private
11300 -- part, so the subprogram must appear in the list of private
11301 -- dependents of the type.
11303 if Is_Tagged_Type (Formal_Type)
11304 or else Ada_Version >= Ada_2012
11306 if Ekind (Scope (Current_Scope)) = E_Package
11307 and then not From_Limited_With (Formal_Type)
11308 and then not Is_Generic_Type (Formal_Type)
11309 and then not Is_Class_Wide_Type (Formal_Type)
11312 (Parent (T), N_Access_Function_Definition,
11313 N_Access_Procedure_Definition)
11317 Private_Dependents (Base_Type (Formal_Type)));
11319 -- Freezing is delayed to ensure that Register_Prim
11320 -- will get called for this operation, which is needed
11321 -- in cases where static dispatch tables aren't built.
11322 -- (Note that the same is done for controlling access
11323 -- parameter cases in function Access_Definition.)
11325 Set_Has_Delayed_Freeze (Current_Scope);
11329 -- Special handling of Value_Type for CIL case
11331 elsif Is_Value_Type (Formal_Type) then
11334 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11335 N_Access_Procedure_Definition)
11337 -- AI05-0151: Tagged incomplete types are allowed in all
11338 -- formal parts. Untagged incomplete types are not allowed
11341 if Ada_Version >= Ada_2012 then
11342 if Is_Tagged_Type (Formal_Type) then
11345 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
11350 ("invalid use of untagged incomplete type&",
11351 Ptype, Formal_Type);
11356 ("invalid use of incomplete type&",
11357 Param_Spec, Formal_Type);
11359 -- Further checks on the legality of incomplete types
11360 -- in formal parts are delayed until the freeze point
11361 -- of the enclosing subprogram or access to subprogram.
11365 elsif Ekind (Formal_Type) = E_Void then
11367 ("premature use of&",
11368 Parameter_Type (Param_Spec), Formal_Type);
11371 -- Ada 2012 (AI-142): Handle aliased parameters
11373 if Ada_Version >= Ada_2012
11374 and then Aliased_Present (Param_Spec)
11376 Set_Is_Aliased (Formal);
11379 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11380 -- declaration corresponding to the null-excluding type of the
11381 -- formal in the enclosing scope. Finally, replace the parameter
11382 -- type of the formal with the internal subtype.
11384 if Ada_Version >= Ada_2005
11385 and then Null_Exclusion_Present (Param_Spec)
11387 if not Is_Access_Type (Formal_Type) then
11389 ("`NOT NULL` allowed only for an access type", Param_Spec);
11392 if Can_Never_Be_Null (Formal_Type)
11393 and then Comes_From_Source (Related_Nod)
11396 ("`NOT NULL` not allowed (& already excludes null)",
11397 Param_Spec, Formal_Type);
11401 Create_Null_Excluding_Itype
11403 Related_Nod => Related_Nod,
11404 Scope_Id => Scope (Current_Scope));
11406 -- If the designated type of the itype is an itype that is
11407 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11408 -- on the access subtype, to prevent order-of-elaboration
11409 -- issues in the backend.
11412 -- type T is access procedure;
11413 -- procedure Op (O : not null T);
11415 if Is_Itype (Directly_Designated_Type (Formal_Type))
11417 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11419 Set_Has_Delayed_Freeze (Formal_Type);
11424 -- An access formal type
11428 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11430 -- No need to continue if we already notified errors
11432 if not Present (Formal_Type) then
11436 -- Ada 2005 (AI-254)
11439 AD : constant Node_Id :=
11440 Access_To_Subprogram_Definition
11441 (Parameter_Type (Param_Spec));
11443 if Present (AD) and then Protected_Present (AD) then
11445 Replace_Anonymous_Access_To_Protected_Subprogram
11451 Set_Etype (Formal, Formal_Type);
11453 -- Deal with default expression if present
11455 Default := Expression (Param_Spec);
11457 if Present (Default) then
11458 Check_SPARK_Restriction
11459 ("default expression is not allowed", Default);
11461 if Out_Present (Param_Spec) then
11463 ("default initialization only allowed for IN parameters",
11467 -- Do the special preanalysis of the expression (see section on
11468 -- "Handling of Default Expressions" in the spec of package Sem).
11470 Preanalyze_Spec_Expression (Default, Formal_Type);
11472 -- An access to constant cannot be the default for
11473 -- an access parameter that is an access to variable.
11475 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11476 and then not Is_Access_Constant (Formal_Type)
11477 and then Is_Access_Type (Etype (Default))
11478 and then Is_Access_Constant (Etype (Default))
11481 ("formal that is access to variable cannot be initialized " &
11482 "with an access-to-constant expression", Default);
11485 -- Check that the designated type of an access parameter's default
11486 -- is not a class-wide type unless the parameter's designated type
11487 -- is also class-wide.
11489 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11490 and then not Designates_From_Limited_With (Formal_Type)
11491 and then Is_Class_Wide_Default (Default)
11492 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11495 ("access to class-wide expression not allowed here", Default);
11498 -- Check incorrect use of dynamically tagged expressions
11500 if Is_Tagged_Type (Formal_Type) then
11501 Check_Dynamically_Tagged_Expression
11503 Typ => Formal_Type,
11504 Related_Nod => Default);
11508 -- Ada 2005 (AI-231): Static checks
11510 if Ada_Version >= Ada_2005
11511 and then Is_Access_Type (Etype (Formal))
11512 and then Can_Never_Be_Null (Etype (Formal))
11514 Null_Exclusion_Static_Checks (Param_Spec);
11517 -- The following checks are relevant when SPARK_Mode is on as these
11518 -- are not standard Ada legality rules.
11520 if SPARK_Mode = On then
11521 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
11523 -- A function cannot have a parameter of mode IN OUT or OUT
11526 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
11528 ("function cannot have parameter of mode `OUT` or "
11529 & "`IN OUT`", Formal);
11531 -- A function cannot have a volatile formal parameter
11532 -- (SPARK RM 7.1.3(10)).
11534 elsif Is_SPARK_Volatile (Formal) then
11536 ("function cannot have a volatile formal parameter",
11540 -- A procedure cannot have a formal parameter of mode IN because
11541 -- it behaves as a constant (SPARK RM 7.1.3(6)).
11543 elsif Ekind (Scope (Formal)) = E_Procedure
11544 and then Ekind (Formal) = E_In_Parameter
11545 and then Is_SPARK_Volatile (Formal)
11548 ("formal parameter of mode `IN` cannot be volatile", Formal);
11556 -- If this is the formal part of a function specification, analyze the
11557 -- subtype mark in the context where the formals are visible but not
11558 -- yet usable, and may hide outer homographs.
11560 if Nkind (Related_Nod) = N_Function_Specification then
11561 Analyze_Return_Type (Related_Nod);
11564 -- Now set the kind (mode) of each formal
11566 Param_Spec := First (T);
11567 while Present (Param_Spec) loop
11568 Formal := Defining_Identifier (Param_Spec);
11569 Set_Formal_Mode (Formal);
11571 if Ekind (Formal) = E_In_Parameter then
11572 Set_Default_Value (Formal, Expression (Param_Spec));
11574 if Present (Expression (Param_Spec)) then
11575 Default := Expression (Param_Spec);
11577 if Is_Scalar_Type (Etype (Default)) then
11578 if Nkind (Parameter_Type (Param_Spec)) /=
11579 N_Access_Definition
11581 Formal_Type := Entity (Parameter_Type (Param_Spec));
11585 (Related_Nod, Parameter_Type (Param_Spec));
11588 Apply_Scalar_Range_Check (Default, Formal_Type);
11592 elsif Ekind (Formal) = E_Out_Parameter then
11593 Num_Out_Params := Num_Out_Params + 1;
11595 if Num_Out_Params = 1 then
11596 First_Out_Param := Formal;
11599 elsif Ekind (Formal) = E_In_Out_Parameter then
11600 Num_Out_Params := Num_Out_Params + 1;
11603 -- Skip remaining processing if formal type was in error
11605 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11606 goto Next_Parameter;
11609 -- Force call by reference if aliased
11611 if Is_Aliased (Formal) then
11612 Set_Mechanism (Formal, By_Reference);
11614 -- Warn if user asked this to be passed by copy
11616 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11618 ("cannot pass aliased parameter & by copy??", Formal);
11621 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11623 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11624 Set_Mechanism (Formal, By_Copy);
11626 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11627 Set_Mechanism (Formal, By_Reference);
11634 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11635 Set_Is_Only_Out_Parameter (First_Out_Param);
11637 end Process_Formals;
11639 ----------------------------
11640 -- Reference_Body_Formals --
11641 ----------------------------
11643 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11648 if Error_Posted (Spec) then
11652 -- Iterate over both lists. They may be of different lengths if the two
11653 -- specs are not conformant.
11655 Fs := First_Formal (Spec);
11656 Fb := First_Formal (Bod);
11657 while Present (Fs) and then Present (Fb) loop
11658 Generate_Reference (Fs, Fb, 'b');
11660 if Style_Check then
11661 Style.Check_Identifier (Fb, Fs);
11664 Set_Spec_Entity (Fb, Fs);
11665 Set_Referenced (Fs, False);
11669 end Reference_Body_Formals;
11671 -------------------------
11672 -- Set_Actual_Subtypes --
11673 -------------------------
11675 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
11677 Formal : Entity_Id;
11679 First_Stmt : Node_Id := Empty;
11680 AS_Needed : Boolean;
11683 -- If this is an empty initialization procedure, no need to create
11684 -- actual subtypes (small optimization).
11686 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
11690 Formal := First_Formal (Subp);
11691 while Present (Formal) loop
11692 T := Etype (Formal);
11694 -- We never need an actual subtype for a constrained formal
11696 if Is_Constrained (T) then
11697 AS_Needed := False;
11699 -- If we have unknown discriminants, then we do not need an actual
11700 -- subtype, or more accurately we cannot figure it out. Note that
11701 -- all class-wide types have unknown discriminants.
11703 elsif Has_Unknown_Discriminants (T) then
11704 AS_Needed := False;
11706 -- At this stage we have an unconstrained type that may need an
11707 -- actual subtype. For sure the actual subtype is needed if we have
11708 -- an unconstrained array type.
11710 elsif Is_Array_Type (T) then
11713 -- The only other case needing an actual subtype is an unconstrained
11714 -- record type which is an IN parameter (we cannot generate actual
11715 -- subtypes for the OUT or IN OUT case, since an assignment can
11716 -- change the discriminant values. However we exclude the case of
11717 -- initialization procedures, since discriminants are handled very
11718 -- specially in this context, see the section entitled "Handling of
11719 -- Discriminants" in Einfo.
11721 -- We also exclude the case of Discrim_SO_Functions (functions used
11722 -- in front end layout mode for size/offset values), since in such
11723 -- functions only discriminants are referenced, and not only are such
11724 -- subtypes not needed, but they cannot always be generated, because
11725 -- of order of elaboration issues.
11727 elsif Is_Record_Type (T)
11728 and then Ekind (Formal) = E_In_Parameter
11729 and then Chars (Formal) /= Name_uInit
11730 and then not Is_Unchecked_Union (T)
11731 and then not Is_Discrim_SO_Function (Subp)
11735 -- All other cases do not need an actual subtype
11738 AS_Needed := False;
11741 -- Generate actual subtypes for unconstrained arrays and
11742 -- unconstrained discriminated records.
11745 if Nkind (N) = N_Accept_Statement then
11747 -- If expansion is active, the formal is replaced by a local
11748 -- variable that renames the corresponding entry of the
11749 -- parameter block, and it is this local variable that may
11750 -- require an actual subtype.
11752 if Expander_Active then
11753 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11755 Decl := Build_Actual_Subtype (T, Formal);
11758 if Present (Handled_Statement_Sequence (N)) then
11760 First (Statements (Handled_Statement_Sequence (N)));
11761 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11762 Mark_Rewrite_Insertion (Decl);
11764 -- If the accept statement has no body, there will be no
11765 -- reference to the actuals, so no need to compute actual
11772 Decl := Build_Actual_Subtype (T, Formal);
11773 Prepend (Decl, Declarations (N));
11774 Mark_Rewrite_Insertion (Decl);
11777 -- The declaration uses the bounds of an existing object, and
11778 -- therefore needs no constraint checks.
11780 Analyze (Decl, Suppress => All_Checks);
11782 -- We need to freeze manually the generated type when it is
11783 -- inserted anywhere else than in a declarative part.
11785 if Present (First_Stmt) then
11786 Insert_List_Before_And_Analyze (First_Stmt,
11787 Freeze_Entity (Defining_Identifier (Decl), N));
11789 -- Ditto if the type has a dynamic predicate, because the
11790 -- generated function will mention the actual subtype.
11792 elsif Has_Dynamic_Predicate_Aspect (T) then
11793 Insert_List_Before_And_Analyze (Decl,
11794 Freeze_Entity (Defining_Identifier (Decl), N));
11797 if Nkind (N) = N_Accept_Statement
11798 and then Expander_Active
11800 Set_Actual_Subtype (Renamed_Object (Formal),
11801 Defining_Identifier (Decl));
11803 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11807 Next_Formal (Formal);
11809 end Set_Actual_Subtypes;
11811 ---------------------
11812 -- Set_Formal_Mode --
11813 ---------------------
11815 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11816 Spec : constant Node_Id := Parent (Formal_Id);
11819 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11820 -- since we ensure that corresponding actuals are always valid at the
11821 -- point of the call.
11823 if Out_Present (Spec) then
11824 if Ekind (Scope (Formal_Id)) = E_Function
11825 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
11827 -- [IN] OUT parameters allowed for functions in Ada 2012
11829 if Ada_Version >= Ada_2012 then
11831 -- Even in Ada 2012 operators can only have IN parameters
11833 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
11834 Error_Msg_N ("operators can only have IN parameters", Spec);
11837 if In_Present (Spec) then
11838 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11840 Set_Ekind (Formal_Id, E_Out_Parameter);
11843 -- But not in earlier versions of Ada
11846 Error_Msg_N ("functions can only have IN parameters", Spec);
11847 Set_Ekind (Formal_Id, E_In_Parameter);
11850 elsif In_Present (Spec) then
11851 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11854 Set_Ekind (Formal_Id, E_Out_Parameter);
11855 Set_Never_Set_In_Source (Formal_Id, True);
11856 Set_Is_True_Constant (Formal_Id, False);
11857 Set_Current_Value (Formal_Id, Empty);
11861 Set_Ekind (Formal_Id, E_In_Parameter);
11864 -- Set Is_Known_Non_Null for access parameters since the language
11865 -- guarantees that access parameters are always non-null. We also set
11866 -- Can_Never_Be_Null, since there is no way to change the value.
11868 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
11870 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
11871 -- null; In Ada 2005, only if then null_exclusion is explicit.
11873 if Ada_Version < Ada_2005
11874 or else Can_Never_Be_Null (Etype (Formal_Id))
11876 Set_Is_Known_Non_Null (Formal_Id);
11877 Set_Can_Never_Be_Null (Formal_Id);
11880 -- Ada 2005 (AI-231): Null-exclusion access subtype
11882 elsif Is_Access_Type (Etype (Formal_Id))
11883 and then Can_Never_Be_Null (Etype (Formal_Id))
11885 Set_Is_Known_Non_Null (Formal_Id);
11887 -- We can also set Can_Never_Be_Null (thus preventing some junk
11888 -- access checks) for the case of an IN parameter, which cannot
11889 -- be changed, or for an IN OUT parameter, which can be changed but
11890 -- not to a null value. But for an OUT parameter, the initial value
11891 -- passed in can be null, so we can't set this flag in that case.
11893 if Ekind (Formal_Id) /= E_Out_Parameter then
11894 Set_Can_Never_Be_Null (Formal_Id);
11898 Set_Mechanism (Formal_Id, Default_Mechanism);
11899 Set_Formal_Validity (Formal_Id);
11900 end Set_Formal_Mode;
11902 -------------------------
11903 -- Set_Formal_Validity --
11904 -------------------------
11906 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
11908 -- If no validity checking, then we cannot assume anything about the
11909 -- validity of parameters, since we do not know there is any checking
11910 -- of the validity on the call side.
11912 if not Validity_Checks_On then
11915 -- If validity checking for parameters is enabled, this means we are
11916 -- not supposed to make any assumptions about argument values.
11918 elsif Validity_Check_Parameters then
11921 -- If we are checking in parameters, we will assume that the caller is
11922 -- also checking parameters, so we can assume the parameter is valid.
11924 elsif Ekind (Formal_Id) = E_In_Parameter
11925 and then Validity_Check_In_Params
11927 Set_Is_Known_Valid (Formal_Id, True);
11929 -- Similar treatment for IN OUT parameters
11931 elsif Ekind (Formal_Id) = E_In_Out_Parameter
11932 and then Validity_Check_In_Out_Params
11934 Set_Is_Known_Valid (Formal_Id, True);
11936 end Set_Formal_Validity;
11938 ------------------------
11939 -- Subtype_Conformant --
11940 ------------------------
11942 function Subtype_Conformant
11943 (New_Id : Entity_Id;
11944 Old_Id : Entity_Id;
11945 Skip_Controlling_Formals : Boolean := False) return Boolean
11949 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
11950 Skip_Controlling_Formals => Skip_Controlling_Formals);
11952 end Subtype_Conformant;
11954 ---------------------
11955 -- Type_Conformant --
11956 ---------------------
11958 function Type_Conformant
11959 (New_Id : Entity_Id;
11960 Old_Id : Entity_Id;
11961 Skip_Controlling_Formals : Boolean := False) return Boolean
11965 May_Hide_Profile := False;
11968 (New_Id, Old_Id, Type_Conformant, False, Result,
11969 Skip_Controlling_Formals => Skip_Controlling_Formals);
11971 end Type_Conformant;
11973 -------------------------------
11974 -- Valid_Operator_Definition --
11975 -------------------------------
11977 procedure Valid_Operator_Definition (Designator : Entity_Id) is
11980 Id : constant Name_Id := Chars (Designator);
11984 F := First_Formal (Designator);
11985 while Present (F) loop
11988 if Present (Default_Value (F)) then
11990 ("default values not allowed for operator parameters",
11997 -- Verify that user-defined operators have proper number of arguments
11998 -- First case of operators which can only be unary
12000 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12003 -- Case of operators which can be unary or binary
12005 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12006 N_OK := (N in 1 .. 2);
12008 -- All other operators can only be binary
12016 ("incorrect number of arguments for operator", Designator);
12020 and then Base_Type (Etype (Designator)) = Standard_Boolean
12021 and then not Is_Intrinsic_Subprogram (Designator)
12024 ("explicit definition of inequality not allowed", Designator);
12026 end Valid_Operator_Definition;