1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2008, 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 Rtsfind; use Rtsfind;
52 with Sem_Cat; use Sem_Cat;
53 with Sem_Ch3; use Sem_Ch3;
54 with Sem_Ch4; use Sem_Ch4;
55 with Sem_Ch5; use Sem_Ch5;
56 with Sem_Ch8; use Sem_Ch8;
57 with Sem_Ch10; use Sem_Ch10;
58 with Sem_Ch12; use Sem_Ch12;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Util; use Sem_Util;
67 with Sem_Type; use Sem_Type;
68 with Sem_Warn; use Sem_Warn;
69 with Sinput; use Sinput;
70 with Stand; use Stand;
71 with Sinfo; use Sinfo;
72 with Sinfo.CN; use Sinfo.CN;
73 with Snames; use Snames;
74 with Stringt; use Stringt;
76 with Stylesw; use Stylesw;
77 with Tbuild; use Tbuild;
78 with Uintp; use Uintp;
79 with Urealp; use Urealp;
80 with Validsw; use Validsw;
82 package body Sem_Ch6 is
84 May_Hide_Profile : Boolean := False;
85 -- This flag is used to indicate that two formals in two subprograms being
86 -- checked for conformance differ only in that one is an access parameter
87 -- while the other is of a general access type with the same designated
88 -- type. In this case, if the rest of the signatures match, a call to
89 -- either subprogram may be ambiguous, which is worth a warning. The flag
90 -- is set in Compatible_Types, and the warning emitted in
91 -- New_Overloaded_Entity.
93 -----------------------
94 -- Local Subprograms --
95 -----------------------
97 procedure Analyze_Return_Statement (N : Node_Id);
98 -- Common processing for simple_ and extended_return_statements
100 procedure Analyze_Function_Return (N : Node_Id);
101 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
102 -- applies to a [generic] function.
104 procedure Analyze_Return_Type (N : Node_Id);
105 -- Subsidiary to Process_Formals: analyze subtype mark in function
106 -- specification, in a context where the formals are visible and hide
109 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
110 -- Analyze a generic subprogram body. N is the body to be analyzed, and
111 -- Gen_Id is the defining entity Id for the corresponding spec.
113 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
114 -- If a subprogram has pragma Inline and inlining is active, use generic
115 -- machinery to build an unexpanded body for the subprogram. This body is
116 -- subsequently used for inline expansions at call sites. If subprogram can
117 -- be inlined (depending on size and nature of local declarations) this
118 -- function returns true. Otherwise subprogram body is treated normally.
119 -- If proper warnings are enabled and the subprogram contains a construct
120 -- that cannot be inlined, the offending construct is flagged accordingly.
122 procedure Check_Conformance
125 Ctype : Conformance_Type;
127 Conforms : out Boolean;
128 Err_Loc : Node_Id := Empty;
129 Get_Inst : Boolean := False;
130 Skip_Controlling_Formals : Boolean := False);
131 -- Given two entities, this procedure checks that the profiles associated
132 -- with these entities meet the conformance criterion given by the third
133 -- parameter. If they conform, Conforms is set True and control returns
134 -- to the caller. If they do not conform, Conforms is set to False, and
135 -- in addition, if Errmsg is True on the call, proper messages are output
136 -- to complain about the conformance failure. If Err_Loc is non_Empty
137 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
138 -- error messages are placed on the appropriate part of the construct
139 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
140 -- against a formal access-to-subprogram type so Get_Instance_Of must
143 procedure Check_Subprogram_Order (N : Node_Id);
144 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
145 -- the alpha ordering rule for N if this ordering requirement applicable.
147 procedure Check_Returns
151 Proc : Entity_Id := Empty);
152 -- Called to check for missing return statements in a function body, or for
153 -- returns present in a procedure body which has No_Return set. HSS is the
154 -- handled statement sequence for the subprogram body. This procedure
155 -- checks all flow paths to make sure they either have return (Mode = 'F',
156 -- used for functions) or do not have a return (Mode = 'P', used for
157 -- No_Return procedures). The flag Err is set if there are any control
158 -- paths not explicitly terminated by a return in the function case, and is
159 -- True otherwise. Proc is the entity for the procedure case and is used
160 -- in posting the warning message.
162 procedure Enter_Overloaded_Entity (S : Entity_Id);
163 -- This procedure makes S, a new overloaded entity, into the first visible
164 -- entity with that name.
166 procedure Install_Entity (E : Entity_Id);
167 -- Make single entity visible. Used for generic formals as well
169 function Is_Non_Overriding_Operation
171 New_E : Entity_Id) return Boolean;
172 -- Enforce the rule given in 12.3(18): a private operation in an instance
173 -- overrides an inherited operation only if the corresponding operation
174 -- was overriding in the generic. This can happen for primitive operations
175 -- of types derived (in the generic unit) from formal private or formal
178 procedure Make_Inequality_Operator (S : Entity_Id);
179 -- Create the declaration for an inequality operator that is implicitly
180 -- created by a user-defined equality operator that yields a boolean.
182 procedure May_Need_Actuals (Fun : Entity_Id);
183 -- Flag functions that can be called without parameters, i.e. those that
184 -- have no parameters, or those for which defaults exist for all parameters
186 procedure Process_PPCs
189 Body_Id : Entity_Id);
190 -- Called from Analyze_Body to deal with scanning post conditions for the
191 -- body and assembling and inserting the _postconditions procedure. N is
192 -- the node for the subprogram body and Body_Id/Spec_Id are the entities
193 -- for the body and separate spec (if there is no separate spec, Spec_Id
196 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
197 -- Formal_Id is an formal parameter entity. This procedure deals with
198 -- setting the proper validity status for this entity, which depends
199 -- on the kind of parameter and the validity checking mode.
201 ------------------------------
202 -- Analyze_Return_Statement --
203 ------------------------------
205 procedure Analyze_Return_Statement (N : Node_Id) is
207 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
208 N_Extended_Return_Statement));
210 Returns_Object : constant Boolean :=
211 Nkind (N) = N_Extended_Return_Statement
213 (Nkind (N) = N_Simple_Return_Statement
214 and then Present (Expression (N)));
215 -- True if we're returning something; that is, "return <expression>;"
216 -- or "return Result : T [:= ...]". False for "return;". Used for error
217 -- checking: If Returns_Object is True, N should apply to a function
218 -- body; otherwise N should apply to a procedure body, entry body,
219 -- accept statement, or extended return statement.
221 function Find_What_It_Applies_To return Entity_Id;
222 -- Find the entity representing the innermost enclosing body, accept
223 -- statement, or extended return statement. If the result is a callable
224 -- construct or extended return statement, then this will be the value
225 -- of the Return_Applies_To attribute. Otherwise, the program is
226 -- illegal. See RM-6.5(4/2).
228 -----------------------------
229 -- Find_What_It_Applies_To --
230 -----------------------------
232 function Find_What_It_Applies_To return Entity_Id is
233 Result : Entity_Id := Empty;
236 -- Loop outward through the Scope_Stack, skipping blocks and loops
238 for J in reverse 0 .. Scope_Stack.Last loop
239 Result := Scope_Stack.Table (J).Entity;
240 exit when Ekind (Result) /= E_Block and then
241 Ekind (Result) /= E_Loop;
244 pragma Assert (Present (Result));
246 end Find_What_It_Applies_To;
248 -- Local declarations
250 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
251 Kind : constant Entity_Kind := Ekind (Scope_Id);
252 Loc : constant Source_Ptr := Sloc (N);
253 Stm_Entity : constant Entity_Id :=
255 (E_Return_Statement, Current_Scope, Loc, 'R');
257 -- Start of processing for Analyze_Return_Statement
260 Set_Return_Statement_Entity (N, Stm_Entity);
262 Set_Etype (Stm_Entity, Standard_Void_Type);
263 Set_Return_Applies_To (Stm_Entity, Scope_Id);
265 -- Place Return entity on scope stack, to simplify enforcement of 6.5
266 -- (4/2): an inner return statement will apply to this extended return.
268 if Nkind (N) = N_Extended_Return_Statement then
269 Push_Scope (Stm_Entity);
272 -- Check that pragma No_Return is obeyed
274 if No_Return (Scope_Id) then
275 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
278 -- Warn on any unassigned OUT parameters if in procedure
280 if Ekind (Scope_Id) = E_Procedure then
281 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
284 -- Check that functions return objects, and other things do not
286 if Kind = E_Function or else Kind = E_Generic_Function then
287 if not Returns_Object then
288 Error_Msg_N ("missing expression in return from function", N);
291 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
292 if Returns_Object then
293 Error_Msg_N ("procedure cannot return value (use function)", N);
296 elsif Kind = E_Entry or else Kind = E_Entry_Family then
297 if Returns_Object then
298 if Is_Protected_Type (Scope (Scope_Id)) then
299 Error_Msg_N ("entry body cannot return value", N);
301 Error_Msg_N ("accept statement cannot return value", N);
305 elsif Kind = E_Return_Statement then
307 -- We are nested within another return statement, which must be an
308 -- extended_return_statement.
310 if Returns_Object then
312 ("extended_return_statement cannot return value; " &
313 "use `""RETURN;""`", N);
317 Error_Msg_N ("illegal context for return statement", N);
320 if Kind = E_Function or else Kind = E_Generic_Function then
321 Analyze_Function_Return (N);
324 if Nkind (N) = N_Extended_Return_Statement then
328 Kill_Current_Values (Last_Assignment_Only => True);
329 Check_Unreachable_Code (N);
330 end Analyze_Return_Statement;
332 ---------------------------------------------
333 -- Analyze_Abstract_Subprogram_Declaration --
334 ---------------------------------------------
336 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
337 Designator : constant Entity_Id :=
338 Analyze_Subprogram_Specification (Specification (N));
339 Scop : constant Entity_Id := Current_Scope;
342 Generate_Definition (Designator);
343 Set_Is_Abstract_Subprogram (Designator);
344 New_Overloaded_Entity (Designator);
345 Check_Delayed_Subprogram (Designator);
347 Set_Categorization_From_Scope (Designator, Scop);
349 if Ekind (Scope (Designator)) = E_Protected_Type then
351 ("abstract subprogram not allowed in protected type", N);
353 -- Issue a warning if the abstract subprogram is neither a dispatching
354 -- operation nor an operation that overrides an inherited subprogram or
355 -- predefined operator, since this most likely indicates a mistake.
357 elsif Warn_On_Redundant_Constructs
358 and then not Is_Dispatching_Operation (Designator)
359 and then not Is_Overriding_Operation (Designator)
360 and then (not Is_Operator_Symbol_Name (Chars (Designator))
361 or else Scop /= Scope (Etype (First_Formal (Designator))))
364 ("?abstract subprogram is not dispatching or overriding", N);
367 Generate_Reference_To_Formals (Designator);
368 end Analyze_Abstract_Subprogram_Declaration;
370 ----------------------------------------
371 -- Analyze_Extended_Return_Statement --
372 ----------------------------------------
374 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
376 Analyze_Return_Statement (N);
377 end Analyze_Extended_Return_Statement;
379 ----------------------------
380 -- Analyze_Function_Call --
381 ----------------------------
383 procedure Analyze_Function_Call (N : Node_Id) is
384 P : constant Node_Id := Name (N);
385 L : constant List_Id := Parameter_Associations (N);
391 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
392 -- as B (A, X). If the rewriting is successful, the call has been
393 -- analyzed and we just return.
395 if Nkind (P) = N_Selected_Component
396 and then Name (N) /= P
397 and then Is_Rewrite_Substitution (N)
398 and then Present (Etype (N))
403 -- If error analyzing name, then set Any_Type as result type and return
405 if Etype (P) = Any_Type then
406 Set_Etype (N, Any_Type);
410 -- Otherwise analyze the parameters
414 while Present (Actual) loop
416 Check_Parameterless_Call (Actual);
422 end Analyze_Function_Call;
424 -----------------------------
425 -- Analyze_Function_Return --
426 -----------------------------
428 procedure Analyze_Function_Return (N : Node_Id) is
429 Loc : constant Source_Ptr := Sloc (N);
430 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
431 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
433 R_Type : constant Entity_Id := Etype (Scope_Id);
434 -- Function result subtype
436 procedure Check_Limited_Return (Expr : Node_Id);
437 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
438 -- limited types. Used only for simple return statements.
439 -- Expr is the expression returned.
441 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
442 -- Check that the return_subtype_indication properly matches the result
443 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
445 --------------------------
446 -- Check_Limited_Return --
447 --------------------------
449 procedure Check_Limited_Return (Expr : Node_Id) is
451 -- Ada 2005 (AI-318-02): Return-by-reference types have been
452 -- removed and replaced by anonymous access results. This is an
453 -- incompatibility with Ada 95. Not clear whether this should be
454 -- enforced yet or perhaps controllable with special switch. ???
456 if Is_Limited_Type (R_Type)
457 and then Comes_From_Source (N)
458 and then not In_Instance_Body
459 and then not OK_For_Limited_Init_In_05 (Expr)
463 if Ada_Version >= Ada_05
464 and then not Debug_Flag_Dot_L
465 and then not GNAT_Mode
468 ("(Ada 2005) cannot copy object of a limited type " &
469 "(RM-2005 6.5(5.5/2))", Expr);
470 if Is_Inherently_Limited_Type (R_Type) then
472 ("\return by reference not permitted in Ada 2005", Expr);
475 -- Warn in Ada 95 mode, to give folks a heads up about this
478 -- In GNAT mode, this is just a warning, to allow it to be
479 -- evilly turned off. Otherwise it is a real error.
481 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
482 if Is_Inherently_Limited_Type (R_Type) then
484 ("return by reference not permitted in Ada 2005 " &
485 "(RM-2005 6.5(5.5/2))?", Expr);
488 ("cannot copy object of a limited type in Ada 2005 " &
489 "(RM-2005 6.5(5.5/2))?", Expr);
492 -- Ada 95 mode, compatibility warnings disabled
495 return; -- skip continuation messages below
499 ("\consider switching to return of access type", Expr);
500 Explain_Limited_Type (R_Type, Expr);
502 end Check_Limited_Return;
504 -------------------------------------
505 -- Check_Return_Subtype_Indication --
506 -------------------------------------
508 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
509 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
510 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
511 -- Subtype given in the extended return statement;
512 -- this must match R_Type.
514 Subtype_Ind : constant Node_Id :=
515 Object_Definition (Original_Node (Obj_Decl));
517 R_Type_Is_Anon_Access :
519 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
521 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
523 Ekind (R_Type) = E_Anonymous_Access_Type;
524 -- True if return type of the function is an anonymous access type
525 -- Can't we make Is_Anonymous_Access_Type in einfo ???
527 R_Stm_Type_Is_Anon_Access :
529 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
531 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
533 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
534 -- True if type of the return object is an anonymous access type
537 -- First, avoid cascade errors:
539 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
543 -- "return access T" case; check that the return statement also has
544 -- "access T", and that the subtypes statically match:
546 if R_Type_Is_Anon_Access then
547 if R_Stm_Type_Is_Anon_Access then
548 if Base_Type (Designated_Type (R_Stm_Type)) /=
549 Base_Type (Designated_Type (R_Type))
550 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
553 ("subtype must statically match function result subtype",
554 Subtype_Mark (Subtype_Ind));
558 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
561 -- Subtype_indication case; check that the types are the same, and
562 -- statically match if appropriate. A null exclusion may be present
563 -- on the return type, on the function specification, on the object
564 -- declaration or on the subtype itself.
566 elsif Base_Type (R_Stm_Type) = Base_Type (R_Type) then
567 if Is_Access_Type (R_Type)
569 (Can_Never_Be_Null (R_Type)
570 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
571 Can_Never_Be_Null (R_Stm_Type)
574 ("subtype must statically match function result subtype",
578 if Is_Constrained (R_Type) then
579 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
581 ("subtype must statically match function result subtype",
586 -- If the function's result type doesn't match the return object
587 -- entity's type, then we check for the case where the result type
588 -- is class-wide, and allow the declaration if the type of the object
589 -- definition matches the class-wide type. This prevents rejection
590 -- in the case where the object declaration is initialized by a call
591 -- to a build-in-place function with a specific result type and the
592 -- object entity had its type changed to that specific type. (Note
593 -- that the ARG believes that return objects should be allowed to
594 -- have a type covered by a class-wide result type in any case, so
595 -- once that relaxation is made (see AI05-32), the above check for
596 -- type compatibility should be changed to test Covers rather than
597 -- equality, and then the following special test will no longer be
600 elsif Is_Class_Wide_Type (R_Type)
602 R_Type = Etype (Object_Definition (Original_Node (Obj_Decl)))
608 ("wrong type for return_subtype_indication", Subtype_Ind);
610 end Check_Return_Subtype_Indication;
612 ---------------------
613 -- Local Variables --
614 ---------------------
618 -- Start of processing for Analyze_Function_Return
621 Set_Return_Present (Scope_Id);
623 if Nkind (N) = N_Simple_Return_Statement then
624 Expr := Expression (N);
625 Analyze_And_Resolve (Expr, R_Type);
626 Check_Limited_Return (Expr);
629 -- Analyze parts specific to extended_return_statement:
632 Obj_Decl : constant Node_Id :=
633 Last (Return_Object_Declarations (N));
635 HSS : constant Node_Id := Handled_Statement_Sequence (N);
638 Expr := Expression (Obj_Decl);
640 -- Note: The check for OK_For_Limited_Init will happen in
641 -- Analyze_Object_Declaration; we treat it as a normal
642 -- object declaration.
646 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
647 Check_Return_Subtype_Indication (Obj_Decl);
649 if Present (HSS) then
652 if Present (Exception_Handlers (HSS)) then
654 -- ???Has_Nested_Block_With_Handler needs to be set.
655 -- Probably by creating an actual N_Block_Statement.
656 -- Probably in Expand.
662 Check_References (Stm_Entity);
666 -- Case of Expr present
670 -- Defend against previous errors
672 and then Nkind (Expr) /= N_Empty
673 and then Present (Etype (Expr))
675 -- Apply constraint check. Note that this is done before the implicit
676 -- conversion of the expression done for anonymous access types to
677 -- ensure correct generation of the null-excluding check associated
678 -- with null-excluding expressions found in return statements.
680 Apply_Constraint_Check (Expr, R_Type);
682 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
683 -- type, apply an implicit conversion of the expression to that type
684 -- to force appropriate static and run-time accessibility checks.
686 if Ada_Version >= Ada_05
687 and then Ekind (R_Type) = E_Anonymous_Access_Type
689 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
690 Analyze_And_Resolve (Expr, R_Type);
693 -- If the result type is class-wide, then check that the return
694 -- expression's type is not declared at a deeper level than the
695 -- function (RM05-6.5(5.6/2)).
697 if Ada_Version >= Ada_05
698 and then Is_Class_Wide_Type (R_Type)
700 if Type_Access_Level (Etype (Expr)) >
701 Subprogram_Access_Level (Scope_Id)
704 ("level of return expression type is deeper than " &
705 "class-wide function!", Expr);
709 if (Is_Class_Wide_Type (Etype (Expr))
710 or else Is_Dynamically_Tagged (Expr))
711 and then not Is_Class_Wide_Type (R_Type)
714 ("dynamically tagged expression not allowed!", Expr);
717 -- ??? A real run-time accessibility check is needed in cases
718 -- involving dereferences of access parameters. For now we just
719 -- check the static cases.
721 if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
722 and then Is_Inherently_Limited_Type (Etype (Scope_Id))
723 and then Object_Access_Level (Expr) >
724 Subprogram_Access_Level (Scope_Id)
727 Make_Raise_Program_Error (Loc,
728 Reason => PE_Accessibility_Check_Failed));
732 ("cannot return a local value by reference?", N);
734 ("\& will be raised at run time?",
735 N, Standard_Program_Error);
739 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
740 and then Null_Exclusion_Present (Parent (Scope_Id))
742 Apply_Compile_Time_Constraint_Error
744 Msg => "(Ada 2005) null not allowed for "
745 & "null-excluding return?",
746 Reason => CE_Null_Not_Allowed);
749 end Analyze_Function_Return;
751 -------------------------------------
752 -- Analyze_Generic_Subprogram_Body --
753 -------------------------------------
755 procedure Analyze_Generic_Subprogram_Body
759 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
760 Kind : constant Entity_Kind := Ekind (Gen_Id);
766 -- Copy body and disable expansion while analyzing the generic For a
767 -- stub, do not copy the stub (which would load the proper body), this
768 -- will be done when the proper body is analyzed.
770 if Nkind (N) /= N_Subprogram_Body_Stub then
771 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
776 Spec := Specification (N);
778 -- Within the body of the generic, the subprogram is callable, and
779 -- behaves like the corresponding non-generic unit.
781 Body_Id := Defining_Entity (Spec);
783 if Kind = E_Generic_Procedure
784 and then Nkind (Spec) /= N_Procedure_Specification
786 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
789 elsif Kind = E_Generic_Function
790 and then Nkind (Spec) /= N_Function_Specification
792 Error_Msg_N ("invalid body for generic function ", Body_Id);
796 Set_Corresponding_Body (Gen_Decl, Body_Id);
798 if Has_Completion (Gen_Id)
799 and then Nkind (Parent (N)) /= N_Subunit
801 Error_Msg_N ("duplicate generic body", N);
804 Set_Has_Completion (Gen_Id);
807 if Nkind (N) = N_Subprogram_Body_Stub then
808 Set_Ekind (Defining_Entity (Specification (N)), Kind);
810 Set_Corresponding_Spec (N, Gen_Id);
813 if Nkind (Parent (N)) = N_Compilation_Unit then
814 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
817 -- Make generic parameters immediately visible in the body. They are
818 -- needed to process the formals declarations. Then make the formals
819 -- visible in a separate step.
825 First_Ent : Entity_Id;
828 First_Ent := First_Entity (Gen_Id);
831 while Present (E) and then not Is_Formal (E) loop
836 Set_Use (Generic_Formal_Declarations (Gen_Decl));
838 -- Now generic formals are visible, and the specification can be
839 -- analyzed, for subsequent conformance check.
841 Body_Id := Analyze_Subprogram_Specification (Spec);
843 -- Make formal parameters visible
847 -- E is the first formal parameter, we loop through the formals
848 -- installing them so that they will be visible.
850 Set_First_Entity (Gen_Id, E);
851 while Present (E) loop
857 -- Visible generic entity is callable within its own body
859 Set_Ekind (Gen_Id, Ekind (Body_Id));
860 Set_Ekind (Body_Id, E_Subprogram_Body);
861 Set_Convention (Body_Id, Convention (Gen_Id));
862 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
863 Set_Scope (Body_Id, Scope (Gen_Id));
864 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
866 if Nkind (N) = N_Subprogram_Body_Stub then
868 -- No body to analyze, so restore state of generic unit
870 Set_Ekind (Gen_Id, Kind);
871 Set_Ekind (Body_Id, Kind);
873 if Present (First_Ent) then
874 Set_First_Entity (Gen_Id, First_Ent);
881 -- If this is a compilation unit, it must be made visible explicitly,
882 -- because the compilation of the declaration, unlike other library
883 -- unit declarations, does not. If it is not a unit, the following
884 -- is redundant but harmless.
886 Set_Is_Immediately_Visible (Gen_Id);
887 Reference_Body_Formals (Gen_Id, Body_Id);
889 if Is_Child_Unit (Gen_Id) then
890 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
893 Set_Actual_Subtypes (N, Current_Scope);
894 Process_PPCs (N, Gen_Id, Body_Id);
896 -- If the generic unit carries pre- or post-conditions, copy them
897 -- to the original generic tree, so that they are properly added
898 -- to any instantiation.
901 Orig : constant Node_Id := Original_Node (N);
905 Cond := First (Declarations (N));
906 while Present (Cond) loop
907 if Nkind (Cond) = N_Pragma
908 and then Pragma_Name (Cond) = Name_Check
910 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
912 elsif Nkind (Cond) = N_Pragma
913 and then Pragma_Name (Cond) = Name_Postcondition
915 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
916 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
925 Analyze_Declarations (Declarations (N));
927 Analyze (Handled_Statement_Sequence (N));
929 Save_Global_References (Original_Node (N));
931 -- Prior to exiting the scope, include generic formals again (if any
932 -- are present) in the set of local entities.
934 if Present (First_Ent) then
935 Set_First_Entity (Gen_Id, First_Ent);
938 Check_References (Gen_Id);
941 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
943 Check_Subprogram_Order (N);
945 -- Outside of its body, unit is generic again
947 Set_Ekind (Gen_Id, Kind);
948 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
951 Style.Check_Identifier (Body_Id, Gen_Id);
954 end Analyze_Generic_Subprogram_Body;
956 -----------------------------
957 -- Analyze_Operator_Symbol --
958 -----------------------------
960 -- An operator symbol such as "+" or "and" may appear in context where the
961 -- literal denotes an entity name, such as "+"(x, y) or in context when it
962 -- is just a string, as in (conjunction = "or"). In these cases the parser
963 -- generates this node, and the semantics does the disambiguation. Other
964 -- such case are actuals in an instantiation, the generic unit in an
965 -- instantiation, and pragma arguments.
967 procedure Analyze_Operator_Symbol (N : Node_Id) is
968 Par : constant Node_Id := Parent (N);
971 if (Nkind (Par) = N_Function_Call
972 and then N = Name (Par))
973 or else Nkind (Par) = N_Function_Instantiation
974 or else (Nkind (Par) = N_Indexed_Component
975 and then N = Prefix (Par))
976 or else (Nkind (Par) = N_Pragma_Argument_Association
977 and then not Is_Pragma_String_Literal (Par))
978 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
979 or else (Nkind (Par) = N_Attribute_Reference
980 and then Attribute_Name (Par) /= Name_Value)
982 Find_Direct_Name (N);
985 Change_Operator_Symbol_To_String_Literal (N);
988 end Analyze_Operator_Symbol;
990 -----------------------------------
991 -- Analyze_Parameter_Association --
992 -----------------------------------
994 procedure Analyze_Parameter_Association (N : Node_Id) is
996 Analyze (Explicit_Actual_Parameter (N));
997 end Analyze_Parameter_Association;
999 ----------------------------
1000 -- Analyze_Procedure_Call --
1001 ----------------------------
1003 procedure Analyze_Procedure_Call (N : Node_Id) is
1004 Loc : constant Source_Ptr := Sloc (N);
1005 P : constant Node_Id := Name (N);
1006 Actuals : constant List_Id := Parameter_Associations (N);
1010 procedure Analyze_Call_And_Resolve;
1011 -- Do Analyze and Resolve calls for procedure call
1013 ------------------------------
1014 -- Analyze_Call_And_Resolve --
1015 ------------------------------
1017 procedure Analyze_Call_And_Resolve is
1019 if Nkind (N) = N_Procedure_Call_Statement then
1021 Resolve (N, Standard_Void_Type);
1025 end Analyze_Call_And_Resolve;
1027 -- Start of processing for Analyze_Procedure_Call
1030 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1031 -- a procedure call or an entry call. The prefix may denote an access
1032 -- to subprogram type, in which case an implicit dereference applies.
1033 -- If the prefix is an indexed component (without implicit dereference)
1034 -- then the construct denotes a call to a member of an entire family.
1035 -- If the prefix is a simple name, it may still denote a call to a
1036 -- parameterless member of an entry family. Resolution of these various
1037 -- interpretations is delicate.
1041 -- If this is a call of the form Obj.Op, the call may have been
1042 -- analyzed and possibly rewritten into a block, in which case
1045 if Analyzed (N) then
1049 -- If error analyzing prefix, then set Any_Type as result and return
1051 if Etype (P) = Any_Type then
1052 Set_Etype (N, Any_Type);
1056 -- Otherwise analyze the parameters
1058 if Present (Actuals) then
1059 Actual := First (Actuals);
1061 while Present (Actual) loop
1063 Check_Parameterless_Call (Actual);
1068 -- Special processing for Elab_Spec and Elab_Body calls
1070 if Nkind (P) = N_Attribute_Reference
1071 and then (Attribute_Name (P) = Name_Elab_Spec
1072 or else Attribute_Name (P) = Name_Elab_Body)
1074 if Present (Actuals) then
1076 ("no parameters allowed for this call", First (Actuals));
1080 Set_Etype (N, Standard_Void_Type);
1083 elsif Is_Entity_Name (P)
1084 and then Is_Record_Type (Etype (Entity (P)))
1085 and then Remote_AST_I_Dereference (P)
1089 elsif Is_Entity_Name (P)
1090 and then Ekind (Entity (P)) /= E_Entry_Family
1092 if Is_Access_Type (Etype (P))
1093 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1094 and then No (Actuals)
1095 and then Comes_From_Source (N)
1097 Error_Msg_N ("missing explicit dereference in call", N);
1100 Analyze_Call_And_Resolve;
1102 -- If the prefix is the simple name of an entry family, this is
1103 -- a parameterless call from within the task body itself.
1105 elsif Is_Entity_Name (P)
1106 and then Nkind (P) = N_Identifier
1107 and then Ekind (Entity (P)) = E_Entry_Family
1108 and then Present (Actuals)
1109 and then No (Next (First (Actuals)))
1111 -- Can be call to parameterless entry family. What appears to be the
1112 -- sole argument is in fact the entry index. Rewrite prefix of node
1113 -- accordingly. Source representation is unchanged by this
1117 Make_Indexed_Component (Loc,
1119 Make_Selected_Component (Loc,
1120 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1121 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1122 Expressions => Actuals);
1123 Set_Name (N, New_N);
1124 Set_Etype (New_N, Standard_Void_Type);
1125 Set_Parameter_Associations (N, No_List);
1126 Analyze_Call_And_Resolve;
1128 elsif Nkind (P) = N_Explicit_Dereference then
1129 if Ekind (Etype (P)) = E_Subprogram_Type then
1130 Analyze_Call_And_Resolve;
1132 Error_Msg_N ("expect access to procedure in call", P);
1135 -- The name can be a selected component or an indexed component that
1136 -- yields an access to subprogram. Such a prefix is legal if the call
1137 -- has parameter associations.
1139 elsif Is_Access_Type (Etype (P))
1140 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1142 if Present (Actuals) then
1143 Analyze_Call_And_Resolve;
1145 Error_Msg_N ("missing explicit dereference in call ", N);
1148 -- If not an access to subprogram, then the prefix must resolve to the
1149 -- name of an entry, entry family, or protected operation.
1151 -- For the case of a simple entry call, P is a selected component where
1152 -- the prefix is the task and the selector name is the entry. A call to
1153 -- a protected procedure will have the same syntax. If the protected
1154 -- object contains overloaded operations, the entity may appear as a
1155 -- function, the context will select the operation whose type is Void.
1157 elsif Nkind (P) = N_Selected_Component
1158 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1160 Ekind (Entity (Selector_Name (P))) = E_Procedure
1162 Ekind (Entity (Selector_Name (P))) = E_Function)
1164 Analyze_Call_And_Resolve;
1166 elsif Nkind (P) = N_Selected_Component
1167 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1168 and then Present (Actuals)
1169 and then No (Next (First (Actuals)))
1171 -- Can be call to parameterless entry family. What appears to be the
1172 -- sole argument is in fact the entry index. Rewrite prefix of node
1173 -- accordingly. Source representation is unchanged by this
1177 Make_Indexed_Component (Loc,
1178 Prefix => New_Copy (P),
1179 Expressions => Actuals);
1180 Set_Name (N, New_N);
1181 Set_Etype (New_N, Standard_Void_Type);
1182 Set_Parameter_Associations (N, No_List);
1183 Analyze_Call_And_Resolve;
1185 -- For the case of a reference to an element of an entry family, P is
1186 -- an indexed component whose prefix is a selected component (task and
1187 -- entry family), and whose index is the entry family index.
1189 elsif Nkind (P) = N_Indexed_Component
1190 and then Nkind (Prefix (P)) = N_Selected_Component
1191 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1193 Analyze_Call_And_Resolve;
1195 -- If the prefix is the name of an entry family, it is a call from
1196 -- within the task body itself.
1198 elsif Nkind (P) = N_Indexed_Component
1199 and then Nkind (Prefix (P)) = N_Identifier
1200 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1203 Make_Selected_Component (Loc,
1204 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1205 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1206 Rewrite (Prefix (P), New_N);
1208 Analyze_Call_And_Resolve;
1210 -- Anything else is an error
1213 Error_Msg_N ("invalid procedure or entry call", N);
1215 end Analyze_Procedure_Call;
1217 -------------------------------------
1218 -- Analyze_Simple_Return_Statement --
1219 -------------------------------------
1221 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1223 if Present (Expression (N)) then
1224 Mark_Coextensions (N, Expression (N));
1227 Analyze_Return_Statement (N);
1228 end Analyze_Simple_Return_Statement;
1230 -------------------------
1231 -- Analyze_Return_Type --
1232 -------------------------
1234 procedure Analyze_Return_Type (N : Node_Id) is
1235 Designator : constant Entity_Id := Defining_Entity (N);
1236 Typ : Entity_Id := Empty;
1239 -- Normal case where result definition does not indicate an error
1241 if Result_Definition (N) /= Error then
1242 if Nkind (Result_Definition (N)) = N_Access_Definition then
1243 Typ := Access_Definition (N, Result_Definition (N));
1244 Set_Parent (Typ, Result_Definition (N));
1245 Set_Is_Local_Anonymous_Access (Typ);
1246 Set_Etype (Designator, Typ);
1248 -- Subtype_Mark case
1251 Find_Type (Result_Definition (N));
1252 Typ := Entity (Result_Definition (N));
1253 Set_Etype (Designator, Typ);
1255 if Ekind (Typ) = E_Incomplete_Type
1256 and then Is_Value_Type (Typ)
1260 elsif Ekind (Typ) = E_Incomplete_Type
1261 or else (Is_Class_Wide_Type (Typ)
1263 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1266 ("invalid use of incomplete type", Result_Definition (N));
1270 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1272 Null_Exclusion_Static_Checks (N);
1274 -- Case where result definition does indicate an error
1277 Set_Etype (Designator, Any_Type);
1279 end Analyze_Return_Type;
1281 -----------------------------
1282 -- Analyze_Subprogram_Body --
1283 -----------------------------
1285 -- This procedure is called for regular subprogram bodies, generic bodies,
1286 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1287 -- specification matters, and is used to create a proper declaration for
1288 -- the subprogram, or to perform conformance checks.
1290 procedure Analyze_Subprogram_Body (N : Node_Id) is
1291 Loc : constant Source_Ptr := Sloc (N);
1292 Body_Deleted : constant Boolean := False;
1293 Body_Spec : constant Node_Id := Specification (N);
1294 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1295 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1296 Conformant : Boolean;
1298 Missing_Ret : Boolean;
1300 Prot_Typ : Entity_Id := Empty;
1301 Spec_Id : Entity_Id;
1302 Spec_Decl : Node_Id := Empty;
1304 Last_Real_Spec_Entity : Entity_Id := Empty;
1305 -- When we analyze a separate spec, the entity chain ends up containing
1306 -- the formals, as well as any itypes generated during analysis of the
1307 -- default expressions for parameters, or the arguments of associated
1308 -- precondition/postcondition pragmas (which are analyzed in the context
1309 -- of the spec since they have visibility on formals).
1311 -- These entities belong with the spec and not the body. However we do
1312 -- the analysis of the body in the context of the spec (again to obtain
1313 -- visibility to the formals), and all the entities generated during
1314 -- this analysis end up also chained to the entity chain of the spec.
1315 -- But they really belong to the body, and there is circuitry to move
1316 -- them from the spec to the body.
1318 -- However, when we do this move, we don't want to move the real spec
1319 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1320 -- variable points to the last real spec entity, so we only move those
1321 -- chained beyond that point. It is initialized to Empty to deal with
1322 -- the case where there is no separate spec.
1324 procedure Check_Anonymous_Return;
1325 -- (Ada 2005): if a function returns an access type that denotes a task,
1326 -- or a type that contains tasks, we must create a master entity for
1327 -- the anonymous type, which typically will be used in an allocator
1328 -- in the body of the function.
1330 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1331 -- Look ahead to recognize a pragma that may appear after the body.
1332 -- If there is a previous spec, check that it appears in the same
1333 -- declarative part. If the pragma is Inline_Always, perform inlining
1334 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1335 -- If the body acts as a spec, and inlining is required, we create a
1336 -- subprogram declaration for it, in order to attach the body to inline.
1337 -- If pragma does not appear after the body, check whether there is
1338 -- an inline pragma before any local declarations.
1340 function Disambiguate_Spec return Entity_Id;
1341 -- When a primitive is declared between the private view and the full
1342 -- view of a concurrent type which implements an interface, a special
1343 -- mechanism is used to find the corresponding spec of the primitive
1346 function Is_Private_Concurrent_Primitive
1347 (Subp_Id : Entity_Id) return Boolean;
1348 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1349 -- type that implements an interface and has a private view.
1351 procedure Set_Trivial_Subprogram (N : Node_Id);
1352 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1353 -- subprogram whose body is being analyzed. N is the statement node
1354 -- causing the flag to be set, if the following statement is a return
1355 -- of an entity, we mark the entity as set in source to suppress any
1356 -- warning on the stylized use of function stubs with a dummy return.
1358 procedure Verify_Overriding_Indicator;
1359 -- If there was a previous spec, the entity has been entered in the
1360 -- current scope previously. If the body itself carries an overriding
1361 -- indicator, check that it is consistent with the known status of the
1364 ----------------------------
1365 -- Check_Anonymous_Return --
1366 ----------------------------
1368 procedure Check_Anonymous_Return is
1373 if Present (Spec_Id) then
1379 if Ekind (Scop) = E_Function
1380 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1381 and then Has_Task (Designated_Type (Etype (Scop)))
1382 and then Expander_Active
1385 Make_Object_Declaration (Loc,
1386 Defining_Identifier =>
1387 Make_Defining_Identifier (Loc, Name_uMaster),
1388 Constant_Present => True,
1389 Object_Definition =>
1390 New_Reference_To (RTE (RE_Master_Id), Loc),
1392 Make_Explicit_Dereference (Loc,
1393 New_Reference_To (RTE (RE_Current_Master), Loc)));
1395 if Present (Declarations (N)) then
1396 Prepend (Decl, Declarations (N));
1398 Set_Declarations (N, New_List (Decl));
1401 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1402 Set_Has_Master_Entity (Scop);
1404 end Check_Anonymous_Return;
1406 -------------------------
1407 -- Check_Inline_Pragma --
1408 -------------------------
1410 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1414 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1415 -- True when N is a pragma Inline or Inline_Always that applies
1416 -- to this subprogram.
1418 -----------------------
1419 -- Is_Inline_Pragma --
1420 -----------------------
1422 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1425 Nkind (N) = N_Pragma
1427 (Pragma_Name (N) = Name_Inline_Always
1430 and then Pragma_Name (N) = Name_Inline))
1433 (Expression (First (Pragma_Argument_Associations (N))))
1435 end Is_Inline_Pragma;
1437 -- Start of processing for Check_Inline_Pragma
1440 if not Expander_Active then
1444 if Is_List_Member (N)
1445 and then Present (Next (N))
1446 and then Is_Inline_Pragma (Next (N))
1450 elsif Nkind (N) /= N_Subprogram_Body_Stub
1451 and then Present (Declarations (N))
1452 and then Is_Inline_Pragma (First (Declarations (N)))
1454 Prag := First (Declarations (N));
1460 if Present (Prag) then
1461 if Present (Spec_Id) then
1462 if List_Containing (N) =
1463 List_Containing (Unit_Declaration_Node (Spec_Id))
1469 -- Create a subprogram declaration, to make treatment uniform
1472 Subp : constant Entity_Id :=
1473 Make_Defining_Identifier (Loc, Chars (Body_Id));
1474 Decl : constant Node_Id :=
1475 Make_Subprogram_Declaration (Loc,
1476 Specification => New_Copy_Tree (Specification (N)));
1478 Set_Defining_Unit_Name (Specification (Decl), Subp);
1480 if Present (First_Formal (Body_Id)) then
1481 Plist := Copy_Parameter_List (Body_Id);
1482 Set_Parameter_Specifications
1483 (Specification (Decl), Plist);
1486 Insert_Before (N, Decl);
1489 Set_Has_Pragma_Inline (Subp);
1491 if Pragma_Name (Prag) = Name_Inline_Always then
1492 Set_Is_Inlined (Subp);
1493 Set_Has_Pragma_Inline_Always (Subp);
1500 end Check_Inline_Pragma;
1502 -----------------------
1503 -- Disambiguate_Spec --
1504 -----------------------
1506 function Disambiguate_Spec return Entity_Id is
1507 Priv_Spec : Entity_Id;
1510 procedure Replace_Types (To_Corresponding : Boolean);
1511 -- Depending on the flag, replace the type of formal parameters of
1512 -- Body_Id if it is a concurrent type implementing interfaces with
1513 -- the corresponding record type or the other way around.
1515 procedure Replace_Types (To_Corresponding : Boolean) is
1517 Formal_Typ : Entity_Id;
1520 Formal := First_Formal (Body_Id);
1521 while Present (Formal) loop
1522 Formal_Typ := Etype (Formal);
1524 -- From concurrent type to corresponding record
1526 if To_Corresponding then
1527 if Is_Concurrent_Type (Formal_Typ)
1528 and then Present (Corresponding_Record_Type (Formal_Typ))
1529 and then Present (Interfaces (
1530 Corresponding_Record_Type (Formal_Typ)))
1533 Corresponding_Record_Type (Formal_Typ));
1536 -- From corresponding record to concurrent type
1539 if Is_Concurrent_Record_Type (Formal_Typ)
1540 and then Present (Interfaces (Formal_Typ))
1543 Corresponding_Concurrent_Type (Formal_Typ));
1547 Next_Formal (Formal);
1551 -- Start of processing for Disambiguate_Spec
1554 -- Try to retrieve the specification of the body as is. All error
1555 -- messages are suppressed because the body may not have a spec in
1556 -- its current state.
1558 Spec_N := Find_Corresponding_Spec (N, False);
1560 -- It is possible that this is the body of a primitive declared
1561 -- between a private and a full view of a concurrent type. The
1562 -- controlling parameter of the spec carries the concurrent type,
1563 -- not the corresponding record type as transformed by Analyze_
1564 -- Subprogram_Specification. In such cases, we undo the change
1565 -- made by the analysis of the specification and try to find the
1568 -- Note that wrappers already have their corresponding specs and
1569 -- bodies set during their creation, so if the candidate spec is
1570 -- a wrapper, then we definately need to swap all types to their
1571 -- original concurrent status.
1574 or else Is_Primitive_Wrapper (Spec_N)
1576 -- Restore all references of corresponding record types to the
1577 -- original concurrent types.
1579 Replace_Types (To_Corresponding => False);
1580 Priv_Spec := Find_Corresponding_Spec (N, False);
1582 -- The current body truly belongs to a primitive declared between
1583 -- a private and a full view. We leave the modified body as is,
1584 -- and return the true spec.
1586 if Present (Priv_Spec)
1587 and then Is_Private_Primitive (Priv_Spec)
1592 -- In case that this is some sort of error, restore the original
1593 -- state of the body.
1595 Replace_Types (To_Corresponding => True);
1599 end Disambiguate_Spec;
1601 -------------------------------------
1602 -- Is_Private_Concurrent_Primitive --
1603 -------------------------------------
1605 function Is_Private_Concurrent_Primitive
1606 (Subp_Id : Entity_Id) return Boolean
1608 Formal_Typ : Entity_Id;
1611 if Present (First_Formal (Subp_Id)) then
1612 Formal_Typ := Etype (First_Formal (Subp_Id));
1614 if Is_Concurrent_Record_Type (Formal_Typ) then
1615 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1618 -- The type of the first formal is a concurrent tagged type with
1622 Is_Concurrent_Type (Formal_Typ)
1623 and then Is_Tagged_Type (Formal_Typ)
1624 and then Has_Private_Declaration (Formal_Typ);
1628 end Is_Private_Concurrent_Primitive;
1630 ----------------------------
1631 -- Set_Trivial_Subprogram --
1632 ----------------------------
1634 procedure Set_Trivial_Subprogram (N : Node_Id) is
1635 Nxt : constant Node_Id := Next (N);
1638 Set_Is_Trivial_Subprogram (Body_Id);
1640 if Present (Spec_Id) then
1641 Set_Is_Trivial_Subprogram (Spec_Id);
1645 and then Nkind (Nxt) = N_Simple_Return_Statement
1646 and then No (Next (Nxt))
1647 and then Present (Expression (Nxt))
1648 and then Is_Entity_Name (Expression (Nxt))
1650 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1652 end Set_Trivial_Subprogram;
1654 ---------------------------------
1655 -- Verify_Overriding_Indicator --
1656 ---------------------------------
1658 procedure Verify_Overriding_Indicator is
1660 if Must_Override (Body_Spec) then
1661 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1662 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1666 elsif not Is_Overriding_Operation (Spec_Id) then
1668 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1671 elsif Must_Not_Override (Body_Spec) then
1672 if Is_Overriding_Operation (Spec_Id) then
1674 ("subprogram& overrides inherited operation",
1675 Body_Spec, Spec_Id);
1677 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1678 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1681 ("subprogram & overrides predefined operator ",
1682 Body_Spec, Spec_Id);
1684 -- If this is not a primitive operation the overriding indicator
1685 -- is altogether illegal.
1687 elsif not Is_Primitive (Spec_Id) then
1688 Error_Msg_N ("overriding indicator only allowed " &
1689 "if subprogram is primitive",
1693 end Verify_Overriding_Indicator;
1695 -- Start of processing for Analyze_Subprogram_Body
1698 if Debug_Flag_C then
1699 Write_Str ("==== Compiling subprogram body ");
1700 Write_Name (Chars (Body_Id));
1701 Write_Str (" from ");
1702 Write_Location (Loc);
1706 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1708 -- Generic subprograms are handled separately. They always have a
1709 -- generic specification. Determine whether current scope has a
1710 -- previous declaration.
1712 -- If the subprogram body is defined within an instance of the same
1713 -- name, the instance appears as a package renaming, and will be hidden
1714 -- within the subprogram.
1716 if Present (Prev_Id)
1717 and then not Is_Overloadable (Prev_Id)
1718 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1719 or else Comes_From_Source (Prev_Id))
1721 if Is_Generic_Subprogram (Prev_Id) then
1723 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1724 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1726 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1730 -- Previous entity conflicts with subprogram name. Attempting to
1731 -- enter name will post error.
1733 Enter_Name (Body_Id);
1737 -- Non-generic case, find the subprogram declaration, if one was seen,
1738 -- or enter new overloaded entity in the current scope. If the
1739 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1740 -- part of the context of one of its subunits. No need to redo the
1743 elsif Prev_Id = Body_Id
1744 and then Has_Completion (Body_Id)
1749 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1751 if Nkind (N) = N_Subprogram_Body_Stub
1752 or else No (Corresponding_Spec (N))
1754 if Is_Private_Concurrent_Primitive (Body_Id) then
1755 Spec_Id := Disambiguate_Spec;
1757 Spec_Id := Find_Corresponding_Spec (N);
1760 -- If this is a duplicate body, no point in analyzing it
1762 if Error_Posted (N) then
1766 -- A subprogram body should cause freezing of its own declaration,
1767 -- but if there was no previous explicit declaration, then the
1768 -- subprogram will get frozen too late (there may be code within
1769 -- the body that depends on the subprogram having been frozen,
1770 -- such as uses of extra formals), so we force it to be frozen
1771 -- here. Same holds if the body and spec are compilation units.
1773 if No (Spec_Id) then
1774 Freeze_Before (N, Body_Id);
1776 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1777 Freeze_Before (N, Spec_Id);
1781 Spec_Id := Corresponding_Spec (N);
1785 -- Do not inline any subprogram that contains nested subprograms, since
1786 -- the backend inlining circuit seems to generate uninitialized
1787 -- references in this case. We know this happens in the case of front
1788 -- end ZCX support, but it also appears it can happen in other cases as
1789 -- well. The backend often rejects attempts to inline in the case of
1790 -- nested procedures anyway, so little if anything is lost by this.
1791 -- Note that this is test is for the benefit of the back-end. There is
1792 -- a separate test for front-end inlining that also rejects nested
1795 -- Do not do this test if errors have been detected, because in some
1796 -- error cases, this code blows up, and we don't need it anyway if
1797 -- there have been errors, since we won't get to the linker anyway.
1799 if Comes_From_Source (Body_Id)
1800 and then Serious_Errors_Detected = 0
1804 P_Ent := Scope (P_Ent);
1805 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1807 if Is_Subprogram (P_Ent) then
1808 Set_Is_Inlined (P_Ent, False);
1810 if Comes_From_Source (P_Ent)
1811 and then Has_Pragma_Inline (P_Ent)
1814 ("cannot inline& (nested subprogram)?",
1821 Check_Inline_Pragma (Spec_Id);
1823 -- Case of fully private operation in the body of the protected type.
1824 -- We must create a declaration for the subprogram, in order to attach
1825 -- the protected subprogram that will be used in internal calls.
1828 and then Comes_From_Source (N)
1829 and then Is_Protected_Type (Current_Scope)
1838 Formal := First_Formal (Body_Id);
1840 -- The protected operation always has at least one formal, namely
1841 -- the object itself, but it is only placed in the parameter list
1842 -- if expansion is enabled.
1845 or else Expander_Active
1847 Plist := Copy_Parameter_List (Body_Id);
1852 if Nkind (Body_Spec) = N_Procedure_Specification then
1854 Make_Procedure_Specification (Loc,
1855 Defining_Unit_Name =>
1856 Make_Defining_Identifier (Sloc (Body_Id),
1857 Chars => Chars (Body_Id)),
1858 Parameter_Specifications => Plist);
1861 Make_Function_Specification (Loc,
1862 Defining_Unit_Name =>
1863 Make_Defining_Identifier (Sloc (Body_Id),
1864 Chars => Chars (Body_Id)),
1865 Parameter_Specifications => Plist,
1866 Result_Definition =>
1867 New_Occurrence_Of (Etype (Body_Id), Loc));
1871 Make_Subprogram_Declaration (Loc,
1872 Specification => New_Spec);
1873 Insert_Before (N, Decl);
1874 Spec_Id := Defining_Unit_Name (New_Spec);
1876 -- Indicate that the entity comes from source, to ensure that
1877 -- cross-reference information is properly generated. The body
1878 -- itself is rewritten during expansion, and the body entity will
1879 -- not appear in calls to the operation.
1881 Set_Comes_From_Source (Spec_Id, True);
1883 Set_Has_Completion (Spec_Id);
1884 Set_Convention (Spec_Id, Convention_Protected);
1887 elsif Present (Spec_Id) then
1888 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1889 Verify_Overriding_Indicator;
1891 -- In general, the spec will be frozen when we start analyzing the
1892 -- body. However, for internally generated operations, such as
1893 -- wrapper functions for inherited operations with controlling
1894 -- results, the spec may not have been frozen by the time we
1895 -- expand the freeze actions that include the bodies. In particular,
1896 -- extra formals for accessibility or for return-in-place may need
1897 -- to be generated. Freeze nodes, if any, are inserted before the
1900 if not Is_Frozen (Spec_Id)
1901 and then Expander_Active
1903 -- Force the generation of its freezing node to ensure proper
1904 -- management of access types in the backend.
1906 -- This is definitely needed for some cases, but it is not clear
1907 -- why, to be investigated further???
1909 Set_Has_Delayed_Freeze (Spec_Id);
1910 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
1914 if Chars (Body_Id) = Name_uPostconditions then
1915 Set_Has_Postconditions (Current_Scope);
1918 -- Place subprogram on scope stack, and make formals visible. If there
1919 -- is a spec, the visible entity remains that of the spec.
1921 if Present (Spec_Id) then
1922 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1924 if Is_Child_Unit (Spec_Id) then
1925 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1929 Style.Check_Identifier (Body_Id, Spec_Id);
1932 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1933 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1935 if Is_Abstract_Subprogram (Spec_Id) then
1936 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1940 Set_Convention (Body_Id, Convention (Spec_Id));
1941 Set_Has_Completion (Spec_Id);
1943 if Is_Protected_Type (Scope (Spec_Id)) then
1944 Prot_Typ := Scope (Spec_Id);
1947 -- If this is a body generated for a renaming, do not check for
1948 -- full conformance. The check is redundant, because the spec of
1949 -- the body is a copy of the spec in the renaming declaration,
1950 -- and the test can lead to spurious errors on nested defaults.
1952 if Present (Spec_Decl)
1953 and then not Comes_From_Source (N)
1955 (Nkind (Original_Node (Spec_Decl)) =
1956 N_Subprogram_Renaming_Declaration
1957 or else (Present (Corresponding_Body (Spec_Decl))
1959 Nkind (Unit_Declaration_Node
1960 (Corresponding_Body (Spec_Decl))) =
1961 N_Subprogram_Renaming_Declaration))
1968 Fully_Conformant, True, Conformant, Body_Id);
1971 -- If the body is not fully conformant, we have to decide if we
1972 -- should analyze it or not. If it has a really messed up profile
1973 -- then we probably should not analyze it, since we will get too
1974 -- many bogus messages.
1976 -- Our decision is to go ahead in the non-fully conformant case
1977 -- only if it is at least mode conformant with the spec. Note
1978 -- that the call to Check_Fully_Conformant has issued the proper
1979 -- error messages to complain about the lack of conformance.
1982 and then not Mode_Conformant (Body_Id, Spec_Id)
1988 if Spec_Id /= Body_Id then
1989 Reference_Body_Formals (Spec_Id, Body_Id);
1992 if Nkind (N) /= N_Subprogram_Body_Stub then
1993 Set_Corresponding_Spec (N, Spec_Id);
1995 -- Ada 2005 (AI-345): If the operation is a primitive operation
1996 -- of a concurrent type, the type of the first parameter has been
1997 -- replaced with the corresponding record, which is the proper
1998 -- run-time structure to use. However, within the body there may
1999 -- be uses of the formals that depend on primitive operations
2000 -- of the type (in particular calls in prefixed form) for which
2001 -- we need the original concurrent type. The operation may have
2002 -- several controlling formals, so the replacement must be done
2005 if Comes_From_Source (Spec_Id)
2006 and then Present (First_Entity (Spec_Id))
2007 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2008 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2010 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2013 (Corresponding_Concurrent_Type
2014 (Etype (First_Entity (Spec_Id))))
2017 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2021 Form := First_Formal (Spec_Id);
2022 while Present (Form) loop
2023 if Etype (Form) = Typ then
2024 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2032 -- Make the formals visible, and place subprogram on scope stack.
2033 -- This is also the point at which we set Last_Real_Spec_Entity
2034 -- to mark the entities which will not be moved to the body.
2036 Install_Formals (Spec_Id);
2037 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2038 Push_Scope (Spec_Id);
2040 -- Make sure that the subprogram is immediately visible. For
2041 -- child units that have no separate spec this is indispensable.
2042 -- Otherwise it is safe albeit redundant.
2044 Set_Is_Immediately_Visible (Spec_Id);
2047 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2048 Set_Ekind (Body_Id, E_Subprogram_Body);
2049 Set_Scope (Body_Id, Scope (Spec_Id));
2050 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2052 -- Case of subprogram body with no previous spec
2056 and then Comes_From_Source (Body_Id)
2057 and then not Suppress_Style_Checks (Body_Id)
2058 and then not In_Instance
2060 Style.Body_With_No_Spec (N);
2063 New_Overloaded_Entity (Body_Id);
2065 if Nkind (N) /= N_Subprogram_Body_Stub then
2066 Set_Acts_As_Spec (N);
2067 Generate_Definition (Body_Id);
2069 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2070 Generate_Reference_To_Formals (Body_Id);
2071 Install_Formals (Body_Id);
2072 Push_Scope (Body_Id);
2076 -- If the return type is an anonymous access type whose designated type
2077 -- is the limited view of a class-wide type and the non-limited view is
2078 -- available, update the return type accordingly.
2080 if Ada_Version >= Ada_05
2081 and then Comes_From_Source (N)
2088 Rtyp := Etype (Current_Scope);
2090 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2091 Etyp := Directly_Designated_Type (Rtyp);
2093 if Is_Class_Wide_Type (Etyp)
2094 and then From_With_Type (Etyp)
2096 Set_Directly_Designated_Type
2097 (Etype (Current_Scope), Available_View (Etyp));
2103 -- If this is the proper body of a stub, we must verify that the stub
2104 -- conforms to the body, and to the previous spec if one was present.
2105 -- we know already that the body conforms to that spec. This test is
2106 -- only required for subprograms that come from source.
2108 if Nkind (Parent (N)) = N_Subunit
2109 and then Comes_From_Source (N)
2110 and then not Error_Posted (Body_Id)
2111 and then Nkind (Corresponding_Stub (Parent (N))) =
2112 N_Subprogram_Body_Stub
2115 Old_Id : constant Entity_Id :=
2117 (Specification (Corresponding_Stub (Parent (N))));
2119 Conformant : Boolean := False;
2122 if No (Spec_Id) then
2123 Check_Fully_Conformant (Body_Id, Old_Id);
2127 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2129 if not Conformant then
2131 -- The stub was taken to be a new declaration. Indicate
2132 -- that it lacks a body.
2134 Set_Has_Completion (Old_Id, False);
2140 Set_Has_Completion (Body_Id);
2141 Check_Eliminated (Body_Id);
2143 if Nkind (N) = N_Subprogram_Body_Stub then
2146 elsif Present (Spec_Id)
2147 and then Expander_Active
2149 (Has_Pragma_Inline_Always (Spec_Id)
2150 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2152 Build_Body_To_Inline (N, Spec_Id);
2155 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2156 -- if its specification we have to install the private withed units.
2157 -- This holds for child units as well.
2159 if Is_Compilation_Unit (Body_Id)
2160 or else Nkind (Parent (N)) = N_Compilation_Unit
2162 Install_Private_With_Clauses (Body_Id);
2165 Check_Anonymous_Return;
2167 -- Set the Protected_Formal field of each extra formal of the protected
2168 -- subprogram to reference the corresponding extra formal of the
2169 -- subprogram that implements it. For regular formals this occurs when
2170 -- the protected subprogram's declaration is expanded, but the extra
2171 -- formals don't get created until the subprogram is frozen. We need to
2172 -- do this before analyzing the protected subprogram's body so that any
2173 -- references to the original subprogram's extra formals will be changed
2174 -- refer to the implementing subprogram's formals (see Expand_Formal).
2176 if Present (Spec_Id)
2177 and then Is_Protected_Type (Scope (Spec_Id))
2178 and then Present (Protected_Body_Subprogram (Spec_Id))
2181 Impl_Subp : constant Entity_Id :=
2182 Protected_Body_Subprogram (Spec_Id);
2183 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2184 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2186 while Present (Prot_Ext_Formal) loop
2187 pragma Assert (Present (Impl_Ext_Formal));
2188 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2189 Next_Formal_With_Extras (Prot_Ext_Formal);
2190 Next_Formal_With_Extras (Impl_Ext_Formal);
2195 -- Now we can go on to analyze the body
2197 HSS := Handled_Statement_Sequence (N);
2198 Set_Actual_Subtypes (N, Current_Scope);
2200 -- Deal with preconditions and postconditions
2202 Process_PPCs (N, Spec_Id, Body_Id);
2204 -- Add a declaration for the Protection object, renaming declarations
2205 -- for discriminals and privals and finally a declaration for the entry
2206 -- family index (if applicable). This form of early expansion is done
2207 -- when the Expander is active because Install_Private_Data_Declarations
2208 -- references entities which were created during regular expansion.
2211 and then Comes_From_Source (N)
2212 and then Present (Prot_Typ)
2213 and then Present (Spec_Id)
2214 and then not Is_Eliminated (Spec_Id)
2216 Install_Private_Data_Declarations
2217 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2220 -- Analyze the declarations (this call will analyze the precondition
2221 -- Check pragmas we prepended to the list, as well as the declaration
2222 -- of the _Postconditions procedure).
2224 Analyze_Declarations (Declarations (N));
2226 -- Check completion, and analyze the statements
2229 Inspect_Deferred_Constant_Completion (Declarations (N));
2232 -- Deal with end of scope processing for the body
2234 Process_End_Label (HSS, 't', Current_Scope);
2236 Check_Subprogram_Order (N);
2237 Set_Analyzed (Body_Id);
2239 -- If we have a separate spec, then the analysis of the declarations
2240 -- caused the entities in the body to be chained to the spec id, but
2241 -- we want them chained to the body id. Only the formal parameters
2242 -- end up chained to the spec id in this case.
2244 if Present (Spec_Id) then
2246 -- We must conform to the categorization of our spec
2248 Validate_Categorization_Dependency (N, Spec_Id);
2250 -- And if this is a child unit, the parent units must conform
2252 if Is_Child_Unit (Spec_Id) then
2253 Validate_Categorization_Dependency
2254 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2257 -- Here is where we move entities from the spec to the body
2259 -- Case where there are entities that stay with the spec
2261 if Present (Last_Real_Spec_Entity) then
2263 -- No body entities (happens when the only real spec entities
2264 -- come from precondition and postcondition pragmas)
2266 if No (Last_Entity (Body_Id)) then
2268 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2270 -- Body entities present (formals), so chain stuff past them
2274 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2277 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2278 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2279 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2281 -- Case where there are no spec entities, in this case there can
2282 -- be no body entities either, so just move everything.
2285 pragma Assert (No (Last_Entity (Body_Id)));
2286 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2287 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2288 Set_First_Entity (Spec_Id, Empty);
2289 Set_Last_Entity (Spec_Id, Empty);
2293 -- If function, check return statements
2295 if Nkind (Body_Spec) = N_Function_Specification then
2300 if Present (Spec_Id) then
2306 if Return_Present (Id) then
2307 Check_Returns (HSS, 'F', Missing_Ret);
2310 Set_Has_Missing_Return (Id);
2313 elsif not Is_Machine_Code_Subprogram (Id)
2314 and then not Body_Deleted
2316 Error_Msg_N ("missing RETURN statement in function body", N);
2320 -- If procedure with No_Return, check returns
2322 elsif Nkind (Body_Spec) = N_Procedure_Specification
2323 and then Present (Spec_Id)
2324 and then No_Return (Spec_Id)
2326 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2329 -- Now we are going to check for variables that are never modified in
2330 -- the body of the procedure. But first we deal with a special case
2331 -- where we want to modify this check. If the body of the subprogram
2332 -- starts with a raise statement or its equivalent, or if the body
2333 -- consists entirely of a null statement, then it is pretty obvious
2334 -- that it is OK to not reference the parameters. For example, this
2335 -- might be the following common idiom for a stubbed function:
2336 -- statement of the procedure raises an exception. In particular this
2337 -- deals with the common idiom of a stubbed function, which might
2338 -- appear as something like
2340 -- function F (A : Integer) return Some_Type;
2343 -- raise Program_Error;
2347 -- Here the purpose of X is simply to satisfy the annoying requirement
2348 -- in Ada that there be at least one return, and we certainly do not
2349 -- want to go posting warnings on X that it is not initialized! On
2350 -- the other hand, if X is entirely unreferenced that should still
2353 -- What we do is to detect these cases, and if we find them, flag the
2354 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2355 -- suppress unwanted warnings. For the case of the function stub above
2356 -- we have a special test to set X as apparently assigned to suppress
2363 -- Skip initial labels (for one thing this occurs when we are in
2364 -- front end ZCX mode, but in any case it is irrelevant), and also
2365 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2367 Stm := First (Statements (HSS));
2368 while Nkind (Stm) = N_Label
2369 or else Nkind (Stm) in N_Push_xxx_Label
2374 -- Do the test on the original statement before expansion
2377 Ostm : constant Node_Id := Original_Node (Stm);
2380 -- If explicit raise statement, turn on flag
2382 if Nkind (Ostm) = N_Raise_Statement then
2383 Set_Trivial_Subprogram (Stm);
2385 -- If null statement, and no following statements, turn on flag
2387 elsif Nkind (Stm) = N_Null_Statement
2388 and then Comes_From_Source (Stm)
2389 and then No (Next (Stm))
2391 Set_Trivial_Subprogram (Stm);
2393 -- Check for explicit call cases which likely raise an exception
2395 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2396 if Is_Entity_Name (Name (Ostm)) then
2398 Ent : constant Entity_Id := Entity (Name (Ostm));
2401 -- If the procedure is marked No_Return, then likely it
2402 -- raises an exception, but in any case it is not coming
2403 -- back here, so turn on the flag.
2405 if Ekind (Ent) = E_Procedure
2406 and then No_Return (Ent)
2408 Set_Trivial_Subprogram (Stm);
2416 -- Check for variables that are never modified
2422 -- If there is a separate spec, then transfer Never_Set_In_Source
2423 -- flags from out parameters to the corresponding entities in the
2424 -- body. The reason we do that is we want to post error flags on
2425 -- the body entities, not the spec entities.
2427 if Present (Spec_Id) then
2428 E1 := First_Entity (Spec_Id);
2429 while Present (E1) loop
2430 if Ekind (E1) = E_Out_Parameter then
2431 E2 := First_Entity (Body_Id);
2432 while Present (E2) loop
2433 exit when Chars (E1) = Chars (E2);
2437 if Present (E2) then
2438 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2446 -- Check references in body unless it was deleted. Note that the
2447 -- check of Body_Deleted here is not just for efficiency, it is
2448 -- necessary to avoid junk warnings on formal parameters.
2450 if not Body_Deleted then
2451 Check_References (Body_Id);
2454 end Analyze_Subprogram_Body;
2456 ------------------------------------
2457 -- Analyze_Subprogram_Declaration --
2458 ------------------------------------
2460 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2461 Designator : constant Entity_Id :=
2462 Analyze_Subprogram_Specification (Specification (N));
2463 Scop : constant Entity_Id := Current_Scope;
2465 -- Start of processing for Analyze_Subprogram_Declaration
2468 Generate_Definition (Designator);
2470 -- Check for RCI unit subprogram declarations for illegal inlined
2471 -- subprograms and subprograms having access parameter or limited
2472 -- parameter without Read and Write attributes (RM E.2.3(12-13)).
2474 Validate_RCI_Subprogram_Declaration (N);
2478 Defining_Entity (N),
2479 " Analyze subprogram spec: ");
2481 if Debug_Flag_C then
2482 Write_Str ("==== Compiling subprogram spec ");
2483 Write_Name (Chars (Designator));
2484 Write_Str (" from ");
2485 Write_Location (Sloc (N));
2489 New_Overloaded_Entity (Designator);
2490 Check_Delayed_Subprogram (Designator);
2492 -- If the type of the first formal of the current subprogram is a non
2493 -- generic tagged private type , mark the subprogram as being a private
2496 if Present (First_Formal (Designator)) then
2498 Formal_Typ : constant Entity_Id :=
2499 Etype (First_Formal (Designator));
2501 Set_Is_Private_Primitive (Designator,
2502 Is_Tagged_Type (Formal_Typ)
2503 and then Is_Private_Type (Formal_Typ)
2504 and then not Is_Generic_Actual_Type (Formal_Typ));
2508 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2511 if Ada_Version >= Ada_05
2512 and then Comes_From_Source (N)
2513 and then Is_Dispatching_Operation (Designator)
2520 if Has_Controlling_Result (Designator) then
2521 Etyp := Etype (Designator);
2524 E := First_Entity (Designator);
2526 and then Is_Formal (E)
2527 and then not Is_Controlling_Formal (E)
2535 if Is_Access_Type (Etyp) then
2536 Etyp := Directly_Designated_Type (Etyp);
2539 if Is_Interface (Etyp)
2540 and then not Is_Abstract_Subprogram (Designator)
2541 and then not (Ekind (Designator) = E_Procedure
2542 and then Null_Present (Specification (N)))
2544 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2546 ("(Ada 2005) interface subprogram % must be abstract or null",
2552 -- What is the following code for, it used to be
2554 -- ??? Set_Suppress_Elaboration_Checks
2555 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2557 -- The following seems equivalent, but a bit dubious
2559 if Elaboration_Checks_Suppressed (Designator) then
2560 Set_Kill_Elaboration_Checks (Designator);
2563 if Scop /= Standard_Standard
2564 and then not Is_Child_Unit (Designator)
2566 Set_Categorization_From_Scope (Designator, Scop);
2568 -- For a compilation unit, check for library-unit pragmas
2570 Push_Scope (Designator);
2571 Set_Categorization_From_Pragmas (N);
2572 Validate_Categorization_Dependency (N, Designator);
2576 -- For a compilation unit, set body required. This flag will only be
2577 -- reset if a valid Import or Interface pragma is processed later on.
2579 if Nkind (Parent (N)) = N_Compilation_Unit then
2580 Set_Body_Required (Parent (N), True);
2582 if Ada_Version >= Ada_05
2583 and then Nkind (Specification (N)) = N_Procedure_Specification
2584 and then Null_Present (Specification (N))
2587 ("null procedure cannot be declared at library level", N);
2591 Generate_Reference_To_Formals (Designator);
2592 Check_Eliminated (Designator);
2594 -- Ada 2005: if procedure is declared with "is null" qualifier,
2595 -- it requires no body.
2597 if Nkind (Specification (N)) = N_Procedure_Specification
2598 and then Null_Present (Specification (N))
2600 Set_Has_Completion (Designator);
2601 Set_Is_Inlined (Designator);
2603 if Is_Protected_Type (Current_Scope) then
2605 ("protected operation cannot be a null procedure", N);
2608 end Analyze_Subprogram_Declaration;
2610 --------------------------------------
2611 -- Analyze_Subprogram_Specification --
2612 --------------------------------------
2614 -- Reminder: N here really is a subprogram specification (not a subprogram
2615 -- declaration). This procedure is called to analyze the specification in
2616 -- both subprogram bodies and subprogram declarations (specs).
2618 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2619 Designator : constant Entity_Id := Defining_Entity (N);
2620 Formals : constant List_Id := Parameter_Specifications (N);
2622 -- Start of processing for Analyze_Subprogram_Specification
2625 Generate_Definition (Designator);
2627 if Nkind (N) = N_Function_Specification then
2628 Set_Ekind (Designator, E_Function);
2629 Set_Mechanism (Designator, Default_Mechanism);
2632 Set_Ekind (Designator, E_Procedure);
2633 Set_Etype (Designator, Standard_Void_Type);
2636 -- Introduce new scope for analysis of the formals and the return type
2638 Set_Scope (Designator, Current_Scope);
2640 if Present (Formals) then
2641 Push_Scope (Designator);
2642 Process_Formals (Formals, N);
2644 -- Ada 2005 (AI-345): If this is an overriding operation of an
2645 -- inherited interface operation, and the controlling type is
2646 -- a synchronized type, replace the type with its corresponding
2647 -- record, to match the proper signature of an overriding operation.
2649 if Ada_Version >= Ada_05 then
2652 Formal_Typ : Entity_Id;
2653 Rec_Typ : Entity_Id;
2656 Formal := First_Formal (Designator);
2657 while Present (Formal) loop
2658 Formal_Typ := Etype (Formal);
2660 if Is_Concurrent_Type (Formal_Typ)
2661 and then Present (Corresponding_Record_Type (Formal_Typ))
2663 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2665 if Present (Interfaces (Rec_Typ)) then
2666 Set_Etype (Formal, Rec_Typ);
2670 Next_Formal (Formal);
2677 elsif Nkind (N) = N_Function_Specification then
2678 Analyze_Return_Type (N);
2681 if Nkind (N) = N_Function_Specification then
2682 if Nkind (Designator) = N_Defining_Operator_Symbol then
2683 Valid_Operator_Definition (Designator);
2686 May_Need_Actuals (Designator);
2688 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2689 -- the subprogram is abstract also. This does not apply to renaming
2690 -- declarations, where abstractness is inherited.
2691 -- In case of primitives associated with abstract interface types
2692 -- the check is applied later (see Analyze_Subprogram_Declaration).
2694 if Is_Abstract_Type (Etype (Designator))
2695 and then not Is_Interface (Etype (Designator))
2696 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2697 and then Nkind (Parent (N)) /=
2698 N_Abstract_Subprogram_Declaration
2700 (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
2703 ("function that returns abstract type must be abstract", N);
2708 end Analyze_Subprogram_Specification;
2710 --------------------------
2711 -- Build_Body_To_Inline --
2712 --------------------------
2714 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
2715 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
2716 Original_Body : Node_Id;
2717 Body_To_Analyze : Node_Id;
2718 Max_Size : constant := 10;
2719 Stat_Count : Integer := 0;
2721 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
2722 -- Check for declarations that make inlining not worthwhile
2724 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
2725 -- Check for statements that make inlining not worthwhile: any tasking
2726 -- statement, nested at any level. Keep track of total number of
2727 -- elementary statements, as a measure of acceptable size.
2729 function Has_Pending_Instantiation return Boolean;
2730 -- If some enclosing body contains instantiations that appear before the
2731 -- corresponding generic body, the enclosing body has a freeze node so
2732 -- that it can be elaborated after the generic itself. This might
2733 -- conflict with subsequent inlinings, so that it is unsafe to try to
2734 -- inline in such a case.
2736 function Has_Single_Return return Boolean;
2737 -- In general we cannot inline functions that return unconstrained type.
2738 -- However, we can handle such functions if all return statements return
2739 -- a local variable that is the only declaration in the body of the
2740 -- function. In that case the call can be replaced by that local
2741 -- variable as is done for other inlined calls.
2743 procedure Remove_Pragmas;
2744 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
2745 -- parameter has no meaning when the body is inlined and the formals
2746 -- are rewritten. Remove it from body to inline. The analysis of the
2747 -- non-inlined body will handle the pragma properly.
2749 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
2750 -- If the body of the subprogram includes a call that returns an
2751 -- unconstrained type, the secondary stack is involved, and it
2752 -- is not worth inlining.
2754 ------------------------------
2755 -- Has_Excluded_Declaration --
2756 ------------------------------
2758 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
2761 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
2762 -- Nested subprograms make a given body ineligible for inlining, but
2763 -- we make an exception for instantiations of unchecked conversion.
2764 -- The body has not been analyzed yet, so check the name, and verify
2765 -- that the visible entity with that name is the predefined unit.
2767 -----------------------------
2768 -- Is_Unchecked_Conversion --
2769 -----------------------------
2771 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
2772 Id : constant Node_Id := Name (D);
2776 if Nkind (Id) = N_Identifier
2777 and then Chars (Id) = Name_Unchecked_Conversion
2779 Conv := Current_Entity (Id);
2781 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
2782 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
2784 Conv := Current_Entity (Selector_Name (Id));
2789 return Present (Conv)
2790 and then Is_Predefined_File_Name
2791 (Unit_File_Name (Get_Source_Unit (Conv)))
2792 and then Is_Intrinsic_Subprogram (Conv);
2793 end Is_Unchecked_Conversion;
2795 -- Start of processing for Has_Excluded_Declaration
2799 while Present (D) loop
2800 if (Nkind (D) = N_Function_Instantiation
2801 and then not Is_Unchecked_Conversion (D))
2802 or else Nkind_In (D, N_Protected_Type_Declaration,
2803 N_Package_Declaration,
2804 N_Package_Instantiation,
2806 N_Procedure_Instantiation,
2807 N_Task_Type_Declaration)
2810 ("cannot inline & (non-allowed declaration)?", D, Subp);
2818 end Has_Excluded_Declaration;
2820 ----------------------------
2821 -- Has_Excluded_Statement --
2822 ----------------------------
2824 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
2830 while Present (S) loop
2831 Stat_Count := Stat_Count + 1;
2833 if Nkind_In (S, N_Abort_Statement,
2834 N_Asynchronous_Select,
2835 N_Conditional_Entry_Call,
2836 N_Delay_Relative_Statement,
2837 N_Delay_Until_Statement,
2842 ("cannot inline & (non-allowed statement)?", S, Subp);
2845 elsif Nkind (S) = N_Block_Statement then
2846 if Present (Declarations (S))
2847 and then Has_Excluded_Declaration (Declarations (S))
2851 elsif Present (Handled_Statement_Sequence (S))
2854 (Exception_Handlers (Handled_Statement_Sequence (S)))
2856 Has_Excluded_Statement
2857 (Statements (Handled_Statement_Sequence (S))))
2862 elsif Nkind (S) = N_Case_Statement then
2863 E := First (Alternatives (S));
2864 while Present (E) loop
2865 if Has_Excluded_Statement (Statements (E)) then
2872 elsif Nkind (S) = N_If_Statement then
2873 if Has_Excluded_Statement (Then_Statements (S)) then
2877 if Present (Elsif_Parts (S)) then
2878 E := First (Elsif_Parts (S));
2879 while Present (E) loop
2880 if Has_Excluded_Statement (Then_Statements (E)) then
2887 if Present (Else_Statements (S))
2888 and then Has_Excluded_Statement (Else_Statements (S))
2893 elsif Nkind (S) = N_Loop_Statement
2894 and then Has_Excluded_Statement (Statements (S))
2903 end Has_Excluded_Statement;
2905 -------------------------------
2906 -- Has_Pending_Instantiation --
2907 -------------------------------
2909 function Has_Pending_Instantiation return Boolean is
2914 while Present (S) loop
2915 if Is_Compilation_Unit (S)
2916 or else Is_Child_Unit (S)
2919 elsif Ekind (S) = E_Package
2920 and then Has_Forward_Instantiation (S)
2929 end Has_Pending_Instantiation;
2931 ------------------------
2932 -- Has_Single_Return --
2933 ------------------------
2935 function Has_Single_Return return Boolean is
2936 Return_Statement : Node_Id := Empty;
2938 function Check_Return (N : Node_Id) return Traverse_Result;
2944 function Check_Return (N : Node_Id) return Traverse_Result is
2946 if Nkind (N) = N_Simple_Return_Statement then
2947 if Present (Expression (N))
2948 and then Is_Entity_Name (Expression (N))
2950 if No (Return_Statement) then
2951 Return_Statement := N;
2954 elsif Chars (Expression (N)) =
2955 Chars (Expression (Return_Statement))
2964 -- Expression has wrong form
2974 function Check_All_Returns is new Traverse_Func (Check_Return);
2976 -- Start of processing for Has_Single_Return
2979 return Check_All_Returns (N) = OK
2980 and then Present (Declarations (N))
2981 and then Present (First (Declarations (N)))
2982 and then Chars (Expression (Return_Statement)) =
2983 Chars (Defining_Identifier (First (Declarations (N))));
2984 end Has_Single_Return;
2986 --------------------
2987 -- Remove_Pragmas --
2988 --------------------
2990 procedure Remove_Pragmas is
2995 Decl := First (Declarations (Body_To_Analyze));
2996 while Present (Decl) loop
2999 if Nkind (Decl) = N_Pragma
3000 and then (Pragma_Name (Decl) = Name_Unreferenced
3002 Pragma_Name (Decl) = Name_Unmodified)
3011 --------------------------
3012 -- Uses_Secondary_Stack --
3013 --------------------------
3015 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3016 function Check_Call (N : Node_Id) return Traverse_Result;
3017 -- Look for function calls that return an unconstrained type
3023 function Check_Call (N : Node_Id) return Traverse_Result is
3025 if Nkind (N) = N_Function_Call
3026 and then Is_Entity_Name (Name (N))
3027 and then Is_Composite_Type (Etype (Entity (Name (N))))
3028 and then not Is_Constrained (Etype (Entity (Name (N))))
3031 ("cannot inline & (call returns unconstrained type)?",
3039 function Check_Calls is new Traverse_Func (Check_Call);
3042 return Check_Calls (Bod) = Abandon;
3043 end Uses_Secondary_Stack;
3045 -- Start of processing for Build_Body_To_Inline
3048 if Nkind (Decl) = N_Subprogram_Declaration
3049 and then Present (Body_To_Inline (Decl))
3051 return; -- Done already.
3053 -- Functions that return unconstrained composite types require
3054 -- secondary stack handling, and cannot currently be inlined, unless
3055 -- all return statements return a local variable that is the first
3056 -- local declaration in the body.
3058 elsif Ekind (Subp) = E_Function
3059 and then not Is_Scalar_Type (Etype (Subp))
3060 and then not Is_Access_Type (Etype (Subp))
3061 and then not Is_Constrained (Etype (Subp))
3063 if not Has_Single_Return then
3065 ("cannot inline & (unconstrained return type)?", N, Subp);
3069 -- Ditto for functions that return controlled types, where controlled
3070 -- actions interfere in complex ways with inlining.
3072 elsif Ekind (Subp) = E_Function
3073 and then Controlled_Type (Etype (Subp))
3076 ("cannot inline & (controlled return type)?", N, Subp);
3080 if Present (Declarations (N))
3081 and then Has_Excluded_Declaration (Declarations (N))
3086 if Present (Handled_Statement_Sequence (N)) then
3087 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3089 ("cannot inline& (exception handler)?",
3090 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3094 Has_Excluded_Statement
3095 (Statements (Handled_Statement_Sequence (N)))
3101 -- We do not inline a subprogram that is too large, unless it is
3102 -- marked Inline_Always. This pragma does not suppress the other
3103 -- checks on inlining (forbidden declarations, handlers, etc).
3105 if Stat_Count > Max_Size
3106 and then not Has_Pragma_Inline_Always (Subp)
3108 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3112 if Has_Pending_Instantiation then
3114 ("cannot inline& (forward instance within enclosing body)?",
3119 -- Within an instance, the body to inline must be treated as a nested
3120 -- generic, so that the proper global references are preserved.
3122 -- Note that we do not do this at the library level, because it is not
3123 -- needed, and furthermore this causes trouble if front end inlining
3124 -- is activated (-gnatN).
3126 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3127 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3128 Original_Body := Copy_Generic_Node (N, Empty, True);
3130 Original_Body := Copy_Separate_Tree (N);
3133 -- We need to capture references to the formals in order to substitute
3134 -- the actuals at the point of inlining, i.e. instantiation. To treat
3135 -- the formals as globals to the body to inline, we nest it within
3136 -- a dummy parameterless subprogram, declared within the real one.
3137 -- To avoid generating an internal name (which is never public, and
3138 -- which affects serial numbers of other generated names), we use
3139 -- an internal symbol that cannot conflict with user declarations.
3141 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3142 Set_Defining_Unit_Name
3143 (Specification (Original_Body),
3144 Make_Defining_Identifier (Sloc (N), Name_uParent));
3145 Set_Corresponding_Spec (Original_Body, Empty);
3147 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3149 -- Set return type of function, which is also global and does not need
3152 if Ekind (Subp) = E_Function then
3153 Set_Result_Definition (Specification (Body_To_Analyze),
3154 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3157 if No (Declarations (N)) then
3158 Set_Declarations (N, New_List (Body_To_Analyze));
3160 Append (Body_To_Analyze, Declarations (N));
3163 Expander_Mode_Save_And_Set (False);
3166 Analyze (Body_To_Analyze);
3167 Push_Scope (Defining_Entity (Body_To_Analyze));
3168 Save_Global_References (Original_Body);
3170 Remove (Body_To_Analyze);
3172 Expander_Mode_Restore;
3174 -- Restore environment if previously saved
3176 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3180 -- If secondary stk used there is no point in inlining. We have
3181 -- already issued the warning in this case, so nothing to do.
3183 if Uses_Secondary_Stack (Body_To_Analyze) then
3187 Set_Body_To_Inline (Decl, Original_Body);
3188 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3189 Set_Is_Inlined (Subp);
3190 end Build_Body_To_Inline;
3196 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3198 -- Do not emit warning if this is a predefined unit which is not
3199 -- the main unit. With validity checks enabled, some predefined
3200 -- subprograms may contain nested subprograms and become ineligible
3203 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3204 and then not In_Extended_Main_Source_Unit (Subp)
3208 elsif Has_Pragma_Inline_Always (Subp) then
3210 -- Remove last character (question mark) to make this into an error,
3211 -- because the Inline_Always pragma cannot be obeyed.
3213 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3215 elsif Ineffective_Inline_Warnings then
3216 Error_Msg_NE (Msg, N, Subp);
3220 -----------------------
3221 -- Check_Conformance --
3222 -----------------------
3224 procedure Check_Conformance
3225 (New_Id : Entity_Id;
3227 Ctype : Conformance_Type;
3229 Conforms : out Boolean;
3230 Err_Loc : Node_Id := Empty;
3231 Get_Inst : Boolean := False;
3232 Skip_Controlling_Formals : Boolean := False)
3234 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3235 -- Post error message for conformance error on given node. Two messages
3236 -- are output. The first points to the previous declaration with a
3237 -- general "no conformance" message. The second is the detailed reason,
3238 -- supplied as Msg. The parameter N provide information for a possible
3239 -- & insertion in the message, and also provides the location for
3240 -- posting the message in the absence of a specified Err_Loc location.
3242 -----------------------
3243 -- Conformance_Error --
3244 -----------------------
3246 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3253 if No (Err_Loc) then
3259 Error_Msg_Sloc := Sloc (Old_Id);
3262 when Type_Conformant =>
3264 ("not type conformant with declaration#!", Enode);
3266 when Mode_Conformant =>
3267 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3269 ("not mode conformant with operation inherited#!",
3273 ("not mode conformant with declaration#!", Enode);
3276 when Subtype_Conformant =>
3277 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3279 ("not subtype conformant with operation inherited#!",
3283 ("not subtype conformant with declaration#!", Enode);
3286 when Fully_Conformant =>
3287 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3289 ("not fully conformant with operation inherited#!",
3293 ("not fully conformant with declaration#!", Enode);
3297 Error_Msg_NE (Msg, Enode, N);
3299 end Conformance_Error;
3303 Old_Type : constant Entity_Id := Etype (Old_Id);
3304 New_Type : constant Entity_Id := Etype (New_Id);
3305 Old_Formal : Entity_Id;
3306 New_Formal : Entity_Id;
3307 Access_Types_Match : Boolean;
3308 Old_Formal_Base : Entity_Id;
3309 New_Formal_Base : Entity_Id;
3311 -- Start of processing for Check_Conformance
3316 -- We need a special case for operators, since they don't appear
3319 if Ctype = Type_Conformant then
3320 if Ekind (New_Id) = E_Operator
3321 and then Operator_Matches_Spec (New_Id, Old_Id)
3327 -- If both are functions/operators, check return types conform
3329 if Old_Type /= Standard_Void_Type
3330 and then New_Type /= Standard_Void_Type
3333 -- If we are checking interface conformance we omit controlling
3334 -- arguments and result, because we are only checking the conformance
3335 -- of the remaining parameters.
3337 if Has_Controlling_Result (Old_Id)
3338 and then Has_Controlling_Result (New_Id)
3339 and then Skip_Controlling_Formals
3343 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3344 Conformance_Error ("\return type does not match!", New_Id);
3348 -- Ada 2005 (AI-231): In case of anonymous access types check the
3349 -- null-exclusion and access-to-constant attributes match.
3351 if Ada_Version >= Ada_05
3352 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3354 (Can_Never_Be_Null (Old_Type)
3355 /= Can_Never_Be_Null (New_Type)
3356 or else Is_Access_Constant (Etype (Old_Type))
3357 /= Is_Access_Constant (Etype (New_Type)))
3359 Conformance_Error ("\return type does not match!", New_Id);
3363 -- If either is a function/operator and the other isn't, error
3365 elsif Old_Type /= Standard_Void_Type
3366 or else New_Type /= Standard_Void_Type
3368 Conformance_Error ("\functions can only match functions!", New_Id);
3372 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3373 -- If this is a renaming as body, refine error message to indicate that
3374 -- the conflict is with the original declaration. If the entity is not
3375 -- frozen, the conventions don't have to match, the one of the renamed
3376 -- entity is inherited.
3378 if Ctype >= Subtype_Conformant then
3379 if Convention (Old_Id) /= Convention (New_Id) then
3381 if not Is_Frozen (New_Id) then
3384 elsif Present (Err_Loc)
3385 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3386 and then Present (Corresponding_Spec (Err_Loc))
3388 Error_Msg_Name_1 := Chars (New_Id);
3390 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3392 Conformance_Error ("\prior declaration for% has convention %!");
3395 Conformance_Error ("\calling conventions do not match!");
3400 elsif Is_Formal_Subprogram (Old_Id)
3401 or else Is_Formal_Subprogram (New_Id)
3403 Conformance_Error ("\formal subprograms not allowed!");
3408 -- Deal with parameters
3410 -- Note: we use the entity information, rather than going directly
3411 -- to the specification in the tree. This is not only simpler, but
3412 -- absolutely necessary for some cases of conformance tests between
3413 -- operators, where the declaration tree simply does not exist!
3415 Old_Formal := First_Formal (Old_Id);
3416 New_Formal := First_Formal (New_Id);
3418 while Present (Old_Formal) and then Present (New_Formal) loop
3419 if Is_Controlling_Formal (Old_Formal)
3420 and then Is_Controlling_Formal (New_Formal)
3421 and then Skip_Controlling_Formals
3423 goto Skip_Controlling_Formal;
3426 if Ctype = Fully_Conformant then
3428 -- Names must match. Error message is more accurate if we do
3429 -- this before checking that the types of the formals match.
3431 if Chars (Old_Formal) /= Chars (New_Formal) then
3432 Conformance_Error ("\name & does not match!", New_Formal);
3434 -- Set error posted flag on new formal as well to stop
3435 -- junk cascaded messages in some cases.
3437 Set_Error_Posted (New_Formal);
3442 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3443 -- case occurs whenever a subprogram is being renamed and one of its
3444 -- parameters imposes a null exclusion. For example:
3446 -- type T is null record;
3447 -- type Acc_T is access T;
3448 -- subtype Acc_T_Sub is Acc_T;
3450 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3451 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3454 Old_Formal_Base := Etype (Old_Formal);
3455 New_Formal_Base := Etype (New_Formal);
3458 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3459 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3462 Access_Types_Match := Ada_Version >= Ada_05
3464 -- Ensure that this rule is only applied when New_Id is a
3465 -- renaming of Old_Id.
3467 and then Nkind (Parent (Parent (New_Id))) =
3468 N_Subprogram_Renaming_Declaration
3469 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3470 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3471 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3473 -- Now handle the allowed access-type case
3475 and then Is_Access_Type (Old_Formal_Base)
3476 and then Is_Access_Type (New_Formal_Base)
3478 -- The type kinds must match. The only exception occurs with
3479 -- multiple generics of the form:
3482 -- type F is private; type A is private;
3483 -- type F_Ptr is access F; type A_Ptr is access A;
3484 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3485 -- package F_Pack is ... package A_Pack is
3486 -- package F_Inst is
3487 -- new F_Pack (A, A_Ptr, A_P);
3489 -- When checking for conformance between the parameters of A_P
3490 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3491 -- because the compiler has transformed A_Ptr into a subtype of
3492 -- F_Ptr. We catch this case in the code below.
3494 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3496 (Is_Generic_Type (Old_Formal_Base)
3497 and then Is_Generic_Type (New_Formal_Base)
3498 and then Is_Internal (New_Formal_Base)
3499 and then Etype (Etype (New_Formal_Base)) =
3501 and then Directly_Designated_Type (Old_Formal_Base) =
3502 Directly_Designated_Type (New_Formal_Base)
3503 and then ((Is_Itype (Old_Formal_Base)
3504 and then Can_Never_Be_Null (Old_Formal_Base))
3506 (Is_Itype (New_Formal_Base)
3507 and then Can_Never_Be_Null (New_Formal_Base)));
3509 -- Types must always match. In the visible part of an instance,
3510 -- usual overloading rules for dispatching operations apply, and
3511 -- we check base types (not the actual subtypes).
3513 if In_Instance_Visible_Part
3514 and then Is_Dispatching_Operation (New_Id)
3516 if not Conforming_Types
3517 (T1 => Base_Type (Etype (Old_Formal)),
3518 T2 => Base_Type (Etype (New_Formal)),
3520 Get_Inst => Get_Inst)
3521 and then not Access_Types_Match
3523 Conformance_Error ("\type of & does not match!", New_Formal);
3527 elsif not Conforming_Types
3528 (T1 => Old_Formal_Base,
3529 T2 => New_Formal_Base,
3531 Get_Inst => Get_Inst)
3532 and then not Access_Types_Match
3534 Conformance_Error ("\type of & does not match!", New_Formal);
3538 -- For mode conformance, mode must match
3540 if Ctype >= Mode_Conformant then
3541 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3542 Conformance_Error ("\mode of & does not match!", New_Formal);
3545 -- Part of mode conformance for access types is having the same
3546 -- constant modifier.
3548 elsif Access_Types_Match
3549 and then Is_Access_Constant (Old_Formal_Base) /=
3550 Is_Access_Constant (New_Formal_Base)
3553 ("\constant modifier does not match!", New_Formal);
3558 if Ctype >= Subtype_Conformant then
3560 -- Ada 2005 (AI-231): In case of anonymous access types check
3561 -- the null-exclusion and access-to-constant attributes must
3564 if Ada_Version >= Ada_05
3565 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3566 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3568 (Can_Never_Be_Null (Old_Formal) /=
3569 Can_Never_Be_Null (New_Formal)
3571 Is_Access_Constant (Etype (Old_Formal)) /=
3572 Is_Access_Constant (Etype (New_Formal)))
3574 -- It is allowed to omit the null-exclusion in case of stream
3575 -- attribute subprograms. We recognize stream subprograms
3576 -- through their TSS-generated suffix.
3579 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3581 if TSS_Name /= TSS_Stream_Read
3582 and then TSS_Name /= TSS_Stream_Write
3583 and then TSS_Name /= TSS_Stream_Input
3584 and then TSS_Name /= TSS_Stream_Output
3587 ("\type of & does not match!", New_Formal);
3594 -- Full conformance checks
3596 if Ctype = Fully_Conformant then
3598 -- We have checked already that names match
3600 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3602 -- Check default expressions for in parameters
3605 NewD : constant Boolean :=
3606 Present (Default_Value (New_Formal));
3607 OldD : constant Boolean :=
3608 Present (Default_Value (Old_Formal));
3610 if NewD or OldD then
3612 -- The old default value has been analyzed because the
3613 -- current full declaration will have frozen everything
3614 -- before. The new default value has not been analyzed,
3615 -- so analyze it now before we check for conformance.
3618 Push_Scope (New_Id);
3619 Preanalyze_Spec_Expression
3620 (Default_Value (New_Formal), Etype (New_Formal));
3624 if not (NewD and OldD)
3625 or else not Fully_Conformant_Expressions
3626 (Default_Value (Old_Formal),
3627 Default_Value (New_Formal))
3630 ("\default expression for & does not match!",
3639 -- A couple of special checks for Ada 83 mode. These checks are
3640 -- skipped if either entity is an operator in package Standard,
3641 -- or if either old or new instance is not from the source program.
3643 if Ada_Version = Ada_83
3644 and then Sloc (Old_Id) > Standard_Location
3645 and then Sloc (New_Id) > Standard_Location
3646 and then Comes_From_Source (Old_Id)
3647 and then Comes_From_Source (New_Id)
3650 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
3651 New_Param : constant Node_Id := Declaration_Node (New_Formal);
3654 -- Explicit IN must be present or absent in both cases. This
3655 -- test is required only in the full conformance case.
3657 if In_Present (Old_Param) /= In_Present (New_Param)
3658 and then Ctype = Fully_Conformant
3661 ("\(Ada 83) IN must appear in both declarations",
3666 -- Grouping (use of comma in param lists) must be the same
3667 -- This is where we catch a misconformance like:
3670 -- A : Integer; B : Integer
3672 -- which are represented identically in the tree except
3673 -- for the setting of the flags More_Ids and Prev_Ids.
3675 if More_Ids (Old_Param) /= More_Ids (New_Param)
3676 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
3679 ("\grouping of & does not match!", New_Formal);
3685 -- This label is required when skipping controlling formals
3687 <<Skip_Controlling_Formal>>
3689 Next_Formal (Old_Formal);
3690 Next_Formal (New_Formal);
3693 if Present (Old_Formal) then
3694 Conformance_Error ("\too few parameters!");
3697 elsif Present (New_Formal) then
3698 Conformance_Error ("\too many parameters!", New_Formal);
3701 end Check_Conformance;
3703 -----------------------
3704 -- Check_Conventions --
3705 -----------------------
3707 procedure Check_Conventions (Typ : Entity_Id) is
3708 Ifaces_List : Elist_Id;
3710 procedure Check_Convention (Op : Entity_Id);
3711 -- Verify that the convention of inherited dispatching operation Op is
3712 -- consistent among all subprograms it overrides. In order to minimize
3713 -- the search, Search_From is utilized to designate a specific point in
3714 -- the list rather than iterating over the whole list once more.
3716 ----------------------
3717 -- Check_Convention --
3718 ----------------------
3720 procedure Check_Convention (Op : Entity_Id) is
3721 Iface_Elmt : Elmt_Id;
3722 Iface_Prim_Elmt : Elmt_Id;
3723 Iface_Prim : Entity_Id;
3726 Iface_Elmt := First_Elmt (Ifaces_List);
3727 while Present (Iface_Elmt) loop
3729 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
3730 while Present (Iface_Prim_Elmt) loop
3731 Iface_Prim := Node (Iface_Prim_Elmt);
3733 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
3734 and then Convention (Iface_Prim) /= Convention (Op)
3737 ("inconsistent conventions in primitive operations", Typ);
3739 Error_Msg_Name_1 := Chars (Op);
3740 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
3741 Error_Msg_Sloc := Sloc (Op);
3743 if Comes_From_Source (Op) then
3744 if not Is_Overriding_Operation (Op) then
3745 Error_Msg_N ("\\primitive % defined #", Typ);
3747 Error_Msg_N ("\\overriding operation % with " &
3748 "convention % defined #", Typ);
3751 else pragma Assert (Present (Alias (Op)));
3752 Error_Msg_Sloc := Sloc (Alias (Op));
3753 Error_Msg_N ("\\inherited operation % with " &
3754 "convention % defined #", Typ);
3757 Error_Msg_Name_1 := Chars (Op);
3759 Get_Convention_Name (Convention (Iface_Prim));
3760 Error_Msg_Sloc := Sloc (Iface_Prim);
3761 Error_Msg_N ("\\overridden operation % with " &
3762 "convention % defined #", Typ);
3764 -- Avoid cascading errors
3769 Next_Elmt (Iface_Prim_Elmt);
3772 Next_Elmt (Iface_Elmt);
3774 end Check_Convention;
3778 Prim_Op : Entity_Id;
3779 Prim_Op_Elmt : Elmt_Id;
3781 -- Start of processing for Check_Conventions
3784 if not Has_Interfaces (Typ) then
3788 Collect_Interfaces (Typ, Ifaces_List);
3790 -- The algorithm checks every overriding dispatching operation against
3791 -- all the corresponding overridden dispatching operations, detecting
3792 -- differences in conventions.
3794 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
3795 while Present (Prim_Op_Elmt) loop
3796 Prim_Op := Node (Prim_Op_Elmt);
3798 -- A small optimization: skip the predefined dispatching operations
3799 -- since they always have the same convention.
3801 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
3802 Check_Convention (Prim_Op);
3805 Next_Elmt (Prim_Op_Elmt);
3807 end Check_Conventions;
3809 ------------------------------
3810 -- Check_Delayed_Subprogram --
3811 ------------------------------
3813 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
3816 procedure Possible_Freeze (T : Entity_Id);
3817 -- T is the type of either a formal parameter or of the return type.
3818 -- If T is not yet frozen and needs a delayed freeze, then the
3819 -- subprogram itself must be delayed.
3821 ---------------------
3822 -- Possible_Freeze --
3823 ---------------------
3825 procedure Possible_Freeze (T : Entity_Id) is
3827 if Has_Delayed_Freeze (T)
3828 and then not Is_Frozen (T)
3830 Set_Has_Delayed_Freeze (Designator);
3832 elsif Is_Access_Type (T)
3833 and then Has_Delayed_Freeze (Designated_Type (T))
3834 and then not Is_Frozen (Designated_Type (T))
3836 Set_Has_Delayed_Freeze (Designator);
3838 end Possible_Freeze;
3840 -- Start of processing for Check_Delayed_Subprogram
3843 -- Never need to freeze abstract subprogram
3845 if Ekind (Designator) /= E_Subprogram_Type
3846 and then Is_Abstract_Subprogram (Designator)
3850 -- Need delayed freeze if return type itself needs a delayed
3851 -- freeze and is not yet frozen.
3853 Possible_Freeze (Etype (Designator));
3854 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
3856 -- Need delayed freeze if any of the formal types themselves need
3857 -- a delayed freeze and are not yet frozen.
3859 F := First_Formal (Designator);
3860 while Present (F) loop
3861 Possible_Freeze (Etype (F));
3862 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
3867 -- Mark functions that return by reference. Note that it cannot be
3868 -- done for delayed_freeze subprograms because the underlying
3869 -- returned type may not be known yet (for private types)
3871 if not Has_Delayed_Freeze (Designator)
3872 and then Expander_Active
3875 Typ : constant Entity_Id := Etype (Designator);
3876 Utyp : constant Entity_Id := Underlying_Type (Typ);
3879 if Is_Inherently_Limited_Type (Typ) then
3880 Set_Returns_By_Ref (Designator);
3882 elsif Present (Utyp) and then CW_Or_Controlled_Type (Utyp) then
3883 Set_Returns_By_Ref (Designator);
3887 end Check_Delayed_Subprogram;
3889 ------------------------------------
3890 -- Check_Discriminant_Conformance --
3891 ------------------------------------
3893 procedure Check_Discriminant_Conformance
3898 Old_Discr : Entity_Id := First_Discriminant (Prev);
3899 New_Discr : Node_Id := First (Discriminant_Specifications (N));
3900 New_Discr_Id : Entity_Id;
3901 New_Discr_Type : Entity_Id;
3903 procedure Conformance_Error (Msg : String; N : Node_Id);
3904 -- Post error message for conformance error on given node. Two messages
3905 -- are output. The first points to the previous declaration with a
3906 -- general "no conformance" message. The second is the detailed reason,
3907 -- supplied as Msg. The parameter N provide information for a possible
3908 -- & insertion in the message.
3910 -----------------------
3911 -- Conformance_Error --
3912 -----------------------
3914 procedure Conformance_Error (Msg : String; N : Node_Id) is
3916 Error_Msg_Sloc := Sloc (Prev_Loc);
3917 Error_Msg_N ("not fully conformant with declaration#!", N);
3918 Error_Msg_NE (Msg, N, N);
3919 end Conformance_Error;
3921 -- Start of processing for Check_Discriminant_Conformance
3924 while Present (Old_Discr) and then Present (New_Discr) loop
3926 New_Discr_Id := Defining_Identifier (New_Discr);
3928 -- The subtype mark of the discriminant on the full type has not
3929 -- been analyzed so we do it here. For an access discriminant a new
3932 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
3934 Access_Definition (N, Discriminant_Type (New_Discr));
3937 Analyze (Discriminant_Type (New_Discr));
3938 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
3941 if not Conforming_Types
3942 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
3944 Conformance_Error ("type of & does not match!", New_Discr_Id);
3947 -- Treat the new discriminant as an occurrence of the old one,
3948 -- for navigation purposes, and fill in some semantic
3949 -- information, for completeness.
3951 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
3952 Set_Etype (New_Discr_Id, Etype (Old_Discr));
3953 Set_Scope (New_Discr_Id, Scope (Old_Discr));
3958 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
3959 Conformance_Error ("name & does not match!", New_Discr_Id);
3963 -- Default expressions must match
3966 NewD : constant Boolean :=
3967 Present (Expression (New_Discr));
3968 OldD : constant Boolean :=
3969 Present (Expression (Parent (Old_Discr)));
3972 if NewD or OldD then
3974 -- The old default value has been analyzed and expanded,
3975 -- because the current full declaration will have frozen
3976 -- everything before. The new default values have not been
3977 -- expanded, so expand now to check conformance.
3980 Preanalyze_Spec_Expression
3981 (Expression (New_Discr), New_Discr_Type);
3984 if not (NewD and OldD)
3985 or else not Fully_Conformant_Expressions
3986 (Expression (Parent (Old_Discr)),
3987 Expression (New_Discr))
3991 ("default expression for & does not match!",
3998 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4000 if Ada_Version = Ada_83 then
4002 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4005 -- Grouping (use of comma in param lists) must be the same
4006 -- This is where we catch a misconformance like:
4009 -- A : Integer; B : Integer
4011 -- which are represented identically in the tree except
4012 -- for the setting of the flags More_Ids and Prev_Ids.
4014 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4015 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4018 ("grouping of & does not match!", New_Discr_Id);
4024 Next_Discriminant (Old_Discr);
4028 if Present (Old_Discr) then
4029 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4032 elsif Present (New_Discr) then
4034 ("too many discriminants!", Defining_Identifier (New_Discr));
4037 end Check_Discriminant_Conformance;
4039 ----------------------------
4040 -- Check_Fully_Conformant --
4041 ----------------------------
4043 procedure Check_Fully_Conformant
4044 (New_Id : Entity_Id;
4046 Err_Loc : Node_Id := Empty)
4049 pragma Warnings (Off, Result);
4052 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4053 end Check_Fully_Conformant;
4055 ---------------------------
4056 -- Check_Mode_Conformant --
4057 ---------------------------
4059 procedure Check_Mode_Conformant
4060 (New_Id : Entity_Id;
4062 Err_Loc : Node_Id := Empty;
4063 Get_Inst : Boolean := False)
4066 pragma Warnings (Off, Result);
4069 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4070 end Check_Mode_Conformant;
4072 --------------------------------
4073 -- Check_Overriding_Indicator --
4074 --------------------------------
4076 procedure Check_Overriding_Indicator
4078 Overridden_Subp : Entity_Id;
4079 Is_Primitive : Boolean)
4085 -- No overriding indicator for literals
4087 if Ekind (Subp) = E_Enumeration_Literal then
4090 elsif Ekind (Subp) = E_Entry then
4091 Decl := Parent (Subp);
4094 Decl := Unit_Declaration_Node (Subp);
4097 if Nkind_In (Decl, N_Subprogram_Body,
4098 N_Subprogram_Body_Stub,
4099 N_Subprogram_Declaration,
4100 N_Abstract_Subprogram_Declaration,
4101 N_Subprogram_Renaming_Declaration)
4103 Spec := Specification (Decl);
4105 elsif Nkind (Decl) = N_Entry_Declaration then
4112 if Present (Overridden_Subp) then
4113 if Must_Not_Override (Spec) then
4114 Error_Msg_Sloc := Sloc (Overridden_Subp);
4116 if Ekind (Subp) = E_Entry then
4118 ("entry & overrides inherited operation #", Spec, Subp);
4121 ("subprogram & overrides inherited operation #", Spec, Subp);
4124 elsif Is_Subprogram (Subp) then
4125 Set_Is_Overriding_Operation (Subp);
4128 -- If Subp is an operator, it may override a predefined operation.
4129 -- In that case overridden_subp is empty because of our implicit
4130 -- representation for predefined operators. We have to check whether the
4131 -- signature of Subp matches that of a predefined operator. Note that
4132 -- first argument provides the name of the operator, and the second
4133 -- argument the signature that may match that of a standard operation.
4134 -- If the indicator is overriding, then the operator must match a
4135 -- predefined signature, because we know already that there is no
4136 -- explicit overridden operation.
4138 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4140 if Must_Not_Override (Spec) then
4141 if not Is_Primitive then
4143 ("overriding indicator only allowed "
4144 & "if subprogram is primitive", Subp);
4146 elsif Operator_Matches_Spec (Subp, Subp) then
4148 ("subprogram & overrides predefined operator ", Spec, Subp);
4151 elsif Is_Overriding_Operation (Subp) then
4154 elsif Must_Override (Spec) then
4155 if not Operator_Matches_Spec (Subp, Subp) then
4156 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4159 Set_Is_Overriding_Operation (Subp);
4163 elsif Must_Override (Spec) then
4164 if Ekind (Subp) = E_Entry then
4165 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4167 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4170 -- If the operation is marked "not overriding" and it's not primitive
4171 -- then an error is issued, unless this is an operation of a task or
4172 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4173 -- has been specified have already been checked above.
4175 elsif Must_Not_Override (Spec)
4176 and then not Is_Primitive
4177 and then Ekind (Subp) /= E_Entry
4178 and then Ekind (Scope (Subp)) /= E_Protected_Type
4181 ("overriding indicator only allowed if subprogram is primitive",
4185 end Check_Overriding_Indicator;
4191 -- Note: this procedure needs to know far too much about how the expander
4192 -- messes with exceptions. The use of the flag Exception_Junk and the
4193 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4194 -- works, but is not very clean. It would be better if the expansion
4195 -- routines would leave Original_Node working nicely, and we could use
4196 -- Original_Node here to ignore all the peculiar expander messing ???
4198 procedure Check_Returns
4202 Proc : Entity_Id := Empty)
4206 procedure Check_Statement_Sequence (L : List_Id);
4207 -- Internal recursive procedure to check a list of statements for proper
4208 -- termination by a return statement (or a transfer of control or a
4209 -- compound statement that is itself internally properly terminated).
4211 ------------------------------
4212 -- Check_Statement_Sequence --
4213 ------------------------------
4215 procedure Check_Statement_Sequence (L : List_Id) is
4220 Raise_Exception_Call : Boolean;
4221 -- Set True if statement sequence terminated by Raise_Exception call
4222 -- or a Reraise_Occurrence call.
4225 Raise_Exception_Call := False;
4227 -- Get last real statement
4229 Last_Stm := Last (L);
4231 -- Deal with digging out exception handler statement sequences that
4232 -- have been transformed by the local raise to goto optimization.
4233 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4234 -- optimization has occurred, we are looking at something like:
4237 -- original stmts in block
4241 -- goto L1; | omitted if No_Exception_Propagation
4246 -- goto L3; -- skip handler when exception not raised
4248 -- <<L1>> -- target label for local exception
4262 -- and what we have to do is to dig out the estmts1 and estmts2
4263 -- sequences (which were the original sequences of statements in
4264 -- the exception handlers) and check them.
4266 if Nkind (Last_Stm) = N_Label
4267 and then Exception_Junk (Last_Stm)
4273 exit when Nkind (Stm) /= N_Block_Statement;
4274 exit when not Exception_Junk (Stm);
4277 exit when Nkind (Stm) /= N_Label;
4278 exit when not Exception_Junk (Stm);
4279 Check_Statement_Sequence
4280 (Statements (Handled_Statement_Sequence (Next (Stm))));
4285 exit when Nkind (Stm) /= N_Goto_Statement;
4286 exit when not Exception_Junk (Stm);
4290 -- Don't count pragmas
4292 while Nkind (Last_Stm) = N_Pragma
4294 -- Don't count call to SS_Release (can happen after Raise_Exception)
4297 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4299 Nkind (Name (Last_Stm)) = N_Identifier
4301 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4303 -- Don't count exception junk
4306 (Nkind_In (Last_Stm, N_Goto_Statement,
4308 N_Object_Declaration)
4309 and then Exception_Junk (Last_Stm))
4310 or else Nkind (Last_Stm) in N_Push_xxx_Label
4311 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4316 -- Here we have the "real" last statement
4318 Kind := Nkind (Last_Stm);
4320 -- Transfer of control, OK. Note that in the No_Return procedure
4321 -- case, we already diagnosed any explicit return statements, so
4322 -- we can treat them as OK in this context.
4324 if Is_Transfer (Last_Stm) then
4327 -- Check cases of explicit non-indirect procedure calls
4329 elsif Kind = N_Procedure_Call_Statement
4330 and then Is_Entity_Name (Name (Last_Stm))
4332 -- Check call to Raise_Exception procedure which is treated
4333 -- specially, as is a call to Reraise_Occurrence.
4335 -- We suppress the warning in these cases since it is likely that
4336 -- the programmer really does not expect to deal with the case
4337 -- of Null_Occurrence, and thus would find a warning about a
4338 -- missing return curious, and raising Program_Error does not
4339 -- seem such a bad behavior if this does occur.
4341 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4342 -- behavior will be to raise Constraint_Error (see AI-329).
4344 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4346 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4348 Raise_Exception_Call := True;
4350 -- For Raise_Exception call, test first argument, if it is
4351 -- an attribute reference for a 'Identity call, then we know
4352 -- that the call cannot possibly return.
4355 Arg : constant Node_Id :=
4356 Original_Node (First_Actual (Last_Stm));
4358 if Nkind (Arg) = N_Attribute_Reference
4359 and then Attribute_Name (Arg) = Name_Identity
4366 -- If statement, need to look inside if there is an else and check
4367 -- each constituent statement sequence for proper termination.
4369 elsif Kind = N_If_Statement
4370 and then Present (Else_Statements (Last_Stm))
4372 Check_Statement_Sequence (Then_Statements (Last_Stm));
4373 Check_Statement_Sequence (Else_Statements (Last_Stm));
4375 if Present (Elsif_Parts (Last_Stm)) then
4377 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4380 while Present (Elsif_Part) loop
4381 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4389 -- Case statement, check each case for proper termination
4391 elsif Kind = N_Case_Statement then
4395 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4396 while Present (Case_Alt) loop
4397 Check_Statement_Sequence (Statements (Case_Alt));
4398 Next_Non_Pragma (Case_Alt);
4404 -- Block statement, check its handled sequence of statements
4406 elsif Kind = N_Block_Statement then
4412 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4421 -- Loop statement. If there is an iteration scheme, we can definitely
4422 -- fall out of the loop. Similarly if there is an exit statement, we
4423 -- can fall out. In either case we need a following return.
4425 elsif Kind = N_Loop_Statement then
4426 if Present (Iteration_Scheme (Last_Stm))
4427 or else Has_Exit (Entity (Identifier (Last_Stm)))
4431 -- A loop with no exit statement or iteration scheme is either
4432 -- an infinite loop, or it has some other exit (raise/return).
4433 -- In either case, no warning is required.
4439 -- Timed entry call, check entry call and delay alternatives
4441 -- Note: in expanded code, the timed entry call has been converted
4442 -- to a set of expanded statements on which the check will work
4443 -- correctly in any case.
4445 elsif Kind = N_Timed_Entry_Call then
4447 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4448 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4451 -- If statement sequence of entry call alternative is missing,
4452 -- then we can definitely fall through, and we post the error
4453 -- message on the entry call alternative itself.
4455 if No (Statements (ECA)) then
4458 -- If statement sequence of delay alternative is missing, then
4459 -- we can definitely fall through, and we post the error
4460 -- message on the delay alternative itself.
4462 -- Note: if both ECA and DCA are missing the return, then we
4463 -- post only one message, should be enough to fix the bugs.
4464 -- If not we will get a message next time on the DCA when the
4467 elsif No (Statements (DCA)) then
4470 -- Else check both statement sequences
4473 Check_Statement_Sequence (Statements (ECA));
4474 Check_Statement_Sequence (Statements (DCA));
4479 -- Conditional entry call, check entry call and else part
4481 -- Note: in expanded code, the conditional entry call has been
4482 -- converted to a set of expanded statements on which the check
4483 -- will work correctly in any case.
4485 elsif Kind = N_Conditional_Entry_Call then
4487 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4490 -- If statement sequence of entry call alternative is missing,
4491 -- then we can definitely fall through, and we post the error
4492 -- message on the entry call alternative itself.
4494 if No (Statements (ECA)) then
4497 -- Else check statement sequence and else part
4500 Check_Statement_Sequence (Statements (ECA));
4501 Check_Statement_Sequence (Else_Statements (Last_Stm));
4507 -- If we fall through, issue appropriate message
4510 if not Raise_Exception_Call then
4512 ("?RETURN statement missing following this statement!",
4515 ("\?Program_Error may be raised at run time!",
4519 -- Note: we set Err even though we have not issued a warning
4520 -- because we still have a case of a missing return. This is
4521 -- an extremely marginal case, probably will never be noticed
4522 -- but we might as well get it right.
4526 -- Otherwise we have the case of a procedure marked No_Return
4529 if not Raise_Exception_Call then
4531 ("?implied return after this statement " &
4532 "will raise Program_Error",
4535 ("\?procedure & is marked as No_Return!",
4540 RE : constant Node_Id :=
4541 Make_Raise_Program_Error (Sloc (Last_Stm),
4542 Reason => PE_Implicit_Return);
4544 Insert_After (Last_Stm, RE);
4548 end Check_Statement_Sequence;
4550 -- Start of processing for Check_Returns
4554 Check_Statement_Sequence (Statements (HSS));
4556 if Present (Exception_Handlers (HSS)) then
4557 Handler := First_Non_Pragma (Exception_Handlers (HSS));
4558 while Present (Handler) loop
4559 Check_Statement_Sequence (Statements (Handler));
4560 Next_Non_Pragma (Handler);
4565 ----------------------------
4566 -- Check_Subprogram_Order --
4567 ----------------------------
4569 procedure Check_Subprogram_Order (N : Node_Id) is
4571 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
4572 -- This is used to check if S1 > S2 in the sense required by this
4573 -- test, for example nameab < namec, but name2 < name10.
4575 -----------------------------
4576 -- Subprogram_Name_Greater --
4577 -----------------------------
4579 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
4584 -- Remove trailing numeric parts
4587 while S1 (L1) in '0' .. '9' loop
4592 while S2 (L2) in '0' .. '9' loop
4596 -- If non-numeric parts non-equal, that's decisive
4598 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
4601 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
4604 -- If non-numeric parts equal, compare suffixed numeric parts. Note
4605 -- that a missing suffix is treated as numeric zero in this test.
4609 while L1 < S1'Last loop
4611 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
4615 while L2 < S2'Last loop
4617 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
4622 end Subprogram_Name_Greater;
4624 -- Start of processing for Check_Subprogram_Order
4627 -- Check body in alpha order if this is option
4630 and then Style_Check_Order_Subprograms
4631 and then Nkind (N) = N_Subprogram_Body
4632 and then Comes_From_Source (N)
4633 and then In_Extended_Main_Source_Unit (N)
4637 renames Scope_Stack.Table
4638 (Scope_Stack.Last).Last_Subprogram_Name;
4640 Body_Id : constant Entity_Id :=
4641 Defining_Entity (Specification (N));
4644 Get_Decoded_Name_String (Chars (Body_Id));
4647 if Subprogram_Name_Greater
4648 (LSN.all, Name_Buffer (1 .. Name_Len))
4650 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
4656 LSN := new String'(Name_Buffer (1 .. Name_Len));
4659 end Check_Subprogram_Order;
4661 ------------------------------
4662 -- Check_Subtype_Conformant --
4663 ------------------------------
4665 procedure Check_Subtype_Conformant
4666 (New_Id : Entity_Id;
4668 Err_Loc : Node_Id := Empty;
4669 Skip_Controlling_Formals : Boolean := False)
4672 pragma Warnings (Off, Result);
4675 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
4676 Skip_Controlling_Formals => Skip_Controlling_Formals);
4677 end Check_Subtype_Conformant;
4679 ---------------------------
4680 -- Check_Type_Conformant --
4681 ---------------------------
4683 procedure Check_Type_Conformant
4684 (New_Id : Entity_Id;
4686 Err_Loc : Node_Id := Empty)
4689 pragma Warnings (Off, Result);
4692 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
4693 end Check_Type_Conformant;
4695 ----------------------
4696 -- Conforming_Types --
4697 ----------------------
4699 function Conforming_Types
4702 Ctype : Conformance_Type;
4703 Get_Inst : Boolean := False) return Boolean
4705 Type_1 : Entity_Id := T1;
4706 Type_2 : Entity_Id := T2;
4707 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
4709 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
4710 -- If neither T1 nor T2 are generic actual types, or if they are in
4711 -- different scopes (e.g. parent and child instances), then verify that
4712 -- the base types are equal. Otherwise T1 and T2 must be on the same
4713 -- subtype chain. The whole purpose of this procedure is to prevent
4714 -- spurious ambiguities in an instantiation that may arise if two
4715 -- distinct generic types are instantiated with the same actual.
4717 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
4718 -- An access parameter can designate an incomplete type. If the
4719 -- incomplete type is the limited view of a type from a limited_
4720 -- with_clause, check whether the non-limited view is available. If
4721 -- it is a (non-limited) incomplete type, get the full view.
4723 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
4724 -- Returns True if and only if either T1 denotes a limited view of T2
4725 -- or T2 denotes a limited view of T1. This can arise when the limited
4726 -- with view of a type is used in a subprogram declaration and the
4727 -- subprogram body is in the scope of a regular with clause for the
4728 -- same unit. In such a case, the two type entities can be considered
4729 -- identical for purposes of conformance checking.
4731 ----------------------
4732 -- Base_Types_Match --
4733 ----------------------
4735 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
4740 elsif Base_Type (T1) = Base_Type (T2) then
4742 -- The following is too permissive. A more precise test should
4743 -- check that the generic actual is an ancestor subtype of the
4746 return not Is_Generic_Actual_Type (T1)
4747 or else not Is_Generic_Actual_Type (T2)
4748 or else Scope (T1) /= Scope (T2);
4753 end Base_Types_Match;
4755 --------------------------
4756 -- Find_Designated_Type --
4757 --------------------------
4759 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
4763 Desig := Directly_Designated_Type (T);
4765 if Ekind (Desig) = E_Incomplete_Type then
4767 -- If regular incomplete type, get full view if available
4769 if Present (Full_View (Desig)) then
4770 Desig := Full_View (Desig);
4772 -- If limited view of a type, get non-limited view if available,
4773 -- and check again for a regular incomplete type.
4775 elsif Present (Non_Limited_View (Desig)) then
4776 Desig := Get_Full_View (Non_Limited_View (Desig));
4781 end Find_Designated_Type;
4783 -------------------------------
4784 -- Matches_Limited_With_View --
4785 -------------------------------
4787 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
4789 -- In some cases a type imported through a limited_with clause, and
4790 -- its nonlimited view are both visible, for example in an anonymous
4791 -- access-to-class-wide type in a formal. Both entities designate the
4794 if From_With_Type (T1)
4795 and then T2 = Available_View (T1)
4799 elsif From_With_Type (T2)
4800 and then T1 = Available_View (T2)
4807 end Matches_Limited_With_View;
4809 -- Start of processing for Conforming_Types
4812 -- The context is an instance association for a formal
4813 -- access-to-subprogram type; the formal parameter types require
4814 -- mapping because they may denote other formal parameters of the
4818 Type_1 := Get_Instance_Of (T1);
4819 Type_2 := Get_Instance_Of (T2);
4822 -- If one of the types is a view of the other introduced by a limited
4823 -- with clause, treat these as conforming for all purposes.
4825 if Matches_Limited_With_View (T1, T2) then
4828 elsif Base_Types_Match (Type_1, Type_2) then
4829 return Ctype <= Mode_Conformant
4830 or else Subtypes_Statically_Match (Type_1, Type_2);
4832 elsif Is_Incomplete_Or_Private_Type (Type_1)
4833 and then Present (Full_View (Type_1))
4834 and then Base_Types_Match (Full_View (Type_1), Type_2)
4836 return Ctype <= Mode_Conformant
4837 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
4839 elsif Ekind (Type_2) = E_Incomplete_Type
4840 and then Present (Full_View (Type_2))
4841 and then Base_Types_Match (Type_1, Full_View (Type_2))
4843 return Ctype <= Mode_Conformant
4844 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4846 elsif Is_Private_Type (Type_2)
4847 and then In_Instance
4848 and then Present (Full_View (Type_2))
4849 and then Base_Types_Match (Type_1, Full_View (Type_2))
4851 return Ctype <= Mode_Conformant
4852 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4855 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
4856 -- treated recursively because they carry a signature.
4858 Are_Anonymous_Access_To_Subprogram_Types :=
4859 Ekind (Type_1) = Ekind (Type_2)
4861 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
4863 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
4865 -- Test anonymous access type case. For this case, static subtype
4866 -- matching is required for mode conformance (RM 6.3.1(15)). We check
4867 -- the base types because we may have built internal subtype entities
4868 -- to handle null-excluding types (see Process_Formals).
4870 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
4872 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
4873 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
4876 Desig_1 : Entity_Id;
4877 Desig_2 : Entity_Id;
4880 -- In Ada2005, access constant indicators must match for
4881 -- subtype conformance.
4883 if Ada_Version >= Ada_05
4884 and then Ctype >= Subtype_Conformant
4886 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
4891 Desig_1 := Find_Designated_Type (Type_1);
4893 Desig_2 := Find_Designated_Type (Type_2);
4895 -- If the context is an instance association for a formal
4896 -- access-to-subprogram type; formal access parameter designated
4897 -- types require mapping because they may denote other formal
4898 -- parameters of the generic unit.
4901 Desig_1 := Get_Instance_Of (Desig_1);
4902 Desig_2 := Get_Instance_Of (Desig_2);
4905 -- It is possible for a Class_Wide_Type to be introduced for an
4906 -- incomplete type, in which case there is a separate class_ wide
4907 -- type for the full view. The types conform if their Etypes
4908 -- conform, i.e. one may be the full view of the other. This can
4909 -- only happen in the context of an access parameter, other uses
4910 -- of an incomplete Class_Wide_Type are illegal.
4912 if Is_Class_Wide_Type (Desig_1)
4913 and then Is_Class_Wide_Type (Desig_2)
4917 (Etype (Base_Type (Desig_1)),
4918 Etype (Base_Type (Desig_2)), Ctype);
4920 elsif Are_Anonymous_Access_To_Subprogram_Types then
4921 if Ada_Version < Ada_05 then
4922 return Ctype = Type_Conformant
4924 Subtypes_Statically_Match (Desig_1, Desig_2);
4926 -- We must check the conformance of the signatures themselves
4930 Conformant : Boolean;
4933 (Desig_1, Desig_2, Ctype, False, Conformant);
4939 return Base_Type (Desig_1) = Base_Type (Desig_2)
4940 and then (Ctype = Type_Conformant
4942 Subtypes_Statically_Match (Desig_1, Desig_2));
4946 -- Otherwise definitely no match
4949 if ((Ekind (Type_1) = E_Anonymous_Access_Type
4950 and then Is_Access_Type (Type_2))
4951 or else (Ekind (Type_2) = E_Anonymous_Access_Type
4952 and then Is_Access_Type (Type_1)))
4955 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
4957 May_Hide_Profile := True;
4962 end Conforming_Types;
4964 --------------------------
4965 -- Create_Extra_Formals --
4966 --------------------------
4968 procedure Create_Extra_Formals (E : Entity_Id) is
4970 First_Extra : Entity_Id := Empty;
4971 Last_Extra : Entity_Id;
4972 Formal_Type : Entity_Id;
4973 P_Formal : Entity_Id := Empty;
4975 function Add_Extra_Formal
4976 (Assoc_Entity : Entity_Id;
4979 Suffix : String) return Entity_Id;
4980 -- Add an extra formal to the current list of formals and extra formals.
4981 -- The extra formal is added to the end of the list of extra formals,
4982 -- and also returned as the result. These formals are always of mode IN.
4983 -- The new formal has the type Typ, is declared in Scope, and its name
4984 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
4986 ----------------------
4987 -- Add_Extra_Formal --
4988 ----------------------
4990 function Add_Extra_Formal
4991 (Assoc_Entity : Entity_Id;
4994 Suffix : String) return Entity_Id
4996 EF : constant Entity_Id :=
4997 Make_Defining_Identifier (Sloc (Assoc_Entity),
4998 Chars => New_External_Name (Chars (Assoc_Entity),
5002 -- A little optimization. Never generate an extra formal for the
5003 -- _init operand of an initialization procedure, since it could
5006 if Chars (Formal) = Name_uInit then
5010 Set_Ekind (EF, E_In_Parameter);
5011 Set_Actual_Subtype (EF, Typ);
5012 Set_Etype (EF, Typ);
5013 Set_Scope (EF, Scope);
5014 Set_Mechanism (EF, Default_Mechanism);
5015 Set_Formal_Validity (EF);
5017 if No (First_Extra) then
5019 Set_Extra_Formals (Scope, First_Extra);
5022 if Present (Last_Extra) then
5023 Set_Extra_Formal (Last_Extra, EF);
5029 end Add_Extra_Formal;
5031 -- Start of processing for Create_Extra_Formals
5034 -- We never generate extra formals if expansion is not active
5035 -- because we don't need them unless we are generating code.
5037 if not Expander_Active then
5041 -- If this is a derived subprogram then the subtypes of the parent
5042 -- subprogram's formal parameters will be used to to determine the need
5043 -- for extra formals.
5045 if Is_Overloadable (E) and then Present (Alias (E)) then
5046 P_Formal := First_Formal (Alias (E));
5049 Last_Extra := Empty;
5050 Formal := First_Formal (E);
5051 while Present (Formal) loop
5052 Last_Extra := Formal;
5053 Next_Formal (Formal);
5056 -- If Extra_formals were already created, don't do it again. This
5057 -- situation may arise for subprogram types created as part of
5058 -- dispatching calls (see Expand_Dispatching_Call)
5060 if Present (Last_Extra) and then
5061 Present (Extra_Formal (Last_Extra))
5066 -- If the subprogram is a predefined dispatching subprogram then don't
5067 -- generate any extra constrained or accessibility level formals. In
5068 -- general we suppress these for internal subprograms (by not calling
5069 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5070 -- generated stream attributes do get passed through because extra
5071 -- build-in-place formals are needed in some cases (limited 'Input).
5073 if Is_Predefined_Dispatching_Operation (E) then
5074 goto Test_For_BIP_Extras;
5077 Formal := First_Formal (E);
5078 while Present (Formal) loop
5080 -- Create extra formal for supporting the attribute 'Constrained.
5081 -- The case of a private type view without discriminants also
5082 -- requires the extra formal if the underlying type has defaulted
5085 if Ekind (Formal) /= E_In_Parameter then
5086 if Present (P_Formal) then
5087 Formal_Type := Etype (P_Formal);
5089 Formal_Type := Etype (Formal);
5092 -- Do not produce extra formals for Unchecked_Union parameters.
5093 -- Jump directly to the end of the loop.
5095 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5096 goto Skip_Extra_Formal_Generation;
5099 if not Has_Discriminants (Formal_Type)
5100 and then Ekind (Formal_Type) in Private_Kind
5101 and then Present (Underlying_Type (Formal_Type))
5103 Formal_Type := Underlying_Type (Formal_Type);
5106 if Has_Discriminants (Formal_Type)
5107 and then not Is_Constrained (Formal_Type)
5108 and then not Is_Indefinite_Subtype (Formal_Type)
5110 Set_Extra_Constrained
5111 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "F"));
5115 -- Create extra formal for supporting accessibility checking. This
5116 -- is done for both anonymous access formals and formals of named
5117 -- access types that are marked as controlling formals. The latter
5118 -- case can occur when Expand_Dispatching_Call creates a subprogram
5119 -- type and substitutes the types of access-to-class-wide actuals
5120 -- for the anonymous access-to-specific-type of controlling formals.
5121 -- Base_Type is applied because in cases where there is a null
5122 -- exclusion the formal may have an access subtype.
5124 -- This is suppressed if we specifically suppress accessibility
5125 -- checks at the package level for either the subprogram, or the
5126 -- package in which it resides. However, we do not suppress it
5127 -- simply if the scope has accessibility checks suppressed, since
5128 -- this could cause trouble when clients are compiled with a
5129 -- different suppression setting. The explicit checks at the
5130 -- package level are safe from this point of view.
5132 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5133 or else (Is_Controlling_Formal (Formal)
5134 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5136 (Explicit_Suppress (E, Accessibility_Check)
5138 Explicit_Suppress (Scope (E), Accessibility_Check))
5141 or else Present (Extra_Accessibility (P_Formal)))
5143 -- Temporary kludge: for now we avoid creating the extra formal
5144 -- for access parameters of protected operations because of
5145 -- problem with the case of internal protected calls. ???
5147 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
5148 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
5150 Set_Extra_Accessibility
5151 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "F"));
5155 -- This label is required when skipping extra formal generation for
5156 -- Unchecked_Union parameters.
5158 <<Skip_Extra_Formal_Generation>>
5160 if Present (P_Formal) then
5161 Next_Formal (P_Formal);
5164 Next_Formal (Formal);
5167 <<Test_For_BIP_Extras>>
5169 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5170 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5172 if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
5174 Result_Subt : constant Entity_Id := Etype (E);
5176 Discard : Entity_Id;
5177 pragma Warnings (Off, Discard);
5180 -- In the case of functions with unconstrained result subtypes,
5181 -- add a 3-state formal indicating whether the return object is
5182 -- allocated by the caller (0), or should be allocated by the
5183 -- callee on the secondary stack (1) or in the global heap (2).
5184 -- For the moment we just use Natural for the type of this formal.
5185 -- Note that this formal isn't usually needed in the case where
5186 -- the result subtype is constrained, but it is needed when the
5187 -- function has a tagged result, because generally such functions
5188 -- can be called in a dispatching context and such calls must be
5189 -- handled like calls to a class-wide function.
5191 if not Is_Constrained (Underlying_Type (Result_Subt))
5192 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5196 (E, Standard_Natural,
5197 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5200 -- In the case of functions whose result type has controlled
5201 -- parts, we have an extra formal of type
5202 -- System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
5203 -- is, we are passing a pointer to a finalization list (which is
5204 -- itself a pointer). This extra formal is then passed along to
5205 -- Move_Final_List in case of successful completion of a return
5206 -- statement. We cannot pass an 'in out' parameter, because we
5207 -- need to update the finalization list during an abort-deferred
5208 -- region, rather than using copy-back after the function
5209 -- returns. This is true even if we are able to get away with
5210 -- having 'in out' parameters, which are normally illegal for
5211 -- functions. This formal is also needed when the function has
5212 -- a tagged result, because generally such functions can be called
5213 -- in a dispatching context and such calls must be handled like
5214 -- calls to class-wide functions.
5216 if Controlled_Type (Result_Subt)
5217 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5221 (E, RTE (RE_Finalizable_Ptr_Ptr),
5222 E, BIP_Formal_Suffix (BIP_Final_List));
5225 -- If the result type contains tasks, we have two extra formals:
5226 -- the master of the tasks to be created, and the caller's
5227 -- activation chain.
5229 if Has_Task (Result_Subt) then
5232 (E, RTE (RE_Master_Id),
5233 E, BIP_Formal_Suffix (BIP_Master));
5236 (E, RTE (RE_Activation_Chain_Access),
5237 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5240 -- All build-in-place functions get an extra formal that will be
5241 -- passed the address of the return object within the caller.
5244 Formal_Type : constant Entity_Id :=
5246 (E_Anonymous_Access_Type, E,
5247 Scope_Id => Scope (E));
5249 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5250 Set_Etype (Formal_Type, Formal_Type);
5251 Set_Depends_On_Private
5252 (Formal_Type, Has_Private_Component (Formal_Type));
5253 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5254 Set_Is_Access_Constant (Formal_Type, False);
5256 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5257 -- the designated type comes from the limited view (for
5258 -- back-end purposes).
5260 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5262 Layout_Type (Formal_Type);
5266 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5270 end Create_Extra_Formals;
5272 -----------------------------
5273 -- Enter_Overloaded_Entity --
5274 -----------------------------
5276 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5277 E : Entity_Id := Current_Entity_In_Scope (S);
5278 C_E : Entity_Id := Current_Entity (S);
5282 Set_Has_Homonym (E);
5283 Set_Has_Homonym (S);
5286 Set_Is_Immediately_Visible (S);
5287 Set_Scope (S, Current_Scope);
5289 -- Chain new entity if front of homonym in current scope, so that
5290 -- homonyms are contiguous.
5295 while Homonym (C_E) /= E loop
5296 C_E := Homonym (C_E);
5299 Set_Homonym (C_E, S);
5303 Set_Current_Entity (S);
5308 Append_Entity (S, Current_Scope);
5309 Set_Public_Status (S);
5311 if Debug_Flag_E then
5312 Write_Str ("New overloaded entity chain: ");
5313 Write_Name (Chars (S));
5316 while Present (E) loop
5317 Write_Str (" "); Write_Int (Int (E));
5324 -- Generate warning for hiding
5327 and then Comes_From_Source (S)
5328 and then In_Extended_Main_Source_Unit (S)
5335 -- Warn unless genuine overloading
5337 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5338 and then (Is_Immediately_Visible (E)
5340 Is_Potentially_Use_Visible (S))
5342 Error_Msg_Sloc := Sloc (E);
5343 Error_Msg_N ("declaration of & hides one#?", S);
5347 end Enter_Overloaded_Entity;
5349 -----------------------------
5350 -- Find_Corresponding_Spec --
5351 -----------------------------
5353 function Find_Corresponding_Spec
5355 Post_Error : Boolean := True) return Entity_Id
5357 Spec : constant Node_Id := Specification (N);
5358 Designator : constant Entity_Id := Defining_Entity (Spec);
5363 E := Current_Entity (Designator);
5364 while Present (E) loop
5366 -- We are looking for a matching spec. It must have the same scope,
5367 -- and the same name, and either be type conformant, or be the case
5368 -- of a library procedure spec and its body (which belong to one
5369 -- another regardless of whether they are type conformant or not).
5371 if Scope (E) = Current_Scope then
5372 if Current_Scope = Standard_Standard
5373 or else (Ekind (E) = Ekind (Designator)
5374 and then Type_Conformant (E, Designator))
5376 -- Within an instantiation, we know that spec and body are
5377 -- subtype conformant, because they were subtype conformant
5378 -- in the generic. We choose the subtype-conformant entity
5379 -- here as well, to resolve spurious ambiguities in the
5380 -- instance that were not present in the generic (i.e. when
5381 -- two different types are given the same actual). If we are
5382 -- looking for a spec to match a body, full conformance is
5386 Set_Convention (Designator, Convention (E));
5388 if Nkind (N) = N_Subprogram_Body
5389 and then Present (Homonym (E))
5390 and then not Fully_Conformant (E, Designator)
5394 elsif not Subtype_Conformant (E, Designator) then
5399 if not Has_Completion (E) then
5400 if Nkind (N) /= N_Subprogram_Body_Stub then
5401 Set_Corresponding_Spec (N, E);
5404 Set_Has_Completion (E);
5407 elsif Nkind (Parent (N)) = N_Subunit then
5409 -- If this is the proper body of a subunit, the completion
5410 -- flag is set when analyzing the stub.
5414 -- If E is an internal function with a controlling result
5415 -- that was created for an operation inherited by a null
5416 -- extension, it may be overridden by a body without a previous
5417 -- spec (one more reason why these should be shunned). In that
5418 -- case remove the generated body, because the current one is
5419 -- the explicit overriding.
5421 elsif Ekind (E) = E_Function
5422 and then Ada_Version >= Ada_05
5423 and then not Comes_From_Source (E)
5424 and then Has_Controlling_Result (E)
5425 and then Is_Null_Extension (Etype (E))
5426 and then Comes_From_Source (Spec)
5428 Set_Has_Completion (E, False);
5430 if Expander_Active then
5432 (Unit_Declaration_Node
5433 (Corresponding_Body (Unit_Declaration_Node (E))));
5436 -- If expansion is disabled, the wrapper function has not
5437 -- been generated, and this is the standard case of a late
5438 -- body overriding an inherited operation.
5444 -- If the body already exists, then this is an error unless
5445 -- the previous declaration is the implicit declaration of a
5446 -- derived subprogram, or this is a spurious overloading in an
5449 elsif No (Alias (E))
5450 and then not Is_Intrinsic_Subprogram (E)
5451 and then not In_Instance
5454 Error_Msg_Sloc := Sloc (E);
5455 if Is_Imported (E) then
5457 ("body not allowed for imported subprogram & declared#",
5460 Error_Msg_NE ("duplicate body for & declared#", N, E);
5464 -- Child units cannot be overloaded, so a conformance mismatch
5465 -- between body and a previous spec is an error.
5467 elsif Is_Child_Unit (E)
5469 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5471 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5476 ("body of child unit does not match previous declaration", N);
5484 -- On exit, we know that no previous declaration of subprogram exists
5487 end Find_Corresponding_Spec;
5489 ----------------------
5490 -- Fully_Conformant --
5491 ----------------------
5493 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5496 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5498 end Fully_Conformant;
5500 ----------------------------------
5501 -- Fully_Conformant_Expressions --
5502 ----------------------------------
5504 function Fully_Conformant_Expressions
5505 (Given_E1 : Node_Id;
5506 Given_E2 : Node_Id) return Boolean
5508 E1 : constant Node_Id := Original_Node (Given_E1);
5509 E2 : constant Node_Id := Original_Node (Given_E2);
5510 -- We always test conformance on original nodes, since it is possible
5511 -- for analysis and/or expansion to make things look as though they
5512 -- conform when they do not, e.g. by converting 1+2 into 3.
5514 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
5515 renames Fully_Conformant_Expressions;
5517 function FCL (L1, L2 : List_Id) return Boolean;
5518 -- Compare elements of two lists for conformance. Elements have to
5519 -- be conformant, and actuals inserted as default parameters do not
5520 -- match explicit actuals with the same value.
5522 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
5523 -- Compare an operator node with a function call
5529 function FCL (L1, L2 : List_Id) return Boolean is
5533 if L1 = No_List then
5539 if L2 = No_List then
5545 -- Compare two lists, skipping rewrite insertions (we want to
5546 -- compare the original trees, not the expanded versions!)
5549 if Is_Rewrite_Insertion (N1) then
5551 elsif Is_Rewrite_Insertion (N2) then
5557 elsif not FCE (N1, N2) then
5570 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
5571 Actuals : constant List_Id := Parameter_Associations (Call_Node);
5576 or else Entity (Op_Node) /= Entity (Name (Call_Node))
5581 Act := First (Actuals);
5583 if Nkind (Op_Node) in N_Binary_Op then
5585 if not FCE (Left_Opnd (Op_Node), Act) then
5592 return Present (Act)
5593 and then FCE (Right_Opnd (Op_Node), Act)
5594 and then No (Next (Act));
5598 -- Start of processing for Fully_Conformant_Expressions
5601 -- Non-conformant if paren count does not match. Note: if some idiot
5602 -- complains that we don't do this right for more than 3 levels of
5603 -- parentheses, they will be treated with the respect they deserve!
5605 if Paren_Count (E1) /= Paren_Count (E2) then
5608 -- If same entities are referenced, then they are conformant even if
5609 -- they have different forms (RM 8.3.1(19-20)).
5611 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
5612 if Present (Entity (E1)) then
5613 return Entity (E1) = Entity (E2)
5614 or else (Chars (Entity (E1)) = Chars (Entity (E2))
5615 and then Ekind (Entity (E1)) = E_Discriminant
5616 and then Ekind (Entity (E2)) = E_In_Parameter);
5618 elsif Nkind (E1) = N_Expanded_Name
5619 and then Nkind (E2) = N_Expanded_Name
5620 and then Nkind (Selector_Name (E1)) = N_Character_Literal
5621 and then Nkind (Selector_Name (E2)) = N_Character_Literal
5623 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
5626 -- Identifiers in component associations don't always have
5627 -- entities, but their names must conform.
5629 return Nkind (E1) = N_Identifier
5630 and then Nkind (E2) = N_Identifier
5631 and then Chars (E1) = Chars (E2);
5634 elsif Nkind (E1) = N_Character_Literal
5635 and then Nkind (E2) = N_Expanded_Name
5637 return Nkind (Selector_Name (E2)) = N_Character_Literal
5638 and then Chars (E1) = Chars (Selector_Name (E2));
5640 elsif Nkind (E2) = N_Character_Literal
5641 and then Nkind (E1) = N_Expanded_Name
5643 return Nkind (Selector_Name (E1)) = N_Character_Literal
5644 and then Chars (E2) = Chars (Selector_Name (E1));
5646 elsif Nkind (E1) in N_Op
5647 and then Nkind (E2) = N_Function_Call
5649 return FCO (E1, E2);
5651 elsif Nkind (E2) in N_Op
5652 and then Nkind (E1) = N_Function_Call
5654 return FCO (E2, E1);
5656 -- Otherwise we must have the same syntactic entity
5658 elsif Nkind (E1) /= Nkind (E2) then
5661 -- At this point, we specialize by node type
5668 FCL (Expressions (E1), Expressions (E2))
5669 and then FCL (Component_Associations (E1),
5670 Component_Associations (E2));
5673 if Nkind (Expression (E1)) = N_Qualified_Expression
5675 Nkind (Expression (E2)) = N_Qualified_Expression
5677 return FCE (Expression (E1), Expression (E2));
5679 -- Check that the subtype marks and any constraints
5684 Indic1 : constant Node_Id := Expression (E1);
5685 Indic2 : constant Node_Id := Expression (E2);
5690 if Nkind (Indic1) /= N_Subtype_Indication then
5692 Nkind (Indic2) /= N_Subtype_Indication
5693 and then Entity (Indic1) = Entity (Indic2);
5695 elsif Nkind (Indic2) /= N_Subtype_Indication then
5697 Nkind (Indic1) /= N_Subtype_Indication
5698 and then Entity (Indic1) = Entity (Indic2);
5701 if Entity (Subtype_Mark (Indic1)) /=
5702 Entity (Subtype_Mark (Indic2))
5707 Elt1 := First (Constraints (Constraint (Indic1)));
5708 Elt2 := First (Constraints (Constraint (Indic2)));
5710 while Present (Elt1) and then Present (Elt2) loop
5711 if not FCE (Elt1, Elt2) then
5724 when N_Attribute_Reference =>
5726 Attribute_Name (E1) = Attribute_Name (E2)
5727 and then FCL (Expressions (E1), Expressions (E2));
5731 Entity (E1) = Entity (E2)
5732 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
5733 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5735 when N_And_Then | N_Or_Else | N_Membership_Test =>
5737 FCE (Left_Opnd (E1), Left_Opnd (E2))
5739 FCE (Right_Opnd (E1), Right_Opnd (E2));
5741 when N_Character_Literal =>
5743 Char_Literal_Value (E1) = Char_Literal_Value (E2);
5745 when N_Component_Association =>
5747 FCL (Choices (E1), Choices (E2))
5748 and then FCE (Expression (E1), Expression (E2));
5750 when N_Conditional_Expression =>
5752 FCL (Expressions (E1), Expressions (E2));
5754 when N_Explicit_Dereference =>
5756 FCE (Prefix (E1), Prefix (E2));
5758 when N_Extension_Aggregate =>
5760 FCL (Expressions (E1), Expressions (E2))
5761 and then Null_Record_Present (E1) =
5762 Null_Record_Present (E2)
5763 and then FCL (Component_Associations (E1),
5764 Component_Associations (E2));
5766 when N_Function_Call =>
5768 FCE (Name (E1), Name (E2))
5769 and then FCL (Parameter_Associations (E1),
5770 Parameter_Associations (E2));
5772 when N_Indexed_Component =>
5774 FCE (Prefix (E1), Prefix (E2))
5775 and then FCL (Expressions (E1), Expressions (E2));
5777 when N_Integer_Literal =>
5778 return (Intval (E1) = Intval (E2));
5783 when N_Operator_Symbol =>
5785 Chars (E1) = Chars (E2);
5787 when N_Others_Choice =>
5790 when N_Parameter_Association =>
5792 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
5793 and then FCE (Explicit_Actual_Parameter (E1),
5794 Explicit_Actual_Parameter (E2));
5796 when N_Qualified_Expression =>
5798 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5799 and then FCE (Expression (E1), Expression (E2));
5803 FCE (Low_Bound (E1), Low_Bound (E2))
5804 and then FCE (High_Bound (E1), High_Bound (E2));
5806 when N_Real_Literal =>
5807 return (Realval (E1) = Realval (E2));
5809 when N_Selected_Component =>
5811 FCE (Prefix (E1), Prefix (E2))
5812 and then FCE (Selector_Name (E1), Selector_Name (E2));
5816 FCE (Prefix (E1), Prefix (E2))
5817 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
5819 when N_String_Literal =>
5821 S1 : constant String_Id := Strval (E1);
5822 S2 : constant String_Id := Strval (E2);
5823 L1 : constant Nat := String_Length (S1);
5824 L2 : constant Nat := String_Length (S2);
5831 for J in 1 .. L1 loop
5832 if Get_String_Char (S1, J) /=
5833 Get_String_Char (S2, J)
5843 when N_Type_Conversion =>
5845 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5846 and then FCE (Expression (E1), Expression (E2));
5850 Entity (E1) = Entity (E2)
5851 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5853 when N_Unchecked_Type_Conversion =>
5855 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5856 and then FCE (Expression (E1), Expression (E2));
5858 -- All other node types cannot appear in this context. Strictly
5859 -- we should raise a fatal internal error. Instead we just ignore
5860 -- the nodes. This means that if anyone makes a mistake in the
5861 -- expander and mucks an expression tree irretrievably, the
5862 -- result will be a failure to detect a (probably very obscure)
5863 -- case of non-conformance, which is better than bombing on some
5864 -- case where two expressions do in fact conform.
5871 end Fully_Conformant_Expressions;
5873 ----------------------------------------
5874 -- Fully_Conformant_Discrete_Subtypes --
5875 ----------------------------------------
5877 function Fully_Conformant_Discrete_Subtypes
5878 (Given_S1 : Node_Id;
5879 Given_S2 : Node_Id) return Boolean
5881 S1 : constant Node_Id := Original_Node (Given_S1);
5882 S2 : constant Node_Id := Original_Node (Given_S2);
5884 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
5885 -- Special-case for a bound given by a discriminant, which in the body
5886 -- is replaced with the discriminal of the enclosing type.
5888 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
5889 -- Check both bounds
5891 -----------------------
5892 -- Conforming_Bounds --
5893 -----------------------
5895 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
5897 if Is_Entity_Name (B1)
5898 and then Is_Entity_Name (B2)
5899 and then Ekind (Entity (B1)) = E_Discriminant
5901 return Chars (B1) = Chars (B2);
5904 return Fully_Conformant_Expressions (B1, B2);
5906 end Conforming_Bounds;
5908 -----------------------
5909 -- Conforming_Ranges --
5910 -----------------------
5912 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
5915 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
5917 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
5918 end Conforming_Ranges;
5920 -- Start of processing for Fully_Conformant_Discrete_Subtypes
5923 if Nkind (S1) /= Nkind (S2) then
5926 elsif Is_Entity_Name (S1) then
5927 return Entity (S1) = Entity (S2);
5929 elsif Nkind (S1) = N_Range then
5930 return Conforming_Ranges (S1, S2);
5932 elsif Nkind (S1) = N_Subtype_Indication then
5934 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
5937 (Range_Expression (Constraint (S1)),
5938 Range_Expression (Constraint (S2)));
5942 end Fully_Conformant_Discrete_Subtypes;
5944 --------------------
5945 -- Install_Entity --
5946 --------------------
5948 procedure Install_Entity (E : Entity_Id) is
5949 Prev : constant Entity_Id := Current_Entity (E);
5951 Set_Is_Immediately_Visible (E);
5952 Set_Current_Entity (E);
5953 Set_Homonym (E, Prev);
5956 ---------------------
5957 -- Install_Formals --
5958 ---------------------
5960 procedure Install_Formals (Id : Entity_Id) is
5963 F := First_Formal (Id);
5964 while Present (F) loop
5968 end Install_Formals;
5970 -----------------------------
5971 -- Is_Interface_Conformant --
5972 -----------------------------
5974 function Is_Interface_Conformant
5975 (Tagged_Type : Entity_Id;
5976 Iface_Prim : Entity_Id;
5977 Prim : Entity_Id) return Boolean
5979 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
5980 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
5983 pragma Assert (Is_Subprogram (Iface_Prim)
5984 and then Is_Subprogram (Prim)
5985 and then Is_Dispatching_Operation (Iface_Prim)
5986 and then Is_Dispatching_Operation (Prim));
5988 pragma Assert (Is_Interface (Iface)
5989 or else (Present (Alias (Iface_Prim))
5992 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
5994 if Prim = Iface_Prim
5995 or else not Is_Subprogram (Prim)
5996 or else Ekind (Prim) /= Ekind (Iface_Prim)
5997 or else not Is_Dispatching_Operation (Prim)
5998 or else Scope (Prim) /= Scope (Tagged_Type)
6000 or else Base_Type (Typ) /= Tagged_Type
6001 or else not Primitive_Names_Match (Iface_Prim, Prim)
6005 -- Case of a procedure, or a function that does not have a controlling
6006 -- result (I or access I).
6008 elsif Ekind (Iface_Prim) = E_Procedure
6009 or else Etype (Prim) = Etype (Iface_Prim)
6010 or else not Has_Controlling_Result (Prim)
6012 return Type_Conformant (Prim, Iface_Prim,
6013 Skip_Controlling_Formals => True);
6015 -- Case of a function returning an interface, or an access to one.
6016 -- Check that the return types correspond.
6018 elsif Implements_Interface (Typ, Iface) then
6019 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6021 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6026 Type_Conformant (Prim, Iface_Prim,
6027 Skip_Controlling_Formals => True);
6033 end Is_Interface_Conformant;
6035 ---------------------------------
6036 -- Is_Non_Overriding_Operation --
6037 ---------------------------------
6039 function Is_Non_Overriding_Operation
6040 (Prev_E : Entity_Id;
6041 New_E : Entity_Id) return Boolean
6045 G_Typ : Entity_Id := Empty;
6047 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6048 -- If F_Type is a derived type associated with a generic actual subtype,
6049 -- then return its Generic_Parent_Type attribute, else return Empty.
6051 function Types_Correspond
6052 (P_Type : Entity_Id;
6053 N_Type : Entity_Id) return Boolean;
6054 -- Returns true if and only if the types (or designated types in the
6055 -- case of anonymous access types) are the same or N_Type is derived
6056 -- directly or indirectly from P_Type.
6058 -----------------------------
6059 -- Get_Generic_Parent_Type --
6060 -----------------------------
6062 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6067 if Is_Derived_Type (F_Typ)
6068 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6070 -- The tree must be traversed to determine the parent subtype in
6071 -- the generic unit, which unfortunately isn't always available
6072 -- via semantic attributes. ??? (Note: The use of Original_Node
6073 -- is needed for cases where a full derived type has been
6076 Indic := Subtype_Indication
6077 (Type_Definition (Original_Node (Parent (F_Typ))));
6079 if Nkind (Indic) = N_Subtype_Indication then
6080 G_Typ := Entity (Subtype_Mark (Indic));
6082 G_Typ := Entity (Indic);
6085 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6086 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6088 return Generic_Parent_Type (Parent (G_Typ));
6093 end Get_Generic_Parent_Type;
6095 ----------------------
6096 -- Types_Correspond --
6097 ----------------------
6099 function Types_Correspond
6100 (P_Type : Entity_Id;
6101 N_Type : Entity_Id) return Boolean
6103 Prev_Type : Entity_Id := Base_Type (P_Type);
6104 New_Type : Entity_Id := Base_Type (N_Type);
6107 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6108 Prev_Type := Designated_Type (Prev_Type);
6111 if Ekind (New_Type) = E_Anonymous_Access_Type then
6112 New_Type := Designated_Type (New_Type);
6115 if Prev_Type = New_Type then
6118 elsif not Is_Class_Wide_Type (New_Type) then
6119 while Etype (New_Type) /= New_Type loop
6120 New_Type := Etype (New_Type);
6121 if New_Type = Prev_Type then
6127 end Types_Correspond;
6129 -- Start of processing for Is_Non_Overriding_Operation
6132 -- In the case where both operations are implicit derived subprograms
6133 -- then neither overrides the other. This can only occur in certain
6134 -- obscure cases (e.g., derivation from homographs created in a generic
6137 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6140 elsif Ekind (Current_Scope) = E_Package
6141 and then Is_Generic_Instance (Current_Scope)
6142 and then In_Private_Part (Current_Scope)
6143 and then Comes_From_Source (New_E)
6145 -- We examine the formals and result subtype of the inherited
6146 -- operation, to determine whether their type is derived from (the
6147 -- instance of) a generic type.
6149 Formal := First_Formal (Prev_E);
6151 while Present (Formal) loop
6152 F_Typ := Base_Type (Etype (Formal));
6154 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6155 F_Typ := Designated_Type (F_Typ);
6158 G_Typ := Get_Generic_Parent_Type (F_Typ);
6160 Next_Formal (Formal);
6163 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6164 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6171 -- If the generic type is a private type, then the original
6172 -- operation was not overriding in the generic, because there was
6173 -- no primitive operation to override.
6175 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6176 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6177 N_Formal_Private_Type_Definition
6181 -- The generic parent type is the ancestor of a formal derived
6182 -- type declaration. We need to check whether it has a primitive
6183 -- operation that should be overridden by New_E in the generic.
6187 P_Formal : Entity_Id;
6188 N_Formal : Entity_Id;
6192 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6195 while Present (Prim_Elt) loop
6196 P_Prim := Node (Prim_Elt);
6198 if Chars (P_Prim) = Chars (New_E)
6199 and then Ekind (P_Prim) = Ekind (New_E)
6201 P_Formal := First_Formal (P_Prim);
6202 N_Formal := First_Formal (New_E);
6203 while Present (P_Formal) and then Present (N_Formal) loop
6204 P_Typ := Etype (P_Formal);
6205 N_Typ := Etype (N_Formal);
6207 if not Types_Correspond (P_Typ, N_Typ) then
6211 Next_Entity (P_Formal);
6212 Next_Entity (N_Formal);
6215 -- Found a matching primitive operation belonging to the
6216 -- formal ancestor type, so the new subprogram is
6220 and then No (N_Formal)
6221 and then (Ekind (New_E) /= E_Function
6224 (Etype (P_Prim), Etype (New_E)))
6230 Next_Elmt (Prim_Elt);
6233 -- If no match found, then the new subprogram does not
6234 -- override in the generic (nor in the instance).
6242 end Is_Non_Overriding_Operation;
6244 ------------------------------
6245 -- Make_Inequality_Operator --
6246 ------------------------------
6248 -- S is the defining identifier of an equality operator. We build a
6249 -- subprogram declaration with the right signature. This operation is
6250 -- intrinsic, because it is always expanded as the negation of the
6251 -- call to the equality function.
6253 procedure Make_Inequality_Operator (S : Entity_Id) is
6254 Loc : constant Source_Ptr := Sloc (S);
6257 Op_Name : Entity_Id;
6259 FF : constant Entity_Id := First_Formal (S);
6260 NF : constant Entity_Id := Next_Formal (FF);
6263 -- Check that equality was properly defined, ignore call if not
6270 A : constant Entity_Id :=
6271 Make_Defining_Identifier (Sloc (FF),
6272 Chars => Chars (FF));
6274 B : constant Entity_Id :=
6275 Make_Defining_Identifier (Sloc (NF),
6276 Chars => Chars (NF));
6279 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6281 Formals := New_List (
6282 Make_Parameter_Specification (Loc,
6283 Defining_Identifier => A,
6285 New_Reference_To (Etype (First_Formal (S)),
6286 Sloc (Etype (First_Formal (S))))),
6288 Make_Parameter_Specification (Loc,
6289 Defining_Identifier => B,
6291 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6292 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6295 Make_Subprogram_Declaration (Loc,
6297 Make_Function_Specification (Loc,
6298 Defining_Unit_Name => Op_Name,
6299 Parameter_Specifications => Formals,
6300 Result_Definition =>
6301 New_Reference_To (Standard_Boolean, Loc)));
6303 -- Insert inequality right after equality if it is explicit or after
6304 -- the derived type when implicit. These entities are created only
6305 -- for visibility purposes, and eventually replaced in the course of
6306 -- expansion, so they do not need to be attached to the tree and seen
6307 -- by the back-end. Keeping them internal also avoids spurious
6308 -- freezing problems. The declaration is inserted in the tree for
6309 -- analysis, and removed afterwards. If the equality operator comes
6310 -- from an explicit declaration, attach the inequality immediately
6311 -- after. Else the equality is inherited from a derived type
6312 -- declaration, so insert inequality after that declaration.
6314 if No (Alias (S)) then
6315 Insert_After (Unit_Declaration_Node (S), Decl);
6316 elsif Is_List_Member (Parent (S)) then
6317 Insert_After (Parent (S), Decl);
6319 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6322 Mark_Rewrite_Insertion (Decl);
6323 Set_Is_Intrinsic_Subprogram (Op_Name);
6326 Set_Has_Completion (Op_Name);
6327 Set_Corresponding_Equality (Op_Name, S);
6328 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6330 end Make_Inequality_Operator;
6332 ----------------------
6333 -- May_Need_Actuals --
6334 ----------------------
6336 procedure May_Need_Actuals (Fun : Entity_Id) is
6341 F := First_Formal (Fun);
6343 while Present (F) loop
6344 if No (Default_Value (F)) then
6352 Set_Needs_No_Actuals (Fun, B);
6353 end May_Need_Actuals;
6355 ---------------------
6356 -- Mode_Conformant --
6357 ---------------------
6359 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6362 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6364 end Mode_Conformant;
6366 ---------------------------
6367 -- New_Overloaded_Entity --
6368 ---------------------------
6370 procedure New_Overloaded_Entity
6372 Derived_Type : Entity_Id := Empty)
6374 Overridden_Subp : Entity_Id := Empty;
6375 -- Set if the current scope has an operation that is type-conformant
6376 -- with S, and becomes hidden by S.
6378 Is_Primitive_Subp : Boolean;
6379 -- Set to True if the new subprogram is primitive
6382 -- Entity that S overrides
6384 Prev_Vis : Entity_Id := Empty;
6385 -- Predecessor of E in Homonym chain
6387 procedure Check_For_Primitive_Subprogram
6388 (Is_Primitive : out Boolean;
6389 Is_Overriding : Boolean := False);
6390 -- If the subprogram being analyzed is a primitive operation of the type
6391 -- of a formal or result, set the Has_Primitive_Operations flag on the
6392 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6393 -- corresponding flag on the entity itself for later use.
6395 procedure Check_Synchronized_Overriding
6396 (Def_Id : Entity_Id;
6397 Overridden_Subp : out Entity_Id);
6398 -- First determine if Def_Id is an entry or a subprogram either defined
6399 -- in the scope of a task or protected type, or is a primitive of such
6400 -- a type. Check whether Def_Id overrides a subprogram of an interface
6401 -- implemented by the synchronized type, return the overridden entity
6404 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6405 -- Check that E is declared in the private part of the current package,
6406 -- or in the package body, where it may hide a previous declaration.
6407 -- We can't use In_Private_Part by itself because this flag is also
6408 -- set when freezing entities, so we must examine the place of the
6409 -- declaration in the tree, and recognize wrapper packages as well.
6411 ------------------------------------
6412 -- Check_For_Primitive_Subprogram --
6413 ------------------------------------
6415 procedure Check_For_Primitive_Subprogram
6416 (Is_Primitive : out Boolean;
6417 Is_Overriding : Boolean := False)
6423 function Visible_Part_Type (T : Entity_Id) return Boolean;
6424 -- Returns true if T is declared in the visible part of
6425 -- the current package scope; otherwise returns false.
6426 -- Assumes that T is declared in a package.
6428 procedure Check_Private_Overriding (T : Entity_Id);
6429 -- Checks that if a primitive abstract subprogram of a visible
6430 -- abstract type is declared in a private part, then it must
6431 -- override an abstract subprogram declared in the visible part.
6432 -- Also checks that if a primitive function with a controlling
6433 -- result is declared in a private part, then it must override
6434 -- a function declared in the visible part.
6436 ------------------------------
6437 -- Check_Private_Overriding --
6438 ------------------------------
6440 procedure Check_Private_Overriding (T : Entity_Id) is
6442 if Ekind (Current_Scope) = E_Package
6443 and then In_Private_Part (Current_Scope)
6444 and then Visible_Part_Type (T)
6445 and then not In_Instance
6447 if Is_Abstract_Type (T)
6448 and then Is_Abstract_Subprogram (S)
6449 and then (not Is_Overriding
6450 or else not Is_Abstract_Subprogram (E))
6452 Error_Msg_N ("abstract subprograms must be visible "
6453 & "(RM 3.9.3(10))!", S);
6455 elsif Ekind (S) = E_Function
6456 and then Is_Tagged_Type (T)
6457 and then T = Base_Type (Etype (S))
6458 and then not Is_Overriding
6461 ("private function with tagged result must"
6462 & " override visible-part function", S);
6464 ("\move subprogram to the visible part"
6465 & " (RM 3.9.3(10))", S);
6468 end Check_Private_Overriding;
6470 -----------------------
6471 -- Visible_Part_Type --
6472 -----------------------
6474 function Visible_Part_Type (T : Entity_Id) return Boolean is
6475 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
6479 -- If the entity is a private type, then it must be
6480 -- declared in a visible part.
6482 if Ekind (T) in Private_Kind then
6486 -- Otherwise, we traverse the visible part looking for its
6487 -- corresponding declaration. We cannot use the declaration
6488 -- node directly because in the private part the entity of a
6489 -- private type is the one in the full view, which does not
6490 -- indicate that it is the completion of something visible.
6492 N := First (Visible_Declarations (Specification (P)));
6493 while Present (N) loop
6494 if Nkind (N) = N_Full_Type_Declaration
6495 and then Present (Defining_Identifier (N))
6496 and then T = Defining_Identifier (N)
6500 elsif Nkind_In (N, N_Private_Type_Declaration,
6501 N_Private_Extension_Declaration)
6502 and then Present (Defining_Identifier (N))
6503 and then T = Full_View (Defining_Identifier (N))
6512 end Visible_Part_Type;
6514 -- Start of processing for Check_For_Primitive_Subprogram
6517 Is_Primitive := False;
6519 if not Comes_From_Source (S) then
6522 -- If subprogram is at library level, it is not primitive operation
6524 elsif Current_Scope = Standard_Standard then
6527 elsif ((Ekind (Current_Scope) = E_Package
6528 or else Ekind (Current_Scope) = E_Generic_Package)
6529 and then not In_Package_Body (Current_Scope))
6530 or else Is_Overriding
6532 -- For function, check return type
6534 if Ekind (S) = E_Function then
6535 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
6536 F_Typ := Designated_Type (Etype (S));
6541 B_Typ := Base_Type (F_Typ);
6543 if Scope (B_Typ) = Current_Scope
6544 and then not Is_Class_Wide_Type (B_Typ)
6545 and then not Is_Generic_Type (B_Typ)
6547 Is_Primitive := True;
6548 Set_Has_Primitive_Operations (B_Typ);
6549 Set_Is_Primitive (S);
6550 Check_Private_Overriding (B_Typ);
6554 -- For all subprograms, check formals
6556 Formal := First_Formal (S);
6557 while Present (Formal) loop
6558 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
6559 F_Typ := Designated_Type (Etype (Formal));
6561 F_Typ := Etype (Formal);
6564 B_Typ := Base_Type (F_Typ);
6566 if Ekind (B_Typ) = E_Access_Subtype then
6567 B_Typ := Base_Type (B_Typ);
6570 if Scope (B_Typ) = Current_Scope
6571 and then not Is_Class_Wide_Type (B_Typ)
6572 and then not Is_Generic_Type (B_Typ)
6574 Is_Primitive := True;
6575 Set_Is_Primitive (S);
6576 Set_Has_Primitive_Operations (B_Typ);
6577 Check_Private_Overriding (B_Typ);
6580 Next_Formal (Formal);
6583 end Check_For_Primitive_Subprogram;
6585 -----------------------------------
6586 -- Check_Synchronized_Overriding --
6587 -----------------------------------
6589 procedure Check_Synchronized_Overriding
6590 (Def_Id : Entity_Id;
6591 Overridden_Subp : out Entity_Id)
6593 Ifaces_List : Elist_Id;
6597 function Matches_Prefixed_View_Profile
6598 (Prim_Params : List_Id;
6599 Iface_Params : List_Id) return Boolean;
6600 -- Determine whether a subprogram's parameter profile Prim_Params
6601 -- matches that of a potentially overridden interface subprogram
6602 -- Iface_Params. Also determine if the type of first parameter of
6603 -- Iface_Params is an implemented interface.
6605 -----------------------------------
6606 -- Matches_Prefixed_View_Profile --
6607 -----------------------------------
6609 function Matches_Prefixed_View_Profile
6610 (Prim_Params : List_Id;
6611 Iface_Params : List_Id) return Boolean
6613 Iface_Id : Entity_Id;
6614 Iface_Param : Node_Id;
6615 Iface_Typ : Entity_Id;
6616 Prim_Id : Entity_Id;
6617 Prim_Param : Node_Id;
6618 Prim_Typ : Entity_Id;
6620 function Is_Implemented
6621 (Ifaces_List : Elist_Id;
6622 Iface : Entity_Id) return Boolean;
6623 -- Determine if Iface is implemented by the current task or
6626 --------------------
6627 -- Is_Implemented --
6628 --------------------
6630 function Is_Implemented
6631 (Ifaces_List : Elist_Id;
6632 Iface : Entity_Id) return Boolean
6634 Iface_Elmt : Elmt_Id;
6637 Iface_Elmt := First_Elmt (Ifaces_List);
6638 while Present (Iface_Elmt) loop
6639 if Node (Iface_Elmt) = Iface then
6643 Next_Elmt (Iface_Elmt);
6649 -- Start of processing for Matches_Prefixed_View_Profile
6652 Iface_Param := First (Iface_Params);
6653 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
6655 if Is_Access_Type (Iface_Typ) then
6656 Iface_Typ := Designated_Type (Iface_Typ);
6659 Prim_Param := First (Prim_Params);
6661 -- The first parameter of the potentially overridden subprogram
6662 -- must be an interface implemented by Prim.
6664 if not Is_Interface (Iface_Typ)
6665 or else not Is_Implemented (Ifaces_List, Iface_Typ)
6670 -- The checks on the object parameters are done, move onto the
6671 -- rest of the parameters.
6673 if not In_Scope then
6674 Prim_Param := Next (Prim_Param);
6677 Iface_Param := Next (Iface_Param);
6678 while Present (Iface_Param) and then Present (Prim_Param) loop
6679 Iface_Id := Defining_Identifier (Iface_Param);
6680 Iface_Typ := Find_Parameter_Type (Iface_Param);
6682 Prim_Id := Defining_Identifier (Prim_Param);
6683 Prim_Typ := Find_Parameter_Type (Prim_Param);
6685 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
6686 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
6687 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
6689 Iface_Typ := Designated_Type (Iface_Typ);
6690 Prim_Typ := Designated_Type (Prim_Typ);
6693 -- Case of multiple interface types inside a parameter profile
6695 -- (Obj_Param : in out Iface; ...; Param : Iface)
6697 -- If the interface type is implemented, then the matching type
6698 -- in the primitive should be the implementing record type.
6700 if Ekind (Iface_Typ) = E_Record_Type
6701 and then Is_Interface (Iface_Typ)
6702 and then Is_Implemented (Ifaces_List, Iface_Typ)
6704 if Prim_Typ /= Typ then
6708 -- The two parameters must be both mode and subtype conformant
6710 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
6712 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
6721 -- One of the two lists contains more parameters than the other
6723 if Present (Iface_Param) or else Present (Prim_Param) then
6728 end Matches_Prefixed_View_Profile;
6730 -- Start of processing for Check_Synchronized_Overriding
6733 Overridden_Subp := Empty;
6735 -- Def_Id must be an entry or a subprogram. We should skip predefined
6736 -- primitives internally generated by the frontend; however at this
6737 -- stage predefined primitives are still not fully decorated. As a
6738 -- minor optimization we skip here internally generated subprograms.
6740 if (Ekind (Def_Id) /= E_Entry
6741 and then Ekind (Def_Id) /= E_Function
6742 and then Ekind (Def_Id) /= E_Procedure)
6743 or else not Comes_From_Source (Def_Id)
6748 -- Search for the concurrent declaration since it contains the list
6749 -- of all implemented interfaces. In this case, the subprogram is
6750 -- declared within the scope of a protected or a task type.
6752 if Present (Scope (Def_Id))
6753 and then Is_Concurrent_Type (Scope (Def_Id))
6754 and then not Is_Generic_Actual_Type (Scope (Def_Id))
6756 Typ := Scope (Def_Id);
6759 -- The enclosing scope is not a synchronized type and the subprogram
6762 elsif No (First_Formal (Def_Id)) then
6765 -- The subprogram has formals and hence it may be a primitive of a
6769 Typ := Etype (First_Formal (Def_Id));
6771 if Is_Access_Type (Typ) then
6772 Typ := Directly_Designated_Type (Typ);
6775 if Is_Concurrent_Type (Typ)
6776 and then not Is_Generic_Actual_Type (Typ)
6780 -- This case occurs when the concurrent type is declared within
6781 -- a generic unit. As a result the corresponding record has been
6782 -- built and used as the type of the first formal, we just have
6783 -- to retrieve the corresponding concurrent type.
6785 elsif Is_Concurrent_Record_Type (Typ)
6786 and then Present (Corresponding_Concurrent_Type (Typ))
6788 Typ := Corresponding_Concurrent_Type (Typ);
6796 -- There is no overriding to check if is an inherited operation in a
6797 -- type derivation on for a generic actual.
6799 Collect_Interfaces (Typ, Ifaces_List);
6801 if Is_Empty_Elmt_List (Ifaces_List) then
6805 -- Determine whether entry or subprogram Def_Id overrides a primitive
6806 -- operation that belongs to one of the interfaces in Ifaces_List.
6809 Candidate : Entity_Id := Empty;
6810 Hom : Entity_Id := Empty;
6811 Iface_Typ : Entity_Id;
6812 Subp : Entity_Id := Empty;
6815 -- Traverse the homonym chain, looking at a potentially
6816 -- overridden subprogram that belongs to an implemented
6819 Hom := Current_Entity_In_Scope (Def_Id);
6820 while Present (Hom) loop
6824 or else not Is_Overloadable (Subp)
6825 or else not Is_Primitive (Subp)
6826 or else not Is_Dispatching_Operation (Subp)
6827 or else not Is_Interface (Find_Dispatching_Type (Subp))
6831 -- Entries and procedures can override abstract or null
6832 -- interface procedures
6834 elsif (Ekind (Def_Id) = E_Procedure
6835 or else Ekind (Def_Id) = E_Entry)
6836 and then Ekind (Subp) = E_Procedure
6837 and then Matches_Prefixed_View_Profile
6838 (Parameter_Specifications (Parent (Def_Id)),
6839 Parameter_Specifications (Parent (Subp)))
6843 -- For an overridden subprogram Subp, check whether the mode
6844 -- of its first parameter is correct depending on the kind
6845 -- of synchronized type.
6848 Formal : constant Node_Id := First_Formal (Candidate);
6851 -- In order for an entry or a protected procedure to
6852 -- override, the first parameter of the overridden
6853 -- routine must be of mode "out", "in out" or
6854 -- access-to-variable.
6856 if (Ekind (Candidate) = E_Entry
6857 or else Ekind (Candidate) = E_Procedure)
6858 and then Is_Protected_Type (Typ)
6859 and then Ekind (Formal) /= E_In_Out_Parameter
6860 and then Ekind (Formal) /= E_Out_Parameter
6861 and then Nkind (Parameter_Type (Parent (Formal)))
6862 /= N_Access_Definition
6866 -- All other cases are OK since a task entry or routine
6867 -- does not have a restriction on the mode of the first
6868 -- parameter of the overridden interface routine.
6871 Overridden_Subp := Candidate;
6876 -- Functions can override abstract interface functions
6878 elsif Ekind (Def_Id) = E_Function
6879 and then Ekind (Subp) = E_Function
6880 and then Matches_Prefixed_View_Profile
6881 (Parameter_Specifications (Parent (Def_Id)),
6882 Parameter_Specifications (Parent (Subp)))
6883 and then Etype (Result_Definition (Parent (Def_Id))) =
6884 Etype (Result_Definition (Parent (Subp)))
6886 Overridden_Subp := Subp;
6890 Hom := Homonym (Hom);
6893 -- After examining all candidates for overriding, we are
6894 -- left with the best match which is a mode incompatible
6895 -- interface routine. Do not emit an error if the Expander
6896 -- is active since this error will be detected later on
6897 -- after all concurrent types are expanded and all wrappers
6898 -- are built. This check is meant for spec-only
6901 if Present (Candidate)
6902 and then not Expander_Active
6905 Find_Parameter_Type (Parent (First_Formal (Candidate)));
6907 -- Def_Id is primitive of a protected type, declared
6908 -- inside the type, and the candidate is primitive of a
6909 -- limited or synchronized interface.
6912 and then Is_Protected_Type (Typ)
6914 (Is_Limited_Interface (Iface_Typ)
6915 or else Is_Protected_Interface (Iface_Typ)
6916 or else Is_Synchronized_Interface (Iface_Typ)
6917 or else Is_Task_Interface (Iface_Typ))
6919 -- Must reword this message, comma before to in -gnatj
6923 ("first formal of & must be of mode `OUT`, `IN OUT`"
6924 & " or access-to-variable", Typ, Candidate);
6926 ("\to be overridden by protected procedure or entry "
6927 & "(RM 9.4(11.9/2))", Typ);
6931 Overridden_Subp := Candidate;
6934 end Check_Synchronized_Overriding;
6936 ----------------------------
6937 -- Is_Private_Declaration --
6938 ----------------------------
6940 function Is_Private_Declaration (E : Entity_Id) return Boolean is
6941 Priv_Decls : List_Id;
6942 Decl : constant Node_Id := Unit_Declaration_Node (E);
6945 if Is_Package_Or_Generic_Package (Current_Scope)
6946 and then In_Private_Part (Current_Scope)
6949 Private_Declarations (
6950 Specification (Unit_Declaration_Node (Current_Scope)));
6952 return In_Package_Body (Current_Scope)
6954 (Is_List_Member (Decl)
6955 and then List_Containing (Decl) = Priv_Decls)
6956 or else (Nkind (Parent (Decl)) = N_Package_Specification
6957 and then not Is_Compilation_Unit (
6958 Defining_Entity (Parent (Decl)))
6959 and then List_Containing (Parent (Parent (Decl)))
6964 end Is_Private_Declaration;
6966 -- Start of processing for New_Overloaded_Entity
6969 -- We need to look for an entity that S may override. This must be a
6970 -- homonym in the current scope, so we look for the first homonym of
6971 -- S in the current scope as the starting point for the search.
6973 E := Current_Entity_In_Scope (S);
6975 -- If there is no homonym then this is definitely not overriding
6978 Enter_Overloaded_Entity (S);
6979 Check_Dispatching_Operation (S, Empty);
6980 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
6982 -- If subprogram has an explicit declaration, check whether it
6983 -- has an overriding indicator.
6985 if Comes_From_Source (S) then
6986 Check_Synchronized_Overriding (S, Overridden_Subp);
6987 Check_Overriding_Indicator
6988 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
6991 -- If there is a homonym that is not overloadable, then we have an
6992 -- error, except for the special cases checked explicitly below.
6994 elsif not Is_Overloadable (E) then
6996 -- Check for spurious conflict produced by a subprogram that has the
6997 -- same name as that of the enclosing generic package. The conflict
6998 -- occurs within an instance, between the subprogram and the renaming
6999 -- declaration for the package. After the subprogram, the package
7000 -- renaming declaration becomes hidden.
7002 if Ekind (E) = E_Package
7003 and then Present (Renamed_Object (E))
7004 and then Renamed_Object (E) = Current_Scope
7005 and then Nkind (Parent (Renamed_Object (E))) =
7006 N_Package_Specification
7007 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7010 Set_Is_Immediately_Visible (E, False);
7011 Enter_Overloaded_Entity (S);
7012 Set_Homonym (S, Homonym (E));
7013 Check_Dispatching_Operation (S, Empty);
7014 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7016 -- If the subprogram is implicit it is hidden by the previous
7017 -- declaration. However if it is dispatching, it must appear in the
7018 -- dispatch table anyway, because it can be dispatched to even if it
7019 -- cannot be called directly.
7021 elsif Present (Alias (S))
7022 and then not Comes_From_Source (S)
7024 Set_Scope (S, Current_Scope);
7026 if Is_Dispatching_Operation (Alias (S)) then
7027 Check_Dispatching_Operation (S, Empty);
7033 Error_Msg_Sloc := Sloc (E);
7035 -- Generate message, with useful additional warning if in generic
7037 if Is_Generic_Unit (E) then
7038 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7039 Error_Msg_N ("\& conflicts with declaration#", S);
7041 Error_Msg_N ("& conflicts with declaration#", S);
7047 -- E exists and is overloadable
7050 -- Ada 2005 (AI-251): Derivation of abstract interface primitives
7051 -- need no check against the homonym chain. They are directly added
7052 -- to the list of primitive operations of Derived_Type.
7054 if Ada_Version >= Ada_05
7055 and then Present (Derived_Type)
7056 and then Is_Dispatching_Operation (Alias (S))
7057 and then Present (Find_Dispatching_Type (Alias (S)))
7058 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7060 goto Add_New_Entity;
7063 Check_Synchronized_Overriding (S, Overridden_Subp);
7065 -- Loop through E and its homonyms to determine if any of them is
7066 -- the candidate for overriding by S.
7068 while Present (E) loop
7070 -- Definitely not interesting if not in the current scope
7072 if Scope (E) /= Current_Scope then
7075 -- Check if we have type conformance
7077 elsif Type_Conformant (E, S) then
7079 -- If the old and new entities have the same profile and one
7080 -- is not the body of the other, then this is an error, unless
7081 -- one of them is implicitly declared.
7083 -- There are some cases when both can be implicit, for example
7084 -- when both a literal and a function that overrides it are
7085 -- inherited in a derivation, or when an inherited operation
7086 -- of a tagged full type overrides the inherited operation of
7087 -- a private extension. Ada 83 had a special rule for the
7088 -- literal case. In Ada95, the later implicit operation hides
7089 -- the former, and the literal is always the former. In the
7090 -- odd case where both are derived operations declared at the
7091 -- same point, both operations should be declared, and in that
7092 -- case we bypass the following test and proceed to the next
7093 -- part (this can only occur for certain obscure cases
7094 -- involving homographs in instances and can't occur for
7095 -- dispatching operations ???). Note that the following
7096 -- condition is less than clear. For example, it's not at all
7097 -- clear why there's a test for E_Entry here. ???
7099 if Present (Alias (S))
7100 and then (No (Alias (E))
7101 or else Comes_From_Source (E)
7102 or else Is_Dispatching_Operation (E))
7104 (Ekind (E) = E_Entry
7105 or else Ekind (E) /= E_Enumeration_Literal)
7107 -- When an derived operation is overloaded it may be due to
7108 -- the fact that the full view of a private extension
7109 -- re-inherits. It has to be dealt with.
7111 if Is_Package_Or_Generic_Package (Current_Scope)
7112 and then In_Private_Part (Current_Scope)
7114 Check_Operation_From_Private_View (S, E);
7117 -- In any case the implicit operation remains hidden by
7118 -- the existing declaration, which is overriding.
7120 Set_Is_Overriding_Operation (E);
7122 if Comes_From_Source (E) then
7123 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7125 -- Indicate that E overrides the operation from which
7128 if Present (Alias (S)) then
7129 Set_Overridden_Operation (E, Alias (S));
7131 Set_Overridden_Operation (E, S);
7137 -- Within an instance, the renaming declarations for
7138 -- actual subprograms may become ambiguous, but they do
7139 -- not hide each other.
7141 elsif Ekind (E) /= E_Entry
7142 and then not Comes_From_Source (E)
7143 and then not Is_Generic_Instance (E)
7144 and then (Present (Alias (E))
7145 or else Is_Intrinsic_Subprogram (E))
7146 and then (not In_Instance
7147 or else No (Parent (E))
7148 or else Nkind (Unit_Declaration_Node (E)) /=
7149 N_Subprogram_Renaming_Declaration)
7151 -- A subprogram child unit is not allowed to override
7152 -- an inherited subprogram (10.1.1(20)).
7154 if Is_Child_Unit (S) then
7156 ("child unit overrides inherited subprogram in parent",
7161 if Is_Non_Overriding_Operation (E, S) then
7162 Enter_Overloaded_Entity (S);
7163 if No (Derived_Type)
7164 or else Is_Tagged_Type (Derived_Type)
7166 Check_Dispatching_Operation (S, Empty);
7172 -- E is a derived operation or an internal operator which
7173 -- is being overridden. Remove E from further visibility.
7174 -- Furthermore, if E is a dispatching operation, it must be
7175 -- replaced in the list of primitive operations of its type
7176 -- (see Override_Dispatching_Operation).
7178 Overridden_Subp := E;
7184 Prev := First_Entity (Current_Scope);
7186 while Present (Prev)
7187 and then Next_Entity (Prev) /= E
7192 -- It is possible for E to be in the current scope and
7193 -- yet not in the entity chain. This can only occur in a
7194 -- generic context where E is an implicit concatenation
7195 -- in the formal part, because in a generic body the
7196 -- entity chain starts with the formals.
7199 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7201 -- E must be removed both from the entity_list of the
7202 -- current scope, and from the visibility chain
7204 if Debug_Flag_E then
7205 Write_Str ("Override implicit operation ");
7206 Write_Int (Int (E));
7210 -- If E is a predefined concatenation, it stands for four
7211 -- different operations. As a result, a single explicit
7212 -- declaration does not hide it. In a possible ambiguous
7213 -- situation, Disambiguate chooses the user-defined op,
7214 -- so it is correct to retain the previous internal one.
7216 if Chars (E) /= Name_Op_Concat
7217 or else Ekind (E) /= E_Operator
7219 -- For nondispatching derived operations that are
7220 -- overridden by a subprogram declared in the private
7221 -- part of a package, we retain the derived
7222 -- subprogram but mark it as not immediately visible.
7223 -- If the derived operation was declared in the
7224 -- visible part then this ensures that it will still
7225 -- be visible outside the package with the proper
7226 -- signature (calls from outside must also be
7227 -- directed to this version rather than the
7228 -- overriding one, unlike the dispatching case).
7229 -- Calls from inside the package will still resolve
7230 -- to the overriding subprogram since the derived one
7231 -- is marked as not visible within the package.
7233 -- If the private operation is dispatching, we achieve
7234 -- the overriding by keeping the implicit operation
7235 -- but setting its alias to be the overriding one. In
7236 -- this fashion the proper body is executed in all
7237 -- cases, but the original signature is used outside
7240 -- If the overriding is not in the private part, we
7241 -- remove the implicit operation altogether.
7243 if Is_Private_Declaration (S) then
7245 if not Is_Dispatching_Operation (E) then
7246 Set_Is_Immediately_Visible (E, False);
7248 -- Work done in Override_Dispatching_Operation,
7249 -- so nothing else need to be done here.
7255 -- Find predecessor of E in Homonym chain
7257 if E = Current_Entity (E) then
7260 Prev_Vis := Current_Entity (E);
7261 while Homonym (Prev_Vis) /= E loop
7262 Prev_Vis := Homonym (Prev_Vis);
7266 if Prev_Vis /= Empty then
7268 -- Skip E in the visibility chain
7270 Set_Homonym (Prev_Vis, Homonym (E));
7273 Set_Name_Entity_Id (Chars (E), Homonym (E));
7276 Set_Next_Entity (Prev, Next_Entity (E));
7278 if No (Next_Entity (Prev)) then
7279 Set_Last_Entity (Current_Scope, Prev);
7285 Enter_Overloaded_Entity (S);
7286 Set_Is_Overriding_Operation (S);
7287 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7289 -- Indicate that S overrides the operation from which
7292 if Comes_From_Source (S) then
7293 if Present (Alias (E)) then
7294 Set_Overridden_Operation (S, Alias (E));
7296 Set_Overridden_Operation (S, E);
7300 if Is_Dispatching_Operation (E) then
7302 -- An overriding dispatching subprogram inherits the
7303 -- convention of the overridden subprogram (by
7306 Set_Convention (S, Convention (E));
7307 Check_Dispatching_Operation (S, E);
7310 Check_Dispatching_Operation (S, Empty);
7313 Check_For_Primitive_Subprogram
7314 (Is_Primitive_Subp, Is_Overriding => True);
7315 goto Check_Inequality;
7318 -- Apparent redeclarations in instances can occur when two
7319 -- formal types get the same actual type. The subprograms in
7320 -- in the instance are legal, even if not callable from the
7321 -- outside. Calls from within are disambiguated elsewhere.
7322 -- For dispatching operations in the visible part, the usual
7323 -- rules apply, and operations with the same profile are not
7326 elsif (In_Instance_Visible_Part
7327 and then not Is_Dispatching_Operation (E))
7328 or else In_Instance_Not_Visible
7332 -- Here we have a real error (identical profile)
7335 Error_Msg_Sloc := Sloc (E);
7337 -- Avoid cascaded errors if the entity appears in
7338 -- subsequent calls.
7340 Set_Scope (S, Current_Scope);
7342 -- Generate error, with extra useful warning for the case
7343 -- of a generic instance with no completion.
7345 if Is_Generic_Instance (S)
7346 and then not Has_Completion (E)
7349 ("instantiation cannot provide body for&", S);
7350 Error_Msg_N ("\& conflicts with declaration#", S);
7352 Error_Msg_N ("& conflicts with declaration#", S);
7359 -- If one subprogram has an access parameter and the other
7360 -- a parameter of an access type, calls to either might be
7361 -- ambiguous. Verify that parameters match except for the
7362 -- access parameter.
7364 if May_Hide_Profile then
7369 F1 := First_Formal (S);
7370 F2 := First_Formal (E);
7371 while Present (F1) and then Present (F2) loop
7372 if Is_Access_Type (Etype (F1)) then
7373 if not Is_Access_Type (Etype (F2))
7374 or else not Conforming_Types
7375 (Designated_Type (Etype (F1)),
7376 Designated_Type (Etype (F2)),
7379 May_Hide_Profile := False;
7383 not Conforming_Types
7384 (Etype (F1), Etype (F2), Type_Conformant)
7386 May_Hide_Profile := False;
7397 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7408 -- On exit, we know that S is a new entity
7410 Enter_Overloaded_Entity (S);
7411 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7412 Check_Overriding_Indicator
7413 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7415 -- If S is a derived operation for an untagged type then by
7416 -- definition it's not a dispatching operation (even if the parent
7417 -- operation was dispatching), so we don't call
7418 -- Check_Dispatching_Operation in that case.
7420 if No (Derived_Type)
7421 or else Is_Tagged_Type (Derived_Type)
7423 Check_Dispatching_Operation (S, Empty);
7427 -- If this is a user-defined equality operator that is not a derived
7428 -- subprogram, create the corresponding inequality. If the operation is
7429 -- dispatching, the expansion is done elsewhere, and we do not create
7430 -- an explicit inequality operation.
7432 <<Check_Inequality>>
7433 if Chars (S) = Name_Op_Eq
7434 and then Etype (S) = Standard_Boolean
7435 and then Present (Parent (S))
7436 and then not Is_Dispatching_Operation (S)
7438 Make_Inequality_Operator (S);
7440 end New_Overloaded_Entity;
7442 ---------------------
7443 -- Process_Formals --
7444 ---------------------
7446 procedure Process_Formals
7448 Related_Nod : Node_Id)
7450 Param_Spec : Node_Id;
7452 Formal_Type : Entity_Id;
7456 Num_Out_Params : Nat := 0;
7457 First_Out_Param : Entity_Id := Empty;
7458 -- Used for setting Is_Only_Out_Parameter
7460 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
7461 -- Check whether the default has a class-wide type. After analysis the
7462 -- default has the type of the formal, so we must also check explicitly
7463 -- for an access attribute.
7465 ---------------------------
7466 -- Is_Class_Wide_Default --
7467 ---------------------------
7469 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
7471 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
7472 or else (Nkind (D) = N_Attribute_Reference
7473 and then Attribute_Name (D) = Name_Access
7474 and then Is_Class_Wide_Type (Etype (Prefix (D))));
7475 end Is_Class_Wide_Default;
7477 -- Start of processing for Process_Formals
7480 -- In order to prevent premature use of the formals in the same formal
7481 -- part, the Ekind is left undefined until all default expressions are
7482 -- analyzed. The Ekind is established in a separate loop at the end.
7484 Param_Spec := First (T);
7485 while Present (Param_Spec) loop
7486 Formal := Defining_Identifier (Param_Spec);
7487 Set_Never_Set_In_Source (Formal, True);
7488 Enter_Name (Formal);
7490 -- Case of ordinary parameters
7492 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
7493 Find_Type (Parameter_Type (Param_Spec));
7494 Ptype := Parameter_Type (Param_Spec);
7496 if Ptype = Error then
7500 Formal_Type := Entity (Ptype);
7502 if Is_Incomplete_Type (Formal_Type)
7504 (Is_Class_Wide_Type (Formal_Type)
7505 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
7507 -- Ada 2005 (AI-326): Tagged incomplete types allowed
7509 if Is_Tagged_Type (Formal_Type) then
7512 -- Special handling of Value_Type for CIL case
7514 elsif Is_Value_Type (Formal_Type) then
7517 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
7518 N_Access_Procedure_Definition)
7520 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
7522 -- An incomplete type that is not tagged is allowed in an
7523 -- access-to-subprogram type only if it is a local declaration
7524 -- with a forthcoming completion (3.10.1 (9.2/2)).
7526 elsif Scope (Formal_Type) /= Scope (Current_Scope) then
7528 ("invalid use of limited view of type", Param_Spec);
7531 elsif Ekind (Formal_Type) = E_Void then
7532 Error_Msg_NE ("premature use of&",
7533 Parameter_Type (Param_Spec), Formal_Type);
7536 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7537 -- declaration corresponding to the null-excluding type of the
7538 -- formal in the enclosing scope. Finally, replace the parameter
7539 -- type of the formal with the internal subtype.
7541 if Ada_Version >= Ada_05
7542 and then Null_Exclusion_Present (Param_Spec)
7544 if not Is_Access_Type (Formal_Type) then
7546 ("`NOT NULL` allowed only for an access type", Param_Spec);
7549 if Can_Never_Be_Null (Formal_Type)
7550 and then Comes_From_Source (Related_Nod)
7553 ("`NOT NULL` not allowed (& already excludes null)",
7559 Create_Null_Excluding_Itype
7561 Related_Nod => Related_Nod,
7562 Scope_Id => Scope (Current_Scope));
7564 -- If the designated type of the itype is an itype we
7565 -- decorate it with the Has_Delayed_Freeze attribute to
7566 -- avoid problems with the backend.
7569 -- type T is access procedure;
7570 -- procedure Op (O : not null T);
7572 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
7573 Set_Has_Delayed_Freeze (Formal_Type);
7578 -- An access formal type
7582 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7584 -- No need to continue if we already notified errors
7586 if not Present (Formal_Type) then
7590 -- Ada 2005 (AI-254)
7593 AD : constant Node_Id :=
7594 Access_To_Subprogram_Definition
7595 (Parameter_Type (Param_Spec));
7597 if Present (AD) and then Protected_Present (AD) then
7599 Replace_Anonymous_Access_To_Protected_Subprogram
7605 Set_Etype (Formal, Formal_Type);
7606 Default := Expression (Param_Spec);
7608 if Present (Default) then
7609 if Out_Present (Param_Spec) then
7611 ("default initialization only allowed for IN parameters",
7615 -- Do the special preanalysis of the expression (see section on
7616 -- "Handling of Default Expressions" in the spec of package Sem).
7618 Preanalyze_Spec_Expression (Default, Formal_Type);
7620 -- An access to constant cannot be the default for
7621 -- an access parameter that is an access to variable.
7623 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7624 and then not Is_Access_Constant (Formal_Type)
7625 and then Is_Access_Type (Etype (Default))
7626 and then Is_Access_Constant (Etype (Default))
7629 ("formal that is access to variable cannot be initialized " &
7630 "with an access-to-constant expression", Default);
7633 -- Check that the designated type of an access parameter's default
7634 -- is not a class-wide type unless the parameter's designated type
7635 -- is also class-wide.
7637 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7638 and then not From_With_Type (Formal_Type)
7639 and then Is_Class_Wide_Default (Default)
7640 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
7643 ("access to class-wide expression not allowed here", Default);
7647 -- Ada 2005 (AI-231): Static checks
7649 if Ada_Version >= Ada_05
7650 and then Is_Access_Type (Etype (Formal))
7651 and then Can_Never_Be_Null (Etype (Formal))
7653 Null_Exclusion_Static_Checks (Param_Spec);
7660 -- If this is the formal part of a function specification, analyze the
7661 -- subtype mark in the context where the formals are visible but not
7662 -- yet usable, and may hide outer homographs.
7664 if Nkind (Related_Nod) = N_Function_Specification then
7665 Analyze_Return_Type (Related_Nod);
7668 -- Now set the kind (mode) of each formal
7670 Param_Spec := First (T);
7672 while Present (Param_Spec) loop
7673 Formal := Defining_Identifier (Param_Spec);
7674 Set_Formal_Mode (Formal);
7676 if Ekind (Formal) = E_In_Parameter then
7677 Set_Default_Value (Formal, Expression (Param_Spec));
7679 if Present (Expression (Param_Spec)) then
7680 Default := Expression (Param_Spec);
7682 if Is_Scalar_Type (Etype (Default)) then
7684 (Parameter_Type (Param_Spec)) /= N_Access_Definition
7686 Formal_Type := Entity (Parameter_Type (Param_Spec));
7689 Formal_Type := Access_Definition
7690 (Related_Nod, Parameter_Type (Param_Spec));
7693 Apply_Scalar_Range_Check (Default, Formal_Type);
7697 elsif Ekind (Formal) = E_Out_Parameter then
7698 Num_Out_Params := Num_Out_Params + 1;
7700 if Num_Out_Params = 1 then
7701 First_Out_Param := Formal;
7704 elsif Ekind (Formal) = E_In_Out_Parameter then
7705 Num_Out_Params := Num_Out_Params + 1;
7711 if Present (First_Out_Param) and then Num_Out_Params = 1 then
7712 Set_Is_Only_Out_Parameter (First_Out_Param);
7714 end Process_Formals;
7720 procedure Process_PPCs
7722 Spec_Id : Entity_Id;
7723 Body_Id : Entity_Id)
7725 Loc : constant Source_Ptr := Sloc (N);
7727 Plist : List_Id := No_List;
7731 function Grab_PPC (Nam : Name_Id) return Node_Id;
7732 -- Prag contains an analyzed precondition or postcondition pragma.
7733 -- This function copies the pragma, changes it to the corresponding
7734 -- Check pragma and returns the Check pragma as the result. The
7735 -- argument Nam is either Name_Precondition or Name_Postcondition.
7741 function Grab_PPC (Nam : Name_Id) return Node_Id is
7742 CP : constant Node_Id := New_Copy_Tree (Prag);
7745 -- Set Analyzed to false, since we want to reanalyze the check
7746 -- procedure. Note that it is only at the outer level that we
7747 -- do this fiddling, for the spec cases, the already preanalyzed
7748 -- parameters are not affected.
7750 -- For a postcondition pragma within a generic, preserve the pragma
7751 -- for later expansion.
7753 Set_Analyzed (CP, False);
7755 if Nam = Name_Postcondition
7756 and then not Expander_Active
7761 -- Change pragma into corresponding pragma Check
7763 Prepend_To (Pragma_Argument_Associations (CP),
7764 Make_Pragma_Argument_Association (Sloc (Prag),
7766 Make_Identifier (Loc,
7768 Set_Pragma_Identifier (CP,
7769 Make_Identifier (Sloc (Prag),
7770 Chars => Name_Check));
7775 -- Start of processing for Process_PPCs
7778 -- Nothing to do if we are not generating code
7780 if Operating_Mode /= Generate_Code then
7784 -- Grab preconditions from spec
7786 if Present (Spec_Id) then
7788 -- Loop through PPC pragmas from spec. Note that preconditions from
7789 -- the body will be analyzed and converted when we scan the body
7790 -- declarations below.
7792 Prag := Spec_PPC_List (Spec_Id);
7793 while Present (Prag) loop
7794 if Pragma_Name (Prag) = Name_Precondition
7795 and then PPC_Enabled (Prag)
7797 -- Add pragma Check at the start of the declarations of N.
7798 -- Note that this processing reverses the order of the list,
7799 -- which is what we want since new entries were chained to
7800 -- the head of the list.
7802 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
7805 Prag := Next_Pragma (Prag);
7809 -- Build postconditions procedure if needed and prepend the following
7810 -- declaration to the start of the declarations for the subprogram.
7812 -- procedure _postconditions [(_Result : resulttype)] is
7814 -- pragma Check (Postcondition, condition [,message]);
7815 -- pragma Check (Postcondition, condition [,message]);
7819 -- First we deal with the postconditions in the body
7821 if Is_Non_Empty_List (Declarations (N)) then
7823 -- Loop through declarations
7825 Prag := First (Declarations (N));
7826 while Present (Prag) loop
7827 if Nkind (Prag) = N_Pragma then
7829 -- If pragma, capture if enabled postcondition, else ignore
7831 if Pragma_Name (Prag) = Name_Postcondition
7832 and then Check_Enabled (Name_Postcondition)
7834 if Plist = No_List then
7835 Plist := Empty_List;
7840 -- If expansion is disabled, as in a generic unit,
7841 -- save pragma for later expansion.
7843 if not Expander_Active then
7844 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
7846 Append (Grab_PPC (Name_Postcondition), Plist);
7852 -- Not a pragma, if comes from source, then end scan
7854 elsif Comes_From_Source (Prag) then
7857 -- Skip stuff not coming from source
7865 -- Now deal with any postconditions from the spec
7867 if Present (Spec_Id) then
7869 -- Loop through PPC pragmas from spec
7871 Prag := Spec_PPC_List (Spec_Id);
7872 while Present (Prag) loop
7873 if Pragma_Name (Prag) = Name_Postcondition
7874 and then PPC_Enabled (Prag)
7876 if Plist = No_List then
7877 Plist := Empty_List;
7880 if not Expander_Active then
7881 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
7883 Append (Grab_PPC (Name_Postcondition), Plist);
7887 Prag := Next_Pragma (Prag);
7891 -- If we had any postconditions and expansion is enabled, build
7892 -- the Postconditions procedure.
7895 and then Expander_Active
7897 Subp := Defining_Entity (N);
7899 if Etype (Subp) /= Standard_Void_Type then
7901 Make_Parameter_Specification (Loc,
7902 Defining_Identifier =>
7903 Make_Defining_Identifier (Loc,
7904 Chars => Name_uResult),
7905 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
7910 Prepend_To (Declarations (N),
7911 Make_Subprogram_Body (Loc,
7913 Make_Procedure_Specification (Loc,
7914 Defining_Unit_Name =>
7915 Make_Defining_Identifier (Loc,
7916 Chars => Name_uPostconditions),
7917 Parameter_Specifications => Parms),
7919 Declarations => Empty_List,
7921 Handled_Statement_Sequence =>
7922 Make_Handled_Sequence_Of_Statements (Loc,
7923 Statements => Plist)));
7925 if Present (Spec_Id) then
7926 Set_Has_Postconditions (Spec_Id);
7928 Set_Has_Postconditions (Body_Id);
7933 ----------------------------
7934 -- Reference_Body_Formals --
7935 ----------------------------
7937 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
7942 if Error_Posted (Spec) then
7946 -- Iterate over both lists. They may be of different lengths if the two
7947 -- specs are not conformant.
7949 Fs := First_Formal (Spec);
7950 Fb := First_Formal (Bod);
7951 while Present (Fs) and then Present (Fb) loop
7952 Generate_Reference (Fs, Fb, 'b');
7955 Style.Check_Identifier (Fb, Fs);
7958 Set_Spec_Entity (Fb, Fs);
7959 Set_Referenced (Fs, False);
7963 end Reference_Body_Formals;
7965 -------------------------
7966 -- Set_Actual_Subtypes --
7967 -------------------------
7969 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
7970 Loc : constant Source_Ptr := Sloc (N);
7974 First_Stmt : Node_Id := Empty;
7975 AS_Needed : Boolean;
7978 -- If this is an empty initialization procedure, no need to create
7979 -- actual subtypes (small optimization).
7981 if Ekind (Subp) = E_Procedure
7982 and then Is_Null_Init_Proc (Subp)
7987 Formal := First_Formal (Subp);
7988 while Present (Formal) loop
7989 T := Etype (Formal);
7991 -- We never need an actual subtype for a constrained formal
7993 if Is_Constrained (T) then
7996 -- If we have unknown discriminants, then we do not need an actual
7997 -- subtype, or more accurately we cannot figure it out! Note that
7998 -- all class-wide types have unknown discriminants.
8000 elsif Has_Unknown_Discriminants (T) then
8003 -- At this stage we have an unconstrained type that may need an
8004 -- actual subtype. For sure the actual subtype is needed if we have
8005 -- an unconstrained array type.
8007 elsif Is_Array_Type (T) then
8010 -- The only other case needing an actual subtype is an unconstrained
8011 -- record type which is an IN parameter (we cannot generate actual
8012 -- subtypes for the OUT or IN OUT case, since an assignment can
8013 -- change the discriminant values. However we exclude the case of
8014 -- initialization procedures, since discriminants are handled very
8015 -- specially in this context, see the section entitled "Handling of
8016 -- Discriminants" in Einfo.
8018 -- We also exclude the case of Discrim_SO_Functions (functions used
8019 -- in front end layout mode for size/offset values), since in such
8020 -- functions only discriminants are referenced, and not only are such
8021 -- subtypes not needed, but they cannot always be generated, because
8022 -- of order of elaboration issues.
8024 elsif Is_Record_Type (T)
8025 and then Ekind (Formal) = E_In_Parameter
8026 and then Chars (Formal) /= Name_uInit
8027 and then not Is_Unchecked_Union (T)
8028 and then not Is_Discrim_SO_Function (Subp)
8032 -- All other cases do not need an actual subtype
8038 -- Generate actual subtypes for unconstrained arrays and
8039 -- unconstrained discriminated records.
8042 if Nkind (N) = N_Accept_Statement then
8044 -- If expansion is active, The formal is replaced by a local
8045 -- variable that renames the corresponding entry of the
8046 -- parameter block, and it is this local variable that may
8047 -- require an actual subtype.
8049 if Expander_Active then
8050 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8052 Decl := Build_Actual_Subtype (T, Formal);
8055 if Present (Handled_Statement_Sequence (N)) then
8057 First (Statements (Handled_Statement_Sequence (N)));
8058 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8059 Mark_Rewrite_Insertion (Decl);
8061 -- If the accept statement has no body, there will be no
8062 -- reference to the actuals, so no need to compute actual
8069 Decl := Build_Actual_Subtype (T, Formal);
8070 Prepend (Decl, Declarations (N));
8071 Mark_Rewrite_Insertion (Decl);
8074 -- The declaration uses the bounds of an existing object, and
8075 -- therefore needs no constraint checks.
8077 Analyze (Decl, Suppress => All_Checks);
8079 -- We need to freeze manually the generated type when it is
8080 -- inserted anywhere else than in a declarative part.
8082 if Present (First_Stmt) then
8083 Insert_List_Before_And_Analyze (First_Stmt,
8084 Freeze_Entity (Defining_Identifier (Decl), Loc));
8087 if Nkind (N) = N_Accept_Statement
8088 and then Expander_Active
8090 Set_Actual_Subtype (Renamed_Object (Formal),
8091 Defining_Identifier (Decl));
8093 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8097 Next_Formal (Formal);
8099 end Set_Actual_Subtypes;
8101 ---------------------
8102 -- Set_Formal_Mode --
8103 ---------------------
8105 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8106 Spec : constant Node_Id := Parent (Formal_Id);
8109 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8110 -- since we ensure that corresponding actuals are always valid at the
8111 -- point of the call.
8113 if Out_Present (Spec) then
8114 if Ekind (Scope (Formal_Id)) = E_Function
8115 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8117 Error_Msg_N ("functions can only have IN parameters", Spec);
8118 Set_Ekind (Formal_Id, E_In_Parameter);
8120 elsif In_Present (Spec) then
8121 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8124 Set_Ekind (Formal_Id, E_Out_Parameter);
8125 Set_Never_Set_In_Source (Formal_Id, True);
8126 Set_Is_True_Constant (Formal_Id, False);
8127 Set_Current_Value (Formal_Id, Empty);
8131 Set_Ekind (Formal_Id, E_In_Parameter);
8134 -- Set Is_Known_Non_Null for access parameters since the language
8135 -- guarantees that access parameters are always non-null. We also set
8136 -- Can_Never_Be_Null, since there is no way to change the value.
8138 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8140 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8141 -- null; In Ada 2005, only if then null_exclusion is explicit.
8143 if Ada_Version < Ada_05
8144 or else Can_Never_Be_Null (Etype (Formal_Id))
8146 Set_Is_Known_Non_Null (Formal_Id);
8147 Set_Can_Never_Be_Null (Formal_Id);
8150 -- Ada 2005 (AI-231): Null-exclusion access subtype
8152 elsif Is_Access_Type (Etype (Formal_Id))
8153 and then Can_Never_Be_Null (Etype (Formal_Id))
8155 Set_Is_Known_Non_Null (Formal_Id);
8158 Set_Mechanism (Formal_Id, Default_Mechanism);
8159 Set_Formal_Validity (Formal_Id);
8160 end Set_Formal_Mode;
8162 -------------------------
8163 -- Set_Formal_Validity --
8164 -------------------------
8166 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8168 -- If no validity checking, then we cannot assume anything about the
8169 -- validity of parameters, since we do not know there is any checking
8170 -- of the validity on the call side.
8172 if not Validity_Checks_On then
8175 -- If validity checking for parameters is enabled, this means we are
8176 -- not supposed to make any assumptions about argument values.
8178 elsif Validity_Check_Parameters then
8181 -- If we are checking in parameters, we will assume that the caller is
8182 -- also checking parameters, so we can assume the parameter is valid.
8184 elsif Ekind (Formal_Id) = E_In_Parameter
8185 and then Validity_Check_In_Params
8187 Set_Is_Known_Valid (Formal_Id, True);
8189 -- Similar treatment for IN OUT parameters
8191 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8192 and then Validity_Check_In_Out_Params
8194 Set_Is_Known_Valid (Formal_Id, True);
8196 end Set_Formal_Validity;
8198 ------------------------
8199 -- Subtype_Conformant --
8200 ------------------------
8202 function Subtype_Conformant
8203 (New_Id : Entity_Id;
8205 Skip_Controlling_Formals : Boolean := False) return Boolean
8209 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8210 Skip_Controlling_Formals => Skip_Controlling_Formals);
8212 end Subtype_Conformant;
8214 ---------------------
8215 -- Type_Conformant --
8216 ---------------------
8218 function Type_Conformant
8219 (New_Id : Entity_Id;
8221 Skip_Controlling_Formals : Boolean := False) return Boolean
8225 May_Hide_Profile := False;
8228 (New_Id, Old_Id, Type_Conformant, False, Result,
8229 Skip_Controlling_Formals => Skip_Controlling_Formals);
8231 end Type_Conformant;
8233 -------------------------------
8234 -- Valid_Operator_Definition --
8235 -------------------------------
8237 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8240 Id : constant Name_Id := Chars (Designator);
8244 F := First_Formal (Designator);
8245 while Present (F) loop
8248 if Present (Default_Value (F)) then
8250 ("default values not allowed for operator parameters",
8257 -- Verify that user-defined operators have proper number of arguments
8258 -- First case of operators which can only be unary
8261 or else Id = Name_Op_Abs
8265 -- Case of operators which can be unary or binary
8267 elsif Id = Name_Op_Add
8268 or Id = Name_Op_Subtract
8270 N_OK := (N in 1 .. 2);
8272 -- All other operators can only be binary
8280 ("incorrect number of arguments for operator", Designator);
8284 and then Base_Type (Etype (Designator)) = Standard_Boolean
8285 and then not Is_Intrinsic_Subprogram (Designator)
8288 ("explicit definition of inequality not allowed", Designator);
8290 end Valid_Operator_Definition;