1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Einfo; use Einfo;
31 with Elists; use Elists;
32 with Errout; use Errout;
33 with Expander; use Expander;
34 with Exp_Ch6; use Exp_Ch6;
35 with Exp_Ch7; use Exp_Ch7;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Itypes; use Itypes;
43 with Lib.Xref; use Lib.Xref;
44 with Layout; use Layout;
45 with Namet; use Namet;
47 with Nlists; use Nlists;
48 with Nmake; use Nmake;
50 with Output; use Output;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch3; use Sem_Ch3;
58 with Sem_Ch4; use Sem_Ch4;
59 with Sem_Ch5; use Sem_Ch5;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch10; use Sem_Ch10;
62 with Sem_Ch12; use Sem_Ch12;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Prag; use Sem_Prag;
70 with Sem_Res; use Sem_Res;
71 with Sem_Util; use Sem_Util;
72 with Sem_Type; use Sem_Type;
73 with Sem_Warn; use Sem_Warn;
74 with Sinput; use Sinput;
75 with Stand; use Stand;
76 with Sinfo; use Sinfo;
77 with Sinfo.CN; use Sinfo.CN;
78 with Snames; use Snames;
79 with Stringt; use Stringt;
81 with Stylesw; use Stylesw;
82 with Tbuild; use Tbuild;
83 with Uintp; use Uintp;
84 with Urealp; use Urealp;
85 with Validsw; use Validsw;
87 package body Sem_Ch6 is
89 May_Hide_Profile : Boolean := False;
90 -- This flag is used to indicate that two formals in two subprograms being
91 -- checked for conformance differ only in that one is an access parameter
92 -- while the other is of a general access type with the same designated
93 -- type. In this case, if the rest of the signatures match, a call to
94 -- either subprogram may be ambiguous, which is worth a warning. The flag
95 -- is set in Compatible_Types, and the warning emitted in
96 -- New_Overloaded_Entity.
98 -----------------------
99 -- Local Subprograms --
100 -----------------------
102 procedure Analyze_Return_Statement (N : Node_Id);
103 -- Common processing for simple and extended return statements
105 procedure Analyze_Function_Return (N : Node_Id);
106 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
107 -- applies to a [generic] function.
109 procedure Analyze_Return_Type (N : Node_Id);
110 -- Subsidiary to Process_Formals: analyze subtype mark in function
111 -- specification in a context where the formals are visible and hide
114 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
115 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
116 -- that we can use RETURN but not skip the debug output at the end.
118 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
119 -- Analyze a generic subprogram body. N is the body to be analyzed, and
120 -- Gen_Id is the defining entity Id for the corresponding spec.
122 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id);
123 -- If a subprogram has pragma Inline and inlining is active, use generic
124 -- machinery to build an unexpanded body for the subprogram. This body is
125 -- subsequently used for inline expansions at call sites. If subprogram can
126 -- be inlined (depending on size and nature of local declarations) this
127 -- function returns true. Otherwise subprogram body is treated normally.
128 -- If proper warnings are enabled and the subprogram contains a construct
129 -- that cannot be inlined, the offending construct is flagged accordingly.
131 procedure Check_Conformance
134 Ctype : Conformance_Type;
136 Conforms : out Boolean;
137 Err_Loc : Node_Id := Empty;
138 Get_Inst : Boolean := False;
139 Skip_Controlling_Formals : Boolean := False);
140 -- Given two entities, this procedure checks that the profiles associated
141 -- with these entities meet the conformance criterion given by the third
142 -- parameter. If they conform, Conforms is set True and control returns
143 -- to the caller. If they do not conform, Conforms is set to False, and
144 -- in addition, if Errmsg is True on the call, proper messages are output
145 -- to complain about the conformance failure. If Err_Loc is non_Empty
146 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
147 -- error messages are placed on the appropriate part of the construct
148 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
149 -- against a formal access-to-subprogram type so Get_Instance_Of must
152 procedure Check_Subprogram_Order (N : Node_Id);
153 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
154 -- the alpha ordering rule for N if this ordering requirement applicable.
156 procedure Check_Returns
160 Proc : Entity_Id := Empty);
161 -- Called to check for missing return statements in a function body, or for
162 -- returns present in a procedure body which has No_Return set. HSS is the
163 -- handled statement sequence for the subprogram body. This procedure
164 -- checks all flow paths to make sure they either have return (Mode = 'F',
165 -- used for functions) or do not have a return (Mode = 'P', used for
166 -- No_Return procedures). The flag Err is set if there are any control
167 -- paths not explicitly terminated by a return in the function case, and is
168 -- True otherwise. Proc is the entity for the procedure case and is used
169 -- in posting the warning message.
171 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
172 -- In Ada 2012, a primitive equality operator on an untagged record type
173 -- must appear before the type is frozen, and have the same visibility as
174 -- that of the type. This procedure checks that this rule is met, and
175 -- otherwise emits an error on the subprogram declaration and a warning
176 -- on the earlier freeze point if it is easy to locate.
178 procedure Enter_Overloaded_Entity (S : Entity_Id);
179 -- This procedure makes S, a new overloaded entity, into the first visible
180 -- entity with that name.
182 procedure Install_Entity (E : Entity_Id);
183 -- Make single entity visible (used for generic formals as well)
185 function Is_Non_Overriding_Operation
187 New_E : Entity_Id) return Boolean;
188 -- Enforce the rule given in 12.3(18): a private operation in an instance
189 -- overrides an inherited operation only if the corresponding operation
190 -- was overriding in the generic. This can happen for primitive operations
191 -- of types derived (in the generic unit) from formal private or formal
194 procedure Make_Inequality_Operator (S : Entity_Id);
195 -- Create the declaration for an inequality operator that is implicitly
196 -- created by a user-defined equality operator that yields a boolean.
198 procedure May_Need_Actuals (Fun : Entity_Id);
199 -- Flag functions that can be called without parameters, i.e. those that
200 -- have no parameters, or those for which defaults exist for all parameters
202 procedure Process_PPCs
205 Body_Id : Entity_Id);
206 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
207 -- conditions for the body and assembling and inserting the _postconditions
208 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
209 -- the entities for the body and separate spec (if there is no separate
210 -- spec, Spec_Id is Empty). Note that invariants and predicates may also
211 -- provide postconditions, and are also handled in this procedure.
213 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
214 -- Formal_Id is an formal parameter entity. This procedure deals with
215 -- setting the proper validity status for this entity, which depends on
216 -- the kind of parameter and the validity checking mode.
218 ------------------------------
219 -- Analyze_Return_Statement --
220 ------------------------------
222 procedure Analyze_Return_Statement (N : Node_Id) is
224 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
225 N_Extended_Return_Statement));
227 Returns_Object : constant Boolean :=
228 Nkind (N) = N_Extended_Return_Statement
230 (Nkind (N) = N_Simple_Return_Statement
231 and then Present (Expression (N)));
232 -- True if we're returning something; that is, "return <expression>;"
233 -- or "return Result : T [:= ...]". False for "return;". Used for error
234 -- checking: If Returns_Object is True, N should apply to a function
235 -- body; otherwise N should apply to a procedure body, entry body,
236 -- accept statement, or extended return statement.
238 function Find_What_It_Applies_To return Entity_Id;
239 -- Find the entity representing the innermost enclosing body, accept
240 -- statement, or extended return statement. If the result is a callable
241 -- construct or extended return statement, then this will be the value
242 -- of the Return_Applies_To attribute. Otherwise, the program is
243 -- illegal. See RM-6.5(4/2).
245 -----------------------------
246 -- Find_What_It_Applies_To --
247 -----------------------------
249 function Find_What_It_Applies_To return Entity_Id is
250 Result : Entity_Id := Empty;
253 -- Loop outward through the Scope_Stack, skipping blocks and loops
255 for J in reverse 0 .. Scope_Stack.Last loop
256 Result := Scope_Stack.Table (J).Entity;
257 exit when Ekind (Result) /= E_Block and then
258 Ekind (Result) /= E_Loop;
261 pragma Assert (Present (Result));
263 end Find_What_It_Applies_To;
265 -- Local declarations
267 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
268 Kind : constant Entity_Kind := Ekind (Scope_Id);
269 Loc : constant Source_Ptr := Sloc (N);
270 Stm_Entity : constant Entity_Id :=
272 (E_Return_Statement, Current_Scope, Loc, 'R');
274 -- Start of processing for Analyze_Return_Statement
277 Set_Return_Statement_Entity (N, Stm_Entity);
279 Set_Etype (Stm_Entity, Standard_Void_Type);
280 Set_Return_Applies_To (Stm_Entity, Scope_Id);
282 -- Place Return entity on scope stack, to simplify enforcement of 6.5
283 -- (4/2): an inner return statement will apply to this extended return.
285 if Nkind (N) = N_Extended_Return_Statement then
286 Push_Scope (Stm_Entity);
289 -- Check that pragma No_Return is obeyed. Don't complain about the
290 -- implicitly-generated return that is placed at the end.
292 if No_Return (Scope_Id) and then Comes_From_Source (N) then
293 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
296 -- Warn on any unassigned OUT parameters if in procedure
298 if Ekind (Scope_Id) = E_Procedure then
299 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
302 -- Check that functions return objects, and other things do not
304 if Kind = E_Function or else Kind = E_Generic_Function then
305 if not Returns_Object then
306 Error_Msg_N ("missing expression in return from function", N);
309 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
310 if Returns_Object then
311 Error_Msg_N ("procedure cannot return value (use function)", N);
314 elsif Kind = E_Entry or else Kind = E_Entry_Family then
315 if Returns_Object then
316 if Is_Protected_Type (Scope (Scope_Id)) then
317 Error_Msg_N ("entry body cannot return value", N);
319 Error_Msg_N ("accept statement cannot return value", N);
323 elsif Kind = E_Return_Statement then
325 -- We are nested within another return statement, which must be an
326 -- extended_return_statement.
328 if Returns_Object then
330 ("extended_return_statement cannot return value; " &
331 "use `""RETURN;""`", N);
335 Error_Msg_N ("illegal context for return statement", N);
338 if Kind = E_Function or else Kind = E_Generic_Function then
339 Analyze_Function_Return (N);
342 if Nkind (N) = N_Extended_Return_Statement then
346 Kill_Current_Values (Last_Assignment_Only => True);
347 Check_Unreachable_Code (N);
348 end Analyze_Return_Statement;
350 ---------------------------------------------
351 -- Analyze_Abstract_Subprogram_Declaration --
352 ---------------------------------------------
354 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
355 Designator : constant Entity_Id :=
356 Analyze_Subprogram_Specification (Specification (N));
357 Scop : constant Entity_Id := Current_Scope;
360 Generate_Definition (Designator);
361 Set_Is_Abstract_Subprogram (Designator);
362 New_Overloaded_Entity (Designator);
363 Check_Delayed_Subprogram (Designator);
365 Set_Categorization_From_Scope (Designator, Scop);
367 if Ekind (Scope (Designator)) = E_Protected_Type then
369 ("abstract subprogram not allowed in protected type", N);
371 -- Issue a warning if the abstract subprogram is neither a dispatching
372 -- operation nor an operation that overrides an inherited subprogram or
373 -- predefined operator, since this most likely indicates a mistake.
375 elsif Warn_On_Redundant_Constructs
376 and then not Is_Dispatching_Operation (Designator)
377 and then not Present (Overridden_Operation (Designator))
378 and then (not Is_Operator_Symbol_Name (Chars (Designator))
379 or else Scop /= Scope (Etype (First_Formal (Designator))))
382 ("?abstract subprogram is not dispatching or overriding", N);
385 Generate_Reference_To_Formals (Designator);
386 Check_Eliminated (Designator);
387 Analyze_Aspect_Specifications (N, Designator, Aspect_Specifications (N));
388 end Analyze_Abstract_Subprogram_Declaration;
390 ----------------------------------------
391 -- Analyze_Extended_Return_Statement --
392 ----------------------------------------
394 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
396 Analyze_Return_Statement (N);
397 end Analyze_Extended_Return_Statement;
399 ----------------------------
400 -- Analyze_Function_Call --
401 ----------------------------
403 procedure Analyze_Function_Call (N : Node_Id) is
404 P : constant Node_Id := Name (N);
405 L : constant List_Id := Parameter_Associations (N);
411 -- A call of the form A.B (X) may be an Ada05 call, which is rewritten
412 -- as B (A, X). If the rewriting is successful, the call has been
413 -- analyzed and we just return.
415 if Nkind (P) = N_Selected_Component
416 and then Name (N) /= P
417 and then Is_Rewrite_Substitution (N)
418 and then Present (Etype (N))
423 -- If error analyzing name, then set Any_Type as result type and return
425 if Etype (P) = Any_Type then
426 Set_Etype (N, Any_Type);
430 -- Otherwise analyze the parameters
434 while Present (Actual) loop
436 Check_Parameterless_Call (Actual);
442 end Analyze_Function_Call;
444 -----------------------------
445 -- Analyze_Function_Return --
446 -----------------------------
448 procedure Analyze_Function_Return (N : Node_Id) is
449 Loc : constant Source_Ptr := Sloc (N);
450 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
451 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
453 R_Type : constant Entity_Id := Etype (Scope_Id);
454 -- Function result subtype
456 procedure Check_Limited_Return (Expr : Node_Id);
457 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
458 -- limited types. Used only for simple return statements.
459 -- Expr is the expression returned.
461 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
462 -- Check that the return_subtype_indication properly matches the result
463 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
465 --------------------------
466 -- Check_Limited_Return --
467 --------------------------
469 procedure Check_Limited_Return (Expr : Node_Id) is
471 -- Ada 2005 (AI-318-02): Return-by-reference types have been
472 -- removed and replaced by anonymous access results. This is an
473 -- incompatibility with Ada 95. Not clear whether this should be
474 -- enforced yet or perhaps controllable with special switch. ???
476 if Is_Limited_Type (R_Type)
477 and then Comes_From_Source (N)
478 and then not In_Instance_Body
479 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
483 if Ada_Version >= Ada_2005
484 and then not Debug_Flag_Dot_L
485 and then not GNAT_Mode
488 ("(Ada 2005) cannot copy object of a limited type " &
489 "(RM-2005 6.5(5.5/2))", Expr);
491 if Is_Immutably_Limited_Type (R_Type) then
493 ("\return by reference not permitted in Ada 2005", Expr);
496 -- Warn in Ada 95 mode, to give folks a heads up about this
499 -- In GNAT mode, this is just a warning, to allow it to be
500 -- evilly turned off. Otherwise it is a real error.
502 -- In a generic context, simplify the warning because it makes
503 -- no sense to discuss pass-by-reference or copy.
505 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
506 if Inside_A_Generic then
508 ("return of limited object not permitted in Ada2005 "
509 & "(RM-2005 6.5(5.5/2))?", Expr);
511 elsif Is_Immutably_Limited_Type (R_Type) then
513 ("return by reference not permitted in Ada 2005 "
514 & "(RM-2005 6.5(5.5/2))?", Expr);
517 ("cannot copy object of a limited type in Ada 2005 "
518 & "(RM-2005 6.5(5.5/2))?", Expr);
521 -- Ada 95 mode, compatibility warnings disabled
524 return; -- skip continuation messages below
527 if not Inside_A_Generic then
529 ("\consider switching to return of access type", Expr);
530 Explain_Limited_Type (R_Type, Expr);
533 end Check_Limited_Return;
535 -------------------------------------
536 -- Check_Return_Subtype_Indication --
537 -------------------------------------
539 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
540 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
542 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
543 -- Subtype given in the extended return statement (must match R_Type)
545 Subtype_Ind : constant Node_Id :=
546 Object_Definition (Original_Node (Obj_Decl));
548 R_Type_Is_Anon_Access :
550 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
552 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
554 Ekind (R_Type) = E_Anonymous_Access_Type;
555 -- True if return type of the function is an anonymous access type
556 -- Can't we make Is_Anonymous_Access_Type in einfo ???
558 R_Stm_Type_Is_Anon_Access :
560 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
562 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
564 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
565 -- True if type of the return object is an anonymous access type
568 -- First, avoid cascaded errors
570 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
574 -- "return access T" case; check that the return statement also has
575 -- "access T", and that the subtypes statically match:
576 -- if this is an access to subprogram the signatures must match.
578 if R_Type_Is_Anon_Access then
579 if R_Stm_Type_Is_Anon_Access then
581 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
583 if Base_Type (Designated_Type (R_Stm_Type)) /=
584 Base_Type (Designated_Type (R_Type))
585 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
588 ("subtype must statically match function result subtype",
589 Subtype_Mark (Subtype_Ind));
593 -- For two anonymous access to subprogram types, the
594 -- types themselves must be type conformant.
596 if not Conforming_Types
597 (R_Stm_Type, R_Type, Fully_Conformant)
600 ("subtype must statically match function result subtype",
606 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
609 -- Subtype indication case: check that the return object's type is
610 -- covered by the result type, and that the subtypes statically match
611 -- when the result subtype is constrained. Also handle record types
612 -- with unknown discriminants for which we have built the underlying
613 -- record view. Coverage is needed to allow specific-type return
614 -- objects when the result type is class-wide (see AI05-32).
616 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
617 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
621 Underlying_Record_View (Base_Type (R_Stm_Type))))
623 -- A null exclusion may be present on the return type, on the
624 -- function specification, on the object declaration or on the
627 if Is_Access_Type (R_Type)
629 (Can_Never_Be_Null (R_Type)
630 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
631 Can_Never_Be_Null (R_Stm_Type)
634 ("subtype must statically match function result subtype",
638 -- AI05-103: for elementary types, subtypes must statically match
640 if Is_Constrained (R_Type)
641 or else Is_Access_Type (R_Type)
643 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
645 ("subtype must statically match function result subtype",
650 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
651 and then Is_Null_Extension (Base_Type (R_Type))
657 ("wrong type for return_subtype_indication", Subtype_Ind);
659 end Check_Return_Subtype_Indication;
661 ---------------------
662 -- Local Variables --
663 ---------------------
667 -- Start of processing for Analyze_Function_Return
670 Set_Return_Present (Scope_Id);
672 if Nkind (N) = N_Simple_Return_Statement then
673 Expr := Expression (N);
674 Analyze_And_Resolve (Expr, R_Type);
675 Check_Limited_Return (Expr);
678 -- Analyze parts specific to extended_return_statement:
681 Obj_Decl : constant Node_Id :=
682 Last (Return_Object_Declarations (N));
684 HSS : constant Node_Id := Handled_Statement_Sequence (N);
687 Expr := Expression (Obj_Decl);
689 -- Note: The check for OK_For_Limited_Init will happen in
690 -- Analyze_Object_Declaration; we treat it as a normal
691 -- object declaration.
693 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
696 Check_Return_Subtype_Indication (Obj_Decl);
698 if Present (HSS) then
701 if Present (Exception_Handlers (HSS)) then
703 -- ???Has_Nested_Block_With_Handler needs to be set.
704 -- Probably by creating an actual N_Block_Statement.
705 -- Probably in Expand.
711 -- Mark the return object as referenced, since the return is an
712 -- implicit reference of the object.
714 Set_Referenced (Defining_Identifier (Obj_Decl));
716 Check_References (Stm_Entity);
720 -- Case of Expr present
724 -- Defend against previous errors
726 and then Nkind (Expr) /= N_Empty
727 and then Present (Etype (Expr))
729 -- Apply constraint check. Note that this is done before the implicit
730 -- conversion of the expression done for anonymous access types to
731 -- ensure correct generation of the null-excluding check associated
732 -- with null-excluding expressions found in return statements.
734 Apply_Constraint_Check (Expr, R_Type);
736 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
737 -- type, apply an implicit conversion of the expression to that type
738 -- to force appropriate static and run-time accessibility checks.
740 if Ada_Version >= Ada_2005
741 and then Ekind (R_Type) = E_Anonymous_Access_Type
743 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
744 Analyze_And_Resolve (Expr, R_Type);
747 -- If the result type is class-wide, then check that the return
748 -- expression's type is not declared at a deeper level than the
749 -- function (RM05-6.5(5.6/2)).
751 if Ada_Version >= Ada_2005
752 and then Is_Class_Wide_Type (R_Type)
754 if Type_Access_Level (Etype (Expr)) >
755 Subprogram_Access_Level (Scope_Id)
758 ("level of return expression type is deeper than " &
759 "class-wide function!", Expr);
763 -- Check incorrect use of dynamically tagged expression
765 if Is_Tagged_Type (R_Type) then
766 Check_Dynamically_Tagged_Expression
772 -- ??? A real run-time accessibility check is needed in cases
773 -- involving dereferences of access parameters. For now we just
774 -- check the static cases.
776 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
777 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
778 and then Object_Access_Level (Expr) >
779 Subprogram_Access_Level (Scope_Id)
782 -- Suppress the message in a generic, where the rewriting
785 if Inside_A_Generic then
790 Make_Raise_Program_Error (Loc,
791 Reason => PE_Accessibility_Check_Failed));
795 ("cannot return a local value by reference?", N);
797 ("\& will be raised at run time?",
798 N, Standard_Program_Error);
803 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
804 and then Null_Exclusion_Present (Parent (Scope_Id))
806 Apply_Compile_Time_Constraint_Error
808 Msg => "(Ada 2005) null not allowed for "
809 & "null-excluding return?",
810 Reason => CE_Null_Not_Allowed);
813 -- Apply checks suggested by AI05-0144 (dangerous order dependence)
815 Check_Order_Dependence;
817 end Analyze_Function_Return;
819 -------------------------------------
820 -- Analyze_Generic_Subprogram_Body --
821 -------------------------------------
823 procedure Analyze_Generic_Subprogram_Body
827 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
828 Kind : constant Entity_Kind := Ekind (Gen_Id);
834 -- Copy body and disable expansion while analyzing the generic For a
835 -- stub, do not copy the stub (which would load the proper body), this
836 -- will be done when the proper body is analyzed.
838 if Nkind (N) /= N_Subprogram_Body_Stub then
839 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
844 Spec := Specification (N);
846 -- Within the body of the generic, the subprogram is callable, and
847 -- behaves like the corresponding non-generic unit.
849 Body_Id := Defining_Entity (Spec);
851 if Kind = E_Generic_Procedure
852 and then Nkind (Spec) /= N_Procedure_Specification
854 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
857 elsif Kind = E_Generic_Function
858 and then Nkind (Spec) /= N_Function_Specification
860 Error_Msg_N ("invalid body for generic function ", Body_Id);
864 Set_Corresponding_Body (Gen_Decl, Body_Id);
866 if Has_Completion (Gen_Id)
867 and then Nkind (Parent (N)) /= N_Subunit
869 Error_Msg_N ("duplicate generic body", N);
872 Set_Has_Completion (Gen_Id);
875 if Nkind (N) = N_Subprogram_Body_Stub then
876 Set_Ekind (Defining_Entity (Specification (N)), Kind);
878 Set_Corresponding_Spec (N, Gen_Id);
881 if Nkind (Parent (N)) = N_Compilation_Unit then
882 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
885 -- Make generic parameters immediately visible in the body. They are
886 -- needed to process the formals declarations. Then make the formals
887 -- visible in a separate step.
893 First_Ent : Entity_Id;
896 First_Ent := First_Entity (Gen_Id);
899 while Present (E) and then not Is_Formal (E) loop
904 Set_Use (Generic_Formal_Declarations (Gen_Decl));
906 -- Now generic formals are visible, and the specification can be
907 -- analyzed, for subsequent conformance check.
909 Body_Id := Analyze_Subprogram_Specification (Spec);
911 -- Make formal parameters visible
915 -- E is the first formal parameter, we loop through the formals
916 -- installing them so that they will be visible.
918 Set_First_Entity (Gen_Id, E);
919 while Present (E) loop
925 -- Visible generic entity is callable within its own body
927 Set_Ekind (Gen_Id, Ekind (Body_Id));
928 Set_Ekind (Body_Id, E_Subprogram_Body);
929 Set_Convention (Body_Id, Convention (Gen_Id));
930 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
931 Set_Scope (Body_Id, Scope (Gen_Id));
932 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
934 if Nkind (N) = N_Subprogram_Body_Stub then
936 -- No body to analyze, so restore state of generic unit
938 Set_Ekind (Gen_Id, Kind);
939 Set_Ekind (Body_Id, Kind);
941 if Present (First_Ent) then
942 Set_First_Entity (Gen_Id, First_Ent);
949 -- If this is a compilation unit, it must be made visible explicitly,
950 -- because the compilation of the declaration, unlike other library
951 -- unit declarations, does not. If it is not a unit, the following
952 -- is redundant but harmless.
954 Set_Is_Immediately_Visible (Gen_Id);
955 Reference_Body_Formals (Gen_Id, Body_Id);
957 if Is_Child_Unit (Gen_Id) then
958 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
961 Set_Actual_Subtypes (N, Current_Scope);
962 Process_PPCs (N, Gen_Id, Body_Id);
964 -- If the generic unit carries pre- or post-conditions, copy them
965 -- to the original generic tree, so that they are properly added
966 -- to any instantiation.
969 Orig : constant Node_Id := Original_Node (N);
973 Cond := First (Declarations (N));
974 while Present (Cond) loop
975 if Nkind (Cond) = N_Pragma
976 and then Pragma_Name (Cond) = Name_Check
978 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
980 elsif Nkind (Cond) = N_Pragma
981 and then Pragma_Name (Cond) = Name_Postcondition
983 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
984 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
993 Analyze_Declarations (Declarations (N));
995 Analyze (Handled_Statement_Sequence (N));
997 Save_Global_References (Original_Node (N));
999 -- Prior to exiting the scope, include generic formals again (if any
1000 -- are present) in the set of local entities.
1002 if Present (First_Ent) then
1003 Set_First_Entity (Gen_Id, First_Ent);
1006 Check_References (Gen_Id);
1009 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1011 Check_Subprogram_Order (N);
1013 -- Outside of its body, unit is generic again
1015 Set_Ekind (Gen_Id, Kind);
1016 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1019 Style.Check_Identifier (Body_Id, Gen_Id);
1023 end Analyze_Generic_Subprogram_Body;
1025 -----------------------------
1026 -- Analyze_Operator_Symbol --
1027 -----------------------------
1029 -- An operator symbol such as "+" or "and" may appear in context where the
1030 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1031 -- is just a string, as in (conjunction = "or"). In these cases the parser
1032 -- generates this node, and the semantics does the disambiguation. Other
1033 -- such case are actuals in an instantiation, the generic unit in an
1034 -- instantiation, and pragma arguments.
1036 procedure Analyze_Operator_Symbol (N : Node_Id) is
1037 Par : constant Node_Id := Parent (N);
1040 if (Nkind (Par) = N_Function_Call
1041 and then N = Name (Par))
1042 or else Nkind (Par) = N_Function_Instantiation
1043 or else (Nkind (Par) = N_Indexed_Component
1044 and then N = Prefix (Par))
1045 or else (Nkind (Par) = N_Pragma_Argument_Association
1046 and then not Is_Pragma_String_Literal (Par))
1047 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1048 or else (Nkind (Par) = N_Attribute_Reference
1049 and then Attribute_Name (Par) /= Name_Value)
1051 Find_Direct_Name (N);
1054 Change_Operator_Symbol_To_String_Literal (N);
1057 end Analyze_Operator_Symbol;
1059 -----------------------------------
1060 -- Analyze_Parameter_Association --
1061 -----------------------------------
1063 procedure Analyze_Parameter_Association (N : Node_Id) is
1065 Analyze (Explicit_Actual_Parameter (N));
1066 end Analyze_Parameter_Association;
1068 --------------------------------------
1069 -- Analyze_Parameterized_Expression --
1070 --------------------------------------
1072 procedure Analyze_Parameterized_Expression (N : Node_Id) is
1073 Loc : constant Source_Ptr := Sloc (N);
1074 LocX : constant Source_Ptr := Sloc (Expression (N));
1077 -- This is one of the occasions on which we write things during semantic
1078 -- analysis. Transform the parameterized expression into an equivalent
1079 -- subprogram body, and then analyze that.
1082 Make_Subprogram_Body (Loc,
1083 Specification => Specification (N),
1084 Declarations => Empty_List,
1085 Handled_Statement_Sequence =>
1086 Make_Handled_Sequence_Of_Statements (LocX,
1087 Statements => New_List (
1088 Make_Simple_Return_Statement (LocX,
1089 Expression => Expression (N))))));
1091 end Analyze_Parameterized_Expression;
1093 ----------------------------
1094 -- Analyze_Procedure_Call --
1095 ----------------------------
1097 procedure Analyze_Procedure_Call (N : Node_Id) is
1098 Loc : constant Source_Ptr := Sloc (N);
1099 P : constant Node_Id := Name (N);
1100 Actuals : constant List_Id := Parameter_Associations (N);
1104 procedure Analyze_Call_And_Resolve;
1105 -- Do Analyze and Resolve calls for procedure call
1106 -- At end, check illegal order dependence.
1108 ------------------------------
1109 -- Analyze_Call_And_Resolve --
1110 ------------------------------
1112 procedure Analyze_Call_And_Resolve is
1114 if Nkind (N) = N_Procedure_Call_Statement then
1116 Resolve (N, Standard_Void_Type);
1118 -- Apply checks suggested by AI05-0144
1120 Check_Order_Dependence;
1125 end Analyze_Call_And_Resolve;
1127 -- Start of processing for Analyze_Procedure_Call
1130 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1131 -- a procedure call or an entry call. The prefix may denote an access
1132 -- to subprogram type, in which case an implicit dereference applies.
1133 -- If the prefix is an indexed component (without implicit dereference)
1134 -- then the construct denotes a call to a member of an entire family.
1135 -- If the prefix is a simple name, it may still denote a call to a
1136 -- parameterless member of an entry family. Resolution of these various
1137 -- interpretations is delicate.
1141 -- If this is a call of the form Obj.Op, the call may have been
1142 -- analyzed and possibly rewritten into a block, in which case
1145 if Analyzed (N) then
1149 -- If there is an error analyzing the name (which may have been
1150 -- rewritten if the original call was in prefix notation) then error
1151 -- has been emitted already, mark node and return.
1154 or else Etype (Name (N)) = Any_Type
1156 Set_Etype (N, Any_Type);
1160 -- Otherwise analyze the parameters
1162 if Present (Actuals) then
1163 Actual := First (Actuals);
1165 while Present (Actual) loop
1167 Check_Parameterless_Call (Actual);
1172 -- Special processing for Elab_Spec and Elab_Body calls
1174 if Nkind (P) = N_Attribute_Reference
1175 and then (Attribute_Name (P) = Name_Elab_Spec
1176 or else Attribute_Name (P) = Name_Elab_Body)
1178 if Present (Actuals) then
1180 ("no parameters allowed for this call", First (Actuals));
1184 Set_Etype (N, Standard_Void_Type);
1187 elsif Is_Entity_Name (P)
1188 and then Is_Record_Type (Etype (Entity (P)))
1189 and then Remote_AST_I_Dereference (P)
1193 elsif Is_Entity_Name (P)
1194 and then Ekind (Entity (P)) /= E_Entry_Family
1196 if Is_Access_Type (Etype (P))
1197 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1198 and then No (Actuals)
1199 and then Comes_From_Source (N)
1201 Error_Msg_N ("missing explicit dereference in call", N);
1204 Analyze_Call_And_Resolve;
1206 -- If the prefix is the simple name of an entry family, this is
1207 -- a parameterless call from within the task body itself.
1209 elsif Is_Entity_Name (P)
1210 and then Nkind (P) = N_Identifier
1211 and then Ekind (Entity (P)) = E_Entry_Family
1212 and then Present (Actuals)
1213 and then No (Next (First (Actuals)))
1215 -- Can be call to parameterless entry family. What appears to be the
1216 -- sole argument is in fact the entry index. Rewrite prefix of node
1217 -- accordingly. Source representation is unchanged by this
1221 Make_Indexed_Component (Loc,
1223 Make_Selected_Component (Loc,
1224 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1225 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1226 Expressions => Actuals);
1227 Set_Name (N, New_N);
1228 Set_Etype (New_N, Standard_Void_Type);
1229 Set_Parameter_Associations (N, No_List);
1230 Analyze_Call_And_Resolve;
1232 elsif Nkind (P) = N_Explicit_Dereference then
1233 if Ekind (Etype (P)) = E_Subprogram_Type then
1234 Analyze_Call_And_Resolve;
1236 Error_Msg_N ("expect access to procedure in call", P);
1239 -- The name can be a selected component or an indexed component that
1240 -- yields an access to subprogram. Such a prefix is legal if the call
1241 -- has parameter associations.
1243 elsif Is_Access_Type (Etype (P))
1244 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1246 if Present (Actuals) then
1247 Analyze_Call_And_Resolve;
1249 Error_Msg_N ("missing explicit dereference in call ", N);
1252 -- If not an access to subprogram, then the prefix must resolve to the
1253 -- name of an entry, entry family, or protected operation.
1255 -- For the case of a simple entry call, P is a selected component where
1256 -- the prefix is the task and the selector name is the entry. A call to
1257 -- a protected procedure will have the same syntax. If the protected
1258 -- object contains overloaded operations, the entity may appear as a
1259 -- function, the context will select the operation whose type is Void.
1261 elsif Nkind (P) = N_Selected_Component
1262 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1264 Ekind (Entity (Selector_Name (P))) = E_Procedure
1266 Ekind (Entity (Selector_Name (P))) = E_Function)
1268 Analyze_Call_And_Resolve;
1270 elsif Nkind (P) = N_Selected_Component
1271 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1272 and then Present (Actuals)
1273 and then No (Next (First (Actuals)))
1275 -- Can be call to parameterless entry family. What appears to be the
1276 -- sole argument is in fact the entry index. Rewrite prefix of node
1277 -- accordingly. Source representation is unchanged by this
1281 Make_Indexed_Component (Loc,
1282 Prefix => New_Copy (P),
1283 Expressions => Actuals);
1284 Set_Name (N, New_N);
1285 Set_Etype (New_N, Standard_Void_Type);
1286 Set_Parameter_Associations (N, No_List);
1287 Analyze_Call_And_Resolve;
1289 -- For the case of a reference to an element of an entry family, P is
1290 -- an indexed component whose prefix is a selected component (task and
1291 -- entry family), and whose index is the entry family index.
1293 elsif Nkind (P) = N_Indexed_Component
1294 and then Nkind (Prefix (P)) = N_Selected_Component
1295 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1297 Analyze_Call_And_Resolve;
1299 -- If the prefix is the name of an entry family, it is a call from
1300 -- within the task body itself.
1302 elsif Nkind (P) = N_Indexed_Component
1303 and then Nkind (Prefix (P)) = N_Identifier
1304 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1307 Make_Selected_Component (Loc,
1308 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1309 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1310 Rewrite (Prefix (P), New_N);
1312 Analyze_Call_And_Resolve;
1314 -- Anything else is an error
1317 Error_Msg_N ("invalid procedure or entry call", N);
1319 end Analyze_Procedure_Call;
1321 -------------------------------------
1322 -- Analyze_Simple_Return_Statement --
1323 -------------------------------------
1325 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1327 if Present (Expression (N)) then
1328 Mark_Coextensions (N, Expression (N));
1331 Analyze_Return_Statement (N);
1332 end Analyze_Simple_Return_Statement;
1334 -------------------------
1335 -- Analyze_Return_Type --
1336 -------------------------
1338 procedure Analyze_Return_Type (N : Node_Id) is
1339 Designator : constant Entity_Id := Defining_Entity (N);
1340 Typ : Entity_Id := Empty;
1343 -- Normal case where result definition does not indicate an error
1345 if Result_Definition (N) /= Error then
1346 if Nkind (Result_Definition (N)) = N_Access_Definition then
1348 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1351 AD : constant Node_Id :=
1352 Access_To_Subprogram_Definition (Result_Definition (N));
1354 if Present (AD) and then Protected_Present (AD) then
1355 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1357 Typ := Access_Definition (N, Result_Definition (N));
1361 Set_Parent (Typ, Result_Definition (N));
1362 Set_Is_Local_Anonymous_Access (Typ);
1363 Set_Etype (Designator, Typ);
1365 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1367 Null_Exclusion_Static_Checks (N);
1369 -- Subtype_Mark case
1372 Find_Type (Result_Definition (N));
1373 Typ := Entity (Result_Definition (N));
1374 Set_Etype (Designator, Typ);
1376 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1378 Null_Exclusion_Static_Checks (N);
1380 -- If a null exclusion is imposed on the result type, then create
1381 -- a null-excluding itype (an access subtype) and use it as the
1382 -- function's Etype. Note that the null exclusion checks are done
1383 -- right before this, because they don't get applied to types that
1384 -- do not come from source.
1386 if Is_Access_Type (Typ)
1387 and then Null_Exclusion_Present (N)
1389 Set_Etype (Designator,
1390 Create_Null_Excluding_Itype
1393 Scope_Id => Scope (Current_Scope)));
1395 -- The new subtype must be elaborated before use because
1396 -- it is visible outside of the function. However its base
1397 -- type may not be frozen yet, so the reference that will
1398 -- force elaboration must be attached to the freezing of
1401 -- If the return specification appears on a proper body,
1402 -- the subtype will have been created already on the spec.
1404 if Is_Frozen (Typ) then
1405 if Nkind (Parent (N)) = N_Subprogram_Body
1406 and then Nkind (Parent (Parent (N))) = N_Subunit
1410 Build_Itype_Reference (Etype (Designator), Parent (N));
1414 Ensure_Freeze_Node (Typ);
1417 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1419 Set_Itype (IR, Etype (Designator));
1420 Append_Freeze_Actions (Typ, New_List (IR));
1425 Set_Etype (Designator, Typ);
1428 if Ekind (Typ) = E_Incomplete_Type
1429 and then Is_Value_Type (Typ)
1433 elsif Ekind (Typ) = E_Incomplete_Type
1434 or else (Is_Class_Wide_Type (Typ)
1436 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1438 -- AI05-0151: Tagged incomplete types are allowed in all formal
1439 -- parts. Untagged incomplete types are not allowed in bodies.
1441 if Ada_Version >= Ada_2012 then
1442 if Is_Tagged_Type (Typ) then
1445 elsif Nkind_In (Parent (Parent (N)),
1451 ("invalid use of untagged incomplete type&",
1457 ("invalid use of incomplete type&", Designator, Typ);
1462 -- Case where result definition does indicate an error
1465 Set_Etype (Designator, Any_Type);
1467 end Analyze_Return_Type;
1469 -----------------------------
1470 -- Analyze_Subprogram_Body --
1471 -----------------------------
1473 procedure Analyze_Subprogram_Body (N : Node_Id) is
1474 Loc : constant Source_Ptr := Sloc (N);
1475 Body_Spec : constant Node_Id := Specification (N);
1476 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1479 if Debug_Flag_C then
1480 Write_Str ("==> subprogram body ");
1481 Write_Name (Chars (Body_Id));
1482 Write_Str (" from ");
1483 Write_Location (Loc);
1488 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1490 -- The real work is split out into the helper, so it can do "return;"
1491 -- without skipping the debug output:
1493 Analyze_Subprogram_Body_Helper (N);
1495 if Debug_Flag_C then
1497 Write_Str ("<== subprogram body ");
1498 Write_Name (Chars (Body_Id));
1499 Write_Str (" from ");
1500 Write_Location (Loc);
1503 end Analyze_Subprogram_Body;
1505 ------------------------------------
1506 -- Analyze_Subprogram_Body_Helper --
1507 ------------------------------------
1509 -- This procedure is called for regular subprogram bodies, generic bodies,
1510 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1511 -- specification matters, and is used to create a proper declaration for
1512 -- the subprogram, or to perform conformance checks.
1514 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1515 Loc : constant Source_Ptr := Sloc (N);
1516 Body_Deleted : constant Boolean := False;
1517 Body_Spec : constant Node_Id := Specification (N);
1518 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1519 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1520 Conformant : Boolean;
1523 Prot_Typ : Entity_Id := Empty;
1524 Spec_Id : Entity_Id;
1525 Spec_Decl : Node_Id := Empty;
1527 Last_Real_Spec_Entity : Entity_Id := Empty;
1528 -- When we analyze a separate spec, the entity chain ends up containing
1529 -- the formals, as well as any itypes generated during analysis of the
1530 -- default expressions for parameters, or the arguments of associated
1531 -- precondition/postcondition pragmas (which are analyzed in the context
1532 -- of the spec since they have visibility on formals).
1534 -- These entities belong with the spec and not the body. However we do
1535 -- the analysis of the body in the context of the spec (again to obtain
1536 -- visibility to the formals), and all the entities generated during
1537 -- this analysis end up also chained to the entity chain of the spec.
1538 -- But they really belong to the body, and there is circuitry to move
1539 -- them from the spec to the body.
1541 -- However, when we do this move, we don't want to move the real spec
1542 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1543 -- variable points to the last real spec entity, so we only move those
1544 -- chained beyond that point. It is initialized to Empty to deal with
1545 -- the case where there is no separate spec.
1547 procedure Check_Anonymous_Return;
1548 -- Ada 2005: if a function returns an access type that denotes a task,
1549 -- or a type that contains tasks, we must create a master entity for
1550 -- the anonymous type, which typically will be used in an allocator
1551 -- in the body of the function.
1553 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1554 -- Look ahead to recognize a pragma that may appear after the body.
1555 -- If there is a previous spec, check that it appears in the same
1556 -- declarative part. If the pragma is Inline_Always, perform inlining
1557 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1558 -- If the body acts as a spec, and inlining is required, we create a
1559 -- subprogram declaration for it, in order to attach the body to inline.
1560 -- If pragma does not appear after the body, check whether there is
1561 -- an inline pragma before any local declarations.
1563 procedure Check_Missing_Return;
1564 -- Checks for a function with a no return statements, and also performs
1565 -- the warning checks implemented by Check_Returns.
1567 function Disambiguate_Spec return Entity_Id;
1568 -- When a primitive is declared between the private view and the full
1569 -- view of a concurrent type which implements an interface, a special
1570 -- mechanism is used to find the corresponding spec of the primitive
1573 function Is_Private_Concurrent_Primitive
1574 (Subp_Id : Entity_Id) return Boolean;
1575 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1576 -- type that implements an interface and has a private view.
1578 procedure Set_Trivial_Subprogram (N : Node_Id);
1579 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1580 -- subprogram whose body is being analyzed. N is the statement node
1581 -- causing the flag to be set, if the following statement is a return
1582 -- of an entity, we mark the entity as set in source to suppress any
1583 -- warning on the stylized use of function stubs with a dummy return.
1585 procedure Verify_Overriding_Indicator;
1586 -- If there was a previous spec, the entity has been entered in the
1587 -- current scope previously. If the body itself carries an overriding
1588 -- indicator, check that it is consistent with the known status of the
1591 ----------------------------
1592 -- Check_Anonymous_Return --
1593 ----------------------------
1595 procedure Check_Anonymous_Return is
1601 if Present (Spec_Id) then
1607 if Ekind (Scop) = E_Function
1608 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1609 and then not Is_Thunk (Scop)
1610 and then (Has_Task (Designated_Type (Etype (Scop)))
1612 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1614 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1615 and then Expander_Active
1617 -- Avoid cases with no tasking support
1619 and then RTE_Available (RE_Current_Master)
1620 and then not Restriction_Active (No_Task_Hierarchy)
1623 Make_Object_Declaration (Loc,
1624 Defining_Identifier =>
1625 Make_Defining_Identifier (Loc, Name_uMaster),
1626 Constant_Present => True,
1627 Object_Definition =>
1628 New_Reference_To (RTE (RE_Master_Id), Loc),
1630 Make_Explicit_Dereference (Loc,
1631 New_Reference_To (RTE (RE_Current_Master), Loc)));
1633 if Present (Declarations (N)) then
1634 Prepend (Decl, Declarations (N));
1636 Set_Declarations (N, New_List (Decl));
1639 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1640 Set_Has_Master_Entity (Scop);
1642 -- Now mark the containing scope as a task master
1645 while Nkind (Par) /= N_Compilation_Unit loop
1646 Par := Parent (Par);
1647 pragma Assert (Present (Par));
1649 -- If we fall off the top, we are at the outer level, and
1650 -- the environment task is our effective master, so nothing
1654 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1656 Set_Is_Task_Master (Par, True);
1661 end Check_Anonymous_Return;
1663 -------------------------
1664 -- Check_Inline_Pragma --
1665 -------------------------
1667 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1671 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1672 -- True when N is a pragma Inline or Inline_Always that applies
1673 -- to this subprogram.
1675 -----------------------
1676 -- Is_Inline_Pragma --
1677 -----------------------
1679 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1682 Nkind (N) = N_Pragma
1684 (Pragma_Name (N) = Name_Inline_Always
1687 and then Pragma_Name (N) = Name_Inline))
1690 (Expression (First (Pragma_Argument_Associations (N))))
1692 end Is_Inline_Pragma;
1694 -- Start of processing for Check_Inline_Pragma
1697 if not Expander_Active then
1701 if Is_List_Member (N)
1702 and then Present (Next (N))
1703 and then Is_Inline_Pragma (Next (N))
1707 elsif Nkind (N) /= N_Subprogram_Body_Stub
1708 and then Present (Declarations (N))
1709 and then Is_Inline_Pragma (First (Declarations (N)))
1711 Prag := First (Declarations (N));
1717 if Present (Prag) then
1718 if Present (Spec_Id) then
1719 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1724 -- Create a subprogram declaration, to make treatment uniform
1727 Subp : constant Entity_Id :=
1728 Make_Defining_Identifier (Loc, Chars (Body_Id));
1729 Decl : constant Node_Id :=
1730 Make_Subprogram_Declaration (Loc,
1732 New_Copy_Tree (Specification (N)));
1735 Set_Defining_Unit_Name (Specification (Decl), Subp);
1737 if Present (First_Formal (Body_Id)) then
1738 Plist := Copy_Parameter_List (Body_Id);
1739 Set_Parameter_Specifications
1740 (Specification (Decl), Plist);
1743 Insert_Before (N, Decl);
1746 Set_Has_Pragma_Inline (Subp);
1748 if Pragma_Name (Prag) = Name_Inline_Always then
1749 Set_Is_Inlined (Subp);
1750 Set_Has_Pragma_Inline_Always (Subp);
1757 end Check_Inline_Pragma;
1759 --------------------------
1760 -- Check_Missing_Return --
1761 --------------------------
1763 procedure Check_Missing_Return is
1765 Missing_Ret : Boolean;
1768 if Nkind (Body_Spec) = N_Function_Specification then
1769 if Present (Spec_Id) then
1775 if Return_Present (Id) then
1776 Check_Returns (HSS, 'F', Missing_Ret);
1779 Set_Has_Missing_Return (Id);
1782 elsif (Is_Generic_Subprogram (Id)
1783 or else not Is_Machine_Code_Subprogram (Id))
1784 and then not Body_Deleted
1786 Error_Msg_N ("missing RETURN statement in function body", N);
1789 -- If procedure with No_Return, check returns
1791 elsif Nkind (Body_Spec) = N_Procedure_Specification
1792 and then Present (Spec_Id)
1793 and then No_Return (Spec_Id)
1795 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1797 end Check_Missing_Return;
1799 -----------------------
1800 -- Disambiguate_Spec --
1801 -----------------------
1803 function Disambiguate_Spec return Entity_Id is
1804 Priv_Spec : Entity_Id;
1807 procedure Replace_Types (To_Corresponding : Boolean);
1808 -- Depending on the flag, replace the type of formal parameters of
1809 -- Body_Id if it is a concurrent type implementing interfaces with
1810 -- the corresponding record type or the other way around.
1812 procedure Replace_Types (To_Corresponding : Boolean) is
1814 Formal_Typ : Entity_Id;
1817 Formal := First_Formal (Body_Id);
1818 while Present (Formal) loop
1819 Formal_Typ := Etype (Formal);
1821 -- From concurrent type to corresponding record
1823 if To_Corresponding then
1824 if Is_Concurrent_Type (Formal_Typ)
1825 and then Present (Corresponding_Record_Type (Formal_Typ))
1826 and then Present (Interfaces (
1827 Corresponding_Record_Type (Formal_Typ)))
1830 Corresponding_Record_Type (Formal_Typ));
1833 -- From corresponding record to concurrent type
1836 if Is_Concurrent_Record_Type (Formal_Typ)
1837 and then Present (Interfaces (Formal_Typ))
1840 Corresponding_Concurrent_Type (Formal_Typ));
1844 Next_Formal (Formal);
1848 -- Start of processing for Disambiguate_Spec
1851 -- Try to retrieve the specification of the body as is. All error
1852 -- messages are suppressed because the body may not have a spec in
1853 -- its current state.
1855 Spec_N := Find_Corresponding_Spec (N, False);
1857 -- It is possible that this is the body of a primitive declared
1858 -- between a private and a full view of a concurrent type. The
1859 -- controlling parameter of the spec carries the concurrent type,
1860 -- not the corresponding record type as transformed by Analyze_
1861 -- Subprogram_Specification. In such cases, we undo the change
1862 -- made by the analysis of the specification and try to find the
1865 -- Note that wrappers already have their corresponding specs and
1866 -- bodies set during their creation, so if the candidate spec is
1867 -- a wrapper, then we definitely need to swap all types to their
1868 -- original concurrent status.
1871 or else Is_Primitive_Wrapper (Spec_N)
1873 -- Restore all references of corresponding record types to the
1874 -- original concurrent types.
1876 Replace_Types (To_Corresponding => False);
1877 Priv_Spec := Find_Corresponding_Spec (N, False);
1879 -- The current body truly belongs to a primitive declared between
1880 -- a private and a full view. We leave the modified body as is,
1881 -- and return the true spec.
1883 if Present (Priv_Spec)
1884 and then Is_Private_Primitive (Priv_Spec)
1889 -- In case that this is some sort of error, restore the original
1890 -- state of the body.
1892 Replace_Types (To_Corresponding => True);
1896 end Disambiguate_Spec;
1898 -------------------------------------
1899 -- Is_Private_Concurrent_Primitive --
1900 -------------------------------------
1902 function Is_Private_Concurrent_Primitive
1903 (Subp_Id : Entity_Id) return Boolean
1905 Formal_Typ : Entity_Id;
1908 if Present (First_Formal (Subp_Id)) then
1909 Formal_Typ := Etype (First_Formal (Subp_Id));
1911 if Is_Concurrent_Record_Type (Formal_Typ) then
1912 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1915 -- The type of the first formal is a concurrent tagged type with
1919 Is_Concurrent_Type (Formal_Typ)
1920 and then Is_Tagged_Type (Formal_Typ)
1921 and then Has_Private_Declaration (Formal_Typ);
1925 end Is_Private_Concurrent_Primitive;
1927 ----------------------------
1928 -- Set_Trivial_Subprogram --
1929 ----------------------------
1931 procedure Set_Trivial_Subprogram (N : Node_Id) is
1932 Nxt : constant Node_Id := Next (N);
1935 Set_Is_Trivial_Subprogram (Body_Id);
1937 if Present (Spec_Id) then
1938 Set_Is_Trivial_Subprogram (Spec_Id);
1942 and then Nkind (Nxt) = N_Simple_Return_Statement
1943 and then No (Next (Nxt))
1944 and then Present (Expression (Nxt))
1945 and then Is_Entity_Name (Expression (Nxt))
1947 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1949 end Set_Trivial_Subprogram;
1951 ---------------------------------
1952 -- Verify_Overriding_Indicator --
1953 ---------------------------------
1955 procedure Verify_Overriding_Indicator is
1957 if Must_Override (Body_Spec) then
1958 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1959 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1963 elsif not Present (Overridden_Operation (Spec_Id)) then
1965 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1968 elsif Must_Not_Override (Body_Spec) then
1969 if Present (Overridden_Operation (Spec_Id)) then
1971 ("subprogram& overrides inherited operation",
1972 Body_Spec, Spec_Id);
1974 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1975 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1978 ("subprogram & overrides predefined operator ",
1979 Body_Spec, Spec_Id);
1981 -- If this is not a primitive operation or protected subprogram,
1982 -- then the overriding indicator is altogether illegal.
1984 elsif not Is_Primitive (Spec_Id)
1985 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
1988 ("overriding indicator only allowed " &
1989 "if subprogram is primitive",
1993 elsif Style_Check -- ??? incorrect use of Style_Check!
1994 and then Present (Overridden_Operation (Spec_Id))
1996 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
1997 Style.Missing_Overriding (N, Body_Id);
1999 end Verify_Overriding_Indicator;
2001 -- Start of processing for Analyze_Subprogram_Body_Helper
2004 -- Generic subprograms are handled separately. They always have a
2005 -- generic specification. Determine whether current scope has a
2006 -- previous declaration.
2008 -- If the subprogram body is defined within an instance of the same
2009 -- name, the instance appears as a package renaming, and will be hidden
2010 -- within the subprogram.
2012 if Present (Prev_Id)
2013 and then not Is_Overloadable (Prev_Id)
2014 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2015 or else Comes_From_Source (Prev_Id))
2017 if Is_Generic_Subprogram (Prev_Id) then
2019 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2020 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2022 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2024 if Nkind (N) = N_Subprogram_Body then
2025 HSS := Handled_Statement_Sequence (N);
2026 Check_Missing_Return;
2032 -- Previous entity conflicts with subprogram name. Attempting to
2033 -- enter name will post error.
2035 Enter_Name (Body_Id);
2039 -- Non-generic case, find the subprogram declaration, if one was seen,
2040 -- or enter new overloaded entity in the current scope. If the
2041 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2042 -- part of the context of one of its subunits. No need to redo the
2045 elsif Prev_Id = Body_Id
2046 and then Has_Completion (Body_Id)
2051 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2053 if Nkind (N) = N_Subprogram_Body_Stub
2054 or else No (Corresponding_Spec (N))
2056 if Is_Private_Concurrent_Primitive (Body_Id) then
2057 Spec_Id := Disambiguate_Spec;
2059 Spec_Id := Find_Corresponding_Spec (N);
2062 -- If this is a duplicate body, no point in analyzing it
2064 if Error_Posted (N) then
2068 -- A subprogram body should cause freezing of its own declaration,
2069 -- but if there was no previous explicit declaration, then the
2070 -- subprogram will get frozen too late (there may be code within
2071 -- the body that depends on the subprogram having been frozen,
2072 -- such as uses of extra formals), so we force it to be frozen
2073 -- here. Same holds if the body and spec are compilation units.
2074 -- Finally, if the return type is an anonymous access to protected
2075 -- subprogram, it must be frozen before the body because its
2076 -- expansion has generated an equivalent type that is used when
2077 -- elaborating the body.
2079 if No (Spec_Id) then
2080 Freeze_Before (N, Body_Id);
2082 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2083 Freeze_Before (N, Spec_Id);
2085 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2086 Freeze_Before (N, Etype (Body_Id));
2090 Spec_Id := Corresponding_Spec (N);
2094 -- Do not inline any subprogram that contains nested subprograms, since
2095 -- the backend inlining circuit seems to generate uninitialized
2096 -- references in this case. We know this happens in the case of front
2097 -- end ZCX support, but it also appears it can happen in other cases as
2098 -- well. The backend often rejects attempts to inline in the case of
2099 -- nested procedures anyway, so little if anything is lost by this.
2100 -- Note that this is test is for the benefit of the back-end. There is
2101 -- a separate test for front-end inlining that also rejects nested
2104 -- Do not do this test if errors have been detected, because in some
2105 -- error cases, this code blows up, and we don't need it anyway if
2106 -- there have been errors, since we won't get to the linker anyway.
2108 if Comes_From_Source (Body_Id)
2109 and then Serious_Errors_Detected = 0
2113 P_Ent := Scope (P_Ent);
2114 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2116 if Is_Subprogram (P_Ent) then
2117 Set_Is_Inlined (P_Ent, False);
2119 if Comes_From_Source (P_Ent)
2120 and then Has_Pragma_Inline (P_Ent)
2123 ("cannot inline& (nested subprogram)?",
2130 Check_Inline_Pragma (Spec_Id);
2132 -- Deal with special case of a fully private operation in the body of
2133 -- the protected type. We must create a declaration for the subprogram,
2134 -- in order to attach the protected subprogram that will be used in
2135 -- internal calls. We exclude compiler generated bodies from the
2136 -- expander since the issue does not arise for those cases.
2139 and then Comes_From_Source (N)
2140 and then Is_Protected_Type (Current_Scope)
2142 Spec_Id := Build_Private_Protected_Declaration (N);
2145 -- If a separate spec is present, then deal with freezing issues
2147 if Present (Spec_Id) then
2148 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2149 Verify_Overriding_Indicator;
2151 -- In general, the spec will be frozen when we start analyzing the
2152 -- body. However, for internally generated operations, such as
2153 -- wrapper functions for inherited operations with controlling
2154 -- results, the spec may not have been frozen by the time we
2155 -- expand the freeze actions that include the bodies. In particular,
2156 -- extra formals for accessibility or for return-in-place may need
2157 -- to be generated. Freeze nodes, if any, are inserted before the
2160 if not Is_Frozen (Spec_Id)
2161 and then Expander_Active
2163 -- Force the generation of its freezing node to ensure proper
2164 -- management of access types in the backend.
2166 -- This is definitely needed for some cases, but it is not clear
2167 -- why, to be investigated further???
2169 Set_Has_Delayed_Freeze (Spec_Id);
2170 Freeze_Before (N, Spec_Id);
2174 -- Mark presence of postcondition procedure in current scope and mark
2175 -- the procedure itself as needing debug info. The latter is important
2176 -- when analyzing decision coverage (for example, for MC/DC coverage).
2178 if Chars (Body_Id) = Name_uPostconditions then
2179 Set_Has_Postconditions (Current_Scope);
2180 Set_Debug_Info_Needed (Body_Id);
2183 -- Place subprogram on scope stack, and make formals visible. If there
2184 -- is a spec, the visible entity remains that of the spec.
2186 if Present (Spec_Id) then
2187 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2189 if Is_Child_Unit (Spec_Id) then
2190 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2194 Style.Check_Identifier (Body_Id, Spec_Id);
2197 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2198 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2200 if Is_Abstract_Subprogram (Spec_Id) then
2201 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2205 Set_Convention (Body_Id, Convention (Spec_Id));
2206 Set_Has_Completion (Spec_Id);
2208 if Is_Protected_Type (Scope (Spec_Id)) then
2209 Prot_Typ := Scope (Spec_Id);
2212 -- If this is a body generated for a renaming, do not check for
2213 -- full conformance. The check is redundant, because the spec of
2214 -- the body is a copy of the spec in the renaming declaration,
2215 -- and the test can lead to spurious errors on nested defaults.
2217 if Present (Spec_Decl)
2218 and then not Comes_From_Source (N)
2220 (Nkind (Original_Node (Spec_Decl)) =
2221 N_Subprogram_Renaming_Declaration
2222 or else (Present (Corresponding_Body (Spec_Decl))
2224 Nkind (Unit_Declaration_Node
2225 (Corresponding_Body (Spec_Decl))) =
2226 N_Subprogram_Renaming_Declaration))
2230 -- Conversely, the spec may have been generated for specless body
2231 -- with an inline pragma.
2233 elsif Comes_From_Source (N)
2234 and then not Comes_From_Source (Spec_Id)
2235 and then Has_Pragma_Inline (Spec_Id)
2242 Fully_Conformant, True, Conformant, Body_Id);
2245 -- If the body is not fully conformant, we have to decide if we
2246 -- should analyze it or not. If it has a really messed up profile
2247 -- then we probably should not analyze it, since we will get too
2248 -- many bogus messages.
2250 -- Our decision is to go ahead in the non-fully conformant case
2251 -- only if it is at least mode conformant with the spec. Note
2252 -- that the call to Check_Fully_Conformant has issued the proper
2253 -- error messages to complain about the lack of conformance.
2256 and then not Mode_Conformant (Body_Id, Spec_Id)
2262 if Spec_Id /= Body_Id then
2263 Reference_Body_Formals (Spec_Id, Body_Id);
2266 if Nkind (N) /= N_Subprogram_Body_Stub then
2267 Set_Corresponding_Spec (N, Spec_Id);
2269 -- Ada 2005 (AI-345): If the operation is a primitive operation
2270 -- of a concurrent type, the type of the first parameter has been
2271 -- replaced with the corresponding record, which is the proper
2272 -- run-time structure to use. However, within the body there may
2273 -- be uses of the formals that depend on primitive operations
2274 -- of the type (in particular calls in prefixed form) for which
2275 -- we need the original concurrent type. The operation may have
2276 -- several controlling formals, so the replacement must be done
2279 if Comes_From_Source (Spec_Id)
2280 and then Present (First_Entity (Spec_Id))
2281 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2282 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2284 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2287 (Corresponding_Concurrent_Type
2288 (Etype (First_Entity (Spec_Id))))
2291 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2295 Form := First_Formal (Spec_Id);
2296 while Present (Form) loop
2297 if Etype (Form) = Typ then
2298 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2306 -- Make the formals visible, and place subprogram on scope stack.
2307 -- This is also the point at which we set Last_Real_Spec_Entity
2308 -- to mark the entities which will not be moved to the body.
2310 Install_Formals (Spec_Id);
2311 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2312 Push_Scope (Spec_Id);
2314 -- Make sure that the subprogram is immediately visible. For
2315 -- child units that have no separate spec this is indispensable.
2316 -- Otherwise it is safe albeit redundant.
2318 Set_Is_Immediately_Visible (Spec_Id);
2321 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2322 Set_Ekind (Body_Id, E_Subprogram_Body);
2323 Set_Scope (Body_Id, Scope (Spec_Id));
2324 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2326 -- Case of subprogram body with no previous spec
2329 -- Check for style warning required
2333 -- Only apply check for source level subprograms for which checks
2334 -- have not been suppressed.
2336 and then Comes_From_Source (Body_Id)
2337 and then not Suppress_Style_Checks (Body_Id)
2339 -- No warnings within an instance
2341 and then not In_Instance
2343 -- No warnings for parameterized expressions
2345 and then Nkind (Original_Node (N)) /= N_Parameterized_Expression
2347 Style.Body_With_No_Spec (N);
2350 New_Overloaded_Entity (Body_Id);
2352 if Nkind (N) /= N_Subprogram_Body_Stub then
2353 Set_Acts_As_Spec (N);
2354 Generate_Definition (Body_Id);
2356 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2357 Generate_Reference_To_Formals (Body_Id);
2358 Install_Formals (Body_Id);
2359 Push_Scope (Body_Id);
2363 -- If the return type is an anonymous access type whose designated type
2364 -- is the limited view of a class-wide type and the non-limited view is
2365 -- available, update the return type accordingly.
2367 if Ada_Version >= Ada_2005
2368 and then Comes_From_Source (N)
2375 Rtyp := Etype (Current_Scope);
2377 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2378 Etyp := Directly_Designated_Type (Rtyp);
2380 if Is_Class_Wide_Type (Etyp)
2381 and then From_With_Type (Etyp)
2383 Set_Directly_Designated_Type
2384 (Etype (Current_Scope), Available_View (Etyp));
2390 -- If this is the proper body of a stub, we must verify that the stub
2391 -- conforms to the body, and to the previous spec if one was present.
2392 -- we know already that the body conforms to that spec. This test is
2393 -- only required for subprograms that come from source.
2395 if Nkind (Parent (N)) = N_Subunit
2396 and then Comes_From_Source (N)
2397 and then not Error_Posted (Body_Id)
2398 and then Nkind (Corresponding_Stub (Parent (N))) =
2399 N_Subprogram_Body_Stub
2402 Old_Id : constant Entity_Id :=
2404 (Specification (Corresponding_Stub (Parent (N))));
2406 Conformant : Boolean := False;
2409 if No (Spec_Id) then
2410 Check_Fully_Conformant (Body_Id, Old_Id);
2414 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2416 if not Conformant then
2418 -- The stub was taken to be a new declaration. Indicate
2419 -- that it lacks a body.
2421 Set_Has_Completion (Old_Id, False);
2427 Set_Has_Completion (Body_Id);
2428 Check_Eliminated (Body_Id);
2430 if Nkind (N) = N_Subprogram_Body_Stub then
2433 elsif Present (Spec_Id)
2434 and then Expander_Active
2436 (Has_Pragma_Inline_Always (Spec_Id)
2437 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2439 Build_Body_To_Inline (N, Spec_Id);
2442 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2443 -- if its specification we have to install the private withed units.
2444 -- This holds for child units as well.
2446 if Is_Compilation_Unit (Body_Id)
2447 or else Nkind (Parent (N)) = N_Compilation_Unit
2449 Install_Private_With_Clauses (Body_Id);
2452 Check_Anonymous_Return;
2454 -- Set the Protected_Formal field of each extra formal of the protected
2455 -- subprogram to reference the corresponding extra formal of the
2456 -- subprogram that implements it. For regular formals this occurs when
2457 -- the protected subprogram's declaration is expanded, but the extra
2458 -- formals don't get created until the subprogram is frozen. We need to
2459 -- do this before analyzing the protected subprogram's body so that any
2460 -- references to the original subprogram's extra formals will be changed
2461 -- refer to the implementing subprogram's formals (see Expand_Formal).
2463 if Present (Spec_Id)
2464 and then Is_Protected_Type (Scope (Spec_Id))
2465 and then Present (Protected_Body_Subprogram (Spec_Id))
2468 Impl_Subp : constant Entity_Id :=
2469 Protected_Body_Subprogram (Spec_Id);
2470 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2471 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2473 while Present (Prot_Ext_Formal) loop
2474 pragma Assert (Present (Impl_Ext_Formal));
2475 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2476 Next_Formal_With_Extras (Prot_Ext_Formal);
2477 Next_Formal_With_Extras (Impl_Ext_Formal);
2482 -- Now we can go on to analyze the body
2484 HSS := Handled_Statement_Sequence (N);
2485 Set_Actual_Subtypes (N, Current_Scope);
2487 -- Deal with preconditions and postconditions
2489 Process_PPCs (N, Spec_Id, Body_Id);
2491 -- Add a declaration for the Protection object, renaming declarations
2492 -- for discriminals and privals and finally a declaration for the entry
2493 -- family index (if applicable). This form of early expansion is done
2494 -- when the Expander is active because Install_Private_Data_Declarations
2495 -- references entities which were created during regular expansion.
2498 and then Comes_From_Source (N)
2499 and then Present (Prot_Typ)
2500 and then Present (Spec_Id)
2501 and then not Is_Eliminated (Spec_Id)
2503 Install_Private_Data_Declarations
2504 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2507 -- Analyze the declarations (this call will analyze the precondition
2508 -- Check pragmas we prepended to the list, as well as the declaration
2509 -- of the _Postconditions procedure).
2511 Analyze_Declarations (Declarations (N));
2513 -- Check completion, and analyze the statements
2516 Inspect_Deferred_Constant_Completion (Declarations (N));
2519 -- Deal with end of scope processing for the body
2521 Process_End_Label (HSS, 't', Current_Scope);
2523 Check_Subprogram_Order (N);
2524 Set_Analyzed (Body_Id);
2526 -- If we have a separate spec, then the analysis of the declarations
2527 -- caused the entities in the body to be chained to the spec id, but
2528 -- we want them chained to the body id. Only the formal parameters
2529 -- end up chained to the spec id in this case.
2531 if Present (Spec_Id) then
2533 -- We must conform to the categorization of our spec
2535 Validate_Categorization_Dependency (N, Spec_Id);
2537 -- And if this is a child unit, the parent units must conform
2539 if Is_Child_Unit (Spec_Id) then
2540 Validate_Categorization_Dependency
2541 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2544 -- Here is where we move entities from the spec to the body
2546 -- Case where there are entities that stay with the spec
2548 if Present (Last_Real_Spec_Entity) then
2550 -- No body entities (happens when the only real spec entities
2551 -- come from precondition and postcondition pragmas)
2553 if No (Last_Entity (Body_Id)) then
2555 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2557 -- Body entities present (formals), so chain stuff past them
2561 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2564 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2565 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2566 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2568 -- Case where there are no spec entities, in this case there can
2569 -- be no body entities either, so just move everything.
2572 pragma Assert (No (Last_Entity (Body_Id)));
2573 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2574 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2575 Set_First_Entity (Spec_Id, Empty);
2576 Set_Last_Entity (Spec_Id, Empty);
2580 Check_Missing_Return;
2582 -- Now we are going to check for variables that are never modified in
2583 -- the body of the procedure. But first we deal with a special case
2584 -- where we want to modify this check. If the body of the subprogram
2585 -- starts with a raise statement or its equivalent, or if the body
2586 -- consists entirely of a null statement, then it is pretty obvious
2587 -- that it is OK to not reference the parameters. For example, this
2588 -- might be the following common idiom for a stubbed function:
2589 -- statement of the procedure raises an exception. In particular this
2590 -- deals with the common idiom of a stubbed function, which might
2591 -- appear as something like
2593 -- function F (A : Integer) return Some_Type;
2596 -- raise Program_Error;
2600 -- Here the purpose of X is simply to satisfy the annoying requirement
2601 -- in Ada that there be at least one return, and we certainly do not
2602 -- want to go posting warnings on X that it is not initialized! On
2603 -- the other hand, if X is entirely unreferenced that should still
2606 -- What we do is to detect these cases, and if we find them, flag the
2607 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2608 -- suppress unwanted warnings. For the case of the function stub above
2609 -- we have a special test to set X as apparently assigned to suppress
2616 -- Skip initial labels (for one thing this occurs when we are in
2617 -- front end ZCX mode, but in any case it is irrelevant), and also
2618 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2620 Stm := First (Statements (HSS));
2621 while Nkind (Stm) = N_Label
2622 or else Nkind (Stm) in N_Push_xxx_Label
2627 -- Do the test on the original statement before expansion
2630 Ostm : constant Node_Id := Original_Node (Stm);
2633 -- If explicit raise statement, turn on flag
2635 if Nkind (Ostm) = N_Raise_Statement then
2636 Set_Trivial_Subprogram (Stm);
2638 -- If null statement, and no following statements, turn on flag
2640 elsif Nkind (Stm) = N_Null_Statement
2641 and then Comes_From_Source (Stm)
2642 and then No (Next (Stm))
2644 Set_Trivial_Subprogram (Stm);
2646 -- Check for explicit call cases which likely raise an exception
2648 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2649 if Is_Entity_Name (Name (Ostm)) then
2651 Ent : constant Entity_Id := Entity (Name (Ostm));
2654 -- If the procedure is marked No_Return, then likely it
2655 -- raises an exception, but in any case it is not coming
2656 -- back here, so turn on the flag.
2658 if Ekind (Ent) = E_Procedure
2659 and then No_Return (Ent)
2661 Set_Trivial_Subprogram (Stm);
2669 -- Check for variables that are never modified
2675 -- If there is a separate spec, then transfer Never_Set_In_Source
2676 -- flags from out parameters to the corresponding entities in the
2677 -- body. The reason we do that is we want to post error flags on
2678 -- the body entities, not the spec entities.
2680 if Present (Spec_Id) then
2681 E1 := First_Entity (Spec_Id);
2682 while Present (E1) loop
2683 if Ekind (E1) = E_Out_Parameter then
2684 E2 := First_Entity (Body_Id);
2685 while Present (E2) loop
2686 exit when Chars (E1) = Chars (E2);
2690 if Present (E2) then
2691 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2699 -- Check references in body unless it was deleted. Note that the
2700 -- check of Body_Deleted here is not just for efficiency, it is
2701 -- necessary to avoid junk warnings on formal parameters.
2703 if not Body_Deleted then
2704 Check_References (Body_Id);
2707 end Analyze_Subprogram_Body_Helper;
2709 ------------------------------------
2710 -- Analyze_Subprogram_Declaration --
2711 ------------------------------------
2713 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2714 Loc : constant Source_Ptr := Sloc (N);
2715 Scop : constant Entity_Id := Current_Scope;
2716 Designator : Entity_Id;
2718 Null_Body : Node_Id := Empty;
2720 -- Start of processing for Analyze_Subprogram_Declaration
2723 -- For a null procedure, capture the profile before analysis, for
2724 -- expansion at the freeze point and at each point of call. The body
2725 -- will only be used if the procedure has preconditions. In that case
2726 -- the body is analyzed at the freeze point.
2728 if Nkind (Specification (N)) = N_Procedure_Specification
2729 and then Null_Present (Specification (N))
2730 and then Expander_Active
2733 Make_Subprogram_Body (Loc,
2735 New_Copy_Tree (Specification (N)),
2738 Handled_Statement_Sequence =>
2739 Make_Handled_Sequence_Of_Statements (Loc,
2740 Statements => New_List (Make_Null_Statement (Loc))));
2742 -- Create new entities for body and formals
2744 Set_Defining_Unit_Name (Specification (Null_Body),
2745 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2746 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2748 Form := First (Parameter_Specifications (Specification (Null_Body)));
2749 while Present (Form) loop
2750 Set_Defining_Identifier (Form,
2751 Make_Defining_Identifier (Loc,
2752 Chars (Defining_Identifier (Form))));
2754 -- Resolve the types of the formals now, because the freeze point
2755 -- may appear in a different context, e.g. an instantiation.
2757 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
2758 Find_Type (Parameter_Type (Form));
2761 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
2763 Find_Type (Subtype_Mark (Parameter_Type (Form)));
2767 -- the case of a null procedure with a formal that is an
2768 -- access_to_subprogram type, and that is used as an actual
2769 -- in an instantiation is left to the enthusiastic reader.
2777 if Is_Protected_Type (Current_Scope) then
2778 Error_Msg_N ("protected operation cannot be a null procedure", N);
2782 Designator := Analyze_Subprogram_Specification (Specification (N));
2783 Generate_Definition (Designator);
2785 if Debug_Flag_C then
2786 Write_Str ("==> subprogram spec ");
2787 Write_Name (Chars (Designator));
2788 Write_Str (" from ");
2789 Write_Location (Sloc (N));
2794 if Nkind (Specification (N)) = N_Procedure_Specification
2795 and then Null_Present (Specification (N))
2797 Set_Has_Completion (Designator);
2799 if Present (Null_Body) then
2800 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2801 Set_Body_To_Inline (N, Null_Body);
2802 Set_Is_Inlined (Designator);
2806 Validate_RCI_Subprogram_Declaration (N);
2807 New_Overloaded_Entity (Designator);
2808 Check_Delayed_Subprogram (Designator);
2810 -- If the type of the first formal of the current subprogram is a
2811 -- nongeneric tagged private type, mark the subprogram as being a
2812 -- private primitive. Ditto if this is a function with controlling
2813 -- result, and the return type is currently private. In both cases,
2814 -- the type of the controlling argument or result must be in the
2815 -- current scope for the operation to be primitive.
2817 if Has_Controlling_Result (Designator)
2818 and then Is_Private_Type (Etype (Designator))
2819 and then Scope (Etype (Designator)) = Current_Scope
2820 and then not Is_Generic_Actual_Type (Etype (Designator))
2822 Set_Is_Private_Primitive (Designator);
2824 elsif Present (First_Formal (Designator)) then
2826 Formal_Typ : constant Entity_Id :=
2827 Etype (First_Formal (Designator));
2829 Set_Is_Private_Primitive (Designator,
2830 Is_Tagged_Type (Formal_Typ)
2831 and then Scope (Formal_Typ) = Current_Scope
2832 and then Is_Private_Type (Formal_Typ)
2833 and then not Is_Generic_Actual_Type (Formal_Typ));
2837 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2840 if Ada_Version >= Ada_2005
2841 and then Comes_From_Source (N)
2842 and then Is_Dispatching_Operation (Designator)
2849 if Has_Controlling_Result (Designator) then
2850 Etyp := Etype (Designator);
2853 E := First_Entity (Designator);
2855 and then Is_Formal (E)
2856 and then not Is_Controlling_Formal (E)
2864 if Is_Access_Type (Etyp) then
2865 Etyp := Directly_Designated_Type (Etyp);
2868 if Is_Interface (Etyp)
2869 and then not Is_Abstract_Subprogram (Designator)
2870 and then not (Ekind (Designator) = E_Procedure
2871 and then Null_Present (Specification (N)))
2873 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2875 ("(Ada 2005) interface subprogram % must be abstract or null",
2881 -- What is the following code for, it used to be
2883 -- ??? Set_Suppress_Elaboration_Checks
2884 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2886 -- The following seems equivalent, but a bit dubious
2888 if Elaboration_Checks_Suppressed (Designator) then
2889 Set_Kill_Elaboration_Checks (Designator);
2892 if Scop /= Standard_Standard
2893 and then not Is_Child_Unit (Designator)
2895 Set_Categorization_From_Scope (Designator, Scop);
2897 -- For a compilation unit, check for library-unit pragmas
2899 Push_Scope (Designator);
2900 Set_Categorization_From_Pragmas (N);
2901 Validate_Categorization_Dependency (N, Designator);
2905 -- For a compilation unit, set body required. This flag will only be
2906 -- reset if a valid Import or Interface pragma is processed later on.
2908 if Nkind (Parent (N)) = N_Compilation_Unit then
2909 Set_Body_Required (Parent (N), True);
2911 if Ada_Version >= Ada_2005
2912 and then Nkind (Specification (N)) = N_Procedure_Specification
2913 and then Null_Present (Specification (N))
2916 ("null procedure cannot be declared at library level", N);
2920 Generate_Reference_To_Formals (Designator);
2921 Check_Eliminated (Designator);
2923 if Debug_Flag_C then
2925 Write_Str ("<== subprogram spec ");
2926 Write_Name (Chars (Designator));
2927 Write_Str (" from ");
2928 Write_Location (Sloc (N));
2932 if Is_Protected_Type (Current_Scope) then
2934 -- Indicate that this is a protected operation, because it may be
2935 -- used in subsequent declarations within the protected type.
2937 Set_Convention (Designator, Convention_Protected);
2940 List_Inherited_Pre_Post_Aspects (Designator);
2941 Analyze_Aspect_Specifications (N, Designator, Aspect_Specifications (N));
2942 end Analyze_Subprogram_Declaration;
2944 --------------------------------------
2945 -- Analyze_Subprogram_Specification --
2946 --------------------------------------
2948 -- Reminder: N here really is a subprogram specification (not a subprogram
2949 -- declaration). This procedure is called to analyze the specification in
2950 -- both subprogram bodies and subprogram declarations (specs).
2952 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2953 Designator : constant Entity_Id := Defining_Entity (N);
2954 Formals : constant List_Id := Parameter_Specifications (N);
2956 -- Start of processing for Analyze_Subprogram_Specification
2959 Generate_Definition (Designator);
2961 if Nkind (N) = N_Function_Specification then
2962 Set_Ekind (Designator, E_Function);
2963 Set_Mechanism (Designator, Default_Mechanism);
2965 Set_Ekind (Designator, E_Procedure);
2966 Set_Etype (Designator, Standard_Void_Type);
2969 -- Introduce new scope for analysis of the formals and the return type
2971 Set_Scope (Designator, Current_Scope);
2973 if Present (Formals) then
2974 Push_Scope (Designator);
2975 Process_Formals (Formals, N);
2977 -- Ada 2005 (AI-345): If this is an overriding operation of an
2978 -- inherited interface operation, and the controlling type is
2979 -- a synchronized type, replace the type with its corresponding
2980 -- record, to match the proper signature of an overriding operation.
2981 -- Same processing for an access parameter whose designated type is
2982 -- derived from a synchronized interface.
2984 if Ada_Version >= Ada_2005 then
2987 Formal_Typ : Entity_Id;
2988 Rec_Typ : Entity_Id;
2989 Desig_Typ : Entity_Id;
2992 Formal := First_Formal (Designator);
2993 while Present (Formal) loop
2994 Formal_Typ := Etype (Formal);
2996 if Is_Concurrent_Type (Formal_Typ)
2997 and then Present (Corresponding_Record_Type (Formal_Typ))
2999 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3001 if Present (Interfaces (Rec_Typ)) then
3002 Set_Etype (Formal, Rec_Typ);
3005 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3006 Desig_Typ := Designated_Type (Formal_Typ);
3008 if Is_Concurrent_Type (Desig_Typ)
3009 and then Present (Corresponding_Record_Type (Desig_Typ))
3011 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3013 if Present (Interfaces (Rec_Typ)) then
3014 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3019 Next_Formal (Formal);
3026 -- The subprogram scope is pushed and popped around the processing of
3027 -- the return type for consistency with call above to Process_Formals
3028 -- (which itself can call Analyze_Return_Type), and to ensure that any
3029 -- itype created for the return type will be associated with the proper
3032 elsif Nkind (N) = N_Function_Specification then
3033 Push_Scope (Designator);
3034 Analyze_Return_Type (N);
3040 if Nkind (N) = N_Function_Specification then
3042 -- Deal with operator symbol case
3044 if Nkind (Designator) = N_Defining_Operator_Symbol then
3045 Valid_Operator_Definition (Designator);
3048 May_Need_Actuals (Designator);
3050 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3051 -- the subprogram is abstract also. This does not apply to renaming
3052 -- declarations, where abstractness is inherited.
3054 -- In case of primitives associated with abstract interface types
3055 -- the check is applied later (see Analyze_Subprogram_Declaration).
3057 if not Nkind_In (Parent (N), N_Subprogram_Renaming_Declaration,
3058 N_Abstract_Subprogram_Declaration,
3059 N_Formal_Abstract_Subprogram_Declaration)
3061 if Is_Abstract_Type (Etype (Designator))
3062 and then not Is_Interface (Etype (Designator))
3065 ("function that returns abstract type must be abstract", N);
3067 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3068 -- access result whose designated type is abstract.
3070 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3072 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3073 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3074 and then Ada_Version >= Ada_2012
3076 Error_Msg_N ("function whose access result designates "
3077 & "abstract type must be abstract", N);
3083 end Analyze_Subprogram_Specification;
3085 --------------------------
3086 -- Build_Body_To_Inline --
3087 --------------------------
3089 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3090 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3091 Original_Body : Node_Id;
3092 Body_To_Analyze : Node_Id;
3093 Max_Size : constant := 10;
3094 Stat_Count : Integer := 0;
3096 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3097 -- Check for declarations that make inlining not worthwhile
3099 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3100 -- Check for statements that make inlining not worthwhile: any tasking
3101 -- statement, nested at any level. Keep track of total number of
3102 -- elementary statements, as a measure of acceptable size.
3104 function Has_Pending_Instantiation return Boolean;
3105 -- If some enclosing body contains instantiations that appear before the
3106 -- corresponding generic body, the enclosing body has a freeze node so
3107 -- that it can be elaborated after the generic itself. This might
3108 -- conflict with subsequent inlinings, so that it is unsafe to try to
3109 -- inline in such a case.
3111 function Has_Single_Return return Boolean;
3112 -- In general we cannot inline functions that return unconstrained type.
3113 -- However, we can handle such functions if all return statements return
3114 -- a local variable that is the only declaration in the body of the
3115 -- function. In that case the call can be replaced by that local
3116 -- variable as is done for other inlined calls.
3118 procedure Remove_Pragmas;
3119 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3120 -- parameter has no meaning when the body is inlined and the formals
3121 -- are rewritten. Remove it from body to inline. The analysis of the
3122 -- non-inlined body will handle the pragma properly.
3124 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3125 -- If the body of the subprogram includes a call that returns an
3126 -- unconstrained type, the secondary stack is involved, and it
3127 -- is not worth inlining.
3129 ------------------------------
3130 -- Has_Excluded_Declaration --
3131 ------------------------------
3133 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3136 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3137 -- Nested subprograms make a given body ineligible for inlining, but
3138 -- we make an exception for instantiations of unchecked conversion.
3139 -- The body has not been analyzed yet, so check the name, and verify
3140 -- that the visible entity with that name is the predefined unit.
3142 -----------------------------
3143 -- Is_Unchecked_Conversion --
3144 -----------------------------
3146 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3147 Id : constant Node_Id := Name (D);
3151 if Nkind (Id) = N_Identifier
3152 and then Chars (Id) = Name_Unchecked_Conversion
3154 Conv := Current_Entity (Id);
3156 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3157 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3159 Conv := Current_Entity (Selector_Name (Id));
3164 return Present (Conv)
3165 and then Is_Predefined_File_Name
3166 (Unit_File_Name (Get_Source_Unit (Conv)))
3167 and then Is_Intrinsic_Subprogram (Conv);
3168 end Is_Unchecked_Conversion;
3170 -- Start of processing for Has_Excluded_Declaration
3174 while Present (D) loop
3175 if (Nkind (D) = N_Function_Instantiation
3176 and then not Is_Unchecked_Conversion (D))
3177 or else Nkind_In (D, N_Protected_Type_Declaration,
3178 N_Package_Declaration,
3179 N_Package_Instantiation,
3181 N_Procedure_Instantiation,
3182 N_Task_Type_Declaration)
3185 ("cannot inline & (non-allowed declaration)?", D, Subp);
3193 end Has_Excluded_Declaration;
3195 ----------------------------
3196 -- Has_Excluded_Statement --
3197 ----------------------------
3199 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3205 while Present (S) loop
3206 Stat_Count := Stat_Count + 1;
3208 if Nkind_In (S, N_Abort_Statement,
3209 N_Asynchronous_Select,
3210 N_Conditional_Entry_Call,
3211 N_Delay_Relative_Statement,
3212 N_Delay_Until_Statement,
3217 ("cannot inline & (non-allowed statement)?", S, Subp);
3220 elsif Nkind (S) = N_Block_Statement then
3221 if Present (Declarations (S))
3222 and then Has_Excluded_Declaration (Declarations (S))
3226 elsif Present (Handled_Statement_Sequence (S))
3229 (Exception_Handlers (Handled_Statement_Sequence (S)))
3231 Has_Excluded_Statement
3232 (Statements (Handled_Statement_Sequence (S))))
3237 elsif Nkind (S) = N_Case_Statement then
3238 E := First (Alternatives (S));
3239 while Present (E) loop
3240 if Has_Excluded_Statement (Statements (E)) then
3247 elsif Nkind (S) = N_If_Statement then
3248 if Has_Excluded_Statement (Then_Statements (S)) then
3252 if Present (Elsif_Parts (S)) then
3253 E := First (Elsif_Parts (S));
3254 while Present (E) loop
3255 if Has_Excluded_Statement (Then_Statements (E)) then
3262 if Present (Else_Statements (S))
3263 and then Has_Excluded_Statement (Else_Statements (S))
3268 elsif Nkind (S) = N_Loop_Statement
3269 and then Has_Excluded_Statement (Statements (S))
3273 elsif Nkind (S) = N_Extended_Return_Statement then
3274 if Has_Excluded_Statement
3275 (Statements (Handled_Statement_Sequence (S)))
3277 (Exception_Handlers (Handled_Statement_Sequence (S)))
3287 end Has_Excluded_Statement;
3289 -------------------------------
3290 -- Has_Pending_Instantiation --
3291 -------------------------------
3293 function Has_Pending_Instantiation return Boolean is
3298 while Present (S) loop
3299 if Is_Compilation_Unit (S)
3300 or else Is_Child_Unit (S)
3304 elsif Ekind (S) = E_Package
3305 and then Has_Forward_Instantiation (S)
3314 end Has_Pending_Instantiation;
3316 ------------------------
3317 -- Has_Single_Return --
3318 ------------------------
3320 function Has_Single_Return return Boolean is
3321 Return_Statement : Node_Id := Empty;
3323 function Check_Return (N : Node_Id) return Traverse_Result;
3329 function Check_Return (N : Node_Id) return Traverse_Result is
3331 if Nkind (N) = N_Simple_Return_Statement then
3332 if Present (Expression (N))
3333 and then Is_Entity_Name (Expression (N))
3335 if No (Return_Statement) then
3336 Return_Statement := N;
3339 elsif Chars (Expression (N)) =
3340 Chars (Expression (Return_Statement))
3348 -- A return statement within an extended return is a noop
3351 elsif No (Expression (N))
3352 and then Nkind (Parent (Parent (N))) =
3353 N_Extended_Return_Statement
3358 -- Expression has wrong form
3363 -- We can only inline a build-in-place function if
3364 -- it has a single extended return.
3366 elsif Nkind (N) = N_Extended_Return_Statement then
3367 if No (Return_Statement) then
3368 Return_Statement := N;
3380 function Check_All_Returns is new Traverse_Func (Check_Return);
3382 -- Start of processing for Has_Single_Return
3385 if Check_All_Returns (N) /= OK then
3388 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3392 return Present (Declarations (N))
3393 and then Present (First (Declarations (N)))
3394 and then Chars (Expression (Return_Statement)) =
3395 Chars (Defining_Identifier (First (Declarations (N))));
3397 end Has_Single_Return;
3399 --------------------
3400 -- Remove_Pragmas --
3401 --------------------
3403 procedure Remove_Pragmas is
3408 Decl := First (Declarations (Body_To_Analyze));
3409 while Present (Decl) loop
3412 if Nkind (Decl) = N_Pragma
3413 and then (Pragma_Name (Decl) = Name_Unreferenced
3415 Pragma_Name (Decl) = Name_Unmodified)
3424 --------------------------
3425 -- Uses_Secondary_Stack --
3426 --------------------------
3428 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3429 function Check_Call (N : Node_Id) return Traverse_Result;
3430 -- Look for function calls that return an unconstrained type
3436 function Check_Call (N : Node_Id) return Traverse_Result is
3438 if Nkind (N) = N_Function_Call
3439 and then Is_Entity_Name (Name (N))
3440 and then Is_Composite_Type (Etype (Entity (Name (N))))
3441 and then not Is_Constrained (Etype (Entity (Name (N))))
3444 ("cannot inline & (call returns unconstrained type)?",
3452 function Check_Calls is new Traverse_Func (Check_Call);
3455 return Check_Calls (Bod) = Abandon;
3456 end Uses_Secondary_Stack;
3458 -- Start of processing for Build_Body_To_Inline
3461 -- Return immediately if done already
3463 if Nkind (Decl) = N_Subprogram_Declaration
3464 and then Present (Body_To_Inline (Decl))
3468 -- Functions that return unconstrained composite types require
3469 -- secondary stack handling, and cannot currently be inlined, unless
3470 -- all return statements return a local variable that is the first
3471 -- local declaration in the body.
3473 elsif Ekind (Subp) = E_Function
3474 and then not Is_Scalar_Type (Etype (Subp))
3475 and then not Is_Access_Type (Etype (Subp))
3476 and then not Is_Constrained (Etype (Subp))
3478 if not Has_Single_Return then
3480 ("cannot inline & (unconstrained return type)?", N, Subp);
3484 -- Ditto for functions that return controlled types, where controlled
3485 -- actions interfere in complex ways with inlining.
3487 elsif Ekind (Subp) = E_Function
3488 and then Needs_Finalization (Etype (Subp))
3491 ("cannot inline & (controlled return type)?", N, Subp);
3495 if Present (Declarations (N))
3496 and then Has_Excluded_Declaration (Declarations (N))
3501 if Present (Handled_Statement_Sequence (N)) then
3502 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3504 ("cannot inline& (exception handler)?",
3505 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3509 Has_Excluded_Statement
3510 (Statements (Handled_Statement_Sequence (N)))
3516 -- We do not inline a subprogram that is too large, unless it is
3517 -- marked Inline_Always. This pragma does not suppress the other
3518 -- checks on inlining (forbidden declarations, handlers, etc).
3520 if Stat_Count > Max_Size
3521 and then not Has_Pragma_Inline_Always (Subp)
3523 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3527 if Has_Pending_Instantiation then
3529 ("cannot inline& (forward instance within enclosing body)?",
3534 -- Within an instance, the body to inline must be treated as a nested
3535 -- generic, so that the proper global references are preserved.
3537 -- Note that we do not do this at the library level, because it is not
3538 -- needed, and furthermore this causes trouble if front end inlining
3539 -- is activated (-gnatN).
3541 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3542 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3543 Original_Body := Copy_Generic_Node (N, Empty, True);
3545 Original_Body := Copy_Separate_Tree (N);
3548 -- We need to capture references to the formals in order to substitute
3549 -- the actuals at the point of inlining, i.e. instantiation. To treat
3550 -- the formals as globals to the body to inline, we nest it within
3551 -- a dummy parameterless subprogram, declared within the real one.
3552 -- To avoid generating an internal name (which is never public, and
3553 -- which affects serial numbers of other generated names), we use
3554 -- an internal symbol that cannot conflict with user declarations.
3556 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3557 Set_Defining_Unit_Name
3558 (Specification (Original_Body),
3559 Make_Defining_Identifier (Sloc (N), Name_uParent));
3560 Set_Corresponding_Spec (Original_Body, Empty);
3562 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3564 -- Set return type of function, which is also global and does not need
3567 if Ekind (Subp) = E_Function then
3568 Set_Result_Definition (Specification (Body_To_Analyze),
3569 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3572 if No (Declarations (N)) then
3573 Set_Declarations (N, New_List (Body_To_Analyze));
3575 Append (Body_To_Analyze, Declarations (N));
3578 Expander_Mode_Save_And_Set (False);
3581 Analyze (Body_To_Analyze);
3582 Push_Scope (Defining_Entity (Body_To_Analyze));
3583 Save_Global_References (Original_Body);
3585 Remove (Body_To_Analyze);
3587 Expander_Mode_Restore;
3589 -- Restore environment if previously saved
3591 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3595 -- If secondary stk used there is no point in inlining. We have
3596 -- already issued the warning in this case, so nothing to do.
3598 if Uses_Secondary_Stack (Body_To_Analyze) then
3602 Set_Body_To_Inline (Decl, Original_Body);
3603 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3604 Set_Is_Inlined (Subp);
3605 end Build_Body_To_Inline;
3611 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3613 -- Do not emit warning if this is a predefined unit which is not the
3614 -- main unit. With validity checks enabled, some predefined subprograms
3615 -- may contain nested subprograms and become ineligible for inlining.
3617 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3618 and then not In_Extended_Main_Source_Unit (Subp)
3622 elsif Has_Pragma_Inline_Always (Subp) then
3624 -- Remove last character (question mark) to make this into an error,
3625 -- because the Inline_Always pragma cannot be obeyed.
3627 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3629 elsif Ineffective_Inline_Warnings then
3630 Error_Msg_NE (Msg, N, Subp);
3634 -----------------------
3635 -- Check_Conformance --
3636 -----------------------
3638 procedure Check_Conformance
3639 (New_Id : Entity_Id;
3641 Ctype : Conformance_Type;
3643 Conforms : out Boolean;
3644 Err_Loc : Node_Id := Empty;
3645 Get_Inst : Boolean := False;
3646 Skip_Controlling_Formals : Boolean := False)
3648 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3649 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3650 -- If Errmsg is True, then processing continues to post an error message
3651 -- for conformance error on given node. Two messages are output. The
3652 -- first message points to the previous declaration with a general "no
3653 -- conformance" message. The second is the detailed reason, supplied as
3654 -- Msg. The parameter N provide information for a possible & insertion
3655 -- in the message, and also provides the location for posting the
3656 -- message in the absence of a specified Err_Loc location.
3658 -----------------------
3659 -- Conformance_Error --
3660 -----------------------
3662 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3669 if No (Err_Loc) then
3675 Error_Msg_Sloc := Sloc (Old_Id);
3678 when Type_Conformant =>
3679 Error_Msg_N -- CODEFIX
3680 ("not type conformant with declaration#!", Enode);
3682 when Mode_Conformant =>
3683 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3685 ("not mode conformant with operation inherited#!",
3689 ("not mode conformant with declaration#!", Enode);
3692 when Subtype_Conformant =>
3693 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3695 ("not subtype conformant with operation inherited#!",
3699 ("not subtype conformant with declaration#!", Enode);
3702 when Fully_Conformant =>
3703 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3704 Error_Msg_N -- CODEFIX
3705 ("not fully conformant with operation inherited#!",
3708 Error_Msg_N -- CODEFIX
3709 ("not fully conformant with declaration#!", Enode);
3713 Error_Msg_NE (Msg, Enode, N);
3715 end Conformance_Error;
3719 Old_Type : constant Entity_Id := Etype (Old_Id);
3720 New_Type : constant Entity_Id := Etype (New_Id);
3721 Old_Formal : Entity_Id;
3722 New_Formal : Entity_Id;
3723 Access_Types_Match : Boolean;
3724 Old_Formal_Base : Entity_Id;
3725 New_Formal_Base : Entity_Id;
3727 -- Start of processing for Check_Conformance
3732 -- We need a special case for operators, since they don't appear
3735 if Ctype = Type_Conformant then
3736 if Ekind (New_Id) = E_Operator
3737 and then Operator_Matches_Spec (New_Id, Old_Id)
3743 -- If both are functions/operators, check return types conform
3745 if Old_Type /= Standard_Void_Type
3746 and then New_Type /= Standard_Void_Type
3749 -- If we are checking interface conformance we omit controlling
3750 -- arguments and result, because we are only checking the conformance
3751 -- of the remaining parameters.
3753 if Has_Controlling_Result (Old_Id)
3754 and then Has_Controlling_Result (New_Id)
3755 and then Skip_Controlling_Formals
3759 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3760 Conformance_Error ("\return type does not match!", New_Id);
3764 -- Ada 2005 (AI-231): In case of anonymous access types check the
3765 -- null-exclusion and access-to-constant attributes match.
3767 if Ada_Version >= Ada_2005
3768 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3770 (Can_Never_Be_Null (Old_Type)
3771 /= Can_Never_Be_Null (New_Type)
3772 or else Is_Access_Constant (Etype (Old_Type))
3773 /= Is_Access_Constant (Etype (New_Type)))
3775 Conformance_Error ("\return type does not match!", New_Id);
3779 -- If either is a function/operator and the other isn't, error
3781 elsif Old_Type /= Standard_Void_Type
3782 or else New_Type /= Standard_Void_Type
3784 Conformance_Error ("\functions can only match functions!", New_Id);
3788 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3789 -- If this is a renaming as body, refine error message to indicate that
3790 -- the conflict is with the original declaration. If the entity is not
3791 -- frozen, the conventions don't have to match, the one of the renamed
3792 -- entity is inherited.
3794 if Ctype >= Subtype_Conformant then
3795 if Convention (Old_Id) /= Convention (New_Id) then
3797 if not Is_Frozen (New_Id) then
3800 elsif Present (Err_Loc)
3801 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3802 and then Present (Corresponding_Spec (Err_Loc))
3804 Error_Msg_Name_1 := Chars (New_Id);
3806 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3807 Conformance_Error ("\prior declaration for% has convention %!");
3810 Conformance_Error ("\calling conventions do not match!");
3815 elsif Is_Formal_Subprogram (Old_Id)
3816 or else Is_Formal_Subprogram (New_Id)
3818 Conformance_Error ("\formal subprograms not allowed!");
3823 -- Deal with parameters
3825 -- Note: we use the entity information, rather than going directly
3826 -- to the specification in the tree. This is not only simpler, but
3827 -- absolutely necessary for some cases of conformance tests between
3828 -- operators, where the declaration tree simply does not exist!
3830 Old_Formal := First_Formal (Old_Id);
3831 New_Formal := First_Formal (New_Id);
3832 while Present (Old_Formal) and then Present (New_Formal) loop
3833 if Is_Controlling_Formal (Old_Formal)
3834 and then Is_Controlling_Formal (New_Formal)
3835 and then Skip_Controlling_Formals
3837 -- The controlling formals will have different types when
3838 -- comparing an interface operation with its match, but both
3839 -- or neither must be access parameters.
3841 if Is_Access_Type (Etype (Old_Formal))
3843 Is_Access_Type (Etype (New_Formal))
3845 goto Skip_Controlling_Formal;
3848 ("\access parameter does not match!", New_Formal);
3852 if Ctype = Fully_Conformant then
3854 -- Names must match. Error message is more accurate if we do
3855 -- this before checking that the types of the formals match.
3857 if Chars (Old_Formal) /= Chars (New_Formal) then
3858 Conformance_Error ("\name & does not match!", New_Formal);
3860 -- Set error posted flag on new formal as well to stop
3861 -- junk cascaded messages in some cases.
3863 Set_Error_Posted (New_Formal);
3867 -- Null exclusion must match
3869 if Null_Exclusion_Present (Parent (Old_Formal))
3871 Null_Exclusion_Present (Parent (New_Formal))
3873 -- Only give error if both come from source. This should be
3874 -- investigated some time, since it should not be needed ???
3876 if Comes_From_Source (Old_Formal)
3878 Comes_From_Source (New_Formal)
3881 ("\null exclusion for & does not match", New_Formal);
3883 -- Mark error posted on the new formal to avoid duplicated
3884 -- complaint about types not matching.
3886 Set_Error_Posted (New_Formal);
3891 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3892 -- case occurs whenever a subprogram is being renamed and one of its
3893 -- parameters imposes a null exclusion. For example:
3895 -- type T is null record;
3896 -- type Acc_T is access T;
3897 -- subtype Acc_T_Sub is Acc_T;
3899 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3900 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3903 Old_Formal_Base := Etype (Old_Formal);
3904 New_Formal_Base := Etype (New_Formal);
3907 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3908 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3911 Access_Types_Match := Ada_Version >= Ada_2005
3913 -- Ensure that this rule is only applied when New_Id is a
3914 -- renaming of Old_Id.
3916 and then Nkind (Parent (Parent (New_Id))) =
3917 N_Subprogram_Renaming_Declaration
3918 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3919 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3920 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3922 -- Now handle the allowed access-type case
3924 and then Is_Access_Type (Old_Formal_Base)
3925 and then Is_Access_Type (New_Formal_Base)
3927 -- The type kinds must match. The only exception occurs with
3928 -- multiple generics of the form:
3931 -- type F is private; type A is private;
3932 -- type F_Ptr is access F; type A_Ptr is access A;
3933 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3934 -- package F_Pack is ... package A_Pack is
3935 -- package F_Inst is
3936 -- new F_Pack (A, A_Ptr, A_P);
3938 -- When checking for conformance between the parameters of A_P
3939 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3940 -- because the compiler has transformed A_Ptr into a subtype of
3941 -- F_Ptr. We catch this case in the code below.
3943 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3945 (Is_Generic_Type (Old_Formal_Base)
3946 and then Is_Generic_Type (New_Formal_Base)
3947 and then Is_Internal (New_Formal_Base)
3948 and then Etype (Etype (New_Formal_Base)) =
3950 and then Directly_Designated_Type (Old_Formal_Base) =
3951 Directly_Designated_Type (New_Formal_Base)
3952 and then ((Is_Itype (Old_Formal_Base)
3953 and then Can_Never_Be_Null (Old_Formal_Base))
3955 (Is_Itype (New_Formal_Base)
3956 and then Can_Never_Be_Null (New_Formal_Base)));
3958 -- Types must always match. In the visible part of an instance,
3959 -- usual overloading rules for dispatching operations apply, and
3960 -- we check base types (not the actual subtypes).
3962 if In_Instance_Visible_Part
3963 and then Is_Dispatching_Operation (New_Id)
3965 if not Conforming_Types
3966 (T1 => Base_Type (Etype (Old_Formal)),
3967 T2 => Base_Type (Etype (New_Formal)),
3969 Get_Inst => Get_Inst)
3970 and then not Access_Types_Match
3972 Conformance_Error ("\type of & does not match!", New_Formal);
3976 elsif not Conforming_Types
3977 (T1 => Old_Formal_Base,
3978 T2 => New_Formal_Base,
3980 Get_Inst => Get_Inst)
3981 and then not Access_Types_Match
3983 -- Don't give error message if old type is Any_Type. This test
3984 -- avoids some cascaded errors, e.g. in case of a bad spec.
3986 if Errmsg and then Old_Formal_Base = Any_Type then
3989 Conformance_Error ("\type of & does not match!", New_Formal);
3995 -- For mode conformance, mode must match
3997 if Ctype >= Mode_Conformant then
3998 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3999 Conformance_Error ("\mode of & does not match!", New_Formal);
4002 -- Part of mode conformance for access types is having the same
4003 -- constant modifier.
4005 elsif Access_Types_Match
4006 and then Is_Access_Constant (Old_Formal_Base) /=
4007 Is_Access_Constant (New_Formal_Base)
4010 ("\constant modifier does not match!", New_Formal);
4015 if Ctype >= Subtype_Conformant then
4017 -- Ada 2005 (AI-231): In case of anonymous access types check
4018 -- the null-exclusion and access-to-constant attributes must
4021 if Ada_Version >= Ada_2005
4022 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4023 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4025 (Can_Never_Be_Null (Old_Formal) /=
4026 Can_Never_Be_Null (New_Formal)
4028 Is_Access_Constant (Etype (Old_Formal)) /=
4029 Is_Access_Constant (Etype (New_Formal)))
4031 -- Do not complain if error already posted on New_Formal. This
4032 -- avoids some redundant error messages.
4034 and then not Error_Posted (New_Formal)
4036 -- It is allowed to omit the null-exclusion in case of stream
4037 -- attribute subprograms. We recognize stream subprograms
4038 -- through their TSS-generated suffix.
4041 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4043 if TSS_Name /= TSS_Stream_Read
4044 and then TSS_Name /= TSS_Stream_Write
4045 and then TSS_Name /= TSS_Stream_Input
4046 and then TSS_Name /= TSS_Stream_Output
4049 ("\type of & does not match!", New_Formal);
4056 -- Full conformance checks
4058 if Ctype = Fully_Conformant then
4060 -- We have checked already that names match
4062 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4064 -- Check default expressions for in parameters
4067 NewD : constant Boolean :=
4068 Present (Default_Value (New_Formal));
4069 OldD : constant Boolean :=
4070 Present (Default_Value (Old_Formal));
4072 if NewD or OldD then
4074 -- The old default value has been analyzed because the
4075 -- current full declaration will have frozen everything
4076 -- before. The new default value has not been analyzed,
4077 -- so analyze it now before we check for conformance.
4080 Push_Scope (New_Id);
4081 Preanalyze_Spec_Expression
4082 (Default_Value (New_Formal), Etype (New_Formal));
4086 if not (NewD and OldD)
4087 or else not Fully_Conformant_Expressions
4088 (Default_Value (Old_Formal),
4089 Default_Value (New_Formal))
4092 ("\default expression for & does not match!",
4101 -- A couple of special checks for Ada 83 mode. These checks are
4102 -- skipped if either entity is an operator in package Standard,
4103 -- or if either old or new instance is not from the source program.
4105 if Ada_Version = Ada_83
4106 and then Sloc (Old_Id) > Standard_Location
4107 and then Sloc (New_Id) > Standard_Location
4108 and then Comes_From_Source (Old_Id)
4109 and then Comes_From_Source (New_Id)
4112 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4113 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4116 -- Explicit IN must be present or absent in both cases. This
4117 -- test is required only in the full conformance case.
4119 if In_Present (Old_Param) /= In_Present (New_Param)
4120 and then Ctype = Fully_Conformant
4123 ("\(Ada 83) IN must appear in both declarations",
4128 -- Grouping (use of comma in param lists) must be the same
4129 -- This is where we catch a misconformance like:
4132 -- A : Integer; B : Integer
4134 -- which are represented identically in the tree except
4135 -- for the setting of the flags More_Ids and Prev_Ids.
4137 if More_Ids (Old_Param) /= More_Ids (New_Param)
4138 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
4141 ("\grouping of & does not match!", New_Formal);
4147 -- This label is required when skipping controlling formals
4149 <<Skip_Controlling_Formal>>
4151 Next_Formal (Old_Formal);
4152 Next_Formal (New_Formal);
4155 if Present (Old_Formal) then
4156 Conformance_Error ("\too few parameters!");
4159 elsif Present (New_Formal) then
4160 Conformance_Error ("\too many parameters!", New_Formal);
4163 end Check_Conformance;
4165 -----------------------
4166 -- Check_Conventions --
4167 -----------------------
4169 procedure Check_Conventions (Typ : Entity_Id) is
4170 Ifaces_List : Elist_Id;
4172 procedure Check_Convention (Op : Entity_Id);
4173 -- Verify that the convention of inherited dispatching operation Op is
4174 -- consistent among all subprograms it overrides. In order to minimize
4175 -- the search, Search_From is utilized to designate a specific point in
4176 -- the list rather than iterating over the whole list once more.
4178 ----------------------
4179 -- Check_Convention --
4180 ----------------------
4182 procedure Check_Convention (Op : Entity_Id) is
4183 Iface_Elmt : Elmt_Id;
4184 Iface_Prim_Elmt : Elmt_Id;
4185 Iface_Prim : Entity_Id;
4188 Iface_Elmt := First_Elmt (Ifaces_List);
4189 while Present (Iface_Elmt) loop
4191 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4192 while Present (Iface_Prim_Elmt) loop
4193 Iface_Prim := Node (Iface_Prim_Elmt);
4195 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4196 and then Convention (Iface_Prim) /= Convention (Op)
4199 ("inconsistent conventions in primitive operations", Typ);
4201 Error_Msg_Name_1 := Chars (Op);
4202 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4203 Error_Msg_Sloc := Sloc (Op);
4205 if Comes_From_Source (Op) or else No (Alias (Op)) then
4206 if not Present (Overridden_Operation (Op)) then
4207 Error_Msg_N ("\\primitive % defined #", Typ);
4210 ("\\overriding operation % with " &
4211 "convention % defined #", Typ);
4214 else pragma Assert (Present (Alias (Op)));
4215 Error_Msg_Sloc := Sloc (Alias (Op));
4217 ("\\inherited operation % with " &
4218 "convention % defined #", Typ);
4221 Error_Msg_Name_1 := Chars (Op);
4223 Get_Convention_Name (Convention (Iface_Prim));
4224 Error_Msg_Sloc := Sloc (Iface_Prim);
4226 ("\\overridden operation % with " &
4227 "convention % defined #", Typ);
4229 -- Avoid cascading errors
4234 Next_Elmt (Iface_Prim_Elmt);
4237 Next_Elmt (Iface_Elmt);
4239 end Check_Convention;
4243 Prim_Op : Entity_Id;
4244 Prim_Op_Elmt : Elmt_Id;
4246 -- Start of processing for Check_Conventions
4249 if not Has_Interfaces (Typ) then
4253 Collect_Interfaces (Typ, Ifaces_List);
4255 -- The algorithm checks every overriding dispatching operation against
4256 -- all the corresponding overridden dispatching operations, detecting
4257 -- differences in conventions.
4259 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4260 while Present (Prim_Op_Elmt) loop
4261 Prim_Op := Node (Prim_Op_Elmt);
4263 -- A small optimization: skip the predefined dispatching operations
4264 -- since they always have the same convention.
4266 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4267 Check_Convention (Prim_Op);
4270 Next_Elmt (Prim_Op_Elmt);
4272 end Check_Conventions;
4274 ------------------------------
4275 -- Check_Delayed_Subprogram --
4276 ------------------------------
4278 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4281 procedure Possible_Freeze (T : Entity_Id);
4282 -- T is the type of either a formal parameter or of the return type.
4283 -- If T is not yet frozen and needs a delayed freeze, then the
4284 -- subprogram itself must be delayed. If T is the limited view of an
4285 -- incomplete type the subprogram must be frozen as well, because
4286 -- T may depend on local types that have not been frozen yet.
4288 ---------------------
4289 -- Possible_Freeze --
4290 ---------------------
4292 procedure Possible_Freeze (T : Entity_Id) is
4294 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4295 Set_Has_Delayed_Freeze (Designator);
4297 elsif Is_Access_Type (T)
4298 and then Has_Delayed_Freeze (Designated_Type (T))
4299 and then not Is_Frozen (Designated_Type (T))
4301 Set_Has_Delayed_Freeze (Designator);
4303 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4304 Set_Has_Delayed_Freeze (Designator);
4307 end Possible_Freeze;
4309 -- Start of processing for Check_Delayed_Subprogram
4312 -- All subprograms, including abstract subprograms, may need a freeze
4313 -- node if some formal type or the return type needs one.
4315 Possible_Freeze (Etype (Designator));
4316 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4318 -- Need delayed freeze if any of the formal types themselves need
4319 -- a delayed freeze and are not yet frozen.
4321 F := First_Formal (Designator);
4322 while Present (F) loop
4323 Possible_Freeze (Etype (F));
4324 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4328 -- Mark functions that return by reference. Note that it cannot be
4329 -- done for delayed_freeze subprograms because the underlying
4330 -- returned type may not be known yet (for private types)
4332 if not Has_Delayed_Freeze (Designator)
4333 and then Expander_Active
4336 Typ : constant Entity_Id := Etype (Designator);
4337 Utyp : constant Entity_Id := Underlying_Type (Typ);
4340 if Is_Immutably_Limited_Type (Typ) then
4341 Set_Returns_By_Ref (Designator);
4343 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4344 Set_Returns_By_Ref (Designator);
4348 end Check_Delayed_Subprogram;
4350 ------------------------------------
4351 -- Check_Discriminant_Conformance --
4352 ------------------------------------
4354 procedure Check_Discriminant_Conformance
4359 Old_Discr : Entity_Id := First_Discriminant (Prev);
4360 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4361 New_Discr_Id : Entity_Id;
4362 New_Discr_Type : Entity_Id;
4364 procedure Conformance_Error (Msg : String; N : Node_Id);
4365 -- Post error message for conformance error on given node. Two messages
4366 -- are output. The first points to the previous declaration with a
4367 -- general "no conformance" message. The second is the detailed reason,
4368 -- supplied as Msg. The parameter N provide information for a possible
4369 -- & insertion in the message.
4371 -----------------------
4372 -- Conformance_Error --
4373 -----------------------
4375 procedure Conformance_Error (Msg : String; N : Node_Id) is
4377 Error_Msg_Sloc := Sloc (Prev_Loc);
4378 Error_Msg_N -- CODEFIX
4379 ("not fully conformant with declaration#!", N);
4380 Error_Msg_NE (Msg, N, N);
4381 end Conformance_Error;
4383 -- Start of processing for Check_Discriminant_Conformance
4386 while Present (Old_Discr) and then Present (New_Discr) loop
4388 New_Discr_Id := Defining_Identifier (New_Discr);
4390 -- The subtype mark of the discriminant on the full type has not
4391 -- been analyzed so we do it here. For an access discriminant a new
4394 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4396 Access_Definition (N, Discriminant_Type (New_Discr));
4399 Analyze (Discriminant_Type (New_Discr));
4400 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4402 -- Ada 2005: if the discriminant definition carries a null
4403 -- exclusion, create an itype to check properly for consistency
4404 -- with partial declaration.
4406 if Is_Access_Type (New_Discr_Type)
4407 and then Null_Exclusion_Present (New_Discr)
4410 Create_Null_Excluding_Itype
4411 (T => New_Discr_Type,
4412 Related_Nod => New_Discr,
4413 Scope_Id => Current_Scope);
4417 if not Conforming_Types
4418 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4420 Conformance_Error ("type of & does not match!", New_Discr_Id);
4423 -- Treat the new discriminant as an occurrence of the old one,
4424 -- for navigation purposes, and fill in some semantic
4425 -- information, for completeness.
4427 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4428 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4429 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4434 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4435 Conformance_Error ("name & does not match!", New_Discr_Id);
4439 -- Default expressions must match
4442 NewD : constant Boolean :=
4443 Present (Expression (New_Discr));
4444 OldD : constant Boolean :=
4445 Present (Expression (Parent (Old_Discr)));
4448 if NewD or OldD then
4450 -- The old default value has been analyzed and expanded,
4451 -- because the current full declaration will have frozen
4452 -- everything before. The new default values have not been
4453 -- expanded, so expand now to check conformance.
4456 Preanalyze_Spec_Expression
4457 (Expression (New_Discr), New_Discr_Type);
4460 if not (NewD and OldD)
4461 or else not Fully_Conformant_Expressions
4462 (Expression (Parent (Old_Discr)),
4463 Expression (New_Discr))
4467 ("default expression for & does not match!",
4474 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4476 if Ada_Version = Ada_83 then
4478 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4481 -- Grouping (use of comma in param lists) must be the same
4482 -- This is where we catch a misconformance like:
4485 -- A : Integer; B : Integer
4487 -- which are represented identically in the tree except
4488 -- for the setting of the flags More_Ids and Prev_Ids.
4490 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4491 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4494 ("grouping of & does not match!", New_Discr_Id);
4500 Next_Discriminant (Old_Discr);
4504 if Present (Old_Discr) then
4505 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4508 elsif Present (New_Discr) then
4510 ("too many discriminants!", Defining_Identifier (New_Discr));
4513 end Check_Discriminant_Conformance;
4515 ----------------------------
4516 -- Check_Fully_Conformant --
4517 ----------------------------
4519 procedure Check_Fully_Conformant
4520 (New_Id : Entity_Id;
4522 Err_Loc : Node_Id := Empty)
4525 pragma Warnings (Off, Result);
4528 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4529 end Check_Fully_Conformant;
4531 ---------------------------
4532 -- Check_Mode_Conformant --
4533 ---------------------------
4535 procedure Check_Mode_Conformant
4536 (New_Id : Entity_Id;
4538 Err_Loc : Node_Id := Empty;
4539 Get_Inst : Boolean := False)
4542 pragma Warnings (Off, Result);
4545 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4546 end Check_Mode_Conformant;
4548 --------------------------------
4549 -- Check_Overriding_Indicator --
4550 --------------------------------
4552 procedure Check_Overriding_Indicator
4554 Overridden_Subp : Entity_Id;
4555 Is_Primitive : Boolean)
4561 -- No overriding indicator for literals
4563 if Ekind (Subp) = E_Enumeration_Literal then
4566 elsif Ekind (Subp) = E_Entry then
4567 Decl := Parent (Subp);
4569 -- No point in analyzing a malformed operator
4571 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4572 and then Error_Posted (Subp)
4577 Decl := Unit_Declaration_Node (Subp);
4580 if Nkind_In (Decl, N_Subprogram_Body,
4581 N_Subprogram_Body_Stub,
4582 N_Subprogram_Declaration,
4583 N_Abstract_Subprogram_Declaration,
4584 N_Subprogram_Renaming_Declaration)
4586 Spec := Specification (Decl);
4588 elsif Nkind (Decl) = N_Entry_Declaration then
4595 -- The overriding operation is type conformant with the overridden one,
4596 -- but the names of the formals are not required to match. If the names
4597 -- appear permuted in the overriding operation, this is a possible
4598 -- source of confusion that is worth diagnosing. Controlling formals
4599 -- often carry names that reflect the type, and it is not worthwhile
4600 -- requiring that their names match.
4602 if Present (Overridden_Subp)
4603 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4610 Form1 := First_Formal (Subp);
4611 Form2 := First_Formal (Overridden_Subp);
4613 -- If the overriding operation is a synchronized operation, skip
4614 -- the first parameter of the overridden operation, which is
4615 -- implicit in the new one. If the operation is declared in the
4616 -- body it is not primitive and all formals must match.
4618 if Is_Concurrent_Type (Scope (Subp))
4619 and then Is_Tagged_Type (Scope (Subp))
4620 and then not Has_Completion (Scope (Subp))
4622 Form2 := Next_Formal (Form2);
4625 if Present (Form1) then
4626 Form1 := Next_Formal (Form1);
4627 Form2 := Next_Formal (Form2);
4630 while Present (Form1) loop
4631 if not Is_Controlling_Formal (Form1)
4632 and then Present (Next_Formal (Form2))
4633 and then Chars (Form1) = Chars (Next_Formal (Form2))
4635 Error_Msg_Node_2 := Alias (Overridden_Subp);
4636 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4638 ("& does not match corresponding formal of&#",
4643 Next_Formal (Form1);
4644 Next_Formal (Form2);
4649 -- If there is an overridden subprogram, then check that there is no
4650 -- "not overriding" indicator, and mark the subprogram as overriding.
4651 -- This is not done if the overridden subprogram is marked as hidden,
4652 -- which can occur for the case of inherited controlled operations
4653 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4654 -- subprogram is not itself hidden. (Note: This condition could probably
4655 -- be simplified, leaving out the testing for the specific controlled
4656 -- cases, but it seems safer and clearer this way, and echoes similar
4657 -- special-case tests of this kind in other places.)
4659 if Present (Overridden_Subp)
4660 and then (not Is_Hidden (Overridden_Subp)
4662 ((Chars (Overridden_Subp) = Name_Initialize
4664 Chars (Overridden_Subp) = Name_Adjust
4666 Chars (Overridden_Subp) = Name_Finalize)
4667 and then Present (Alias (Overridden_Subp))
4668 and then not Is_Hidden (Alias (Overridden_Subp))))
4670 if Must_Not_Override (Spec) then
4671 Error_Msg_Sloc := Sloc (Overridden_Subp);
4673 if Ekind (Subp) = E_Entry then
4675 ("entry & overrides inherited operation #", Spec, Subp);
4678 ("subprogram & overrides inherited operation #", Spec, Subp);
4681 elsif Is_Subprogram (Subp) then
4682 if No (Overridden_Operation (Subp)) then
4684 -- For entities generated by Derive_Subprograms the overridden
4685 -- operation is the inherited primitive (which is available
4686 -- through the attribute alias)
4688 if (Is_Dispatching_Operation (Subp)
4689 or else Is_Dispatching_Operation (Overridden_Subp))
4690 and then not Comes_From_Source (Overridden_Subp)
4691 and then Find_Dispatching_Type (Overridden_Subp) =
4692 Find_Dispatching_Type (Subp)
4693 and then Present (Alias (Overridden_Subp))
4694 and then Comes_From_Source (Alias (Overridden_Subp))
4696 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
4698 Set_Overridden_Operation (Subp, Overridden_Subp);
4703 -- If primitive flag is set or this is a protected operation, then
4704 -- the operation is overriding at the point of its declaration, so
4705 -- warn if necessary. Otherwise it may have been declared before the
4706 -- operation it overrides and no check is required.
4709 and then not Must_Override (Spec)
4710 and then (Is_Primitive
4711 or else Ekind (Scope (Subp)) = E_Protected_Type)
4713 Style.Missing_Overriding (Decl, Subp);
4716 -- If Subp is an operator, it may override a predefined operation, if
4717 -- it is defined in the same scope as the type to which it applies.
4718 -- In that case Overridden_Subp is empty because of our implicit
4719 -- representation for predefined operators. We have to check whether the
4720 -- signature of Subp matches that of a predefined operator. Note that
4721 -- first argument provides the name of the operator, and the second
4722 -- argument the signature that may match that of a standard operation.
4723 -- If the indicator is overriding, then the operator must match a
4724 -- predefined signature, because we know already that there is no
4725 -- explicit overridden operation.
4727 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4729 Typ : constant Entity_Id :=
4730 Base_Type (Etype (First_Formal (Subp)));
4732 Can_Override : constant Boolean :=
4733 Operator_Matches_Spec (Subp, Subp)
4734 and then Scope (Subp) = Scope (Typ)
4735 and then not Is_Class_Wide_Type (Typ);
4738 if Must_Not_Override (Spec) then
4740 -- If this is not a primitive or a protected subprogram, then
4741 -- "not overriding" is illegal.
4744 and then Ekind (Scope (Subp)) /= E_Protected_Type
4747 ("overriding indicator only allowed "
4748 & "if subprogram is primitive", Subp);
4750 elsif Can_Override then
4752 ("subprogram& overrides predefined operator ", Spec, Subp);
4755 elsif Must_Override (Spec) then
4756 if No (Overridden_Operation (Subp))
4757 and then not Can_Override
4759 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4762 elsif not Error_Posted (Subp)
4763 and then Style_Check
4764 and then Can_Override
4766 not Is_Predefined_File_Name
4767 (Unit_File_Name (Get_Source_Unit (Subp)))
4769 -- If style checks are enabled, indicate that the indicator is
4770 -- missing. However, at the point of declaration, the type of
4771 -- which this is a primitive operation may be private, in which
4772 -- case the indicator would be premature.
4774 if Has_Private_Declaration (Etype (Subp))
4775 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4779 Style.Missing_Overriding (Decl, Subp);
4784 elsif Must_Override (Spec) then
4785 if Ekind (Subp) = E_Entry then
4786 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4788 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4791 -- If the operation is marked "not overriding" and it's not primitive
4792 -- then an error is issued, unless this is an operation of a task or
4793 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4794 -- has been specified have already been checked above.
4796 elsif Must_Not_Override (Spec)
4797 and then not Is_Primitive
4798 and then Ekind (Subp) /= E_Entry
4799 and then Ekind (Scope (Subp)) /= E_Protected_Type
4802 ("overriding indicator only allowed if subprogram is primitive",
4806 end Check_Overriding_Indicator;
4812 -- Note: this procedure needs to know far too much about how the expander
4813 -- messes with exceptions. The use of the flag Exception_Junk and the
4814 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4815 -- works, but is not very clean. It would be better if the expansion
4816 -- routines would leave Original_Node working nicely, and we could use
4817 -- Original_Node here to ignore all the peculiar expander messing ???
4819 procedure Check_Returns
4823 Proc : Entity_Id := Empty)
4827 procedure Check_Statement_Sequence (L : List_Id);
4828 -- Internal recursive procedure to check a list of statements for proper
4829 -- termination by a return statement (or a transfer of control or a
4830 -- compound statement that is itself internally properly terminated).
4832 ------------------------------
4833 -- Check_Statement_Sequence --
4834 ------------------------------
4836 procedure Check_Statement_Sequence (L : List_Id) is
4841 Raise_Exception_Call : Boolean;
4842 -- Set True if statement sequence terminated by Raise_Exception call
4843 -- or a Reraise_Occurrence call.
4846 Raise_Exception_Call := False;
4848 -- Get last real statement
4850 Last_Stm := Last (L);
4852 -- Deal with digging out exception handler statement sequences that
4853 -- have been transformed by the local raise to goto optimization.
4854 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4855 -- optimization has occurred, we are looking at something like:
4858 -- original stmts in block
4862 -- goto L1; | omitted if No_Exception_Propagation
4867 -- goto L3; -- skip handler when exception not raised
4869 -- <<L1>> -- target label for local exception
4883 -- and what we have to do is to dig out the estmts1 and estmts2
4884 -- sequences (which were the original sequences of statements in
4885 -- the exception handlers) and check them.
4887 if Nkind (Last_Stm) = N_Label
4888 and then Exception_Junk (Last_Stm)
4894 exit when Nkind (Stm) /= N_Block_Statement;
4895 exit when not Exception_Junk (Stm);
4898 exit when Nkind (Stm) /= N_Label;
4899 exit when not Exception_Junk (Stm);
4900 Check_Statement_Sequence
4901 (Statements (Handled_Statement_Sequence (Next (Stm))));
4906 exit when Nkind (Stm) /= N_Goto_Statement;
4907 exit when not Exception_Junk (Stm);
4911 -- Don't count pragmas
4913 while Nkind (Last_Stm) = N_Pragma
4915 -- Don't count call to SS_Release (can happen after Raise_Exception)
4918 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4920 Nkind (Name (Last_Stm)) = N_Identifier
4922 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4924 -- Don't count exception junk
4927 (Nkind_In (Last_Stm, N_Goto_Statement,
4929 N_Object_Declaration)
4930 and then Exception_Junk (Last_Stm))
4931 or else Nkind (Last_Stm) in N_Push_xxx_Label
4932 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4937 -- Here we have the "real" last statement
4939 Kind := Nkind (Last_Stm);
4941 -- Transfer of control, OK. Note that in the No_Return procedure
4942 -- case, we already diagnosed any explicit return statements, so
4943 -- we can treat them as OK in this context.
4945 if Is_Transfer (Last_Stm) then
4948 -- Check cases of explicit non-indirect procedure calls
4950 elsif Kind = N_Procedure_Call_Statement
4951 and then Is_Entity_Name (Name (Last_Stm))
4953 -- Check call to Raise_Exception procedure which is treated
4954 -- specially, as is a call to Reraise_Occurrence.
4956 -- We suppress the warning in these cases since it is likely that
4957 -- the programmer really does not expect to deal with the case
4958 -- of Null_Occurrence, and thus would find a warning about a
4959 -- missing return curious, and raising Program_Error does not
4960 -- seem such a bad behavior if this does occur.
4962 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4963 -- behavior will be to raise Constraint_Error (see AI-329).
4965 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4967 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4969 Raise_Exception_Call := True;
4971 -- For Raise_Exception call, test first argument, if it is
4972 -- an attribute reference for a 'Identity call, then we know
4973 -- that the call cannot possibly return.
4976 Arg : constant Node_Id :=
4977 Original_Node (First_Actual (Last_Stm));
4979 if Nkind (Arg) = N_Attribute_Reference
4980 and then Attribute_Name (Arg) = Name_Identity
4987 -- If statement, need to look inside if there is an else and check
4988 -- each constituent statement sequence for proper termination.
4990 elsif Kind = N_If_Statement
4991 and then Present (Else_Statements (Last_Stm))
4993 Check_Statement_Sequence (Then_Statements (Last_Stm));
4994 Check_Statement_Sequence (Else_Statements (Last_Stm));
4996 if Present (Elsif_Parts (Last_Stm)) then
4998 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
5001 while Present (Elsif_Part) loop
5002 Check_Statement_Sequence (Then_Statements (Elsif_Part));
5010 -- Case statement, check each case for proper termination
5012 elsif Kind = N_Case_Statement then
5016 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
5017 while Present (Case_Alt) loop
5018 Check_Statement_Sequence (Statements (Case_Alt));
5019 Next_Non_Pragma (Case_Alt);
5025 -- Block statement, check its handled sequence of statements
5027 elsif Kind = N_Block_Statement then
5033 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
5042 -- Loop statement. If there is an iteration scheme, we can definitely
5043 -- fall out of the loop. Similarly if there is an exit statement, we
5044 -- can fall out. In either case we need a following return.
5046 elsif Kind = N_Loop_Statement then
5047 if Present (Iteration_Scheme (Last_Stm))
5048 or else Has_Exit (Entity (Identifier (Last_Stm)))
5052 -- A loop with no exit statement or iteration scheme is either
5053 -- an infinite loop, or it has some other exit (raise/return).
5054 -- In either case, no warning is required.
5060 -- Timed entry call, check entry call and delay alternatives
5062 -- Note: in expanded code, the timed entry call has been converted
5063 -- to a set of expanded statements on which the check will work
5064 -- correctly in any case.
5066 elsif Kind = N_Timed_Entry_Call then
5068 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5069 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
5072 -- If statement sequence of entry call alternative is missing,
5073 -- then we can definitely fall through, and we post the error
5074 -- message on the entry call alternative itself.
5076 if No (Statements (ECA)) then
5079 -- If statement sequence of delay alternative is missing, then
5080 -- we can definitely fall through, and we post the error
5081 -- message on the delay alternative itself.
5083 -- Note: if both ECA and DCA are missing the return, then we
5084 -- post only one message, should be enough to fix the bugs.
5085 -- If not we will get a message next time on the DCA when the
5088 elsif No (Statements (DCA)) then
5091 -- Else check both statement sequences
5094 Check_Statement_Sequence (Statements (ECA));
5095 Check_Statement_Sequence (Statements (DCA));
5100 -- Conditional entry call, check entry call and else part
5102 -- Note: in expanded code, the conditional entry call has been
5103 -- converted to a set of expanded statements on which the check
5104 -- will work correctly in any case.
5106 elsif Kind = N_Conditional_Entry_Call then
5108 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5111 -- If statement sequence of entry call alternative is missing,
5112 -- then we can definitely fall through, and we post the error
5113 -- message on the entry call alternative itself.
5115 if No (Statements (ECA)) then
5118 -- Else check statement sequence and else part
5121 Check_Statement_Sequence (Statements (ECA));
5122 Check_Statement_Sequence (Else_Statements (Last_Stm));
5128 -- If we fall through, issue appropriate message
5131 if not Raise_Exception_Call then
5133 ("?RETURN statement missing following this statement!",
5136 ("\?Program_Error may be raised at run time!",
5140 -- Note: we set Err even though we have not issued a warning
5141 -- because we still have a case of a missing return. This is
5142 -- an extremely marginal case, probably will never be noticed
5143 -- but we might as well get it right.
5147 -- Otherwise we have the case of a procedure marked No_Return
5150 if not Raise_Exception_Call then
5152 ("?implied return after this statement " &
5153 "will raise Program_Error",
5156 ("\?procedure & is marked as No_Return!",
5161 RE : constant Node_Id :=
5162 Make_Raise_Program_Error (Sloc (Last_Stm),
5163 Reason => PE_Implicit_Return);
5165 Insert_After (Last_Stm, RE);
5169 end Check_Statement_Sequence;
5171 -- Start of processing for Check_Returns
5175 Check_Statement_Sequence (Statements (HSS));
5177 if Present (Exception_Handlers (HSS)) then
5178 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5179 while Present (Handler) loop
5180 Check_Statement_Sequence (Statements (Handler));
5181 Next_Non_Pragma (Handler);
5186 ----------------------------
5187 -- Check_Subprogram_Order --
5188 ----------------------------
5190 procedure Check_Subprogram_Order (N : Node_Id) is
5192 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5193 -- This is used to check if S1 > S2 in the sense required by this
5194 -- test, for example nameab < namec, but name2 < name10.
5196 -----------------------------
5197 -- Subprogram_Name_Greater --
5198 -----------------------------
5200 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5205 -- Remove trailing numeric parts
5208 while S1 (L1) in '0' .. '9' loop
5213 while S2 (L2) in '0' .. '9' loop
5217 -- If non-numeric parts non-equal, that's decisive
5219 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5222 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5225 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5226 -- that a missing suffix is treated as numeric zero in this test.
5230 while L1 < S1'Last loop
5232 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5236 while L2 < S2'Last loop
5238 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5243 end Subprogram_Name_Greater;
5245 -- Start of processing for Check_Subprogram_Order
5248 -- Check body in alpha order if this is option
5251 and then Style_Check_Order_Subprograms
5252 and then Nkind (N) = N_Subprogram_Body
5253 and then Comes_From_Source (N)
5254 and then In_Extended_Main_Source_Unit (N)
5258 renames Scope_Stack.Table
5259 (Scope_Stack.Last).Last_Subprogram_Name;
5261 Body_Id : constant Entity_Id :=
5262 Defining_Entity (Specification (N));
5265 Get_Decoded_Name_String (Chars (Body_Id));
5268 if Subprogram_Name_Greater
5269 (LSN.all, Name_Buffer (1 .. Name_Len))
5271 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5277 LSN := new String'(Name_Buffer (1 .. Name_Len));
5280 end Check_Subprogram_Order;
5282 ------------------------------
5283 -- Check_Subtype_Conformant --
5284 ------------------------------
5286 procedure Check_Subtype_Conformant
5287 (New_Id : Entity_Id;
5289 Err_Loc : Node_Id := Empty;
5290 Skip_Controlling_Formals : Boolean := False)
5293 pragma Warnings (Off, Result);
5296 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5297 Skip_Controlling_Formals => Skip_Controlling_Formals);
5298 end Check_Subtype_Conformant;
5300 ---------------------------
5301 -- Check_Type_Conformant --
5302 ---------------------------
5304 procedure Check_Type_Conformant
5305 (New_Id : Entity_Id;
5307 Err_Loc : Node_Id := Empty)
5310 pragma Warnings (Off, Result);
5313 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5314 end Check_Type_Conformant;
5316 ----------------------
5317 -- Conforming_Types --
5318 ----------------------
5320 function Conforming_Types
5323 Ctype : Conformance_Type;
5324 Get_Inst : Boolean := False) return Boolean
5326 Type_1 : Entity_Id := T1;
5327 Type_2 : Entity_Id := T2;
5328 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5330 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5331 -- If neither T1 nor T2 are generic actual types, or if they are in
5332 -- different scopes (e.g. parent and child instances), then verify that
5333 -- the base types are equal. Otherwise T1 and T2 must be on the same
5334 -- subtype chain. The whole purpose of this procedure is to prevent
5335 -- spurious ambiguities in an instantiation that may arise if two
5336 -- distinct generic types are instantiated with the same actual.
5338 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5339 -- An access parameter can designate an incomplete type. If the
5340 -- incomplete type is the limited view of a type from a limited_
5341 -- with_clause, check whether the non-limited view is available. If
5342 -- it is a (non-limited) incomplete type, get the full view.
5344 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5345 -- Returns True if and only if either T1 denotes a limited view of T2
5346 -- or T2 denotes a limited view of T1. This can arise when the limited
5347 -- with view of a type is used in a subprogram declaration and the
5348 -- subprogram body is in the scope of a regular with clause for the
5349 -- same unit. In such a case, the two type entities can be considered
5350 -- identical for purposes of conformance checking.
5352 ----------------------
5353 -- Base_Types_Match --
5354 ----------------------
5356 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5361 elsif Base_Type (T1) = Base_Type (T2) then
5363 -- The following is too permissive. A more precise test should
5364 -- check that the generic actual is an ancestor subtype of the
5367 return not Is_Generic_Actual_Type (T1)
5368 or else not Is_Generic_Actual_Type (T2)
5369 or else Scope (T1) /= Scope (T2);
5374 end Base_Types_Match;
5376 --------------------------
5377 -- Find_Designated_Type --
5378 --------------------------
5380 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5384 Desig := Directly_Designated_Type (T);
5386 if Ekind (Desig) = E_Incomplete_Type then
5388 -- If regular incomplete type, get full view if available
5390 if Present (Full_View (Desig)) then
5391 Desig := Full_View (Desig);
5393 -- If limited view of a type, get non-limited view if available,
5394 -- and check again for a regular incomplete type.
5396 elsif Present (Non_Limited_View (Desig)) then
5397 Desig := Get_Full_View (Non_Limited_View (Desig));
5402 end Find_Designated_Type;
5404 -------------------------------
5405 -- Matches_Limited_With_View --
5406 -------------------------------
5408 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5410 -- In some cases a type imported through a limited_with clause, and
5411 -- its nonlimited view are both visible, for example in an anonymous
5412 -- access-to-class-wide type in a formal. Both entities designate the
5415 if From_With_Type (T1)
5416 and then T2 = Available_View (T1)
5420 elsif From_With_Type (T2)
5421 and then T1 = Available_View (T2)
5428 end Matches_Limited_With_View;
5430 -- Start of processing for Conforming_Types
5433 -- The context is an instance association for a formal
5434 -- access-to-subprogram type; the formal parameter types require
5435 -- mapping because they may denote other formal parameters of the
5439 Type_1 := Get_Instance_Of (T1);
5440 Type_2 := Get_Instance_Of (T2);
5443 -- If one of the types is a view of the other introduced by a limited
5444 -- with clause, treat these as conforming for all purposes.
5446 if Matches_Limited_With_View (T1, T2) then
5449 elsif Base_Types_Match (Type_1, Type_2) then
5450 return Ctype <= Mode_Conformant
5451 or else Subtypes_Statically_Match (Type_1, Type_2);
5453 elsif Is_Incomplete_Or_Private_Type (Type_1)
5454 and then Present (Full_View (Type_1))
5455 and then Base_Types_Match (Full_View (Type_1), Type_2)
5457 return Ctype <= Mode_Conformant
5458 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5460 elsif Ekind (Type_2) = E_Incomplete_Type
5461 and then Present (Full_View (Type_2))
5462 and then Base_Types_Match (Type_1, Full_View (Type_2))
5464 return Ctype <= Mode_Conformant
5465 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5467 elsif Is_Private_Type (Type_2)
5468 and then In_Instance
5469 and then Present (Full_View (Type_2))
5470 and then Base_Types_Match (Type_1, Full_View (Type_2))
5472 return Ctype <= Mode_Conformant
5473 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5476 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5477 -- treated recursively because they carry a signature.
5479 Are_Anonymous_Access_To_Subprogram_Types :=
5480 Ekind (Type_1) = Ekind (Type_2)
5482 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5484 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5486 -- Test anonymous access type case. For this case, static subtype
5487 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5488 -- the base types because we may have built internal subtype entities
5489 -- to handle null-excluding types (see Process_Formals).
5491 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5493 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5494 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5497 Desig_1 : Entity_Id;
5498 Desig_2 : Entity_Id;
5501 -- In Ada2005, access constant indicators must match for
5502 -- subtype conformance.
5504 if Ada_Version >= Ada_2005
5505 and then Ctype >= Subtype_Conformant
5507 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5512 Desig_1 := Find_Designated_Type (Type_1);
5513 Desig_2 := Find_Designated_Type (Type_2);
5515 -- If the context is an instance association for a formal
5516 -- access-to-subprogram type; formal access parameter designated
5517 -- types require mapping because they may denote other formal
5518 -- parameters of the generic unit.
5521 Desig_1 := Get_Instance_Of (Desig_1);
5522 Desig_2 := Get_Instance_Of (Desig_2);
5525 -- It is possible for a Class_Wide_Type to be introduced for an
5526 -- incomplete type, in which case there is a separate class_ wide
5527 -- type for the full view. The types conform if their Etypes
5528 -- conform, i.e. one may be the full view of the other. This can
5529 -- only happen in the context of an access parameter, other uses
5530 -- of an incomplete Class_Wide_Type are illegal.
5532 if Is_Class_Wide_Type (Desig_1)
5534 Is_Class_Wide_Type (Desig_2)
5538 (Etype (Base_Type (Desig_1)),
5539 Etype (Base_Type (Desig_2)), Ctype);
5541 elsif Are_Anonymous_Access_To_Subprogram_Types then
5542 if Ada_Version < Ada_2005 then
5543 return Ctype = Type_Conformant
5545 Subtypes_Statically_Match (Desig_1, Desig_2);
5547 -- We must check the conformance of the signatures themselves
5551 Conformant : Boolean;
5554 (Desig_1, Desig_2, Ctype, False, Conformant);
5560 return Base_Type (Desig_1) = Base_Type (Desig_2)
5561 and then (Ctype = Type_Conformant
5563 Subtypes_Statically_Match (Desig_1, Desig_2));
5567 -- Otherwise definitely no match
5570 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5571 and then Is_Access_Type (Type_2))
5572 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5573 and then Is_Access_Type (Type_1)))
5576 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5578 May_Hide_Profile := True;
5583 end Conforming_Types;
5585 --------------------------
5586 -- Create_Extra_Formals --
5587 --------------------------
5589 procedure Create_Extra_Formals (E : Entity_Id) is
5591 First_Extra : Entity_Id := Empty;
5592 Last_Extra : Entity_Id;
5593 Formal_Type : Entity_Id;
5594 P_Formal : Entity_Id := Empty;
5596 function Add_Extra_Formal
5597 (Assoc_Entity : Entity_Id;
5600 Suffix : String) return Entity_Id;
5601 -- Add an extra formal to the current list of formals and extra formals.
5602 -- The extra formal is added to the end of the list of extra formals,
5603 -- and also returned as the result. These formals are always of mode IN.
5604 -- The new formal has the type Typ, is declared in Scope, and its name
5605 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5606 -- The following suffixes are currently used. They should not be changed
5607 -- without coordinating with CodePeer, which makes use of these to
5608 -- provide better messages.
5610 -- O denotes the Constrained bit.
5611 -- L denotes the accessibility level.
5612 -- BIP_xxx denotes an extra formal for a build-in-place function. See
5613 -- the full list in exp_ch6.BIP_Formal_Kind.
5615 ----------------------
5616 -- Add_Extra_Formal --
5617 ----------------------
5619 function Add_Extra_Formal
5620 (Assoc_Entity : Entity_Id;
5623 Suffix : String) return Entity_Id
5625 EF : constant Entity_Id :=
5626 Make_Defining_Identifier (Sloc (Assoc_Entity),
5627 Chars => New_External_Name (Chars (Assoc_Entity),
5631 -- A little optimization. Never generate an extra formal for the
5632 -- _init operand of an initialization procedure, since it could
5635 if Chars (Formal) = Name_uInit then
5639 Set_Ekind (EF, E_In_Parameter);
5640 Set_Actual_Subtype (EF, Typ);
5641 Set_Etype (EF, Typ);
5642 Set_Scope (EF, Scope);
5643 Set_Mechanism (EF, Default_Mechanism);
5644 Set_Formal_Validity (EF);
5646 if No (First_Extra) then
5648 Set_Extra_Formals (Scope, First_Extra);
5651 if Present (Last_Extra) then
5652 Set_Extra_Formal (Last_Extra, EF);
5658 end Add_Extra_Formal;
5660 -- Start of processing for Create_Extra_Formals
5663 -- We never generate extra formals if expansion is not active
5664 -- because we don't need them unless we are generating code.
5666 if not Expander_Active then
5670 -- If this is a derived subprogram then the subtypes of the parent
5671 -- subprogram's formal parameters will be used to determine the need
5672 -- for extra formals.
5674 if Is_Overloadable (E) and then Present (Alias (E)) then
5675 P_Formal := First_Formal (Alias (E));
5678 Last_Extra := Empty;
5679 Formal := First_Formal (E);
5680 while Present (Formal) loop
5681 Last_Extra := Formal;
5682 Next_Formal (Formal);
5685 -- If Extra_formals were already created, don't do it again. This
5686 -- situation may arise for subprogram types created as part of
5687 -- dispatching calls (see Expand_Dispatching_Call)
5689 if Present (Last_Extra) and then
5690 Present (Extra_Formal (Last_Extra))
5695 -- If the subprogram is a predefined dispatching subprogram then don't
5696 -- generate any extra constrained or accessibility level formals. In
5697 -- general we suppress these for internal subprograms (by not calling
5698 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5699 -- generated stream attributes do get passed through because extra
5700 -- build-in-place formals are needed in some cases (limited 'Input).
5702 if Is_Predefined_Internal_Operation (E) then
5703 goto Test_For_BIP_Extras;
5706 Formal := First_Formal (E);
5707 while Present (Formal) loop
5709 -- Create extra formal for supporting the attribute 'Constrained.
5710 -- The case of a private type view without discriminants also
5711 -- requires the extra formal if the underlying type has defaulted
5714 if Ekind (Formal) /= E_In_Parameter then
5715 if Present (P_Formal) then
5716 Formal_Type := Etype (P_Formal);
5718 Formal_Type := Etype (Formal);
5721 -- Do not produce extra formals for Unchecked_Union parameters.
5722 -- Jump directly to the end of the loop.
5724 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5725 goto Skip_Extra_Formal_Generation;
5728 if not Has_Discriminants (Formal_Type)
5729 and then Ekind (Formal_Type) in Private_Kind
5730 and then Present (Underlying_Type (Formal_Type))
5732 Formal_Type := Underlying_Type (Formal_Type);
5735 -- Suppress the extra formal if formal's subtype is constrained or
5736 -- indefinite, or we're compiling for Ada 2012 and the underlying
5737 -- type is tagged and limited. In Ada 2012, a limited tagged type
5738 -- can have defaulted discriminants, but 'Constrained is required
5739 -- to return True, so the formal is never needed (see AI05-0214).
5740 -- Note that this ensures consistency of calling sequences for
5741 -- dispatching operations when some types in a class have defaults
5742 -- on discriminants and others do not (and requiring the extra
5743 -- formal would introduce distributed overhead).
5745 if Has_Discriminants (Formal_Type)
5746 and then not Is_Constrained (Formal_Type)
5747 and then not Is_Indefinite_Subtype (Formal_Type)
5748 and then (Ada_Version < Ada_2012
5750 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
5751 and then Is_Limited_Type (Formal_Type)))
5753 Set_Extra_Constrained
5754 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
5758 -- Create extra formal for supporting accessibility checking. This
5759 -- is done for both anonymous access formals and formals of named
5760 -- access types that are marked as controlling formals. The latter
5761 -- case can occur when Expand_Dispatching_Call creates a subprogram
5762 -- type and substitutes the types of access-to-class-wide actuals
5763 -- for the anonymous access-to-specific-type of controlling formals.
5764 -- Base_Type is applied because in cases where there is a null
5765 -- exclusion the formal may have an access subtype.
5767 -- This is suppressed if we specifically suppress accessibility
5768 -- checks at the package level for either the subprogram, or the
5769 -- package in which it resides. However, we do not suppress it
5770 -- simply if the scope has accessibility checks suppressed, since
5771 -- this could cause trouble when clients are compiled with a
5772 -- different suppression setting. The explicit checks at the
5773 -- package level are safe from this point of view.
5775 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5776 or else (Is_Controlling_Formal (Formal)
5777 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5779 (Explicit_Suppress (E, Accessibility_Check)
5781 Explicit_Suppress (Scope (E), Accessibility_Check))
5784 or else Present (Extra_Accessibility (P_Formal)))
5786 Set_Extra_Accessibility
5787 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
5790 -- This label is required when skipping extra formal generation for
5791 -- Unchecked_Union parameters.
5793 <<Skip_Extra_Formal_Generation>>
5795 if Present (P_Formal) then
5796 Next_Formal (P_Formal);
5799 Next_Formal (Formal);
5802 <<Test_For_BIP_Extras>>
5804 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5805 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5807 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
5809 Result_Subt : constant Entity_Id := Etype (E);
5811 Discard : Entity_Id;
5812 pragma Warnings (Off, Discard);
5815 -- In the case of functions with unconstrained result subtypes,
5816 -- add a 4-state formal indicating whether the return object is
5817 -- allocated by the caller (1), or should be allocated by the
5818 -- callee on the secondary stack (2), in the global heap (3), or
5819 -- in a user-defined storage pool (4). For the moment we just use
5820 -- Natural for the type of this formal. Note that this formal
5821 -- isn't usually needed in the case where the result subtype is
5822 -- constrained, but it is needed when the function has a tagged
5823 -- result, because generally such functions can be called in a
5824 -- dispatching context and such calls must be handled like calls
5825 -- to a class-wide function.
5827 if not Is_Constrained (Underlying_Type (Result_Subt))
5828 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5832 (E, Standard_Natural,
5833 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5836 -- For functions whose result type has controlled parts, we have
5837 -- an extra formal of type System.Finalization_Implementation.
5838 -- Finalizable_Ptr_Ptr. That is, we are passing a pointer to a
5839 -- finalization list (which is itself a pointer). This extra
5840 -- formal is then passed along to Move_Final_List in case of
5841 -- successful completion of a return statement. We cannot pass an
5842 -- 'in out' parameter, because we need to update the finalization
5843 -- list during an abort-deferred region, rather than using
5844 -- copy-back after the function returns. This is true even if we
5845 -- are able to get away with having 'in out' parameters, which are
5846 -- normally illegal for functions. This formal is also needed when
5847 -- the function has a tagged result.
5849 if Needs_BIP_Final_List (E) then
5852 (E, RTE (RE_Finalizable_Ptr_Ptr),
5853 E, BIP_Formal_Suffix (BIP_Final_List));
5856 -- If the result type contains tasks, we have two extra formals:
5857 -- the master of the tasks to be created, and the caller's
5858 -- activation chain.
5860 if Has_Task (Result_Subt) then
5863 (E, RTE (RE_Master_Id),
5864 E, BIP_Formal_Suffix (BIP_Master));
5867 (E, RTE (RE_Activation_Chain_Access),
5868 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5871 -- All build-in-place functions get an extra formal that will be
5872 -- passed the address of the return object within the caller.
5875 Formal_Type : constant Entity_Id :=
5877 (E_Anonymous_Access_Type, E,
5878 Scope_Id => Scope (E));
5880 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5881 Set_Etype (Formal_Type, Formal_Type);
5882 Set_Depends_On_Private
5883 (Formal_Type, Has_Private_Component (Formal_Type));
5884 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5885 Set_Is_Access_Constant (Formal_Type, False);
5887 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5888 -- the designated type comes from the limited view (for
5889 -- back-end purposes).
5891 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5893 Layout_Type (Formal_Type);
5897 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5901 end Create_Extra_Formals;
5903 -----------------------------
5904 -- Enter_Overloaded_Entity --
5905 -----------------------------
5907 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5908 E : Entity_Id := Current_Entity_In_Scope (S);
5909 C_E : Entity_Id := Current_Entity (S);
5913 Set_Has_Homonym (E);
5914 Set_Has_Homonym (S);
5917 Set_Is_Immediately_Visible (S);
5918 Set_Scope (S, Current_Scope);
5920 -- Chain new entity if front of homonym in current scope, so that
5921 -- homonyms are contiguous.
5926 while Homonym (C_E) /= E loop
5927 C_E := Homonym (C_E);
5930 Set_Homonym (C_E, S);
5934 Set_Current_Entity (S);
5939 Append_Entity (S, Current_Scope);
5940 Set_Public_Status (S);
5942 if Debug_Flag_E then
5943 Write_Str ("New overloaded entity chain: ");
5944 Write_Name (Chars (S));
5947 while Present (E) loop
5948 Write_Str (" "); Write_Int (Int (E));
5955 -- Generate warning for hiding
5958 and then Comes_From_Source (S)
5959 and then In_Extended_Main_Source_Unit (S)
5966 -- Warn unless genuine overloading. Do not emit warning on
5967 -- hiding predefined operators in Standard (these are either an
5968 -- (artifact of our implicit declarations, or simple noise) but
5969 -- keep warning on a operator defined on a local subtype, because
5970 -- of the real danger that different operators may be applied in
5971 -- various parts of the program.
5973 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5974 and then (Is_Immediately_Visible (E)
5976 Is_Potentially_Use_Visible (S))
5978 if Scope (E) /= Standard_Standard then
5979 Error_Msg_Sloc := Sloc (E);
5980 Error_Msg_N ("declaration of & hides one#?", S);
5982 elsif Nkind (S) = N_Defining_Operator_Symbol
5985 Base_Type (Etype (First_Formal (S)))) /= Scope (S)
5988 ("declaration of & hides predefined operator?", S);
5993 end Enter_Overloaded_Entity;
5995 -----------------------------
5996 -- Check_Untagged_Equality --
5997 -----------------------------
5999 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
6000 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
6001 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
6005 if Nkind (Decl) = N_Subprogram_Declaration
6006 and then Is_Record_Type (Typ)
6007 and then not Is_Tagged_Type (Typ)
6009 -- If the type is not declared in a package, or if we are in the
6010 -- body of the package or in some other scope, the new operation is
6011 -- not primitive, and therefore legal, though suspicious. If the
6012 -- type is a generic actual (sub)type, the operation is not primitive
6013 -- either because the base type is declared elsewhere.
6015 if Is_Frozen (Typ) then
6016 if Ekind (Scope (Typ)) /= E_Package
6017 or else Scope (Typ) /= Current_Scope
6021 elsif Is_Generic_Actual_Type (Typ) then
6024 elsif In_Package_Body (Scope (Typ)) then
6026 ("equality operator must be declared "
6027 & "before type& is frozen", Eq_Op, Typ);
6029 ("\move declaration to package spec", Eq_Op);
6033 ("equality operator must be declared "
6034 & "before type& is frozen", Eq_Op, Typ);
6036 Obj_Decl := Next (Parent (Typ));
6037 while Present (Obj_Decl)
6038 and then Obj_Decl /= Decl
6040 if Nkind (Obj_Decl) = N_Object_Declaration
6041 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
6043 Error_Msg_NE ("type& is frozen by declaration?",
6046 ("\an equality operator cannot be declared after this "
6047 & "point ('R'M 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
6055 elsif not In_Same_List (Parent (Typ), Decl)
6056 and then not Is_Limited_Type (Typ)
6059 -- This makes it illegal to have a primitive equality declared in
6060 -- the private part if the type is visible.
6062 Error_Msg_N ("equality operator appears too late", Eq_Op);
6065 end Check_Untagged_Equality;
6067 -----------------------------
6068 -- Find_Corresponding_Spec --
6069 -----------------------------
6071 function Find_Corresponding_Spec
6073 Post_Error : Boolean := True) return Entity_Id
6075 Spec : constant Node_Id := Specification (N);
6076 Designator : constant Entity_Id := Defining_Entity (Spec);
6081 E := Current_Entity (Designator);
6082 while Present (E) loop
6084 -- We are looking for a matching spec. It must have the same scope,
6085 -- and the same name, and either be type conformant, or be the case
6086 -- of a library procedure spec and its body (which belong to one
6087 -- another regardless of whether they are type conformant or not).
6089 if Scope (E) = Current_Scope then
6090 if Current_Scope = Standard_Standard
6091 or else (Ekind (E) = Ekind (Designator)
6092 and then Type_Conformant (E, Designator))
6094 -- Within an instantiation, we know that spec and body are
6095 -- subtype conformant, because they were subtype conformant
6096 -- in the generic. We choose the subtype-conformant entity
6097 -- here as well, to resolve spurious ambiguities in the
6098 -- instance that were not present in the generic (i.e. when
6099 -- two different types are given the same actual). If we are
6100 -- looking for a spec to match a body, full conformance is
6104 Set_Convention (Designator, Convention (E));
6106 if Nkind (N) = N_Subprogram_Body
6107 and then Present (Homonym (E))
6108 and then not Fully_Conformant (E, Designator)
6112 elsif not Subtype_Conformant (E, Designator) then
6117 if not Has_Completion (E) then
6118 if Nkind (N) /= N_Subprogram_Body_Stub then
6119 Set_Corresponding_Spec (N, E);
6122 Set_Has_Completion (E);
6125 elsif Nkind (Parent (N)) = N_Subunit then
6127 -- If this is the proper body of a subunit, the completion
6128 -- flag is set when analyzing the stub.
6132 -- If E is an internal function with a controlling result
6133 -- that was created for an operation inherited by a null
6134 -- extension, it may be overridden by a body without a previous
6135 -- spec (one more reason why these should be shunned). In that
6136 -- case remove the generated body if present, because the
6137 -- current one is the explicit overriding.
6139 elsif Ekind (E) = E_Function
6140 and then Ada_Version >= Ada_2005
6141 and then not Comes_From_Source (E)
6142 and then Has_Controlling_Result (E)
6143 and then Is_Null_Extension (Etype (E))
6144 and then Comes_From_Source (Spec)
6146 Set_Has_Completion (E, False);
6149 and then Nkind (Parent (E)) = N_Function_Specification
6152 (Unit_Declaration_Node
6153 (Corresponding_Body (Unit_Declaration_Node (E))));
6157 -- If expansion is disabled, or if the wrapper function has
6158 -- not been generated yet, this a late body overriding an
6159 -- inherited operation, or it is an overriding by some other
6160 -- declaration before the controlling result is frozen. In
6161 -- either case this is a declaration of a new entity.
6167 -- If the body already exists, then this is an error unless
6168 -- the previous declaration is the implicit declaration of a
6169 -- derived subprogram, or this is a spurious overloading in an
6172 elsif No (Alias (E))
6173 and then not Is_Intrinsic_Subprogram (E)
6174 and then not In_Instance
6177 Error_Msg_Sloc := Sloc (E);
6179 if Is_Imported (E) then
6181 ("body not allowed for imported subprogram & declared#",
6184 Error_Msg_NE ("duplicate body for & declared#", N, E);
6188 -- Child units cannot be overloaded, so a conformance mismatch
6189 -- between body and a previous spec is an error.
6191 elsif Is_Child_Unit (E)
6193 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
6195 Nkind (Parent (Unit_Declaration_Node (Designator))) =
6200 ("body of child unit does not match previous declaration", N);
6208 -- On exit, we know that no previous declaration of subprogram exists
6211 end Find_Corresponding_Spec;
6213 ----------------------
6214 -- Fully_Conformant --
6215 ----------------------
6217 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6220 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
6222 end Fully_Conformant;
6224 ----------------------------------
6225 -- Fully_Conformant_Expressions --
6226 ----------------------------------
6228 function Fully_Conformant_Expressions
6229 (Given_E1 : Node_Id;
6230 Given_E2 : Node_Id) return Boolean
6232 E1 : constant Node_Id := Original_Node (Given_E1);
6233 E2 : constant Node_Id := Original_Node (Given_E2);
6234 -- We always test conformance on original nodes, since it is possible
6235 -- for analysis and/or expansion to make things look as though they
6236 -- conform when they do not, e.g. by converting 1+2 into 3.
6238 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6239 renames Fully_Conformant_Expressions;
6241 function FCL (L1, L2 : List_Id) return Boolean;
6242 -- Compare elements of two lists for conformance. Elements have to
6243 -- be conformant, and actuals inserted as default parameters do not
6244 -- match explicit actuals with the same value.
6246 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6247 -- Compare an operator node with a function call
6253 function FCL (L1, L2 : List_Id) return Boolean is
6257 if L1 = No_List then
6263 if L2 = No_List then
6269 -- Compare two lists, skipping rewrite insertions (we want to
6270 -- compare the original trees, not the expanded versions!)
6273 if Is_Rewrite_Insertion (N1) then
6275 elsif Is_Rewrite_Insertion (N2) then
6281 elsif not FCE (N1, N2) then
6294 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6295 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6300 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6305 Act := First (Actuals);
6307 if Nkind (Op_Node) in N_Binary_Op then
6308 if not FCE (Left_Opnd (Op_Node), Act) then
6315 return Present (Act)
6316 and then FCE (Right_Opnd (Op_Node), Act)
6317 and then No (Next (Act));
6321 -- Start of processing for Fully_Conformant_Expressions
6324 -- Non-conformant if paren count does not match. Note: if some idiot
6325 -- complains that we don't do this right for more than 3 levels of
6326 -- parentheses, they will be treated with the respect they deserve!
6328 if Paren_Count (E1) /= Paren_Count (E2) then
6331 -- If same entities are referenced, then they are conformant even if
6332 -- they have different forms (RM 8.3.1(19-20)).
6334 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6335 if Present (Entity (E1)) then
6336 return Entity (E1) = Entity (E2)
6337 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6338 and then Ekind (Entity (E1)) = E_Discriminant
6339 and then Ekind (Entity (E2)) = E_In_Parameter);
6341 elsif Nkind (E1) = N_Expanded_Name
6342 and then Nkind (E2) = N_Expanded_Name
6343 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6344 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6346 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6349 -- Identifiers in component associations don't always have
6350 -- entities, but their names must conform.
6352 return Nkind (E1) = N_Identifier
6353 and then Nkind (E2) = N_Identifier
6354 and then Chars (E1) = Chars (E2);
6357 elsif Nkind (E1) = N_Character_Literal
6358 and then Nkind (E2) = N_Expanded_Name
6360 return Nkind (Selector_Name (E2)) = N_Character_Literal
6361 and then Chars (E1) = Chars (Selector_Name (E2));
6363 elsif Nkind (E2) = N_Character_Literal
6364 and then Nkind (E1) = N_Expanded_Name
6366 return Nkind (Selector_Name (E1)) = N_Character_Literal
6367 and then Chars (E2) = Chars (Selector_Name (E1));
6369 elsif Nkind (E1) in N_Op
6370 and then Nkind (E2) = N_Function_Call
6372 return FCO (E1, E2);
6374 elsif Nkind (E2) in N_Op
6375 and then Nkind (E1) = N_Function_Call
6377 return FCO (E2, E1);
6379 -- Otherwise we must have the same syntactic entity
6381 elsif Nkind (E1) /= Nkind (E2) then
6384 -- At this point, we specialize by node type
6391 FCL (Expressions (E1), Expressions (E2))
6393 FCL (Component_Associations (E1),
6394 Component_Associations (E2));
6397 if Nkind (Expression (E1)) = N_Qualified_Expression
6399 Nkind (Expression (E2)) = N_Qualified_Expression
6401 return FCE (Expression (E1), Expression (E2));
6403 -- Check that the subtype marks and any constraints
6408 Indic1 : constant Node_Id := Expression (E1);
6409 Indic2 : constant Node_Id := Expression (E2);
6414 if Nkind (Indic1) /= N_Subtype_Indication then
6416 Nkind (Indic2) /= N_Subtype_Indication
6417 and then Entity (Indic1) = Entity (Indic2);
6419 elsif Nkind (Indic2) /= N_Subtype_Indication then
6421 Nkind (Indic1) /= N_Subtype_Indication
6422 and then Entity (Indic1) = Entity (Indic2);
6425 if Entity (Subtype_Mark (Indic1)) /=
6426 Entity (Subtype_Mark (Indic2))
6431 Elt1 := First (Constraints (Constraint (Indic1)));
6432 Elt2 := First (Constraints (Constraint (Indic2)));
6433 while Present (Elt1) and then Present (Elt2) loop
6434 if not FCE (Elt1, Elt2) then
6447 when N_Attribute_Reference =>
6449 Attribute_Name (E1) = Attribute_Name (E2)
6450 and then FCL (Expressions (E1), Expressions (E2));
6454 Entity (E1) = Entity (E2)
6455 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6456 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6458 when N_Short_Circuit | N_Membership_Test =>
6460 FCE (Left_Opnd (E1), Left_Opnd (E2))
6462 FCE (Right_Opnd (E1), Right_Opnd (E2));
6464 when N_Case_Expression =>
6470 if not FCE (Expression (E1), Expression (E2)) then
6474 Alt1 := First (Alternatives (E1));
6475 Alt2 := First (Alternatives (E2));
6477 if Present (Alt1) /= Present (Alt2) then
6479 elsif No (Alt1) then
6483 if not FCE (Expression (Alt1), Expression (Alt2))
6484 or else not FCL (Discrete_Choices (Alt1),
6485 Discrete_Choices (Alt2))
6496 when N_Character_Literal =>
6498 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6500 when N_Component_Association =>
6502 FCL (Choices (E1), Choices (E2))
6504 FCE (Expression (E1), Expression (E2));
6506 when N_Conditional_Expression =>
6508 FCL (Expressions (E1), Expressions (E2));
6510 when N_Explicit_Dereference =>
6512 FCE (Prefix (E1), Prefix (E2));
6514 when N_Extension_Aggregate =>
6516 FCL (Expressions (E1), Expressions (E2))
6517 and then Null_Record_Present (E1) =
6518 Null_Record_Present (E2)
6519 and then FCL (Component_Associations (E1),
6520 Component_Associations (E2));
6522 when N_Function_Call =>
6524 FCE (Name (E1), Name (E2))
6526 FCL (Parameter_Associations (E1),
6527 Parameter_Associations (E2));
6529 when N_Indexed_Component =>
6531 FCE (Prefix (E1), Prefix (E2))
6533 FCL (Expressions (E1), Expressions (E2));
6535 when N_Integer_Literal =>
6536 return (Intval (E1) = Intval (E2));
6541 when N_Operator_Symbol =>
6543 Chars (E1) = Chars (E2);
6545 when N_Others_Choice =>
6548 when N_Parameter_Association =>
6550 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6551 and then FCE (Explicit_Actual_Parameter (E1),
6552 Explicit_Actual_Parameter (E2));
6554 when N_Qualified_Expression =>
6556 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6558 FCE (Expression (E1), Expression (E2));
6562 FCE (Low_Bound (E1), Low_Bound (E2))
6564 FCE (High_Bound (E1), High_Bound (E2));
6566 when N_Real_Literal =>
6567 return (Realval (E1) = Realval (E2));
6569 when N_Selected_Component =>
6571 FCE (Prefix (E1), Prefix (E2))
6573 FCE (Selector_Name (E1), Selector_Name (E2));
6577 FCE (Prefix (E1), Prefix (E2))
6579 FCE (Discrete_Range (E1), Discrete_Range (E2));
6581 when N_String_Literal =>
6583 S1 : constant String_Id := Strval (E1);
6584 S2 : constant String_Id := Strval (E2);
6585 L1 : constant Nat := String_Length (S1);
6586 L2 : constant Nat := String_Length (S2);
6593 for J in 1 .. L1 loop
6594 if Get_String_Char (S1, J) /=
6595 Get_String_Char (S2, J)
6605 when N_Type_Conversion =>
6607 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6609 FCE (Expression (E1), Expression (E2));
6613 Entity (E1) = Entity (E2)
6615 FCE (Right_Opnd (E1), Right_Opnd (E2));
6617 when N_Unchecked_Type_Conversion =>
6619 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6621 FCE (Expression (E1), Expression (E2));
6623 -- All other node types cannot appear in this context. Strictly
6624 -- we should raise a fatal internal error. Instead we just ignore
6625 -- the nodes. This means that if anyone makes a mistake in the
6626 -- expander and mucks an expression tree irretrievably, the
6627 -- result will be a failure to detect a (probably very obscure)
6628 -- case of non-conformance, which is better than bombing on some
6629 -- case where two expressions do in fact conform.
6636 end Fully_Conformant_Expressions;
6638 ----------------------------------------
6639 -- Fully_Conformant_Discrete_Subtypes --
6640 ----------------------------------------
6642 function Fully_Conformant_Discrete_Subtypes
6643 (Given_S1 : Node_Id;
6644 Given_S2 : Node_Id) return Boolean
6646 S1 : constant Node_Id := Original_Node (Given_S1);
6647 S2 : constant Node_Id := Original_Node (Given_S2);
6649 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6650 -- Special-case for a bound given by a discriminant, which in the body
6651 -- is replaced with the discriminal of the enclosing type.
6653 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6654 -- Check both bounds
6656 -----------------------
6657 -- Conforming_Bounds --
6658 -----------------------
6660 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6662 if Is_Entity_Name (B1)
6663 and then Is_Entity_Name (B2)
6664 and then Ekind (Entity (B1)) = E_Discriminant
6666 return Chars (B1) = Chars (B2);
6669 return Fully_Conformant_Expressions (B1, B2);
6671 end Conforming_Bounds;
6673 -----------------------
6674 -- Conforming_Ranges --
6675 -----------------------
6677 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6680 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6682 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6683 end Conforming_Ranges;
6685 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6688 if Nkind (S1) /= Nkind (S2) then
6691 elsif Is_Entity_Name (S1) then
6692 return Entity (S1) = Entity (S2);
6694 elsif Nkind (S1) = N_Range then
6695 return Conforming_Ranges (S1, S2);
6697 elsif Nkind (S1) = N_Subtype_Indication then
6699 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6702 (Range_Expression (Constraint (S1)),
6703 Range_Expression (Constraint (S2)));
6707 end Fully_Conformant_Discrete_Subtypes;
6709 --------------------
6710 -- Install_Entity --
6711 --------------------
6713 procedure Install_Entity (E : Entity_Id) is
6714 Prev : constant Entity_Id := Current_Entity (E);
6716 Set_Is_Immediately_Visible (E);
6717 Set_Current_Entity (E);
6718 Set_Homonym (E, Prev);
6721 ---------------------
6722 -- Install_Formals --
6723 ---------------------
6725 procedure Install_Formals (Id : Entity_Id) is
6728 F := First_Formal (Id);
6729 while Present (F) loop
6733 end Install_Formals;
6735 -----------------------------
6736 -- Is_Interface_Conformant --
6737 -----------------------------
6739 function Is_Interface_Conformant
6740 (Tagged_Type : Entity_Id;
6741 Iface_Prim : Entity_Id;
6742 Prim : Entity_Id) return Boolean
6744 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6745 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6748 pragma Assert (Is_Subprogram (Iface_Prim)
6749 and then Is_Subprogram (Prim)
6750 and then Is_Dispatching_Operation (Iface_Prim)
6751 and then Is_Dispatching_Operation (Prim));
6753 pragma Assert (Is_Interface (Iface)
6754 or else (Present (Alias (Iface_Prim))
6757 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6759 if Prim = Iface_Prim
6760 or else not Is_Subprogram (Prim)
6761 or else Ekind (Prim) /= Ekind (Iface_Prim)
6762 or else not Is_Dispatching_Operation (Prim)
6763 or else Scope (Prim) /= Scope (Tagged_Type)
6765 or else Base_Type (Typ) /= Tagged_Type
6766 or else not Primitive_Names_Match (Iface_Prim, Prim)
6770 -- Case of a procedure, or a function that does not have a controlling
6771 -- result (I or access I).
6773 elsif Ekind (Iface_Prim) = E_Procedure
6774 or else Etype (Prim) = Etype (Iface_Prim)
6775 or else not Has_Controlling_Result (Prim)
6777 return Type_Conformant
6778 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
6780 -- Case of a function returning an interface, or an access to one.
6781 -- Check that the return types correspond.
6783 elsif Implements_Interface (Typ, Iface) then
6784 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6786 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6791 Type_Conformant (Prim, Iface_Prim,
6792 Skip_Controlling_Formals => True);
6798 end Is_Interface_Conformant;
6800 ---------------------------------
6801 -- Is_Non_Overriding_Operation --
6802 ---------------------------------
6804 function Is_Non_Overriding_Operation
6805 (Prev_E : Entity_Id;
6806 New_E : Entity_Id) return Boolean
6810 G_Typ : Entity_Id := Empty;
6812 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6813 -- If F_Type is a derived type associated with a generic actual subtype,
6814 -- then return its Generic_Parent_Type attribute, else return Empty.
6816 function Types_Correspond
6817 (P_Type : Entity_Id;
6818 N_Type : Entity_Id) return Boolean;
6819 -- Returns true if and only if the types (or designated types in the
6820 -- case of anonymous access types) are the same or N_Type is derived
6821 -- directly or indirectly from P_Type.
6823 -----------------------------
6824 -- Get_Generic_Parent_Type --
6825 -----------------------------
6827 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6832 if Is_Derived_Type (F_Typ)
6833 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6835 -- The tree must be traversed to determine the parent subtype in
6836 -- the generic unit, which unfortunately isn't always available
6837 -- via semantic attributes. ??? (Note: The use of Original_Node
6838 -- is needed for cases where a full derived type has been
6841 Indic := Subtype_Indication
6842 (Type_Definition (Original_Node (Parent (F_Typ))));
6844 if Nkind (Indic) = N_Subtype_Indication then
6845 G_Typ := Entity (Subtype_Mark (Indic));
6847 G_Typ := Entity (Indic);
6850 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6851 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6853 return Generic_Parent_Type (Parent (G_Typ));
6858 end Get_Generic_Parent_Type;
6860 ----------------------
6861 -- Types_Correspond --
6862 ----------------------
6864 function Types_Correspond
6865 (P_Type : Entity_Id;
6866 N_Type : Entity_Id) return Boolean
6868 Prev_Type : Entity_Id := Base_Type (P_Type);
6869 New_Type : Entity_Id := Base_Type (N_Type);
6872 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6873 Prev_Type := Designated_Type (Prev_Type);
6876 if Ekind (New_Type) = E_Anonymous_Access_Type then
6877 New_Type := Designated_Type (New_Type);
6880 if Prev_Type = New_Type then
6883 elsif not Is_Class_Wide_Type (New_Type) then
6884 while Etype (New_Type) /= New_Type loop
6885 New_Type := Etype (New_Type);
6886 if New_Type = Prev_Type then
6892 end Types_Correspond;
6894 -- Start of processing for Is_Non_Overriding_Operation
6897 -- In the case where both operations are implicit derived subprograms
6898 -- then neither overrides the other. This can only occur in certain
6899 -- obscure cases (e.g., derivation from homographs created in a generic
6902 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6905 elsif Ekind (Current_Scope) = E_Package
6906 and then Is_Generic_Instance (Current_Scope)
6907 and then In_Private_Part (Current_Scope)
6908 and then Comes_From_Source (New_E)
6910 -- We examine the formals and result subtype of the inherited
6911 -- operation, to determine whether their type is derived from (the
6912 -- instance of) a generic type.
6914 Formal := First_Formal (Prev_E);
6915 while Present (Formal) loop
6916 F_Typ := Base_Type (Etype (Formal));
6918 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6919 F_Typ := Designated_Type (F_Typ);
6922 G_Typ := Get_Generic_Parent_Type (F_Typ);
6924 Next_Formal (Formal);
6927 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6928 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6935 -- If the generic type is a private type, then the original operation
6936 -- was not overriding in the generic, because there was no primitive
6937 -- operation to override.
6939 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6940 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6941 N_Formal_Private_Type_Definition
6945 -- The generic parent type is the ancestor of a formal derived
6946 -- type declaration. We need to check whether it has a primitive
6947 -- operation that should be overridden by New_E in the generic.
6951 P_Formal : Entity_Id;
6952 N_Formal : Entity_Id;
6956 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6959 while Present (Prim_Elt) loop
6960 P_Prim := Node (Prim_Elt);
6962 if Chars (P_Prim) = Chars (New_E)
6963 and then Ekind (P_Prim) = Ekind (New_E)
6965 P_Formal := First_Formal (P_Prim);
6966 N_Formal := First_Formal (New_E);
6967 while Present (P_Formal) and then Present (N_Formal) loop
6968 P_Typ := Etype (P_Formal);
6969 N_Typ := Etype (N_Formal);
6971 if not Types_Correspond (P_Typ, N_Typ) then
6975 Next_Entity (P_Formal);
6976 Next_Entity (N_Formal);
6979 -- Found a matching primitive operation belonging to the
6980 -- formal ancestor type, so the new subprogram is
6984 and then No (N_Formal)
6985 and then (Ekind (New_E) /= E_Function
6988 (Etype (P_Prim), Etype (New_E)))
6994 Next_Elmt (Prim_Elt);
6997 -- If no match found, then the new subprogram does not
6998 -- override in the generic (nor in the instance).
7006 end Is_Non_Overriding_Operation;
7008 -------------------------------------
7009 -- List_Inherited_Pre_Post_Aspects --
7010 -------------------------------------
7012 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
7014 if Opt.List_Inherited_Aspects
7015 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
7018 Inherited : constant Subprogram_List :=
7019 Inherited_Subprograms (E);
7023 for J in Inherited'Range loop
7024 P := Spec_PPC_List (Inherited (J));
7025 while Present (P) loop
7026 Error_Msg_Sloc := Sloc (P);
7028 if Class_Present (P) and then not Split_PPC (P) then
7029 if Pragma_Name (P) = Name_Precondition then
7031 ("?info: & inherits `Pre''Class` aspect from #", E);
7034 ("?info: & inherits `Post''Class` aspect from #", E);
7038 P := Next_Pragma (P);
7043 end List_Inherited_Pre_Post_Aspects;
7045 ------------------------------
7046 -- Make_Inequality_Operator --
7047 ------------------------------
7049 -- S is the defining identifier of an equality operator. We build a
7050 -- subprogram declaration with the right signature. This operation is
7051 -- intrinsic, because it is always expanded as the negation of the
7052 -- call to the equality function.
7054 procedure Make_Inequality_Operator (S : Entity_Id) is
7055 Loc : constant Source_Ptr := Sloc (S);
7058 Op_Name : Entity_Id;
7060 FF : constant Entity_Id := First_Formal (S);
7061 NF : constant Entity_Id := Next_Formal (FF);
7064 -- Check that equality was properly defined, ignore call if not
7071 A : constant Entity_Id :=
7072 Make_Defining_Identifier (Sloc (FF),
7073 Chars => Chars (FF));
7075 B : constant Entity_Id :=
7076 Make_Defining_Identifier (Sloc (NF),
7077 Chars => Chars (NF));
7080 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
7082 Formals := New_List (
7083 Make_Parameter_Specification (Loc,
7084 Defining_Identifier => A,
7086 New_Reference_To (Etype (First_Formal (S)),
7087 Sloc (Etype (First_Formal (S))))),
7089 Make_Parameter_Specification (Loc,
7090 Defining_Identifier => B,
7092 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
7093 Sloc (Etype (Next_Formal (First_Formal (S)))))));
7096 Make_Subprogram_Declaration (Loc,
7098 Make_Function_Specification (Loc,
7099 Defining_Unit_Name => Op_Name,
7100 Parameter_Specifications => Formals,
7101 Result_Definition =>
7102 New_Reference_To (Standard_Boolean, Loc)));
7104 -- Insert inequality right after equality if it is explicit or after
7105 -- the derived type when implicit. These entities are created only
7106 -- for visibility purposes, and eventually replaced in the course of
7107 -- expansion, so they do not need to be attached to the tree and seen
7108 -- by the back-end. Keeping them internal also avoids spurious
7109 -- freezing problems. The declaration is inserted in the tree for
7110 -- analysis, and removed afterwards. If the equality operator comes
7111 -- from an explicit declaration, attach the inequality immediately
7112 -- after. Else the equality is inherited from a derived type
7113 -- declaration, so insert inequality after that declaration.
7115 if No (Alias (S)) then
7116 Insert_After (Unit_Declaration_Node (S), Decl);
7117 elsif Is_List_Member (Parent (S)) then
7118 Insert_After (Parent (S), Decl);
7120 Insert_After (Parent (Etype (First_Formal (S))), Decl);
7123 Mark_Rewrite_Insertion (Decl);
7124 Set_Is_Intrinsic_Subprogram (Op_Name);
7127 Set_Has_Completion (Op_Name);
7128 Set_Corresponding_Equality (Op_Name, S);
7129 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
7131 end Make_Inequality_Operator;
7133 ----------------------
7134 -- May_Need_Actuals --
7135 ----------------------
7137 procedure May_Need_Actuals (Fun : Entity_Id) is
7142 F := First_Formal (Fun);
7144 while Present (F) loop
7145 if No (Default_Value (F)) then
7153 Set_Needs_No_Actuals (Fun, B);
7154 end May_Need_Actuals;
7156 ---------------------
7157 -- Mode_Conformant --
7158 ---------------------
7160 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7163 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
7165 end Mode_Conformant;
7167 ---------------------------
7168 -- New_Overloaded_Entity --
7169 ---------------------------
7171 procedure New_Overloaded_Entity
7173 Derived_Type : Entity_Id := Empty)
7175 Overridden_Subp : Entity_Id := Empty;
7176 -- Set if the current scope has an operation that is type-conformant
7177 -- with S, and becomes hidden by S.
7179 Is_Primitive_Subp : Boolean;
7180 -- Set to True if the new subprogram is primitive
7183 -- Entity that S overrides
7185 Prev_Vis : Entity_Id := Empty;
7186 -- Predecessor of E in Homonym chain
7188 procedure Check_For_Primitive_Subprogram
7189 (Is_Primitive : out Boolean;
7190 Is_Overriding : Boolean := False);
7191 -- If the subprogram being analyzed is a primitive operation of the type
7192 -- of a formal or result, set the Has_Primitive_Operations flag on the
7193 -- type, and set Is_Primitive to True (otherwise set to False). Set the
7194 -- corresponding flag on the entity itself for later use.
7196 procedure Check_Synchronized_Overriding
7197 (Def_Id : Entity_Id;
7198 Overridden_Subp : out Entity_Id);
7199 -- First determine if Def_Id is an entry or a subprogram either defined
7200 -- in the scope of a task or protected type, or is a primitive of such
7201 -- a type. Check whether Def_Id overrides a subprogram of an interface
7202 -- implemented by the synchronized type, return the overridden entity
7205 function Is_Private_Declaration (E : Entity_Id) return Boolean;
7206 -- Check that E is declared in the private part of the current package,
7207 -- or in the package body, where it may hide a previous declaration.
7208 -- We can't use In_Private_Part by itself because this flag is also
7209 -- set when freezing entities, so we must examine the place of the
7210 -- declaration in the tree, and recognize wrapper packages as well.
7212 function Is_Overriding_Alias
7214 New_E : Entity_Id) return Boolean;
7215 -- Check whether new subprogram and old subprogram are both inherited
7216 -- from subprograms that have distinct dispatch table entries. This can
7217 -- occur with derivations from instances with accidental homonyms.
7218 -- The function is conservative given that the converse is only true
7219 -- within instances that contain accidental overloadings.
7221 ------------------------------------
7222 -- Check_For_Primitive_Subprogram --
7223 ------------------------------------
7225 procedure Check_For_Primitive_Subprogram
7226 (Is_Primitive : out Boolean;
7227 Is_Overriding : Boolean := False)
7233 function Visible_Part_Type (T : Entity_Id) return Boolean;
7234 -- Returns true if T is declared in the visible part of the current
7235 -- package scope; otherwise returns false. Assumes that T is declared
7238 procedure Check_Private_Overriding (T : Entity_Id);
7239 -- Checks that if a primitive abstract subprogram of a visible
7240 -- abstract type is declared in a private part, then it must override
7241 -- an abstract subprogram declared in the visible part. Also checks
7242 -- that if a primitive function with a controlling result is declared
7243 -- in a private part, then it must override a function declared in
7244 -- the visible part.
7246 ------------------------------
7247 -- Check_Private_Overriding --
7248 ------------------------------
7250 procedure Check_Private_Overriding (T : Entity_Id) is
7252 if Is_Package_Or_Generic_Package (Current_Scope)
7253 and then In_Private_Part (Current_Scope)
7254 and then Visible_Part_Type (T)
7255 and then not In_Instance
7257 if Is_Abstract_Type (T)
7258 and then Is_Abstract_Subprogram (S)
7259 and then (not Is_Overriding
7260 or else not Is_Abstract_Subprogram (E))
7263 ("abstract subprograms must be visible "
7264 & "(RM 3.9.3(10))!", S);
7266 elsif Ekind (S) = E_Function
7267 and then not Is_Overriding
7269 if Is_Tagged_Type (T)
7270 and then T = Base_Type (Etype (S))
7273 ("private function with tagged result must"
7274 & " override visible-part function", S);
7276 ("\move subprogram to the visible part"
7277 & " (RM 3.9.3(10))", S);
7279 -- AI05-0073: extend this test to the case of a function
7280 -- with a controlling access result.
7282 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
7283 and then Is_Tagged_Type (Designated_Type (Etype (S)))
7285 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
7286 and then Ada_Version >= Ada_2012
7289 ("private function with controlling access result "
7290 & "must override visible-part function", S);
7292 ("\move subprogram to the visible part"
7293 & " (RM 3.9.3(10))", S);
7297 end Check_Private_Overriding;
7299 -----------------------
7300 -- Visible_Part_Type --
7301 -----------------------
7303 function Visible_Part_Type (T : Entity_Id) return Boolean is
7304 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7308 -- If the entity is a private type, then it must be declared in a
7311 if Ekind (T) in Private_Kind then
7315 -- Otherwise, we traverse the visible part looking for its
7316 -- corresponding declaration. We cannot use the declaration
7317 -- node directly because in the private part the entity of a
7318 -- private type is the one in the full view, which does not
7319 -- indicate that it is the completion of something visible.
7321 N := First (Visible_Declarations (Specification (P)));
7322 while Present (N) loop
7323 if Nkind (N) = N_Full_Type_Declaration
7324 and then Present (Defining_Identifier (N))
7325 and then T = Defining_Identifier (N)
7329 elsif Nkind_In (N, N_Private_Type_Declaration,
7330 N_Private_Extension_Declaration)
7331 and then Present (Defining_Identifier (N))
7332 and then T = Full_View (Defining_Identifier (N))
7341 end Visible_Part_Type;
7343 -- Start of processing for Check_For_Primitive_Subprogram
7346 Is_Primitive := False;
7348 if not Comes_From_Source (S) then
7351 -- If subprogram is at library level, it is not primitive operation
7353 elsif Current_Scope = Standard_Standard then
7356 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7357 and then not In_Package_Body (Current_Scope))
7358 or else Is_Overriding
7360 -- For function, check return type
7362 if Ekind (S) = E_Function then
7363 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7364 F_Typ := Designated_Type (Etype (S));
7369 B_Typ := Base_Type (F_Typ);
7371 if Scope (B_Typ) = Current_Scope
7372 and then not Is_Class_Wide_Type (B_Typ)
7373 and then not Is_Generic_Type (B_Typ)
7375 Is_Primitive := True;
7376 Set_Has_Primitive_Operations (B_Typ);
7377 Set_Is_Primitive (S);
7378 Check_Private_Overriding (B_Typ);
7382 -- For all subprograms, check formals
7384 Formal := First_Formal (S);
7385 while Present (Formal) loop
7386 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7387 F_Typ := Designated_Type (Etype (Formal));
7389 F_Typ := Etype (Formal);
7392 B_Typ := Base_Type (F_Typ);
7394 if Ekind (B_Typ) = E_Access_Subtype then
7395 B_Typ := Base_Type (B_Typ);
7398 if Scope (B_Typ) = Current_Scope
7399 and then not Is_Class_Wide_Type (B_Typ)
7400 and then not Is_Generic_Type (B_Typ)
7402 Is_Primitive := True;
7403 Set_Is_Primitive (S);
7404 Set_Has_Primitive_Operations (B_Typ);
7405 Check_Private_Overriding (B_Typ);
7408 Next_Formal (Formal);
7411 end Check_For_Primitive_Subprogram;
7413 -----------------------------------
7414 -- Check_Synchronized_Overriding --
7415 -----------------------------------
7417 procedure Check_Synchronized_Overriding
7418 (Def_Id : Entity_Id;
7419 Overridden_Subp : out Entity_Id)
7421 Ifaces_List : Elist_Id;
7425 function Matches_Prefixed_View_Profile
7426 (Prim_Params : List_Id;
7427 Iface_Params : List_Id) return Boolean;
7428 -- Determine whether a subprogram's parameter profile Prim_Params
7429 -- matches that of a potentially overridden interface subprogram
7430 -- Iface_Params. Also determine if the type of first parameter of
7431 -- Iface_Params is an implemented interface.
7433 -----------------------------------
7434 -- Matches_Prefixed_View_Profile --
7435 -----------------------------------
7437 function Matches_Prefixed_View_Profile
7438 (Prim_Params : List_Id;
7439 Iface_Params : List_Id) return Boolean
7441 Iface_Id : Entity_Id;
7442 Iface_Param : Node_Id;
7443 Iface_Typ : Entity_Id;
7444 Prim_Id : Entity_Id;
7445 Prim_Param : Node_Id;
7446 Prim_Typ : Entity_Id;
7448 function Is_Implemented
7449 (Ifaces_List : Elist_Id;
7450 Iface : Entity_Id) return Boolean;
7451 -- Determine if Iface is implemented by the current task or
7454 --------------------
7455 -- Is_Implemented --
7456 --------------------
7458 function Is_Implemented
7459 (Ifaces_List : Elist_Id;
7460 Iface : Entity_Id) return Boolean
7462 Iface_Elmt : Elmt_Id;
7465 Iface_Elmt := First_Elmt (Ifaces_List);
7466 while Present (Iface_Elmt) loop
7467 if Node (Iface_Elmt) = Iface then
7471 Next_Elmt (Iface_Elmt);
7477 -- Start of processing for Matches_Prefixed_View_Profile
7480 Iface_Param := First (Iface_Params);
7481 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7483 if Is_Access_Type (Iface_Typ) then
7484 Iface_Typ := Designated_Type (Iface_Typ);
7487 Prim_Param := First (Prim_Params);
7489 -- The first parameter of the potentially overridden subprogram
7490 -- must be an interface implemented by Prim.
7492 if not Is_Interface (Iface_Typ)
7493 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7498 -- The checks on the object parameters are done, move onto the
7499 -- rest of the parameters.
7501 if not In_Scope then
7502 Prim_Param := Next (Prim_Param);
7505 Iface_Param := Next (Iface_Param);
7506 while Present (Iface_Param) and then Present (Prim_Param) loop
7507 Iface_Id := Defining_Identifier (Iface_Param);
7508 Iface_Typ := Find_Parameter_Type (Iface_Param);
7510 Prim_Id := Defining_Identifier (Prim_Param);
7511 Prim_Typ := Find_Parameter_Type (Prim_Param);
7513 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7514 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7515 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7517 Iface_Typ := Designated_Type (Iface_Typ);
7518 Prim_Typ := Designated_Type (Prim_Typ);
7521 -- Case of multiple interface types inside a parameter profile
7523 -- (Obj_Param : in out Iface; ...; Param : Iface)
7525 -- If the interface type is implemented, then the matching type
7526 -- in the primitive should be the implementing record type.
7528 if Ekind (Iface_Typ) = E_Record_Type
7529 and then Is_Interface (Iface_Typ)
7530 and then Is_Implemented (Ifaces_List, Iface_Typ)
7532 if Prim_Typ /= Typ then
7536 -- The two parameters must be both mode and subtype conformant
7538 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7540 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7549 -- One of the two lists contains more parameters than the other
7551 if Present (Iface_Param) or else Present (Prim_Param) then
7556 end Matches_Prefixed_View_Profile;
7558 -- Start of processing for Check_Synchronized_Overriding
7561 Overridden_Subp := Empty;
7563 -- Def_Id must be an entry or a subprogram. We should skip predefined
7564 -- primitives internally generated by the frontend; however at this
7565 -- stage predefined primitives are still not fully decorated. As a
7566 -- minor optimization we skip here internally generated subprograms.
7568 if (Ekind (Def_Id) /= E_Entry
7569 and then Ekind (Def_Id) /= E_Function
7570 and then Ekind (Def_Id) /= E_Procedure)
7571 or else not Comes_From_Source (Def_Id)
7576 -- Search for the concurrent declaration since it contains the list
7577 -- of all implemented interfaces. In this case, the subprogram is
7578 -- declared within the scope of a protected or a task type.
7580 if Present (Scope (Def_Id))
7581 and then Is_Concurrent_Type (Scope (Def_Id))
7582 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7584 Typ := Scope (Def_Id);
7587 -- The enclosing scope is not a synchronized type and the subprogram
7590 elsif No (First_Formal (Def_Id)) then
7593 -- The subprogram has formals and hence it may be a primitive of a
7597 Typ := Etype (First_Formal (Def_Id));
7599 if Is_Access_Type (Typ) then
7600 Typ := Directly_Designated_Type (Typ);
7603 if Is_Concurrent_Type (Typ)
7604 and then not Is_Generic_Actual_Type (Typ)
7608 -- This case occurs when the concurrent type is declared within
7609 -- a generic unit. As a result the corresponding record has been
7610 -- built and used as the type of the first formal, we just have
7611 -- to retrieve the corresponding concurrent type.
7613 elsif Is_Concurrent_Record_Type (Typ)
7614 and then Present (Corresponding_Concurrent_Type (Typ))
7616 Typ := Corresponding_Concurrent_Type (Typ);
7624 -- There is no overriding to check if is an inherited operation in a
7625 -- type derivation on for a generic actual.
7627 Collect_Interfaces (Typ, Ifaces_List);
7629 if Is_Empty_Elmt_List (Ifaces_List) then
7633 -- Determine whether entry or subprogram Def_Id overrides a primitive
7634 -- operation that belongs to one of the interfaces in Ifaces_List.
7637 Candidate : Entity_Id := Empty;
7638 Hom : Entity_Id := Empty;
7639 Iface_Typ : Entity_Id;
7640 Subp : Entity_Id := Empty;
7643 -- Traverse the homonym chain, looking for a potentially
7644 -- overridden subprogram that belongs to an implemented
7647 Hom := Current_Entity_In_Scope (Def_Id);
7648 while Present (Hom) loop
7652 or else not Is_Overloadable (Subp)
7653 or else not Is_Primitive (Subp)
7654 or else not Is_Dispatching_Operation (Subp)
7655 or else not Present (Find_Dispatching_Type (Subp))
7656 or else not Is_Interface (Find_Dispatching_Type (Subp))
7660 -- Entries and procedures can override abstract or null
7661 -- interface procedures.
7663 elsif (Ekind (Def_Id) = E_Procedure
7664 or else Ekind (Def_Id) = E_Entry)
7665 and then Ekind (Subp) = E_Procedure
7666 and then Matches_Prefixed_View_Profile
7667 (Parameter_Specifications (Parent (Def_Id)),
7668 Parameter_Specifications (Parent (Subp)))
7672 -- For an overridden subprogram Subp, check whether the mode
7673 -- of its first parameter is correct depending on the kind
7674 -- of synchronized type.
7677 Formal : constant Node_Id := First_Formal (Candidate);
7680 -- In order for an entry or a protected procedure to
7681 -- override, the first parameter of the overridden
7682 -- routine must be of mode "out", "in out" or
7683 -- access-to-variable.
7685 if (Ekind (Candidate) = E_Entry
7686 or else Ekind (Candidate) = E_Procedure)
7687 and then Is_Protected_Type (Typ)
7688 and then Ekind (Formal) /= E_In_Out_Parameter
7689 and then Ekind (Formal) /= E_Out_Parameter
7690 and then Nkind (Parameter_Type (Parent (Formal)))
7691 /= N_Access_Definition
7695 -- All other cases are OK since a task entry or routine
7696 -- does not have a restriction on the mode of the first
7697 -- parameter of the overridden interface routine.
7700 Overridden_Subp := Candidate;
7705 -- Functions can override abstract interface functions
7707 elsif Ekind (Def_Id) = E_Function
7708 and then Ekind (Subp) = E_Function
7709 and then Matches_Prefixed_View_Profile
7710 (Parameter_Specifications (Parent (Def_Id)),
7711 Parameter_Specifications (Parent (Subp)))
7712 and then Etype (Result_Definition (Parent (Def_Id))) =
7713 Etype (Result_Definition (Parent (Subp)))
7715 Overridden_Subp := Subp;
7719 Hom := Homonym (Hom);
7722 -- After examining all candidates for overriding, we are left with
7723 -- the best match which is a mode incompatible interface routine.
7724 -- Do not emit an error if the Expander is active since this error
7725 -- will be detected later on after all concurrent types are
7726 -- expanded and all wrappers are built. This check is meant for
7727 -- spec-only compilations.
7729 if Present (Candidate) and then not Expander_Active then
7731 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7733 -- Def_Id is primitive of a protected type, declared inside the
7734 -- type, and the candidate is primitive of a limited or
7735 -- synchronized interface.
7738 and then Is_Protected_Type (Typ)
7740 (Is_Limited_Interface (Iface_Typ)
7741 or else Is_Protected_Interface (Iface_Typ)
7742 or else Is_Synchronized_Interface (Iface_Typ)
7743 or else Is_Task_Interface (Iface_Typ))
7746 ("first formal of & must be of mode `OUT`, `IN OUT`"
7747 & " or access-to-variable", Typ, Candidate);
7749 ("\in order to be overridden by protected procedure or "
7750 & "entry (RM 9.4(11.9/2))", Typ);
7754 Overridden_Subp := Candidate;
7757 end Check_Synchronized_Overriding;
7759 ----------------------------
7760 -- Is_Private_Declaration --
7761 ----------------------------
7763 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7764 Priv_Decls : List_Id;
7765 Decl : constant Node_Id := Unit_Declaration_Node (E);
7768 if Is_Package_Or_Generic_Package (Current_Scope)
7769 and then In_Private_Part (Current_Scope)
7772 Private_Declarations (
7773 Specification (Unit_Declaration_Node (Current_Scope)));
7775 return In_Package_Body (Current_Scope)
7777 (Is_List_Member (Decl)
7778 and then List_Containing (Decl) = Priv_Decls)
7779 or else (Nkind (Parent (Decl)) = N_Package_Specification
7782 (Defining_Entity (Parent (Decl)))
7783 and then List_Containing (Parent (Parent (Decl)))
7788 end Is_Private_Declaration;
7790 --------------------------
7791 -- Is_Overriding_Alias --
7792 --------------------------
7794 function Is_Overriding_Alias
7796 New_E : Entity_Id) return Boolean
7798 AO : constant Entity_Id := Alias (Old_E);
7799 AN : constant Entity_Id := Alias (New_E);
7802 return Scope (AO) /= Scope (AN)
7803 or else No (DTC_Entity (AO))
7804 or else No (DTC_Entity (AN))
7805 or else DT_Position (AO) = DT_Position (AN);
7806 end Is_Overriding_Alias;
7808 -- Start of processing for New_Overloaded_Entity
7811 -- We need to look for an entity that S may override. This must be a
7812 -- homonym in the current scope, so we look for the first homonym of
7813 -- S in the current scope as the starting point for the search.
7815 E := Current_Entity_In_Scope (S);
7817 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
7818 -- They are directly added to the list of primitive operations of
7819 -- Derived_Type, unless this is a rederivation in the private part
7820 -- of an operation that was already derived in the visible part of
7821 -- the current package.
7823 if Ada_Version >= Ada_2005
7824 and then Present (Derived_Type)
7825 and then Present (Alias (S))
7826 and then Is_Dispatching_Operation (Alias (S))
7827 and then Present (Find_Dispatching_Type (Alias (S)))
7828 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7830 -- For private types, when the full-view is processed we propagate to
7831 -- the full view the non-overridden entities whose attribute "alias"
7832 -- references an interface primitive. These entities were added by
7833 -- Derive_Subprograms to ensure that interface primitives are
7836 -- Inside_Freeze_Actions is non zero when S corresponds with an
7837 -- internal entity that links an interface primitive with its
7838 -- covering primitive through attribute Interface_Alias (see
7839 -- Add_Internal_Interface_Entities).
7841 if Inside_Freezing_Actions = 0
7842 and then Is_Package_Or_Generic_Package (Current_Scope)
7843 and then In_Private_Part (Current_Scope)
7844 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
7845 and then Nkind (Parent (S)) = N_Full_Type_Declaration
7846 and then Full_View (Defining_Identifier (Parent (E)))
7847 = Defining_Identifier (Parent (S))
7848 and then Alias (E) = Alias (S)
7850 Check_Operation_From_Private_View (S, E);
7851 Set_Is_Dispatching_Operation (S);
7856 Enter_Overloaded_Entity (S);
7857 Check_Dispatching_Operation (S, Empty);
7858 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7864 -- If there is no homonym then this is definitely not overriding
7867 Enter_Overloaded_Entity (S);
7868 Check_Dispatching_Operation (S, Empty);
7869 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7871 -- If subprogram has an explicit declaration, check whether it
7872 -- has an overriding indicator.
7874 if Comes_From_Source (S) then
7875 Check_Synchronized_Overriding (S, Overridden_Subp);
7877 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
7878 -- it may have overridden some hidden inherited primitive. Update
7879 -- Overridden_Subp to avoid spurious errors when checking the
7880 -- overriding indicator.
7882 if Ada_Version >= Ada_2012
7883 and then No (Overridden_Subp)
7884 and then Is_Dispatching_Operation (S)
7885 and then Present (Overridden_Operation (S))
7887 Overridden_Subp := Overridden_Operation (S);
7890 Check_Overriding_Indicator
7891 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7894 -- If there is a homonym that is not overloadable, then we have an
7895 -- error, except for the special cases checked explicitly below.
7897 elsif not Is_Overloadable (E) then
7899 -- Check for spurious conflict produced by a subprogram that has the
7900 -- same name as that of the enclosing generic package. The conflict
7901 -- occurs within an instance, between the subprogram and the renaming
7902 -- declaration for the package. After the subprogram, the package
7903 -- renaming declaration becomes hidden.
7905 if Ekind (E) = E_Package
7906 and then Present (Renamed_Object (E))
7907 and then Renamed_Object (E) = Current_Scope
7908 and then Nkind (Parent (Renamed_Object (E))) =
7909 N_Package_Specification
7910 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7913 Set_Is_Immediately_Visible (E, False);
7914 Enter_Overloaded_Entity (S);
7915 Set_Homonym (S, Homonym (E));
7916 Check_Dispatching_Operation (S, Empty);
7917 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7919 -- If the subprogram is implicit it is hidden by the previous
7920 -- declaration. However if it is dispatching, it must appear in the
7921 -- dispatch table anyway, because it can be dispatched to even if it
7922 -- cannot be called directly.
7924 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
7925 Set_Scope (S, Current_Scope);
7927 if Is_Dispatching_Operation (Alias (S)) then
7928 Check_Dispatching_Operation (S, Empty);
7934 Error_Msg_Sloc := Sloc (E);
7936 -- Generate message, with useful additional warning if in generic
7938 if Is_Generic_Unit (E) then
7939 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7940 Error_Msg_N ("\& conflicts with declaration#", S);
7942 Error_Msg_N ("& conflicts with declaration#", S);
7948 -- E exists and is overloadable
7951 Check_Synchronized_Overriding (S, Overridden_Subp);
7953 -- Loop through E and its homonyms to determine if any of them is
7954 -- the candidate for overriding by S.
7956 while Present (E) loop
7958 -- Definitely not interesting if not in the current scope
7960 if Scope (E) /= Current_Scope then
7963 -- Check if we have type conformance
7965 elsif Type_Conformant (E, S) then
7967 -- If the old and new entities have the same profile and one
7968 -- is not the body of the other, then this is an error, unless
7969 -- one of them is implicitly declared.
7971 -- There are some cases when both can be implicit, for example
7972 -- when both a literal and a function that overrides it are
7973 -- inherited in a derivation, or when an inherited operation
7974 -- of a tagged full type overrides the inherited operation of
7975 -- a private extension. Ada 83 had a special rule for the
7976 -- literal case. In Ada95, the later implicit operation hides
7977 -- the former, and the literal is always the former. In the
7978 -- odd case where both are derived operations declared at the
7979 -- same point, both operations should be declared, and in that
7980 -- case we bypass the following test and proceed to the next
7981 -- part. This can only occur for certain obscure cases in
7982 -- instances, when an operation on a type derived from a formal
7983 -- private type does not override a homograph inherited from
7984 -- the actual. In subsequent derivations of such a type, the
7985 -- DT positions of these operations remain distinct, if they
7988 if Present (Alias (S))
7989 and then (No (Alias (E))
7990 or else Comes_From_Source (E)
7991 or else Is_Abstract_Subprogram (S)
7993 (Is_Dispatching_Operation (E)
7994 and then Is_Overriding_Alias (E, S)))
7995 and then Ekind (E) /= E_Enumeration_Literal
7997 -- When an derived operation is overloaded it may be due to
7998 -- the fact that the full view of a private extension
7999 -- re-inherits. It has to be dealt with.
8001 if Is_Package_Or_Generic_Package (Current_Scope)
8002 and then In_Private_Part (Current_Scope)
8004 Check_Operation_From_Private_View (S, E);
8007 -- In any case the implicit operation remains hidden by the
8008 -- existing declaration, which is overriding. Indicate that
8009 -- E overrides the operation from which S is inherited.
8011 if Present (Alias (S)) then
8012 Set_Overridden_Operation (E, Alias (S));
8014 Set_Overridden_Operation (E, S);
8017 if Comes_From_Source (E) then
8018 Check_Overriding_Indicator (E, S, Is_Primitive => False);
8023 -- Within an instance, the renaming declarations for actual
8024 -- subprograms may become ambiguous, but they do not hide each
8027 elsif Ekind (E) /= E_Entry
8028 and then not Comes_From_Source (E)
8029 and then not Is_Generic_Instance (E)
8030 and then (Present (Alias (E))
8031 or else Is_Intrinsic_Subprogram (E))
8032 and then (not In_Instance
8033 or else No (Parent (E))
8034 or else Nkind (Unit_Declaration_Node (E)) /=
8035 N_Subprogram_Renaming_Declaration)
8037 -- A subprogram child unit is not allowed to override an
8038 -- inherited subprogram (10.1.1(20)).
8040 if Is_Child_Unit (S) then
8042 ("child unit overrides inherited subprogram in parent",
8047 if Is_Non_Overriding_Operation (E, S) then
8048 Enter_Overloaded_Entity (S);
8050 if No (Derived_Type)
8051 or else Is_Tagged_Type (Derived_Type)
8053 Check_Dispatching_Operation (S, Empty);
8059 -- E is a derived operation or an internal operator which
8060 -- is being overridden. Remove E from further visibility.
8061 -- Furthermore, if E is a dispatching operation, it must be
8062 -- replaced in the list of primitive operations of its type
8063 -- (see Override_Dispatching_Operation).
8065 Overridden_Subp := E;
8071 Prev := First_Entity (Current_Scope);
8072 while Present (Prev)
8073 and then Next_Entity (Prev) /= E
8078 -- It is possible for E to be in the current scope and
8079 -- yet not in the entity chain. This can only occur in a
8080 -- generic context where E is an implicit concatenation
8081 -- in the formal part, because in a generic body the
8082 -- entity chain starts with the formals.
8085 (Present (Prev) or else Chars (E) = Name_Op_Concat);
8087 -- E must be removed both from the entity_list of the
8088 -- current scope, and from the visibility chain
8090 if Debug_Flag_E then
8091 Write_Str ("Override implicit operation ");
8092 Write_Int (Int (E));
8096 -- If E is a predefined concatenation, it stands for four
8097 -- different operations. As a result, a single explicit
8098 -- declaration does not hide it. In a possible ambiguous
8099 -- situation, Disambiguate chooses the user-defined op,
8100 -- so it is correct to retain the previous internal one.
8102 if Chars (E) /= Name_Op_Concat
8103 or else Ekind (E) /= E_Operator
8105 -- For nondispatching derived operations that are
8106 -- overridden by a subprogram declared in the private
8107 -- part of a package, we retain the derived subprogram
8108 -- but mark it as not immediately visible. If the
8109 -- derived operation was declared in the visible part
8110 -- then this ensures that it will still be visible
8111 -- outside the package with the proper signature
8112 -- (calls from outside must also be directed to this
8113 -- version rather than the overriding one, unlike the
8114 -- dispatching case). Calls from inside the package
8115 -- will still resolve to the overriding subprogram
8116 -- since the derived one is marked as not visible
8117 -- within the package.
8119 -- If the private operation is dispatching, we achieve
8120 -- the overriding by keeping the implicit operation
8121 -- but setting its alias to be the overriding one. In
8122 -- this fashion the proper body is executed in all
8123 -- cases, but the original signature is used outside
8126 -- If the overriding is not in the private part, we
8127 -- remove the implicit operation altogether.
8129 if Is_Private_Declaration (S) then
8130 if not Is_Dispatching_Operation (E) then
8131 Set_Is_Immediately_Visible (E, False);
8133 -- Work done in Override_Dispatching_Operation,
8134 -- so nothing else need to be done here.
8140 -- Find predecessor of E in Homonym chain
8142 if E = Current_Entity (E) then
8145 Prev_Vis := Current_Entity (E);
8146 while Homonym (Prev_Vis) /= E loop
8147 Prev_Vis := Homonym (Prev_Vis);
8151 if Prev_Vis /= Empty then
8153 -- Skip E in the visibility chain
8155 Set_Homonym (Prev_Vis, Homonym (E));
8158 Set_Name_Entity_Id (Chars (E), Homonym (E));
8161 Set_Next_Entity (Prev, Next_Entity (E));
8163 if No (Next_Entity (Prev)) then
8164 Set_Last_Entity (Current_Scope, Prev);
8169 Enter_Overloaded_Entity (S);
8171 -- For entities generated by Derive_Subprograms the
8172 -- overridden operation is the inherited primitive
8173 -- (which is available through the attribute alias).
8175 if not (Comes_From_Source (E))
8176 and then Is_Dispatching_Operation (E)
8177 and then Find_Dispatching_Type (E) =
8178 Find_Dispatching_Type (S)
8179 and then Present (Alias (E))
8180 and then Comes_From_Source (Alias (E))
8182 Set_Overridden_Operation (S, Alias (E));
8184 Set_Overridden_Operation (S, E);
8187 Check_Overriding_Indicator (S, E, Is_Primitive => True);
8189 -- If S is a user-defined subprogram or a null procedure
8190 -- expanded to override an inherited null procedure, or a
8191 -- predefined dispatching primitive then indicate that E
8192 -- overrides the operation from which S is inherited.
8194 if Comes_From_Source (S)
8196 (Present (Parent (S))
8198 Nkind (Parent (S)) = N_Procedure_Specification
8200 Null_Present (Parent (S)))
8202 (Present (Alias (E))
8204 Is_Predefined_Dispatching_Operation (Alias (E)))
8206 if Present (Alias (E)) then
8207 Set_Overridden_Operation (S, Alias (E));
8211 if Is_Dispatching_Operation (E) then
8213 -- An overriding dispatching subprogram inherits the
8214 -- convention of the overridden subprogram (AI-117).
8216 Set_Convention (S, Convention (E));
8217 Check_Dispatching_Operation (S, E);
8220 Check_Dispatching_Operation (S, Empty);
8223 Check_For_Primitive_Subprogram
8224 (Is_Primitive_Subp, Is_Overriding => True);
8225 goto Check_Inequality;
8228 -- Apparent redeclarations in instances can occur when two
8229 -- formal types get the same actual type. The subprograms in
8230 -- in the instance are legal, even if not callable from the
8231 -- outside. Calls from within are disambiguated elsewhere.
8232 -- For dispatching operations in the visible part, the usual
8233 -- rules apply, and operations with the same profile are not
8236 elsif (In_Instance_Visible_Part
8237 and then not Is_Dispatching_Operation (E))
8238 or else In_Instance_Not_Visible
8242 -- Here we have a real error (identical profile)
8245 Error_Msg_Sloc := Sloc (E);
8247 -- Avoid cascaded errors if the entity appears in
8248 -- subsequent calls.
8250 Set_Scope (S, Current_Scope);
8252 -- Generate error, with extra useful warning for the case
8253 -- of a generic instance with no completion.
8255 if Is_Generic_Instance (S)
8256 and then not Has_Completion (E)
8259 ("instantiation cannot provide body for&", S);
8260 Error_Msg_N ("\& conflicts with declaration#", S);
8262 Error_Msg_N ("& conflicts with declaration#", S);
8269 -- If one subprogram has an access parameter and the other
8270 -- a parameter of an access type, calls to either might be
8271 -- ambiguous. Verify that parameters match except for the
8272 -- access parameter.
8274 if May_Hide_Profile then
8280 F1 := First_Formal (S);
8281 F2 := First_Formal (E);
8282 while Present (F1) and then Present (F2) loop
8283 if Is_Access_Type (Etype (F1)) then
8284 if not Is_Access_Type (Etype (F2))
8285 or else not Conforming_Types
8286 (Designated_Type (Etype (F1)),
8287 Designated_Type (Etype (F2)),
8290 May_Hide_Profile := False;
8294 not Conforming_Types
8295 (Etype (F1), Etype (F2), Type_Conformant)
8297 May_Hide_Profile := False;
8308 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
8317 -- On exit, we know that S is a new entity
8319 Enter_Overloaded_Entity (S);
8320 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8321 Check_Overriding_Indicator
8322 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8324 -- If S is a derived operation for an untagged type then by
8325 -- definition it's not a dispatching operation (even if the parent
8326 -- operation was dispatching), so we don't call
8327 -- Check_Dispatching_Operation in that case.
8329 if No (Derived_Type)
8330 or else Is_Tagged_Type (Derived_Type)
8332 Check_Dispatching_Operation (S, Empty);
8336 -- If this is a user-defined equality operator that is not a derived
8337 -- subprogram, create the corresponding inequality. If the operation is
8338 -- dispatching, the expansion is done elsewhere, and we do not create
8339 -- an explicit inequality operation.
8341 <<Check_Inequality>>
8342 if Chars (S) = Name_Op_Eq
8343 and then Etype (S) = Standard_Boolean
8344 and then Present (Parent (S))
8345 and then not Is_Dispatching_Operation (S)
8347 Make_Inequality_Operator (S);
8349 if Ada_Version >= Ada_2012 then
8350 Check_Untagged_Equality (S);
8353 end New_Overloaded_Entity;
8355 ---------------------
8356 -- Process_Formals --
8357 ---------------------
8359 procedure Process_Formals
8361 Related_Nod : Node_Id)
8363 Param_Spec : Node_Id;
8365 Formal_Type : Entity_Id;
8369 Num_Out_Params : Nat := 0;
8370 First_Out_Param : Entity_Id := Empty;
8371 -- Used for setting Is_Only_Out_Parameter
8373 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8374 -- Determine whether an access type designates a type coming from a
8377 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8378 -- Check whether the default has a class-wide type. After analysis the
8379 -- default has the type of the formal, so we must also check explicitly
8380 -- for an access attribute.
8382 -------------------------------
8383 -- Designates_From_With_Type --
8384 -------------------------------
8386 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8387 Desig : Entity_Id := Typ;
8390 if Is_Access_Type (Desig) then
8391 Desig := Directly_Designated_Type (Desig);
8394 if Is_Class_Wide_Type (Desig) then
8395 Desig := Root_Type (Desig);
8399 Ekind (Desig) = E_Incomplete_Type
8400 and then From_With_Type (Desig);
8401 end Designates_From_With_Type;
8403 ---------------------------
8404 -- Is_Class_Wide_Default --
8405 ---------------------------
8407 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8409 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8410 or else (Nkind (D) = N_Attribute_Reference
8411 and then Attribute_Name (D) = Name_Access
8412 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8413 end Is_Class_Wide_Default;
8415 -- Start of processing for Process_Formals
8418 -- In order to prevent premature use of the formals in the same formal
8419 -- part, the Ekind is left undefined until all default expressions are
8420 -- analyzed. The Ekind is established in a separate loop at the end.
8422 Param_Spec := First (T);
8423 while Present (Param_Spec) loop
8424 Formal := Defining_Identifier (Param_Spec);
8425 Set_Never_Set_In_Source (Formal, True);
8426 Enter_Name (Formal);
8428 -- Case of ordinary parameters
8430 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8431 Find_Type (Parameter_Type (Param_Spec));
8432 Ptype := Parameter_Type (Param_Spec);
8434 if Ptype = Error then
8438 Formal_Type := Entity (Ptype);
8440 if Is_Incomplete_Type (Formal_Type)
8442 (Is_Class_Wide_Type (Formal_Type)
8443 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8445 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8446 -- primitive operations, as long as their completion is
8447 -- in the same declarative part. If in the private part
8448 -- this means that the type cannot be a Taft-amendment type.
8449 -- Check is done on package exit. For access to subprograms,
8450 -- the use is legal for Taft-amendment types.
8452 if Is_Tagged_Type (Formal_Type) then
8453 if Ekind (Scope (Current_Scope)) = E_Package
8454 and then In_Private_Part (Scope (Current_Scope))
8455 and then not From_With_Type (Formal_Type)
8456 and then not Is_Class_Wide_Type (Formal_Type)
8459 (Parent (T), N_Access_Function_Definition,
8460 N_Access_Procedure_Definition)
8464 Private_Dependents (Base_Type (Formal_Type)));
8468 -- Special handling of Value_Type for CIL case
8470 elsif Is_Value_Type (Formal_Type) then
8473 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8474 N_Access_Procedure_Definition)
8477 -- AI05-0151: Tagged incomplete types are allowed in all
8478 -- formal parts. Untagged incomplete types are not allowed
8481 if Ada_Version >= Ada_2012 then
8482 if Is_Tagged_Type (Formal_Type) then
8485 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
8490 ("invalid use of untagged incomplete type&",
8491 Ptype, Formal_Type);
8496 ("invalid use of incomplete type&",
8497 Param_Spec, Formal_Type);
8499 -- Further checks on the legality of incomplete types
8500 -- in formal parts are delayed until the freeze point
8501 -- of the enclosing subprogram or access to subprogram.
8505 elsif Ekind (Formal_Type) = E_Void then
8507 ("premature use of&",
8508 Parameter_Type (Param_Spec), Formal_Type);
8511 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8512 -- declaration corresponding to the null-excluding type of the
8513 -- formal in the enclosing scope. Finally, replace the parameter
8514 -- type of the formal with the internal subtype.
8516 if Ada_Version >= Ada_2005
8517 and then Null_Exclusion_Present (Param_Spec)
8519 if not Is_Access_Type (Formal_Type) then
8521 ("`NOT NULL` allowed only for an access type", Param_Spec);
8524 if Can_Never_Be_Null (Formal_Type)
8525 and then Comes_From_Source (Related_Nod)
8528 ("`NOT NULL` not allowed (& already excludes null)",
8529 Param_Spec, Formal_Type);
8533 Create_Null_Excluding_Itype
8535 Related_Nod => Related_Nod,
8536 Scope_Id => Scope (Current_Scope));
8538 -- If the designated type of the itype is an itype we
8539 -- decorate it with the Has_Delayed_Freeze attribute to
8540 -- avoid problems with the backend.
8543 -- type T is access procedure;
8544 -- procedure Op (O : not null T);
8546 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8547 Set_Has_Delayed_Freeze (Formal_Type);
8552 -- An access formal type
8556 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8558 -- No need to continue if we already notified errors
8560 if not Present (Formal_Type) then
8564 -- Ada 2005 (AI-254)
8567 AD : constant Node_Id :=
8568 Access_To_Subprogram_Definition
8569 (Parameter_Type (Param_Spec));
8571 if Present (AD) and then Protected_Present (AD) then
8573 Replace_Anonymous_Access_To_Protected_Subprogram
8579 Set_Etype (Formal, Formal_Type);
8580 Default := Expression (Param_Spec);
8582 if Present (Default) then
8583 if Out_Present (Param_Spec) then
8585 ("default initialization only allowed for IN parameters",
8589 -- Do the special preanalysis of the expression (see section on
8590 -- "Handling of Default Expressions" in the spec of package Sem).
8592 Preanalyze_Spec_Expression (Default, Formal_Type);
8594 -- An access to constant cannot be the default for
8595 -- an access parameter that is an access to variable.
8597 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8598 and then not Is_Access_Constant (Formal_Type)
8599 and then Is_Access_Type (Etype (Default))
8600 and then Is_Access_Constant (Etype (Default))
8603 ("formal that is access to variable cannot be initialized " &
8604 "with an access-to-constant expression", Default);
8607 -- Check that the designated type of an access parameter's default
8608 -- is not a class-wide type unless the parameter's designated type
8609 -- is also class-wide.
8611 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8612 and then not Designates_From_With_Type (Formal_Type)
8613 and then Is_Class_Wide_Default (Default)
8614 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8617 ("access to class-wide expression not allowed here", Default);
8620 -- Check incorrect use of dynamically tagged expressions
8622 if Is_Tagged_Type (Formal_Type) then
8623 Check_Dynamically_Tagged_Expression
8626 Related_Nod => Default);
8630 -- Ada 2005 (AI-231): Static checks
8632 if Ada_Version >= Ada_2005
8633 and then Is_Access_Type (Etype (Formal))
8634 and then Can_Never_Be_Null (Etype (Formal))
8636 Null_Exclusion_Static_Checks (Param_Spec);
8643 -- If this is the formal part of a function specification, analyze the
8644 -- subtype mark in the context where the formals are visible but not
8645 -- yet usable, and may hide outer homographs.
8647 if Nkind (Related_Nod) = N_Function_Specification then
8648 Analyze_Return_Type (Related_Nod);
8651 -- Now set the kind (mode) of each formal
8653 Param_Spec := First (T);
8654 while Present (Param_Spec) loop
8655 Formal := Defining_Identifier (Param_Spec);
8656 Set_Formal_Mode (Formal);
8658 if Ekind (Formal) = E_In_Parameter then
8659 Set_Default_Value (Formal, Expression (Param_Spec));
8661 if Present (Expression (Param_Spec)) then
8662 Default := Expression (Param_Spec);
8664 if Is_Scalar_Type (Etype (Default)) then
8666 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8668 Formal_Type := Entity (Parameter_Type (Param_Spec));
8671 Formal_Type := Access_Definition
8672 (Related_Nod, Parameter_Type (Param_Spec));
8675 Apply_Scalar_Range_Check (Default, Formal_Type);
8679 elsif Ekind (Formal) = E_Out_Parameter then
8680 Num_Out_Params := Num_Out_Params + 1;
8682 if Num_Out_Params = 1 then
8683 First_Out_Param := Formal;
8686 elsif Ekind (Formal) = E_In_Out_Parameter then
8687 Num_Out_Params := Num_Out_Params + 1;
8693 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8694 Set_Is_Only_Out_Parameter (First_Out_Param);
8696 end Process_Formals;
8702 procedure Process_PPCs
8704 Spec_Id : Entity_Id;
8705 Body_Id : Entity_Id)
8707 Loc : constant Source_Ptr := Sloc (N);
8711 Designator : Entity_Id;
8712 -- Subprogram designator, set from Spec_Id if present, else Body_Id
8714 Precond : Node_Id := Empty;
8715 -- Set non-Empty if we prepend precondition to the declarations. This
8716 -- is used to hook up inherited preconditions (adding the condition
8717 -- expression with OR ELSE, and adding the message).
8719 Inherited_Precond : Node_Id;
8720 -- Precondition inherited from parent subprogram
8722 Inherited : constant Subprogram_List :=
8723 Inherited_Subprograms (Spec_Id);
8724 -- List of subprograms inherited by this subprogram
8726 Plist : List_Id := No_List;
8727 -- List of generated postconditions
8729 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
8730 -- Prag contains an analyzed precondition or postcondition pragma. This
8731 -- function copies the pragma, changes it to the corresponding Check
8732 -- pragma and returns the Check pragma as the result. If Pspec is non-
8733 -- empty, this is the case of inheriting a PPC, where we must change
8734 -- references to parameters of the inherited subprogram to point to the
8735 -- corresponding parameters of the current subprogram.
8737 function Invariants_Or_Predicates_Present return Boolean;
8738 -- Determines if any invariants or predicates are present for any OUT
8739 -- or IN OUT parameters of the subprogram, or (for a function) if the
8740 -- return value has an invariant.
8746 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
8747 Nam : constant Name_Id := Pragma_Name (Prag);
8752 -- Prepare map if this is the case where we have to map entities of
8753 -- arguments in the overridden subprogram to corresponding entities
8754 -- of the current subprogram.
8765 Map := New_Elmt_List;
8766 PF := First_Formal (Pspec);
8767 CF := First_Formal (Designator);
8768 while Present (PF) loop
8769 Append_Elmt (PF, Map);
8770 Append_Elmt (CF, Map);
8777 -- Now we can copy the tree, doing any required substitutions
8779 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
8781 -- Set Analyzed to false, since we want to reanalyze the check
8782 -- procedure. Note that it is only at the outer level that we
8783 -- do this fiddling, for the spec cases, the already preanalyzed
8784 -- parameters are not affected.
8786 Set_Analyzed (CP, False);
8788 -- We also make sure Comes_From_Source is False for the copy
8790 Set_Comes_From_Source (CP, False);
8792 -- For a postcondition pragma within a generic, preserve the pragma
8793 -- for later expansion.
8795 if Nam = Name_Postcondition
8796 and then not Expander_Active
8801 -- Change copy of pragma into corresponding pragma Check
8803 Prepend_To (Pragma_Argument_Associations (CP),
8804 Make_Pragma_Argument_Association (Sloc (Prag),
8805 Expression => Make_Identifier (Loc, Nam)));
8806 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
8808 -- If this is inherited case and the current message starts with
8809 -- "failed p", we change it to "failed inherited p...".
8811 if Present (Pspec) then
8813 Msg : constant Node_Id :=
8814 Last (Pragma_Argument_Associations (CP));
8817 if Chars (Msg) = Name_Message then
8818 String_To_Name_Buffer (Strval (Expression (Msg)));
8820 if Name_Buffer (1 .. 8) = "failed p" then
8821 Insert_Str_In_Name_Buffer ("inherited ", 8);
8823 (Expression (Last (Pragma_Argument_Associations (CP))),
8824 String_From_Name_Buffer);
8830 -- Return the check pragma
8835 --------------------------------------
8836 -- Invariants_Or_Predicates_Present --
8837 --------------------------------------
8839 function Invariants_Or_Predicates_Present return Boolean is
8843 -- Check function return result
8845 if Ekind (Designator) /= E_Procedure
8846 and then Has_Invariants (Etype (Designator))
8853 Formal := First_Formal (Designator);
8854 while Present (Formal) loop
8855 if Ekind (Formal) /= E_In_Parameter
8857 (Has_Invariants (Etype (Formal))
8858 or else Present (Predicate_Function (Etype (Formal))))
8863 Next_Formal (Formal);
8867 end Invariants_Or_Predicates_Present;
8869 -- Start of processing for Process_PPCs
8872 -- Capture designator from spec if present, else from body
8874 if Present (Spec_Id) then
8875 Designator := Spec_Id;
8877 Designator := Body_Id;
8880 -- Grab preconditions from spec
8882 if Present (Spec_Id) then
8884 -- Loop through PPC pragmas from spec. Note that preconditions from
8885 -- the body will be analyzed and converted when we scan the body
8886 -- declarations below.
8888 Prag := Spec_PPC_List (Spec_Id);
8889 while Present (Prag) loop
8890 if Pragma_Name (Prag) = Name_Precondition then
8892 -- For Pre (or Precondition pragma), we simply prepend the
8893 -- pragma to the list of declarations right away so that it
8894 -- will be executed at the start of the procedure. Note that
8895 -- this processing reverses the order of the list, which is
8896 -- what we want since new entries were chained to the head of
8897 -- the list. There can be more then one precondition when we
8898 -- use pragma Precondition
8900 if not Class_Present (Prag) then
8901 Prepend (Grab_PPC, Declarations (N));
8903 -- For Pre'Class there can only be one pragma, and we save
8904 -- it in Precond for now. We will add inherited Pre'Class
8905 -- stuff before inserting this pragma in the declarations.
8907 Precond := Grab_PPC;
8911 Prag := Next_Pragma (Prag);
8914 -- Now deal with inherited preconditions
8916 for J in Inherited'Range loop
8917 Prag := Spec_PPC_List (Inherited (J));
8919 while Present (Prag) loop
8920 if Pragma_Name (Prag) = Name_Precondition
8921 and then Class_Present (Prag)
8923 Inherited_Precond := Grab_PPC (Inherited (J));
8925 -- No precondition so far, so establish this as the first
8927 if No (Precond) then
8928 Precond := Inherited_Precond;
8930 -- Here we already have a precondition, add inherited one
8933 -- Add new precondition to old one using OR ELSE
8936 New_Expr : constant Node_Id :=
8940 (Pragma_Argument_Associations
8941 (Inherited_Precond))));
8942 Old_Expr : constant Node_Id :=
8946 (Pragma_Argument_Associations
8950 if Paren_Count (Old_Expr) = 0 then
8951 Set_Paren_Count (Old_Expr, 1);
8954 if Paren_Count (New_Expr) = 0 then
8955 Set_Paren_Count (New_Expr, 1);
8959 Make_Or_Else (Sloc (Old_Expr),
8960 Left_Opnd => Relocate_Node (Old_Expr),
8961 Right_Opnd => New_Expr));
8964 -- Add new message in the form:
8966 -- failed precondition from bla
8967 -- also failed inherited precondition from bla
8970 -- Skip this if exception locations are suppressed
8972 if not Exception_Locations_Suppressed then
8974 New_Msg : constant Node_Id :=
8977 (Pragma_Argument_Associations
8978 (Inherited_Precond)));
8979 Old_Msg : constant Node_Id :=
8982 (Pragma_Argument_Associations
8985 Start_String (Strval (Old_Msg));
8986 Store_String_Chars (ASCII.LF & " also ");
8987 Store_String_Chars (Strval (New_Msg));
8988 Set_Strval (Old_Msg, End_String);
8994 Prag := Next_Pragma (Prag);
8998 -- If we have built a precondition for Pre'Class (including any
8999 -- Pre'Class aspects inherited from parent subprograms), then we
9000 -- insert this composite precondition at this stage.
9002 if Present (Precond) then
9003 Prepend (Precond, Declarations (N));
9007 -- Build postconditions procedure if needed and prepend the following
9008 -- declaration to the start of the declarations for the subprogram.
9010 -- procedure _postconditions [(_Result : resulttype)] is
9012 -- pragma Check (Postcondition, condition [,message]);
9013 -- pragma Check (Postcondition, condition [,message]);
9015 -- Invariant_Procedure (_Result) ...
9016 -- Invariant_Procedure (Arg1)
9020 -- First we deal with the postconditions in the body
9022 if Is_Non_Empty_List (Declarations (N)) then
9024 -- Loop through declarations
9026 Prag := First (Declarations (N));
9027 while Present (Prag) loop
9028 if Nkind (Prag) = N_Pragma then
9030 -- If pragma, capture if enabled postcondition, else ignore
9032 if Pragma_Name (Prag) = Name_Postcondition
9033 and then Check_Enabled (Name_Postcondition)
9035 if Plist = No_List then
9036 Plist := Empty_List;
9041 -- If expansion is disabled, as in a generic unit, save
9042 -- pragma for later expansion.
9044 if not Expander_Active then
9045 Prepend (Grab_PPC, Declarations (N));
9047 Append (Grab_PPC, Plist);
9053 -- Not a pragma, if comes from source, then end scan
9055 elsif Comes_From_Source (Prag) then
9058 -- Skip stuff not coming from source
9066 -- Now deal with any postconditions from the spec
9068 if Present (Spec_Id) then
9069 Spec_Postconditions : declare
9070 procedure Process_Post_Conditions
9073 -- This processes the Spec_PPC_List from Spec, processing any
9074 -- postconditions from the list. If Class is True, then only
9075 -- postconditions marked with Class_Present are considered.
9076 -- The caller has checked that Spec_PPC_List is non-Empty.
9078 -----------------------------
9079 -- Process_Post_Conditions --
9080 -----------------------------
9082 procedure Process_Post_Conditions
9095 -- Loop through PPC pragmas from spec
9097 Prag := Spec_PPC_List (Spec);
9099 if Pragma_Name (Prag) = Name_Postcondition
9100 and then (not Class or else Class_Present (Prag))
9102 if Plist = No_List then
9103 Plist := Empty_List;
9106 if not Expander_Active then
9108 (Grab_PPC (Pspec), Declarations (N));
9110 Append (Grab_PPC (Pspec), Plist);
9114 Prag := Next_Pragma (Prag);
9115 exit when No (Prag);
9117 end Process_Post_Conditions;
9119 -- Start of processing for Spec_Postconditions
9122 if Present (Spec_PPC_List (Spec_Id)) then
9123 Process_Post_Conditions (Spec_Id, Class => False);
9126 -- Process inherited postconditions
9128 for J in Inherited'Range loop
9129 if Present (Spec_PPC_List (Inherited (J))) then
9130 Process_Post_Conditions (Inherited (J), Class => True);
9133 end Spec_Postconditions;
9136 -- If we had any postconditions and expansion is enabled, or if the
9137 -- procedure has invariants, then build the _Postconditions procedure.
9139 if (Present (Plist) or else Invariants_Or_Predicates_Present)
9140 and then Expander_Active
9143 Plist := Empty_List;
9146 -- Special processing for function case
9148 if Ekind (Designator) /= E_Procedure then
9150 Rent : constant Entity_Id :=
9151 Make_Defining_Identifier (Loc,
9152 Chars => Name_uResult);
9153 Ftyp : constant Entity_Id := Etype (Designator);
9156 Set_Etype (Rent, Ftyp);
9158 -- Add argument for return
9162 Make_Parameter_Specification (Loc,
9163 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
9164 Defining_Identifier => Rent));
9166 -- Add invariant call if returning type with invariants
9168 if Has_Invariants (Etype (Rent))
9169 and then Present (Invariant_Procedure (Etype (Rent)))
9172 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
9176 -- Procedure rather than a function
9182 -- Add invariant calls and predicate calls for parameters. Note that
9183 -- this is done for functions as well, since in Ada 2012 they can
9184 -- have IN OUT args.
9191 Formal := First_Formal (Designator);
9192 while Present (Formal) loop
9193 if Ekind (Formal) /= E_In_Parameter then
9194 Ftype := Etype (Formal);
9196 if Has_Invariants (Ftype)
9197 and then Present (Invariant_Procedure (Ftype))
9201 (New_Occurrence_Of (Formal, Loc)));
9204 if Present (Predicate_Function (Ftype)) then
9206 Make_Predicate_Check
9207 (Ftype, New_Occurrence_Of (Formal, Loc)));
9211 Next_Formal (Formal);
9215 -- Build and insert postcondition procedure
9218 Post_Proc : constant Entity_Id :=
9219 Make_Defining_Identifier (Loc,
9220 Chars => Name_uPostconditions);
9221 -- The entity for the _Postconditions procedure
9224 Prepend_To (Declarations (N),
9225 Make_Subprogram_Body (Loc,
9227 Make_Procedure_Specification (Loc,
9228 Defining_Unit_Name => Post_Proc,
9229 Parameter_Specifications => Parms),
9231 Declarations => Empty_List,
9233 Handled_Statement_Sequence =>
9234 Make_Handled_Sequence_Of_Statements (Loc,
9235 Statements => Plist)));
9237 -- If this is a procedure, set the Postcondition_Proc attribute on
9238 -- the proper defining entity for the subprogram.
9240 if Ekind (Designator) = E_Procedure then
9241 Set_Postcondition_Proc (Designator, Post_Proc);
9245 Set_Has_Postconditions (Designator);
9249 ----------------------------
9250 -- Reference_Body_Formals --
9251 ----------------------------
9253 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
9258 if Error_Posted (Spec) then
9262 -- Iterate over both lists. They may be of different lengths if the two
9263 -- specs are not conformant.
9265 Fs := First_Formal (Spec);
9266 Fb := First_Formal (Bod);
9267 while Present (Fs) and then Present (Fb) loop
9268 Generate_Reference (Fs, Fb, 'b');
9271 Style.Check_Identifier (Fb, Fs);
9274 Set_Spec_Entity (Fb, Fs);
9275 Set_Referenced (Fs, False);
9279 end Reference_Body_Formals;
9281 -------------------------
9282 -- Set_Actual_Subtypes --
9283 -------------------------
9285 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
9289 First_Stmt : Node_Id := Empty;
9290 AS_Needed : Boolean;
9293 -- If this is an empty initialization procedure, no need to create
9294 -- actual subtypes (small optimization).
9296 if Ekind (Subp) = E_Procedure
9297 and then Is_Null_Init_Proc (Subp)
9302 Formal := First_Formal (Subp);
9303 while Present (Formal) loop
9304 T := Etype (Formal);
9306 -- We never need an actual subtype for a constrained formal
9308 if Is_Constrained (T) then
9311 -- If we have unknown discriminants, then we do not need an actual
9312 -- subtype, or more accurately we cannot figure it out! Note that
9313 -- all class-wide types have unknown discriminants.
9315 elsif Has_Unknown_Discriminants (T) then
9318 -- At this stage we have an unconstrained type that may need an
9319 -- actual subtype. For sure the actual subtype is needed if we have
9320 -- an unconstrained array type.
9322 elsif Is_Array_Type (T) then
9325 -- The only other case needing an actual subtype is an unconstrained
9326 -- record type which is an IN parameter (we cannot generate actual
9327 -- subtypes for the OUT or IN OUT case, since an assignment can
9328 -- change the discriminant values. However we exclude the case of
9329 -- initialization procedures, since discriminants are handled very
9330 -- specially in this context, see the section entitled "Handling of
9331 -- Discriminants" in Einfo.
9333 -- We also exclude the case of Discrim_SO_Functions (functions used
9334 -- in front end layout mode for size/offset values), since in such
9335 -- functions only discriminants are referenced, and not only are such
9336 -- subtypes not needed, but they cannot always be generated, because
9337 -- of order of elaboration issues.
9339 elsif Is_Record_Type (T)
9340 and then Ekind (Formal) = E_In_Parameter
9341 and then Chars (Formal) /= Name_uInit
9342 and then not Is_Unchecked_Union (T)
9343 and then not Is_Discrim_SO_Function (Subp)
9347 -- All other cases do not need an actual subtype
9353 -- Generate actual subtypes for unconstrained arrays and
9354 -- unconstrained discriminated records.
9357 if Nkind (N) = N_Accept_Statement then
9359 -- If expansion is active, The formal is replaced by a local
9360 -- variable that renames the corresponding entry of the
9361 -- parameter block, and it is this local variable that may
9362 -- require an actual subtype.
9364 if Expander_Active then
9365 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
9367 Decl := Build_Actual_Subtype (T, Formal);
9370 if Present (Handled_Statement_Sequence (N)) then
9372 First (Statements (Handled_Statement_Sequence (N)));
9373 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
9374 Mark_Rewrite_Insertion (Decl);
9376 -- If the accept statement has no body, there will be no
9377 -- reference to the actuals, so no need to compute actual
9384 Decl := Build_Actual_Subtype (T, Formal);
9385 Prepend (Decl, Declarations (N));
9386 Mark_Rewrite_Insertion (Decl);
9389 -- The declaration uses the bounds of an existing object, and
9390 -- therefore needs no constraint checks.
9392 Analyze (Decl, Suppress => All_Checks);
9394 -- We need to freeze manually the generated type when it is
9395 -- inserted anywhere else than in a declarative part.
9397 if Present (First_Stmt) then
9398 Insert_List_Before_And_Analyze (First_Stmt,
9399 Freeze_Entity (Defining_Identifier (Decl), N));
9402 if Nkind (N) = N_Accept_Statement
9403 and then Expander_Active
9405 Set_Actual_Subtype (Renamed_Object (Formal),
9406 Defining_Identifier (Decl));
9408 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
9412 Next_Formal (Formal);
9414 end Set_Actual_Subtypes;
9416 ---------------------
9417 -- Set_Formal_Mode --
9418 ---------------------
9420 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
9421 Spec : constant Node_Id := Parent (Formal_Id);
9424 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
9425 -- since we ensure that corresponding actuals are always valid at the
9426 -- point of the call.
9428 if Out_Present (Spec) then
9429 if Ekind (Scope (Formal_Id)) = E_Function
9430 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
9432 -- [IN] OUT parameters allowed for functions in Ada 2012
9434 if Ada_Version >= Ada_2012 then
9435 if In_Present (Spec) then
9436 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9438 Set_Ekind (Formal_Id, E_Out_Parameter);
9441 -- But not in earlier versions of Ada
9444 Error_Msg_N ("functions can only have IN parameters", Spec);
9445 Set_Ekind (Formal_Id, E_In_Parameter);
9448 elsif In_Present (Spec) then
9449 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9452 Set_Ekind (Formal_Id, E_Out_Parameter);
9453 Set_Never_Set_In_Source (Formal_Id, True);
9454 Set_Is_True_Constant (Formal_Id, False);
9455 Set_Current_Value (Formal_Id, Empty);
9459 Set_Ekind (Formal_Id, E_In_Parameter);
9462 -- Set Is_Known_Non_Null for access parameters since the language
9463 -- guarantees that access parameters are always non-null. We also set
9464 -- Can_Never_Be_Null, since there is no way to change the value.
9466 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
9468 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
9469 -- null; In Ada 2005, only if then null_exclusion is explicit.
9471 if Ada_Version < Ada_2005
9472 or else Can_Never_Be_Null (Etype (Formal_Id))
9474 Set_Is_Known_Non_Null (Formal_Id);
9475 Set_Can_Never_Be_Null (Formal_Id);
9478 -- Ada 2005 (AI-231): Null-exclusion access subtype
9480 elsif Is_Access_Type (Etype (Formal_Id))
9481 and then Can_Never_Be_Null (Etype (Formal_Id))
9483 Set_Is_Known_Non_Null (Formal_Id);
9486 Set_Mechanism (Formal_Id, Default_Mechanism);
9487 Set_Formal_Validity (Formal_Id);
9488 end Set_Formal_Mode;
9490 -------------------------
9491 -- Set_Formal_Validity --
9492 -------------------------
9494 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
9496 -- If no validity checking, then we cannot assume anything about the
9497 -- validity of parameters, since we do not know there is any checking
9498 -- of the validity on the call side.
9500 if not Validity_Checks_On then
9503 -- If validity checking for parameters is enabled, this means we are
9504 -- not supposed to make any assumptions about argument values.
9506 elsif Validity_Check_Parameters then
9509 -- If we are checking in parameters, we will assume that the caller is
9510 -- also checking parameters, so we can assume the parameter is valid.
9512 elsif Ekind (Formal_Id) = E_In_Parameter
9513 and then Validity_Check_In_Params
9515 Set_Is_Known_Valid (Formal_Id, True);
9517 -- Similar treatment for IN OUT parameters
9519 elsif Ekind (Formal_Id) = E_In_Out_Parameter
9520 and then Validity_Check_In_Out_Params
9522 Set_Is_Known_Valid (Formal_Id, True);
9524 end Set_Formal_Validity;
9526 ------------------------
9527 -- Subtype_Conformant --
9528 ------------------------
9530 function Subtype_Conformant
9531 (New_Id : Entity_Id;
9533 Skip_Controlling_Formals : Boolean := False) return Boolean
9537 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
9538 Skip_Controlling_Formals => Skip_Controlling_Formals);
9540 end Subtype_Conformant;
9542 ---------------------
9543 -- Type_Conformant --
9544 ---------------------
9546 function Type_Conformant
9547 (New_Id : Entity_Id;
9549 Skip_Controlling_Formals : Boolean := False) return Boolean
9553 May_Hide_Profile := False;
9556 (New_Id, Old_Id, Type_Conformant, False, Result,
9557 Skip_Controlling_Formals => Skip_Controlling_Formals);
9559 end Type_Conformant;
9561 -------------------------------
9562 -- Valid_Operator_Definition --
9563 -------------------------------
9565 procedure Valid_Operator_Definition (Designator : Entity_Id) is
9568 Id : constant Name_Id := Chars (Designator);
9572 F := First_Formal (Designator);
9573 while Present (F) loop
9576 if Present (Default_Value (F)) then
9578 ("default values not allowed for operator parameters",
9585 -- Verify that user-defined operators have proper number of arguments
9586 -- First case of operators which can only be unary
9589 or else Id = Name_Op_Abs
9593 -- Case of operators which can be unary or binary
9595 elsif Id = Name_Op_Add
9596 or Id = Name_Op_Subtract
9598 N_OK := (N in 1 .. 2);
9600 -- All other operators can only be binary
9608 ("incorrect number of arguments for operator", Designator);
9612 and then Base_Type (Etype (Designator)) = Standard_Boolean
9613 and then not Is_Intrinsic_Subprogram (Designator)
9616 ("explicit definition of inequality not allowed", Designator);
9618 end Valid_Operator_Definition;