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_Aux; use Sem_Aux;
53 with Sem_Cat; use Sem_Cat;
54 with Sem_Ch3; use Sem_Ch3;
55 with Sem_Ch4; use Sem_Ch4;
56 with Sem_Ch5; use Sem_Ch5;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch10; use Sem_Ch10;
59 with Sem_Ch12; use Sem_Ch12;
60 with Sem_Disp; use Sem_Disp;
61 with Sem_Dist; use Sem_Dist;
62 with Sem_Elim; use Sem_Elim;
63 with Sem_Eval; use Sem_Eval;
64 with Sem_Mech; use Sem_Mech;
65 with Sem_Prag; use Sem_Prag;
66 with Sem_Res; use Sem_Res;
67 with Sem_Util; use Sem_Util;
68 with Sem_Type; use Sem_Type;
69 with Sem_Warn; use Sem_Warn;
70 with Sinput; use Sinput;
71 with Stand; use Stand;
72 with Sinfo; use Sinfo;
73 with Sinfo.CN; use Sinfo.CN;
74 with Snames; use Snames;
75 with Stringt; use Stringt;
77 with Stylesw; use Stylesw;
78 with Tbuild; use Tbuild;
79 with Uintp; use Uintp;
80 with Urealp; use Urealp;
81 with Validsw; use Validsw;
83 package body Sem_Ch6 is
85 May_Hide_Profile : Boolean := False;
86 -- This flag is used to indicate that two formals in two subprograms being
87 -- checked for conformance differ only in that one is an access parameter
88 -- while the other is of a general access type with the same designated
89 -- type. In this case, if the rest of the signatures match, a call to
90 -- either subprogram may be ambiguous, which is worth a warning. The flag
91 -- is set in Compatible_Types, and the warning emitted in
92 -- New_Overloaded_Entity.
94 -----------------------
95 -- Local Subprograms --
96 -----------------------
98 procedure Analyze_Return_Statement (N : Node_Id);
99 -- Common processing for simple_ and extended_return_statements
101 procedure Analyze_Function_Return (N : Node_Id);
102 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
103 -- applies to a [generic] function.
105 procedure Analyze_Return_Type (N : Node_Id);
106 -- Subsidiary to Process_Formals: analyze subtype mark in function
107 -- specification, in a context where the formals are visible and hide
110 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
111 -- Analyze a generic subprogram body. N is the body to be analyzed, and
112 -- Gen_Id is the defining entity Id for the corresponding spec.
114 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
115 -- If a subprogram has pragma Inline and inlining is active, use generic
116 -- machinery to build an unexpanded body for the subprogram. This body is
117 -- subsequently used for inline expansions at call sites. If subprogram can
118 -- be inlined (depending on size and nature of local declarations) this
119 -- function returns true. Otherwise subprogram body is treated normally.
120 -- If proper warnings are enabled and the subprogram contains a construct
121 -- that cannot be inlined, the offending construct is flagged accordingly.
123 procedure Check_Conformance
126 Ctype : Conformance_Type;
128 Conforms : out Boolean;
129 Err_Loc : Node_Id := Empty;
130 Get_Inst : Boolean := False;
131 Skip_Controlling_Formals : Boolean := False);
132 -- Given two entities, this procedure checks that the profiles associated
133 -- with these entities meet the conformance criterion given by the third
134 -- parameter. If they conform, Conforms is set True and control returns
135 -- to the caller. If they do not conform, Conforms is set to False, and
136 -- in addition, if Errmsg is True on the call, proper messages are output
137 -- to complain about the conformance failure. If Err_Loc is non_Empty
138 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
139 -- error messages are placed on the appropriate part of the construct
140 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
141 -- against a formal access-to-subprogram type so Get_Instance_Of must
144 procedure Check_Subprogram_Order (N : Node_Id);
145 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
146 -- the alpha ordering rule for N if this ordering requirement applicable.
148 procedure Check_Returns
152 Proc : Entity_Id := Empty);
153 -- Called to check for missing return statements in a function body, or for
154 -- returns present in a procedure body which has No_Return set. HSS is the
155 -- handled statement sequence for the subprogram body. This procedure
156 -- checks all flow paths to make sure they either have return (Mode = 'F',
157 -- used for functions) or do not have a return (Mode = 'P', used for
158 -- No_Return procedures). The flag Err is set if there are any control
159 -- paths not explicitly terminated by a return in the function case, and is
160 -- True otherwise. Proc is the entity for the procedure case and is used
161 -- in posting the warning message.
163 procedure Enter_Overloaded_Entity (S : Entity_Id);
164 -- This procedure makes S, a new overloaded entity, into the first visible
165 -- entity with that name.
167 procedure Install_Entity (E : Entity_Id);
168 -- Make single entity visible. Used for generic formals as well
170 function Is_Non_Overriding_Operation
172 New_E : Entity_Id) return Boolean;
173 -- Enforce the rule given in 12.3(18): a private operation in an instance
174 -- overrides an inherited operation only if the corresponding operation
175 -- was overriding in the generic. This can happen for primitive operations
176 -- of types derived (in the generic unit) from formal private or formal
179 procedure Make_Inequality_Operator (S : Entity_Id);
180 -- Create the declaration for an inequality operator that is implicitly
181 -- created by a user-defined equality operator that yields a boolean.
183 procedure May_Need_Actuals (Fun : Entity_Id);
184 -- Flag functions that can be called without parameters, i.e. those that
185 -- have no parameters, or those for which defaults exist for all parameters
187 procedure Process_PPCs
190 Body_Id : Entity_Id);
191 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
192 -- conditions for the body and assembling and inserting the _postconditions
193 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
194 -- the entities for the body and separate spec (if there is no separate
195 -- spec, Spec_Id is Empty).
197 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
198 -- Formal_Id is an formal parameter entity. This procedure deals with
199 -- setting the proper validity status for this entity, which depends
200 -- on the kind of parameter and the validity checking mode.
202 ------------------------------
203 -- Analyze_Return_Statement --
204 ------------------------------
206 procedure Analyze_Return_Statement (N : Node_Id) is
208 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
209 N_Extended_Return_Statement));
211 Returns_Object : constant Boolean :=
212 Nkind (N) = N_Extended_Return_Statement
214 (Nkind (N) = N_Simple_Return_Statement
215 and then Present (Expression (N)));
216 -- True if we're returning something; that is, "return <expression>;"
217 -- or "return Result : T [:= ...]". False for "return;". Used for error
218 -- checking: If Returns_Object is True, N should apply to a function
219 -- body; otherwise N should apply to a procedure body, entry body,
220 -- accept statement, or extended return statement.
222 function Find_What_It_Applies_To return Entity_Id;
223 -- Find the entity representing the innermost enclosing body, accept
224 -- statement, or extended return statement. If the result is a callable
225 -- construct or extended return statement, then this will be the value
226 -- of the Return_Applies_To attribute. Otherwise, the program is
227 -- illegal. See RM-6.5(4/2).
229 -----------------------------
230 -- Find_What_It_Applies_To --
231 -----------------------------
233 function Find_What_It_Applies_To return Entity_Id is
234 Result : Entity_Id := Empty;
237 -- Loop outward through the Scope_Stack, skipping blocks and loops
239 for J in reverse 0 .. Scope_Stack.Last loop
240 Result := Scope_Stack.Table (J).Entity;
241 exit when Ekind (Result) /= E_Block and then
242 Ekind (Result) /= E_Loop;
245 pragma Assert (Present (Result));
247 end Find_What_It_Applies_To;
249 -- Local declarations
251 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
252 Kind : constant Entity_Kind := Ekind (Scope_Id);
253 Loc : constant Source_Ptr := Sloc (N);
254 Stm_Entity : constant Entity_Id :=
256 (E_Return_Statement, Current_Scope, Loc, 'R');
258 -- Start of processing for Analyze_Return_Statement
261 Set_Return_Statement_Entity (N, Stm_Entity);
263 Set_Etype (Stm_Entity, Standard_Void_Type);
264 Set_Return_Applies_To (Stm_Entity, Scope_Id);
266 -- Place Return entity on scope stack, to simplify enforcement of 6.5
267 -- (4/2): an inner return statement will apply to this extended return.
269 if Nkind (N) = N_Extended_Return_Statement then
270 Push_Scope (Stm_Entity);
273 -- Check that pragma No_Return is obeyed. Don't complain about the
274 -- implicitly-generated return that is placed at the end.
276 if No_Return (Scope_Id) and then Comes_From_Source (N) then
277 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
280 -- Warn on any unassigned OUT parameters if in procedure
282 if Ekind (Scope_Id) = E_Procedure then
283 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
286 -- Check that functions return objects, and other things do not
288 if Kind = E_Function or else Kind = E_Generic_Function then
289 if not Returns_Object then
290 Error_Msg_N ("missing expression in return from function", N);
293 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
294 if Returns_Object then
295 Error_Msg_N ("procedure cannot return value (use function)", N);
298 elsif Kind = E_Entry or else Kind = E_Entry_Family then
299 if Returns_Object then
300 if Is_Protected_Type (Scope (Scope_Id)) then
301 Error_Msg_N ("entry body cannot return value", N);
303 Error_Msg_N ("accept statement cannot return value", N);
307 elsif Kind = E_Return_Statement then
309 -- We are nested within another return statement, which must be an
310 -- extended_return_statement.
312 if Returns_Object then
314 ("extended_return_statement cannot return value; " &
315 "use `""RETURN;""`", N);
319 Error_Msg_N ("illegal context for return statement", N);
322 if Kind = E_Function or else Kind = E_Generic_Function then
323 Analyze_Function_Return (N);
326 if Nkind (N) = N_Extended_Return_Statement then
330 Kill_Current_Values (Last_Assignment_Only => True);
331 Check_Unreachable_Code (N);
332 end Analyze_Return_Statement;
334 ---------------------------------------------
335 -- Analyze_Abstract_Subprogram_Declaration --
336 ---------------------------------------------
338 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
339 Designator : constant Entity_Id :=
340 Analyze_Subprogram_Specification (Specification (N));
341 Scop : constant Entity_Id := Current_Scope;
344 Generate_Definition (Designator);
345 Set_Is_Abstract_Subprogram (Designator);
346 New_Overloaded_Entity (Designator);
347 Check_Delayed_Subprogram (Designator);
349 Set_Categorization_From_Scope (Designator, Scop);
351 if Ekind (Scope (Designator)) = E_Protected_Type then
353 ("abstract subprogram not allowed in protected type", N);
355 -- Issue a warning if the abstract subprogram is neither a dispatching
356 -- operation nor an operation that overrides an inherited subprogram or
357 -- predefined operator, since this most likely indicates a mistake.
359 elsif Warn_On_Redundant_Constructs
360 and then not Is_Dispatching_Operation (Designator)
361 and then not Is_Overriding_Operation (Designator)
362 and then (not Is_Operator_Symbol_Name (Chars (Designator))
363 or else Scop /= Scope (Etype (First_Formal (Designator))))
366 ("?abstract subprogram is not dispatching or overriding", N);
369 Generate_Reference_To_Formals (Designator);
370 end Analyze_Abstract_Subprogram_Declaration;
372 ----------------------------------------
373 -- Analyze_Extended_Return_Statement --
374 ----------------------------------------
376 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
378 Analyze_Return_Statement (N);
379 end Analyze_Extended_Return_Statement;
381 ----------------------------
382 -- Analyze_Function_Call --
383 ----------------------------
385 procedure Analyze_Function_Call (N : Node_Id) is
386 P : constant Node_Id := Name (N);
387 L : constant List_Id := Parameter_Associations (N);
393 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
394 -- as B (A, X). If the rewriting is successful, the call has been
395 -- analyzed and we just return.
397 if Nkind (P) = N_Selected_Component
398 and then Name (N) /= P
399 and then Is_Rewrite_Substitution (N)
400 and then Present (Etype (N))
405 -- If error analyzing name, then set Any_Type as result type and return
407 if Etype (P) = Any_Type then
408 Set_Etype (N, Any_Type);
412 -- Otherwise analyze the parameters
416 while Present (Actual) loop
418 Check_Parameterless_Call (Actual);
424 end Analyze_Function_Call;
426 -----------------------------
427 -- Analyze_Function_Return --
428 -----------------------------
430 procedure Analyze_Function_Return (N : Node_Id) is
431 Loc : constant Source_Ptr := Sloc (N);
432 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
433 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
435 R_Type : constant Entity_Id := Etype (Scope_Id);
436 -- Function result subtype
438 procedure Check_Limited_Return (Expr : Node_Id);
439 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
440 -- limited types. Used only for simple return statements.
441 -- Expr is the expression returned.
443 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
444 -- Check that the return_subtype_indication properly matches the result
445 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
447 --------------------------
448 -- Check_Limited_Return --
449 --------------------------
451 procedure Check_Limited_Return (Expr : Node_Id) is
453 -- Ada 2005 (AI-318-02): Return-by-reference types have been
454 -- removed and replaced by anonymous access results. This is an
455 -- incompatibility with Ada 95. Not clear whether this should be
456 -- enforced yet or perhaps controllable with special switch. ???
458 if Is_Limited_Type (R_Type)
459 and then Comes_From_Source (N)
460 and then not In_Instance_Body
461 and then not OK_For_Limited_Init_In_05 (Expr)
465 if Ada_Version >= Ada_05
466 and then not Debug_Flag_Dot_L
467 and then not GNAT_Mode
470 ("(Ada 2005) cannot copy object of a limited type " &
471 "(RM-2005 6.5(5.5/2))", Expr);
472 if Is_Inherently_Limited_Type (R_Type) then
474 ("\return by reference not permitted in Ada 2005", Expr);
477 -- Warn in Ada 95 mode, to give folks a heads up about this
480 -- In GNAT mode, this is just a warning, to allow it to be
481 -- evilly turned off. Otherwise it is a real error.
483 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
484 if Is_Inherently_Limited_Type (R_Type) then
486 ("return by reference not permitted in Ada 2005 " &
487 "(RM-2005 6.5(5.5/2))?", Expr);
490 ("cannot copy object of a limited type in Ada 2005 " &
491 "(RM-2005 6.5(5.5/2))?", Expr);
494 -- Ada 95 mode, compatibility warnings disabled
497 return; -- skip continuation messages below
501 ("\consider switching to return of access type", Expr);
502 Explain_Limited_Type (R_Type, Expr);
504 end Check_Limited_Return;
506 -------------------------------------
507 -- Check_Return_Subtype_Indication --
508 -------------------------------------
510 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
511 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
512 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
513 -- Subtype given in the extended return statement;
514 -- this must match R_Type.
516 Subtype_Ind : constant Node_Id :=
517 Object_Definition (Original_Node (Obj_Decl));
519 R_Type_Is_Anon_Access :
521 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
523 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
525 Ekind (R_Type) = E_Anonymous_Access_Type;
526 -- True if return type of the function is an anonymous access type
527 -- Can't we make Is_Anonymous_Access_Type in einfo ???
529 R_Stm_Type_Is_Anon_Access :
531 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
533 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
535 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
536 -- True if type of the return object is an anonymous access type
539 -- First, avoid cascade errors:
541 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
545 -- "return access T" case; check that the return statement also has
546 -- "access T", and that the subtypes statically match:
547 -- if this is an access to subprogram the signatures must match.
549 if R_Type_Is_Anon_Access then
550 if R_Stm_Type_Is_Anon_Access then
552 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
554 if Base_Type (Designated_Type (R_Stm_Type)) /=
555 Base_Type (Designated_Type (R_Type))
556 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
559 ("subtype must statically match function result subtype",
560 Subtype_Mark (Subtype_Ind));
564 -- For two anonymous access to subprogram types, the
565 -- types themselves must be type conformant.
567 if not Conforming_Types
568 (R_Stm_Type, R_Type, Fully_Conformant)
571 ("subtype must statically match function result subtype",
577 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
580 -- Subtype_indication case; check that the types are the same, and
581 -- statically match if appropriate. A null exclusion may be present
582 -- on the return type, on the function specification, on the object
583 -- declaration or on the subtype itself.
585 elsif Base_Type (R_Stm_Type) = Base_Type (R_Type) then
586 if Is_Access_Type (R_Type)
588 (Can_Never_Be_Null (R_Type)
589 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
590 Can_Never_Be_Null (R_Stm_Type)
593 ("subtype must statically match function result subtype",
597 if Is_Constrained (R_Type) then
598 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
600 ("subtype must statically match function result subtype",
605 -- If the function's result type doesn't match the return object
606 -- entity's type, then we check for the case where the result type
607 -- is class-wide, and allow the declaration if the type of the object
608 -- definition matches the class-wide type. This prevents rejection
609 -- in the case where the object declaration is initialized by a call
610 -- to a build-in-place function with a specific result type and the
611 -- object entity had its type changed to that specific type. This is
612 -- also allowed in the case where Obj_Decl does not come from source,
613 -- which can occur for an expansion of a simple return statement of
614 -- a build-in-place class-wide function when the result expression
615 -- has a specific type, because a return object with a specific type
616 -- is created. (Note that the ARG believes that return objects should
617 -- be allowed to have a type covered by a class-wide result type in
618 -- any case, so once that relaxation is made (see AI05-32), the above
619 -- check for type compatibility should be changed to test Covers
620 -- rather than equality, and the following special test will no
621 -- longer be needed. ???)
623 elsif Is_Class_Wide_Type (R_Type)
625 (R_Type = Etype (Object_Definition (Original_Node (Obj_Decl)))
626 or else not Comes_From_Source (Obj_Decl))
632 ("wrong type for return_subtype_indication", Subtype_Ind);
634 end Check_Return_Subtype_Indication;
636 ---------------------
637 -- Local Variables --
638 ---------------------
642 -- Start of processing for Analyze_Function_Return
645 Set_Return_Present (Scope_Id);
647 if Nkind (N) = N_Simple_Return_Statement then
648 Expr := Expression (N);
649 Analyze_And_Resolve (Expr, R_Type);
650 Check_Limited_Return (Expr);
653 -- Analyze parts specific to extended_return_statement:
656 Obj_Decl : constant Node_Id :=
657 Last (Return_Object_Declarations (N));
659 HSS : constant Node_Id := Handled_Statement_Sequence (N);
662 Expr := Expression (Obj_Decl);
664 -- Note: The check for OK_For_Limited_Init will happen in
665 -- Analyze_Object_Declaration; we treat it as a normal
666 -- object declaration.
668 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
671 Check_Return_Subtype_Indication (Obj_Decl);
673 if Present (HSS) then
676 if Present (Exception_Handlers (HSS)) then
678 -- ???Has_Nested_Block_With_Handler needs to be set.
679 -- Probably by creating an actual N_Block_Statement.
680 -- Probably in Expand.
686 Check_References (Stm_Entity);
690 -- Case of Expr present
694 -- Defend against previous errors
696 and then Nkind (Expr) /= N_Empty
697 and then Present (Etype (Expr))
699 -- Apply constraint check. Note that this is done before the implicit
700 -- conversion of the expression done for anonymous access types to
701 -- ensure correct generation of the null-excluding check associated
702 -- with null-excluding expressions found in return statements.
704 Apply_Constraint_Check (Expr, R_Type);
706 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
707 -- type, apply an implicit conversion of the expression to that type
708 -- to force appropriate static and run-time accessibility checks.
710 if Ada_Version >= Ada_05
711 and then Ekind (R_Type) = E_Anonymous_Access_Type
713 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
714 Analyze_And_Resolve (Expr, R_Type);
717 -- If the result type is class-wide, then check that the return
718 -- expression's type is not declared at a deeper level than the
719 -- function (RM05-6.5(5.6/2)).
721 if Ada_Version >= Ada_05
722 and then Is_Class_Wide_Type (R_Type)
724 if Type_Access_Level (Etype (Expr)) >
725 Subprogram_Access_Level (Scope_Id)
728 ("level of return expression type is deeper than " &
729 "class-wide function!", Expr);
733 if (Is_Class_Wide_Type (Etype (Expr))
734 or else Is_Dynamically_Tagged (Expr))
735 and then not Is_Class_Wide_Type (R_Type)
738 ("dynamically tagged expression not allowed!", Expr);
741 -- ??? A real run-time accessibility check is needed in cases
742 -- involving dereferences of access parameters. For now we just
743 -- check the static cases.
745 if (Ada_Version < Ada_05 or else Debug_Flag_Dot_L)
746 and then Is_Inherently_Limited_Type (Etype (Scope_Id))
747 and then Object_Access_Level (Expr) >
748 Subprogram_Access_Level (Scope_Id)
751 Make_Raise_Program_Error (Loc,
752 Reason => PE_Accessibility_Check_Failed));
756 ("cannot return a local value by reference?", N);
758 ("\& will be raised at run time?",
759 N, Standard_Program_Error);
763 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
764 and then Null_Exclusion_Present (Parent (Scope_Id))
766 Apply_Compile_Time_Constraint_Error
768 Msg => "(Ada 2005) null not allowed for "
769 & "null-excluding return?",
770 Reason => CE_Null_Not_Allowed);
773 end Analyze_Function_Return;
775 -------------------------------------
776 -- Analyze_Generic_Subprogram_Body --
777 -------------------------------------
779 procedure Analyze_Generic_Subprogram_Body
783 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
784 Kind : constant Entity_Kind := Ekind (Gen_Id);
790 -- Copy body and disable expansion while analyzing the generic For a
791 -- stub, do not copy the stub (which would load the proper body), this
792 -- will be done when the proper body is analyzed.
794 if Nkind (N) /= N_Subprogram_Body_Stub then
795 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
800 Spec := Specification (N);
802 -- Within the body of the generic, the subprogram is callable, and
803 -- behaves like the corresponding non-generic unit.
805 Body_Id := Defining_Entity (Spec);
807 if Kind = E_Generic_Procedure
808 and then Nkind (Spec) /= N_Procedure_Specification
810 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
813 elsif Kind = E_Generic_Function
814 and then Nkind (Spec) /= N_Function_Specification
816 Error_Msg_N ("invalid body for generic function ", Body_Id);
820 Set_Corresponding_Body (Gen_Decl, Body_Id);
822 if Has_Completion (Gen_Id)
823 and then Nkind (Parent (N)) /= N_Subunit
825 Error_Msg_N ("duplicate generic body", N);
828 Set_Has_Completion (Gen_Id);
831 if Nkind (N) = N_Subprogram_Body_Stub then
832 Set_Ekind (Defining_Entity (Specification (N)), Kind);
834 Set_Corresponding_Spec (N, Gen_Id);
837 if Nkind (Parent (N)) = N_Compilation_Unit then
838 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
841 -- Make generic parameters immediately visible in the body. They are
842 -- needed to process the formals declarations. Then make the formals
843 -- visible in a separate step.
849 First_Ent : Entity_Id;
852 First_Ent := First_Entity (Gen_Id);
855 while Present (E) and then not Is_Formal (E) loop
860 Set_Use (Generic_Formal_Declarations (Gen_Decl));
862 -- Now generic formals are visible, and the specification can be
863 -- analyzed, for subsequent conformance check.
865 Body_Id := Analyze_Subprogram_Specification (Spec);
867 -- Make formal parameters visible
871 -- E is the first formal parameter, we loop through the formals
872 -- installing them so that they will be visible.
874 Set_First_Entity (Gen_Id, E);
875 while Present (E) loop
881 -- Visible generic entity is callable within its own body
883 Set_Ekind (Gen_Id, Ekind (Body_Id));
884 Set_Ekind (Body_Id, E_Subprogram_Body);
885 Set_Convention (Body_Id, Convention (Gen_Id));
886 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
887 Set_Scope (Body_Id, Scope (Gen_Id));
888 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
890 if Nkind (N) = N_Subprogram_Body_Stub then
892 -- No body to analyze, so restore state of generic unit
894 Set_Ekind (Gen_Id, Kind);
895 Set_Ekind (Body_Id, Kind);
897 if Present (First_Ent) then
898 Set_First_Entity (Gen_Id, First_Ent);
905 -- If this is a compilation unit, it must be made visible explicitly,
906 -- because the compilation of the declaration, unlike other library
907 -- unit declarations, does not. If it is not a unit, the following
908 -- is redundant but harmless.
910 Set_Is_Immediately_Visible (Gen_Id);
911 Reference_Body_Formals (Gen_Id, Body_Id);
913 if Is_Child_Unit (Gen_Id) then
914 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
917 Set_Actual_Subtypes (N, Current_Scope);
918 Process_PPCs (N, Gen_Id, Body_Id);
920 -- If the generic unit carries pre- or post-conditions, copy them
921 -- to the original generic tree, so that they are properly added
922 -- to any instantiation.
925 Orig : constant Node_Id := Original_Node (N);
929 Cond := First (Declarations (N));
930 while Present (Cond) loop
931 if Nkind (Cond) = N_Pragma
932 and then Pragma_Name (Cond) = Name_Check
934 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
936 elsif Nkind (Cond) = N_Pragma
937 and then Pragma_Name (Cond) = Name_Postcondition
939 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
940 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
949 Analyze_Declarations (Declarations (N));
951 Analyze (Handled_Statement_Sequence (N));
953 Save_Global_References (Original_Node (N));
955 -- Prior to exiting the scope, include generic formals again (if any
956 -- are present) in the set of local entities.
958 if Present (First_Ent) then
959 Set_First_Entity (Gen_Id, First_Ent);
962 Check_References (Gen_Id);
965 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
967 Check_Subprogram_Order (N);
969 -- Outside of its body, unit is generic again
971 Set_Ekind (Gen_Id, Kind);
972 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
975 Style.Check_Identifier (Body_Id, Gen_Id);
978 end Analyze_Generic_Subprogram_Body;
980 -----------------------------
981 -- Analyze_Operator_Symbol --
982 -----------------------------
984 -- An operator symbol such as "+" or "and" may appear in context where the
985 -- literal denotes an entity name, such as "+"(x, y) or in context when it
986 -- is just a string, as in (conjunction = "or"). In these cases the parser
987 -- generates this node, and the semantics does the disambiguation. Other
988 -- such case are actuals in an instantiation, the generic unit in an
989 -- instantiation, and pragma arguments.
991 procedure Analyze_Operator_Symbol (N : Node_Id) is
992 Par : constant Node_Id := Parent (N);
995 if (Nkind (Par) = N_Function_Call
996 and then N = Name (Par))
997 or else Nkind (Par) = N_Function_Instantiation
998 or else (Nkind (Par) = N_Indexed_Component
999 and then N = Prefix (Par))
1000 or else (Nkind (Par) = N_Pragma_Argument_Association
1001 and then not Is_Pragma_String_Literal (Par))
1002 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1003 or else (Nkind (Par) = N_Attribute_Reference
1004 and then Attribute_Name (Par) /= Name_Value)
1006 Find_Direct_Name (N);
1009 Change_Operator_Symbol_To_String_Literal (N);
1012 end Analyze_Operator_Symbol;
1014 -----------------------------------
1015 -- Analyze_Parameter_Association --
1016 -----------------------------------
1018 procedure Analyze_Parameter_Association (N : Node_Id) is
1020 Analyze (Explicit_Actual_Parameter (N));
1021 end Analyze_Parameter_Association;
1023 ----------------------------
1024 -- Analyze_Procedure_Call --
1025 ----------------------------
1027 procedure Analyze_Procedure_Call (N : Node_Id) is
1028 Loc : constant Source_Ptr := Sloc (N);
1029 P : constant Node_Id := Name (N);
1030 Actuals : constant List_Id := Parameter_Associations (N);
1034 procedure Analyze_Call_And_Resolve;
1035 -- Do Analyze and Resolve calls for procedure call
1037 ------------------------------
1038 -- Analyze_Call_And_Resolve --
1039 ------------------------------
1041 procedure Analyze_Call_And_Resolve is
1043 if Nkind (N) = N_Procedure_Call_Statement then
1045 Resolve (N, Standard_Void_Type);
1049 end Analyze_Call_And_Resolve;
1051 -- Start of processing for Analyze_Procedure_Call
1054 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1055 -- a procedure call or an entry call. The prefix may denote an access
1056 -- to subprogram type, in which case an implicit dereference applies.
1057 -- If the prefix is an indexed component (without implicit dereference)
1058 -- then the construct denotes a call to a member of an entire family.
1059 -- If the prefix is a simple name, it may still denote a call to a
1060 -- parameterless member of an entry family. Resolution of these various
1061 -- interpretations is delicate.
1065 -- If this is a call of the form Obj.Op, the call may have been
1066 -- analyzed and possibly rewritten into a block, in which case
1069 if Analyzed (N) then
1073 -- If error analyzing prefix, then set Any_Type as result and return
1075 if Etype (P) = Any_Type then
1076 Set_Etype (N, Any_Type);
1080 -- Otherwise analyze the parameters
1082 if Present (Actuals) then
1083 Actual := First (Actuals);
1085 while Present (Actual) loop
1087 Check_Parameterless_Call (Actual);
1092 -- Special processing for Elab_Spec and Elab_Body calls
1094 if Nkind (P) = N_Attribute_Reference
1095 and then (Attribute_Name (P) = Name_Elab_Spec
1096 or else Attribute_Name (P) = Name_Elab_Body)
1098 if Present (Actuals) then
1100 ("no parameters allowed for this call", First (Actuals));
1104 Set_Etype (N, Standard_Void_Type);
1107 elsif Is_Entity_Name (P)
1108 and then Is_Record_Type (Etype (Entity (P)))
1109 and then Remote_AST_I_Dereference (P)
1113 elsif Is_Entity_Name (P)
1114 and then Ekind (Entity (P)) /= E_Entry_Family
1116 if Is_Access_Type (Etype (P))
1117 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1118 and then No (Actuals)
1119 and then Comes_From_Source (N)
1121 Error_Msg_N ("missing explicit dereference in call", N);
1124 Analyze_Call_And_Resolve;
1126 -- If the prefix is the simple name of an entry family, this is
1127 -- a parameterless call from within the task body itself.
1129 elsif Is_Entity_Name (P)
1130 and then Nkind (P) = N_Identifier
1131 and then Ekind (Entity (P)) = E_Entry_Family
1132 and then Present (Actuals)
1133 and then No (Next (First (Actuals)))
1135 -- Can be call to parameterless entry family. What appears to be the
1136 -- sole argument is in fact the entry index. Rewrite prefix of node
1137 -- accordingly. Source representation is unchanged by this
1141 Make_Indexed_Component (Loc,
1143 Make_Selected_Component (Loc,
1144 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1145 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1146 Expressions => Actuals);
1147 Set_Name (N, New_N);
1148 Set_Etype (New_N, Standard_Void_Type);
1149 Set_Parameter_Associations (N, No_List);
1150 Analyze_Call_And_Resolve;
1152 elsif Nkind (P) = N_Explicit_Dereference then
1153 if Ekind (Etype (P)) = E_Subprogram_Type then
1154 Analyze_Call_And_Resolve;
1156 Error_Msg_N ("expect access to procedure in call", P);
1159 -- The name can be a selected component or an indexed component that
1160 -- yields an access to subprogram. Such a prefix is legal if the call
1161 -- has parameter associations.
1163 elsif Is_Access_Type (Etype (P))
1164 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1166 if Present (Actuals) then
1167 Analyze_Call_And_Resolve;
1169 Error_Msg_N ("missing explicit dereference in call ", N);
1172 -- If not an access to subprogram, then the prefix must resolve to the
1173 -- name of an entry, entry family, or protected operation.
1175 -- For the case of a simple entry call, P is a selected component where
1176 -- the prefix is the task and the selector name is the entry. A call to
1177 -- a protected procedure will have the same syntax. If the protected
1178 -- object contains overloaded operations, the entity may appear as a
1179 -- function, the context will select the operation whose type is Void.
1181 elsif Nkind (P) = N_Selected_Component
1182 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1184 Ekind (Entity (Selector_Name (P))) = E_Procedure
1186 Ekind (Entity (Selector_Name (P))) = E_Function)
1188 Analyze_Call_And_Resolve;
1190 elsif Nkind (P) = N_Selected_Component
1191 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1192 and then Present (Actuals)
1193 and then No (Next (First (Actuals)))
1195 -- Can be call to parameterless entry family. What appears to be the
1196 -- sole argument is in fact the entry index. Rewrite prefix of node
1197 -- accordingly. Source representation is unchanged by this
1201 Make_Indexed_Component (Loc,
1202 Prefix => New_Copy (P),
1203 Expressions => Actuals);
1204 Set_Name (N, New_N);
1205 Set_Etype (New_N, Standard_Void_Type);
1206 Set_Parameter_Associations (N, No_List);
1207 Analyze_Call_And_Resolve;
1209 -- For the case of a reference to an element of an entry family, P is
1210 -- an indexed component whose prefix is a selected component (task and
1211 -- entry family), and whose index is the entry family index.
1213 elsif Nkind (P) = N_Indexed_Component
1214 and then Nkind (Prefix (P)) = N_Selected_Component
1215 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1217 Analyze_Call_And_Resolve;
1219 -- If the prefix is the name of an entry family, it is a call from
1220 -- within the task body itself.
1222 elsif Nkind (P) = N_Indexed_Component
1223 and then Nkind (Prefix (P)) = N_Identifier
1224 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1227 Make_Selected_Component (Loc,
1228 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1229 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1230 Rewrite (Prefix (P), New_N);
1232 Analyze_Call_And_Resolve;
1234 -- Anything else is an error
1237 Error_Msg_N ("invalid procedure or entry call", N);
1239 end Analyze_Procedure_Call;
1241 -------------------------------------
1242 -- Analyze_Simple_Return_Statement --
1243 -------------------------------------
1245 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1247 if Present (Expression (N)) then
1248 Mark_Coextensions (N, Expression (N));
1251 Analyze_Return_Statement (N);
1252 end Analyze_Simple_Return_Statement;
1254 -------------------------
1255 -- Analyze_Return_Type --
1256 -------------------------
1258 procedure Analyze_Return_Type (N : Node_Id) is
1259 Designator : constant Entity_Id := Defining_Entity (N);
1260 Typ : Entity_Id := Empty;
1263 -- Normal case where result definition does not indicate an error
1265 if Result_Definition (N) /= Error then
1266 if Nkind (Result_Definition (N)) = N_Access_Definition then
1268 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1271 AD : constant Node_Id :=
1272 Access_To_Subprogram_Definition (Result_Definition (N));
1274 if Present (AD) and then Protected_Present (AD) then
1275 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1277 Typ := Access_Definition (N, Result_Definition (N));
1281 Set_Parent (Typ, Result_Definition (N));
1282 Set_Is_Local_Anonymous_Access (Typ);
1283 Set_Etype (Designator, Typ);
1285 -- Subtype_Mark case
1288 Find_Type (Result_Definition (N));
1289 Typ := Entity (Result_Definition (N));
1290 Set_Etype (Designator, Typ);
1292 if Ekind (Typ) = E_Incomplete_Type
1293 and then Is_Value_Type (Typ)
1297 elsif Ekind (Typ) = E_Incomplete_Type
1298 or else (Is_Class_Wide_Type (Typ)
1300 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1303 ("invalid use of incomplete type", Result_Definition (N));
1307 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1309 Null_Exclusion_Static_Checks (N);
1311 -- Case where result definition does indicate an error
1314 Set_Etype (Designator, Any_Type);
1316 end Analyze_Return_Type;
1318 -----------------------------
1319 -- Analyze_Subprogram_Body --
1320 -----------------------------
1322 -- This procedure is called for regular subprogram bodies, generic bodies,
1323 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1324 -- specification matters, and is used to create a proper declaration for
1325 -- the subprogram, or to perform conformance checks.
1327 procedure Analyze_Subprogram_Body (N : Node_Id) is
1328 Loc : constant Source_Ptr := Sloc (N);
1329 Body_Deleted : constant Boolean := False;
1330 Body_Spec : constant Node_Id := Specification (N);
1331 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1332 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1333 Conformant : Boolean;
1335 Missing_Ret : Boolean;
1337 Prot_Typ : Entity_Id := Empty;
1338 Spec_Id : Entity_Id;
1339 Spec_Decl : Node_Id := Empty;
1341 Last_Real_Spec_Entity : Entity_Id := Empty;
1342 -- When we analyze a separate spec, the entity chain ends up containing
1343 -- the formals, as well as any itypes generated during analysis of the
1344 -- default expressions for parameters, or the arguments of associated
1345 -- precondition/postcondition pragmas (which are analyzed in the context
1346 -- of the spec since they have visibility on formals).
1348 -- These entities belong with the spec and not the body. However we do
1349 -- the analysis of the body in the context of the spec (again to obtain
1350 -- visibility to the formals), and all the entities generated during
1351 -- this analysis end up also chained to the entity chain of the spec.
1352 -- But they really belong to the body, and there is circuitry to move
1353 -- them from the spec to the body.
1355 -- However, when we do this move, we don't want to move the real spec
1356 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1357 -- variable points to the last real spec entity, so we only move those
1358 -- chained beyond that point. It is initialized to Empty to deal with
1359 -- the case where there is no separate spec.
1361 procedure Check_Anonymous_Return;
1362 -- (Ada 2005): if a function returns an access type that denotes a task,
1363 -- or a type that contains tasks, we must create a master entity for
1364 -- the anonymous type, which typically will be used in an allocator
1365 -- in the body of the function.
1367 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1368 -- Look ahead to recognize a pragma that may appear after the body.
1369 -- If there is a previous spec, check that it appears in the same
1370 -- declarative part. If the pragma is Inline_Always, perform inlining
1371 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1372 -- If the body acts as a spec, and inlining is required, we create a
1373 -- subprogram declaration for it, in order to attach the body to inline.
1374 -- If pragma does not appear after the body, check whether there is
1375 -- an inline pragma before any local declarations.
1377 function Disambiguate_Spec return Entity_Id;
1378 -- When a primitive is declared between the private view and the full
1379 -- view of a concurrent type which implements an interface, a special
1380 -- mechanism is used to find the corresponding spec of the primitive
1383 function Is_Private_Concurrent_Primitive
1384 (Subp_Id : Entity_Id) return Boolean;
1385 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1386 -- type that implements an interface and has a private view.
1388 procedure Set_Trivial_Subprogram (N : Node_Id);
1389 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1390 -- subprogram whose body is being analyzed. N is the statement node
1391 -- causing the flag to be set, if the following statement is a return
1392 -- of an entity, we mark the entity as set in source to suppress any
1393 -- warning on the stylized use of function stubs with a dummy return.
1395 procedure Verify_Overriding_Indicator;
1396 -- If there was a previous spec, the entity has been entered in the
1397 -- current scope previously. If the body itself carries an overriding
1398 -- indicator, check that it is consistent with the known status of the
1401 ----------------------------
1402 -- Check_Anonymous_Return --
1403 ----------------------------
1405 procedure Check_Anonymous_Return is
1410 if Present (Spec_Id) then
1416 if Ekind (Scop) = E_Function
1417 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1418 and then Has_Task (Designated_Type (Etype (Scop)))
1419 and then Expander_Active
1422 Make_Object_Declaration (Loc,
1423 Defining_Identifier =>
1424 Make_Defining_Identifier (Loc, Name_uMaster),
1425 Constant_Present => True,
1426 Object_Definition =>
1427 New_Reference_To (RTE (RE_Master_Id), Loc),
1429 Make_Explicit_Dereference (Loc,
1430 New_Reference_To (RTE (RE_Current_Master), Loc)));
1432 if Present (Declarations (N)) then
1433 Prepend (Decl, Declarations (N));
1435 Set_Declarations (N, New_List (Decl));
1438 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1439 Set_Has_Master_Entity (Scop);
1441 end Check_Anonymous_Return;
1443 -------------------------
1444 -- Check_Inline_Pragma --
1445 -------------------------
1447 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1451 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1452 -- True when N is a pragma Inline or Inline_Always that applies
1453 -- to this subprogram.
1455 -----------------------
1456 -- Is_Inline_Pragma --
1457 -----------------------
1459 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1462 Nkind (N) = N_Pragma
1464 (Pragma_Name (N) = Name_Inline_Always
1467 and then Pragma_Name (N) = Name_Inline))
1470 (Expression (First (Pragma_Argument_Associations (N))))
1472 end Is_Inline_Pragma;
1474 -- Start of processing for Check_Inline_Pragma
1477 if not Expander_Active then
1481 if Is_List_Member (N)
1482 and then Present (Next (N))
1483 and then Is_Inline_Pragma (Next (N))
1487 elsif Nkind (N) /= N_Subprogram_Body_Stub
1488 and then Present (Declarations (N))
1489 and then Is_Inline_Pragma (First (Declarations (N)))
1491 Prag := First (Declarations (N));
1497 if Present (Prag) then
1498 if Present (Spec_Id) then
1499 if List_Containing (N) =
1500 List_Containing (Unit_Declaration_Node (Spec_Id))
1506 -- Create a subprogram declaration, to make treatment uniform
1509 Subp : constant Entity_Id :=
1510 Make_Defining_Identifier (Loc, Chars (Body_Id));
1511 Decl : constant Node_Id :=
1512 Make_Subprogram_Declaration (Loc,
1513 Specification => New_Copy_Tree (Specification (N)));
1515 Set_Defining_Unit_Name (Specification (Decl), Subp);
1517 if Present (First_Formal (Body_Id)) then
1518 Plist := Copy_Parameter_List (Body_Id);
1519 Set_Parameter_Specifications
1520 (Specification (Decl), Plist);
1523 Insert_Before (N, Decl);
1526 Set_Has_Pragma_Inline (Subp);
1528 if Pragma_Name (Prag) = Name_Inline_Always then
1529 Set_Is_Inlined (Subp);
1530 Set_Has_Pragma_Inline_Always (Subp);
1537 end Check_Inline_Pragma;
1539 -----------------------
1540 -- Disambiguate_Spec --
1541 -----------------------
1543 function Disambiguate_Spec return Entity_Id is
1544 Priv_Spec : Entity_Id;
1547 procedure Replace_Types (To_Corresponding : Boolean);
1548 -- Depending on the flag, replace the type of formal parameters of
1549 -- Body_Id if it is a concurrent type implementing interfaces with
1550 -- the corresponding record type or the other way around.
1552 procedure Replace_Types (To_Corresponding : Boolean) is
1554 Formal_Typ : Entity_Id;
1557 Formal := First_Formal (Body_Id);
1558 while Present (Formal) loop
1559 Formal_Typ := Etype (Formal);
1561 -- From concurrent type to corresponding record
1563 if To_Corresponding then
1564 if Is_Concurrent_Type (Formal_Typ)
1565 and then Present (Corresponding_Record_Type (Formal_Typ))
1566 and then Present (Interfaces (
1567 Corresponding_Record_Type (Formal_Typ)))
1570 Corresponding_Record_Type (Formal_Typ));
1573 -- From corresponding record to concurrent type
1576 if Is_Concurrent_Record_Type (Formal_Typ)
1577 and then Present (Interfaces (Formal_Typ))
1580 Corresponding_Concurrent_Type (Formal_Typ));
1584 Next_Formal (Formal);
1588 -- Start of processing for Disambiguate_Spec
1591 -- Try to retrieve the specification of the body as is. All error
1592 -- messages are suppressed because the body may not have a spec in
1593 -- its current state.
1595 Spec_N := Find_Corresponding_Spec (N, False);
1597 -- It is possible that this is the body of a primitive declared
1598 -- between a private and a full view of a concurrent type. The
1599 -- controlling parameter of the spec carries the concurrent type,
1600 -- not the corresponding record type as transformed by Analyze_
1601 -- Subprogram_Specification. In such cases, we undo the change
1602 -- made by the analysis of the specification and try to find the
1605 -- Note that wrappers already have their corresponding specs and
1606 -- bodies set during their creation, so if the candidate spec is
1607 -- a wrapper, then we definitely need to swap all types to their
1608 -- original concurrent status.
1611 or else Is_Primitive_Wrapper (Spec_N)
1613 -- Restore all references of corresponding record types to the
1614 -- original concurrent types.
1616 Replace_Types (To_Corresponding => False);
1617 Priv_Spec := Find_Corresponding_Spec (N, False);
1619 -- The current body truly belongs to a primitive declared between
1620 -- a private and a full view. We leave the modified body as is,
1621 -- and return the true spec.
1623 if Present (Priv_Spec)
1624 and then Is_Private_Primitive (Priv_Spec)
1629 -- In case that this is some sort of error, restore the original
1630 -- state of the body.
1632 Replace_Types (To_Corresponding => True);
1636 end Disambiguate_Spec;
1638 -------------------------------------
1639 -- Is_Private_Concurrent_Primitive --
1640 -------------------------------------
1642 function Is_Private_Concurrent_Primitive
1643 (Subp_Id : Entity_Id) return Boolean
1645 Formal_Typ : Entity_Id;
1648 if Present (First_Formal (Subp_Id)) then
1649 Formal_Typ := Etype (First_Formal (Subp_Id));
1651 if Is_Concurrent_Record_Type (Formal_Typ) then
1652 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1655 -- The type of the first formal is a concurrent tagged type with
1659 Is_Concurrent_Type (Formal_Typ)
1660 and then Is_Tagged_Type (Formal_Typ)
1661 and then Has_Private_Declaration (Formal_Typ);
1665 end Is_Private_Concurrent_Primitive;
1667 ----------------------------
1668 -- Set_Trivial_Subprogram --
1669 ----------------------------
1671 procedure Set_Trivial_Subprogram (N : Node_Id) is
1672 Nxt : constant Node_Id := Next (N);
1675 Set_Is_Trivial_Subprogram (Body_Id);
1677 if Present (Spec_Id) then
1678 Set_Is_Trivial_Subprogram (Spec_Id);
1682 and then Nkind (Nxt) = N_Simple_Return_Statement
1683 and then No (Next (Nxt))
1684 and then Present (Expression (Nxt))
1685 and then Is_Entity_Name (Expression (Nxt))
1687 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1689 end Set_Trivial_Subprogram;
1691 ---------------------------------
1692 -- Verify_Overriding_Indicator --
1693 ---------------------------------
1695 procedure Verify_Overriding_Indicator is
1697 if Must_Override (Body_Spec) then
1698 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1699 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1703 elsif not Is_Overriding_Operation (Spec_Id) then
1705 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1708 elsif Must_Not_Override (Body_Spec) then
1709 if Is_Overriding_Operation (Spec_Id) then
1711 ("subprogram& overrides inherited operation",
1712 Body_Spec, Spec_Id);
1714 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1715 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1718 ("subprogram & overrides predefined operator ",
1719 Body_Spec, Spec_Id);
1721 -- If this is not a primitive operation the overriding indicator
1722 -- is altogether illegal.
1724 elsif not Is_Primitive (Spec_Id) then
1725 Error_Msg_N ("overriding indicator only allowed " &
1726 "if subprogram is primitive",
1731 and then Is_Overriding_Operation (Spec_Id)
1733 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
1734 Style.Missing_Overriding (N, Body_Id);
1736 end Verify_Overriding_Indicator;
1738 -- Start of processing for Analyze_Subprogram_Body
1741 if Debug_Flag_C then
1742 Write_Str ("==== Compiling subprogram body ");
1743 Write_Name (Chars (Body_Id));
1744 Write_Str (" from ");
1745 Write_Location (Loc);
1749 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1751 -- Generic subprograms are handled separately. They always have a
1752 -- generic specification. Determine whether current scope has a
1753 -- previous declaration.
1755 -- If the subprogram body is defined within an instance of the same
1756 -- name, the instance appears as a package renaming, and will be hidden
1757 -- within the subprogram.
1759 if Present (Prev_Id)
1760 and then not Is_Overloadable (Prev_Id)
1761 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
1762 or else Comes_From_Source (Prev_Id))
1764 if Is_Generic_Subprogram (Prev_Id) then
1766 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1767 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1769 Analyze_Generic_Subprogram_Body (N, Spec_Id);
1773 -- Previous entity conflicts with subprogram name. Attempting to
1774 -- enter name will post error.
1776 Enter_Name (Body_Id);
1780 -- Non-generic case, find the subprogram declaration, if one was seen,
1781 -- or enter new overloaded entity in the current scope. If the
1782 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
1783 -- part of the context of one of its subunits. No need to redo the
1786 elsif Prev_Id = Body_Id
1787 and then Has_Completion (Body_Id)
1792 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
1794 if Nkind (N) = N_Subprogram_Body_Stub
1795 or else No (Corresponding_Spec (N))
1797 if Is_Private_Concurrent_Primitive (Body_Id) then
1798 Spec_Id := Disambiguate_Spec;
1800 Spec_Id := Find_Corresponding_Spec (N);
1803 -- If this is a duplicate body, no point in analyzing it
1805 if Error_Posted (N) then
1809 -- A subprogram body should cause freezing of its own declaration,
1810 -- but if there was no previous explicit declaration, then the
1811 -- subprogram will get frozen too late (there may be code within
1812 -- the body that depends on the subprogram having been frozen,
1813 -- such as uses of extra formals), so we force it to be frozen
1814 -- here. Same holds if the body and spec are compilation units.
1815 -- Finally, if the return type is an anonymous access to protected
1816 -- subprogram, it must be frozen before the body because its
1817 -- expansion has generated an equivalent type that is used when
1818 -- elaborating the body.
1820 if No (Spec_Id) then
1821 Freeze_Before (N, Body_Id);
1823 elsif Nkind (Parent (N)) = N_Compilation_Unit then
1824 Freeze_Before (N, Spec_Id);
1826 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
1827 Freeze_Before (N, Etype (Body_Id));
1831 Spec_Id := Corresponding_Spec (N);
1835 -- Do not inline any subprogram that contains nested subprograms, since
1836 -- the backend inlining circuit seems to generate uninitialized
1837 -- references in this case. We know this happens in the case of front
1838 -- end ZCX support, but it also appears it can happen in other cases as
1839 -- well. The backend often rejects attempts to inline in the case of
1840 -- nested procedures anyway, so little if anything is lost by this.
1841 -- Note that this is test is for the benefit of the back-end. There is
1842 -- a separate test for front-end inlining that also rejects nested
1845 -- Do not do this test if errors have been detected, because in some
1846 -- error cases, this code blows up, and we don't need it anyway if
1847 -- there have been errors, since we won't get to the linker anyway.
1849 if Comes_From_Source (Body_Id)
1850 and then Serious_Errors_Detected = 0
1854 P_Ent := Scope (P_Ent);
1855 exit when No (P_Ent) or else P_Ent = Standard_Standard;
1857 if Is_Subprogram (P_Ent) then
1858 Set_Is_Inlined (P_Ent, False);
1860 if Comes_From_Source (P_Ent)
1861 and then Has_Pragma_Inline (P_Ent)
1864 ("cannot inline& (nested subprogram)?",
1871 Check_Inline_Pragma (Spec_Id);
1873 -- Deal with special case of a fully private operation in the body of
1874 -- the protected type. We must create a declaration for the subprogram,
1875 -- in order to attach the protected subprogram that will be used in
1876 -- internal calls. We exclude compiler generated bodies from the
1877 -- expander since the issue does not arise for those cases.
1880 and then Comes_From_Source (N)
1881 and then Is_Protected_Type (Current_Scope)
1890 Formal := First_Formal (Body_Id);
1892 -- The protected operation always has at least one formal, namely
1893 -- the object itself, but it is only placed in the parameter list
1894 -- if expansion is enabled.
1897 or else Expander_Active
1899 Plist := Copy_Parameter_List (Body_Id);
1904 if Nkind (Body_Spec) = N_Procedure_Specification then
1906 Make_Procedure_Specification (Loc,
1907 Defining_Unit_Name =>
1908 Make_Defining_Identifier (Sloc (Body_Id),
1909 Chars => Chars (Body_Id)),
1910 Parameter_Specifications => Plist);
1913 Make_Function_Specification (Loc,
1914 Defining_Unit_Name =>
1915 Make_Defining_Identifier (Sloc (Body_Id),
1916 Chars => Chars (Body_Id)),
1917 Parameter_Specifications => Plist,
1918 Result_Definition =>
1919 New_Occurrence_Of (Etype (Body_Id), Loc));
1923 Make_Subprogram_Declaration (Loc,
1924 Specification => New_Spec);
1925 Insert_Before (N, Decl);
1926 Spec_Id := Defining_Unit_Name (New_Spec);
1928 -- Indicate that the entity comes from source, to ensure that
1929 -- cross-reference information is properly generated. The body
1930 -- itself is rewritten during expansion, and the body entity will
1931 -- not appear in calls to the operation.
1933 Set_Comes_From_Source (Spec_Id, True);
1935 Set_Has_Completion (Spec_Id);
1936 Set_Convention (Spec_Id, Convention_Protected);
1940 -- If a separate spec is present, then deal with freezing issues
1942 if Present (Spec_Id) then
1943 Spec_Decl := Unit_Declaration_Node (Spec_Id);
1944 Verify_Overriding_Indicator;
1946 -- In general, the spec will be frozen when we start analyzing the
1947 -- body. However, for internally generated operations, such as
1948 -- wrapper functions for inherited operations with controlling
1949 -- results, the spec may not have been frozen by the time we
1950 -- expand the freeze actions that include the bodies. In particular,
1951 -- extra formals for accessibility or for return-in-place may need
1952 -- to be generated. Freeze nodes, if any, are inserted before the
1955 if not Is_Frozen (Spec_Id)
1956 and then Expander_Active
1958 -- Force the generation of its freezing node to ensure proper
1959 -- management of access types in the backend.
1961 -- This is definitely needed for some cases, but it is not clear
1962 -- why, to be investigated further???
1964 Set_Has_Delayed_Freeze (Spec_Id);
1965 Insert_Actions (N, Freeze_Entity (Spec_Id, Loc));
1969 -- Mark presence of postcondition proc in current scope
1971 if Chars (Body_Id) = Name_uPostconditions then
1972 Set_Has_Postconditions (Current_Scope);
1975 -- Place subprogram on scope stack, and make formals visible. If there
1976 -- is a spec, the visible entity remains that of the spec.
1978 if Present (Spec_Id) then
1979 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
1981 if Is_Child_Unit (Spec_Id) then
1982 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
1986 Style.Check_Identifier (Body_Id, Spec_Id);
1989 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
1990 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
1992 if Is_Abstract_Subprogram (Spec_Id) then
1993 Error_Msg_N ("an abstract subprogram cannot have a body", N);
1997 Set_Convention (Body_Id, Convention (Spec_Id));
1998 Set_Has_Completion (Spec_Id);
2000 if Is_Protected_Type (Scope (Spec_Id)) then
2001 Prot_Typ := Scope (Spec_Id);
2004 -- If this is a body generated for a renaming, do not check for
2005 -- full conformance. The check is redundant, because the spec of
2006 -- the body is a copy of the spec in the renaming declaration,
2007 -- and the test can lead to spurious errors on nested defaults.
2009 if Present (Spec_Decl)
2010 and then not Comes_From_Source (N)
2012 (Nkind (Original_Node (Spec_Decl)) =
2013 N_Subprogram_Renaming_Declaration
2014 or else (Present (Corresponding_Body (Spec_Decl))
2016 Nkind (Unit_Declaration_Node
2017 (Corresponding_Body (Spec_Decl))) =
2018 N_Subprogram_Renaming_Declaration))
2025 Fully_Conformant, True, Conformant, Body_Id);
2028 -- If the body is not fully conformant, we have to decide if we
2029 -- should analyze it or not. If it has a really messed up profile
2030 -- then we probably should not analyze it, since we will get too
2031 -- many bogus messages.
2033 -- Our decision is to go ahead in the non-fully conformant case
2034 -- only if it is at least mode conformant with the spec. Note
2035 -- that the call to Check_Fully_Conformant has issued the proper
2036 -- error messages to complain about the lack of conformance.
2039 and then not Mode_Conformant (Body_Id, Spec_Id)
2045 if Spec_Id /= Body_Id then
2046 Reference_Body_Formals (Spec_Id, Body_Id);
2049 if Nkind (N) /= N_Subprogram_Body_Stub then
2050 Set_Corresponding_Spec (N, Spec_Id);
2052 -- Ada 2005 (AI-345): If the operation is a primitive operation
2053 -- of a concurrent type, the type of the first parameter has been
2054 -- replaced with the corresponding record, which is the proper
2055 -- run-time structure to use. However, within the body there may
2056 -- be uses of the formals that depend on primitive operations
2057 -- of the type (in particular calls in prefixed form) for which
2058 -- we need the original concurrent type. The operation may have
2059 -- several controlling formals, so the replacement must be done
2062 if Comes_From_Source (Spec_Id)
2063 and then Present (First_Entity (Spec_Id))
2064 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2065 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2067 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2070 (Corresponding_Concurrent_Type
2071 (Etype (First_Entity (Spec_Id))))
2074 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2078 Form := First_Formal (Spec_Id);
2079 while Present (Form) loop
2080 if Etype (Form) = Typ then
2081 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2089 -- Make the formals visible, and place subprogram on scope stack.
2090 -- This is also the point at which we set Last_Real_Spec_Entity
2091 -- to mark the entities which will not be moved to the body.
2093 Install_Formals (Spec_Id);
2094 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2095 Push_Scope (Spec_Id);
2097 -- Make sure that the subprogram is immediately visible. For
2098 -- child units that have no separate spec this is indispensable.
2099 -- Otherwise it is safe albeit redundant.
2101 Set_Is_Immediately_Visible (Spec_Id);
2104 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2105 Set_Ekind (Body_Id, E_Subprogram_Body);
2106 Set_Scope (Body_Id, Scope (Spec_Id));
2107 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2109 -- Case of subprogram body with no previous spec
2113 and then Comes_From_Source (Body_Id)
2114 and then not Suppress_Style_Checks (Body_Id)
2115 and then not In_Instance
2117 Style.Body_With_No_Spec (N);
2120 New_Overloaded_Entity (Body_Id);
2122 if Nkind (N) /= N_Subprogram_Body_Stub then
2123 Set_Acts_As_Spec (N);
2124 Generate_Definition (Body_Id);
2126 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2127 Generate_Reference_To_Formals (Body_Id);
2128 Install_Formals (Body_Id);
2129 Push_Scope (Body_Id);
2133 -- If the return type is an anonymous access type whose designated type
2134 -- is the limited view of a class-wide type and the non-limited view is
2135 -- available, update the return type accordingly.
2137 if Ada_Version >= Ada_05
2138 and then Comes_From_Source (N)
2145 Rtyp := Etype (Current_Scope);
2147 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2148 Etyp := Directly_Designated_Type (Rtyp);
2150 if Is_Class_Wide_Type (Etyp)
2151 and then From_With_Type (Etyp)
2153 Set_Directly_Designated_Type
2154 (Etype (Current_Scope), Available_View (Etyp));
2160 -- If this is the proper body of a stub, we must verify that the stub
2161 -- conforms to the body, and to the previous spec if one was present.
2162 -- we know already that the body conforms to that spec. This test is
2163 -- only required for subprograms that come from source.
2165 if Nkind (Parent (N)) = N_Subunit
2166 and then Comes_From_Source (N)
2167 and then not Error_Posted (Body_Id)
2168 and then Nkind (Corresponding_Stub (Parent (N))) =
2169 N_Subprogram_Body_Stub
2172 Old_Id : constant Entity_Id :=
2174 (Specification (Corresponding_Stub (Parent (N))));
2176 Conformant : Boolean := False;
2179 if No (Spec_Id) then
2180 Check_Fully_Conformant (Body_Id, Old_Id);
2184 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2186 if not Conformant then
2188 -- The stub was taken to be a new declaration. Indicate
2189 -- that it lacks a body.
2191 Set_Has_Completion (Old_Id, False);
2197 Set_Has_Completion (Body_Id);
2198 Check_Eliminated (Body_Id);
2200 if Nkind (N) = N_Subprogram_Body_Stub then
2203 elsif Present (Spec_Id)
2204 and then Expander_Active
2206 (Has_Pragma_Inline_Always (Spec_Id)
2207 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2209 Build_Body_To_Inline (N, Spec_Id);
2212 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2213 -- if its specification we have to install the private withed units.
2214 -- This holds for child units as well.
2216 if Is_Compilation_Unit (Body_Id)
2217 or else Nkind (Parent (N)) = N_Compilation_Unit
2219 Install_Private_With_Clauses (Body_Id);
2222 Check_Anonymous_Return;
2224 -- Set the Protected_Formal field of each extra formal of the protected
2225 -- subprogram to reference the corresponding extra formal of the
2226 -- subprogram that implements it. For regular formals this occurs when
2227 -- the protected subprogram's declaration is expanded, but the extra
2228 -- formals don't get created until the subprogram is frozen. We need to
2229 -- do this before analyzing the protected subprogram's body so that any
2230 -- references to the original subprogram's extra formals will be changed
2231 -- refer to the implementing subprogram's formals (see Expand_Formal).
2233 if Present (Spec_Id)
2234 and then Is_Protected_Type (Scope (Spec_Id))
2235 and then Present (Protected_Body_Subprogram (Spec_Id))
2238 Impl_Subp : constant Entity_Id :=
2239 Protected_Body_Subprogram (Spec_Id);
2240 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2241 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2243 while Present (Prot_Ext_Formal) loop
2244 pragma Assert (Present (Impl_Ext_Formal));
2245 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2246 Next_Formal_With_Extras (Prot_Ext_Formal);
2247 Next_Formal_With_Extras (Impl_Ext_Formal);
2252 -- Now we can go on to analyze the body
2254 HSS := Handled_Statement_Sequence (N);
2255 Set_Actual_Subtypes (N, Current_Scope);
2257 -- Deal with preconditions and postconditions
2259 Process_PPCs (N, Spec_Id, Body_Id);
2261 -- Add a declaration for the Protection object, renaming declarations
2262 -- for discriminals and privals and finally a declaration for the entry
2263 -- family index (if applicable). This form of early expansion is done
2264 -- when the Expander is active because Install_Private_Data_Declarations
2265 -- references entities which were created during regular expansion.
2268 and then Comes_From_Source (N)
2269 and then Present (Prot_Typ)
2270 and then Present (Spec_Id)
2271 and then not Is_Eliminated (Spec_Id)
2273 Install_Private_Data_Declarations
2274 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2277 -- Analyze the declarations (this call will analyze the precondition
2278 -- Check pragmas we prepended to the list, as well as the declaration
2279 -- of the _Postconditions procedure).
2281 Analyze_Declarations (Declarations (N));
2283 -- Check completion, and analyze the statements
2286 Inspect_Deferred_Constant_Completion (Declarations (N));
2289 -- Deal with end of scope processing for the body
2291 Process_End_Label (HSS, 't', Current_Scope);
2293 Check_Subprogram_Order (N);
2294 Set_Analyzed (Body_Id);
2296 -- If we have a separate spec, then the analysis of the declarations
2297 -- caused the entities in the body to be chained to the spec id, but
2298 -- we want them chained to the body id. Only the formal parameters
2299 -- end up chained to the spec id in this case.
2301 if Present (Spec_Id) then
2303 -- We must conform to the categorization of our spec
2305 Validate_Categorization_Dependency (N, Spec_Id);
2307 -- And if this is a child unit, the parent units must conform
2309 if Is_Child_Unit (Spec_Id) then
2310 Validate_Categorization_Dependency
2311 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2314 -- Here is where we move entities from the spec to the body
2316 -- Case where there are entities that stay with the spec
2318 if Present (Last_Real_Spec_Entity) then
2320 -- No body entities (happens when the only real spec entities
2321 -- come from precondition and postcondition pragmas)
2323 if No (Last_Entity (Body_Id)) then
2325 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2327 -- Body entities present (formals), so chain stuff past them
2331 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2334 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2335 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2336 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2338 -- Case where there are no spec entities, in this case there can
2339 -- be no body entities either, so just move everything.
2342 pragma Assert (No (Last_Entity (Body_Id)));
2343 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2344 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2345 Set_First_Entity (Spec_Id, Empty);
2346 Set_Last_Entity (Spec_Id, Empty);
2350 -- If function, check return statements
2352 if Nkind (Body_Spec) = N_Function_Specification then
2357 if Present (Spec_Id) then
2363 if Return_Present (Id) then
2364 Check_Returns (HSS, 'F', Missing_Ret);
2367 Set_Has_Missing_Return (Id);
2370 elsif not Is_Machine_Code_Subprogram (Id)
2371 and then not Body_Deleted
2373 Error_Msg_N ("missing RETURN statement in function body", N);
2377 -- If procedure with No_Return, check returns
2379 elsif Nkind (Body_Spec) = N_Procedure_Specification
2380 and then Present (Spec_Id)
2381 and then No_Return (Spec_Id)
2383 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2386 -- Now we are going to check for variables that are never modified in
2387 -- the body of the procedure. But first we deal with a special case
2388 -- where we want to modify this check. If the body of the subprogram
2389 -- starts with a raise statement or its equivalent, or if the body
2390 -- consists entirely of a null statement, then it is pretty obvious
2391 -- that it is OK to not reference the parameters. For example, this
2392 -- might be the following common idiom for a stubbed function:
2393 -- statement of the procedure raises an exception. In particular this
2394 -- deals with the common idiom of a stubbed function, which might
2395 -- appear as something like
2397 -- function F (A : Integer) return Some_Type;
2400 -- raise Program_Error;
2404 -- Here the purpose of X is simply to satisfy the annoying requirement
2405 -- in Ada that there be at least one return, and we certainly do not
2406 -- want to go posting warnings on X that it is not initialized! On
2407 -- the other hand, if X is entirely unreferenced that should still
2410 -- What we do is to detect these cases, and if we find them, flag the
2411 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2412 -- suppress unwanted warnings. For the case of the function stub above
2413 -- we have a special test to set X as apparently assigned to suppress
2420 -- Skip initial labels (for one thing this occurs when we are in
2421 -- front end ZCX mode, but in any case it is irrelevant), and also
2422 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2424 Stm := First (Statements (HSS));
2425 while Nkind (Stm) = N_Label
2426 or else Nkind (Stm) in N_Push_xxx_Label
2431 -- Do the test on the original statement before expansion
2434 Ostm : constant Node_Id := Original_Node (Stm);
2437 -- If explicit raise statement, turn on flag
2439 if Nkind (Ostm) = N_Raise_Statement then
2440 Set_Trivial_Subprogram (Stm);
2442 -- If null statement, and no following statements, turn on flag
2444 elsif Nkind (Stm) = N_Null_Statement
2445 and then Comes_From_Source (Stm)
2446 and then No (Next (Stm))
2448 Set_Trivial_Subprogram (Stm);
2450 -- Check for explicit call cases which likely raise an exception
2452 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2453 if Is_Entity_Name (Name (Ostm)) then
2455 Ent : constant Entity_Id := Entity (Name (Ostm));
2458 -- If the procedure is marked No_Return, then likely it
2459 -- raises an exception, but in any case it is not coming
2460 -- back here, so turn on the flag.
2462 if Ekind (Ent) = E_Procedure
2463 and then No_Return (Ent)
2465 Set_Trivial_Subprogram (Stm);
2473 -- Check for variables that are never modified
2479 -- If there is a separate spec, then transfer Never_Set_In_Source
2480 -- flags from out parameters to the corresponding entities in the
2481 -- body. The reason we do that is we want to post error flags on
2482 -- the body entities, not the spec entities.
2484 if Present (Spec_Id) then
2485 E1 := First_Entity (Spec_Id);
2486 while Present (E1) loop
2487 if Ekind (E1) = E_Out_Parameter then
2488 E2 := First_Entity (Body_Id);
2489 while Present (E2) loop
2490 exit when Chars (E1) = Chars (E2);
2494 if Present (E2) then
2495 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2503 -- Check references in body unless it was deleted. Note that the
2504 -- check of Body_Deleted here is not just for efficiency, it is
2505 -- necessary to avoid junk warnings on formal parameters.
2507 if not Body_Deleted then
2508 Check_References (Body_Id);
2511 end Analyze_Subprogram_Body;
2513 ------------------------------------
2514 -- Analyze_Subprogram_Declaration --
2515 ------------------------------------
2517 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2518 Designator : constant Entity_Id :=
2519 Analyze_Subprogram_Specification (Specification (N));
2520 Scop : constant Entity_Id := Current_Scope;
2522 -- Start of processing for Analyze_Subprogram_Declaration
2525 Generate_Definition (Designator);
2527 -- Check for RCI unit subprogram declarations for illegal inlined
2528 -- subprograms and subprograms having access parameter or limited
2529 -- parameter without Read and Write attributes (RM E.2.3(12-13)).
2531 Validate_RCI_Subprogram_Declaration (N);
2535 Defining_Entity (N),
2536 " Analyze subprogram spec: ");
2538 if Debug_Flag_C then
2539 Write_Str ("==== Compiling subprogram spec ");
2540 Write_Name (Chars (Designator));
2541 Write_Str (" from ");
2542 Write_Location (Sloc (N));
2546 New_Overloaded_Entity (Designator);
2547 Check_Delayed_Subprogram (Designator);
2549 -- If the type of the first formal of the current subprogram is a non
2550 -- generic tagged private type , mark the subprogram as being a private
2553 if Present (First_Formal (Designator)) then
2555 Formal_Typ : constant Entity_Id :=
2556 Etype (First_Formal (Designator));
2558 Set_Is_Private_Primitive (Designator,
2559 Is_Tagged_Type (Formal_Typ)
2560 and then Is_Private_Type (Formal_Typ)
2561 and then not Is_Generic_Actual_Type (Formal_Typ));
2565 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2568 if Ada_Version >= Ada_05
2569 and then Comes_From_Source (N)
2570 and then Is_Dispatching_Operation (Designator)
2577 if Has_Controlling_Result (Designator) then
2578 Etyp := Etype (Designator);
2581 E := First_Entity (Designator);
2583 and then Is_Formal (E)
2584 and then not Is_Controlling_Formal (E)
2592 if Is_Access_Type (Etyp) then
2593 Etyp := Directly_Designated_Type (Etyp);
2596 if Is_Interface (Etyp)
2597 and then not Is_Abstract_Subprogram (Designator)
2598 and then not (Ekind (Designator) = E_Procedure
2599 and then Null_Present (Specification (N)))
2601 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2603 ("(Ada 2005) interface subprogram % must be abstract or null",
2609 -- What is the following code for, it used to be
2611 -- ??? Set_Suppress_Elaboration_Checks
2612 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2614 -- The following seems equivalent, but a bit dubious
2616 if Elaboration_Checks_Suppressed (Designator) then
2617 Set_Kill_Elaboration_Checks (Designator);
2620 if Scop /= Standard_Standard
2621 and then not Is_Child_Unit (Designator)
2623 Set_Categorization_From_Scope (Designator, Scop);
2625 -- For a compilation unit, check for library-unit pragmas
2627 Push_Scope (Designator);
2628 Set_Categorization_From_Pragmas (N);
2629 Validate_Categorization_Dependency (N, Designator);
2633 -- For a compilation unit, set body required. This flag will only be
2634 -- reset if a valid Import or Interface pragma is processed later on.
2636 if Nkind (Parent (N)) = N_Compilation_Unit then
2637 Set_Body_Required (Parent (N), True);
2639 if Ada_Version >= Ada_05
2640 and then Nkind (Specification (N)) = N_Procedure_Specification
2641 and then Null_Present (Specification (N))
2644 ("null procedure cannot be declared at library level", N);
2648 Generate_Reference_To_Formals (Designator);
2649 Check_Eliminated (Designator);
2651 -- Ada 2005: if procedure is declared with "is null" qualifier,
2652 -- it requires no body.
2654 if Nkind (Specification (N)) = N_Procedure_Specification
2655 and then Null_Present (Specification (N))
2657 Set_Has_Completion (Designator);
2658 Set_Is_Inlined (Designator);
2660 if Is_Protected_Type (Current_Scope) then
2662 ("protected operation cannot be a null procedure", N);
2665 end Analyze_Subprogram_Declaration;
2667 --------------------------------------
2668 -- Analyze_Subprogram_Specification --
2669 --------------------------------------
2671 -- Reminder: N here really is a subprogram specification (not a subprogram
2672 -- declaration). This procedure is called to analyze the specification in
2673 -- both subprogram bodies and subprogram declarations (specs).
2675 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2676 Designator : constant Entity_Id := Defining_Entity (N);
2677 Formals : constant List_Id := Parameter_Specifications (N);
2679 -- Start of processing for Analyze_Subprogram_Specification
2682 Generate_Definition (Designator);
2684 if Nkind (N) = N_Function_Specification then
2685 Set_Ekind (Designator, E_Function);
2686 Set_Mechanism (Designator, Default_Mechanism);
2689 Set_Ekind (Designator, E_Procedure);
2690 Set_Etype (Designator, Standard_Void_Type);
2693 -- Introduce new scope for analysis of the formals and the return type
2695 Set_Scope (Designator, Current_Scope);
2697 if Present (Formals) then
2698 Push_Scope (Designator);
2699 Process_Formals (Formals, N);
2701 -- Ada 2005 (AI-345): If this is an overriding operation of an
2702 -- inherited interface operation, and the controlling type is
2703 -- a synchronized type, replace the type with its corresponding
2704 -- record, to match the proper signature of an overriding operation.
2706 if Ada_Version >= Ada_05 then
2709 Formal_Typ : Entity_Id;
2710 Rec_Typ : Entity_Id;
2713 Formal := First_Formal (Designator);
2714 while Present (Formal) loop
2715 Formal_Typ := Etype (Formal);
2717 if Is_Concurrent_Type (Formal_Typ)
2718 and then Present (Corresponding_Record_Type (Formal_Typ))
2720 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2722 if Present (Interfaces (Rec_Typ)) then
2723 Set_Etype (Formal, Rec_Typ);
2727 Next_Formal (Formal);
2734 elsif Nkind (N) = N_Function_Specification then
2735 Analyze_Return_Type (N);
2738 if Nkind (N) = N_Function_Specification then
2739 if Nkind (Designator) = N_Defining_Operator_Symbol then
2740 Valid_Operator_Definition (Designator);
2743 May_Need_Actuals (Designator);
2745 -- Ada 2005 (AI-251): If the return type is abstract, verify that
2746 -- the subprogram is abstract also. This does not apply to renaming
2747 -- declarations, where abstractness is inherited.
2748 -- In case of primitives associated with abstract interface types
2749 -- the check is applied later (see Analyze_Subprogram_Declaration).
2751 if Is_Abstract_Type (Etype (Designator))
2752 and then not Is_Interface (Etype (Designator))
2753 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
2754 and then Nkind (Parent (N)) /=
2755 N_Abstract_Subprogram_Declaration
2757 (Nkind (Parent (N))) /= N_Formal_Abstract_Subprogram_Declaration
2760 ("function that returns abstract type must be abstract", N);
2765 end Analyze_Subprogram_Specification;
2767 --------------------------
2768 -- Build_Body_To_Inline --
2769 --------------------------
2771 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
2772 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
2773 Original_Body : Node_Id;
2774 Body_To_Analyze : Node_Id;
2775 Max_Size : constant := 10;
2776 Stat_Count : Integer := 0;
2778 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
2779 -- Check for declarations that make inlining not worthwhile
2781 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
2782 -- Check for statements that make inlining not worthwhile: any tasking
2783 -- statement, nested at any level. Keep track of total number of
2784 -- elementary statements, as a measure of acceptable size.
2786 function Has_Pending_Instantiation return Boolean;
2787 -- If some enclosing body contains instantiations that appear before the
2788 -- corresponding generic body, the enclosing body has a freeze node so
2789 -- that it can be elaborated after the generic itself. This might
2790 -- conflict with subsequent inlinings, so that it is unsafe to try to
2791 -- inline in such a case.
2793 function Has_Single_Return return Boolean;
2794 -- In general we cannot inline functions that return unconstrained type.
2795 -- However, we can handle such functions if all return statements return
2796 -- a local variable that is the only declaration in the body of the
2797 -- function. In that case the call can be replaced by that local
2798 -- variable as is done for other inlined calls.
2800 procedure Remove_Pragmas;
2801 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
2802 -- parameter has no meaning when the body is inlined and the formals
2803 -- are rewritten. Remove it from body to inline. The analysis of the
2804 -- non-inlined body will handle the pragma properly.
2806 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
2807 -- If the body of the subprogram includes a call that returns an
2808 -- unconstrained type, the secondary stack is involved, and it
2809 -- is not worth inlining.
2811 ------------------------------
2812 -- Has_Excluded_Declaration --
2813 ------------------------------
2815 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
2818 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
2819 -- Nested subprograms make a given body ineligible for inlining, but
2820 -- we make an exception for instantiations of unchecked conversion.
2821 -- The body has not been analyzed yet, so check the name, and verify
2822 -- that the visible entity with that name is the predefined unit.
2824 -----------------------------
2825 -- Is_Unchecked_Conversion --
2826 -----------------------------
2828 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
2829 Id : constant Node_Id := Name (D);
2833 if Nkind (Id) = N_Identifier
2834 and then Chars (Id) = Name_Unchecked_Conversion
2836 Conv := Current_Entity (Id);
2838 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
2839 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
2841 Conv := Current_Entity (Selector_Name (Id));
2846 return Present (Conv)
2847 and then Is_Predefined_File_Name
2848 (Unit_File_Name (Get_Source_Unit (Conv)))
2849 and then Is_Intrinsic_Subprogram (Conv);
2850 end Is_Unchecked_Conversion;
2852 -- Start of processing for Has_Excluded_Declaration
2856 while Present (D) loop
2857 if (Nkind (D) = N_Function_Instantiation
2858 and then not Is_Unchecked_Conversion (D))
2859 or else Nkind_In (D, N_Protected_Type_Declaration,
2860 N_Package_Declaration,
2861 N_Package_Instantiation,
2863 N_Procedure_Instantiation,
2864 N_Task_Type_Declaration)
2867 ("cannot inline & (non-allowed declaration)?", D, Subp);
2875 end Has_Excluded_Declaration;
2877 ----------------------------
2878 -- Has_Excluded_Statement --
2879 ----------------------------
2881 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
2887 while Present (S) loop
2888 Stat_Count := Stat_Count + 1;
2890 if Nkind_In (S, N_Abort_Statement,
2891 N_Asynchronous_Select,
2892 N_Conditional_Entry_Call,
2893 N_Delay_Relative_Statement,
2894 N_Delay_Until_Statement,
2899 ("cannot inline & (non-allowed statement)?", S, Subp);
2902 elsif Nkind (S) = N_Block_Statement then
2903 if Present (Declarations (S))
2904 and then Has_Excluded_Declaration (Declarations (S))
2908 elsif Present (Handled_Statement_Sequence (S))
2911 (Exception_Handlers (Handled_Statement_Sequence (S)))
2913 Has_Excluded_Statement
2914 (Statements (Handled_Statement_Sequence (S))))
2919 elsif Nkind (S) = N_Case_Statement then
2920 E := First (Alternatives (S));
2921 while Present (E) loop
2922 if Has_Excluded_Statement (Statements (E)) then
2929 elsif Nkind (S) = N_If_Statement then
2930 if Has_Excluded_Statement (Then_Statements (S)) then
2934 if Present (Elsif_Parts (S)) then
2935 E := First (Elsif_Parts (S));
2936 while Present (E) loop
2937 if Has_Excluded_Statement (Then_Statements (E)) then
2944 if Present (Else_Statements (S))
2945 and then Has_Excluded_Statement (Else_Statements (S))
2950 elsif Nkind (S) = N_Loop_Statement
2951 and then Has_Excluded_Statement (Statements (S))
2960 end Has_Excluded_Statement;
2962 -------------------------------
2963 -- Has_Pending_Instantiation --
2964 -------------------------------
2966 function Has_Pending_Instantiation return Boolean is
2971 while Present (S) loop
2972 if Is_Compilation_Unit (S)
2973 or else Is_Child_Unit (S)
2976 elsif Ekind (S) = E_Package
2977 and then Has_Forward_Instantiation (S)
2986 end Has_Pending_Instantiation;
2988 ------------------------
2989 -- Has_Single_Return --
2990 ------------------------
2992 function Has_Single_Return return Boolean is
2993 Return_Statement : Node_Id := Empty;
2995 function Check_Return (N : Node_Id) return Traverse_Result;
3001 function Check_Return (N : Node_Id) return Traverse_Result is
3003 if Nkind (N) = N_Simple_Return_Statement then
3004 if Present (Expression (N))
3005 and then Is_Entity_Name (Expression (N))
3007 if No (Return_Statement) then
3008 Return_Statement := N;
3011 elsif Chars (Expression (N)) =
3012 Chars (Expression (Return_Statement))
3021 -- Expression has wrong form
3031 function Check_All_Returns is new Traverse_Func (Check_Return);
3033 -- Start of processing for Has_Single_Return
3036 return Check_All_Returns (N) = OK
3037 and then Present (Declarations (N))
3038 and then Present (First (Declarations (N)))
3039 and then Chars (Expression (Return_Statement)) =
3040 Chars (Defining_Identifier (First (Declarations (N))));
3041 end Has_Single_Return;
3043 --------------------
3044 -- Remove_Pragmas --
3045 --------------------
3047 procedure Remove_Pragmas is
3052 Decl := First (Declarations (Body_To_Analyze));
3053 while Present (Decl) loop
3056 if Nkind (Decl) = N_Pragma
3057 and then (Pragma_Name (Decl) = Name_Unreferenced
3059 Pragma_Name (Decl) = Name_Unmodified)
3068 --------------------------
3069 -- Uses_Secondary_Stack --
3070 --------------------------
3072 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3073 function Check_Call (N : Node_Id) return Traverse_Result;
3074 -- Look for function calls that return an unconstrained type
3080 function Check_Call (N : Node_Id) return Traverse_Result is
3082 if Nkind (N) = N_Function_Call
3083 and then Is_Entity_Name (Name (N))
3084 and then Is_Composite_Type (Etype (Entity (Name (N))))
3085 and then not Is_Constrained (Etype (Entity (Name (N))))
3088 ("cannot inline & (call returns unconstrained type)?",
3096 function Check_Calls is new Traverse_Func (Check_Call);
3099 return Check_Calls (Bod) = Abandon;
3100 end Uses_Secondary_Stack;
3102 -- Start of processing for Build_Body_To_Inline
3105 -- Return immediately if done already
3107 if Nkind (Decl) = N_Subprogram_Declaration
3108 and then Present (Body_To_Inline (Decl))
3112 -- Functions that return unconstrained composite types require
3113 -- secondary stack handling, and cannot currently be inlined, unless
3114 -- all return statements return a local variable that is the first
3115 -- local declaration in the body.
3117 elsif Ekind (Subp) = E_Function
3118 and then not Is_Scalar_Type (Etype (Subp))
3119 and then not Is_Access_Type (Etype (Subp))
3120 and then not Is_Constrained (Etype (Subp))
3122 if not Has_Single_Return then
3124 ("cannot inline & (unconstrained return type)?", N, Subp);
3128 -- Ditto for functions that return controlled types, where controlled
3129 -- actions interfere in complex ways with inlining.
3131 elsif Ekind (Subp) = E_Function
3132 and then Needs_Finalization (Etype (Subp))
3135 ("cannot inline & (controlled return type)?", N, Subp);
3139 if Present (Declarations (N))
3140 and then Has_Excluded_Declaration (Declarations (N))
3145 if Present (Handled_Statement_Sequence (N)) then
3146 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3148 ("cannot inline& (exception handler)?",
3149 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3153 Has_Excluded_Statement
3154 (Statements (Handled_Statement_Sequence (N)))
3160 -- We do not inline a subprogram that is too large, unless it is
3161 -- marked Inline_Always. This pragma does not suppress the other
3162 -- checks on inlining (forbidden declarations, handlers, etc).
3164 if Stat_Count > Max_Size
3165 and then not Has_Pragma_Inline_Always (Subp)
3167 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3171 if Has_Pending_Instantiation then
3173 ("cannot inline& (forward instance within enclosing body)?",
3178 -- Within an instance, the body to inline must be treated as a nested
3179 -- generic, so that the proper global references are preserved.
3181 -- Note that we do not do this at the library level, because it is not
3182 -- needed, and furthermore this causes trouble if front end inlining
3183 -- is activated (-gnatN).
3185 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3186 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3187 Original_Body := Copy_Generic_Node (N, Empty, True);
3189 Original_Body := Copy_Separate_Tree (N);
3192 -- We need to capture references to the formals in order to substitute
3193 -- the actuals at the point of inlining, i.e. instantiation. To treat
3194 -- the formals as globals to the body to inline, we nest it within
3195 -- a dummy parameterless subprogram, declared within the real one.
3196 -- To avoid generating an internal name (which is never public, and
3197 -- which affects serial numbers of other generated names), we use
3198 -- an internal symbol that cannot conflict with user declarations.
3200 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3201 Set_Defining_Unit_Name
3202 (Specification (Original_Body),
3203 Make_Defining_Identifier (Sloc (N), Name_uParent));
3204 Set_Corresponding_Spec (Original_Body, Empty);
3206 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3208 -- Set return type of function, which is also global and does not need
3211 if Ekind (Subp) = E_Function then
3212 Set_Result_Definition (Specification (Body_To_Analyze),
3213 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3216 if No (Declarations (N)) then
3217 Set_Declarations (N, New_List (Body_To_Analyze));
3219 Append (Body_To_Analyze, Declarations (N));
3222 Expander_Mode_Save_And_Set (False);
3225 Analyze (Body_To_Analyze);
3226 Push_Scope (Defining_Entity (Body_To_Analyze));
3227 Save_Global_References (Original_Body);
3229 Remove (Body_To_Analyze);
3231 Expander_Mode_Restore;
3233 -- Restore environment if previously saved
3235 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3239 -- If secondary stk used there is no point in inlining. We have
3240 -- already issued the warning in this case, so nothing to do.
3242 if Uses_Secondary_Stack (Body_To_Analyze) then
3246 Set_Body_To_Inline (Decl, Original_Body);
3247 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3248 Set_Is_Inlined (Subp);
3249 end Build_Body_To_Inline;
3255 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3257 -- Do not emit warning if this is a predefined unit which is not
3258 -- the main unit. With validity checks enabled, some predefined
3259 -- subprograms may contain nested subprograms and become ineligible
3262 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3263 and then not In_Extended_Main_Source_Unit (Subp)
3267 elsif Has_Pragma_Inline_Always (Subp) then
3269 -- Remove last character (question mark) to make this into an error,
3270 -- because the Inline_Always pragma cannot be obeyed.
3272 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3274 elsif Ineffective_Inline_Warnings then
3275 Error_Msg_NE (Msg, N, Subp);
3279 -----------------------
3280 -- Check_Conformance --
3281 -----------------------
3283 procedure Check_Conformance
3284 (New_Id : Entity_Id;
3286 Ctype : Conformance_Type;
3288 Conforms : out Boolean;
3289 Err_Loc : Node_Id := Empty;
3290 Get_Inst : Boolean := False;
3291 Skip_Controlling_Formals : Boolean := False)
3293 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3294 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3295 -- If Errmsg is True, then processing continues to post an error message
3296 -- for conformance error on given node. Two messages are output. The
3297 -- first message points to the previous declaration with a general "no
3298 -- conformance" message. The second is the detailed reason, supplied as
3299 -- Msg. The parameter N provide information for a possible & insertion
3300 -- in the message, and also provides the location for posting the
3301 -- message in the absence of a specified Err_Loc location.
3303 -----------------------
3304 -- Conformance_Error --
3305 -----------------------
3307 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3314 if No (Err_Loc) then
3320 Error_Msg_Sloc := Sloc (Old_Id);
3323 when Type_Conformant =>
3325 ("not type conformant with declaration#!", Enode);
3327 when Mode_Conformant =>
3328 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3330 ("not mode conformant with operation inherited#!",
3334 ("not mode conformant with declaration#!", Enode);
3337 when Subtype_Conformant =>
3338 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3340 ("not subtype conformant with operation inherited#!",
3344 ("not subtype conformant with declaration#!", Enode);
3347 when Fully_Conformant =>
3348 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3350 ("not fully conformant with operation inherited#!",
3354 ("not fully conformant with declaration#!", Enode);
3358 Error_Msg_NE (Msg, Enode, N);
3360 end Conformance_Error;
3364 Old_Type : constant Entity_Id := Etype (Old_Id);
3365 New_Type : constant Entity_Id := Etype (New_Id);
3366 Old_Formal : Entity_Id;
3367 New_Formal : Entity_Id;
3368 Access_Types_Match : Boolean;
3369 Old_Formal_Base : Entity_Id;
3370 New_Formal_Base : Entity_Id;
3372 -- Start of processing for Check_Conformance
3377 -- We need a special case for operators, since they don't appear
3380 if Ctype = Type_Conformant then
3381 if Ekind (New_Id) = E_Operator
3382 and then Operator_Matches_Spec (New_Id, Old_Id)
3388 -- If both are functions/operators, check return types conform
3390 if Old_Type /= Standard_Void_Type
3391 and then New_Type /= Standard_Void_Type
3394 -- If we are checking interface conformance we omit controlling
3395 -- arguments and result, because we are only checking the conformance
3396 -- of the remaining parameters.
3398 if Has_Controlling_Result (Old_Id)
3399 and then Has_Controlling_Result (New_Id)
3400 and then Skip_Controlling_Formals
3404 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3405 Conformance_Error ("\return type does not match!", New_Id);
3409 -- Ada 2005 (AI-231): In case of anonymous access types check the
3410 -- null-exclusion and access-to-constant attributes match.
3412 if Ada_Version >= Ada_05
3413 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3415 (Can_Never_Be_Null (Old_Type)
3416 /= Can_Never_Be_Null (New_Type)
3417 or else Is_Access_Constant (Etype (Old_Type))
3418 /= Is_Access_Constant (Etype (New_Type)))
3420 Conformance_Error ("\return type does not match!", New_Id);
3424 -- If either is a function/operator and the other isn't, error
3426 elsif Old_Type /= Standard_Void_Type
3427 or else New_Type /= Standard_Void_Type
3429 Conformance_Error ("\functions can only match functions!", New_Id);
3433 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3434 -- If this is a renaming as body, refine error message to indicate that
3435 -- the conflict is with the original declaration. If the entity is not
3436 -- frozen, the conventions don't have to match, the one of the renamed
3437 -- entity is inherited.
3439 if Ctype >= Subtype_Conformant then
3440 if Convention (Old_Id) /= Convention (New_Id) then
3442 if not Is_Frozen (New_Id) then
3445 elsif Present (Err_Loc)
3446 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3447 and then Present (Corresponding_Spec (Err_Loc))
3449 Error_Msg_Name_1 := Chars (New_Id);
3451 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3453 Conformance_Error ("\prior declaration for% has convention %!");
3456 Conformance_Error ("\calling conventions do not match!");
3461 elsif Is_Formal_Subprogram (Old_Id)
3462 or else Is_Formal_Subprogram (New_Id)
3464 Conformance_Error ("\formal subprograms not allowed!");
3469 -- Deal with parameters
3471 -- Note: we use the entity information, rather than going directly
3472 -- to the specification in the tree. This is not only simpler, but
3473 -- absolutely necessary for some cases of conformance tests between
3474 -- operators, where the declaration tree simply does not exist!
3476 Old_Formal := First_Formal (Old_Id);
3477 New_Formal := First_Formal (New_Id);
3478 while Present (Old_Formal) and then Present (New_Formal) loop
3479 if Is_Controlling_Formal (Old_Formal)
3480 and then Is_Controlling_Formal (New_Formal)
3481 and then Skip_Controlling_Formals
3483 -- The controlling formals will have different types when
3484 -- comparing an interface operation with its match, but both
3485 -- or neither must be access parameters.
3487 if Is_Access_Type (Etype (Old_Formal))
3489 Is_Access_Type (Etype (New_Formal))
3491 goto Skip_Controlling_Formal;
3494 ("\access parameter does not match!", New_Formal);
3498 if Ctype = Fully_Conformant then
3500 -- Names must match. Error message is more accurate if we do
3501 -- this before checking that the types of the formals match.
3503 if Chars (Old_Formal) /= Chars (New_Formal) then
3504 Conformance_Error ("\name & does not match!", New_Formal);
3506 -- Set error posted flag on new formal as well to stop
3507 -- junk cascaded messages in some cases.
3509 Set_Error_Posted (New_Formal);
3514 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3515 -- case occurs whenever a subprogram is being renamed and one of its
3516 -- parameters imposes a null exclusion. For example:
3518 -- type T is null record;
3519 -- type Acc_T is access T;
3520 -- subtype Acc_T_Sub is Acc_T;
3522 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3523 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3526 Old_Formal_Base := Etype (Old_Formal);
3527 New_Formal_Base := Etype (New_Formal);
3530 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3531 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3534 Access_Types_Match := Ada_Version >= Ada_05
3536 -- Ensure that this rule is only applied when New_Id is a
3537 -- renaming of Old_Id.
3539 and then Nkind (Parent (Parent (New_Id))) =
3540 N_Subprogram_Renaming_Declaration
3541 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3542 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3543 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3545 -- Now handle the allowed access-type case
3547 and then Is_Access_Type (Old_Formal_Base)
3548 and then Is_Access_Type (New_Formal_Base)
3550 -- The type kinds must match. The only exception occurs with
3551 -- multiple generics of the form:
3554 -- type F is private; type A is private;
3555 -- type F_Ptr is access F; type A_Ptr is access A;
3556 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3557 -- package F_Pack is ... package A_Pack is
3558 -- package F_Inst is
3559 -- new F_Pack (A, A_Ptr, A_P);
3561 -- When checking for conformance between the parameters of A_P
3562 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3563 -- because the compiler has transformed A_Ptr into a subtype of
3564 -- F_Ptr. We catch this case in the code below.
3566 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3568 (Is_Generic_Type (Old_Formal_Base)
3569 and then Is_Generic_Type (New_Formal_Base)
3570 and then Is_Internal (New_Formal_Base)
3571 and then Etype (Etype (New_Formal_Base)) =
3573 and then Directly_Designated_Type (Old_Formal_Base) =
3574 Directly_Designated_Type (New_Formal_Base)
3575 and then ((Is_Itype (Old_Formal_Base)
3576 and then Can_Never_Be_Null (Old_Formal_Base))
3578 (Is_Itype (New_Formal_Base)
3579 and then Can_Never_Be_Null (New_Formal_Base)));
3581 -- Types must always match. In the visible part of an instance,
3582 -- usual overloading rules for dispatching operations apply, and
3583 -- we check base types (not the actual subtypes).
3585 if In_Instance_Visible_Part
3586 and then Is_Dispatching_Operation (New_Id)
3588 if not Conforming_Types
3589 (T1 => Base_Type (Etype (Old_Formal)),
3590 T2 => Base_Type (Etype (New_Formal)),
3592 Get_Inst => Get_Inst)
3593 and then not Access_Types_Match
3595 Conformance_Error ("\type of & does not match!", New_Formal);
3599 elsif not Conforming_Types
3600 (T1 => Old_Formal_Base,
3601 T2 => New_Formal_Base,
3603 Get_Inst => Get_Inst)
3604 and then not Access_Types_Match
3606 -- Don't give error message if old type is Any_Type. This test
3607 -- avoids some cascaded errors, e.g. in case of a bad spec.
3609 if Errmsg and then Old_Formal_Base = Any_Type then
3612 Conformance_Error ("\type of & does not match!", New_Formal);
3618 -- For mode conformance, mode must match
3620 if Ctype >= Mode_Conformant then
3621 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3622 Conformance_Error ("\mode of & does not match!", New_Formal);
3625 -- Part of mode conformance for access types is having the same
3626 -- constant modifier.
3628 elsif Access_Types_Match
3629 and then Is_Access_Constant (Old_Formal_Base) /=
3630 Is_Access_Constant (New_Formal_Base)
3633 ("\constant modifier does not match!", New_Formal);
3638 if Ctype >= Subtype_Conformant then
3640 -- Ada 2005 (AI-231): In case of anonymous access types check
3641 -- the null-exclusion and access-to-constant attributes must
3644 if Ada_Version >= Ada_05
3645 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
3646 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
3648 (Can_Never_Be_Null (Old_Formal) /=
3649 Can_Never_Be_Null (New_Formal)
3651 Is_Access_Constant (Etype (Old_Formal)) /=
3652 Is_Access_Constant (Etype (New_Formal)))
3654 -- It is allowed to omit the null-exclusion in case of stream
3655 -- attribute subprograms. We recognize stream subprograms
3656 -- through their TSS-generated suffix.
3659 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
3661 if TSS_Name /= TSS_Stream_Read
3662 and then TSS_Name /= TSS_Stream_Write
3663 and then TSS_Name /= TSS_Stream_Input
3664 and then TSS_Name /= TSS_Stream_Output
3667 ("\type of & does not match!", New_Formal);
3674 -- Full conformance checks
3676 if Ctype = Fully_Conformant then
3678 -- We have checked already that names match
3680 if Parameter_Mode (Old_Formal) = E_In_Parameter then
3682 -- Check default expressions for in parameters
3685 NewD : constant Boolean :=
3686 Present (Default_Value (New_Formal));
3687 OldD : constant Boolean :=
3688 Present (Default_Value (Old_Formal));
3690 if NewD or OldD then
3692 -- The old default value has been analyzed because the
3693 -- current full declaration will have frozen everything
3694 -- before. The new default value has not been analyzed,
3695 -- so analyze it now before we check for conformance.
3698 Push_Scope (New_Id);
3699 Preanalyze_Spec_Expression
3700 (Default_Value (New_Formal), Etype (New_Formal));
3704 if not (NewD and OldD)
3705 or else not Fully_Conformant_Expressions
3706 (Default_Value (Old_Formal),
3707 Default_Value (New_Formal))
3710 ("\default expression for & does not match!",
3719 -- A couple of special checks for Ada 83 mode. These checks are
3720 -- skipped if either entity is an operator in package Standard,
3721 -- or if either old or new instance is not from the source program.
3723 if Ada_Version = Ada_83
3724 and then Sloc (Old_Id) > Standard_Location
3725 and then Sloc (New_Id) > Standard_Location
3726 and then Comes_From_Source (Old_Id)
3727 and then Comes_From_Source (New_Id)
3730 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
3731 New_Param : constant Node_Id := Declaration_Node (New_Formal);
3734 -- Explicit IN must be present or absent in both cases. This
3735 -- test is required only in the full conformance case.
3737 if In_Present (Old_Param) /= In_Present (New_Param)
3738 and then Ctype = Fully_Conformant
3741 ("\(Ada 83) IN must appear in both declarations",
3746 -- Grouping (use of comma in param lists) must be the same
3747 -- This is where we catch a misconformance like:
3750 -- A : Integer; B : Integer
3752 -- which are represented identically in the tree except
3753 -- for the setting of the flags More_Ids and Prev_Ids.
3755 if More_Ids (Old_Param) /= More_Ids (New_Param)
3756 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
3759 ("\grouping of & does not match!", New_Formal);
3765 -- This label is required when skipping controlling formals
3767 <<Skip_Controlling_Formal>>
3769 Next_Formal (Old_Formal);
3770 Next_Formal (New_Formal);
3773 if Present (Old_Formal) then
3774 Conformance_Error ("\too few parameters!");
3777 elsif Present (New_Formal) then
3778 Conformance_Error ("\too many parameters!", New_Formal);
3781 end Check_Conformance;
3783 -----------------------
3784 -- Check_Conventions --
3785 -----------------------
3787 procedure Check_Conventions (Typ : Entity_Id) is
3788 Ifaces_List : Elist_Id;
3790 procedure Check_Convention (Op : Entity_Id);
3791 -- Verify that the convention of inherited dispatching operation Op is
3792 -- consistent among all subprograms it overrides. In order to minimize
3793 -- the search, Search_From is utilized to designate a specific point in
3794 -- the list rather than iterating over the whole list once more.
3796 ----------------------
3797 -- Check_Convention --
3798 ----------------------
3800 procedure Check_Convention (Op : Entity_Id) is
3801 Iface_Elmt : Elmt_Id;
3802 Iface_Prim_Elmt : Elmt_Id;
3803 Iface_Prim : Entity_Id;
3806 Iface_Elmt := First_Elmt (Ifaces_List);
3807 while Present (Iface_Elmt) loop
3809 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
3810 while Present (Iface_Prim_Elmt) loop
3811 Iface_Prim := Node (Iface_Prim_Elmt);
3813 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
3814 and then Convention (Iface_Prim) /= Convention (Op)
3817 ("inconsistent conventions in primitive operations", Typ);
3819 Error_Msg_Name_1 := Chars (Op);
3820 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
3821 Error_Msg_Sloc := Sloc (Op);
3823 if Comes_From_Source (Op) then
3824 if not Is_Overriding_Operation (Op) then
3825 Error_Msg_N ("\\primitive % defined #", Typ);
3827 Error_Msg_N ("\\overriding operation % with " &
3828 "convention % defined #", Typ);
3831 else pragma Assert (Present (Alias (Op)));
3832 Error_Msg_Sloc := Sloc (Alias (Op));
3833 Error_Msg_N ("\\inherited operation % with " &
3834 "convention % defined #", Typ);
3837 Error_Msg_Name_1 := Chars (Op);
3839 Get_Convention_Name (Convention (Iface_Prim));
3840 Error_Msg_Sloc := Sloc (Iface_Prim);
3841 Error_Msg_N ("\\overridden operation % with " &
3842 "convention % defined #", Typ);
3844 -- Avoid cascading errors
3849 Next_Elmt (Iface_Prim_Elmt);
3852 Next_Elmt (Iface_Elmt);
3854 end Check_Convention;
3858 Prim_Op : Entity_Id;
3859 Prim_Op_Elmt : Elmt_Id;
3861 -- Start of processing for Check_Conventions
3864 if not Has_Interfaces (Typ) then
3868 Collect_Interfaces (Typ, Ifaces_List);
3870 -- The algorithm checks every overriding dispatching operation against
3871 -- all the corresponding overridden dispatching operations, detecting
3872 -- differences in conventions.
3874 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
3875 while Present (Prim_Op_Elmt) loop
3876 Prim_Op := Node (Prim_Op_Elmt);
3878 -- A small optimization: skip the predefined dispatching operations
3879 -- since they always have the same convention.
3881 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
3882 Check_Convention (Prim_Op);
3885 Next_Elmt (Prim_Op_Elmt);
3887 end Check_Conventions;
3889 ------------------------------
3890 -- Check_Delayed_Subprogram --
3891 ------------------------------
3893 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
3896 procedure Possible_Freeze (T : Entity_Id);
3897 -- T is the type of either a formal parameter or of the return type.
3898 -- If T is not yet frozen and needs a delayed freeze, then the
3899 -- subprogram itself must be delayed.
3901 ---------------------
3902 -- Possible_Freeze --
3903 ---------------------
3905 procedure Possible_Freeze (T : Entity_Id) is
3907 if Has_Delayed_Freeze (T)
3908 and then not Is_Frozen (T)
3910 Set_Has_Delayed_Freeze (Designator);
3912 elsif Is_Access_Type (T)
3913 and then Has_Delayed_Freeze (Designated_Type (T))
3914 and then not Is_Frozen (Designated_Type (T))
3916 Set_Has_Delayed_Freeze (Designator);
3918 end Possible_Freeze;
3920 -- Start of processing for Check_Delayed_Subprogram
3923 -- Never need to freeze abstract subprogram
3925 if Ekind (Designator) /= E_Subprogram_Type
3926 and then Is_Abstract_Subprogram (Designator)
3930 -- Need delayed freeze if return type itself needs a delayed
3931 -- freeze and is not yet frozen.
3933 Possible_Freeze (Etype (Designator));
3934 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
3936 -- Need delayed freeze if any of the formal types themselves need
3937 -- a delayed freeze and are not yet frozen.
3939 F := First_Formal (Designator);
3940 while Present (F) loop
3941 Possible_Freeze (Etype (F));
3942 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
3947 -- Mark functions that return by reference. Note that it cannot be
3948 -- done for delayed_freeze subprograms because the underlying
3949 -- returned type may not be known yet (for private types)
3951 if not Has_Delayed_Freeze (Designator)
3952 and then Expander_Active
3955 Typ : constant Entity_Id := Etype (Designator);
3956 Utyp : constant Entity_Id := Underlying_Type (Typ);
3959 if Is_Inherently_Limited_Type (Typ) then
3960 Set_Returns_By_Ref (Designator);
3962 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
3963 Set_Returns_By_Ref (Designator);
3967 end Check_Delayed_Subprogram;
3969 ------------------------------------
3970 -- Check_Discriminant_Conformance --
3971 ------------------------------------
3973 procedure Check_Discriminant_Conformance
3978 Old_Discr : Entity_Id := First_Discriminant (Prev);
3979 New_Discr : Node_Id := First (Discriminant_Specifications (N));
3980 New_Discr_Id : Entity_Id;
3981 New_Discr_Type : Entity_Id;
3983 procedure Conformance_Error (Msg : String; N : Node_Id);
3984 -- Post error message for conformance error on given node. Two messages
3985 -- are output. The first points to the previous declaration with a
3986 -- general "no conformance" message. The second is the detailed reason,
3987 -- supplied as Msg. The parameter N provide information for a possible
3988 -- & insertion in the message.
3990 -----------------------
3991 -- Conformance_Error --
3992 -----------------------
3994 procedure Conformance_Error (Msg : String; N : Node_Id) is
3996 Error_Msg_Sloc := Sloc (Prev_Loc);
3997 Error_Msg_N ("not fully conformant with declaration#!", N);
3998 Error_Msg_NE (Msg, N, N);
3999 end Conformance_Error;
4001 -- Start of processing for Check_Discriminant_Conformance
4004 while Present (Old_Discr) and then Present (New_Discr) loop
4006 New_Discr_Id := Defining_Identifier (New_Discr);
4008 -- The subtype mark of the discriminant on the full type has not
4009 -- been analyzed so we do it here. For an access discriminant a new
4012 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4014 Access_Definition (N, Discriminant_Type (New_Discr));
4017 Analyze (Discriminant_Type (New_Discr));
4018 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4021 if not Conforming_Types
4022 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4024 Conformance_Error ("type of & does not match!", New_Discr_Id);
4027 -- Treat the new discriminant as an occurrence of the old one,
4028 -- for navigation purposes, and fill in some semantic
4029 -- information, for completeness.
4031 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4032 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4033 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4038 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4039 Conformance_Error ("name & does not match!", New_Discr_Id);
4043 -- Default expressions must match
4046 NewD : constant Boolean :=
4047 Present (Expression (New_Discr));
4048 OldD : constant Boolean :=
4049 Present (Expression (Parent (Old_Discr)));
4052 if NewD or OldD then
4054 -- The old default value has been analyzed and expanded,
4055 -- because the current full declaration will have frozen
4056 -- everything before. The new default values have not been
4057 -- expanded, so expand now to check conformance.
4060 Preanalyze_Spec_Expression
4061 (Expression (New_Discr), New_Discr_Type);
4064 if not (NewD and OldD)
4065 or else not Fully_Conformant_Expressions
4066 (Expression (Parent (Old_Discr)),
4067 Expression (New_Discr))
4071 ("default expression for & does not match!",
4078 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4080 if Ada_Version = Ada_83 then
4082 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4085 -- Grouping (use of comma in param lists) must be the same
4086 -- This is where we catch a misconformance like:
4089 -- A : Integer; B : Integer
4091 -- which are represented identically in the tree except
4092 -- for the setting of the flags More_Ids and Prev_Ids.
4094 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4095 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4098 ("grouping of & does not match!", New_Discr_Id);
4104 Next_Discriminant (Old_Discr);
4108 if Present (Old_Discr) then
4109 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4112 elsif Present (New_Discr) then
4114 ("too many discriminants!", Defining_Identifier (New_Discr));
4117 end Check_Discriminant_Conformance;
4119 ----------------------------
4120 -- Check_Fully_Conformant --
4121 ----------------------------
4123 procedure Check_Fully_Conformant
4124 (New_Id : Entity_Id;
4126 Err_Loc : Node_Id := Empty)
4129 pragma Warnings (Off, Result);
4132 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4133 end Check_Fully_Conformant;
4135 ---------------------------
4136 -- Check_Mode_Conformant --
4137 ---------------------------
4139 procedure Check_Mode_Conformant
4140 (New_Id : Entity_Id;
4142 Err_Loc : Node_Id := Empty;
4143 Get_Inst : Boolean := False)
4146 pragma Warnings (Off, Result);
4149 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4150 end Check_Mode_Conformant;
4152 --------------------------------
4153 -- Check_Overriding_Indicator --
4154 --------------------------------
4156 procedure Check_Overriding_Indicator
4158 Overridden_Subp : Entity_Id;
4159 Is_Primitive : Boolean)
4165 -- No overriding indicator for literals
4167 if Ekind (Subp) = E_Enumeration_Literal then
4170 elsif Ekind (Subp) = E_Entry then
4171 Decl := Parent (Subp);
4174 Decl := Unit_Declaration_Node (Subp);
4177 if Nkind_In (Decl, N_Subprogram_Body,
4178 N_Subprogram_Body_Stub,
4179 N_Subprogram_Declaration,
4180 N_Abstract_Subprogram_Declaration,
4181 N_Subprogram_Renaming_Declaration)
4183 Spec := Specification (Decl);
4185 elsif Nkind (Decl) = N_Entry_Declaration then
4192 if Present (Overridden_Subp) then
4193 if Must_Not_Override (Spec) then
4194 Error_Msg_Sloc := Sloc (Overridden_Subp);
4196 if Ekind (Subp) = E_Entry then
4198 ("entry & overrides inherited operation #", Spec, Subp);
4201 ("subprogram & overrides inherited operation #", Spec, Subp);
4204 elsif Is_Subprogram (Subp) then
4205 Set_Is_Overriding_Operation (Subp);
4208 -- If primitive flag is set, operation is overriding at the
4209 -- point of its declaration, so warn if necessary. Otherwise
4210 -- it may have been declared before the operation it overrides
4211 -- and no check is required.
4214 and then not Must_Override (Spec)
4215 and then Is_Primitive
4217 Style.Missing_Overriding (Decl, Subp);
4220 -- If Subp is an operator, it may override a predefined operation.
4221 -- In that case overridden_subp is empty because of our implicit
4222 -- representation for predefined operators. We have to check whether the
4223 -- signature of Subp matches that of a predefined operator. Note that
4224 -- first argument provides the name of the operator, and the second
4225 -- argument the signature that may match that of a standard operation.
4226 -- If the indicator is overriding, then the operator must match a
4227 -- predefined signature, because we know already that there is no
4228 -- explicit overridden operation.
4230 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4232 if Must_Not_Override (Spec) then
4233 if not Is_Primitive then
4235 ("overriding indicator only allowed "
4236 & "if subprogram is primitive", Subp);
4238 elsif Operator_Matches_Spec (Subp, Subp) then
4240 ("subprogram & overrides predefined operator ", Spec, Subp);
4243 elsif Must_Override (Spec) then
4244 if Is_Overriding_Operation (Subp) then
4245 Set_Is_Overriding_Operation (Subp);
4247 elsif not Operator_Matches_Spec (Subp, Subp) then
4248 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4251 elsif not Error_Posted (Subp)
4252 and then Style_Check
4253 and then Operator_Matches_Spec (Subp, Subp)
4255 not Is_Predefined_File_Name
4256 (Unit_File_Name (Get_Source_Unit (Subp)))
4258 Set_Is_Overriding_Operation (Subp);
4259 Style.Missing_Overriding (Decl, Subp);
4262 elsif Must_Override (Spec) then
4263 if Ekind (Subp) = E_Entry then
4264 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4266 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4269 -- If the operation is marked "not overriding" and it's not primitive
4270 -- then an error is issued, unless this is an operation of a task or
4271 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4272 -- has been specified have already been checked above.
4274 elsif Must_Not_Override (Spec)
4275 and then not Is_Primitive
4276 and then Ekind (Subp) /= E_Entry
4277 and then Ekind (Scope (Subp)) /= E_Protected_Type
4280 ("overriding indicator only allowed if subprogram is primitive",
4284 end Check_Overriding_Indicator;
4290 -- Note: this procedure needs to know far too much about how the expander
4291 -- messes with exceptions. The use of the flag Exception_Junk and the
4292 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4293 -- works, but is not very clean. It would be better if the expansion
4294 -- routines would leave Original_Node working nicely, and we could use
4295 -- Original_Node here to ignore all the peculiar expander messing ???
4297 procedure Check_Returns
4301 Proc : Entity_Id := Empty)
4305 procedure Check_Statement_Sequence (L : List_Id);
4306 -- Internal recursive procedure to check a list of statements for proper
4307 -- termination by a return statement (or a transfer of control or a
4308 -- compound statement that is itself internally properly terminated).
4310 ------------------------------
4311 -- Check_Statement_Sequence --
4312 ------------------------------
4314 procedure Check_Statement_Sequence (L : List_Id) is
4319 Raise_Exception_Call : Boolean;
4320 -- Set True if statement sequence terminated by Raise_Exception call
4321 -- or a Reraise_Occurrence call.
4324 Raise_Exception_Call := False;
4326 -- Get last real statement
4328 Last_Stm := Last (L);
4330 -- Deal with digging out exception handler statement sequences that
4331 -- have been transformed by the local raise to goto optimization.
4332 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4333 -- optimization has occurred, we are looking at something like:
4336 -- original stmts in block
4340 -- goto L1; | omitted if No_Exception_Propagation
4345 -- goto L3; -- skip handler when exception not raised
4347 -- <<L1>> -- target label for local exception
4361 -- and what we have to do is to dig out the estmts1 and estmts2
4362 -- sequences (which were the original sequences of statements in
4363 -- the exception handlers) and check them.
4365 if Nkind (Last_Stm) = N_Label
4366 and then Exception_Junk (Last_Stm)
4372 exit when Nkind (Stm) /= N_Block_Statement;
4373 exit when not Exception_Junk (Stm);
4376 exit when Nkind (Stm) /= N_Label;
4377 exit when not Exception_Junk (Stm);
4378 Check_Statement_Sequence
4379 (Statements (Handled_Statement_Sequence (Next (Stm))));
4384 exit when Nkind (Stm) /= N_Goto_Statement;
4385 exit when not Exception_Junk (Stm);
4389 -- Don't count pragmas
4391 while Nkind (Last_Stm) = N_Pragma
4393 -- Don't count call to SS_Release (can happen after Raise_Exception)
4396 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4398 Nkind (Name (Last_Stm)) = N_Identifier
4400 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4402 -- Don't count exception junk
4405 (Nkind_In (Last_Stm, N_Goto_Statement,
4407 N_Object_Declaration)
4408 and then Exception_Junk (Last_Stm))
4409 or else Nkind (Last_Stm) in N_Push_xxx_Label
4410 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4415 -- Here we have the "real" last statement
4417 Kind := Nkind (Last_Stm);
4419 -- Transfer of control, OK. Note that in the No_Return procedure
4420 -- case, we already diagnosed any explicit return statements, so
4421 -- we can treat them as OK in this context.
4423 if Is_Transfer (Last_Stm) then
4426 -- Check cases of explicit non-indirect procedure calls
4428 elsif Kind = N_Procedure_Call_Statement
4429 and then Is_Entity_Name (Name (Last_Stm))
4431 -- Check call to Raise_Exception procedure which is treated
4432 -- specially, as is a call to Reraise_Occurrence.
4434 -- We suppress the warning in these cases since it is likely that
4435 -- the programmer really does not expect to deal with the case
4436 -- of Null_Occurrence, and thus would find a warning about a
4437 -- missing return curious, and raising Program_Error does not
4438 -- seem such a bad behavior if this does occur.
4440 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4441 -- behavior will be to raise Constraint_Error (see AI-329).
4443 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4445 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4447 Raise_Exception_Call := True;
4449 -- For Raise_Exception call, test first argument, if it is
4450 -- an attribute reference for a 'Identity call, then we know
4451 -- that the call cannot possibly return.
4454 Arg : constant Node_Id :=
4455 Original_Node (First_Actual (Last_Stm));
4457 if Nkind (Arg) = N_Attribute_Reference
4458 and then Attribute_Name (Arg) = Name_Identity
4465 -- If statement, need to look inside if there is an else and check
4466 -- each constituent statement sequence for proper termination.
4468 elsif Kind = N_If_Statement
4469 and then Present (Else_Statements (Last_Stm))
4471 Check_Statement_Sequence (Then_Statements (Last_Stm));
4472 Check_Statement_Sequence (Else_Statements (Last_Stm));
4474 if Present (Elsif_Parts (Last_Stm)) then
4476 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4479 while Present (Elsif_Part) loop
4480 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4488 -- Case statement, check each case for proper termination
4490 elsif Kind = N_Case_Statement then
4494 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4495 while Present (Case_Alt) loop
4496 Check_Statement_Sequence (Statements (Case_Alt));
4497 Next_Non_Pragma (Case_Alt);
4503 -- Block statement, check its handled sequence of statements
4505 elsif Kind = N_Block_Statement then
4511 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
4520 -- Loop statement. If there is an iteration scheme, we can definitely
4521 -- fall out of the loop. Similarly if there is an exit statement, we
4522 -- can fall out. In either case we need a following return.
4524 elsif Kind = N_Loop_Statement then
4525 if Present (Iteration_Scheme (Last_Stm))
4526 or else Has_Exit (Entity (Identifier (Last_Stm)))
4530 -- A loop with no exit statement or iteration scheme is either
4531 -- an infinite loop, or it has some other exit (raise/return).
4532 -- In either case, no warning is required.
4538 -- Timed entry call, check entry call and delay alternatives
4540 -- Note: in expanded code, the timed entry call has been converted
4541 -- to a set of expanded statements on which the check will work
4542 -- correctly in any case.
4544 elsif Kind = N_Timed_Entry_Call then
4546 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4547 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
4550 -- If statement sequence of entry call alternative is missing,
4551 -- then we can definitely fall through, and we post the error
4552 -- message on the entry call alternative itself.
4554 if No (Statements (ECA)) then
4557 -- If statement sequence of delay alternative is missing, then
4558 -- we can definitely fall through, and we post the error
4559 -- message on the delay alternative itself.
4561 -- Note: if both ECA and DCA are missing the return, then we
4562 -- post only one message, should be enough to fix the bugs.
4563 -- If not we will get a message next time on the DCA when the
4566 elsif No (Statements (DCA)) then
4569 -- Else check both statement sequences
4572 Check_Statement_Sequence (Statements (ECA));
4573 Check_Statement_Sequence (Statements (DCA));
4578 -- Conditional entry call, check entry call and else part
4580 -- Note: in expanded code, the conditional entry call has been
4581 -- converted to a set of expanded statements on which the check
4582 -- will work correctly in any case.
4584 elsif Kind = N_Conditional_Entry_Call then
4586 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
4589 -- If statement sequence of entry call alternative is missing,
4590 -- then we can definitely fall through, and we post the error
4591 -- message on the entry call alternative itself.
4593 if No (Statements (ECA)) then
4596 -- Else check statement sequence and else part
4599 Check_Statement_Sequence (Statements (ECA));
4600 Check_Statement_Sequence (Else_Statements (Last_Stm));
4606 -- If we fall through, issue appropriate message
4609 if not Raise_Exception_Call then
4611 ("?RETURN statement missing following this statement!",
4614 ("\?Program_Error may be raised at run time!",
4618 -- Note: we set Err even though we have not issued a warning
4619 -- because we still have a case of a missing return. This is
4620 -- an extremely marginal case, probably will never be noticed
4621 -- but we might as well get it right.
4625 -- Otherwise we have the case of a procedure marked No_Return
4628 if not Raise_Exception_Call then
4630 ("?implied return after this statement " &
4631 "will raise Program_Error",
4634 ("\?procedure & is marked as No_Return!",
4639 RE : constant Node_Id :=
4640 Make_Raise_Program_Error (Sloc (Last_Stm),
4641 Reason => PE_Implicit_Return);
4643 Insert_After (Last_Stm, RE);
4647 end Check_Statement_Sequence;
4649 -- Start of processing for Check_Returns
4653 Check_Statement_Sequence (Statements (HSS));
4655 if Present (Exception_Handlers (HSS)) then
4656 Handler := First_Non_Pragma (Exception_Handlers (HSS));
4657 while Present (Handler) loop
4658 Check_Statement_Sequence (Statements (Handler));
4659 Next_Non_Pragma (Handler);
4664 ----------------------------
4665 -- Check_Subprogram_Order --
4666 ----------------------------
4668 procedure Check_Subprogram_Order (N : Node_Id) is
4670 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
4671 -- This is used to check if S1 > S2 in the sense required by this
4672 -- test, for example nameab < namec, but name2 < name10.
4674 -----------------------------
4675 -- Subprogram_Name_Greater --
4676 -----------------------------
4678 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
4683 -- Remove trailing numeric parts
4686 while S1 (L1) in '0' .. '9' loop
4691 while S2 (L2) in '0' .. '9' loop
4695 -- If non-numeric parts non-equal, that's decisive
4697 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
4700 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
4703 -- If non-numeric parts equal, compare suffixed numeric parts. Note
4704 -- that a missing suffix is treated as numeric zero in this test.
4708 while L1 < S1'Last loop
4710 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
4714 while L2 < S2'Last loop
4716 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
4721 end Subprogram_Name_Greater;
4723 -- Start of processing for Check_Subprogram_Order
4726 -- Check body in alpha order if this is option
4729 and then Style_Check_Order_Subprograms
4730 and then Nkind (N) = N_Subprogram_Body
4731 and then Comes_From_Source (N)
4732 and then In_Extended_Main_Source_Unit (N)
4736 renames Scope_Stack.Table
4737 (Scope_Stack.Last).Last_Subprogram_Name;
4739 Body_Id : constant Entity_Id :=
4740 Defining_Entity (Specification (N));
4743 Get_Decoded_Name_String (Chars (Body_Id));
4746 if Subprogram_Name_Greater
4747 (LSN.all, Name_Buffer (1 .. Name_Len))
4749 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
4755 LSN := new String'(Name_Buffer (1 .. Name_Len));
4758 end Check_Subprogram_Order;
4760 ------------------------------
4761 -- Check_Subtype_Conformant --
4762 ------------------------------
4764 procedure Check_Subtype_Conformant
4765 (New_Id : Entity_Id;
4767 Err_Loc : Node_Id := Empty;
4768 Skip_Controlling_Formals : Boolean := False)
4771 pragma Warnings (Off, Result);
4774 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
4775 Skip_Controlling_Formals => Skip_Controlling_Formals);
4776 end Check_Subtype_Conformant;
4778 ---------------------------
4779 -- Check_Type_Conformant --
4780 ---------------------------
4782 procedure Check_Type_Conformant
4783 (New_Id : Entity_Id;
4785 Err_Loc : Node_Id := Empty)
4788 pragma Warnings (Off, Result);
4791 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
4792 end Check_Type_Conformant;
4794 ----------------------
4795 -- Conforming_Types --
4796 ----------------------
4798 function Conforming_Types
4801 Ctype : Conformance_Type;
4802 Get_Inst : Boolean := False) return Boolean
4804 Type_1 : Entity_Id := T1;
4805 Type_2 : Entity_Id := T2;
4806 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
4808 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
4809 -- If neither T1 nor T2 are generic actual types, or if they are in
4810 -- different scopes (e.g. parent and child instances), then verify that
4811 -- the base types are equal. Otherwise T1 and T2 must be on the same
4812 -- subtype chain. The whole purpose of this procedure is to prevent
4813 -- spurious ambiguities in an instantiation that may arise if two
4814 -- distinct generic types are instantiated with the same actual.
4816 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
4817 -- An access parameter can designate an incomplete type. If the
4818 -- incomplete type is the limited view of a type from a limited_
4819 -- with_clause, check whether the non-limited view is available. If
4820 -- it is a (non-limited) incomplete type, get the full view.
4822 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
4823 -- Returns True if and only if either T1 denotes a limited view of T2
4824 -- or T2 denotes a limited view of T1. This can arise when the limited
4825 -- with view of a type is used in a subprogram declaration and the
4826 -- subprogram body is in the scope of a regular with clause for the
4827 -- same unit. In such a case, the two type entities can be considered
4828 -- identical for purposes of conformance checking.
4830 ----------------------
4831 -- Base_Types_Match --
4832 ----------------------
4834 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
4839 elsif Base_Type (T1) = Base_Type (T2) then
4841 -- The following is too permissive. A more precise test should
4842 -- check that the generic actual is an ancestor subtype of the
4845 return not Is_Generic_Actual_Type (T1)
4846 or else not Is_Generic_Actual_Type (T2)
4847 or else Scope (T1) /= Scope (T2);
4852 end Base_Types_Match;
4854 --------------------------
4855 -- Find_Designated_Type --
4856 --------------------------
4858 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
4862 Desig := Directly_Designated_Type (T);
4864 if Ekind (Desig) = E_Incomplete_Type then
4866 -- If regular incomplete type, get full view if available
4868 if Present (Full_View (Desig)) then
4869 Desig := Full_View (Desig);
4871 -- If limited view of a type, get non-limited view if available,
4872 -- and check again for a regular incomplete type.
4874 elsif Present (Non_Limited_View (Desig)) then
4875 Desig := Get_Full_View (Non_Limited_View (Desig));
4880 end Find_Designated_Type;
4882 -------------------------------
4883 -- Matches_Limited_With_View --
4884 -------------------------------
4886 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
4888 -- In some cases a type imported through a limited_with clause, and
4889 -- its nonlimited view are both visible, for example in an anonymous
4890 -- access-to-class-wide type in a formal. Both entities designate the
4893 if From_With_Type (T1)
4894 and then T2 = Available_View (T1)
4898 elsif From_With_Type (T2)
4899 and then T1 = Available_View (T2)
4906 end Matches_Limited_With_View;
4908 -- Start of processing for Conforming_Types
4911 -- The context is an instance association for a formal
4912 -- access-to-subprogram type; the formal parameter types require
4913 -- mapping because they may denote other formal parameters of the
4917 Type_1 := Get_Instance_Of (T1);
4918 Type_2 := Get_Instance_Of (T2);
4921 -- If one of the types is a view of the other introduced by a limited
4922 -- with clause, treat these as conforming for all purposes.
4924 if Matches_Limited_With_View (T1, T2) then
4927 elsif Base_Types_Match (Type_1, Type_2) then
4928 return Ctype <= Mode_Conformant
4929 or else Subtypes_Statically_Match (Type_1, Type_2);
4931 elsif Is_Incomplete_Or_Private_Type (Type_1)
4932 and then Present (Full_View (Type_1))
4933 and then Base_Types_Match (Full_View (Type_1), Type_2)
4935 return Ctype <= Mode_Conformant
4936 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
4938 elsif Ekind (Type_2) = E_Incomplete_Type
4939 and then Present (Full_View (Type_2))
4940 and then Base_Types_Match (Type_1, Full_View (Type_2))
4942 return Ctype <= Mode_Conformant
4943 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4945 elsif Is_Private_Type (Type_2)
4946 and then In_Instance
4947 and then Present (Full_View (Type_2))
4948 and then Base_Types_Match (Type_1, Full_View (Type_2))
4950 return Ctype <= Mode_Conformant
4951 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
4954 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
4955 -- treated recursively because they carry a signature.
4957 Are_Anonymous_Access_To_Subprogram_Types :=
4958 Ekind (Type_1) = Ekind (Type_2)
4960 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
4962 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
4964 -- Test anonymous access type case. For this case, static subtype
4965 -- matching is required for mode conformance (RM 6.3.1(15)). We check
4966 -- the base types because we may have built internal subtype entities
4967 -- to handle null-excluding types (see Process_Formals).
4969 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
4971 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
4972 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
4975 Desig_1 : Entity_Id;
4976 Desig_2 : Entity_Id;
4979 -- In Ada2005, access constant indicators must match for
4980 -- subtype conformance.
4982 if Ada_Version >= Ada_05
4983 and then Ctype >= Subtype_Conformant
4985 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
4990 Desig_1 := Find_Designated_Type (Type_1);
4992 Desig_2 := Find_Designated_Type (Type_2);
4994 -- If the context is an instance association for a formal
4995 -- access-to-subprogram type; formal access parameter designated
4996 -- types require mapping because they may denote other formal
4997 -- parameters of the generic unit.
5000 Desig_1 := Get_Instance_Of (Desig_1);
5001 Desig_2 := Get_Instance_Of (Desig_2);
5004 -- It is possible for a Class_Wide_Type to be introduced for an
5005 -- incomplete type, in which case there is a separate class_ wide
5006 -- type for the full view. The types conform if their Etypes
5007 -- conform, i.e. one may be the full view of the other. This can
5008 -- only happen in the context of an access parameter, other uses
5009 -- of an incomplete Class_Wide_Type are illegal.
5011 if Is_Class_Wide_Type (Desig_1)
5012 and then Is_Class_Wide_Type (Desig_2)
5016 (Etype (Base_Type (Desig_1)),
5017 Etype (Base_Type (Desig_2)), Ctype);
5019 elsif Are_Anonymous_Access_To_Subprogram_Types then
5020 if Ada_Version < Ada_05 then
5021 return Ctype = Type_Conformant
5023 Subtypes_Statically_Match (Desig_1, Desig_2);
5025 -- We must check the conformance of the signatures themselves
5029 Conformant : Boolean;
5032 (Desig_1, Desig_2, Ctype, False, Conformant);
5038 return Base_Type (Desig_1) = Base_Type (Desig_2)
5039 and then (Ctype = Type_Conformant
5041 Subtypes_Statically_Match (Desig_1, Desig_2));
5045 -- Otherwise definitely no match
5048 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5049 and then Is_Access_Type (Type_2))
5050 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5051 and then Is_Access_Type (Type_1)))
5054 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5056 May_Hide_Profile := True;
5061 end Conforming_Types;
5063 --------------------------
5064 -- Create_Extra_Formals --
5065 --------------------------
5067 procedure Create_Extra_Formals (E : Entity_Id) is
5069 First_Extra : Entity_Id := Empty;
5070 Last_Extra : Entity_Id;
5071 Formal_Type : Entity_Id;
5072 P_Formal : Entity_Id := Empty;
5074 function Add_Extra_Formal
5075 (Assoc_Entity : Entity_Id;
5078 Suffix : String) return Entity_Id;
5079 -- Add an extra formal to the current list of formals and extra formals.
5080 -- The extra formal is added to the end of the list of extra formals,
5081 -- and also returned as the result. These formals are always of mode IN.
5082 -- The new formal has the type Typ, is declared in Scope, and its name
5083 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5085 ----------------------
5086 -- Add_Extra_Formal --
5087 ----------------------
5089 function Add_Extra_Formal
5090 (Assoc_Entity : Entity_Id;
5093 Suffix : String) return Entity_Id
5095 EF : constant Entity_Id :=
5096 Make_Defining_Identifier (Sloc (Assoc_Entity),
5097 Chars => New_External_Name (Chars (Assoc_Entity),
5101 -- A little optimization. Never generate an extra formal for the
5102 -- _init operand of an initialization procedure, since it could
5105 if Chars (Formal) = Name_uInit then
5109 Set_Ekind (EF, E_In_Parameter);
5110 Set_Actual_Subtype (EF, Typ);
5111 Set_Etype (EF, Typ);
5112 Set_Scope (EF, Scope);
5113 Set_Mechanism (EF, Default_Mechanism);
5114 Set_Formal_Validity (EF);
5116 if No (First_Extra) then
5118 Set_Extra_Formals (Scope, First_Extra);
5121 if Present (Last_Extra) then
5122 Set_Extra_Formal (Last_Extra, EF);
5128 end Add_Extra_Formal;
5130 -- Start of processing for Create_Extra_Formals
5133 -- We never generate extra formals if expansion is not active
5134 -- because we don't need them unless we are generating code.
5136 if not Expander_Active then
5140 -- If this is a derived subprogram then the subtypes of the parent
5141 -- subprogram's formal parameters will be used to determine the need
5142 -- for extra formals.
5144 if Is_Overloadable (E) and then Present (Alias (E)) then
5145 P_Formal := First_Formal (Alias (E));
5148 Last_Extra := Empty;
5149 Formal := First_Formal (E);
5150 while Present (Formal) loop
5151 Last_Extra := Formal;
5152 Next_Formal (Formal);
5155 -- If Extra_formals were already created, don't do it again. This
5156 -- situation may arise for subprogram types created as part of
5157 -- dispatching calls (see Expand_Dispatching_Call)
5159 if Present (Last_Extra) and then
5160 Present (Extra_Formal (Last_Extra))
5165 -- If the subprogram is a predefined dispatching subprogram then don't
5166 -- generate any extra constrained or accessibility level formals. In
5167 -- general we suppress these for internal subprograms (by not calling
5168 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5169 -- generated stream attributes do get passed through because extra
5170 -- build-in-place formals are needed in some cases (limited 'Input).
5172 if Is_Predefined_Dispatching_Operation (E) then
5173 goto Test_For_BIP_Extras;
5176 Formal := First_Formal (E);
5177 while Present (Formal) loop
5179 -- Create extra formal for supporting the attribute 'Constrained.
5180 -- The case of a private type view without discriminants also
5181 -- requires the extra formal if the underlying type has defaulted
5184 if Ekind (Formal) /= E_In_Parameter then
5185 if Present (P_Formal) then
5186 Formal_Type := Etype (P_Formal);
5188 Formal_Type := Etype (Formal);
5191 -- Do not produce extra formals for Unchecked_Union parameters.
5192 -- Jump directly to the end of the loop.
5194 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5195 goto Skip_Extra_Formal_Generation;
5198 if not Has_Discriminants (Formal_Type)
5199 and then Ekind (Formal_Type) in Private_Kind
5200 and then Present (Underlying_Type (Formal_Type))
5202 Formal_Type := Underlying_Type (Formal_Type);
5205 if Has_Discriminants (Formal_Type)
5206 and then not Is_Constrained (Formal_Type)
5207 and then not Is_Indefinite_Subtype (Formal_Type)
5209 Set_Extra_Constrained
5210 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "F"));
5214 -- Create extra formal for supporting accessibility checking. This
5215 -- is done for both anonymous access formals and formals of named
5216 -- access types that are marked as controlling formals. The latter
5217 -- case can occur when Expand_Dispatching_Call creates a subprogram
5218 -- type and substitutes the types of access-to-class-wide actuals
5219 -- for the anonymous access-to-specific-type of controlling formals.
5220 -- Base_Type is applied because in cases where there is a null
5221 -- exclusion the formal may have an access subtype.
5223 -- This is suppressed if we specifically suppress accessibility
5224 -- checks at the package level for either the subprogram, or the
5225 -- package in which it resides. However, we do not suppress it
5226 -- simply if the scope has accessibility checks suppressed, since
5227 -- this could cause trouble when clients are compiled with a
5228 -- different suppression setting. The explicit checks at the
5229 -- package level are safe from this point of view.
5231 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5232 or else (Is_Controlling_Formal (Formal)
5233 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5235 (Explicit_Suppress (E, Accessibility_Check)
5237 Explicit_Suppress (Scope (E), Accessibility_Check))
5240 or else Present (Extra_Accessibility (P_Formal)))
5242 -- Temporary kludge: for now we avoid creating the extra formal
5243 -- for access parameters of protected operations because of
5244 -- problem with the case of internal protected calls. ???
5246 if Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Definition
5247 and then Nkind (Parent (Parent (Parent (E)))) /= N_Protected_Body
5249 Set_Extra_Accessibility
5250 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "F"));
5254 -- This label is required when skipping extra formal generation for
5255 -- Unchecked_Union parameters.
5257 <<Skip_Extra_Formal_Generation>>
5259 if Present (P_Formal) then
5260 Next_Formal (P_Formal);
5263 Next_Formal (Formal);
5266 <<Test_For_BIP_Extras>>
5268 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5269 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5271 if Ada_Version >= Ada_05 and then Is_Build_In_Place_Function (E) then
5273 Result_Subt : constant Entity_Id := Etype (E);
5275 Discard : Entity_Id;
5276 pragma Warnings (Off, Discard);
5279 -- In the case of functions with unconstrained result subtypes,
5280 -- add a 3-state formal indicating whether the return object is
5281 -- allocated by the caller (0), or should be allocated by the
5282 -- callee on the secondary stack (1) or in the global heap (2).
5283 -- For the moment we just use Natural for the type of this formal.
5284 -- Note that this formal isn't usually needed in the case where
5285 -- the result subtype is constrained, but it is needed when the
5286 -- function has a tagged result, because generally such functions
5287 -- can be called in a dispatching context and such calls must be
5288 -- handled like calls to a class-wide function.
5290 if not Is_Constrained (Underlying_Type (Result_Subt))
5291 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5295 (E, Standard_Natural,
5296 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5299 -- In the case of functions whose result type has controlled
5300 -- parts, we have an extra formal of type
5301 -- System.Finalization_Implementation.Finalizable_Ptr_Ptr. That
5302 -- is, we are passing a pointer to a finalization list (which is
5303 -- itself a pointer). This extra formal is then passed along to
5304 -- Move_Final_List in case of successful completion of a return
5305 -- statement. We cannot pass an 'in out' parameter, because we
5306 -- need to update the finalization list during an abort-deferred
5307 -- region, rather than using copy-back after the function
5308 -- returns. This is true even if we are able to get away with
5309 -- having 'in out' parameters, which are normally illegal for
5310 -- functions. This formal is also needed when the function has
5313 if Needs_BIP_Final_List (E) then
5316 (E, RTE (RE_Finalizable_Ptr_Ptr),
5317 E, BIP_Formal_Suffix (BIP_Final_List));
5320 -- If the result type contains tasks, we have two extra formals:
5321 -- the master of the tasks to be created, and the caller's
5322 -- activation chain.
5324 if Has_Task (Result_Subt) then
5327 (E, RTE (RE_Master_Id),
5328 E, BIP_Formal_Suffix (BIP_Master));
5331 (E, RTE (RE_Activation_Chain_Access),
5332 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5335 -- All build-in-place functions get an extra formal that will be
5336 -- passed the address of the return object within the caller.
5339 Formal_Type : constant Entity_Id :=
5341 (E_Anonymous_Access_Type, E,
5342 Scope_Id => Scope (E));
5344 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5345 Set_Etype (Formal_Type, Formal_Type);
5346 Set_Depends_On_Private
5347 (Formal_Type, Has_Private_Component (Formal_Type));
5348 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5349 Set_Is_Access_Constant (Formal_Type, False);
5351 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5352 -- the designated type comes from the limited view (for
5353 -- back-end purposes).
5355 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5357 Layout_Type (Formal_Type);
5361 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5365 end Create_Extra_Formals;
5367 -----------------------------
5368 -- Enter_Overloaded_Entity --
5369 -----------------------------
5371 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5372 E : Entity_Id := Current_Entity_In_Scope (S);
5373 C_E : Entity_Id := Current_Entity (S);
5377 Set_Has_Homonym (E);
5378 Set_Has_Homonym (S);
5381 Set_Is_Immediately_Visible (S);
5382 Set_Scope (S, Current_Scope);
5384 -- Chain new entity if front of homonym in current scope, so that
5385 -- homonyms are contiguous.
5390 while Homonym (C_E) /= E loop
5391 C_E := Homonym (C_E);
5394 Set_Homonym (C_E, S);
5398 Set_Current_Entity (S);
5403 Append_Entity (S, Current_Scope);
5404 Set_Public_Status (S);
5406 if Debug_Flag_E then
5407 Write_Str ("New overloaded entity chain: ");
5408 Write_Name (Chars (S));
5411 while Present (E) loop
5412 Write_Str (" "); Write_Int (Int (E));
5419 -- Generate warning for hiding
5422 and then Comes_From_Source (S)
5423 and then In_Extended_Main_Source_Unit (S)
5430 -- Warn unless genuine overloading
5432 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5433 and then (Is_Immediately_Visible (E)
5435 Is_Potentially_Use_Visible (S))
5437 Error_Msg_Sloc := Sloc (E);
5438 Error_Msg_N ("declaration of & hides one#?", S);
5442 end Enter_Overloaded_Entity;
5444 -----------------------------
5445 -- Find_Corresponding_Spec --
5446 -----------------------------
5448 function Find_Corresponding_Spec
5450 Post_Error : Boolean := True) return Entity_Id
5452 Spec : constant Node_Id := Specification (N);
5453 Designator : constant Entity_Id := Defining_Entity (Spec);
5458 E := Current_Entity (Designator);
5459 while Present (E) loop
5461 -- We are looking for a matching spec. It must have the same scope,
5462 -- and the same name, and either be type conformant, or be the case
5463 -- of a library procedure spec and its body (which belong to one
5464 -- another regardless of whether they are type conformant or not).
5466 if Scope (E) = Current_Scope then
5467 if Current_Scope = Standard_Standard
5468 or else (Ekind (E) = Ekind (Designator)
5469 and then Type_Conformant (E, Designator))
5471 -- Within an instantiation, we know that spec and body are
5472 -- subtype conformant, because they were subtype conformant
5473 -- in the generic. We choose the subtype-conformant entity
5474 -- here as well, to resolve spurious ambiguities in the
5475 -- instance that were not present in the generic (i.e. when
5476 -- two different types are given the same actual). If we are
5477 -- looking for a spec to match a body, full conformance is
5481 Set_Convention (Designator, Convention (E));
5483 if Nkind (N) = N_Subprogram_Body
5484 and then Present (Homonym (E))
5485 and then not Fully_Conformant (E, Designator)
5489 elsif not Subtype_Conformant (E, Designator) then
5494 if not Has_Completion (E) then
5495 if Nkind (N) /= N_Subprogram_Body_Stub then
5496 Set_Corresponding_Spec (N, E);
5499 Set_Has_Completion (E);
5502 elsif Nkind (Parent (N)) = N_Subunit then
5504 -- If this is the proper body of a subunit, the completion
5505 -- flag is set when analyzing the stub.
5509 -- If E is an internal function with a controlling result
5510 -- that was created for an operation inherited by a null
5511 -- extension, it may be overridden by a body without a previous
5512 -- spec (one more reason why these should be shunned). In that
5513 -- case remove the generated body, because the current one is
5514 -- the explicit overriding.
5516 elsif Ekind (E) = E_Function
5517 and then Ada_Version >= Ada_05
5518 and then not Comes_From_Source (E)
5519 and then Has_Controlling_Result (E)
5520 and then Is_Null_Extension (Etype (E))
5521 and then Comes_From_Source (Spec)
5523 Set_Has_Completion (E, False);
5525 if Expander_Active then
5527 (Unit_Declaration_Node
5528 (Corresponding_Body (Unit_Declaration_Node (E))));
5531 -- If expansion is disabled, the wrapper function has not
5532 -- been generated, and this is the standard case of a late
5533 -- body overriding an inherited operation.
5539 -- If the body already exists, then this is an error unless
5540 -- the previous declaration is the implicit declaration of a
5541 -- derived subprogram, or this is a spurious overloading in an
5544 elsif No (Alias (E))
5545 and then not Is_Intrinsic_Subprogram (E)
5546 and then not In_Instance
5549 Error_Msg_Sloc := Sloc (E);
5551 if Is_Imported (E) then
5553 ("body not allowed for imported subprogram & declared#",
5556 Error_Msg_NE ("duplicate body for & declared#", N, E);
5560 -- Child units cannot be overloaded, so a conformance mismatch
5561 -- between body and a previous spec is an error.
5563 elsif Is_Child_Unit (E)
5565 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5567 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5572 ("body of child unit does not match previous declaration", N);
5580 -- On exit, we know that no previous declaration of subprogram exists
5583 end Find_Corresponding_Spec;
5585 ----------------------
5586 -- Fully_Conformant --
5587 ----------------------
5589 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5592 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5594 end Fully_Conformant;
5596 ----------------------------------
5597 -- Fully_Conformant_Expressions --
5598 ----------------------------------
5600 function Fully_Conformant_Expressions
5601 (Given_E1 : Node_Id;
5602 Given_E2 : Node_Id) return Boolean
5604 E1 : constant Node_Id := Original_Node (Given_E1);
5605 E2 : constant Node_Id := Original_Node (Given_E2);
5606 -- We always test conformance on original nodes, since it is possible
5607 -- for analysis and/or expansion to make things look as though they
5608 -- conform when they do not, e.g. by converting 1+2 into 3.
5610 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
5611 renames Fully_Conformant_Expressions;
5613 function FCL (L1, L2 : List_Id) return Boolean;
5614 -- Compare elements of two lists for conformance. Elements have to
5615 -- be conformant, and actuals inserted as default parameters do not
5616 -- match explicit actuals with the same value.
5618 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
5619 -- Compare an operator node with a function call
5625 function FCL (L1, L2 : List_Id) return Boolean is
5629 if L1 = No_List then
5635 if L2 = No_List then
5641 -- Compare two lists, skipping rewrite insertions (we want to
5642 -- compare the original trees, not the expanded versions!)
5645 if Is_Rewrite_Insertion (N1) then
5647 elsif Is_Rewrite_Insertion (N2) then
5653 elsif not FCE (N1, N2) then
5666 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
5667 Actuals : constant List_Id := Parameter_Associations (Call_Node);
5672 or else Entity (Op_Node) /= Entity (Name (Call_Node))
5677 Act := First (Actuals);
5679 if Nkind (Op_Node) in N_Binary_Op then
5680 if not FCE (Left_Opnd (Op_Node), Act) then
5687 return Present (Act)
5688 and then FCE (Right_Opnd (Op_Node), Act)
5689 and then No (Next (Act));
5693 -- Start of processing for Fully_Conformant_Expressions
5696 -- Non-conformant if paren count does not match. Note: if some idiot
5697 -- complains that we don't do this right for more than 3 levels of
5698 -- parentheses, they will be treated with the respect they deserve!
5700 if Paren_Count (E1) /= Paren_Count (E2) then
5703 -- If same entities are referenced, then they are conformant even if
5704 -- they have different forms (RM 8.3.1(19-20)).
5706 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
5707 if Present (Entity (E1)) then
5708 return Entity (E1) = Entity (E2)
5709 or else (Chars (Entity (E1)) = Chars (Entity (E2))
5710 and then Ekind (Entity (E1)) = E_Discriminant
5711 and then Ekind (Entity (E2)) = E_In_Parameter);
5713 elsif Nkind (E1) = N_Expanded_Name
5714 and then Nkind (E2) = N_Expanded_Name
5715 and then Nkind (Selector_Name (E1)) = N_Character_Literal
5716 and then Nkind (Selector_Name (E2)) = N_Character_Literal
5718 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
5721 -- Identifiers in component associations don't always have
5722 -- entities, but their names must conform.
5724 return Nkind (E1) = N_Identifier
5725 and then Nkind (E2) = N_Identifier
5726 and then Chars (E1) = Chars (E2);
5729 elsif Nkind (E1) = N_Character_Literal
5730 and then Nkind (E2) = N_Expanded_Name
5732 return Nkind (Selector_Name (E2)) = N_Character_Literal
5733 and then Chars (E1) = Chars (Selector_Name (E2));
5735 elsif Nkind (E2) = N_Character_Literal
5736 and then Nkind (E1) = N_Expanded_Name
5738 return Nkind (Selector_Name (E1)) = N_Character_Literal
5739 and then Chars (E2) = Chars (Selector_Name (E1));
5741 elsif Nkind (E1) in N_Op
5742 and then Nkind (E2) = N_Function_Call
5744 return FCO (E1, E2);
5746 elsif Nkind (E2) in N_Op
5747 and then Nkind (E1) = N_Function_Call
5749 return FCO (E2, E1);
5751 -- Otherwise we must have the same syntactic entity
5753 elsif Nkind (E1) /= Nkind (E2) then
5756 -- At this point, we specialize by node type
5763 FCL (Expressions (E1), Expressions (E2))
5764 and then FCL (Component_Associations (E1),
5765 Component_Associations (E2));
5768 if Nkind (Expression (E1)) = N_Qualified_Expression
5770 Nkind (Expression (E2)) = N_Qualified_Expression
5772 return FCE (Expression (E1), Expression (E2));
5774 -- Check that the subtype marks and any constraints
5779 Indic1 : constant Node_Id := Expression (E1);
5780 Indic2 : constant Node_Id := Expression (E2);
5785 if Nkind (Indic1) /= N_Subtype_Indication then
5787 Nkind (Indic2) /= N_Subtype_Indication
5788 and then Entity (Indic1) = Entity (Indic2);
5790 elsif Nkind (Indic2) /= N_Subtype_Indication then
5792 Nkind (Indic1) /= N_Subtype_Indication
5793 and then Entity (Indic1) = Entity (Indic2);
5796 if Entity (Subtype_Mark (Indic1)) /=
5797 Entity (Subtype_Mark (Indic2))
5802 Elt1 := First (Constraints (Constraint (Indic1)));
5803 Elt2 := First (Constraints (Constraint (Indic2)));
5804 while Present (Elt1) and then Present (Elt2) loop
5805 if not FCE (Elt1, Elt2) then
5818 when N_Attribute_Reference =>
5820 Attribute_Name (E1) = Attribute_Name (E2)
5821 and then FCL (Expressions (E1), Expressions (E2));
5825 Entity (E1) = Entity (E2)
5826 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
5827 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5829 when N_And_Then | N_Or_Else | N_Membership_Test =>
5831 FCE (Left_Opnd (E1), Left_Opnd (E2))
5833 FCE (Right_Opnd (E1), Right_Opnd (E2));
5835 when N_Character_Literal =>
5837 Char_Literal_Value (E1) = Char_Literal_Value (E2);
5839 when N_Component_Association =>
5841 FCL (Choices (E1), Choices (E2))
5842 and then FCE (Expression (E1), Expression (E2));
5844 when N_Conditional_Expression =>
5846 FCL (Expressions (E1), Expressions (E2));
5848 when N_Explicit_Dereference =>
5850 FCE (Prefix (E1), Prefix (E2));
5852 when N_Extension_Aggregate =>
5854 FCL (Expressions (E1), Expressions (E2))
5855 and then Null_Record_Present (E1) =
5856 Null_Record_Present (E2)
5857 and then FCL (Component_Associations (E1),
5858 Component_Associations (E2));
5860 when N_Function_Call =>
5862 FCE (Name (E1), Name (E2))
5863 and then FCL (Parameter_Associations (E1),
5864 Parameter_Associations (E2));
5866 when N_Indexed_Component =>
5868 FCE (Prefix (E1), Prefix (E2))
5869 and then FCL (Expressions (E1), Expressions (E2));
5871 when N_Integer_Literal =>
5872 return (Intval (E1) = Intval (E2));
5877 when N_Operator_Symbol =>
5879 Chars (E1) = Chars (E2);
5881 when N_Others_Choice =>
5884 when N_Parameter_Association =>
5886 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
5887 and then FCE (Explicit_Actual_Parameter (E1),
5888 Explicit_Actual_Parameter (E2));
5890 when N_Qualified_Expression =>
5892 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5893 and then FCE (Expression (E1), Expression (E2));
5897 FCE (Low_Bound (E1), Low_Bound (E2))
5898 and then FCE (High_Bound (E1), High_Bound (E2));
5900 when N_Real_Literal =>
5901 return (Realval (E1) = Realval (E2));
5903 when N_Selected_Component =>
5905 FCE (Prefix (E1), Prefix (E2))
5906 and then FCE (Selector_Name (E1), Selector_Name (E2));
5910 FCE (Prefix (E1), Prefix (E2))
5911 and then FCE (Discrete_Range (E1), Discrete_Range (E2));
5913 when N_String_Literal =>
5915 S1 : constant String_Id := Strval (E1);
5916 S2 : constant String_Id := Strval (E2);
5917 L1 : constant Nat := String_Length (S1);
5918 L2 : constant Nat := String_Length (S2);
5925 for J in 1 .. L1 loop
5926 if Get_String_Char (S1, J) /=
5927 Get_String_Char (S2, J)
5937 when N_Type_Conversion =>
5939 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5940 and then FCE (Expression (E1), Expression (E2));
5944 Entity (E1) = Entity (E2)
5945 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
5947 when N_Unchecked_Type_Conversion =>
5949 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
5950 and then FCE (Expression (E1), Expression (E2));
5952 -- All other node types cannot appear in this context. Strictly
5953 -- we should raise a fatal internal error. Instead we just ignore
5954 -- the nodes. This means that if anyone makes a mistake in the
5955 -- expander and mucks an expression tree irretrievably, the
5956 -- result will be a failure to detect a (probably very obscure)
5957 -- case of non-conformance, which is better than bombing on some
5958 -- case where two expressions do in fact conform.
5965 end Fully_Conformant_Expressions;
5967 ----------------------------------------
5968 -- Fully_Conformant_Discrete_Subtypes --
5969 ----------------------------------------
5971 function Fully_Conformant_Discrete_Subtypes
5972 (Given_S1 : Node_Id;
5973 Given_S2 : Node_Id) return Boolean
5975 S1 : constant Node_Id := Original_Node (Given_S1);
5976 S2 : constant Node_Id := Original_Node (Given_S2);
5978 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
5979 -- Special-case for a bound given by a discriminant, which in the body
5980 -- is replaced with the discriminal of the enclosing type.
5982 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
5983 -- Check both bounds
5985 -----------------------
5986 -- Conforming_Bounds --
5987 -----------------------
5989 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
5991 if Is_Entity_Name (B1)
5992 and then Is_Entity_Name (B2)
5993 and then Ekind (Entity (B1)) = E_Discriminant
5995 return Chars (B1) = Chars (B2);
5998 return Fully_Conformant_Expressions (B1, B2);
6000 end Conforming_Bounds;
6002 -----------------------
6003 -- Conforming_Ranges --
6004 -----------------------
6006 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6009 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6011 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6012 end Conforming_Ranges;
6014 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6017 if Nkind (S1) /= Nkind (S2) then
6020 elsif Is_Entity_Name (S1) then
6021 return Entity (S1) = Entity (S2);
6023 elsif Nkind (S1) = N_Range then
6024 return Conforming_Ranges (S1, S2);
6026 elsif Nkind (S1) = N_Subtype_Indication then
6028 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6031 (Range_Expression (Constraint (S1)),
6032 Range_Expression (Constraint (S2)));
6036 end Fully_Conformant_Discrete_Subtypes;
6038 --------------------
6039 -- Install_Entity --
6040 --------------------
6042 procedure Install_Entity (E : Entity_Id) is
6043 Prev : constant Entity_Id := Current_Entity (E);
6045 Set_Is_Immediately_Visible (E);
6046 Set_Current_Entity (E);
6047 Set_Homonym (E, Prev);
6050 ---------------------
6051 -- Install_Formals --
6052 ---------------------
6054 procedure Install_Formals (Id : Entity_Id) is
6057 F := First_Formal (Id);
6058 while Present (F) loop
6062 end Install_Formals;
6064 -----------------------------
6065 -- Is_Interface_Conformant --
6066 -----------------------------
6068 function Is_Interface_Conformant
6069 (Tagged_Type : Entity_Id;
6070 Iface_Prim : Entity_Id;
6071 Prim : Entity_Id) return Boolean
6073 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6074 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6077 pragma Assert (Is_Subprogram (Iface_Prim)
6078 and then Is_Subprogram (Prim)
6079 and then Is_Dispatching_Operation (Iface_Prim)
6080 and then Is_Dispatching_Operation (Prim));
6082 pragma Assert (Is_Interface (Iface)
6083 or else (Present (Alias (Iface_Prim))
6086 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6088 if Prim = Iface_Prim
6089 or else not Is_Subprogram (Prim)
6090 or else Ekind (Prim) /= Ekind (Iface_Prim)
6091 or else not Is_Dispatching_Operation (Prim)
6092 or else Scope (Prim) /= Scope (Tagged_Type)
6094 or else Base_Type (Typ) /= Tagged_Type
6095 or else not Primitive_Names_Match (Iface_Prim, Prim)
6099 -- Case of a procedure, or a function that does not have a controlling
6100 -- result (I or access I).
6102 elsif Ekind (Iface_Prim) = E_Procedure
6103 or else Etype (Prim) = Etype (Iface_Prim)
6104 or else not Has_Controlling_Result (Prim)
6106 return Type_Conformant (Prim, Iface_Prim,
6107 Skip_Controlling_Formals => True);
6109 -- Case of a function returning an interface, or an access to one.
6110 -- Check that the return types correspond.
6112 elsif Implements_Interface (Typ, Iface) then
6113 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6115 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6120 Type_Conformant (Prim, Iface_Prim,
6121 Skip_Controlling_Formals => True);
6127 end Is_Interface_Conformant;
6129 ---------------------------------
6130 -- Is_Non_Overriding_Operation --
6131 ---------------------------------
6133 function Is_Non_Overriding_Operation
6134 (Prev_E : Entity_Id;
6135 New_E : Entity_Id) return Boolean
6139 G_Typ : Entity_Id := Empty;
6141 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6142 -- If F_Type is a derived type associated with a generic actual subtype,
6143 -- then return its Generic_Parent_Type attribute, else return Empty.
6145 function Types_Correspond
6146 (P_Type : Entity_Id;
6147 N_Type : Entity_Id) return Boolean;
6148 -- Returns true if and only if the types (or designated types in the
6149 -- case of anonymous access types) are the same or N_Type is derived
6150 -- directly or indirectly from P_Type.
6152 -----------------------------
6153 -- Get_Generic_Parent_Type --
6154 -----------------------------
6156 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6161 if Is_Derived_Type (F_Typ)
6162 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6164 -- The tree must be traversed to determine the parent subtype in
6165 -- the generic unit, which unfortunately isn't always available
6166 -- via semantic attributes. ??? (Note: The use of Original_Node
6167 -- is needed for cases where a full derived type has been
6170 Indic := Subtype_Indication
6171 (Type_Definition (Original_Node (Parent (F_Typ))));
6173 if Nkind (Indic) = N_Subtype_Indication then
6174 G_Typ := Entity (Subtype_Mark (Indic));
6176 G_Typ := Entity (Indic);
6179 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6180 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6182 return Generic_Parent_Type (Parent (G_Typ));
6187 end Get_Generic_Parent_Type;
6189 ----------------------
6190 -- Types_Correspond --
6191 ----------------------
6193 function Types_Correspond
6194 (P_Type : Entity_Id;
6195 N_Type : Entity_Id) return Boolean
6197 Prev_Type : Entity_Id := Base_Type (P_Type);
6198 New_Type : Entity_Id := Base_Type (N_Type);
6201 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6202 Prev_Type := Designated_Type (Prev_Type);
6205 if Ekind (New_Type) = E_Anonymous_Access_Type then
6206 New_Type := Designated_Type (New_Type);
6209 if Prev_Type = New_Type then
6212 elsif not Is_Class_Wide_Type (New_Type) then
6213 while Etype (New_Type) /= New_Type loop
6214 New_Type := Etype (New_Type);
6215 if New_Type = Prev_Type then
6221 end Types_Correspond;
6223 -- Start of processing for Is_Non_Overriding_Operation
6226 -- In the case where both operations are implicit derived subprograms
6227 -- then neither overrides the other. This can only occur in certain
6228 -- obscure cases (e.g., derivation from homographs created in a generic
6231 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6234 elsif Ekind (Current_Scope) = E_Package
6235 and then Is_Generic_Instance (Current_Scope)
6236 and then In_Private_Part (Current_Scope)
6237 and then Comes_From_Source (New_E)
6239 -- We examine the formals and result subtype of the inherited
6240 -- operation, to determine whether their type is derived from (the
6241 -- instance of) a generic type.
6243 Formal := First_Formal (Prev_E);
6245 while Present (Formal) loop
6246 F_Typ := Base_Type (Etype (Formal));
6248 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6249 F_Typ := Designated_Type (F_Typ);
6252 G_Typ := Get_Generic_Parent_Type (F_Typ);
6254 Next_Formal (Formal);
6257 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6258 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6265 -- If the generic type is a private type, then the original operation
6266 -- was not overriding in the generic, because there was no primitive
6267 -- operation to override.
6269 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6270 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6271 N_Formal_Private_Type_Definition
6275 -- The generic parent type is the ancestor of a formal derived
6276 -- type declaration. We need to check whether it has a primitive
6277 -- operation that should be overridden by New_E in the generic.
6281 P_Formal : Entity_Id;
6282 N_Formal : Entity_Id;
6286 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6289 while Present (Prim_Elt) loop
6290 P_Prim := Node (Prim_Elt);
6292 if Chars (P_Prim) = Chars (New_E)
6293 and then Ekind (P_Prim) = Ekind (New_E)
6295 P_Formal := First_Formal (P_Prim);
6296 N_Formal := First_Formal (New_E);
6297 while Present (P_Formal) and then Present (N_Formal) loop
6298 P_Typ := Etype (P_Formal);
6299 N_Typ := Etype (N_Formal);
6301 if not Types_Correspond (P_Typ, N_Typ) then
6305 Next_Entity (P_Formal);
6306 Next_Entity (N_Formal);
6309 -- Found a matching primitive operation belonging to the
6310 -- formal ancestor type, so the new subprogram is
6314 and then No (N_Formal)
6315 and then (Ekind (New_E) /= E_Function
6318 (Etype (P_Prim), Etype (New_E)))
6324 Next_Elmt (Prim_Elt);
6327 -- If no match found, then the new subprogram does not
6328 -- override in the generic (nor in the instance).
6336 end Is_Non_Overriding_Operation;
6338 ------------------------------
6339 -- Make_Inequality_Operator --
6340 ------------------------------
6342 -- S is the defining identifier of an equality operator. We build a
6343 -- subprogram declaration with the right signature. This operation is
6344 -- intrinsic, because it is always expanded as the negation of the
6345 -- call to the equality function.
6347 procedure Make_Inequality_Operator (S : Entity_Id) is
6348 Loc : constant Source_Ptr := Sloc (S);
6351 Op_Name : Entity_Id;
6353 FF : constant Entity_Id := First_Formal (S);
6354 NF : constant Entity_Id := Next_Formal (FF);
6357 -- Check that equality was properly defined, ignore call if not
6364 A : constant Entity_Id :=
6365 Make_Defining_Identifier (Sloc (FF),
6366 Chars => Chars (FF));
6368 B : constant Entity_Id :=
6369 Make_Defining_Identifier (Sloc (NF),
6370 Chars => Chars (NF));
6373 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6375 Formals := New_List (
6376 Make_Parameter_Specification (Loc,
6377 Defining_Identifier => A,
6379 New_Reference_To (Etype (First_Formal (S)),
6380 Sloc (Etype (First_Formal (S))))),
6382 Make_Parameter_Specification (Loc,
6383 Defining_Identifier => B,
6385 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6386 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6389 Make_Subprogram_Declaration (Loc,
6391 Make_Function_Specification (Loc,
6392 Defining_Unit_Name => Op_Name,
6393 Parameter_Specifications => Formals,
6394 Result_Definition =>
6395 New_Reference_To (Standard_Boolean, Loc)));
6397 -- Insert inequality right after equality if it is explicit or after
6398 -- the derived type when implicit. These entities are created only
6399 -- for visibility purposes, and eventually replaced in the course of
6400 -- expansion, so they do not need to be attached to the tree and seen
6401 -- by the back-end. Keeping them internal also avoids spurious
6402 -- freezing problems. The declaration is inserted in the tree for
6403 -- analysis, and removed afterwards. If the equality operator comes
6404 -- from an explicit declaration, attach the inequality immediately
6405 -- after. Else the equality is inherited from a derived type
6406 -- declaration, so insert inequality after that declaration.
6408 if No (Alias (S)) then
6409 Insert_After (Unit_Declaration_Node (S), Decl);
6410 elsif Is_List_Member (Parent (S)) then
6411 Insert_After (Parent (S), Decl);
6413 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6416 Mark_Rewrite_Insertion (Decl);
6417 Set_Is_Intrinsic_Subprogram (Op_Name);
6420 Set_Has_Completion (Op_Name);
6421 Set_Corresponding_Equality (Op_Name, S);
6422 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6424 end Make_Inequality_Operator;
6426 ----------------------
6427 -- May_Need_Actuals --
6428 ----------------------
6430 procedure May_Need_Actuals (Fun : Entity_Id) is
6435 F := First_Formal (Fun);
6437 while Present (F) loop
6438 if No (Default_Value (F)) then
6446 Set_Needs_No_Actuals (Fun, B);
6447 end May_Need_Actuals;
6449 ---------------------
6450 -- Mode_Conformant --
6451 ---------------------
6453 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6456 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6458 end Mode_Conformant;
6460 ---------------------------
6461 -- New_Overloaded_Entity --
6462 ---------------------------
6464 procedure New_Overloaded_Entity
6466 Derived_Type : Entity_Id := Empty)
6468 Overridden_Subp : Entity_Id := Empty;
6469 -- Set if the current scope has an operation that is type-conformant
6470 -- with S, and becomes hidden by S.
6472 Is_Primitive_Subp : Boolean;
6473 -- Set to True if the new subprogram is primitive
6476 -- Entity that S overrides
6478 Prev_Vis : Entity_Id := Empty;
6479 -- Predecessor of E in Homonym chain
6481 procedure Check_For_Primitive_Subprogram
6482 (Is_Primitive : out Boolean;
6483 Is_Overriding : Boolean := False);
6484 -- If the subprogram being analyzed is a primitive operation of the type
6485 -- of a formal or result, set the Has_Primitive_Operations flag on the
6486 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6487 -- corresponding flag on the entity itself for later use.
6489 procedure Check_Synchronized_Overriding
6490 (Def_Id : Entity_Id;
6491 Overridden_Subp : out Entity_Id);
6492 -- First determine if Def_Id is an entry or a subprogram either defined
6493 -- in the scope of a task or protected type, or is a primitive of such
6494 -- a type. Check whether Def_Id overrides a subprogram of an interface
6495 -- implemented by the synchronized type, return the overridden entity
6498 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6499 -- Check that E is declared in the private part of the current package,
6500 -- or in the package body, where it may hide a previous declaration.
6501 -- We can't use In_Private_Part by itself because this flag is also
6502 -- set when freezing entities, so we must examine the place of the
6503 -- declaration in the tree, and recognize wrapper packages as well.
6505 function Is_Overriding_Alias
6507 New_E : Entity_Id) return Boolean;
6508 -- Check whether new subprogram and old subprogram are both inherited
6509 -- from subprograms that have distinct dispatch table entries. This can
6510 -- occur with derivations from instances with accidental homonyms.
6511 -- The function is conservative given that the converse is only true
6512 -- within instances that contain accidental overloadings.
6514 ------------------------------------
6515 -- Check_For_Primitive_Subprogram --
6516 ------------------------------------
6518 procedure Check_For_Primitive_Subprogram
6519 (Is_Primitive : out Boolean;
6520 Is_Overriding : Boolean := False)
6526 function Visible_Part_Type (T : Entity_Id) return Boolean;
6527 -- Returns true if T is declared in the visible part of the current
6528 -- package scope; otherwise returns false. Assumes that T is declared
6531 procedure Check_Private_Overriding (T : Entity_Id);
6532 -- Checks that if a primitive abstract subprogram of a visible
6533 -- abstract type is declared in a private part, then it must override
6534 -- an abstract subprogram declared in the visible part. Also checks
6535 -- that if a primitive function with a controlling result is declared
6536 -- in a private part, then it must override a function declared in
6537 -- the visible part.
6539 ------------------------------
6540 -- Check_Private_Overriding --
6541 ------------------------------
6543 procedure Check_Private_Overriding (T : Entity_Id) is
6545 if Is_Package_Or_Generic_Package (Current_Scope)
6546 and then In_Private_Part (Current_Scope)
6547 and then Visible_Part_Type (T)
6548 and then not In_Instance
6550 if Is_Abstract_Type (T)
6551 and then Is_Abstract_Subprogram (S)
6552 and then (not Is_Overriding
6553 or else not Is_Abstract_Subprogram (E))
6555 Error_Msg_N ("abstract subprograms must be visible "
6556 & "(RM 3.9.3(10))!", S);
6558 elsif Ekind (S) = E_Function
6559 and then Is_Tagged_Type (T)
6560 and then T = Base_Type (Etype (S))
6561 and then not Is_Overriding
6564 ("private function with tagged result must"
6565 & " override visible-part function", S);
6567 ("\move subprogram to the visible part"
6568 & " (RM 3.9.3(10))", S);
6571 end Check_Private_Overriding;
6573 -----------------------
6574 -- Visible_Part_Type --
6575 -----------------------
6577 function Visible_Part_Type (T : Entity_Id) return Boolean is
6578 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
6582 -- If the entity is a private type, then it must be declared in a
6585 if Ekind (T) in Private_Kind then
6589 -- Otherwise, we traverse the visible part looking for its
6590 -- corresponding declaration. We cannot use the declaration
6591 -- node directly because in the private part the entity of a
6592 -- private type is the one in the full view, which does not
6593 -- indicate that it is the completion of something visible.
6595 N := First (Visible_Declarations (Specification (P)));
6596 while Present (N) loop
6597 if Nkind (N) = N_Full_Type_Declaration
6598 and then Present (Defining_Identifier (N))
6599 and then T = Defining_Identifier (N)
6603 elsif Nkind_In (N, N_Private_Type_Declaration,
6604 N_Private_Extension_Declaration)
6605 and then Present (Defining_Identifier (N))
6606 and then T = Full_View (Defining_Identifier (N))
6615 end Visible_Part_Type;
6617 -- Start of processing for Check_For_Primitive_Subprogram
6620 Is_Primitive := False;
6622 if not Comes_From_Source (S) then
6625 -- If subprogram is at library level, it is not primitive operation
6627 elsif Current_Scope = Standard_Standard then
6630 elsif (Is_Package_Or_Generic_Package (Current_Scope)
6631 and then not In_Package_Body (Current_Scope))
6632 or else Is_Overriding
6634 -- For function, check return type
6636 if Ekind (S) = E_Function then
6637 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
6638 F_Typ := Designated_Type (Etype (S));
6643 B_Typ := Base_Type (F_Typ);
6645 if Scope (B_Typ) = Current_Scope
6646 and then not Is_Class_Wide_Type (B_Typ)
6647 and then not Is_Generic_Type (B_Typ)
6649 Is_Primitive := True;
6650 Set_Has_Primitive_Operations (B_Typ);
6651 Set_Is_Primitive (S);
6652 Check_Private_Overriding (B_Typ);
6656 -- For all subprograms, check formals
6658 Formal := First_Formal (S);
6659 while Present (Formal) loop
6660 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
6661 F_Typ := Designated_Type (Etype (Formal));
6663 F_Typ := Etype (Formal);
6666 B_Typ := Base_Type (F_Typ);
6668 if Ekind (B_Typ) = E_Access_Subtype then
6669 B_Typ := Base_Type (B_Typ);
6672 if Scope (B_Typ) = Current_Scope
6673 and then not Is_Class_Wide_Type (B_Typ)
6674 and then not Is_Generic_Type (B_Typ)
6676 Is_Primitive := True;
6677 Set_Is_Primitive (S);
6678 Set_Has_Primitive_Operations (B_Typ);
6679 Check_Private_Overriding (B_Typ);
6682 Next_Formal (Formal);
6685 end Check_For_Primitive_Subprogram;
6687 -----------------------------------
6688 -- Check_Synchronized_Overriding --
6689 -----------------------------------
6691 procedure Check_Synchronized_Overriding
6692 (Def_Id : Entity_Id;
6693 Overridden_Subp : out Entity_Id)
6695 Ifaces_List : Elist_Id;
6699 function Matches_Prefixed_View_Profile
6700 (Prim_Params : List_Id;
6701 Iface_Params : List_Id) return Boolean;
6702 -- Determine whether a subprogram's parameter profile Prim_Params
6703 -- matches that of a potentially overridden interface subprogram
6704 -- Iface_Params. Also determine if the type of first parameter of
6705 -- Iface_Params is an implemented interface.
6707 -----------------------------------
6708 -- Matches_Prefixed_View_Profile --
6709 -----------------------------------
6711 function Matches_Prefixed_View_Profile
6712 (Prim_Params : List_Id;
6713 Iface_Params : List_Id) return Boolean
6715 Iface_Id : Entity_Id;
6716 Iface_Param : Node_Id;
6717 Iface_Typ : Entity_Id;
6718 Prim_Id : Entity_Id;
6719 Prim_Param : Node_Id;
6720 Prim_Typ : Entity_Id;
6722 function Is_Implemented
6723 (Ifaces_List : Elist_Id;
6724 Iface : Entity_Id) return Boolean;
6725 -- Determine if Iface is implemented by the current task or
6728 --------------------
6729 -- Is_Implemented --
6730 --------------------
6732 function Is_Implemented
6733 (Ifaces_List : Elist_Id;
6734 Iface : Entity_Id) return Boolean
6736 Iface_Elmt : Elmt_Id;
6739 Iface_Elmt := First_Elmt (Ifaces_List);
6740 while Present (Iface_Elmt) loop
6741 if Node (Iface_Elmt) = Iface then
6745 Next_Elmt (Iface_Elmt);
6751 -- Start of processing for Matches_Prefixed_View_Profile
6754 Iface_Param := First (Iface_Params);
6755 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
6757 if Is_Access_Type (Iface_Typ) then
6758 Iface_Typ := Designated_Type (Iface_Typ);
6761 Prim_Param := First (Prim_Params);
6763 -- The first parameter of the potentially overridden subprogram
6764 -- must be an interface implemented by Prim.
6766 if not Is_Interface (Iface_Typ)
6767 or else not Is_Implemented (Ifaces_List, Iface_Typ)
6772 -- The checks on the object parameters are done, move onto the
6773 -- rest of the parameters.
6775 if not In_Scope then
6776 Prim_Param := Next (Prim_Param);
6779 Iface_Param := Next (Iface_Param);
6780 while Present (Iface_Param) and then Present (Prim_Param) loop
6781 Iface_Id := Defining_Identifier (Iface_Param);
6782 Iface_Typ := Find_Parameter_Type (Iface_Param);
6784 Prim_Id := Defining_Identifier (Prim_Param);
6785 Prim_Typ := Find_Parameter_Type (Prim_Param);
6787 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
6788 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
6789 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
6791 Iface_Typ := Designated_Type (Iface_Typ);
6792 Prim_Typ := Designated_Type (Prim_Typ);
6795 -- Case of multiple interface types inside a parameter profile
6797 -- (Obj_Param : in out Iface; ...; Param : Iface)
6799 -- If the interface type is implemented, then the matching type
6800 -- in the primitive should be the implementing record type.
6802 if Ekind (Iface_Typ) = E_Record_Type
6803 and then Is_Interface (Iface_Typ)
6804 and then Is_Implemented (Ifaces_List, Iface_Typ)
6806 if Prim_Typ /= Typ then
6810 -- The two parameters must be both mode and subtype conformant
6812 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
6814 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
6823 -- One of the two lists contains more parameters than the other
6825 if Present (Iface_Param) or else Present (Prim_Param) then
6830 end Matches_Prefixed_View_Profile;
6832 -- Start of processing for Check_Synchronized_Overriding
6835 Overridden_Subp := Empty;
6837 -- Def_Id must be an entry or a subprogram. We should skip predefined
6838 -- primitives internally generated by the frontend; however at this
6839 -- stage predefined primitives are still not fully decorated. As a
6840 -- minor optimization we skip here internally generated subprograms.
6842 if (Ekind (Def_Id) /= E_Entry
6843 and then Ekind (Def_Id) /= E_Function
6844 and then Ekind (Def_Id) /= E_Procedure)
6845 or else not Comes_From_Source (Def_Id)
6850 -- Search for the concurrent declaration since it contains the list
6851 -- of all implemented interfaces. In this case, the subprogram is
6852 -- declared within the scope of a protected or a task type.
6854 if Present (Scope (Def_Id))
6855 and then Is_Concurrent_Type (Scope (Def_Id))
6856 and then not Is_Generic_Actual_Type (Scope (Def_Id))
6858 Typ := Scope (Def_Id);
6861 -- The enclosing scope is not a synchronized type and the subprogram
6864 elsif No (First_Formal (Def_Id)) then
6867 -- The subprogram has formals and hence it may be a primitive of a
6871 Typ := Etype (First_Formal (Def_Id));
6873 if Is_Access_Type (Typ) then
6874 Typ := Directly_Designated_Type (Typ);
6877 if Is_Concurrent_Type (Typ)
6878 and then not Is_Generic_Actual_Type (Typ)
6882 -- This case occurs when the concurrent type is declared within
6883 -- a generic unit. As a result the corresponding record has been
6884 -- built and used as the type of the first formal, we just have
6885 -- to retrieve the corresponding concurrent type.
6887 elsif Is_Concurrent_Record_Type (Typ)
6888 and then Present (Corresponding_Concurrent_Type (Typ))
6890 Typ := Corresponding_Concurrent_Type (Typ);
6898 -- There is no overriding to check if is an inherited operation in a
6899 -- type derivation on for a generic actual.
6901 Collect_Interfaces (Typ, Ifaces_List);
6903 if Is_Empty_Elmt_List (Ifaces_List) then
6907 -- Determine whether entry or subprogram Def_Id overrides a primitive
6908 -- operation that belongs to one of the interfaces in Ifaces_List.
6911 Candidate : Entity_Id := Empty;
6912 Hom : Entity_Id := Empty;
6913 Iface_Typ : Entity_Id;
6914 Subp : Entity_Id := Empty;
6917 -- Traverse the homonym chain, looking at a potentially
6918 -- overridden subprogram that belongs to an implemented
6921 Hom := Current_Entity_In_Scope (Def_Id);
6922 while Present (Hom) loop
6926 or else not Is_Overloadable (Subp)
6927 or else not Is_Primitive (Subp)
6928 or else not Is_Dispatching_Operation (Subp)
6929 or else not Is_Interface (Find_Dispatching_Type (Subp))
6933 -- Entries and procedures can override abstract or null
6934 -- interface procedures
6936 elsif (Ekind (Def_Id) = E_Procedure
6937 or else Ekind (Def_Id) = E_Entry)
6938 and then Ekind (Subp) = E_Procedure
6939 and then Matches_Prefixed_View_Profile
6940 (Parameter_Specifications (Parent (Def_Id)),
6941 Parameter_Specifications (Parent (Subp)))
6945 -- For an overridden subprogram Subp, check whether the mode
6946 -- of its first parameter is correct depending on the kind
6947 -- of synchronized type.
6950 Formal : constant Node_Id := First_Formal (Candidate);
6953 -- In order for an entry or a protected procedure to
6954 -- override, the first parameter of the overridden
6955 -- routine must be of mode "out", "in out" or
6956 -- access-to-variable.
6958 if (Ekind (Candidate) = E_Entry
6959 or else Ekind (Candidate) = E_Procedure)
6960 and then Is_Protected_Type (Typ)
6961 and then Ekind (Formal) /= E_In_Out_Parameter
6962 and then Ekind (Formal) /= E_Out_Parameter
6963 and then Nkind (Parameter_Type (Parent (Formal)))
6964 /= N_Access_Definition
6968 -- All other cases are OK since a task entry or routine
6969 -- does not have a restriction on the mode of the first
6970 -- parameter of the overridden interface routine.
6973 Overridden_Subp := Candidate;
6978 -- Functions can override abstract interface functions
6980 elsif Ekind (Def_Id) = E_Function
6981 and then Ekind (Subp) = E_Function
6982 and then Matches_Prefixed_View_Profile
6983 (Parameter_Specifications (Parent (Def_Id)),
6984 Parameter_Specifications (Parent (Subp)))
6985 and then Etype (Result_Definition (Parent (Def_Id))) =
6986 Etype (Result_Definition (Parent (Subp)))
6988 Overridden_Subp := Subp;
6992 Hom := Homonym (Hom);
6995 -- After examining all candidates for overriding, we are
6996 -- left with the best match which is a mode incompatible
6997 -- interface routine. Do not emit an error if the Expander
6998 -- is active since this error will be detected later on
6999 -- after all concurrent types are expanded and all wrappers
7000 -- are built. This check is meant for spec-only
7003 if Present (Candidate)
7004 and then not Expander_Active
7007 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7009 -- Def_Id is primitive of a protected type, declared
7010 -- inside the type, and the candidate is primitive of a
7011 -- limited or synchronized interface.
7014 and then Is_Protected_Type (Typ)
7016 (Is_Limited_Interface (Iface_Typ)
7017 or else Is_Protected_Interface (Iface_Typ)
7018 or else Is_Synchronized_Interface (Iface_Typ)
7019 or else Is_Task_Interface (Iface_Typ))
7021 -- Must reword this message, comma before to in -gnatj
7025 ("first formal of & must be of mode `OUT`, `IN OUT`"
7026 & " or access-to-variable", Typ, Candidate);
7028 ("\to be overridden by protected procedure or entry "
7029 & "(RM 9.4(11.9/2))", Typ);
7033 Overridden_Subp := Candidate;
7036 end Check_Synchronized_Overriding;
7038 ----------------------------
7039 -- Is_Private_Declaration --
7040 ----------------------------
7042 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7043 Priv_Decls : List_Id;
7044 Decl : constant Node_Id := Unit_Declaration_Node (E);
7047 if Is_Package_Or_Generic_Package (Current_Scope)
7048 and then In_Private_Part (Current_Scope)
7051 Private_Declarations (
7052 Specification (Unit_Declaration_Node (Current_Scope)));
7054 return In_Package_Body (Current_Scope)
7056 (Is_List_Member (Decl)
7057 and then List_Containing (Decl) = Priv_Decls)
7058 or else (Nkind (Parent (Decl)) = N_Package_Specification
7061 (Defining_Entity (Parent (Decl)))
7062 and then List_Containing (Parent (Parent (Decl)))
7067 end Is_Private_Declaration;
7069 --------------------------
7070 -- Is_Overriding_Alias --
7071 --------------------------
7073 function Is_Overriding_Alias
7075 New_E : Entity_Id) return Boolean
7077 AO : constant Entity_Id := Alias (Old_E);
7078 AN : constant Entity_Id := Alias (New_E);
7081 return Scope (AO) /= Scope (AN)
7082 or else No (DTC_Entity (AO))
7083 or else No (DTC_Entity (AN))
7084 or else DT_Position (AO) = DT_Position (AN);
7085 end Is_Overriding_Alias;
7087 -- Start of processing for New_Overloaded_Entity
7090 -- We need to look for an entity that S may override. This must be a
7091 -- homonym in the current scope, so we look for the first homonym of
7092 -- S in the current scope as the starting point for the search.
7094 E := Current_Entity_In_Scope (S);
7096 -- If there is no homonym then this is definitely not overriding
7099 Enter_Overloaded_Entity (S);
7100 Check_Dispatching_Operation (S, Empty);
7101 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7103 -- If subprogram has an explicit declaration, check whether it
7104 -- has an overriding indicator.
7106 if Comes_From_Source (S) then
7107 Check_Synchronized_Overriding (S, Overridden_Subp);
7108 Check_Overriding_Indicator
7109 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7112 -- If there is a homonym that is not overloadable, then we have an
7113 -- error, except for the special cases checked explicitly below.
7115 elsif not Is_Overloadable (E) then
7117 -- Check for spurious conflict produced by a subprogram that has the
7118 -- same name as that of the enclosing generic package. The conflict
7119 -- occurs within an instance, between the subprogram and the renaming
7120 -- declaration for the package. After the subprogram, the package
7121 -- renaming declaration becomes hidden.
7123 if Ekind (E) = E_Package
7124 and then Present (Renamed_Object (E))
7125 and then Renamed_Object (E) = Current_Scope
7126 and then Nkind (Parent (Renamed_Object (E))) =
7127 N_Package_Specification
7128 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7131 Set_Is_Immediately_Visible (E, False);
7132 Enter_Overloaded_Entity (S);
7133 Set_Homonym (S, Homonym (E));
7134 Check_Dispatching_Operation (S, Empty);
7135 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7137 -- If the subprogram is implicit it is hidden by the previous
7138 -- declaration. However if it is dispatching, it must appear in the
7139 -- dispatch table anyway, because it can be dispatched to even if it
7140 -- cannot be called directly.
7142 elsif Present (Alias (S))
7143 and then not Comes_From_Source (S)
7145 Set_Scope (S, Current_Scope);
7147 if Is_Dispatching_Operation (Alias (S)) then
7148 Check_Dispatching_Operation (S, Empty);
7154 Error_Msg_Sloc := Sloc (E);
7156 -- Generate message, with useful additional warning if in generic
7158 if Is_Generic_Unit (E) then
7159 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7160 Error_Msg_N ("\& conflicts with declaration#", S);
7162 Error_Msg_N ("& conflicts with declaration#", S);
7168 -- E exists and is overloadable
7171 -- Ada 2005 (AI-251): Derivation of abstract interface primitives
7172 -- need no check against the homonym chain. They are directly added
7173 -- to the list of primitive operations of Derived_Type.
7175 if Ada_Version >= Ada_05
7176 and then Present (Derived_Type)
7177 and then Is_Dispatching_Operation (Alias (S))
7178 and then Present (Find_Dispatching_Type (Alias (S)))
7179 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7181 goto Add_New_Entity;
7184 Check_Synchronized_Overriding (S, Overridden_Subp);
7186 -- Loop through E and its homonyms to determine if any of them is
7187 -- the candidate for overriding by S.
7189 while Present (E) loop
7191 -- Definitely not interesting if not in the current scope
7193 if Scope (E) /= Current_Scope then
7196 -- Check if we have type conformance
7198 elsif Type_Conformant (E, S) then
7200 -- If the old and new entities have the same profile and one
7201 -- is not the body of the other, then this is an error, unless
7202 -- one of them is implicitly declared.
7204 -- There are some cases when both can be implicit, for example
7205 -- when both a literal and a function that overrides it are
7206 -- inherited in a derivation, or when an inherited operation
7207 -- of a tagged full type overrides the inherited operation of
7208 -- a private extension. Ada 83 had a special rule for the
7209 -- literal case. In Ada95, the later implicit operation hides
7210 -- the former, and the literal is always the former. In the
7211 -- odd case where both are derived operations declared at the
7212 -- same point, both operations should be declared, and in that
7213 -- case we bypass the following test and proceed to the next
7214 -- part. This can only occur for certain obscure cases in
7215 -- instances, when an operation on a type derived from a formal
7216 -- private type does not override a homograph inherited from
7217 -- the actual. In subsequent derivations of such a type, the
7218 -- DT positions of these operations remain distinct, if they
7221 if Present (Alias (S))
7222 and then (No (Alias (E))
7223 or else Comes_From_Source (E)
7224 or else Is_Abstract_Subprogram (S)
7226 (Is_Dispatching_Operation (E)
7227 and then Is_Overriding_Alias (E, S)))
7228 and then Ekind (E) /= E_Enumeration_Literal
7230 -- When an derived operation is overloaded it may be due to
7231 -- the fact that the full view of a private extension
7232 -- re-inherits. It has to be dealt with.
7234 if Is_Package_Or_Generic_Package (Current_Scope)
7235 and then In_Private_Part (Current_Scope)
7237 Check_Operation_From_Private_View (S, E);
7240 -- In any case the implicit operation remains hidden by
7241 -- the existing declaration, which is overriding.
7243 Set_Is_Overriding_Operation (E);
7245 if Comes_From_Source (E) then
7246 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7248 -- Indicate that E overrides the operation from which
7251 if Present (Alias (S)) then
7252 Set_Overridden_Operation (E, Alias (S));
7254 Set_Overridden_Operation (E, S);
7260 -- Within an instance, the renaming declarations for
7261 -- actual subprograms may become ambiguous, but they do
7262 -- not hide each other.
7264 elsif Ekind (E) /= E_Entry
7265 and then not Comes_From_Source (E)
7266 and then not Is_Generic_Instance (E)
7267 and then (Present (Alias (E))
7268 or else Is_Intrinsic_Subprogram (E))
7269 and then (not In_Instance
7270 or else No (Parent (E))
7271 or else Nkind (Unit_Declaration_Node (E)) /=
7272 N_Subprogram_Renaming_Declaration)
7274 -- A subprogram child unit is not allowed to override
7275 -- an inherited subprogram (10.1.1(20)).
7277 if Is_Child_Unit (S) then
7279 ("child unit overrides inherited subprogram in parent",
7284 if Is_Non_Overriding_Operation (E, S) then
7285 Enter_Overloaded_Entity (S);
7287 if No (Derived_Type)
7288 or else Is_Tagged_Type (Derived_Type)
7290 Check_Dispatching_Operation (S, Empty);
7296 -- E is a derived operation or an internal operator which
7297 -- is being overridden. Remove E from further visibility.
7298 -- Furthermore, if E is a dispatching operation, it must be
7299 -- replaced in the list of primitive operations of its type
7300 -- (see Override_Dispatching_Operation).
7302 Overridden_Subp := E;
7308 Prev := First_Entity (Current_Scope);
7309 while Present (Prev)
7310 and then Next_Entity (Prev) /= E
7315 -- It is possible for E to be in the current scope and
7316 -- yet not in the entity chain. This can only occur in a
7317 -- generic context where E is an implicit concatenation
7318 -- in the formal part, because in a generic body the
7319 -- entity chain starts with the formals.
7322 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7324 -- E must be removed both from the entity_list of the
7325 -- current scope, and from the visibility chain
7327 if Debug_Flag_E then
7328 Write_Str ("Override implicit operation ");
7329 Write_Int (Int (E));
7333 -- If E is a predefined concatenation, it stands for four
7334 -- different operations. As a result, a single explicit
7335 -- declaration does not hide it. In a possible ambiguous
7336 -- situation, Disambiguate chooses the user-defined op,
7337 -- so it is correct to retain the previous internal one.
7339 if Chars (E) /= Name_Op_Concat
7340 or else Ekind (E) /= E_Operator
7342 -- For nondispatching derived operations that are
7343 -- overridden by a subprogram declared in the private
7344 -- part of a package, we retain the derived subprogram
7345 -- but mark it as not immediately visible. If the
7346 -- derived operation was declared in the visible part
7347 -- then this ensures that it will still be visible
7348 -- outside the package with the proper signature
7349 -- (calls from outside must also be directed to this
7350 -- version rather than the overriding one, unlike the
7351 -- dispatching case). Calls from inside the package
7352 -- will still resolve to the overriding subprogram
7353 -- since the derived one is marked as not visible
7354 -- within the package.
7356 -- If the private operation is dispatching, we achieve
7357 -- the overriding by keeping the implicit operation
7358 -- but setting its alias to be the overriding one. In
7359 -- this fashion the proper body is executed in all
7360 -- cases, but the original signature is used outside
7363 -- If the overriding is not in the private part, we
7364 -- remove the implicit operation altogether.
7366 if Is_Private_Declaration (S) then
7367 if not Is_Dispatching_Operation (E) then
7368 Set_Is_Immediately_Visible (E, False);
7370 -- Work done in Override_Dispatching_Operation,
7371 -- so nothing else need to be done here.
7377 -- Find predecessor of E in Homonym chain
7379 if E = Current_Entity (E) then
7382 Prev_Vis := Current_Entity (E);
7383 while Homonym (Prev_Vis) /= E loop
7384 Prev_Vis := Homonym (Prev_Vis);
7388 if Prev_Vis /= Empty then
7390 -- Skip E in the visibility chain
7392 Set_Homonym (Prev_Vis, Homonym (E));
7395 Set_Name_Entity_Id (Chars (E), Homonym (E));
7398 Set_Next_Entity (Prev, Next_Entity (E));
7400 if No (Next_Entity (Prev)) then
7401 Set_Last_Entity (Current_Scope, Prev);
7407 Enter_Overloaded_Entity (S);
7408 Set_Is_Overriding_Operation (S);
7409 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7411 -- Indicate that S overrides the operation from which
7414 if Comes_From_Source (S) then
7415 if Present (Alias (E)) then
7416 Set_Overridden_Operation (S, Alias (E));
7418 Set_Overridden_Operation (S, E);
7422 if Is_Dispatching_Operation (E) then
7424 -- An overriding dispatching subprogram inherits the
7425 -- convention of the overridden subprogram (by
7428 Set_Convention (S, Convention (E));
7429 Check_Dispatching_Operation (S, E);
7432 Check_Dispatching_Operation (S, Empty);
7435 Check_For_Primitive_Subprogram
7436 (Is_Primitive_Subp, Is_Overriding => True);
7437 goto Check_Inequality;
7440 -- Apparent redeclarations in instances can occur when two
7441 -- formal types get the same actual type. The subprograms in
7442 -- in the instance are legal, even if not callable from the
7443 -- outside. Calls from within are disambiguated elsewhere.
7444 -- For dispatching operations in the visible part, the usual
7445 -- rules apply, and operations with the same profile are not
7448 elsif (In_Instance_Visible_Part
7449 and then not Is_Dispatching_Operation (E))
7450 or else In_Instance_Not_Visible
7454 -- Here we have a real error (identical profile)
7457 Error_Msg_Sloc := Sloc (E);
7459 -- Avoid cascaded errors if the entity appears in
7460 -- subsequent calls.
7462 Set_Scope (S, Current_Scope);
7464 -- Generate error, with extra useful warning for the case
7465 -- of a generic instance with no completion.
7467 if Is_Generic_Instance (S)
7468 and then not Has_Completion (E)
7471 ("instantiation cannot provide body for&", S);
7472 Error_Msg_N ("\& conflicts with declaration#", S);
7474 Error_Msg_N ("& conflicts with declaration#", S);
7481 -- If one subprogram has an access parameter and the other
7482 -- a parameter of an access type, calls to either might be
7483 -- ambiguous. Verify that parameters match except for the
7484 -- access parameter.
7486 if May_Hide_Profile then
7492 F1 := First_Formal (S);
7493 F2 := First_Formal (E);
7494 while Present (F1) and then Present (F2) loop
7495 if Is_Access_Type (Etype (F1)) then
7496 if not Is_Access_Type (Etype (F2))
7497 or else not Conforming_Types
7498 (Designated_Type (Etype (F1)),
7499 Designated_Type (Etype (F2)),
7502 May_Hide_Profile := False;
7506 not Conforming_Types
7507 (Etype (F1), Etype (F2), Type_Conformant)
7509 May_Hide_Profile := False;
7520 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7531 -- On exit, we know that S is a new entity
7533 Enter_Overloaded_Entity (S);
7534 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7535 Check_Overriding_Indicator
7536 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7538 -- If S is a derived operation for an untagged type then by
7539 -- definition it's not a dispatching operation (even if the parent
7540 -- operation was dispatching), so we don't call
7541 -- Check_Dispatching_Operation in that case.
7543 if No (Derived_Type)
7544 or else Is_Tagged_Type (Derived_Type)
7546 Check_Dispatching_Operation (S, Empty);
7550 -- If this is a user-defined equality operator that is not a derived
7551 -- subprogram, create the corresponding inequality. If the operation is
7552 -- dispatching, the expansion is done elsewhere, and we do not create
7553 -- an explicit inequality operation.
7555 <<Check_Inequality>>
7556 if Chars (S) = Name_Op_Eq
7557 and then Etype (S) = Standard_Boolean
7558 and then Present (Parent (S))
7559 and then not Is_Dispatching_Operation (S)
7561 Make_Inequality_Operator (S);
7563 end New_Overloaded_Entity;
7565 ---------------------
7566 -- Process_Formals --
7567 ---------------------
7569 procedure Process_Formals
7571 Related_Nod : Node_Id)
7573 Param_Spec : Node_Id;
7575 Formal_Type : Entity_Id;
7579 Num_Out_Params : Nat := 0;
7580 First_Out_Param : Entity_Id := Empty;
7581 -- Used for setting Is_Only_Out_Parameter
7583 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
7584 -- Check whether the default has a class-wide type. After analysis the
7585 -- default has the type of the formal, so we must also check explicitly
7586 -- for an access attribute.
7588 ---------------------------
7589 -- Is_Class_Wide_Default --
7590 ---------------------------
7592 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
7594 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
7595 or else (Nkind (D) = N_Attribute_Reference
7596 and then Attribute_Name (D) = Name_Access
7597 and then Is_Class_Wide_Type (Etype (Prefix (D))));
7598 end Is_Class_Wide_Default;
7600 -- Start of processing for Process_Formals
7603 -- In order to prevent premature use of the formals in the same formal
7604 -- part, the Ekind is left undefined until all default expressions are
7605 -- analyzed. The Ekind is established in a separate loop at the end.
7607 Param_Spec := First (T);
7608 while Present (Param_Spec) loop
7609 Formal := Defining_Identifier (Param_Spec);
7610 Set_Never_Set_In_Source (Formal, True);
7611 Enter_Name (Formal);
7613 -- Case of ordinary parameters
7615 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
7616 Find_Type (Parameter_Type (Param_Spec));
7617 Ptype := Parameter_Type (Param_Spec);
7619 if Ptype = Error then
7623 Formal_Type := Entity (Ptype);
7625 if Is_Incomplete_Type (Formal_Type)
7627 (Is_Class_Wide_Type (Formal_Type)
7628 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
7630 -- Ada 2005 (AI-326): Tagged incomplete types allowed
7632 if Is_Tagged_Type (Formal_Type) then
7635 -- Special handling of Value_Type for CIL case
7637 elsif Is_Value_Type (Formal_Type) then
7640 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
7641 N_Access_Procedure_Definition)
7643 Error_Msg_N ("invalid use of incomplete type", Param_Spec);
7645 -- An incomplete type that is not tagged is allowed in an
7646 -- access-to-subprogram type only if it is a local declaration
7647 -- with a forthcoming completion (3.10.1 (9.2/2)).
7649 elsif Scope (Formal_Type) /= Scope (Current_Scope) then
7651 ("invalid use of limited view of type", Param_Spec);
7654 elsif Ekind (Formal_Type) = E_Void then
7655 Error_Msg_NE ("premature use of&",
7656 Parameter_Type (Param_Spec), Formal_Type);
7659 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7660 -- declaration corresponding to the null-excluding type of the
7661 -- formal in the enclosing scope. Finally, replace the parameter
7662 -- type of the formal with the internal subtype.
7664 if Ada_Version >= Ada_05
7665 and then Null_Exclusion_Present (Param_Spec)
7667 if not Is_Access_Type (Formal_Type) then
7669 ("`NOT NULL` allowed only for an access type", Param_Spec);
7672 if Can_Never_Be_Null (Formal_Type)
7673 and then Comes_From_Source (Related_Nod)
7676 ("`NOT NULL` not allowed (& already excludes null)",
7682 Create_Null_Excluding_Itype
7684 Related_Nod => Related_Nod,
7685 Scope_Id => Scope (Current_Scope));
7687 -- If the designated type of the itype is an itype we
7688 -- decorate it with the Has_Delayed_Freeze attribute to
7689 -- avoid problems with the backend.
7692 -- type T is access procedure;
7693 -- procedure Op (O : not null T);
7695 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
7696 Set_Has_Delayed_Freeze (Formal_Type);
7701 -- An access formal type
7705 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7707 -- No need to continue if we already notified errors
7709 if not Present (Formal_Type) then
7713 -- Ada 2005 (AI-254)
7716 AD : constant Node_Id :=
7717 Access_To_Subprogram_Definition
7718 (Parameter_Type (Param_Spec));
7720 if Present (AD) and then Protected_Present (AD) then
7722 Replace_Anonymous_Access_To_Protected_Subprogram
7728 Set_Etype (Formal, Formal_Type);
7729 Default := Expression (Param_Spec);
7731 if Present (Default) then
7732 if Out_Present (Param_Spec) then
7734 ("default initialization only allowed for IN parameters",
7738 -- Do the special preanalysis of the expression (see section on
7739 -- "Handling of Default Expressions" in the spec of package Sem).
7741 Preanalyze_Spec_Expression (Default, Formal_Type);
7743 -- An access to constant cannot be the default for
7744 -- an access parameter that is an access to variable.
7746 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7747 and then not Is_Access_Constant (Formal_Type)
7748 and then Is_Access_Type (Etype (Default))
7749 and then Is_Access_Constant (Etype (Default))
7752 ("formal that is access to variable cannot be initialized " &
7753 "with an access-to-constant expression", Default);
7756 -- Check that the designated type of an access parameter's default
7757 -- is not a class-wide type unless the parameter's designated type
7758 -- is also class-wide.
7760 if Ekind (Formal_Type) = E_Anonymous_Access_Type
7761 and then not From_With_Type (Formal_Type)
7762 and then Is_Class_Wide_Default (Default)
7763 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
7766 ("access to class-wide expression not allowed here", Default);
7770 -- Ada 2005 (AI-231): Static checks
7772 if Ada_Version >= Ada_05
7773 and then Is_Access_Type (Etype (Formal))
7774 and then Can_Never_Be_Null (Etype (Formal))
7776 Null_Exclusion_Static_Checks (Param_Spec);
7783 -- If this is the formal part of a function specification, analyze the
7784 -- subtype mark in the context where the formals are visible but not
7785 -- yet usable, and may hide outer homographs.
7787 if Nkind (Related_Nod) = N_Function_Specification then
7788 Analyze_Return_Type (Related_Nod);
7791 -- Now set the kind (mode) of each formal
7793 Param_Spec := First (T);
7795 while Present (Param_Spec) loop
7796 Formal := Defining_Identifier (Param_Spec);
7797 Set_Formal_Mode (Formal);
7799 if Ekind (Formal) = E_In_Parameter then
7800 Set_Default_Value (Formal, Expression (Param_Spec));
7802 if Present (Expression (Param_Spec)) then
7803 Default := Expression (Param_Spec);
7805 if Is_Scalar_Type (Etype (Default)) then
7807 (Parameter_Type (Param_Spec)) /= N_Access_Definition
7809 Formal_Type := Entity (Parameter_Type (Param_Spec));
7812 Formal_Type := Access_Definition
7813 (Related_Nod, Parameter_Type (Param_Spec));
7816 Apply_Scalar_Range_Check (Default, Formal_Type);
7820 elsif Ekind (Formal) = E_Out_Parameter then
7821 Num_Out_Params := Num_Out_Params + 1;
7823 if Num_Out_Params = 1 then
7824 First_Out_Param := Formal;
7827 elsif Ekind (Formal) = E_In_Out_Parameter then
7828 Num_Out_Params := Num_Out_Params + 1;
7834 if Present (First_Out_Param) and then Num_Out_Params = 1 then
7835 Set_Is_Only_Out_Parameter (First_Out_Param);
7837 end Process_Formals;
7843 procedure Process_PPCs
7845 Spec_Id : Entity_Id;
7846 Body_Id : Entity_Id)
7848 Loc : constant Source_Ptr := Sloc (N);
7850 Plist : List_Id := No_List;
7854 function Grab_PPC (Nam : Name_Id) return Node_Id;
7855 -- Prag contains an analyzed precondition or postcondition pragma.
7856 -- This function copies the pragma, changes it to the corresponding
7857 -- Check pragma and returns the Check pragma as the result. The
7858 -- argument Nam is either Name_Precondition or Name_Postcondition.
7864 function Grab_PPC (Nam : Name_Id) return Node_Id is
7865 CP : constant Node_Id := New_Copy_Tree (Prag);
7868 -- Set Analyzed to false, since we want to reanalyze the check
7869 -- procedure. Note that it is only at the outer level that we
7870 -- do this fiddling, for the spec cases, the already preanalyzed
7871 -- parameters are not affected.
7873 -- For a postcondition pragma within a generic, preserve the pragma
7874 -- for later expansion.
7876 Set_Analyzed (CP, False);
7878 if Nam = Name_Postcondition
7879 and then not Expander_Active
7884 -- Change pragma into corresponding pragma Check
7886 Prepend_To (Pragma_Argument_Associations (CP),
7887 Make_Pragma_Argument_Association (Sloc (Prag),
7889 Make_Identifier (Loc,
7891 Set_Pragma_Identifier (CP,
7892 Make_Identifier (Sloc (Prag),
7893 Chars => Name_Check));
7898 -- Start of processing for Process_PPCs
7901 -- Nothing to do if we are not generating code
7903 if Operating_Mode /= Generate_Code then
7907 -- Grab preconditions from spec
7909 if Present (Spec_Id) then
7911 -- Loop through PPC pragmas from spec. Note that preconditions from
7912 -- the body will be analyzed and converted when we scan the body
7913 -- declarations below.
7915 Prag := Spec_PPC_List (Spec_Id);
7916 while Present (Prag) loop
7917 if Pragma_Name (Prag) = Name_Precondition
7918 and then PPC_Enabled (Prag)
7920 -- Add pragma Check at the start of the declarations of N.
7921 -- Note that this processing reverses the order of the list,
7922 -- which is what we want since new entries were chained to
7923 -- the head of the list.
7925 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
7928 Prag := Next_Pragma (Prag);
7932 -- Build postconditions procedure if needed and prepend the following
7933 -- declaration to the start of the declarations for the subprogram.
7935 -- procedure _postconditions [(_Result : resulttype)] is
7937 -- pragma Check (Postcondition, condition [,message]);
7938 -- pragma Check (Postcondition, condition [,message]);
7942 -- First we deal with the postconditions in the body
7944 if Is_Non_Empty_List (Declarations (N)) then
7946 -- Loop through declarations
7948 Prag := First (Declarations (N));
7949 while Present (Prag) loop
7950 if Nkind (Prag) = N_Pragma then
7952 -- If pragma, capture if enabled postcondition, else ignore
7954 if Pragma_Name (Prag) = Name_Postcondition
7955 and then Check_Enabled (Name_Postcondition)
7957 if Plist = No_List then
7958 Plist := Empty_List;
7963 -- If expansion is disabled, as in a generic unit,
7964 -- save pragma for later expansion.
7966 if not Expander_Active then
7967 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
7969 Append (Grab_PPC (Name_Postcondition), Plist);
7975 -- Not a pragma, if comes from source, then end scan
7977 elsif Comes_From_Source (Prag) then
7980 -- Skip stuff not coming from source
7988 -- Now deal with any postconditions from the spec
7990 if Present (Spec_Id) then
7992 -- Loop through PPC pragmas from spec
7994 Prag := Spec_PPC_List (Spec_Id);
7995 while Present (Prag) loop
7996 if Pragma_Name (Prag) = Name_Postcondition
7997 and then PPC_Enabled (Prag)
7999 if Plist = No_List then
8000 Plist := Empty_List;
8003 if not Expander_Active then
8004 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8006 Append (Grab_PPC (Name_Postcondition), Plist);
8010 Prag := Next_Pragma (Prag);
8014 -- If we had any postconditions and expansion is enabled, build
8015 -- the _Postconditions procedure.
8018 and then Expander_Active
8020 Subp := Defining_Entity (N);
8022 if Etype (Subp) /= Standard_Void_Type then
8024 Make_Parameter_Specification (Loc,
8025 Defining_Identifier =>
8026 Make_Defining_Identifier (Loc,
8027 Chars => Name_uResult),
8028 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
8034 Post_Proc : constant Entity_Id :=
8035 Make_Defining_Identifier (Loc,
8036 Chars => Name_uPostconditions);
8037 -- The entity for the _Postconditions procedure
8039 Prepend_To (Declarations (N),
8040 Make_Subprogram_Body (Loc,
8042 Make_Procedure_Specification (Loc,
8043 Defining_Unit_Name => Post_Proc,
8044 Parameter_Specifications => Parms),
8046 Declarations => Empty_List,
8048 Handled_Statement_Sequence =>
8049 Make_Handled_Sequence_Of_Statements (Loc,
8050 Statements => Plist)));
8052 -- If this is a procedure, set the Postcondition_Proc attribute
8054 if Etype (Subp) = Standard_Void_Type then
8055 Set_Postcondition_Proc (Spec_Id, Post_Proc);
8059 if Present (Spec_Id) then
8060 Set_Has_Postconditions (Spec_Id);
8062 Set_Has_Postconditions (Body_Id);
8067 ----------------------------
8068 -- Reference_Body_Formals --
8069 ----------------------------
8071 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
8076 if Error_Posted (Spec) then
8080 -- Iterate over both lists. They may be of different lengths if the two
8081 -- specs are not conformant.
8083 Fs := First_Formal (Spec);
8084 Fb := First_Formal (Bod);
8085 while Present (Fs) and then Present (Fb) loop
8086 Generate_Reference (Fs, Fb, 'b');
8089 Style.Check_Identifier (Fb, Fs);
8092 Set_Spec_Entity (Fb, Fs);
8093 Set_Referenced (Fs, False);
8097 end Reference_Body_Formals;
8099 -------------------------
8100 -- Set_Actual_Subtypes --
8101 -------------------------
8103 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
8104 Loc : constant Source_Ptr := Sloc (N);
8108 First_Stmt : Node_Id := Empty;
8109 AS_Needed : Boolean;
8112 -- If this is an empty initialization procedure, no need to create
8113 -- actual subtypes (small optimization).
8115 if Ekind (Subp) = E_Procedure
8116 and then Is_Null_Init_Proc (Subp)
8121 Formal := First_Formal (Subp);
8122 while Present (Formal) loop
8123 T := Etype (Formal);
8125 -- We never need an actual subtype for a constrained formal
8127 if Is_Constrained (T) then
8130 -- If we have unknown discriminants, then we do not need an actual
8131 -- subtype, or more accurately we cannot figure it out! Note that
8132 -- all class-wide types have unknown discriminants.
8134 elsif Has_Unknown_Discriminants (T) then
8137 -- At this stage we have an unconstrained type that may need an
8138 -- actual subtype. For sure the actual subtype is needed if we have
8139 -- an unconstrained array type.
8141 elsif Is_Array_Type (T) then
8144 -- The only other case needing an actual subtype is an unconstrained
8145 -- record type which is an IN parameter (we cannot generate actual
8146 -- subtypes for the OUT or IN OUT case, since an assignment can
8147 -- change the discriminant values. However we exclude the case of
8148 -- initialization procedures, since discriminants are handled very
8149 -- specially in this context, see the section entitled "Handling of
8150 -- Discriminants" in Einfo.
8152 -- We also exclude the case of Discrim_SO_Functions (functions used
8153 -- in front end layout mode for size/offset values), since in such
8154 -- functions only discriminants are referenced, and not only are such
8155 -- subtypes not needed, but they cannot always be generated, because
8156 -- of order of elaboration issues.
8158 elsif Is_Record_Type (T)
8159 and then Ekind (Formal) = E_In_Parameter
8160 and then Chars (Formal) /= Name_uInit
8161 and then not Is_Unchecked_Union (T)
8162 and then not Is_Discrim_SO_Function (Subp)
8166 -- All other cases do not need an actual subtype
8172 -- Generate actual subtypes for unconstrained arrays and
8173 -- unconstrained discriminated records.
8176 if Nkind (N) = N_Accept_Statement then
8178 -- If expansion is active, The formal is replaced by a local
8179 -- variable that renames the corresponding entry of the
8180 -- parameter block, and it is this local variable that may
8181 -- require an actual subtype.
8183 if Expander_Active then
8184 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8186 Decl := Build_Actual_Subtype (T, Formal);
8189 if Present (Handled_Statement_Sequence (N)) then
8191 First (Statements (Handled_Statement_Sequence (N)));
8192 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8193 Mark_Rewrite_Insertion (Decl);
8195 -- If the accept statement has no body, there will be no
8196 -- reference to the actuals, so no need to compute actual
8203 Decl := Build_Actual_Subtype (T, Formal);
8204 Prepend (Decl, Declarations (N));
8205 Mark_Rewrite_Insertion (Decl);
8208 -- The declaration uses the bounds of an existing object, and
8209 -- therefore needs no constraint checks.
8211 Analyze (Decl, Suppress => All_Checks);
8213 -- We need to freeze manually the generated type when it is
8214 -- inserted anywhere else than in a declarative part.
8216 if Present (First_Stmt) then
8217 Insert_List_Before_And_Analyze (First_Stmt,
8218 Freeze_Entity (Defining_Identifier (Decl), Loc));
8221 if Nkind (N) = N_Accept_Statement
8222 and then Expander_Active
8224 Set_Actual_Subtype (Renamed_Object (Formal),
8225 Defining_Identifier (Decl));
8227 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8231 Next_Formal (Formal);
8233 end Set_Actual_Subtypes;
8235 ---------------------
8236 -- Set_Formal_Mode --
8237 ---------------------
8239 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8240 Spec : constant Node_Id := Parent (Formal_Id);
8243 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8244 -- since we ensure that corresponding actuals are always valid at the
8245 -- point of the call.
8247 if Out_Present (Spec) then
8248 if Ekind (Scope (Formal_Id)) = E_Function
8249 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8251 Error_Msg_N ("functions can only have IN parameters", Spec);
8252 Set_Ekind (Formal_Id, E_In_Parameter);
8254 elsif In_Present (Spec) then
8255 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8258 Set_Ekind (Formal_Id, E_Out_Parameter);
8259 Set_Never_Set_In_Source (Formal_Id, True);
8260 Set_Is_True_Constant (Formal_Id, False);
8261 Set_Current_Value (Formal_Id, Empty);
8265 Set_Ekind (Formal_Id, E_In_Parameter);
8268 -- Set Is_Known_Non_Null for access parameters since the language
8269 -- guarantees that access parameters are always non-null. We also set
8270 -- Can_Never_Be_Null, since there is no way to change the value.
8272 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8274 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8275 -- null; In Ada 2005, only if then null_exclusion is explicit.
8277 if Ada_Version < Ada_05
8278 or else Can_Never_Be_Null (Etype (Formal_Id))
8280 Set_Is_Known_Non_Null (Formal_Id);
8281 Set_Can_Never_Be_Null (Formal_Id);
8284 -- Ada 2005 (AI-231): Null-exclusion access subtype
8286 elsif Is_Access_Type (Etype (Formal_Id))
8287 and then Can_Never_Be_Null (Etype (Formal_Id))
8289 Set_Is_Known_Non_Null (Formal_Id);
8292 Set_Mechanism (Formal_Id, Default_Mechanism);
8293 Set_Formal_Validity (Formal_Id);
8294 end Set_Formal_Mode;
8296 -------------------------
8297 -- Set_Formal_Validity --
8298 -------------------------
8300 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8302 -- If no validity checking, then we cannot assume anything about the
8303 -- validity of parameters, since we do not know there is any checking
8304 -- of the validity on the call side.
8306 if not Validity_Checks_On then
8309 -- If validity checking for parameters is enabled, this means we are
8310 -- not supposed to make any assumptions about argument values.
8312 elsif Validity_Check_Parameters then
8315 -- If we are checking in parameters, we will assume that the caller is
8316 -- also checking parameters, so we can assume the parameter is valid.
8318 elsif Ekind (Formal_Id) = E_In_Parameter
8319 and then Validity_Check_In_Params
8321 Set_Is_Known_Valid (Formal_Id, True);
8323 -- Similar treatment for IN OUT parameters
8325 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8326 and then Validity_Check_In_Out_Params
8328 Set_Is_Known_Valid (Formal_Id, True);
8330 end Set_Formal_Validity;
8332 ------------------------
8333 -- Subtype_Conformant --
8334 ------------------------
8336 function Subtype_Conformant
8337 (New_Id : Entity_Id;
8339 Skip_Controlling_Formals : Boolean := False) return Boolean
8343 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8344 Skip_Controlling_Formals => Skip_Controlling_Formals);
8346 end Subtype_Conformant;
8348 ---------------------
8349 -- Type_Conformant --
8350 ---------------------
8352 function Type_Conformant
8353 (New_Id : Entity_Id;
8355 Skip_Controlling_Formals : Boolean := False) return Boolean
8359 May_Hide_Profile := False;
8362 (New_Id, Old_Id, Type_Conformant, False, Result,
8363 Skip_Controlling_Formals => Skip_Controlling_Formals);
8365 end Type_Conformant;
8367 -------------------------------
8368 -- Valid_Operator_Definition --
8369 -------------------------------
8371 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8374 Id : constant Name_Id := Chars (Designator);
8378 F := First_Formal (Designator);
8379 while Present (F) loop
8382 if Present (Default_Value (F)) then
8384 ("default values not allowed for operator parameters",
8391 -- Verify that user-defined operators have proper number of arguments
8392 -- First case of operators which can only be unary
8395 or else Id = Name_Op_Abs
8399 -- Case of operators which can be unary or binary
8401 elsif Id = Name_Op_Add
8402 or Id = Name_Op_Subtract
8404 N_OK := (N in 1 .. 2);
8406 -- All other operators can only be binary
8414 ("incorrect number of arguments for operator", Designator);
8418 and then Base_Type (Etype (Designator)) = Standard_Boolean
8419 and then not Is_Intrinsic_Subprogram (Designator)
8422 ("explicit definition of inequality not allowed", Designator);
8424 end Valid_Operator_Definition;