1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, 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 Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Layout; use Layout;
44 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
49 with Output; use Output;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch5; use Sem_Ch5;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Util; use Sem_Util;
71 with Sem_Type; use Sem_Type;
72 with Sem_Warn; use Sem_Warn;
73 with Sinput; use Sinput;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinfo.CN; use Sinfo.CN;
77 with Snames; use Snames;
78 with Stringt; use Stringt;
80 with Stylesw; use Stylesw;
81 with Tbuild; use Tbuild;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
84 with Validsw; use Validsw;
86 package body Sem_Ch6 is
88 May_Hide_Profile : Boolean := False;
89 -- This flag is used to indicate that two formals in two subprograms being
90 -- checked for conformance differ only in that one is an access parameter
91 -- while the other is of a general access type with the same designated
92 -- type. In this case, if the rest of the signatures match, a call to
93 -- either subprogram may be ambiguous, which is worth a warning. The flag
94 -- is set in Compatible_Types, and the warning emitted in
95 -- New_Overloaded_Entity.
97 -----------------------
98 -- Local Subprograms --
99 -----------------------
101 procedure Analyze_Return_Statement (N : Node_Id);
102 -- Common processing for simple and extended return statements
104 procedure Analyze_Function_Return (N : Node_Id);
105 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
106 -- applies to a [generic] function.
108 procedure Analyze_Return_Type (N : Node_Id);
109 -- Subsidiary to Process_Formals: analyze subtype mark in function
110 -- specification in a context where the formals are visible and hide
113 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
114 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
115 -- that we can use RETURN but not skip the debug output at the end.
117 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
118 -- Analyze a generic subprogram body. N is the body to be analyzed, and
119 -- Gen_Id is the defining entity Id for the corresponding spec.
121 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
122 -- If a subprogram has pragma Inline and inlining is active, use generic
123 -- machinery to build an unexpanded body for the subprogram. This body is
124 -- subsequently used for inline expansions at call sites. If subprogram can
125 -- be inlined (depending on size and nature of local declarations) this
126 -- function returns true. Otherwise subprogram body is treated normally.
127 -- If proper warnings are enabled and the subprogram contains a construct
128 -- that cannot be inlined, the offending construct is flagged accordingly.
130 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
131 -- Returns true if Subp can override a predefined operator.
133 procedure Check_Conformance
136 Ctype : Conformance_Type;
138 Conforms : out Boolean;
139 Err_Loc : Node_Id := Empty;
140 Get_Inst : Boolean := False;
141 Skip_Controlling_Formals : Boolean := False);
142 -- Given two entities, this procedure checks that the profiles associated
143 -- with these entities meet the conformance criterion given by the third
144 -- parameter. If they conform, Conforms is set True and control returns
145 -- to the caller. If they do not conform, Conforms is set to False, and
146 -- in addition, if Errmsg is True on the call, proper messages are output
147 -- to complain about the conformance failure. If Err_Loc is non_Empty
148 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
149 -- error messages are placed on the appropriate part of the construct
150 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
151 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 procedure Check_Subprogram_Order (N : Node_Id);
155 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
156 -- the alpha ordering rule for N if this ordering requirement applicable.
158 procedure Check_Returns
162 Proc : Entity_Id := Empty);
163 -- Called to check for missing return statements in a function body, or for
164 -- returns present in a procedure body which has No_Return set. HSS is the
165 -- handled statement sequence for the subprogram body. This procedure
166 -- checks all flow paths to make sure they either have return (Mode = 'F',
167 -- used for functions) or do not have a return (Mode = 'P', used for
168 -- No_Return procedures). The flag Err is set if there are any control
169 -- paths not explicitly terminated by a return in the function case, and is
170 -- True otherwise. Proc is the entity for the procedure case and is used
171 -- in posting the warning message.
173 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
174 -- In Ada 2012, a primitive equality operator on an untagged record type
175 -- must appear before the type is frozen, and have the same visibility as
176 -- that of the type. This procedure checks that this rule is met, and
177 -- otherwise emits an error on the subprogram declaration and a warning
178 -- on the earlier freeze point if it is easy to locate.
180 procedure Enter_Overloaded_Entity (S : Entity_Id);
181 -- This procedure makes S, a new overloaded entity, into the first visible
182 -- entity with that name.
184 procedure Install_Entity (E : Entity_Id);
185 -- Make single entity visible (used for generic formals as well)
187 function Is_Non_Overriding_Operation
189 New_E : Entity_Id) return Boolean;
190 -- Enforce the rule given in 12.3(18): a private operation in an instance
191 -- overrides an inherited operation only if the corresponding operation
192 -- was overriding in the generic. This can happen for primitive operations
193 -- of types derived (in the generic unit) from formal private or formal
196 procedure Make_Inequality_Operator (S : Entity_Id);
197 -- Create the declaration for an inequality operator that is implicitly
198 -- created by a user-defined equality operator that yields a boolean.
200 procedure May_Need_Actuals (Fun : Entity_Id);
201 -- Flag functions that can be called without parameters, i.e. those that
202 -- have no parameters, or those for which defaults exist for all parameters
204 procedure Process_PPCs
207 Body_Id : Entity_Id);
208 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
209 -- conditions for the body and assembling and inserting the _postconditions
210 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
211 -- the entities for the body and separate spec (if there is no separate
212 -- spec, Spec_Id is Empty). Note that invariants and predicates may also
213 -- provide postconditions, and are also handled in this procedure.
215 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
216 -- Formal_Id is an formal parameter entity. This procedure deals with
217 -- setting the proper validity status for this entity, which depends on
218 -- the kind of parameter and the validity checking mode.
220 ---------------------------------------------
221 -- Analyze_Abstract_Subprogram_Declaration --
222 ---------------------------------------------
224 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
225 Designator : constant Entity_Id :=
226 Analyze_Subprogram_Specification (Specification (N));
227 Scop : constant Entity_Id := Current_Scope;
230 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
232 Generate_Definition (Designator);
233 Set_Is_Abstract_Subprogram (Designator);
234 New_Overloaded_Entity (Designator);
235 Check_Delayed_Subprogram (Designator);
237 Set_Categorization_From_Scope (Designator, Scop);
239 if Ekind (Scope (Designator)) = E_Protected_Type then
241 ("abstract subprogram not allowed in protected type", N);
243 -- Issue a warning if the abstract subprogram is neither a dispatching
244 -- operation nor an operation that overrides an inherited subprogram or
245 -- predefined operator, since this most likely indicates a mistake.
247 elsif Warn_On_Redundant_Constructs
248 and then not Is_Dispatching_Operation (Designator)
249 and then not Present (Overridden_Operation (Designator))
250 and then (not Is_Operator_Symbol_Name (Chars (Designator))
251 or else Scop /= Scope (Etype (First_Formal (Designator))))
254 ("?abstract subprogram is not dispatching or overriding", N);
257 Generate_Reference_To_Formals (Designator);
258 Check_Eliminated (Designator);
260 if Has_Aspects (N) then
261 Analyze_Aspect_Specifications (N, Designator);
263 end Analyze_Abstract_Subprogram_Declaration;
265 ---------------------------------
266 -- Analyze_Expression_Function --
267 ---------------------------------
269 procedure Analyze_Expression_Function (N : Node_Id) is
270 Loc : constant Source_Ptr := Sloc (N);
271 LocX : constant Source_Ptr := Sloc (Expression (N));
272 Def_Id : constant Entity_Id := Defining_Entity (Specification (N));
276 Prev : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
277 -- If the expression is a completion, Prev is the entity whose
278 -- declaration is completed.
281 -- This is one of the occasions on which we transform the tree during
282 -- semantic analysis. If this is a completion, transform the expression
283 -- function into an equivalent subprogram body, and analyze it.
285 -- Expression functions are inlined unconditionally. The back-end will
286 -- determine whether this is possible.
288 Inline_Processing_Required := True;
291 Make_Subprogram_Body (Loc,
292 Specification => Specification (N),
293 Declarations => Empty_List,
294 Handled_Statement_Sequence =>
295 Make_Handled_Sequence_Of_Statements (LocX,
296 Statements => New_List (
297 Make_Simple_Return_Statement (LocX,
298 Expression => Expression (N)))));
301 and then Ekind (Prev) = E_Generic_Function
303 -- If the expression completes a generic subprogram, we must create a
304 -- separate node for the body, because at instantiation the original
305 -- node of the generic copy must be a generic subprogram body, and
306 -- cannot be a expression function. Otherwise we just rewrite the
307 -- expression with the non-generic body.
309 Insert_After (N, New_Body);
310 Rewrite (N, Make_Null_Statement (Loc));
313 Set_Is_Inlined (Prev);
315 elsif Present (Prev) then
316 Rewrite (N, New_Body);
317 Set_Is_Inlined (Prev);
320 -- If this is not a completion, create both a declaration and a body,
321 -- so that the expression can be inlined whenever possible.
325 Make_Subprogram_Declaration (Loc,
326 Specification => Specification (N));
327 Rewrite (N, New_Decl);
329 Set_Is_Inlined (Defining_Entity (New_Decl));
331 -- Create new set of formals for specification in body.
333 Set_Specification (New_Body,
334 Make_Function_Specification (Loc,
335 Defining_Unit_Name =>
336 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))),
337 Parameter_Specifications =>
338 Copy_Parameter_List (Defining_Entity (New_Decl)),
340 New_Copy_Tree (Result_Definition (Specification (New_Decl)))));
342 Insert_After (N, New_Body);
345 end Analyze_Expression_Function;
347 ----------------------------------------
348 -- Analyze_Extended_Return_Statement --
349 ----------------------------------------
351 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
353 Analyze_Return_Statement (N);
354 end Analyze_Extended_Return_Statement;
356 ----------------------------
357 -- Analyze_Function_Call --
358 ----------------------------
360 procedure Analyze_Function_Call (N : Node_Id) is
361 P : constant Node_Id := Name (N);
362 Actuals : constant List_Id := Parameter_Associations (N);
368 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
369 -- as B (A, X). If the rewriting is successful, the call has been
370 -- analyzed and we just return.
372 if Nkind (P) = N_Selected_Component
373 and then Name (N) /= P
374 and then Is_Rewrite_Substitution (N)
375 and then Present (Etype (N))
380 -- If error analyzing name, then set Any_Type as result type and return
382 if Etype (P) = Any_Type then
383 Set_Etype (N, Any_Type);
387 -- Otherwise analyze the parameters
389 if Present (Actuals) then
390 Actual := First (Actuals);
391 while Present (Actual) loop
393 Check_Parameterless_Call (Actual);
399 end Analyze_Function_Call;
401 -----------------------------
402 -- Analyze_Function_Return --
403 -----------------------------
405 procedure Analyze_Function_Return (N : Node_Id) is
406 Loc : constant Source_Ptr := Sloc (N);
407 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
408 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
410 R_Type : constant Entity_Id := Etype (Scope_Id);
411 -- Function result subtype
413 procedure Check_Limited_Return (Expr : Node_Id);
414 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
415 -- limited types. Used only for simple return statements.
416 -- Expr is the expression returned.
418 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
419 -- Check that the return_subtype_indication properly matches the result
420 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
422 --------------------------
423 -- Check_Limited_Return --
424 --------------------------
426 procedure Check_Limited_Return (Expr : Node_Id) is
428 -- Ada 2005 (AI-318-02): Return-by-reference types have been
429 -- removed and replaced by anonymous access results. This is an
430 -- incompatibility with Ada 95. Not clear whether this should be
431 -- enforced yet or perhaps controllable with special switch. ???
433 if Is_Limited_Type (R_Type)
434 and then Comes_From_Source (N)
435 and then not In_Instance_Body
436 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
440 if Ada_Version >= Ada_2005
441 and then not Debug_Flag_Dot_L
442 and then not GNAT_Mode
445 ("(Ada 2005) cannot copy object of a limited type " &
446 "(RM-2005 6.5(5.5/2))", Expr);
448 if Is_Immutably_Limited_Type (R_Type) then
450 ("\return by reference not permitted in Ada 2005", Expr);
453 -- Warn in Ada 95 mode, to give folks a heads up about this
456 -- In GNAT mode, this is just a warning, to allow it to be
457 -- evilly turned off. Otherwise it is a real error.
459 -- In a generic context, simplify the warning because it makes
460 -- no sense to discuss pass-by-reference or copy.
462 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
463 if Inside_A_Generic then
465 ("return of limited object not permitted in Ada2005 "
466 & "(RM-2005 6.5(5.5/2))?", Expr);
468 elsif Is_Immutably_Limited_Type (R_Type) then
470 ("return by reference not permitted in Ada 2005 "
471 & "(RM-2005 6.5(5.5/2))?", Expr);
474 ("cannot copy object of a limited type in Ada 2005 "
475 & "(RM-2005 6.5(5.5/2))?", Expr);
478 -- Ada 95 mode, compatibility warnings disabled
481 return; -- skip continuation messages below
484 if not Inside_A_Generic then
486 ("\consider switching to return of access type", Expr);
487 Explain_Limited_Type (R_Type, Expr);
490 end Check_Limited_Return;
492 -------------------------------------
493 -- Check_Return_Subtype_Indication --
494 -------------------------------------
496 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
497 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
499 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
500 -- Subtype given in the extended return statement (must match R_Type)
502 Subtype_Ind : constant Node_Id :=
503 Object_Definition (Original_Node (Obj_Decl));
505 R_Type_Is_Anon_Access :
507 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
509 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
511 Ekind (R_Type) = E_Anonymous_Access_Type;
512 -- True if return type of the function is an anonymous access type
513 -- Can't we make Is_Anonymous_Access_Type in einfo ???
515 R_Stm_Type_Is_Anon_Access :
517 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
519 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
521 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
522 -- True if type of the return object is an anonymous access type
525 -- First, avoid cascaded errors
527 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
531 -- "return access T" case; check that the return statement also has
532 -- "access T", and that the subtypes statically match:
533 -- if this is an access to subprogram the signatures must match.
535 if R_Type_Is_Anon_Access then
536 if R_Stm_Type_Is_Anon_Access then
538 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
540 if Base_Type (Designated_Type (R_Stm_Type)) /=
541 Base_Type (Designated_Type (R_Type))
542 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
545 ("subtype must statically match function result subtype",
546 Subtype_Mark (Subtype_Ind));
550 -- For two anonymous access to subprogram types, the
551 -- types themselves must be type conformant.
553 if not Conforming_Types
554 (R_Stm_Type, R_Type, Fully_Conformant)
557 ("subtype must statically match function result subtype",
563 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
566 -- Subtype indication case: check that the return object's type is
567 -- covered by the result type, and that the subtypes statically match
568 -- when the result subtype is constrained. Also handle record types
569 -- with unknown discriminants for which we have built the underlying
570 -- record view. Coverage is needed to allow specific-type return
571 -- objects when the result type is class-wide (see AI05-32).
573 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
574 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
578 Underlying_Record_View (Base_Type (R_Stm_Type))))
580 -- A null exclusion may be present on the return type, on the
581 -- function specification, on the object declaration or on the
584 if Is_Access_Type (R_Type)
586 (Can_Never_Be_Null (R_Type)
587 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
588 Can_Never_Be_Null (R_Stm_Type)
591 ("subtype must statically match function result subtype",
595 -- AI05-103: for elementary types, subtypes must statically match
597 if Is_Constrained (R_Type)
598 or else Is_Access_Type (R_Type)
600 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
602 ("subtype must statically match function result subtype",
607 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
608 and then Is_Null_Extension (Base_Type (R_Type))
614 ("wrong type for return_subtype_indication", Subtype_Ind);
616 end Check_Return_Subtype_Indication;
618 ---------------------
619 -- Local Variables --
620 ---------------------
624 -- Start of processing for Analyze_Function_Return
627 Set_Return_Present (Scope_Id);
629 if Nkind (N) = N_Simple_Return_Statement then
630 Expr := Expression (N);
632 -- Guard against a malformed expression. The parser may have tried to
633 -- recover but the node is not analyzable.
635 if Nkind (Expr) = N_Error then
636 Set_Etype (Expr, Any_Type);
637 Expander_Mode_Save_And_Set (False);
641 Analyze_And_Resolve (Expr, R_Type);
642 Check_Limited_Return (Expr);
645 -- RETURN only allowed in SPARK is as the last statement function
647 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
649 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
650 or else Present (Next (N)))
652 Mark_Non_ALFA_Subprogram
653 ("RETURN should be the last statement in ALFA", N);
654 Check_SPARK_Restriction
655 ("RETURN should be the last statement in function", N);
659 Mark_Non_ALFA_Subprogram ("extended RETURN is not in ALFA", N);
660 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
662 -- Analyze parts specific to extended_return_statement:
665 Obj_Decl : constant Node_Id :=
666 Last (Return_Object_Declarations (N));
668 HSS : constant Node_Id := Handled_Statement_Sequence (N);
671 Expr := Expression (Obj_Decl);
673 -- Note: The check for OK_For_Limited_Init will happen in
674 -- Analyze_Object_Declaration; we treat it as a normal
675 -- object declaration.
677 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
680 Check_Return_Subtype_Indication (Obj_Decl);
682 if Present (HSS) then
685 if Present (Exception_Handlers (HSS)) then
687 -- ???Has_Nested_Block_With_Handler needs to be set.
688 -- Probably by creating an actual N_Block_Statement.
689 -- Probably in Expand.
695 -- Mark the return object as referenced, since the return is an
696 -- implicit reference of the object.
698 Set_Referenced (Defining_Identifier (Obj_Decl));
700 Check_References (Stm_Entity);
704 -- Case of Expr present
708 -- Defend against previous errors
710 and then Nkind (Expr) /= N_Empty
711 and then Present (Etype (Expr))
713 -- Apply constraint check. Note that this is done before the implicit
714 -- conversion of the expression done for anonymous access types to
715 -- ensure correct generation of the null-excluding check associated
716 -- with null-excluding expressions found in return statements.
718 Apply_Constraint_Check (Expr, R_Type);
720 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
721 -- type, apply an implicit conversion of the expression to that type
722 -- to force appropriate static and run-time accessibility checks.
724 if Ada_Version >= Ada_2005
725 and then Ekind (R_Type) = E_Anonymous_Access_Type
727 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
728 Analyze_And_Resolve (Expr, R_Type);
731 -- If the result type is class-wide, then check that the return
732 -- expression's type is not declared at a deeper level than the
733 -- function (RM05-6.5(5.6/2)).
735 if Ada_Version >= Ada_2005
736 and then Is_Class_Wide_Type (R_Type)
738 if Type_Access_Level (Etype (Expr)) >
739 Subprogram_Access_Level (Scope_Id)
742 ("level of return expression type is deeper than " &
743 "class-wide function!", Expr);
747 -- Check incorrect use of dynamically tagged expression
749 if Is_Tagged_Type (R_Type) then
750 Check_Dynamically_Tagged_Expression
756 -- ??? A real run-time accessibility check is needed in cases
757 -- involving dereferences of access parameters. For now we just
758 -- check the static cases.
760 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
761 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
762 and then Object_Access_Level (Expr) >
763 Subprogram_Access_Level (Scope_Id)
766 -- Suppress the message in a generic, where the rewriting
769 if Inside_A_Generic then
774 Make_Raise_Program_Error (Loc,
775 Reason => PE_Accessibility_Check_Failed));
779 ("cannot return a local value by reference?", N);
781 ("\& will be raised at run time?",
782 N, Standard_Program_Error);
787 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
788 and then Null_Exclusion_Present (Parent (Scope_Id))
790 Apply_Compile_Time_Constraint_Error
792 Msg => "(Ada 2005) null not allowed for "
793 & "null-excluding return?",
794 Reason => CE_Null_Not_Allowed);
797 -- Apply checks suggested by AI05-0144 (dangerous order dependence)
799 Check_Order_Dependence;
801 end Analyze_Function_Return;
803 -------------------------------------
804 -- Analyze_Generic_Subprogram_Body --
805 -------------------------------------
807 procedure Analyze_Generic_Subprogram_Body
811 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
812 Kind : constant Entity_Kind := Ekind (Gen_Id);
818 -- Copy body and disable expansion while analyzing the generic For a
819 -- stub, do not copy the stub (which would load the proper body), this
820 -- will be done when the proper body is analyzed.
822 if Nkind (N) /= N_Subprogram_Body_Stub then
823 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
828 Spec := Specification (N);
830 -- Within the body of the generic, the subprogram is callable, and
831 -- behaves like the corresponding non-generic unit.
833 Body_Id := Defining_Entity (Spec);
835 if Kind = E_Generic_Procedure
836 and then Nkind (Spec) /= N_Procedure_Specification
838 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
841 elsif Kind = E_Generic_Function
842 and then Nkind (Spec) /= N_Function_Specification
844 Error_Msg_N ("invalid body for generic function ", Body_Id);
848 Set_Corresponding_Body (Gen_Decl, Body_Id);
850 if Has_Completion (Gen_Id)
851 and then Nkind (Parent (N)) /= N_Subunit
853 Error_Msg_N ("duplicate generic body", N);
856 Set_Has_Completion (Gen_Id);
859 if Nkind (N) = N_Subprogram_Body_Stub then
860 Set_Ekind (Defining_Entity (Specification (N)), Kind);
862 Set_Corresponding_Spec (N, Gen_Id);
865 if Nkind (Parent (N)) = N_Compilation_Unit then
866 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
869 -- Make generic parameters immediately visible in the body. They are
870 -- needed to process the formals declarations. Then make the formals
871 -- visible in a separate step.
877 First_Ent : Entity_Id;
880 First_Ent := First_Entity (Gen_Id);
883 while Present (E) and then not Is_Formal (E) loop
888 Set_Use (Generic_Formal_Declarations (Gen_Decl));
890 -- Now generic formals are visible, and the specification can be
891 -- analyzed, for subsequent conformance check.
893 Body_Id := Analyze_Subprogram_Specification (Spec);
895 -- Make formal parameters visible
899 -- E is the first formal parameter, we loop through the formals
900 -- installing them so that they will be visible.
902 Set_First_Entity (Gen_Id, E);
903 while Present (E) loop
909 -- Visible generic entity is callable within its own body
911 Set_Ekind (Gen_Id, Ekind (Body_Id));
912 Set_Ekind (Body_Id, E_Subprogram_Body);
913 Set_Convention (Body_Id, Convention (Gen_Id));
914 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
915 Set_Scope (Body_Id, Scope (Gen_Id));
916 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
918 if Nkind (N) = N_Subprogram_Body_Stub then
920 -- No body to analyze, so restore state of generic unit
922 Set_Ekind (Gen_Id, Kind);
923 Set_Ekind (Body_Id, Kind);
925 if Present (First_Ent) then
926 Set_First_Entity (Gen_Id, First_Ent);
933 -- If this is a compilation unit, it must be made visible explicitly,
934 -- because the compilation of the declaration, unlike other library
935 -- unit declarations, does not. If it is not a unit, the following
936 -- is redundant but harmless.
938 Set_Is_Immediately_Visible (Gen_Id);
939 Reference_Body_Formals (Gen_Id, Body_Id);
941 if Is_Child_Unit (Gen_Id) then
942 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
945 Set_Actual_Subtypes (N, Current_Scope);
946 Process_PPCs (N, Gen_Id, Body_Id);
948 -- If the generic unit carries pre- or post-conditions, copy them
949 -- to the original generic tree, so that they are properly added
950 -- to any instantiation.
953 Orig : constant Node_Id := Original_Node (N);
957 Cond := First (Declarations (N));
958 while Present (Cond) loop
959 if Nkind (Cond) = N_Pragma
960 and then Pragma_Name (Cond) = Name_Check
962 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
964 elsif Nkind (Cond) = N_Pragma
965 and then Pragma_Name (Cond) = Name_Postcondition
967 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
968 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
977 Analyze_Declarations (Declarations (N));
979 Analyze (Handled_Statement_Sequence (N));
981 Save_Global_References (Original_Node (N));
983 -- Prior to exiting the scope, include generic formals again (if any
984 -- are present) in the set of local entities.
986 if Present (First_Ent) then
987 Set_First_Entity (Gen_Id, First_Ent);
990 Check_References (Gen_Id);
993 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
995 Check_Subprogram_Order (N);
997 -- Outside of its body, unit is generic again
999 Set_Ekind (Gen_Id, Kind);
1000 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1003 Style.Check_Identifier (Body_Id, Gen_Id);
1007 end Analyze_Generic_Subprogram_Body;
1009 -----------------------------
1010 -- Analyze_Operator_Symbol --
1011 -----------------------------
1013 -- An operator symbol such as "+" or "and" may appear in context where the
1014 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1015 -- is just a string, as in (conjunction = "or"). In these cases the parser
1016 -- generates this node, and the semantics does the disambiguation. Other
1017 -- such case are actuals in an instantiation, the generic unit in an
1018 -- instantiation, and pragma arguments.
1020 procedure Analyze_Operator_Symbol (N : Node_Id) is
1021 Par : constant Node_Id := Parent (N);
1024 if (Nkind (Par) = N_Function_Call
1025 and then N = Name (Par))
1026 or else Nkind (Par) = N_Function_Instantiation
1027 or else (Nkind (Par) = N_Indexed_Component
1028 and then N = Prefix (Par))
1029 or else (Nkind (Par) = N_Pragma_Argument_Association
1030 and then not Is_Pragma_String_Literal (Par))
1031 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1032 or else (Nkind (Par) = N_Attribute_Reference
1033 and then Attribute_Name (Par) /= Name_Value)
1035 Find_Direct_Name (N);
1038 Change_Operator_Symbol_To_String_Literal (N);
1041 end Analyze_Operator_Symbol;
1043 -----------------------------------
1044 -- Analyze_Parameter_Association --
1045 -----------------------------------
1047 procedure Analyze_Parameter_Association (N : Node_Id) is
1049 Analyze (Explicit_Actual_Parameter (N));
1050 end Analyze_Parameter_Association;
1052 ----------------------------
1053 -- Analyze_Procedure_Call --
1054 ----------------------------
1056 procedure Analyze_Procedure_Call (N : Node_Id) is
1057 Loc : constant Source_Ptr := Sloc (N);
1058 P : constant Node_Id := Name (N);
1059 Actuals : constant List_Id := Parameter_Associations (N);
1063 procedure Analyze_Call_And_Resolve;
1064 -- Do Analyze and Resolve calls for procedure call
1065 -- At end, check illegal order dependence.
1067 ------------------------------
1068 -- Analyze_Call_And_Resolve --
1069 ------------------------------
1071 procedure Analyze_Call_And_Resolve is
1073 if Nkind (N) = N_Procedure_Call_Statement then
1075 Resolve (N, Standard_Void_Type);
1077 -- Apply checks suggested by AI05-0144
1079 Check_Order_Dependence;
1084 end Analyze_Call_And_Resolve;
1086 -- Start of processing for Analyze_Procedure_Call
1089 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1090 -- a procedure call or an entry call. The prefix may denote an access
1091 -- to subprogram type, in which case an implicit dereference applies.
1092 -- If the prefix is an indexed component (without implicit dereference)
1093 -- then the construct denotes a call to a member of an entire family.
1094 -- If the prefix is a simple name, it may still denote a call to a
1095 -- parameterless member of an entry family. Resolution of these various
1096 -- interpretations is delicate.
1100 -- If this is a call of the form Obj.Op, the call may have been
1101 -- analyzed and possibly rewritten into a block, in which case
1104 if Analyzed (N) then
1108 -- If there is an error analyzing the name (which may have been
1109 -- rewritten if the original call was in prefix notation) then error
1110 -- has been emitted already, mark node and return.
1113 or else Etype (Name (N)) = Any_Type
1115 Set_Etype (N, Any_Type);
1119 -- Otherwise analyze the parameters
1121 if Present (Actuals) then
1122 Actual := First (Actuals);
1124 while Present (Actual) loop
1126 Check_Parameterless_Call (Actual);
1131 -- Special processing for Elab_Spec and Elab_Body calls
1133 if Nkind (P) = N_Attribute_Reference
1134 and then (Attribute_Name (P) = Name_Elab_Spec
1135 or else Attribute_Name (P) = Name_Elab_Body)
1137 if Present (Actuals) then
1139 ("no parameters allowed for this call", First (Actuals));
1143 Set_Etype (N, Standard_Void_Type);
1146 elsif Is_Entity_Name (P)
1147 and then Is_Record_Type (Etype (Entity (P)))
1148 and then Remote_AST_I_Dereference (P)
1152 elsif Is_Entity_Name (P)
1153 and then Ekind (Entity (P)) /= E_Entry_Family
1155 if Is_Access_Type (Etype (P))
1156 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1157 and then No (Actuals)
1158 and then Comes_From_Source (N)
1160 Error_Msg_N ("missing explicit dereference in call", N);
1163 Analyze_Call_And_Resolve;
1165 -- If the prefix is the simple name of an entry family, this is
1166 -- a parameterless call from within the task body itself.
1168 elsif Is_Entity_Name (P)
1169 and then Nkind (P) = N_Identifier
1170 and then Ekind (Entity (P)) = E_Entry_Family
1171 and then Present (Actuals)
1172 and then No (Next (First (Actuals)))
1174 -- Can be call to parameterless entry family. What appears to be the
1175 -- sole argument is in fact the entry index. Rewrite prefix of node
1176 -- accordingly. Source representation is unchanged by this
1180 Make_Indexed_Component (Loc,
1182 Make_Selected_Component (Loc,
1183 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1184 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1185 Expressions => Actuals);
1186 Set_Name (N, New_N);
1187 Set_Etype (New_N, Standard_Void_Type);
1188 Set_Parameter_Associations (N, No_List);
1189 Analyze_Call_And_Resolve;
1191 elsif Nkind (P) = N_Explicit_Dereference then
1192 if Ekind (Etype (P)) = E_Subprogram_Type then
1193 Analyze_Call_And_Resolve;
1195 Error_Msg_N ("expect access to procedure in call", P);
1198 -- The name can be a selected component or an indexed component that
1199 -- yields an access to subprogram. Such a prefix is legal if the call
1200 -- has parameter associations.
1202 elsif Is_Access_Type (Etype (P))
1203 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1205 if Present (Actuals) then
1206 Analyze_Call_And_Resolve;
1208 Error_Msg_N ("missing explicit dereference in call ", N);
1211 -- If not an access to subprogram, then the prefix must resolve to the
1212 -- name of an entry, entry family, or protected operation.
1214 -- For the case of a simple entry call, P is a selected component where
1215 -- the prefix is the task and the selector name is the entry. A call to
1216 -- a protected procedure will have the same syntax. If the protected
1217 -- object contains overloaded operations, the entity may appear as a
1218 -- function, the context will select the operation whose type is Void.
1220 elsif Nkind (P) = N_Selected_Component
1221 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1223 Ekind (Entity (Selector_Name (P))) = E_Procedure
1225 Ekind (Entity (Selector_Name (P))) = E_Function)
1227 Analyze_Call_And_Resolve;
1229 elsif Nkind (P) = N_Selected_Component
1230 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1231 and then Present (Actuals)
1232 and then No (Next (First (Actuals)))
1234 -- Can be call to parameterless entry family. What appears to be the
1235 -- sole argument is in fact the entry index. Rewrite prefix of node
1236 -- accordingly. Source representation is unchanged by this
1240 Make_Indexed_Component (Loc,
1241 Prefix => New_Copy (P),
1242 Expressions => Actuals);
1243 Set_Name (N, New_N);
1244 Set_Etype (New_N, Standard_Void_Type);
1245 Set_Parameter_Associations (N, No_List);
1246 Analyze_Call_And_Resolve;
1248 -- For the case of a reference to an element of an entry family, P is
1249 -- an indexed component whose prefix is a selected component (task and
1250 -- entry family), and whose index is the entry family index.
1252 elsif Nkind (P) = N_Indexed_Component
1253 and then Nkind (Prefix (P)) = N_Selected_Component
1254 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1256 Analyze_Call_And_Resolve;
1258 -- If the prefix is the name of an entry family, it is a call from
1259 -- within the task body itself.
1261 elsif Nkind (P) = N_Indexed_Component
1262 and then Nkind (Prefix (P)) = N_Identifier
1263 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1266 Make_Selected_Component (Loc,
1267 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1268 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1269 Rewrite (Prefix (P), New_N);
1271 Analyze_Call_And_Resolve;
1273 -- Anything else is an error
1276 Error_Msg_N ("invalid procedure or entry call", N);
1278 end Analyze_Procedure_Call;
1280 ------------------------------
1281 -- Analyze_Return_Statement --
1282 ------------------------------
1284 procedure Analyze_Return_Statement (N : Node_Id) is
1286 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1287 N_Extended_Return_Statement));
1289 Returns_Object : constant Boolean :=
1290 Nkind (N) = N_Extended_Return_Statement
1292 (Nkind (N) = N_Simple_Return_Statement
1293 and then Present (Expression (N)));
1294 -- True if we're returning something; that is, "return <expression>;"
1295 -- or "return Result : T [:= ...]". False for "return;". Used for error
1296 -- checking: If Returns_Object is True, N should apply to a function
1297 -- body; otherwise N should apply to a procedure body, entry body,
1298 -- accept statement, or extended return statement.
1300 function Find_What_It_Applies_To return Entity_Id;
1301 -- Find the entity representing the innermost enclosing body, accept
1302 -- statement, or extended return statement. If the result is a callable
1303 -- construct or extended return statement, then this will be the value
1304 -- of the Return_Applies_To attribute. Otherwise, the program is
1305 -- illegal. See RM-6.5(4/2).
1307 -----------------------------
1308 -- Find_What_It_Applies_To --
1309 -----------------------------
1311 function Find_What_It_Applies_To return Entity_Id is
1312 Result : Entity_Id := Empty;
1315 -- Loop outward through the Scope_Stack, skipping blocks and loops
1317 for J in reverse 0 .. Scope_Stack.Last loop
1318 Result := Scope_Stack.Table (J).Entity;
1319 exit when Ekind (Result) /= E_Block and then
1320 Ekind (Result) /= E_Loop;
1323 pragma Assert (Present (Result));
1325 end Find_What_It_Applies_To;
1327 -- Local declarations
1329 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1330 Kind : constant Entity_Kind := Ekind (Scope_Id);
1331 Loc : constant Source_Ptr := Sloc (N);
1332 Stm_Entity : constant Entity_Id :=
1334 (E_Return_Statement, Current_Scope, Loc, 'R');
1336 -- Start of processing for Analyze_Return_Statement
1339 Set_Return_Statement_Entity (N, Stm_Entity);
1341 Set_Etype (Stm_Entity, Standard_Void_Type);
1342 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1344 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1345 -- (4/2): an inner return statement will apply to this extended return.
1347 if Nkind (N) = N_Extended_Return_Statement then
1348 Push_Scope (Stm_Entity);
1351 -- Check that pragma No_Return is obeyed. Don't complain about the
1352 -- implicitly-generated return that is placed at the end.
1354 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1355 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1358 -- Warn on any unassigned OUT parameters if in procedure
1360 if Ekind (Scope_Id) = E_Procedure then
1361 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1364 -- Check that functions return objects, and other things do not
1366 if Kind = E_Function or else Kind = E_Generic_Function then
1367 if not Returns_Object then
1368 Error_Msg_N ("missing expression in return from function", N);
1371 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1372 if Returns_Object then
1373 Error_Msg_N ("procedure cannot return value (use function)", N);
1376 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1377 if Returns_Object then
1378 if Is_Protected_Type (Scope (Scope_Id)) then
1379 Error_Msg_N ("entry body cannot return value", N);
1381 Error_Msg_N ("accept statement cannot return value", N);
1385 elsif Kind = E_Return_Statement then
1387 -- We are nested within another return statement, which must be an
1388 -- extended_return_statement.
1390 if Returns_Object then
1392 ("extended_return_statement cannot return value; " &
1393 "use `""RETURN;""`", N);
1397 Error_Msg_N ("illegal context for return statement", N);
1400 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1401 Analyze_Function_Return (N);
1403 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1404 Set_Return_Present (Scope_Id);
1407 if Nkind (N) = N_Extended_Return_Statement then
1411 Kill_Current_Values (Last_Assignment_Only => True);
1412 Check_Unreachable_Code (N);
1413 end Analyze_Return_Statement;
1415 -------------------------------------
1416 -- Analyze_Simple_Return_Statement --
1417 -------------------------------------
1419 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1421 if Present (Expression (N)) then
1422 Mark_Coextensions (N, Expression (N));
1425 Analyze_Return_Statement (N);
1426 end Analyze_Simple_Return_Statement;
1428 -------------------------
1429 -- Analyze_Return_Type --
1430 -------------------------
1432 procedure Analyze_Return_Type (N : Node_Id) is
1433 Designator : constant Entity_Id := Defining_Entity (N);
1434 Typ : Entity_Id := Empty;
1437 -- Normal case where result definition does not indicate an error
1439 if Result_Definition (N) /= Error then
1440 if Nkind (Result_Definition (N)) = N_Access_Definition then
1441 Check_SPARK_Restriction
1442 ("access result is not allowed", Result_Definition (N));
1444 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1447 AD : constant Node_Id :=
1448 Access_To_Subprogram_Definition (Result_Definition (N));
1450 if Present (AD) and then Protected_Present (AD) then
1451 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1453 Typ := Access_Definition (N, Result_Definition (N));
1457 Set_Parent (Typ, Result_Definition (N));
1458 Set_Is_Local_Anonymous_Access (Typ);
1459 Set_Etype (Designator, Typ);
1461 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1463 Null_Exclusion_Static_Checks (N);
1465 -- Subtype_Mark case
1468 Find_Type (Result_Definition (N));
1469 Typ := Entity (Result_Definition (N));
1470 Set_Etype (Designator, Typ);
1472 -- If the result type of a subprogram is not in ALFA, then the
1473 -- subprogram is not in ALFA.
1475 if not Is_In_ALFA (Typ) then
1476 Set_Is_In_ALFA (Designator, False);
1479 -- Unconstrained array as result is not allowed in SPARK
1481 if Is_Array_Type (Typ)
1482 and then not Is_Constrained (Typ)
1484 Check_SPARK_Restriction
1485 ("returning an unconstrained array is not allowed",
1486 Result_Definition (N));
1489 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1491 Null_Exclusion_Static_Checks (N);
1493 -- If a null exclusion is imposed on the result type, then create
1494 -- a null-excluding itype (an access subtype) and use it as the
1495 -- function's Etype. Note that the null exclusion checks are done
1496 -- right before this, because they don't get applied to types that
1497 -- do not come from source.
1499 if Is_Access_Type (Typ)
1500 and then Null_Exclusion_Present (N)
1502 Set_Etype (Designator,
1503 Create_Null_Excluding_Itype
1506 Scope_Id => Scope (Current_Scope)));
1508 -- The new subtype must be elaborated before use because
1509 -- it is visible outside of the function. However its base
1510 -- type may not be frozen yet, so the reference that will
1511 -- force elaboration must be attached to the freezing of
1514 -- If the return specification appears on a proper body,
1515 -- the subtype will have been created already on the spec.
1517 if Is_Frozen (Typ) then
1518 if Nkind (Parent (N)) = N_Subprogram_Body
1519 and then Nkind (Parent (Parent (N))) = N_Subunit
1523 Build_Itype_Reference (Etype (Designator), Parent (N));
1527 Ensure_Freeze_Node (Typ);
1530 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1532 Set_Itype (IR, Etype (Designator));
1533 Append_Freeze_Actions (Typ, New_List (IR));
1538 Set_Etype (Designator, Typ);
1541 if Ekind (Typ) = E_Incomplete_Type
1542 and then Is_Value_Type (Typ)
1546 elsif Ekind (Typ) = E_Incomplete_Type
1547 or else (Is_Class_Wide_Type (Typ)
1549 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1551 -- AI05-0151: Tagged incomplete types are allowed in all formal
1552 -- parts. Untagged incomplete types are not allowed in bodies.
1554 if Ada_Version >= Ada_2012 then
1555 if Is_Tagged_Type (Typ) then
1558 elsif Nkind_In (Parent (Parent (N)),
1564 ("invalid use of untagged incomplete type&",
1570 ("invalid use of incomplete type&", Designator, Typ);
1575 -- Case where result definition does indicate an error
1578 Set_Etype (Designator, Any_Type);
1580 end Analyze_Return_Type;
1582 -----------------------------
1583 -- Analyze_Subprogram_Body --
1584 -----------------------------
1586 procedure Analyze_Subprogram_Body (N : Node_Id) is
1587 Loc : constant Source_Ptr := Sloc (N);
1588 Body_Spec : constant Node_Id := Specification (N);
1589 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1592 if Debug_Flag_C then
1593 Write_Str ("==> subprogram body ");
1594 Write_Name (Chars (Body_Id));
1595 Write_Str (" from ");
1596 Write_Location (Loc);
1601 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1603 -- The real work is split out into the helper, so it can do "return;"
1604 -- without skipping the debug output:
1606 Analyze_Subprogram_Body_Helper (N);
1608 if Debug_Flag_C then
1610 Write_Str ("<== subprogram body ");
1611 Write_Name (Chars (Body_Id));
1612 Write_Str (" from ");
1613 Write_Location (Loc);
1616 end Analyze_Subprogram_Body;
1618 ------------------------------------
1619 -- Analyze_Subprogram_Body_Helper --
1620 ------------------------------------
1622 -- This procedure is called for regular subprogram bodies, generic bodies,
1623 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1624 -- specification matters, and is used to create a proper declaration for
1625 -- the subprogram, or to perform conformance checks.
1627 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1628 Loc : constant Source_Ptr := Sloc (N);
1629 Body_Deleted : constant Boolean := False;
1630 Body_Spec : constant Node_Id := Specification (N);
1631 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1632 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1633 Conformant : Boolean;
1636 Prot_Typ : Entity_Id := Empty;
1637 Spec_Id : Entity_Id;
1638 Spec_Decl : Node_Id := Empty;
1640 Last_Real_Spec_Entity : Entity_Id := Empty;
1641 -- When we analyze a separate spec, the entity chain ends up containing
1642 -- the formals, as well as any itypes generated during analysis of the
1643 -- default expressions for parameters, or the arguments of associated
1644 -- precondition/postcondition pragmas (which are analyzed in the context
1645 -- of the spec since they have visibility on formals).
1647 -- These entities belong with the spec and not the body. However we do
1648 -- the analysis of the body in the context of the spec (again to obtain
1649 -- visibility to the formals), and all the entities generated during
1650 -- this analysis end up also chained to the entity chain of the spec.
1651 -- But they really belong to the body, and there is circuitry to move
1652 -- them from the spec to the body.
1654 -- However, when we do this move, we don't want to move the real spec
1655 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1656 -- variable points to the last real spec entity, so we only move those
1657 -- chained beyond that point. It is initialized to Empty to deal with
1658 -- the case where there is no separate spec.
1660 procedure Check_Anonymous_Return;
1661 -- Ada 2005: if a function returns an access type that denotes a task,
1662 -- or a type that contains tasks, we must create a master entity for
1663 -- the anonymous type, which typically will be used in an allocator
1664 -- in the body of the function.
1666 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1667 -- Look ahead to recognize a pragma that may appear after the body.
1668 -- If there is a previous spec, check that it appears in the same
1669 -- declarative part. If the pragma is Inline_Always, perform inlining
1670 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1671 -- If the body acts as a spec, and inlining is required, we create a
1672 -- subprogram declaration for it, in order to attach the body to inline.
1673 -- If pragma does not appear after the body, check whether there is
1674 -- an inline pragma before any local declarations.
1676 procedure Check_Missing_Return;
1677 -- Checks for a function with a no return statements, and also performs
1678 -- the warning checks implemented by Check_Returns. In formal mode, also
1679 -- verify that a function ends with a RETURN and that a procedure does
1680 -- not contain any RETURN.
1682 function Disambiguate_Spec return Entity_Id;
1683 -- When a primitive is declared between the private view and the full
1684 -- view of a concurrent type which implements an interface, a special
1685 -- mechanism is used to find the corresponding spec of the primitive
1688 function Is_Private_Concurrent_Primitive
1689 (Subp_Id : Entity_Id) return Boolean;
1690 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1691 -- type that implements an interface and has a private view.
1693 procedure Set_Trivial_Subprogram (N : Node_Id);
1694 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1695 -- subprogram whose body is being analyzed. N is the statement node
1696 -- causing the flag to be set, if the following statement is a return
1697 -- of an entity, we mark the entity as set in source to suppress any
1698 -- warning on the stylized use of function stubs with a dummy return.
1700 procedure Verify_Overriding_Indicator;
1701 -- If there was a previous spec, the entity has been entered in the
1702 -- current scope previously. If the body itself carries an overriding
1703 -- indicator, check that it is consistent with the known status of the
1706 ----------------------------
1707 -- Check_Anonymous_Return --
1708 ----------------------------
1710 procedure Check_Anonymous_Return is
1716 if Present (Spec_Id) then
1722 if Ekind (Scop) = E_Function
1723 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1724 and then not Is_Thunk (Scop)
1725 and then (Has_Task (Designated_Type (Etype (Scop)))
1727 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1729 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1730 and then Expander_Active
1732 -- Avoid cases with no tasking support
1734 and then RTE_Available (RE_Current_Master)
1735 and then not Restriction_Active (No_Task_Hierarchy)
1738 Make_Object_Declaration (Loc,
1739 Defining_Identifier =>
1740 Make_Defining_Identifier (Loc, Name_uMaster),
1741 Constant_Present => True,
1742 Object_Definition =>
1743 New_Reference_To (RTE (RE_Master_Id), Loc),
1745 Make_Explicit_Dereference (Loc,
1746 New_Reference_To (RTE (RE_Current_Master), Loc)));
1748 if Present (Declarations (N)) then
1749 Prepend (Decl, Declarations (N));
1751 Set_Declarations (N, New_List (Decl));
1754 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1755 Set_Has_Master_Entity (Scop);
1757 -- Now mark the containing scope as a task master
1760 while Nkind (Par) /= N_Compilation_Unit loop
1761 Par := Parent (Par);
1762 pragma Assert (Present (Par));
1764 -- If we fall off the top, we are at the outer level, and
1765 -- the environment task is our effective master, so nothing
1769 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1771 Set_Is_Task_Master (Par, True);
1776 end Check_Anonymous_Return;
1778 -------------------------
1779 -- Check_Inline_Pragma --
1780 -------------------------
1782 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1786 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1787 -- True when N is a pragma Inline or Inline_Always that applies
1788 -- to this subprogram.
1790 -----------------------
1791 -- Is_Inline_Pragma --
1792 -----------------------
1794 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1797 Nkind (N) = N_Pragma
1799 (Pragma_Name (N) = Name_Inline_Always
1802 and then Pragma_Name (N) = Name_Inline))
1805 (Expression (First (Pragma_Argument_Associations (N))))
1807 end Is_Inline_Pragma;
1809 -- Start of processing for Check_Inline_Pragma
1812 if not Expander_Active then
1816 if Is_List_Member (N)
1817 and then Present (Next (N))
1818 and then Is_Inline_Pragma (Next (N))
1822 elsif Nkind (N) /= N_Subprogram_Body_Stub
1823 and then Present (Declarations (N))
1824 and then Is_Inline_Pragma (First (Declarations (N)))
1826 Prag := First (Declarations (N));
1832 if Present (Prag) then
1833 if Present (Spec_Id) then
1834 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1839 -- Create a subprogram declaration, to make treatment uniform
1842 Subp : constant Entity_Id :=
1843 Make_Defining_Identifier (Loc, Chars (Body_Id));
1844 Decl : constant Node_Id :=
1845 Make_Subprogram_Declaration (Loc,
1847 New_Copy_Tree (Specification (N)));
1850 Set_Defining_Unit_Name (Specification (Decl), Subp);
1852 if Present (First_Formal (Body_Id)) then
1853 Plist := Copy_Parameter_List (Body_Id);
1854 Set_Parameter_Specifications
1855 (Specification (Decl), Plist);
1858 Insert_Before (N, Decl);
1861 Set_Has_Pragma_Inline (Subp);
1863 if Pragma_Name (Prag) = Name_Inline_Always then
1864 Set_Is_Inlined (Subp);
1865 Set_Has_Pragma_Inline_Always (Subp);
1872 end Check_Inline_Pragma;
1874 --------------------------
1875 -- Check_Missing_Return --
1876 --------------------------
1878 procedure Check_Missing_Return is
1880 Missing_Ret : Boolean;
1883 if Nkind (Body_Spec) = N_Function_Specification then
1884 if Present (Spec_Id) then
1890 if Return_Present (Id) then
1891 Check_Returns (HSS, 'F', Missing_Ret);
1894 Set_Has_Missing_Return (Id);
1897 elsif (Is_Generic_Subprogram (Id)
1898 or else not Is_Machine_Code_Subprogram (Id))
1899 and then not Body_Deleted
1901 Error_Msg_N ("missing RETURN statement in function body", N);
1904 -- If procedure with No_Return, check returns
1906 elsif Nkind (Body_Spec) = N_Procedure_Specification
1907 and then Present (Spec_Id)
1908 and then No_Return (Spec_Id)
1910 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1913 -- Special checks in formal mode
1915 if Nkind (Body_Spec) = N_Function_Specification then
1917 -- In formal mode, last statement of a function should be a return
1920 Stat : constant Node_Id := Last_Source_Statement (HSS);
1923 and then not Nkind_In (Stat, N_Simple_Return_Statement,
1924 N_Extended_Return_Statement)
1926 Set_Body_Is_In_ALFA (Id, False);
1927 Check_SPARK_Restriction
1928 ("last statement in function should be RETURN", Stat);
1932 -- In formal mode, verify that a procedure has no return
1934 elsif Nkind (Body_Spec) = N_Procedure_Specification then
1935 if Present (Spec_Id) then
1941 -- Would be nice to point to return statement here, can we
1942 -- borrow the Check_Returns procedure here ???
1944 if Return_Present (Id) then
1945 Set_Body_Is_In_ALFA (Id, False);
1946 Check_SPARK_Restriction
1947 ("procedure should not have RETURN", N);
1950 end Check_Missing_Return;
1952 -----------------------
1953 -- Disambiguate_Spec --
1954 -----------------------
1956 function Disambiguate_Spec return Entity_Id is
1957 Priv_Spec : Entity_Id;
1960 procedure Replace_Types (To_Corresponding : Boolean);
1961 -- Depending on the flag, replace the type of formal parameters of
1962 -- Body_Id if it is a concurrent type implementing interfaces with
1963 -- the corresponding record type or the other way around.
1965 procedure Replace_Types (To_Corresponding : Boolean) is
1967 Formal_Typ : Entity_Id;
1970 Formal := First_Formal (Body_Id);
1971 while Present (Formal) loop
1972 Formal_Typ := Etype (Formal);
1974 if Is_Class_Wide_Type (Formal_Typ) then
1975 Formal_Typ := Root_Type (Formal_Typ);
1978 -- From concurrent type to corresponding record
1980 if To_Corresponding then
1981 if Is_Concurrent_Type (Formal_Typ)
1982 and then Present (Corresponding_Record_Type (Formal_Typ))
1983 and then Present (Interfaces (
1984 Corresponding_Record_Type (Formal_Typ)))
1987 Corresponding_Record_Type (Formal_Typ));
1990 -- From corresponding record to concurrent type
1993 if Is_Concurrent_Record_Type (Formal_Typ)
1994 and then Present (Interfaces (Formal_Typ))
1997 Corresponding_Concurrent_Type (Formal_Typ));
2001 Next_Formal (Formal);
2005 -- Start of processing for Disambiguate_Spec
2008 -- Try to retrieve the specification of the body as is. All error
2009 -- messages are suppressed because the body may not have a spec in
2010 -- its current state.
2012 Spec_N := Find_Corresponding_Spec (N, False);
2014 -- It is possible that this is the body of a primitive declared
2015 -- between a private and a full view of a concurrent type. The
2016 -- controlling parameter of the spec carries the concurrent type,
2017 -- not the corresponding record type as transformed by Analyze_
2018 -- Subprogram_Specification. In such cases, we undo the change
2019 -- made by the analysis of the specification and try to find the
2022 -- Note that wrappers already have their corresponding specs and
2023 -- bodies set during their creation, so if the candidate spec is
2024 -- a wrapper, then we definitely need to swap all types to their
2025 -- original concurrent status.
2028 or else Is_Primitive_Wrapper (Spec_N)
2030 -- Restore all references of corresponding record types to the
2031 -- original concurrent types.
2033 Replace_Types (To_Corresponding => False);
2034 Priv_Spec := Find_Corresponding_Spec (N, False);
2036 -- The current body truly belongs to a primitive declared between
2037 -- a private and a full view. We leave the modified body as is,
2038 -- and return the true spec.
2040 if Present (Priv_Spec)
2041 and then Is_Private_Primitive (Priv_Spec)
2046 -- In case that this is some sort of error, restore the original
2047 -- state of the body.
2049 Replace_Types (To_Corresponding => True);
2053 end Disambiguate_Spec;
2055 -------------------------------------
2056 -- Is_Private_Concurrent_Primitive --
2057 -------------------------------------
2059 function Is_Private_Concurrent_Primitive
2060 (Subp_Id : Entity_Id) return Boolean
2062 Formal_Typ : Entity_Id;
2065 if Present (First_Formal (Subp_Id)) then
2066 Formal_Typ := Etype (First_Formal (Subp_Id));
2068 if Is_Concurrent_Record_Type (Formal_Typ) then
2069 if Is_Class_Wide_Type (Formal_Typ) then
2070 Formal_Typ := Root_Type (Formal_Typ);
2073 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2076 -- The type of the first formal is a concurrent tagged type with
2080 Is_Concurrent_Type (Formal_Typ)
2081 and then Is_Tagged_Type (Formal_Typ)
2082 and then Has_Private_Declaration (Formal_Typ);
2086 end Is_Private_Concurrent_Primitive;
2088 ----------------------------
2089 -- Set_Trivial_Subprogram --
2090 ----------------------------
2092 procedure Set_Trivial_Subprogram (N : Node_Id) is
2093 Nxt : constant Node_Id := Next (N);
2096 Set_Is_Trivial_Subprogram (Body_Id);
2098 if Present (Spec_Id) then
2099 Set_Is_Trivial_Subprogram (Spec_Id);
2103 and then Nkind (Nxt) = N_Simple_Return_Statement
2104 and then No (Next (Nxt))
2105 and then Present (Expression (Nxt))
2106 and then Is_Entity_Name (Expression (Nxt))
2108 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2110 end Set_Trivial_Subprogram;
2112 ---------------------------------
2113 -- Verify_Overriding_Indicator --
2114 ---------------------------------
2116 procedure Verify_Overriding_Indicator is
2118 if Must_Override (Body_Spec) then
2119 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2120 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2124 elsif not Present (Overridden_Operation (Spec_Id)) then
2126 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2129 elsif Must_Not_Override (Body_Spec) then
2130 if Present (Overridden_Operation (Spec_Id)) then
2132 ("subprogram& overrides inherited operation",
2133 Body_Spec, Spec_Id);
2135 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2136 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2139 ("subprogram & overrides predefined operator ",
2140 Body_Spec, Spec_Id);
2142 -- If this is not a primitive operation or protected subprogram,
2143 -- then the overriding indicator is altogether illegal.
2145 elsif not Is_Primitive (Spec_Id)
2146 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2149 ("overriding indicator only allowed " &
2150 "if subprogram is primitive",
2155 and then Present (Overridden_Operation (Spec_Id))
2157 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2158 Style.Missing_Overriding (N, Body_Id);
2161 and then Can_Override_Operator (Spec_Id)
2162 and then not Is_Predefined_File_Name
2163 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2165 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2166 Style.Missing_Overriding (N, Body_Id);
2168 end Verify_Overriding_Indicator;
2170 -- Start of processing for Analyze_Subprogram_Body_Helper
2173 -- Generic subprograms are handled separately. They always have a
2174 -- generic specification. Determine whether current scope has a
2175 -- previous declaration.
2177 -- If the subprogram body is defined within an instance of the same
2178 -- name, the instance appears as a package renaming, and will be hidden
2179 -- within the subprogram.
2181 if Present (Prev_Id)
2182 and then not Is_Overloadable (Prev_Id)
2183 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2184 or else Comes_From_Source (Prev_Id))
2186 if Is_Generic_Subprogram (Prev_Id) then
2188 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2189 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2191 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2193 if Nkind (N) = N_Subprogram_Body then
2194 HSS := Handled_Statement_Sequence (N);
2195 Check_Missing_Return;
2201 -- Previous entity conflicts with subprogram name. Attempting to
2202 -- enter name will post error.
2204 Enter_Name (Body_Id);
2208 -- Non-generic case, find the subprogram declaration, if one was seen,
2209 -- or enter new overloaded entity in the current scope. If the
2210 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2211 -- part of the context of one of its subunits. No need to redo the
2214 elsif Prev_Id = Body_Id
2215 and then Has_Completion (Body_Id)
2220 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2222 if Nkind (N) = N_Subprogram_Body_Stub
2223 or else No (Corresponding_Spec (N))
2225 if Is_Private_Concurrent_Primitive (Body_Id) then
2226 Spec_Id := Disambiguate_Spec;
2228 Spec_Id := Find_Corresponding_Spec (N);
2231 -- If this is a duplicate body, no point in analyzing it
2233 if Error_Posted (N) then
2237 -- A subprogram body should cause freezing of its own declaration,
2238 -- but if there was no previous explicit declaration, then the
2239 -- subprogram will get frozen too late (there may be code within
2240 -- the body that depends on the subprogram having been frozen,
2241 -- such as uses of extra formals), so we force it to be frozen
2242 -- here. Same holds if the body and spec are compilation units.
2243 -- Finally, if the return type is an anonymous access to protected
2244 -- subprogram, it must be frozen before the body because its
2245 -- expansion has generated an equivalent type that is used when
2246 -- elaborating the body.
2248 if No (Spec_Id) then
2249 Freeze_Before (N, Body_Id);
2251 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2252 Freeze_Before (N, Spec_Id);
2254 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2255 Freeze_Before (N, Etype (Body_Id));
2259 Spec_Id := Corresponding_Spec (N);
2263 -- By default, consider that the subprogram body is in ALFA if the spec
2264 -- is in ALFA. This is reversed later if some expression or statement is
2270 if Present (Spec_Id) then
2276 if Is_In_ALFA (Id) then
2277 Set_Body_Is_In_ALFA (Id);
2281 -- Do not inline any subprogram that contains nested subprograms, since
2282 -- the backend inlining circuit seems to generate uninitialized
2283 -- references in this case. We know this happens in the case of front
2284 -- end ZCX support, but it also appears it can happen in other cases as
2285 -- well. The backend often rejects attempts to inline in the case of
2286 -- nested procedures anyway, so little if anything is lost by this.
2287 -- Note that this is test is for the benefit of the back-end. There is
2288 -- a separate test for front-end inlining that also rejects nested
2291 -- Do not do this test if errors have been detected, because in some
2292 -- error cases, this code blows up, and we don't need it anyway if
2293 -- there have been errors, since we won't get to the linker anyway.
2295 if Comes_From_Source (Body_Id)
2296 and then Serious_Errors_Detected = 0
2300 P_Ent := Scope (P_Ent);
2301 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2303 if Is_Subprogram (P_Ent) then
2304 Set_Is_Inlined (P_Ent, False);
2306 if Comes_From_Source (P_Ent)
2307 and then Has_Pragma_Inline (P_Ent)
2310 ("cannot inline& (nested subprogram)?",
2317 Check_Inline_Pragma (Spec_Id);
2319 -- Deal with special case of a fully private operation in the body of
2320 -- the protected type. We must create a declaration for the subprogram,
2321 -- in order to attach the protected subprogram that will be used in
2322 -- internal calls. We exclude compiler generated bodies from the
2323 -- expander since the issue does not arise for those cases.
2326 and then Comes_From_Source (N)
2327 and then Is_Protected_Type (Current_Scope)
2329 Spec_Id := Build_Private_Protected_Declaration (N);
2332 -- If a separate spec is present, then deal with freezing issues
2334 if Present (Spec_Id) then
2335 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2336 Verify_Overriding_Indicator;
2338 -- In general, the spec will be frozen when we start analyzing the
2339 -- body. However, for internally generated operations, such as
2340 -- wrapper functions for inherited operations with controlling
2341 -- results, the spec may not have been frozen by the time we
2342 -- expand the freeze actions that include the bodies. In particular,
2343 -- extra formals for accessibility or for return-in-place may need
2344 -- to be generated. Freeze nodes, if any, are inserted before the
2347 if not Is_Frozen (Spec_Id)
2348 and then Expander_Active
2350 -- Force the generation of its freezing node to ensure proper
2351 -- management of access types in the backend.
2353 -- This is definitely needed for some cases, but it is not clear
2354 -- why, to be investigated further???
2356 Set_Has_Delayed_Freeze (Spec_Id);
2357 Freeze_Before (N, Spec_Id);
2361 -- Mark presence of postcondition procedure in current scope and mark
2362 -- the procedure itself as needing debug info. The latter is important
2363 -- when analyzing decision coverage (for example, for MC/DC coverage).
2365 if Chars (Body_Id) = Name_uPostconditions then
2366 Set_Has_Postconditions (Current_Scope);
2367 Set_Debug_Info_Needed (Body_Id);
2370 -- Place subprogram on scope stack, and make formals visible. If there
2371 -- is a spec, the visible entity remains that of the spec.
2373 if Present (Spec_Id) then
2374 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2376 if Is_Child_Unit (Spec_Id) then
2377 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2381 Style.Check_Identifier (Body_Id, Spec_Id);
2384 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2385 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2387 if Is_Abstract_Subprogram (Spec_Id) then
2388 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2392 Set_Convention (Body_Id, Convention (Spec_Id));
2393 Set_Has_Completion (Spec_Id);
2395 if Is_Protected_Type (Scope (Spec_Id)) then
2396 Prot_Typ := Scope (Spec_Id);
2399 -- If this is a body generated for a renaming, do not check for
2400 -- full conformance. The check is redundant, because the spec of
2401 -- the body is a copy of the spec in the renaming declaration,
2402 -- and the test can lead to spurious errors on nested defaults.
2404 if Present (Spec_Decl)
2405 and then not Comes_From_Source (N)
2407 (Nkind (Original_Node (Spec_Decl)) =
2408 N_Subprogram_Renaming_Declaration
2409 or else (Present (Corresponding_Body (Spec_Decl))
2411 Nkind (Unit_Declaration_Node
2412 (Corresponding_Body (Spec_Decl))) =
2413 N_Subprogram_Renaming_Declaration))
2417 -- Conversely, the spec may have been generated for specless body
2418 -- with an inline pragma.
2420 elsif Comes_From_Source (N)
2421 and then not Comes_From_Source (Spec_Id)
2422 and then Has_Pragma_Inline (Spec_Id)
2429 Fully_Conformant, True, Conformant, Body_Id);
2432 -- If the body is not fully conformant, we have to decide if we
2433 -- should analyze it or not. If it has a really messed up profile
2434 -- then we probably should not analyze it, since we will get too
2435 -- many bogus messages.
2437 -- Our decision is to go ahead in the non-fully conformant case
2438 -- only if it is at least mode conformant with the spec. Note
2439 -- that the call to Check_Fully_Conformant has issued the proper
2440 -- error messages to complain about the lack of conformance.
2443 and then not Mode_Conformant (Body_Id, Spec_Id)
2449 if Spec_Id /= Body_Id then
2450 Reference_Body_Formals (Spec_Id, Body_Id);
2453 if Nkind (N) /= N_Subprogram_Body_Stub then
2454 Set_Corresponding_Spec (N, Spec_Id);
2456 -- Ada 2005 (AI-345): If the operation is a primitive operation
2457 -- of a concurrent type, the type of the first parameter has been
2458 -- replaced with the corresponding record, which is the proper
2459 -- run-time structure to use. However, within the body there may
2460 -- be uses of the formals that depend on primitive operations
2461 -- of the type (in particular calls in prefixed form) for which
2462 -- we need the original concurrent type. The operation may have
2463 -- several controlling formals, so the replacement must be done
2466 if Comes_From_Source (Spec_Id)
2467 and then Present (First_Entity (Spec_Id))
2468 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2469 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2471 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2474 (Corresponding_Concurrent_Type
2475 (Etype (First_Entity (Spec_Id))))
2478 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2482 Form := First_Formal (Spec_Id);
2483 while Present (Form) loop
2484 if Etype (Form) = Typ then
2485 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2493 -- Make the formals visible, and place subprogram on scope stack.
2494 -- This is also the point at which we set Last_Real_Spec_Entity
2495 -- to mark the entities which will not be moved to the body.
2497 Install_Formals (Spec_Id);
2498 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2499 Push_Scope (Spec_Id);
2501 -- Make sure that the subprogram is immediately visible. For
2502 -- child units that have no separate spec this is indispensable.
2503 -- Otherwise it is safe albeit redundant.
2505 Set_Is_Immediately_Visible (Spec_Id);
2508 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2509 Set_Ekind (Body_Id, E_Subprogram_Body);
2510 Set_Scope (Body_Id, Scope (Spec_Id));
2511 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2512 Set_Is_In_ALFA (Body_Id, False);
2514 -- Case of subprogram body with no previous spec
2517 -- Check for style warning required
2521 -- Only apply check for source level subprograms for which checks
2522 -- have not been suppressed.
2524 and then Comes_From_Source (Body_Id)
2525 and then not Suppress_Style_Checks (Body_Id)
2527 -- No warnings within an instance
2529 and then not In_Instance
2531 -- No warnings for expression functions
2533 and then Nkind (Original_Node (N)) /= N_Expression_Function
2535 Style.Body_With_No_Spec (N);
2538 New_Overloaded_Entity (Body_Id);
2540 if Nkind (N) /= N_Subprogram_Body_Stub then
2541 Set_Acts_As_Spec (N);
2542 Generate_Definition (Body_Id);
2544 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2545 Generate_Reference_To_Formals (Body_Id);
2546 Install_Formals (Body_Id);
2547 Push_Scope (Body_Id);
2551 -- If the return type is an anonymous access type whose designated type
2552 -- is the limited view of a class-wide type and the non-limited view is
2553 -- available, update the return type accordingly.
2555 if Ada_Version >= Ada_2005
2556 and then Comes_From_Source (N)
2563 Rtyp := Etype (Current_Scope);
2565 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2566 Etyp := Directly_Designated_Type (Rtyp);
2568 if Is_Class_Wide_Type (Etyp)
2569 and then From_With_Type (Etyp)
2571 Set_Directly_Designated_Type
2572 (Etype (Current_Scope), Available_View (Etyp));
2578 -- If this is the proper body of a stub, we must verify that the stub
2579 -- conforms to the body, and to the previous spec if one was present.
2580 -- we know already that the body conforms to that spec. This test is
2581 -- only required for subprograms that come from source.
2583 if Nkind (Parent (N)) = N_Subunit
2584 and then Comes_From_Source (N)
2585 and then not Error_Posted (Body_Id)
2586 and then Nkind (Corresponding_Stub (Parent (N))) =
2587 N_Subprogram_Body_Stub
2590 Old_Id : constant Entity_Id :=
2592 (Specification (Corresponding_Stub (Parent (N))));
2594 Conformant : Boolean := False;
2597 if No (Spec_Id) then
2598 Check_Fully_Conformant (Body_Id, Old_Id);
2602 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2604 if not Conformant then
2606 -- The stub was taken to be a new declaration. Indicate
2607 -- that it lacks a body.
2609 Set_Has_Completion (Old_Id, False);
2615 Set_Has_Completion (Body_Id);
2616 Check_Eliminated (Body_Id);
2618 if Nkind (N) = N_Subprogram_Body_Stub then
2621 elsif Present (Spec_Id)
2622 and then Expander_Active
2624 (Has_Pragma_Inline_Always (Spec_Id)
2625 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2627 Build_Body_To_Inline (N, Spec_Id);
2630 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2631 -- if its specification we have to install the private withed units.
2632 -- This holds for child units as well.
2634 if Is_Compilation_Unit (Body_Id)
2635 or else Nkind (Parent (N)) = N_Compilation_Unit
2637 Install_Private_With_Clauses (Body_Id);
2640 Check_Anonymous_Return;
2642 -- Set the Protected_Formal field of each extra formal of the protected
2643 -- subprogram to reference the corresponding extra formal of the
2644 -- subprogram that implements it. For regular formals this occurs when
2645 -- the protected subprogram's declaration is expanded, but the extra
2646 -- formals don't get created until the subprogram is frozen. We need to
2647 -- do this before analyzing the protected subprogram's body so that any
2648 -- references to the original subprogram's extra formals will be changed
2649 -- refer to the implementing subprogram's formals (see Expand_Formal).
2651 if Present (Spec_Id)
2652 and then Is_Protected_Type (Scope (Spec_Id))
2653 and then Present (Protected_Body_Subprogram (Spec_Id))
2656 Impl_Subp : constant Entity_Id :=
2657 Protected_Body_Subprogram (Spec_Id);
2658 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2659 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2661 while Present (Prot_Ext_Formal) loop
2662 pragma Assert (Present (Impl_Ext_Formal));
2663 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2664 Next_Formal_With_Extras (Prot_Ext_Formal);
2665 Next_Formal_With_Extras (Impl_Ext_Formal);
2670 -- Now we can go on to analyze the body
2672 HSS := Handled_Statement_Sequence (N);
2673 Set_Actual_Subtypes (N, Current_Scope);
2675 -- Deal with preconditions and postconditions
2677 Process_PPCs (N, Spec_Id, Body_Id);
2679 -- Add a declaration for the Protection object, renaming declarations
2680 -- for discriminals and privals and finally a declaration for the entry
2681 -- family index (if applicable). This form of early expansion is done
2682 -- when the Expander is active because Install_Private_Data_Declarations
2683 -- references entities which were created during regular expansion.
2686 and then Comes_From_Source (N)
2687 and then Present (Prot_Typ)
2688 and then Present (Spec_Id)
2689 and then not Is_Eliminated (Spec_Id)
2691 Install_Private_Data_Declarations
2692 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2695 -- Analyze the declarations (this call will analyze the precondition
2696 -- Check pragmas we prepended to the list, as well as the declaration
2697 -- of the _Postconditions procedure).
2699 Analyze_Declarations (Declarations (N));
2701 -- Check completion, and analyze the statements
2704 Inspect_Deferred_Constant_Completion (Declarations (N));
2707 -- Deal with end of scope processing for the body
2709 Process_End_Label (HSS, 't', Current_Scope);
2711 Check_Subprogram_Order (N);
2712 Set_Analyzed (Body_Id);
2714 -- If we have a separate spec, then the analysis of the declarations
2715 -- caused the entities in the body to be chained to the spec id, but
2716 -- we want them chained to the body id. Only the formal parameters
2717 -- end up chained to the spec id in this case.
2719 if Present (Spec_Id) then
2721 -- We must conform to the categorization of our spec
2723 Validate_Categorization_Dependency (N, Spec_Id);
2725 -- And if this is a child unit, the parent units must conform
2727 if Is_Child_Unit (Spec_Id) then
2728 Validate_Categorization_Dependency
2729 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2732 -- Here is where we move entities from the spec to the body
2734 -- Case where there are entities that stay with the spec
2736 if Present (Last_Real_Spec_Entity) then
2738 -- No body entities (happens when the only real spec entities
2739 -- come from precondition and postcondition pragmas)
2741 if No (Last_Entity (Body_Id)) then
2743 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2745 -- Body entities present (formals), so chain stuff past them
2749 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2752 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2753 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2754 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2756 -- Case where there are no spec entities, in this case there can
2757 -- be no body entities either, so just move everything.
2760 pragma Assert (No (Last_Entity (Body_Id)));
2761 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2762 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2763 Set_First_Entity (Spec_Id, Empty);
2764 Set_Last_Entity (Spec_Id, Empty);
2768 Check_Missing_Return;
2770 -- Now we are going to check for variables that are never modified in
2771 -- the body of the procedure. But first we deal with a special case
2772 -- where we want to modify this check. If the body of the subprogram
2773 -- starts with a raise statement or its equivalent, or if the body
2774 -- consists entirely of a null statement, then it is pretty obvious
2775 -- that it is OK to not reference the parameters. For example, this
2776 -- might be the following common idiom for a stubbed function:
2777 -- statement of the procedure raises an exception. In particular this
2778 -- deals with the common idiom of a stubbed function, which might
2779 -- appear as something like
2781 -- function F (A : Integer) return Some_Type;
2784 -- raise Program_Error;
2788 -- Here the purpose of X is simply to satisfy the annoying requirement
2789 -- in Ada that there be at least one return, and we certainly do not
2790 -- want to go posting warnings on X that it is not initialized! On
2791 -- the other hand, if X is entirely unreferenced that should still
2794 -- What we do is to detect these cases, and if we find them, flag the
2795 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2796 -- suppress unwanted warnings. For the case of the function stub above
2797 -- we have a special test to set X as apparently assigned to suppress
2804 -- Skip initial labels (for one thing this occurs when we are in
2805 -- front end ZCX mode, but in any case it is irrelevant), and also
2806 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2808 Stm := First (Statements (HSS));
2809 while Nkind (Stm) = N_Label
2810 or else Nkind (Stm) in N_Push_xxx_Label
2815 -- Do the test on the original statement before expansion
2818 Ostm : constant Node_Id := Original_Node (Stm);
2821 -- If explicit raise statement, turn on flag
2823 if Nkind (Ostm) = N_Raise_Statement then
2824 Set_Trivial_Subprogram (Stm);
2826 -- If null statement, and no following statements, turn on flag
2828 elsif Nkind (Stm) = N_Null_Statement
2829 and then Comes_From_Source (Stm)
2830 and then No (Next (Stm))
2832 Set_Trivial_Subprogram (Stm);
2834 -- Check for explicit call cases which likely raise an exception
2836 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2837 if Is_Entity_Name (Name (Ostm)) then
2839 Ent : constant Entity_Id := Entity (Name (Ostm));
2842 -- If the procedure is marked No_Return, then likely it
2843 -- raises an exception, but in any case it is not coming
2844 -- back here, so turn on the flag.
2846 if Ekind (Ent) = E_Procedure
2847 and then No_Return (Ent)
2849 Set_Trivial_Subprogram (Stm);
2857 -- Check for variables that are never modified
2863 -- If there is a separate spec, then transfer Never_Set_In_Source
2864 -- flags from out parameters to the corresponding entities in the
2865 -- body. The reason we do that is we want to post error flags on
2866 -- the body entities, not the spec entities.
2868 if Present (Spec_Id) then
2869 E1 := First_Entity (Spec_Id);
2870 while Present (E1) loop
2871 if Ekind (E1) = E_Out_Parameter then
2872 E2 := First_Entity (Body_Id);
2873 while Present (E2) loop
2874 exit when Chars (E1) = Chars (E2);
2878 if Present (E2) then
2879 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2887 -- Check references in body unless it was deleted. Note that the
2888 -- check of Body_Deleted here is not just for efficiency, it is
2889 -- necessary to avoid junk warnings on formal parameters.
2891 if not Body_Deleted then
2892 Check_References (Body_Id);
2895 end Analyze_Subprogram_Body_Helper;
2897 ------------------------------------
2898 -- Analyze_Subprogram_Declaration --
2899 ------------------------------------
2901 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2902 Loc : constant Source_Ptr := Sloc (N);
2903 Scop : constant Entity_Id := Current_Scope;
2904 Designator : Entity_Id;
2906 Null_Body : Node_Id := Empty;
2908 -- Start of processing for Analyze_Subprogram_Declaration
2911 -- Null procedures are not allowed in SPARK
2913 if Nkind (Specification (N)) = N_Procedure_Specification
2914 and then Null_Present (Specification (N))
2916 Check_SPARK_Restriction ("null procedure is not allowed", N);
2919 -- For a null procedure, capture the profile before analysis, for
2920 -- expansion at the freeze point and at each point of call. The body
2921 -- will only be used if the procedure has preconditions. In that case
2922 -- the body is analyzed at the freeze point.
2924 if Nkind (Specification (N)) = N_Procedure_Specification
2925 and then Null_Present (Specification (N))
2926 and then Expander_Active
2929 Make_Subprogram_Body (Loc,
2931 New_Copy_Tree (Specification (N)),
2934 Handled_Statement_Sequence =>
2935 Make_Handled_Sequence_Of_Statements (Loc,
2936 Statements => New_List (Make_Null_Statement (Loc))));
2938 -- Create new entities for body and formals
2940 Set_Defining_Unit_Name (Specification (Null_Body),
2941 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2942 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2944 Form := First (Parameter_Specifications (Specification (Null_Body)));
2945 while Present (Form) loop
2946 Set_Defining_Identifier (Form,
2947 Make_Defining_Identifier (Loc,
2948 Chars (Defining_Identifier (Form))));
2950 -- Resolve the types of the formals now, because the freeze point
2951 -- may appear in a different context, e.g. an instantiation.
2953 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
2954 Find_Type (Parameter_Type (Form));
2957 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
2959 Find_Type (Subtype_Mark (Parameter_Type (Form)));
2963 -- the case of a null procedure with a formal that is an
2964 -- access_to_subprogram type, and that is used as an actual
2965 -- in an instantiation is left to the enthusiastic reader.
2973 if Is_Protected_Type (Current_Scope) then
2974 Error_Msg_N ("protected operation cannot be a null procedure", N);
2978 Designator := Analyze_Subprogram_Specification (Specification (N));
2979 Generate_Definition (Designator);
2981 if Debug_Flag_C then
2982 Write_Str ("==> subprogram spec ");
2983 Write_Name (Chars (Designator));
2984 Write_Str (" from ");
2985 Write_Location (Sloc (N));
2990 if Nkind (Specification (N)) = N_Procedure_Specification
2991 and then Null_Present (Specification (N))
2993 Set_Has_Completion (Designator);
2995 if Present (Null_Body) then
2996 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2997 Set_Body_To_Inline (N, Null_Body);
2998 Set_Is_Inlined (Designator);
3002 Validate_RCI_Subprogram_Declaration (N);
3003 New_Overloaded_Entity (Designator);
3004 Check_Delayed_Subprogram (Designator);
3006 -- If the type of the first formal of the current subprogram is a
3007 -- nongeneric tagged private type, mark the subprogram as being a
3008 -- private primitive. Ditto if this is a function with controlling
3009 -- result, and the return type is currently private. In both cases,
3010 -- the type of the controlling argument or result must be in the
3011 -- current scope for the operation to be primitive.
3013 if Has_Controlling_Result (Designator)
3014 and then Is_Private_Type (Etype (Designator))
3015 and then Scope (Etype (Designator)) = Current_Scope
3016 and then not Is_Generic_Actual_Type (Etype (Designator))
3018 Set_Is_Private_Primitive (Designator);
3020 elsif Present (First_Formal (Designator)) then
3022 Formal_Typ : constant Entity_Id :=
3023 Etype (First_Formal (Designator));
3025 Set_Is_Private_Primitive (Designator,
3026 Is_Tagged_Type (Formal_Typ)
3027 and then Scope (Formal_Typ) = Current_Scope
3028 and then Is_Private_Type (Formal_Typ)
3029 and then not Is_Generic_Actual_Type (Formal_Typ));
3033 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3036 if Ada_Version >= Ada_2005
3037 and then Comes_From_Source (N)
3038 and then Is_Dispatching_Operation (Designator)
3045 if Has_Controlling_Result (Designator) then
3046 Etyp := Etype (Designator);
3049 E := First_Entity (Designator);
3051 and then Is_Formal (E)
3052 and then not Is_Controlling_Formal (E)
3060 if Is_Access_Type (Etyp) then
3061 Etyp := Directly_Designated_Type (Etyp);
3064 if Is_Interface (Etyp)
3065 and then not Is_Abstract_Subprogram (Designator)
3066 and then not (Ekind (Designator) = E_Procedure
3067 and then Null_Present (Specification (N)))
3069 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3071 ("(Ada 2005) interface subprogram % must be abstract or null",
3077 -- What is the following code for, it used to be
3079 -- ??? Set_Suppress_Elaboration_Checks
3080 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3082 -- The following seems equivalent, but a bit dubious
3084 if Elaboration_Checks_Suppressed (Designator) then
3085 Set_Kill_Elaboration_Checks (Designator);
3088 if Scop /= Standard_Standard
3089 and then not Is_Child_Unit (Designator)
3091 Set_Categorization_From_Scope (Designator, Scop);
3093 -- For a compilation unit, check for library-unit pragmas
3095 Push_Scope (Designator);
3096 Set_Categorization_From_Pragmas (N);
3097 Validate_Categorization_Dependency (N, Designator);
3101 -- For a compilation unit, set body required. This flag will only be
3102 -- reset if a valid Import or Interface pragma is processed later on.
3104 if Nkind (Parent (N)) = N_Compilation_Unit then
3105 Set_Body_Required (Parent (N), True);
3107 if Ada_Version >= Ada_2005
3108 and then Nkind (Specification (N)) = N_Procedure_Specification
3109 and then Null_Present (Specification (N))
3112 ("null procedure cannot be declared at library level", N);
3116 Generate_Reference_To_Formals (Designator);
3117 Check_Eliminated (Designator);
3119 if Debug_Flag_C then
3121 Write_Str ("<== subprogram spec ");
3122 Write_Name (Chars (Designator));
3123 Write_Str (" from ");
3124 Write_Location (Sloc (N));
3128 if Is_Protected_Type (Current_Scope) then
3130 -- Indicate that this is a protected operation, because it may be
3131 -- used in subsequent declarations within the protected type.
3133 Set_Convention (Designator, Convention_Protected);
3136 List_Inherited_Pre_Post_Aspects (Designator);
3138 if Has_Aspects (N) then
3139 Analyze_Aspect_Specifications (N, Designator);
3141 end Analyze_Subprogram_Declaration;
3143 --------------------------------------
3144 -- Analyze_Subprogram_Specification --
3145 --------------------------------------
3147 -- Reminder: N here really is a subprogram specification (not a subprogram
3148 -- declaration). This procedure is called to analyze the specification in
3149 -- both subprogram bodies and subprogram declarations (specs).
3151 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3152 Designator : constant Entity_Id := Defining_Entity (N);
3153 Formals : constant List_Id := Parameter_Specifications (N);
3155 -- Start of processing for Analyze_Subprogram_Specification
3158 -- By default, consider that the subprogram spec is in ALFA. This is
3159 -- reversed later if some parameter or result is not in ALFA.
3161 Set_Is_In_ALFA (Designator);
3163 -- User-defined operator is not allowed in SPARK, except as a renaming
3165 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3166 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3168 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3171 -- Proceed with analysis
3173 Generate_Definition (Designator);
3175 if Nkind (N) = N_Function_Specification then
3176 Set_Ekind (Designator, E_Function);
3177 Set_Mechanism (Designator, Default_Mechanism);
3179 Set_Ekind (Designator, E_Procedure);
3180 Set_Etype (Designator, Standard_Void_Type);
3183 -- Introduce new scope for analysis of the formals and the return type
3185 Set_Scope (Designator, Current_Scope);
3187 if Present (Formals) then
3188 Push_Scope (Designator);
3189 Process_Formals (Formals, N);
3191 -- Ada 2005 (AI-345): If this is an overriding operation of an
3192 -- inherited interface operation, and the controlling type is
3193 -- a synchronized type, replace the type with its corresponding
3194 -- record, to match the proper signature of an overriding operation.
3195 -- Same processing for an access parameter whose designated type is
3196 -- derived from a synchronized interface.
3198 if Ada_Version >= Ada_2005 then
3201 Formal_Typ : Entity_Id;
3202 Rec_Typ : Entity_Id;
3203 Desig_Typ : Entity_Id;
3206 Formal := First_Formal (Designator);
3207 while Present (Formal) loop
3208 Formal_Typ := Etype (Formal);
3210 if Is_Concurrent_Type (Formal_Typ)
3211 and then Present (Corresponding_Record_Type (Formal_Typ))
3213 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3215 if Present (Interfaces (Rec_Typ)) then
3216 Set_Etype (Formal, Rec_Typ);
3219 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3220 Desig_Typ := Designated_Type (Formal_Typ);
3222 if Is_Concurrent_Type (Desig_Typ)
3223 and then Present (Corresponding_Record_Type (Desig_Typ))
3225 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3227 if Present (Interfaces (Rec_Typ)) then
3228 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3233 Next_Formal (Formal);
3240 -- The subprogram scope is pushed and popped around the processing of
3241 -- the return type for consistency with call above to Process_Formals
3242 -- (which itself can call Analyze_Return_Type), and to ensure that any
3243 -- itype created for the return type will be associated with the proper
3246 elsif Nkind (N) = N_Function_Specification then
3247 Push_Scope (Designator);
3248 Analyze_Return_Type (N);
3254 if Nkind (N) = N_Function_Specification then
3256 -- Deal with operator symbol case
3258 if Nkind (Designator) = N_Defining_Operator_Symbol then
3259 Valid_Operator_Definition (Designator);
3262 May_Need_Actuals (Designator);
3264 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3265 -- the subprogram is abstract also. This does not apply to renaming
3266 -- declarations, where abstractness is inherited.
3268 -- In case of primitives associated with abstract interface types
3269 -- the check is applied later (see Analyze_Subprogram_Declaration).
3271 if not Nkind_In (Parent (N), N_Subprogram_Renaming_Declaration,
3272 N_Abstract_Subprogram_Declaration,
3273 N_Formal_Abstract_Subprogram_Declaration)
3275 if Is_Abstract_Type (Etype (Designator))
3276 and then not Is_Interface (Etype (Designator))
3279 ("function that returns abstract type must be abstract", N);
3281 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3282 -- access result whose designated type is abstract.
3284 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3286 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3287 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3288 and then Ada_Version >= Ada_2012
3290 Error_Msg_N ("function whose access result designates "
3291 & "abstract type must be abstract", N);
3297 end Analyze_Subprogram_Specification;
3299 --------------------------
3300 -- Build_Body_To_Inline --
3301 --------------------------
3303 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3304 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3305 Original_Body : Node_Id;
3306 Body_To_Analyze : Node_Id;
3307 Max_Size : constant := 10;
3308 Stat_Count : Integer := 0;
3310 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3311 -- Check for declarations that make inlining not worthwhile
3313 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3314 -- Check for statements that make inlining not worthwhile: any tasking
3315 -- statement, nested at any level. Keep track of total number of
3316 -- elementary statements, as a measure of acceptable size.
3318 function Has_Pending_Instantiation return Boolean;
3319 -- If some enclosing body contains instantiations that appear before the
3320 -- corresponding generic body, the enclosing body has a freeze node so
3321 -- that it can be elaborated after the generic itself. This might
3322 -- conflict with subsequent inlinings, so that it is unsafe to try to
3323 -- inline in such a case.
3325 function Has_Single_Return return Boolean;
3326 -- In general we cannot inline functions that return unconstrained type.
3327 -- However, we can handle such functions if all return statements return
3328 -- a local variable that is the only declaration in the body of the
3329 -- function. In that case the call can be replaced by that local
3330 -- variable as is done for other inlined calls.
3332 procedure Remove_Pragmas;
3333 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3334 -- parameter has no meaning when the body is inlined and the formals
3335 -- are rewritten. Remove it from body to inline. The analysis of the
3336 -- non-inlined body will handle the pragma properly.
3338 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3339 -- If the body of the subprogram includes a call that returns an
3340 -- unconstrained type, the secondary stack is involved, and it
3341 -- is not worth inlining.
3343 ------------------------------
3344 -- Has_Excluded_Declaration --
3345 ------------------------------
3347 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3350 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3351 -- Nested subprograms make a given body ineligible for inlining, but
3352 -- we make an exception for instantiations of unchecked conversion.
3353 -- The body has not been analyzed yet, so check the name, and verify
3354 -- that the visible entity with that name is the predefined unit.
3356 -----------------------------
3357 -- Is_Unchecked_Conversion --
3358 -----------------------------
3360 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3361 Id : constant Node_Id := Name (D);
3365 if Nkind (Id) = N_Identifier
3366 and then Chars (Id) = Name_Unchecked_Conversion
3368 Conv := Current_Entity (Id);
3370 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3371 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3373 Conv := Current_Entity (Selector_Name (Id));
3378 return Present (Conv)
3379 and then Is_Predefined_File_Name
3380 (Unit_File_Name (Get_Source_Unit (Conv)))
3381 and then Is_Intrinsic_Subprogram (Conv);
3382 end Is_Unchecked_Conversion;
3384 -- Start of processing for Has_Excluded_Declaration
3388 while Present (D) loop
3389 if (Nkind (D) = N_Function_Instantiation
3390 and then not Is_Unchecked_Conversion (D))
3391 or else Nkind_In (D, N_Protected_Type_Declaration,
3392 N_Package_Declaration,
3393 N_Package_Instantiation,
3395 N_Procedure_Instantiation,
3396 N_Task_Type_Declaration)
3399 ("cannot inline & (non-allowed declaration)?", D, Subp);
3407 end Has_Excluded_Declaration;
3409 ----------------------------
3410 -- Has_Excluded_Statement --
3411 ----------------------------
3413 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3419 while Present (S) loop
3420 Stat_Count := Stat_Count + 1;
3422 if Nkind_In (S, N_Abort_Statement,
3423 N_Asynchronous_Select,
3424 N_Conditional_Entry_Call,
3425 N_Delay_Relative_Statement,
3426 N_Delay_Until_Statement,
3431 ("cannot inline & (non-allowed statement)?", S, Subp);
3434 elsif Nkind (S) = N_Block_Statement then
3435 if Present (Declarations (S))
3436 and then Has_Excluded_Declaration (Declarations (S))
3440 elsif Present (Handled_Statement_Sequence (S))
3443 (Exception_Handlers (Handled_Statement_Sequence (S)))
3445 Has_Excluded_Statement
3446 (Statements (Handled_Statement_Sequence (S))))
3451 elsif Nkind (S) = N_Case_Statement then
3452 E := First (Alternatives (S));
3453 while Present (E) loop
3454 if Has_Excluded_Statement (Statements (E)) then
3461 elsif Nkind (S) = N_If_Statement then
3462 if Has_Excluded_Statement (Then_Statements (S)) then
3466 if Present (Elsif_Parts (S)) then
3467 E := First (Elsif_Parts (S));
3468 while Present (E) loop
3469 if Has_Excluded_Statement (Then_Statements (E)) then
3476 if Present (Else_Statements (S))
3477 and then Has_Excluded_Statement (Else_Statements (S))
3482 elsif Nkind (S) = N_Loop_Statement
3483 and then Has_Excluded_Statement (Statements (S))
3487 elsif Nkind (S) = N_Extended_Return_Statement then
3488 if Has_Excluded_Statement
3489 (Statements (Handled_Statement_Sequence (S)))
3491 (Exception_Handlers (Handled_Statement_Sequence (S)))
3501 end Has_Excluded_Statement;
3503 -------------------------------
3504 -- Has_Pending_Instantiation --
3505 -------------------------------
3507 function Has_Pending_Instantiation return Boolean is
3512 while Present (S) loop
3513 if Is_Compilation_Unit (S)
3514 or else Is_Child_Unit (S)
3518 elsif Ekind (S) = E_Package
3519 and then Has_Forward_Instantiation (S)
3528 end Has_Pending_Instantiation;
3530 ------------------------
3531 -- Has_Single_Return --
3532 ------------------------
3534 function Has_Single_Return return Boolean is
3535 Return_Statement : Node_Id := Empty;
3537 function Check_Return (N : Node_Id) return Traverse_Result;
3543 function Check_Return (N : Node_Id) return Traverse_Result is
3545 if Nkind (N) = N_Simple_Return_Statement then
3546 if Present (Expression (N))
3547 and then Is_Entity_Name (Expression (N))
3549 if No (Return_Statement) then
3550 Return_Statement := N;
3553 elsif Chars (Expression (N)) =
3554 Chars (Expression (Return_Statement))
3562 -- A return statement within an extended return is a noop
3565 elsif No (Expression (N))
3566 and then Nkind (Parent (Parent (N))) =
3567 N_Extended_Return_Statement
3572 -- Expression has wrong form
3577 -- We can only inline a build-in-place function if
3578 -- it has a single extended return.
3580 elsif Nkind (N) = N_Extended_Return_Statement then
3581 if No (Return_Statement) then
3582 Return_Statement := N;
3594 function Check_All_Returns is new Traverse_Func (Check_Return);
3596 -- Start of processing for Has_Single_Return
3599 if Check_All_Returns (N) /= OK then
3602 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3606 return Present (Declarations (N))
3607 and then Present (First (Declarations (N)))
3608 and then Chars (Expression (Return_Statement)) =
3609 Chars (Defining_Identifier (First (Declarations (N))));
3611 end Has_Single_Return;
3613 --------------------
3614 -- Remove_Pragmas --
3615 --------------------
3617 procedure Remove_Pragmas is
3622 Decl := First (Declarations (Body_To_Analyze));
3623 while Present (Decl) loop
3626 if Nkind (Decl) = N_Pragma
3627 and then (Pragma_Name (Decl) = Name_Unreferenced
3629 Pragma_Name (Decl) = Name_Unmodified)
3638 --------------------------
3639 -- Uses_Secondary_Stack --
3640 --------------------------
3642 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3643 function Check_Call (N : Node_Id) return Traverse_Result;
3644 -- Look for function calls that return an unconstrained type
3650 function Check_Call (N : Node_Id) return Traverse_Result is
3652 if Nkind (N) = N_Function_Call
3653 and then Is_Entity_Name (Name (N))
3654 and then Is_Composite_Type (Etype (Entity (Name (N))))
3655 and then not Is_Constrained (Etype (Entity (Name (N))))
3658 ("cannot inline & (call returns unconstrained type)?",
3666 function Check_Calls is new Traverse_Func (Check_Call);
3669 return Check_Calls (Bod) = Abandon;
3670 end Uses_Secondary_Stack;
3672 -- Start of processing for Build_Body_To_Inline
3675 -- Return immediately if done already
3677 if Nkind (Decl) = N_Subprogram_Declaration
3678 and then Present (Body_To_Inline (Decl))
3682 -- Functions that return unconstrained composite types require
3683 -- secondary stack handling, and cannot currently be inlined, unless
3684 -- all return statements return a local variable that is the first
3685 -- local declaration in the body.
3687 elsif Ekind (Subp) = E_Function
3688 and then not Is_Scalar_Type (Etype (Subp))
3689 and then not Is_Access_Type (Etype (Subp))
3690 and then not Is_Constrained (Etype (Subp))
3692 if not Has_Single_Return then
3694 ("cannot inline & (unconstrained return type)?", N, Subp);
3698 -- Ditto for functions that return controlled types, where controlled
3699 -- actions interfere in complex ways with inlining.
3701 elsif Ekind (Subp) = E_Function
3702 and then Needs_Finalization (Etype (Subp))
3705 ("cannot inline & (controlled return type)?", N, Subp);
3709 if Present (Declarations (N))
3710 and then Has_Excluded_Declaration (Declarations (N))
3715 if Present (Handled_Statement_Sequence (N)) then
3716 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3718 ("cannot inline& (exception handler)?",
3719 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3723 Has_Excluded_Statement
3724 (Statements (Handled_Statement_Sequence (N)))
3730 -- We do not inline a subprogram that is too large, unless it is
3731 -- marked Inline_Always. This pragma does not suppress the other
3732 -- checks on inlining (forbidden declarations, handlers, etc).
3734 if Stat_Count > Max_Size
3735 and then not Has_Pragma_Inline_Always (Subp)
3737 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3741 if Has_Pending_Instantiation then
3743 ("cannot inline& (forward instance within enclosing body)?",
3748 -- Within an instance, the body to inline must be treated as a nested
3749 -- generic, so that the proper global references are preserved.
3751 -- Note that we do not do this at the library level, because it is not
3752 -- needed, and furthermore this causes trouble if front end inlining
3753 -- is activated (-gnatN).
3755 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3756 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3757 Original_Body := Copy_Generic_Node (N, Empty, True);
3759 Original_Body := Copy_Separate_Tree (N);
3762 -- We need to capture references to the formals in order to substitute
3763 -- the actuals at the point of inlining, i.e. instantiation. To treat
3764 -- the formals as globals to the body to inline, we nest it within
3765 -- a dummy parameterless subprogram, declared within the real one.
3766 -- To avoid generating an internal name (which is never public, and
3767 -- which affects serial numbers of other generated names), we use
3768 -- an internal symbol that cannot conflict with user declarations.
3770 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3771 Set_Defining_Unit_Name
3772 (Specification (Original_Body),
3773 Make_Defining_Identifier (Sloc (N), Name_uParent));
3774 Set_Corresponding_Spec (Original_Body, Empty);
3776 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3778 -- Set return type of function, which is also global and does not need
3781 if Ekind (Subp) = E_Function then
3782 Set_Result_Definition (Specification (Body_To_Analyze),
3783 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3786 if No (Declarations (N)) then
3787 Set_Declarations (N, New_List (Body_To_Analyze));
3789 Append (Body_To_Analyze, Declarations (N));
3792 Expander_Mode_Save_And_Set (False);
3795 Analyze (Body_To_Analyze);
3796 Push_Scope (Defining_Entity (Body_To_Analyze));
3797 Save_Global_References (Original_Body);
3799 Remove (Body_To_Analyze);
3801 Expander_Mode_Restore;
3803 -- Restore environment if previously saved
3805 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3809 -- If secondary stk used there is no point in inlining. We have
3810 -- already issued the warning in this case, so nothing to do.
3812 if Uses_Secondary_Stack (Body_To_Analyze) then
3816 Set_Body_To_Inline (Decl, Original_Body);
3817 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3818 Set_Is_Inlined (Subp);
3819 end Build_Body_To_Inline;
3825 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3827 -- Do not emit warning if this is a predefined unit which is not the
3828 -- main unit. With validity checks enabled, some predefined subprograms
3829 -- may contain nested subprograms and become ineligible for inlining.
3831 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3832 and then not In_Extended_Main_Source_Unit (Subp)
3836 elsif Has_Pragma_Inline_Always (Subp) then
3838 -- Remove last character (question mark) to make this into an error,
3839 -- because the Inline_Always pragma cannot be obeyed.
3841 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3843 elsif Ineffective_Inline_Warnings then
3844 Error_Msg_NE (Msg, N, Subp);
3848 -----------------------
3849 -- Check_Conformance --
3850 -----------------------
3852 procedure Check_Conformance
3853 (New_Id : Entity_Id;
3855 Ctype : Conformance_Type;
3857 Conforms : out Boolean;
3858 Err_Loc : Node_Id := Empty;
3859 Get_Inst : Boolean := False;
3860 Skip_Controlling_Formals : Boolean := False)
3862 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3863 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3864 -- If Errmsg is True, then processing continues to post an error message
3865 -- for conformance error on given node. Two messages are output. The
3866 -- first message points to the previous declaration with a general "no
3867 -- conformance" message. The second is the detailed reason, supplied as
3868 -- Msg. The parameter N provide information for a possible & insertion
3869 -- in the message, and also provides the location for posting the
3870 -- message in the absence of a specified Err_Loc location.
3872 -----------------------
3873 -- Conformance_Error --
3874 -----------------------
3876 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3883 if No (Err_Loc) then
3889 Error_Msg_Sloc := Sloc (Old_Id);
3892 when Type_Conformant =>
3893 Error_Msg_N -- CODEFIX
3894 ("not type conformant with declaration#!", Enode);
3896 when Mode_Conformant =>
3897 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3899 ("not mode conformant with operation inherited#!",
3903 ("not mode conformant with declaration#!", Enode);
3906 when Subtype_Conformant =>
3907 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3909 ("not subtype conformant with operation inherited#!",
3913 ("not subtype conformant with declaration#!", Enode);
3916 when Fully_Conformant =>
3917 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3918 Error_Msg_N -- CODEFIX
3919 ("not fully conformant with operation inherited#!",
3922 Error_Msg_N -- CODEFIX
3923 ("not fully conformant with declaration#!", Enode);
3927 Error_Msg_NE (Msg, Enode, N);
3929 end Conformance_Error;
3933 Old_Type : constant Entity_Id := Etype (Old_Id);
3934 New_Type : constant Entity_Id := Etype (New_Id);
3935 Old_Formal : Entity_Id;
3936 New_Formal : Entity_Id;
3937 Access_Types_Match : Boolean;
3938 Old_Formal_Base : Entity_Id;
3939 New_Formal_Base : Entity_Id;
3941 -- Start of processing for Check_Conformance
3946 -- We need a special case for operators, since they don't appear
3949 if Ctype = Type_Conformant then
3950 if Ekind (New_Id) = E_Operator
3951 and then Operator_Matches_Spec (New_Id, Old_Id)
3957 -- If both are functions/operators, check return types conform
3959 if Old_Type /= Standard_Void_Type
3960 and then New_Type /= Standard_Void_Type
3963 -- If we are checking interface conformance we omit controlling
3964 -- arguments and result, because we are only checking the conformance
3965 -- of the remaining parameters.
3967 if Has_Controlling_Result (Old_Id)
3968 and then Has_Controlling_Result (New_Id)
3969 and then Skip_Controlling_Formals
3973 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3974 Conformance_Error ("\return type does not match!", New_Id);
3978 -- Ada 2005 (AI-231): In case of anonymous access types check the
3979 -- null-exclusion and access-to-constant attributes match.
3981 if Ada_Version >= Ada_2005
3982 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3984 (Can_Never_Be_Null (Old_Type)
3985 /= Can_Never_Be_Null (New_Type)
3986 or else Is_Access_Constant (Etype (Old_Type))
3987 /= Is_Access_Constant (Etype (New_Type)))
3989 Conformance_Error ("\return type does not match!", New_Id);
3993 -- If either is a function/operator and the other isn't, error
3995 elsif Old_Type /= Standard_Void_Type
3996 or else New_Type /= Standard_Void_Type
3998 Conformance_Error ("\functions can only match functions!", New_Id);
4002 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4003 -- If this is a renaming as body, refine error message to indicate that
4004 -- the conflict is with the original declaration. If the entity is not
4005 -- frozen, the conventions don't have to match, the one of the renamed
4006 -- entity is inherited.
4008 if Ctype >= Subtype_Conformant then
4009 if Convention (Old_Id) /= Convention (New_Id) then
4011 if not Is_Frozen (New_Id) then
4014 elsif Present (Err_Loc)
4015 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4016 and then Present (Corresponding_Spec (Err_Loc))
4018 Error_Msg_Name_1 := Chars (New_Id);
4020 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4021 Conformance_Error ("\prior declaration for% has convention %!");
4024 Conformance_Error ("\calling conventions do not match!");
4029 elsif Is_Formal_Subprogram (Old_Id)
4030 or else Is_Formal_Subprogram (New_Id)
4032 Conformance_Error ("\formal subprograms not allowed!");
4037 -- Deal with parameters
4039 -- Note: we use the entity information, rather than going directly
4040 -- to the specification in the tree. This is not only simpler, but
4041 -- absolutely necessary for some cases of conformance tests between
4042 -- operators, where the declaration tree simply does not exist!
4044 Old_Formal := First_Formal (Old_Id);
4045 New_Formal := First_Formal (New_Id);
4046 while Present (Old_Formal) and then Present (New_Formal) loop
4047 if Is_Controlling_Formal (Old_Formal)
4048 and then Is_Controlling_Formal (New_Formal)
4049 and then Skip_Controlling_Formals
4051 -- The controlling formals will have different types when
4052 -- comparing an interface operation with its match, but both
4053 -- or neither must be access parameters.
4055 if Is_Access_Type (Etype (Old_Formal))
4057 Is_Access_Type (Etype (New_Formal))
4059 goto Skip_Controlling_Formal;
4062 ("\access parameter does not match!", New_Formal);
4066 if Ctype = Fully_Conformant then
4068 -- Names must match. Error message is more accurate if we do
4069 -- this before checking that the types of the formals match.
4071 if Chars (Old_Formal) /= Chars (New_Formal) then
4072 Conformance_Error ("\name & does not match!", New_Formal);
4074 -- Set error posted flag on new formal as well to stop
4075 -- junk cascaded messages in some cases.
4077 Set_Error_Posted (New_Formal);
4081 -- Null exclusion must match
4083 if Null_Exclusion_Present (Parent (Old_Formal))
4085 Null_Exclusion_Present (Parent (New_Formal))
4087 -- Only give error if both come from source. This should be
4088 -- investigated some time, since it should not be needed ???
4090 if Comes_From_Source (Old_Formal)
4092 Comes_From_Source (New_Formal)
4095 ("\null exclusion for & does not match", New_Formal);
4097 -- Mark error posted on the new formal to avoid duplicated
4098 -- complaint about types not matching.
4100 Set_Error_Posted (New_Formal);
4105 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4106 -- case occurs whenever a subprogram is being renamed and one of its
4107 -- parameters imposes a null exclusion. For example:
4109 -- type T is null record;
4110 -- type Acc_T is access T;
4111 -- subtype Acc_T_Sub is Acc_T;
4113 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4114 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4117 Old_Formal_Base := Etype (Old_Formal);
4118 New_Formal_Base := Etype (New_Formal);
4121 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4122 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4125 Access_Types_Match := Ada_Version >= Ada_2005
4127 -- Ensure that this rule is only applied when New_Id is a
4128 -- renaming of Old_Id.
4130 and then Nkind (Parent (Parent (New_Id))) =
4131 N_Subprogram_Renaming_Declaration
4132 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4133 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4134 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4136 -- Now handle the allowed access-type case
4138 and then Is_Access_Type (Old_Formal_Base)
4139 and then Is_Access_Type (New_Formal_Base)
4141 -- The type kinds must match. The only exception occurs with
4142 -- multiple generics of the form:
4145 -- type F is private; type A is private;
4146 -- type F_Ptr is access F; type A_Ptr is access A;
4147 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4148 -- package F_Pack is ... package A_Pack is
4149 -- package F_Inst is
4150 -- new F_Pack (A, A_Ptr, A_P);
4152 -- When checking for conformance between the parameters of A_P
4153 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4154 -- because the compiler has transformed A_Ptr into a subtype of
4155 -- F_Ptr. We catch this case in the code below.
4157 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4159 (Is_Generic_Type (Old_Formal_Base)
4160 and then Is_Generic_Type (New_Formal_Base)
4161 and then Is_Internal (New_Formal_Base)
4162 and then Etype (Etype (New_Formal_Base)) =
4164 and then Directly_Designated_Type (Old_Formal_Base) =
4165 Directly_Designated_Type (New_Formal_Base)
4166 and then ((Is_Itype (Old_Formal_Base)
4167 and then Can_Never_Be_Null (Old_Formal_Base))
4169 (Is_Itype (New_Formal_Base)
4170 and then Can_Never_Be_Null (New_Formal_Base)));
4172 -- Types must always match. In the visible part of an instance,
4173 -- usual overloading rules for dispatching operations apply, and
4174 -- we check base types (not the actual subtypes).
4176 if In_Instance_Visible_Part
4177 and then Is_Dispatching_Operation (New_Id)
4179 if not Conforming_Types
4180 (T1 => Base_Type (Etype (Old_Formal)),
4181 T2 => Base_Type (Etype (New_Formal)),
4183 Get_Inst => Get_Inst)
4184 and then not Access_Types_Match
4186 Conformance_Error ("\type of & does not match!", New_Formal);
4190 elsif not Conforming_Types
4191 (T1 => Old_Formal_Base,
4192 T2 => New_Formal_Base,
4194 Get_Inst => Get_Inst)
4195 and then not Access_Types_Match
4197 -- Don't give error message if old type is Any_Type. This test
4198 -- avoids some cascaded errors, e.g. in case of a bad spec.
4200 if Errmsg and then Old_Formal_Base = Any_Type then
4203 Conformance_Error ("\type of & does not match!", New_Formal);
4209 -- For mode conformance, mode must match
4211 if Ctype >= Mode_Conformant then
4212 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4213 Conformance_Error ("\mode of & does not match!", New_Formal);
4216 -- Part of mode conformance for access types is having the same
4217 -- constant modifier.
4219 elsif Access_Types_Match
4220 and then Is_Access_Constant (Old_Formal_Base) /=
4221 Is_Access_Constant (New_Formal_Base)
4224 ("\constant modifier does not match!", New_Formal);
4229 if Ctype >= Subtype_Conformant then
4231 -- Ada 2005 (AI-231): In case of anonymous access types check
4232 -- the null-exclusion and access-to-constant attributes must
4233 -- match. For null exclusion, we test the types rather than the
4234 -- formals themselves, since the attribute is only set reliably
4235 -- on the formals in the Ada 95 case, and we exclude the case
4236 -- where Old_Formal is marked as controlling, to avoid errors
4237 -- when matching completing bodies with dispatching declarations
4238 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
4240 if Ada_Version >= Ada_2005
4241 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4242 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4244 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
4245 Can_Never_Be_Null (Etype (New_Formal))
4247 not Is_Controlling_Formal (Old_Formal))
4249 Is_Access_Constant (Etype (Old_Formal)) /=
4250 Is_Access_Constant (Etype (New_Formal)))
4252 -- Do not complain if error already posted on New_Formal. This
4253 -- avoids some redundant error messages.
4255 and then not Error_Posted (New_Formal)
4257 -- It is allowed to omit the null-exclusion in case of stream
4258 -- attribute subprograms. We recognize stream subprograms
4259 -- through their TSS-generated suffix.
4262 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4264 if TSS_Name /= TSS_Stream_Read
4265 and then TSS_Name /= TSS_Stream_Write
4266 and then TSS_Name /= TSS_Stream_Input
4267 and then TSS_Name /= TSS_Stream_Output
4270 ("\type of & does not match!", New_Formal);
4277 -- Full conformance checks
4279 if Ctype = Fully_Conformant then
4281 -- We have checked already that names match
4283 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4285 -- Check default expressions for in parameters
4288 NewD : constant Boolean :=
4289 Present (Default_Value (New_Formal));
4290 OldD : constant Boolean :=
4291 Present (Default_Value (Old_Formal));
4293 if NewD or OldD then
4295 -- The old default value has been analyzed because the
4296 -- current full declaration will have frozen everything
4297 -- before. The new default value has not been analyzed,
4298 -- so analyze it now before we check for conformance.
4301 Push_Scope (New_Id);
4302 Preanalyze_Spec_Expression
4303 (Default_Value (New_Formal), Etype (New_Formal));
4307 if not (NewD and OldD)
4308 or else not Fully_Conformant_Expressions
4309 (Default_Value (Old_Formal),
4310 Default_Value (New_Formal))
4313 ("\default expression for & does not match!",
4322 -- A couple of special checks for Ada 83 mode. These checks are
4323 -- skipped if either entity is an operator in package Standard,
4324 -- or if either old or new instance is not from the source program.
4326 if Ada_Version = Ada_83
4327 and then Sloc (Old_Id) > Standard_Location
4328 and then Sloc (New_Id) > Standard_Location
4329 and then Comes_From_Source (Old_Id)
4330 and then Comes_From_Source (New_Id)
4333 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4334 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4337 -- Explicit IN must be present or absent in both cases. This
4338 -- test is required only in the full conformance case.
4340 if In_Present (Old_Param) /= In_Present (New_Param)
4341 and then Ctype = Fully_Conformant
4344 ("\(Ada 83) IN must appear in both declarations",
4349 -- Grouping (use of comma in param lists) must be the same
4350 -- This is where we catch a misconformance like:
4353 -- A : Integer; B : Integer
4355 -- which are represented identically in the tree except
4356 -- for the setting of the flags More_Ids and Prev_Ids.
4358 if More_Ids (Old_Param) /= More_Ids (New_Param)
4359 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
4362 ("\grouping of & does not match!", New_Formal);
4368 -- This label is required when skipping controlling formals
4370 <<Skip_Controlling_Formal>>
4372 Next_Formal (Old_Formal);
4373 Next_Formal (New_Formal);
4376 if Present (Old_Formal) then
4377 Conformance_Error ("\too few parameters!");
4380 elsif Present (New_Formal) then
4381 Conformance_Error ("\too many parameters!", New_Formal);
4384 end Check_Conformance;
4386 -----------------------
4387 -- Check_Conventions --
4388 -----------------------
4390 procedure Check_Conventions (Typ : Entity_Id) is
4391 Ifaces_List : Elist_Id;
4393 procedure Check_Convention (Op : Entity_Id);
4394 -- Verify that the convention of inherited dispatching operation Op is
4395 -- consistent among all subprograms it overrides. In order to minimize
4396 -- the search, Search_From is utilized to designate a specific point in
4397 -- the list rather than iterating over the whole list once more.
4399 ----------------------
4400 -- Check_Convention --
4401 ----------------------
4403 procedure Check_Convention (Op : Entity_Id) is
4404 Iface_Elmt : Elmt_Id;
4405 Iface_Prim_Elmt : Elmt_Id;
4406 Iface_Prim : Entity_Id;
4409 Iface_Elmt := First_Elmt (Ifaces_List);
4410 while Present (Iface_Elmt) loop
4412 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4413 while Present (Iface_Prim_Elmt) loop
4414 Iface_Prim := Node (Iface_Prim_Elmt);
4416 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4417 and then Convention (Iface_Prim) /= Convention (Op)
4420 ("inconsistent conventions in primitive operations", Typ);
4422 Error_Msg_Name_1 := Chars (Op);
4423 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4424 Error_Msg_Sloc := Sloc (Op);
4426 if Comes_From_Source (Op) or else No (Alias (Op)) then
4427 if not Present (Overridden_Operation (Op)) then
4428 Error_Msg_N ("\\primitive % defined #", Typ);
4431 ("\\overriding operation % with " &
4432 "convention % defined #", Typ);
4435 else pragma Assert (Present (Alias (Op)));
4436 Error_Msg_Sloc := Sloc (Alias (Op));
4438 ("\\inherited operation % with " &
4439 "convention % defined #", Typ);
4442 Error_Msg_Name_1 := Chars (Op);
4444 Get_Convention_Name (Convention (Iface_Prim));
4445 Error_Msg_Sloc := Sloc (Iface_Prim);
4447 ("\\overridden operation % with " &
4448 "convention % defined #", Typ);
4450 -- Avoid cascading errors
4455 Next_Elmt (Iface_Prim_Elmt);
4458 Next_Elmt (Iface_Elmt);
4460 end Check_Convention;
4464 Prim_Op : Entity_Id;
4465 Prim_Op_Elmt : Elmt_Id;
4467 -- Start of processing for Check_Conventions
4470 if not Has_Interfaces (Typ) then
4474 Collect_Interfaces (Typ, Ifaces_List);
4476 -- The algorithm checks every overriding dispatching operation against
4477 -- all the corresponding overridden dispatching operations, detecting
4478 -- differences in conventions.
4480 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4481 while Present (Prim_Op_Elmt) loop
4482 Prim_Op := Node (Prim_Op_Elmt);
4484 -- A small optimization: skip the predefined dispatching operations
4485 -- since they always have the same convention.
4487 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4488 Check_Convention (Prim_Op);
4491 Next_Elmt (Prim_Op_Elmt);
4493 end Check_Conventions;
4495 ------------------------------
4496 -- Check_Delayed_Subprogram --
4497 ------------------------------
4499 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4502 procedure Possible_Freeze (T : Entity_Id);
4503 -- T is the type of either a formal parameter or of the return type.
4504 -- If T is not yet frozen and needs a delayed freeze, then the
4505 -- subprogram itself must be delayed. If T is the limited view of an
4506 -- incomplete type the subprogram must be frozen as well, because
4507 -- T may depend on local types that have not been frozen yet.
4509 ---------------------
4510 -- Possible_Freeze --
4511 ---------------------
4513 procedure Possible_Freeze (T : Entity_Id) is
4515 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4516 Set_Has_Delayed_Freeze (Designator);
4518 elsif Is_Access_Type (T)
4519 and then Has_Delayed_Freeze (Designated_Type (T))
4520 and then not Is_Frozen (Designated_Type (T))
4522 Set_Has_Delayed_Freeze (Designator);
4524 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4525 Set_Has_Delayed_Freeze (Designator);
4528 end Possible_Freeze;
4530 -- Start of processing for Check_Delayed_Subprogram
4533 -- All subprograms, including abstract subprograms, may need a freeze
4534 -- node if some formal type or the return type needs one.
4536 Possible_Freeze (Etype (Designator));
4537 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4539 -- Need delayed freeze if any of the formal types themselves need
4540 -- a delayed freeze and are not yet frozen.
4542 F := First_Formal (Designator);
4543 while Present (F) loop
4544 Possible_Freeze (Etype (F));
4545 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4549 -- Mark functions that return by reference. Note that it cannot be
4550 -- done for delayed_freeze subprograms because the underlying
4551 -- returned type may not be known yet (for private types)
4553 if not Has_Delayed_Freeze (Designator)
4554 and then Expander_Active
4557 Typ : constant Entity_Id := Etype (Designator);
4558 Utyp : constant Entity_Id := Underlying_Type (Typ);
4561 if Is_Immutably_Limited_Type (Typ) then
4562 Set_Returns_By_Ref (Designator);
4564 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4565 Set_Returns_By_Ref (Designator);
4569 end Check_Delayed_Subprogram;
4571 ------------------------------------
4572 -- Check_Discriminant_Conformance --
4573 ------------------------------------
4575 procedure Check_Discriminant_Conformance
4580 Old_Discr : Entity_Id := First_Discriminant (Prev);
4581 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4582 New_Discr_Id : Entity_Id;
4583 New_Discr_Type : Entity_Id;
4585 procedure Conformance_Error (Msg : String; N : Node_Id);
4586 -- Post error message for conformance error on given node. Two messages
4587 -- are output. The first points to the previous declaration with a
4588 -- general "no conformance" message. The second is the detailed reason,
4589 -- supplied as Msg. The parameter N provide information for a possible
4590 -- & insertion in the message.
4592 -----------------------
4593 -- Conformance_Error --
4594 -----------------------
4596 procedure Conformance_Error (Msg : String; N : Node_Id) is
4598 Error_Msg_Sloc := Sloc (Prev_Loc);
4599 Error_Msg_N -- CODEFIX
4600 ("not fully conformant with declaration#!", N);
4601 Error_Msg_NE (Msg, N, N);
4602 end Conformance_Error;
4604 -- Start of processing for Check_Discriminant_Conformance
4607 while Present (Old_Discr) and then Present (New_Discr) loop
4609 New_Discr_Id := Defining_Identifier (New_Discr);
4611 -- The subtype mark of the discriminant on the full type has not
4612 -- been analyzed so we do it here. For an access discriminant a new
4615 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4617 Access_Definition (N, Discriminant_Type (New_Discr));
4620 Analyze (Discriminant_Type (New_Discr));
4621 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4623 -- Ada 2005: if the discriminant definition carries a null
4624 -- exclusion, create an itype to check properly for consistency
4625 -- with partial declaration.
4627 if Is_Access_Type (New_Discr_Type)
4628 and then Null_Exclusion_Present (New_Discr)
4631 Create_Null_Excluding_Itype
4632 (T => New_Discr_Type,
4633 Related_Nod => New_Discr,
4634 Scope_Id => Current_Scope);
4638 if not Conforming_Types
4639 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4641 Conformance_Error ("type of & does not match!", New_Discr_Id);
4644 -- Treat the new discriminant as an occurrence of the old one,
4645 -- for navigation purposes, and fill in some semantic
4646 -- information, for completeness.
4648 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4649 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4650 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4655 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4656 Conformance_Error ("name & does not match!", New_Discr_Id);
4660 -- Default expressions must match
4663 NewD : constant Boolean :=
4664 Present (Expression (New_Discr));
4665 OldD : constant Boolean :=
4666 Present (Expression (Parent (Old_Discr)));
4669 if NewD or OldD then
4671 -- The old default value has been analyzed and expanded,
4672 -- because the current full declaration will have frozen
4673 -- everything before. The new default values have not been
4674 -- expanded, so expand now to check conformance.
4677 Preanalyze_Spec_Expression
4678 (Expression (New_Discr), New_Discr_Type);
4681 if not (NewD and OldD)
4682 or else not Fully_Conformant_Expressions
4683 (Expression (Parent (Old_Discr)),
4684 Expression (New_Discr))
4688 ("default expression for & does not match!",
4695 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4697 if Ada_Version = Ada_83 then
4699 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4702 -- Grouping (use of comma in param lists) must be the same
4703 -- This is where we catch a misconformance like:
4706 -- A : Integer; B : Integer
4708 -- which are represented identically in the tree except
4709 -- for the setting of the flags More_Ids and Prev_Ids.
4711 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4712 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4715 ("grouping of & does not match!", New_Discr_Id);
4721 Next_Discriminant (Old_Discr);
4725 if Present (Old_Discr) then
4726 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4729 elsif Present (New_Discr) then
4731 ("too many discriminants!", Defining_Identifier (New_Discr));
4734 end Check_Discriminant_Conformance;
4736 ----------------------------
4737 -- Check_Fully_Conformant --
4738 ----------------------------
4740 procedure Check_Fully_Conformant
4741 (New_Id : Entity_Id;
4743 Err_Loc : Node_Id := Empty)
4746 pragma Warnings (Off, Result);
4749 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4750 end Check_Fully_Conformant;
4752 ---------------------------
4753 -- Check_Mode_Conformant --
4754 ---------------------------
4756 procedure Check_Mode_Conformant
4757 (New_Id : Entity_Id;
4759 Err_Loc : Node_Id := Empty;
4760 Get_Inst : Boolean := False)
4763 pragma Warnings (Off, Result);
4766 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4767 end Check_Mode_Conformant;
4769 --------------------------------
4770 -- Check_Overriding_Indicator --
4771 --------------------------------
4773 procedure Check_Overriding_Indicator
4775 Overridden_Subp : Entity_Id;
4776 Is_Primitive : Boolean)
4782 -- No overriding indicator for literals
4784 if Ekind (Subp) = E_Enumeration_Literal then
4787 elsif Ekind (Subp) = E_Entry then
4788 Decl := Parent (Subp);
4790 -- No point in analyzing a malformed operator
4792 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4793 and then Error_Posted (Subp)
4798 Decl := Unit_Declaration_Node (Subp);
4801 if Nkind_In (Decl, N_Subprogram_Body,
4802 N_Subprogram_Body_Stub,
4803 N_Subprogram_Declaration,
4804 N_Abstract_Subprogram_Declaration,
4805 N_Subprogram_Renaming_Declaration)
4807 Spec := Specification (Decl);
4809 elsif Nkind (Decl) = N_Entry_Declaration then
4816 -- The overriding operation is type conformant with the overridden one,
4817 -- but the names of the formals are not required to match. If the names
4818 -- appear permuted in the overriding operation, this is a possible
4819 -- source of confusion that is worth diagnosing. Controlling formals
4820 -- often carry names that reflect the type, and it is not worthwhile
4821 -- requiring that their names match.
4823 if Present (Overridden_Subp)
4824 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4831 Form1 := First_Formal (Subp);
4832 Form2 := First_Formal (Overridden_Subp);
4834 -- If the overriding operation is a synchronized operation, skip
4835 -- the first parameter of the overridden operation, which is
4836 -- implicit in the new one. If the operation is declared in the
4837 -- body it is not primitive and all formals must match.
4839 if Is_Concurrent_Type (Scope (Subp))
4840 and then Is_Tagged_Type (Scope (Subp))
4841 and then not Has_Completion (Scope (Subp))
4843 Form2 := Next_Formal (Form2);
4846 if Present (Form1) then
4847 Form1 := Next_Formal (Form1);
4848 Form2 := Next_Formal (Form2);
4851 while Present (Form1) loop
4852 if not Is_Controlling_Formal (Form1)
4853 and then Present (Next_Formal (Form2))
4854 and then Chars (Form1) = Chars (Next_Formal (Form2))
4856 Error_Msg_Node_2 := Alias (Overridden_Subp);
4857 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4859 ("& does not match corresponding formal of&#",
4864 Next_Formal (Form1);
4865 Next_Formal (Form2);
4870 -- If there is an overridden subprogram, then check that there is no
4871 -- "not overriding" indicator, and mark the subprogram as overriding.
4872 -- This is not done if the overridden subprogram is marked as hidden,
4873 -- which can occur for the case of inherited controlled operations
4874 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4875 -- subprogram is not itself hidden. (Note: This condition could probably
4876 -- be simplified, leaving out the testing for the specific controlled
4877 -- cases, but it seems safer and clearer this way, and echoes similar
4878 -- special-case tests of this kind in other places.)
4880 if Present (Overridden_Subp)
4881 and then (not Is_Hidden (Overridden_Subp)
4883 ((Chars (Overridden_Subp) = Name_Initialize
4885 Chars (Overridden_Subp) = Name_Adjust
4887 Chars (Overridden_Subp) = Name_Finalize)
4888 and then Present (Alias (Overridden_Subp))
4889 and then not Is_Hidden (Alias (Overridden_Subp))))
4891 if Must_Not_Override (Spec) then
4892 Error_Msg_Sloc := Sloc (Overridden_Subp);
4894 if Ekind (Subp) = E_Entry then
4896 ("entry & overrides inherited operation #", Spec, Subp);
4899 ("subprogram & overrides inherited operation #", Spec, Subp);
4902 elsif Is_Subprogram (Subp) then
4903 if Is_Init_Proc (Subp) then
4906 elsif No (Overridden_Operation (Subp)) then
4908 -- For entities generated by Derive_Subprograms the overridden
4909 -- operation is the inherited primitive (which is available
4910 -- through the attribute alias)
4912 if (Is_Dispatching_Operation (Subp)
4913 or else Is_Dispatching_Operation (Overridden_Subp))
4914 and then not Comes_From_Source (Overridden_Subp)
4915 and then Find_Dispatching_Type (Overridden_Subp) =
4916 Find_Dispatching_Type (Subp)
4917 and then Present (Alias (Overridden_Subp))
4918 and then Comes_From_Source (Alias (Overridden_Subp))
4920 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
4923 Set_Overridden_Operation (Subp, Overridden_Subp);
4928 -- If primitive flag is set or this is a protected operation, then
4929 -- the operation is overriding at the point of its declaration, so
4930 -- warn if necessary. Otherwise it may have been declared before the
4931 -- operation it overrides and no check is required.
4934 and then not Must_Override (Spec)
4935 and then (Is_Primitive
4936 or else Ekind (Scope (Subp)) = E_Protected_Type)
4938 Style.Missing_Overriding (Decl, Subp);
4941 -- If Subp is an operator, it may override a predefined operation, if
4942 -- it is defined in the same scope as the type to which it applies.
4943 -- In that case Overridden_Subp is empty because of our implicit
4944 -- representation for predefined operators. We have to check whether the
4945 -- signature of Subp matches that of a predefined operator. Note that
4946 -- first argument provides the name of the operator, and the second
4947 -- argument the signature that may match that of a standard operation.
4948 -- If the indicator is overriding, then the operator must match a
4949 -- predefined signature, because we know already that there is no
4950 -- explicit overridden operation.
4952 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4953 if Must_Not_Override (Spec) then
4955 -- If this is not a primitive or a protected subprogram, then
4956 -- "not overriding" is illegal.
4959 and then Ekind (Scope (Subp)) /= E_Protected_Type
4962 ("overriding indicator only allowed "
4963 & "if subprogram is primitive", Subp);
4965 elsif Can_Override_Operator (Subp) then
4967 ("subprogram& overrides predefined operator ", Spec, Subp);
4970 elsif Must_Override (Spec) then
4971 if No (Overridden_Operation (Subp))
4972 and then not Can_Override_Operator (Subp)
4974 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4977 elsif not Error_Posted (Subp)
4978 and then Style_Check
4979 and then Can_Override_Operator (Subp)
4981 not Is_Predefined_File_Name
4982 (Unit_File_Name (Get_Source_Unit (Subp)))
4984 -- If style checks are enabled, indicate that the indicator is
4985 -- missing. However, at the point of declaration, the type of
4986 -- which this is a primitive operation may be private, in which
4987 -- case the indicator would be premature.
4989 if Has_Private_Declaration (Etype (Subp))
4990 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4994 Style.Missing_Overriding (Decl, Subp);
4998 elsif Must_Override (Spec) then
4999 if Ekind (Subp) = E_Entry then
5000 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5002 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5005 -- If the operation is marked "not overriding" and it's not primitive
5006 -- then an error is issued, unless this is an operation of a task or
5007 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5008 -- has been specified have already been checked above.
5010 elsif Must_Not_Override (Spec)
5011 and then not Is_Primitive
5012 and then Ekind (Subp) /= E_Entry
5013 and then Ekind (Scope (Subp)) /= E_Protected_Type
5016 ("overriding indicator only allowed if subprogram is primitive",
5020 end Check_Overriding_Indicator;
5026 -- Note: this procedure needs to know far too much about how the expander
5027 -- messes with exceptions. The use of the flag Exception_Junk and the
5028 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5029 -- works, but is not very clean. It would be better if the expansion
5030 -- routines would leave Original_Node working nicely, and we could use
5031 -- Original_Node here to ignore all the peculiar expander messing ???
5033 procedure Check_Returns
5037 Proc : Entity_Id := Empty)
5041 procedure Check_Statement_Sequence (L : List_Id);
5042 -- Internal recursive procedure to check a list of statements for proper
5043 -- termination by a return statement (or a transfer of control or a
5044 -- compound statement that is itself internally properly terminated).
5046 ------------------------------
5047 -- Check_Statement_Sequence --
5048 ------------------------------
5050 procedure Check_Statement_Sequence (L : List_Id) is
5055 Raise_Exception_Call : Boolean;
5056 -- Set True if statement sequence terminated by Raise_Exception call
5057 -- or a Reraise_Occurrence call.
5060 Raise_Exception_Call := False;
5062 -- Get last real statement
5064 Last_Stm := Last (L);
5066 -- Deal with digging out exception handler statement sequences that
5067 -- have been transformed by the local raise to goto optimization.
5068 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5069 -- optimization has occurred, we are looking at something like:
5072 -- original stmts in block
5076 -- goto L1; | omitted if No_Exception_Propagation
5081 -- goto L3; -- skip handler when exception not raised
5083 -- <<L1>> -- target label for local exception
5097 -- and what we have to do is to dig out the estmts1 and estmts2
5098 -- sequences (which were the original sequences of statements in
5099 -- the exception handlers) and check them.
5101 if Nkind (Last_Stm) = N_Label
5102 and then Exception_Junk (Last_Stm)
5108 exit when Nkind (Stm) /= N_Block_Statement;
5109 exit when not Exception_Junk (Stm);
5112 exit when Nkind (Stm) /= N_Label;
5113 exit when not Exception_Junk (Stm);
5114 Check_Statement_Sequence
5115 (Statements (Handled_Statement_Sequence (Next (Stm))));
5120 exit when Nkind (Stm) /= N_Goto_Statement;
5121 exit when not Exception_Junk (Stm);
5125 -- Don't count pragmas
5127 while Nkind (Last_Stm) = N_Pragma
5129 -- Don't count call to SS_Release (can happen after Raise_Exception)
5132 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5134 Nkind (Name (Last_Stm)) = N_Identifier
5136 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5138 -- Don't count exception junk
5141 (Nkind_In (Last_Stm, N_Goto_Statement,
5143 N_Object_Declaration)
5144 and then Exception_Junk (Last_Stm))
5145 or else Nkind (Last_Stm) in N_Push_xxx_Label
5146 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5151 -- Here we have the "real" last statement
5153 Kind := Nkind (Last_Stm);
5155 -- Transfer of control, OK. Note that in the No_Return procedure
5156 -- case, we already diagnosed any explicit return statements, so
5157 -- we can treat them as OK in this context.
5159 if Is_Transfer (Last_Stm) then
5162 -- Check cases of explicit non-indirect procedure calls
5164 elsif Kind = N_Procedure_Call_Statement
5165 and then Is_Entity_Name (Name (Last_Stm))
5167 -- Check call to Raise_Exception procedure which is treated
5168 -- specially, as is a call to Reraise_Occurrence.
5170 -- We suppress the warning in these cases since it is likely that
5171 -- the programmer really does not expect to deal with the case
5172 -- of Null_Occurrence, and thus would find a warning about a
5173 -- missing return curious, and raising Program_Error does not
5174 -- seem such a bad behavior if this does occur.
5176 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5177 -- behavior will be to raise Constraint_Error (see AI-329).
5179 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
5181 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
5183 Raise_Exception_Call := True;
5185 -- For Raise_Exception call, test first argument, if it is
5186 -- an attribute reference for a 'Identity call, then we know
5187 -- that the call cannot possibly return.
5190 Arg : constant Node_Id :=
5191 Original_Node (First_Actual (Last_Stm));
5193 if Nkind (Arg) = N_Attribute_Reference
5194 and then Attribute_Name (Arg) = Name_Identity
5201 -- If statement, need to look inside if there is an else and check
5202 -- each constituent statement sequence for proper termination.
5204 elsif Kind = N_If_Statement
5205 and then Present (Else_Statements (Last_Stm))
5207 Check_Statement_Sequence (Then_Statements (Last_Stm));
5208 Check_Statement_Sequence (Else_Statements (Last_Stm));
5210 if Present (Elsif_Parts (Last_Stm)) then
5212 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
5215 while Present (Elsif_Part) loop
5216 Check_Statement_Sequence (Then_Statements (Elsif_Part));
5224 -- Case statement, check each case for proper termination
5226 elsif Kind = N_Case_Statement then
5230 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
5231 while Present (Case_Alt) loop
5232 Check_Statement_Sequence (Statements (Case_Alt));
5233 Next_Non_Pragma (Case_Alt);
5239 -- Block statement, check its handled sequence of statements
5241 elsif Kind = N_Block_Statement then
5247 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
5256 -- Loop statement. If there is an iteration scheme, we can definitely
5257 -- fall out of the loop. Similarly if there is an exit statement, we
5258 -- can fall out. In either case we need a following return.
5260 elsif Kind = N_Loop_Statement then
5261 if Present (Iteration_Scheme (Last_Stm))
5262 or else Has_Exit (Entity (Identifier (Last_Stm)))
5266 -- A loop with no exit statement or iteration scheme is either
5267 -- an infinite loop, or it has some other exit (raise/return).
5268 -- In either case, no warning is required.
5274 -- Timed entry call, check entry call and delay alternatives
5276 -- Note: in expanded code, the timed entry call has been converted
5277 -- to a set of expanded statements on which the check will work
5278 -- correctly in any case.
5280 elsif Kind = N_Timed_Entry_Call then
5282 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5283 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
5286 -- If statement sequence of entry call alternative is missing,
5287 -- then we can definitely fall through, and we post the error
5288 -- message on the entry call alternative itself.
5290 if No (Statements (ECA)) then
5293 -- If statement sequence of delay alternative is missing, then
5294 -- we can definitely fall through, and we post the error
5295 -- message on the delay alternative itself.
5297 -- Note: if both ECA and DCA are missing the return, then we
5298 -- post only one message, should be enough to fix the bugs.
5299 -- If not we will get a message next time on the DCA when the
5302 elsif No (Statements (DCA)) then
5305 -- Else check both statement sequences
5308 Check_Statement_Sequence (Statements (ECA));
5309 Check_Statement_Sequence (Statements (DCA));
5314 -- Conditional entry call, check entry call and else part
5316 -- Note: in expanded code, the conditional entry call has been
5317 -- converted to a set of expanded statements on which the check
5318 -- will work correctly in any case.
5320 elsif Kind = N_Conditional_Entry_Call then
5322 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5325 -- If statement sequence of entry call alternative is missing,
5326 -- then we can definitely fall through, and we post the error
5327 -- message on the entry call alternative itself.
5329 if No (Statements (ECA)) then
5332 -- Else check statement sequence and else part
5335 Check_Statement_Sequence (Statements (ECA));
5336 Check_Statement_Sequence (Else_Statements (Last_Stm));
5342 -- If we fall through, issue appropriate message
5345 if not Raise_Exception_Call then
5347 ("?RETURN statement missing following this statement!",
5350 ("\?Program_Error may be raised at run time!",
5354 -- Note: we set Err even though we have not issued a warning
5355 -- because we still have a case of a missing return. This is
5356 -- an extremely marginal case, probably will never be noticed
5357 -- but we might as well get it right.
5361 -- Otherwise we have the case of a procedure marked No_Return
5364 if not Raise_Exception_Call then
5366 ("?implied return after this statement " &
5367 "will raise Program_Error",
5370 ("\?procedure & is marked as No_Return!",
5375 RE : constant Node_Id :=
5376 Make_Raise_Program_Error (Sloc (Last_Stm),
5377 Reason => PE_Implicit_Return);
5379 Insert_After (Last_Stm, RE);
5383 end Check_Statement_Sequence;
5385 -- Start of processing for Check_Returns
5389 Check_Statement_Sequence (Statements (HSS));
5391 if Present (Exception_Handlers (HSS)) then
5392 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5393 while Present (Handler) loop
5394 Check_Statement_Sequence (Statements (Handler));
5395 Next_Non_Pragma (Handler);
5400 ----------------------------
5401 -- Check_Subprogram_Order --
5402 ----------------------------
5404 procedure Check_Subprogram_Order (N : Node_Id) is
5406 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5407 -- This is used to check if S1 > S2 in the sense required by this
5408 -- test, for example nameab < namec, but name2 < name10.
5410 -----------------------------
5411 -- Subprogram_Name_Greater --
5412 -----------------------------
5414 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5419 -- Remove trailing numeric parts
5422 while S1 (L1) in '0' .. '9' loop
5427 while S2 (L2) in '0' .. '9' loop
5431 -- If non-numeric parts non-equal, that's decisive
5433 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5436 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5439 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5440 -- that a missing suffix is treated as numeric zero in this test.
5444 while L1 < S1'Last loop
5446 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5450 while L2 < S2'Last loop
5452 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5457 end Subprogram_Name_Greater;
5459 -- Start of processing for Check_Subprogram_Order
5462 -- Check body in alpha order if this is option
5465 and then Style_Check_Order_Subprograms
5466 and then Nkind (N) = N_Subprogram_Body
5467 and then Comes_From_Source (N)
5468 and then In_Extended_Main_Source_Unit (N)
5472 renames Scope_Stack.Table
5473 (Scope_Stack.Last).Last_Subprogram_Name;
5475 Body_Id : constant Entity_Id :=
5476 Defining_Entity (Specification (N));
5479 Get_Decoded_Name_String (Chars (Body_Id));
5482 if Subprogram_Name_Greater
5483 (LSN.all, Name_Buffer (1 .. Name_Len))
5485 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5491 LSN := new String'(Name_Buffer (1 .. Name_Len));
5494 end Check_Subprogram_Order;
5496 ------------------------------
5497 -- Check_Subtype_Conformant --
5498 ------------------------------
5500 procedure Check_Subtype_Conformant
5501 (New_Id : Entity_Id;
5503 Err_Loc : Node_Id := Empty;
5504 Skip_Controlling_Formals : Boolean := False)
5507 pragma Warnings (Off, Result);
5510 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5511 Skip_Controlling_Formals => Skip_Controlling_Formals);
5512 end Check_Subtype_Conformant;
5514 ---------------------------
5515 -- Check_Type_Conformant --
5516 ---------------------------
5518 procedure Check_Type_Conformant
5519 (New_Id : Entity_Id;
5521 Err_Loc : Node_Id := Empty)
5524 pragma Warnings (Off, Result);
5527 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5528 end Check_Type_Conformant;
5530 ---------------------------
5531 -- Can_Override_Operator --
5532 ---------------------------
5534 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
5537 if Nkind (Subp) /= N_Defining_Operator_Symbol then
5541 Typ := Base_Type (Etype (First_Formal (Subp)));
5543 return Operator_Matches_Spec (Subp, Subp)
5544 and then Scope (Subp) = Scope (Typ)
5545 and then not Is_Class_Wide_Type (Typ);
5547 end Can_Override_Operator;
5549 ----------------------
5550 -- Conforming_Types --
5551 ----------------------
5553 function Conforming_Types
5556 Ctype : Conformance_Type;
5557 Get_Inst : Boolean := False) return Boolean
5559 Type_1 : Entity_Id := T1;
5560 Type_2 : Entity_Id := T2;
5561 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5563 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5564 -- If neither T1 nor T2 are generic actual types, or if they are in
5565 -- different scopes (e.g. parent and child instances), then verify that
5566 -- the base types are equal. Otherwise T1 and T2 must be on the same
5567 -- subtype chain. The whole purpose of this procedure is to prevent
5568 -- spurious ambiguities in an instantiation that may arise if two
5569 -- distinct generic types are instantiated with the same actual.
5571 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5572 -- An access parameter can designate an incomplete type. If the
5573 -- incomplete type is the limited view of a type from a limited_
5574 -- with_clause, check whether the non-limited view is available. If
5575 -- it is a (non-limited) incomplete type, get the full view.
5577 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5578 -- Returns True if and only if either T1 denotes a limited view of T2
5579 -- or T2 denotes a limited view of T1. This can arise when the limited
5580 -- with view of a type is used in a subprogram declaration and the
5581 -- subprogram body is in the scope of a regular with clause for the
5582 -- same unit. In such a case, the two type entities can be considered
5583 -- identical for purposes of conformance checking.
5585 ----------------------
5586 -- Base_Types_Match --
5587 ----------------------
5589 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5594 elsif Base_Type (T1) = Base_Type (T2) then
5596 -- The following is too permissive. A more precise test should
5597 -- check that the generic actual is an ancestor subtype of the
5600 return not Is_Generic_Actual_Type (T1)
5601 or else not Is_Generic_Actual_Type (T2)
5602 or else Scope (T1) /= Scope (T2);
5607 end Base_Types_Match;
5609 --------------------------
5610 -- Find_Designated_Type --
5611 --------------------------
5613 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5617 Desig := Directly_Designated_Type (T);
5619 if Ekind (Desig) = E_Incomplete_Type then
5621 -- If regular incomplete type, get full view if available
5623 if Present (Full_View (Desig)) then
5624 Desig := Full_View (Desig);
5626 -- If limited view of a type, get non-limited view if available,
5627 -- and check again for a regular incomplete type.
5629 elsif Present (Non_Limited_View (Desig)) then
5630 Desig := Get_Full_View (Non_Limited_View (Desig));
5635 end Find_Designated_Type;
5637 -------------------------------
5638 -- Matches_Limited_With_View --
5639 -------------------------------
5641 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5643 -- In some cases a type imported through a limited_with clause, and
5644 -- its nonlimited view are both visible, for example in an anonymous
5645 -- access-to-class-wide type in a formal. Both entities designate the
5648 if From_With_Type (T1)
5649 and then T2 = Available_View (T1)
5653 elsif From_With_Type (T2)
5654 and then T1 = Available_View (T2)
5661 end Matches_Limited_With_View;
5663 -- Start of processing for Conforming_Types
5666 -- The context is an instance association for a formal
5667 -- access-to-subprogram type; the formal parameter types require
5668 -- mapping because they may denote other formal parameters of the
5672 Type_1 := Get_Instance_Of (T1);
5673 Type_2 := Get_Instance_Of (T2);
5676 -- If one of the types is a view of the other introduced by a limited
5677 -- with clause, treat these as conforming for all purposes.
5679 if Matches_Limited_With_View (T1, T2) then
5682 elsif Base_Types_Match (Type_1, Type_2) then
5683 return Ctype <= Mode_Conformant
5684 or else Subtypes_Statically_Match (Type_1, Type_2);
5686 elsif Is_Incomplete_Or_Private_Type (Type_1)
5687 and then Present (Full_View (Type_1))
5688 and then Base_Types_Match (Full_View (Type_1), Type_2)
5690 return Ctype <= Mode_Conformant
5691 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5693 elsif Ekind (Type_2) = E_Incomplete_Type
5694 and then Present (Full_View (Type_2))
5695 and then Base_Types_Match (Type_1, Full_View (Type_2))
5697 return Ctype <= Mode_Conformant
5698 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5700 elsif Is_Private_Type (Type_2)
5701 and then In_Instance
5702 and then Present (Full_View (Type_2))
5703 and then Base_Types_Match (Type_1, Full_View (Type_2))
5705 return Ctype <= Mode_Conformant
5706 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5709 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5710 -- treated recursively because they carry a signature.
5712 Are_Anonymous_Access_To_Subprogram_Types :=
5713 Ekind (Type_1) = Ekind (Type_2)
5715 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5717 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5719 -- Test anonymous access type case. For this case, static subtype
5720 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5721 -- the base types because we may have built internal subtype entities
5722 -- to handle null-excluding types (see Process_Formals).
5724 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5726 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5727 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5730 Desig_1 : Entity_Id;
5731 Desig_2 : Entity_Id;
5734 -- In Ada2005, access constant indicators must match for
5735 -- subtype conformance.
5737 if Ada_Version >= Ada_2005
5738 and then Ctype >= Subtype_Conformant
5740 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5745 Desig_1 := Find_Designated_Type (Type_1);
5746 Desig_2 := Find_Designated_Type (Type_2);
5748 -- If the context is an instance association for a formal
5749 -- access-to-subprogram type; formal access parameter designated
5750 -- types require mapping because they may denote other formal
5751 -- parameters of the generic unit.
5754 Desig_1 := Get_Instance_Of (Desig_1);
5755 Desig_2 := Get_Instance_Of (Desig_2);
5758 -- It is possible for a Class_Wide_Type to be introduced for an
5759 -- incomplete type, in which case there is a separate class_ wide
5760 -- type for the full view. The types conform if their Etypes
5761 -- conform, i.e. one may be the full view of the other. This can
5762 -- only happen in the context of an access parameter, other uses
5763 -- of an incomplete Class_Wide_Type are illegal.
5765 if Is_Class_Wide_Type (Desig_1)
5767 Is_Class_Wide_Type (Desig_2)
5771 (Etype (Base_Type (Desig_1)),
5772 Etype (Base_Type (Desig_2)), Ctype);
5774 elsif Are_Anonymous_Access_To_Subprogram_Types then
5775 if Ada_Version < Ada_2005 then
5776 return Ctype = Type_Conformant
5778 Subtypes_Statically_Match (Desig_1, Desig_2);
5780 -- We must check the conformance of the signatures themselves
5784 Conformant : Boolean;
5787 (Desig_1, Desig_2, Ctype, False, Conformant);
5793 return Base_Type (Desig_1) = Base_Type (Desig_2)
5794 and then (Ctype = Type_Conformant
5796 Subtypes_Statically_Match (Desig_1, Desig_2));
5800 -- Otherwise definitely no match
5803 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5804 and then Is_Access_Type (Type_2))
5805 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5806 and then Is_Access_Type (Type_1)))
5809 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5811 May_Hide_Profile := True;
5816 end Conforming_Types;
5818 --------------------------
5819 -- Create_Extra_Formals --
5820 --------------------------
5822 procedure Create_Extra_Formals (E : Entity_Id) is
5824 First_Extra : Entity_Id := Empty;
5825 Last_Extra : Entity_Id;
5826 Formal_Type : Entity_Id;
5827 P_Formal : Entity_Id := Empty;
5829 function Add_Extra_Formal
5830 (Assoc_Entity : Entity_Id;
5833 Suffix : String) return Entity_Id;
5834 -- Add an extra formal to the current list of formals and extra formals.
5835 -- The extra formal is added to the end of the list of extra formals,
5836 -- and also returned as the result. These formals are always of mode IN.
5837 -- The new formal has the type Typ, is declared in Scope, and its name
5838 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5839 -- The following suffixes are currently used. They should not be changed
5840 -- without coordinating with CodePeer, which makes use of these to
5841 -- provide better messages.
5843 -- O denotes the Constrained bit.
5844 -- L denotes the accessibility level.
5845 -- BIP_xxx denotes an extra formal for a build-in-place function. See
5846 -- the full list in exp_ch6.BIP_Formal_Kind.
5848 ----------------------
5849 -- Add_Extra_Formal --
5850 ----------------------
5852 function Add_Extra_Formal
5853 (Assoc_Entity : Entity_Id;
5856 Suffix : String) return Entity_Id
5858 EF : constant Entity_Id :=
5859 Make_Defining_Identifier (Sloc (Assoc_Entity),
5860 Chars => New_External_Name (Chars (Assoc_Entity),
5864 -- A little optimization. Never generate an extra formal for the
5865 -- _init operand of an initialization procedure, since it could
5868 if Chars (Formal) = Name_uInit then
5872 Set_Ekind (EF, E_In_Parameter);
5873 Set_Actual_Subtype (EF, Typ);
5874 Set_Etype (EF, Typ);
5875 Set_Scope (EF, Scope);
5876 Set_Mechanism (EF, Default_Mechanism);
5877 Set_Formal_Validity (EF);
5879 if No (First_Extra) then
5881 Set_Extra_Formals (Scope, First_Extra);
5884 if Present (Last_Extra) then
5885 Set_Extra_Formal (Last_Extra, EF);
5891 end Add_Extra_Formal;
5893 -- Start of processing for Create_Extra_Formals
5896 -- We never generate extra formals if expansion is not active
5897 -- because we don't need them unless we are generating code.
5899 if not Expander_Active then
5903 -- If this is a derived subprogram then the subtypes of the parent
5904 -- subprogram's formal parameters will be used to determine the need
5905 -- for extra formals.
5907 if Is_Overloadable (E) and then Present (Alias (E)) then
5908 P_Formal := First_Formal (Alias (E));
5911 Last_Extra := Empty;
5912 Formal := First_Formal (E);
5913 while Present (Formal) loop
5914 Last_Extra := Formal;
5915 Next_Formal (Formal);
5918 -- If Extra_formals were already created, don't do it again. This
5919 -- situation may arise for subprogram types created as part of
5920 -- dispatching calls (see Expand_Dispatching_Call)
5922 if Present (Last_Extra) and then
5923 Present (Extra_Formal (Last_Extra))
5928 -- If the subprogram is a predefined dispatching subprogram then don't
5929 -- generate any extra constrained or accessibility level formals. In
5930 -- general we suppress these for internal subprograms (by not calling
5931 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5932 -- generated stream attributes do get passed through because extra
5933 -- build-in-place formals are needed in some cases (limited 'Input).
5935 if Is_Predefined_Internal_Operation (E) then
5936 goto Test_For_BIP_Extras;
5939 Formal := First_Formal (E);
5940 while Present (Formal) loop
5942 -- Create extra formal for supporting the attribute 'Constrained.
5943 -- The case of a private type view without discriminants also
5944 -- requires the extra formal if the underlying type has defaulted
5947 if Ekind (Formal) /= E_In_Parameter then
5948 if Present (P_Formal) then
5949 Formal_Type := Etype (P_Formal);
5951 Formal_Type := Etype (Formal);
5954 -- Do not produce extra formals for Unchecked_Union parameters.
5955 -- Jump directly to the end of the loop.
5957 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5958 goto Skip_Extra_Formal_Generation;
5961 if not Has_Discriminants (Formal_Type)
5962 and then Ekind (Formal_Type) in Private_Kind
5963 and then Present (Underlying_Type (Formal_Type))
5965 Formal_Type := Underlying_Type (Formal_Type);
5968 -- Suppress the extra formal if formal's subtype is constrained or
5969 -- indefinite, or we're compiling for Ada 2012 and the underlying
5970 -- type is tagged and limited. In Ada 2012, a limited tagged type
5971 -- can have defaulted discriminants, but 'Constrained is required
5972 -- to return True, so the formal is never needed (see AI05-0214).
5973 -- Note that this ensures consistency of calling sequences for
5974 -- dispatching operations when some types in a class have defaults
5975 -- on discriminants and others do not (and requiring the extra
5976 -- formal would introduce distributed overhead).
5978 if Has_Discriminants (Formal_Type)
5979 and then not Is_Constrained (Formal_Type)
5980 and then not Is_Indefinite_Subtype (Formal_Type)
5981 and then (Ada_Version < Ada_2012
5983 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
5984 and then Is_Limited_Type (Formal_Type)))
5986 Set_Extra_Constrained
5987 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
5991 -- Create extra formal for supporting accessibility checking. This
5992 -- is done for both anonymous access formals and formals of named
5993 -- access types that are marked as controlling formals. The latter
5994 -- case can occur when Expand_Dispatching_Call creates a subprogram
5995 -- type and substitutes the types of access-to-class-wide actuals
5996 -- for the anonymous access-to-specific-type of controlling formals.
5997 -- Base_Type is applied because in cases where there is a null
5998 -- exclusion the formal may have an access subtype.
6000 -- This is suppressed if we specifically suppress accessibility
6001 -- checks at the package level for either the subprogram, or the
6002 -- package in which it resides. However, we do not suppress it
6003 -- simply if the scope has accessibility checks suppressed, since
6004 -- this could cause trouble when clients are compiled with a
6005 -- different suppression setting. The explicit checks at the
6006 -- package level are safe from this point of view.
6008 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
6009 or else (Is_Controlling_Formal (Formal)
6010 and then Is_Access_Type (Base_Type (Etype (Formal)))))
6012 (Explicit_Suppress (E, Accessibility_Check)
6014 Explicit_Suppress (Scope (E), Accessibility_Check))
6017 or else Present (Extra_Accessibility (P_Formal)))
6019 Set_Extra_Accessibility
6020 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
6023 -- This label is required when skipping extra formal generation for
6024 -- Unchecked_Union parameters.
6026 <<Skip_Extra_Formal_Generation>>
6028 if Present (P_Formal) then
6029 Next_Formal (P_Formal);
6032 Next_Formal (Formal);
6035 <<Test_For_BIP_Extras>>
6037 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
6038 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
6040 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
6042 Result_Subt : constant Entity_Id := Etype (E);
6044 Discard : Entity_Id;
6045 pragma Warnings (Off, Discard);
6048 -- In the case of functions with unconstrained result subtypes,
6049 -- add a 4-state formal indicating whether the return object is
6050 -- allocated by the caller (1), or should be allocated by the
6051 -- callee on the secondary stack (2), in the global heap (3), or
6052 -- in a user-defined storage pool (4). For the moment we just use
6053 -- Natural for the type of this formal. Note that this formal
6054 -- isn't usually needed in the case where the result subtype is
6055 -- constrained, but it is needed when the function has a tagged
6056 -- result, because generally such functions can be called in a
6057 -- dispatching context and such calls must be handled like calls
6058 -- to a class-wide function.
6060 if not Is_Constrained (Underlying_Type (Result_Subt))
6061 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
6065 (E, Standard_Natural,
6066 E, BIP_Formal_Suffix (BIP_Alloc_Form));
6069 -- In the case of functions whose result type needs finalization,
6070 -- add an extra formal of type Ada.Finalization.Heap_Management.
6071 -- Finalization_Collection_Ptr.
6073 if Needs_BIP_Collection (E) then
6076 (E, RTE (RE_Finalization_Collection_Ptr),
6077 E, BIP_Formal_Suffix (BIP_Collection));
6080 -- If the result type contains tasks, we have two extra formals:
6081 -- the master of the tasks to be created, and the caller's
6082 -- activation chain.
6084 if Has_Task (Result_Subt) then
6087 (E, RTE (RE_Master_Id),
6088 E, BIP_Formal_Suffix (BIP_Master));
6091 (E, RTE (RE_Activation_Chain_Access),
6092 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6095 -- All build-in-place functions get an extra formal that will be
6096 -- passed the address of the return object within the caller.
6099 Formal_Type : constant Entity_Id :=
6101 (E_Anonymous_Access_Type, E,
6102 Scope_Id => Scope (E));
6104 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
6105 Set_Etype (Formal_Type, Formal_Type);
6106 Set_Depends_On_Private
6107 (Formal_Type, Has_Private_Component (Formal_Type));
6108 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
6109 Set_Is_Access_Constant (Formal_Type, False);
6111 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
6112 -- the designated type comes from the limited view (for
6113 -- back-end purposes).
6115 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
6117 Layout_Type (Formal_Type);
6121 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
6125 end Create_Extra_Formals;
6127 -----------------------------
6128 -- Enter_Overloaded_Entity --
6129 -----------------------------
6131 procedure Enter_Overloaded_Entity (S : Entity_Id) is
6132 E : Entity_Id := Current_Entity_In_Scope (S);
6133 C_E : Entity_Id := Current_Entity (S);
6137 Set_Has_Homonym (E);
6138 Set_Has_Homonym (S);
6141 Set_Is_Immediately_Visible (S);
6142 Set_Scope (S, Current_Scope);
6144 -- Chain new entity if front of homonym in current scope, so that
6145 -- homonyms are contiguous.
6150 while Homonym (C_E) /= E loop
6151 C_E := Homonym (C_E);
6154 Set_Homonym (C_E, S);
6158 Set_Current_Entity (S);
6163 Append_Entity (S, Current_Scope);
6164 Set_Public_Status (S);
6166 if Debug_Flag_E then
6167 Write_Str ("New overloaded entity chain: ");
6168 Write_Name (Chars (S));
6171 while Present (E) loop
6172 Write_Str (" "); Write_Int (Int (E));
6179 -- Generate warning for hiding
6182 and then Comes_From_Source (S)
6183 and then In_Extended_Main_Source_Unit (S)
6190 -- Warn unless genuine overloading. Do not emit warning on
6191 -- hiding predefined operators in Standard (these are either an
6192 -- (artifact of our implicit declarations, or simple noise) but
6193 -- keep warning on a operator defined on a local subtype, because
6194 -- of the real danger that different operators may be applied in
6195 -- various parts of the program.
6197 -- Note that if E and S have the same scope, there is never any
6198 -- hiding. Either the two conflict, and the program is illegal,
6199 -- or S is overriding an implicit inherited subprogram.
6201 if Scope (E) /= Scope (S)
6202 and then (not Is_Overloadable (E)
6203 or else Subtype_Conformant (E, S))
6204 and then (Is_Immediately_Visible (E)
6206 Is_Potentially_Use_Visible (S))
6208 if Scope (E) /= Standard_Standard then
6209 Error_Msg_Sloc := Sloc (E);
6210 Error_Msg_N ("declaration of & hides one#?", S);
6212 elsif Nkind (S) = N_Defining_Operator_Symbol
6214 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
6217 ("declaration of & hides predefined operator?", S);
6222 end Enter_Overloaded_Entity;
6224 -----------------------------
6225 -- Check_Untagged_Equality --
6226 -----------------------------
6228 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
6229 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
6230 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
6234 if Nkind (Decl) = N_Subprogram_Declaration
6235 and then Is_Record_Type (Typ)
6236 and then not Is_Tagged_Type (Typ)
6238 -- If the type is not declared in a package, or if we are in the
6239 -- body of the package or in some other scope, the new operation is
6240 -- not primitive, and therefore legal, though suspicious. If the
6241 -- type is a generic actual (sub)type, the operation is not primitive
6242 -- either because the base type is declared elsewhere.
6244 if Is_Frozen (Typ) then
6245 if Ekind (Scope (Typ)) /= E_Package
6246 or else Scope (Typ) /= Current_Scope
6250 elsif Is_Generic_Actual_Type (Typ) then
6253 elsif In_Package_Body (Scope (Typ)) then
6255 ("equality operator must be declared "
6256 & "before type& is frozen", Eq_Op, Typ);
6258 ("\move declaration to package spec", Eq_Op);
6262 ("equality operator must be declared "
6263 & "before type& is frozen", Eq_Op, Typ);
6265 Obj_Decl := Next (Parent (Typ));
6266 while Present (Obj_Decl)
6267 and then Obj_Decl /= Decl
6269 if Nkind (Obj_Decl) = N_Object_Declaration
6270 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
6272 Error_Msg_NE ("type& is frozen by declaration?",
6275 ("\an equality operator cannot be declared after this "
6276 & "point (RM 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
6284 elsif not In_Same_List (Parent (Typ), Decl)
6285 and then not Is_Limited_Type (Typ)
6288 -- This makes it illegal to have a primitive equality declared in
6289 -- the private part if the type is visible.
6291 Error_Msg_N ("equality operator appears too late", Eq_Op);
6294 end Check_Untagged_Equality;
6296 -----------------------------
6297 -- Find_Corresponding_Spec --
6298 -----------------------------
6300 function Find_Corresponding_Spec
6302 Post_Error : Boolean := True) return Entity_Id
6304 Spec : constant Node_Id := Specification (N);
6305 Designator : constant Entity_Id := Defining_Entity (Spec);
6310 E := Current_Entity (Designator);
6311 while Present (E) loop
6313 -- We are looking for a matching spec. It must have the same scope,
6314 -- and the same name, and either be type conformant, or be the case
6315 -- of a library procedure spec and its body (which belong to one
6316 -- another regardless of whether they are type conformant or not).
6318 if Scope (E) = Current_Scope then
6319 if Current_Scope = Standard_Standard
6320 or else (Ekind (E) = Ekind (Designator)
6321 and then Type_Conformant (E, Designator))
6323 -- Within an instantiation, we know that spec and body are
6324 -- subtype conformant, because they were subtype conformant
6325 -- in the generic. We choose the subtype-conformant entity
6326 -- here as well, to resolve spurious ambiguities in the
6327 -- instance that were not present in the generic (i.e. when
6328 -- two different types are given the same actual). If we are
6329 -- looking for a spec to match a body, full conformance is
6333 Set_Convention (Designator, Convention (E));
6335 -- Skip past subprogram bodies and subprogram renamings that
6336 -- may appear to have a matching spec, but that aren't fully
6337 -- conformant with it. That can occur in cases where an
6338 -- actual type causes unrelated homographs in the instance.
6340 if Nkind_In (N, N_Subprogram_Body,
6341 N_Subprogram_Renaming_Declaration)
6342 and then Present (Homonym (E))
6343 and then not Fully_Conformant (Designator, E)
6347 elsif not Subtype_Conformant (Designator, E) then
6352 if not Has_Completion (E) then
6353 if Nkind (N) /= N_Subprogram_Body_Stub then
6354 Set_Corresponding_Spec (N, E);
6357 Set_Has_Completion (E);
6360 elsif Nkind (Parent (N)) = N_Subunit then
6362 -- If this is the proper body of a subunit, the completion
6363 -- flag is set when analyzing the stub.
6367 -- If E is an internal function with a controlling result
6368 -- that was created for an operation inherited by a null
6369 -- extension, it may be overridden by a body without a previous
6370 -- spec (one more reason why these should be shunned). In that
6371 -- case remove the generated body if present, because the
6372 -- current one is the explicit overriding.
6374 elsif Ekind (E) = E_Function
6375 and then Ada_Version >= Ada_2005
6376 and then not Comes_From_Source (E)
6377 and then Has_Controlling_Result (E)
6378 and then Is_Null_Extension (Etype (E))
6379 and then Comes_From_Source (Spec)
6381 Set_Has_Completion (E, False);
6384 and then Nkind (Parent (E)) = N_Function_Specification
6387 (Unit_Declaration_Node
6388 (Corresponding_Body (Unit_Declaration_Node (E))));
6392 -- If expansion is disabled, or if the wrapper function has
6393 -- not been generated yet, this a late body overriding an
6394 -- inherited operation, or it is an overriding by some other
6395 -- declaration before the controlling result is frozen. In
6396 -- either case this is a declaration of a new entity.
6402 -- If the body already exists, then this is an error unless
6403 -- the previous declaration is the implicit declaration of a
6404 -- derived subprogram, or this is a spurious overloading in an
6407 elsif No (Alias (E))
6408 and then not Is_Intrinsic_Subprogram (E)
6409 and then not In_Instance
6412 Error_Msg_Sloc := Sloc (E);
6414 if Is_Imported (E) then
6416 ("body not allowed for imported subprogram & declared#",
6419 Error_Msg_NE ("duplicate body for & declared#", N, E);
6423 -- Child units cannot be overloaded, so a conformance mismatch
6424 -- between body and a previous spec is an error.
6426 elsif Is_Child_Unit (E)
6428 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
6430 Nkind (Parent (Unit_Declaration_Node (Designator))) =
6435 ("body of child unit does not match previous declaration", N);
6443 -- On exit, we know that no previous declaration of subprogram exists
6446 end Find_Corresponding_Spec;
6448 ----------------------
6449 -- Fully_Conformant --
6450 ----------------------
6452 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6455 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
6457 end Fully_Conformant;
6459 ----------------------------------
6460 -- Fully_Conformant_Expressions --
6461 ----------------------------------
6463 function Fully_Conformant_Expressions
6464 (Given_E1 : Node_Id;
6465 Given_E2 : Node_Id) return Boolean
6467 E1 : constant Node_Id := Original_Node (Given_E1);
6468 E2 : constant Node_Id := Original_Node (Given_E2);
6469 -- We always test conformance on original nodes, since it is possible
6470 -- for analysis and/or expansion to make things look as though they
6471 -- conform when they do not, e.g. by converting 1+2 into 3.
6473 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6474 renames Fully_Conformant_Expressions;
6476 function FCL (L1, L2 : List_Id) return Boolean;
6477 -- Compare elements of two lists for conformance. Elements have to
6478 -- be conformant, and actuals inserted as default parameters do not
6479 -- match explicit actuals with the same value.
6481 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6482 -- Compare an operator node with a function call
6488 function FCL (L1, L2 : List_Id) return Boolean is
6492 if L1 = No_List then
6498 if L2 = No_List then
6504 -- Compare two lists, skipping rewrite insertions (we want to
6505 -- compare the original trees, not the expanded versions!)
6508 if Is_Rewrite_Insertion (N1) then
6510 elsif Is_Rewrite_Insertion (N2) then
6516 elsif not FCE (N1, N2) then
6529 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6530 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6535 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6540 Act := First (Actuals);
6542 if Nkind (Op_Node) in N_Binary_Op then
6543 if not FCE (Left_Opnd (Op_Node), Act) then
6550 return Present (Act)
6551 and then FCE (Right_Opnd (Op_Node), Act)
6552 and then No (Next (Act));
6556 -- Start of processing for Fully_Conformant_Expressions
6559 -- Non-conformant if paren count does not match. Note: if some idiot
6560 -- complains that we don't do this right for more than 3 levels of
6561 -- parentheses, they will be treated with the respect they deserve!
6563 if Paren_Count (E1) /= Paren_Count (E2) then
6566 -- If same entities are referenced, then they are conformant even if
6567 -- they have different forms (RM 8.3.1(19-20)).
6569 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6570 if Present (Entity (E1)) then
6571 return Entity (E1) = Entity (E2)
6572 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6573 and then Ekind (Entity (E1)) = E_Discriminant
6574 and then Ekind (Entity (E2)) = E_In_Parameter);
6576 elsif Nkind (E1) = N_Expanded_Name
6577 and then Nkind (E2) = N_Expanded_Name
6578 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6579 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6581 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6584 -- Identifiers in component associations don't always have
6585 -- entities, but their names must conform.
6587 return Nkind (E1) = N_Identifier
6588 and then Nkind (E2) = N_Identifier
6589 and then Chars (E1) = Chars (E2);
6592 elsif Nkind (E1) = N_Character_Literal
6593 and then Nkind (E2) = N_Expanded_Name
6595 return Nkind (Selector_Name (E2)) = N_Character_Literal
6596 and then Chars (E1) = Chars (Selector_Name (E2));
6598 elsif Nkind (E2) = N_Character_Literal
6599 and then Nkind (E1) = N_Expanded_Name
6601 return Nkind (Selector_Name (E1)) = N_Character_Literal
6602 and then Chars (E2) = Chars (Selector_Name (E1));
6604 elsif Nkind (E1) in N_Op
6605 and then Nkind (E2) = N_Function_Call
6607 return FCO (E1, E2);
6609 elsif Nkind (E2) in N_Op
6610 and then Nkind (E1) = N_Function_Call
6612 return FCO (E2, E1);
6614 -- Otherwise we must have the same syntactic entity
6616 elsif Nkind (E1) /= Nkind (E2) then
6619 -- At this point, we specialize by node type
6626 FCL (Expressions (E1), Expressions (E2))
6628 FCL (Component_Associations (E1),
6629 Component_Associations (E2));
6632 if Nkind (Expression (E1)) = N_Qualified_Expression
6634 Nkind (Expression (E2)) = N_Qualified_Expression
6636 return FCE (Expression (E1), Expression (E2));
6638 -- Check that the subtype marks and any constraints
6643 Indic1 : constant Node_Id := Expression (E1);
6644 Indic2 : constant Node_Id := Expression (E2);
6649 if Nkind (Indic1) /= N_Subtype_Indication then
6651 Nkind (Indic2) /= N_Subtype_Indication
6652 and then Entity (Indic1) = Entity (Indic2);
6654 elsif Nkind (Indic2) /= N_Subtype_Indication then
6656 Nkind (Indic1) /= N_Subtype_Indication
6657 and then Entity (Indic1) = Entity (Indic2);
6660 if Entity (Subtype_Mark (Indic1)) /=
6661 Entity (Subtype_Mark (Indic2))
6666 Elt1 := First (Constraints (Constraint (Indic1)));
6667 Elt2 := First (Constraints (Constraint (Indic2)));
6668 while Present (Elt1) and then Present (Elt2) loop
6669 if not FCE (Elt1, Elt2) then
6682 when N_Attribute_Reference =>
6684 Attribute_Name (E1) = Attribute_Name (E2)
6685 and then FCL (Expressions (E1), Expressions (E2));
6689 Entity (E1) = Entity (E2)
6690 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6691 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6693 when N_Short_Circuit | N_Membership_Test =>
6695 FCE (Left_Opnd (E1), Left_Opnd (E2))
6697 FCE (Right_Opnd (E1), Right_Opnd (E2));
6699 when N_Case_Expression =>
6705 if not FCE (Expression (E1), Expression (E2)) then
6709 Alt1 := First (Alternatives (E1));
6710 Alt2 := First (Alternatives (E2));
6712 if Present (Alt1) /= Present (Alt2) then
6714 elsif No (Alt1) then
6718 if not FCE (Expression (Alt1), Expression (Alt2))
6719 or else not FCL (Discrete_Choices (Alt1),
6720 Discrete_Choices (Alt2))
6731 when N_Character_Literal =>
6733 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6735 when N_Component_Association =>
6737 FCL (Choices (E1), Choices (E2))
6739 FCE (Expression (E1), Expression (E2));
6741 when N_Conditional_Expression =>
6743 FCL (Expressions (E1), Expressions (E2));
6745 when N_Explicit_Dereference =>
6747 FCE (Prefix (E1), Prefix (E2));
6749 when N_Extension_Aggregate =>
6751 FCL (Expressions (E1), Expressions (E2))
6752 and then Null_Record_Present (E1) =
6753 Null_Record_Present (E2)
6754 and then FCL (Component_Associations (E1),
6755 Component_Associations (E2));
6757 when N_Function_Call =>
6759 FCE (Name (E1), Name (E2))
6761 FCL (Parameter_Associations (E1),
6762 Parameter_Associations (E2));
6764 when N_Indexed_Component =>
6766 FCE (Prefix (E1), Prefix (E2))
6768 FCL (Expressions (E1), Expressions (E2));
6770 when N_Integer_Literal =>
6771 return (Intval (E1) = Intval (E2));
6776 when N_Operator_Symbol =>
6778 Chars (E1) = Chars (E2);
6780 when N_Others_Choice =>
6783 when N_Parameter_Association =>
6785 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6786 and then FCE (Explicit_Actual_Parameter (E1),
6787 Explicit_Actual_Parameter (E2));
6789 when N_Qualified_Expression =>
6791 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6793 FCE (Expression (E1), Expression (E2));
6795 when N_Quantified_Expression =>
6796 if not FCE (Condition (E1), Condition (E2)) then
6800 if Present (Loop_Parameter_Specification (E1))
6801 and then Present (Loop_Parameter_Specification (E2))
6804 L1 : constant Node_Id :=
6805 Loop_Parameter_Specification (E1);
6806 L2 : constant Node_Id :=
6807 Loop_Parameter_Specification (E2);
6811 Reverse_Present (L1) = Reverse_Present (L2)
6813 FCE (Defining_Identifier (L1),
6814 Defining_Identifier (L2))
6816 FCE (Discrete_Subtype_Definition (L1),
6817 Discrete_Subtype_Definition (L2));
6820 else -- quantified expression with an iterator
6822 I1 : constant Node_Id := Iterator_Specification (E1);
6823 I2 : constant Node_Id := Iterator_Specification (E2);
6827 FCE (Defining_Identifier (I1),
6828 Defining_Identifier (I2))
6830 Of_Present (I1) = Of_Present (I2)
6832 Reverse_Present (I1) = Reverse_Present (I2)
6833 and then FCE (Name (I1), Name (I2))
6834 and then FCE (Subtype_Indication (I1),
6835 Subtype_Indication (I2));
6841 FCE (Low_Bound (E1), Low_Bound (E2))
6843 FCE (High_Bound (E1), High_Bound (E2));
6845 when N_Real_Literal =>
6846 return (Realval (E1) = Realval (E2));
6848 when N_Selected_Component =>
6850 FCE (Prefix (E1), Prefix (E2))
6852 FCE (Selector_Name (E1), Selector_Name (E2));
6856 FCE (Prefix (E1), Prefix (E2))
6858 FCE (Discrete_Range (E1), Discrete_Range (E2));
6860 when N_String_Literal =>
6862 S1 : constant String_Id := Strval (E1);
6863 S2 : constant String_Id := Strval (E2);
6864 L1 : constant Nat := String_Length (S1);
6865 L2 : constant Nat := String_Length (S2);
6872 for J in 1 .. L1 loop
6873 if Get_String_Char (S1, J) /=
6874 Get_String_Char (S2, J)
6884 when N_Type_Conversion =>
6886 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6888 FCE (Expression (E1), Expression (E2));
6892 Entity (E1) = Entity (E2)
6894 FCE (Right_Opnd (E1), Right_Opnd (E2));
6896 when N_Unchecked_Type_Conversion =>
6898 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6900 FCE (Expression (E1), Expression (E2));
6902 -- All other node types cannot appear in this context. Strictly
6903 -- we should raise a fatal internal error. Instead we just ignore
6904 -- the nodes. This means that if anyone makes a mistake in the
6905 -- expander and mucks an expression tree irretrievably, the
6906 -- result will be a failure to detect a (probably very obscure)
6907 -- case of non-conformance, which is better than bombing on some
6908 -- case where two expressions do in fact conform.
6915 end Fully_Conformant_Expressions;
6917 ----------------------------------------
6918 -- Fully_Conformant_Discrete_Subtypes --
6919 ----------------------------------------
6921 function Fully_Conformant_Discrete_Subtypes
6922 (Given_S1 : Node_Id;
6923 Given_S2 : Node_Id) return Boolean
6925 S1 : constant Node_Id := Original_Node (Given_S1);
6926 S2 : constant Node_Id := Original_Node (Given_S2);
6928 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6929 -- Special-case for a bound given by a discriminant, which in the body
6930 -- is replaced with the discriminal of the enclosing type.
6932 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6933 -- Check both bounds
6935 -----------------------
6936 -- Conforming_Bounds --
6937 -----------------------
6939 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6941 if Is_Entity_Name (B1)
6942 and then Is_Entity_Name (B2)
6943 and then Ekind (Entity (B1)) = E_Discriminant
6945 return Chars (B1) = Chars (B2);
6948 return Fully_Conformant_Expressions (B1, B2);
6950 end Conforming_Bounds;
6952 -----------------------
6953 -- Conforming_Ranges --
6954 -----------------------
6956 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6959 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6961 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6962 end Conforming_Ranges;
6964 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6967 if Nkind (S1) /= Nkind (S2) then
6970 elsif Is_Entity_Name (S1) then
6971 return Entity (S1) = Entity (S2);
6973 elsif Nkind (S1) = N_Range then
6974 return Conforming_Ranges (S1, S2);
6976 elsif Nkind (S1) = N_Subtype_Indication then
6978 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6981 (Range_Expression (Constraint (S1)),
6982 Range_Expression (Constraint (S2)));
6986 end Fully_Conformant_Discrete_Subtypes;
6988 --------------------
6989 -- Install_Entity --
6990 --------------------
6992 procedure Install_Entity (E : Entity_Id) is
6993 Prev : constant Entity_Id := Current_Entity (E);
6995 Set_Is_Immediately_Visible (E);
6996 Set_Current_Entity (E);
6997 Set_Homonym (E, Prev);
7000 ---------------------
7001 -- Install_Formals --
7002 ---------------------
7004 procedure Install_Formals (Id : Entity_Id) is
7007 F := First_Formal (Id);
7008 while Present (F) loop
7012 end Install_Formals;
7014 -----------------------------
7015 -- Is_Interface_Conformant --
7016 -----------------------------
7018 function Is_Interface_Conformant
7019 (Tagged_Type : Entity_Id;
7020 Iface_Prim : Entity_Id;
7021 Prim : Entity_Id) return Boolean
7023 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
7024 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
7027 pragma Assert (Is_Subprogram (Iface_Prim)
7028 and then Is_Subprogram (Prim)
7029 and then Is_Dispatching_Operation (Iface_Prim)
7030 and then Is_Dispatching_Operation (Prim));
7032 pragma Assert (Is_Interface (Iface)
7033 or else (Present (Alias (Iface_Prim))
7036 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
7038 if Prim = Iface_Prim
7039 or else not Is_Subprogram (Prim)
7040 or else Ekind (Prim) /= Ekind (Iface_Prim)
7041 or else not Is_Dispatching_Operation (Prim)
7042 or else Scope (Prim) /= Scope (Tagged_Type)
7044 or else Base_Type (Typ) /= Tagged_Type
7045 or else not Primitive_Names_Match (Iface_Prim, Prim)
7049 -- Case of a procedure, or a function that does not have a controlling
7050 -- result (I or access I).
7052 elsif Ekind (Iface_Prim) = E_Procedure
7053 or else Etype (Prim) = Etype (Iface_Prim)
7054 or else not Has_Controlling_Result (Prim)
7056 return Type_Conformant
7057 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
7059 -- Case of a function returning an interface, or an access to one.
7060 -- Check that the return types correspond.
7062 elsif Implements_Interface (Typ, Iface) then
7063 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
7065 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
7070 Type_Conformant (Prim, Iface_Prim,
7071 Skip_Controlling_Formals => True);
7077 end Is_Interface_Conformant;
7079 ---------------------------------
7080 -- Is_Non_Overriding_Operation --
7081 ---------------------------------
7083 function Is_Non_Overriding_Operation
7084 (Prev_E : Entity_Id;
7085 New_E : Entity_Id) return Boolean
7089 G_Typ : Entity_Id := Empty;
7091 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
7092 -- If F_Type is a derived type associated with a generic actual subtype,
7093 -- then return its Generic_Parent_Type attribute, else return Empty.
7095 function Types_Correspond
7096 (P_Type : Entity_Id;
7097 N_Type : Entity_Id) return Boolean;
7098 -- Returns true if and only if the types (or designated types in the
7099 -- case of anonymous access types) are the same or N_Type is derived
7100 -- directly or indirectly from P_Type.
7102 -----------------------------
7103 -- Get_Generic_Parent_Type --
7104 -----------------------------
7106 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
7111 if Is_Derived_Type (F_Typ)
7112 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
7114 -- The tree must be traversed to determine the parent subtype in
7115 -- the generic unit, which unfortunately isn't always available
7116 -- via semantic attributes. ??? (Note: The use of Original_Node
7117 -- is needed for cases where a full derived type has been
7120 Indic := Subtype_Indication
7121 (Type_Definition (Original_Node (Parent (F_Typ))));
7123 if Nkind (Indic) = N_Subtype_Indication then
7124 G_Typ := Entity (Subtype_Mark (Indic));
7126 G_Typ := Entity (Indic);
7129 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
7130 and then Present (Generic_Parent_Type (Parent (G_Typ)))
7132 return Generic_Parent_Type (Parent (G_Typ));
7137 end Get_Generic_Parent_Type;
7139 ----------------------
7140 -- Types_Correspond --
7141 ----------------------
7143 function Types_Correspond
7144 (P_Type : Entity_Id;
7145 N_Type : Entity_Id) return Boolean
7147 Prev_Type : Entity_Id := Base_Type (P_Type);
7148 New_Type : Entity_Id := Base_Type (N_Type);
7151 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
7152 Prev_Type := Designated_Type (Prev_Type);
7155 if Ekind (New_Type) = E_Anonymous_Access_Type then
7156 New_Type := Designated_Type (New_Type);
7159 if Prev_Type = New_Type then
7162 elsif not Is_Class_Wide_Type (New_Type) then
7163 while Etype (New_Type) /= New_Type loop
7164 New_Type := Etype (New_Type);
7165 if New_Type = Prev_Type then
7171 end Types_Correspond;
7173 -- Start of processing for Is_Non_Overriding_Operation
7176 -- In the case where both operations are implicit derived subprograms
7177 -- then neither overrides the other. This can only occur in certain
7178 -- obscure cases (e.g., derivation from homographs created in a generic
7181 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
7184 elsif Ekind (Current_Scope) = E_Package
7185 and then Is_Generic_Instance (Current_Scope)
7186 and then In_Private_Part (Current_Scope)
7187 and then Comes_From_Source (New_E)
7189 -- We examine the formals and result subtype of the inherited
7190 -- operation, to determine whether their type is derived from (the
7191 -- instance of) a generic type.
7193 Formal := First_Formal (Prev_E);
7194 while Present (Formal) loop
7195 F_Typ := Base_Type (Etype (Formal));
7197 if Ekind (F_Typ) = E_Anonymous_Access_Type then
7198 F_Typ := Designated_Type (F_Typ);
7201 G_Typ := Get_Generic_Parent_Type (F_Typ);
7203 Next_Formal (Formal);
7206 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
7207 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
7214 -- If the generic type is a private type, then the original operation
7215 -- was not overriding in the generic, because there was no primitive
7216 -- operation to override.
7218 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
7219 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
7220 N_Formal_Private_Type_Definition
7224 -- The generic parent type is the ancestor of a formal derived
7225 -- type declaration. We need to check whether it has a primitive
7226 -- operation that should be overridden by New_E in the generic.
7230 P_Formal : Entity_Id;
7231 N_Formal : Entity_Id;
7235 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
7238 while Present (Prim_Elt) loop
7239 P_Prim := Node (Prim_Elt);
7241 if Chars (P_Prim) = Chars (New_E)
7242 and then Ekind (P_Prim) = Ekind (New_E)
7244 P_Formal := First_Formal (P_Prim);
7245 N_Formal := First_Formal (New_E);
7246 while Present (P_Formal) and then Present (N_Formal) loop
7247 P_Typ := Etype (P_Formal);
7248 N_Typ := Etype (N_Formal);
7250 if not Types_Correspond (P_Typ, N_Typ) then
7254 Next_Entity (P_Formal);
7255 Next_Entity (N_Formal);
7258 -- Found a matching primitive operation belonging to the
7259 -- formal ancestor type, so the new subprogram is
7263 and then No (N_Formal)
7264 and then (Ekind (New_E) /= E_Function
7267 (Etype (P_Prim), Etype (New_E)))
7273 Next_Elmt (Prim_Elt);
7276 -- If no match found, then the new subprogram does not
7277 -- override in the generic (nor in the instance).
7285 end Is_Non_Overriding_Operation;
7287 -------------------------------------
7288 -- List_Inherited_Pre_Post_Aspects --
7289 -------------------------------------
7291 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
7293 if Opt.List_Inherited_Aspects
7294 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
7297 Inherited : constant Subprogram_List :=
7298 Inherited_Subprograms (E);
7302 for J in Inherited'Range loop
7303 P := Spec_PPC_List (Inherited (J));
7304 while Present (P) loop
7305 Error_Msg_Sloc := Sloc (P);
7307 if Class_Present (P) and then not Split_PPC (P) then
7308 if Pragma_Name (P) = Name_Precondition then
7310 ("?info: & inherits `Pre''Class` aspect from #", E);
7313 ("?info: & inherits `Post''Class` aspect from #", E);
7317 P := Next_Pragma (P);
7322 end List_Inherited_Pre_Post_Aspects;
7324 ------------------------------
7325 -- Make_Inequality_Operator --
7326 ------------------------------
7328 -- S is the defining identifier of an equality operator. We build a
7329 -- subprogram declaration with the right signature. This operation is
7330 -- intrinsic, because it is always expanded as the negation of the
7331 -- call to the equality function.
7333 procedure Make_Inequality_Operator (S : Entity_Id) is
7334 Loc : constant Source_Ptr := Sloc (S);
7337 Op_Name : Entity_Id;
7339 FF : constant Entity_Id := First_Formal (S);
7340 NF : constant Entity_Id := Next_Formal (FF);
7343 -- Check that equality was properly defined, ignore call if not
7350 A : constant Entity_Id :=
7351 Make_Defining_Identifier (Sloc (FF),
7352 Chars => Chars (FF));
7354 B : constant Entity_Id :=
7355 Make_Defining_Identifier (Sloc (NF),
7356 Chars => Chars (NF));
7359 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
7361 Formals := New_List (
7362 Make_Parameter_Specification (Loc,
7363 Defining_Identifier => A,
7365 New_Reference_To (Etype (First_Formal (S)),
7366 Sloc (Etype (First_Formal (S))))),
7368 Make_Parameter_Specification (Loc,
7369 Defining_Identifier => B,
7371 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
7372 Sloc (Etype (Next_Formal (First_Formal (S)))))));
7375 Make_Subprogram_Declaration (Loc,
7377 Make_Function_Specification (Loc,
7378 Defining_Unit_Name => Op_Name,
7379 Parameter_Specifications => Formals,
7380 Result_Definition =>
7381 New_Reference_To (Standard_Boolean, Loc)));
7383 -- Insert inequality right after equality if it is explicit or after
7384 -- the derived type when implicit. These entities are created only
7385 -- for visibility purposes, and eventually replaced in the course of
7386 -- expansion, so they do not need to be attached to the tree and seen
7387 -- by the back-end. Keeping them internal also avoids spurious
7388 -- freezing problems. The declaration is inserted in the tree for
7389 -- analysis, and removed afterwards. If the equality operator comes
7390 -- from an explicit declaration, attach the inequality immediately
7391 -- after. Else the equality is inherited from a derived type
7392 -- declaration, so insert inequality after that declaration.
7394 if No (Alias (S)) then
7395 Insert_After (Unit_Declaration_Node (S), Decl);
7396 elsif Is_List_Member (Parent (S)) then
7397 Insert_After (Parent (S), Decl);
7399 Insert_After (Parent (Etype (First_Formal (S))), Decl);
7402 Mark_Rewrite_Insertion (Decl);
7403 Set_Is_Intrinsic_Subprogram (Op_Name);
7406 Set_Has_Completion (Op_Name);
7407 Set_Corresponding_Equality (Op_Name, S);
7408 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
7410 end Make_Inequality_Operator;
7412 ----------------------
7413 -- May_Need_Actuals --
7414 ----------------------
7416 procedure May_Need_Actuals (Fun : Entity_Id) is
7421 F := First_Formal (Fun);
7423 while Present (F) loop
7424 if No (Default_Value (F)) then
7432 Set_Needs_No_Actuals (Fun, B);
7433 end May_Need_Actuals;
7435 ---------------------
7436 -- Mode_Conformant --
7437 ---------------------
7439 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7442 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
7444 end Mode_Conformant;
7446 ---------------------------
7447 -- New_Overloaded_Entity --
7448 ---------------------------
7450 procedure New_Overloaded_Entity
7452 Derived_Type : Entity_Id := Empty)
7454 Overridden_Subp : Entity_Id := Empty;
7455 -- Set if the current scope has an operation that is type-conformant
7456 -- with S, and becomes hidden by S.
7458 Is_Primitive_Subp : Boolean;
7459 -- Set to True if the new subprogram is primitive
7462 -- Entity that S overrides
7464 Prev_Vis : Entity_Id := Empty;
7465 -- Predecessor of E in Homonym chain
7467 procedure Check_For_Primitive_Subprogram
7468 (Is_Primitive : out Boolean;
7469 Is_Overriding : Boolean := False);
7470 -- If the subprogram being analyzed is a primitive operation of the type
7471 -- of a formal or result, set the Has_Primitive_Operations flag on the
7472 -- type, and set Is_Primitive to True (otherwise set to False). Set the
7473 -- corresponding flag on the entity itself for later use.
7475 procedure Check_Synchronized_Overriding
7476 (Def_Id : Entity_Id;
7477 Overridden_Subp : out Entity_Id);
7478 -- First determine if Def_Id is an entry or a subprogram either defined
7479 -- in the scope of a task or protected type, or is a primitive of such
7480 -- a type. Check whether Def_Id overrides a subprogram of an interface
7481 -- implemented by the synchronized type, return the overridden entity
7484 function Is_Private_Declaration (E : Entity_Id) return Boolean;
7485 -- Check that E is declared in the private part of the current package,
7486 -- or in the package body, where it may hide a previous declaration.
7487 -- We can't use In_Private_Part by itself because this flag is also
7488 -- set when freezing entities, so we must examine the place of the
7489 -- declaration in the tree, and recognize wrapper packages as well.
7491 function Is_Overriding_Alias
7493 New_E : Entity_Id) return Boolean;
7494 -- Check whether new subprogram and old subprogram are both inherited
7495 -- from subprograms that have distinct dispatch table entries. This can
7496 -- occur with derivations from instances with accidental homonyms.
7497 -- The function is conservative given that the converse is only true
7498 -- within instances that contain accidental overloadings.
7500 ------------------------------------
7501 -- Check_For_Primitive_Subprogram --
7502 ------------------------------------
7504 procedure Check_For_Primitive_Subprogram
7505 (Is_Primitive : out Boolean;
7506 Is_Overriding : Boolean := False)
7512 function Visible_Part_Type (T : Entity_Id) return Boolean;
7513 -- Returns true if T is declared in the visible part of the current
7514 -- package scope; otherwise returns false. Assumes that T is declared
7517 procedure Check_Private_Overriding (T : Entity_Id);
7518 -- Checks that if a primitive abstract subprogram of a visible
7519 -- abstract type is declared in a private part, then it must override
7520 -- an abstract subprogram declared in the visible part. Also checks
7521 -- that if a primitive function with a controlling result is declared
7522 -- in a private part, then it must override a function declared in
7523 -- the visible part.
7525 ------------------------------
7526 -- Check_Private_Overriding --
7527 ------------------------------
7529 procedure Check_Private_Overriding (T : Entity_Id) is
7531 if Is_Package_Or_Generic_Package (Current_Scope)
7532 and then In_Private_Part (Current_Scope)
7533 and then Visible_Part_Type (T)
7534 and then not In_Instance
7536 if Is_Abstract_Type (T)
7537 and then Is_Abstract_Subprogram (S)
7538 and then (not Is_Overriding
7539 or else not Is_Abstract_Subprogram (E))
7542 ("abstract subprograms must be visible "
7543 & "(RM 3.9.3(10))!", S);
7545 elsif Ekind (S) = E_Function
7546 and then not Is_Overriding
7548 if Is_Tagged_Type (T)
7549 and then T = Base_Type (Etype (S))
7552 ("private function with tagged result must"
7553 & " override visible-part function", S);
7555 ("\move subprogram to the visible part"
7556 & " (RM 3.9.3(10))", S);
7558 -- AI05-0073: extend this test to the case of a function
7559 -- with a controlling access result.
7561 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
7562 and then Is_Tagged_Type (Designated_Type (Etype (S)))
7564 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
7565 and then Ada_Version >= Ada_2012
7568 ("private function with controlling access result "
7569 & "must override visible-part function", S);
7571 ("\move subprogram to the visible part"
7572 & " (RM 3.9.3(10))", S);
7576 end Check_Private_Overriding;
7578 -----------------------
7579 -- Visible_Part_Type --
7580 -----------------------
7582 function Visible_Part_Type (T : Entity_Id) return Boolean is
7583 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7587 -- If the entity is a private type, then it must be declared in a
7590 if Ekind (T) in Private_Kind then
7594 -- Otherwise, we traverse the visible part looking for its
7595 -- corresponding declaration. We cannot use the declaration
7596 -- node directly because in the private part the entity of a
7597 -- private type is the one in the full view, which does not
7598 -- indicate that it is the completion of something visible.
7600 N := First (Visible_Declarations (Specification (P)));
7601 while Present (N) loop
7602 if Nkind (N) = N_Full_Type_Declaration
7603 and then Present (Defining_Identifier (N))
7604 and then T = Defining_Identifier (N)
7608 elsif Nkind_In (N, N_Private_Type_Declaration,
7609 N_Private_Extension_Declaration)
7610 and then Present (Defining_Identifier (N))
7611 and then T = Full_View (Defining_Identifier (N))
7620 end Visible_Part_Type;
7622 -- Start of processing for Check_For_Primitive_Subprogram
7625 Is_Primitive := False;
7627 if not Comes_From_Source (S) then
7630 -- If subprogram is at library level, it is not primitive operation
7632 elsif Current_Scope = Standard_Standard then
7635 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7636 and then not In_Package_Body (Current_Scope))
7637 or else Is_Overriding
7639 -- For function, check return type
7641 if Ekind (S) = E_Function then
7642 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7643 F_Typ := Designated_Type (Etype (S));
7648 B_Typ := Base_Type (F_Typ);
7650 if Scope (B_Typ) = Current_Scope
7651 and then not Is_Class_Wide_Type (B_Typ)
7652 and then not Is_Generic_Type (B_Typ)
7654 Is_Primitive := True;
7655 Set_Has_Primitive_Operations (B_Typ);
7656 Set_Is_Primitive (S);
7657 Check_Private_Overriding (B_Typ);
7661 -- For all subprograms, check formals
7663 Formal := First_Formal (S);
7664 while Present (Formal) loop
7665 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7666 F_Typ := Designated_Type (Etype (Formal));
7668 F_Typ := Etype (Formal);
7671 B_Typ := Base_Type (F_Typ);
7673 if Ekind (B_Typ) = E_Access_Subtype then
7674 B_Typ := Base_Type (B_Typ);
7677 if Scope (B_Typ) = Current_Scope
7678 and then not Is_Class_Wide_Type (B_Typ)
7679 and then not Is_Generic_Type (B_Typ)
7681 Is_Primitive := True;
7682 Set_Is_Primitive (S);
7683 Set_Has_Primitive_Operations (B_Typ);
7684 Check_Private_Overriding (B_Typ);
7687 Next_Formal (Formal);
7690 end Check_For_Primitive_Subprogram;
7692 -----------------------------------
7693 -- Check_Synchronized_Overriding --
7694 -----------------------------------
7696 procedure Check_Synchronized_Overriding
7697 (Def_Id : Entity_Id;
7698 Overridden_Subp : out Entity_Id)
7700 Ifaces_List : Elist_Id;
7704 function Matches_Prefixed_View_Profile
7705 (Prim_Params : List_Id;
7706 Iface_Params : List_Id) return Boolean;
7707 -- Determine whether a subprogram's parameter profile Prim_Params
7708 -- matches that of a potentially overridden interface subprogram
7709 -- Iface_Params. Also determine if the type of first parameter of
7710 -- Iface_Params is an implemented interface.
7712 -----------------------------------
7713 -- Matches_Prefixed_View_Profile --
7714 -----------------------------------
7716 function Matches_Prefixed_View_Profile
7717 (Prim_Params : List_Id;
7718 Iface_Params : List_Id) return Boolean
7720 Iface_Id : Entity_Id;
7721 Iface_Param : Node_Id;
7722 Iface_Typ : Entity_Id;
7723 Prim_Id : Entity_Id;
7724 Prim_Param : Node_Id;
7725 Prim_Typ : Entity_Id;
7727 function Is_Implemented
7728 (Ifaces_List : Elist_Id;
7729 Iface : Entity_Id) return Boolean;
7730 -- Determine if Iface is implemented by the current task or
7733 --------------------
7734 -- Is_Implemented --
7735 --------------------
7737 function Is_Implemented
7738 (Ifaces_List : Elist_Id;
7739 Iface : Entity_Id) return Boolean
7741 Iface_Elmt : Elmt_Id;
7744 Iface_Elmt := First_Elmt (Ifaces_List);
7745 while Present (Iface_Elmt) loop
7746 if Node (Iface_Elmt) = Iface then
7750 Next_Elmt (Iface_Elmt);
7756 -- Start of processing for Matches_Prefixed_View_Profile
7759 Iface_Param := First (Iface_Params);
7760 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7762 if Is_Access_Type (Iface_Typ) then
7763 Iface_Typ := Designated_Type (Iface_Typ);
7766 Prim_Param := First (Prim_Params);
7768 -- The first parameter of the potentially overridden subprogram
7769 -- must be an interface implemented by Prim.
7771 if not Is_Interface (Iface_Typ)
7772 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7777 -- The checks on the object parameters are done, move onto the
7778 -- rest of the parameters.
7780 if not In_Scope then
7781 Prim_Param := Next (Prim_Param);
7784 Iface_Param := Next (Iface_Param);
7785 while Present (Iface_Param) and then Present (Prim_Param) loop
7786 Iface_Id := Defining_Identifier (Iface_Param);
7787 Iface_Typ := Find_Parameter_Type (Iface_Param);
7789 Prim_Id := Defining_Identifier (Prim_Param);
7790 Prim_Typ := Find_Parameter_Type (Prim_Param);
7792 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7793 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7794 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7796 Iface_Typ := Designated_Type (Iface_Typ);
7797 Prim_Typ := Designated_Type (Prim_Typ);
7800 -- Case of multiple interface types inside a parameter profile
7802 -- (Obj_Param : in out Iface; ...; Param : Iface)
7804 -- If the interface type is implemented, then the matching type
7805 -- in the primitive should be the implementing record type.
7807 if Ekind (Iface_Typ) = E_Record_Type
7808 and then Is_Interface (Iface_Typ)
7809 and then Is_Implemented (Ifaces_List, Iface_Typ)
7811 if Prim_Typ /= Typ then
7815 -- The two parameters must be both mode and subtype conformant
7817 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7819 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7828 -- One of the two lists contains more parameters than the other
7830 if Present (Iface_Param) or else Present (Prim_Param) then
7835 end Matches_Prefixed_View_Profile;
7837 -- Start of processing for Check_Synchronized_Overriding
7840 Overridden_Subp := Empty;
7842 -- Def_Id must be an entry or a subprogram. We should skip predefined
7843 -- primitives internally generated by the frontend; however at this
7844 -- stage predefined primitives are still not fully decorated. As a
7845 -- minor optimization we skip here internally generated subprograms.
7847 if (Ekind (Def_Id) /= E_Entry
7848 and then Ekind (Def_Id) /= E_Function
7849 and then Ekind (Def_Id) /= E_Procedure)
7850 or else not Comes_From_Source (Def_Id)
7855 -- Search for the concurrent declaration since it contains the list
7856 -- of all implemented interfaces. In this case, the subprogram is
7857 -- declared within the scope of a protected or a task type.
7859 if Present (Scope (Def_Id))
7860 and then Is_Concurrent_Type (Scope (Def_Id))
7861 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7863 Typ := Scope (Def_Id);
7866 -- The enclosing scope is not a synchronized type and the subprogram
7869 elsif No (First_Formal (Def_Id)) then
7872 -- The subprogram has formals and hence it may be a primitive of a
7876 Typ := Etype (First_Formal (Def_Id));
7878 if Is_Access_Type (Typ) then
7879 Typ := Directly_Designated_Type (Typ);
7882 if Is_Concurrent_Type (Typ)
7883 and then not Is_Generic_Actual_Type (Typ)
7887 -- This case occurs when the concurrent type is declared within
7888 -- a generic unit. As a result the corresponding record has been
7889 -- built and used as the type of the first formal, we just have
7890 -- to retrieve the corresponding concurrent type.
7892 elsif Is_Concurrent_Record_Type (Typ)
7893 and then Present (Corresponding_Concurrent_Type (Typ))
7895 Typ := Corresponding_Concurrent_Type (Typ);
7903 -- There is no overriding to check if is an inherited operation in a
7904 -- type derivation on for a generic actual.
7906 Collect_Interfaces (Typ, Ifaces_List);
7908 if Is_Empty_Elmt_List (Ifaces_List) then
7912 -- Determine whether entry or subprogram Def_Id overrides a primitive
7913 -- operation that belongs to one of the interfaces in Ifaces_List.
7916 Candidate : Entity_Id := Empty;
7917 Hom : Entity_Id := Empty;
7918 Iface_Typ : Entity_Id;
7919 Subp : Entity_Id := Empty;
7922 -- Traverse the homonym chain, looking for a potentially
7923 -- overridden subprogram that belongs to an implemented
7926 Hom := Current_Entity_In_Scope (Def_Id);
7927 while Present (Hom) loop
7931 or else not Is_Overloadable (Subp)
7932 or else not Is_Primitive (Subp)
7933 or else not Is_Dispatching_Operation (Subp)
7934 or else not Present (Find_Dispatching_Type (Subp))
7935 or else not Is_Interface (Find_Dispatching_Type (Subp))
7939 -- Entries and procedures can override abstract or null
7940 -- interface procedures.
7942 elsif (Ekind (Def_Id) = E_Procedure
7943 or else Ekind (Def_Id) = E_Entry)
7944 and then Ekind (Subp) = E_Procedure
7945 and then Matches_Prefixed_View_Profile
7946 (Parameter_Specifications (Parent (Def_Id)),
7947 Parameter_Specifications (Parent (Subp)))
7951 -- For an overridden subprogram Subp, check whether the mode
7952 -- of its first parameter is correct depending on the kind
7953 -- of synchronized type.
7956 Formal : constant Node_Id := First_Formal (Candidate);
7959 -- In order for an entry or a protected procedure to
7960 -- override, the first parameter of the overridden
7961 -- routine must be of mode "out", "in out" or
7962 -- access-to-variable.
7964 if (Ekind (Candidate) = E_Entry
7965 or else Ekind (Candidate) = E_Procedure)
7966 and then Is_Protected_Type (Typ)
7967 and then Ekind (Formal) /= E_In_Out_Parameter
7968 and then Ekind (Formal) /= E_Out_Parameter
7969 and then Nkind (Parameter_Type (Parent (Formal)))
7970 /= N_Access_Definition
7974 -- All other cases are OK since a task entry or routine
7975 -- does not have a restriction on the mode of the first
7976 -- parameter of the overridden interface routine.
7979 Overridden_Subp := Candidate;
7984 -- Functions can override abstract interface functions
7986 elsif Ekind (Def_Id) = E_Function
7987 and then Ekind (Subp) = E_Function
7988 and then Matches_Prefixed_View_Profile
7989 (Parameter_Specifications (Parent (Def_Id)),
7990 Parameter_Specifications (Parent (Subp)))
7991 and then Etype (Result_Definition (Parent (Def_Id))) =
7992 Etype (Result_Definition (Parent (Subp)))
7994 Overridden_Subp := Subp;
7998 Hom := Homonym (Hom);
8001 -- After examining all candidates for overriding, we are left with
8002 -- the best match which is a mode incompatible interface routine.
8003 -- Do not emit an error if the Expander is active since this error
8004 -- will be detected later on after all concurrent types are
8005 -- expanded and all wrappers are built. This check is meant for
8006 -- spec-only compilations.
8008 if Present (Candidate) and then not Expander_Active then
8010 Find_Parameter_Type (Parent (First_Formal (Candidate)));
8012 -- Def_Id is primitive of a protected type, declared inside the
8013 -- type, and the candidate is primitive of a limited or
8014 -- synchronized interface.
8017 and then Is_Protected_Type (Typ)
8019 (Is_Limited_Interface (Iface_Typ)
8020 or else Is_Protected_Interface (Iface_Typ)
8021 or else Is_Synchronized_Interface (Iface_Typ)
8022 or else Is_Task_Interface (Iface_Typ))
8025 ("first formal of & must be of mode `OUT`, `IN OUT`"
8026 & " or access-to-variable", Typ, Candidate);
8028 ("\in order to be overridden by protected procedure or "
8029 & "entry (RM 9.4(11.9/2))", Typ);
8033 Overridden_Subp := Candidate;
8036 end Check_Synchronized_Overriding;
8038 ----------------------------
8039 -- Is_Private_Declaration --
8040 ----------------------------
8042 function Is_Private_Declaration (E : Entity_Id) return Boolean is
8043 Priv_Decls : List_Id;
8044 Decl : constant Node_Id := Unit_Declaration_Node (E);
8047 if Is_Package_Or_Generic_Package (Current_Scope)
8048 and then In_Private_Part (Current_Scope)
8051 Private_Declarations (
8052 Specification (Unit_Declaration_Node (Current_Scope)));
8054 return In_Package_Body (Current_Scope)
8056 (Is_List_Member (Decl)
8057 and then List_Containing (Decl) = Priv_Decls)
8058 or else (Nkind (Parent (Decl)) = N_Package_Specification
8061 (Defining_Entity (Parent (Decl)))
8062 and then List_Containing (Parent (Parent (Decl)))
8067 end Is_Private_Declaration;
8069 --------------------------
8070 -- Is_Overriding_Alias --
8071 --------------------------
8073 function Is_Overriding_Alias
8075 New_E : Entity_Id) return Boolean
8077 AO : constant Entity_Id := Alias (Old_E);
8078 AN : constant Entity_Id := Alias (New_E);
8081 return Scope (AO) /= Scope (AN)
8082 or else No (DTC_Entity (AO))
8083 or else No (DTC_Entity (AN))
8084 or else DT_Position (AO) = DT_Position (AN);
8085 end Is_Overriding_Alias;
8087 -- Start of processing for New_Overloaded_Entity
8090 -- We need to look for an entity that S may override. This must be a
8091 -- homonym in the current scope, so we look for the first homonym of
8092 -- S in the current scope as the starting point for the search.
8094 E := Current_Entity_In_Scope (S);
8096 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
8097 -- They are directly added to the list of primitive operations of
8098 -- Derived_Type, unless this is a rederivation in the private part
8099 -- of an operation that was already derived in the visible part of
8100 -- the current package.
8102 if Ada_Version >= Ada_2005
8103 and then Present (Derived_Type)
8104 and then Present (Alias (S))
8105 and then Is_Dispatching_Operation (Alias (S))
8106 and then Present (Find_Dispatching_Type (Alias (S)))
8107 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
8109 -- For private types, when the full-view is processed we propagate to
8110 -- the full view the non-overridden entities whose attribute "alias"
8111 -- references an interface primitive. These entities were added by
8112 -- Derive_Subprograms to ensure that interface primitives are
8115 -- Inside_Freeze_Actions is non zero when S corresponds with an
8116 -- internal entity that links an interface primitive with its
8117 -- covering primitive through attribute Interface_Alias (see
8118 -- Add_Internal_Interface_Entities).
8120 if Inside_Freezing_Actions = 0
8121 and then Is_Package_Or_Generic_Package (Current_Scope)
8122 and then In_Private_Part (Current_Scope)
8123 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
8124 and then Nkind (Parent (S)) = N_Full_Type_Declaration
8125 and then Full_View (Defining_Identifier (Parent (E)))
8126 = Defining_Identifier (Parent (S))
8127 and then Alias (E) = Alias (S)
8129 Check_Operation_From_Private_View (S, E);
8130 Set_Is_Dispatching_Operation (S);
8135 Enter_Overloaded_Entity (S);
8136 Check_Dispatching_Operation (S, Empty);
8137 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8143 -- If there is no homonym then this is definitely not overriding
8146 Enter_Overloaded_Entity (S);
8147 Check_Dispatching_Operation (S, Empty);
8148 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8150 -- If subprogram has an explicit declaration, check whether it
8151 -- has an overriding indicator.
8153 if Comes_From_Source (S) then
8154 Check_Synchronized_Overriding (S, Overridden_Subp);
8156 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
8157 -- it may have overridden some hidden inherited primitive. Update
8158 -- Overridden_Subp to avoid spurious errors when checking the
8159 -- overriding indicator.
8161 if Ada_Version >= Ada_2012
8162 and then No (Overridden_Subp)
8163 and then Is_Dispatching_Operation (S)
8164 and then Present (Overridden_Operation (S))
8166 Overridden_Subp := Overridden_Operation (S);
8169 Check_Overriding_Indicator
8170 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8173 -- If there is a homonym that is not overloadable, then we have an
8174 -- error, except for the special cases checked explicitly below.
8176 elsif not Is_Overloadable (E) then
8178 -- Check for spurious conflict produced by a subprogram that has the
8179 -- same name as that of the enclosing generic package. The conflict
8180 -- occurs within an instance, between the subprogram and the renaming
8181 -- declaration for the package. After the subprogram, the package
8182 -- renaming declaration becomes hidden.
8184 if Ekind (E) = E_Package
8185 and then Present (Renamed_Object (E))
8186 and then Renamed_Object (E) = Current_Scope
8187 and then Nkind (Parent (Renamed_Object (E))) =
8188 N_Package_Specification
8189 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
8192 Set_Is_Immediately_Visible (E, False);
8193 Enter_Overloaded_Entity (S);
8194 Set_Homonym (S, Homonym (E));
8195 Check_Dispatching_Operation (S, Empty);
8196 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
8198 -- If the subprogram is implicit it is hidden by the previous
8199 -- declaration. However if it is dispatching, it must appear in the
8200 -- dispatch table anyway, because it can be dispatched to even if it
8201 -- cannot be called directly.
8203 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
8204 Set_Scope (S, Current_Scope);
8206 if Is_Dispatching_Operation (Alias (S)) then
8207 Check_Dispatching_Operation (S, Empty);
8213 Error_Msg_Sloc := Sloc (E);
8215 -- Generate message, with useful additional warning if in generic
8217 if Is_Generic_Unit (E) then
8218 Error_Msg_N ("previous generic unit cannot be overloaded", S);
8219 Error_Msg_N ("\& conflicts with declaration#", S);
8221 Error_Msg_N ("& conflicts with declaration#", S);
8227 -- E exists and is overloadable
8230 Check_Synchronized_Overriding (S, Overridden_Subp);
8232 -- Loop through E and its homonyms to determine if any of them is
8233 -- the candidate for overriding by S.
8235 while Present (E) loop
8237 -- Definitely not interesting if not in the current scope
8239 if Scope (E) /= Current_Scope then
8242 -- Check if we have type conformance
8244 elsif Type_Conformant (E, S) then
8246 -- If the old and new entities have the same profile and one
8247 -- is not the body of the other, then this is an error, unless
8248 -- one of them is implicitly declared.
8250 -- There are some cases when both can be implicit, for example
8251 -- when both a literal and a function that overrides it are
8252 -- inherited in a derivation, or when an inherited operation
8253 -- of a tagged full type overrides the inherited operation of
8254 -- a private extension. Ada 83 had a special rule for the
8255 -- literal case. In Ada95, the later implicit operation hides
8256 -- the former, and the literal is always the former. In the
8257 -- odd case where both are derived operations declared at the
8258 -- same point, both operations should be declared, and in that
8259 -- case we bypass the following test and proceed to the next
8260 -- part. This can only occur for certain obscure cases in
8261 -- instances, when an operation on a type derived from a formal
8262 -- private type does not override a homograph inherited from
8263 -- the actual. In subsequent derivations of such a type, the
8264 -- DT positions of these operations remain distinct, if they
8267 if Present (Alias (S))
8268 and then (No (Alias (E))
8269 or else Comes_From_Source (E)
8270 or else Is_Abstract_Subprogram (S)
8272 (Is_Dispatching_Operation (E)
8273 and then Is_Overriding_Alias (E, S)))
8274 and then Ekind (E) /= E_Enumeration_Literal
8276 -- When an derived operation is overloaded it may be due to
8277 -- the fact that the full view of a private extension
8278 -- re-inherits. It has to be dealt with.
8280 if Is_Package_Or_Generic_Package (Current_Scope)
8281 and then In_Private_Part (Current_Scope)
8283 Check_Operation_From_Private_View (S, E);
8286 -- In any case the implicit operation remains hidden by the
8287 -- existing declaration, which is overriding. Indicate that
8288 -- E overrides the operation from which S is inherited.
8290 if Present (Alias (S)) then
8291 Set_Overridden_Operation (E, Alias (S));
8293 Set_Overridden_Operation (E, S);
8296 if Comes_From_Source (E) then
8297 Check_Overriding_Indicator (E, S, Is_Primitive => False);
8302 -- Within an instance, the renaming declarations for actual
8303 -- subprograms may become ambiguous, but they do not hide each
8306 elsif Ekind (E) /= E_Entry
8307 and then not Comes_From_Source (E)
8308 and then not Is_Generic_Instance (E)
8309 and then (Present (Alias (E))
8310 or else Is_Intrinsic_Subprogram (E))
8311 and then (not In_Instance
8312 or else No (Parent (E))
8313 or else Nkind (Unit_Declaration_Node (E)) /=
8314 N_Subprogram_Renaming_Declaration)
8316 -- A subprogram child unit is not allowed to override an
8317 -- inherited subprogram (10.1.1(20)).
8319 if Is_Child_Unit (S) then
8321 ("child unit overrides inherited subprogram in parent",
8326 if Is_Non_Overriding_Operation (E, S) then
8327 Enter_Overloaded_Entity (S);
8329 if No (Derived_Type)
8330 or else Is_Tagged_Type (Derived_Type)
8332 Check_Dispatching_Operation (S, Empty);
8338 -- E is a derived operation or an internal operator which
8339 -- is being overridden. Remove E from further visibility.
8340 -- Furthermore, if E is a dispatching operation, it must be
8341 -- replaced in the list of primitive operations of its type
8342 -- (see Override_Dispatching_Operation).
8344 Overridden_Subp := E;
8350 Prev := First_Entity (Current_Scope);
8351 while Present (Prev)
8352 and then Next_Entity (Prev) /= E
8357 -- It is possible for E to be in the current scope and
8358 -- yet not in the entity chain. This can only occur in a
8359 -- generic context where E is an implicit concatenation
8360 -- in the formal part, because in a generic body the
8361 -- entity chain starts with the formals.
8364 (Present (Prev) or else Chars (E) = Name_Op_Concat);
8366 -- E must be removed both from the entity_list of the
8367 -- current scope, and from the visibility chain
8369 if Debug_Flag_E then
8370 Write_Str ("Override implicit operation ");
8371 Write_Int (Int (E));
8375 -- If E is a predefined concatenation, it stands for four
8376 -- different operations. As a result, a single explicit
8377 -- declaration does not hide it. In a possible ambiguous
8378 -- situation, Disambiguate chooses the user-defined op,
8379 -- so it is correct to retain the previous internal one.
8381 if Chars (E) /= Name_Op_Concat
8382 or else Ekind (E) /= E_Operator
8384 -- For nondispatching derived operations that are
8385 -- overridden by a subprogram declared in the private
8386 -- part of a package, we retain the derived subprogram
8387 -- but mark it as not immediately visible. If the
8388 -- derived operation was declared in the visible part
8389 -- then this ensures that it will still be visible
8390 -- outside the package with the proper signature
8391 -- (calls from outside must also be directed to this
8392 -- version rather than the overriding one, unlike the
8393 -- dispatching case). Calls from inside the package
8394 -- will still resolve to the overriding subprogram
8395 -- since the derived one is marked as not visible
8396 -- within the package.
8398 -- If the private operation is dispatching, we achieve
8399 -- the overriding by keeping the implicit operation
8400 -- but setting its alias to be the overriding one. In
8401 -- this fashion the proper body is executed in all
8402 -- cases, but the original signature is used outside
8405 -- If the overriding is not in the private part, we
8406 -- remove the implicit operation altogether.
8408 if Is_Private_Declaration (S) then
8409 if not Is_Dispatching_Operation (E) then
8410 Set_Is_Immediately_Visible (E, False);
8412 -- Work done in Override_Dispatching_Operation,
8413 -- so nothing else need to be done here.
8419 -- Find predecessor of E in Homonym chain
8421 if E = Current_Entity (E) then
8424 Prev_Vis := Current_Entity (E);
8425 while Homonym (Prev_Vis) /= E loop
8426 Prev_Vis := Homonym (Prev_Vis);
8430 if Prev_Vis /= Empty then
8432 -- Skip E in the visibility chain
8434 Set_Homonym (Prev_Vis, Homonym (E));
8437 Set_Name_Entity_Id (Chars (E), Homonym (E));
8440 Set_Next_Entity (Prev, Next_Entity (E));
8442 if No (Next_Entity (Prev)) then
8443 Set_Last_Entity (Current_Scope, Prev);
8448 Enter_Overloaded_Entity (S);
8450 -- For entities generated by Derive_Subprograms the
8451 -- overridden operation is the inherited primitive
8452 -- (which is available through the attribute alias).
8454 if not (Comes_From_Source (E))
8455 and then Is_Dispatching_Operation (E)
8456 and then Find_Dispatching_Type (E) =
8457 Find_Dispatching_Type (S)
8458 and then Present (Alias (E))
8459 and then Comes_From_Source (Alias (E))
8461 Set_Overridden_Operation (S, Alias (E));
8463 -- Normal case of setting entity as overridden
8465 -- Note: Static_Initialization and Overridden_Operation
8466 -- attributes use the same field in subprogram entities.
8467 -- Static_Initialization is only defined for internal
8468 -- initialization procedures, where Overridden_Operation
8469 -- is irrelevant. Therefore the setting of this attribute
8470 -- must check whether the target is an init_proc.
8472 elsif not Is_Init_Proc (S) then
8473 Set_Overridden_Operation (S, E);
8476 Check_Overriding_Indicator (S, E, Is_Primitive => True);
8478 -- If S is a user-defined subprogram or a null procedure
8479 -- expanded to override an inherited null procedure, or a
8480 -- predefined dispatching primitive then indicate that E
8481 -- overrides the operation from which S is inherited.
8483 if Comes_From_Source (S)
8485 (Present (Parent (S))
8487 Nkind (Parent (S)) = N_Procedure_Specification
8489 Null_Present (Parent (S)))
8491 (Present (Alias (E))
8493 Is_Predefined_Dispatching_Operation (Alias (E)))
8495 if Present (Alias (E)) then
8496 Set_Overridden_Operation (S, Alias (E));
8500 if Is_Dispatching_Operation (E) then
8502 -- An overriding dispatching subprogram inherits the
8503 -- convention of the overridden subprogram (AI-117).
8505 Set_Convention (S, Convention (E));
8506 Check_Dispatching_Operation (S, E);
8509 Check_Dispatching_Operation (S, Empty);
8512 Check_For_Primitive_Subprogram
8513 (Is_Primitive_Subp, Is_Overriding => True);
8514 goto Check_Inequality;
8517 -- Apparent redeclarations in instances can occur when two
8518 -- formal types get the same actual type. The subprograms in
8519 -- in the instance are legal, even if not callable from the
8520 -- outside. Calls from within are disambiguated elsewhere.
8521 -- For dispatching operations in the visible part, the usual
8522 -- rules apply, and operations with the same profile are not
8525 elsif (In_Instance_Visible_Part
8526 and then not Is_Dispatching_Operation (E))
8527 or else In_Instance_Not_Visible
8531 -- Here we have a real error (identical profile)
8534 Error_Msg_Sloc := Sloc (E);
8536 -- Avoid cascaded errors if the entity appears in
8537 -- subsequent calls.
8539 Set_Scope (S, Current_Scope);
8541 -- Generate error, with extra useful warning for the case
8542 -- of a generic instance with no completion.
8544 if Is_Generic_Instance (S)
8545 and then not Has_Completion (E)
8548 ("instantiation cannot provide body for&", S);
8549 Error_Msg_N ("\& conflicts with declaration#", S);
8551 Error_Msg_N ("& conflicts with declaration#", S);
8558 -- If one subprogram has an access parameter and the other
8559 -- a parameter of an access type, calls to either might be
8560 -- ambiguous. Verify that parameters match except for the
8561 -- access parameter.
8563 if May_Hide_Profile then
8569 F1 := First_Formal (S);
8570 F2 := First_Formal (E);
8571 while Present (F1) and then Present (F2) loop
8572 if Is_Access_Type (Etype (F1)) then
8573 if not Is_Access_Type (Etype (F2))
8574 or else not Conforming_Types
8575 (Designated_Type (Etype (F1)),
8576 Designated_Type (Etype (F2)),
8579 May_Hide_Profile := False;
8583 not Conforming_Types
8584 (Etype (F1), Etype (F2), Type_Conformant)
8586 May_Hide_Profile := False;
8597 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
8606 -- On exit, we know that S is a new entity
8608 Enter_Overloaded_Entity (S);
8609 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8610 Check_Overriding_Indicator
8611 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8613 -- Overloading is not allowed in SPARK, except for operators
8615 if Nkind (S) /= N_Defining_Operator_Symbol then
8616 Error_Msg_Sloc := Sloc (Homonym (S));
8617 Check_SPARK_Restriction
8618 ("overloading not allowed with entity#", S);
8621 -- If S is a derived operation for an untagged type then by
8622 -- definition it's not a dispatching operation (even if the parent
8623 -- operation was dispatching), so Check_Dispatching_Operation is not
8624 -- called in that case.
8626 if No (Derived_Type)
8627 or else Is_Tagged_Type (Derived_Type)
8629 Check_Dispatching_Operation (S, Empty);
8633 -- If this is a user-defined equality operator that is not a derived
8634 -- subprogram, create the corresponding inequality. If the operation is
8635 -- dispatching, the expansion is done elsewhere, and we do not create
8636 -- an explicit inequality operation.
8638 <<Check_Inequality>>
8639 if Chars (S) = Name_Op_Eq
8640 and then Etype (S) = Standard_Boolean
8641 and then Present (Parent (S))
8642 and then not Is_Dispatching_Operation (S)
8644 Make_Inequality_Operator (S);
8646 if Ada_Version >= Ada_2012 then
8647 Check_Untagged_Equality (S);
8650 end New_Overloaded_Entity;
8652 ---------------------
8653 -- Process_Formals --
8654 ---------------------
8656 procedure Process_Formals
8658 Related_Nod : Node_Id)
8660 Param_Spec : Node_Id;
8662 Formal_Type : Entity_Id;
8666 Num_Out_Params : Nat := 0;
8667 First_Out_Param : Entity_Id := Empty;
8668 -- Used for setting Is_Only_Out_Parameter
8670 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8671 -- Determine whether an access type designates a type coming from a
8674 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8675 -- Check whether the default has a class-wide type. After analysis the
8676 -- default has the type of the formal, so we must also check explicitly
8677 -- for an access attribute.
8679 -------------------------------
8680 -- Designates_From_With_Type --
8681 -------------------------------
8683 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8684 Desig : Entity_Id := Typ;
8687 if Is_Access_Type (Desig) then
8688 Desig := Directly_Designated_Type (Desig);
8691 if Is_Class_Wide_Type (Desig) then
8692 Desig := Root_Type (Desig);
8696 Ekind (Desig) = E_Incomplete_Type
8697 and then From_With_Type (Desig);
8698 end Designates_From_With_Type;
8700 ---------------------------
8701 -- Is_Class_Wide_Default --
8702 ---------------------------
8704 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8706 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8707 or else (Nkind (D) = N_Attribute_Reference
8708 and then Attribute_Name (D) = Name_Access
8709 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8710 end Is_Class_Wide_Default;
8712 -- Start of processing for Process_Formals
8715 -- In order to prevent premature use of the formals in the same formal
8716 -- part, the Ekind is left undefined until all default expressions are
8717 -- analyzed. The Ekind is established in a separate loop at the end.
8719 Param_Spec := First (T);
8720 while Present (Param_Spec) loop
8721 Formal := Defining_Identifier (Param_Spec);
8722 Set_Never_Set_In_Source (Formal, True);
8723 Enter_Name (Formal);
8725 -- Case of ordinary parameters
8727 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8728 Find_Type (Parameter_Type (Param_Spec));
8729 Ptype := Parameter_Type (Param_Spec);
8731 if Ptype = Error then
8735 Formal_Type := Entity (Ptype);
8737 if Is_Incomplete_Type (Formal_Type)
8739 (Is_Class_Wide_Type (Formal_Type)
8740 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8742 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8743 -- primitive operations, as long as their completion is
8744 -- in the same declarative part. If in the private part
8745 -- this means that the type cannot be a Taft-amendment type.
8746 -- Check is done on package exit. For access to subprograms,
8747 -- the use is legal for Taft-amendment types.
8749 if Is_Tagged_Type (Formal_Type) then
8750 if Ekind (Scope (Current_Scope)) = E_Package
8751 and then not From_With_Type (Formal_Type)
8752 and then not Is_Class_Wide_Type (Formal_Type)
8755 (Parent (T), N_Access_Function_Definition,
8756 N_Access_Procedure_Definition)
8760 Private_Dependents (Base_Type (Formal_Type)));
8762 -- Freezing is delayed to ensure that Register_Prim
8763 -- will get called for this operation, which is needed
8764 -- in cases where static dispatch tables aren't built.
8765 -- (Note that the same is done for controlling access
8766 -- parameter cases in function Access_Definition.)
8768 Set_Has_Delayed_Freeze (Current_Scope);
8772 -- Special handling of Value_Type for CIL case
8774 elsif Is_Value_Type (Formal_Type) then
8777 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8778 N_Access_Procedure_Definition)
8781 -- AI05-0151: Tagged incomplete types are allowed in all
8782 -- formal parts. Untagged incomplete types are not allowed
8785 if Ada_Version >= Ada_2012 then
8786 if Is_Tagged_Type (Formal_Type) then
8789 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
8794 ("invalid use of untagged incomplete type&",
8795 Ptype, Formal_Type);
8800 ("invalid use of incomplete type&",
8801 Param_Spec, Formal_Type);
8803 -- Further checks on the legality of incomplete types
8804 -- in formal parts are delayed until the freeze point
8805 -- of the enclosing subprogram or access to subprogram.
8809 elsif Ekind (Formal_Type) = E_Void then
8811 ("premature use of&",
8812 Parameter_Type (Param_Spec), Formal_Type);
8815 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8816 -- declaration corresponding to the null-excluding type of the
8817 -- formal in the enclosing scope. Finally, replace the parameter
8818 -- type of the formal with the internal subtype.
8820 if Ada_Version >= Ada_2005
8821 and then Null_Exclusion_Present (Param_Spec)
8823 if not Is_Access_Type (Formal_Type) then
8825 ("`NOT NULL` allowed only for an access type", Param_Spec);
8828 if Can_Never_Be_Null (Formal_Type)
8829 and then Comes_From_Source (Related_Nod)
8832 ("`NOT NULL` not allowed (& already excludes null)",
8833 Param_Spec, Formal_Type);
8837 Create_Null_Excluding_Itype
8839 Related_Nod => Related_Nod,
8840 Scope_Id => Scope (Current_Scope));
8842 -- If the designated type of the itype is an itype we
8843 -- decorate it with the Has_Delayed_Freeze attribute to
8844 -- avoid problems with the backend.
8847 -- type T is access procedure;
8848 -- procedure Op (O : not null T);
8850 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8851 Set_Has_Delayed_Freeze (Formal_Type);
8856 -- An access formal type
8860 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8862 -- No need to continue if we already notified errors
8864 if not Present (Formal_Type) then
8868 -- Ada 2005 (AI-254)
8871 AD : constant Node_Id :=
8872 Access_To_Subprogram_Definition
8873 (Parameter_Type (Param_Spec));
8875 if Present (AD) and then Protected_Present (AD) then
8877 Replace_Anonymous_Access_To_Protected_Subprogram
8883 Set_Etype (Formal, Formal_Type);
8885 -- The parameter is in ALFA if-and-only-if its type is in ALFA
8887 if Is_In_ALFA (Formal_Type) then
8888 Set_Is_In_ALFA (Formal);
8890 Mark_Non_ALFA_Subprogram ("formal is not in ALFA", Formal);
8893 Default := Expression (Param_Spec);
8895 if Present (Default) then
8896 Check_SPARK_Restriction
8897 ("default expression is not allowed", Default);
8899 if Out_Present (Param_Spec) then
8901 ("default initialization only allowed for IN parameters",
8905 -- Do the special preanalysis of the expression (see section on
8906 -- "Handling of Default Expressions" in the spec of package Sem).
8908 Preanalyze_Spec_Expression (Default, Formal_Type);
8910 -- An access to constant cannot be the default for
8911 -- an access parameter that is an access to variable.
8913 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8914 and then not Is_Access_Constant (Formal_Type)
8915 and then Is_Access_Type (Etype (Default))
8916 and then Is_Access_Constant (Etype (Default))
8919 ("formal that is access to variable cannot be initialized " &
8920 "with an access-to-constant expression", Default);
8923 -- Check that the designated type of an access parameter's default
8924 -- is not a class-wide type unless the parameter's designated type
8925 -- is also class-wide.
8927 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8928 and then not Designates_From_With_Type (Formal_Type)
8929 and then Is_Class_Wide_Default (Default)
8930 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8933 ("access to class-wide expression not allowed here", Default);
8936 -- Check incorrect use of dynamically tagged expressions
8938 if Is_Tagged_Type (Formal_Type) then
8939 Check_Dynamically_Tagged_Expression
8942 Related_Nod => Default);
8946 -- Ada 2005 (AI-231): Static checks
8948 if Ada_Version >= Ada_2005
8949 and then Is_Access_Type (Etype (Formal))
8950 and then Can_Never_Be_Null (Etype (Formal))
8952 Null_Exclusion_Static_Checks (Param_Spec);
8959 -- If this is the formal part of a function specification, analyze the
8960 -- subtype mark in the context where the formals are visible but not
8961 -- yet usable, and may hide outer homographs.
8963 if Nkind (Related_Nod) = N_Function_Specification then
8964 Analyze_Return_Type (Related_Nod);
8967 -- Now set the kind (mode) of each formal
8969 Param_Spec := First (T);
8970 while Present (Param_Spec) loop
8971 Formal := Defining_Identifier (Param_Spec);
8972 Set_Formal_Mode (Formal);
8974 if Ekind (Formal) = E_In_Parameter then
8975 Set_Default_Value (Formal, Expression (Param_Spec));
8977 if Present (Expression (Param_Spec)) then
8978 Default := Expression (Param_Spec);
8980 if Is_Scalar_Type (Etype (Default)) then
8982 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8984 Formal_Type := Entity (Parameter_Type (Param_Spec));
8987 Formal_Type := Access_Definition
8988 (Related_Nod, Parameter_Type (Param_Spec));
8991 Apply_Scalar_Range_Check (Default, Formal_Type);
8995 elsif Ekind (Formal) = E_Out_Parameter then
8996 Num_Out_Params := Num_Out_Params + 1;
8998 if Num_Out_Params = 1 then
8999 First_Out_Param := Formal;
9002 elsif Ekind (Formal) = E_In_Out_Parameter then
9003 Num_Out_Params := Num_Out_Params + 1;
9009 if Present (First_Out_Param) and then Num_Out_Params = 1 then
9010 Set_Is_Only_Out_Parameter (First_Out_Param);
9012 end Process_Formals;
9018 procedure Process_PPCs
9020 Spec_Id : Entity_Id;
9021 Body_Id : Entity_Id)
9023 Loc : constant Source_Ptr := Sloc (N);
9027 Designator : Entity_Id;
9028 -- Subprogram designator, set from Spec_Id if present, else Body_Id
9030 Precond : Node_Id := Empty;
9031 -- Set non-Empty if we prepend precondition to the declarations. This
9032 -- is used to hook up inherited preconditions (adding the condition
9033 -- expression with OR ELSE, and adding the message).
9035 Inherited_Precond : Node_Id;
9036 -- Precondition inherited from parent subprogram
9038 Inherited : constant Subprogram_List :=
9039 Inherited_Subprograms (Spec_Id);
9040 -- List of subprograms inherited by this subprogram
9042 Plist : List_Id := No_List;
9043 -- List of generated postconditions
9045 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
9046 -- Prag contains an analyzed precondition or postcondition pragma. This
9047 -- function copies the pragma, changes it to the corresponding Check
9048 -- pragma and returns the Check pragma as the result. If Pspec is non-
9049 -- empty, this is the case of inheriting a PPC, where we must change
9050 -- references to parameters of the inherited subprogram to point to the
9051 -- corresponding parameters of the current subprogram.
9053 function Invariants_Or_Predicates_Present return Boolean;
9054 -- Determines if any invariants or predicates are present for any OUT
9055 -- or IN OUT parameters of the subprogram, or (for a function) if the
9056 -- return value has an invariant.
9062 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
9063 Nam : constant Name_Id := Pragma_Name (Prag);
9068 -- Prepare map if this is the case where we have to map entities of
9069 -- arguments in the overridden subprogram to corresponding entities
9070 -- of the current subprogram.
9081 Map := New_Elmt_List;
9082 PF := First_Formal (Pspec);
9083 CF := First_Formal (Designator);
9084 while Present (PF) loop
9085 Append_Elmt (PF, Map);
9086 Append_Elmt (CF, Map);
9093 -- Now we can copy the tree, doing any required substitutions
9095 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
9097 -- Set Analyzed to false, since we want to reanalyze the check
9098 -- procedure. Note that it is only at the outer level that we
9099 -- do this fiddling, for the spec cases, the already preanalyzed
9100 -- parameters are not affected.
9102 Set_Analyzed (CP, False);
9104 -- We also make sure Comes_From_Source is False for the copy
9106 Set_Comes_From_Source (CP, False);
9108 -- For a postcondition pragma within a generic, preserve the pragma
9109 -- for later expansion.
9111 if Nam = Name_Postcondition
9112 and then not Expander_Active
9117 -- Change copy of pragma into corresponding pragma Check
9119 Prepend_To (Pragma_Argument_Associations (CP),
9120 Make_Pragma_Argument_Association (Sloc (Prag),
9121 Expression => Make_Identifier (Loc, Nam)));
9122 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
9124 -- If this is inherited case and the current message starts with
9125 -- "failed p", we change it to "failed inherited p...".
9127 if Present (Pspec) then
9129 Msg : constant Node_Id :=
9130 Last (Pragma_Argument_Associations (CP));
9133 if Chars (Msg) = Name_Message then
9134 String_To_Name_Buffer (Strval (Expression (Msg)));
9136 if Name_Buffer (1 .. 8) = "failed p" then
9137 Insert_Str_In_Name_Buffer ("inherited ", 8);
9139 (Expression (Last (Pragma_Argument_Associations (CP))),
9140 String_From_Name_Buffer);
9146 -- Return the check pragma
9151 --------------------------------------
9152 -- Invariants_Or_Predicates_Present --
9153 --------------------------------------
9155 function Invariants_Or_Predicates_Present return Boolean is
9159 -- Check function return result
9161 if Ekind (Designator) /= E_Procedure
9162 and then Has_Invariants (Etype (Designator))
9169 Formal := First_Formal (Designator);
9170 while Present (Formal) loop
9171 if Ekind (Formal) /= E_In_Parameter
9173 (Has_Invariants (Etype (Formal))
9174 or else Present (Predicate_Function (Etype (Formal))))
9179 Next_Formal (Formal);
9183 end Invariants_Or_Predicates_Present;
9185 -- Start of processing for Process_PPCs
9188 -- Capture designator from spec if present, else from body
9190 if Present (Spec_Id) then
9191 Designator := Spec_Id;
9193 Designator := Body_Id;
9196 -- Grab preconditions from spec
9198 if Present (Spec_Id) then
9200 -- Loop through PPC pragmas from spec. Note that preconditions from
9201 -- the body will be analyzed and converted when we scan the body
9202 -- declarations below.
9204 Prag := Spec_PPC_List (Spec_Id);
9205 while Present (Prag) loop
9206 if Pragma_Name (Prag) = Name_Precondition then
9208 -- For Pre (or Precondition pragma), we simply prepend the
9209 -- pragma to the list of declarations right away so that it
9210 -- will be executed at the start of the procedure. Note that
9211 -- this processing reverses the order of the list, which is
9212 -- what we want since new entries were chained to the head of
9213 -- the list. There can be more then one precondition when we
9214 -- use pragma Precondition
9216 if not Class_Present (Prag) then
9217 Prepend (Grab_PPC, Declarations (N));
9219 -- For Pre'Class there can only be one pragma, and we save
9220 -- it in Precond for now. We will add inherited Pre'Class
9221 -- stuff before inserting this pragma in the declarations.
9223 Precond := Grab_PPC;
9227 Prag := Next_Pragma (Prag);
9230 -- Now deal with inherited preconditions
9232 for J in Inherited'Range loop
9233 Prag := Spec_PPC_List (Inherited (J));
9235 while Present (Prag) loop
9236 if Pragma_Name (Prag) = Name_Precondition
9237 and then Class_Present (Prag)
9239 Inherited_Precond := Grab_PPC (Inherited (J));
9241 -- No precondition so far, so establish this as the first
9243 if No (Precond) then
9244 Precond := Inherited_Precond;
9246 -- Here we already have a precondition, add inherited one
9249 -- Add new precondition to old one using OR ELSE
9252 New_Expr : constant Node_Id :=
9256 (Pragma_Argument_Associations
9257 (Inherited_Precond))));
9258 Old_Expr : constant Node_Id :=
9262 (Pragma_Argument_Associations
9266 if Paren_Count (Old_Expr) = 0 then
9267 Set_Paren_Count (Old_Expr, 1);
9270 if Paren_Count (New_Expr) = 0 then
9271 Set_Paren_Count (New_Expr, 1);
9275 Make_Or_Else (Sloc (Old_Expr),
9276 Left_Opnd => Relocate_Node (Old_Expr),
9277 Right_Opnd => New_Expr));
9280 -- Add new message in the form:
9282 -- failed precondition from bla
9283 -- also failed inherited precondition from bla
9286 -- Skip this if exception locations are suppressed
9288 if not Exception_Locations_Suppressed then
9290 New_Msg : constant Node_Id :=
9293 (Pragma_Argument_Associations
9294 (Inherited_Precond)));
9295 Old_Msg : constant Node_Id :=
9298 (Pragma_Argument_Associations
9301 Start_String (Strval (Old_Msg));
9302 Store_String_Chars (ASCII.LF & " also ");
9303 Store_String_Chars (Strval (New_Msg));
9304 Set_Strval (Old_Msg, End_String);
9310 Prag := Next_Pragma (Prag);
9314 -- If we have built a precondition for Pre'Class (including any
9315 -- Pre'Class aspects inherited from parent subprograms), then we
9316 -- insert this composite precondition at this stage.
9318 if Present (Precond) then
9319 Prepend (Precond, Declarations (N));
9323 -- Build postconditions procedure if needed and prepend the following
9324 -- declaration to the start of the declarations for the subprogram.
9326 -- procedure _postconditions [(_Result : resulttype)] is
9328 -- pragma Check (Postcondition, condition [,message]);
9329 -- pragma Check (Postcondition, condition [,message]);
9331 -- Invariant_Procedure (_Result) ...
9332 -- Invariant_Procedure (Arg1)
9336 -- First we deal with the postconditions in the body
9338 if Is_Non_Empty_List (Declarations (N)) then
9340 -- Loop through declarations
9342 Prag := First (Declarations (N));
9343 while Present (Prag) loop
9344 if Nkind (Prag) = N_Pragma then
9346 -- If pragma, capture if enabled postcondition, else ignore
9348 if Pragma_Name (Prag) = Name_Postcondition
9349 and then Check_Enabled (Name_Postcondition)
9351 if Plist = No_List then
9352 Plist := Empty_List;
9357 -- If expansion is disabled, as in a generic unit, save
9358 -- pragma for later expansion.
9360 if not Expander_Active then
9361 Prepend (Grab_PPC, Declarations (N));
9363 Append (Grab_PPC, Plist);
9369 -- Not a pragma, if comes from source, then end scan
9371 elsif Comes_From_Source (Prag) then
9374 -- Skip stuff not coming from source
9382 -- Now deal with any postconditions from the spec
9384 if Present (Spec_Id) then
9385 Spec_Postconditions : declare
9386 procedure Process_Post_Conditions
9389 -- This processes the Spec_PPC_List from Spec, processing any
9390 -- postconditions from the list. If Class is True, then only
9391 -- postconditions marked with Class_Present are considered.
9392 -- The caller has checked that Spec_PPC_List is non-Empty.
9394 -----------------------------
9395 -- Process_Post_Conditions --
9396 -----------------------------
9398 procedure Process_Post_Conditions
9411 -- Loop through PPC pragmas from spec
9413 Prag := Spec_PPC_List (Spec);
9415 if Pragma_Name (Prag) = Name_Postcondition
9416 and then (not Class or else Class_Present (Prag))
9418 if Plist = No_List then
9419 Plist := Empty_List;
9422 if not Expander_Active then
9424 (Grab_PPC (Pspec), Declarations (N));
9426 Append (Grab_PPC (Pspec), Plist);
9430 Prag := Next_Pragma (Prag);
9431 exit when No (Prag);
9433 end Process_Post_Conditions;
9435 -- Start of processing for Spec_Postconditions
9438 if Present (Spec_PPC_List (Spec_Id)) then
9439 Process_Post_Conditions (Spec_Id, Class => False);
9442 -- Process inherited postconditions
9444 for J in Inherited'Range loop
9445 if Present (Spec_PPC_List (Inherited (J))) then
9446 Process_Post_Conditions (Inherited (J), Class => True);
9449 end Spec_Postconditions;
9452 -- If we had any postconditions and expansion is enabled, or if the
9453 -- procedure has invariants, then build the _Postconditions procedure.
9455 if (Present (Plist) or else Invariants_Or_Predicates_Present)
9456 and then Expander_Active
9459 Plist := Empty_List;
9462 -- Special processing for function case
9464 if Ekind (Designator) /= E_Procedure then
9466 Rent : constant Entity_Id :=
9467 Make_Defining_Identifier (Loc,
9468 Chars => Name_uResult);
9469 Ftyp : constant Entity_Id := Etype (Designator);
9472 Set_Etype (Rent, Ftyp);
9474 -- Add argument for return
9478 Make_Parameter_Specification (Loc,
9479 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
9480 Defining_Identifier => Rent));
9482 -- Add invariant call if returning type with invariants
9484 if Has_Invariants (Etype (Rent))
9485 and then Present (Invariant_Procedure (Etype (Rent)))
9488 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
9492 -- Procedure rather than a function
9498 -- Add invariant calls and predicate calls for parameters. Note that
9499 -- this is done for functions as well, since in Ada 2012 they can
9500 -- have IN OUT args.
9507 Formal := First_Formal (Designator);
9508 while Present (Formal) loop
9509 if Ekind (Formal) /= E_In_Parameter then
9510 Ftype := Etype (Formal);
9512 if Has_Invariants (Ftype)
9513 and then Present (Invariant_Procedure (Ftype))
9517 (New_Occurrence_Of (Formal, Loc)));
9520 if Present (Predicate_Function (Ftype)) then
9522 Make_Predicate_Check
9523 (Ftype, New_Occurrence_Of (Formal, Loc)));
9527 Next_Formal (Formal);
9531 -- Build and insert postcondition procedure
9534 Post_Proc : constant Entity_Id :=
9535 Make_Defining_Identifier (Loc,
9536 Chars => Name_uPostconditions);
9537 -- The entity for the _Postconditions procedure
9540 Prepend_To (Declarations (N),
9541 Make_Subprogram_Body (Loc,
9543 Make_Procedure_Specification (Loc,
9544 Defining_Unit_Name => Post_Proc,
9545 Parameter_Specifications => Parms),
9547 Declarations => Empty_List,
9549 Handled_Statement_Sequence =>
9550 Make_Handled_Sequence_Of_Statements (Loc,
9551 Statements => Plist)));
9553 Set_Ekind (Post_Proc, E_Procedure);
9554 Set_Is_Postcondition_Proc (Post_Proc);
9556 -- If this is a procedure, set the Postcondition_Proc attribute on
9557 -- the proper defining entity for the subprogram.
9559 if Ekind (Designator) = E_Procedure then
9560 Set_Postcondition_Proc (Designator, Post_Proc);
9564 Set_Has_Postconditions (Designator);
9568 ----------------------------
9569 -- Reference_Body_Formals --
9570 ----------------------------
9572 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
9577 if Error_Posted (Spec) then
9581 -- Iterate over both lists. They may be of different lengths if the two
9582 -- specs are not conformant.
9584 Fs := First_Formal (Spec);
9585 Fb := First_Formal (Bod);
9586 while Present (Fs) and then Present (Fb) loop
9587 Generate_Reference (Fs, Fb, 'b');
9590 Style.Check_Identifier (Fb, Fs);
9593 Set_Spec_Entity (Fb, Fs);
9594 Set_Referenced (Fs, False);
9598 end Reference_Body_Formals;
9600 -------------------------
9601 -- Set_Actual_Subtypes --
9602 -------------------------
9604 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
9608 First_Stmt : Node_Id := Empty;
9609 AS_Needed : Boolean;
9612 -- If this is an empty initialization procedure, no need to create
9613 -- actual subtypes (small optimization).
9615 if Ekind (Subp) = E_Procedure
9616 and then Is_Null_Init_Proc (Subp)
9621 Formal := First_Formal (Subp);
9622 while Present (Formal) loop
9623 T := Etype (Formal);
9625 -- We never need an actual subtype for a constrained formal
9627 if Is_Constrained (T) then
9630 -- If we have unknown discriminants, then we do not need an actual
9631 -- subtype, or more accurately we cannot figure it out! Note that
9632 -- all class-wide types have unknown discriminants.
9634 elsif Has_Unknown_Discriminants (T) then
9637 -- At this stage we have an unconstrained type that may need an
9638 -- actual subtype. For sure the actual subtype is needed if we have
9639 -- an unconstrained array type.
9641 elsif Is_Array_Type (T) then
9644 -- The only other case needing an actual subtype is an unconstrained
9645 -- record type which is an IN parameter (we cannot generate actual
9646 -- subtypes for the OUT or IN OUT case, since an assignment can
9647 -- change the discriminant values. However we exclude the case of
9648 -- initialization procedures, since discriminants are handled very
9649 -- specially in this context, see the section entitled "Handling of
9650 -- Discriminants" in Einfo.
9652 -- We also exclude the case of Discrim_SO_Functions (functions used
9653 -- in front end layout mode for size/offset values), since in such
9654 -- functions only discriminants are referenced, and not only are such
9655 -- subtypes not needed, but they cannot always be generated, because
9656 -- of order of elaboration issues.
9658 elsif Is_Record_Type (T)
9659 and then Ekind (Formal) = E_In_Parameter
9660 and then Chars (Formal) /= Name_uInit
9661 and then not Is_Unchecked_Union (T)
9662 and then not Is_Discrim_SO_Function (Subp)
9666 -- All other cases do not need an actual subtype
9672 -- Generate actual subtypes for unconstrained arrays and
9673 -- unconstrained discriminated records.
9676 if Nkind (N) = N_Accept_Statement then
9678 -- If expansion is active, The formal is replaced by a local
9679 -- variable that renames the corresponding entry of the
9680 -- parameter block, and it is this local variable that may
9681 -- require an actual subtype.
9683 if Expander_Active then
9684 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
9686 Decl := Build_Actual_Subtype (T, Formal);
9689 if Present (Handled_Statement_Sequence (N)) then
9691 First (Statements (Handled_Statement_Sequence (N)));
9692 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
9693 Mark_Rewrite_Insertion (Decl);
9695 -- If the accept statement has no body, there will be no
9696 -- reference to the actuals, so no need to compute actual
9703 Decl := Build_Actual_Subtype (T, Formal);
9704 Prepend (Decl, Declarations (N));
9705 Mark_Rewrite_Insertion (Decl);
9708 -- The declaration uses the bounds of an existing object, and
9709 -- therefore needs no constraint checks.
9711 Analyze (Decl, Suppress => All_Checks);
9713 -- We need to freeze manually the generated type when it is
9714 -- inserted anywhere else than in a declarative part.
9716 if Present (First_Stmt) then
9717 Insert_List_Before_And_Analyze (First_Stmt,
9718 Freeze_Entity (Defining_Identifier (Decl), N));
9721 if Nkind (N) = N_Accept_Statement
9722 and then Expander_Active
9724 Set_Actual_Subtype (Renamed_Object (Formal),
9725 Defining_Identifier (Decl));
9727 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
9731 Next_Formal (Formal);
9733 end Set_Actual_Subtypes;
9735 ---------------------
9736 -- Set_Formal_Mode --
9737 ---------------------
9739 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
9740 Spec : constant Node_Id := Parent (Formal_Id);
9743 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
9744 -- since we ensure that corresponding actuals are always valid at the
9745 -- point of the call.
9747 if Out_Present (Spec) then
9748 if Ekind (Scope (Formal_Id)) = E_Function
9749 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
9751 -- [IN] OUT parameters allowed for functions in Ada 2012
9753 if Ada_Version >= Ada_2012 then
9754 if In_Present (Spec) then
9755 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9757 Set_Ekind (Formal_Id, E_Out_Parameter);
9760 -- But not in earlier versions of Ada
9763 Error_Msg_N ("functions can only have IN parameters", Spec);
9764 Set_Ekind (Formal_Id, E_In_Parameter);
9767 elsif In_Present (Spec) then
9768 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9771 Set_Ekind (Formal_Id, E_Out_Parameter);
9772 Set_Never_Set_In_Source (Formal_Id, True);
9773 Set_Is_True_Constant (Formal_Id, False);
9774 Set_Current_Value (Formal_Id, Empty);
9778 Set_Ekind (Formal_Id, E_In_Parameter);
9781 -- Set Is_Known_Non_Null for access parameters since the language
9782 -- guarantees that access parameters are always non-null. We also set
9783 -- Can_Never_Be_Null, since there is no way to change the value.
9785 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
9787 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
9788 -- null; In Ada 2005, only if then null_exclusion is explicit.
9790 if Ada_Version < Ada_2005
9791 or else Can_Never_Be_Null (Etype (Formal_Id))
9793 Set_Is_Known_Non_Null (Formal_Id);
9794 Set_Can_Never_Be_Null (Formal_Id);
9797 -- Ada 2005 (AI-231): Null-exclusion access subtype
9799 elsif Is_Access_Type (Etype (Formal_Id))
9800 and then Can_Never_Be_Null (Etype (Formal_Id))
9802 Set_Is_Known_Non_Null (Formal_Id);
9805 Set_Mechanism (Formal_Id, Default_Mechanism);
9806 Set_Formal_Validity (Formal_Id);
9807 end Set_Formal_Mode;
9809 -------------------------
9810 -- Set_Formal_Validity --
9811 -------------------------
9813 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
9815 -- If no validity checking, then we cannot assume anything about the
9816 -- validity of parameters, since we do not know there is any checking
9817 -- of the validity on the call side.
9819 if not Validity_Checks_On then
9822 -- If validity checking for parameters is enabled, this means we are
9823 -- not supposed to make any assumptions about argument values.
9825 elsif Validity_Check_Parameters then
9828 -- If we are checking in parameters, we will assume that the caller is
9829 -- also checking parameters, so we can assume the parameter is valid.
9831 elsif Ekind (Formal_Id) = E_In_Parameter
9832 and then Validity_Check_In_Params
9834 Set_Is_Known_Valid (Formal_Id, True);
9836 -- Similar treatment for IN OUT parameters
9838 elsif Ekind (Formal_Id) = E_In_Out_Parameter
9839 and then Validity_Check_In_Out_Params
9841 Set_Is_Known_Valid (Formal_Id, True);
9843 end Set_Formal_Validity;
9845 ------------------------
9846 -- Subtype_Conformant --
9847 ------------------------
9849 function Subtype_Conformant
9850 (New_Id : Entity_Id;
9852 Skip_Controlling_Formals : Boolean := False) return Boolean
9856 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
9857 Skip_Controlling_Formals => Skip_Controlling_Formals);
9859 end Subtype_Conformant;
9861 ---------------------
9862 -- Type_Conformant --
9863 ---------------------
9865 function Type_Conformant
9866 (New_Id : Entity_Id;
9868 Skip_Controlling_Formals : Boolean := False) return Boolean
9872 May_Hide_Profile := False;
9875 (New_Id, Old_Id, Type_Conformant, False, Result,
9876 Skip_Controlling_Formals => Skip_Controlling_Formals);
9878 end Type_Conformant;
9880 -------------------------------
9881 -- Valid_Operator_Definition --
9882 -------------------------------
9884 procedure Valid_Operator_Definition (Designator : Entity_Id) is
9887 Id : constant Name_Id := Chars (Designator);
9891 F := First_Formal (Designator);
9892 while Present (F) loop
9895 if Present (Default_Value (F)) then
9897 ("default values not allowed for operator parameters",
9904 -- Verify that user-defined operators have proper number of arguments
9905 -- First case of operators which can only be unary
9908 or else Id = Name_Op_Abs
9912 -- Case of operators which can be unary or binary
9914 elsif Id = Name_Op_Add
9915 or Id = Name_Op_Subtract
9917 N_OK := (N in 1 .. 2);
9919 -- All other operators can only be binary
9927 ("incorrect number of arguments for operator", Designator);
9931 and then Base_Type (Etype (Designator)) = Standard_Boolean
9932 and then not Is_Intrinsic_Subprogram (Designator)
9935 ("explicit definition of inequality not allowed", Designator);
9937 end Valid_Operator_Definition;