1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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_Formal_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));
275 Prev : constant Entity_Id := Current_Entity_In_Scope (Def_Id);
276 -- If the expression is a completion, Prev is the entity whose
277 -- declaration is completed.
280 -- This is one of the occasions on which we transform the tree during
281 -- semantic analysis. Transform the expression function into an
282 -- equivalent subprogram body, and then analyze that.
285 Make_Subprogram_Body (Loc,
286 Specification => Specification (N),
287 Declarations => Empty_List,
288 Handled_Statement_Sequence =>
289 Make_Handled_Sequence_Of_Statements (LocX,
290 Statements => New_List (
291 Make_Simple_Return_Statement (LocX,
292 Expression => Expression (N)))));
295 and then Ekind (Prev) = E_Generic_Function
297 -- If the expression completes a generic subprogram, we must create a
298 -- separate node for the body, because at instantiation the original
299 -- node of the generic copy must be a generic subprogram body, and
300 -- cannot be a expression function. Otherwise we just rewrite the
301 -- expression with the non-generic body.
303 Insert_After (N, New_Body);
304 Rewrite (N, Make_Null_Statement (Loc));
309 Rewrite (N, New_Body);
312 end Analyze_Expression_Function;
314 ----------------------------------------
315 -- Analyze_Extended_Return_Statement --
316 ----------------------------------------
318 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
320 Analyze_Return_Statement (N);
321 end Analyze_Extended_Return_Statement;
323 ----------------------------
324 -- Analyze_Function_Call --
325 ----------------------------
327 procedure Analyze_Function_Call (N : Node_Id) is
328 P : constant Node_Id := Name (N);
329 L : constant List_Id := Parameter_Associations (N);
335 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
336 -- as B (A, X). If the rewriting is successful, the call has been
337 -- analyzed and we just return.
339 if Nkind (P) = N_Selected_Component
340 and then Name (N) /= P
341 and then Is_Rewrite_Substitution (N)
342 and then Present (Etype (N))
347 -- If error analyzing name, then set Any_Type as result type and return
349 if Etype (P) = Any_Type then
350 Set_Etype (N, Any_Type);
354 -- Otherwise analyze the parameters
358 while Present (Actual) loop
360 Check_Parameterless_Call (Actual);
366 end Analyze_Function_Call;
368 -----------------------------
369 -- Analyze_Function_Return --
370 -----------------------------
372 procedure Analyze_Function_Return (N : Node_Id) is
373 Loc : constant Source_Ptr := Sloc (N);
374 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
375 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
377 R_Type : constant Entity_Id := Etype (Scope_Id);
378 -- Function result subtype
380 procedure Check_Limited_Return (Expr : Node_Id);
381 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
382 -- limited types. Used only for simple return statements.
383 -- Expr is the expression returned.
385 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
386 -- Check that the return_subtype_indication properly matches the result
387 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
389 --------------------------
390 -- Check_Limited_Return --
391 --------------------------
393 procedure Check_Limited_Return (Expr : Node_Id) is
395 -- Ada 2005 (AI-318-02): Return-by-reference types have been
396 -- removed and replaced by anonymous access results. This is an
397 -- incompatibility with Ada 95. Not clear whether this should be
398 -- enforced yet or perhaps controllable with special switch. ???
400 if Is_Limited_Type (R_Type)
401 and then Comes_From_Source (N)
402 and then not In_Instance_Body
403 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
407 if Ada_Version >= Ada_2005
408 and then not Debug_Flag_Dot_L
409 and then not GNAT_Mode
412 ("(Ada 2005) cannot copy object of a limited type " &
413 "(RM-2005 6.5(5.5/2))", Expr);
415 if Is_Immutably_Limited_Type (R_Type) then
417 ("\return by reference not permitted in Ada 2005", Expr);
420 -- Warn in Ada 95 mode, to give folks a heads up about this
423 -- In GNAT mode, this is just a warning, to allow it to be
424 -- evilly turned off. Otherwise it is a real error.
426 -- In a generic context, simplify the warning because it makes
427 -- no sense to discuss pass-by-reference or copy.
429 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
430 if Inside_A_Generic then
432 ("return of limited object not permitted in Ada2005 "
433 & "(RM-2005 6.5(5.5/2))?", Expr);
435 elsif Is_Immutably_Limited_Type (R_Type) then
437 ("return by reference not permitted in Ada 2005 "
438 & "(RM-2005 6.5(5.5/2))?", Expr);
441 ("cannot copy object of a limited type in Ada 2005 "
442 & "(RM-2005 6.5(5.5/2))?", Expr);
445 -- Ada 95 mode, compatibility warnings disabled
448 return; -- skip continuation messages below
451 if not Inside_A_Generic then
453 ("\consider switching to return of access type", Expr);
454 Explain_Limited_Type (R_Type, Expr);
457 end Check_Limited_Return;
459 -------------------------------------
460 -- Check_Return_Subtype_Indication --
461 -------------------------------------
463 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
464 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
466 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
467 -- Subtype given in the extended return statement (must match R_Type)
469 Subtype_Ind : constant Node_Id :=
470 Object_Definition (Original_Node (Obj_Decl));
472 R_Type_Is_Anon_Access :
474 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
476 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
478 Ekind (R_Type) = E_Anonymous_Access_Type;
479 -- True if return type of the function is an anonymous access type
480 -- Can't we make Is_Anonymous_Access_Type in einfo ???
482 R_Stm_Type_Is_Anon_Access :
484 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
486 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
488 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
489 -- True if type of the return object is an anonymous access type
492 -- First, avoid cascaded errors
494 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
498 -- "return access T" case; check that the return statement also has
499 -- "access T", and that the subtypes statically match:
500 -- if this is an access to subprogram the signatures must match.
502 if R_Type_Is_Anon_Access then
503 if R_Stm_Type_Is_Anon_Access then
505 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
507 if Base_Type (Designated_Type (R_Stm_Type)) /=
508 Base_Type (Designated_Type (R_Type))
509 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
512 ("subtype must statically match function result subtype",
513 Subtype_Mark (Subtype_Ind));
517 -- For two anonymous access to subprogram types, the
518 -- types themselves must be type conformant.
520 if not Conforming_Types
521 (R_Stm_Type, R_Type, Fully_Conformant)
524 ("subtype must statically match function result subtype",
530 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
533 -- Subtype indication case: check that the return object's type is
534 -- covered by the result type, and that the subtypes statically match
535 -- when the result subtype is constrained. Also handle record types
536 -- with unknown discriminants for which we have built the underlying
537 -- record view. Coverage is needed to allow specific-type return
538 -- objects when the result type is class-wide (see AI05-32).
540 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
541 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
545 Underlying_Record_View (Base_Type (R_Stm_Type))))
547 -- A null exclusion may be present on the return type, on the
548 -- function specification, on the object declaration or on the
551 if Is_Access_Type (R_Type)
553 (Can_Never_Be_Null (R_Type)
554 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
555 Can_Never_Be_Null (R_Stm_Type)
558 ("subtype must statically match function result subtype",
562 -- AI05-103: for elementary types, subtypes must statically match
564 if Is_Constrained (R_Type)
565 or else Is_Access_Type (R_Type)
567 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
569 ("subtype must statically match function result subtype",
574 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
575 and then Is_Null_Extension (Base_Type (R_Type))
581 ("wrong type for return_subtype_indication", Subtype_Ind);
583 end Check_Return_Subtype_Indication;
585 ---------------------
586 -- Local Variables --
587 ---------------------
591 -- Start of processing for Analyze_Function_Return
594 Set_Return_Present (Scope_Id);
596 if Nkind (N) = N_Simple_Return_Statement then
597 Expr := Expression (N);
598 Analyze_And_Resolve (Expr, R_Type);
599 Check_Limited_Return (Expr);
601 -- The only RETURN allowed in SPARK or ALFA is as the last statement
604 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
606 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
607 or else Present (Next (N)))
609 Check_Formal_Restriction
610 ("RETURN should be the last statement in function", N);
614 Check_Formal_Restriction ("extended RETURN is not allowed", N);
616 -- Analyze parts specific to extended_return_statement:
619 Obj_Decl : constant Node_Id :=
620 Last (Return_Object_Declarations (N));
622 HSS : constant Node_Id := Handled_Statement_Sequence (N);
625 Expr := Expression (Obj_Decl);
627 -- Note: The check for OK_For_Limited_Init will happen in
628 -- Analyze_Object_Declaration; we treat it as a normal
629 -- object declaration.
631 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
634 Check_Return_Subtype_Indication (Obj_Decl);
636 if Present (HSS) then
639 if Present (Exception_Handlers (HSS)) then
641 -- ???Has_Nested_Block_With_Handler needs to be set.
642 -- Probably by creating an actual N_Block_Statement.
643 -- Probably in Expand.
649 -- Mark the return object as referenced, since the return is an
650 -- implicit reference of the object.
652 Set_Referenced (Defining_Identifier (Obj_Decl));
654 Check_References (Stm_Entity);
658 -- Case of Expr present
662 -- Defend against previous errors
664 and then Nkind (Expr) /= N_Empty
665 and then Present (Etype (Expr))
667 -- Apply constraint check. Note that this is done before the implicit
668 -- conversion of the expression done for anonymous access types to
669 -- ensure correct generation of the null-excluding check associated
670 -- with null-excluding expressions found in return statements.
672 Apply_Constraint_Check (Expr, R_Type);
674 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
675 -- type, apply an implicit conversion of the expression to that type
676 -- to force appropriate static and run-time accessibility checks.
678 if Ada_Version >= Ada_2005
679 and then Ekind (R_Type) = E_Anonymous_Access_Type
681 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
682 Analyze_And_Resolve (Expr, R_Type);
685 -- If the result type is class-wide, then check that the return
686 -- expression's type is not declared at a deeper level than the
687 -- function (RM05-6.5(5.6/2)).
689 if Ada_Version >= Ada_2005
690 and then Is_Class_Wide_Type (R_Type)
692 if Type_Access_Level (Etype (Expr)) >
693 Subprogram_Access_Level (Scope_Id)
696 ("level of return expression type is deeper than " &
697 "class-wide function!", Expr);
701 -- Check incorrect use of dynamically tagged expression
703 if Is_Tagged_Type (R_Type) then
704 Check_Dynamically_Tagged_Expression
710 -- ??? A real run-time accessibility check is needed in cases
711 -- involving dereferences of access parameters. For now we just
712 -- check the static cases.
714 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
715 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
716 and then Object_Access_Level (Expr) >
717 Subprogram_Access_Level (Scope_Id)
720 -- Suppress the message in a generic, where the rewriting
723 if Inside_A_Generic then
728 Make_Raise_Program_Error (Loc,
729 Reason => PE_Accessibility_Check_Failed));
733 ("cannot return a local value by reference?", N);
735 ("\& will be raised at run time?",
736 N, Standard_Program_Error);
741 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
742 and then Null_Exclusion_Present (Parent (Scope_Id))
744 Apply_Compile_Time_Constraint_Error
746 Msg => "(Ada 2005) null not allowed for "
747 & "null-excluding return?",
748 Reason => CE_Null_Not_Allowed);
751 -- Apply checks suggested by AI05-0144 (dangerous order dependence)
753 Check_Order_Dependence;
755 end Analyze_Function_Return;
757 -------------------------------------
758 -- Analyze_Generic_Subprogram_Body --
759 -------------------------------------
761 procedure Analyze_Generic_Subprogram_Body
765 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
766 Kind : constant Entity_Kind := Ekind (Gen_Id);
772 -- Copy body and disable expansion while analyzing the generic For a
773 -- stub, do not copy the stub (which would load the proper body), this
774 -- will be done when the proper body is analyzed.
776 if Nkind (N) /= N_Subprogram_Body_Stub then
777 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
782 Spec := Specification (N);
784 -- Within the body of the generic, the subprogram is callable, and
785 -- behaves like the corresponding non-generic unit.
787 Body_Id := Defining_Entity (Spec);
789 if Kind = E_Generic_Procedure
790 and then Nkind (Spec) /= N_Procedure_Specification
792 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
795 elsif Kind = E_Generic_Function
796 and then Nkind (Spec) /= N_Function_Specification
798 Error_Msg_N ("invalid body for generic function ", Body_Id);
802 Set_Corresponding_Body (Gen_Decl, Body_Id);
804 if Has_Completion (Gen_Id)
805 and then Nkind (Parent (N)) /= N_Subunit
807 Error_Msg_N ("duplicate generic body", N);
810 Set_Has_Completion (Gen_Id);
813 if Nkind (N) = N_Subprogram_Body_Stub then
814 Set_Ekind (Defining_Entity (Specification (N)), Kind);
816 Set_Corresponding_Spec (N, Gen_Id);
819 if Nkind (Parent (N)) = N_Compilation_Unit then
820 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
823 -- Make generic parameters immediately visible in the body. They are
824 -- needed to process the formals declarations. Then make the formals
825 -- visible in a separate step.
831 First_Ent : Entity_Id;
834 First_Ent := First_Entity (Gen_Id);
837 while Present (E) and then not Is_Formal (E) loop
842 Set_Use (Generic_Formal_Declarations (Gen_Decl));
844 -- Now generic formals are visible, and the specification can be
845 -- analyzed, for subsequent conformance check.
847 Body_Id := Analyze_Subprogram_Specification (Spec);
849 -- Make formal parameters visible
853 -- E is the first formal parameter, we loop through the formals
854 -- installing them so that they will be visible.
856 Set_First_Entity (Gen_Id, E);
857 while Present (E) loop
863 -- Visible generic entity is callable within its own body
865 Set_Ekind (Gen_Id, Ekind (Body_Id));
866 Set_Ekind (Body_Id, E_Subprogram_Body);
867 Set_Convention (Body_Id, Convention (Gen_Id));
868 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
869 Set_Scope (Body_Id, Scope (Gen_Id));
870 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
872 if Nkind (N) = N_Subprogram_Body_Stub then
874 -- No body to analyze, so restore state of generic unit
876 Set_Ekind (Gen_Id, Kind);
877 Set_Ekind (Body_Id, Kind);
879 if Present (First_Ent) then
880 Set_First_Entity (Gen_Id, First_Ent);
887 -- If this is a compilation unit, it must be made visible explicitly,
888 -- because the compilation of the declaration, unlike other library
889 -- unit declarations, does not. If it is not a unit, the following
890 -- is redundant but harmless.
892 Set_Is_Immediately_Visible (Gen_Id);
893 Reference_Body_Formals (Gen_Id, Body_Id);
895 if Is_Child_Unit (Gen_Id) then
896 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
899 Set_Actual_Subtypes (N, Current_Scope);
900 Process_PPCs (N, Gen_Id, Body_Id);
902 -- If the generic unit carries pre- or post-conditions, copy them
903 -- to the original generic tree, so that they are properly added
904 -- to any instantiation.
907 Orig : constant Node_Id := Original_Node (N);
911 Cond := First (Declarations (N));
912 while Present (Cond) loop
913 if Nkind (Cond) = N_Pragma
914 and then Pragma_Name (Cond) = Name_Check
916 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
918 elsif Nkind (Cond) = N_Pragma
919 and then Pragma_Name (Cond) = Name_Postcondition
921 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
922 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
931 Analyze_Declarations (Declarations (N));
933 Analyze (Handled_Statement_Sequence (N));
935 Save_Global_References (Original_Node (N));
937 -- Prior to exiting the scope, include generic formals again (if any
938 -- are present) in the set of local entities.
940 if Present (First_Ent) then
941 Set_First_Entity (Gen_Id, First_Ent);
944 Check_References (Gen_Id);
947 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
949 Check_Subprogram_Order (N);
951 -- Outside of its body, unit is generic again
953 Set_Ekind (Gen_Id, Kind);
954 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
957 Style.Check_Identifier (Body_Id, Gen_Id);
961 end Analyze_Generic_Subprogram_Body;
963 -----------------------------
964 -- Analyze_Operator_Symbol --
965 -----------------------------
967 -- An operator symbol such as "+" or "and" may appear in context where the
968 -- literal denotes an entity name, such as "+"(x, y) or in context when it
969 -- is just a string, as in (conjunction = "or"). In these cases the parser
970 -- generates this node, and the semantics does the disambiguation. Other
971 -- such case are actuals in an instantiation, the generic unit in an
972 -- instantiation, and pragma arguments.
974 procedure Analyze_Operator_Symbol (N : Node_Id) is
975 Par : constant Node_Id := Parent (N);
978 if (Nkind (Par) = N_Function_Call
979 and then N = Name (Par))
980 or else Nkind (Par) = N_Function_Instantiation
981 or else (Nkind (Par) = N_Indexed_Component
982 and then N = Prefix (Par))
983 or else (Nkind (Par) = N_Pragma_Argument_Association
984 and then not Is_Pragma_String_Literal (Par))
985 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
986 or else (Nkind (Par) = N_Attribute_Reference
987 and then Attribute_Name (Par) /= Name_Value)
989 Find_Direct_Name (N);
992 Change_Operator_Symbol_To_String_Literal (N);
995 end Analyze_Operator_Symbol;
997 -----------------------------------
998 -- Analyze_Parameter_Association --
999 -----------------------------------
1001 procedure Analyze_Parameter_Association (N : Node_Id) is
1003 Analyze (Explicit_Actual_Parameter (N));
1004 end Analyze_Parameter_Association;
1006 ----------------------------
1007 -- Analyze_Procedure_Call --
1008 ----------------------------
1010 procedure Analyze_Procedure_Call (N : Node_Id) is
1011 Loc : constant Source_Ptr := Sloc (N);
1012 P : constant Node_Id := Name (N);
1013 Actuals : constant List_Id := Parameter_Associations (N);
1017 procedure Analyze_Call_And_Resolve;
1018 -- Do Analyze and Resolve calls for procedure call
1019 -- At end, check illegal order dependence.
1021 ------------------------------
1022 -- Analyze_Call_And_Resolve --
1023 ------------------------------
1025 procedure Analyze_Call_And_Resolve is
1027 if Nkind (N) = N_Procedure_Call_Statement then
1029 Resolve (N, Standard_Void_Type);
1031 -- Apply checks suggested by AI05-0144
1033 Check_Order_Dependence;
1038 end Analyze_Call_And_Resolve;
1040 -- Start of processing for Analyze_Procedure_Call
1043 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1044 -- a procedure call or an entry call. The prefix may denote an access
1045 -- to subprogram type, in which case an implicit dereference applies.
1046 -- If the prefix is an indexed component (without implicit dereference)
1047 -- then the construct denotes a call to a member of an entire family.
1048 -- If the prefix is a simple name, it may still denote a call to a
1049 -- parameterless member of an entry family. Resolution of these various
1050 -- interpretations is delicate.
1054 -- If this is a call of the form Obj.Op, the call may have been
1055 -- analyzed and possibly rewritten into a block, in which case
1058 if Analyzed (N) then
1062 -- If there is an error analyzing the name (which may have been
1063 -- rewritten if the original call was in prefix notation) then error
1064 -- has been emitted already, mark node and return.
1067 or else Etype (Name (N)) = Any_Type
1069 Set_Etype (N, Any_Type);
1073 -- Otherwise analyze the parameters
1075 if Present (Actuals) then
1076 Actual := First (Actuals);
1078 while Present (Actual) loop
1080 Check_Parameterless_Call (Actual);
1085 -- Special processing for Elab_Spec and Elab_Body calls
1087 if Nkind (P) = N_Attribute_Reference
1088 and then (Attribute_Name (P) = Name_Elab_Spec
1089 or else Attribute_Name (P) = Name_Elab_Body)
1091 if Present (Actuals) then
1093 ("no parameters allowed for this call", First (Actuals));
1097 Set_Etype (N, Standard_Void_Type);
1100 elsif Is_Entity_Name (P)
1101 and then Is_Record_Type (Etype (Entity (P)))
1102 and then Remote_AST_I_Dereference (P)
1106 elsif Is_Entity_Name (P)
1107 and then Ekind (Entity (P)) /= E_Entry_Family
1109 if Is_Access_Type (Etype (P))
1110 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1111 and then No (Actuals)
1112 and then Comes_From_Source (N)
1114 Error_Msg_N ("missing explicit dereference in call", N);
1117 Analyze_Call_And_Resolve;
1119 -- If the prefix is the simple name of an entry family, this is
1120 -- a parameterless call from within the task body itself.
1122 elsif Is_Entity_Name (P)
1123 and then Nkind (P) = N_Identifier
1124 and then Ekind (Entity (P)) = E_Entry_Family
1125 and then Present (Actuals)
1126 and then No (Next (First (Actuals)))
1128 -- Can be call to parameterless entry family. What appears to be the
1129 -- sole argument is in fact the entry index. Rewrite prefix of node
1130 -- accordingly. Source representation is unchanged by this
1134 Make_Indexed_Component (Loc,
1136 Make_Selected_Component (Loc,
1137 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1138 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1139 Expressions => Actuals);
1140 Set_Name (N, New_N);
1141 Set_Etype (New_N, Standard_Void_Type);
1142 Set_Parameter_Associations (N, No_List);
1143 Analyze_Call_And_Resolve;
1145 elsif Nkind (P) = N_Explicit_Dereference then
1146 if Ekind (Etype (P)) = E_Subprogram_Type then
1147 Analyze_Call_And_Resolve;
1149 Error_Msg_N ("expect access to procedure in call", P);
1152 -- The name can be a selected component or an indexed component that
1153 -- yields an access to subprogram. Such a prefix is legal if the call
1154 -- has parameter associations.
1156 elsif Is_Access_Type (Etype (P))
1157 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1159 if Present (Actuals) then
1160 Analyze_Call_And_Resolve;
1162 Error_Msg_N ("missing explicit dereference in call ", N);
1165 -- If not an access to subprogram, then the prefix must resolve to the
1166 -- name of an entry, entry family, or protected operation.
1168 -- For the case of a simple entry call, P is a selected component where
1169 -- the prefix is the task and the selector name is the entry. A call to
1170 -- a protected procedure will have the same syntax. If the protected
1171 -- object contains overloaded operations, the entity may appear as a
1172 -- function, the context will select the operation whose type is Void.
1174 elsif Nkind (P) = N_Selected_Component
1175 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1177 Ekind (Entity (Selector_Name (P))) = E_Procedure
1179 Ekind (Entity (Selector_Name (P))) = E_Function)
1181 Analyze_Call_And_Resolve;
1183 elsif Nkind (P) = N_Selected_Component
1184 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1185 and then Present (Actuals)
1186 and then No (Next (First (Actuals)))
1188 -- Can be call to parameterless entry family. What appears to be the
1189 -- sole argument is in fact the entry index. Rewrite prefix of node
1190 -- accordingly. Source representation is unchanged by this
1194 Make_Indexed_Component (Loc,
1195 Prefix => New_Copy (P),
1196 Expressions => Actuals);
1197 Set_Name (N, New_N);
1198 Set_Etype (New_N, Standard_Void_Type);
1199 Set_Parameter_Associations (N, No_List);
1200 Analyze_Call_And_Resolve;
1202 -- For the case of a reference to an element of an entry family, P is
1203 -- an indexed component whose prefix is a selected component (task and
1204 -- entry family), and whose index is the entry family index.
1206 elsif Nkind (P) = N_Indexed_Component
1207 and then Nkind (Prefix (P)) = N_Selected_Component
1208 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1210 Analyze_Call_And_Resolve;
1212 -- If the prefix is the name of an entry family, it is a call from
1213 -- within the task body itself.
1215 elsif Nkind (P) = N_Indexed_Component
1216 and then Nkind (Prefix (P)) = N_Identifier
1217 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1220 Make_Selected_Component (Loc,
1221 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1222 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1223 Rewrite (Prefix (P), New_N);
1225 Analyze_Call_And_Resolve;
1227 -- Anything else is an error
1230 Error_Msg_N ("invalid procedure or entry call", N);
1232 end Analyze_Procedure_Call;
1234 ------------------------------
1235 -- Analyze_Return_Statement --
1236 ------------------------------
1238 procedure Analyze_Return_Statement (N : Node_Id) is
1240 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1241 N_Extended_Return_Statement));
1243 Returns_Object : constant Boolean :=
1244 Nkind (N) = N_Extended_Return_Statement
1246 (Nkind (N) = N_Simple_Return_Statement
1247 and then Present (Expression (N)));
1248 -- True if we're returning something; that is, "return <expression>;"
1249 -- or "return Result : T [:= ...]". False for "return;". Used for error
1250 -- checking: If Returns_Object is True, N should apply to a function
1251 -- body; otherwise N should apply to a procedure body, entry body,
1252 -- accept statement, or extended return statement.
1254 function Find_What_It_Applies_To return Entity_Id;
1255 -- Find the entity representing the innermost enclosing body, accept
1256 -- statement, or extended return statement. If the result is a callable
1257 -- construct or extended return statement, then this will be the value
1258 -- of the Return_Applies_To attribute. Otherwise, the program is
1259 -- illegal. See RM-6.5(4/2).
1261 -----------------------------
1262 -- Find_What_It_Applies_To --
1263 -----------------------------
1265 function Find_What_It_Applies_To return Entity_Id is
1266 Result : Entity_Id := Empty;
1269 -- Loop outward through the Scope_Stack, skipping blocks and loops
1271 for J in reverse 0 .. Scope_Stack.Last loop
1272 Result := Scope_Stack.Table (J).Entity;
1273 exit when Ekind (Result) /= E_Block and then
1274 Ekind (Result) /= E_Loop;
1277 pragma Assert (Present (Result));
1279 end Find_What_It_Applies_To;
1281 -- Local declarations
1283 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1284 Kind : constant Entity_Kind := Ekind (Scope_Id);
1285 Loc : constant Source_Ptr := Sloc (N);
1286 Stm_Entity : constant Entity_Id :=
1288 (E_Return_Statement, Current_Scope, Loc, 'R');
1290 -- Start of processing for Analyze_Return_Statement
1293 Set_Return_Statement_Entity (N, Stm_Entity);
1295 Set_Etype (Stm_Entity, Standard_Void_Type);
1296 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1298 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1299 -- (4/2): an inner return statement will apply to this extended return.
1301 if Nkind (N) = N_Extended_Return_Statement then
1302 Push_Scope (Stm_Entity);
1305 -- Check that pragma No_Return is obeyed. Don't complain about the
1306 -- implicitly-generated return that is placed at the end.
1308 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1309 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1312 -- Warn on any unassigned OUT parameters if in procedure
1314 if Ekind (Scope_Id) = E_Procedure then
1315 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1318 -- Check that functions return objects, and other things do not
1320 if Kind = E_Function or else Kind = E_Generic_Function then
1321 if not Returns_Object then
1322 Error_Msg_N ("missing expression in return from function", N);
1325 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1326 if Returns_Object then
1327 Error_Msg_N ("procedure cannot return value (use function)", N);
1330 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1331 if Returns_Object then
1332 if Is_Protected_Type (Scope (Scope_Id)) then
1333 Error_Msg_N ("entry body cannot return value", N);
1335 Error_Msg_N ("accept statement cannot return value", N);
1339 elsif Kind = E_Return_Statement then
1341 -- We are nested within another return statement, which must be an
1342 -- extended_return_statement.
1344 if Returns_Object then
1346 ("extended_return_statement cannot return value; " &
1347 "use `""RETURN;""`", N);
1351 Error_Msg_N ("illegal context for return statement", N);
1354 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1355 Analyze_Function_Return (N);
1357 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1358 Set_Return_Present (Scope_Id);
1361 if Nkind (N) = N_Extended_Return_Statement then
1365 Kill_Current_Values (Last_Assignment_Only => True);
1366 Check_Unreachable_Code (N);
1367 end Analyze_Return_Statement;
1369 -------------------------------------
1370 -- Analyze_Simple_Return_Statement --
1371 -------------------------------------
1373 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1375 if Present (Expression (N)) then
1376 Mark_Coextensions (N, Expression (N));
1379 Analyze_Return_Statement (N);
1380 end Analyze_Simple_Return_Statement;
1382 -------------------------
1383 -- Analyze_Return_Type --
1384 -------------------------
1386 procedure Analyze_Return_Type (N : Node_Id) is
1387 Designator : constant Entity_Id := Defining_Entity (N);
1388 Typ : Entity_Id := Empty;
1391 -- Normal case where result definition does not indicate an error
1393 if Result_Definition (N) /= Error then
1394 if Nkind (Result_Definition (N)) = N_Access_Definition then
1396 Check_Formal_Restriction
1397 ("access result is not allowed", Result_Definition (N));
1399 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1402 AD : constant Node_Id :=
1403 Access_To_Subprogram_Definition (Result_Definition (N));
1405 if Present (AD) and then Protected_Present (AD) then
1406 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1408 Typ := Access_Definition (N, Result_Definition (N));
1412 Set_Parent (Typ, Result_Definition (N));
1413 Set_Is_Local_Anonymous_Access (Typ);
1414 Set_Etype (Designator, Typ);
1416 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1418 Null_Exclusion_Static_Checks (N);
1420 -- Subtype_Mark case
1423 Find_Type (Result_Definition (N));
1424 Typ := Entity (Result_Definition (N));
1425 Set_Etype (Designator, Typ);
1427 -- Unconstrained array as result is not allowed in SPARK or ALFA
1429 if Is_Array_Type (Typ)
1430 and then not Is_Constrained (Typ)
1432 Check_Formal_Restriction
1433 ("returning an unconstrained array is not allowed",
1434 Result_Definition (N));
1437 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1439 Null_Exclusion_Static_Checks (N);
1441 -- If a null exclusion is imposed on the result type, then create
1442 -- a null-excluding itype (an access subtype) and use it as the
1443 -- function's Etype. Note that the null exclusion checks are done
1444 -- right before this, because they don't get applied to types that
1445 -- do not come from source.
1447 if Is_Access_Type (Typ)
1448 and then Null_Exclusion_Present (N)
1450 Set_Etype (Designator,
1451 Create_Null_Excluding_Itype
1454 Scope_Id => Scope (Current_Scope)));
1456 -- The new subtype must be elaborated before use because
1457 -- it is visible outside of the function. However its base
1458 -- type may not be frozen yet, so the reference that will
1459 -- force elaboration must be attached to the freezing of
1462 -- If the return specification appears on a proper body,
1463 -- the subtype will have been created already on the spec.
1465 if Is_Frozen (Typ) then
1466 if Nkind (Parent (N)) = N_Subprogram_Body
1467 and then Nkind (Parent (Parent (N))) = N_Subunit
1471 Build_Itype_Reference (Etype (Designator), Parent (N));
1475 Ensure_Freeze_Node (Typ);
1478 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1480 Set_Itype (IR, Etype (Designator));
1481 Append_Freeze_Actions (Typ, New_List (IR));
1486 Set_Etype (Designator, Typ);
1489 if Ekind (Typ) = E_Incomplete_Type
1490 and then Is_Value_Type (Typ)
1494 elsif Ekind (Typ) = E_Incomplete_Type
1495 or else (Is_Class_Wide_Type (Typ)
1497 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1499 -- AI05-0151: Tagged incomplete types are allowed in all formal
1500 -- parts. Untagged incomplete types are not allowed in bodies.
1502 if Ada_Version >= Ada_2012 then
1503 if Is_Tagged_Type (Typ) then
1506 elsif Nkind_In (Parent (Parent (N)),
1512 ("invalid use of untagged incomplete type&",
1518 ("invalid use of incomplete type&", Designator, Typ);
1523 -- Case where result definition does indicate an error
1526 Set_Etype (Designator, Any_Type);
1528 end Analyze_Return_Type;
1530 -----------------------------
1531 -- Analyze_Subprogram_Body --
1532 -----------------------------
1534 procedure Analyze_Subprogram_Body (N : Node_Id) is
1535 Loc : constant Source_Ptr := Sloc (N);
1536 Body_Spec : constant Node_Id := Specification (N);
1537 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1540 if Debug_Flag_C then
1541 Write_Str ("==> subprogram body ");
1542 Write_Name (Chars (Body_Id));
1543 Write_Str (" from ");
1544 Write_Location (Loc);
1549 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1551 -- The real work is split out into the helper, so it can do "return;"
1552 -- without skipping the debug output:
1554 Analyze_Subprogram_Body_Helper (N);
1556 if Debug_Flag_C then
1558 Write_Str ("<== subprogram body ");
1559 Write_Name (Chars (Body_Id));
1560 Write_Str (" from ");
1561 Write_Location (Loc);
1564 end Analyze_Subprogram_Body;
1566 ------------------------------------
1567 -- Analyze_Subprogram_Body_Helper --
1568 ------------------------------------
1570 -- This procedure is called for regular subprogram bodies, generic bodies,
1571 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1572 -- specification matters, and is used to create a proper declaration for
1573 -- the subprogram, or to perform conformance checks.
1575 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1576 Loc : constant Source_Ptr := Sloc (N);
1577 Body_Deleted : constant Boolean := False;
1578 Body_Spec : constant Node_Id := Specification (N);
1579 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1580 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1581 Conformant : Boolean;
1584 Prot_Typ : Entity_Id := Empty;
1585 Spec_Id : Entity_Id;
1586 Spec_Decl : Node_Id := Empty;
1588 Last_Real_Spec_Entity : Entity_Id := Empty;
1589 -- When we analyze a separate spec, the entity chain ends up containing
1590 -- the formals, as well as any itypes generated during analysis of the
1591 -- default expressions for parameters, or the arguments of associated
1592 -- precondition/postcondition pragmas (which are analyzed in the context
1593 -- of the spec since they have visibility on formals).
1595 -- These entities belong with the spec and not the body. However we do
1596 -- the analysis of the body in the context of the spec (again to obtain
1597 -- visibility to the formals), and all the entities generated during
1598 -- this analysis end up also chained to the entity chain of the spec.
1599 -- But they really belong to the body, and there is circuitry to move
1600 -- them from the spec to the body.
1602 -- However, when we do this move, we don't want to move the real spec
1603 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1604 -- variable points to the last real spec entity, so we only move those
1605 -- chained beyond that point. It is initialized to Empty to deal with
1606 -- the case where there is no separate spec.
1608 procedure Check_Anonymous_Return;
1609 -- Ada 2005: if a function returns an access type that denotes a task,
1610 -- or a type that contains tasks, we must create a master entity for
1611 -- the anonymous type, which typically will be used in an allocator
1612 -- in the body of the function.
1614 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1615 -- Look ahead to recognize a pragma that may appear after the body.
1616 -- If there is a previous spec, check that it appears in the same
1617 -- declarative part. If the pragma is Inline_Always, perform inlining
1618 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1619 -- If the body acts as a spec, and inlining is required, we create a
1620 -- subprogram declaration for it, in order to attach the body to inline.
1621 -- If pragma does not appear after the body, check whether there is
1622 -- an inline pragma before any local declarations.
1624 procedure Check_Missing_Return;
1625 -- Checks for a function with a no return statements, and also performs
1626 -- the warning checks implemented by Check_Returns. In formal mode, also
1627 -- verify that a function ends with a RETURN and that a procedure does
1628 -- not contain any RETURN.
1630 function Disambiguate_Spec return Entity_Id;
1631 -- When a primitive is declared between the private view and the full
1632 -- view of a concurrent type which implements an interface, a special
1633 -- mechanism is used to find the corresponding spec of the primitive
1636 function Is_Private_Concurrent_Primitive
1637 (Subp_Id : Entity_Id) return Boolean;
1638 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1639 -- type that implements an interface and has a private view.
1641 procedure Set_Trivial_Subprogram (N : Node_Id);
1642 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1643 -- subprogram whose body is being analyzed. N is the statement node
1644 -- causing the flag to be set, if the following statement is a return
1645 -- of an entity, we mark the entity as set in source to suppress any
1646 -- warning on the stylized use of function stubs with a dummy return.
1648 procedure Verify_Overriding_Indicator;
1649 -- If there was a previous spec, the entity has been entered in the
1650 -- current scope previously. If the body itself carries an overriding
1651 -- indicator, check that it is consistent with the known status of the
1654 ----------------------------
1655 -- Check_Anonymous_Return --
1656 ----------------------------
1658 procedure Check_Anonymous_Return is
1664 if Present (Spec_Id) then
1670 if Ekind (Scop) = E_Function
1671 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1672 and then not Is_Thunk (Scop)
1673 and then (Has_Task (Designated_Type (Etype (Scop)))
1675 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1677 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1678 and then Expander_Active
1680 -- Avoid cases with no tasking support
1682 and then RTE_Available (RE_Current_Master)
1683 and then not Restriction_Active (No_Task_Hierarchy)
1686 Make_Object_Declaration (Loc,
1687 Defining_Identifier =>
1688 Make_Defining_Identifier (Loc, Name_uMaster),
1689 Constant_Present => True,
1690 Object_Definition =>
1691 New_Reference_To (RTE (RE_Master_Id), Loc),
1693 Make_Explicit_Dereference (Loc,
1694 New_Reference_To (RTE (RE_Current_Master), Loc)));
1696 if Present (Declarations (N)) then
1697 Prepend (Decl, Declarations (N));
1699 Set_Declarations (N, New_List (Decl));
1702 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1703 Set_Has_Master_Entity (Scop);
1705 -- Now mark the containing scope as a task master
1708 while Nkind (Par) /= N_Compilation_Unit loop
1709 Par := Parent (Par);
1710 pragma Assert (Present (Par));
1712 -- If we fall off the top, we are at the outer level, and
1713 -- the environment task is our effective master, so nothing
1717 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1719 Set_Is_Task_Master (Par, True);
1724 end Check_Anonymous_Return;
1726 -------------------------
1727 -- Check_Inline_Pragma --
1728 -------------------------
1730 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1734 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1735 -- True when N is a pragma Inline or Inline_Always that applies
1736 -- to this subprogram.
1738 -----------------------
1739 -- Is_Inline_Pragma --
1740 -----------------------
1742 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1745 Nkind (N) = N_Pragma
1747 (Pragma_Name (N) = Name_Inline_Always
1750 and then Pragma_Name (N) = Name_Inline))
1753 (Expression (First (Pragma_Argument_Associations (N))))
1755 end Is_Inline_Pragma;
1757 -- Start of processing for Check_Inline_Pragma
1760 if not Expander_Active then
1764 if Is_List_Member (N)
1765 and then Present (Next (N))
1766 and then Is_Inline_Pragma (Next (N))
1770 elsif Nkind (N) /= N_Subprogram_Body_Stub
1771 and then Present (Declarations (N))
1772 and then Is_Inline_Pragma (First (Declarations (N)))
1774 Prag := First (Declarations (N));
1780 if Present (Prag) then
1781 if Present (Spec_Id) then
1782 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1787 -- Create a subprogram declaration, to make treatment uniform
1790 Subp : constant Entity_Id :=
1791 Make_Defining_Identifier (Loc, Chars (Body_Id));
1792 Decl : constant Node_Id :=
1793 Make_Subprogram_Declaration (Loc,
1795 New_Copy_Tree (Specification (N)));
1798 Set_Defining_Unit_Name (Specification (Decl), Subp);
1800 if Present (First_Formal (Body_Id)) then
1801 Plist := Copy_Parameter_List (Body_Id);
1802 Set_Parameter_Specifications
1803 (Specification (Decl), Plist);
1806 Insert_Before (N, Decl);
1809 Set_Has_Pragma_Inline (Subp);
1811 if Pragma_Name (Prag) = Name_Inline_Always then
1812 Set_Is_Inlined (Subp);
1813 Set_Has_Pragma_Inline_Always (Subp);
1820 end Check_Inline_Pragma;
1822 --------------------------
1823 -- Check_Missing_Return --
1824 --------------------------
1826 procedure Check_Missing_Return is
1828 Missing_Ret : Boolean;
1831 if Nkind (Body_Spec) = N_Function_Specification then
1832 if Present (Spec_Id) then
1838 if Return_Present (Id) then
1839 Check_Returns (HSS, 'F', Missing_Ret);
1842 Set_Has_Missing_Return (Id);
1845 elsif (Is_Generic_Subprogram (Id)
1846 or else not Is_Machine_Code_Subprogram (Id))
1847 and then not Body_Deleted
1849 Error_Msg_N ("missing RETURN statement in function body", N);
1852 -- If procedure with No_Return, check returns
1854 elsif Nkind (Body_Spec) = N_Procedure_Specification
1855 and then Present (Spec_Id)
1856 and then No_Return (Spec_Id)
1858 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1861 -- Special checks in formal mode
1863 if Nkind (Body_Spec) = N_Function_Specification then
1864 -- In formal mode, the last statement of a function should be a
1865 -- return statement.
1868 Stat : constant Node_Id := Last_Source_Statement (HSS);
1871 and then not Nkind_In (Stat,
1872 N_Simple_Return_Statement,
1873 N_Extended_Return_Statement)
1875 Check_Formal_Restriction
1876 ("last statement in function should be RETURN", Stat);
1880 -- In formal mode, verify that a procedure has no return
1882 elsif Nkind (Body_Spec) = N_Procedure_Specification then
1883 if Present (Spec_Id) then
1889 -- Would be nice to point to return statement here, can we
1890 -- borrow the Check_Returns procedure here ???
1892 if Return_Present (Id) then
1893 Check_Formal_Restriction
1894 ("procedure should not have RETURN", N);
1897 end Check_Missing_Return;
1899 -----------------------
1900 -- Disambiguate_Spec --
1901 -----------------------
1903 function Disambiguate_Spec return Entity_Id is
1904 Priv_Spec : Entity_Id;
1907 procedure Replace_Types (To_Corresponding : Boolean);
1908 -- Depending on the flag, replace the type of formal parameters of
1909 -- Body_Id if it is a concurrent type implementing interfaces with
1910 -- the corresponding record type or the other way around.
1912 procedure Replace_Types (To_Corresponding : Boolean) is
1914 Formal_Typ : Entity_Id;
1917 Formal := First_Formal (Body_Id);
1918 while Present (Formal) loop
1919 Formal_Typ := Etype (Formal);
1921 -- From concurrent type to corresponding record
1923 if To_Corresponding then
1924 if Is_Concurrent_Type (Formal_Typ)
1925 and then Present (Corresponding_Record_Type (Formal_Typ))
1926 and then Present (Interfaces (
1927 Corresponding_Record_Type (Formal_Typ)))
1930 Corresponding_Record_Type (Formal_Typ));
1933 -- From corresponding record to concurrent type
1936 if Is_Concurrent_Record_Type (Formal_Typ)
1937 and then Present (Interfaces (Formal_Typ))
1940 Corresponding_Concurrent_Type (Formal_Typ));
1944 Next_Formal (Formal);
1948 -- Start of processing for Disambiguate_Spec
1951 -- Try to retrieve the specification of the body as is. All error
1952 -- messages are suppressed because the body may not have a spec in
1953 -- its current state.
1955 Spec_N := Find_Corresponding_Spec (N, False);
1957 -- It is possible that this is the body of a primitive declared
1958 -- between a private and a full view of a concurrent type. The
1959 -- controlling parameter of the spec carries the concurrent type,
1960 -- not the corresponding record type as transformed by Analyze_
1961 -- Subprogram_Specification. In such cases, we undo the change
1962 -- made by the analysis of the specification and try to find the
1965 -- Note that wrappers already have their corresponding specs and
1966 -- bodies set during their creation, so if the candidate spec is
1967 -- a wrapper, then we definitely need to swap all types to their
1968 -- original concurrent status.
1971 or else Is_Primitive_Wrapper (Spec_N)
1973 -- Restore all references of corresponding record types to the
1974 -- original concurrent types.
1976 Replace_Types (To_Corresponding => False);
1977 Priv_Spec := Find_Corresponding_Spec (N, False);
1979 -- The current body truly belongs to a primitive declared between
1980 -- a private and a full view. We leave the modified body as is,
1981 -- and return the true spec.
1983 if Present (Priv_Spec)
1984 and then Is_Private_Primitive (Priv_Spec)
1989 -- In case that this is some sort of error, restore the original
1990 -- state of the body.
1992 Replace_Types (To_Corresponding => True);
1996 end Disambiguate_Spec;
1998 -------------------------------------
1999 -- Is_Private_Concurrent_Primitive --
2000 -------------------------------------
2002 function Is_Private_Concurrent_Primitive
2003 (Subp_Id : Entity_Id) return Boolean
2005 Formal_Typ : Entity_Id;
2008 if Present (First_Formal (Subp_Id)) then
2009 Formal_Typ := Etype (First_Formal (Subp_Id));
2011 if Is_Concurrent_Record_Type (Formal_Typ) then
2012 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2015 -- The type of the first formal is a concurrent tagged type with
2019 Is_Concurrent_Type (Formal_Typ)
2020 and then Is_Tagged_Type (Formal_Typ)
2021 and then Has_Private_Declaration (Formal_Typ);
2025 end Is_Private_Concurrent_Primitive;
2027 ----------------------------
2028 -- Set_Trivial_Subprogram --
2029 ----------------------------
2031 procedure Set_Trivial_Subprogram (N : Node_Id) is
2032 Nxt : constant Node_Id := Next (N);
2035 Set_Is_Trivial_Subprogram (Body_Id);
2037 if Present (Spec_Id) then
2038 Set_Is_Trivial_Subprogram (Spec_Id);
2042 and then Nkind (Nxt) = N_Simple_Return_Statement
2043 and then No (Next (Nxt))
2044 and then Present (Expression (Nxt))
2045 and then Is_Entity_Name (Expression (Nxt))
2047 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2049 end Set_Trivial_Subprogram;
2051 ---------------------------------
2052 -- Verify_Overriding_Indicator --
2053 ---------------------------------
2055 procedure Verify_Overriding_Indicator is
2057 if Must_Override (Body_Spec) then
2058 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2059 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2063 elsif not Present (Overridden_Operation (Spec_Id)) then
2065 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2068 elsif Must_Not_Override (Body_Spec) then
2069 if Present (Overridden_Operation (Spec_Id)) then
2071 ("subprogram& overrides inherited operation",
2072 Body_Spec, Spec_Id);
2074 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2075 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2078 ("subprogram & overrides predefined operator ",
2079 Body_Spec, Spec_Id);
2081 -- If this is not a primitive operation or protected subprogram,
2082 -- then the overriding indicator is altogether illegal.
2084 elsif not Is_Primitive (Spec_Id)
2085 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2088 ("overriding indicator only allowed " &
2089 "if subprogram is primitive",
2094 and then Present (Overridden_Operation (Spec_Id))
2096 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2097 Style.Missing_Overriding (N, Body_Id);
2100 and then Can_Override_Operator (Spec_Id)
2101 and then not Is_Predefined_File_Name
2102 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2104 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2105 Style.Missing_Overriding (N, Body_Id);
2107 end Verify_Overriding_Indicator;
2109 -- Start of processing for Analyze_Subprogram_Body_Helper
2112 -- Generic subprograms are handled separately. They always have a
2113 -- generic specification. Determine whether current scope has a
2114 -- previous declaration.
2116 -- If the subprogram body is defined within an instance of the same
2117 -- name, the instance appears as a package renaming, and will be hidden
2118 -- within the subprogram.
2120 if Present (Prev_Id)
2121 and then not Is_Overloadable (Prev_Id)
2122 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2123 or else Comes_From_Source (Prev_Id))
2125 if Is_Generic_Subprogram (Prev_Id) then
2127 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2128 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2130 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2132 if Nkind (N) = N_Subprogram_Body then
2133 HSS := Handled_Statement_Sequence (N);
2134 Check_Missing_Return;
2140 -- Previous entity conflicts with subprogram name. Attempting to
2141 -- enter name will post error.
2143 Enter_Name (Body_Id);
2147 -- Non-generic case, find the subprogram declaration, if one was seen,
2148 -- or enter new overloaded entity in the current scope. If the
2149 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2150 -- part of the context of one of its subunits. No need to redo the
2153 elsif Prev_Id = Body_Id
2154 and then Has_Completion (Body_Id)
2159 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2161 if Nkind (N) = N_Subprogram_Body_Stub
2162 or else No (Corresponding_Spec (N))
2164 if Is_Private_Concurrent_Primitive (Body_Id) then
2165 Spec_Id := Disambiguate_Spec;
2167 Spec_Id := Find_Corresponding_Spec (N);
2170 -- If this is a duplicate body, no point in analyzing it
2172 if Error_Posted (N) then
2176 -- A subprogram body should cause freezing of its own declaration,
2177 -- but if there was no previous explicit declaration, then the
2178 -- subprogram will get frozen too late (there may be code within
2179 -- the body that depends on the subprogram having been frozen,
2180 -- such as uses of extra formals), so we force it to be frozen
2181 -- here. Same holds if the body and spec are compilation units.
2182 -- Finally, if the return type is an anonymous access to protected
2183 -- subprogram, it must be frozen before the body because its
2184 -- expansion has generated an equivalent type that is used when
2185 -- elaborating the body.
2187 if No (Spec_Id) then
2188 Freeze_Before (N, Body_Id);
2190 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2191 Freeze_Before (N, Spec_Id);
2193 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2194 Freeze_Before (N, Etype (Body_Id));
2198 Spec_Id := Corresponding_Spec (N);
2202 -- Do not inline any subprogram that contains nested subprograms, since
2203 -- the backend inlining circuit seems to generate uninitialized
2204 -- references in this case. We know this happens in the case of front
2205 -- end ZCX support, but it also appears it can happen in other cases as
2206 -- well. The backend often rejects attempts to inline in the case of
2207 -- nested procedures anyway, so little if anything is lost by this.
2208 -- Note that this is test is for the benefit of the back-end. There is
2209 -- a separate test for front-end inlining that also rejects nested
2212 -- Do not do this test if errors have been detected, because in some
2213 -- error cases, this code blows up, and we don't need it anyway if
2214 -- there have been errors, since we won't get to the linker anyway.
2216 if Comes_From_Source (Body_Id)
2217 and then Serious_Errors_Detected = 0
2221 P_Ent := Scope (P_Ent);
2222 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2224 if Is_Subprogram (P_Ent) then
2225 Set_Is_Inlined (P_Ent, False);
2227 if Comes_From_Source (P_Ent)
2228 and then Has_Pragma_Inline (P_Ent)
2231 ("cannot inline& (nested subprogram)?",
2238 Check_Inline_Pragma (Spec_Id);
2240 -- Deal with special case of a fully private operation in the body of
2241 -- the protected type. We must create a declaration for the subprogram,
2242 -- in order to attach the protected subprogram that will be used in
2243 -- internal calls. We exclude compiler generated bodies from the
2244 -- expander since the issue does not arise for those cases.
2247 and then Comes_From_Source (N)
2248 and then Is_Protected_Type (Current_Scope)
2250 Spec_Id := Build_Private_Protected_Declaration (N);
2253 -- If a separate spec is present, then deal with freezing issues
2255 if Present (Spec_Id) then
2256 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2257 Verify_Overriding_Indicator;
2259 -- In general, the spec will be frozen when we start analyzing the
2260 -- body. However, for internally generated operations, such as
2261 -- wrapper functions for inherited operations with controlling
2262 -- results, the spec may not have been frozen by the time we
2263 -- expand the freeze actions that include the bodies. In particular,
2264 -- extra formals for accessibility or for return-in-place may need
2265 -- to be generated. Freeze nodes, if any, are inserted before the
2268 if not Is_Frozen (Spec_Id)
2269 and then Expander_Active
2271 -- Force the generation of its freezing node to ensure proper
2272 -- management of access types in the backend.
2274 -- This is definitely needed for some cases, but it is not clear
2275 -- why, to be investigated further???
2277 Set_Has_Delayed_Freeze (Spec_Id);
2278 Freeze_Before (N, Spec_Id);
2282 -- Mark presence of postcondition procedure in current scope and mark
2283 -- the procedure itself as needing debug info. The latter is important
2284 -- when analyzing decision coverage (for example, for MC/DC coverage).
2286 if Chars (Body_Id) = Name_uPostconditions then
2287 Set_Has_Postconditions (Current_Scope);
2288 Set_Debug_Info_Needed (Body_Id);
2291 -- Place subprogram on scope stack, and make formals visible. If there
2292 -- is a spec, the visible entity remains that of the spec.
2294 if Present (Spec_Id) then
2295 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2297 if Is_Child_Unit (Spec_Id) then
2298 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2302 Style.Check_Identifier (Body_Id, Spec_Id);
2305 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2306 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2308 if Is_Abstract_Subprogram (Spec_Id) then
2309 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2313 Set_Convention (Body_Id, Convention (Spec_Id));
2314 Set_Has_Completion (Spec_Id);
2316 if Is_Protected_Type (Scope (Spec_Id)) then
2317 Prot_Typ := Scope (Spec_Id);
2320 -- If this is a body generated for a renaming, do not check for
2321 -- full conformance. The check is redundant, because the spec of
2322 -- the body is a copy of the spec in the renaming declaration,
2323 -- and the test can lead to spurious errors on nested defaults.
2325 if Present (Spec_Decl)
2326 and then not Comes_From_Source (N)
2328 (Nkind (Original_Node (Spec_Decl)) =
2329 N_Subprogram_Renaming_Declaration
2330 or else (Present (Corresponding_Body (Spec_Decl))
2332 Nkind (Unit_Declaration_Node
2333 (Corresponding_Body (Spec_Decl))) =
2334 N_Subprogram_Renaming_Declaration))
2338 -- Conversely, the spec may have been generated for specless body
2339 -- with an inline pragma.
2341 elsif Comes_From_Source (N)
2342 and then not Comes_From_Source (Spec_Id)
2343 and then Has_Pragma_Inline (Spec_Id)
2350 Fully_Conformant, True, Conformant, Body_Id);
2353 -- If the body is not fully conformant, we have to decide if we
2354 -- should analyze it or not. If it has a really messed up profile
2355 -- then we probably should not analyze it, since we will get too
2356 -- many bogus messages.
2358 -- Our decision is to go ahead in the non-fully conformant case
2359 -- only if it is at least mode conformant with the spec. Note
2360 -- that the call to Check_Fully_Conformant has issued the proper
2361 -- error messages to complain about the lack of conformance.
2364 and then not Mode_Conformant (Body_Id, Spec_Id)
2370 if Spec_Id /= Body_Id then
2371 Reference_Body_Formals (Spec_Id, Body_Id);
2374 if Nkind (N) /= N_Subprogram_Body_Stub then
2375 Set_Corresponding_Spec (N, Spec_Id);
2377 -- Ada 2005 (AI-345): If the operation is a primitive operation
2378 -- of a concurrent type, the type of the first parameter has been
2379 -- replaced with the corresponding record, which is the proper
2380 -- run-time structure to use. However, within the body there may
2381 -- be uses of the formals that depend on primitive operations
2382 -- of the type (in particular calls in prefixed form) for which
2383 -- we need the original concurrent type. The operation may have
2384 -- several controlling formals, so the replacement must be done
2387 if Comes_From_Source (Spec_Id)
2388 and then Present (First_Entity (Spec_Id))
2389 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2390 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2392 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2395 (Corresponding_Concurrent_Type
2396 (Etype (First_Entity (Spec_Id))))
2399 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2403 Form := First_Formal (Spec_Id);
2404 while Present (Form) loop
2405 if Etype (Form) = Typ then
2406 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2414 -- Make the formals visible, and place subprogram on scope stack.
2415 -- This is also the point at which we set Last_Real_Spec_Entity
2416 -- to mark the entities which will not be moved to the body.
2418 Install_Formals (Spec_Id);
2419 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2420 Push_Scope (Spec_Id);
2422 -- Make sure that the subprogram is immediately visible. For
2423 -- child units that have no separate spec this is indispensable.
2424 -- Otherwise it is safe albeit redundant.
2426 Set_Is_Immediately_Visible (Spec_Id);
2429 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2430 Set_Ekind (Body_Id, E_Subprogram_Body);
2431 Set_Scope (Body_Id, Scope (Spec_Id));
2432 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2434 -- Case of subprogram body with no previous spec
2437 -- Check for style warning required
2441 -- Only apply check for source level subprograms for which checks
2442 -- have not been suppressed.
2444 and then Comes_From_Source (Body_Id)
2445 and then not Suppress_Style_Checks (Body_Id)
2447 -- No warnings within an instance
2449 and then not In_Instance
2451 -- No warnings for expression functions
2453 and then Nkind (Original_Node (N)) /= N_Expression_Function
2455 Style.Body_With_No_Spec (N);
2458 New_Overloaded_Entity (Body_Id);
2460 if Nkind (N) /= N_Subprogram_Body_Stub then
2461 Set_Acts_As_Spec (N);
2462 Generate_Definition (Body_Id);
2464 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2465 Generate_Reference_To_Formals (Body_Id);
2466 Install_Formals (Body_Id);
2467 Push_Scope (Body_Id);
2471 -- If the return type is an anonymous access type whose designated type
2472 -- is the limited view of a class-wide type and the non-limited view is
2473 -- available, update the return type accordingly.
2475 if Ada_Version >= Ada_2005
2476 and then Comes_From_Source (N)
2483 Rtyp := Etype (Current_Scope);
2485 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2486 Etyp := Directly_Designated_Type (Rtyp);
2488 if Is_Class_Wide_Type (Etyp)
2489 and then From_With_Type (Etyp)
2491 Set_Directly_Designated_Type
2492 (Etype (Current_Scope), Available_View (Etyp));
2498 -- If this is the proper body of a stub, we must verify that the stub
2499 -- conforms to the body, and to the previous spec if one was present.
2500 -- we know already that the body conforms to that spec. This test is
2501 -- only required for subprograms that come from source.
2503 if Nkind (Parent (N)) = N_Subunit
2504 and then Comes_From_Source (N)
2505 and then not Error_Posted (Body_Id)
2506 and then Nkind (Corresponding_Stub (Parent (N))) =
2507 N_Subprogram_Body_Stub
2510 Old_Id : constant Entity_Id :=
2512 (Specification (Corresponding_Stub (Parent (N))));
2514 Conformant : Boolean := False;
2517 if No (Spec_Id) then
2518 Check_Fully_Conformant (Body_Id, Old_Id);
2522 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2524 if not Conformant then
2526 -- The stub was taken to be a new declaration. Indicate
2527 -- that it lacks a body.
2529 Set_Has_Completion (Old_Id, False);
2535 Set_Has_Completion (Body_Id);
2536 Check_Eliminated (Body_Id);
2538 if Nkind (N) = N_Subprogram_Body_Stub then
2541 elsif Present (Spec_Id)
2542 and then Expander_Active
2544 (Has_Pragma_Inline_Always (Spec_Id)
2545 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2547 Build_Body_To_Inline (N, Spec_Id);
2550 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2551 -- if its specification we have to install the private withed units.
2552 -- This holds for child units as well.
2554 if Is_Compilation_Unit (Body_Id)
2555 or else Nkind (Parent (N)) = N_Compilation_Unit
2557 Install_Private_With_Clauses (Body_Id);
2560 Check_Anonymous_Return;
2562 -- Set the Protected_Formal field of each extra formal of the protected
2563 -- subprogram to reference the corresponding extra formal of the
2564 -- subprogram that implements it. For regular formals this occurs when
2565 -- the protected subprogram's declaration is expanded, but the extra
2566 -- formals don't get created until the subprogram is frozen. We need to
2567 -- do this before analyzing the protected subprogram's body so that any
2568 -- references to the original subprogram's extra formals will be changed
2569 -- refer to the implementing subprogram's formals (see Expand_Formal).
2571 if Present (Spec_Id)
2572 and then Is_Protected_Type (Scope (Spec_Id))
2573 and then Present (Protected_Body_Subprogram (Spec_Id))
2576 Impl_Subp : constant Entity_Id :=
2577 Protected_Body_Subprogram (Spec_Id);
2578 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2579 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2581 while Present (Prot_Ext_Formal) loop
2582 pragma Assert (Present (Impl_Ext_Formal));
2583 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2584 Next_Formal_With_Extras (Prot_Ext_Formal);
2585 Next_Formal_With_Extras (Impl_Ext_Formal);
2590 -- Now we can go on to analyze the body
2592 HSS := Handled_Statement_Sequence (N);
2593 Set_Actual_Subtypes (N, Current_Scope);
2595 -- Deal with preconditions and postconditions
2597 Process_PPCs (N, Spec_Id, Body_Id);
2599 -- Add a declaration for the Protection object, renaming declarations
2600 -- for discriminals and privals and finally a declaration for the entry
2601 -- family index (if applicable). This form of early expansion is done
2602 -- when the Expander is active because Install_Private_Data_Declarations
2603 -- references entities which were created during regular expansion.
2606 and then Comes_From_Source (N)
2607 and then Present (Prot_Typ)
2608 and then Present (Spec_Id)
2609 and then not Is_Eliminated (Spec_Id)
2611 Install_Private_Data_Declarations
2612 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2615 -- Analyze the declarations (this call will analyze the precondition
2616 -- Check pragmas we prepended to the list, as well as the declaration
2617 -- of the _Postconditions procedure).
2619 Analyze_Declarations (Declarations (N));
2621 -- Check completion, and analyze the statements
2624 Inspect_Deferred_Constant_Completion (Declarations (N));
2627 -- Deal with end of scope processing for the body
2629 Process_End_Label (HSS, 't', Current_Scope);
2631 Check_Subprogram_Order (N);
2632 Set_Analyzed (Body_Id);
2634 -- If we have a separate spec, then the analysis of the declarations
2635 -- caused the entities in the body to be chained to the spec id, but
2636 -- we want them chained to the body id. Only the formal parameters
2637 -- end up chained to the spec id in this case.
2639 if Present (Spec_Id) then
2641 -- We must conform to the categorization of our spec
2643 Validate_Categorization_Dependency (N, Spec_Id);
2645 -- And if this is a child unit, the parent units must conform
2647 if Is_Child_Unit (Spec_Id) then
2648 Validate_Categorization_Dependency
2649 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2652 -- Here is where we move entities from the spec to the body
2654 -- Case where there are entities that stay with the spec
2656 if Present (Last_Real_Spec_Entity) then
2658 -- No body entities (happens when the only real spec entities
2659 -- come from precondition and postcondition pragmas)
2661 if No (Last_Entity (Body_Id)) then
2663 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2665 -- Body entities present (formals), so chain stuff past them
2669 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2672 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2673 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2674 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2676 -- Case where there are no spec entities, in this case there can
2677 -- be no body entities either, so just move everything.
2680 pragma Assert (No (Last_Entity (Body_Id)));
2681 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2682 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2683 Set_First_Entity (Spec_Id, Empty);
2684 Set_Last_Entity (Spec_Id, Empty);
2688 Check_Missing_Return;
2690 -- Now we are going to check for variables that are never modified in
2691 -- the body of the procedure. But first we deal with a special case
2692 -- where we want to modify this check. If the body of the subprogram
2693 -- starts with a raise statement or its equivalent, or if the body
2694 -- consists entirely of a null statement, then it is pretty obvious
2695 -- that it is OK to not reference the parameters. For example, this
2696 -- might be the following common idiom for a stubbed function:
2697 -- statement of the procedure raises an exception. In particular this
2698 -- deals with the common idiom of a stubbed function, which might
2699 -- appear as something like
2701 -- function F (A : Integer) return Some_Type;
2704 -- raise Program_Error;
2708 -- Here the purpose of X is simply to satisfy the annoying requirement
2709 -- in Ada that there be at least one return, and we certainly do not
2710 -- want to go posting warnings on X that it is not initialized! On
2711 -- the other hand, if X is entirely unreferenced that should still
2714 -- What we do is to detect these cases, and if we find them, flag the
2715 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2716 -- suppress unwanted warnings. For the case of the function stub above
2717 -- we have a special test to set X as apparently assigned to suppress
2724 -- Skip initial labels (for one thing this occurs when we are in
2725 -- front end ZCX mode, but in any case it is irrelevant), and also
2726 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2728 Stm := First (Statements (HSS));
2729 while Nkind (Stm) = N_Label
2730 or else Nkind (Stm) in N_Push_xxx_Label
2735 -- Do the test on the original statement before expansion
2738 Ostm : constant Node_Id := Original_Node (Stm);
2741 -- If explicit raise statement, turn on flag
2743 if Nkind (Ostm) = N_Raise_Statement then
2744 Set_Trivial_Subprogram (Stm);
2746 -- If null statement, and no following statements, turn on flag
2748 elsif Nkind (Stm) = N_Null_Statement
2749 and then Comes_From_Source (Stm)
2750 and then No (Next (Stm))
2752 Set_Trivial_Subprogram (Stm);
2754 -- Check for explicit call cases which likely raise an exception
2756 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2757 if Is_Entity_Name (Name (Ostm)) then
2759 Ent : constant Entity_Id := Entity (Name (Ostm));
2762 -- If the procedure is marked No_Return, then likely it
2763 -- raises an exception, but in any case it is not coming
2764 -- back here, so turn on the flag.
2766 if Ekind (Ent) = E_Procedure
2767 and then No_Return (Ent)
2769 Set_Trivial_Subprogram (Stm);
2777 -- Check for variables that are never modified
2783 -- If there is a separate spec, then transfer Never_Set_In_Source
2784 -- flags from out parameters to the corresponding entities in the
2785 -- body. The reason we do that is we want to post error flags on
2786 -- the body entities, not the spec entities.
2788 if Present (Spec_Id) then
2789 E1 := First_Entity (Spec_Id);
2790 while Present (E1) loop
2791 if Ekind (E1) = E_Out_Parameter then
2792 E2 := First_Entity (Body_Id);
2793 while Present (E2) loop
2794 exit when Chars (E1) = Chars (E2);
2798 if Present (E2) then
2799 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2807 -- Check references in body unless it was deleted. Note that the
2808 -- check of Body_Deleted here is not just for efficiency, it is
2809 -- necessary to avoid junk warnings on formal parameters.
2811 if not Body_Deleted then
2812 Check_References (Body_Id);
2815 end Analyze_Subprogram_Body_Helper;
2817 ------------------------------------
2818 -- Analyze_Subprogram_Declaration --
2819 ------------------------------------
2821 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2822 Loc : constant Source_Ptr := Sloc (N);
2823 Scop : constant Entity_Id := Current_Scope;
2824 Designator : Entity_Id;
2826 Null_Body : Node_Id := Empty;
2828 -- Start of processing for Analyze_Subprogram_Declaration
2831 -- Null procedures are not allowed in SPARK or ALFA
2833 if Nkind (Specification (N)) = N_Procedure_Specification
2834 and then Null_Present (Specification (N))
2836 Check_Formal_Restriction ("null procedure is not allowed", N);
2839 -- For a null procedure, capture the profile before analysis, for
2840 -- expansion at the freeze point and at each point of call. The body
2841 -- will only be used if the procedure has preconditions. In that case
2842 -- the body is analyzed at the freeze point.
2844 if Nkind (Specification (N)) = N_Procedure_Specification
2845 and then Null_Present (Specification (N))
2846 and then Expander_Active
2849 Make_Subprogram_Body (Loc,
2851 New_Copy_Tree (Specification (N)),
2854 Handled_Statement_Sequence =>
2855 Make_Handled_Sequence_Of_Statements (Loc,
2856 Statements => New_List (Make_Null_Statement (Loc))));
2858 -- Create new entities for body and formals
2860 Set_Defining_Unit_Name (Specification (Null_Body),
2861 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2862 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2864 Form := First (Parameter_Specifications (Specification (Null_Body)));
2865 while Present (Form) loop
2866 Set_Defining_Identifier (Form,
2867 Make_Defining_Identifier (Loc,
2868 Chars (Defining_Identifier (Form))));
2870 -- Resolve the types of the formals now, because the freeze point
2871 -- may appear in a different context, e.g. an instantiation.
2873 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
2874 Find_Type (Parameter_Type (Form));
2877 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
2879 Find_Type (Subtype_Mark (Parameter_Type (Form)));
2883 -- the case of a null procedure with a formal that is an
2884 -- access_to_subprogram type, and that is used as an actual
2885 -- in an instantiation is left to the enthusiastic reader.
2893 if Is_Protected_Type (Current_Scope) then
2894 Error_Msg_N ("protected operation cannot be a null procedure", N);
2898 Designator := Analyze_Subprogram_Specification (Specification (N));
2899 Generate_Definition (Designator);
2901 if Debug_Flag_C then
2902 Write_Str ("==> subprogram spec ");
2903 Write_Name (Chars (Designator));
2904 Write_Str (" from ");
2905 Write_Location (Sloc (N));
2910 if Nkind (Specification (N)) = N_Procedure_Specification
2911 and then Null_Present (Specification (N))
2913 Set_Has_Completion (Designator);
2915 if Present (Null_Body) then
2916 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2917 Set_Body_To_Inline (N, Null_Body);
2918 Set_Is_Inlined (Designator);
2922 Validate_RCI_Subprogram_Declaration (N);
2923 New_Overloaded_Entity (Designator);
2924 Check_Delayed_Subprogram (Designator);
2926 -- If the type of the first formal of the current subprogram is a
2927 -- nongeneric tagged private type, mark the subprogram as being a
2928 -- private primitive. Ditto if this is a function with controlling
2929 -- result, and the return type is currently private. In both cases,
2930 -- the type of the controlling argument or result must be in the
2931 -- current scope for the operation to be primitive.
2933 if Has_Controlling_Result (Designator)
2934 and then Is_Private_Type (Etype (Designator))
2935 and then Scope (Etype (Designator)) = Current_Scope
2936 and then not Is_Generic_Actual_Type (Etype (Designator))
2938 Set_Is_Private_Primitive (Designator);
2940 elsif Present (First_Formal (Designator)) then
2942 Formal_Typ : constant Entity_Id :=
2943 Etype (First_Formal (Designator));
2945 Set_Is_Private_Primitive (Designator,
2946 Is_Tagged_Type (Formal_Typ)
2947 and then Scope (Formal_Typ) = Current_Scope
2948 and then Is_Private_Type (Formal_Typ)
2949 and then not Is_Generic_Actual_Type (Formal_Typ));
2953 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2956 if Ada_Version >= Ada_2005
2957 and then Comes_From_Source (N)
2958 and then Is_Dispatching_Operation (Designator)
2965 if Has_Controlling_Result (Designator) then
2966 Etyp := Etype (Designator);
2969 E := First_Entity (Designator);
2971 and then Is_Formal (E)
2972 and then not Is_Controlling_Formal (E)
2980 if Is_Access_Type (Etyp) then
2981 Etyp := Directly_Designated_Type (Etyp);
2984 if Is_Interface (Etyp)
2985 and then not Is_Abstract_Subprogram (Designator)
2986 and then not (Ekind (Designator) = E_Procedure
2987 and then Null_Present (Specification (N)))
2989 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2991 ("(Ada 2005) interface subprogram % must be abstract or null",
2997 -- What is the following code for, it used to be
2999 -- ??? Set_Suppress_Elaboration_Checks
3000 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3002 -- The following seems equivalent, but a bit dubious
3004 if Elaboration_Checks_Suppressed (Designator) then
3005 Set_Kill_Elaboration_Checks (Designator);
3008 if Scop /= Standard_Standard
3009 and then not Is_Child_Unit (Designator)
3011 Set_Categorization_From_Scope (Designator, Scop);
3013 -- For a compilation unit, check for library-unit pragmas
3015 Push_Scope (Designator);
3016 Set_Categorization_From_Pragmas (N);
3017 Validate_Categorization_Dependency (N, Designator);
3021 -- For a compilation unit, set body required. This flag will only be
3022 -- reset if a valid Import or Interface pragma is processed later on.
3024 if Nkind (Parent (N)) = N_Compilation_Unit then
3025 Set_Body_Required (Parent (N), True);
3027 if Ada_Version >= Ada_2005
3028 and then Nkind (Specification (N)) = N_Procedure_Specification
3029 and then Null_Present (Specification (N))
3032 ("null procedure cannot be declared at library level", N);
3036 Generate_Reference_To_Formals (Designator);
3037 Check_Eliminated (Designator);
3039 if Debug_Flag_C then
3041 Write_Str ("<== subprogram spec ");
3042 Write_Name (Chars (Designator));
3043 Write_Str (" from ");
3044 Write_Location (Sloc (N));
3048 if Is_Protected_Type (Current_Scope) then
3050 -- Indicate that this is a protected operation, because it may be
3051 -- used in subsequent declarations within the protected type.
3053 Set_Convention (Designator, Convention_Protected);
3056 List_Inherited_Pre_Post_Aspects (Designator);
3058 if Has_Aspects (N) then
3059 Analyze_Aspect_Specifications (N, Designator);
3061 end Analyze_Subprogram_Declaration;
3063 --------------------------------------
3064 -- Analyze_Subprogram_Specification --
3065 --------------------------------------
3067 -- Reminder: N here really is a subprogram specification (not a subprogram
3068 -- declaration). This procedure is called to analyze the specification in
3069 -- both subprogram bodies and subprogram declarations (specs).
3071 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3072 Designator : constant Entity_Id := Defining_Entity (N);
3073 Formals : constant List_Id := Parameter_Specifications (N);
3075 -- Start of processing for Analyze_Subprogram_Specification
3078 -- User-defined operator is not allowed in SPARK or ALFA
3080 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol then
3081 Check_Formal_Restriction ("user-defined operator is not allowed", N);
3084 -- Proceed with analysis
3086 Generate_Definition (Designator);
3088 if Nkind (N) = N_Function_Specification then
3089 Set_Ekind (Designator, E_Function);
3090 Set_Mechanism (Designator, Default_Mechanism);
3092 Set_Ekind (Designator, E_Procedure);
3093 Set_Etype (Designator, Standard_Void_Type);
3096 -- Introduce new scope for analysis of the formals and the return type
3098 Set_Scope (Designator, Current_Scope);
3100 if Present (Formals) then
3101 Push_Scope (Designator);
3102 Process_Formals (Formals, N);
3104 -- Ada 2005 (AI-345): If this is an overriding operation of an
3105 -- inherited interface operation, and the controlling type is
3106 -- a synchronized type, replace the type with its corresponding
3107 -- record, to match the proper signature of an overriding operation.
3108 -- Same processing for an access parameter whose designated type is
3109 -- derived from a synchronized interface.
3111 if Ada_Version >= Ada_2005 then
3114 Formal_Typ : Entity_Id;
3115 Rec_Typ : Entity_Id;
3116 Desig_Typ : Entity_Id;
3119 Formal := First_Formal (Designator);
3120 while Present (Formal) loop
3121 Formal_Typ := Etype (Formal);
3123 if Is_Concurrent_Type (Formal_Typ)
3124 and then Present (Corresponding_Record_Type (Formal_Typ))
3126 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3128 if Present (Interfaces (Rec_Typ)) then
3129 Set_Etype (Formal, Rec_Typ);
3132 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3133 Desig_Typ := Designated_Type (Formal_Typ);
3135 if Is_Concurrent_Type (Desig_Typ)
3136 and then Present (Corresponding_Record_Type (Desig_Typ))
3138 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3140 if Present (Interfaces (Rec_Typ)) then
3141 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3146 Next_Formal (Formal);
3153 -- The subprogram scope is pushed and popped around the processing of
3154 -- the return type for consistency with call above to Process_Formals
3155 -- (which itself can call Analyze_Return_Type), and to ensure that any
3156 -- itype created for the return type will be associated with the proper
3159 elsif Nkind (N) = N_Function_Specification then
3160 Push_Scope (Designator);
3161 Analyze_Return_Type (N);
3167 if Nkind (N) = N_Function_Specification then
3169 -- Deal with operator symbol case
3171 if Nkind (Designator) = N_Defining_Operator_Symbol then
3172 Valid_Operator_Definition (Designator);
3175 May_Need_Actuals (Designator);
3177 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3178 -- the subprogram is abstract also. This does not apply to renaming
3179 -- declarations, where abstractness is inherited.
3181 -- In case of primitives associated with abstract interface types
3182 -- the check is applied later (see Analyze_Subprogram_Declaration).
3184 if not Nkind_In (Parent (N), N_Subprogram_Renaming_Declaration,
3185 N_Abstract_Subprogram_Declaration,
3186 N_Formal_Abstract_Subprogram_Declaration)
3188 if Is_Abstract_Type (Etype (Designator))
3189 and then not Is_Interface (Etype (Designator))
3192 ("function that returns abstract type must be abstract", N);
3194 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3195 -- access result whose designated type is abstract.
3197 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3199 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3200 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3201 and then Ada_Version >= Ada_2012
3203 Error_Msg_N ("function whose access result designates "
3204 & "abstract type must be abstract", N);
3210 end Analyze_Subprogram_Specification;
3212 --------------------------
3213 -- Build_Body_To_Inline --
3214 --------------------------
3216 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3217 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3218 Original_Body : Node_Id;
3219 Body_To_Analyze : Node_Id;
3220 Max_Size : constant := 10;
3221 Stat_Count : Integer := 0;
3223 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3224 -- Check for declarations that make inlining not worthwhile
3226 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3227 -- Check for statements that make inlining not worthwhile: any tasking
3228 -- statement, nested at any level. Keep track of total number of
3229 -- elementary statements, as a measure of acceptable size.
3231 function Has_Pending_Instantiation return Boolean;
3232 -- If some enclosing body contains instantiations that appear before the
3233 -- corresponding generic body, the enclosing body has a freeze node so
3234 -- that it can be elaborated after the generic itself. This might
3235 -- conflict with subsequent inlinings, so that it is unsafe to try to
3236 -- inline in such a case.
3238 function Has_Single_Return return Boolean;
3239 -- In general we cannot inline functions that return unconstrained type.
3240 -- However, we can handle such functions if all return statements return
3241 -- a local variable that is the only declaration in the body of the
3242 -- function. In that case the call can be replaced by that local
3243 -- variable as is done for other inlined calls.
3245 procedure Remove_Pragmas;
3246 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3247 -- parameter has no meaning when the body is inlined and the formals
3248 -- are rewritten. Remove it from body to inline. The analysis of the
3249 -- non-inlined body will handle the pragma properly.
3251 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3252 -- If the body of the subprogram includes a call that returns an
3253 -- unconstrained type, the secondary stack is involved, and it
3254 -- is not worth inlining.
3256 ------------------------------
3257 -- Has_Excluded_Declaration --
3258 ------------------------------
3260 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3263 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3264 -- Nested subprograms make a given body ineligible for inlining, but
3265 -- we make an exception for instantiations of unchecked conversion.
3266 -- The body has not been analyzed yet, so check the name, and verify
3267 -- that the visible entity with that name is the predefined unit.
3269 -----------------------------
3270 -- Is_Unchecked_Conversion --
3271 -----------------------------
3273 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3274 Id : constant Node_Id := Name (D);
3278 if Nkind (Id) = N_Identifier
3279 and then Chars (Id) = Name_Unchecked_Conversion
3281 Conv := Current_Entity (Id);
3283 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3284 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3286 Conv := Current_Entity (Selector_Name (Id));
3291 return Present (Conv)
3292 and then Is_Predefined_File_Name
3293 (Unit_File_Name (Get_Source_Unit (Conv)))
3294 and then Is_Intrinsic_Subprogram (Conv);
3295 end Is_Unchecked_Conversion;
3297 -- Start of processing for Has_Excluded_Declaration
3301 while Present (D) loop
3302 if (Nkind (D) = N_Function_Instantiation
3303 and then not Is_Unchecked_Conversion (D))
3304 or else Nkind_In (D, N_Protected_Type_Declaration,
3305 N_Package_Declaration,
3306 N_Package_Instantiation,
3308 N_Procedure_Instantiation,
3309 N_Task_Type_Declaration)
3312 ("cannot inline & (non-allowed declaration)?", D, Subp);
3320 end Has_Excluded_Declaration;
3322 ----------------------------
3323 -- Has_Excluded_Statement --
3324 ----------------------------
3326 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3332 while Present (S) loop
3333 Stat_Count := Stat_Count + 1;
3335 if Nkind_In (S, N_Abort_Statement,
3336 N_Asynchronous_Select,
3337 N_Conditional_Entry_Call,
3338 N_Delay_Relative_Statement,
3339 N_Delay_Until_Statement,
3344 ("cannot inline & (non-allowed statement)?", S, Subp);
3347 elsif Nkind (S) = N_Block_Statement then
3348 if Present (Declarations (S))
3349 and then Has_Excluded_Declaration (Declarations (S))
3353 elsif Present (Handled_Statement_Sequence (S))
3356 (Exception_Handlers (Handled_Statement_Sequence (S)))
3358 Has_Excluded_Statement
3359 (Statements (Handled_Statement_Sequence (S))))
3364 elsif Nkind (S) = N_Case_Statement then
3365 E := First (Alternatives (S));
3366 while Present (E) loop
3367 if Has_Excluded_Statement (Statements (E)) then
3374 elsif Nkind (S) = N_If_Statement then
3375 if Has_Excluded_Statement (Then_Statements (S)) then
3379 if Present (Elsif_Parts (S)) then
3380 E := First (Elsif_Parts (S));
3381 while Present (E) loop
3382 if Has_Excluded_Statement (Then_Statements (E)) then
3389 if Present (Else_Statements (S))
3390 and then Has_Excluded_Statement (Else_Statements (S))
3395 elsif Nkind (S) = N_Loop_Statement
3396 and then Has_Excluded_Statement (Statements (S))
3400 elsif Nkind (S) = N_Extended_Return_Statement then
3401 if Has_Excluded_Statement
3402 (Statements (Handled_Statement_Sequence (S)))
3404 (Exception_Handlers (Handled_Statement_Sequence (S)))
3414 end Has_Excluded_Statement;
3416 -------------------------------
3417 -- Has_Pending_Instantiation --
3418 -------------------------------
3420 function Has_Pending_Instantiation return Boolean is
3425 while Present (S) loop
3426 if Is_Compilation_Unit (S)
3427 or else Is_Child_Unit (S)
3431 elsif Ekind (S) = E_Package
3432 and then Has_Forward_Instantiation (S)
3441 end Has_Pending_Instantiation;
3443 ------------------------
3444 -- Has_Single_Return --
3445 ------------------------
3447 function Has_Single_Return return Boolean is
3448 Return_Statement : Node_Id := Empty;
3450 function Check_Return (N : Node_Id) return Traverse_Result;
3456 function Check_Return (N : Node_Id) return Traverse_Result is
3458 if Nkind (N) = N_Simple_Return_Statement then
3459 if Present (Expression (N))
3460 and then Is_Entity_Name (Expression (N))
3462 if No (Return_Statement) then
3463 Return_Statement := N;
3466 elsif Chars (Expression (N)) =
3467 Chars (Expression (Return_Statement))
3475 -- A return statement within an extended return is a noop
3478 elsif No (Expression (N))
3479 and then Nkind (Parent (Parent (N))) =
3480 N_Extended_Return_Statement
3485 -- Expression has wrong form
3490 -- We can only inline a build-in-place function if
3491 -- it has a single extended return.
3493 elsif Nkind (N) = N_Extended_Return_Statement then
3494 if No (Return_Statement) then
3495 Return_Statement := N;
3507 function Check_All_Returns is new Traverse_Func (Check_Return);
3509 -- Start of processing for Has_Single_Return
3512 if Check_All_Returns (N) /= OK then
3515 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3519 return Present (Declarations (N))
3520 and then Present (First (Declarations (N)))
3521 and then Chars (Expression (Return_Statement)) =
3522 Chars (Defining_Identifier (First (Declarations (N))));
3524 end Has_Single_Return;
3526 --------------------
3527 -- Remove_Pragmas --
3528 --------------------
3530 procedure Remove_Pragmas is
3535 Decl := First (Declarations (Body_To_Analyze));
3536 while Present (Decl) loop
3539 if Nkind (Decl) = N_Pragma
3540 and then (Pragma_Name (Decl) = Name_Unreferenced
3542 Pragma_Name (Decl) = Name_Unmodified)
3551 --------------------------
3552 -- Uses_Secondary_Stack --
3553 --------------------------
3555 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3556 function Check_Call (N : Node_Id) return Traverse_Result;
3557 -- Look for function calls that return an unconstrained type
3563 function Check_Call (N : Node_Id) return Traverse_Result is
3565 if Nkind (N) = N_Function_Call
3566 and then Is_Entity_Name (Name (N))
3567 and then Is_Composite_Type (Etype (Entity (Name (N))))
3568 and then not Is_Constrained (Etype (Entity (Name (N))))
3571 ("cannot inline & (call returns unconstrained type)?",
3579 function Check_Calls is new Traverse_Func (Check_Call);
3582 return Check_Calls (Bod) = Abandon;
3583 end Uses_Secondary_Stack;
3585 -- Start of processing for Build_Body_To_Inline
3588 -- Return immediately if done already
3590 if Nkind (Decl) = N_Subprogram_Declaration
3591 and then Present (Body_To_Inline (Decl))
3595 -- Functions that return unconstrained composite types require
3596 -- secondary stack handling, and cannot currently be inlined, unless
3597 -- all return statements return a local variable that is the first
3598 -- local declaration in the body.
3600 elsif Ekind (Subp) = E_Function
3601 and then not Is_Scalar_Type (Etype (Subp))
3602 and then not Is_Access_Type (Etype (Subp))
3603 and then not Is_Constrained (Etype (Subp))
3605 if not Has_Single_Return then
3607 ("cannot inline & (unconstrained return type)?", N, Subp);
3611 -- Ditto for functions that return controlled types, where controlled
3612 -- actions interfere in complex ways with inlining.
3614 elsif Ekind (Subp) = E_Function
3615 and then Needs_Finalization (Etype (Subp))
3618 ("cannot inline & (controlled return type)?", N, Subp);
3622 if Present (Declarations (N))
3623 and then Has_Excluded_Declaration (Declarations (N))
3628 if Present (Handled_Statement_Sequence (N)) then
3629 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3631 ("cannot inline& (exception handler)?",
3632 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3636 Has_Excluded_Statement
3637 (Statements (Handled_Statement_Sequence (N)))
3643 -- We do not inline a subprogram that is too large, unless it is
3644 -- marked Inline_Always. This pragma does not suppress the other
3645 -- checks on inlining (forbidden declarations, handlers, etc).
3647 if Stat_Count > Max_Size
3648 and then not Has_Pragma_Inline_Always (Subp)
3650 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3654 if Has_Pending_Instantiation then
3656 ("cannot inline& (forward instance within enclosing body)?",
3661 -- Within an instance, the body to inline must be treated as a nested
3662 -- generic, so that the proper global references are preserved.
3664 -- Note that we do not do this at the library level, because it is not
3665 -- needed, and furthermore this causes trouble if front end inlining
3666 -- is activated (-gnatN).
3668 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3669 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3670 Original_Body := Copy_Generic_Node (N, Empty, True);
3672 Original_Body := Copy_Separate_Tree (N);
3675 -- We need to capture references to the formals in order to substitute
3676 -- the actuals at the point of inlining, i.e. instantiation. To treat
3677 -- the formals as globals to the body to inline, we nest it within
3678 -- a dummy parameterless subprogram, declared within the real one.
3679 -- To avoid generating an internal name (which is never public, and
3680 -- which affects serial numbers of other generated names), we use
3681 -- an internal symbol that cannot conflict with user declarations.
3683 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3684 Set_Defining_Unit_Name
3685 (Specification (Original_Body),
3686 Make_Defining_Identifier (Sloc (N), Name_uParent));
3687 Set_Corresponding_Spec (Original_Body, Empty);
3689 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3691 -- Set return type of function, which is also global and does not need
3694 if Ekind (Subp) = E_Function then
3695 Set_Result_Definition (Specification (Body_To_Analyze),
3696 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3699 if No (Declarations (N)) then
3700 Set_Declarations (N, New_List (Body_To_Analyze));
3702 Append (Body_To_Analyze, Declarations (N));
3705 Expander_Mode_Save_And_Set (False);
3708 Analyze (Body_To_Analyze);
3709 Push_Scope (Defining_Entity (Body_To_Analyze));
3710 Save_Global_References (Original_Body);
3712 Remove (Body_To_Analyze);
3714 Expander_Mode_Restore;
3716 -- Restore environment if previously saved
3718 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3722 -- If secondary stk used there is no point in inlining. We have
3723 -- already issued the warning in this case, so nothing to do.
3725 if Uses_Secondary_Stack (Body_To_Analyze) then
3729 Set_Body_To_Inline (Decl, Original_Body);
3730 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3731 Set_Is_Inlined (Subp);
3732 end Build_Body_To_Inline;
3738 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3740 -- Do not emit warning if this is a predefined unit which is not the
3741 -- main unit. With validity checks enabled, some predefined subprograms
3742 -- may contain nested subprograms and become ineligible for inlining.
3744 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3745 and then not In_Extended_Main_Source_Unit (Subp)
3749 elsif Has_Pragma_Inline_Always (Subp) then
3751 -- Remove last character (question mark) to make this into an error,
3752 -- because the Inline_Always pragma cannot be obeyed.
3754 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3756 elsif Ineffective_Inline_Warnings then
3757 Error_Msg_NE (Msg, N, Subp);
3761 -----------------------
3762 -- Check_Conformance --
3763 -----------------------
3765 procedure Check_Conformance
3766 (New_Id : Entity_Id;
3768 Ctype : Conformance_Type;
3770 Conforms : out Boolean;
3771 Err_Loc : Node_Id := Empty;
3772 Get_Inst : Boolean := False;
3773 Skip_Controlling_Formals : Boolean := False)
3775 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3776 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3777 -- If Errmsg is True, then processing continues to post an error message
3778 -- for conformance error on given node. Two messages are output. The
3779 -- first message points to the previous declaration with a general "no
3780 -- conformance" message. The second is the detailed reason, supplied as
3781 -- Msg. The parameter N provide information for a possible & insertion
3782 -- in the message, and also provides the location for posting the
3783 -- message in the absence of a specified Err_Loc location.
3785 -----------------------
3786 -- Conformance_Error --
3787 -----------------------
3789 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3796 if No (Err_Loc) then
3802 Error_Msg_Sloc := Sloc (Old_Id);
3805 when Type_Conformant =>
3806 Error_Msg_N -- CODEFIX
3807 ("not type conformant with declaration#!", Enode);
3809 when Mode_Conformant =>
3810 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3812 ("not mode conformant with operation inherited#!",
3816 ("not mode conformant with declaration#!", Enode);
3819 when Subtype_Conformant =>
3820 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3822 ("not subtype conformant with operation inherited#!",
3826 ("not subtype conformant with declaration#!", Enode);
3829 when Fully_Conformant =>
3830 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3831 Error_Msg_N -- CODEFIX
3832 ("not fully conformant with operation inherited#!",
3835 Error_Msg_N -- CODEFIX
3836 ("not fully conformant with declaration#!", Enode);
3840 Error_Msg_NE (Msg, Enode, N);
3842 end Conformance_Error;
3846 Old_Type : constant Entity_Id := Etype (Old_Id);
3847 New_Type : constant Entity_Id := Etype (New_Id);
3848 Old_Formal : Entity_Id;
3849 New_Formal : Entity_Id;
3850 Access_Types_Match : Boolean;
3851 Old_Formal_Base : Entity_Id;
3852 New_Formal_Base : Entity_Id;
3854 -- Start of processing for Check_Conformance
3859 -- We need a special case for operators, since they don't appear
3862 if Ctype = Type_Conformant then
3863 if Ekind (New_Id) = E_Operator
3864 and then Operator_Matches_Spec (New_Id, Old_Id)
3870 -- If both are functions/operators, check return types conform
3872 if Old_Type /= Standard_Void_Type
3873 and then New_Type /= Standard_Void_Type
3876 -- If we are checking interface conformance we omit controlling
3877 -- arguments and result, because we are only checking the conformance
3878 -- of the remaining parameters.
3880 if Has_Controlling_Result (Old_Id)
3881 and then Has_Controlling_Result (New_Id)
3882 and then Skip_Controlling_Formals
3886 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3887 Conformance_Error ("\return type does not match!", New_Id);
3891 -- Ada 2005 (AI-231): In case of anonymous access types check the
3892 -- null-exclusion and access-to-constant attributes match.
3894 if Ada_Version >= Ada_2005
3895 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3897 (Can_Never_Be_Null (Old_Type)
3898 /= Can_Never_Be_Null (New_Type)
3899 or else Is_Access_Constant (Etype (Old_Type))
3900 /= Is_Access_Constant (Etype (New_Type)))
3902 Conformance_Error ("\return type does not match!", New_Id);
3906 -- If either is a function/operator and the other isn't, error
3908 elsif Old_Type /= Standard_Void_Type
3909 or else New_Type /= Standard_Void_Type
3911 Conformance_Error ("\functions can only match functions!", New_Id);
3915 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3916 -- If this is a renaming as body, refine error message to indicate that
3917 -- the conflict is with the original declaration. If the entity is not
3918 -- frozen, the conventions don't have to match, the one of the renamed
3919 -- entity is inherited.
3921 if Ctype >= Subtype_Conformant then
3922 if Convention (Old_Id) /= Convention (New_Id) then
3924 if not Is_Frozen (New_Id) then
3927 elsif Present (Err_Loc)
3928 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3929 and then Present (Corresponding_Spec (Err_Loc))
3931 Error_Msg_Name_1 := Chars (New_Id);
3933 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3934 Conformance_Error ("\prior declaration for% has convention %!");
3937 Conformance_Error ("\calling conventions do not match!");
3942 elsif Is_Formal_Subprogram (Old_Id)
3943 or else Is_Formal_Subprogram (New_Id)
3945 Conformance_Error ("\formal subprograms not allowed!");
3950 -- Deal with parameters
3952 -- Note: we use the entity information, rather than going directly
3953 -- to the specification in the tree. This is not only simpler, but
3954 -- absolutely necessary for some cases of conformance tests between
3955 -- operators, where the declaration tree simply does not exist!
3957 Old_Formal := First_Formal (Old_Id);
3958 New_Formal := First_Formal (New_Id);
3959 while Present (Old_Formal) and then Present (New_Formal) loop
3960 if Is_Controlling_Formal (Old_Formal)
3961 and then Is_Controlling_Formal (New_Formal)
3962 and then Skip_Controlling_Formals
3964 -- The controlling formals will have different types when
3965 -- comparing an interface operation with its match, but both
3966 -- or neither must be access parameters.
3968 if Is_Access_Type (Etype (Old_Formal))
3970 Is_Access_Type (Etype (New_Formal))
3972 goto Skip_Controlling_Formal;
3975 ("\access parameter does not match!", New_Formal);
3979 if Ctype = Fully_Conformant then
3981 -- Names must match. Error message is more accurate if we do
3982 -- this before checking that the types of the formals match.
3984 if Chars (Old_Formal) /= Chars (New_Formal) then
3985 Conformance_Error ("\name & does not match!", New_Formal);
3987 -- Set error posted flag on new formal as well to stop
3988 -- junk cascaded messages in some cases.
3990 Set_Error_Posted (New_Formal);
3994 -- Null exclusion must match
3996 if Null_Exclusion_Present (Parent (Old_Formal))
3998 Null_Exclusion_Present (Parent (New_Formal))
4000 -- Only give error if both come from source. This should be
4001 -- investigated some time, since it should not be needed ???
4003 if Comes_From_Source (Old_Formal)
4005 Comes_From_Source (New_Formal)
4008 ("\null exclusion for & does not match", New_Formal);
4010 -- Mark error posted on the new formal to avoid duplicated
4011 -- complaint about types not matching.
4013 Set_Error_Posted (New_Formal);
4018 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4019 -- case occurs whenever a subprogram is being renamed and one of its
4020 -- parameters imposes a null exclusion. For example:
4022 -- type T is null record;
4023 -- type Acc_T is access T;
4024 -- subtype Acc_T_Sub is Acc_T;
4026 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4027 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4030 Old_Formal_Base := Etype (Old_Formal);
4031 New_Formal_Base := Etype (New_Formal);
4034 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4035 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4038 Access_Types_Match := Ada_Version >= Ada_2005
4040 -- Ensure that this rule is only applied when New_Id is a
4041 -- renaming of Old_Id.
4043 and then Nkind (Parent (Parent (New_Id))) =
4044 N_Subprogram_Renaming_Declaration
4045 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4046 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4047 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4049 -- Now handle the allowed access-type case
4051 and then Is_Access_Type (Old_Formal_Base)
4052 and then Is_Access_Type (New_Formal_Base)
4054 -- The type kinds must match. The only exception occurs with
4055 -- multiple generics of the form:
4058 -- type F is private; type A is private;
4059 -- type F_Ptr is access F; type A_Ptr is access A;
4060 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4061 -- package F_Pack is ... package A_Pack is
4062 -- package F_Inst is
4063 -- new F_Pack (A, A_Ptr, A_P);
4065 -- When checking for conformance between the parameters of A_P
4066 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4067 -- because the compiler has transformed A_Ptr into a subtype of
4068 -- F_Ptr. We catch this case in the code below.
4070 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4072 (Is_Generic_Type (Old_Formal_Base)
4073 and then Is_Generic_Type (New_Formal_Base)
4074 and then Is_Internal (New_Formal_Base)
4075 and then Etype (Etype (New_Formal_Base)) =
4077 and then Directly_Designated_Type (Old_Formal_Base) =
4078 Directly_Designated_Type (New_Formal_Base)
4079 and then ((Is_Itype (Old_Formal_Base)
4080 and then Can_Never_Be_Null (Old_Formal_Base))
4082 (Is_Itype (New_Formal_Base)
4083 and then Can_Never_Be_Null (New_Formal_Base)));
4085 -- Types must always match. In the visible part of an instance,
4086 -- usual overloading rules for dispatching operations apply, and
4087 -- we check base types (not the actual subtypes).
4089 if In_Instance_Visible_Part
4090 and then Is_Dispatching_Operation (New_Id)
4092 if not Conforming_Types
4093 (T1 => Base_Type (Etype (Old_Formal)),
4094 T2 => Base_Type (Etype (New_Formal)),
4096 Get_Inst => Get_Inst)
4097 and then not Access_Types_Match
4099 Conformance_Error ("\type of & does not match!", New_Formal);
4103 elsif not Conforming_Types
4104 (T1 => Old_Formal_Base,
4105 T2 => New_Formal_Base,
4107 Get_Inst => Get_Inst)
4108 and then not Access_Types_Match
4110 -- Don't give error message if old type is Any_Type. This test
4111 -- avoids some cascaded errors, e.g. in case of a bad spec.
4113 if Errmsg and then Old_Formal_Base = Any_Type then
4116 Conformance_Error ("\type of & does not match!", New_Formal);
4122 -- For mode conformance, mode must match
4124 if Ctype >= Mode_Conformant then
4125 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4126 Conformance_Error ("\mode of & does not match!", New_Formal);
4129 -- Part of mode conformance for access types is having the same
4130 -- constant modifier.
4132 elsif Access_Types_Match
4133 and then Is_Access_Constant (Old_Formal_Base) /=
4134 Is_Access_Constant (New_Formal_Base)
4137 ("\constant modifier does not match!", New_Formal);
4142 if Ctype >= Subtype_Conformant then
4144 -- Ada 2005 (AI-231): In case of anonymous access types check
4145 -- the null-exclusion and access-to-constant attributes must
4148 if Ada_Version >= Ada_2005
4149 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4150 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4152 (Can_Never_Be_Null (Old_Formal) /=
4153 Can_Never_Be_Null (New_Formal)
4155 Is_Access_Constant (Etype (Old_Formal)) /=
4156 Is_Access_Constant (Etype (New_Formal)))
4158 -- Do not complain if error already posted on New_Formal. This
4159 -- avoids some redundant error messages.
4161 and then not Error_Posted (New_Formal)
4163 -- It is allowed to omit the null-exclusion in case of stream
4164 -- attribute subprograms. We recognize stream subprograms
4165 -- through their TSS-generated suffix.
4168 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4170 if TSS_Name /= TSS_Stream_Read
4171 and then TSS_Name /= TSS_Stream_Write
4172 and then TSS_Name /= TSS_Stream_Input
4173 and then TSS_Name /= TSS_Stream_Output
4176 ("\type of & does not match!", New_Formal);
4183 -- Full conformance checks
4185 if Ctype = Fully_Conformant then
4187 -- We have checked already that names match
4189 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4191 -- Check default expressions for in parameters
4194 NewD : constant Boolean :=
4195 Present (Default_Value (New_Formal));
4196 OldD : constant Boolean :=
4197 Present (Default_Value (Old_Formal));
4199 if NewD or OldD then
4201 -- The old default value has been analyzed because the
4202 -- current full declaration will have frozen everything
4203 -- before. The new default value has not been analyzed,
4204 -- so analyze it now before we check for conformance.
4207 Push_Scope (New_Id);
4208 Preanalyze_Spec_Expression
4209 (Default_Value (New_Formal), Etype (New_Formal));
4213 if not (NewD and OldD)
4214 or else not Fully_Conformant_Expressions
4215 (Default_Value (Old_Formal),
4216 Default_Value (New_Formal))
4219 ("\default expression for & does not match!",
4228 -- A couple of special checks for Ada 83 mode. These checks are
4229 -- skipped if either entity is an operator in package Standard,
4230 -- or if either old or new instance is not from the source program.
4232 if Ada_Version = Ada_83
4233 and then Sloc (Old_Id) > Standard_Location
4234 and then Sloc (New_Id) > Standard_Location
4235 and then Comes_From_Source (Old_Id)
4236 and then Comes_From_Source (New_Id)
4239 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4240 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4243 -- Explicit IN must be present or absent in both cases. This
4244 -- test is required only in the full conformance case.
4246 if In_Present (Old_Param) /= In_Present (New_Param)
4247 and then Ctype = Fully_Conformant
4250 ("\(Ada 83) IN must appear in both declarations",
4255 -- Grouping (use of comma in param lists) must be the same
4256 -- This is where we catch a misconformance like:
4259 -- A : Integer; B : Integer
4261 -- which are represented identically in the tree except
4262 -- for the setting of the flags More_Ids and Prev_Ids.
4264 if More_Ids (Old_Param) /= More_Ids (New_Param)
4265 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
4268 ("\grouping of & does not match!", New_Formal);
4274 -- This label is required when skipping controlling formals
4276 <<Skip_Controlling_Formal>>
4278 Next_Formal (Old_Formal);
4279 Next_Formal (New_Formal);
4282 if Present (Old_Formal) then
4283 Conformance_Error ("\too few parameters!");
4286 elsif Present (New_Formal) then
4287 Conformance_Error ("\too many parameters!", New_Formal);
4290 end Check_Conformance;
4292 -----------------------
4293 -- Check_Conventions --
4294 -----------------------
4296 procedure Check_Conventions (Typ : Entity_Id) is
4297 Ifaces_List : Elist_Id;
4299 procedure Check_Convention (Op : Entity_Id);
4300 -- Verify that the convention of inherited dispatching operation Op is
4301 -- consistent among all subprograms it overrides. In order to minimize
4302 -- the search, Search_From is utilized to designate a specific point in
4303 -- the list rather than iterating over the whole list once more.
4305 ----------------------
4306 -- Check_Convention --
4307 ----------------------
4309 procedure Check_Convention (Op : Entity_Id) is
4310 Iface_Elmt : Elmt_Id;
4311 Iface_Prim_Elmt : Elmt_Id;
4312 Iface_Prim : Entity_Id;
4315 Iface_Elmt := First_Elmt (Ifaces_List);
4316 while Present (Iface_Elmt) loop
4318 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4319 while Present (Iface_Prim_Elmt) loop
4320 Iface_Prim := Node (Iface_Prim_Elmt);
4322 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4323 and then Convention (Iface_Prim) /= Convention (Op)
4326 ("inconsistent conventions in primitive operations", Typ);
4328 Error_Msg_Name_1 := Chars (Op);
4329 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4330 Error_Msg_Sloc := Sloc (Op);
4332 if Comes_From_Source (Op) or else No (Alias (Op)) then
4333 if not Present (Overridden_Operation (Op)) then
4334 Error_Msg_N ("\\primitive % defined #", Typ);
4337 ("\\overriding operation % with " &
4338 "convention % defined #", Typ);
4341 else pragma Assert (Present (Alias (Op)));
4342 Error_Msg_Sloc := Sloc (Alias (Op));
4344 ("\\inherited operation % with " &
4345 "convention % defined #", Typ);
4348 Error_Msg_Name_1 := Chars (Op);
4350 Get_Convention_Name (Convention (Iface_Prim));
4351 Error_Msg_Sloc := Sloc (Iface_Prim);
4353 ("\\overridden operation % with " &
4354 "convention % defined #", Typ);
4356 -- Avoid cascading errors
4361 Next_Elmt (Iface_Prim_Elmt);
4364 Next_Elmt (Iface_Elmt);
4366 end Check_Convention;
4370 Prim_Op : Entity_Id;
4371 Prim_Op_Elmt : Elmt_Id;
4373 -- Start of processing for Check_Conventions
4376 if not Has_Interfaces (Typ) then
4380 Collect_Interfaces (Typ, Ifaces_List);
4382 -- The algorithm checks every overriding dispatching operation against
4383 -- all the corresponding overridden dispatching operations, detecting
4384 -- differences in conventions.
4386 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4387 while Present (Prim_Op_Elmt) loop
4388 Prim_Op := Node (Prim_Op_Elmt);
4390 -- A small optimization: skip the predefined dispatching operations
4391 -- since they always have the same convention.
4393 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4394 Check_Convention (Prim_Op);
4397 Next_Elmt (Prim_Op_Elmt);
4399 end Check_Conventions;
4401 ------------------------------
4402 -- Check_Delayed_Subprogram --
4403 ------------------------------
4405 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4408 procedure Possible_Freeze (T : Entity_Id);
4409 -- T is the type of either a formal parameter or of the return type.
4410 -- If T is not yet frozen and needs a delayed freeze, then the
4411 -- subprogram itself must be delayed. If T is the limited view of an
4412 -- incomplete type the subprogram must be frozen as well, because
4413 -- T may depend on local types that have not been frozen yet.
4415 ---------------------
4416 -- Possible_Freeze --
4417 ---------------------
4419 procedure Possible_Freeze (T : Entity_Id) is
4421 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4422 Set_Has_Delayed_Freeze (Designator);
4424 elsif Is_Access_Type (T)
4425 and then Has_Delayed_Freeze (Designated_Type (T))
4426 and then not Is_Frozen (Designated_Type (T))
4428 Set_Has_Delayed_Freeze (Designator);
4430 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4431 Set_Has_Delayed_Freeze (Designator);
4434 end Possible_Freeze;
4436 -- Start of processing for Check_Delayed_Subprogram
4439 -- All subprograms, including abstract subprograms, may need a freeze
4440 -- node if some formal type or the return type needs one.
4442 Possible_Freeze (Etype (Designator));
4443 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4445 -- Need delayed freeze if any of the formal types themselves need
4446 -- a delayed freeze and are not yet frozen.
4448 F := First_Formal (Designator);
4449 while Present (F) loop
4450 Possible_Freeze (Etype (F));
4451 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4455 -- Mark functions that return by reference. Note that it cannot be
4456 -- done for delayed_freeze subprograms because the underlying
4457 -- returned type may not be known yet (for private types)
4459 if not Has_Delayed_Freeze (Designator)
4460 and then Expander_Active
4463 Typ : constant Entity_Id := Etype (Designator);
4464 Utyp : constant Entity_Id := Underlying_Type (Typ);
4467 if Is_Immutably_Limited_Type (Typ) then
4468 Set_Returns_By_Ref (Designator);
4470 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4471 Set_Returns_By_Ref (Designator);
4475 end Check_Delayed_Subprogram;
4477 ------------------------------------
4478 -- Check_Discriminant_Conformance --
4479 ------------------------------------
4481 procedure Check_Discriminant_Conformance
4486 Old_Discr : Entity_Id := First_Discriminant (Prev);
4487 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4488 New_Discr_Id : Entity_Id;
4489 New_Discr_Type : Entity_Id;
4491 procedure Conformance_Error (Msg : String; N : Node_Id);
4492 -- Post error message for conformance error on given node. Two messages
4493 -- are output. The first points to the previous declaration with a
4494 -- general "no conformance" message. The second is the detailed reason,
4495 -- supplied as Msg. The parameter N provide information for a possible
4496 -- & insertion in the message.
4498 -----------------------
4499 -- Conformance_Error --
4500 -----------------------
4502 procedure Conformance_Error (Msg : String; N : Node_Id) is
4504 Error_Msg_Sloc := Sloc (Prev_Loc);
4505 Error_Msg_N -- CODEFIX
4506 ("not fully conformant with declaration#!", N);
4507 Error_Msg_NE (Msg, N, N);
4508 end Conformance_Error;
4510 -- Start of processing for Check_Discriminant_Conformance
4513 while Present (Old_Discr) and then Present (New_Discr) loop
4515 New_Discr_Id := Defining_Identifier (New_Discr);
4517 -- The subtype mark of the discriminant on the full type has not
4518 -- been analyzed so we do it here. For an access discriminant a new
4521 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4523 Access_Definition (N, Discriminant_Type (New_Discr));
4526 Analyze (Discriminant_Type (New_Discr));
4527 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4529 -- Ada 2005: if the discriminant definition carries a null
4530 -- exclusion, create an itype to check properly for consistency
4531 -- with partial declaration.
4533 if Is_Access_Type (New_Discr_Type)
4534 and then Null_Exclusion_Present (New_Discr)
4537 Create_Null_Excluding_Itype
4538 (T => New_Discr_Type,
4539 Related_Nod => New_Discr,
4540 Scope_Id => Current_Scope);
4544 if not Conforming_Types
4545 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4547 Conformance_Error ("type of & does not match!", New_Discr_Id);
4550 -- Treat the new discriminant as an occurrence of the old one,
4551 -- for navigation purposes, and fill in some semantic
4552 -- information, for completeness.
4554 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4555 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4556 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4561 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4562 Conformance_Error ("name & does not match!", New_Discr_Id);
4566 -- Default expressions must match
4569 NewD : constant Boolean :=
4570 Present (Expression (New_Discr));
4571 OldD : constant Boolean :=
4572 Present (Expression (Parent (Old_Discr)));
4575 if NewD or OldD then
4577 -- The old default value has been analyzed and expanded,
4578 -- because the current full declaration will have frozen
4579 -- everything before. The new default values have not been
4580 -- expanded, so expand now to check conformance.
4583 Preanalyze_Spec_Expression
4584 (Expression (New_Discr), New_Discr_Type);
4587 if not (NewD and OldD)
4588 or else not Fully_Conformant_Expressions
4589 (Expression (Parent (Old_Discr)),
4590 Expression (New_Discr))
4594 ("default expression for & does not match!",
4601 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4603 if Ada_Version = Ada_83 then
4605 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4608 -- Grouping (use of comma in param lists) must be the same
4609 -- This is where we catch a misconformance like:
4612 -- A : Integer; B : Integer
4614 -- which are represented identically in the tree except
4615 -- for the setting of the flags More_Ids and Prev_Ids.
4617 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4618 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4621 ("grouping of & does not match!", New_Discr_Id);
4627 Next_Discriminant (Old_Discr);
4631 if Present (Old_Discr) then
4632 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4635 elsif Present (New_Discr) then
4637 ("too many discriminants!", Defining_Identifier (New_Discr));
4640 end Check_Discriminant_Conformance;
4642 ----------------------------
4643 -- Check_Fully_Conformant --
4644 ----------------------------
4646 procedure Check_Fully_Conformant
4647 (New_Id : Entity_Id;
4649 Err_Loc : Node_Id := Empty)
4652 pragma Warnings (Off, Result);
4655 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4656 end Check_Fully_Conformant;
4658 ---------------------------
4659 -- Check_Mode_Conformant --
4660 ---------------------------
4662 procedure Check_Mode_Conformant
4663 (New_Id : Entity_Id;
4665 Err_Loc : Node_Id := Empty;
4666 Get_Inst : Boolean := False)
4669 pragma Warnings (Off, Result);
4672 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4673 end Check_Mode_Conformant;
4675 --------------------------------
4676 -- Check_Overriding_Indicator --
4677 --------------------------------
4679 procedure Check_Overriding_Indicator
4681 Overridden_Subp : Entity_Id;
4682 Is_Primitive : Boolean)
4688 -- No overriding indicator for literals
4690 if Ekind (Subp) = E_Enumeration_Literal then
4693 elsif Ekind (Subp) = E_Entry then
4694 Decl := Parent (Subp);
4696 -- No point in analyzing a malformed operator
4698 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4699 and then Error_Posted (Subp)
4704 Decl := Unit_Declaration_Node (Subp);
4707 if Nkind_In (Decl, N_Subprogram_Body,
4708 N_Subprogram_Body_Stub,
4709 N_Subprogram_Declaration,
4710 N_Abstract_Subprogram_Declaration,
4711 N_Subprogram_Renaming_Declaration)
4713 Spec := Specification (Decl);
4715 elsif Nkind (Decl) = N_Entry_Declaration then
4722 -- The overriding operation is type conformant with the overridden one,
4723 -- but the names of the formals are not required to match. If the names
4724 -- appear permuted in the overriding operation, this is a possible
4725 -- source of confusion that is worth diagnosing. Controlling formals
4726 -- often carry names that reflect the type, and it is not worthwhile
4727 -- requiring that their names match.
4729 if Present (Overridden_Subp)
4730 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4737 Form1 := First_Formal (Subp);
4738 Form2 := First_Formal (Overridden_Subp);
4740 -- If the overriding operation is a synchronized operation, skip
4741 -- the first parameter of the overridden operation, which is
4742 -- implicit in the new one. If the operation is declared in the
4743 -- body it is not primitive and all formals must match.
4745 if Is_Concurrent_Type (Scope (Subp))
4746 and then Is_Tagged_Type (Scope (Subp))
4747 and then not Has_Completion (Scope (Subp))
4749 Form2 := Next_Formal (Form2);
4752 if Present (Form1) then
4753 Form1 := Next_Formal (Form1);
4754 Form2 := Next_Formal (Form2);
4757 while Present (Form1) loop
4758 if not Is_Controlling_Formal (Form1)
4759 and then Present (Next_Formal (Form2))
4760 and then Chars (Form1) = Chars (Next_Formal (Form2))
4762 Error_Msg_Node_2 := Alias (Overridden_Subp);
4763 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4765 ("& does not match corresponding formal of&#",
4770 Next_Formal (Form1);
4771 Next_Formal (Form2);
4776 -- If there is an overridden subprogram, then check that there is no
4777 -- "not overriding" indicator, and mark the subprogram as overriding.
4778 -- This is not done if the overridden subprogram is marked as hidden,
4779 -- which can occur for the case of inherited controlled operations
4780 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4781 -- subprogram is not itself hidden. (Note: This condition could probably
4782 -- be simplified, leaving out the testing for the specific controlled
4783 -- cases, but it seems safer and clearer this way, and echoes similar
4784 -- special-case tests of this kind in other places.)
4786 if Present (Overridden_Subp)
4787 and then (not Is_Hidden (Overridden_Subp)
4789 ((Chars (Overridden_Subp) = Name_Initialize
4791 Chars (Overridden_Subp) = Name_Adjust
4793 Chars (Overridden_Subp) = Name_Finalize)
4794 and then Present (Alias (Overridden_Subp))
4795 and then not Is_Hidden (Alias (Overridden_Subp))))
4797 if Must_Not_Override (Spec) then
4798 Error_Msg_Sloc := Sloc (Overridden_Subp);
4800 if Ekind (Subp) = E_Entry then
4802 ("entry & overrides inherited operation #", Spec, Subp);
4805 ("subprogram & overrides inherited operation #", Spec, Subp);
4808 elsif Is_Subprogram (Subp) then
4809 if No (Overridden_Operation (Subp)) then
4811 -- For entities generated by Derive_Subprograms the overridden
4812 -- operation is the inherited primitive (which is available
4813 -- through the attribute alias)
4815 if (Is_Dispatching_Operation (Subp)
4816 or else Is_Dispatching_Operation (Overridden_Subp))
4817 and then not Comes_From_Source (Overridden_Subp)
4818 and then Find_Dispatching_Type (Overridden_Subp) =
4819 Find_Dispatching_Type (Subp)
4820 and then Present (Alias (Overridden_Subp))
4821 and then Comes_From_Source (Alias (Overridden_Subp))
4823 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
4825 Set_Overridden_Operation (Subp, Overridden_Subp);
4830 -- If primitive flag is set or this is a protected operation, then
4831 -- the operation is overriding at the point of its declaration, so
4832 -- warn if necessary. Otherwise it may have been declared before the
4833 -- operation it overrides and no check is required.
4836 and then not Must_Override (Spec)
4837 and then (Is_Primitive
4838 or else Ekind (Scope (Subp)) = E_Protected_Type)
4840 Style.Missing_Overriding (Decl, Subp);
4843 -- If Subp is an operator, it may override a predefined operation, if
4844 -- it is defined in the same scope as the type to which it applies.
4845 -- In that case Overridden_Subp is empty because of our implicit
4846 -- representation for predefined operators. We have to check whether the
4847 -- signature of Subp matches that of a predefined operator. Note that
4848 -- first argument provides the name of the operator, and the second
4849 -- argument the signature that may match that of a standard operation.
4850 -- If the indicator is overriding, then the operator must match a
4851 -- predefined signature, because we know already that there is no
4852 -- explicit overridden operation.
4854 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4855 if Must_Not_Override (Spec) then
4857 -- If this is not a primitive or a protected subprogram, then
4858 -- "not overriding" is illegal.
4861 and then Ekind (Scope (Subp)) /= E_Protected_Type
4864 ("overriding indicator only allowed "
4865 & "if subprogram is primitive", Subp);
4867 elsif Can_Override_Operator (Subp) then
4869 ("subprogram& overrides predefined operator ", Spec, Subp);
4872 elsif Must_Override (Spec) then
4873 if No (Overridden_Operation (Subp))
4874 and then not Can_Override_Operator (Subp)
4876 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4879 elsif not Error_Posted (Subp)
4880 and then Style_Check
4881 and then Can_Override_Operator (Subp)
4883 not Is_Predefined_File_Name
4884 (Unit_File_Name (Get_Source_Unit (Subp)))
4886 -- If style checks are enabled, indicate that the indicator is
4887 -- missing. However, at the point of declaration, the type of
4888 -- which this is a primitive operation may be private, in which
4889 -- case the indicator would be premature.
4891 if Has_Private_Declaration (Etype (Subp))
4892 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4896 Style.Missing_Overriding (Decl, Subp);
4900 elsif Must_Override (Spec) then
4901 if Ekind (Subp) = E_Entry then
4902 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4904 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4907 -- If the operation is marked "not overriding" and it's not primitive
4908 -- then an error is issued, unless this is an operation of a task or
4909 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4910 -- has been specified have already been checked above.
4912 elsif Must_Not_Override (Spec)
4913 and then not Is_Primitive
4914 and then Ekind (Subp) /= E_Entry
4915 and then Ekind (Scope (Subp)) /= E_Protected_Type
4918 ("overriding indicator only allowed if subprogram is primitive",
4922 end Check_Overriding_Indicator;
4928 -- Note: this procedure needs to know far too much about how the expander
4929 -- messes with exceptions. The use of the flag Exception_Junk and the
4930 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4931 -- works, but is not very clean. It would be better if the expansion
4932 -- routines would leave Original_Node working nicely, and we could use
4933 -- Original_Node here to ignore all the peculiar expander messing ???
4935 procedure Check_Returns
4939 Proc : Entity_Id := Empty)
4943 procedure Check_Statement_Sequence (L : List_Id);
4944 -- Internal recursive procedure to check a list of statements for proper
4945 -- termination by a return statement (or a transfer of control or a
4946 -- compound statement that is itself internally properly terminated).
4948 ------------------------------
4949 -- Check_Statement_Sequence --
4950 ------------------------------
4952 procedure Check_Statement_Sequence (L : List_Id) is
4957 Raise_Exception_Call : Boolean;
4958 -- Set True if statement sequence terminated by Raise_Exception call
4959 -- or a Reraise_Occurrence call.
4962 Raise_Exception_Call := False;
4964 -- Get last real statement
4966 Last_Stm := Last (L);
4968 -- Deal with digging out exception handler statement sequences that
4969 -- have been transformed by the local raise to goto optimization.
4970 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4971 -- optimization has occurred, we are looking at something like:
4974 -- original stmts in block
4978 -- goto L1; | omitted if No_Exception_Propagation
4983 -- goto L3; -- skip handler when exception not raised
4985 -- <<L1>> -- target label for local exception
4999 -- and what we have to do is to dig out the estmts1 and estmts2
5000 -- sequences (which were the original sequences of statements in
5001 -- the exception handlers) and check them.
5003 if Nkind (Last_Stm) = N_Label
5004 and then Exception_Junk (Last_Stm)
5010 exit when Nkind (Stm) /= N_Block_Statement;
5011 exit when not Exception_Junk (Stm);
5014 exit when Nkind (Stm) /= N_Label;
5015 exit when not Exception_Junk (Stm);
5016 Check_Statement_Sequence
5017 (Statements (Handled_Statement_Sequence (Next (Stm))));
5022 exit when Nkind (Stm) /= N_Goto_Statement;
5023 exit when not Exception_Junk (Stm);
5027 -- Don't count pragmas
5029 while Nkind (Last_Stm) = N_Pragma
5031 -- Don't count call to SS_Release (can happen after Raise_Exception)
5034 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5036 Nkind (Name (Last_Stm)) = N_Identifier
5038 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5040 -- Don't count exception junk
5043 (Nkind_In (Last_Stm, N_Goto_Statement,
5045 N_Object_Declaration)
5046 and then Exception_Junk (Last_Stm))
5047 or else Nkind (Last_Stm) in N_Push_xxx_Label
5048 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5053 -- Here we have the "real" last statement
5055 Kind := Nkind (Last_Stm);
5057 -- Transfer of control, OK. Note that in the No_Return procedure
5058 -- case, we already diagnosed any explicit return statements, so
5059 -- we can treat them as OK in this context.
5061 if Is_Transfer (Last_Stm) then
5064 -- Check cases of explicit non-indirect procedure calls
5066 elsif Kind = N_Procedure_Call_Statement
5067 and then Is_Entity_Name (Name (Last_Stm))
5069 -- Check call to Raise_Exception procedure which is treated
5070 -- specially, as is a call to Reraise_Occurrence.
5072 -- We suppress the warning in these cases since it is likely that
5073 -- the programmer really does not expect to deal with the case
5074 -- of Null_Occurrence, and thus would find a warning about a
5075 -- missing return curious, and raising Program_Error does not
5076 -- seem such a bad behavior if this does occur.
5078 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5079 -- behavior will be to raise Constraint_Error (see AI-329).
5081 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
5083 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
5085 Raise_Exception_Call := True;
5087 -- For Raise_Exception call, test first argument, if it is
5088 -- an attribute reference for a 'Identity call, then we know
5089 -- that the call cannot possibly return.
5092 Arg : constant Node_Id :=
5093 Original_Node (First_Actual (Last_Stm));
5095 if Nkind (Arg) = N_Attribute_Reference
5096 and then Attribute_Name (Arg) = Name_Identity
5103 -- If statement, need to look inside if there is an else and check
5104 -- each constituent statement sequence for proper termination.
5106 elsif Kind = N_If_Statement
5107 and then Present (Else_Statements (Last_Stm))
5109 Check_Statement_Sequence (Then_Statements (Last_Stm));
5110 Check_Statement_Sequence (Else_Statements (Last_Stm));
5112 if Present (Elsif_Parts (Last_Stm)) then
5114 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
5117 while Present (Elsif_Part) loop
5118 Check_Statement_Sequence (Then_Statements (Elsif_Part));
5126 -- Case statement, check each case for proper termination
5128 elsif Kind = N_Case_Statement then
5132 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
5133 while Present (Case_Alt) loop
5134 Check_Statement_Sequence (Statements (Case_Alt));
5135 Next_Non_Pragma (Case_Alt);
5141 -- Block statement, check its handled sequence of statements
5143 elsif Kind = N_Block_Statement then
5149 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
5158 -- Loop statement. If there is an iteration scheme, we can definitely
5159 -- fall out of the loop. Similarly if there is an exit statement, we
5160 -- can fall out. In either case we need a following return.
5162 elsif Kind = N_Loop_Statement then
5163 if Present (Iteration_Scheme (Last_Stm))
5164 or else Has_Exit (Entity (Identifier (Last_Stm)))
5168 -- A loop with no exit statement or iteration scheme is either
5169 -- an infinite loop, or it has some other exit (raise/return).
5170 -- In either case, no warning is required.
5176 -- Timed entry call, check entry call and delay alternatives
5178 -- Note: in expanded code, the timed entry call has been converted
5179 -- to a set of expanded statements on which the check will work
5180 -- correctly in any case.
5182 elsif Kind = N_Timed_Entry_Call then
5184 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5185 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
5188 -- If statement sequence of entry call alternative is missing,
5189 -- then we can definitely fall through, and we post the error
5190 -- message on the entry call alternative itself.
5192 if No (Statements (ECA)) then
5195 -- If statement sequence of delay alternative is missing, then
5196 -- we can definitely fall through, and we post the error
5197 -- message on the delay alternative itself.
5199 -- Note: if both ECA and DCA are missing the return, then we
5200 -- post only one message, should be enough to fix the bugs.
5201 -- If not we will get a message next time on the DCA when the
5204 elsif No (Statements (DCA)) then
5207 -- Else check both statement sequences
5210 Check_Statement_Sequence (Statements (ECA));
5211 Check_Statement_Sequence (Statements (DCA));
5216 -- Conditional entry call, check entry call and else part
5218 -- Note: in expanded code, the conditional entry call has been
5219 -- converted to a set of expanded statements on which the check
5220 -- will work correctly in any case.
5222 elsif Kind = N_Conditional_Entry_Call then
5224 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5227 -- If statement sequence of entry call alternative is missing,
5228 -- then we can definitely fall through, and we post the error
5229 -- message on the entry call alternative itself.
5231 if No (Statements (ECA)) then
5234 -- Else check statement sequence and else part
5237 Check_Statement_Sequence (Statements (ECA));
5238 Check_Statement_Sequence (Else_Statements (Last_Stm));
5244 -- If we fall through, issue appropriate message
5247 if not Raise_Exception_Call then
5249 ("?RETURN statement missing following this statement!",
5252 ("\?Program_Error may be raised at run time!",
5256 -- Note: we set Err even though we have not issued a warning
5257 -- because we still have a case of a missing return. This is
5258 -- an extremely marginal case, probably will never be noticed
5259 -- but we might as well get it right.
5263 -- Otherwise we have the case of a procedure marked No_Return
5266 if not Raise_Exception_Call then
5268 ("?implied return after this statement " &
5269 "will raise Program_Error",
5272 ("\?procedure & is marked as No_Return!",
5277 RE : constant Node_Id :=
5278 Make_Raise_Program_Error (Sloc (Last_Stm),
5279 Reason => PE_Implicit_Return);
5281 Insert_After (Last_Stm, RE);
5285 end Check_Statement_Sequence;
5287 -- Start of processing for Check_Returns
5291 Check_Statement_Sequence (Statements (HSS));
5293 if Present (Exception_Handlers (HSS)) then
5294 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5295 while Present (Handler) loop
5296 Check_Statement_Sequence (Statements (Handler));
5297 Next_Non_Pragma (Handler);
5302 ----------------------------
5303 -- Check_Subprogram_Order --
5304 ----------------------------
5306 procedure Check_Subprogram_Order (N : Node_Id) is
5308 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5309 -- This is used to check if S1 > S2 in the sense required by this
5310 -- test, for example nameab < namec, but name2 < name10.
5312 -----------------------------
5313 -- Subprogram_Name_Greater --
5314 -----------------------------
5316 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5321 -- Remove trailing numeric parts
5324 while S1 (L1) in '0' .. '9' loop
5329 while S2 (L2) in '0' .. '9' loop
5333 -- If non-numeric parts non-equal, that's decisive
5335 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5338 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5341 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5342 -- that a missing suffix is treated as numeric zero in this test.
5346 while L1 < S1'Last loop
5348 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5352 while L2 < S2'Last loop
5354 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5359 end Subprogram_Name_Greater;
5361 -- Start of processing for Check_Subprogram_Order
5364 -- Check body in alpha order if this is option
5367 and then Style_Check_Order_Subprograms
5368 and then Nkind (N) = N_Subprogram_Body
5369 and then Comes_From_Source (N)
5370 and then In_Extended_Main_Source_Unit (N)
5374 renames Scope_Stack.Table
5375 (Scope_Stack.Last).Last_Subprogram_Name;
5377 Body_Id : constant Entity_Id :=
5378 Defining_Entity (Specification (N));
5381 Get_Decoded_Name_String (Chars (Body_Id));
5384 if Subprogram_Name_Greater
5385 (LSN.all, Name_Buffer (1 .. Name_Len))
5387 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5393 LSN := new String'(Name_Buffer (1 .. Name_Len));
5396 end Check_Subprogram_Order;
5398 ------------------------------
5399 -- Check_Subtype_Conformant --
5400 ------------------------------
5402 procedure Check_Subtype_Conformant
5403 (New_Id : Entity_Id;
5405 Err_Loc : Node_Id := Empty;
5406 Skip_Controlling_Formals : Boolean := False)
5409 pragma Warnings (Off, Result);
5412 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5413 Skip_Controlling_Formals => Skip_Controlling_Formals);
5414 end Check_Subtype_Conformant;
5416 ---------------------------
5417 -- Check_Type_Conformant --
5418 ---------------------------
5420 procedure Check_Type_Conformant
5421 (New_Id : Entity_Id;
5423 Err_Loc : Node_Id := Empty)
5426 pragma Warnings (Off, Result);
5429 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5430 end Check_Type_Conformant;
5432 ---------------------------
5433 -- Can_Override_Operator --
5434 ---------------------------
5436 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
5439 if Nkind (Subp) /= N_Defining_Operator_Symbol then
5443 Typ := Base_Type (Etype (First_Formal (Subp)));
5445 return Operator_Matches_Spec (Subp, Subp)
5446 and then Scope (Subp) = Scope (Typ)
5447 and then not Is_Class_Wide_Type (Typ);
5449 end Can_Override_Operator;
5451 ----------------------
5452 -- Conforming_Types --
5453 ----------------------
5455 function Conforming_Types
5458 Ctype : Conformance_Type;
5459 Get_Inst : Boolean := False) return Boolean
5461 Type_1 : Entity_Id := T1;
5462 Type_2 : Entity_Id := T2;
5463 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5465 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5466 -- If neither T1 nor T2 are generic actual types, or if they are in
5467 -- different scopes (e.g. parent and child instances), then verify that
5468 -- the base types are equal. Otherwise T1 and T2 must be on the same
5469 -- subtype chain. The whole purpose of this procedure is to prevent
5470 -- spurious ambiguities in an instantiation that may arise if two
5471 -- distinct generic types are instantiated with the same actual.
5473 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5474 -- An access parameter can designate an incomplete type. If the
5475 -- incomplete type is the limited view of a type from a limited_
5476 -- with_clause, check whether the non-limited view is available. If
5477 -- it is a (non-limited) incomplete type, get the full view.
5479 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5480 -- Returns True if and only if either T1 denotes a limited view of T2
5481 -- or T2 denotes a limited view of T1. This can arise when the limited
5482 -- with view of a type is used in a subprogram declaration and the
5483 -- subprogram body is in the scope of a regular with clause for the
5484 -- same unit. In such a case, the two type entities can be considered
5485 -- identical for purposes of conformance checking.
5487 ----------------------
5488 -- Base_Types_Match --
5489 ----------------------
5491 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5496 elsif Base_Type (T1) = Base_Type (T2) then
5498 -- The following is too permissive. A more precise test should
5499 -- check that the generic actual is an ancestor subtype of the
5502 return not Is_Generic_Actual_Type (T1)
5503 or else not Is_Generic_Actual_Type (T2)
5504 or else Scope (T1) /= Scope (T2);
5509 end Base_Types_Match;
5511 --------------------------
5512 -- Find_Designated_Type --
5513 --------------------------
5515 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5519 Desig := Directly_Designated_Type (T);
5521 if Ekind (Desig) = E_Incomplete_Type then
5523 -- If regular incomplete type, get full view if available
5525 if Present (Full_View (Desig)) then
5526 Desig := Full_View (Desig);
5528 -- If limited view of a type, get non-limited view if available,
5529 -- and check again for a regular incomplete type.
5531 elsif Present (Non_Limited_View (Desig)) then
5532 Desig := Get_Full_View (Non_Limited_View (Desig));
5537 end Find_Designated_Type;
5539 -------------------------------
5540 -- Matches_Limited_With_View --
5541 -------------------------------
5543 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5545 -- In some cases a type imported through a limited_with clause, and
5546 -- its nonlimited view are both visible, for example in an anonymous
5547 -- access-to-class-wide type in a formal. Both entities designate the
5550 if From_With_Type (T1)
5551 and then T2 = Available_View (T1)
5555 elsif From_With_Type (T2)
5556 and then T1 = Available_View (T2)
5563 end Matches_Limited_With_View;
5565 -- Start of processing for Conforming_Types
5568 -- The context is an instance association for a formal
5569 -- access-to-subprogram type; the formal parameter types require
5570 -- mapping because they may denote other formal parameters of the
5574 Type_1 := Get_Instance_Of (T1);
5575 Type_2 := Get_Instance_Of (T2);
5578 -- If one of the types is a view of the other introduced by a limited
5579 -- with clause, treat these as conforming for all purposes.
5581 if Matches_Limited_With_View (T1, T2) then
5584 elsif Base_Types_Match (Type_1, Type_2) then
5585 return Ctype <= Mode_Conformant
5586 or else Subtypes_Statically_Match (Type_1, Type_2);
5588 elsif Is_Incomplete_Or_Private_Type (Type_1)
5589 and then Present (Full_View (Type_1))
5590 and then Base_Types_Match (Full_View (Type_1), Type_2)
5592 return Ctype <= Mode_Conformant
5593 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5595 elsif Ekind (Type_2) = E_Incomplete_Type
5596 and then Present (Full_View (Type_2))
5597 and then Base_Types_Match (Type_1, Full_View (Type_2))
5599 return Ctype <= Mode_Conformant
5600 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5602 elsif Is_Private_Type (Type_2)
5603 and then In_Instance
5604 and then Present (Full_View (Type_2))
5605 and then Base_Types_Match (Type_1, Full_View (Type_2))
5607 return Ctype <= Mode_Conformant
5608 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5611 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5612 -- treated recursively because they carry a signature.
5614 Are_Anonymous_Access_To_Subprogram_Types :=
5615 Ekind (Type_1) = Ekind (Type_2)
5617 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5619 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5621 -- Test anonymous access type case. For this case, static subtype
5622 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5623 -- the base types because we may have built internal subtype entities
5624 -- to handle null-excluding types (see Process_Formals).
5626 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5628 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5629 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5632 Desig_1 : Entity_Id;
5633 Desig_2 : Entity_Id;
5636 -- In Ada2005, access constant indicators must match for
5637 -- subtype conformance.
5639 if Ada_Version >= Ada_2005
5640 and then Ctype >= Subtype_Conformant
5642 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5647 Desig_1 := Find_Designated_Type (Type_1);
5648 Desig_2 := Find_Designated_Type (Type_2);
5650 -- If the context is an instance association for a formal
5651 -- access-to-subprogram type; formal access parameter designated
5652 -- types require mapping because they may denote other formal
5653 -- parameters of the generic unit.
5656 Desig_1 := Get_Instance_Of (Desig_1);
5657 Desig_2 := Get_Instance_Of (Desig_2);
5660 -- It is possible for a Class_Wide_Type to be introduced for an
5661 -- incomplete type, in which case there is a separate class_ wide
5662 -- type for the full view. The types conform if their Etypes
5663 -- conform, i.e. one may be the full view of the other. This can
5664 -- only happen in the context of an access parameter, other uses
5665 -- of an incomplete Class_Wide_Type are illegal.
5667 if Is_Class_Wide_Type (Desig_1)
5669 Is_Class_Wide_Type (Desig_2)
5673 (Etype (Base_Type (Desig_1)),
5674 Etype (Base_Type (Desig_2)), Ctype);
5676 elsif Are_Anonymous_Access_To_Subprogram_Types then
5677 if Ada_Version < Ada_2005 then
5678 return Ctype = Type_Conformant
5680 Subtypes_Statically_Match (Desig_1, Desig_2);
5682 -- We must check the conformance of the signatures themselves
5686 Conformant : Boolean;
5689 (Desig_1, Desig_2, Ctype, False, Conformant);
5695 return Base_Type (Desig_1) = Base_Type (Desig_2)
5696 and then (Ctype = Type_Conformant
5698 Subtypes_Statically_Match (Desig_1, Desig_2));
5702 -- Otherwise definitely no match
5705 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5706 and then Is_Access_Type (Type_2))
5707 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5708 and then Is_Access_Type (Type_1)))
5711 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5713 May_Hide_Profile := True;
5718 end Conforming_Types;
5720 --------------------------
5721 -- Create_Extra_Formals --
5722 --------------------------
5724 procedure Create_Extra_Formals (E : Entity_Id) is
5726 First_Extra : Entity_Id := Empty;
5727 Last_Extra : Entity_Id;
5728 Formal_Type : Entity_Id;
5729 P_Formal : Entity_Id := Empty;
5731 function Add_Extra_Formal
5732 (Assoc_Entity : Entity_Id;
5735 Suffix : String) return Entity_Id;
5736 -- Add an extra formal to the current list of formals and extra formals.
5737 -- The extra formal is added to the end of the list of extra formals,
5738 -- and also returned as the result. These formals are always of mode IN.
5739 -- The new formal has the type Typ, is declared in Scope, and its name
5740 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5741 -- The following suffixes are currently used. They should not be changed
5742 -- without coordinating with CodePeer, which makes use of these to
5743 -- provide better messages.
5745 -- O denotes the Constrained bit.
5746 -- L denotes the accessibility level.
5747 -- BIP_xxx denotes an extra formal for a build-in-place function. See
5748 -- the full list in exp_ch6.BIP_Formal_Kind.
5750 ----------------------
5751 -- Add_Extra_Formal --
5752 ----------------------
5754 function Add_Extra_Formal
5755 (Assoc_Entity : Entity_Id;
5758 Suffix : String) return Entity_Id
5760 EF : constant Entity_Id :=
5761 Make_Defining_Identifier (Sloc (Assoc_Entity),
5762 Chars => New_External_Name (Chars (Assoc_Entity),
5766 -- A little optimization. Never generate an extra formal for the
5767 -- _init operand of an initialization procedure, since it could
5770 if Chars (Formal) = Name_uInit then
5774 Set_Ekind (EF, E_In_Parameter);
5775 Set_Actual_Subtype (EF, Typ);
5776 Set_Etype (EF, Typ);
5777 Set_Scope (EF, Scope);
5778 Set_Mechanism (EF, Default_Mechanism);
5779 Set_Formal_Validity (EF);
5781 if No (First_Extra) then
5783 Set_Extra_Formals (Scope, First_Extra);
5786 if Present (Last_Extra) then
5787 Set_Extra_Formal (Last_Extra, EF);
5793 end Add_Extra_Formal;
5795 -- Start of processing for Create_Extra_Formals
5798 -- We never generate extra formals if expansion is not active
5799 -- because we don't need them unless we are generating code.
5801 if not Expander_Active then
5805 -- If this is a derived subprogram then the subtypes of the parent
5806 -- subprogram's formal parameters will be used to determine the need
5807 -- for extra formals.
5809 if Is_Overloadable (E) and then Present (Alias (E)) then
5810 P_Formal := First_Formal (Alias (E));
5813 Last_Extra := Empty;
5814 Formal := First_Formal (E);
5815 while Present (Formal) loop
5816 Last_Extra := Formal;
5817 Next_Formal (Formal);
5820 -- If Extra_formals were already created, don't do it again. This
5821 -- situation may arise for subprogram types created as part of
5822 -- dispatching calls (see Expand_Dispatching_Call)
5824 if Present (Last_Extra) and then
5825 Present (Extra_Formal (Last_Extra))
5830 -- If the subprogram is a predefined dispatching subprogram then don't
5831 -- generate any extra constrained or accessibility level formals. In
5832 -- general we suppress these for internal subprograms (by not calling
5833 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5834 -- generated stream attributes do get passed through because extra
5835 -- build-in-place formals are needed in some cases (limited 'Input).
5837 if Is_Predefined_Internal_Operation (E) then
5838 goto Test_For_BIP_Extras;
5841 Formal := First_Formal (E);
5842 while Present (Formal) loop
5844 -- Create extra formal for supporting the attribute 'Constrained.
5845 -- The case of a private type view without discriminants also
5846 -- requires the extra formal if the underlying type has defaulted
5849 if Ekind (Formal) /= E_In_Parameter then
5850 if Present (P_Formal) then
5851 Formal_Type := Etype (P_Formal);
5853 Formal_Type := Etype (Formal);
5856 -- Do not produce extra formals for Unchecked_Union parameters.
5857 -- Jump directly to the end of the loop.
5859 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5860 goto Skip_Extra_Formal_Generation;
5863 if not Has_Discriminants (Formal_Type)
5864 and then Ekind (Formal_Type) in Private_Kind
5865 and then Present (Underlying_Type (Formal_Type))
5867 Formal_Type := Underlying_Type (Formal_Type);
5870 -- Suppress the extra formal if formal's subtype is constrained or
5871 -- indefinite, or we're compiling for Ada 2012 and the underlying
5872 -- type is tagged and limited. In Ada 2012, a limited tagged type
5873 -- can have defaulted discriminants, but 'Constrained is required
5874 -- to return True, so the formal is never needed (see AI05-0214).
5875 -- Note that this ensures consistency of calling sequences for
5876 -- dispatching operations when some types in a class have defaults
5877 -- on discriminants and others do not (and requiring the extra
5878 -- formal would introduce distributed overhead).
5880 if Has_Discriminants (Formal_Type)
5881 and then not Is_Constrained (Formal_Type)
5882 and then not Is_Indefinite_Subtype (Formal_Type)
5883 and then (Ada_Version < Ada_2012
5885 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
5886 and then Is_Limited_Type (Formal_Type)))
5888 Set_Extra_Constrained
5889 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
5893 -- Create extra formal for supporting accessibility checking. This
5894 -- is done for both anonymous access formals and formals of named
5895 -- access types that are marked as controlling formals. The latter
5896 -- case can occur when Expand_Dispatching_Call creates a subprogram
5897 -- type and substitutes the types of access-to-class-wide actuals
5898 -- for the anonymous access-to-specific-type of controlling formals.
5899 -- Base_Type is applied because in cases where there is a null
5900 -- exclusion the formal may have an access subtype.
5902 -- This is suppressed if we specifically suppress accessibility
5903 -- checks at the package level for either the subprogram, or the
5904 -- package in which it resides. However, we do not suppress it
5905 -- simply if the scope has accessibility checks suppressed, since
5906 -- this could cause trouble when clients are compiled with a
5907 -- different suppression setting. The explicit checks at the
5908 -- package level are safe from this point of view.
5910 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5911 or else (Is_Controlling_Formal (Formal)
5912 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5914 (Explicit_Suppress (E, Accessibility_Check)
5916 Explicit_Suppress (Scope (E), Accessibility_Check))
5919 or else Present (Extra_Accessibility (P_Formal)))
5921 Set_Extra_Accessibility
5922 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
5925 -- This label is required when skipping extra formal generation for
5926 -- Unchecked_Union parameters.
5928 <<Skip_Extra_Formal_Generation>>
5930 if Present (P_Formal) then
5931 Next_Formal (P_Formal);
5934 Next_Formal (Formal);
5937 <<Test_For_BIP_Extras>>
5939 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5940 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5942 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
5944 Result_Subt : constant Entity_Id := Etype (E);
5946 Discard : Entity_Id;
5947 pragma Warnings (Off, Discard);
5950 -- In the case of functions with unconstrained result subtypes,
5951 -- add a 4-state formal indicating whether the return object is
5952 -- allocated by the caller (1), or should be allocated by the
5953 -- callee on the secondary stack (2), in the global heap (3), or
5954 -- in a user-defined storage pool (4). For the moment we just use
5955 -- Natural for the type of this formal. Note that this formal
5956 -- isn't usually needed in the case where the result subtype is
5957 -- constrained, but it is needed when the function has a tagged
5958 -- result, because generally such functions can be called in a
5959 -- dispatching context and such calls must be handled like calls
5960 -- to a class-wide function.
5962 if not Is_Constrained (Underlying_Type (Result_Subt))
5963 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5967 (E, Standard_Natural,
5968 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5971 -- For functions whose result type has controlled parts, we have
5972 -- an extra formal of type System.Finalization_Implementation.
5973 -- Finalizable_Ptr_Ptr. That is, we are passing a pointer to a
5974 -- finalization list (which is itself a pointer). This extra
5975 -- formal is then passed along to Move_Final_List in case of
5976 -- successful completion of a return statement. We cannot pass an
5977 -- 'in out' parameter, because we need to update the finalization
5978 -- list during an abort-deferred region, rather than using
5979 -- copy-back after the function returns. This is true even if we
5980 -- are able to get away with having 'in out' parameters, which are
5981 -- normally illegal for functions. This formal is also needed when
5982 -- the function has a tagged result.
5984 if Needs_BIP_Final_List (E) then
5987 (E, RTE (RE_Finalizable_Ptr_Ptr),
5988 E, BIP_Formal_Suffix (BIP_Final_List));
5991 -- If the result type contains tasks, we have two extra formals:
5992 -- the master of the tasks to be created, and the caller's
5993 -- activation chain.
5995 if Has_Task (Result_Subt) then
5998 (E, RTE (RE_Master_Id),
5999 E, BIP_Formal_Suffix (BIP_Master));
6002 (E, RTE (RE_Activation_Chain_Access),
6003 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6006 -- All build-in-place functions get an extra formal that will be
6007 -- passed the address of the return object within the caller.
6010 Formal_Type : constant Entity_Id :=
6012 (E_Anonymous_Access_Type, E,
6013 Scope_Id => Scope (E));
6015 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
6016 Set_Etype (Formal_Type, Formal_Type);
6017 Set_Depends_On_Private
6018 (Formal_Type, Has_Private_Component (Formal_Type));
6019 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
6020 Set_Is_Access_Constant (Formal_Type, False);
6022 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
6023 -- the designated type comes from the limited view (for
6024 -- back-end purposes).
6026 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
6028 Layout_Type (Formal_Type);
6032 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
6036 end Create_Extra_Formals;
6038 -----------------------------
6039 -- Enter_Overloaded_Entity --
6040 -----------------------------
6042 procedure Enter_Overloaded_Entity (S : Entity_Id) is
6043 E : Entity_Id := Current_Entity_In_Scope (S);
6044 C_E : Entity_Id := Current_Entity (S);
6048 Set_Has_Homonym (E);
6049 Set_Has_Homonym (S);
6052 Set_Is_Immediately_Visible (S);
6053 Set_Scope (S, Current_Scope);
6055 -- Chain new entity if front of homonym in current scope, so that
6056 -- homonyms are contiguous.
6061 while Homonym (C_E) /= E loop
6062 C_E := Homonym (C_E);
6065 Set_Homonym (C_E, S);
6069 Set_Current_Entity (S);
6074 Append_Entity (S, Current_Scope);
6075 Set_Public_Status (S);
6077 if Debug_Flag_E then
6078 Write_Str ("New overloaded entity chain: ");
6079 Write_Name (Chars (S));
6082 while Present (E) loop
6083 Write_Str (" "); Write_Int (Int (E));
6090 -- Generate warning for hiding
6093 and then Comes_From_Source (S)
6094 and then In_Extended_Main_Source_Unit (S)
6101 -- Warn unless genuine overloading. Do not emit warning on
6102 -- hiding predefined operators in Standard (these are either an
6103 -- (artifact of our implicit declarations, or simple noise) but
6104 -- keep warning on a operator defined on a local subtype, because
6105 -- of the real danger that different operators may be applied in
6106 -- various parts of the program.
6108 -- Note that if E and S have the same scope, there is never any
6109 -- hiding. Either the two conflict, and the program is illegal,
6110 -- or S is overriding an implicit inherited subprogram.
6112 if Scope (E) /= Scope (S)
6113 and then (not Is_Overloadable (E)
6114 or else Subtype_Conformant (E, S))
6115 and then (Is_Immediately_Visible (E)
6117 Is_Potentially_Use_Visible (S))
6119 if Scope (E) /= Standard_Standard then
6120 Error_Msg_Sloc := Sloc (E);
6121 Error_Msg_N ("declaration of & hides one#?", S);
6123 elsif Nkind (S) = N_Defining_Operator_Symbol
6125 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
6128 ("declaration of & hides predefined operator?", S);
6133 end Enter_Overloaded_Entity;
6135 -----------------------------
6136 -- Check_Untagged_Equality --
6137 -----------------------------
6139 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
6140 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
6141 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
6145 if Nkind (Decl) = N_Subprogram_Declaration
6146 and then Is_Record_Type (Typ)
6147 and then not Is_Tagged_Type (Typ)
6149 -- If the type is not declared in a package, or if we are in the
6150 -- body of the package or in some other scope, the new operation is
6151 -- not primitive, and therefore legal, though suspicious. If the
6152 -- type is a generic actual (sub)type, the operation is not primitive
6153 -- either because the base type is declared elsewhere.
6155 if Is_Frozen (Typ) then
6156 if Ekind (Scope (Typ)) /= E_Package
6157 or else Scope (Typ) /= Current_Scope
6161 elsif Is_Generic_Actual_Type (Typ) then
6164 elsif In_Package_Body (Scope (Typ)) then
6166 ("equality operator must be declared "
6167 & "before type& is frozen", Eq_Op, Typ);
6169 ("\move declaration to package spec", Eq_Op);
6173 ("equality operator must be declared "
6174 & "before type& is frozen", Eq_Op, Typ);
6176 Obj_Decl := Next (Parent (Typ));
6177 while Present (Obj_Decl)
6178 and then Obj_Decl /= Decl
6180 if Nkind (Obj_Decl) = N_Object_Declaration
6181 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
6183 Error_Msg_NE ("type& is frozen by declaration?",
6186 ("\an equality operator cannot be declared after this "
6187 & "point ('R'M 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
6195 elsif not In_Same_List (Parent (Typ), Decl)
6196 and then not Is_Limited_Type (Typ)
6199 -- This makes it illegal to have a primitive equality declared in
6200 -- the private part if the type is visible.
6202 Error_Msg_N ("equality operator appears too late", Eq_Op);
6205 end Check_Untagged_Equality;
6207 -----------------------------
6208 -- Find_Corresponding_Spec --
6209 -----------------------------
6211 function Find_Corresponding_Spec
6213 Post_Error : Boolean := True) return Entity_Id
6215 Spec : constant Node_Id := Specification (N);
6216 Designator : constant Entity_Id := Defining_Entity (Spec);
6221 E := Current_Entity (Designator);
6222 while Present (E) loop
6224 -- We are looking for a matching spec. It must have the same scope,
6225 -- and the same name, and either be type conformant, or be the case
6226 -- of a library procedure spec and its body (which belong to one
6227 -- another regardless of whether they are type conformant or not).
6229 if Scope (E) = Current_Scope then
6230 if Current_Scope = Standard_Standard
6231 or else (Ekind (E) = Ekind (Designator)
6232 and then Type_Conformant (E, Designator))
6234 -- Within an instantiation, we know that spec and body are
6235 -- subtype conformant, because they were subtype conformant
6236 -- in the generic. We choose the subtype-conformant entity
6237 -- here as well, to resolve spurious ambiguities in the
6238 -- instance that were not present in the generic (i.e. when
6239 -- two different types are given the same actual). If we are
6240 -- looking for a spec to match a body, full conformance is
6244 Set_Convention (Designator, Convention (E));
6246 if Nkind (N) = N_Subprogram_Body
6247 and then Present (Homonym (E))
6248 and then not Fully_Conformant (E, Designator)
6252 elsif not Subtype_Conformant (E, Designator) then
6257 if not Has_Completion (E) then
6258 if Nkind (N) /= N_Subprogram_Body_Stub then
6259 Set_Corresponding_Spec (N, E);
6262 Set_Has_Completion (E);
6265 elsif Nkind (Parent (N)) = N_Subunit then
6267 -- If this is the proper body of a subunit, the completion
6268 -- flag is set when analyzing the stub.
6272 -- If E is an internal function with a controlling result
6273 -- that was created for an operation inherited by a null
6274 -- extension, it may be overridden by a body without a previous
6275 -- spec (one more reason why these should be shunned). In that
6276 -- case remove the generated body if present, because the
6277 -- current one is the explicit overriding.
6279 elsif Ekind (E) = E_Function
6280 and then Ada_Version >= Ada_2005
6281 and then not Comes_From_Source (E)
6282 and then Has_Controlling_Result (E)
6283 and then Is_Null_Extension (Etype (E))
6284 and then Comes_From_Source (Spec)
6286 Set_Has_Completion (E, False);
6289 and then Nkind (Parent (E)) = N_Function_Specification
6292 (Unit_Declaration_Node
6293 (Corresponding_Body (Unit_Declaration_Node (E))));
6297 -- If expansion is disabled, or if the wrapper function has
6298 -- not been generated yet, this a late body overriding an
6299 -- inherited operation, or it is an overriding by some other
6300 -- declaration before the controlling result is frozen. In
6301 -- either case this is a declaration of a new entity.
6307 -- If the body already exists, then this is an error unless
6308 -- the previous declaration is the implicit declaration of a
6309 -- derived subprogram, or this is a spurious overloading in an
6312 elsif No (Alias (E))
6313 and then not Is_Intrinsic_Subprogram (E)
6314 and then not In_Instance
6317 Error_Msg_Sloc := Sloc (E);
6319 if Is_Imported (E) then
6321 ("body not allowed for imported subprogram & declared#",
6324 Error_Msg_NE ("duplicate body for & declared#", N, E);
6328 -- Child units cannot be overloaded, so a conformance mismatch
6329 -- between body and a previous spec is an error.
6331 elsif Is_Child_Unit (E)
6333 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
6335 Nkind (Parent (Unit_Declaration_Node (Designator))) =
6340 ("body of child unit does not match previous declaration", N);
6348 -- On exit, we know that no previous declaration of subprogram exists
6351 end Find_Corresponding_Spec;
6353 ----------------------
6354 -- Fully_Conformant --
6355 ----------------------
6357 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6360 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
6362 end Fully_Conformant;
6364 ----------------------------------
6365 -- Fully_Conformant_Expressions --
6366 ----------------------------------
6368 function Fully_Conformant_Expressions
6369 (Given_E1 : Node_Id;
6370 Given_E2 : Node_Id) return Boolean
6372 E1 : constant Node_Id := Original_Node (Given_E1);
6373 E2 : constant Node_Id := Original_Node (Given_E2);
6374 -- We always test conformance on original nodes, since it is possible
6375 -- for analysis and/or expansion to make things look as though they
6376 -- conform when they do not, e.g. by converting 1+2 into 3.
6378 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6379 renames Fully_Conformant_Expressions;
6381 function FCL (L1, L2 : List_Id) return Boolean;
6382 -- Compare elements of two lists for conformance. Elements have to
6383 -- be conformant, and actuals inserted as default parameters do not
6384 -- match explicit actuals with the same value.
6386 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6387 -- Compare an operator node with a function call
6393 function FCL (L1, L2 : List_Id) return Boolean is
6397 if L1 = No_List then
6403 if L2 = No_List then
6409 -- Compare two lists, skipping rewrite insertions (we want to
6410 -- compare the original trees, not the expanded versions!)
6413 if Is_Rewrite_Insertion (N1) then
6415 elsif Is_Rewrite_Insertion (N2) then
6421 elsif not FCE (N1, N2) then
6434 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6435 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6440 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6445 Act := First (Actuals);
6447 if Nkind (Op_Node) in N_Binary_Op then
6448 if not FCE (Left_Opnd (Op_Node), Act) then
6455 return Present (Act)
6456 and then FCE (Right_Opnd (Op_Node), Act)
6457 and then No (Next (Act));
6461 -- Start of processing for Fully_Conformant_Expressions
6464 -- Non-conformant if paren count does not match. Note: if some idiot
6465 -- complains that we don't do this right for more than 3 levels of
6466 -- parentheses, they will be treated with the respect they deserve!
6468 if Paren_Count (E1) /= Paren_Count (E2) then
6471 -- If same entities are referenced, then they are conformant even if
6472 -- they have different forms (RM 8.3.1(19-20)).
6474 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6475 if Present (Entity (E1)) then
6476 return Entity (E1) = Entity (E2)
6477 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6478 and then Ekind (Entity (E1)) = E_Discriminant
6479 and then Ekind (Entity (E2)) = E_In_Parameter);
6481 elsif Nkind (E1) = N_Expanded_Name
6482 and then Nkind (E2) = N_Expanded_Name
6483 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6484 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6486 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6489 -- Identifiers in component associations don't always have
6490 -- entities, but their names must conform.
6492 return Nkind (E1) = N_Identifier
6493 and then Nkind (E2) = N_Identifier
6494 and then Chars (E1) = Chars (E2);
6497 elsif Nkind (E1) = N_Character_Literal
6498 and then Nkind (E2) = N_Expanded_Name
6500 return Nkind (Selector_Name (E2)) = N_Character_Literal
6501 and then Chars (E1) = Chars (Selector_Name (E2));
6503 elsif Nkind (E2) = N_Character_Literal
6504 and then Nkind (E1) = N_Expanded_Name
6506 return Nkind (Selector_Name (E1)) = N_Character_Literal
6507 and then Chars (E2) = Chars (Selector_Name (E1));
6509 elsif Nkind (E1) in N_Op
6510 and then Nkind (E2) = N_Function_Call
6512 return FCO (E1, E2);
6514 elsif Nkind (E2) in N_Op
6515 and then Nkind (E1) = N_Function_Call
6517 return FCO (E2, E1);
6519 -- Otherwise we must have the same syntactic entity
6521 elsif Nkind (E1) /= Nkind (E2) then
6524 -- At this point, we specialize by node type
6531 FCL (Expressions (E1), Expressions (E2))
6533 FCL (Component_Associations (E1),
6534 Component_Associations (E2));
6537 if Nkind (Expression (E1)) = N_Qualified_Expression
6539 Nkind (Expression (E2)) = N_Qualified_Expression
6541 return FCE (Expression (E1), Expression (E2));
6543 -- Check that the subtype marks and any constraints
6548 Indic1 : constant Node_Id := Expression (E1);
6549 Indic2 : constant Node_Id := Expression (E2);
6554 if Nkind (Indic1) /= N_Subtype_Indication then
6556 Nkind (Indic2) /= N_Subtype_Indication
6557 and then Entity (Indic1) = Entity (Indic2);
6559 elsif Nkind (Indic2) /= N_Subtype_Indication then
6561 Nkind (Indic1) /= N_Subtype_Indication
6562 and then Entity (Indic1) = Entity (Indic2);
6565 if Entity (Subtype_Mark (Indic1)) /=
6566 Entity (Subtype_Mark (Indic2))
6571 Elt1 := First (Constraints (Constraint (Indic1)));
6572 Elt2 := First (Constraints (Constraint (Indic2)));
6573 while Present (Elt1) and then Present (Elt2) loop
6574 if not FCE (Elt1, Elt2) then
6587 when N_Attribute_Reference =>
6589 Attribute_Name (E1) = Attribute_Name (E2)
6590 and then FCL (Expressions (E1), Expressions (E2));
6594 Entity (E1) = Entity (E2)
6595 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6596 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6598 when N_Short_Circuit | N_Membership_Test =>
6600 FCE (Left_Opnd (E1), Left_Opnd (E2))
6602 FCE (Right_Opnd (E1), Right_Opnd (E2));
6604 when N_Case_Expression =>
6610 if not FCE (Expression (E1), Expression (E2)) then
6614 Alt1 := First (Alternatives (E1));
6615 Alt2 := First (Alternatives (E2));
6617 if Present (Alt1) /= Present (Alt2) then
6619 elsif No (Alt1) then
6623 if not FCE (Expression (Alt1), Expression (Alt2))
6624 or else not FCL (Discrete_Choices (Alt1),
6625 Discrete_Choices (Alt2))
6636 when N_Character_Literal =>
6638 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6640 when N_Component_Association =>
6642 FCL (Choices (E1), Choices (E2))
6644 FCE (Expression (E1), Expression (E2));
6646 when N_Conditional_Expression =>
6648 FCL (Expressions (E1), Expressions (E2));
6650 when N_Explicit_Dereference =>
6652 FCE (Prefix (E1), Prefix (E2));
6654 when N_Extension_Aggregate =>
6656 FCL (Expressions (E1), Expressions (E2))
6657 and then Null_Record_Present (E1) =
6658 Null_Record_Present (E2)
6659 and then FCL (Component_Associations (E1),
6660 Component_Associations (E2));
6662 when N_Function_Call =>
6664 FCE (Name (E1), Name (E2))
6666 FCL (Parameter_Associations (E1),
6667 Parameter_Associations (E2));
6669 when N_Indexed_Component =>
6671 FCE (Prefix (E1), Prefix (E2))
6673 FCL (Expressions (E1), Expressions (E2));
6675 when N_Integer_Literal =>
6676 return (Intval (E1) = Intval (E2));
6681 when N_Operator_Symbol =>
6683 Chars (E1) = Chars (E2);
6685 when N_Others_Choice =>
6688 when N_Parameter_Association =>
6690 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6691 and then FCE (Explicit_Actual_Parameter (E1),
6692 Explicit_Actual_Parameter (E2));
6694 when N_Qualified_Expression =>
6696 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6698 FCE (Expression (E1), Expression (E2));
6700 when N_Quantified_Expression =>
6701 if not FCE (Condition (E1), Condition (E2)) then
6705 if Present (Loop_Parameter_Specification (E1))
6706 and then Present (Loop_Parameter_Specification (E2))
6709 L1 : constant Node_Id :=
6710 Loop_Parameter_Specification (E1);
6711 L2 : constant Node_Id :=
6712 Loop_Parameter_Specification (E2);
6716 Reverse_Present (L1) = Reverse_Present (L2)
6718 FCE (Defining_Identifier (L1),
6719 Defining_Identifier (L2))
6721 FCE (Discrete_Subtype_Definition (L1),
6722 Discrete_Subtype_Definition (L2));
6725 else -- quantified expression with an iterator
6727 I1 : constant Node_Id := Iterator_Specification (E1);
6728 I2 : constant Node_Id := Iterator_Specification (E2);
6732 FCE (Defining_Identifier (I1),
6733 Defining_Identifier (I2))
6735 Of_Present (I1) = Of_Present (I2)
6737 Reverse_Present (I1) = Reverse_Present (I2)
6738 and then FCE (Name (I1), Name (I2))
6739 and then FCE (Subtype_Indication (I1),
6740 Subtype_Indication (I2));
6746 FCE (Low_Bound (E1), Low_Bound (E2))
6748 FCE (High_Bound (E1), High_Bound (E2));
6750 when N_Real_Literal =>
6751 return (Realval (E1) = Realval (E2));
6753 when N_Selected_Component =>
6755 FCE (Prefix (E1), Prefix (E2))
6757 FCE (Selector_Name (E1), Selector_Name (E2));
6761 FCE (Prefix (E1), Prefix (E2))
6763 FCE (Discrete_Range (E1), Discrete_Range (E2));
6765 when N_String_Literal =>
6767 S1 : constant String_Id := Strval (E1);
6768 S2 : constant String_Id := Strval (E2);
6769 L1 : constant Nat := String_Length (S1);
6770 L2 : constant Nat := String_Length (S2);
6777 for J in 1 .. L1 loop
6778 if Get_String_Char (S1, J) /=
6779 Get_String_Char (S2, J)
6789 when N_Type_Conversion =>
6791 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6793 FCE (Expression (E1), Expression (E2));
6797 Entity (E1) = Entity (E2)
6799 FCE (Right_Opnd (E1), Right_Opnd (E2));
6801 when N_Unchecked_Type_Conversion =>
6803 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6805 FCE (Expression (E1), Expression (E2));
6807 -- All other node types cannot appear in this context. Strictly
6808 -- we should raise a fatal internal error. Instead we just ignore
6809 -- the nodes. This means that if anyone makes a mistake in the
6810 -- expander and mucks an expression tree irretrievably, the
6811 -- result will be a failure to detect a (probably very obscure)
6812 -- case of non-conformance, which is better than bombing on some
6813 -- case where two expressions do in fact conform.
6820 end Fully_Conformant_Expressions;
6822 ----------------------------------------
6823 -- Fully_Conformant_Discrete_Subtypes --
6824 ----------------------------------------
6826 function Fully_Conformant_Discrete_Subtypes
6827 (Given_S1 : Node_Id;
6828 Given_S2 : Node_Id) return Boolean
6830 S1 : constant Node_Id := Original_Node (Given_S1);
6831 S2 : constant Node_Id := Original_Node (Given_S2);
6833 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6834 -- Special-case for a bound given by a discriminant, which in the body
6835 -- is replaced with the discriminal of the enclosing type.
6837 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6838 -- Check both bounds
6840 -----------------------
6841 -- Conforming_Bounds --
6842 -----------------------
6844 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6846 if Is_Entity_Name (B1)
6847 and then Is_Entity_Name (B2)
6848 and then Ekind (Entity (B1)) = E_Discriminant
6850 return Chars (B1) = Chars (B2);
6853 return Fully_Conformant_Expressions (B1, B2);
6855 end Conforming_Bounds;
6857 -----------------------
6858 -- Conforming_Ranges --
6859 -----------------------
6861 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6864 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6866 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6867 end Conforming_Ranges;
6869 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6872 if Nkind (S1) /= Nkind (S2) then
6875 elsif Is_Entity_Name (S1) then
6876 return Entity (S1) = Entity (S2);
6878 elsif Nkind (S1) = N_Range then
6879 return Conforming_Ranges (S1, S2);
6881 elsif Nkind (S1) = N_Subtype_Indication then
6883 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6886 (Range_Expression (Constraint (S1)),
6887 Range_Expression (Constraint (S2)));
6891 end Fully_Conformant_Discrete_Subtypes;
6893 --------------------
6894 -- Install_Entity --
6895 --------------------
6897 procedure Install_Entity (E : Entity_Id) is
6898 Prev : constant Entity_Id := Current_Entity (E);
6900 Set_Is_Immediately_Visible (E);
6901 Set_Current_Entity (E);
6902 Set_Homonym (E, Prev);
6905 ---------------------
6906 -- Install_Formals --
6907 ---------------------
6909 procedure Install_Formals (Id : Entity_Id) is
6912 F := First_Formal (Id);
6913 while Present (F) loop
6917 end Install_Formals;
6919 -----------------------------
6920 -- Is_Interface_Conformant --
6921 -----------------------------
6923 function Is_Interface_Conformant
6924 (Tagged_Type : Entity_Id;
6925 Iface_Prim : Entity_Id;
6926 Prim : Entity_Id) return Boolean
6928 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6929 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6932 pragma Assert (Is_Subprogram (Iface_Prim)
6933 and then Is_Subprogram (Prim)
6934 and then Is_Dispatching_Operation (Iface_Prim)
6935 and then Is_Dispatching_Operation (Prim));
6937 pragma Assert (Is_Interface (Iface)
6938 or else (Present (Alias (Iface_Prim))
6941 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6943 if Prim = Iface_Prim
6944 or else not Is_Subprogram (Prim)
6945 or else Ekind (Prim) /= Ekind (Iface_Prim)
6946 or else not Is_Dispatching_Operation (Prim)
6947 or else Scope (Prim) /= Scope (Tagged_Type)
6949 or else Base_Type (Typ) /= Tagged_Type
6950 or else not Primitive_Names_Match (Iface_Prim, Prim)
6954 -- Case of a procedure, or a function that does not have a controlling
6955 -- result (I or access I).
6957 elsif Ekind (Iface_Prim) = E_Procedure
6958 or else Etype (Prim) = Etype (Iface_Prim)
6959 or else not Has_Controlling_Result (Prim)
6961 return Type_Conformant
6962 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
6964 -- Case of a function returning an interface, or an access to one.
6965 -- Check that the return types correspond.
6967 elsif Implements_Interface (Typ, Iface) then
6968 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6970 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6975 Type_Conformant (Prim, Iface_Prim,
6976 Skip_Controlling_Formals => True);
6982 end Is_Interface_Conformant;
6984 ---------------------------------
6985 -- Is_Non_Overriding_Operation --
6986 ---------------------------------
6988 function Is_Non_Overriding_Operation
6989 (Prev_E : Entity_Id;
6990 New_E : Entity_Id) return Boolean
6994 G_Typ : Entity_Id := Empty;
6996 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6997 -- If F_Type is a derived type associated with a generic actual subtype,
6998 -- then return its Generic_Parent_Type attribute, else return Empty.
7000 function Types_Correspond
7001 (P_Type : Entity_Id;
7002 N_Type : Entity_Id) return Boolean;
7003 -- Returns true if and only if the types (or designated types in the
7004 -- case of anonymous access types) are the same or N_Type is derived
7005 -- directly or indirectly from P_Type.
7007 -----------------------------
7008 -- Get_Generic_Parent_Type --
7009 -----------------------------
7011 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
7016 if Is_Derived_Type (F_Typ)
7017 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
7019 -- The tree must be traversed to determine the parent subtype in
7020 -- the generic unit, which unfortunately isn't always available
7021 -- via semantic attributes. ??? (Note: The use of Original_Node
7022 -- is needed for cases where a full derived type has been
7025 Indic := Subtype_Indication
7026 (Type_Definition (Original_Node (Parent (F_Typ))));
7028 if Nkind (Indic) = N_Subtype_Indication then
7029 G_Typ := Entity (Subtype_Mark (Indic));
7031 G_Typ := Entity (Indic);
7034 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
7035 and then Present (Generic_Parent_Type (Parent (G_Typ)))
7037 return Generic_Parent_Type (Parent (G_Typ));
7042 end Get_Generic_Parent_Type;
7044 ----------------------
7045 -- Types_Correspond --
7046 ----------------------
7048 function Types_Correspond
7049 (P_Type : Entity_Id;
7050 N_Type : Entity_Id) return Boolean
7052 Prev_Type : Entity_Id := Base_Type (P_Type);
7053 New_Type : Entity_Id := Base_Type (N_Type);
7056 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
7057 Prev_Type := Designated_Type (Prev_Type);
7060 if Ekind (New_Type) = E_Anonymous_Access_Type then
7061 New_Type := Designated_Type (New_Type);
7064 if Prev_Type = New_Type then
7067 elsif not Is_Class_Wide_Type (New_Type) then
7068 while Etype (New_Type) /= New_Type loop
7069 New_Type := Etype (New_Type);
7070 if New_Type = Prev_Type then
7076 end Types_Correspond;
7078 -- Start of processing for Is_Non_Overriding_Operation
7081 -- In the case where both operations are implicit derived subprograms
7082 -- then neither overrides the other. This can only occur in certain
7083 -- obscure cases (e.g., derivation from homographs created in a generic
7086 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
7089 elsif Ekind (Current_Scope) = E_Package
7090 and then Is_Generic_Instance (Current_Scope)
7091 and then In_Private_Part (Current_Scope)
7092 and then Comes_From_Source (New_E)
7094 -- We examine the formals and result subtype of the inherited
7095 -- operation, to determine whether their type is derived from (the
7096 -- instance of) a generic type.
7098 Formal := First_Formal (Prev_E);
7099 while Present (Formal) loop
7100 F_Typ := Base_Type (Etype (Formal));
7102 if Ekind (F_Typ) = E_Anonymous_Access_Type then
7103 F_Typ := Designated_Type (F_Typ);
7106 G_Typ := Get_Generic_Parent_Type (F_Typ);
7108 Next_Formal (Formal);
7111 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
7112 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
7119 -- If the generic type is a private type, then the original operation
7120 -- was not overriding in the generic, because there was no primitive
7121 -- operation to override.
7123 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
7124 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
7125 N_Formal_Private_Type_Definition
7129 -- The generic parent type is the ancestor of a formal derived
7130 -- type declaration. We need to check whether it has a primitive
7131 -- operation that should be overridden by New_E in the generic.
7135 P_Formal : Entity_Id;
7136 N_Formal : Entity_Id;
7140 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
7143 while Present (Prim_Elt) loop
7144 P_Prim := Node (Prim_Elt);
7146 if Chars (P_Prim) = Chars (New_E)
7147 and then Ekind (P_Prim) = Ekind (New_E)
7149 P_Formal := First_Formal (P_Prim);
7150 N_Formal := First_Formal (New_E);
7151 while Present (P_Formal) and then Present (N_Formal) loop
7152 P_Typ := Etype (P_Formal);
7153 N_Typ := Etype (N_Formal);
7155 if not Types_Correspond (P_Typ, N_Typ) then
7159 Next_Entity (P_Formal);
7160 Next_Entity (N_Formal);
7163 -- Found a matching primitive operation belonging to the
7164 -- formal ancestor type, so the new subprogram is
7168 and then No (N_Formal)
7169 and then (Ekind (New_E) /= E_Function
7172 (Etype (P_Prim), Etype (New_E)))
7178 Next_Elmt (Prim_Elt);
7181 -- If no match found, then the new subprogram does not
7182 -- override in the generic (nor in the instance).
7190 end Is_Non_Overriding_Operation;
7192 -------------------------------------
7193 -- List_Inherited_Pre_Post_Aspects --
7194 -------------------------------------
7196 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
7198 if Opt.List_Inherited_Aspects
7199 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
7202 Inherited : constant Subprogram_List :=
7203 Inherited_Subprograms (E);
7207 for J in Inherited'Range loop
7208 P := Spec_PPC_List (Inherited (J));
7209 while Present (P) loop
7210 Error_Msg_Sloc := Sloc (P);
7212 if Class_Present (P) and then not Split_PPC (P) then
7213 if Pragma_Name (P) = Name_Precondition then
7215 ("?info: & inherits `Pre''Class` aspect from #", E);
7218 ("?info: & inherits `Post''Class` aspect from #", E);
7222 P := Next_Pragma (P);
7227 end List_Inherited_Pre_Post_Aspects;
7229 ------------------------------
7230 -- Make_Inequality_Operator --
7231 ------------------------------
7233 -- S is the defining identifier of an equality operator. We build a
7234 -- subprogram declaration with the right signature. This operation is
7235 -- intrinsic, because it is always expanded as the negation of the
7236 -- call to the equality function.
7238 procedure Make_Inequality_Operator (S : Entity_Id) is
7239 Loc : constant Source_Ptr := Sloc (S);
7242 Op_Name : Entity_Id;
7244 FF : constant Entity_Id := First_Formal (S);
7245 NF : constant Entity_Id := Next_Formal (FF);
7248 -- Check that equality was properly defined, ignore call if not
7255 A : constant Entity_Id :=
7256 Make_Defining_Identifier (Sloc (FF),
7257 Chars => Chars (FF));
7259 B : constant Entity_Id :=
7260 Make_Defining_Identifier (Sloc (NF),
7261 Chars => Chars (NF));
7264 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
7266 Formals := New_List (
7267 Make_Parameter_Specification (Loc,
7268 Defining_Identifier => A,
7270 New_Reference_To (Etype (First_Formal (S)),
7271 Sloc (Etype (First_Formal (S))))),
7273 Make_Parameter_Specification (Loc,
7274 Defining_Identifier => B,
7276 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
7277 Sloc (Etype (Next_Formal (First_Formal (S)))))));
7280 Make_Subprogram_Declaration (Loc,
7282 Make_Function_Specification (Loc,
7283 Defining_Unit_Name => Op_Name,
7284 Parameter_Specifications => Formals,
7285 Result_Definition =>
7286 New_Reference_To (Standard_Boolean, Loc)));
7288 -- Insert inequality right after equality if it is explicit or after
7289 -- the derived type when implicit. These entities are created only
7290 -- for visibility purposes, and eventually replaced in the course of
7291 -- expansion, so they do not need to be attached to the tree and seen
7292 -- by the back-end. Keeping them internal also avoids spurious
7293 -- freezing problems. The declaration is inserted in the tree for
7294 -- analysis, and removed afterwards. If the equality operator comes
7295 -- from an explicit declaration, attach the inequality immediately
7296 -- after. Else the equality is inherited from a derived type
7297 -- declaration, so insert inequality after that declaration.
7299 if No (Alias (S)) then
7300 Insert_After (Unit_Declaration_Node (S), Decl);
7301 elsif Is_List_Member (Parent (S)) then
7302 Insert_After (Parent (S), Decl);
7304 Insert_After (Parent (Etype (First_Formal (S))), Decl);
7307 Mark_Rewrite_Insertion (Decl);
7308 Set_Is_Intrinsic_Subprogram (Op_Name);
7311 Set_Has_Completion (Op_Name);
7312 Set_Corresponding_Equality (Op_Name, S);
7313 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
7315 end Make_Inequality_Operator;
7317 ----------------------
7318 -- May_Need_Actuals --
7319 ----------------------
7321 procedure May_Need_Actuals (Fun : Entity_Id) is
7326 F := First_Formal (Fun);
7328 while Present (F) loop
7329 if No (Default_Value (F)) then
7337 Set_Needs_No_Actuals (Fun, B);
7338 end May_Need_Actuals;
7340 ---------------------
7341 -- Mode_Conformant --
7342 ---------------------
7344 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7347 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
7349 end Mode_Conformant;
7351 ---------------------------
7352 -- New_Overloaded_Entity --
7353 ---------------------------
7355 procedure New_Overloaded_Entity
7357 Derived_Type : Entity_Id := Empty)
7359 Overridden_Subp : Entity_Id := Empty;
7360 -- Set if the current scope has an operation that is type-conformant
7361 -- with S, and becomes hidden by S.
7363 Is_Primitive_Subp : Boolean;
7364 -- Set to True if the new subprogram is primitive
7367 -- Entity that S overrides
7369 Prev_Vis : Entity_Id := Empty;
7370 -- Predecessor of E in Homonym chain
7372 procedure Check_For_Primitive_Subprogram
7373 (Is_Primitive : out Boolean;
7374 Is_Overriding : Boolean := False);
7375 -- If the subprogram being analyzed is a primitive operation of the type
7376 -- of a formal or result, set the Has_Primitive_Operations flag on the
7377 -- type, and set Is_Primitive to True (otherwise set to False). Set the
7378 -- corresponding flag on the entity itself for later use.
7380 procedure Check_Synchronized_Overriding
7381 (Def_Id : Entity_Id;
7382 Overridden_Subp : out Entity_Id);
7383 -- First determine if Def_Id is an entry or a subprogram either defined
7384 -- in the scope of a task or protected type, or is a primitive of such
7385 -- a type. Check whether Def_Id overrides a subprogram of an interface
7386 -- implemented by the synchronized type, return the overridden entity
7389 function Is_Private_Declaration (E : Entity_Id) return Boolean;
7390 -- Check that E is declared in the private part of the current package,
7391 -- or in the package body, where it may hide a previous declaration.
7392 -- We can't use In_Private_Part by itself because this flag is also
7393 -- set when freezing entities, so we must examine the place of the
7394 -- declaration in the tree, and recognize wrapper packages as well.
7396 function Is_Overriding_Alias
7398 New_E : Entity_Id) return Boolean;
7399 -- Check whether new subprogram and old subprogram are both inherited
7400 -- from subprograms that have distinct dispatch table entries. This can
7401 -- occur with derivations from instances with accidental homonyms.
7402 -- The function is conservative given that the converse is only true
7403 -- within instances that contain accidental overloadings.
7405 ------------------------------------
7406 -- Check_For_Primitive_Subprogram --
7407 ------------------------------------
7409 procedure Check_For_Primitive_Subprogram
7410 (Is_Primitive : out Boolean;
7411 Is_Overriding : Boolean := False)
7417 function Visible_Part_Type (T : Entity_Id) return Boolean;
7418 -- Returns true if T is declared in the visible part of the current
7419 -- package scope; otherwise returns false. Assumes that T is declared
7422 procedure Check_Private_Overriding (T : Entity_Id);
7423 -- Checks that if a primitive abstract subprogram of a visible
7424 -- abstract type is declared in a private part, then it must override
7425 -- an abstract subprogram declared in the visible part. Also checks
7426 -- that if a primitive function with a controlling result is declared
7427 -- in a private part, then it must override a function declared in
7428 -- the visible part.
7430 ------------------------------
7431 -- Check_Private_Overriding --
7432 ------------------------------
7434 procedure Check_Private_Overriding (T : Entity_Id) is
7436 if Is_Package_Or_Generic_Package (Current_Scope)
7437 and then In_Private_Part (Current_Scope)
7438 and then Visible_Part_Type (T)
7439 and then not In_Instance
7441 if Is_Abstract_Type (T)
7442 and then Is_Abstract_Subprogram (S)
7443 and then (not Is_Overriding
7444 or else not Is_Abstract_Subprogram (E))
7447 ("abstract subprograms must be visible "
7448 & "(RM 3.9.3(10))!", S);
7450 elsif Ekind (S) = E_Function
7451 and then not Is_Overriding
7453 if Is_Tagged_Type (T)
7454 and then T = Base_Type (Etype (S))
7457 ("private function with tagged result must"
7458 & " override visible-part function", S);
7460 ("\move subprogram to the visible part"
7461 & " (RM 3.9.3(10))", S);
7463 -- AI05-0073: extend this test to the case of a function
7464 -- with a controlling access result.
7466 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
7467 and then Is_Tagged_Type (Designated_Type (Etype (S)))
7469 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
7470 and then Ada_Version >= Ada_2012
7473 ("private function with controlling access result "
7474 & "must override visible-part function", S);
7476 ("\move subprogram to the visible part"
7477 & " (RM 3.9.3(10))", S);
7481 end Check_Private_Overriding;
7483 -----------------------
7484 -- Visible_Part_Type --
7485 -----------------------
7487 function Visible_Part_Type (T : Entity_Id) return Boolean is
7488 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7492 -- If the entity is a private type, then it must be declared in a
7495 if Ekind (T) in Private_Kind then
7499 -- Otherwise, we traverse the visible part looking for its
7500 -- corresponding declaration. We cannot use the declaration
7501 -- node directly because in the private part the entity of a
7502 -- private type is the one in the full view, which does not
7503 -- indicate that it is the completion of something visible.
7505 N := First (Visible_Declarations (Specification (P)));
7506 while Present (N) loop
7507 if Nkind (N) = N_Full_Type_Declaration
7508 and then Present (Defining_Identifier (N))
7509 and then T = Defining_Identifier (N)
7513 elsif Nkind_In (N, N_Private_Type_Declaration,
7514 N_Private_Extension_Declaration)
7515 and then Present (Defining_Identifier (N))
7516 and then T = Full_View (Defining_Identifier (N))
7525 end Visible_Part_Type;
7527 -- Start of processing for Check_For_Primitive_Subprogram
7530 Is_Primitive := False;
7532 if not Comes_From_Source (S) then
7535 -- If subprogram is at library level, it is not primitive operation
7537 elsif Current_Scope = Standard_Standard then
7540 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7541 and then not In_Package_Body (Current_Scope))
7542 or else Is_Overriding
7544 -- For function, check return type
7546 if Ekind (S) = E_Function then
7547 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7548 F_Typ := Designated_Type (Etype (S));
7553 B_Typ := Base_Type (F_Typ);
7555 if Scope (B_Typ) = Current_Scope
7556 and then not Is_Class_Wide_Type (B_Typ)
7557 and then not Is_Generic_Type (B_Typ)
7559 Is_Primitive := True;
7560 Set_Has_Primitive_Operations (B_Typ);
7561 Set_Is_Primitive (S);
7562 Check_Private_Overriding (B_Typ);
7566 -- For all subprograms, check formals
7568 Formal := First_Formal (S);
7569 while Present (Formal) loop
7570 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7571 F_Typ := Designated_Type (Etype (Formal));
7573 F_Typ := Etype (Formal);
7576 B_Typ := Base_Type (F_Typ);
7578 if Ekind (B_Typ) = E_Access_Subtype then
7579 B_Typ := Base_Type (B_Typ);
7582 if Scope (B_Typ) = Current_Scope
7583 and then not Is_Class_Wide_Type (B_Typ)
7584 and then not Is_Generic_Type (B_Typ)
7586 Is_Primitive := True;
7587 Set_Is_Primitive (S);
7588 Set_Has_Primitive_Operations (B_Typ);
7589 Check_Private_Overriding (B_Typ);
7592 Next_Formal (Formal);
7595 end Check_For_Primitive_Subprogram;
7597 -----------------------------------
7598 -- Check_Synchronized_Overriding --
7599 -----------------------------------
7601 procedure Check_Synchronized_Overriding
7602 (Def_Id : Entity_Id;
7603 Overridden_Subp : out Entity_Id)
7605 Ifaces_List : Elist_Id;
7609 function Matches_Prefixed_View_Profile
7610 (Prim_Params : List_Id;
7611 Iface_Params : List_Id) return Boolean;
7612 -- Determine whether a subprogram's parameter profile Prim_Params
7613 -- matches that of a potentially overridden interface subprogram
7614 -- Iface_Params. Also determine if the type of first parameter of
7615 -- Iface_Params is an implemented interface.
7617 -----------------------------------
7618 -- Matches_Prefixed_View_Profile --
7619 -----------------------------------
7621 function Matches_Prefixed_View_Profile
7622 (Prim_Params : List_Id;
7623 Iface_Params : List_Id) return Boolean
7625 Iface_Id : Entity_Id;
7626 Iface_Param : Node_Id;
7627 Iface_Typ : Entity_Id;
7628 Prim_Id : Entity_Id;
7629 Prim_Param : Node_Id;
7630 Prim_Typ : Entity_Id;
7632 function Is_Implemented
7633 (Ifaces_List : Elist_Id;
7634 Iface : Entity_Id) return Boolean;
7635 -- Determine if Iface is implemented by the current task or
7638 --------------------
7639 -- Is_Implemented --
7640 --------------------
7642 function Is_Implemented
7643 (Ifaces_List : Elist_Id;
7644 Iface : Entity_Id) return Boolean
7646 Iface_Elmt : Elmt_Id;
7649 Iface_Elmt := First_Elmt (Ifaces_List);
7650 while Present (Iface_Elmt) loop
7651 if Node (Iface_Elmt) = Iface then
7655 Next_Elmt (Iface_Elmt);
7661 -- Start of processing for Matches_Prefixed_View_Profile
7664 Iface_Param := First (Iface_Params);
7665 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7667 if Is_Access_Type (Iface_Typ) then
7668 Iface_Typ := Designated_Type (Iface_Typ);
7671 Prim_Param := First (Prim_Params);
7673 -- The first parameter of the potentially overridden subprogram
7674 -- must be an interface implemented by Prim.
7676 if not Is_Interface (Iface_Typ)
7677 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7682 -- The checks on the object parameters are done, move onto the
7683 -- rest of the parameters.
7685 if not In_Scope then
7686 Prim_Param := Next (Prim_Param);
7689 Iface_Param := Next (Iface_Param);
7690 while Present (Iface_Param) and then Present (Prim_Param) loop
7691 Iface_Id := Defining_Identifier (Iface_Param);
7692 Iface_Typ := Find_Parameter_Type (Iface_Param);
7694 Prim_Id := Defining_Identifier (Prim_Param);
7695 Prim_Typ := Find_Parameter_Type (Prim_Param);
7697 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7698 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7699 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7701 Iface_Typ := Designated_Type (Iface_Typ);
7702 Prim_Typ := Designated_Type (Prim_Typ);
7705 -- Case of multiple interface types inside a parameter profile
7707 -- (Obj_Param : in out Iface; ...; Param : Iface)
7709 -- If the interface type is implemented, then the matching type
7710 -- in the primitive should be the implementing record type.
7712 if Ekind (Iface_Typ) = E_Record_Type
7713 and then Is_Interface (Iface_Typ)
7714 and then Is_Implemented (Ifaces_List, Iface_Typ)
7716 if Prim_Typ /= Typ then
7720 -- The two parameters must be both mode and subtype conformant
7722 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7724 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7733 -- One of the two lists contains more parameters than the other
7735 if Present (Iface_Param) or else Present (Prim_Param) then
7740 end Matches_Prefixed_View_Profile;
7742 -- Start of processing for Check_Synchronized_Overriding
7745 Overridden_Subp := Empty;
7747 -- Def_Id must be an entry or a subprogram. We should skip predefined
7748 -- primitives internally generated by the frontend; however at this
7749 -- stage predefined primitives are still not fully decorated. As a
7750 -- minor optimization we skip here internally generated subprograms.
7752 if (Ekind (Def_Id) /= E_Entry
7753 and then Ekind (Def_Id) /= E_Function
7754 and then Ekind (Def_Id) /= E_Procedure)
7755 or else not Comes_From_Source (Def_Id)
7760 -- Search for the concurrent declaration since it contains the list
7761 -- of all implemented interfaces. In this case, the subprogram is
7762 -- declared within the scope of a protected or a task type.
7764 if Present (Scope (Def_Id))
7765 and then Is_Concurrent_Type (Scope (Def_Id))
7766 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7768 Typ := Scope (Def_Id);
7771 -- The enclosing scope is not a synchronized type and the subprogram
7774 elsif No (First_Formal (Def_Id)) then
7777 -- The subprogram has formals and hence it may be a primitive of a
7781 Typ := Etype (First_Formal (Def_Id));
7783 if Is_Access_Type (Typ) then
7784 Typ := Directly_Designated_Type (Typ);
7787 if Is_Concurrent_Type (Typ)
7788 and then not Is_Generic_Actual_Type (Typ)
7792 -- This case occurs when the concurrent type is declared within
7793 -- a generic unit. As a result the corresponding record has been
7794 -- built and used as the type of the first formal, we just have
7795 -- to retrieve the corresponding concurrent type.
7797 elsif Is_Concurrent_Record_Type (Typ)
7798 and then Present (Corresponding_Concurrent_Type (Typ))
7800 Typ := Corresponding_Concurrent_Type (Typ);
7808 -- There is no overriding to check if is an inherited operation in a
7809 -- type derivation on for a generic actual.
7811 Collect_Interfaces (Typ, Ifaces_List);
7813 if Is_Empty_Elmt_List (Ifaces_List) then
7817 -- Determine whether entry or subprogram Def_Id overrides a primitive
7818 -- operation that belongs to one of the interfaces in Ifaces_List.
7821 Candidate : Entity_Id := Empty;
7822 Hom : Entity_Id := Empty;
7823 Iface_Typ : Entity_Id;
7824 Subp : Entity_Id := Empty;
7827 -- Traverse the homonym chain, looking for a potentially
7828 -- overridden subprogram that belongs to an implemented
7831 Hom := Current_Entity_In_Scope (Def_Id);
7832 while Present (Hom) loop
7836 or else not Is_Overloadable (Subp)
7837 or else not Is_Primitive (Subp)
7838 or else not Is_Dispatching_Operation (Subp)
7839 or else not Present (Find_Dispatching_Type (Subp))
7840 or else not Is_Interface (Find_Dispatching_Type (Subp))
7844 -- Entries and procedures can override abstract or null
7845 -- interface procedures.
7847 elsif (Ekind (Def_Id) = E_Procedure
7848 or else Ekind (Def_Id) = E_Entry)
7849 and then Ekind (Subp) = E_Procedure
7850 and then Matches_Prefixed_View_Profile
7851 (Parameter_Specifications (Parent (Def_Id)),
7852 Parameter_Specifications (Parent (Subp)))
7856 -- For an overridden subprogram Subp, check whether the mode
7857 -- of its first parameter is correct depending on the kind
7858 -- of synchronized type.
7861 Formal : constant Node_Id := First_Formal (Candidate);
7864 -- In order for an entry or a protected procedure to
7865 -- override, the first parameter of the overridden
7866 -- routine must be of mode "out", "in out" or
7867 -- access-to-variable.
7869 if (Ekind (Candidate) = E_Entry
7870 or else Ekind (Candidate) = E_Procedure)
7871 and then Is_Protected_Type (Typ)
7872 and then Ekind (Formal) /= E_In_Out_Parameter
7873 and then Ekind (Formal) /= E_Out_Parameter
7874 and then Nkind (Parameter_Type (Parent (Formal)))
7875 /= N_Access_Definition
7879 -- All other cases are OK since a task entry or routine
7880 -- does not have a restriction on the mode of the first
7881 -- parameter of the overridden interface routine.
7884 Overridden_Subp := Candidate;
7889 -- Functions can override abstract interface functions
7891 elsif Ekind (Def_Id) = E_Function
7892 and then Ekind (Subp) = E_Function
7893 and then Matches_Prefixed_View_Profile
7894 (Parameter_Specifications (Parent (Def_Id)),
7895 Parameter_Specifications (Parent (Subp)))
7896 and then Etype (Result_Definition (Parent (Def_Id))) =
7897 Etype (Result_Definition (Parent (Subp)))
7899 Overridden_Subp := Subp;
7903 Hom := Homonym (Hom);
7906 -- After examining all candidates for overriding, we are left with
7907 -- the best match which is a mode incompatible interface routine.
7908 -- Do not emit an error if the Expander is active since this error
7909 -- will be detected later on after all concurrent types are
7910 -- expanded and all wrappers are built. This check is meant for
7911 -- spec-only compilations.
7913 if Present (Candidate) and then not Expander_Active then
7915 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7917 -- Def_Id is primitive of a protected type, declared inside the
7918 -- type, and the candidate is primitive of a limited or
7919 -- synchronized interface.
7922 and then Is_Protected_Type (Typ)
7924 (Is_Limited_Interface (Iface_Typ)
7925 or else Is_Protected_Interface (Iface_Typ)
7926 or else Is_Synchronized_Interface (Iface_Typ)
7927 or else Is_Task_Interface (Iface_Typ))
7930 ("first formal of & must be of mode `OUT`, `IN OUT`"
7931 & " or access-to-variable", Typ, Candidate);
7933 ("\in order to be overridden by protected procedure or "
7934 & "entry (RM 9.4(11.9/2))", Typ);
7938 Overridden_Subp := Candidate;
7941 end Check_Synchronized_Overriding;
7943 ----------------------------
7944 -- Is_Private_Declaration --
7945 ----------------------------
7947 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7948 Priv_Decls : List_Id;
7949 Decl : constant Node_Id := Unit_Declaration_Node (E);
7952 if Is_Package_Or_Generic_Package (Current_Scope)
7953 and then In_Private_Part (Current_Scope)
7956 Private_Declarations (
7957 Specification (Unit_Declaration_Node (Current_Scope)));
7959 return In_Package_Body (Current_Scope)
7961 (Is_List_Member (Decl)
7962 and then List_Containing (Decl) = Priv_Decls)
7963 or else (Nkind (Parent (Decl)) = N_Package_Specification
7966 (Defining_Entity (Parent (Decl)))
7967 and then List_Containing (Parent (Parent (Decl)))
7972 end Is_Private_Declaration;
7974 --------------------------
7975 -- Is_Overriding_Alias --
7976 --------------------------
7978 function Is_Overriding_Alias
7980 New_E : Entity_Id) return Boolean
7982 AO : constant Entity_Id := Alias (Old_E);
7983 AN : constant Entity_Id := Alias (New_E);
7986 return Scope (AO) /= Scope (AN)
7987 or else No (DTC_Entity (AO))
7988 or else No (DTC_Entity (AN))
7989 or else DT_Position (AO) = DT_Position (AN);
7990 end Is_Overriding_Alias;
7992 -- Start of processing for New_Overloaded_Entity
7995 -- We need to look for an entity that S may override. This must be a
7996 -- homonym in the current scope, so we look for the first homonym of
7997 -- S in the current scope as the starting point for the search.
7999 E := Current_Entity_In_Scope (S);
8001 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
8002 -- They are directly added to the list of primitive operations of
8003 -- Derived_Type, unless this is a rederivation in the private part
8004 -- of an operation that was already derived in the visible part of
8005 -- the current package.
8007 if Ada_Version >= Ada_2005
8008 and then Present (Derived_Type)
8009 and then Present (Alias (S))
8010 and then Is_Dispatching_Operation (Alias (S))
8011 and then Present (Find_Dispatching_Type (Alias (S)))
8012 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
8014 -- For private types, when the full-view is processed we propagate to
8015 -- the full view the non-overridden entities whose attribute "alias"
8016 -- references an interface primitive. These entities were added by
8017 -- Derive_Subprograms to ensure that interface primitives are
8020 -- Inside_Freeze_Actions is non zero when S corresponds with an
8021 -- internal entity that links an interface primitive with its
8022 -- covering primitive through attribute Interface_Alias (see
8023 -- Add_Internal_Interface_Entities).
8025 if Inside_Freezing_Actions = 0
8026 and then Is_Package_Or_Generic_Package (Current_Scope)
8027 and then In_Private_Part (Current_Scope)
8028 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
8029 and then Nkind (Parent (S)) = N_Full_Type_Declaration
8030 and then Full_View (Defining_Identifier (Parent (E)))
8031 = Defining_Identifier (Parent (S))
8032 and then Alias (E) = Alias (S)
8034 Check_Operation_From_Private_View (S, E);
8035 Set_Is_Dispatching_Operation (S);
8040 Enter_Overloaded_Entity (S);
8041 Check_Dispatching_Operation (S, Empty);
8042 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8048 -- If there is no homonym then this is definitely not overriding
8051 Enter_Overloaded_Entity (S);
8052 Check_Dispatching_Operation (S, Empty);
8053 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8055 -- If subprogram has an explicit declaration, check whether it
8056 -- has an overriding indicator.
8058 if Comes_From_Source (S) then
8059 Check_Synchronized_Overriding (S, Overridden_Subp);
8061 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
8062 -- it may have overridden some hidden inherited primitive. Update
8063 -- Overridden_Subp to avoid spurious errors when checking the
8064 -- overriding indicator.
8066 if Ada_Version >= Ada_2012
8067 and then No (Overridden_Subp)
8068 and then Is_Dispatching_Operation (S)
8069 and then Present (Overridden_Operation (S))
8071 Overridden_Subp := Overridden_Operation (S);
8074 Check_Overriding_Indicator
8075 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8078 -- If there is a homonym that is not overloadable, then we have an
8079 -- error, except for the special cases checked explicitly below.
8081 elsif not Is_Overloadable (E) then
8083 -- Check for spurious conflict produced by a subprogram that has the
8084 -- same name as that of the enclosing generic package. The conflict
8085 -- occurs within an instance, between the subprogram and the renaming
8086 -- declaration for the package. After the subprogram, the package
8087 -- renaming declaration becomes hidden.
8089 if Ekind (E) = E_Package
8090 and then Present (Renamed_Object (E))
8091 and then Renamed_Object (E) = Current_Scope
8092 and then Nkind (Parent (Renamed_Object (E))) =
8093 N_Package_Specification
8094 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
8097 Set_Is_Immediately_Visible (E, False);
8098 Enter_Overloaded_Entity (S);
8099 Set_Homonym (S, Homonym (E));
8100 Check_Dispatching_Operation (S, Empty);
8101 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
8103 -- If the subprogram is implicit it is hidden by the previous
8104 -- declaration. However if it is dispatching, it must appear in the
8105 -- dispatch table anyway, because it can be dispatched to even if it
8106 -- cannot be called directly.
8108 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
8109 Set_Scope (S, Current_Scope);
8111 if Is_Dispatching_Operation (Alias (S)) then
8112 Check_Dispatching_Operation (S, Empty);
8118 Error_Msg_Sloc := Sloc (E);
8120 -- Generate message, with useful additional warning if in generic
8122 if Is_Generic_Unit (E) then
8123 Error_Msg_N ("previous generic unit cannot be overloaded", S);
8124 Error_Msg_N ("\& conflicts with declaration#", S);
8126 Error_Msg_N ("& conflicts with declaration#", S);
8132 -- E exists and is overloadable
8135 Check_Synchronized_Overriding (S, Overridden_Subp);
8137 -- Loop through E and its homonyms to determine if any of them is
8138 -- the candidate for overriding by S.
8140 while Present (E) loop
8142 -- Definitely not interesting if not in the current scope
8144 if Scope (E) /= Current_Scope then
8147 -- Check if we have type conformance
8149 elsif Type_Conformant (E, S) then
8151 -- If the old and new entities have the same profile and one
8152 -- is not the body of the other, then this is an error, unless
8153 -- one of them is implicitly declared.
8155 -- There are some cases when both can be implicit, for example
8156 -- when both a literal and a function that overrides it are
8157 -- inherited in a derivation, or when an inherited operation
8158 -- of a tagged full type overrides the inherited operation of
8159 -- a private extension. Ada 83 had a special rule for the
8160 -- literal case. In Ada95, the later implicit operation hides
8161 -- the former, and the literal is always the former. In the
8162 -- odd case where both are derived operations declared at the
8163 -- same point, both operations should be declared, and in that
8164 -- case we bypass the following test and proceed to the next
8165 -- part. This can only occur for certain obscure cases in
8166 -- instances, when an operation on a type derived from a formal
8167 -- private type does not override a homograph inherited from
8168 -- the actual. In subsequent derivations of such a type, the
8169 -- DT positions of these operations remain distinct, if they
8172 if Present (Alias (S))
8173 and then (No (Alias (E))
8174 or else Comes_From_Source (E)
8175 or else Is_Abstract_Subprogram (S)
8177 (Is_Dispatching_Operation (E)
8178 and then Is_Overriding_Alias (E, S)))
8179 and then Ekind (E) /= E_Enumeration_Literal
8181 -- When an derived operation is overloaded it may be due to
8182 -- the fact that the full view of a private extension
8183 -- re-inherits. It has to be dealt with.
8185 if Is_Package_Or_Generic_Package (Current_Scope)
8186 and then In_Private_Part (Current_Scope)
8188 Check_Operation_From_Private_View (S, E);
8191 -- In any case the implicit operation remains hidden by the
8192 -- existing declaration, which is overriding. Indicate that
8193 -- E overrides the operation from which S is inherited.
8195 if Present (Alias (S)) then
8196 Set_Overridden_Operation (E, Alias (S));
8198 Set_Overridden_Operation (E, S);
8201 if Comes_From_Source (E) then
8202 Check_Overriding_Indicator (E, S, Is_Primitive => False);
8207 -- Within an instance, the renaming declarations for actual
8208 -- subprograms may become ambiguous, but they do not hide each
8211 elsif Ekind (E) /= E_Entry
8212 and then not Comes_From_Source (E)
8213 and then not Is_Generic_Instance (E)
8214 and then (Present (Alias (E))
8215 or else Is_Intrinsic_Subprogram (E))
8216 and then (not In_Instance
8217 or else No (Parent (E))
8218 or else Nkind (Unit_Declaration_Node (E)) /=
8219 N_Subprogram_Renaming_Declaration)
8221 -- A subprogram child unit is not allowed to override an
8222 -- inherited subprogram (10.1.1(20)).
8224 if Is_Child_Unit (S) then
8226 ("child unit overrides inherited subprogram in parent",
8231 if Is_Non_Overriding_Operation (E, S) then
8232 Enter_Overloaded_Entity (S);
8234 if No (Derived_Type)
8235 or else Is_Tagged_Type (Derived_Type)
8237 Check_Dispatching_Operation (S, Empty);
8243 -- E is a derived operation or an internal operator which
8244 -- is being overridden. Remove E from further visibility.
8245 -- Furthermore, if E is a dispatching operation, it must be
8246 -- replaced in the list of primitive operations of its type
8247 -- (see Override_Dispatching_Operation).
8249 Overridden_Subp := E;
8255 Prev := First_Entity (Current_Scope);
8256 while Present (Prev)
8257 and then Next_Entity (Prev) /= E
8262 -- It is possible for E to be in the current scope and
8263 -- yet not in the entity chain. This can only occur in a
8264 -- generic context where E is an implicit concatenation
8265 -- in the formal part, because in a generic body the
8266 -- entity chain starts with the formals.
8269 (Present (Prev) or else Chars (E) = Name_Op_Concat);
8271 -- E must be removed both from the entity_list of the
8272 -- current scope, and from the visibility chain
8274 if Debug_Flag_E then
8275 Write_Str ("Override implicit operation ");
8276 Write_Int (Int (E));
8280 -- If E is a predefined concatenation, it stands for four
8281 -- different operations. As a result, a single explicit
8282 -- declaration does not hide it. In a possible ambiguous
8283 -- situation, Disambiguate chooses the user-defined op,
8284 -- so it is correct to retain the previous internal one.
8286 if Chars (E) /= Name_Op_Concat
8287 or else Ekind (E) /= E_Operator
8289 -- For nondispatching derived operations that are
8290 -- overridden by a subprogram declared in the private
8291 -- part of a package, we retain the derived subprogram
8292 -- but mark it as not immediately visible. If the
8293 -- derived operation was declared in the visible part
8294 -- then this ensures that it will still be visible
8295 -- outside the package with the proper signature
8296 -- (calls from outside must also be directed to this
8297 -- version rather than the overriding one, unlike the
8298 -- dispatching case). Calls from inside the package
8299 -- will still resolve to the overriding subprogram
8300 -- since the derived one is marked as not visible
8301 -- within the package.
8303 -- If the private operation is dispatching, we achieve
8304 -- the overriding by keeping the implicit operation
8305 -- but setting its alias to be the overriding one. In
8306 -- this fashion the proper body is executed in all
8307 -- cases, but the original signature is used outside
8310 -- If the overriding is not in the private part, we
8311 -- remove the implicit operation altogether.
8313 if Is_Private_Declaration (S) then
8314 if not Is_Dispatching_Operation (E) then
8315 Set_Is_Immediately_Visible (E, False);
8317 -- Work done in Override_Dispatching_Operation,
8318 -- so nothing else need to be done here.
8324 -- Find predecessor of E in Homonym chain
8326 if E = Current_Entity (E) then
8329 Prev_Vis := Current_Entity (E);
8330 while Homonym (Prev_Vis) /= E loop
8331 Prev_Vis := Homonym (Prev_Vis);
8335 if Prev_Vis /= Empty then
8337 -- Skip E in the visibility chain
8339 Set_Homonym (Prev_Vis, Homonym (E));
8342 Set_Name_Entity_Id (Chars (E), Homonym (E));
8345 Set_Next_Entity (Prev, Next_Entity (E));
8347 if No (Next_Entity (Prev)) then
8348 Set_Last_Entity (Current_Scope, Prev);
8353 Enter_Overloaded_Entity (S);
8355 -- For entities generated by Derive_Subprograms the
8356 -- overridden operation is the inherited primitive
8357 -- (which is available through the attribute alias).
8359 if not (Comes_From_Source (E))
8360 and then Is_Dispatching_Operation (E)
8361 and then Find_Dispatching_Type (E) =
8362 Find_Dispatching_Type (S)
8363 and then Present (Alias (E))
8364 and then Comes_From_Source (Alias (E))
8366 Set_Overridden_Operation (S, Alias (E));
8368 Set_Overridden_Operation (S, E);
8371 Check_Overriding_Indicator (S, E, Is_Primitive => True);
8373 -- If S is a user-defined subprogram or a null procedure
8374 -- expanded to override an inherited null procedure, or a
8375 -- predefined dispatching primitive then indicate that E
8376 -- overrides the operation from which S is inherited.
8378 if Comes_From_Source (S)
8380 (Present (Parent (S))
8382 Nkind (Parent (S)) = N_Procedure_Specification
8384 Null_Present (Parent (S)))
8386 (Present (Alias (E))
8388 Is_Predefined_Dispatching_Operation (Alias (E)))
8390 if Present (Alias (E)) then
8391 Set_Overridden_Operation (S, Alias (E));
8395 if Is_Dispatching_Operation (E) then
8397 -- An overriding dispatching subprogram inherits the
8398 -- convention of the overridden subprogram (AI-117).
8400 Set_Convention (S, Convention (E));
8401 Check_Dispatching_Operation (S, E);
8404 Check_Dispatching_Operation (S, Empty);
8407 Check_For_Primitive_Subprogram
8408 (Is_Primitive_Subp, Is_Overriding => True);
8409 goto Check_Inequality;
8412 -- Apparent redeclarations in instances can occur when two
8413 -- formal types get the same actual type. The subprograms in
8414 -- in the instance are legal, even if not callable from the
8415 -- outside. Calls from within are disambiguated elsewhere.
8416 -- For dispatching operations in the visible part, the usual
8417 -- rules apply, and operations with the same profile are not
8420 elsif (In_Instance_Visible_Part
8421 and then not Is_Dispatching_Operation (E))
8422 or else In_Instance_Not_Visible
8426 -- Here we have a real error (identical profile)
8429 Error_Msg_Sloc := Sloc (E);
8431 -- Avoid cascaded errors if the entity appears in
8432 -- subsequent calls.
8434 Set_Scope (S, Current_Scope);
8436 -- Generate error, with extra useful warning for the case
8437 -- of a generic instance with no completion.
8439 if Is_Generic_Instance (S)
8440 and then not Has_Completion (E)
8443 ("instantiation cannot provide body for&", S);
8444 Error_Msg_N ("\& conflicts with declaration#", S);
8446 Error_Msg_N ("& conflicts with declaration#", S);
8453 -- If one subprogram has an access parameter and the other
8454 -- a parameter of an access type, calls to either might be
8455 -- ambiguous. Verify that parameters match except for the
8456 -- access parameter.
8458 if May_Hide_Profile then
8464 F1 := First_Formal (S);
8465 F2 := First_Formal (E);
8466 while Present (F1) and then Present (F2) loop
8467 if Is_Access_Type (Etype (F1)) then
8468 if not Is_Access_Type (Etype (F2))
8469 or else not Conforming_Types
8470 (Designated_Type (Etype (F1)),
8471 Designated_Type (Etype (F2)),
8474 May_Hide_Profile := False;
8478 not Conforming_Types
8479 (Etype (F1), Etype (F2), Type_Conformant)
8481 May_Hide_Profile := False;
8492 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
8501 -- On exit, we know that S is a new entity
8503 Enter_Overloaded_Entity (S);
8504 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8505 Check_Overriding_Indicator
8506 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8508 -- Overloading is not allowed in SPARK or ALFA
8510 Error_Msg_Sloc := Sloc (Homonym (S));
8511 Check_Formal_Restriction ("overloading not allowed with entity#", S);
8513 -- If S is a derived operation for an untagged type then by
8514 -- definition it's not a dispatching operation (even if the parent
8515 -- operation was dispatching), so we don't call
8516 -- Check_Dispatching_Operation in that case.
8518 if No (Derived_Type)
8519 or else Is_Tagged_Type (Derived_Type)
8521 Check_Dispatching_Operation (S, Empty);
8525 -- If this is a user-defined equality operator that is not a derived
8526 -- subprogram, create the corresponding inequality. If the operation is
8527 -- dispatching, the expansion is done elsewhere, and we do not create
8528 -- an explicit inequality operation.
8530 <<Check_Inequality>>
8531 if Chars (S) = Name_Op_Eq
8532 and then Etype (S) = Standard_Boolean
8533 and then Present (Parent (S))
8534 and then not Is_Dispatching_Operation (S)
8536 Make_Inequality_Operator (S);
8538 if Ada_Version >= Ada_2012 then
8539 Check_Untagged_Equality (S);
8542 end New_Overloaded_Entity;
8544 ---------------------
8545 -- Process_Formals --
8546 ---------------------
8548 procedure Process_Formals
8550 Related_Nod : Node_Id)
8552 Param_Spec : Node_Id;
8554 Formal_Type : Entity_Id;
8558 Num_Out_Params : Nat := 0;
8559 First_Out_Param : Entity_Id := Empty;
8560 -- Used for setting Is_Only_Out_Parameter
8562 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8563 -- Determine whether an access type designates a type coming from a
8566 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8567 -- Check whether the default has a class-wide type. After analysis the
8568 -- default has the type of the formal, so we must also check explicitly
8569 -- for an access attribute.
8571 -------------------------------
8572 -- Designates_From_With_Type --
8573 -------------------------------
8575 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8576 Desig : Entity_Id := Typ;
8579 if Is_Access_Type (Desig) then
8580 Desig := Directly_Designated_Type (Desig);
8583 if Is_Class_Wide_Type (Desig) then
8584 Desig := Root_Type (Desig);
8588 Ekind (Desig) = E_Incomplete_Type
8589 and then From_With_Type (Desig);
8590 end Designates_From_With_Type;
8592 ---------------------------
8593 -- Is_Class_Wide_Default --
8594 ---------------------------
8596 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8598 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8599 or else (Nkind (D) = N_Attribute_Reference
8600 and then Attribute_Name (D) = Name_Access
8601 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8602 end Is_Class_Wide_Default;
8604 -- Start of processing for Process_Formals
8607 -- In order to prevent premature use of the formals in the same formal
8608 -- part, the Ekind is left undefined until all default expressions are
8609 -- analyzed. The Ekind is established in a separate loop at the end.
8611 Param_Spec := First (T);
8612 while Present (Param_Spec) loop
8613 Formal := Defining_Identifier (Param_Spec);
8614 Set_Never_Set_In_Source (Formal, True);
8615 Enter_Name (Formal);
8617 -- Case of ordinary parameters
8619 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8620 Find_Type (Parameter_Type (Param_Spec));
8621 Ptype := Parameter_Type (Param_Spec);
8623 if Ptype = Error then
8627 Formal_Type := Entity (Ptype);
8629 if Is_Incomplete_Type (Formal_Type)
8631 (Is_Class_Wide_Type (Formal_Type)
8632 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8634 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8635 -- primitive operations, as long as their completion is
8636 -- in the same declarative part. If in the private part
8637 -- this means that the type cannot be a Taft-amendment type.
8638 -- Check is done on package exit. For access to subprograms,
8639 -- the use is legal for Taft-amendment types.
8641 if Is_Tagged_Type (Formal_Type) then
8642 if Ekind (Scope (Current_Scope)) = E_Package
8643 and then In_Private_Part (Scope (Current_Scope))
8644 and then not From_With_Type (Formal_Type)
8645 and then not Is_Class_Wide_Type (Formal_Type)
8648 (Parent (T), N_Access_Function_Definition,
8649 N_Access_Procedure_Definition)
8653 Private_Dependents (Base_Type (Formal_Type)));
8657 -- Special handling of Value_Type for CIL case
8659 elsif Is_Value_Type (Formal_Type) then
8662 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8663 N_Access_Procedure_Definition)
8666 -- AI05-0151: Tagged incomplete types are allowed in all
8667 -- formal parts. Untagged incomplete types are not allowed
8670 if Ada_Version >= Ada_2012 then
8671 if Is_Tagged_Type (Formal_Type) then
8674 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
8679 ("invalid use of untagged incomplete type&",
8680 Ptype, Formal_Type);
8685 ("invalid use of incomplete type&",
8686 Param_Spec, Formal_Type);
8688 -- Further checks on the legality of incomplete types
8689 -- in formal parts are delayed until the freeze point
8690 -- of the enclosing subprogram or access to subprogram.
8694 elsif Ekind (Formal_Type) = E_Void then
8696 ("premature use of&",
8697 Parameter_Type (Param_Spec), Formal_Type);
8700 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8701 -- declaration corresponding to the null-excluding type of the
8702 -- formal in the enclosing scope. Finally, replace the parameter
8703 -- type of the formal with the internal subtype.
8705 if Ada_Version >= Ada_2005
8706 and then Null_Exclusion_Present (Param_Spec)
8708 if not Is_Access_Type (Formal_Type) then
8710 ("`NOT NULL` allowed only for an access type", Param_Spec);
8713 if Can_Never_Be_Null (Formal_Type)
8714 and then Comes_From_Source (Related_Nod)
8717 ("`NOT NULL` not allowed (& already excludes null)",
8718 Param_Spec, Formal_Type);
8722 Create_Null_Excluding_Itype
8724 Related_Nod => Related_Nod,
8725 Scope_Id => Scope (Current_Scope));
8727 -- If the designated type of the itype is an itype we
8728 -- decorate it with the Has_Delayed_Freeze attribute to
8729 -- avoid problems with the backend.
8732 -- type T is access procedure;
8733 -- procedure Op (O : not null T);
8735 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8736 Set_Has_Delayed_Freeze (Formal_Type);
8741 -- An access formal type
8745 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8747 -- No need to continue if we already notified errors
8749 if not Present (Formal_Type) then
8753 -- Ada 2005 (AI-254)
8756 AD : constant Node_Id :=
8757 Access_To_Subprogram_Definition
8758 (Parameter_Type (Param_Spec));
8760 if Present (AD) and then Protected_Present (AD) then
8762 Replace_Anonymous_Access_To_Protected_Subprogram
8768 Set_Etype (Formal, Formal_Type);
8769 Default := Expression (Param_Spec);
8771 if Present (Default) then
8773 Check_Formal_Restriction
8774 ("default expression is not allowed", Default);
8776 if Out_Present (Param_Spec) then
8778 ("default initialization only allowed for IN parameters",
8782 -- Do the special preanalysis of the expression (see section on
8783 -- "Handling of Default Expressions" in the spec of package Sem).
8785 Preanalyze_Spec_Expression (Default, Formal_Type);
8787 -- An access to constant cannot be the default for
8788 -- an access parameter that is an access to variable.
8790 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8791 and then not Is_Access_Constant (Formal_Type)
8792 and then Is_Access_Type (Etype (Default))
8793 and then Is_Access_Constant (Etype (Default))
8796 ("formal that is access to variable cannot be initialized " &
8797 "with an access-to-constant expression", Default);
8800 -- Check that the designated type of an access parameter's default
8801 -- is not a class-wide type unless the parameter's designated type
8802 -- is also class-wide.
8804 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8805 and then not Designates_From_With_Type (Formal_Type)
8806 and then Is_Class_Wide_Default (Default)
8807 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8810 ("access to class-wide expression not allowed here", Default);
8813 -- Check incorrect use of dynamically tagged expressions
8815 if Is_Tagged_Type (Formal_Type) then
8816 Check_Dynamically_Tagged_Expression
8819 Related_Nod => Default);
8823 -- Ada 2005 (AI-231): Static checks
8825 if Ada_Version >= Ada_2005
8826 and then Is_Access_Type (Etype (Formal))
8827 and then Can_Never_Be_Null (Etype (Formal))
8829 Null_Exclusion_Static_Checks (Param_Spec);
8836 -- If this is the formal part of a function specification, analyze the
8837 -- subtype mark in the context where the formals are visible but not
8838 -- yet usable, and may hide outer homographs.
8840 if Nkind (Related_Nod) = N_Function_Specification then
8841 Analyze_Return_Type (Related_Nod);
8844 -- Now set the kind (mode) of each formal
8846 Param_Spec := First (T);
8847 while Present (Param_Spec) loop
8848 Formal := Defining_Identifier (Param_Spec);
8849 Set_Formal_Mode (Formal);
8851 if Ekind (Formal) = E_In_Parameter then
8852 Set_Default_Value (Formal, Expression (Param_Spec));
8854 if Present (Expression (Param_Spec)) then
8855 Default := Expression (Param_Spec);
8857 if Is_Scalar_Type (Etype (Default)) then
8859 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8861 Formal_Type := Entity (Parameter_Type (Param_Spec));
8864 Formal_Type := Access_Definition
8865 (Related_Nod, Parameter_Type (Param_Spec));
8868 Apply_Scalar_Range_Check (Default, Formal_Type);
8872 elsif Ekind (Formal) = E_Out_Parameter then
8873 Num_Out_Params := Num_Out_Params + 1;
8875 if Num_Out_Params = 1 then
8876 First_Out_Param := Formal;
8879 elsif Ekind (Formal) = E_In_Out_Parameter then
8880 Num_Out_Params := Num_Out_Params + 1;
8886 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8887 Set_Is_Only_Out_Parameter (First_Out_Param);
8889 end Process_Formals;
8895 procedure Process_PPCs
8897 Spec_Id : Entity_Id;
8898 Body_Id : Entity_Id)
8900 Loc : constant Source_Ptr := Sloc (N);
8904 Designator : Entity_Id;
8905 -- Subprogram designator, set from Spec_Id if present, else Body_Id
8907 Precond : Node_Id := Empty;
8908 -- Set non-Empty if we prepend precondition to the declarations. This
8909 -- is used to hook up inherited preconditions (adding the condition
8910 -- expression with OR ELSE, and adding the message).
8912 Inherited_Precond : Node_Id;
8913 -- Precondition inherited from parent subprogram
8915 Inherited : constant Subprogram_List :=
8916 Inherited_Subprograms (Spec_Id);
8917 -- List of subprograms inherited by this subprogram
8919 Plist : List_Id := No_List;
8920 -- List of generated postconditions
8922 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
8923 -- Prag contains an analyzed precondition or postcondition pragma. This
8924 -- function copies the pragma, changes it to the corresponding Check
8925 -- pragma and returns the Check pragma as the result. If Pspec is non-
8926 -- empty, this is the case of inheriting a PPC, where we must change
8927 -- references to parameters of the inherited subprogram to point to the
8928 -- corresponding parameters of the current subprogram.
8930 function Invariants_Or_Predicates_Present return Boolean;
8931 -- Determines if any invariants or predicates are present for any OUT
8932 -- or IN OUT parameters of the subprogram, or (for a function) if the
8933 -- return value has an invariant.
8939 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
8940 Nam : constant Name_Id := Pragma_Name (Prag);
8945 -- Prepare map if this is the case where we have to map entities of
8946 -- arguments in the overridden subprogram to corresponding entities
8947 -- of the current subprogram.
8958 Map := New_Elmt_List;
8959 PF := First_Formal (Pspec);
8960 CF := First_Formal (Designator);
8961 while Present (PF) loop
8962 Append_Elmt (PF, Map);
8963 Append_Elmt (CF, Map);
8970 -- Now we can copy the tree, doing any required substitutions
8972 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
8974 -- Set Analyzed to false, since we want to reanalyze the check
8975 -- procedure. Note that it is only at the outer level that we
8976 -- do this fiddling, for the spec cases, the already preanalyzed
8977 -- parameters are not affected.
8979 Set_Analyzed (CP, False);
8981 -- We also make sure Comes_From_Source is False for the copy
8983 Set_Comes_From_Source (CP, False);
8985 -- For a postcondition pragma within a generic, preserve the pragma
8986 -- for later expansion.
8988 if Nam = Name_Postcondition
8989 and then not Expander_Active
8994 -- Change copy of pragma into corresponding pragma Check
8996 Prepend_To (Pragma_Argument_Associations (CP),
8997 Make_Pragma_Argument_Association (Sloc (Prag),
8998 Expression => Make_Identifier (Loc, Nam)));
8999 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
9001 -- If this is inherited case and the current message starts with
9002 -- "failed p", we change it to "failed inherited p...".
9004 if Present (Pspec) then
9006 Msg : constant Node_Id :=
9007 Last (Pragma_Argument_Associations (CP));
9010 if Chars (Msg) = Name_Message then
9011 String_To_Name_Buffer (Strval (Expression (Msg)));
9013 if Name_Buffer (1 .. 8) = "failed p" then
9014 Insert_Str_In_Name_Buffer ("inherited ", 8);
9016 (Expression (Last (Pragma_Argument_Associations (CP))),
9017 String_From_Name_Buffer);
9023 -- Return the check pragma
9028 --------------------------------------
9029 -- Invariants_Or_Predicates_Present --
9030 --------------------------------------
9032 function Invariants_Or_Predicates_Present return Boolean is
9036 -- Check function return result
9038 if Ekind (Designator) /= E_Procedure
9039 and then Has_Invariants (Etype (Designator))
9046 Formal := First_Formal (Designator);
9047 while Present (Formal) loop
9048 if Ekind (Formal) /= E_In_Parameter
9050 (Has_Invariants (Etype (Formal))
9051 or else Present (Predicate_Function (Etype (Formal))))
9056 Next_Formal (Formal);
9060 end Invariants_Or_Predicates_Present;
9062 -- Start of processing for Process_PPCs
9065 -- Capture designator from spec if present, else from body
9067 if Present (Spec_Id) then
9068 Designator := Spec_Id;
9070 Designator := Body_Id;
9073 -- Grab preconditions from spec
9075 if Present (Spec_Id) then
9077 -- Loop through PPC pragmas from spec. Note that preconditions from
9078 -- the body will be analyzed and converted when we scan the body
9079 -- declarations below.
9081 Prag := Spec_PPC_List (Spec_Id);
9082 while Present (Prag) loop
9083 if Pragma_Name (Prag) = Name_Precondition then
9085 -- For Pre (or Precondition pragma), we simply prepend the
9086 -- pragma to the list of declarations right away so that it
9087 -- will be executed at the start of the procedure. Note that
9088 -- this processing reverses the order of the list, which is
9089 -- what we want since new entries were chained to the head of
9090 -- the list. There can be more then one precondition when we
9091 -- use pragma Precondition
9093 if not Class_Present (Prag) then
9094 Prepend (Grab_PPC, Declarations (N));
9096 -- For Pre'Class there can only be one pragma, and we save
9097 -- it in Precond for now. We will add inherited Pre'Class
9098 -- stuff before inserting this pragma in the declarations.
9100 Precond := Grab_PPC;
9104 Prag := Next_Pragma (Prag);
9107 -- Now deal with inherited preconditions
9109 for J in Inherited'Range loop
9110 Prag := Spec_PPC_List (Inherited (J));
9112 while Present (Prag) loop
9113 if Pragma_Name (Prag) = Name_Precondition
9114 and then Class_Present (Prag)
9116 Inherited_Precond := Grab_PPC (Inherited (J));
9118 -- No precondition so far, so establish this as the first
9120 if No (Precond) then
9121 Precond := Inherited_Precond;
9123 -- Here we already have a precondition, add inherited one
9126 -- Add new precondition to old one using OR ELSE
9129 New_Expr : constant Node_Id :=
9133 (Pragma_Argument_Associations
9134 (Inherited_Precond))));
9135 Old_Expr : constant Node_Id :=
9139 (Pragma_Argument_Associations
9143 if Paren_Count (Old_Expr) = 0 then
9144 Set_Paren_Count (Old_Expr, 1);
9147 if Paren_Count (New_Expr) = 0 then
9148 Set_Paren_Count (New_Expr, 1);
9152 Make_Or_Else (Sloc (Old_Expr),
9153 Left_Opnd => Relocate_Node (Old_Expr),
9154 Right_Opnd => New_Expr));
9157 -- Add new message in the form:
9159 -- failed precondition from bla
9160 -- also failed inherited precondition from bla
9163 -- Skip this if exception locations are suppressed
9165 if not Exception_Locations_Suppressed then
9167 New_Msg : constant Node_Id :=
9170 (Pragma_Argument_Associations
9171 (Inherited_Precond)));
9172 Old_Msg : constant Node_Id :=
9175 (Pragma_Argument_Associations
9178 Start_String (Strval (Old_Msg));
9179 Store_String_Chars (ASCII.LF & " also ");
9180 Store_String_Chars (Strval (New_Msg));
9181 Set_Strval (Old_Msg, End_String);
9187 Prag := Next_Pragma (Prag);
9191 -- If we have built a precondition for Pre'Class (including any
9192 -- Pre'Class aspects inherited from parent subprograms), then we
9193 -- insert this composite precondition at this stage.
9195 if Present (Precond) then
9196 Prepend (Precond, Declarations (N));
9200 -- Build postconditions procedure if needed and prepend the following
9201 -- declaration to the start of the declarations for the subprogram.
9203 -- procedure _postconditions [(_Result : resulttype)] is
9205 -- pragma Check (Postcondition, condition [,message]);
9206 -- pragma Check (Postcondition, condition [,message]);
9208 -- Invariant_Procedure (_Result) ...
9209 -- Invariant_Procedure (Arg1)
9213 -- First we deal with the postconditions in the body
9215 if Is_Non_Empty_List (Declarations (N)) then
9217 -- Loop through declarations
9219 Prag := First (Declarations (N));
9220 while Present (Prag) loop
9221 if Nkind (Prag) = N_Pragma then
9223 -- If pragma, capture if enabled postcondition, else ignore
9225 if Pragma_Name (Prag) = Name_Postcondition
9226 and then Check_Enabled (Name_Postcondition)
9228 if Plist = No_List then
9229 Plist := Empty_List;
9234 -- If expansion is disabled, as in a generic unit, save
9235 -- pragma for later expansion.
9237 if not Expander_Active then
9238 Prepend (Grab_PPC, Declarations (N));
9240 Append (Grab_PPC, Plist);
9246 -- Not a pragma, if comes from source, then end scan
9248 elsif Comes_From_Source (Prag) then
9251 -- Skip stuff not coming from source
9259 -- Now deal with any postconditions from the spec
9261 if Present (Spec_Id) then
9262 Spec_Postconditions : declare
9263 procedure Process_Post_Conditions
9266 -- This processes the Spec_PPC_List from Spec, processing any
9267 -- postconditions from the list. If Class is True, then only
9268 -- postconditions marked with Class_Present are considered.
9269 -- The caller has checked that Spec_PPC_List is non-Empty.
9271 -----------------------------
9272 -- Process_Post_Conditions --
9273 -----------------------------
9275 procedure Process_Post_Conditions
9288 -- Loop through PPC pragmas from spec
9290 Prag := Spec_PPC_List (Spec);
9292 if Pragma_Name (Prag) = Name_Postcondition
9293 and then (not Class or else Class_Present (Prag))
9295 if Plist = No_List then
9296 Plist := Empty_List;
9299 if not Expander_Active then
9301 (Grab_PPC (Pspec), Declarations (N));
9303 Append (Grab_PPC (Pspec), Plist);
9307 Prag := Next_Pragma (Prag);
9308 exit when No (Prag);
9310 end Process_Post_Conditions;
9312 -- Start of processing for Spec_Postconditions
9315 if Present (Spec_PPC_List (Spec_Id)) then
9316 Process_Post_Conditions (Spec_Id, Class => False);
9319 -- Process inherited postconditions
9321 for J in Inherited'Range loop
9322 if Present (Spec_PPC_List (Inherited (J))) then
9323 Process_Post_Conditions (Inherited (J), Class => True);
9326 end Spec_Postconditions;
9329 -- If we had any postconditions and expansion is enabled, or if the
9330 -- procedure has invariants, then build the _Postconditions procedure.
9332 if (Present (Plist) or else Invariants_Or_Predicates_Present)
9333 and then Expander_Active
9336 Plist := Empty_List;
9339 -- Special processing for function case
9341 if Ekind (Designator) /= E_Procedure then
9343 Rent : constant Entity_Id :=
9344 Make_Defining_Identifier (Loc,
9345 Chars => Name_uResult);
9346 Ftyp : constant Entity_Id := Etype (Designator);
9349 Set_Etype (Rent, Ftyp);
9351 -- Add argument for return
9355 Make_Parameter_Specification (Loc,
9356 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
9357 Defining_Identifier => Rent));
9359 -- Add invariant call if returning type with invariants
9361 if Has_Invariants (Etype (Rent))
9362 and then Present (Invariant_Procedure (Etype (Rent)))
9365 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
9369 -- Procedure rather than a function
9375 -- Add invariant calls and predicate calls for parameters. Note that
9376 -- this is done for functions as well, since in Ada 2012 they can
9377 -- have IN OUT args.
9384 Formal := First_Formal (Designator);
9385 while Present (Formal) loop
9386 if Ekind (Formal) /= E_In_Parameter then
9387 Ftype := Etype (Formal);
9389 if Has_Invariants (Ftype)
9390 and then Present (Invariant_Procedure (Ftype))
9394 (New_Occurrence_Of (Formal, Loc)));
9397 if Present (Predicate_Function (Ftype)) then
9399 Make_Predicate_Check
9400 (Ftype, New_Occurrence_Of (Formal, Loc)));
9404 Next_Formal (Formal);
9408 -- Build and insert postcondition procedure
9411 Post_Proc : constant Entity_Id :=
9412 Make_Defining_Identifier (Loc,
9413 Chars => Name_uPostconditions);
9414 -- The entity for the _Postconditions procedure
9417 Prepend_To (Declarations (N),
9418 Make_Subprogram_Body (Loc,
9420 Make_Procedure_Specification (Loc,
9421 Defining_Unit_Name => Post_Proc,
9422 Parameter_Specifications => Parms),
9424 Declarations => Empty_List,
9426 Handled_Statement_Sequence =>
9427 Make_Handled_Sequence_Of_Statements (Loc,
9428 Statements => Plist)));
9430 -- If this is a procedure, set the Postcondition_Proc attribute on
9431 -- the proper defining entity for the subprogram.
9433 if Ekind (Designator) = E_Procedure then
9434 Set_Postcondition_Proc (Designator, Post_Proc);
9438 Set_Has_Postconditions (Designator);
9442 ----------------------------
9443 -- Reference_Body_Formals --
9444 ----------------------------
9446 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
9451 if Error_Posted (Spec) then
9455 -- Iterate over both lists. They may be of different lengths if the two
9456 -- specs are not conformant.
9458 Fs := First_Formal (Spec);
9459 Fb := First_Formal (Bod);
9460 while Present (Fs) and then Present (Fb) loop
9461 Generate_Reference (Fs, Fb, 'b');
9464 Style.Check_Identifier (Fb, Fs);
9467 Set_Spec_Entity (Fb, Fs);
9468 Set_Referenced (Fs, False);
9472 end Reference_Body_Formals;
9474 -------------------------
9475 -- Set_Actual_Subtypes --
9476 -------------------------
9478 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
9482 First_Stmt : Node_Id := Empty;
9483 AS_Needed : Boolean;
9486 -- If this is an empty initialization procedure, no need to create
9487 -- actual subtypes (small optimization).
9489 if Ekind (Subp) = E_Procedure
9490 and then Is_Null_Init_Proc (Subp)
9495 Formal := First_Formal (Subp);
9496 while Present (Formal) loop
9497 T := Etype (Formal);
9499 -- We never need an actual subtype for a constrained formal
9501 if Is_Constrained (T) then
9504 -- If we have unknown discriminants, then we do not need an actual
9505 -- subtype, or more accurately we cannot figure it out! Note that
9506 -- all class-wide types have unknown discriminants.
9508 elsif Has_Unknown_Discriminants (T) then
9511 -- At this stage we have an unconstrained type that may need an
9512 -- actual subtype. For sure the actual subtype is needed if we have
9513 -- an unconstrained array type.
9515 elsif Is_Array_Type (T) then
9518 -- The only other case needing an actual subtype is an unconstrained
9519 -- record type which is an IN parameter (we cannot generate actual
9520 -- subtypes for the OUT or IN OUT case, since an assignment can
9521 -- change the discriminant values. However we exclude the case of
9522 -- initialization procedures, since discriminants are handled very
9523 -- specially in this context, see the section entitled "Handling of
9524 -- Discriminants" in Einfo.
9526 -- We also exclude the case of Discrim_SO_Functions (functions used
9527 -- in front end layout mode for size/offset values), since in such
9528 -- functions only discriminants are referenced, and not only are such
9529 -- subtypes not needed, but they cannot always be generated, because
9530 -- of order of elaboration issues.
9532 elsif Is_Record_Type (T)
9533 and then Ekind (Formal) = E_In_Parameter
9534 and then Chars (Formal) /= Name_uInit
9535 and then not Is_Unchecked_Union (T)
9536 and then not Is_Discrim_SO_Function (Subp)
9540 -- All other cases do not need an actual subtype
9546 -- Generate actual subtypes for unconstrained arrays and
9547 -- unconstrained discriminated records.
9550 if Nkind (N) = N_Accept_Statement then
9552 -- If expansion is active, The formal is replaced by a local
9553 -- variable that renames the corresponding entry of the
9554 -- parameter block, and it is this local variable that may
9555 -- require an actual subtype.
9557 if Expander_Active then
9558 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
9560 Decl := Build_Actual_Subtype (T, Formal);
9563 if Present (Handled_Statement_Sequence (N)) then
9565 First (Statements (Handled_Statement_Sequence (N)));
9566 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
9567 Mark_Rewrite_Insertion (Decl);
9569 -- If the accept statement has no body, there will be no
9570 -- reference to the actuals, so no need to compute actual
9577 Decl := Build_Actual_Subtype (T, Formal);
9578 Prepend (Decl, Declarations (N));
9579 Mark_Rewrite_Insertion (Decl);
9582 -- The declaration uses the bounds of an existing object, and
9583 -- therefore needs no constraint checks.
9585 Analyze (Decl, Suppress => All_Checks);
9587 -- We need to freeze manually the generated type when it is
9588 -- inserted anywhere else than in a declarative part.
9590 if Present (First_Stmt) then
9591 Insert_List_Before_And_Analyze (First_Stmt,
9592 Freeze_Entity (Defining_Identifier (Decl), N));
9595 if Nkind (N) = N_Accept_Statement
9596 and then Expander_Active
9598 Set_Actual_Subtype (Renamed_Object (Formal),
9599 Defining_Identifier (Decl));
9601 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
9605 Next_Formal (Formal);
9607 end Set_Actual_Subtypes;
9609 ---------------------
9610 -- Set_Formal_Mode --
9611 ---------------------
9613 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
9614 Spec : constant Node_Id := Parent (Formal_Id);
9617 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
9618 -- since we ensure that corresponding actuals are always valid at the
9619 -- point of the call.
9621 if Out_Present (Spec) then
9622 if Ekind (Scope (Formal_Id)) = E_Function
9623 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
9625 -- [IN] OUT parameters allowed for functions in Ada 2012
9627 if Ada_Version >= Ada_2012 then
9628 if In_Present (Spec) then
9629 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9631 Set_Ekind (Formal_Id, E_Out_Parameter);
9634 -- But not in earlier versions of Ada
9637 Error_Msg_N ("functions can only have IN parameters", Spec);
9638 Set_Ekind (Formal_Id, E_In_Parameter);
9641 elsif In_Present (Spec) then
9642 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9645 Set_Ekind (Formal_Id, E_Out_Parameter);
9646 Set_Never_Set_In_Source (Formal_Id, True);
9647 Set_Is_True_Constant (Formal_Id, False);
9648 Set_Current_Value (Formal_Id, Empty);
9652 Set_Ekind (Formal_Id, E_In_Parameter);
9655 -- Set Is_Known_Non_Null for access parameters since the language
9656 -- guarantees that access parameters are always non-null. We also set
9657 -- Can_Never_Be_Null, since there is no way to change the value.
9659 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
9661 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
9662 -- null; In Ada 2005, only if then null_exclusion is explicit.
9664 if Ada_Version < Ada_2005
9665 or else Can_Never_Be_Null (Etype (Formal_Id))
9667 Set_Is_Known_Non_Null (Formal_Id);
9668 Set_Can_Never_Be_Null (Formal_Id);
9671 -- Ada 2005 (AI-231): Null-exclusion access subtype
9673 elsif Is_Access_Type (Etype (Formal_Id))
9674 and then Can_Never_Be_Null (Etype (Formal_Id))
9676 Set_Is_Known_Non_Null (Formal_Id);
9679 Set_Mechanism (Formal_Id, Default_Mechanism);
9680 Set_Formal_Validity (Formal_Id);
9681 end Set_Formal_Mode;
9683 -------------------------
9684 -- Set_Formal_Validity --
9685 -------------------------
9687 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
9689 -- If no validity checking, then we cannot assume anything about the
9690 -- validity of parameters, since we do not know there is any checking
9691 -- of the validity on the call side.
9693 if not Validity_Checks_On then
9696 -- If validity checking for parameters is enabled, this means we are
9697 -- not supposed to make any assumptions about argument values.
9699 elsif Validity_Check_Parameters then
9702 -- If we are checking in parameters, we will assume that the caller is
9703 -- also checking parameters, so we can assume the parameter is valid.
9705 elsif Ekind (Formal_Id) = E_In_Parameter
9706 and then Validity_Check_In_Params
9708 Set_Is_Known_Valid (Formal_Id, True);
9710 -- Similar treatment for IN OUT parameters
9712 elsif Ekind (Formal_Id) = E_In_Out_Parameter
9713 and then Validity_Check_In_Out_Params
9715 Set_Is_Known_Valid (Formal_Id, True);
9717 end Set_Formal_Validity;
9719 ------------------------
9720 -- Subtype_Conformant --
9721 ------------------------
9723 function Subtype_Conformant
9724 (New_Id : Entity_Id;
9726 Skip_Controlling_Formals : Boolean := False) return Boolean
9730 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
9731 Skip_Controlling_Formals => Skip_Controlling_Formals);
9733 end Subtype_Conformant;
9735 ---------------------
9736 -- Type_Conformant --
9737 ---------------------
9739 function Type_Conformant
9740 (New_Id : Entity_Id;
9742 Skip_Controlling_Formals : Boolean := False) return Boolean
9746 May_Hide_Profile := False;
9749 (New_Id, Old_Id, Type_Conformant, False, Result,
9750 Skip_Controlling_Formals => Skip_Controlling_Formals);
9752 end Type_Conformant;
9754 -------------------------------
9755 -- Valid_Operator_Definition --
9756 -------------------------------
9758 procedure Valid_Operator_Definition (Designator : Entity_Id) is
9761 Id : constant Name_Id := Chars (Designator);
9765 F := First_Formal (Designator);
9766 while Present (F) loop
9769 if Present (Default_Value (F)) then
9771 ("default values not allowed for operator parameters",
9778 -- Verify that user-defined operators have proper number of arguments
9779 -- First case of operators which can only be unary
9782 or else Id = Name_Op_Abs
9786 -- Case of operators which can be unary or binary
9788 elsif Id = Name_Op_Add
9789 or Id = Name_Op_Subtract
9791 N_OK := (N in 1 .. 2);
9793 -- All other operators can only be binary
9801 ("incorrect number of arguments for operator", Designator);
9805 and then Base_Type (Etype (Designator)) = Standard_Boolean
9806 and then not Is_Intrinsic_Subprogram (Designator)
9809 ("explicit definition of inequality not allowed", Designator);
9811 end Valid_Operator_Definition;