1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, 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_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elim; use Sem_Elim;
65 with Sem_Eval; use Sem_Eval;
66 with Sem_Mech; use Sem_Mech;
67 with Sem_Prag; use Sem_Prag;
68 with Sem_Res; use Sem_Res;
69 with Sem_Util; use Sem_Util;
70 with Sem_Type; use Sem_Type;
71 with Sem_Warn; use Sem_Warn;
72 with Sinput; use Sinput;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinfo.CN; use Sinfo.CN;
76 with Snames; use Snames;
77 with Stringt; use Stringt;
79 with Stylesw; use Stylesw;
80 with Tbuild; use Tbuild;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
83 with Validsw; use Validsw;
85 package body Sem_Ch6 is
87 May_Hide_Profile : Boolean := False;
88 -- This flag is used to indicate that two formals in two subprograms being
89 -- checked for conformance differ only in that one is an access parameter
90 -- while the other is of a general access type with the same designated
91 -- type. In this case, if the rest of the signatures match, a call to
92 -- either subprogram may be ambiguous, which is worth a warning. The flag
93 -- is set in Compatible_Types, and the warning emitted in
94 -- New_Overloaded_Entity.
96 -----------------------
97 -- Local Subprograms --
98 -----------------------
100 procedure Analyze_Return_Statement (N : Node_Id);
101 -- Common processing for simple_ and extended_return_statements
103 procedure Analyze_Function_Return (N : Node_Id);
104 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
105 -- applies to a [generic] function.
107 procedure Analyze_Return_Type (N : Node_Id);
108 -- Subsidiary to Process_Formals: analyze subtype mark in function
109 -- specification, in a context where the formals are visible and hide
112 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
113 -- Does all the real work of Analyze_Subprogram_Body
115 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
116 -- Analyze a generic subprogram body. N is the body to be analyzed, and
117 -- Gen_Id is the defining entity Id for the corresponding spec.
119 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
120 -- If a subprogram has pragma Inline and inlining is active, use generic
121 -- machinery to build an unexpanded body for the subprogram. This body is
122 -- subsequently used for inline expansions at call sites. If subprogram can
123 -- be inlined (depending on size and nature of local declarations) this
124 -- function returns true. Otherwise subprogram body is treated normally.
125 -- If proper warnings are enabled and the subprogram contains a construct
126 -- that cannot be inlined, the offending construct is flagged accordingly.
128 procedure Check_Conformance
131 Ctype : Conformance_Type;
133 Conforms : out Boolean;
134 Err_Loc : Node_Id := Empty;
135 Get_Inst : Boolean := False;
136 Skip_Controlling_Formals : Boolean := False);
137 -- Given two entities, this procedure checks that the profiles associated
138 -- with these entities meet the conformance criterion given by the third
139 -- parameter. If they conform, Conforms is set True and control returns
140 -- to the caller. If they do not conform, Conforms is set to False, and
141 -- in addition, if Errmsg is True on the call, proper messages are output
142 -- to complain about the conformance failure. If Err_Loc is non_Empty
143 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
144 -- error messages are placed on the appropriate part of the construct
145 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
146 -- against a formal access-to-subprogram type so Get_Instance_Of must
149 procedure Check_Subprogram_Order (N : Node_Id);
150 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
151 -- the alpha ordering rule for N if this ordering requirement applicable.
153 procedure Check_Returns
157 Proc : Entity_Id := Empty);
158 -- Called to check for missing return statements in a function body, or for
159 -- returns present in a procedure body which has No_Return set. HSS is the
160 -- handled statement sequence for the subprogram body. This procedure
161 -- checks all flow paths to make sure they either have return (Mode = 'F',
162 -- used for functions) or do not have a return (Mode = 'P', used for
163 -- No_Return procedures). The flag Err is set if there are any control
164 -- paths not explicitly terminated by a return in the function case, and is
165 -- True otherwise. Proc is the entity for the procedure case and is used
166 -- in posting the warning message.
168 procedure Enter_Overloaded_Entity (S : Entity_Id);
169 -- This procedure makes S, a new overloaded entity, into the first visible
170 -- entity with that name.
172 procedure Install_Entity (E : Entity_Id);
173 -- Make single entity visible. Used for generic formals as well
175 function Is_Non_Overriding_Operation
177 New_E : Entity_Id) return Boolean;
178 -- Enforce the rule given in 12.3(18): a private operation in an instance
179 -- overrides an inherited operation only if the corresponding operation
180 -- was overriding in the generic. This can happen for primitive operations
181 -- of types derived (in the generic unit) from formal private or formal
184 procedure Make_Inequality_Operator (S : Entity_Id);
185 -- Create the declaration for an inequality operator that is implicitly
186 -- created by a user-defined equality operator that yields a boolean.
188 procedure May_Need_Actuals (Fun : Entity_Id);
189 -- Flag functions that can be called without parameters, i.e. those that
190 -- have no parameters, or those for which defaults exist for all parameters
192 procedure Process_PPCs
195 Body_Id : Entity_Id);
196 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
197 -- conditions for the body and assembling and inserting the _postconditions
198 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
199 -- the entities for the body and separate spec (if there is no separate
200 -- spec, Spec_Id is Empty).
202 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
203 -- Formal_Id is an formal parameter entity. This procedure deals with
204 -- setting the proper validity status for this entity, which depends on
205 -- the kind of parameter and the validity checking mode.
207 ------------------------------
208 -- Analyze_Return_Statement --
209 ------------------------------
211 procedure Analyze_Return_Statement (N : Node_Id) is
213 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
214 N_Extended_Return_Statement));
216 Returns_Object : constant Boolean :=
217 Nkind (N) = N_Extended_Return_Statement
219 (Nkind (N) = N_Simple_Return_Statement
220 and then Present (Expression (N)));
221 -- True if we're returning something; that is, "return <expression>;"
222 -- or "return Result : T [:= ...]". False for "return;". Used for error
223 -- checking: If Returns_Object is True, N should apply to a function
224 -- body; otherwise N should apply to a procedure body, entry body,
225 -- accept statement, or extended return statement.
227 function Find_What_It_Applies_To return Entity_Id;
228 -- Find the entity representing the innermost enclosing body, accept
229 -- statement, or extended return statement. If the result is a callable
230 -- construct or extended return statement, then this will be the value
231 -- of the Return_Applies_To attribute. Otherwise, the program is
232 -- illegal. See RM-6.5(4/2).
234 -----------------------------
235 -- Find_What_It_Applies_To --
236 -----------------------------
238 function Find_What_It_Applies_To return Entity_Id is
239 Result : Entity_Id := Empty;
242 -- Loop outward through the Scope_Stack, skipping blocks and loops
244 for J in reverse 0 .. Scope_Stack.Last loop
245 Result := Scope_Stack.Table (J).Entity;
246 exit when Ekind (Result) /= E_Block and then
247 Ekind (Result) /= E_Loop;
250 pragma Assert (Present (Result));
252 end Find_What_It_Applies_To;
254 -- Local declarations
256 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
257 Kind : constant Entity_Kind := Ekind (Scope_Id);
258 Loc : constant Source_Ptr := Sloc (N);
259 Stm_Entity : constant Entity_Id :=
261 (E_Return_Statement, Current_Scope, Loc, 'R');
263 -- Start of processing for Analyze_Return_Statement
266 Set_Return_Statement_Entity (N, Stm_Entity);
268 Set_Etype (Stm_Entity, Standard_Void_Type);
269 Set_Return_Applies_To (Stm_Entity, Scope_Id);
271 -- Place Return entity on scope stack, to simplify enforcement of 6.5
272 -- (4/2): an inner return statement will apply to this extended return.
274 if Nkind (N) = N_Extended_Return_Statement then
275 Push_Scope (Stm_Entity);
278 -- Check that pragma No_Return is obeyed. Don't complain about the
279 -- implicitly-generated return that is placed at the end.
281 if No_Return (Scope_Id) and then Comes_From_Source (N) then
282 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
285 -- Warn on any unassigned OUT parameters if in procedure
287 if Ekind (Scope_Id) = E_Procedure then
288 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
291 -- Check that functions return objects, and other things do not
293 if Kind = E_Function or else Kind = E_Generic_Function then
294 if not Returns_Object then
295 Error_Msg_N ("missing expression in return from function", N);
298 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
299 if Returns_Object then
300 Error_Msg_N ("procedure cannot return value (use function)", N);
303 elsif Kind = E_Entry or else Kind = E_Entry_Family then
304 if Returns_Object then
305 if Is_Protected_Type (Scope (Scope_Id)) then
306 Error_Msg_N ("entry body cannot return value", N);
308 Error_Msg_N ("accept statement cannot return value", N);
312 elsif Kind = E_Return_Statement then
314 -- We are nested within another return statement, which must be an
315 -- extended_return_statement.
317 if Returns_Object then
319 ("extended_return_statement cannot return value; " &
320 "use `""RETURN;""`", N);
324 Error_Msg_N ("illegal context for return statement", N);
327 if Kind = E_Function or else Kind = E_Generic_Function then
328 Analyze_Function_Return (N);
331 if Nkind (N) = N_Extended_Return_Statement then
335 Kill_Current_Values (Last_Assignment_Only => True);
336 Check_Unreachable_Code (N);
337 end Analyze_Return_Statement;
339 ---------------------------------------------
340 -- Analyze_Abstract_Subprogram_Declaration --
341 ---------------------------------------------
343 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
344 Designator : constant Entity_Id :=
345 Analyze_Subprogram_Specification (Specification (N));
346 Scop : constant Entity_Id := Current_Scope;
349 Generate_Definition (Designator);
350 Set_Is_Abstract_Subprogram (Designator);
351 New_Overloaded_Entity (Designator);
352 Check_Delayed_Subprogram (Designator);
354 Set_Categorization_From_Scope (Designator, Scop);
356 if Ekind (Scope (Designator)) = E_Protected_Type then
358 ("abstract subprogram not allowed in protected type", N);
360 -- Issue a warning if the abstract subprogram is neither a dispatching
361 -- operation nor an operation that overrides an inherited subprogram or
362 -- predefined operator, since this most likely indicates a mistake.
364 elsif Warn_On_Redundant_Constructs
365 and then not Is_Dispatching_Operation (Designator)
366 and then not Is_Overriding_Operation (Designator)
367 and then (not Is_Operator_Symbol_Name (Chars (Designator))
368 or else Scop /= Scope (Etype (First_Formal (Designator))))
371 ("?abstract subprogram is not dispatching or overriding", N);
374 Generate_Reference_To_Formals (Designator);
375 Check_Eliminated (Designator);
376 end Analyze_Abstract_Subprogram_Declaration;
378 ----------------------------------------
379 -- Analyze_Extended_Return_Statement --
380 ----------------------------------------
382 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
384 Analyze_Return_Statement (N);
385 end Analyze_Extended_Return_Statement;
387 ----------------------------
388 -- Analyze_Function_Call --
389 ----------------------------
391 procedure Analyze_Function_Call (N : Node_Id) is
392 P : constant Node_Id := Name (N);
393 L : constant List_Id := Parameter_Associations (N);
399 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
400 -- as B (A, X). If the rewriting is successful, the call has been
401 -- analyzed and we just return.
403 if Nkind (P) = N_Selected_Component
404 and then Name (N) /= P
405 and then Is_Rewrite_Substitution (N)
406 and then Present (Etype (N))
411 -- If error analyzing name, then set Any_Type as result type and return
413 if Etype (P) = Any_Type then
414 Set_Etype (N, Any_Type);
418 -- Otherwise analyze the parameters
422 while Present (Actual) loop
424 Check_Parameterless_Call (Actual);
430 end Analyze_Function_Call;
432 -----------------------------
433 -- Analyze_Function_Return --
434 -----------------------------
436 procedure Analyze_Function_Return (N : Node_Id) is
437 Loc : constant Source_Ptr := Sloc (N);
438 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
439 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
441 R_Type : constant Entity_Id := Etype (Scope_Id);
442 -- Function result subtype
444 procedure Check_Limited_Return (Expr : Node_Id);
445 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
446 -- limited types. Used only for simple return statements.
447 -- Expr is the expression returned.
449 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
450 -- Check that the return_subtype_indication properly matches the result
451 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
453 --------------------------
454 -- Check_Limited_Return --
455 --------------------------
457 procedure Check_Limited_Return (Expr : Node_Id) is
459 -- Ada 2005 (AI-318-02): Return-by-reference types have been
460 -- removed and replaced by anonymous access results. This is an
461 -- incompatibility with Ada 95. Not clear whether this should be
462 -- enforced yet or perhaps controllable with special switch. ???
464 if Is_Limited_Type (R_Type)
465 and then Comes_From_Source (N)
466 and then not In_Instance_Body
467 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
471 if Ada_Version >= Ada_05
472 and then not Debug_Flag_Dot_L
473 and then not GNAT_Mode
476 ("(Ada 2005) cannot copy object of a limited type " &
477 "(RM-2005 6.5(5.5/2))", Expr);
478 if Is_Inherently_Limited_Type (R_Type) then
480 ("\return by reference not permitted in Ada 2005", Expr);
483 -- Warn in Ada 95 mode, to give folks a heads up about this
486 -- In GNAT mode, this is just a warning, to allow it to be
487 -- evilly turned off. Otherwise it is a real error.
489 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
490 if Is_Inherently_Limited_Type (R_Type) then
492 ("return by reference not permitted in Ada 2005 " &
493 "(RM-2005 6.5(5.5/2))?", Expr);
496 ("cannot copy object of a limited type in Ada 2005 " &
497 "(RM-2005 6.5(5.5/2))?", Expr);
500 -- Ada 95 mode, compatibility warnings disabled
503 return; -- skip continuation messages below
507 ("\consider switching to return of access type", Expr);
508 Explain_Limited_Type (R_Type, Expr);
510 end Check_Limited_Return;
512 -------------------------------------
513 -- Check_Return_Subtype_Indication --
514 -------------------------------------
516 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
517 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
518 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
519 -- Subtype given in the extended return statement;
520 -- this must match R_Type.
522 Subtype_Ind : constant Node_Id :=
523 Object_Definition (Original_Node (Obj_Decl));
525 R_Type_Is_Anon_Access :
527 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
529 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
531 Ekind (R_Type) = E_Anonymous_Access_Type;
532 -- True if return type of the function is an anonymous access type
533 -- Can't we make Is_Anonymous_Access_Type in einfo ???
535 R_Stm_Type_Is_Anon_Access :
537 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
539 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
541 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
542 -- True if type of the return object is an anonymous access type
545 -- First, avoid cascade errors:
547 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
551 -- "return access T" case; check that the return statement also has
552 -- "access T", and that the subtypes statically match:
553 -- if this is an access to subprogram the signatures must match.
555 if R_Type_Is_Anon_Access then
556 if R_Stm_Type_Is_Anon_Access then
558 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
560 if Base_Type (Designated_Type (R_Stm_Type)) /=
561 Base_Type (Designated_Type (R_Type))
562 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
565 ("subtype must statically match function result subtype",
566 Subtype_Mark (Subtype_Ind));
570 -- For two anonymous access to subprogram types, the
571 -- types themselves must be type conformant.
573 if not Conforming_Types
574 (R_Stm_Type, R_Type, Fully_Conformant)
577 ("subtype must statically match function result subtype",
583 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
586 -- Subtype indication case: check that the return object's type is
587 -- covered by the result type, and that the subtypes statically match
588 -- when the result subtype is constrained. Also handle record types
589 -- with unknown discriminants for which we have built the underlying
590 -- record view. Coverage is needed to allow specific-type return
591 -- objects when the result type is class-wide (see AI05-32).
593 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
594 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
598 Underlying_Record_View (Base_Type (R_Stm_Type))))
600 -- A null exclusion may be present on the return type, on the
601 -- function specification, on the object declaration or on the
604 if Is_Access_Type (R_Type)
606 (Can_Never_Be_Null (R_Type)
607 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
608 Can_Never_Be_Null (R_Stm_Type)
611 ("subtype must statically match function result subtype",
615 if Is_Constrained (R_Type) then
616 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
618 ("subtype must statically match function result subtype",
623 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
624 and then Is_Null_Extension (Base_Type (R_Type))
630 ("wrong type for return_subtype_indication", Subtype_Ind);
632 end Check_Return_Subtype_Indication;
634 ---------------------
635 -- Local Variables --
636 ---------------------
640 -- Start of processing for Analyze_Function_Return
643 Set_Return_Present (Scope_Id);
645 if Nkind (N) = N_Simple_Return_Statement then
646 Expr := Expression (N);
647 Analyze_And_Resolve (Expr, R_Type);
648 Check_Limited_Return (Expr);
651 -- Analyze parts specific to extended_return_statement:
654 Obj_Decl : constant Node_Id :=
655 Last (Return_Object_Declarations (N));
657 HSS : constant Node_Id := Handled_Statement_Sequence (N);
660 Expr := Expression (Obj_Decl);
662 -- Note: The check for OK_For_Limited_Init will happen in
663 -- Analyze_Object_Declaration; we treat it as a normal
664 -- object declaration.
666 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
669 Check_Return_Subtype_Indication (Obj_Decl);
671 if Present (HSS) then
674 if Present (Exception_Handlers (HSS)) then
676 -- ???Has_Nested_Block_With_Handler needs to be set.
677 -- Probably by creating an actual N_Block_Statement.
678 -- Probably in Expand.
684 -- Mark the return object as referenced, since the return is an
685 -- implicit reference of the object.
687 Set_Referenced (Defining_Identifier (Obj_Decl));
689 Check_References (Stm_Entity);
693 -- Case of Expr present
697 -- Defend against previous errors
699 and then Nkind (Expr) /= N_Empty
700 and then Present (Etype (Expr))
702 -- Apply constraint check. Note that this is done before the implicit
703 -- conversion of the expression done for anonymous access types to
704 -- ensure correct generation of the null-excluding check associated
705 -- with null-excluding expressions found in return statements.
707 Apply_Constraint_Check (Expr, R_Type);
709 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
710 -- type, apply an implicit conversion of the expression to that type
711 -- to force appropriate static and run-time accessibility checks.
713 if Ada_Version >= Ada_05
714 and then Ekind (R_Type) = E_Anonymous_Access_Type
716 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
717 Analyze_And_Resolve (Expr, R_Type);
720 -- If the result type is class-wide, then check that the return
721 -- expression's type is not declared at a deeper level than the
722 -- function (RM05-6.5(5.6/2)).
724 if Ada_Version >= Ada_05
725 and then Is_Class_Wide_Type (R_Type)
727 if Type_Access_Level (Etype (Expr)) >
728 Subprogram_Access_Level (Scope_Id)
731 ("level of return expression type is deeper than " &
732 "class-wide function!", Expr);
736 -- Check incorrect use of dynamically tagged expression
738 if Is_Tagged_Type (R_Type) then
739 Check_Dynamically_Tagged_Expression
745 -- ??? A real run-time accessibility check is needed in cases
746 -- involving dereferences of access parameters. For now we just
747 -- check the static cases.
749 if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
750 and then Is_Inherently_Limited_Type (Etype (Scope_Id))
751 and then Object_Access_Level (Expr) >
752 Subprogram_Access_Level (Scope_Id)
755 Make_Raise_Program_Error (Loc,
756 Reason => PE_Accessibility_Check_Failed));
760 ("cannot return a local value by reference?", N);
762 ("\& will be raised at run time?",
763 N, Standard_Program_Error);
767 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
768 and then Null_Exclusion_Present (Parent (Scope_Id))
770 Apply_Compile_Time_Constraint_Error
772 Msg => "(Ada 2005) null not allowed for "
773 & "null-excluding return?",
774 Reason => CE_Null_Not_Allowed);
777 end Analyze_Function_Return;
779 -------------------------------------
780 -- Analyze_Generic_Subprogram_Body --
781 -------------------------------------
783 procedure Analyze_Generic_Subprogram_Body
787 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
788 Kind : constant Entity_Kind := Ekind (Gen_Id);
794 -- Copy body and disable expansion while analyzing the generic For a
795 -- stub, do not copy the stub (which would load the proper body), this
796 -- will be done when the proper body is analyzed.
798 if Nkind (N) /= N_Subprogram_Body_Stub then
799 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
804 Spec := Specification (N);
806 -- Within the body of the generic, the subprogram is callable, and
807 -- behaves like the corresponding non-generic unit.
809 Body_Id := Defining_Entity (Spec);
811 if Kind = E_Generic_Procedure
812 and then Nkind (Spec) /= N_Procedure_Specification
814 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
817 elsif Kind = E_Generic_Function
818 and then Nkind (Spec) /= N_Function_Specification
820 Error_Msg_N ("invalid body for generic function ", Body_Id);
824 Set_Corresponding_Body (Gen_Decl, Body_Id);
826 if Has_Completion (Gen_Id)
827 and then Nkind (Parent (N)) /= N_Subunit
829 Error_Msg_N ("duplicate generic body", N);
832 Set_Has_Completion (Gen_Id);
835 if Nkind (N) = N_Subprogram_Body_Stub then
836 Set_Ekind (Defining_Entity (Specification (N)), Kind);
838 Set_Corresponding_Spec (N, Gen_Id);
841 if Nkind (Parent (N)) = N_Compilation_Unit then
842 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
845 -- Make generic parameters immediately visible in the body. They are
846 -- needed to process the formals declarations. Then make the formals
847 -- visible in a separate step.
853 First_Ent : Entity_Id;
856 First_Ent := First_Entity (Gen_Id);
859 while Present (E) and then not Is_Formal (E) loop
864 Set_Use (Generic_Formal_Declarations (Gen_Decl));
866 -- Now generic formals are visible, and the specification can be
867 -- analyzed, for subsequent conformance check.
869 Body_Id := Analyze_Subprogram_Specification (Spec);
871 -- Make formal parameters visible
875 -- E is the first formal parameter, we loop through the formals
876 -- installing them so that they will be visible.
878 Set_First_Entity (Gen_Id, E);
879 while Present (E) loop
885 -- Visible generic entity is callable within its own body
887 Set_Ekind (Gen_Id, Ekind (Body_Id));
888 Set_Ekind (Body_Id, E_Subprogram_Body);
889 Set_Convention (Body_Id, Convention (Gen_Id));
890 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
891 Set_Scope (Body_Id, Scope (Gen_Id));
892 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
894 if Nkind (N) = N_Subprogram_Body_Stub then
896 -- No body to analyze, so restore state of generic unit
898 Set_Ekind (Gen_Id, Kind);
899 Set_Ekind (Body_Id, Kind);
901 if Present (First_Ent) then
902 Set_First_Entity (Gen_Id, First_Ent);
909 -- If this is a compilation unit, it must be made visible explicitly,
910 -- because the compilation of the declaration, unlike other library
911 -- unit declarations, does not. If it is not a unit, the following
912 -- is redundant but harmless.
914 Set_Is_Immediately_Visible (Gen_Id);
915 Reference_Body_Formals (Gen_Id, Body_Id);
917 if Is_Child_Unit (Gen_Id) then
918 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
921 Set_Actual_Subtypes (N, Current_Scope);
922 Process_PPCs (N, Gen_Id, Body_Id);
924 -- If the generic unit carries pre- or post-conditions, copy them
925 -- to the original generic tree, so that they are properly added
926 -- to any instantiation.
929 Orig : constant Node_Id := Original_Node (N);
933 Cond := First (Declarations (N));
934 while Present (Cond) loop
935 if Nkind (Cond) = N_Pragma
936 and then Pragma_Name (Cond) = Name_Check
938 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
940 elsif Nkind (Cond) = N_Pragma
941 and then Pragma_Name (Cond) = Name_Postcondition
943 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
944 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
953 Analyze_Declarations (Declarations (N));
955 Analyze (Handled_Statement_Sequence (N));
957 Save_Global_References (Original_Node (N));
959 -- Prior to exiting the scope, include generic formals again (if any
960 -- are present) in the set of local entities.
962 if Present (First_Ent) then
963 Set_First_Entity (Gen_Id, First_Ent);
966 Check_References (Gen_Id);
969 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
971 Check_Subprogram_Order (N);
973 -- Outside of its body, unit is generic again
975 Set_Ekind (Gen_Id, Kind);
976 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
979 Style.Check_Identifier (Body_Id, Gen_Id);
982 end Analyze_Generic_Subprogram_Body;
984 -----------------------------
985 -- Analyze_Operator_Symbol --
986 -----------------------------
988 -- An operator symbol such as "+" or "and" may appear in context where the
989 -- literal denotes an entity name, such as "+"(x, y) or in context when it
990 -- is just a string, as in (conjunction = "or"). In these cases the parser
991 -- generates this node, and the semantics does the disambiguation. Other
992 -- such case are actuals in an instantiation, the generic unit in an
993 -- instantiation, and pragma arguments.
995 procedure Analyze_Operator_Symbol (N : Node_Id) is
996 Par : constant Node_Id := Parent (N);
999 if (Nkind (Par) = N_Function_Call
1000 and then N = Name (Par))
1001 or else Nkind (Par) = N_Function_Instantiation
1002 or else (Nkind (Par) = N_Indexed_Component
1003 and then N = Prefix (Par))
1004 or else (Nkind (Par) = N_Pragma_Argument_Association
1005 and then not Is_Pragma_String_Literal (Par))
1006 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1007 or else (Nkind (Par) = N_Attribute_Reference
1008 and then Attribute_Name (Par) /= Name_Value)
1010 Find_Direct_Name (N);
1013 Change_Operator_Symbol_To_String_Literal (N);
1016 end Analyze_Operator_Symbol;
1018 -----------------------------------
1019 -- Analyze_Parameter_Association --
1020 -----------------------------------
1022 procedure Analyze_Parameter_Association (N : Node_Id) is
1024 Analyze (Explicit_Actual_Parameter (N));
1025 end Analyze_Parameter_Association;
1027 ----------------------------
1028 -- Analyze_Procedure_Call --
1029 ----------------------------
1031 procedure Analyze_Procedure_Call (N : Node_Id) is
1032 Loc : constant Source_Ptr := Sloc (N);
1033 P : constant Node_Id := Name (N);
1034 Actuals : constant List_Id := Parameter_Associations (N);
1038 procedure Analyze_Call_And_Resolve;
1039 -- Do Analyze and Resolve calls for procedure call
1041 ------------------------------
1042 -- Analyze_Call_And_Resolve --
1043 ------------------------------
1045 procedure Analyze_Call_And_Resolve is
1047 if Nkind (N) = N_Procedure_Call_Statement then
1049 Resolve (N, Standard_Void_Type);
1053 end Analyze_Call_And_Resolve;
1055 -- Start of processing for Analyze_Procedure_Call
1058 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1059 -- a procedure call or an entry call. The prefix may denote an access
1060 -- to subprogram type, in which case an implicit dereference applies.
1061 -- If the prefix is an indexed component (without implicit dereference)
1062 -- then the construct denotes a call to a member of an entire family.
1063 -- If the prefix is a simple name, it may still denote a call to a
1064 -- parameterless member of an entry family. Resolution of these various
1065 -- interpretations is delicate.
1069 -- If this is a call of the form Obj.Op, the call may have been
1070 -- analyzed and possibly rewritten into a block, in which case
1073 if Analyzed (N) then
1077 -- If there is an error analyzing the name (which may have been
1078 -- rewritten if the original call was in prefix notation) then error
1079 -- has been emitted already, mark node and return.
1082 or else Etype (Name (N)) = Any_Type
1084 Set_Etype (N, Any_Type);
1088 -- Otherwise analyze the parameters
1090 if Present (Actuals) then
1091 Actual := First (Actuals);
1093 while Present (Actual) loop
1095 Check_Parameterless_Call (Actual);
1100 -- Special processing for Elab_Spec and Elab_Body calls
1102 if Nkind (P) = N_Attribute_Reference
1103 and then (Attribute_Name (P) = Name_Elab_Spec
1104 or else Attribute_Name (P) = Name_Elab_Body)
1106 if Present (Actuals) then
1108 ("no parameters allowed for this call", First (Actuals));
1112 Set_Etype (N, Standard_Void_Type);
1115 elsif Is_Entity_Name (P)
1116 and then Is_Record_Type (Etype (Entity (P)))
1117 and then Remote_AST_I_Dereference (P)
1121 elsif Is_Entity_Name (P)
1122 and then Ekind (Entity (P)) /= E_Entry_Family
1124 if Is_Access_Type (Etype (P))
1125 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1126 and then No (Actuals)
1127 and then Comes_From_Source (N)
1129 Error_Msg_N ("missing explicit dereference in call", N);
1132 Analyze_Call_And_Resolve;
1134 -- If the prefix is the simple name of an entry family, this is
1135 -- a parameterless call from within the task body itself.
1137 elsif Is_Entity_Name (P)
1138 and then Nkind (P) = N_Identifier
1139 and then Ekind (Entity (P)) = E_Entry_Family
1140 and then Present (Actuals)
1141 and then No (Next (First (Actuals)))
1143 -- Can be call to parameterless entry family. What appears to be the
1144 -- sole argument is in fact the entry index. Rewrite prefix of node
1145 -- accordingly. Source representation is unchanged by this
1149 Make_Indexed_Component (Loc,
1151 Make_Selected_Component (Loc,
1152 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1153 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1154 Expressions => Actuals);
1155 Set_Name (N, New_N);
1156 Set_Etype (New_N, Standard_Void_Type);
1157 Set_Parameter_Associations (N, No_List);
1158 Analyze_Call_And_Resolve;
1160 elsif Nkind (P) = N_Explicit_Dereference then
1161 if Ekind (Etype (P)) = E_Subprogram_Type then
1162 Analyze_Call_And_Resolve;
1164 Error_Msg_N ("expect access to procedure in call", P);
1167 -- The name can be a selected component or an indexed component that
1168 -- yields an access to subprogram. Such a prefix is legal if the call
1169 -- has parameter associations.
1171 elsif Is_Access_Type (Etype (P))
1172 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1174 if Present (Actuals) then
1175 Analyze_Call_And_Resolve;
1177 Error_Msg_N ("missing explicit dereference in call ", N);
1180 -- If not an access to subprogram, then the prefix must resolve to the
1181 -- name of an entry, entry family, or protected operation.
1183 -- For the case of a simple entry call, P is a selected component where
1184 -- the prefix is the task and the selector name is the entry. A call to
1185 -- a protected procedure will have the same syntax. If the protected
1186 -- object contains overloaded operations, the entity may appear as a
1187 -- function, the context will select the operation whose type is Void.
1189 elsif Nkind (P) = N_Selected_Component
1190 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1192 Ekind (Entity (Selector_Name (P))) = E_Procedure
1194 Ekind (Entity (Selector_Name (P))) = E_Function)
1196 Analyze_Call_And_Resolve;
1198 elsif Nkind (P) = N_Selected_Component
1199 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1200 and then Present (Actuals)
1201 and then No (Next (First (Actuals)))
1203 -- Can be call to parameterless entry family. What appears to be the
1204 -- sole argument is in fact the entry index. Rewrite prefix of node
1205 -- accordingly. Source representation is unchanged by this
1209 Make_Indexed_Component (Loc,
1210 Prefix => New_Copy (P),
1211 Expressions => Actuals);
1212 Set_Name (N, New_N);
1213 Set_Etype (New_N, Standard_Void_Type);
1214 Set_Parameter_Associations (N, No_List);
1215 Analyze_Call_And_Resolve;
1217 -- For the case of a reference to an element of an entry family, P is
1218 -- an indexed component whose prefix is a selected component (task and
1219 -- entry family), and whose index is the entry family index.
1221 elsif Nkind (P) = N_Indexed_Component
1222 and then Nkind (Prefix (P)) = N_Selected_Component
1223 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1225 Analyze_Call_And_Resolve;
1227 -- If the prefix is the name of an entry family, it is a call from
1228 -- within the task body itself.
1230 elsif Nkind (P) = N_Indexed_Component
1231 and then Nkind (Prefix (P)) = N_Identifier
1232 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1235 Make_Selected_Component (Loc,
1236 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1237 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1238 Rewrite (Prefix (P), New_N);
1240 Analyze_Call_And_Resolve;
1242 -- Anything else is an error
1245 Error_Msg_N ("invalid procedure or entry call", N);
1247 end Analyze_Procedure_Call;
1249 -------------------------------------
1250 -- Analyze_Simple_Return_Statement --
1251 -------------------------------------
1253 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1255 if Present (Expression (N)) then
1256 Mark_Coextensions (N, Expression (N));
1259 Analyze_Return_Statement (N);
1260 end Analyze_Simple_Return_Statement;
1262 -------------------------
1263 -- Analyze_Return_Type --
1264 -------------------------
1266 procedure Analyze_Return_Type (N : Node_Id) is
1267 Designator : constant Entity_Id := Defining_Entity (N);
1268 Typ : Entity_Id := Empty;
1271 -- Normal case where result definition does not indicate an error
1273 if Result_Definition (N) /= Error then
1274 if Nkind (Result_Definition (N)) = N_Access_Definition then
1276 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1279 AD : constant Node_Id :=
1280 Access_To_Subprogram_Definition (Result_Definition (N));
1282 if Present (AD) and then Protected_Present (AD) then
1283 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1285 Typ := Access_Definition (N, Result_Definition (N));
1289 Set_Parent (Typ, Result_Definition (N));
1290 Set_Is_Local_Anonymous_Access (Typ);
1291 Set_Etype (Designator, Typ);
1293 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1295 Null_Exclusion_Static_Checks (N);
1297 -- Subtype_Mark case
1300 Find_Type (Result_Definition (N));
1301 Typ := Entity (Result_Definition (N));
1302 Set_Etype (Designator, Typ);
1304 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1306 Null_Exclusion_Static_Checks (N);
1308 -- If a null exclusion is imposed on the result type, then create
1309 -- a null-excluding itype (an access subtype) and use it as the
1310 -- function's Etype. Note that the null exclusion checks are done
1311 -- right before this, because they don't get applied to types that
1312 -- do not come from source.
1314 if Is_Access_Type (Typ)
1315 and then Null_Exclusion_Present (N)
1317 Set_Etype (Designator,
1318 Create_Null_Excluding_Itype
1321 Scope_Id => Scope (Current_Scope)));
1323 -- The new subtype must be elaborated before use because
1324 -- it is visible outside of the function. However its base
1325 -- type may not be frozen yet, so the reference that will
1326 -- force elaboration must be attached to the freezing of
1329 -- If the return specification appears on a proper body,
1330 -- the subtype will have been created already on the spec.
1332 if Is_Frozen (Typ) then
1333 if Nkind (Parent (N)) = N_Subprogram_Body
1334 and then Nkind (Parent (Parent (N))) = N_Subunit
1338 Build_Itype_Reference (Etype (Designator), Parent (N));
1342 Ensure_Freeze_Node (Typ);
1345 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1347 Set_Itype (IR, Etype (Designator));
1348 Append_Freeze_Actions (Typ, New_List (IR));
1353 Set_Etype (Designator, Typ);
1356 if Ekind (Typ) = E_Incomplete_Type
1357 and then Is_Value_Type (Typ)
1361 elsif Ekind (Typ) = E_Incomplete_Type
1362 or else (Is_Class_Wide_Type (Typ)
1364 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1367 ("invalid use of incomplete type&", Designator, Typ);
1371 -- Case where result definition does indicate an error
1374 Set_Etype (Designator, Any_Type);
1376 end Analyze_Return_Type;
1378 -----------------------------
1379 -- Analyze_Subprogram_Body --
1380 -----------------------------
1382 procedure Analyze_Subprogram_Body (N : Node_Id) is
1383 Loc : constant Source_Ptr := Sloc (N);
1384 Body_Spec : constant Node_Id := Specification (N);
1385 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1388 if Debug_Flag_C then
1389 Write_Str ("==> subprogram body ");
1390 Write_Name (Chars (Body_Id));
1391 Write_Str (" from ");
1392 Write_Location (Loc);
1397 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1399 -- The real work is split out into the helper, so it can do "return;"
1400 -- without skipping the debug output:
1402 Analyze_Subprogram_Body_Helper (N);
1404 if Debug_Flag_C then
1406 Write_Str ("<== subprogram body ");
1407 Write_Name (Chars (Body_Id));
1408 Write_Str (" from ");
1409 Write_Location (Loc);
1412 end Analyze_Subprogram_Body;
1414 ------------------------------------
1415 -- Analyze_Subprogram_Body_Helper --
1416 ------------------------------------
1418 -- This procedure is called for regular subprogram bodies, generic bodies,
1419 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1420 -- specification matters, and is used to create a proper declaration for
1421 -- the subprogram, or to perform conformance checks.
1423 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1424 Loc : constant Source_Ptr := Sloc (N);
1425 Body_Deleted : constant Boolean := False;
1426 Body_Spec : constant Node_Id := Specification (N);
1427 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1428 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1429 Conformant : Boolean;
1431 Missing_Ret : Boolean;
1433 Prot_Typ : Entity_Id := Empty;
1434 Spec_Id : Entity_Id;
1435 Spec_Decl : Node_Id := Empty;
1437 Last_Real_Spec_Entity : Entity_Id := Empty;
1438 -- When we analyze a separate spec, the entity chain ends up containing
1439 -- the formals, as well as any itypes generated during analysis of the
1440 -- default expressions for parameters, or the arguments of associated
1441 -- precondition/postcondition pragmas (which are analyzed in the context
1442 -- of the spec since they have visibility on formals).
1444 -- These entities belong with the spec and not the body. However we do
1445 -- the analysis of the body in the context of the spec (again to obtain
1446 -- visibility to the formals), and all the entities generated during
1447 -- this analysis end up also chained to the entity chain of the spec.
1448 -- But they really belong to the body, and there is circuitry to move
1449 -- them from the spec to the body.
1451 -- However, when we do this move, we don't want to move the real spec
1452 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1453 -- variable points to the last real spec entity, so we only move those
1454 -- chained beyond that point. It is initialized to Empty to deal with
1455 -- the case where there is no separate spec.
1457 procedure Check_Anonymous_Return;
1458 -- Ada 2005: if a function returns an access type that denotes a task,
1459 -- or a type that contains tasks, we must create a master entity for
1460 -- the anonymous type, which typically will be used in an allocator
1461 -- in the body of the function.
1463 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1464 -- Look ahead to recognize a pragma that may appear after the body.
1465 -- If there is a previous spec, check that it appears in the same
1466 -- declarative part. If the pragma is Inline_Always, perform inlining
1467 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1468 -- If the body acts as a spec, and inlining is required, we create a
1469 -- subprogram declaration for it, in order to attach the body to inline.
1470 -- If pragma does not appear after the body, check whether there is
1471 -- an inline pragma before any local declarations.
1473 function Disambiguate_Spec return Entity_Id;
1474 -- When a primitive is declared between the private view and the full
1475 -- view of a concurrent type which implements an interface, a special
1476 -- mechanism is used to find the corresponding spec of the primitive
1479 function Is_Private_Concurrent_Primitive
1480 (Subp_Id : Entity_Id) return Boolean;
1481 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1482 -- type that implements an interface and has a private view.
1484 procedure Set_Trivial_Subprogram (N : Node_Id);
1485 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1486 -- subprogram whose body is being analyzed. N is the statement node
1487 -- causing the flag to be set, if the following statement is a return
1488 -- of an entity, we mark the entity as set in source to suppress any
1489 -- warning on the stylized use of function stubs with a dummy return.
1491 procedure Verify_Overriding_Indicator;
1492 -- If there was a previous spec, the entity has been entered in the
1493 -- current scope previously. If the body itself carries an overriding
1494 -- indicator, check that it is consistent with the known status of the
1497 ----------------------------
1498 -- Check_Anonymous_Return --
1499 ----------------------------
1501 procedure Check_Anonymous_Return is
1507 if Present (Spec_Id) then
1513 if Ekind (Scop) = E_Function
1514 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1515 and then not Is_Thunk (Scop)
1516 and then (Has_Task (Designated_Type (Etype (Scop)))
1518 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1520 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1521 and then Expander_Active
1523 -- Avoid cases with no tasking support
1525 and then RTE_Available (RE_Current_Master)
1526 and then not Restriction_Active (No_Task_Hierarchy)
1529 Make_Object_Declaration (Loc,
1530 Defining_Identifier =>
1531 Make_Defining_Identifier (Loc, Name_uMaster),
1532 Constant_Present => True,
1533 Object_Definition =>
1534 New_Reference_To (RTE (RE_Master_Id), Loc),
1536 Make_Explicit_Dereference (Loc,
1537 New_Reference_To (RTE (RE_Current_Master), Loc)));
1539 if Present (Declarations (N)) then
1540 Prepend (Decl, Declarations (N));
1542 Set_Declarations (N, New_List (Decl));
1545 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1546 Set_Has_Master_Entity (Scop);
1548 -- Now mark the containing scope as a task master
1551 while Nkind (Par) /= N_Compilation_Unit loop
1552 Par := Parent (Par);
1553 pragma Assert (Present (Par));
1555 -- If we fall off the top, we are at the outer level, and
1556 -- the environment task is our effective master, so nothing
1560 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1562 Set_Is_Task_Master (Par, True);
1567 end Check_Anonymous_Return;
1569 -------------------------
1570 -- Check_Inline_Pragma --
1571 -------------------------
1573 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1577 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1578 -- True when N is a pragma Inline or Inline_Always that applies
1579 -- to this subprogram.
1581 -----------------------
1582 -- Is_Inline_Pragma --
1583 -----------------------
1585 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1588 Nkind (N) = N_Pragma
1590 (Pragma_Name (N) = Name_Inline_Always
1593 and then Pragma_Name (N) = Name_Inline))
1596 (Expression (First (Pragma_Argument_Associations (N))))
1598 end Is_Inline_Pragma;
1600 -- Start of processing for Check_Inline_Pragma
1603 if not Expander_Active then
1607 if Is_List_Member (N)
1608 and then Present (Next (N))
1609 and then Is_Inline_Pragma (Next (N))
1613 elsif Nkind (N) /= N_Subprogram_Body_Stub
1614 and then Present (Declarations (N))
1615 and then Is_Inline_Pragma (First (Declarations (N)))
1617 Prag := First (Declarations (N));
1623 if Present (Prag) then
1624 if Present (Spec_Id) then
1625 if List_Containing (N) =
1626 List_Containing (Unit_Declaration_Node (Spec_Id))
1632 -- Create a subprogram declaration, to make treatment uniform
1635 Subp : constant Entity_Id :=
1636 Make_Defining_Identifier (Loc, Chars (Body_Id));
1637 Decl : constant Node_Id :=
1638 Make_Subprogram_Declaration (Loc,
1639 Specification => New_Copy_Tree (Specification (N)));
1641 Set_Defining_Unit_Name (Specification (Decl), Subp);
1643 if Present (First_Formal (Body_Id)) then
1644 Plist := Copy_Parameter_List (Body_Id);
1645 Set_Parameter_Specifications
1646 (Specification (Decl), Plist);
1649 Insert_Before (N, Decl);
1652 Set_Has_Pragma_Inline (Subp);
1654 if Pragma_Name (Prag) = Name_Inline_Always then
1655 Set_Is_Inlined (Subp);
1656 Set_Has_Pragma_Inline_Always (Subp);
1663 end Check_Inline_Pragma;
1665 -----------------------
1666 -- Disambiguate_Spec --
1667 -----------------------
1669 function Disambiguate_Spec return Entity_Id is
1670 Priv_Spec : Entity_Id;
1673 procedure Replace_Types (To_Corresponding : Boolean);
1674 -- Depending on the flag, replace the type of formal parameters of
1675 -- Body_Id if it is a concurrent type implementing interfaces with
1676 -- the corresponding record type or the other way around.
1678 procedure Replace_Types (To_Corresponding : Boolean) is
1680 Formal_Typ : Entity_Id;
1683 Formal := First_Formal (Body_Id);
1684 while Present (Formal) loop
1685 Formal_Typ := Etype (Formal);
1687 -- From concurrent type to corresponding record
1689 if To_Corresponding then
1690 if Is_Concurrent_Type (Formal_Typ)
1691 and then Present (Corresponding_Record_Type (Formal_Typ))
1692 and then Present (Interfaces (
1693 Corresponding_Record_Type (Formal_Typ)))
1696 Corresponding_Record_Type (Formal_Typ));
1699 -- From corresponding record to concurrent type
1702 if Is_Concurrent_Record_Type (Formal_Typ)
1703 and then Present (Interfaces (Formal_Typ))
1706 Corresponding_Concurrent_Type (Formal_Typ));
1710 Next_Formal (Formal);
1714 -- Start of processing for Disambiguate_Spec
1717 -- Try to retrieve the specification of the body as is. All error
1718 -- messages are suppressed because the body may not have a spec in
1719 -- its current state.
1721 Spec_N := Find_Corresponding_Spec (N, False);
1723 -- It is possible that this is the body of a primitive declared
1724 -- between a private and a full view of a concurrent type. The
1725 -- controlling parameter of the spec carries the concurrent type,
1726 -- not the corresponding record type as transformed by Analyze_
1727 -- Subprogram_Specification. In such cases, we undo the change
1728 -- made by the analysis of the specification and try to find the
1731 -- Note that wrappers already have their corresponding specs and
1732 -- bodies set during their creation, so if the candidate spec is
1733 -- a wrapper, then we definitely need to swap all types to their
1734 -- original concurrent status.
1737 or else Is_Primitive_Wrapper (Spec_N)
1739 -- Restore all references of corresponding record types to the
1740 -- original concurrent types.
1742 Replace_Types (To_Corresponding => False);
1743 Priv_Spec := Find_Corresponding_Spec (N, False);
1745 -- The current body truly belongs to a primitive declared between
1746 -- a private and a full view. We leave the modified body as is,
1747 -- and return the true spec.
1749 if Present (Priv_Spec)
1750 and then Is_Private_Primitive (Priv_Spec)
1755 -- In case that this is some sort of error, restore the original
1756 -- state of the body.
1758 Replace_Types (To_Corresponding => True);
1762 end Disambiguate_Spec;
1764 -------------------------------------
1765 -- Is_Private_Concurrent_Primitive --
1766 -------------------------------------
1768 function Is_Private_Concurrent_Primitive
1769 (Subp_Id : Entity_Id) return Boolean
1771 Formal_Typ : Entity_Id;
1774 if Present (First_Formal (Subp_Id)) then
1775 Formal_Typ := Etype (First_Formal (Subp_Id));
1777 if Is_Concurrent_Record_Type (Formal_Typ) then
1778 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1781 -- The type of the first formal is a concurrent tagged type with
1785 Is_Concurrent_Type (Formal_Typ)
1786 and then Is_Tagged_Type (Formal_Typ)
1787 and then Has_Private_Declaration (Formal_Typ);
1791 end Is_Private_Concurrent_Primitive;
1793 ----------------------------
1794 -- Set_Trivial_Subprogram --
1795 ----------------------------
1797 procedure Set_Trivial_Subprogram (N : Node_Id) is
1798 Nxt : constant Node_Id := Next (N);
1801 Set_Is_Trivial_Subprogram (Body_Id);
1803 if Present (Spec_Id) then
1804 Set_Is_Trivial_Subprogram (Spec_Id);
1808 and then Nkind (Nxt) = N_Simple_Return_Statement
1809 and then No (Next (Nxt))
1810 and then Present (Expression (Nxt))
1811 and then Is_Entity_Name (Expression (Nxt))
1813 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1815 end Set_Trivial_Subprogram;
1817 ---------------------------------
1818 -- Verify_Overriding_Indicator --
1819 ---------------------------------
1821 procedure Verify_Overriding_Indicator is
1823 if Must_Override (Body_Spec) then
1824 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1825 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1829 elsif not Is_Overriding_Operation (Spec_Id) then
1831 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1834 elsif Must_Not_Override (Body_Spec) then
1835 if Is_Overriding_Operation (Spec_Id) then
1837 ("subprogram& overrides inherited operation",
1838 Body_Spec, Spec_Id);
1840 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1841 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1844 ("subprogram & overrides predefined operator ",
1845 Body_Spec, Spec_Id);
1847 -- If this is not a primitive operation or protected subprogram,
1848 -- then the overriding indicator is altogether illegal.
1850 elsif not Is_Primitive (Spec_Id)
1851 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
1853 Error_Msg_N ("overriding indicator only allowed " &
1854 "if subprogram is primitive",
1858 elsif Style_Check -- ??? incorrect use of Style_Check!
1859 and then Is_Overriding_Operation (Spec_Id)
1861 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
1862 Style.Missing_Overriding (N, Body_Id);
1864 end Verify_Overriding_Indicator;
1866 -- Start of processing for Analyze_Subprogram_Body_Helper
1869 -- Generic subprograms are handled separately. They always have a
1870 -- generic specification. Determine whether current scope has a
1871 -- previous declaration.
1873 -- If the subprogram body is defined within an instance of the same
1874 -- name, the instance appears as a package renaming, and will be hidden
1875 -- within the subprogram.
1877 if Present (Prev_Id)
1878 and then not Is_Overloadable (Prev_Id)
1879 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1880 or else Comes_From_Source (Prev_Id))
1882 if Is_Generic_Subprogram (Prev_Id) then
1884 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1885 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1887 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1891 -- Previous entity conflicts with subprogram name. Attempting to
1892 -- enter name will post error.
1894 Enter_Name (Body_Id);
1898 -- Non-generic case, find the subprogram declaration, if one was seen,
1899 -- or enter new overloaded entity in the current scope. If the
1900 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1901 -- part of the context of one of its subunits. No need to redo the
1904 elsif Prev_Id = Body_Id
1905 and then Has_Completion (Body_Id)
1910 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1912 if Nkind (N) = N_Subprogram_Body_Stub
1913 or else No (Corresponding_Spec (N))
1915 if Is_Private_Concurrent_Primitive (Body_Id) then
1916 Spec_Id := Disambiguate_Spec;
1918 Spec_Id := Find_Corresponding_Spec (N);
1921 -- If this is a duplicate body, no point in analyzing it
1923 if Error_Posted (N) then
1927 -- A subprogram body should cause freezing of its own declaration,
1928 -- but if there was no previous explicit declaration, then the
1929 -- subprogram will get frozen too late (there may be code within
1930 -- the body that depends on the subprogram having been frozen,
1931 -- such as uses of extra formals), so we force it to be frozen
1932 -- here. Same holds if the body and spec are compilation units.
1933 -- Finally, if the return type is an anonymous access to protected
1934 -- subprogram, it must be frozen before the body because its
1935 -- expansion has generated an equivalent type that is used when
1936 -- elaborating the body.
1938 if No (Spec_Id) then
1939 Freeze_Before (N, Body_Id);
1941 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1942 Freeze_Before (N, Spec_Id);
1944 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
1945 Freeze_Before (N, Etype (Body_Id));
1949 Spec_Id := Corresponding_Spec (N);
1953 -- Do not inline any subprogram that contains nested subprograms, since
1954 -- the backend inlining circuit seems to generate uninitialized
1955 -- references in this case. We know this happens in the case of front
1956 -- end ZCX support, but it also appears it can happen in other cases as
1957 -- well. The backend often rejects attempts to inline in the case of
1958 -- nested procedures anyway, so little if anything is lost by this.
1959 -- Note that this is test is for the benefit of the back-end. There is
1960 -- a separate test for front-end inlining that also rejects nested
1963 -- Do not do this test if errors have been detected, because in some
1964 -- error cases, this code blows up, and we don't need it anyway if
1965 -- there have been errors, since we won't get to the linker anyway.
1967 if Comes_From_Source (Body_Id)
1968 and then Serious_Errors_Detected = 0
1972 P_Ent := Scope (P_Ent);
1973 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1975 if Is_Subprogram (P_Ent) then
1976 Set_Is_Inlined (P_Ent, False);
1978 if Comes_From_Source (P_Ent)
1979 and then Has_Pragma_Inline (P_Ent)
1982 ("cannot inline& (nested subprogram)?",
1989 Check_Inline_Pragma (Spec_Id);
1991 -- Deal with special case of a fully private operation in the body of
1992 -- the protected type. We must create a declaration for the subprogram,
1993 -- in order to attach the protected subprogram that will be used in
1994 -- internal calls. We exclude compiler generated bodies from the
1995 -- expander since the issue does not arise for those cases.
1998 and then Comes_From_Source (N)
1999 and then Is_Protected_Type (Current_Scope)
2001 Spec_Id := Build_Private_Protected_Declaration (N);
2004 -- If a separate spec is present, then deal with freezing issues
2006 if Present (Spec_Id) then
2007 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2008 Verify_Overriding_Indicator;
2010 -- In general, the spec will be frozen when we start analyzing the
2011 -- body. However, for internally generated operations, such as
2012 -- wrapper functions for inherited operations with controlling
2013 -- results, the spec may not have been frozen by the time we
2014 -- expand the freeze actions that include the bodies. In particular,
2015 -- extra formals for accessibility or for return-in-place may need
2016 -- to be generated. Freeze nodes, if any, are inserted before the
2019 if not Is_Frozen (Spec_Id)
2020 and then Expander_Active
2022 -- Force the generation of its freezing node to ensure proper
2023 -- management of access types in the backend.
2025 -- This is definitely needed for some cases, but it is not clear
2026 -- why, to be investigated further???
2028 Set_Has_Delayed_Freeze (Spec_Id);
2029 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
2033 -- Mark presence of postcondition procedure in current scope and mark
2034 -- the procedure itself as needing debug info. The latter is important
2035 -- when analyzing decision coverage (for example, for MC/DC coverage).
2037 if Chars (Body_Id) = Name_uPostconditions then
2038 Set_Has_Postconditions (Current_Scope);
2039 Set_Debug_Info_Needed (Body_Id);
2042 -- Place subprogram on scope stack, and make formals visible. If there
2043 -- is a spec, the visible entity remains that of the spec.
2045 if Present (Spec_Id) then
2046 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2048 if Is_Child_Unit (Spec_Id) then
2049 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2053 Style.Check_Identifier (Body_Id, Spec_Id);
2056 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2057 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2059 if Is_Abstract_Subprogram (Spec_Id) then
2060 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2064 Set_Convention (Body_Id, Convention (Spec_Id));
2065 Set_Has_Completion (Spec_Id);
2067 if Is_Protected_Type (Scope (Spec_Id)) then
2068 Prot_Typ := Scope (Spec_Id);
2071 -- If this is a body generated for a renaming, do not check for
2072 -- full conformance. The check is redundant, because the spec of
2073 -- the body is a copy of the spec in the renaming declaration,
2074 -- and the test can lead to spurious errors on nested defaults.
2076 if Present (Spec_Decl)
2077 and then not Comes_From_Source (N)
2079 (Nkind (Original_Node (Spec_Decl)) =
2080 N_Subprogram_Renaming_Declaration
2081 or else (Present (Corresponding_Body (Spec_Decl))
2083 Nkind (Unit_Declaration_Node
2084 (Corresponding_Body (Spec_Decl))) =
2085 N_Subprogram_Renaming_Declaration))
2089 -- Conversely, the spec may have been generated for specless body
2090 -- with an inline pragma.
2092 elsif Comes_From_Source (N)
2093 and then not Comes_From_Source (Spec_Id)
2094 and then Has_Pragma_Inline (Spec_Id)
2101 Fully_Conformant, True, Conformant, Body_Id);
2104 -- If the body is not fully conformant, we have to decide if we
2105 -- should analyze it or not. If it has a really messed up profile
2106 -- then we probably should not analyze it, since we will get too
2107 -- many bogus messages.
2109 -- Our decision is to go ahead in the non-fully conformant case
2110 -- only if it is at least mode conformant with the spec. Note
2111 -- that the call to Check_Fully_Conformant has issued the proper
2112 -- error messages to complain about the lack of conformance.
2115 and then not Mode_Conformant (Body_Id, Spec_Id)
2121 if Spec_Id /= Body_Id then
2122 Reference_Body_Formals (Spec_Id, Body_Id);
2125 if Nkind (N) /= N_Subprogram_Body_Stub then
2126 Set_Corresponding_Spec (N, Spec_Id);
2128 -- Ada 2005 (AI-345): If the operation is a primitive operation
2129 -- of a concurrent type, the type of the first parameter has been
2130 -- replaced with the corresponding record, which is the proper
2131 -- run-time structure to use. However, within the body there may
2132 -- be uses of the formals that depend on primitive operations
2133 -- of the type (in particular calls in prefixed form) for which
2134 -- we need the original concurrent type. The operation may have
2135 -- several controlling formals, so the replacement must be done
2138 if Comes_From_Source (Spec_Id)
2139 and then Present (First_Entity (Spec_Id))
2140 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2141 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2143 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2146 (Corresponding_Concurrent_Type
2147 (Etype (First_Entity (Spec_Id))))
2150 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2154 Form := First_Formal (Spec_Id);
2155 while Present (Form) loop
2156 if Etype (Form) = Typ then
2157 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2165 -- Make the formals visible, and place subprogram on scope stack.
2166 -- This is also the point at which we set Last_Real_Spec_Entity
2167 -- to mark the entities which will not be moved to the body.
2169 Install_Formals (Spec_Id);
2170 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2171 Push_Scope (Spec_Id);
2173 -- Make sure that the subprogram is immediately visible. For
2174 -- child units that have no separate spec this is indispensable.
2175 -- Otherwise it is safe albeit redundant.
2177 Set_Is_Immediately_Visible (Spec_Id);
2180 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2181 Set_Ekind (Body_Id, E_Subprogram_Body);
2182 Set_Scope (Body_Id, Scope (Spec_Id));
2183 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2185 -- Case of subprogram body with no previous spec
2189 and then Comes_From_Source (Body_Id)
2190 and then not Suppress_Style_Checks (Body_Id)
2191 and then not In_Instance
2193 Style.Body_With_No_Spec (N);
2196 New_Overloaded_Entity (Body_Id);
2198 if Nkind (N) /= N_Subprogram_Body_Stub then
2199 Set_Acts_As_Spec (N);
2200 Generate_Definition (Body_Id);
2202 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2203 Generate_Reference_To_Formals (Body_Id);
2204 Install_Formals (Body_Id);
2205 Push_Scope (Body_Id);
2209 -- If the return type is an anonymous access type whose designated type
2210 -- is the limited view of a class-wide type and the non-limited view is
2211 -- available, update the return type accordingly.
2213 if Ada_Version >= Ada_05
2214 and then Comes_From_Source (N)
2221 Rtyp := Etype (Current_Scope);
2223 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2224 Etyp := Directly_Designated_Type (Rtyp);
2226 if Is_Class_Wide_Type (Etyp)
2227 and then From_With_Type (Etyp)
2229 Set_Directly_Designated_Type
2230 (Etype (Current_Scope), Available_View (Etyp));
2236 -- If this is the proper body of a stub, we must verify that the stub
2237 -- conforms to the body, and to the previous spec if one was present.
2238 -- we know already that the body conforms to that spec. This test is
2239 -- only required for subprograms that come from source.
2241 if Nkind (Parent (N)) = N_Subunit
2242 and then Comes_From_Source (N)
2243 and then not Error_Posted (Body_Id)
2244 and then Nkind (Corresponding_Stub (Parent (N))) =
2245 N_Subprogram_Body_Stub
2248 Old_Id : constant Entity_Id :=
2250 (Specification (Corresponding_Stub (Parent (N))));
2252 Conformant : Boolean := False;
2255 if No (Spec_Id) then
2256 Check_Fully_Conformant (Body_Id, Old_Id);
2260 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2262 if not Conformant then
2264 -- The stub was taken to be a new declaration. Indicate
2265 -- that it lacks a body.
2267 Set_Has_Completion (Old_Id, False);
2273 Set_Has_Completion (Body_Id);
2274 Check_Eliminated (Body_Id);
2276 if Nkind (N) = N_Subprogram_Body_Stub then
2279 elsif Present (Spec_Id)
2280 and then Expander_Active
2282 (Has_Pragma_Inline_Always (Spec_Id)
2283 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2285 Build_Body_To_Inline (N, Spec_Id);
2288 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2289 -- if its specification we have to install the private withed units.
2290 -- This holds for child units as well.
2292 if Is_Compilation_Unit (Body_Id)
2293 or else Nkind (Parent (N)) = N_Compilation_Unit
2295 Install_Private_With_Clauses (Body_Id);
2298 Check_Anonymous_Return;
2300 -- Set the Protected_Formal field of each extra formal of the protected
2301 -- subprogram to reference the corresponding extra formal of the
2302 -- subprogram that implements it. For regular formals this occurs when
2303 -- the protected subprogram's declaration is expanded, but the extra
2304 -- formals don't get created until the subprogram is frozen. We need to
2305 -- do this before analyzing the protected subprogram's body so that any
2306 -- references to the original subprogram's extra formals will be changed
2307 -- refer to the implementing subprogram's formals (see Expand_Formal).
2309 if Present (Spec_Id)
2310 and then Is_Protected_Type (Scope (Spec_Id))
2311 and then Present (Protected_Body_Subprogram (Spec_Id))
2314 Impl_Subp : constant Entity_Id :=
2315 Protected_Body_Subprogram (Spec_Id);
2316 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2317 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2319 while Present (Prot_Ext_Formal) loop
2320 pragma Assert (Present (Impl_Ext_Formal));
2321 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2322 Next_Formal_With_Extras (Prot_Ext_Formal);
2323 Next_Formal_With_Extras (Impl_Ext_Formal);
2328 -- Now we can go on to analyze the body
2330 HSS := Handled_Statement_Sequence (N);
2331 Set_Actual_Subtypes (N, Current_Scope);
2333 -- Deal with preconditions and postconditions
2335 Process_PPCs (N, Spec_Id, Body_Id);
2337 -- Add a declaration for the Protection object, renaming declarations
2338 -- for discriminals and privals and finally a declaration for the entry
2339 -- family index (if applicable). This form of early expansion is done
2340 -- when the Expander is active because Install_Private_Data_Declarations
2341 -- references entities which were created during regular expansion.
2344 and then Comes_From_Source (N)
2345 and then Present (Prot_Typ)
2346 and then Present (Spec_Id)
2347 and then not Is_Eliminated (Spec_Id)
2349 Install_Private_Data_Declarations
2350 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2353 -- Analyze the declarations (this call will analyze the precondition
2354 -- Check pragmas we prepended to the list, as well as the declaration
2355 -- of the _Postconditions procedure).
2357 Analyze_Declarations (Declarations (N));
2359 -- Check completion, and analyze the statements
2362 Inspect_Deferred_Constant_Completion (Declarations (N));
2365 -- Deal with end of scope processing for the body
2367 Process_End_Label (HSS, 't', Current_Scope);
2369 Check_Subprogram_Order (N);
2370 Set_Analyzed (Body_Id);
2372 -- If we have a separate spec, then the analysis of the declarations
2373 -- caused the entities in the body to be chained to the spec id, but
2374 -- we want them chained to the body id. Only the formal parameters
2375 -- end up chained to the spec id in this case.
2377 if Present (Spec_Id) then
2379 -- We must conform to the categorization of our spec
2381 Validate_Categorization_Dependency (N, Spec_Id);
2383 -- And if this is a child unit, the parent units must conform
2385 if Is_Child_Unit (Spec_Id) then
2386 Validate_Categorization_Dependency
2387 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2390 -- Here is where we move entities from the spec to the body
2392 -- Case where there are entities that stay with the spec
2394 if Present (Last_Real_Spec_Entity) then
2396 -- No body entities (happens when the only real spec entities
2397 -- come from precondition and postcondition pragmas)
2399 if No (Last_Entity (Body_Id)) then
2401 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2403 -- Body entities present (formals), so chain stuff past them
2407 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2410 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2411 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2412 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2414 -- Case where there are no spec entities, in this case there can
2415 -- be no body entities either, so just move everything.
2418 pragma Assert (No (Last_Entity (Body_Id)));
2419 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2420 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2421 Set_First_Entity (Spec_Id, Empty);
2422 Set_Last_Entity (Spec_Id, Empty);
2426 -- If function, check return statements
2428 if Nkind (Body_Spec) = N_Function_Specification then
2433 if Present (Spec_Id) then
2439 if Return_Present (Id) then
2440 Check_Returns (HSS, 'F', Missing_Ret);
2443 Set_Has_Missing_Return (Id);
2446 elsif not Is_Machine_Code_Subprogram (Id)
2447 and then not Body_Deleted
2449 Error_Msg_N ("missing RETURN statement in function body", N);
2453 -- If procedure with No_Return, check returns
2455 elsif Nkind (Body_Spec) = N_Procedure_Specification
2456 and then Present (Spec_Id)
2457 and then No_Return (Spec_Id)
2459 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2462 -- Now we are going to check for variables that are never modified in
2463 -- the body of the procedure. But first we deal with a special case
2464 -- where we want to modify this check. If the body of the subprogram
2465 -- starts with a raise statement or its equivalent, or if the body
2466 -- consists entirely of a null statement, then it is pretty obvious
2467 -- that it is OK to not reference the parameters. For example, this
2468 -- might be the following common idiom for a stubbed function:
2469 -- statement of the procedure raises an exception. In particular this
2470 -- deals with the common idiom of a stubbed function, which might
2471 -- appear as something like
2473 -- function F (A : Integer) return Some_Type;
2476 -- raise Program_Error;
2480 -- Here the purpose of X is simply to satisfy the annoying requirement
2481 -- in Ada that there be at least one return, and we certainly do not
2482 -- want to go posting warnings on X that it is not initialized! On
2483 -- the other hand, if X is entirely unreferenced that should still
2486 -- What we do is to detect these cases, and if we find them, flag the
2487 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2488 -- suppress unwanted warnings. For the case of the function stub above
2489 -- we have a special test to set X as apparently assigned to suppress
2496 -- Skip initial labels (for one thing this occurs when we are in
2497 -- front end ZCX mode, but in any case it is irrelevant), and also
2498 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2500 Stm := First (Statements (HSS));
2501 while Nkind (Stm) = N_Label
2502 or else Nkind (Stm) in N_Push_xxx_Label
2507 -- Do the test on the original statement before expansion
2510 Ostm : constant Node_Id := Original_Node (Stm);
2513 -- If explicit raise statement, turn on flag
2515 if Nkind (Ostm) = N_Raise_Statement then
2516 Set_Trivial_Subprogram (Stm);
2518 -- If null statement, and no following statements, turn on flag
2520 elsif Nkind (Stm) = N_Null_Statement
2521 and then Comes_From_Source (Stm)
2522 and then No (Next (Stm))
2524 Set_Trivial_Subprogram (Stm);
2526 -- Check for explicit call cases which likely raise an exception
2528 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2529 if Is_Entity_Name (Name (Ostm)) then
2531 Ent : constant Entity_Id := Entity (Name (Ostm));
2534 -- If the procedure is marked No_Return, then likely it
2535 -- raises an exception, but in any case it is not coming
2536 -- back here, so turn on the flag.
2538 if Ekind (Ent) = E_Procedure
2539 and then No_Return (Ent)
2541 Set_Trivial_Subprogram (Stm);
2549 -- Check for variables that are never modified
2555 -- If there is a separate spec, then transfer Never_Set_In_Source
2556 -- flags from out parameters to the corresponding entities in the
2557 -- body. The reason we do that is we want to post error flags on
2558 -- the body entities, not the spec entities.
2560 if Present (Spec_Id) then
2561 E1 := First_Entity (Spec_Id);
2562 while Present (E1) loop
2563 if Ekind (E1) = E_Out_Parameter then
2564 E2 := First_Entity (Body_Id);
2565 while Present (E2) loop
2566 exit when Chars (E1) = Chars (E2);
2570 if Present (E2) then
2571 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2579 -- Check references in body unless it was deleted. Note that the
2580 -- check of Body_Deleted here is not just for efficiency, it is
2581 -- necessary to avoid junk warnings on formal parameters.
2583 if not Body_Deleted then
2584 Check_References (Body_Id);
2587 end Analyze_Subprogram_Body_Helper;
2589 ------------------------------------
2590 -- Analyze_Subprogram_Declaration --
2591 ------------------------------------
2593 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2594 Loc : constant Source_Ptr := Sloc (N);
2595 Designator : Entity_Id;
2597 Scop : constant Entity_Id := Current_Scope;
2598 Null_Body : Node_Id := Empty;
2600 -- Start of processing for Analyze_Subprogram_Declaration
2603 -- For a null procedure, capture the profile before analysis, for
2604 -- expansion at the freeze point and at each point of call.
2605 -- The body will only be used if the procedure has preconditions.
2606 -- In that case the body is analyzed at the freeze point.
2608 if Nkind (Specification (N)) = N_Procedure_Specification
2609 and then Null_Present (Specification (N))
2610 and then Expander_Active
2613 Make_Subprogram_Body (Loc,
2615 New_Copy_Tree (Specification (N)),
2618 Handled_Statement_Sequence =>
2619 Make_Handled_Sequence_Of_Statements (Loc,
2620 Statements => New_List (Make_Null_Statement (Loc))));
2622 -- Create new entities for body and formals
2624 Set_Defining_Unit_Name (Specification (Null_Body),
2625 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2626 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2628 Form := First (Parameter_Specifications (Specification (Null_Body)));
2629 while Present (Form) loop
2630 Set_Defining_Identifier (Form,
2631 Make_Defining_Identifier (Loc,
2632 Chars (Defining_Identifier (Form))));
2636 if Is_Protected_Type (Current_Scope) then
2638 ("protected operation cannot be a null procedure", N);
2642 Designator := Analyze_Subprogram_Specification (Specification (N));
2643 Generate_Definition (Designator);
2645 if Debug_Flag_C then
2646 Write_Str ("==> subprogram spec ");
2647 Write_Name (Chars (Designator));
2648 Write_Str (" from ");
2649 Write_Location (Sloc (N));
2654 if Nkind (Specification (N)) = N_Procedure_Specification
2655 and then Null_Present (Specification (N))
2657 Set_Has_Completion (Designator);
2659 if Present (Null_Body) then
2660 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2661 Set_Body_To_Inline (N, Null_Body);
2662 Set_Is_Inlined (Designator);
2666 Validate_RCI_Subprogram_Declaration (N);
2667 New_Overloaded_Entity (Designator);
2668 Check_Delayed_Subprogram (Designator);
2670 -- If the type of the first formal of the current subprogram is a
2671 -- nongeneric tagged private type, mark the subprogram as being a
2672 -- private primitive. Ditto if this is a function with controlling
2673 -- result, and the return type is currently private. In both cases,
2674 -- the type of the controlling argument or result must be in the
2675 -- current scope for the operation to be primitive.
2677 if Has_Controlling_Result (Designator)
2678 and then Is_Private_Type (Etype (Designator))
2679 and then Scope (Etype (Designator)) = Current_Scope
2680 and then not Is_Generic_Actual_Type (Etype (Designator))
2682 Set_Is_Private_Primitive (Designator);
2684 elsif Present (First_Formal (Designator)) then
2686 Formal_Typ : constant Entity_Id :=
2687 Etype (First_Formal (Designator));
2689 Set_Is_Private_Primitive (Designator,
2690 Is_Tagged_Type (Formal_Typ)
2691 and then Scope (Formal_Typ) = Current_Scope
2692 and then Is_Private_Type (Formal_Typ)
2693 and then not Is_Generic_Actual_Type (Formal_Typ));
2697 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2700 if Ada_Version >= Ada_05
2701 and then Comes_From_Source (N)
2702 and then Is_Dispatching_Operation (Designator)
2709 if Has_Controlling_Result (Designator) then
2710 Etyp := Etype (Designator);
2713 E := First_Entity (Designator);
2715 and then Is_Formal (E)
2716 and then not Is_Controlling_Formal (E)
2724 if Is_Access_Type (Etyp) then
2725 Etyp := Directly_Designated_Type (Etyp);
2728 if Is_Interface (Etyp)
2729 and then not Is_Abstract_Subprogram (Designator)
2730 and then not (Ekind (Designator) = E_Procedure
2731 and then Null_Present (Specification (N)))
2733 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2735 ("(Ada 2005) interface subprogram % must be abstract or null",
2741 -- What is the following code for, it used to be
2743 -- ??? Set_Suppress_Elaboration_Checks
2744 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2746 -- The following seems equivalent, but a bit dubious
2748 if Elaboration_Checks_Suppressed (Designator) then
2749 Set_Kill_Elaboration_Checks (Designator);
2752 if Scop /= Standard_Standard
2753 and then not Is_Child_Unit (Designator)
2755 Set_Categorization_From_Scope (Designator, Scop);
2757 -- For a compilation unit, check for library-unit pragmas
2759 Push_Scope (Designator);
2760 Set_Categorization_From_Pragmas (N);
2761 Validate_Categorization_Dependency (N, Designator);
2765 -- For a compilation unit, set body required. This flag will only be
2766 -- reset if a valid Import or Interface pragma is processed later on.
2768 if Nkind (Parent (N)) = N_Compilation_Unit then
2769 Set_Body_Required (Parent (N), True);
2771 if Ada_Version >= Ada_05
2772 and then Nkind (Specification (N)) = N_Procedure_Specification
2773 and then Null_Present (Specification (N))
2776 ("null procedure cannot be declared at library level", N);
2780 Generate_Reference_To_Formals (Designator);
2781 Check_Eliminated (Designator);
2783 if Debug_Flag_C then
2785 Write_Str ("<== subprogram spec ");
2786 Write_Name (Chars (Designator));
2787 Write_Str (" from ");
2788 Write_Location (Sloc (N));
2791 end Analyze_Subprogram_Declaration;
2793 --------------------------------------
2794 -- Analyze_Subprogram_Specification --
2795 --------------------------------------
2797 -- Reminder: N here really is a subprogram specification (not a subprogram
2798 -- declaration). This procedure is called to analyze the specification in
2799 -- both subprogram bodies and subprogram declarations (specs).
2801 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2802 Designator : constant Entity_Id := Defining_Entity (N);
2803 Formals : constant List_Id := Parameter_Specifications (N);
2805 -- Start of processing for Analyze_Subprogram_Specification
2808 Generate_Definition (Designator);
2810 if Nkind (N) = N_Function_Specification then
2811 Set_Ekind (Designator, E_Function);
2812 Set_Mechanism (Designator, Default_Mechanism);
2815 Set_Ekind (Designator, E_Procedure);
2816 Set_Etype (Designator, Standard_Void_Type);
2819 -- Introduce new scope for analysis of the formals and the return type
2821 Set_Scope (Designator, Current_Scope);
2823 if Present (Formals) then
2824 Push_Scope (Designator);
2825 Process_Formals (Formals, N);
2827 -- Ada 2005 (AI-345): If this is an overriding operation of an
2828 -- inherited interface operation, and the controlling type is
2829 -- a synchronized type, replace the type with its corresponding
2830 -- record, to match the proper signature of an overriding operation.
2831 -- Same processing for an access parameter whose designated type is
2832 -- derived from a synchronized interface.
2834 if Ada_Version >= Ada_05 then
2837 Formal_Typ : Entity_Id;
2838 Rec_Typ : Entity_Id;
2839 Desig_Typ : Entity_Id;
2842 Formal := First_Formal (Designator);
2843 while Present (Formal) loop
2844 Formal_Typ := Etype (Formal);
2846 if Is_Concurrent_Type (Formal_Typ)
2847 and then Present (Corresponding_Record_Type (Formal_Typ))
2849 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2851 if Present (Interfaces (Rec_Typ)) then
2852 Set_Etype (Formal, Rec_Typ);
2855 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
2856 Desig_Typ := Designated_Type (Formal_Typ);
2858 if Is_Concurrent_Type (Desig_Typ)
2859 and then Present (Corresponding_Record_Type (Desig_Typ))
2861 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
2863 if Present (Interfaces (Rec_Typ)) then
2864 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
2869 Next_Formal (Formal);
2876 -- The subprogram scope is pushed and popped around the processing of
2877 -- the return type for consistency with call above to Process_Formals
2878 -- (which itself can call Analyze_Return_Type), and to ensure that any
2879 -- itype created for the return type will be associated with the proper
2882 elsif Nkind (N) = N_Function_Specification then
2883 Push_Scope (Designator);
2885 Analyze_Return_Type (N);
2890 if Nkind (N) = N_Function_Specification then
2891 if Nkind (Designator) = N_Defining_Operator_Symbol then
2892 Valid_Operator_Definition (Designator);
2895 May_Need_Actuals (Designator);
2897 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2898 -- the subprogram is abstract also. This does not apply to renaming
2899 -- declarations, where abstractness is inherited.
2900 -- In case of primitives associated with abstract interface types
2901 -- the check is applied later (see Analyze_Subprogram_Declaration).
2903 if Is_Abstract_Type (Etype (Designator))
2904 and then not Is_Interface (Etype (Designator))
2905 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2906 and then Nkind (Parent (N)) /=
2907 N_Abstract_Subprogram_Declaration
2909 (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
2912 ("function that returns abstract type must be abstract", N);
2917 end Analyze_Subprogram_Specification;
2919 --------------------------
2920 -- Build_Body_To_Inline --
2921 --------------------------
2923 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
2924 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
2925 Original_Body : Node_Id;
2926 Body_To_Analyze : Node_Id;
2927 Max_Size : constant := 10;
2928 Stat_Count : Integer := 0;
2930 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
2931 -- Check for declarations that make inlining not worthwhile
2933 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
2934 -- Check for statements that make inlining not worthwhile: any tasking
2935 -- statement, nested at any level. Keep track of total number of
2936 -- elementary statements, as a measure of acceptable size.
2938 function Has_Pending_Instantiation return Boolean;
2939 -- If some enclosing body contains instantiations that appear before the
2940 -- corresponding generic body, the enclosing body has a freeze node so
2941 -- that it can be elaborated after the generic itself. This might
2942 -- conflict with subsequent inlinings, so that it is unsafe to try to
2943 -- inline in such a case.
2945 function Has_Single_Return return Boolean;
2946 -- In general we cannot inline functions that return unconstrained type.
2947 -- However, we can handle such functions if all return statements return
2948 -- a local variable that is the only declaration in the body of the
2949 -- function. In that case the call can be replaced by that local
2950 -- variable as is done for other inlined calls.
2952 procedure Remove_Pragmas;
2953 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
2954 -- parameter has no meaning when the body is inlined and the formals
2955 -- are rewritten. Remove it from body to inline. The analysis of the
2956 -- non-inlined body will handle the pragma properly.
2958 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
2959 -- If the body of the subprogram includes a call that returns an
2960 -- unconstrained type, the secondary stack is involved, and it
2961 -- is not worth inlining.
2963 ------------------------------
2964 -- Has_Excluded_Declaration --
2965 ------------------------------
2967 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
2970 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
2971 -- Nested subprograms make a given body ineligible for inlining, but
2972 -- we make an exception for instantiations of unchecked conversion.
2973 -- The body has not been analyzed yet, so check the name, and verify
2974 -- that the visible entity with that name is the predefined unit.
2976 -----------------------------
2977 -- Is_Unchecked_Conversion --
2978 -----------------------------
2980 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
2981 Id : constant Node_Id := Name (D);
2985 if Nkind (Id) = N_Identifier
2986 and then Chars (Id) = Name_Unchecked_Conversion
2988 Conv := Current_Entity (Id);
2990 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
2991 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
2993 Conv := Current_Entity (Selector_Name (Id));
2998 return Present (Conv)
2999 and then Is_Predefined_File_Name
3000 (Unit_File_Name (Get_Source_Unit (Conv)))
3001 and then Is_Intrinsic_Subprogram (Conv);
3002 end Is_Unchecked_Conversion;
3004 -- Start of processing for Has_Excluded_Declaration
3008 while Present (D) loop
3009 if (Nkind (D) = N_Function_Instantiation
3010 and then not Is_Unchecked_Conversion (D))
3011 or else Nkind_In (D, N_Protected_Type_Declaration,
3012 N_Package_Declaration,
3013 N_Package_Instantiation,
3015 N_Procedure_Instantiation,
3016 N_Task_Type_Declaration)
3019 ("cannot inline & (non-allowed declaration)?", D, Subp);
3027 end Has_Excluded_Declaration;
3029 ----------------------------
3030 -- Has_Excluded_Statement --
3031 ----------------------------
3033 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3039 while Present (S) loop
3040 Stat_Count := Stat_Count + 1;
3042 if Nkind_In (S, N_Abort_Statement,
3043 N_Asynchronous_Select,
3044 N_Conditional_Entry_Call,
3045 N_Delay_Relative_Statement,
3046 N_Delay_Until_Statement,
3051 ("cannot inline & (non-allowed statement)?", S, Subp);
3054 elsif Nkind (S) = N_Block_Statement then
3055 if Present (Declarations (S))
3056 and then Has_Excluded_Declaration (Declarations (S))
3060 elsif Present (Handled_Statement_Sequence (S))
3063 (Exception_Handlers (Handled_Statement_Sequence (S)))
3065 Has_Excluded_Statement
3066 (Statements (Handled_Statement_Sequence (S))))
3071 elsif Nkind (S) = N_Case_Statement then
3072 E := First (Alternatives (S));
3073 while Present (E) loop
3074 if Has_Excluded_Statement (Statements (E)) then
3081 elsif Nkind (S) = N_If_Statement then
3082 if Has_Excluded_Statement (Then_Statements (S)) then
3086 if Present (Elsif_Parts (S)) then
3087 E := First (Elsif_Parts (S));
3088 while Present (E) loop
3089 if Has_Excluded_Statement (Then_Statements (E)) then
3096 if Present (Else_Statements (S))
3097 and then Has_Excluded_Statement (Else_Statements (S))
3102 elsif Nkind (S) = N_Loop_Statement
3103 and then Has_Excluded_Statement (Statements (S))
3112 end Has_Excluded_Statement;
3114 -------------------------------
3115 -- Has_Pending_Instantiation --
3116 -------------------------------
3118 function Has_Pending_Instantiation return Boolean is
3123 while Present (S) loop
3124 if Is_Compilation_Unit (S)
3125 or else Is_Child_Unit (S)
3128 elsif Ekind (S) = E_Package
3129 and then Has_Forward_Instantiation (S)
3138 end Has_Pending_Instantiation;
3140 ------------------------
3141 -- Has_Single_Return --
3142 ------------------------
3144 function Has_Single_Return return Boolean is
3145 Return_Statement : Node_Id := Empty;
3147 function Check_Return (N : Node_Id) return Traverse_Result;
3153 function Check_Return (N : Node_Id) return Traverse_Result is
3155 if Nkind (N) = N_Simple_Return_Statement then
3156 if Present (Expression (N))
3157 and then Is_Entity_Name (Expression (N))
3159 if No (Return_Statement) then
3160 Return_Statement := N;
3163 elsif Chars (Expression (N)) =
3164 Chars (Expression (Return_Statement))
3173 -- Expression has wrong form
3183 function Check_All_Returns is new Traverse_Func (Check_Return);
3185 -- Start of processing for Has_Single_Return
3188 return Check_All_Returns (N) = OK
3189 and then Present (Declarations (N))
3190 and then Present (First (Declarations (N)))
3191 and then Chars (Expression (Return_Statement)) =
3192 Chars (Defining_Identifier (First (Declarations (N))));
3193 end Has_Single_Return;
3195 --------------------
3196 -- Remove_Pragmas --
3197 --------------------
3199 procedure Remove_Pragmas is
3204 Decl := First (Declarations (Body_To_Analyze));
3205 while Present (Decl) loop
3208 if Nkind (Decl) = N_Pragma
3209 and then (Pragma_Name (Decl) = Name_Unreferenced
3211 Pragma_Name (Decl) = Name_Unmodified)
3220 --------------------------
3221 -- Uses_Secondary_Stack --
3222 --------------------------
3224 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3225 function Check_Call (N : Node_Id) return Traverse_Result;
3226 -- Look for function calls that return an unconstrained type
3232 function Check_Call (N : Node_Id) return Traverse_Result is
3234 if Nkind (N) = N_Function_Call
3235 and then Is_Entity_Name (Name (N))
3236 and then Is_Composite_Type (Etype (Entity (Name (N))))
3237 and then not Is_Constrained (Etype (Entity (Name (N))))
3240 ("cannot inline & (call returns unconstrained type)?",
3248 function Check_Calls is new Traverse_Func (Check_Call);
3251 return Check_Calls (Bod) = Abandon;
3252 end Uses_Secondary_Stack;
3254 -- Start of processing for Build_Body_To_Inline
3257 -- Return immediately if done already
3259 if Nkind (Decl) = N_Subprogram_Declaration
3260 and then Present (Body_To_Inline (Decl))
3264 -- Functions that return unconstrained composite types require
3265 -- secondary stack handling, and cannot currently be inlined, unless
3266 -- all return statements return a local variable that is the first
3267 -- local declaration in the body.
3269 elsif Ekind (Subp) = E_Function
3270 and then not Is_Scalar_Type (Etype (Subp))
3271 and then not Is_Access_Type (Etype (Subp))
3272 and then not Is_Constrained (Etype (Subp))
3274 if not Has_Single_Return then
3276 ("cannot inline & (unconstrained return type)?", N, Subp);
3280 -- Ditto for functions that return controlled types, where controlled
3281 -- actions interfere in complex ways with inlining.
3283 elsif Ekind (Subp) = E_Function
3284 and then Needs_Finalization (Etype (Subp))
3287 ("cannot inline & (controlled return type)?", N, Subp);
3291 if Present (Declarations (N))
3292 and then Has_Excluded_Declaration (Declarations (N))
3297 if Present (Handled_Statement_Sequence (N)) then
3298 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3300 ("cannot inline& (exception handler)?",
3301 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3305 Has_Excluded_Statement
3306 (Statements (Handled_Statement_Sequence (N)))
3312 -- We do not inline a subprogram that is too large, unless it is
3313 -- marked Inline_Always. This pragma does not suppress the other
3314 -- checks on inlining (forbidden declarations, handlers, etc).
3316 if Stat_Count > Max_Size
3317 and then not Has_Pragma_Inline_Always (Subp)
3319 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3323 if Has_Pending_Instantiation then
3325 ("cannot inline& (forward instance within enclosing body)?",
3330 -- Within an instance, the body to inline must be treated as a nested
3331 -- generic, so that the proper global references are preserved.
3333 -- Note that we do not do this at the library level, because it is not
3334 -- needed, and furthermore this causes trouble if front end inlining
3335 -- is activated (-gnatN).
3337 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3338 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3339 Original_Body := Copy_Generic_Node (N, Empty, True);
3341 Original_Body := Copy_Separate_Tree (N);
3344 -- We need to capture references to the formals in order to substitute
3345 -- the actuals at the point of inlining, i.e. instantiation. To treat
3346 -- the formals as globals to the body to inline, we nest it within
3347 -- a dummy parameterless subprogram, declared within the real one.
3348 -- To avoid generating an internal name (which is never public, and
3349 -- which affects serial numbers of other generated names), we use
3350 -- an internal symbol that cannot conflict with user declarations.
3352 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3353 Set_Defining_Unit_Name
3354 (Specification (Original_Body),
3355 Make_Defining_Identifier (Sloc (N), Name_uParent));
3356 Set_Corresponding_Spec (Original_Body, Empty);
3358 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3360 -- Set return type of function, which is also global and does not need
3363 if Ekind (Subp) = E_Function then
3364 Set_Result_Definition (Specification (Body_To_Analyze),
3365 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3368 if No (Declarations (N)) then
3369 Set_Declarations (N, New_List (Body_To_Analyze));
3371 Append (Body_To_Analyze, Declarations (N));
3374 Expander_Mode_Save_And_Set (False);
3377 Analyze (Body_To_Analyze);
3378 Push_Scope (Defining_Entity (Body_To_Analyze));
3379 Save_Global_References (Original_Body);
3381 Remove (Body_To_Analyze);
3383 Expander_Mode_Restore;
3385 -- Restore environment if previously saved
3387 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3391 -- If secondary stk used there is no point in inlining. We have
3392 -- already issued the warning in this case, so nothing to do.
3394 if Uses_Secondary_Stack (Body_To_Analyze) then
3398 Set_Body_To_Inline (Decl, Original_Body);
3399 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3400 Set_Is_Inlined (Subp);
3401 end Build_Body_To_Inline;
3407 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3409 -- Do not emit warning if this is a predefined unit which is not
3410 -- the main unit. With validity checks enabled, some predefined
3411 -- subprograms may contain nested subprograms and become ineligible
3414 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3415 and then not In_Extended_Main_Source_Unit (Subp)
3419 elsif Has_Pragma_Inline_Always (Subp) then
3421 -- Remove last character (question mark) to make this into an error,
3422 -- because the Inline_Always pragma cannot be obeyed.
3424 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3426 elsif Ineffective_Inline_Warnings then
3427 Error_Msg_NE (Msg, N, Subp);
3431 -----------------------
3432 -- Check_Conformance --
3433 -----------------------
3435 procedure Check_Conformance
3436 (New_Id : Entity_Id;
3438 Ctype : Conformance_Type;
3440 Conforms : out Boolean;
3441 Err_Loc : Node_Id := Empty;
3442 Get_Inst : Boolean := False;
3443 Skip_Controlling_Formals : Boolean := False)
3445 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3446 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3447 -- If Errmsg is True, then processing continues to post an error message
3448 -- for conformance error on given node. Two messages are output. The
3449 -- first message points to the previous declaration with a general "no
3450 -- conformance" message. The second is the detailed reason, supplied as
3451 -- Msg. The parameter N provide information for a possible & insertion
3452 -- in the message, and also provides the location for posting the
3453 -- message in the absence of a specified Err_Loc location.
3455 -----------------------
3456 -- Conformance_Error --
3457 -----------------------
3459 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3466 if No (Err_Loc) then
3472 Error_Msg_Sloc := Sloc (Old_Id);
3475 when Type_Conformant =>
3476 Error_Msg_N -- CODEFIX
3477 ("not type conformant with declaration#!", Enode);
3479 when Mode_Conformant =>
3480 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3481 Error_Msg_N -- CODEFIX???
3482 ("not mode conformant with operation inherited#!",
3485 Error_Msg_N -- CODEFIX???
3486 ("not mode conformant with declaration#!", Enode);
3489 when Subtype_Conformant =>
3490 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3491 Error_Msg_N -- CODEFIX???
3492 ("not subtype conformant with operation inherited#!",
3495 Error_Msg_N -- CODEFIX???
3496 ("not subtype conformant with declaration#!", Enode);
3499 when Fully_Conformant =>
3500 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3501 Error_Msg_N -- CODEFIX
3502 ("not fully conformant with operation inherited#!",
3505 Error_Msg_N -- CODEFIX
3506 ("not fully conformant with declaration#!", Enode);
3510 Error_Msg_NE (Msg, Enode, N);
3512 end Conformance_Error;
3516 Old_Type : constant Entity_Id := Etype (Old_Id);
3517 New_Type : constant Entity_Id := Etype (New_Id);
3518 Old_Formal : Entity_Id;
3519 New_Formal : Entity_Id;
3520 Access_Types_Match : Boolean;
3521 Old_Formal_Base : Entity_Id;
3522 New_Formal_Base : Entity_Id;
3524 -- Start of processing for Check_Conformance
3529 -- We need a special case for operators, since they don't appear
3532 if Ctype = Type_Conformant then
3533 if Ekind (New_Id) = E_Operator
3534 and then Operator_Matches_Spec (New_Id, Old_Id)
3540 -- If both are functions/operators, check return types conform
3542 if Old_Type /= Standard_Void_Type
3543 and then New_Type /= Standard_Void_Type
3546 -- If we are checking interface conformance we omit controlling
3547 -- arguments and result, because we are only checking the conformance
3548 -- of the remaining parameters.
3550 if Has_Controlling_Result (Old_Id)
3551 and then Has_Controlling_Result (New_Id)
3552 and then Skip_Controlling_Formals
3556 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3557 Conformance_Error ("\return type does not match!", New_Id);
3561 -- Ada 2005 (AI-231): In case of anonymous access types check the
3562 -- null-exclusion and access-to-constant attributes match.
3564 if Ada_Version >= Ada_05
3565 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3567 (Can_Never_Be_Null (Old_Type)
3568 /= Can_Never_Be_Null (New_Type)
3569 or else Is_Access_Constant (Etype (Old_Type))
3570 /= Is_Access_Constant (Etype (New_Type)))
3572 Conformance_Error ("\return type does not match!", New_Id);
3576 -- If either is a function/operator and the other isn't, error
3578 elsif Old_Type /= Standard_Void_Type
3579 or else New_Type /= Standard_Void_Type
3581 Conformance_Error ("\functions can only match functions!", New_Id);
3585 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3586 -- If this is a renaming as body, refine error message to indicate that
3587 -- the conflict is with the original declaration. If the entity is not
3588 -- frozen, the conventions don't have to match, the one of the renamed
3589 -- entity is inherited.
3591 if Ctype >= Subtype_Conformant then
3592 if Convention (Old_Id) /= Convention (New_Id) then
3594 if not Is_Frozen (New_Id) then
3597 elsif Present (Err_Loc)
3598 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3599 and then Present (Corresponding_Spec (Err_Loc))
3601 Error_Msg_Name_1 := Chars (New_Id);
3603 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3605 Conformance_Error ("\prior declaration for% has convention %!");
3608 Conformance_Error ("\calling conventions do not match!");
3613 elsif Is_Formal_Subprogram (Old_Id)
3614 or else Is_Formal_Subprogram (New_Id)
3616 Conformance_Error ("\formal subprograms not allowed!");
3621 -- Deal with parameters
3623 -- Note: we use the entity information, rather than going directly
3624 -- to the specification in the tree. This is not only simpler, but
3625 -- absolutely necessary for some cases of conformance tests between
3626 -- operators, where the declaration tree simply does not exist!
3628 Old_Formal := First_Formal (Old_Id);
3629 New_Formal := First_Formal (New_Id);
3630 while Present (Old_Formal) and then Present (New_Formal) loop
3631 if Is_Controlling_Formal (Old_Formal)
3632 and then Is_Controlling_Formal (New_Formal)
3633 and then Skip_Controlling_Formals
3635 -- The controlling formals will have different types when
3636 -- comparing an interface operation with its match, but both
3637 -- or neither must be access parameters.
3639 if Is_Access_Type (Etype (Old_Formal))
3641 Is_Access_Type (Etype (New_Formal))
3643 goto Skip_Controlling_Formal;
3646 ("\access parameter does not match!", New_Formal);
3650 if Ctype = Fully_Conformant then
3652 -- Names must match. Error message is more accurate if we do
3653 -- this before checking that the types of the formals match.
3655 if Chars (Old_Formal) /= Chars (New_Formal) then
3656 Conformance_Error ("\name & does not match!", New_Formal);
3658 -- Set error posted flag on new formal as well to stop
3659 -- junk cascaded messages in some cases.
3661 Set_Error_Posted (New_Formal);
3666 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3667 -- case occurs whenever a subprogram is being renamed and one of its
3668 -- parameters imposes a null exclusion. For example:
3670 -- type T is null record;
3671 -- type Acc_T is access T;
3672 -- subtype Acc_T_Sub is Acc_T;
3674 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3675 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3678 Old_Formal_Base := Etype (Old_Formal);
3679 New_Formal_Base := Etype (New_Formal);
3682 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3683 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3686 Access_Types_Match := Ada_Version >= Ada_05
3688 -- Ensure that this rule is only applied when New_Id is a
3689 -- renaming of Old_Id.
3691 and then Nkind (Parent (Parent (New_Id))) =
3692 N_Subprogram_Renaming_Declaration
3693 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3694 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3695 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3697 -- Now handle the allowed access-type case
3699 and then Is_Access_Type (Old_Formal_Base)
3700 and then Is_Access_Type (New_Formal_Base)
3702 -- The type kinds must match. The only exception occurs with
3703 -- multiple generics of the form:
3706 -- type F is private; type A is private;
3707 -- type F_Ptr is access F; type A_Ptr is access A;
3708 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3709 -- package F_Pack is ... package A_Pack is
3710 -- package F_Inst is
3711 -- new F_Pack (A, A_Ptr, A_P);
3713 -- When checking for conformance between the parameters of A_P
3714 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3715 -- because the compiler has transformed A_Ptr into a subtype of
3716 -- F_Ptr. We catch this case in the code below.
3718 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3720 (Is_Generic_Type (Old_Formal_Base)
3721 and then Is_Generic_Type (New_Formal_Base)
3722 and then Is_Internal (New_Formal_Base)
3723 and then Etype (Etype (New_Formal_Base)) =
3725 and then Directly_Designated_Type (Old_Formal_Base) =
3726 Directly_Designated_Type (New_Formal_Base)
3727 and then ((Is_Itype (Old_Formal_Base)
3728 and then Can_Never_Be_Null (Old_Formal_Base))
3730 (Is_Itype (New_Formal_Base)
3731 and then Can_Never_Be_Null (New_Formal_Base)));
3733 -- Types must always match. In the visible part of an instance,
3734 -- usual overloading rules for dispatching operations apply, and
3735 -- we check base types (not the actual subtypes).
3737 if In_Instance_Visible_Part
3738 and then Is_Dispatching_Operation (New_Id)
3740 if not Conforming_Types
3741 (T1 => Base_Type (Etype (Old_Formal)),
3742 T2 => Base_Type (Etype (New_Formal)),
3744 Get_Inst => Get_Inst)
3745 and then not Access_Types_Match
3747 Conformance_Error ("\type of & does not match!", New_Formal);
3751 elsif not Conforming_Types
3752 (T1 => Old_Formal_Base,
3753 T2 => New_Formal_Base,
3755 Get_Inst => Get_Inst)
3756 and then not Access_Types_Match
3758 -- Don't give error message if old type is Any_Type. This test
3759 -- avoids some cascaded errors, e.g. in case of a bad spec.
3761 if Errmsg and then Old_Formal_Base = Any_Type then
3764 Conformance_Error ("\type of & does not match!", New_Formal);
3770 -- For mode conformance, mode must match
3772 if Ctype >= Mode_Conformant then
3773 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3774 Conformance_Error ("\mode of & does not match!", New_Formal);
3777 -- Part of mode conformance for access types is having the same
3778 -- constant modifier.
3780 elsif Access_Types_Match
3781 and then Is_Access_Constant (Old_Formal_Base) /=
3782 Is_Access_Constant (New_Formal_Base)
3785 ("\constant modifier does not match!", New_Formal);
3790 if Ctype >= Subtype_Conformant then
3792 -- Ada 2005 (AI-231): In case of anonymous access types check
3793 -- the null-exclusion and access-to-constant attributes must
3796 if Ada_Version >= Ada_05
3797 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3798 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3800 (Can_Never_Be_Null (Old_Formal) /=
3801 Can_Never_Be_Null (New_Formal)
3803 Is_Access_Constant (Etype (Old_Formal)) /=
3804 Is_Access_Constant (Etype (New_Formal)))
3806 -- It is allowed to omit the null-exclusion in case of stream
3807 -- attribute subprograms. We recognize stream subprograms
3808 -- through their TSS-generated suffix.
3811 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3813 if TSS_Name /= TSS_Stream_Read
3814 and then TSS_Name /= TSS_Stream_Write
3815 and then TSS_Name /= TSS_Stream_Input
3816 and then TSS_Name /= TSS_Stream_Output
3819 ("\type of & does not match!", New_Formal);
3826 -- Full conformance checks
3828 if Ctype = Fully_Conformant then
3830 -- We have checked already that names match
3832 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3834 -- Check default expressions for in parameters
3837 NewD : constant Boolean :=
3838 Present (Default_Value (New_Formal));
3839 OldD : constant Boolean :=
3840 Present (Default_Value (Old_Formal));
3842 if NewD or OldD then
3844 -- The old default value has been analyzed because the
3845 -- current full declaration will have frozen everything
3846 -- before. The new default value has not been analyzed,
3847 -- so analyze it now before we check for conformance.
3850 Push_Scope (New_Id);
3851 Preanalyze_Spec_Expression
3852 (Default_Value (New_Formal), Etype (New_Formal));
3856 if not (NewD and OldD)
3857 or else not Fully_Conformant_Expressions
3858 (Default_Value (Old_Formal),
3859 Default_Value (New_Formal))
3862 ("\default expression for & does not match!",
3871 -- A couple of special checks for Ada 83 mode. These checks are
3872 -- skipped if either entity is an operator in package Standard,
3873 -- or if either old or new instance is not from the source program.
3875 if Ada_Version = Ada_83
3876 and then Sloc (Old_Id) > Standard_Location
3877 and then Sloc (New_Id) > Standard_Location
3878 and then Comes_From_Source (Old_Id)
3879 and then Comes_From_Source (New_Id)
3882 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
3883 New_Param : constant Node_Id := Declaration_Node (New_Formal);
3886 -- Explicit IN must be present or absent in both cases. This
3887 -- test is required only in the full conformance case.
3889 if In_Present (Old_Param) /= In_Present (New_Param)
3890 and then Ctype = Fully_Conformant
3893 ("\(Ada 83) IN must appear in both declarations",
3898 -- Grouping (use of comma in param lists) must be the same
3899 -- This is where we catch a misconformance like:
3902 -- A : Integer; B : Integer
3904 -- which are represented identically in the tree except
3905 -- for the setting of the flags More_Ids and Prev_Ids.
3907 if More_Ids (Old_Param) /= More_Ids (New_Param)
3908 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
3911 ("\grouping of & does not match!", New_Formal);
3917 -- This label is required when skipping controlling formals
3919 <<Skip_Controlling_Formal>>
3921 Next_Formal (Old_Formal);
3922 Next_Formal (New_Formal);
3925 if Present (Old_Formal) then
3926 Conformance_Error ("\too few parameters!");
3929 elsif Present (New_Formal) then
3930 Conformance_Error ("\too many parameters!", New_Formal);
3933 end Check_Conformance;
3935 -----------------------
3936 -- Check_Conventions --
3937 -----------------------
3939 procedure Check_Conventions (Typ : Entity_Id) is
3940 Ifaces_List : Elist_Id;
3942 procedure Check_Convention (Op : Entity_Id);
3943 -- Verify that the convention of inherited dispatching operation Op is
3944 -- consistent among all subprograms it overrides. In order to minimize
3945 -- the search, Search_From is utilized to designate a specific point in
3946 -- the list rather than iterating over the whole list once more.
3948 ----------------------
3949 -- Check_Convention --
3950 ----------------------
3952 procedure Check_Convention (Op : Entity_Id) is
3953 Iface_Elmt : Elmt_Id;
3954 Iface_Prim_Elmt : Elmt_Id;
3955 Iface_Prim : Entity_Id;
3958 Iface_Elmt := First_Elmt (Ifaces_List);
3959 while Present (Iface_Elmt) loop
3961 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
3962 while Present (Iface_Prim_Elmt) loop
3963 Iface_Prim := Node (Iface_Prim_Elmt);
3965 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
3966 and then Convention (Iface_Prim) /= Convention (Op)
3969 ("inconsistent conventions in primitive operations", Typ);
3971 Error_Msg_Name_1 := Chars (Op);
3972 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
3973 Error_Msg_Sloc := Sloc (Op);
3975 if Comes_From_Source (Op) then
3976 if not Is_Overriding_Operation (Op) then
3977 Error_Msg_N ("\\primitive % defined #", Typ);
3979 Error_Msg_N ("\\overriding operation % with " &
3980 "convention % defined #", Typ);
3983 else pragma Assert (Present (Alias (Op)));
3984 Error_Msg_Sloc := Sloc (Alias (Op));
3985 Error_Msg_N ("\\inherited operation % with " &
3986 "convention % defined #", Typ);
3989 Error_Msg_Name_1 := Chars (Op);
3991 Get_Convention_Name (Convention (Iface_Prim));
3992 Error_Msg_Sloc := Sloc (Iface_Prim);
3993 Error_Msg_N ("\\overridden operation % with " &
3994 "convention % defined #", Typ);
3996 -- Avoid cascading errors
4001 Next_Elmt (Iface_Prim_Elmt);
4004 Next_Elmt (Iface_Elmt);
4006 end Check_Convention;
4010 Prim_Op : Entity_Id;
4011 Prim_Op_Elmt : Elmt_Id;
4013 -- Start of processing for Check_Conventions
4016 if not Has_Interfaces (Typ) then
4020 Collect_Interfaces (Typ, Ifaces_List);
4022 -- The algorithm checks every overriding dispatching operation against
4023 -- all the corresponding overridden dispatching operations, detecting
4024 -- differences in conventions.
4026 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4027 while Present (Prim_Op_Elmt) loop
4028 Prim_Op := Node (Prim_Op_Elmt);
4030 -- A small optimization: skip the predefined dispatching operations
4031 -- since they always have the same convention.
4033 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4034 Check_Convention (Prim_Op);
4037 Next_Elmt (Prim_Op_Elmt);
4039 end Check_Conventions;
4041 ------------------------------
4042 -- Check_Delayed_Subprogram --
4043 ------------------------------
4045 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4048 procedure Possible_Freeze (T : Entity_Id);
4049 -- T is the type of either a formal parameter or of the return type.
4050 -- If T is not yet frozen and needs a delayed freeze, then the
4051 -- subprogram itself must be delayed. If T is the limited view of an
4052 -- incomplete type the subprogram must be frozen as well, because
4053 -- T may depend on local types that have not been frozen yet.
4055 ---------------------
4056 -- Possible_Freeze --
4057 ---------------------
4059 procedure Possible_Freeze (T : Entity_Id) is
4061 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4062 Set_Has_Delayed_Freeze (Designator);
4064 elsif Is_Access_Type (T)
4065 and then Has_Delayed_Freeze (Designated_Type (T))
4066 and then not Is_Frozen (Designated_Type (T))
4068 Set_Has_Delayed_Freeze (Designator);
4070 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4071 Set_Has_Delayed_Freeze (Designator);
4074 end Possible_Freeze;
4076 -- Start of processing for Check_Delayed_Subprogram
4079 -- Never need to freeze abstract subprogram
4081 if Ekind (Designator) /= E_Subprogram_Type
4082 and then Is_Abstract_Subprogram (Designator)
4086 -- Need delayed freeze if return type itself needs a delayed
4087 -- freeze and is not yet frozen.
4089 Possible_Freeze (Etype (Designator));
4090 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4092 -- Need delayed freeze if any of the formal types themselves need
4093 -- a delayed freeze and are not yet frozen.
4095 F := First_Formal (Designator);
4096 while Present (F) loop
4097 Possible_Freeze (Etype (F));
4098 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4103 -- Mark functions that return by reference. Note that it cannot be
4104 -- done for delayed_freeze subprograms because the underlying
4105 -- returned type may not be known yet (for private types)
4107 if not Has_Delayed_Freeze (Designator)
4108 and then Expander_Active
4111 Typ : constant Entity_Id := Etype (Designator);
4112 Utyp : constant Entity_Id := Underlying_Type (Typ);
4115 if Is_Inherently_Limited_Type (Typ) then
4116 Set_Returns_By_Ref (Designator);
4118 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4119 Set_Returns_By_Ref (Designator);
4123 end Check_Delayed_Subprogram;
4125 ------------------------------------
4126 -- Check_Discriminant_Conformance --
4127 ------------------------------------
4129 procedure Check_Discriminant_Conformance
4134 Old_Discr : Entity_Id := First_Discriminant (Prev);
4135 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4136 New_Discr_Id : Entity_Id;
4137 New_Discr_Type : Entity_Id;
4139 procedure Conformance_Error (Msg : String; N : Node_Id);
4140 -- Post error message for conformance error on given node. Two messages
4141 -- are output. The first points to the previous declaration with a
4142 -- general "no conformance" message. The second is the detailed reason,
4143 -- supplied as Msg. The parameter N provide information for a possible
4144 -- & insertion in the message.
4146 -----------------------
4147 -- Conformance_Error --
4148 -----------------------
4150 procedure Conformance_Error (Msg : String; N : Node_Id) is
4152 Error_Msg_Sloc := Sloc (Prev_Loc);
4153 Error_Msg_N -- CODEFIX
4154 ("not fully conformant with declaration#!", N);
4155 Error_Msg_NE (Msg, N, N);
4156 end Conformance_Error;
4158 -- Start of processing for Check_Discriminant_Conformance
4161 while Present (Old_Discr) and then Present (New_Discr) loop
4163 New_Discr_Id := Defining_Identifier (New_Discr);
4165 -- The subtype mark of the discriminant on the full type has not
4166 -- been analyzed so we do it here. For an access discriminant a new
4169 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4171 Access_Definition (N, Discriminant_Type (New_Discr));
4174 Analyze (Discriminant_Type (New_Discr));
4175 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4177 -- Ada 2005: if the discriminant definition carries a null
4178 -- exclusion, create an itype to check properly for consistency
4179 -- with partial declaration.
4181 if Is_Access_Type (New_Discr_Type)
4182 and then Null_Exclusion_Present (New_Discr)
4185 Create_Null_Excluding_Itype
4186 (T => New_Discr_Type,
4187 Related_Nod => New_Discr,
4188 Scope_Id => Current_Scope);
4192 if not Conforming_Types
4193 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4195 Conformance_Error ("type of & does not match!", New_Discr_Id);
4198 -- Treat the new discriminant as an occurrence of the old one,
4199 -- for navigation purposes, and fill in some semantic
4200 -- information, for completeness.
4202 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4203 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4204 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4209 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4210 Conformance_Error ("name & does not match!", New_Discr_Id);
4214 -- Default expressions must match
4217 NewD : constant Boolean :=
4218 Present (Expression (New_Discr));
4219 OldD : constant Boolean :=
4220 Present (Expression (Parent (Old_Discr)));
4223 if NewD or OldD then
4225 -- The old default value has been analyzed and expanded,
4226 -- because the current full declaration will have frozen
4227 -- everything before. The new default values have not been
4228 -- expanded, so expand now to check conformance.
4231 Preanalyze_Spec_Expression
4232 (Expression (New_Discr), New_Discr_Type);
4235 if not (NewD and OldD)
4236 or else not Fully_Conformant_Expressions
4237 (Expression (Parent (Old_Discr)),
4238 Expression (New_Discr))
4242 ("default expression for & does not match!",
4249 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4251 if Ada_Version = Ada_83 then
4253 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4256 -- Grouping (use of comma in param lists) must be the same
4257 -- This is where we catch a misconformance like:
4260 -- A : Integer; B : Integer
4262 -- which are represented identically in the tree except
4263 -- for the setting of the flags More_Ids and Prev_Ids.
4265 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4266 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4269 ("grouping of & does not match!", New_Discr_Id);
4275 Next_Discriminant (Old_Discr);
4279 if Present (Old_Discr) then
4280 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4283 elsif Present (New_Discr) then
4285 ("too many discriminants!", Defining_Identifier (New_Discr));
4288 end Check_Discriminant_Conformance;
4290 ----------------------------
4291 -- Check_Fully_Conformant --
4292 ----------------------------
4294 procedure Check_Fully_Conformant
4295 (New_Id : Entity_Id;
4297 Err_Loc : Node_Id := Empty)
4300 pragma Warnings (Off, Result);
4303 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4304 end Check_Fully_Conformant;
4306 ---------------------------
4307 -- Check_Mode_Conformant --
4308 ---------------------------
4310 procedure Check_Mode_Conformant
4311 (New_Id : Entity_Id;
4313 Err_Loc : Node_Id := Empty;
4314 Get_Inst : Boolean := False)
4317 pragma Warnings (Off, Result);
4320 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4321 end Check_Mode_Conformant;
4323 --------------------------------
4324 -- Check_Overriding_Indicator --
4325 --------------------------------
4327 procedure Check_Overriding_Indicator
4329 Overridden_Subp : Entity_Id;
4330 Is_Primitive : Boolean)
4336 -- No overriding indicator for literals
4338 if Ekind (Subp) = E_Enumeration_Literal then
4341 elsif Ekind (Subp) = E_Entry then
4342 Decl := Parent (Subp);
4344 -- No point in analyzing a malformed operator
4346 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4347 and then Error_Posted (Subp)
4352 Decl := Unit_Declaration_Node (Subp);
4355 if Nkind_In (Decl, N_Subprogram_Body,
4356 N_Subprogram_Body_Stub,
4357 N_Subprogram_Declaration,
4358 N_Abstract_Subprogram_Declaration,
4359 N_Subprogram_Renaming_Declaration)
4361 Spec := Specification (Decl);
4363 elsif Nkind (Decl) = N_Entry_Declaration then
4370 -- The overriding operation is type conformant with the overridden one,
4371 -- but the names of the formals are not required to match. If the names
4372 -- appear permuted in the overriding operation, this is a possible
4373 -- source of confusion that is worth diagnosing. Controlling formals
4374 -- often carry names that reflect the type, and it is not worthwhile
4375 -- requiring that their names match.
4377 if Present (Overridden_Subp)
4378 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4385 Form1 := First_Formal (Subp);
4386 Form2 := First_Formal (Overridden_Subp);
4388 -- If the overriding operation is a synchronized operation, skip
4389 -- the first parameter of the overridden operation, which is
4390 -- implicit in the new one. If the operation is declared in the
4391 -- body it is not primitive and all formals must match.
4393 if Is_Concurrent_Type (Scope (Subp))
4394 and then Is_Tagged_Type (Scope (Subp))
4395 and then not Has_Completion (Scope (Subp))
4397 Form2 := Next_Formal (Form2);
4400 if Present (Form1) then
4401 Form1 := Next_Formal (Form1);
4402 Form2 := Next_Formal (Form2);
4405 while Present (Form1) loop
4406 if not Is_Controlling_Formal (Form1)
4407 and then Present (Next_Formal (Form2))
4408 and then Chars (Form1) = Chars (Next_Formal (Form2))
4410 Error_Msg_Node_2 := Alias (Overridden_Subp);
4411 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4412 Error_Msg_NE ("& does not match corresponding formal of&#",
4417 Next_Formal (Form1);
4418 Next_Formal (Form2);
4423 -- If there is an overridden subprogram, then check that there is not
4424 -- a "not overriding" indicator, and mark the subprogram as overriding.
4425 -- This is not done if the overridden subprogram is marked as hidden,
4426 -- which can occur for the case of inherited controlled operations
4427 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4428 -- subprogram is not itself hidden. (Note: This condition could probably
4429 -- be simplified, leaving out the testing for the specific controlled
4430 -- cases, but it seems safer and clearer this way, and echoes similar
4431 -- special-case tests of this kind in other places.)
4433 if Present (Overridden_Subp)
4434 and then (not Is_Hidden (Overridden_Subp)
4436 ((Chars (Overridden_Subp) = Name_Initialize
4437 or else Chars (Overridden_Subp) = Name_Adjust
4438 or else Chars (Overridden_Subp) = Name_Finalize)
4439 and then Present (Alias (Overridden_Subp))
4440 and then not Is_Hidden (Alias (Overridden_Subp))))
4442 if Must_Not_Override (Spec) then
4443 Error_Msg_Sloc := Sloc (Overridden_Subp);
4445 if Ekind (Subp) = E_Entry then
4447 ("entry & overrides inherited operation #", Spec, Subp);
4450 ("subprogram & overrides inherited operation #", Spec, Subp);
4453 elsif Is_Subprogram (Subp) then
4454 Set_Is_Overriding_Operation (Subp);
4457 -- If primitive flag is set or this is a protected operation, then
4458 -- the operation is overriding at the point of its declaration, so
4459 -- warn if necessary. Otherwise it may have been declared before the
4460 -- operation it overrides and no check is required.
4463 and then not Must_Override (Spec)
4464 and then (Is_Primitive
4465 or else Ekind (Scope (Subp)) = E_Protected_Type)
4467 Style.Missing_Overriding (Decl, Subp);
4470 -- If Subp is an operator, it may override a predefined operation, if
4471 -- it is defined in the same scope as the type to which it applies.
4472 -- In that case overridden_subp is empty because of our implicit
4473 -- representation for predefined operators. We have to check whether the
4474 -- signature of Subp matches that of a predefined operator. Note that
4475 -- first argument provides the name of the operator, and the second
4476 -- argument the signature that may match that of a standard operation.
4477 -- If the indicator is overriding, then the operator must match a
4478 -- predefined signature, because we know already that there is no
4479 -- explicit overridden operation.
4481 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4483 Typ : constant Entity_Id :=
4484 Base_Type (Etype (First_Formal (Subp)));
4486 Can_Override : constant Boolean :=
4487 Operator_Matches_Spec (Subp, Subp)
4488 and then Scope (Subp) = Scope (Typ)
4489 and then not Is_Class_Wide_Type (Typ);
4492 if Must_Not_Override (Spec) then
4494 -- If this is not a primitive or a protected subprogram, then
4495 -- "not overriding" is illegal.
4498 and then Ekind (Scope (Subp)) /= E_Protected_Type
4501 ("overriding indicator only allowed "
4502 & "if subprogram is primitive", Subp);
4504 elsif Can_Override then
4506 ("subprogram & overrides predefined operator ",
4510 elsif Must_Override (Spec) then
4511 if Is_Overriding_Operation (Subp) then
4512 Set_Is_Overriding_Operation (Subp);
4514 elsif not Can_Override then
4515 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4518 elsif not Error_Posted (Subp)
4519 and then Style_Check
4520 and then Can_Override
4522 not Is_Predefined_File_Name
4523 (Unit_File_Name (Get_Source_Unit (Subp)))
4525 Set_Is_Overriding_Operation (Subp);
4527 -- If style checks are enabled, indicate that the indicator is
4528 -- missing. However, at the point of declaration, the type of
4529 -- which this is a primitive operation may be private, in which
4530 -- case the indicator would be premature.
4532 if Has_Private_Declaration (Etype (Subp))
4533 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4537 Style.Missing_Overriding (Decl, Subp);
4542 elsif Must_Override (Spec) then
4543 if Ekind (Subp) = E_Entry then
4544 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4546 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4549 -- If the operation is marked "not overriding" and it's not primitive
4550 -- then an error is issued, unless this is an operation of a task or
4551 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4552 -- has been specified have already been checked above.
4554 elsif Must_Not_Override (Spec)
4555 and then not Is_Primitive
4556 and then Ekind (Subp) /= E_Entry
4557 and then Ekind (Scope (Subp)) /= E_Protected_Type
4560 ("overriding indicator only allowed if subprogram is primitive",
4564 end Check_Overriding_Indicator;
4570 -- Note: this procedure needs to know far too much about how the expander
4571 -- messes with exceptions. The use of the flag Exception_Junk and the
4572 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4573 -- works, but is not very clean. It would be better if the expansion
4574 -- routines would leave Original_Node working nicely, and we could use
4575 -- Original_Node here to ignore all the peculiar expander messing ???
4577 procedure Check_Returns
4581 Proc : Entity_Id := Empty)
4585 procedure Check_Statement_Sequence (L : List_Id);
4586 -- Internal recursive procedure to check a list of statements for proper
4587 -- termination by a return statement (or a transfer of control or a
4588 -- compound statement that is itself internally properly terminated).
4590 ------------------------------
4591 -- Check_Statement_Sequence --
4592 ------------------------------
4594 procedure Check_Statement_Sequence (L : List_Id) is
4599 Raise_Exception_Call : Boolean;
4600 -- Set True if statement sequence terminated by Raise_Exception call
4601 -- or a Reraise_Occurrence call.
4604 Raise_Exception_Call := False;
4606 -- Get last real statement
4608 Last_Stm := Last (L);
4610 -- Deal with digging out exception handler statement sequences that
4611 -- have been transformed by the local raise to goto optimization.
4612 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4613 -- optimization has occurred, we are looking at something like:
4616 -- original stmts in block
4620 -- goto L1; | omitted if No_Exception_Propagation
4625 -- goto L3; -- skip handler when exception not raised
4627 -- <<L1>> -- target label for local exception
4641 -- and what we have to do is to dig out the estmts1 and estmts2
4642 -- sequences (which were the original sequences of statements in
4643 -- the exception handlers) and check them.
4645 if Nkind (Last_Stm) = N_Label
4646 and then Exception_Junk (Last_Stm)
4652 exit when Nkind (Stm) /= N_Block_Statement;
4653 exit when not Exception_Junk (Stm);
4656 exit when Nkind (Stm) /= N_Label;
4657 exit when not Exception_Junk (Stm);
4658 Check_Statement_Sequence
4659 (Statements (Handled_Statement_Sequence (Next (Stm))));
4664 exit when Nkind (Stm) /= N_Goto_Statement;
4665 exit when not Exception_Junk (Stm);
4669 -- Don't count pragmas
4671 while Nkind (Last_Stm) = N_Pragma
4673 -- Don't count call to SS_Release (can happen after Raise_Exception)
4676 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4678 Nkind (Name (Last_Stm)) = N_Identifier
4680 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4682 -- Don't count exception junk
4685 (Nkind_In (Last_Stm, N_Goto_Statement,
4687 N_Object_Declaration)
4688 and then Exception_Junk (Last_Stm))
4689 or else Nkind (Last_Stm) in N_Push_xxx_Label
4690 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4695 -- Here we have the "real" last statement
4697 Kind := Nkind (Last_Stm);
4699 -- Transfer of control, OK. Note that in the No_Return procedure
4700 -- case, we already diagnosed any explicit return statements, so
4701 -- we can treat them as OK in this context.
4703 if Is_Transfer (Last_Stm) then
4706 -- Check cases of explicit non-indirect procedure calls
4708 elsif Kind = N_Procedure_Call_Statement
4709 and then Is_Entity_Name (Name (Last_Stm))
4711 -- Check call to Raise_Exception procedure which is treated
4712 -- specially, as is a call to Reraise_Occurrence.
4714 -- We suppress the warning in these cases since it is likely that
4715 -- the programmer really does not expect to deal with the case
4716 -- of Null_Occurrence, and thus would find a warning about a
4717 -- missing return curious, and raising Program_Error does not
4718 -- seem such a bad behavior if this does occur.
4720 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4721 -- behavior will be to raise Constraint_Error (see AI-329).
4723 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4725 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4727 Raise_Exception_Call := True;
4729 -- For Raise_Exception call, test first argument, if it is
4730 -- an attribute reference for a 'Identity call, then we know
4731 -- that the call cannot possibly return.
4734 Arg : constant Node_Id :=
4735 Original_Node (First_Actual (Last_Stm));
4737 if Nkind (Arg) = N_Attribute_Reference
4738 and then Attribute_Name (Arg) = Name_Identity
4745 -- If statement, need to look inside if there is an else and check
4746 -- each constituent statement sequence for proper termination.
4748 elsif Kind = N_If_Statement
4749 and then Present (Else_Statements (Last_Stm))
4751 Check_Statement_Sequence (Then_Statements (Last_Stm));
4752 Check_Statement_Sequence (Else_Statements (Last_Stm));
4754 if Present (Elsif_Parts (Last_Stm)) then
4756 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4759 while Present (Elsif_Part) loop
4760 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4768 -- Case statement, check each case for proper termination
4770 elsif Kind = N_Case_Statement then
4774 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4775 while Present (Case_Alt) loop
4776 Check_Statement_Sequence (Statements (Case_Alt));
4777 Next_Non_Pragma (Case_Alt);
4783 -- Block statement, check its handled sequence of statements
4785 elsif Kind = N_Block_Statement then
4791 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4800 -- Loop statement. If there is an iteration scheme, we can definitely
4801 -- fall out of the loop. Similarly if there is an exit statement, we
4802 -- can fall out. In either case we need a following return.
4804 elsif Kind = N_Loop_Statement then
4805 if Present (Iteration_Scheme (Last_Stm))
4806 or else Has_Exit (Entity (Identifier (Last_Stm)))
4810 -- A loop with no exit statement or iteration scheme is either
4811 -- an infinite loop, or it has some other exit (raise/return).
4812 -- In either case, no warning is required.
4818 -- Timed entry call, check entry call and delay alternatives
4820 -- Note: in expanded code, the timed entry call has been converted
4821 -- to a set of expanded statements on which the check will work
4822 -- correctly in any case.
4824 elsif Kind = N_Timed_Entry_Call then
4826 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4827 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4830 -- If statement sequence of entry call alternative is missing,
4831 -- then we can definitely fall through, and we post the error
4832 -- message on the entry call alternative itself.
4834 if No (Statements (ECA)) then
4837 -- If statement sequence of delay alternative is missing, then
4838 -- we can definitely fall through, and we post the error
4839 -- message on the delay alternative itself.
4841 -- Note: if both ECA and DCA are missing the return, then we
4842 -- post only one message, should be enough to fix the bugs.
4843 -- If not we will get a message next time on the DCA when the
4846 elsif No (Statements (DCA)) then
4849 -- Else check both statement sequences
4852 Check_Statement_Sequence (Statements (ECA));
4853 Check_Statement_Sequence (Statements (DCA));
4858 -- Conditional entry call, check entry call and else part
4860 -- Note: in expanded code, the conditional entry call has been
4861 -- converted to a set of expanded statements on which the check
4862 -- will work correctly in any case.
4864 elsif Kind = N_Conditional_Entry_Call then
4866 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4869 -- If statement sequence of entry call alternative is missing,
4870 -- then we can definitely fall through, and we post the error
4871 -- message on the entry call alternative itself.
4873 if No (Statements (ECA)) then
4876 -- Else check statement sequence and else part
4879 Check_Statement_Sequence (Statements (ECA));
4880 Check_Statement_Sequence (Else_Statements (Last_Stm));
4886 -- If we fall through, issue appropriate message
4889 if not Raise_Exception_Call then
4891 ("?RETURN statement missing following this statement!",
4894 ("\?Program_Error may be raised at run time!",
4898 -- Note: we set Err even though we have not issued a warning
4899 -- because we still have a case of a missing return. This is
4900 -- an extremely marginal case, probably will never be noticed
4901 -- but we might as well get it right.
4905 -- Otherwise we have the case of a procedure marked No_Return
4908 if not Raise_Exception_Call then
4910 ("?implied return after this statement " &
4911 "will raise Program_Error",
4914 ("\?procedure & is marked as No_Return!",
4919 RE : constant Node_Id :=
4920 Make_Raise_Program_Error (Sloc (Last_Stm),
4921 Reason => PE_Implicit_Return);
4923 Insert_After (Last_Stm, RE);
4927 end Check_Statement_Sequence;
4929 -- Start of processing for Check_Returns
4933 Check_Statement_Sequence (Statements (HSS));
4935 if Present (Exception_Handlers (HSS)) then
4936 Handler := First_Non_Pragma (Exception_Handlers (HSS));
4937 while Present (Handler) loop
4938 Check_Statement_Sequence (Statements (Handler));
4939 Next_Non_Pragma (Handler);
4944 ----------------------------
4945 -- Check_Subprogram_Order --
4946 ----------------------------
4948 procedure Check_Subprogram_Order (N : Node_Id) is
4950 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
4951 -- This is used to check if S1 > S2 in the sense required by this
4952 -- test, for example nameab < namec, but name2 < name10.
4954 -----------------------------
4955 -- Subprogram_Name_Greater --
4956 -----------------------------
4958 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
4963 -- Remove trailing numeric parts
4966 while S1 (L1) in '0' .. '9' loop
4971 while S2 (L2) in '0' .. '9' loop
4975 -- If non-numeric parts non-equal, that's decisive
4977 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
4980 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
4983 -- If non-numeric parts equal, compare suffixed numeric parts. Note
4984 -- that a missing suffix is treated as numeric zero in this test.
4988 while L1 < S1'Last loop
4990 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
4994 while L2 < S2'Last loop
4996 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5001 end Subprogram_Name_Greater;
5003 -- Start of processing for Check_Subprogram_Order
5006 -- Check body in alpha order if this is option
5009 and then Style_Check_Order_Subprograms
5010 and then Nkind (N) = N_Subprogram_Body
5011 and then Comes_From_Source (N)
5012 and then In_Extended_Main_Source_Unit (N)
5016 renames Scope_Stack.Table
5017 (Scope_Stack.Last).Last_Subprogram_Name;
5019 Body_Id : constant Entity_Id :=
5020 Defining_Entity (Specification (N));
5023 Get_Decoded_Name_String (Chars (Body_Id));
5026 if Subprogram_Name_Greater
5027 (LSN.all, Name_Buffer (1 .. Name_Len))
5029 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5035 LSN := new String'(Name_Buffer (1 .. Name_Len));
5038 end Check_Subprogram_Order;
5040 ------------------------------
5041 -- Check_Subtype_Conformant --
5042 ------------------------------
5044 procedure Check_Subtype_Conformant
5045 (New_Id : Entity_Id;
5047 Err_Loc : Node_Id := Empty;
5048 Skip_Controlling_Formals : Boolean := False)
5051 pragma Warnings (Off, Result);
5054 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5055 Skip_Controlling_Formals => Skip_Controlling_Formals);
5056 end Check_Subtype_Conformant;
5058 ---------------------------
5059 -- Check_Type_Conformant --
5060 ---------------------------
5062 procedure Check_Type_Conformant
5063 (New_Id : Entity_Id;
5065 Err_Loc : Node_Id := Empty)
5068 pragma Warnings (Off, Result);
5071 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5072 end Check_Type_Conformant;
5074 ----------------------
5075 -- Conforming_Types --
5076 ----------------------
5078 function Conforming_Types
5081 Ctype : Conformance_Type;
5082 Get_Inst : Boolean := False) return Boolean
5084 Type_1 : Entity_Id := T1;
5085 Type_2 : Entity_Id := T2;
5086 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5088 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5089 -- If neither T1 nor T2 are generic actual types, or if they are in
5090 -- different scopes (e.g. parent and child instances), then verify that
5091 -- the base types are equal. Otherwise T1 and T2 must be on the same
5092 -- subtype chain. The whole purpose of this procedure is to prevent
5093 -- spurious ambiguities in an instantiation that may arise if two
5094 -- distinct generic types are instantiated with the same actual.
5096 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5097 -- An access parameter can designate an incomplete type. If the
5098 -- incomplete type is the limited view of a type from a limited_
5099 -- with_clause, check whether the non-limited view is available. If
5100 -- it is a (non-limited) incomplete type, get the full view.
5102 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5103 -- Returns True if and only if either T1 denotes a limited view of T2
5104 -- or T2 denotes a limited view of T1. This can arise when the limited
5105 -- with view of a type is used in a subprogram declaration and the
5106 -- subprogram body is in the scope of a regular with clause for the
5107 -- same unit. In such a case, the two type entities can be considered
5108 -- identical for purposes of conformance checking.
5110 ----------------------
5111 -- Base_Types_Match --
5112 ----------------------
5114 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5119 elsif Base_Type (T1) = Base_Type (T2) then
5121 -- The following is too permissive. A more precise test should
5122 -- check that the generic actual is an ancestor subtype of the
5125 return not Is_Generic_Actual_Type (T1)
5126 or else not Is_Generic_Actual_Type (T2)
5127 or else Scope (T1) /= Scope (T2);
5132 end Base_Types_Match;
5134 --------------------------
5135 -- Find_Designated_Type --
5136 --------------------------
5138 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5142 Desig := Directly_Designated_Type (T);
5144 if Ekind (Desig) = E_Incomplete_Type then
5146 -- If regular incomplete type, get full view if available
5148 if Present (Full_View (Desig)) then
5149 Desig := Full_View (Desig);
5151 -- If limited view of a type, get non-limited view if available,
5152 -- and check again for a regular incomplete type.
5154 elsif Present (Non_Limited_View (Desig)) then
5155 Desig := Get_Full_View (Non_Limited_View (Desig));
5160 end Find_Designated_Type;
5162 -------------------------------
5163 -- Matches_Limited_With_View --
5164 -------------------------------
5166 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5168 -- In some cases a type imported through a limited_with clause, and
5169 -- its nonlimited view are both visible, for example in an anonymous
5170 -- access-to-class-wide type in a formal. Both entities designate the
5173 if From_With_Type (T1)
5174 and then T2 = Available_View (T1)
5178 elsif From_With_Type (T2)
5179 and then T1 = Available_View (T2)
5186 end Matches_Limited_With_View;
5188 -- Start of processing for Conforming_Types
5191 -- The context is an instance association for a formal
5192 -- access-to-subprogram type; the formal parameter types require
5193 -- mapping because they may denote other formal parameters of the
5197 Type_1 := Get_Instance_Of (T1);
5198 Type_2 := Get_Instance_Of (T2);
5201 -- If one of the types is a view of the other introduced by a limited
5202 -- with clause, treat these as conforming for all purposes.
5204 if Matches_Limited_With_View (T1, T2) then
5207 elsif Base_Types_Match (Type_1, Type_2) then
5208 return Ctype <= Mode_Conformant
5209 or else Subtypes_Statically_Match (Type_1, Type_2);
5211 elsif Is_Incomplete_Or_Private_Type (Type_1)
5212 and then Present (Full_View (Type_1))
5213 and then Base_Types_Match (Full_View (Type_1), Type_2)
5215 return Ctype <= Mode_Conformant
5216 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5218 elsif Ekind (Type_2) = E_Incomplete_Type
5219 and then Present (Full_View (Type_2))
5220 and then Base_Types_Match (Type_1, Full_View (Type_2))
5222 return Ctype <= Mode_Conformant
5223 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5225 elsif Is_Private_Type (Type_2)
5226 and then In_Instance
5227 and then Present (Full_View (Type_2))
5228 and then Base_Types_Match (Type_1, Full_View (Type_2))
5230 return Ctype <= Mode_Conformant
5231 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5234 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5235 -- treated recursively because they carry a signature.
5237 Are_Anonymous_Access_To_Subprogram_Types :=
5238 Ekind (Type_1) = Ekind (Type_2)
5240 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5242 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5244 -- Test anonymous access type case. For this case, static subtype
5245 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5246 -- the base types because we may have built internal subtype entities
5247 -- to handle null-excluding types (see Process_Formals).
5249 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5251 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5252 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5255 Desig_1 : Entity_Id;
5256 Desig_2 : Entity_Id;
5259 -- In Ada2005, access constant indicators must match for
5260 -- subtype conformance.
5262 if Ada_Version >= Ada_05
5263 and then Ctype >= Subtype_Conformant
5265 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5270 Desig_1 := Find_Designated_Type (Type_1);
5272 Desig_2 := Find_Designated_Type (Type_2);
5274 -- If the context is an instance association for a formal
5275 -- access-to-subprogram type; formal access parameter designated
5276 -- types require mapping because they may denote other formal
5277 -- parameters of the generic unit.
5280 Desig_1 := Get_Instance_Of (Desig_1);
5281 Desig_2 := Get_Instance_Of (Desig_2);
5284 -- It is possible for a Class_Wide_Type to be introduced for an
5285 -- incomplete type, in which case there is a separate class_ wide
5286 -- type for the full view. The types conform if their Etypes
5287 -- conform, i.e. one may be the full view of the other. This can
5288 -- only happen in the context of an access parameter, other uses
5289 -- of an incomplete Class_Wide_Type are illegal.
5291 if Is_Class_Wide_Type (Desig_1)
5292 and then Is_Class_Wide_Type (Desig_2)
5296 (Etype (Base_Type (Desig_1)),
5297 Etype (Base_Type (Desig_2)), Ctype);
5299 elsif Are_Anonymous_Access_To_Subprogram_Types then
5300 if Ada_Version < Ada_05 then
5301 return Ctype = Type_Conformant
5303 Subtypes_Statically_Match (Desig_1, Desig_2);
5305 -- We must check the conformance of the signatures themselves
5309 Conformant : Boolean;
5312 (Desig_1, Desig_2, Ctype, False, Conformant);
5318 return Base_Type (Desig_1) = Base_Type (Desig_2)
5319 and then (Ctype = Type_Conformant
5321 Subtypes_Statically_Match (Desig_1, Desig_2));
5325 -- Otherwise definitely no match
5328 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5329 and then Is_Access_Type (Type_2))
5330 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5331 and then Is_Access_Type (Type_1)))
5334 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5336 May_Hide_Profile := True;
5341 end Conforming_Types;
5343 --------------------------
5344 -- Create_Extra_Formals --
5345 --------------------------
5347 procedure Create_Extra_Formals (E : Entity_Id) is
5349 First_Extra : Entity_Id := Empty;
5350 Last_Extra : Entity_Id;
5351 Formal_Type : Entity_Id;
5352 P_Formal : Entity_Id := Empty;
5354 function Add_Extra_Formal
5355 (Assoc_Entity : Entity_Id;
5358 Suffix : String) return Entity_Id;
5359 -- Add an extra formal to the current list of formals and extra formals.
5360 -- The extra formal is added to the end of the list of extra formals,
5361 -- and also returned as the result. These formals are always of mode IN.
5362 -- The new formal has the type Typ, is declared in Scope, and its name
5363 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5365 ----------------------
5366 -- Add_Extra_Formal --
5367 ----------------------
5369 function Add_Extra_Formal
5370 (Assoc_Entity : Entity_Id;
5373 Suffix : String) return Entity_Id
5375 EF : constant Entity_Id :=
5376 Make_Defining_Identifier (Sloc (Assoc_Entity),
5377 Chars => New_External_Name (Chars (Assoc_Entity),
5381 -- A little optimization. Never generate an extra formal for the
5382 -- _init operand of an initialization procedure, since it could
5385 if Chars (Formal) = Name_uInit then
5389 Set_Ekind (EF, E_In_Parameter);
5390 Set_Actual_Subtype (EF, Typ);
5391 Set_Etype (EF, Typ);
5392 Set_Scope (EF, Scope);
5393 Set_Mechanism (EF, Default_Mechanism);
5394 Set_Formal_Validity (EF);
5396 if No (First_Extra) then
5398 Set_Extra_Formals (Scope, First_Extra);
5401 if Present (Last_Extra) then
5402 Set_Extra_Formal (Last_Extra, EF);
5408 end Add_Extra_Formal;
5410 -- Start of processing for Create_Extra_Formals
5413 -- We never generate extra formals if expansion is not active
5414 -- because we don't need them unless we are generating code.
5416 if not Expander_Active then
5420 -- If this is a derived subprogram then the subtypes of the parent
5421 -- subprogram's formal parameters will be used to determine the need
5422 -- for extra formals.
5424 if Is_Overloadable (E) and then Present (Alias (E)) then
5425 P_Formal := First_Formal (Alias (E));
5428 Last_Extra := Empty;
5429 Formal := First_Formal (E);
5430 while Present (Formal) loop
5431 Last_Extra := Formal;
5432 Next_Formal (Formal);
5435 -- If Extra_formals were already created, don't do it again. This
5436 -- situation may arise for subprogram types created as part of
5437 -- dispatching calls (see Expand_Dispatching_Call)
5439 if Present (Last_Extra) and then
5440 Present (Extra_Formal (Last_Extra))
5445 -- If the subprogram is a predefined dispatching subprogram then don't
5446 -- generate any extra constrained or accessibility level formals. In
5447 -- general we suppress these for internal subprograms (by not calling
5448 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5449 -- generated stream attributes do get passed through because extra
5450 -- build-in-place formals are needed in some cases (limited 'Input).
5452 if Is_Predefined_Internal_Operation (E) then
5453 goto Test_For_BIP_Extras;
5456 Formal := First_Formal (E);
5457 while Present (Formal) loop
5459 -- Create extra formal for supporting the attribute 'Constrained.
5460 -- The case of a private type view without discriminants also
5461 -- requires the extra formal if the underlying type has defaulted
5464 if Ekind (Formal) /= E_In_Parameter then
5465 if Present (P_Formal) then
5466 Formal_Type := Etype (P_Formal);
5468 Formal_Type := Etype (Formal);
5471 -- Do not produce extra formals for Unchecked_Union parameters.
5472 -- Jump directly to the end of the loop.
5474 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5475 goto Skip_Extra_Formal_Generation;
5478 if not Has_Discriminants (Formal_Type)
5479 and then Ekind (Formal_Type) in Private_Kind
5480 and then Present (Underlying_Type (Formal_Type))
5482 Formal_Type := Underlying_Type (Formal_Type);
5485 if Has_Discriminants (Formal_Type)
5486 and then not Is_Constrained (Formal_Type)
5487 and then not Is_Indefinite_Subtype (Formal_Type)
5489 Set_Extra_Constrained
5490 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "F"));
5494 -- Create extra formal for supporting accessibility checking. This
5495 -- is done for both anonymous access formals and formals of named
5496 -- access types that are marked as controlling formals. The latter
5497 -- case can occur when Expand_Dispatching_Call creates a subprogram
5498 -- type and substitutes the types of access-to-class-wide actuals
5499 -- for the anonymous access-to-specific-type of controlling formals.
5500 -- Base_Type is applied because in cases where there is a null
5501 -- exclusion the formal may have an access subtype.
5503 -- This is suppressed if we specifically suppress accessibility
5504 -- checks at the package level for either the subprogram, or the
5505 -- package in which it resides. However, we do not suppress it
5506 -- simply if the scope has accessibility checks suppressed, since
5507 -- this could cause trouble when clients are compiled with a
5508 -- different suppression setting. The explicit checks at the
5509 -- package level are safe from this point of view.
5511 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5512 or else (Is_Controlling_Formal (Formal)
5513 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5515 (Explicit_Suppress (E, Accessibility_Check)
5517 Explicit_Suppress (Scope (E), Accessibility_Check))
5520 or else Present (Extra_Accessibility (P_Formal)))
5522 Set_Extra_Accessibility
5523 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "F"));
5526 -- This label is required when skipping extra formal generation for
5527 -- Unchecked_Union parameters.
5529 <<Skip_Extra_Formal_Generation>>
5531 if Present (P_Formal) then
5532 Next_Formal (P_Formal);
5535 Next_Formal (Formal);
5538 <<Test_For_BIP_Extras>>
5540 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5541 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5543 if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
5545 Result_Subt : constant Entity_Id := Etype (E);
5547 Discard : Entity_Id;
5548 pragma Warnings (Off, Discard);
5551 -- In the case of functions with unconstrained result subtypes,
5552 -- add a 3-state formal indicating whether the return object is
5553 -- allocated by the caller (0), or should be allocated by the
5554 -- callee on the secondary stack (1) or in the global heap (2).
5555 -- For the moment we just use Natural for the type of this formal.
5556 -- Note that this formal isn't usually needed in the case where
5557 -- the result subtype is constrained, but it is needed when the
5558 -- function has a tagged result, because generally such functions
5559 -- can be called in a dispatching context and such calls must be
5560 -- handled like calls to a class-wide function.
5562 if not Is_Constrained (Underlying_Type (Result_Subt))
5563 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5567 (E, Standard_Natural,
5568 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5571 -- In the case of functions whose result type has controlled
5572 -- parts, we have an extra formal of type
5573 -- System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
5574 -- is, we are passing a pointer to a finalization list (which is
5575 -- itself a pointer). This extra formal is then passed along to
5576 -- Move_Final_List in case of successful completion of a return
5577 -- statement. We cannot pass an 'in out' parameter, because we
5578 -- need to update the finalization list during an abort-deferred
5579 -- region, rather than using copy-back after the function
5580 -- returns. This is true even if we are able to get away with
5581 -- having 'in out' parameters, which are normally illegal for
5582 -- functions. This formal is also needed when the function has
5585 if Needs_BIP_Final_List (E) then
5588 (E, RTE (RE_Finalizable_Ptr_Ptr),
5589 E, BIP_Formal_Suffix (BIP_Final_List));
5592 -- If the result type contains tasks, we have two extra formals:
5593 -- the master of the tasks to be created, and the caller's
5594 -- activation chain.
5596 if Has_Task (Result_Subt) then
5599 (E, RTE (RE_Master_Id),
5600 E, BIP_Formal_Suffix (BIP_Master));
5603 (E, RTE (RE_Activation_Chain_Access),
5604 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5607 -- All build-in-place functions get an extra formal that will be
5608 -- passed the address of the return object within the caller.
5611 Formal_Type : constant Entity_Id :=
5613 (E_Anonymous_Access_Type, E,
5614 Scope_Id => Scope (E));
5616 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5617 Set_Etype (Formal_Type, Formal_Type);
5618 Set_Depends_On_Private
5619 (Formal_Type, Has_Private_Component (Formal_Type));
5620 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5621 Set_Is_Access_Constant (Formal_Type, False);
5623 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5624 -- the designated type comes from the limited view (for
5625 -- back-end purposes).
5627 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5629 Layout_Type (Formal_Type);
5633 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5637 end Create_Extra_Formals;
5639 -----------------------------
5640 -- Enter_Overloaded_Entity --
5641 -----------------------------
5643 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5644 E : Entity_Id := Current_Entity_In_Scope (S);
5645 C_E : Entity_Id := Current_Entity (S);
5649 Set_Has_Homonym (E);
5650 Set_Has_Homonym (S);
5653 Set_Is_Immediately_Visible (S);
5654 Set_Scope (S, Current_Scope);
5656 -- Chain new entity if front of homonym in current scope, so that
5657 -- homonyms are contiguous.
5662 while Homonym (C_E) /= E loop
5663 C_E := Homonym (C_E);
5666 Set_Homonym (C_E, S);
5670 Set_Current_Entity (S);
5675 Append_Entity (S, Current_Scope);
5676 Set_Public_Status (S);
5678 if Debug_Flag_E then
5679 Write_Str ("New overloaded entity chain: ");
5680 Write_Name (Chars (S));
5683 while Present (E) loop
5684 Write_Str (" "); Write_Int (Int (E));
5691 -- Generate warning for hiding
5694 and then Comes_From_Source (S)
5695 and then In_Extended_Main_Source_Unit (S)
5702 -- Warn unless genuine overloading
5704 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5705 and then (Is_Immediately_Visible (E)
5707 Is_Potentially_Use_Visible (S))
5709 Error_Msg_Sloc := Sloc (E);
5710 Error_Msg_N ("declaration of & hides one#?", S);
5714 end Enter_Overloaded_Entity;
5716 -----------------------------
5717 -- Find_Corresponding_Spec --
5718 -----------------------------
5720 function Find_Corresponding_Spec
5722 Post_Error : Boolean := True) return Entity_Id
5724 Spec : constant Node_Id := Specification (N);
5725 Designator : constant Entity_Id := Defining_Entity (Spec);
5730 E := Current_Entity (Designator);
5731 while Present (E) loop
5733 -- We are looking for a matching spec. It must have the same scope,
5734 -- and the same name, and either be type conformant, or be the case
5735 -- of a library procedure spec and its body (which belong to one
5736 -- another regardless of whether they are type conformant or not).
5738 if Scope (E) = Current_Scope then
5739 if Current_Scope = Standard_Standard
5740 or else (Ekind (E) = Ekind (Designator)
5741 and then Type_Conformant (E, Designator))
5743 -- Within an instantiation, we know that spec and body are
5744 -- subtype conformant, because they were subtype conformant
5745 -- in the generic. We choose the subtype-conformant entity
5746 -- here as well, to resolve spurious ambiguities in the
5747 -- instance that were not present in the generic (i.e. when
5748 -- two different types are given the same actual). If we are
5749 -- looking for a spec to match a body, full conformance is
5753 Set_Convention (Designator, Convention (E));
5755 if Nkind (N) = N_Subprogram_Body
5756 and then Present (Homonym (E))
5757 and then not Fully_Conformant (E, Designator)
5761 elsif not Subtype_Conformant (E, Designator) then
5766 if not Has_Completion (E) then
5767 if Nkind (N) /= N_Subprogram_Body_Stub then
5768 Set_Corresponding_Spec (N, E);
5771 Set_Has_Completion (E);
5774 elsif Nkind (Parent (N)) = N_Subunit then
5776 -- If this is the proper body of a subunit, the completion
5777 -- flag is set when analyzing the stub.
5781 -- If E is an internal function with a controlling result
5782 -- that was created for an operation inherited by a null
5783 -- extension, it may be overridden by a body without a previous
5784 -- spec (one more reason why these should be shunned). In that
5785 -- case remove the generated body, because the current one is
5786 -- the explicit overriding.
5788 elsif Ekind (E) = E_Function
5789 and then Ada_Version >= Ada_05
5790 and then not Comes_From_Source (E)
5791 and then Has_Controlling_Result (E)
5792 and then Is_Null_Extension (Etype (E))
5793 and then Comes_From_Source (Spec)
5795 Set_Has_Completion (E, False);
5797 if Expander_Active then
5799 (Unit_Declaration_Node
5800 (Corresponding_Body (Unit_Declaration_Node (E))));
5803 -- If expansion is disabled, the wrapper function has not
5804 -- been generated, and this is the standard case of a late
5805 -- body overriding an inherited operation.
5811 -- If the body already exists, then this is an error unless
5812 -- the previous declaration is the implicit declaration of a
5813 -- derived subprogram, or this is a spurious overloading in an
5816 elsif No (Alias (E))
5817 and then not Is_Intrinsic_Subprogram (E)
5818 and then not In_Instance
5821 Error_Msg_Sloc := Sloc (E);
5823 if Is_Imported (E) then
5825 ("body not allowed for imported subprogram & declared#",
5828 Error_Msg_NE ("duplicate body for & declared#", N, E);
5832 -- Child units cannot be overloaded, so a conformance mismatch
5833 -- between body and a previous spec is an error.
5835 elsif Is_Child_Unit (E)
5837 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5839 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5844 ("body of child unit does not match previous declaration", N);
5852 -- On exit, we know that no previous declaration of subprogram exists
5855 end Find_Corresponding_Spec;
5857 ----------------------
5858 -- Fully_Conformant --
5859 ----------------------
5861 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5864 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5866 end Fully_Conformant;
5868 ----------------------------------
5869 -- Fully_Conformant_Expressions --
5870 ----------------------------------
5872 function Fully_Conformant_Expressions
5873 (Given_E1 : Node_Id;
5874 Given_E2 : Node_Id) return Boolean
5876 E1 : constant Node_Id := Original_Node (Given_E1);
5877 E2 : constant Node_Id := Original_Node (Given_E2);
5878 -- We always test conformance on original nodes, since it is possible
5879 -- for analysis and/or expansion to make things look as though they
5880 -- conform when they do not, e.g. by converting 1+2 into 3.
5882 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
5883 renames Fully_Conformant_Expressions;
5885 function FCL (L1, L2 : List_Id) return Boolean;
5886 -- Compare elements of two lists for conformance. Elements have to
5887 -- be conformant, and actuals inserted as default parameters do not
5888 -- match explicit actuals with the same value.
5890 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
5891 -- Compare an operator node with a function call
5897 function FCL (L1, L2 : List_Id) return Boolean is
5901 if L1 = No_List then
5907 if L2 = No_List then
5913 -- Compare two lists, skipping rewrite insertions (we want to
5914 -- compare the original trees, not the expanded versions!)
5917 if Is_Rewrite_Insertion (N1) then
5919 elsif Is_Rewrite_Insertion (N2) then
5925 elsif not FCE (N1, N2) then
5938 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
5939 Actuals : constant List_Id := Parameter_Associations (Call_Node);
5944 or else Entity (Op_Node) /= Entity (Name (Call_Node))
5949 Act := First (Actuals);
5951 if Nkind (Op_Node) in N_Binary_Op then
5952 if not FCE (Left_Opnd (Op_Node), Act) then
5959 return Present (Act)
5960 and then FCE (Right_Opnd (Op_Node), Act)
5961 and then No (Next (Act));
5965 -- Start of processing for Fully_Conformant_Expressions
5968 -- Non-conformant if paren count does not match. Note: if some idiot
5969 -- complains that we don't do this right for more than 3 levels of
5970 -- parentheses, they will be treated with the respect they deserve!
5972 if Paren_Count (E1) /= Paren_Count (E2) then
5975 -- If same entities are referenced, then they are conformant even if
5976 -- they have different forms (RM 8.3.1(19-20)).
5978 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
5979 if Present (Entity (E1)) then
5980 return Entity (E1) = Entity (E2)
5981 or else (Chars (Entity (E1)) = Chars (Entity (E2))
5982 and then Ekind (Entity (E1)) = E_Discriminant
5983 and then Ekind (Entity (E2)) = E_In_Parameter);
5985 elsif Nkind (E1) = N_Expanded_Name
5986 and then Nkind (E2) = N_Expanded_Name
5987 and then Nkind (Selector_Name (E1)) = N_Character_Literal
5988 and then Nkind (Selector_Name (E2)) = N_Character_Literal
5990 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
5993 -- Identifiers in component associations don't always have
5994 -- entities, but their names must conform.
5996 return Nkind (E1) = N_Identifier
5997 and then Nkind (E2) = N_Identifier
5998 and then Chars (E1) = Chars (E2);
6001 elsif Nkind (E1) = N_Character_Literal
6002 and then Nkind (E2) = N_Expanded_Name
6004 return Nkind (Selector_Name (E2)) = N_Character_Literal
6005 and then Chars (E1) = Chars (Selector_Name (E2));
6007 elsif Nkind (E2) = N_Character_Literal
6008 and then Nkind (E1) = N_Expanded_Name
6010 return Nkind (Selector_Name (E1)) = N_Character_Literal
6011 and then Chars (E2) = Chars (Selector_Name (E1));
6013 elsif Nkind (E1) in N_Op
6014 and then Nkind (E2) = N_Function_Call
6016 return FCO (E1, E2);
6018 elsif Nkind (E2) in N_Op
6019 and then Nkind (E1) = N_Function_Call
6021 return FCO (E2, E1);
6023 -- Otherwise we must have the same syntactic entity
6025 elsif Nkind (E1) /= Nkind (E2) then
6028 -- At this point, we specialize by node type
6035 FCL (Expressions (E1), Expressions (E2))
6036 and then FCL (Component_Associations (E1),
6037 Component_Associations (E2));
6040 if Nkind (Expression (E1)) = N_Qualified_Expression
6042 Nkind (Expression (E2)) = N_Qualified_Expression
6044 return FCE (Expression (E1), Expression (E2));
6046 -- Check that the subtype marks and any constraints
6051 Indic1 : constant Node_Id := Expression (E1);
6052 Indic2 : constant Node_Id := Expression (E2);
6057 if Nkind (Indic1) /= N_Subtype_Indication then
6059 Nkind (Indic2) /= N_Subtype_Indication
6060 and then Entity (Indic1) = Entity (Indic2);
6062 elsif Nkind (Indic2) /= N_Subtype_Indication then
6064 Nkind (Indic1) /= N_Subtype_Indication
6065 and then Entity (Indic1) = Entity (Indic2);
6068 if Entity (Subtype_Mark (Indic1)) /=
6069 Entity (Subtype_Mark (Indic2))
6074 Elt1 := First (Constraints (Constraint (Indic1)));
6075 Elt2 := First (Constraints (Constraint (Indic2)));
6076 while Present (Elt1) and then Present (Elt2) loop
6077 if not FCE (Elt1, Elt2) then
6090 when N_Attribute_Reference =>
6092 Attribute_Name (E1) = Attribute_Name (E2)
6093 and then FCL (Expressions (E1), Expressions (E2));
6097 Entity (E1) = Entity (E2)
6098 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6099 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6101 when N_Short_Circuit | N_Membership_Test =>
6103 FCE (Left_Opnd (E1), Left_Opnd (E2))
6105 FCE (Right_Opnd (E1), Right_Opnd (E2));
6107 when N_Character_Literal =>
6109 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6111 when N_Component_Association =>
6113 FCL (Choices (E1), Choices (E2))
6114 and then FCE (Expression (E1), Expression (E2));
6116 when N_Conditional_Expression =>
6118 FCL (Expressions (E1), Expressions (E2));
6120 when N_Explicit_Dereference =>
6122 FCE (Prefix (E1), Prefix (E2));
6124 when N_Extension_Aggregate =>
6126 FCL (Expressions (E1), Expressions (E2))
6127 and then Null_Record_Present (E1) =
6128 Null_Record_Present (E2)
6129 and then FCL (Component_Associations (E1),
6130 Component_Associations (E2));
6132 when N_Function_Call =>
6134 FCE (Name (E1), Name (E2))
6135 and then FCL (Parameter_Associations (E1),
6136 Parameter_Associations (E2));
6138 when N_Indexed_Component =>
6140 FCE (Prefix (E1), Prefix (E2))
6141 and then FCL (Expressions (E1), Expressions (E2));
6143 when N_Integer_Literal =>
6144 return (Intval (E1) = Intval (E2));
6149 when N_Operator_Symbol =>
6151 Chars (E1) = Chars (E2);
6153 when N_Others_Choice =>
6156 when N_Parameter_Association =>
6158 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6159 and then FCE (Explicit_Actual_Parameter (E1),
6160 Explicit_Actual_Parameter (E2));
6162 when N_Qualified_Expression =>
6164 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6165 and then FCE (Expression (E1), Expression (E2));
6169 FCE (Low_Bound (E1), Low_Bound (E2))
6170 and then FCE (High_Bound (E1), High_Bound (E2));
6172 when N_Real_Literal =>
6173 return (Realval (E1) = Realval (E2));
6175 when N_Selected_Component =>
6177 FCE (Prefix (E1), Prefix (E2))
6178 and then FCE (Selector_Name (E1), Selector_Name (E2));
6182 FCE (Prefix (E1), Prefix (E2))
6183 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
6185 when N_String_Literal =>
6187 S1 : constant String_Id := Strval (E1);
6188 S2 : constant String_Id := Strval (E2);
6189 L1 : constant Nat := String_Length (S1);
6190 L2 : constant Nat := String_Length (S2);
6197 for J in 1 .. L1 loop
6198 if Get_String_Char (S1, J) /=
6199 Get_String_Char (S2, J)
6209 when N_Type_Conversion =>
6211 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6212 and then FCE (Expression (E1), Expression (E2));
6216 Entity (E1) = Entity (E2)
6217 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6219 when N_Unchecked_Type_Conversion =>
6221 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6222 and then FCE (Expression (E1), Expression (E2));
6224 -- All other node types cannot appear in this context. Strictly
6225 -- we should raise a fatal internal error. Instead we just ignore
6226 -- the nodes. This means that if anyone makes a mistake in the
6227 -- expander and mucks an expression tree irretrievably, the
6228 -- result will be a failure to detect a (probably very obscure)
6229 -- case of non-conformance, which is better than bombing on some
6230 -- case where two expressions do in fact conform.
6237 end Fully_Conformant_Expressions;
6239 ----------------------------------------
6240 -- Fully_Conformant_Discrete_Subtypes --
6241 ----------------------------------------
6243 function Fully_Conformant_Discrete_Subtypes
6244 (Given_S1 : Node_Id;
6245 Given_S2 : Node_Id) return Boolean
6247 S1 : constant Node_Id := Original_Node (Given_S1);
6248 S2 : constant Node_Id := Original_Node (Given_S2);
6250 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6251 -- Special-case for a bound given by a discriminant, which in the body
6252 -- is replaced with the discriminal of the enclosing type.
6254 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6255 -- Check both bounds
6257 -----------------------
6258 -- Conforming_Bounds --
6259 -----------------------
6261 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6263 if Is_Entity_Name (B1)
6264 and then Is_Entity_Name (B2)
6265 and then Ekind (Entity (B1)) = E_Discriminant
6267 return Chars (B1) = Chars (B2);
6270 return Fully_Conformant_Expressions (B1, B2);
6272 end Conforming_Bounds;
6274 -----------------------
6275 -- Conforming_Ranges --
6276 -----------------------
6278 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6281 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6283 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6284 end Conforming_Ranges;
6286 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6289 if Nkind (S1) /= Nkind (S2) then
6292 elsif Is_Entity_Name (S1) then
6293 return Entity (S1) = Entity (S2);
6295 elsif Nkind (S1) = N_Range then
6296 return Conforming_Ranges (S1, S2);
6298 elsif Nkind (S1) = N_Subtype_Indication then
6300 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6303 (Range_Expression (Constraint (S1)),
6304 Range_Expression (Constraint (S2)));
6308 end Fully_Conformant_Discrete_Subtypes;
6310 --------------------
6311 -- Install_Entity --
6312 --------------------
6314 procedure Install_Entity (E : Entity_Id) is
6315 Prev : constant Entity_Id := Current_Entity (E);
6317 Set_Is_Immediately_Visible (E);
6318 Set_Current_Entity (E);
6319 Set_Homonym (E, Prev);
6322 ---------------------
6323 -- Install_Formals --
6324 ---------------------
6326 procedure Install_Formals (Id : Entity_Id) is
6329 F := First_Formal (Id);
6330 while Present (F) loop
6334 end Install_Formals;
6336 -----------------------------
6337 -- Is_Interface_Conformant --
6338 -----------------------------
6340 function Is_Interface_Conformant
6341 (Tagged_Type : Entity_Id;
6342 Iface_Prim : Entity_Id;
6343 Prim : Entity_Id) return Boolean
6345 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6346 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6349 pragma Assert (Is_Subprogram (Iface_Prim)
6350 and then Is_Subprogram (Prim)
6351 and then Is_Dispatching_Operation (Iface_Prim)
6352 and then Is_Dispatching_Operation (Prim));
6354 pragma Assert (Is_Interface (Iface)
6355 or else (Present (Alias (Iface_Prim))
6358 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6360 if Prim = Iface_Prim
6361 or else not Is_Subprogram (Prim)
6362 or else Ekind (Prim) /= Ekind (Iface_Prim)
6363 or else not Is_Dispatching_Operation (Prim)
6364 or else Scope (Prim) /= Scope (Tagged_Type)
6366 or else Base_Type (Typ) /= Tagged_Type
6367 or else not Primitive_Names_Match (Iface_Prim, Prim)
6371 -- Case of a procedure, or a function that does not have a controlling
6372 -- result (I or access I).
6374 elsif Ekind (Iface_Prim) = E_Procedure
6375 or else Etype (Prim) = Etype (Iface_Prim)
6376 or else not Has_Controlling_Result (Prim)
6378 return Type_Conformant (Prim, Iface_Prim,
6379 Skip_Controlling_Formals => True);
6381 -- Case of a function returning an interface, or an access to one.
6382 -- Check that the return types correspond.
6384 elsif Implements_Interface (Typ, Iface) then
6385 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6387 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6392 Type_Conformant (Prim, Iface_Prim,
6393 Skip_Controlling_Formals => True);
6399 end Is_Interface_Conformant;
6401 ---------------------------------
6402 -- Is_Non_Overriding_Operation --
6403 ---------------------------------
6405 function Is_Non_Overriding_Operation
6406 (Prev_E : Entity_Id;
6407 New_E : Entity_Id) return Boolean
6411 G_Typ : Entity_Id := Empty;
6413 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6414 -- If F_Type is a derived type associated with a generic actual subtype,
6415 -- then return its Generic_Parent_Type attribute, else return Empty.
6417 function Types_Correspond
6418 (P_Type : Entity_Id;
6419 N_Type : Entity_Id) return Boolean;
6420 -- Returns true if and only if the types (or designated types in the
6421 -- case of anonymous access types) are the same or N_Type is derived
6422 -- directly or indirectly from P_Type.
6424 -----------------------------
6425 -- Get_Generic_Parent_Type --
6426 -----------------------------
6428 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6433 if Is_Derived_Type (F_Typ)
6434 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6436 -- The tree must be traversed to determine the parent subtype in
6437 -- the generic unit, which unfortunately isn't always available
6438 -- via semantic attributes. ??? (Note: The use of Original_Node
6439 -- is needed for cases where a full derived type has been
6442 Indic := Subtype_Indication
6443 (Type_Definition (Original_Node (Parent (F_Typ))));
6445 if Nkind (Indic) = N_Subtype_Indication then
6446 G_Typ := Entity (Subtype_Mark (Indic));
6448 G_Typ := Entity (Indic);
6451 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6452 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6454 return Generic_Parent_Type (Parent (G_Typ));
6459 end Get_Generic_Parent_Type;
6461 ----------------------
6462 -- Types_Correspond --
6463 ----------------------
6465 function Types_Correspond
6466 (P_Type : Entity_Id;
6467 N_Type : Entity_Id) return Boolean
6469 Prev_Type : Entity_Id := Base_Type (P_Type);
6470 New_Type : Entity_Id := Base_Type (N_Type);
6473 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6474 Prev_Type := Designated_Type (Prev_Type);
6477 if Ekind (New_Type) = E_Anonymous_Access_Type then
6478 New_Type := Designated_Type (New_Type);
6481 if Prev_Type = New_Type then
6484 elsif not Is_Class_Wide_Type (New_Type) then
6485 while Etype (New_Type) /= New_Type loop
6486 New_Type := Etype (New_Type);
6487 if New_Type = Prev_Type then
6493 end Types_Correspond;
6495 -- Start of processing for Is_Non_Overriding_Operation
6498 -- In the case where both operations are implicit derived subprograms
6499 -- then neither overrides the other. This can only occur in certain
6500 -- obscure cases (e.g., derivation from homographs created in a generic
6503 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6506 elsif Ekind (Current_Scope) = E_Package
6507 and then Is_Generic_Instance (Current_Scope)
6508 and then In_Private_Part (Current_Scope)
6509 and then Comes_From_Source (New_E)
6511 -- We examine the formals and result subtype of the inherited
6512 -- operation, to determine whether their type is derived from (the
6513 -- instance of) a generic type.
6515 Formal := First_Formal (Prev_E);
6517 while Present (Formal) loop
6518 F_Typ := Base_Type (Etype (Formal));
6520 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6521 F_Typ := Designated_Type (F_Typ);
6524 G_Typ := Get_Generic_Parent_Type (F_Typ);
6526 Next_Formal (Formal);
6529 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6530 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6537 -- If the generic type is a private type, then the original operation
6538 -- was not overriding in the generic, because there was no primitive
6539 -- operation to override.
6541 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6542 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6543 N_Formal_Private_Type_Definition
6547 -- The generic parent type is the ancestor of a formal derived
6548 -- type declaration. We need to check whether it has a primitive
6549 -- operation that should be overridden by New_E in the generic.
6553 P_Formal : Entity_Id;
6554 N_Formal : Entity_Id;
6558 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6561 while Present (Prim_Elt) loop
6562 P_Prim := Node (Prim_Elt);
6564 if Chars (P_Prim) = Chars (New_E)
6565 and then Ekind (P_Prim) = Ekind (New_E)
6567 P_Formal := First_Formal (P_Prim);
6568 N_Formal := First_Formal (New_E);
6569 while Present (P_Formal) and then Present (N_Formal) loop
6570 P_Typ := Etype (P_Formal);
6571 N_Typ := Etype (N_Formal);
6573 if not Types_Correspond (P_Typ, N_Typ) then
6577 Next_Entity (P_Formal);
6578 Next_Entity (N_Formal);
6581 -- Found a matching primitive operation belonging to the
6582 -- formal ancestor type, so the new subprogram is
6586 and then No (N_Formal)
6587 and then (Ekind (New_E) /= E_Function
6590 (Etype (P_Prim), Etype (New_E)))
6596 Next_Elmt (Prim_Elt);
6599 -- If no match found, then the new subprogram does not
6600 -- override in the generic (nor in the instance).
6608 end Is_Non_Overriding_Operation;
6610 ------------------------------
6611 -- Make_Inequality_Operator --
6612 ------------------------------
6614 -- S is the defining identifier of an equality operator. We build a
6615 -- subprogram declaration with the right signature. This operation is
6616 -- intrinsic, because it is always expanded as the negation of the
6617 -- call to the equality function.
6619 procedure Make_Inequality_Operator (S : Entity_Id) is
6620 Loc : constant Source_Ptr := Sloc (S);
6623 Op_Name : Entity_Id;
6625 FF : constant Entity_Id := First_Formal (S);
6626 NF : constant Entity_Id := Next_Formal (FF);
6629 -- Check that equality was properly defined, ignore call if not
6636 A : constant Entity_Id :=
6637 Make_Defining_Identifier (Sloc (FF),
6638 Chars => Chars (FF));
6640 B : constant Entity_Id :=
6641 Make_Defining_Identifier (Sloc (NF),
6642 Chars => Chars (NF));
6645 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6647 Formals := New_List (
6648 Make_Parameter_Specification (Loc,
6649 Defining_Identifier => A,
6651 New_Reference_To (Etype (First_Formal (S)),
6652 Sloc (Etype (First_Formal (S))))),
6654 Make_Parameter_Specification (Loc,
6655 Defining_Identifier => B,
6657 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6658 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6661 Make_Subprogram_Declaration (Loc,
6663 Make_Function_Specification (Loc,
6664 Defining_Unit_Name => Op_Name,
6665 Parameter_Specifications => Formals,
6666 Result_Definition =>
6667 New_Reference_To (Standard_Boolean, Loc)));
6669 -- Insert inequality right after equality if it is explicit or after
6670 -- the derived type when implicit. These entities are created only
6671 -- for visibility purposes, and eventually replaced in the course of
6672 -- expansion, so they do not need to be attached to the tree and seen
6673 -- by the back-end. Keeping them internal also avoids spurious
6674 -- freezing problems. The declaration is inserted in the tree for
6675 -- analysis, and removed afterwards. If the equality operator comes
6676 -- from an explicit declaration, attach the inequality immediately
6677 -- after. Else the equality is inherited from a derived type
6678 -- declaration, so insert inequality after that declaration.
6680 if No (Alias (S)) then
6681 Insert_After (Unit_Declaration_Node (S), Decl);
6682 elsif Is_List_Member (Parent (S)) then
6683 Insert_After (Parent (S), Decl);
6685 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6688 Mark_Rewrite_Insertion (Decl);
6689 Set_Is_Intrinsic_Subprogram (Op_Name);
6692 Set_Has_Completion (Op_Name);
6693 Set_Corresponding_Equality (Op_Name, S);
6694 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6696 end Make_Inequality_Operator;
6698 ----------------------
6699 -- May_Need_Actuals --
6700 ----------------------
6702 procedure May_Need_Actuals (Fun : Entity_Id) is
6707 F := First_Formal (Fun);
6709 while Present (F) loop
6710 if No (Default_Value (F)) then
6718 Set_Needs_No_Actuals (Fun, B);
6719 end May_Need_Actuals;
6721 ---------------------
6722 -- Mode_Conformant --
6723 ---------------------
6725 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6728 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6730 end Mode_Conformant;
6732 ---------------------------
6733 -- New_Overloaded_Entity --
6734 ---------------------------
6736 procedure New_Overloaded_Entity
6738 Derived_Type : Entity_Id := Empty)
6740 Overridden_Subp : Entity_Id := Empty;
6741 -- Set if the current scope has an operation that is type-conformant
6742 -- with S, and becomes hidden by S.
6744 Is_Primitive_Subp : Boolean;
6745 -- Set to True if the new subprogram is primitive
6748 -- Entity that S overrides
6750 Prev_Vis : Entity_Id := Empty;
6751 -- Predecessor of E in Homonym chain
6753 procedure Check_For_Primitive_Subprogram
6754 (Is_Primitive : out Boolean;
6755 Is_Overriding : Boolean := False);
6756 -- If the subprogram being analyzed is a primitive operation of the type
6757 -- of a formal or result, set the Has_Primitive_Operations flag on the
6758 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6759 -- corresponding flag on the entity itself for later use.
6761 procedure Check_Synchronized_Overriding
6762 (Def_Id : Entity_Id;
6763 Overridden_Subp : out Entity_Id);
6764 -- First determine if Def_Id is an entry or a subprogram either defined
6765 -- in the scope of a task or protected type, or is a primitive of such
6766 -- a type. Check whether Def_Id overrides a subprogram of an interface
6767 -- implemented by the synchronized type, return the overridden entity
6770 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6771 -- Check that E is declared in the private part of the current package,
6772 -- or in the package body, where it may hide a previous declaration.
6773 -- We can't use In_Private_Part by itself because this flag is also
6774 -- set when freezing entities, so we must examine the place of the
6775 -- declaration in the tree, and recognize wrapper packages as well.
6777 function Is_Overriding_Alias
6779 New_E : Entity_Id) return Boolean;
6780 -- Check whether new subprogram and old subprogram are both inherited
6781 -- from subprograms that have distinct dispatch table entries. This can
6782 -- occur with derivations from instances with accidental homonyms.
6783 -- The function is conservative given that the converse is only true
6784 -- within instances that contain accidental overloadings.
6786 ------------------------------------
6787 -- Check_For_Primitive_Subprogram --
6788 ------------------------------------
6790 procedure Check_For_Primitive_Subprogram
6791 (Is_Primitive : out Boolean;
6792 Is_Overriding : Boolean := False)
6798 function Visible_Part_Type (T : Entity_Id) return Boolean;
6799 -- Returns true if T is declared in the visible part of the current
6800 -- package scope; otherwise returns false. Assumes that T is declared
6803 procedure Check_Private_Overriding (T : Entity_Id);
6804 -- Checks that if a primitive abstract subprogram of a visible
6805 -- abstract type is declared in a private part, then it must override
6806 -- an abstract subprogram declared in the visible part. Also checks
6807 -- that if a primitive function with a controlling result is declared
6808 -- in a private part, then it must override a function declared in
6809 -- the visible part.
6811 ------------------------------
6812 -- Check_Private_Overriding --
6813 ------------------------------
6815 procedure Check_Private_Overriding (T : Entity_Id) is
6817 if Is_Package_Or_Generic_Package (Current_Scope)
6818 and then In_Private_Part (Current_Scope)
6819 and then Visible_Part_Type (T)
6820 and then not In_Instance
6822 if Is_Abstract_Type (T)
6823 and then Is_Abstract_Subprogram (S)
6824 and then (not Is_Overriding
6825 or else not Is_Abstract_Subprogram (E))
6827 Error_Msg_N ("abstract subprograms must be visible "
6828 & "(RM 3.9.3(10))!", S);
6830 elsif Ekind (S) = E_Function
6831 and then Is_Tagged_Type (T)
6832 and then T = Base_Type (Etype (S))
6833 and then not Is_Overriding
6836 ("private function with tagged result must"
6837 & " override visible-part function", S);
6839 ("\move subprogram to the visible part"
6840 & " (RM 3.9.3(10))", S);
6843 end Check_Private_Overriding;
6845 -----------------------
6846 -- Visible_Part_Type --
6847 -----------------------
6849 function Visible_Part_Type (T : Entity_Id) return Boolean is
6850 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
6854 -- If the entity is a private type, then it must be declared in a
6857 if Ekind (T) in Private_Kind then
6861 -- Otherwise, we traverse the visible part looking for its
6862 -- corresponding declaration. We cannot use the declaration
6863 -- node directly because in the private part the entity of a
6864 -- private type is the one in the full view, which does not
6865 -- indicate that it is the completion of something visible.
6867 N := First (Visible_Declarations (Specification (P)));
6868 while Present (N) loop
6869 if Nkind (N) = N_Full_Type_Declaration
6870 and then Present (Defining_Identifier (N))
6871 and then T = Defining_Identifier (N)
6875 elsif Nkind_In (N, N_Private_Type_Declaration,
6876 N_Private_Extension_Declaration)
6877 and then Present (Defining_Identifier (N))
6878 and then T = Full_View (Defining_Identifier (N))
6887 end Visible_Part_Type;
6889 -- Start of processing for Check_For_Primitive_Subprogram
6892 Is_Primitive := False;
6894 if not Comes_From_Source (S) then
6897 -- If subprogram is at library level, it is not primitive operation
6899 elsif Current_Scope = Standard_Standard then
6902 elsif (Is_Package_Or_Generic_Package (Current_Scope)
6903 and then not In_Package_Body (Current_Scope))
6904 or else Is_Overriding
6906 -- For function, check return type
6908 if Ekind (S) = E_Function then
6909 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
6910 F_Typ := Designated_Type (Etype (S));
6915 B_Typ := Base_Type (F_Typ);
6917 if Scope (B_Typ) = Current_Scope
6918 and then not Is_Class_Wide_Type (B_Typ)
6919 and then not Is_Generic_Type (B_Typ)
6921 Is_Primitive := True;
6922 Set_Has_Primitive_Operations (B_Typ);
6923 Set_Is_Primitive (S);
6924 Check_Private_Overriding (B_Typ);
6928 -- For all subprograms, check formals
6930 Formal := First_Formal (S);
6931 while Present (Formal) loop
6932 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
6933 F_Typ := Designated_Type (Etype (Formal));
6935 F_Typ := Etype (Formal);
6938 B_Typ := Base_Type (F_Typ);
6940 if Ekind (B_Typ) = E_Access_Subtype then
6941 B_Typ := Base_Type (B_Typ);
6944 if Scope (B_Typ) = Current_Scope
6945 and then not Is_Class_Wide_Type (B_Typ)
6946 and then not Is_Generic_Type (B_Typ)
6948 Is_Primitive := True;
6949 Set_Is_Primitive (S);
6950 Set_Has_Primitive_Operations (B_Typ);
6951 Check_Private_Overriding (B_Typ);
6954 Next_Formal (Formal);
6957 end Check_For_Primitive_Subprogram;
6959 -----------------------------------
6960 -- Check_Synchronized_Overriding --
6961 -----------------------------------
6963 procedure Check_Synchronized_Overriding
6964 (Def_Id : Entity_Id;
6965 Overridden_Subp : out Entity_Id)
6967 Ifaces_List : Elist_Id;
6971 function Matches_Prefixed_View_Profile
6972 (Prim_Params : List_Id;
6973 Iface_Params : List_Id) return Boolean;
6974 -- Determine whether a subprogram's parameter profile Prim_Params
6975 -- matches that of a potentially overridden interface subprogram
6976 -- Iface_Params. Also determine if the type of first parameter of
6977 -- Iface_Params is an implemented interface.
6979 -----------------------------------
6980 -- Matches_Prefixed_View_Profile --
6981 -----------------------------------
6983 function Matches_Prefixed_View_Profile
6984 (Prim_Params : List_Id;
6985 Iface_Params : List_Id) return Boolean
6987 Iface_Id : Entity_Id;
6988 Iface_Param : Node_Id;
6989 Iface_Typ : Entity_Id;
6990 Prim_Id : Entity_Id;
6991 Prim_Param : Node_Id;
6992 Prim_Typ : Entity_Id;
6994 function Is_Implemented
6995 (Ifaces_List : Elist_Id;
6996 Iface : Entity_Id) return Boolean;
6997 -- Determine if Iface is implemented by the current task or
7000 --------------------
7001 -- Is_Implemented --
7002 --------------------
7004 function Is_Implemented
7005 (Ifaces_List : Elist_Id;
7006 Iface : Entity_Id) return Boolean
7008 Iface_Elmt : Elmt_Id;
7011 Iface_Elmt := First_Elmt (Ifaces_List);
7012 while Present (Iface_Elmt) loop
7013 if Node (Iface_Elmt) = Iface then
7017 Next_Elmt (Iface_Elmt);
7023 -- Start of processing for Matches_Prefixed_View_Profile
7026 Iface_Param := First (Iface_Params);
7027 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7029 if Is_Access_Type (Iface_Typ) then
7030 Iface_Typ := Designated_Type (Iface_Typ);
7033 Prim_Param := First (Prim_Params);
7035 -- The first parameter of the potentially overridden subprogram
7036 -- must be an interface implemented by Prim.
7038 if not Is_Interface (Iface_Typ)
7039 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7044 -- The checks on the object parameters are done, move onto the
7045 -- rest of the parameters.
7047 if not In_Scope then
7048 Prim_Param := Next (Prim_Param);
7051 Iface_Param := Next (Iface_Param);
7052 while Present (Iface_Param) and then Present (Prim_Param) loop
7053 Iface_Id := Defining_Identifier (Iface_Param);
7054 Iface_Typ := Find_Parameter_Type (Iface_Param);
7056 Prim_Id := Defining_Identifier (Prim_Param);
7057 Prim_Typ := Find_Parameter_Type (Prim_Param);
7059 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7060 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7061 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7063 Iface_Typ := Designated_Type (Iface_Typ);
7064 Prim_Typ := Designated_Type (Prim_Typ);
7067 -- Case of multiple interface types inside a parameter profile
7069 -- (Obj_Param : in out Iface; ...; Param : Iface)
7071 -- If the interface type is implemented, then the matching type
7072 -- in the primitive should be the implementing record type.
7074 if Ekind (Iface_Typ) = E_Record_Type
7075 and then Is_Interface (Iface_Typ)
7076 and then Is_Implemented (Ifaces_List, Iface_Typ)
7078 if Prim_Typ /= Typ then
7082 -- The two parameters must be both mode and subtype conformant
7084 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7086 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7095 -- One of the two lists contains more parameters than the other
7097 if Present (Iface_Param) or else Present (Prim_Param) then
7102 end Matches_Prefixed_View_Profile;
7104 -- Start of processing for Check_Synchronized_Overriding
7107 Overridden_Subp := Empty;
7109 -- Def_Id must be an entry or a subprogram. We should skip predefined
7110 -- primitives internally generated by the frontend; however at this
7111 -- stage predefined primitives are still not fully decorated. As a
7112 -- minor optimization we skip here internally generated subprograms.
7114 if (Ekind (Def_Id) /= E_Entry
7115 and then Ekind (Def_Id) /= E_Function
7116 and then Ekind (Def_Id) /= E_Procedure)
7117 or else not Comes_From_Source (Def_Id)
7122 -- Search for the concurrent declaration since it contains the list
7123 -- of all implemented interfaces. In this case, the subprogram is
7124 -- declared within the scope of a protected or a task type.
7126 if Present (Scope (Def_Id))
7127 and then Is_Concurrent_Type (Scope (Def_Id))
7128 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7130 Typ := Scope (Def_Id);
7133 -- The enclosing scope is not a synchronized type and the subprogram
7136 elsif No (First_Formal (Def_Id)) then
7139 -- The subprogram has formals and hence it may be a primitive of a
7143 Typ := Etype (First_Formal (Def_Id));
7145 if Is_Access_Type (Typ) then
7146 Typ := Directly_Designated_Type (Typ);
7149 if Is_Concurrent_Type (Typ)
7150 and then not Is_Generic_Actual_Type (Typ)
7154 -- This case occurs when the concurrent type is declared within
7155 -- a generic unit. As a result the corresponding record has been
7156 -- built and used as the type of the first formal, we just have
7157 -- to retrieve the corresponding concurrent type.
7159 elsif Is_Concurrent_Record_Type (Typ)
7160 and then Present (Corresponding_Concurrent_Type (Typ))
7162 Typ := Corresponding_Concurrent_Type (Typ);
7170 -- There is no overriding to check if is an inherited operation in a
7171 -- type derivation on for a generic actual.
7173 Collect_Interfaces (Typ, Ifaces_List);
7175 if Is_Empty_Elmt_List (Ifaces_List) then
7179 -- Determine whether entry or subprogram Def_Id overrides a primitive
7180 -- operation that belongs to one of the interfaces in Ifaces_List.
7183 Candidate : Entity_Id := Empty;
7184 Hom : Entity_Id := Empty;
7185 Iface_Typ : Entity_Id;
7186 Subp : Entity_Id := Empty;
7189 -- Traverse the homonym chain, looking at a potentially
7190 -- overridden subprogram that belongs to an implemented
7193 Hom := Current_Entity_In_Scope (Def_Id);
7194 while Present (Hom) loop
7198 or else not Is_Overloadable (Subp)
7199 or else not Is_Primitive (Subp)
7200 or else not Is_Dispatching_Operation (Subp)
7201 or else not Present (Find_Dispatching_Type (Subp))
7202 or else not Is_Interface (Find_Dispatching_Type (Subp))
7206 -- Entries and procedures can override abstract or null
7207 -- interface procedures
7209 elsif (Ekind (Def_Id) = E_Procedure
7210 or else Ekind (Def_Id) = E_Entry)
7211 and then Ekind (Subp) = E_Procedure
7212 and then Matches_Prefixed_View_Profile
7213 (Parameter_Specifications (Parent (Def_Id)),
7214 Parameter_Specifications (Parent (Subp)))
7218 -- For an overridden subprogram Subp, check whether the mode
7219 -- of its first parameter is correct depending on the kind
7220 -- of synchronized type.
7223 Formal : constant Node_Id := First_Formal (Candidate);
7226 -- In order for an entry or a protected procedure to
7227 -- override, the first parameter of the overridden
7228 -- routine must be of mode "out", "in out" or
7229 -- access-to-variable.
7231 if (Ekind (Candidate) = E_Entry
7232 or else Ekind (Candidate) = E_Procedure)
7233 and then Is_Protected_Type (Typ)
7234 and then Ekind (Formal) /= E_In_Out_Parameter
7235 and then Ekind (Formal) /= E_Out_Parameter
7236 and then Nkind (Parameter_Type (Parent (Formal)))
7237 /= N_Access_Definition
7241 -- All other cases are OK since a task entry or routine
7242 -- does not have a restriction on the mode of the first
7243 -- parameter of the overridden interface routine.
7246 Overridden_Subp := Candidate;
7251 -- Functions can override abstract interface functions
7253 elsif Ekind (Def_Id) = E_Function
7254 and then Ekind (Subp) = E_Function
7255 and then Matches_Prefixed_View_Profile
7256 (Parameter_Specifications (Parent (Def_Id)),
7257 Parameter_Specifications (Parent (Subp)))
7258 and then Etype (Result_Definition (Parent (Def_Id))) =
7259 Etype (Result_Definition (Parent (Subp)))
7261 Overridden_Subp := Subp;
7265 Hom := Homonym (Hom);
7268 -- After examining all candidates for overriding, we are
7269 -- left with the best match which is a mode incompatible
7270 -- interface routine. Do not emit an error if the Expander
7271 -- is active since this error will be detected later on
7272 -- after all concurrent types are expanded and all wrappers
7273 -- are built. This check is meant for spec-only
7276 if Present (Candidate)
7277 and then not Expander_Active
7280 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7282 -- Def_Id is primitive of a protected type, declared
7283 -- inside the type, and the candidate is primitive of a
7284 -- limited or synchronized interface.
7287 and then Is_Protected_Type (Typ)
7289 (Is_Limited_Interface (Iface_Typ)
7290 or else Is_Protected_Interface (Iface_Typ)
7291 or else Is_Synchronized_Interface (Iface_Typ)
7292 or else Is_Task_Interface (Iface_Typ))
7294 -- Must reword this message, comma before to in -gnatj
7298 ("first formal of & must be of mode `OUT`, `IN OUT`"
7299 & " or access-to-variable", Typ, Candidate);
7301 ("\to be overridden by protected procedure or entry "
7302 & "(RM 9.4(11.9/2))", Typ);
7306 Overridden_Subp := Candidate;
7309 end Check_Synchronized_Overriding;
7311 ----------------------------
7312 -- Is_Private_Declaration --
7313 ----------------------------
7315 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7316 Priv_Decls : List_Id;
7317 Decl : constant Node_Id := Unit_Declaration_Node (E);
7320 if Is_Package_Or_Generic_Package (Current_Scope)
7321 and then In_Private_Part (Current_Scope)
7324 Private_Declarations (
7325 Specification (Unit_Declaration_Node (Current_Scope)));
7327 return In_Package_Body (Current_Scope)
7329 (Is_List_Member (Decl)
7330 and then List_Containing (Decl) = Priv_Decls)
7331 or else (Nkind (Parent (Decl)) = N_Package_Specification
7334 (Defining_Entity (Parent (Decl)))
7335 and then List_Containing (Parent (Parent (Decl)))
7340 end Is_Private_Declaration;
7342 --------------------------
7343 -- Is_Overriding_Alias --
7344 --------------------------
7346 function Is_Overriding_Alias
7348 New_E : Entity_Id) return Boolean
7350 AO : constant Entity_Id := Alias (Old_E);
7351 AN : constant Entity_Id := Alias (New_E);
7354 return Scope (AO) /= Scope (AN)
7355 or else No (DTC_Entity (AO))
7356 or else No (DTC_Entity (AN))
7357 or else DT_Position (AO) = DT_Position (AN);
7358 end Is_Overriding_Alias;
7360 -- Start of processing for New_Overloaded_Entity
7363 -- We need to look for an entity that S may override. This must be a
7364 -- homonym in the current scope, so we look for the first homonym of
7365 -- S in the current scope as the starting point for the search.
7367 E := Current_Entity_In_Scope (S);
7369 -- If there is no homonym then this is definitely not overriding
7372 Enter_Overloaded_Entity (S);
7373 Check_Dispatching_Operation (S, Empty);
7374 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7376 -- If subprogram has an explicit declaration, check whether it
7377 -- has an overriding indicator.
7379 if Comes_From_Source (S) then
7380 Check_Synchronized_Overriding (S, Overridden_Subp);
7381 Check_Overriding_Indicator
7382 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7385 -- If there is a homonym that is not overloadable, then we have an
7386 -- error, except for the special cases checked explicitly below.
7388 elsif not Is_Overloadable (E) then
7390 -- Check for spurious conflict produced by a subprogram that has the
7391 -- same name as that of the enclosing generic package. The conflict
7392 -- occurs within an instance, between the subprogram and the renaming
7393 -- declaration for the package. After the subprogram, the package
7394 -- renaming declaration becomes hidden.
7396 if Ekind (E) = E_Package
7397 and then Present (Renamed_Object (E))
7398 and then Renamed_Object (E) = Current_Scope
7399 and then Nkind (Parent (Renamed_Object (E))) =
7400 N_Package_Specification
7401 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7404 Set_Is_Immediately_Visible (E, False);
7405 Enter_Overloaded_Entity (S);
7406 Set_Homonym (S, Homonym (E));
7407 Check_Dispatching_Operation (S, Empty);
7408 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7410 -- If the subprogram is implicit it is hidden by the previous
7411 -- declaration. However if it is dispatching, it must appear in the
7412 -- dispatch table anyway, because it can be dispatched to even if it
7413 -- cannot be called directly.
7415 elsif Present (Alias (S))
7416 and then not Comes_From_Source (S)
7418 Set_Scope (S, Current_Scope);
7420 if Is_Dispatching_Operation (Alias (S)) then
7421 Check_Dispatching_Operation (S, Empty);
7427 Error_Msg_Sloc := Sloc (E);
7429 -- Generate message, with useful additional warning if in generic
7431 if Is_Generic_Unit (E) then
7432 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7433 Error_Msg_N ("\& conflicts with declaration#", S);
7435 Error_Msg_N ("& conflicts with declaration#", S);
7441 -- E exists and is overloadable
7444 -- Ada 2005 (AI-251): Derivation of abstract interface primitives
7445 -- need no check against the homonym chain. They are directly added
7446 -- to the list of primitive operations of Derived_Type.
7448 if Ada_Version >= Ada_05
7449 and then Present (Derived_Type)
7450 and then Is_Dispatching_Operation (Alias (S))
7451 and then Present (Find_Dispatching_Type (Alias (S)))
7452 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7454 goto Add_New_Entity;
7457 Check_Synchronized_Overriding (S, Overridden_Subp);
7459 -- Loop through E and its homonyms to determine if any of them is
7460 -- the candidate for overriding by S.
7462 while Present (E) loop
7464 -- Definitely not interesting if not in the current scope
7466 if Scope (E) /= Current_Scope then
7469 -- Check if we have type conformance
7471 elsif Type_Conformant (E, S) then
7473 -- If the old and new entities have the same profile and one
7474 -- is not the body of the other, then this is an error, unless
7475 -- one of them is implicitly declared.
7477 -- There are some cases when both can be implicit, for example
7478 -- when both a literal and a function that overrides it are
7479 -- inherited in a derivation, or when an inherited operation
7480 -- of a tagged full type overrides the inherited operation of
7481 -- a private extension. Ada 83 had a special rule for the
7482 -- literal case. In Ada95, the later implicit operation hides
7483 -- the former, and the literal is always the former. In the
7484 -- odd case where both are derived operations declared at the
7485 -- same point, both operations should be declared, and in that
7486 -- case we bypass the following test and proceed to the next
7487 -- part. This can only occur for certain obscure cases in
7488 -- instances, when an operation on a type derived from a formal
7489 -- private type does not override a homograph inherited from
7490 -- the actual. In subsequent derivations of such a type, the
7491 -- DT positions of these operations remain distinct, if they
7494 if Present (Alias (S))
7495 and then (No (Alias (E))
7496 or else Comes_From_Source (E)
7497 or else Is_Abstract_Subprogram (S)
7499 (Is_Dispatching_Operation (E)
7500 and then Is_Overriding_Alias (E, S)))
7501 and then Ekind (E) /= E_Enumeration_Literal
7503 -- When an derived operation is overloaded it may be due to
7504 -- the fact that the full view of a private extension
7505 -- re-inherits. It has to be dealt with.
7507 if Is_Package_Or_Generic_Package (Current_Scope)
7508 and then In_Private_Part (Current_Scope)
7510 Check_Operation_From_Private_View (S, E);
7513 -- In any case the implicit operation remains hidden by
7514 -- the existing declaration, which is overriding.
7516 Set_Is_Overriding_Operation (E);
7518 if Comes_From_Source (E) then
7519 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7521 -- Indicate that E overrides the operation from which
7524 if Present (Alias (S)) then
7525 Set_Overridden_Operation (E, Alias (S));
7527 Set_Overridden_Operation (E, S);
7533 -- Within an instance, the renaming declarations for actual
7534 -- subprograms may become ambiguous, but they do not hide each
7537 elsif Ekind (E) /= E_Entry
7538 and then not Comes_From_Source (E)
7539 and then not Is_Generic_Instance (E)
7540 and then (Present (Alias (E))
7541 or else Is_Intrinsic_Subprogram (E))
7542 and then (not In_Instance
7543 or else No (Parent (E))
7544 or else Nkind (Unit_Declaration_Node (E)) /=
7545 N_Subprogram_Renaming_Declaration)
7547 -- A subprogram child unit is not allowed to override an
7548 -- inherited subprogram (10.1.1(20)).
7550 if Is_Child_Unit (S) then
7552 ("child unit overrides inherited subprogram in parent",
7557 if Is_Non_Overriding_Operation (E, S) then
7558 Enter_Overloaded_Entity (S);
7560 if No (Derived_Type)
7561 or else Is_Tagged_Type (Derived_Type)
7563 Check_Dispatching_Operation (S, Empty);
7569 -- E is a derived operation or an internal operator which
7570 -- is being overridden. Remove E from further visibility.
7571 -- Furthermore, if E is a dispatching operation, it must be
7572 -- replaced in the list of primitive operations of its type
7573 -- (see Override_Dispatching_Operation).
7575 Overridden_Subp := E;
7581 Prev := First_Entity (Current_Scope);
7582 while Present (Prev)
7583 and then Next_Entity (Prev) /= E
7588 -- It is possible for E to be in the current scope and
7589 -- yet not in the entity chain. This can only occur in a
7590 -- generic context where E is an implicit concatenation
7591 -- in the formal part, because in a generic body the
7592 -- entity chain starts with the formals.
7595 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7597 -- E must be removed both from the entity_list of the
7598 -- current scope, and from the visibility chain
7600 if Debug_Flag_E then
7601 Write_Str ("Override implicit operation ");
7602 Write_Int (Int (E));
7606 -- If E is a predefined concatenation, it stands for four
7607 -- different operations. As a result, a single explicit
7608 -- declaration does not hide it. In a possible ambiguous
7609 -- situation, Disambiguate chooses the user-defined op,
7610 -- so it is correct to retain the previous internal one.
7612 if Chars (E) /= Name_Op_Concat
7613 or else Ekind (E) /= E_Operator
7615 -- For nondispatching derived operations that are
7616 -- overridden by a subprogram declared in the private
7617 -- part of a package, we retain the derived subprogram
7618 -- but mark it as not immediately visible. If the
7619 -- derived operation was declared in the visible part
7620 -- then this ensures that it will still be visible
7621 -- outside the package with the proper signature
7622 -- (calls from outside must also be directed to this
7623 -- version rather than the overriding one, unlike the
7624 -- dispatching case). Calls from inside the package
7625 -- will still resolve to the overriding subprogram
7626 -- since the derived one is marked as not visible
7627 -- within the package.
7629 -- If the private operation is dispatching, we achieve
7630 -- the overriding by keeping the implicit operation
7631 -- but setting its alias to be the overriding one. In
7632 -- this fashion the proper body is executed in all
7633 -- cases, but the original signature is used outside
7636 -- If the overriding is not in the private part, we
7637 -- remove the implicit operation altogether.
7639 if Is_Private_Declaration (S) then
7640 if not Is_Dispatching_Operation (E) then
7641 Set_Is_Immediately_Visible (E, False);
7643 -- Work done in Override_Dispatching_Operation,
7644 -- so nothing else need to be done here.
7650 -- Find predecessor of E in Homonym chain
7652 if E = Current_Entity (E) then
7655 Prev_Vis := Current_Entity (E);
7656 while Homonym (Prev_Vis) /= E loop
7657 Prev_Vis := Homonym (Prev_Vis);
7661 if Prev_Vis /= Empty then
7663 -- Skip E in the visibility chain
7665 Set_Homonym (Prev_Vis, Homonym (E));
7668 Set_Name_Entity_Id (Chars (E), Homonym (E));
7671 Set_Next_Entity (Prev, Next_Entity (E));
7673 if No (Next_Entity (Prev)) then
7674 Set_Last_Entity (Current_Scope, Prev);
7680 Enter_Overloaded_Entity (S);
7681 Set_Is_Overriding_Operation (S);
7682 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7684 -- If S is a user-defined subprogram or a null procedure
7685 -- expanded to override an inherited null procedure, then
7686 -- indicate that E overrides the operation from which S
7687 -- is inherited. It seems odd that Overridden_Operation
7688 -- isn't set in all cases where Is_Overriding_Operation
7689 -- is true, but doing so causes infinite loops in the
7690 -- compiler for implicit overriding subprograms. ???
7692 if Comes_From_Source (S)
7694 (Present (Parent (S))
7696 Nkind (Parent (S)) = N_Procedure_Specification
7698 Null_Present (Parent (S)))
7700 if Present (Alias (E)) then
7701 Set_Overridden_Operation (S, Alias (E));
7703 Set_Overridden_Operation (S, E);
7707 if Is_Dispatching_Operation (E) then
7709 -- An overriding dispatching subprogram inherits the
7710 -- convention of the overridden subprogram (by
7713 Set_Convention (S, Convention (E));
7714 Check_Dispatching_Operation (S, E);
7717 Check_Dispatching_Operation (S, Empty);
7720 Check_For_Primitive_Subprogram
7721 (Is_Primitive_Subp, Is_Overriding => True);
7722 goto Check_Inequality;
7725 -- Apparent redeclarations in instances can occur when two
7726 -- formal types get the same actual type. The subprograms in
7727 -- in the instance are legal, even if not callable from the
7728 -- outside. Calls from within are disambiguated elsewhere.
7729 -- For dispatching operations in the visible part, the usual
7730 -- rules apply, and operations with the same profile are not
7733 elsif (In_Instance_Visible_Part
7734 and then not Is_Dispatching_Operation (E))
7735 or else In_Instance_Not_Visible
7739 -- Here we have a real error (identical profile)
7742 Error_Msg_Sloc := Sloc (E);
7744 -- Avoid cascaded errors if the entity appears in
7745 -- subsequent calls.
7747 Set_Scope (S, Current_Scope);
7749 -- Generate error, with extra useful warning for the case
7750 -- of a generic instance with no completion.
7752 if Is_Generic_Instance (S)
7753 and then not Has_Completion (E)
7756 ("instantiation cannot provide body for&", S);
7757 Error_Msg_N ("\& conflicts with declaration#", S);
7759 Error_Msg_N ("& conflicts with declaration#", S);
7766 -- If one subprogram has an access parameter and the other
7767 -- a parameter of an access type, calls to either might be
7768 -- ambiguous. Verify that parameters match except for the
7769 -- access parameter.
7771 if May_Hide_Profile then
7777 F1 := First_Formal (S);
7778 F2 := First_Formal (E);
7779 while Present (F1) and then Present (F2) loop
7780 if Is_Access_Type (Etype (F1)) then
7781 if not Is_Access_Type (Etype (F2))
7782 or else not Conforming_Types
7783 (Designated_Type (Etype (F1)),
7784 Designated_Type (Etype (F2)),
7787 May_Hide_Profile := False;
7791 not Conforming_Types
7792 (Etype (F1), Etype (F2), Type_Conformant)
7794 May_Hide_Profile := False;
7805 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7816 -- On exit, we know that S is a new entity
7818 Enter_Overloaded_Entity (S);
7819 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7820 Check_Overriding_Indicator
7821 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7823 -- If S is a derived operation for an untagged type then by
7824 -- definition it's not a dispatching operation (even if the parent
7825 -- operation was dispatching), so we don't call
7826 -- Check_Dispatching_Operation in that case.
7828 if No (Derived_Type)
7829 or else Is_Tagged_Type (Derived_Type)
7831 Check_Dispatching_Operation (S, Empty);
7835 -- If this is a user-defined equality operator that is not a derived
7836 -- subprogram, create the corresponding inequality. If the operation is
7837 -- dispatching, the expansion is done elsewhere, and we do not create
7838 -- an explicit inequality operation.
7840 <<Check_Inequality>>
7841 if Chars (S) = Name_Op_Eq
7842 and then Etype (S) = Standard_Boolean
7843 and then Present (Parent (S))
7844 and then not Is_Dispatching_Operation (S)
7846 Make_Inequality_Operator (S);
7848 end New_Overloaded_Entity;
7850 ---------------------
7851 -- Process_Formals --
7852 ---------------------
7854 procedure Process_Formals
7856 Related_Nod : Node_Id)
7858 Param_Spec : Node_Id;
7860 Formal_Type : Entity_Id;
7864 Num_Out_Params : Nat := 0;
7865 First_Out_Param : Entity_Id := Empty;
7866 -- Used for setting Is_Only_Out_Parameter
7868 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
7869 -- Determine whether an access type designates a type coming from a
7872 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
7873 -- Check whether the default has a class-wide type. After analysis the
7874 -- default has the type of the formal, so we must also check explicitly
7875 -- for an access attribute.
7877 -------------------------------
7878 -- Designates_From_With_Type --
7879 -------------------------------
7881 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
7882 Desig : Entity_Id := Typ;
7885 if Is_Access_Type (Desig) then
7886 Desig := Directly_Designated_Type (Desig);
7889 if Is_Class_Wide_Type (Desig) then
7890 Desig := Root_Type (Desig);
7894 Ekind (Desig) = E_Incomplete_Type
7895 and then From_With_Type (Desig);
7896 end Designates_From_With_Type;
7898 ---------------------------
7899 -- Is_Class_Wide_Default --
7900 ---------------------------
7902 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
7904 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
7905 or else (Nkind (D) = N_Attribute_Reference
7906 and then Attribute_Name (D) = Name_Access
7907 and then Is_Class_Wide_Type (Etype (Prefix (D))));
7908 end Is_Class_Wide_Default;
7910 -- Start of processing for Process_Formals
7913 -- In order to prevent premature use of the formals in the same formal
7914 -- part, the Ekind is left undefined until all default expressions are
7915 -- analyzed. The Ekind is established in a separate loop at the end.
7917 Param_Spec := First (T);
7918 while Present (Param_Spec) loop
7919 Formal := Defining_Identifier (Param_Spec);
7920 Set_Never_Set_In_Source (Formal, True);
7921 Enter_Name (Formal);
7923 -- Case of ordinary parameters
7925 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
7926 Find_Type (Parameter_Type (Param_Spec));
7927 Ptype := Parameter_Type (Param_Spec);
7929 if Ptype = Error then
7933 Formal_Type := Entity (Ptype);
7935 if Is_Incomplete_Type (Formal_Type)
7937 (Is_Class_Wide_Type (Formal_Type)
7938 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
7940 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
7941 -- primitive operations, as long as their completion is
7942 -- in the same declarative part. If in the private part
7943 -- this means that the type cannot be a Taft-amendment type.
7944 -- Check is done on package exit. For access to subprograms,
7945 -- the use is legal for Taft-amendment types.
7947 if Is_Tagged_Type (Formal_Type) then
7948 if Ekind (Scope (Current_Scope)) = E_Package
7949 and then In_Private_Part (Scope (Current_Scope))
7950 and then not From_With_Type (Formal_Type)
7951 and then not Is_Class_Wide_Type (Formal_Type)
7954 (Parent (T), N_Access_Function_Definition,
7955 N_Access_Procedure_Definition)
7959 Private_Dependents (Base_Type (Formal_Type)));
7963 -- Special handling of Value_Type for CIL case
7965 elsif Is_Value_Type (Formal_Type) then
7968 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
7969 N_Access_Procedure_Definition)
7972 ("invalid use of incomplete type&",
7973 Param_Spec, Formal_Type);
7975 -- Further checks on the legality of incomplete types
7976 -- in formal parts must be delayed until the freeze point
7977 -- of the enclosing subprogram or access to subprogram.
7980 elsif Ekind (Formal_Type) = E_Void then
7981 Error_Msg_NE ("premature use of&",
7982 Parameter_Type (Param_Spec), Formal_Type);
7985 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7986 -- declaration corresponding to the null-excluding type of the
7987 -- formal in the enclosing scope. Finally, replace the parameter
7988 -- type of the formal with the internal subtype.
7990 if Ada_Version >= Ada_05
7991 and then Null_Exclusion_Present (Param_Spec)
7993 if not Is_Access_Type (Formal_Type) then
7995 ("`NOT NULL` allowed only for an access type", Param_Spec);
7998 if Can_Never_Be_Null (Formal_Type)
7999 and then Comes_From_Source (Related_Nod)
8002 ("`NOT NULL` not allowed (& already excludes null)",
8008 Create_Null_Excluding_Itype
8010 Related_Nod => Related_Nod,
8011 Scope_Id => Scope (Current_Scope));
8013 -- If the designated type of the itype is an itype we
8014 -- decorate it with the Has_Delayed_Freeze attribute to
8015 -- avoid problems with the backend.
8018 -- type T is access procedure;
8019 -- procedure Op (O : not null T);
8021 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8022 Set_Has_Delayed_Freeze (Formal_Type);
8027 -- An access formal type
8031 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8033 -- No need to continue if we already notified errors
8035 if not Present (Formal_Type) then
8039 -- Ada 2005 (AI-254)
8042 AD : constant Node_Id :=
8043 Access_To_Subprogram_Definition
8044 (Parameter_Type (Param_Spec));
8046 if Present (AD) and then Protected_Present (AD) then
8048 Replace_Anonymous_Access_To_Protected_Subprogram
8054 Set_Etype (Formal, Formal_Type);
8055 Default := Expression (Param_Spec);
8057 if Present (Default) then
8058 if Out_Present (Param_Spec) then
8060 ("default initialization only allowed for IN parameters",
8064 -- Do the special preanalysis of the expression (see section on
8065 -- "Handling of Default Expressions" in the spec of package Sem).
8067 Preanalyze_Spec_Expression (Default, Formal_Type);
8069 -- An access to constant cannot be the default for
8070 -- an access parameter that is an access to variable.
8072 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8073 and then not Is_Access_Constant (Formal_Type)
8074 and then Is_Access_Type (Etype (Default))
8075 and then Is_Access_Constant (Etype (Default))
8078 ("formal that is access to variable cannot be initialized " &
8079 "with an access-to-constant expression", Default);
8082 -- Check that the designated type of an access parameter's default
8083 -- is not a class-wide type unless the parameter's designated type
8084 -- is also class-wide.
8086 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8087 and then not Designates_From_With_Type (Formal_Type)
8088 and then Is_Class_Wide_Default (Default)
8089 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8092 ("access to class-wide expression not allowed here", Default);
8095 -- Check incorrect use of dynamically tagged expressions
8097 if Is_Tagged_Type (Formal_Type) then
8098 Check_Dynamically_Tagged_Expression
8101 Related_Nod => Default);
8105 -- Ada 2005 (AI-231): Static checks
8107 if Ada_Version >= Ada_05
8108 and then Is_Access_Type (Etype (Formal))
8109 and then Can_Never_Be_Null (Etype (Formal))
8111 Null_Exclusion_Static_Checks (Param_Spec);
8118 -- If this is the formal part of a function specification, analyze the
8119 -- subtype mark in the context where the formals are visible but not
8120 -- yet usable, and may hide outer homographs.
8122 if Nkind (Related_Nod) = N_Function_Specification then
8123 Analyze_Return_Type (Related_Nod);
8126 -- Now set the kind (mode) of each formal
8128 Param_Spec := First (T);
8130 while Present (Param_Spec) loop
8131 Formal := Defining_Identifier (Param_Spec);
8132 Set_Formal_Mode (Formal);
8134 if Ekind (Formal) = E_In_Parameter then
8135 Set_Default_Value (Formal, Expression (Param_Spec));
8137 if Present (Expression (Param_Spec)) then
8138 Default := Expression (Param_Spec);
8140 if Is_Scalar_Type (Etype (Default)) then
8142 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8144 Formal_Type := Entity (Parameter_Type (Param_Spec));
8147 Formal_Type := Access_Definition
8148 (Related_Nod, Parameter_Type (Param_Spec));
8151 Apply_Scalar_Range_Check (Default, Formal_Type);
8155 elsif Ekind (Formal) = E_Out_Parameter then
8156 Num_Out_Params := Num_Out_Params + 1;
8158 if Num_Out_Params = 1 then
8159 First_Out_Param := Formal;
8162 elsif Ekind (Formal) = E_In_Out_Parameter then
8163 Num_Out_Params := Num_Out_Params + 1;
8169 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8170 Set_Is_Only_Out_Parameter (First_Out_Param);
8172 end Process_Formals;
8178 procedure Process_PPCs
8180 Spec_Id : Entity_Id;
8181 Body_Id : Entity_Id)
8183 Loc : constant Source_Ptr := Sloc (N);
8185 Plist : List_Id := No_List;
8189 function Grab_PPC (Nam : Name_Id) return Node_Id;
8190 -- Prag contains an analyzed precondition or postcondition pragma.
8191 -- This function copies the pragma, changes it to the corresponding
8192 -- Check pragma and returns the Check pragma as the result. The
8193 -- argument Nam is either Name_Precondition or Name_Postcondition.
8199 function Grab_PPC (Nam : Name_Id) return Node_Id is
8200 CP : constant Node_Id := New_Copy_Tree (Prag);
8203 -- Set Analyzed to false, since we want to reanalyze the check
8204 -- procedure. Note that it is only at the outer level that we
8205 -- do this fiddling, for the spec cases, the already preanalyzed
8206 -- parameters are not affected.
8208 -- For a postcondition pragma within a generic, preserve the pragma
8209 -- for later expansion.
8211 Set_Analyzed (CP, False);
8213 if Nam = Name_Postcondition
8214 and then not Expander_Active
8219 -- Change pragma into corresponding pragma Check
8221 Prepend_To (Pragma_Argument_Associations (CP),
8222 Make_Pragma_Argument_Association (Sloc (Prag),
8224 Make_Identifier (Loc,
8226 Set_Pragma_Identifier (CP,
8227 Make_Identifier (Sloc (Prag),
8228 Chars => Name_Check));
8233 -- Start of processing for Process_PPCs
8236 -- Nothing to do if we are not generating code
8238 if Operating_Mode /= Generate_Code then
8242 -- Grab preconditions from spec
8244 if Present (Spec_Id) then
8246 -- Loop through PPC pragmas from spec. Note that preconditions from
8247 -- the body will be analyzed and converted when we scan the body
8248 -- declarations below.
8250 Prag := Spec_PPC_List (Spec_Id);
8251 while Present (Prag) loop
8252 if Pragma_Name (Prag) = Name_Precondition
8253 and then Pragma_Enabled (Prag)
8255 -- Add pragma Check at the start of the declarations of N.
8256 -- Note that this processing reverses the order of the list,
8257 -- which is what we want since new entries were chained to
8258 -- the head of the list.
8260 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
8263 Prag := Next_Pragma (Prag);
8267 -- Build postconditions procedure if needed and prepend the following
8268 -- declaration to the start of the declarations for the subprogram.
8270 -- procedure _postconditions [(_Result : resulttype)] is
8272 -- pragma Check (Postcondition, condition [,message]);
8273 -- pragma Check (Postcondition, condition [,message]);
8277 -- First we deal with the postconditions in the body
8279 if Is_Non_Empty_List (Declarations (N)) then
8281 -- Loop through declarations
8283 Prag := First (Declarations (N));
8284 while Present (Prag) loop
8285 if Nkind (Prag) = N_Pragma then
8287 -- If pragma, capture if enabled postcondition, else ignore
8289 if Pragma_Name (Prag) = Name_Postcondition
8290 and then Check_Enabled (Name_Postcondition)
8292 if Plist = No_List then
8293 Plist := Empty_List;
8298 -- If expansion is disabled, as in a generic unit,
8299 -- save pragma for later expansion.
8301 if not Expander_Active then
8302 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8304 Append (Grab_PPC (Name_Postcondition), Plist);
8310 -- Not a pragma, if comes from source, then end scan
8312 elsif Comes_From_Source (Prag) then
8315 -- Skip stuff not coming from source
8323 -- Now deal with any postconditions from the spec
8325 if Present (Spec_Id) then
8327 -- Loop through PPC pragmas from spec
8329 Prag := Spec_PPC_List (Spec_Id);
8330 while Present (Prag) loop
8331 if Pragma_Name (Prag) = Name_Postcondition
8332 and then Pragma_Enabled (Prag)
8334 if Plist = No_List then
8335 Plist := Empty_List;
8338 if not Expander_Active then
8339 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8341 Append (Grab_PPC (Name_Postcondition), Plist);
8345 Prag := Next_Pragma (Prag);
8349 -- If we had any postconditions and expansion is enabled, build
8350 -- the _Postconditions procedure.
8353 and then Expander_Active
8355 Subp := Defining_Entity (N);
8357 if Etype (Subp) /= Standard_Void_Type then
8359 Make_Parameter_Specification (Loc,
8360 Defining_Identifier =>
8361 Make_Defining_Identifier (Loc,
8362 Chars => Name_uResult),
8363 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
8369 Post_Proc : constant Entity_Id :=
8370 Make_Defining_Identifier (Loc,
8371 Chars => Name_uPostconditions);
8372 -- The entity for the _Postconditions procedure
8374 Prepend_To (Declarations (N),
8375 Make_Subprogram_Body (Loc,
8377 Make_Procedure_Specification (Loc,
8378 Defining_Unit_Name => Post_Proc,
8379 Parameter_Specifications => Parms),
8381 Declarations => Empty_List,
8383 Handled_Statement_Sequence =>
8384 Make_Handled_Sequence_Of_Statements (Loc,
8385 Statements => Plist)));
8387 -- If this is a procedure, set the Postcondition_Proc attribute on
8388 -- the proper defining entity for the subprogram.
8390 if Etype (Subp) = Standard_Void_Type then
8391 if Present (Spec_Id) then
8392 Set_Postcondition_Proc (Spec_Id, Post_Proc);
8394 Set_Postcondition_Proc (Body_Id, Post_Proc);
8399 if Present (Spec_Id) then
8400 Set_Has_Postconditions (Spec_Id);
8402 Set_Has_Postconditions (Body_Id);
8407 ----------------------------
8408 -- Reference_Body_Formals --
8409 ----------------------------
8411 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
8416 if Error_Posted (Spec) then
8420 -- Iterate over both lists. They may be of different lengths if the two
8421 -- specs are not conformant.
8423 Fs := First_Formal (Spec);
8424 Fb := First_Formal (Bod);
8425 while Present (Fs) and then Present (Fb) loop
8426 Generate_Reference (Fs, Fb, 'b');
8429 Style.Check_Identifier (Fb, Fs);
8432 Set_Spec_Entity (Fb, Fs);
8433 Set_Referenced (Fs, False);
8437 end Reference_Body_Formals;
8439 -------------------------
8440 -- Set_Actual_Subtypes --
8441 -------------------------
8443 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
8444 Loc : constant Source_Ptr := Sloc (N);
8448 First_Stmt : Node_Id := Empty;
8449 AS_Needed : Boolean;
8452 -- If this is an empty initialization procedure, no need to create
8453 -- actual subtypes (small optimization).
8455 if Ekind (Subp) = E_Procedure
8456 and then Is_Null_Init_Proc (Subp)
8461 Formal := First_Formal (Subp);
8462 while Present (Formal) loop
8463 T := Etype (Formal);
8465 -- We never need an actual subtype for a constrained formal
8467 if Is_Constrained (T) then
8470 -- If we have unknown discriminants, then we do not need an actual
8471 -- subtype, or more accurately we cannot figure it out! Note that
8472 -- all class-wide types have unknown discriminants.
8474 elsif Has_Unknown_Discriminants (T) then
8477 -- At this stage we have an unconstrained type that may need an
8478 -- actual subtype. For sure the actual subtype is needed if we have
8479 -- an unconstrained array type.
8481 elsif Is_Array_Type (T) then
8484 -- The only other case needing an actual subtype is an unconstrained
8485 -- record type which is an IN parameter (we cannot generate actual
8486 -- subtypes for the OUT or IN OUT case, since an assignment can
8487 -- change the discriminant values. However we exclude the case of
8488 -- initialization procedures, since discriminants are handled very
8489 -- specially in this context, see the section entitled "Handling of
8490 -- Discriminants" in Einfo.
8492 -- We also exclude the case of Discrim_SO_Functions (functions used
8493 -- in front end layout mode for size/offset values), since in such
8494 -- functions only discriminants are referenced, and not only are such
8495 -- subtypes not needed, but they cannot always be generated, because
8496 -- of order of elaboration issues.
8498 elsif Is_Record_Type (T)
8499 and then Ekind (Formal) = E_In_Parameter
8500 and then Chars (Formal) /= Name_uInit
8501 and then not Is_Unchecked_Union (T)
8502 and then not Is_Discrim_SO_Function (Subp)
8506 -- All other cases do not need an actual subtype
8512 -- Generate actual subtypes for unconstrained arrays and
8513 -- unconstrained discriminated records.
8516 if Nkind (N) = N_Accept_Statement then
8518 -- If expansion is active, The formal is replaced by a local
8519 -- variable that renames the corresponding entry of the
8520 -- parameter block, and it is this local variable that may
8521 -- require an actual subtype.
8523 if Expander_Active then
8524 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8526 Decl := Build_Actual_Subtype (T, Formal);
8529 if Present (Handled_Statement_Sequence (N)) then
8531 First (Statements (Handled_Statement_Sequence (N)));
8532 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8533 Mark_Rewrite_Insertion (Decl);
8535 -- If the accept statement has no body, there will be no
8536 -- reference to the actuals, so no need to compute actual
8543 Decl := Build_Actual_Subtype (T, Formal);
8544 Prepend (Decl, Declarations (N));
8545 Mark_Rewrite_Insertion (Decl);
8548 -- The declaration uses the bounds of an existing object, and
8549 -- therefore needs no constraint checks.
8551 Analyze (Decl, Suppress => All_Checks);
8553 -- We need to freeze manually the generated type when it is
8554 -- inserted anywhere else than in a declarative part.
8556 if Present (First_Stmt) then
8557 Insert_List_Before_And_Analyze (First_Stmt,
8558 Freeze_Entity (Defining_Identifier (Decl), Loc));
8561 if Nkind (N) = N_Accept_Statement
8562 and then Expander_Active
8564 Set_Actual_Subtype (Renamed_Object (Formal),
8565 Defining_Identifier (Decl));
8567 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8571 Next_Formal (Formal);
8573 end Set_Actual_Subtypes;
8575 ---------------------
8576 -- Set_Formal_Mode --
8577 ---------------------
8579 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8580 Spec : constant Node_Id := Parent (Formal_Id);
8583 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8584 -- since we ensure that corresponding actuals are always valid at the
8585 -- point of the call.
8587 if Out_Present (Spec) then
8588 if Ekind (Scope (Formal_Id)) = E_Function
8589 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8591 Error_Msg_N ("functions can only have IN parameters", Spec);
8592 Set_Ekind (Formal_Id, E_In_Parameter);
8594 elsif In_Present (Spec) then
8595 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8598 Set_Ekind (Formal_Id, E_Out_Parameter);
8599 Set_Never_Set_In_Source (Formal_Id, True);
8600 Set_Is_True_Constant (Formal_Id, False);
8601 Set_Current_Value (Formal_Id, Empty);
8605 Set_Ekind (Formal_Id, E_In_Parameter);
8608 -- Set Is_Known_Non_Null for access parameters since the language
8609 -- guarantees that access parameters are always non-null. We also set
8610 -- Can_Never_Be_Null, since there is no way to change the value.
8612 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8614 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8615 -- null; In Ada 2005, only if then null_exclusion is explicit.
8617 if Ada_Version < Ada_05
8618 or else Can_Never_Be_Null (Etype (Formal_Id))
8620 Set_Is_Known_Non_Null (Formal_Id);
8621 Set_Can_Never_Be_Null (Formal_Id);
8624 -- Ada 2005 (AI-231): Null-exclusion access subtype
8626 elsif Is_Access_Type (Etype (Formal_Id))
8627 and then Can_Never_Be_Null (Etype (Formal_Id))
8629 Set_Is_Known_Non_Null (Formal_Id);
8632 Set_Mechanism (Formal_Id, Default_Mechanism);
8633 Set_Formal_Validity (Formal_Id);
8634 end Set_Formal_Mode;
8636 -------------------------
8637 -- Set_Formal_Validity --
8638 -------------------------
8640 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8642 -- If no validity checking, then we cannot assume anything about the
8643 -- validity of parameters, since we do not know there is any checking
8644 -- of the validity on the call side.
8646 if not Validity_Checks_On then
8649 -- If validity checking for parameters is enabled, this means we are
8650 -- not supposed to make any assumptions about argument values.
8652 elsif Validity_Check_Parameters then
8655 -- If we are checking in parameters, we will assume that the caller is
8656 -- also checking parameters, so we can assume the parameter is valid.
8658 elsif Ekind (Formal_Id) = E_In_Parameter
8659 and then Validity_Check_In_Params
8661 Set_Is_Known_Valid (Formal_Id, True);
8663 -- Similar treatment for IN OUT parameters
8665 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8666 and then Validity_Check_In_Out_Params
8668 Set_Is_Known_Valid (Formal_Id, True);
8670 end Set_Formal_Validity;
8672 ------------------------
8673 -- Subtype_Conformant --
8674 ------------------------
8676 function Subtype_Conformant
8677 (New_Id : Entity_Id;
8679 Skip_Controlling_Formals : Boolean := False) return Boolean
8683 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8684 Skip_Controlling_Formals => Skip_Controlling_Formals);
8686 end Subtype_Conformant;
8688 ---------------------
8689 -- Type_Conformant --
8690 ---------------------
8692 function Type_Conformant
8693 (New_Id : Entity_Id;
8695 Skip_Controlling_Formals : Boolean := False) return Boolean
8699 May_Hide_Profile := False;
8702 (New_Id, Old_Id, Type_Conformant, False, Result,
8703 Skip_Controlling_Formals => Skip_Controlling_Formals);
8705 end Type_Conformant;
8707 -------------------------------
8708 -- Valid_Operator_Definition --
8709 -------------------------------
8711 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8714 Id : constant Name_Id := Chars (Designator);
8718 F := First_Formal (Designator);
8719 while Present (F) loop
8722 if Present (Default_Value (F)) then
8724 ("default values not allowed for operator parameters",
8731 -- Verify that user-defined operators have proper number of arguments
8732 -- First case of operators which can only be unary
8735 or else Id = Name_Op_Abs
8739 -- Case of operators which can be unary or binary
8741 elsif Id = Name_Op_Add
8742 or Id = Name_Op_Subtract
8744 N_OK := (N in 1 .. 2);
8746 -- All other operators can only be binary
8754 ("incorrect number of arguments for operator", Designator);
8758 and then Base_Type (Etype (Designator)) = Standard_Boolean
8759 and then not Is_Intrinsic_Subprogram (Designator)
8762 ("explicit definition of inequality not allowed", Designator);
8764 end Valid_Operator_Definition;