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 also provide a source
211 -- of postconditions, which 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 Is_Overriding_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)
814 -- (Disabled for now)
816 -- Check_Order_Dependence;
818 end Analyze_Function_Return;
820 -------------------------------------
821 -- Analyze_Generic_Subprogram_Body --
822 -------------------------------------
824 procedure Analyze_Generic_Subprogram_Body
828 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
829 Kind : constant Entity_Kind := Ekind (Gen_Id);
835 -- Copy body and disable expansion while analyzing the generic For a
836 -- stub, do not copy the stub (which would load the proper body), this
837 -- will be done when the proper body is analyzed.
839 if Nkind (N) /= N_Subprogram_Body_Stub then
840 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
845 Spec := Specification (N);
847 -- Within the body of the generic, the subprogram is callable, and
848 -- behaves like the corresponding non-generic unit.
850 Body_Id := Defining_Entity (Spec);
852 if Kind = E_Generic_Procedure
853 and then Nkind (Spec) /= N_Procedure_Specification
855 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
858 elsif Kind = E_Generic_Function
859 and then Nkind (Spec) /= N_Function_Specification
861 Error_Msg_N ("invalid body for generic function ", Body_Id);
865 Set_Corresponding_Body (Gen_Decl, Body_Id);
867 if Has_Completion (Gen_Id)
868 and then Nkind (Parent (N)) /= N_Subunit
870 Error_Msg_N ("duplicate generic body", N);
873 Set_Has_Completion (Gen_Id);
876 if Nkind (N) = N_Subprogram_Body_Stub then
877 Set_Ekind (Defining_Entity (Specification (N)), Kind);
879 Set_Corresponding_Spec (N, Gen_Id);
882 if Nkind (Parent (N)) = N_Compilation_Unit then
883 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
886 -- Make generic parameters immediately visible in the body. They are
887 -- needed to process the formals declarations. Then make the formals
888 -- visible in a separate step.
894 First_Ent : Entity_Id;
897 First_Ent := First_Entity (Gen_Id);
900 while Present (E) and then not Is_Formal (E) loop
905 Set_Use (Generic_Formal_Declarations (Gen_Decl));
907 -- Now generic formals are visible, and the specification can be
908 -- analyzed, for subsequent conformance check.
910 Body_Id := Analyze_Subprogram_Specification (Spec);
912 -- Make formal parameters visible
916 -- E is the first formal parameter, we loop through the formals
917 -- installing them so that they will be visible.
919 Set_First_Entity (Gen_Id, E);
920 while Present (E) loop
926 -- Visible generic entity is callable within its own body
928 Set_Ekind (Gen_Id, Ekind (Body_Id));
929 Set_Ekind (Body_Id, E_Subprogram_Body);
930 Set_Convention (Body_Id, Convention (Gen_Id));
931 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
932 Set_Scope (Body_Id, Scope (Gen_Id));
933 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
935 if Nkind (N) = N_Subprogram_Body_Stub then
937 -- No body to analyze, so restore state of generic unit
939 Set_Ekind (Gen_Id, Kind);
940 Set_Ekind (Body_Id, Kind);
942 if Present (First_Ent) then
943 Set_First_Entity (Gen_Id, First_Ent);
950 -- If this is a compilation unit, it must be made visible explicitly,
951 -- because the compilation of the declaration, unlike other library
952 -- unit declarations, does not. If it is not a unit, the following
953 -- is redundant but harmless.
955 Set_Is_Immediately_Visible (Gen_Id);
956 Reference_Body_Formals (Gen_Id, Body_Id);
958 if Is_Child_Unit (Gen_Id) then
959 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
962 Set_Actual_Subtypes (N, Current_Scope);
963 Process_PPCs (N, Gen_Id, Body_Id);
965 -- If the generic unit carries pre- or post-conditions, copy them
966 -- to the original generic tree, so that they are properly added
967 -- to any instantiation.
970 Orig : constant Node_Id := Original_Node (N);
974 Cond := First (Declarations (N));
975 while Present (Cond) loop
976 if Nkind (Cond) = N_Pragma
977 and then Pragma_Name (Cond) = Name_Check
979 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
981 elsif Nkind (Cond) = N_Pragma
982 and then Pragma_Name (Cond) = Name_Postcondition
984 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
985 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
994 Analyze_Declarations (Declarations (N));
996 Analyze (Handled_Statement_Sequence (N));
998 Save_Global_References (Original_Node (N));
1000 -- Prior to exiting the scope, include generic formals again (if any
1001 -- are present) in the set of local entities.
1003 if Present (First_Ent) then
1004 Set_First_Entity (Gen_Id, First_Ent);
1007 Check_References (Gen_Id);
1010 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1012 Check_Subprogram_Order (N);
1014 -- Outside of its body, unit is generic again
1016 Set_Ekind (Gen_Id, Kind);
1017 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1020 Style.Check_Identifier (Body_Id, Gen_Id);
1024 end Analyze_Generic_Subprogram_Body;
1026 -----------------------------
1027 -- Analyze_Operator_Symbol --
1028 -----------------------------
1030 -- An operator symbol such as "+" or "and" may appear in context where the
1031 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1032 -- is just a string, as in (conjunction = "or"). In these cases the parser
1033 -- generates this node, and the semantics does the disambiguation. Other
1034 -- such case are actuals in an instantiation, the generic unit in an
1035 -- instantiation, and pragma arguments.
1037 procedure Analyze_Operator_Symbol (N : Node_Id) is
1038 Par : constant Node_Id := Parent (N);
1041 if (Nkind (Par) = N_Function_Call
1042 and then N = Name (Par))
1043 or else Nkind (Par) = N_Function_Instantiation
1044 or else (Nkind (Par) = N_Indexed_Component
1045 and then N = Prefix (Par))
1046 or else (Nkind (Par) = N_Pragma_Argument_Association
1047 and then not Is_Pragma_String_Literal (Par))
1048 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1049 or else (Nkind (Par) = N_Attribute_Reference
1050 and then Attribute_Name (Par) /= Name_Value)
1052 Find_Direct_Name (N);
1055 Change_Operator_Symbol_To_String_Literal (N);
1058 end Analyze_Operator_Symbol;
1060 -----------------------------------
1061 -- Analyze_Parameter_Association --
1062 -----------------------------------
1064 procedure Analyze_Parameter_Association (N : Node_Id) is
1066 Analyze (Explicit_Actual_Parameter (N));
1067 end Analyze_Parameter_Association;
1069 --------------------------------------
1070 -- Analyze_Parameterized_Expression --
1071 --------------------------------------
1073 procedure Analyze_Parameterized_Expression (N : Node_Id) is
1074 Loc : constant Source_Ptr := Sloc (N);
1075 LocX : constant Source_Ptr := Sloc (Expression (N));
1078 -- This is one of the occasions on which we write things during semantic
1079 -- analysis. Transform the parameterized expression into an equivalent
1080 -- subprogram body, and then analyze that.
1083 Make_Subprogram_Body (Loc,
1084 Specification => Specification (N),
1085 Declarations => Empty_List,
1086 Handled_Statement_Sequence =>
1087 Make_Handled_Sequence_Of_Statements (LocX,
1088 Statements => New_List (
1089 Make_Simple_Return_Statement (LocX,
1090 Expression => Expression (N))))));
1092 end Analyze_Parameterized_Expression;
1094 ----------------------------
1095 -- Analyze_Procedure_Call --
1096 ----------------------------
1098 procedure Analyze_Procedure_Call (N : Node_Id) is
1099 Loc : constant Source_Ptr := Sloc (N);
1100 P : constant Node_Id := Name (N);
1101 Actuals : constant List_Id := Parameter_Associations (N);
1105 procedure Analyze_Call_And_Resolve;
1106 -- Do Analyze and Resolve calls for procedure call
1107 -- At end, check illegal order dependence.
1109 ------------------------------
1110 -- Analyze_Call_And_Resolve --
1111 ------------------------------
1113 procedure Analyze_Call_And_Resolve is
1115 if Nkind (N) = N_Procedure_Call_Statement then
1117 Resolve (N, Standard_Void_Type);
1119 -- Apply checks suggested by AI05-0144 (Disabled for now)
1121 -- Check_Order_Dependence;
1126 end Analyze_Call_And_Resolve;
1128 -- Start of processing for Analyze_Procedure_Call
1131 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1132 -- a procedure call or an entry call. The prefix may denote an access
1133 -- to subprogram type, in which case an implicit dereference applies.
1134 -- If the prefix is an indexed component (without implicit dereference)
1135 -- then the construct denotes a call to a member of an entire family.
1136 -- If the prefix is a simple name, it may still denote a call to a
1137 -- parameterless member of an entry family. Resolution of these various
1138 -- interpretations is delicate.
1142 -- If this is a call of the form Obj.Op, the call may have been
1143 -- analyzed and possibly rewritten into a block, in which case
1146 if Analyzed (N) then
1150 -- If there is an error analyzing the name (which may have been
1151 -- rewritten if the original call was in prefix notation) then error
1152 -- has been emitted already, mark node and return.
1155 or else Etype (Name (N)) = Any_Type
1157 Set_Etype (N, Any_Type);
1161 -- Otherwise analyze the parameters
1163 if Present (Actuals) then
1164 Actual := First (Actuals);
1166 while Present (Actual) loop
1168 Check_Parameterless_Call (Actual);
1173 -- Special processing for Elab_Spec and Elab_Body calls
1175 if Nkind (P) = N_Attribute_Reference
1176 and then (Attribute_Name (P) = Name_Elab_Spec
1177 or else Attribute_Name (P) = Name_Elab_Body)
1179 if Present (Actuals) then
1181 ("no parameters allowed for this call", First (Actuals));
1185 Set_Etype (N, Standard_Void_Type);
1188 elsif Is_Entity_Name (P)
1189 and then Is_Record_Type (Etype (Entity (P)))
1190 and then Remote_AST_I_Dereference (P)
1194 elsif Is_Entity_Name (P)
1195 and then Ekind (Entity (P)) /= E_Entry_Family
1197 if Is_Access_Type (Etype (P))
1198 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1199 and then No (Actuals)
1200 and then Comes_From_Source (N)
1202 Error_Msg_N ("missing explicit dereference in call", N);
1205 Analyze_Call_And_Resolve;
1207 -- If the prefix is the simple name of an entry family, this is
1208 -- a parameterless call from within the task body itself.
1210 elsif Is_Entity_Name (P)
1211 and then Nkind (P) = N_Identifier
1212 and then Ekind (Entity (P)) = E_Entry_Family
1213 and then Present (Actuals)
1214 and then No (Next (First (Actuals)))
1216 -- Can be call to parameterless entry family. What appears to be the
1217 -- sole argument is in fact the entry index. Rewrite prefix of node
1218 -- accordingly. Source representation is unchanged by this
1222 Make_Indexed_Component (Loc,
1224 Make_Selected_Component (Loc,
1225 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1226 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1227 Expressions => Actuals);
1228 Set_Name (N, New_N);
1229 Set_Etype (New_N, Standard_Void_Type);
1230 Set_Parameter_Associations (N, No_List);
1231 Analyze_Call_And_Resolve;
1233 elsif Nkind (P) = N_Explicit_Dereference then
1234 if Ekind (Etype (P)) = E_Subprogram_Type then
1235 Analyze_Call_And_Resolve;
1237 Error_Msg_N ("expect access to procedure in call", P);
1240 -- The name can be a selected component or an indexed component that
1241 -- yields an access to subprogram. Such a prefix is legal if the call
1242 -- has parameter associations.
1244 elsif Is_Access_Type (Etype (P))
1245 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1247 if Present (Actuals) then
1248 Analyze_Call_And_Resolve;
1250 Error_Msg_N ("missing explicit dereference in call ", N);
1253 -- If not an access to subprogram, then the prefix must resolve to the
1254 -- name of an entry, entry family, or protected operation.
1256 -- For the case of a simple entry call, P is a selected component where
1257 -- the prefix is the task and the selector name is the entry. A call to
1258 -- a protected procedure will have the same syntax. If the protected
1259 -- object contains overloaded operations, the entity may appear as a
1260 -- function, the context will select the operation whose type is Void.
1262 elsif Nkind (P) = N_Selected_Component
1263 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1265 Ekind (Entity (Selector_Name (P))) = E_Procedure
1267 Ekind (Entity (Selector_Name (P))) = E_Function)
1269 Analyze_Call_And_Resolve;
1271 elsif Nkind (P) = N_Selected_Component
1272 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1273 and then Present (Actuals)
1274 and then No (Next (First (Actuals)))
1276 -- Can be call to parameterless entry family. What appears to be the
1277 -- sole argument is in fact the entry index. Rewrite prefix of node
1278 -- accordingly. Source representation is unchanged by this
1282 Make_Indexed_Component (Loc,
1283 Prefix => New_Copy (P),
1284 Expressions => Actuals);
1285 Set_Name (N, New_N);
1286 Set_Etype (New_N, Standard_Void_Type);
1287 Set_Parameter_Associations (N, No_List);
1288 Analyze_Call_And_Resolve;
1290 -- For the case of a reference to an element of an entry family, P is
1291 -- an indexed component whose prefix is a selected component (task and
1292 -- entry family), and whose index is the entry family index.
1294 elsif Nkind (P) = N_Indexed_Component
1295 and then Nkind (Prefix (P)) = N_Selected_Component
1296 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1298 Analyze_Call_And_Resolve;
1300 -- If the prefix is the name of an entry family, it is a call from
1301 -- within the task body itself.
1303 elsif Nkind (P) = N_Indexed_Component
1304 and then Nkind (Prefix (P)) = N_Identifier
1305 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1308 Make_Selected_Component (Loc,
1309 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1310 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1311 Rewrite (Prefix (P), New_N);
1313 Analyze_Call_And_Resolve;
1315 -- Anything else is an error
1318 Error_Msg_N ("invalid procedure or entry call", N);
1320 end Analyze_Procedure_Call;
1322 -------------------------------------
1323 -- Analyze_Simple_Return_Statement --
1324 -------------------------------------
1326 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1328 if Present (Expression (N)) then
1329 Mark_Coextensions (N, Expression (N));
1332 Analyze_Return_Statement (N);
1333 end Analyze_Simple_Return_Statement;
1335 -------------------------
1336 -- Analyze_Return_Type --
1337 -------------------------
1339 procedure Analyze_Return_Type (N : Node_Id) is
1340 Designator : constant Entity_Id := Defining_Entity (N);
1341 Typ : Entity_Id := Empty;
1344 -- Normal case where result definition does not indicate an error
1346 if Result_Definition (N) /= Error then
1347 if Nkind (Result_Definition (N)) = N_Access_Definition then
1349 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1352 AD : constant Node_Id :=
1353 Access_To_Subprogram_Definition (Result_Definition (N));
1355 if Present (AD) and then Protected_Present (AD) then
1356 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1358 Typ := Access_Definition (N, Result_Definition (N));
1362 Set_Parent (Typ, Result_Definition (N));
1363 Set_Is_Local_Anonymous_Access (Typ);
1364 Set_Etype (Designator, Typ);
1366 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1368 Null_Exclusion_Static_Checks (N);
1370 -- Subtype_Mark case
1373 Find_Type (Result_Definition (N));
1374 Typ := Entity (Result_Definition (N));
1375 Set_Etype (Designator, Typ);
1377 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1379 Null_Exclusion_Static_Checks (N);
1381 -- If a null exclusion is imposed on the result type, then create
1382 -- a null-excluding itype (an access subtype) and use it as the
1383 -- function's Etype. Note that the null exclusion checks are done
1384 -- right before this, because they don't get applied to types that
1385 -- do not come from source.
1387 if Is_Access_Type (Typ)
1388 and then Null_Exclusion_Present (N)
1390 Set_Etype (Designator,
1391 Create_Null_Excluding_Itype
1394 Scope_Id => Scope (Current_Scope)));
1396 -- The new subtype must be elaborated before use because
1397 -- it is visible outside of the function. However its base
1398 -- type may not be frozen yet, so the reference that will
1399 -- force elaboration must be attached to the freezing of
1402 -- If the return specification appears on a proper body,
1403 -- the subtype will have been created already on the spec.
1405 if Is_Frozen (Typ) then
1406 if Nkind (Parent (N)) = N_Subprogram_Body
1407 and then Nkind (Parent (Parent (N))) = N_Subunit
1411 Build_Itype_Reference (Etype (Designator), Parent (N));
1415 Ensure_Freeze_Node (Typ);
1418 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1420 Set_Itype (IR, Etype (Designator));
1421 Append_Freeze_Actions (Typ, New_List (IR));
1426 Set_Etype (Designator, Typ);
1429 if Ekind (Typ) = E_Incomplete_Type
1430 and then Is_Value_Type (Typ)
1434 elsif Ekind (Typ) = E_Incomplete_Type
1435 or else (Is_Class_Wide_Type (Typ)
1437 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1439 -- AI05-0151: Tagged incomplete types are allowed in all formal
1440 -- parts. Untagged incomplete types are not allowed in bodies.
1442 if Ada_Version >= Ada_2012 then
1443 if Is_Tagged_Type (Typ) then
1446 elsif Nkind_In (Parent (Parent (N)),
1452 ("invalid use of untagged incomplete type&",
1458 ("invalid use of incomplete type&", Designator, Typ);
1463 -- Case where result definition does indicate an error
1466 Set_Etype (Designator, Any_Type);
1468 end Analyze_Return_Type;
1470 -----------------------------
1471 -- Analyze_Subprogram_Body --
1472 -----------------------------
1474 procedure Analyze_Subprogram_Body (N : Node_Id) is
1475 Loc : constant Source_Ptr := Sloc (N);
1476 Body_Spec : constant Node_Id := Specification (N);
1477 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1480 if Debug_Flag_C then
1481 Write_Str ("==> subprogram body ");
1482 Write_Name (Chars (Body_Id));
1483 Write_Str (" from ");
1484 Write_Location (Loc);
1489 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1491 -- The real work is split out into the helper, so it can do "return;"
1492 -- without skipping the debug output:
1494 Analyze_Subprogram_Body_Helper (N);
1496 if Debug_Flag_C then
1498 Write_Str ("<== subprogram body ");
1499 Write_Name (Chars (Body_Id));
1500 Write_Str (" from ");
1501 Write_Location (Loc);
1504 end Analyze_Subprogram_Body;
1506 ------------------------------------
1507 -- Analyze_Subprogram_Body_Helper --
1508 ------------------------------------
1510 -- This procedure is called for regular subprogram bodies, generic bodies,
1511 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1512 -- specification matters, and is used to create a proper declaration for
1513 -- the subprogram, or to perform conformance checks.
1515 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1516 Loc : constant Source_Ptr := Sloc (N);
1517 Body_Deleted : constant Boolean := False;
1518 Body_Spec : constant Node_Id := Specification (N);
1519 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1520 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1521 Conformant : Boolean;
1524 Prot_Typ : Entity_Id := Empty;
1525 Spec_Id : Entity_Id;
1526 Spec_Decl : Node_Id := Empty;
1528 Last_Real_Spec_Entity : Entity_Id := Empty;
1529 -- When we analyze a separate spec, the entity chain ends up containing
1530 -- the formals, as well as any itypes generated during analysis of the
1531 -- default expressions for parameters, or the arguments of associated
1532 -- precondition/postcondition pragmas (which are analyzed in the context
1533 -- of the spec since they have visibility on formals).
1535 -- These entities belong with the spec and not the body. However we do
1536 -- the analysis of the body in the context of the spec (again to obtain
1537 -- visibility to the formals), and all the entities generated during
1538 -- this analysis end up also chained to the entity chain of the spec.
1539 -- But they really belong to the body, and there is circuitry to move
1540 -- them from the spec to the body.
1542 -- However, when we do this move, we don't want to move the real spec
1543 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1544 -- variable points to the last real spec entity, so we only move those
1545 -- chained beyond that point. It is initialized to Empty to deal with
1546 -- the case where there is no separate spec.
1548 procedure Check_Anonymous_Return;
1549 -- Ada 2005: if a function returns an access type that denotes a task,
1550 -- or a type that contains tasks, we must create a master entity for
1551 -- the anonymous type, which typically will be used in an allocator
1552 -- in the body of the function.
1554 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1555 -- Look ahead to recognize a pragma that may appear after the body.
1556 -- If there is a previous spec, check that it appears in the same
1557 -- declarative part. If the pragma is Inline_Always, perform inlining
1558 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1559 -- If the body acts as a spec, and inlining is required, we create a
1560 -- subprogram declaration for it, in order to attach the body to inline.
1561 -- If pragma does not appear after the body, check whether there is
1562 -- an inline pragma before any local declarations.
1564 procedure Check_Missing_Return;
1565 -- Checks for a function with a no return statements, and also performs
1566 -- the warning checks implemented by Check_Returns.
1568 function Disambiguate_Spec return Entity_Id;
1569 -- When a primitive is declared between the private view and the full
1570 -- view of a concurrent type which implements an interface, a special
1571 -- mechanism is used to find the corresponding spec of the primitive
1574 function Is_Private_Concurrent_Primitive
1575 (Subp_Id : Entity_Id) return Boolean;
1576 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1577 -- type that implements an interface and has a private view.
1579 procedure Set_Trivial_Subprogram (N : Node_Id);
1580 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1581 -- subprogram whose body is being analyzed. N is the statement node
1582 -- causing the flag to be set, if the following statement is a return
1583 -- of an entity, we mark the entity as set in source to suppress any
1584 -- warning on the stylized use of function stubs with a dummy return.
1586 procedure Verify_Overriding_Indicator;
1587 -- If there was a previous spec, the entity has been entered in the
1588 -- current scope previously. If the body itself carries an overriding
1589 -- indicator, check that it is consistent with the known status of the
1592 ----------------------------
1593 -- Check_Anonymous_Return --
1594 ----------------------------
1596 procedure Check_Anonymous_Return is
1602 if Present (Spec_Id) then
1608 if Ekind (Scop) = E_Function
1609 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1610 and then not Is_Thunk (Scop)
1611 and then (Has_Task (Designated_Type (Etype (Scop)))
1613 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1615 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1616 and then Expander_Active
1618 -- Avoid cases with no tasking support
1620 and then RTE_Available (RE_Current_Master)
1621 and then not Restriction_Active (No_Task_Hierarchy)
1624 Make_Object_Declaration (Loc,
1625 Defining_Identifier =>
1626 Make_Defining_Identifier (Loc, Name_uMaster),
1627 Constant_Present => True,
1628 Object_Definition =>
1629 New_Reference_To (RTE (RE_Master_Id), Loc),
1631 Make_Explicit_Dereference (Loc,
1632 New_Reference_To (RTE (RE_Current_Master), Loc)));
1634 if Present (Declarations (N)) then
1635 Prepend (Decl, Declarations (N));
1637 Set_Declarations (N, New_List (Decl));
1640 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1641 Set_Has_Master_Entity (Scop);
1643 -- Now mark the containing scope as a task master
1646 while Nkind (Par) /= N_Compilation_Unit loop
1647 Par := Parent (Par);
1648 pragma Assert (Present (Par));
1650 -- If we fall off the top, we are at the outer level, and
1651 -- the environment task is our effective master, so nothing
1655 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1657 Set_Is_Task_Master (Par, True);
1662 end Check_Anonymous_Return;
1664 -------------------------
1665 -- Check_Inline_Pragma --
1666 -------------------------
1668 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1672 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1673 -- True when N is a pragma Inline or Inline_Always that applies
1674 -- to this subprogram.
1676 -----------------------
1677 -- Is_Inline_Pragma --
1678 -----------------------
1680 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1683 Nkind (N) = N_Pragma
1685 (Pragma_Name (N) = Name_Inline_Always
1688 and then Pragma_Name (N) = Name_Inline))
1691 (Expression (First (Pragma_Argument_Associations (N))))
1693 end Is_Inline_Pragma;
1695 -- Start of processing for Check_Inline_Pragma
1698 if not Expander_Active then
1702 if Is_List_Member (N)
1703 and then Present (Next (N))
1704 and then Is_Inline_Pragma (Next (N))
1708 elsif Nkind (N) /= N_Subprogram_Body_Stub
1709 and then Present (Declarations (N))
1710 and then Is_Inline_Pragma (First (Declarations (N)))
1712 Prag := First (Declarations (N));
1718 if Present (Prag) then
1719 if Present (Spec_Id) then
1720 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1725 -- Create a subprogram declaration, to make treatment uniform
1728 Subp : constant Entity_Id :=
1729 Make_Defining_Identifier (Loc, Chars (Body_Id));
1730 Decl : constant Node_Id :=
1731 Make_Subprogram_Declaration (Loc,
1733 New_Copy_Tree (Specification (N)));
1736 Set_Defining_Unit_Name (Specification (Decl), Subp);
1738 if Present (First_Formal (Body_Id)) then
1739 Plist := Copy_Parameter_List (Body_Id);
1740 Set_Parameter_Specifications
1741 (Specification (Decl), Plist);
1744 Insert_Before (N, Decl);
1747 Set_Has_Pragma_Inline (Subp);
1749 if Pragma_Name (Prag) = Name_Inline_Always then
1750 Set_Is_Inlined (Subp);
1751 Set_Has_Pragma_Inline_Always (Subp);
1758 end Check_Inline_Pragma;
1760 --------------------------
1761 -- Check_Missing_Return --
1762 --------------------------
1764 procedure Check_Missing_Return is
1766 Missing_Ret : Boolean;
1769 if Nkind (Body_Spec) = N_Function_Specification then
1770 if Present (Spec_Id) then
1776 if Return_Present (Id) then
1777 Check_Returns (HSS, 'F', Missing_Ret);
1780 Set_Has_Missing_Return (Id);
1783 elsif (Is_Generic_Subprogram (Id)
1784 or else not Is_Machine_Code_Subprogram (Id))
1785 and then not Body_Deleted
1787 Error_Msg_N ("missing RETURN statement in function body", N);
1790 -- If procedure with No_Return, check returns
1792 elsif Nkind (Body_Spec) = N_Procedure_Specification
1793 and then Present (Spec_Id)
1794 and then No_Return (Spec_Id)
1796 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
1798 end Check_Missing_Return;
1800 -----------------------
1801 -- Disambiguate_Spec --
1802 -----------------------
1804 function Disambiguate_Spec return Entity_Id is
1805 Priv_Spec : Entity_Id;
1808 procedure Replace_Types (To_Corresponding : Boolean);
1809 -- Depending on the flag, replace the type of formal parameters of
1810 -- Body_Id if it is a concurrent type implementing interfaces with
1811 -- the corresponding record type or the other way around.
1813 procedure Replace_Types (To_Corresponding : Boolean) is
1815 Formal_Typ : Entity_Id;
1818 Formal := First_Formal (Body_Id);
1819 while Present (Formal) loop
1820 Formal_Typ := Etype (Formal);
1822 -- From concurrent type to corresponding record
1824 if To_Corresponding then
1825 if Is_Concurrent_Type (Formal_Typ)
1826 and then Present (Corresponding_Record_Type (Formal_Typ))
1827 and then Present (Interfaces (
1828 Corresponding_Record_Type (Formal_Typ)))
1831 Corresponding_Record_Type (Formal_Typ));
1834 -- From corresponding record to concurrent type
1837 if Is_Concurrent_Record_Type (Formal_Typ)
1838 and then Present (Interfaces (Formal_Typ))
1841 Corresponding_Concurrent_Type (Formal_Typ));
1845 Next_Formal (Formal);
1849 -- Start of processing for Disambiguate_Spec
1852 -- Try to retrieve the specification of the body as is. All error
1853 -- messages are suppressed because the body may not have a spec in
1854 -- its current state.
1856 Spec_N := Find_Corresponding_Spec (N, False);
1858 -- It is possible that this is the body of a primitive declared
1859 -- between a private and a full view of a concurrent type. The
1860 -- controlling parameter of the spec carries the concurrent type,
1861 -- not the corresponding record type as transformed by Analyze_
1862 -- Subprogram_Specification. In such cases, we undo the change
1863 -- made by the analysis of the specification and try to find the
1866 -- Note that wrappers already have their corresponding specs and
1867 -- bodies set during their creation, so if the candidate spec is
1868 -- a wrapper, then we definitely need to swap all types to their
1869 -- original concurrent status.
1872 or else Is_Primitive_Wrapper (Spec_N)
1874 -- Restore all references of corresponding record types to the
1875 -- original concurrent types.
1877 Replace_Types (To_Corresponding => False);
1878 Priv_Spec := Find_Corresponding_Spec (N, False);
1880 -- The current body truly belongs to a primitive declared between
1881 -- a private and a full view. We leave the modified body as is,
1882 -- and return the true spec.
1884 if Present (Priv_Spec)
1885 and then Is_Private_Primitive (Priv_Spec)
1890 -- In case that this is some sort of error, restore the original
1891 -- state of the body.
1893 Replace_Types (To_Corresponding => True);
1897 end Disambiguate_Spec;
1899 -------------------------------------
1900 -- Is_Private_Concurrent_Primitive --
1901 -------------------------------------
1903 function Is_Private_Concurrent_Primitive
1904 (Subp_Id : Entity_Id) return Boolean
1906 Formal_Typ : Entity_Id;
1909 if Present (First_Formal (Subp_Id)) then
1910 Formal_Typ := Etype (First_Formal (Subp_Id));
1912 if Is_Concurrent_Record_Type (Formal_Typ) then
1913 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
1916 -- The type of the first formal is a concurrent tagged type with
1920 Is_Concurrent_Type (Formal_Typ)
1921 and then Is_Tagged_Type (Formal_Typ)
1922 and then Has_Private_Declaration (Formal_Typ);
1926 end Is_Private_Concurrent_Primitive;
1928 ----------------------------
1929 -- Set_Trivial_Subprogram --
1930 ----------------------------
1932 procedure Set_Trivial_Subprogram (N : Node_Id) is
1933 Nxt : constant Node_Id := Next (N);
1936 Set_Is_Trivial_Subprogram (Body_Id);
1938 if Present (Spec_Id) then
1939 Set_Is_Trivial_Subprogram (Spec_Id);
1943 and then Nkind (Nxt) = N_Simple_Return_Statement
1944 and then No (Next (Nxt))
1945 and then Present (Expression (Nxt))
1946 and then Is_Entity_Name (Expression (Nxt))
1948 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
1950 end Set_Trivial_Subprogram;
1952 ---------------------------------
1953 -- Verify_Overriding_Indicator --
1954 ---------------------------------
1956 procedure Verify_Overriding_Indicator is
1958 if Must_Override (Body_Spec) then
1959 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
1960 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1964 elsif not Is_Overriding_Operation (Spec_Id) then
1966 ("subprogram& is not overriding", Body_Spec, Spec_Id);
1969 elsif Must_Not_Override (Body_Spec) then
1970 if Is_Overriding_Operation (Spec_Id) then
1972 ("subprogram& overrides inherited operation",
1973 Body_Spec, Spec_Id);
1975 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
1976 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
1979 ("subprogram & overrides predefined operator ",
1980 Body_Spec, Spec_Id);
1982 -- If this is not a primitive operation or protected subprogram,
1983 -- then the overriding indicator is altogether illegal.
1985 elsif not Is_Primitive (Spec_Id)
1986 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
1989 ("overriding indicator only allowed " &
1990 "if subprogram is primitive",
1994 elsif Style_Check -- ??? incorrect use of Style_Check!
1995 and then Is_Overriding_Operation (Spec_Id)
1997 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
1998 Style.Missing_Overriding (N, Body_Id);
2000 end Verify_Overriding_Indicator;
2002 -- Start of processing for Analyze_Subprogram_Body_Helper
2005 -- Generic subprograms are handled separately. They always have a
2006 -- generic specification. Determine whether current scope has a
2007 -- previous declaration.
2009 -- If the subprogram body is defined within an instance of the same
2010 -- name, the instance appears as a package renaming, and will be hidden
2011 -- within the subprogram.
2013 if Present (Prev_Id)
2014 and then not Is_Overloadable (Prev_Id)
2015 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2016 or else Comes_From_Source (Prev_Id))
2018 if Is_Generic_Subprogram (Prev_Id) then
2020 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2021 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2023 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2025 if Nkind (N) = N_Subprogram_Body then
2026 HSS := Handled_Statement_Sequence (N);
2027 Check_Missing_Return;
2033 -- Previous entity conflicts with subprogram name. Attempting to
2034 -- enter name will post error.
2036 Enter_Name (Body_Id);
2040 -- Non-generic case, find the subprogram declaration, if one was seen,
2041 -- or enter new overloaded entity in the current scope. If the
2042 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2043 -- part of the context of one of its subunits. No need to redo the
2046 elsif Prev_Id = Body_Id
2047 and then Has_Completion (Body_Id)
2052 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2054 if Nkind (N) = N_Subprogram_Body_Stub
2055 or else No (Corresponding_Spec (N))
2057 if Is_Private_Concurrent_Primitive (Body_Id) then
2058 Spec_Id := Disambiguate_Spec;
2060 Spec_Id := Find_Corresponding_Spec (N);
2063 -- If this is a duplicate body, no point in analyzing it
2065 if Error_Posted (N) then
2069 -- A subprogram body should cause freezing of its own declaration,
2070 -- but if there was no previous explicit declaration, then the
2071 -- subprogram will get frozen too late (there may be code within
2072 -- the body that depends on the subprogram having been frozen,
2073 -- such as uses of extra formals), so we force it to be frozen
2074 -- here. Same holds if the body and spec are compilation units.
2075 -- Finally, if the return type is an anonymous access to protected
2076 -- subprogram, it must be frozen before the body because its
2077 -- expansion has generated an equivalent type that is used when
2078 -- elaborating the body.
2080 if No (Spec_Id) then
2081 Freeze_Before (N, Body_Id);
2083 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2084 Freeze_Before (N, Spec_Id);
2086 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2087 Freeze_Before (N, Etype (Body_Id));
2091 Spec_Id := Corresponding_Spec (N);
2095 -- Do not inline any subprogram that contains nested subprograms, since
2096 -- the backend inlining circuit seems to generate uninitialized
2097 -- references in this case. We know this happens in the case of front
2098 -- end ZCX support, but it also appears it can happen in other cases as
2099 -- well. The backend often rejects attempts to inline in the case of
2100 -- nested procedures anyway, so little if anything is lost by this.
2101 -- Note that this is test is for the benefit of the back-end. There is
2102 -- a separate test for front-end inlining that also rejects nested
2105 -- Do not do this test if errors have been detected, because in some
2106 -- error cases, this code blows up, and we don't need it anyway if
2107 -- there have been errors, since we won't get to the linker anyway.
2109 if Comes_From_Source (Body_Id)
2110 and then Serious_Errors_Detected = 0
2114 P_Ent := Scope (P_Ent);
2115 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2117 if Is_Subprogram (P_Ent) then
2118 Set_Is_Inlined (P_Ent, False);
2120 if Comes_From_Source (P_Ent)
2121 and then Has_Pragma_Inline (P_Ent)
2124 ("cannot inline& (nested subprogram)?",
2131 Check_Inline_Pragma (Spec_Id);
2133 -- Deal with special case of a fully private operation in the body of
2134 -- the protected type. We must create a declaration for the subprogram,
2135 -- in order to attach the protected subprogram that will be used in
2136 -- internal calls. We exclude compiler generated bodies from the
2137 -- expander since the issue does not arise for those cases.
2140 and then Comes_From_Source (N)
2141 and then Is_Protected_Type (Current_Scope)
2143 Spec_Id := Build_Private_Protected_Declaration (N);
2146 -- If a separate spec is present, then deal with freezing issues
2148 if Present (Spec_Id) then
2149 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2150 Verify_Overriding_Indicator;
2152 -- In general, the spec will be frozen when we start analyzing the
2153 -- body. However, for internally generated operations, such as
2154 -- wrapper functions for inherited operations with controlling
2155 -- results, the spec may not have been frozen by the time we
2156 -- expand the freeze actions that include the bodies. In particular,
2157 -- extra formals for accessibility or for return-in-place may need
2158 -- to be generated. Freeze nodes, if any, are inserted before the
2161 if not Is_Frozen (Spec_Id)
2162 and then Expander_Active
2164 -- Force the generation of its freezing node to ensure proper
2165 -- management of access types in the backend.
2167 -- This is definitely needed for some cases, but it is not clear
2168 -- why, to be investigated further???
2170 Set_Has_Delayed_Freeze (Spec_Id);
2171 Freeze_Before (N, Spec_Id);
2175 -- Mark presence of postcondition procedure in current scope and mark
2176 -- the procedure itself as needing debug info. The latter is important
2177 -- when analyzing decision coverage (for example, for MC/DC coverage).
2179 if Chars (Body_Id) = Name_uPostconditions then
2180 Set_Has_Postconditions (Current_Scope);
2181 Set_Debug_Info_Needed (Body_Id);
2184 -- Place subprogram on scope stack, and make formals visible. If there
2185 -- is a spec, the visible entity remains that of the spec.
2187 if Present (Spec_Id) then
2188 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2190 if Is_Child_Unit (Spec_Id) then
2191 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2195 Style.Check_Identifier (Body_Id, Spec_Id);
2198 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2199 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2201 if Is_Abstract_Subprogram (Spec_Id) then
2202 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2206 Set_Convention (Body_Id, Convention (Spec_Id));
2207 Set_Has_Completion (Spec_Id);
2209 if Is_Protected_Type (Scope (Spec_Id)) then
2210 Prot_Typ := Scope (Spec_Id);
2213 -- If this is a body generated for a renaming, do not check for
2214 -- full conformance. The check is redundant, because the spec of
2215 -- the body is a copy of the spec in the renaming declaration,
2216 -- and the test can lead to spurious errors on nested defaults.
2218 if Present (Spec_Decl)
2219 and then not Comes_From_Source (N)
2221 (Nkind (Original_Node (Spec_Decl)) =
2222 N_Subprogram_Renaming_Declaration
2223 or else (Present (Corresponding_Body (Spec_Decl))
2225 Nkind (Unit_Declaration_Node
2226 (Corresponding_Body (Spec_Decl))) =
2227 N_Subprogram_Renaming_Declaration))
2231 -- Conversely, the spec may have been generated for specless body
2232 -- with an inline pragma.
2234 elsif Comes_From_Source (N)
2235 and then not Comes_From_Source (Spec_Id)
2236 and then Has_Pragma_Inline (Spec_Id)
2243 Fully_Conformant, True, Conformant, Body_Id);
2246 -- If the body is not fully conformant, we have to decide if we
2247 -- should analyze it or not. If it has a really messed up profile
2248 -- then we probably should not analyze it, since we will get too
2249 -- many bogus messages.
2251 -- Our decision is to go ahead in the non-fully conformant case
2252 -- only if it is at least mode conformant with the spec. Note
2253 -- that the call to Check_Fully_Conformant has issued the proper
2254 -- error messages to complain about the lack of conformance.
2257 and then not Mode_Conformant (Body_Id, Spec_Id)
2263 if Spec_Id /= Body_Id then
2264 Reference_Body_Formals (Spec_Id, Body_Id);
2267 if Nkind (N) /= N_Subprogram_Body_Stub then
2268 Set_Corresponding_Spec (N, Spec_Id);
2270 -- Ada 2005 (AI-345): If the operation is a primitive operation
2271 -- of a concurrent type, the type of the first parameter has been
2272 -- replaced with the corresponding record, which is the proper
2273 -- run-time structure to use. However, within the body there may
2274 -- be uses of the formals that depend on primitive operations
2275 -- of the type (in particular calls in prefixed form) for which
2276 -- we need the original concurrent type. The operation may have
2277 -- several controlling formals, so the replacement must be done
2280 if Comes_From_Source (Spec_Id)
2281 and then Present (First_Entity (Spec_Id))
2282 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2283 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2285 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2288 (Corresponding_Concurrent_Type
2289 (Etype (First_Entity (Spec_Id))))
2292 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2296 Form := First_Formal (Spec_Id);
2297 while Present (Form) loop
2298 if Etype (Form) = Typ then
2299 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2307 -- Make the formals visible, and place subprogram on scope stack.
2308 -- This is also the point at which we set Last_Real_Spec_Entity
2309 -- to mark the entities which will not be moved to the body.
2311 Install_Formals (Spec_Id);
2312 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2313 Push_Scope (Spec_Id);
2315 -- Make sure that the subprogram is immediately visible. For
2316 -- child units that have no separate spec this is indispensable.
2317 -- Otherwise it is safe albeit redundant.
2319 Set_Is_Immediately_Visible (Spec_Id);
2322 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2323 Set_Ekind (Body_Id, E_Subprogram_Body);
2324 Set_Scope (Body_Id, Scope (Spec_Id));
2325 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2327 -- Case of subprogram body with no previous spec
2331 and then Comes_From_Source (Body_Id)
2332 and then not Suppress_Style_Checks (Body_Id)
2333 and then not In_Instance
2335 Style.Body_With_No_Spec (N);
2338 New_Overloaded_Entity (Body_Id);
2340 if Nkind (N) /= N_Subprogram_Body_Stub then
2341 Set_Acts_As_Spec (N);
2342 Generate_Definition (Body_Id);
2344 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2345 Generate_Reference_To_Formals (Body_Id);
2346 Install_Formals (Body_Id);
2347 Push_Scope (Body_Id);
2351 -- If the return type is an anonymous access type whose designated type
2352 -- is the limited view of a class-wide type and the non-limited view is
2353 -- available, update the return type accordingly.
2355 if Ada_Version >= Ada_2005
2356 and then Comes_From_Source (N)
2363 Rtyp := Etype (Current_Scope);
2365 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2366 Etyp := Directly_Designated_Type (Rtyp);
2368 if Is_Class_Wide_Type (Etyp)
2369 and then From_With_Type (Etyp)
2371 Set_Directly_Designated_Type
2372 (Etype (Current_Scope), Available_View (Etyp));
2378 -- If this is the proper body of a stub, we must verify that the stub
2379 -- conforms to the body, and to the previous spec if one was present.
2380 -- we know already that the body conforms to that spec. This test is
2381 -- only required for subprograms that come from source.
2383 if Nkind (Parent (N)) = N_Subunit
2384 and then Comes_From_Source (N)
2385 and then not Error_Posted (Body_Id)
2386 and then Nkind (Corresponding_Stub (Parent (N))) =
2387 N_Subprogram_Body_Stub
2390 Old_Id : constant Entity_Id :=
2392 (Specification (Corresponding_Stub (Parent (N))));
2394 Conformant : Boolean := False;
2397 if No (Spec_Id) then
2398 Check_Fully_Conformant (Body_Id, Old_Id);
2402 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2404 if not Conformant then
2406 -- The stub was taken to be a new declaration. Indicate
2407 -- that it lacks a body.
2409 Set_Has_Completion (Old_Id, False);
2415 Set_Has_Completion (Body_Id);
2416 Check_Eliminated (Body_Id);
2418 if Nkind (N) = N_Subprogram_Body_Stub then
2421 elsif Present (Spec_Id)
2422 and then Expander_Active
2424 (Has_Pragma_Inline_Always (Spec_Id)
2425 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2427 Build_Body_To_Inline (N, Spec_Id);
2430 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2431 -- if its specification we have to install the private withed units.
2432 -- This holds for child units as well.
2434 if Is_Compilation_Unit (Body_Id)
2435 or else Nkind (Parent (N)) = N_Compilation_Unit
2437 Install_Private_With_Clauses (Body_Id);
2440 Check_Anonymous_Return;
2442 -- Set the Protected_Formal field of each extra formal of the protected
2443 -- subprogram to reference the corresponding extra formal of the
2444 -- subprogram that implements it. For regular formals this occurs when
2445 -- the protected subprogram's declaration is expanded, but the extra
2446 -- formals don't get created until the subprogram is frozen. We need to
2447 -- do this before analyzing the protected subprogram's body so that any
2448 -- references to the original subprogram's extra formals will be changed
2449 -- refer to the implementing subprogram's formals (see Expand_Formal).
2451 if Present (Spec_Id)
2452 and then Is_Protected_Type (Scope (Spec_Id))
2453 and then Present (Protected_Body_Subprogram (Spec_Id))
2456 Impl_Subp : constant Entity_Id :=
2457 Protected_Body_Subprogram (Spec_Id);
2458 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2459 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2461 while Present (Prot_Ext_Formal) loop
2462 pragma Assert (Present (Impl_Ext_Formal));
2463 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2464 Next_Formal_With_Extras (Prot_Ext_Formal);
2465 Next_Formal_With_Extras (Impl_Ext_Formal);
2470 -- Now we can go on to analyze the body
2472 HSS := Handled_Statement_Sequence (N);
2473 Set_Actual_Subtypes (N, Current_Scope);
2475 -- Deal with preconditions and postconditions
2477 Process_PPCs (N, Spec_Id, Body_Id);
2479 -- Add a declaration for the Protection object, renaming declarations
2480 -- for discriminals and privals and finally a declaration for the entry
2481 -- family index (if applicable). This form of early expansion is done
2482 -- when the Expander is active because Install_Private_Data_Declarations
2483 -- references entities which were created during regular expansion.
2486 and then Comes_From_Source (N)
2487 and then Present (Prot_Typ)
2488 and then Present (Spec_Id)
2489 and then not Is_Eliminated (Spec_Id)
2491 Install_Private_Data_Declarations
2492 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2495 -- Analyze the declarations (this call will analyze the precondition
2496 -- Check pragmas we prepended to the list, as well as the declaration
2497 -- of the _Postconditions procedure).
2499 Analyze_Declarations (Declarations (N));
2501 -- Check completion, and analyze the statements
2504 Inspect_Deferred_Constant_Completion (Declarations (N));
2507 -- Deal with end of scope processing for the body
2509 Process_End_Label (HSS, 't', Current_Scope);
2511 Check_Subprogram_Order (N);
2512 Set_Analyzed (Body_Id);
2514 -- If we have a separate spec, then the analysis of the declarations
2515 -- caused the entities in the body to be chained to the spec id, but
2516 -- we want them chained to the body id. Only the formal parameters
2517 -- end up chained to the spec id in this case.
2519 if Present (Spec_Id) then
2521 -- We must conform to the categorization of our spec
2523 Validate_Categorization_Dependency (N, Spec_Id);
2525 -- And if this is a child unit, the parent units must conform
2527 if Is_Child_Unit (Spec_Id) then
2528 Validate_Categorization_Dependency
2529 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2532 -- Here is where we move entities from the spec to the body
2534 -- Case where there are entities that stay with the spec
2536 if Present (Last_Real_Spec_Entity) then
2538 -- No body entities (happens when the only real spec entities
2539 -- come from precondition and postcondition pragmas)
2541 if No (Last_Entity (Body_Id)) then
2543 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2545 -- Body entities present (formals), so chain stuff past them
2549 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2552 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2553 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2554 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2556 -- Case where there are no spec entities, in this case there can
2557 -- be no body entities either, so just move everything.
2560 pragma Assert (No (Last_Entity (Body_Id)));
2561 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2562 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2563 Set_First_Entity (Spec_Id, Empty);
2564 Set_Last_Entity (Spec_Id, Empty);
2568 Check_Missing_Return;
2570 -- Now we are going to check for variables that are never modified in
2571 -- the body of the procedure. But first we deal with a special case
2572 -- where we want to modify this check. If the body of the subprogram
2573 -- starts with a raise statement or its equivalent, or if the body
2574 -- consists entirely of a null statement, then it is pretty obvious
2575 -- that it is OK to not reference the parameters. For example, this
2576 -- might be the following common idiom for a stubbed function:
2577 -- statement of the procedure raises an exception. In particular this
2578 -- deals with the common idiom of a stubbed function, which might
2579 -- appear as something like
2581 -- function F (A : Integer) return Some_Type;
2584 -- raise Program_Error;
2588 -- Here the purpose of X is simply to satisfy the annoying requirement
2589 -- in Ada that there be at least one return, and we certainly do not
2590 -- want to go posting warnings on X that it is not initialized! On
2591 -- the other hand, if X is entirely unreferenced that should still
2594 -- What we do is to detect these cases, and if we find them, flag the
2595 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2596 -- suppress unwanted warnings. For the case of the function stub above
2597 -- we have a special test to set X as apparently assigned to suppress
2604 -- Skip initial labels (for one thing this occurs when we are in
2605 -- front end ZCX mode, but in any case it is irrelevant), and also
2606 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2608 Stm := First (Statements (HSS));
2609 while Nkind (Stm) = N_Label
2610 or else Nkind (Stm) in N_Push_xxx_Label
2615 -- Do the test on the original statement before expansion
2618 Ostm : constant Node_Id := Original_Node (Stm);
2621 -- If explicit raise statement, turn on flag
2623 if Nkind (Ostm) = N_Raise_Statement then
2624 Set_Trivial_Subprogram (Stm);
2626 -- If null statement, and no following statements, turn on flag
2628 elsif Nkind (Stm) = N_Null_Statement
2629 and then Comes_From_Source (Stm)
2630 and then No (Next (Stm))
2632 Set_Trivial_Subprogram (Stm);
2634 -- Check for explicit call cases which likely raise an exception
2636 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
2637 if Is_Entity_Name (Name (Ostm)) then
2639 Ent : constant Entity_Id := Entity (Name (Ostm));
2642 -- If the procedure is marked No_Return, then likely it
2643 -- raises an exception, but in any case it is not coming
2644 -- back here, so turn on the flag.
2646 if Ekind (Ent) = E_Procedure
2647 and then No_Return (Ent)
2649 Set_Trivial_Subprogram (Stm);
2657 -- Check for variables that are never modified
2663 -- If there is a separate spec, then transfer Never_Set_In_Source
2664 -- flags from out parameters to the corresponding entities in the
2665 -- body. The reason we do that is we want to post error flags on
2666 -- the body entities, not the spec entities.
2668 if Present (Spec_Id) then
2669 E1 := First_Entity (Spec_Id);
2670 while Present (E1) loop
2671 if Ekind (E1) = E_Out_Parameter then
2672 E2 := First_Entity (Body_Id);
2673 while Present (E2) loop
2674 exit when Chars (E1) = Chars (E2);
2678 if Present (E2) then
2679 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
2687 -- Check references in body unless it was deleted. Note that the
2688 -- check of Body_Deleted here is not just for efficiency, it is
2689 -- necessary to avoid junk warnings on formal parameters.
2691 if not Body_Deleted then
2692 Check_References (Body_Id);
2695 end Analyze_Subprogram_Body_Helper;
2697 ------------------------------------
2698 -- Analyze_Subprogram_Declaration --
2699 ------------------------------------
2701 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
2702 Loc : constant Source_Ptr := Sloc (N);
2703 Scop : constant Entity_Id := Current_Scope;
2704 Designator : Entity_Id;
2706 Null_Body : Node_Id := Empty;
2708 -- Start of processing for Analyze_Subprogram_Declaration
2711 -- For a null procedure, capture the profile before analysis, for
2712 -- expansion at the freeze point and at each point of call. The body
2713 -- will only be used if the procedure has preconditions. In that case
2714 -- the body is analyzed at the freeze point.
2716 if Nkind (Specification (N)) = N_Procedure_Specification
2717 and then Null_Present (Specification (N))
2718 and then Expander_Active
2721 Make_Subprogram_Body (Loc,
2723 New_Copy_Tree (Specification (N)),
2726 Handled_Statement_Sequence =>
2727 Make_Handled_Sequence_Of_Statements (Loc,
2728 Statements => New_List (Make_Null_Statement (Loc))));
2730 -- Create new entities for body and formals
2732 Set_Defining_Unit_Name (Specification (Null_Body),
2733 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
2734 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2736 Form := First (Parameter_Specifications (Specification (Null_Body)));
2737 while Present (Form) loop
2738 Set_Defining_Identifier (Form,
2739 Make_Defining_Identifier (Loc,
2740 Chars (Defining_Identifier (Form))));
2742 -- Resolve the types of the formals now, because the freeze point
2743 -- may appear in a different context, e.g. an instantiation.
2745 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
2746 Find_Type (Parameter_Type (Form));
2749 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
2751 Find_Type (Subtype_Mark (Parameter_Type (Form)));
2755 -- the case of a null procedure with a formal that is an
2756 -- access_to_subprogram type, and that is used as an actual
2757 -- in an instantiation is left to the enthusiastic reader.
2765 if Is_Protected_Type (Current_Scope) then
2766 Error_Msg_N ("protected operation cannot be a null procedure", N);
2770 Designator := Analyze_Subprogram_Specification (Specification (N));
2771 Generate_Definition (Designator);
2773 if Debug_Flag_C then
2774 Write_Str ("==> subprogram spec ");
2775 Write_Name (Chars (Designator));
2776 Write_Str (" from ");
2777 Write_Location (Sloc (N));
2782 if Nkind (Specification (N)) = N_Procedure_Specification
2783 and then Null_Present (Specification (N))
2785 Set_Has_Completion (Designator);
2787 if Present (Null_Body) then
2788 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
2789 Set_Body_To_Inline (N, Null_Body);
2790 Set_Is_Inlined (Designator);
2794 Validate_RCI_Subprogram_Declaration (N);
2795 New_Overloaded_Entity (Designator);
2796 Check_Delayed_Subprogram (Designator);
2798 -- If the type of the first formal of the current subprogram is a
2799 -- nongeneric tagged private type, mark the subprogram as being a
2800 -- private primitive. Ditto if this is a function with controlling
2801 -- result, and the return type is currently private. In both cases,
2802 -- the type of the controlling argument or result must be in the
2803 -- current scope for the operation to be primitive.
2805 if Has_Controlling_Result (Designator)
2806 and then Is_Private_Type (Etype (Designator))
2807 and then Scope (Etype (Designator)) = Current_Scope
2808 and then not Is_Generic_Actual_Type (Etype (Designator))
2810 Set_Is_Private_Primitive (Designator);
2812 elsif Present (First_Formal (Designator)) then
2814 Formal_Typ : constant Entity_Id :=
2815 Etype (First_Formal (Designator));
2817 Set_Is_Private_Primitive (Designator,
2818 Is_Tagged_Type (Formal_Typ)
2819 and then Scope (Formal_Typ) = Current_Scope
2820 and then Is_Private_Type (Formal_Typ)
2821 and then not Is_Generic_Actual_Type (Formal_Typ));
2825 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
2828 if Ada_Version >= Ada_2005
2829 and then Comes_From_Source (N)
2830 and then Is_Dispatching_Operation (Designator)
2837 if Has_Controlling_Result (Designator) then
2838 Etyp := Etype (Designator);
2841 E := First_Entity (Designator);
2843 and then Is_Formal (E)
2844 and then not Is_Controlling_Formal (E)
2852 if Is_Access_Type (Etyp) then
2853 Etyp := Directly_Designated_Type (Etyp);
2856 if Is_Interface (Etyp)
2857 and then not Is_Abstract_Subprogram (Designator)
2858 and then not (Ekind (Designator) = E_Procedure
2859 and then Null_Present (Specification (N)))
2861 Error_Msg_Name_1 := Chars (Defining_Entity (N));
2863 ("(Ada 2005) interface subprogram % must be abstract or null",
2869 -- What is the following code for, it used to be
2871 -- ??? Set_Suppress_Elaboration_Checks
2872 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
2874 -- The following seems equivalent, but a bit dubious
2876 if Elaboration_Checks_Suppressed (Designator) then
2877 Set_Kill_Elaboration_Checks (Designator);
2880 if Scop /= Standard_Standard
2881 and then not Is_Child_Unit (Designator)
2883 Set_Categorization_From_Scope (Designator, Scop);
2885 -- For a compilation unit, check for library-unit pragmas
2887 Push_Scope (Designator);
2888 Set_Categorization_From_Pragmas (N);
2889 Validate_Categorization_Dependency (N, Designator);
2893 -- For a compilation unit, set body required. This flag will only be
2894 -- reset if a valid Import or Interface pragma is processed later on.
2896 if Nkind (Parent (N)) = N_Compilation_Unit then
2897 Set_Body_Required (Parent (N), True);
2899 if Ada_Version >= Ada_2005
2900 and then Nkind (Specification (N)) = N_Procedure_Specification
2901 and then Null_Present (Specification (N))
2904 ("null procedure cannot be declared at library level", N);
2908 Generate_Reference_To_Formals (Designator);
2909 Check_Eliminated (Designator);
2911 if Debug_Flag_C then
2913 Write_Str ("<== subprogram spec ");
2914 Write_Name (Chars (Designator));
2915 Write_Str (" from ");
2916 Write_Location (Sloc (N));
2920 List_Inherited_Pre_Post_Aspects (Designator);
2921 Analyze_Aspect_Specifications (N, Designator, Aspect_Specifications (N));
2922 end Analyze_Subprogram_Declaration;
2924 --------------------------------------
2925 -- Analyze_Subprogram_Specification --
2926 --------------------------------------
2928 -- Reminder: N here really is a subprogram specification (not a subprogram
2929 -- declaration). This procedure is called to analyze the specification in
2930 -- both subprogram bodies and subprogram declarations (specs).
2932 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
2933 Designator : constant Entity_Id := Defining_Entity (N);
2934 Formals : constant List_Id := Parameter_Specifications (N);
2936 -- Start of processing for Analyze_Subprogram_Specification
2939 Generate_Definition (Designator);
2941 if Nkind (N) = N_Function_Specification then
2942 Set_Ekind (Designator, E_Function);
2943 Set_Mechanism (Designator, Default_Mechanism);
2945 Set_Ekind (Designator, E_Procedure);
2946 Set_Etype (Designator, Standard_Void_Type);
2949 -- Introduce new scope for analysis of the formals and the return type
2951 Set_Scope (Designator, Current_Scope);
2953 if Present (Formals) then
2954 Push_Scope (Designator);
2955 Process_Formals (Formals, N);
2957 -- Ada 2005 (AI-345): If this is an overriding operation of an
2958 -- inherited interface operation, and the controlling type is
2959 -- a synchronized type, replace the type with its corresponding
2960 -- record, to match the proper signature of an overriding operation.
2961 -- Same processing for an access parameter whose designated type is
2962 -- derived from a synchronized interface.
2964 if Ada_Version >= Ada_2005 then
2967 Formal_Typ : Entity_Id;
2968 Rec_Typ : Entity_Id;
2969 Desig_Typ : Entity_Id;
2972 Formal := First_Formal (Designator);
2973 while Present (Formal) loop
2974 Formal_Typ := Etype (Formal);
2976 if Is_Concurrent_Type (Formal_Typ)
2977 and then Present (Corresponding_Record_Type (Formal_Typ))
2979 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
2981 if Present (Interfaces (Rec_Typ)) then
2982 Set_Etype (Formal, Rec_Typ);
2985 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
2986 Desig_Typ := Designated_Type (Formal_Typ);
2988 if Is_Concurrent_Type (Desig_Typ)
2989 and then Present (Corresponding_Record_Type (Desig_Typ))
2991 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
2993 if Present (Interfaces (Rec_Typ)) then
2994 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
2999 Next_Formal (Formal);
3006 -- The subprogram scope is pushed and popped around the processing of
3007 -- the return type for consistency with call above to Process_Formals
3008 -- (which itself can call Analyze_Return_Type), and to ensure that any
3009 -- itype created for the return type will be associated with the proper
3012 elsif Nkind (N) = N_Function_Specification then
3013 Push_Scope (Designator);
3014 Analyze_Return_Type (N);
3020 if Nkind (N) = N_Function_Specification then
3022 -- Deal with operator symbol case
3024 if Nkind (Designator) = N_Defining_Operator_Symbol then
3025 Valid_Operator_Definition (Designator);
3028 May_Need_Actuals (Designator);
3030 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3031 -- the subprogram is abstract also. This does not apply to renaming
3032 -- declarations, where abstractness is inherited.
3034 -- In case of primitives associated with abstract interface types
3035 -- the check is applied later (see Analyze_Subprogram_Declaration).
3037 if not Nkind_In (Parent (N), N_Subprogram_Renaming_Declaration,
3038 N_Abstract_Subprogram_Declaration,
3039 N_Formal_Abstract_Subprogram_Declaration)
3041 if Is_Abstract_Type (Etype (Designator))
3042 and then not Is_Interface (Etype (Designator))
3045 ("function that returns abstract type must be abstract", N);
3047 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3048 -- access result whose designated type is abstract.
3050 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3052 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3053 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3054 and then Ada_Version >= Ada_2012
3056 Error_Msg_N ("function whose access result designates "
3057 & "abstract type must be abstract", N);
3063 end Analyze_Subprogram_Specification;
3065 --------------------------
3066 -- Build_Body_To_Inline --
3067 --------------------------
3069 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3070 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3071 Original_Body : Node_Id;
3072 Body_To_Analyze : Node_Id;
3073 Max_Size : constant := 10;
3074 Stat_Count : Integer := 0;
3076 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3077 -- Check for declarations that make inlining not worthwhile
3079 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3080 -- Check for statements that make inlining not worthwhile: any tasking
3081 -- statement, nested at any level. Keep track of total number of
3082 -- elementary statements, as a measure of acceptable size.
3084 function Has_Pending_Instantiation return Boolean;
3085 -- If some enclosing body contains instantiations that appear before the
3086 -- corresponding generic body, the enclosing body has a freeze node so
3087 -- that it can be elaborated after the generic itself. This might
3088 -- conflict with subsequent inlinings, so that it is unsafe to try to
3089 -- inline in such a case.
3091 function Has_Single_Return return Boolean;
3092 -- In general we cannot inline functions that return unconstrained type.
3093 -- However, we can handle such functions if all return statements return
3094 -- a local variable that is the only declaration in the body of the
3095 -- function. In that case the call can be replaced by that local
3096 -- variable as is done for other inlined calls.
3098 procedure Remove_Pragmas;
3099 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3100 -- parameter has no meaning when the body is inlined and the formals
3101 -- are rewritten. Remove it from body to inline. The analysis of the
3102 -- non-inlined body will handle the pragma properly.
3104 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3105 -- If the body of the subprogram includes a call that returns an
3106 -- unconstrained type, the secondary stack is involved, and it
3107 -- is not worth inlining.
3109 ------------------------------
3110 -- Has_Excluded_Declaration --
3111 ------------------------------
3113 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3116 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3117 -- Nested subprograms make a given body ineligible for inlining, but
3118 -- we make an exception for instantiations of unchecked conversion.
3119 -- The body has not been analyzed yet, so check the name, and verify
3120 -- that the visible entity with that name is the predefined unit.
3122 -----------------------------
3123 -- Is_Unchecked_Conversion --
3124 -----------------------------
3126 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3127 Id : constant Node_Id := Name (D);
3131 if Nkind (Id) = N_Identifier
3132 and then Chars (Id) = Name_Unchecked_Conversion
3134 Conv := Current_Entity (Id);
3136 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3137 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3139 Conv := Current_Entity (Selector_Name (Id));
3144 return Present (Conv)
3145 and then Is_Predefined_File_Name
3146 (Unit_File_Name (Get_Source_Unit (Conv)))
3147 and then Is_Intrinsic_Subprogram (Conv);
3148 end Is_Unchecked_Conversion;
3150 -- Start of processing for Has_Excluded_Declaration
3154 while Present (D) loop
3155 if (Nkind (D) = N_Function_Instantiation
3156 and then not Is_Unchecked_Conversion (D))
3157 or else Nkind_In (D, N_Protected_Type_Declaration,
3158 N_Package_Declaration,
3159 N_Package_Instantiation,
3161 N_Procedure_Instantiation,
3162 N_Task_Type_Declaration)
3165 ("cannot inline & (non-allowed declaration)?", D, Subp);
3173 end Has_Excluded_Declaration;
3175 ----------------------------
3176 -- Has_Excluded_Statement --
3177 ----------------------------
3179 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3185 while Present (S) loop
3186 Stat_Count := Stat_Count + 1;
3188 if Nkind_In (S, N_Abort_Statement,
3189 N_Asynchronous_Select,
3190 N_Conditional_Entry_Call,
3191 N_Delay_Relative_Statement,
3192 N_Delay_Until_Statement,
3197 ("cannot inline & (non-allowed statement)?", S, Subp);
3200 elsif Nkind (S) = N_Block_Statement then
3201 if Present (Declarations (S))
3202 and then Has_Excluded_Declaration (Declarations (S))
3206 elsif Present (Handled_Statement_Sequence (S))
3209 (Exception_Handlers (Handled_Statement_Sequence (S)))
3211 Has_Excluded_Statement
3212 (Statements (Handled_Statement_Sequence (S))))
3217 elsif Nkind (S) = N_Case_Statement then
3218 E := First (Alternatives (S));
3219 while Present (E) loop
3220 if Has_Excluded_Statement (Statements (E)) then
3227 elsif Nkind (S) = N_If_Statement then
3228 if Has_Excluded_Statement (Then_Statements (S)) then
3232 if Present (Elsif_Parts (S)) then
3233 E := First (Elsif_Parts (S));
3234 while Present (E) loop
3235 if Has_Excluded_Statement (Then_Statements (E)) then
3242 if Present (Else_Statements (S))
3243 and then Has_Excluded_Statement (Else_Statements (S))
3248 elsif Nkind (S) = N_Loop_Statement
3249 and then Has_Excluded_Statement (Statements (S))
3253 elsif Nkind (S) = N_Extended_Return_Statement then
3254 if Has_Excluded_Statement
3255 (Statements (Handled_Statement_Sequence (S)))
3257 (Exception_Handlers (Handled_Statement_Sequence (S)))
3267 end Has_Excluded_Statement;
3269 -------------------------------
3270 -- Has_Pending_Instantiation --
3271 -------------------------------
3273 function Has_Pending_Instantiation return Boolean is
3278 while Present (S) loop
3279 if Is_Compilation_Unit (S)
3280 or else Is_Child_Unit (S)
3284 elsif Ekind (S) = E_Package
3285 and then Has_Forward_Instantiation (S)
3294 end Has_Pending_Instantiation;
3296 ------------------------
3297 -- Has_Single_Return --
3298 ------------------------
3300 function Has_Single_Return return Boolean is
3301 Return_Statement : Node_Id := Empty;
3303 function Check_Return (N : Node_Id) return Traverse_Result;
3309 function Check_Return (N : Node_Id) return Traverse_Result is
3311 if Nkind (N) = N_Simple_Return_Statement then
3312 if Present (Expression (N))
3313 and then Is_Entity_Name (Expression (N))
3315 if No (Return_Statement) then
3316 Return_Statement := N;
3319 elsif Chars (Expression (N)) =
3320 Chars (Expression (Return_Statement))
3328 -- A return statement within an extended return is a noop
3331 elsif No (Expression (N))
3332 and then Nkind (Parent (Parent (N))) =
3333 N_Extended_Return_Statement
3338 -- Expression has wrong form
3343 -- We can only inline a build-in-place function if
3344 -- it has a single extended return.
3346 elsif Nkind (N) = N_Extended_Return_Statement then
3347 if No (Return_Statement) then
3348 Return_Statement := N;
3360 function Check_All_Returns is new Traverse_Func (Check_Return);
3362 -- Start of processing for Has_Single_Return
3365 if Check_All_Returns (N) /= OK then
3368 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3372 return Present (Declarations (N))
3373 and then Present (First (Declarations (N)))
3374 and then Chars (Expression (Return_Statement)) =
3375 Chars (Defining_Identifier (First (Declarations (N))));
3377 end Has_Single_Return;
3379 --------------------
3380 -- Remove_Pragmas --
3381 --------------------
3383 procedure Remove_Pragmas is
3388 Decl := First (Declarations (Body_To_Analyze));
3389 while Present (Decl) loop
3392 if Nkind (Decl) = N_Pragma
3393 and then (Pragma_Name (Decl) = Name_Unreferenced
3395 Pragma_Name (Decl) = Name_Unmodified)
3404 --------------------------
3405 -- Uses_Secondary_Stack --
3406 --------------------------
3408 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3409 function Check_Call (N : Node_Id) return Traverse_Result;
3410 -- Look for function calls that return an unconstrained type
3416 function Check_Call (N : Node_Id) return Traverse_Result is
3418 if Nkind (N) = N_Function_Call
3419 and then Is_Entity_Name (Name (N))
3420 and then Is_Composite_Type (Etype (Entity (Name (N))))
3421 and then not Is_Constrained (Etype (Entity (Name (N))))
3424 ("cannot inline & (call returns unconstrained type)?",
3432 function Check_Calls is new Traverse_Func (Check_Call);
3435 return Check_Calls (Bod) = Abandon;
3436 end Uses_Secondary_Stack;
3438 -- Start of processing for Build_Body_To_Inline
3441 -- Return immediately if done already
3443 if Nkind (Decl) = N_Subprogram_Declaration
3444 and then Present (Body_To_Inline (Decl))
3448 -- Functions that return unconstrained composite types require
3449 -- secondary stack handling, and cannot currently be inlined, unless
3450 -- all return statements return a local variable that is the first
3451 -- local declaration in the body.
3453 elsif Ekind (Subp) = E_Function
3454 and then not Is_Scalar_Type (Etype (Subp))
3455 and then not Is_Access_Type (Etype (Subp))
3456 and then not Is_Constrained (Etype (Subp))
3458 if not Has_Single_Return then
3460 ("cannot inline & (unconstrained return type)?", N, Subp);
3464 -- Ditto for functions that return controlled types, where controlled
3465 -- actions interfere in complex ways with inlining.
3467 elsif Ekind (Subp) = E_Function
3468 and then Needs_Finalization (Etype (Subp))
3471 ("cannot inline & (controlled return type)?", N, Subp);
3475 if Present (Declarations (N))
3476 and then Has_Excluded_Declaration (Declarations (N))
3481 if Present (Handled_Statement_Sequence (N)) then
3482 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3484 ("cannot inline& (exception handler)?",
3485 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3489 Has_Excluded_Statement
3490 (Statements (Handled_Statement_Sequence (N)))
3496 -- We do not inline a subprogram that is too large, unless it is
3497 -- marked Inline_Always. This pragma does not suppress the other
3498 -- checks on inlining (forbidden declarations, handlers, etc).
3500 if Stat_Count > Max_Size
3501 and then not Has_Pragma_Inline_Always (Subp)
3503 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3507 if Has_Pending_Instantiation then
3509 ("cannot inline& (forward instance within enclosing body)?",
3514 -- Within an instance, the body to inline must be treated as a nested
3515 -- generic, so that the proper global references are preserved.
3517 -- Note that we do not do this at the library level, because it is not
3518 -- needed, and furthermore this causes trouble if front end inlining
3519 -- is activated (-gnatN).
3521 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3522 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3523 Original_Body := Copy_Generic_Node (N, Empty, True);
3525 Original_Body := Copy_Separate_Tree (N);
3528 -- We need to capture references to the formals in order to substitute
3529 -- the actuals at the point of inlining, i.e. instantiation. To treat
3530 -- the formals as globals to the body to inline, we nest it within
3531 -- a dummy parameterless subprogram, declared within the real one.
3532 -- To avoid generating an internal name (which is never public, and
3533 -- which affects serial numbers of other generated names), we use
3534 -- an internal symbol that cannot conflict with user declarations.
3536 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3537 Set_Defining_Unit_Name
3538 (Specification (Original_Body),
3539 Make_Defining_Identifier (Sloc (N), Name_uParent));
3540 Set_Corresponding_Spec (Original_Body, Empty);
3542 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3544 -- Set return type of function, which is also global and does not need
3547 if Ekind (Subp) = E_Function then
3548 Set_Result_Definition (Specification (Body_To_Analyze),
3549 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3552 if No (Declarations (N)) then
3553 Set_Declarations (N, New_List (Body_To_Analyze));
3555 Append (Body_To_Analyze, Declarations (N));
3558 Expander_Mode_Save_And_Set (False);
3561 Analyze (Body_To_Analyze);
3562 Push_Scope (Defining_Entity (Body_To_Analyze));
3563 Save_Global_References (Original_Body);
3565 Remove (Body_To_Analyze);
3567 Expander_Mode_Restore;
3569 -- Restore environment if previously saved
3571 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3575 -- If secondary stk used there is no point in inlining. We have
3576 -- already issued the warning in this case, so nothing to do.
3578 if Uses_Secondary_Stack (Body_To_Analyze) then
3582 Set_Body_To_Inline (Decl, Original_Body);
3583 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
3584 Set_Is_Inlined (Subp);
3585 end Build_Body_To_Inline;
3591 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
3593 -- Do not emit warning if this is a predefined unit which is not the
3594 -- main unit. With validity checks enabled, some predefined subprograms
3595 -- may contain nested subprograms and become ineligible for inlining.
3597 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
3598 and then not In_Extended_Main_Source_Unit (Subp)
3602 elsif Has_Pragma_Inline_Always (Subp) then
3604 -- Remove last character (question mark) to make this into an error,
3605 -- because the Inline_Always pragma cannot be obeyed.
3607 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
3609 elsif Ineffective_Inline_Warnings then
3610 Error_Msg_NE (Msg, N, Subp);
3614 -----------------------
3615 -- Check_Conformance --
3616 -----------------------
3618 procedure Check_Conformance
3619 (New_Id : Entity_Id;
3621 Ctype : Conformance_Type;
3623 Conforms : out Boolean;
3624 Err_Loc : Node_Id := Empty;
3625 Get_Inst : Boolean := False;
3626 Skip_Controlling_Formals : Boolean := False)
3628 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
3629 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
3630 -- If Errmsg is True, then processing continues to post an error message
3631 -- for conformance error on given node. Two messages are output. The
3632 -- first message points to the previous declaration with a general "no
3633 -- conformance" message. The second is the detailed reason, supplied as
3634 -- Msg. The parameter N provide information for a possible & insertion
3635 -- in the message, and also provides the location for posting the
3636 -- message in the absence of a specified Err_Loc location.
3638 -----------------------
3639 -- Conformance_Error --
3640 -----------------------
3642 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
3649 if No (Err_Loc) then
3655 Error_Msg_Sloc := Sloc (Old_Id);
3658 when Type_Conformant =>
3659 Error_Msg_N -- CODEFIX
3660 ("not type conformant with declaration#!", Enode);
3662 when Mode_Conformant =>
3663 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3665 ("not mode conformant with operation inherited#!",
3669 ("not mode conformant with declaration#!", Enode);
3672 when Subtype_Conformant =>
3673 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3675 ("not subtype conformant with operation inherited#!",
3679 ("not subtype conformant with declaration#!", Enode);
3682 when Fully_Conformant =>
3683 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
3684 Error_Msg_N -- CODEFIX
3685 ("not fully conformant with operation inherited#!",
3688 Error_Msg_N -- CODEFIX
3689 ("not fully conformant with declaration#!", Enode);
3693 Error_Msg_NE (Msg, Enode, N);
3695 end Conformance_Error;
3699 Old_Type : constant Entity_Id := Etype (Old_Id);
3700 New_Type : constant Entity_Id := Etype (New_Id);
3701 Old_Formal : Entity_Id;
3702 New_Formal : Entity_Id;
3703 Access_Types_Match : Boolean;
3704 Old_Formal_Base : Entity_Id;
3705 New_Formal_Base : Entity_Id;
3707 -- Start of processing for Check_Conformance
3712 -- We need a special case for operators, since they don't appear
3715 if Ctype = Type_Conformant then
3716 if Ekind (New_Id) = E_Operator
3717 and then Operator_Matches_Spec (New_Id, Old_Id)
3723 -- If both are functions/operators, check return types conform
3725 if Old_Type /= Standard_Void_Type
3726 and then New_Type /= Standard_Void_Type
3729 -- If we are checking interface conformance we omit controlling
3730 -- arguments and result, because we are only checking the conformance
3731 -- of the remaining parameters.
3733 if Has_Controlling_Result (Old_Id)
3734 and then Has_Controlling_Result (New_Id)
3735 and then Skip_Controlling_Formals
3739 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
3740 Conformance_Error ("\return type does not match!", New_Id);
3744 -- Ada 2005 (AI-231): In case of anonymous access types check the
3745 -- null-exclusion and access-to-constant attributes match.
3747 if Ada_Version >= Ada_2005
3748 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
3750 (Can_Never_Be_Null (Old_Type)
3751 /= Can_Never_Be_Null (New_Type)
3752 or else Is_Access_Constant (Etype (Old_Type))
3753 /= Is_Access_Constant (Etype (New_Type)))
3755 Conformance_Error ("\return type does not match!", New_Id);
3759 -- If either is a function/operator and the other isn't, error
3761 elsif Old_Type /= Standard_Void_Type
3762 or else New_Type /= Standard_Void_Type
3764 Conformance_Error ("\functions can only match functions!", New_Id);
3768 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
3769 -- If this is a renaming as body, refine error message to indicate that
3770 -- the conflict is with the original declaration. If the entity is not
3771 -- frozen, the conventions don't have to match, the one of the renamed
3772 -- entity is inherited.
3774 if Ctype >= Subtype_Conformant then
3775 if Convention (Old_Id) /= Convention (New_Id) then
3777 if not Is_Frozen (New_Id) then
3780 elsif Present (Err_Loc)
3781 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
3782 and then Present (Corresponding_Spec (Err_Loc))
3784 Error_Msg_Name_1 := Chars (New_Id);
3786 Name_Ada + Convention_Id'Pos (Convention (New_Id));
3787 Conformance_Error ("\prior declaration for% has convention %!");
3790 Conformance_Error ("\calling conventions do not match!");
3795 elsif Is_Formal_Subprogram (Old_Id)
3796 or else Is_Formal_Subprogram (New_Id)
3798 Conformance_Error ("\formal subprograms not allowed!");
3803 -- Deal with parameters
3805 -- Note: we use the entity information, rather than going directly
3806 -- to the specification in the tree. This is not only simpler, but
3807 -- absolutely necessary for some cases of conformance tests between
3808 -- operators, where the declaration tree simply does not exist!
3810 Old_Formal := First_Formal (Old_Id);
3811 New_Formal := First_Formal (New_Id);
3812 while Present (Old_Formal) and then Present (New_Formal) loop
3813 if Is_Controlling_Formal (Old_Formal)
3814 and then Is_Controlling_Formal (New_Formal)
3815 and then Skip_Controlling_Formals
3817 -- The controlling formals will have different types when
3818 -- comparing an interface operation with its match, but both
3819 -- or neither must be access parameters.
3821 if Is_Access_Type (Etype (Old_Formal))
3823 Is_Access_Type (Etype (New_Formal))
3825 goto Skip_Controlling_Formal;
3828 ("\access parameter does not match!", New_Formal);
3832 if Ctype = Fully_Conformant then
3834 -- Names must match. Error message is more accurate if we do
3835 -- this before checking that the types of the formals match.
3837 if Chars (Old_Formal) /= Chars (New_Formal) then
3838 Conformance_Error ("\name & does not match!", New_Formal);
3840 -- Set error posted flag on new formal as well to stop
3841 -- junk cascaded messages in some cases.
3843 Set_Error_Posted (New_Formal);
3847 -- Null exclusion must match
3849 if Null_Exclusion_Present (Parent (Old_Formal))
3851 Null_Exclusion_Present (Parent (New_Formal))
3853 -- Only give error if both come from source. This should be
3854 -- investigated some time, since it should not be needed ???
3856 if Comes_From_Source (Old_Formal)
3858 Comes_From_Source (New_Formal)
3861 ("\null exclusion for & does not match", New_Formal);
3863 -- Mark error posted on the new formal to avoid duplicated
3864 -- complaint about types not matching.
3866 Set_Error_Posted (New_Formal);
3871 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
3872 -- case occurs whenever a subprogram is being renamed and one of its
3873 -- parameters imposes a null exclusion. For example:
3875 -- type T is null record;
3876 -- type Acc_T is access T;
3877 -- subtype Acc_T_Sub is Acc_T;
3879 -- procedure P (Obj : not null Acc_T_Sub); -- itype
3880 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
3883 Old_Formal_Base := Etype (Old_Formal);
3884 New_Formal_Base := Etype (New_Formal);
3887 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
3888 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
3891 Access_Types_Match := Ada_Version >= Ada_2005
3893 -- Ensure that this rule is only applied when New_Id is a
3894 -- renaming of Old_Id.
3896 and then Nkind (Parent (Parent (New_Id))) =
3897 N_Subprogram_Renaming_Declaration
3898 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
3899 and then Present (Entity (Name (Parent (Parent (New_Id)))))
3900 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
3902 -- Now handle the allowed access-type case
3904 and then Is_Access_Type (Old_Formal_Base)
3905 and then Is_Access_Type (New_Formal_Base)
3907 -- The type kinds must match. The only exception occurs with
3908 -- multiple generics of the form:
3911 -- type F is private; type A is private;
3912 -- type F_Ptr is access F; type A_Ptr is access A;
3913 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
3914 -- package F_Pack is ... package A_Pack is
3915 -- package F_Inst is
3916 -- new F_Pack (A, A_Ptr, A_P);
3918 -- When checking for conformance between the parameters of A_P
3919 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
3920 -- because the compiler has transformed A_Ptr into a subtype of
3921 -- F_Ptr. We catch this case in the code below.
3923 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
3925 (Is_Generic_Type (Old_Formal_Base)
3926 and then Is_Generic_Type (New_Formal_Base)
3927 and then Is_Internal (New_Formal_Base)
3928 and then Etype (Etype (New_Formal_Base)) =
3930 and then Directly_Designated_Type (Old_Formal_Base) =
3931 Directly_Designated_Type (New_Formal_Base)
3932 and then ((Is_Itype (Old_Formal_Base)
3933 and then Can_Never_Be_Null (Old_Formal_Base))
3935 (Is_Itype (New_Formal_Base)
3936 and then Can_Never_Be_Null (New_Formal_Base)));
3938 -- Types must always match. In the visible part of an instance,
3939 -- usual overloading rules for dispatching operations apply, and
3940 -- we check base types (not the actual subtypes).
3942 if In_Instance_Visible_Part
3943 and then Is_Dispatching_Operation (New_Id)
3945 if not Conforming_Types
3946 (T1 => Base_Type (Etype (Old_Formal)),
3947 T2 => Base_Type (Etype (New_Formal)),
3949 Get_Inst => Get_Inst)
3950 and then not Access_Types_Match
3952 Conformance_Error ("\type of & does not match!", New_Formal);
3956 elsif not Conforming_Types
3957 (T1 => Old_Formal_Base,
3958 T2 => New_Formal_Base,
3960 Get_Inst => Get_Inst)
3961 and then not Access_Types_Match
3963 -- Don't give error message if old type is Any_Type. This test
3964 -- avoids some cascaded errors, e.g. in case of a bad spec.
3966 if Errmsg and then Old_Formal_Base = Any_Type then
3969 Conformance_Error ("\type of & does not match!", New_Formal);
3975 -- For mode conformance, mode must match
3977 if Ctype >= Mode_Conformant then
3978 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
3979 Conformance_Error ("\mode of & does not match!", New_Formal);
3982 -- Part of mode conformance for access types is having the same
3983 -- constant modifier.
3985 elsif Access_Types_Match
3986 and then Is_Access_Constant (Old_Formal_Base) /=
3987 Is_Access_Constant (New_Formal_Base)
3990 ("\constant modifier does not match!", New_Formal);
3995 if Ctype >= Subtype_Conformant then
3997 -- Ada 2005 (AI-231): In case of anonymous access types check
3998 -- the null-exclusion and access-to-constant attributes must
4001 if Ada_Version >= Ada_2005
4002 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4003 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4005 (Can_Never_Be_Null (Old_Formal) /=
4006 Can_Never_Be_Null (New_Formal)
4008 Is_Access_Constant (Etype (Old_Formal)) /=
4009 Is_Access_Constant (Etype (New_Formal)))
4011 -- Do not complain if error already posted on New_Formal. This
4012 -- avoids some redundant error messages.
4014 and then not Error_Posted (New_Formal)
4016 -- It is allowed to omit the null-exclusion in case of stream
4017 -- attribute subprograms. We recognize stream subprograms
4018 -- through their TSS-generated suffix.
4021 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4023 if TSS_Name /= TSS_Stream_Read
4024 and then TSS_Name /= TSS_Stream_Write
4025 and then TSS_Name /= TSS_Stream_Input
4026 and then TSS_Name /= TSS_Stream_Output
4029 ("\type of & does not match!", New_Formal);
4036 -- Full conformance checks
4038 if Ctype = Fully_Conformant then
4040 -- We have checked already that names match
4042 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4044 -- Check default expressions for in parameters
4047 NewD : constant Boolean :=
4048 Present (Default_Value (New_Formal));
4049 OldD : constant Boolean :=
4050 Present (Default_Value (Old_Formal));
4052 if NewD or OldD then
4054 -- The old default value has been analyzed because the
4055 -- current full declaration will have frozen everything
4056 -- before. The new default value has not been analyzed,
4057 -- so analyze it now before we check for conformance.
4060 Push_Scope (New_Id);
4061 Preanalyze_Spec_Expression
4062 (Default_Value (New_Formal), Etype (New_Formal));
4066 if not (NewD and OldD)
4067 or else not Fully_Conformant_Expressions
4068 (Default_Value (Old_Formal),
4069 Default_Value (New_Formal))
4072 ("\default expression for & does not match!",
4081 -- A couple of special checks for Ada 83 mode. These checks are
4082 -- skipped if either entity is an operator in package Standard,
4083 -- or if either old or new instance is not from the source program.
4085 if Ada_Version = Ada_83
4086 and then Sloc (Old_Id) > Standard_Location
4087 and then Sloc (New_Id) > Standard_Location
4088 and then Comes_From_Source (Old_Id)
4089 and then Comes_From_Source (New_Id)
4092 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4093 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4096 -- Explicit IN must be present or absent in both cases. This
4097 -- test is required only in the full conformance case.
4099 if In_Present (Old_Param) /= In_Present (New_Param)
4100 and then Ctype = Fully_Conformant
4103 ("\(Ada 83) IN must appear in both declarations",
4108 -- Grouping (use of comma in param lists) must be the same
4109 -- This is where we catch a misconformance like:
4112 -- A : Integer; B : Integer
4114 -- which are represented identically in the tree except
4115 -- for the setting of the flags More_Ids and Prev_Ids.
4117 if More_Ids (Old_Param) /= More_Ids (New_Param)
4118 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
4121 ("\grouping of & does not match!", New_Formal);
4127 -- This label is required when skipping controlling formals
4129 <<Skip_Controlling_Formal>>
4131 Next_Formal (Old_Formal);
4132 Next_Formal (New_Formal);
4135 if Present (Old_Formal) then
4136 Conformance_Error ("\too few parameters!");
4139 elsif Present (New_Formal) then
4140 Conformance_Error ("\too many parameters!", New_Formal);
4143 end Check_Conformance;
4145 -----------------------
4146 -- Check_Conventions --
4147 -----------------------
4149 procedure Check_Conventions (Typ : Entity_Id) is
4150 Ifaces_List : Elist_Id;
4152 procedure Check_Convention (Op : Entity_Id);
4153 -- Verify that the convention of inherited dispatching operation Op is
4154 -- consistent among all subprograms it overrides. In order to minimize
4155 -- the search, Search_From is utilized to designate a specific point in
4156 -- the list rather than iterating over the whole list once more.
4158 ----------------------
4159 -- Check_Convention --
4160 ----------------------
4162 procedure Check_Convention (Op : Entity_Id) is
4163 Iface_Elmt : Elmt_Id;
4164 Iface_Prim_Elmt : Elmt_Id;
4165 Iface_Prim : Entity_Id;
4168 Iface_Elmt := First_Elmt (Ifaces_List);
4169 while Present (Iface_Elmt) loop
4171 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4172 while Present (Iface_Prim_Elmt) loop
4173 Iface_Prim := Node (Iface_Prim_Elmt);
4175 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4176 and then Convention (Iface_Prim) /= Convention (Op)
4179 ("inconsistent conventions in primitive operations", Typ);
4181 Error_Msg_Name_1 := Chars (Op);
4182 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4183 Error_Msg_Sloc := Sloc (Op);
4185 if Comes_From_Source (Op) or else No (Alias (Op)) then
4186 if not Is_Overriding_Operation (Op) then
4187 Error_Msg_N ("\\primitive % defined #", Typ);
4190 ("\\overriding operation % with " &
4191 "convention % defined #", Typ);
4194 else pragma Assert (Present (Alias (Op)));
4195 Error_Msg_Sloc := Sloc (Alias (Op));
4197 ("\\inherited operation % with " &
4198 "convention % defined #", Typ);
4201 Error_Msg_Name_1 := Chars (Op);
4203 Get_Convention_Name (Convention (Iface_Prim));
4204 Error_Msg_Sloc := Sloc (Iface_Prim);
4206 ("\\overridden operation % with " &
4207 "convention % defined #", Typ);
4209 -- Avoid cascading errors
4214 Next_Elmt (Iface_Prim_Elmt);
4217 Next_Elmt (Iface_Elmt);
4219 end Check_Convention;
4223 Prim_Op : Entity_Id;
4224 Prim_Op_Elmt : Elmt_Id;
4226 -- Start of processing for Check_Conventions
4229 if not Has_Interfaces (Typ) then
4233 Collect_Interfaces (Typ, Ifaces_List);
4235 -- The algorithm checks every overriding dispatching operation against
4236 -- all the corresponding overridden dispatching operations, detecting
4237 -- differences in conventions.
4239 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4240 while Present (Prim_Op_Elmt) loop
4241 Prim_Op := Node (Prim_Op_Elmt);
4243 -- A small optimization: skip the predefined dispatching operations
4244 -- since they always have the same convention.
4246 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4247 Check_Convention (Prim_Op);
4250 Next_Elmt (Prim_Op_Elmt);
4252 end Check_Conventions;
4254 ------------------------------
4255 -- Check_Delayed_Subprogram --
4256 ------------------------------
4258 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4261 procedure Possible_Freeze (T : Entity_Id);
4262 -- T is the type of either a formal parameter or of the return type.
4263 -- If T is not yet frozen and needs a delayed freeze, then the
4264 -- subprogram itself must be delayed. If T is the limited view of an
4265 -- incomplete type the subprogram must be frozen as well, because
4266 -- T may depend on local types that have not been frozen yet.
4268 ---------------------
4269 -- Possible_Freeze --
4270 ---------------------
4272 procedure Possible_Freeze (T : Entity_Id) is
4274 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4275 Set_Has_Delayed_Freeze (Designator);
4277 elsif Is_Access_Type (T)
4278 and then Has_Delayed_Freeze (Designated_Type (T))
4279 and then not Is_Frozen (Designated_Type (T))
4281 Set_Has_Delayed_Freeze (Designator);
4283 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4284 Set_Has_Delayed_Freeze (Designator);
4287 end Possible_Freeze;
4289 -- Start of processing for Check_Delayed_Subprogram
4292 -- All subprograms, including abstract subprograms, may need a freeze
4293 -- node if some formal type or the return type needs one.
4295 Possible_Freeze (Etype (Designator));
4296 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4298 -- Need delayed freeze if any of the formal types themselves need
4299 -- a delayed freeze and are not yet frozen.
4301 F := First_Formal (Designator);
4302 while Present (F) loop
4303 Possible_Freeze (Etype (F));
4304 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4308 -- Mark functions that return by reference. Note that it cannot be
4309 -- done for delayed_freeze subprograms because the underlying
4310 -- returned type may not be known yet (for private types)
4312 if not Has_Delayed_Freeze (Designator)
4313 and then Expander_Active
4316 Typ : constant Entity_Id := Etype (Designator);
4317 Utyp : constant Entity_Id := Underlying_Type (Typ);
4320 if Is_Immutably_Limited_Type (Typ) then
4321 Set_Returns_By_Ref (Designator);
4323 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4324 Set_Returns_By_Ref (Designator);
4328 end Check_Delayed_Subprogram;
4330 ------------------------------------
4331 -- Check_Discriminant_Conformance --
4332 ------------------------------------
4334 procedure Check_Discriminant_Conformance
4339 Old_Discr : Entity_Id := First_Discriminant (Prev);
4340 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4341 New_Discr_Id : Entity_Id;
4342 New_Discr_Type : Entity_Id;
4344 procedure Conformance_Error (Msg : String; N : Node_Id);
4345 -- Post error message for conformance error on given node. Two messages
4346 -- are output. The first points to the previous declaration with a
4347 -- general "no conformance" message. The second is the detailed reason,
4348 -- supplied as Msg. The parameter N provide information for a possible
4349 -- & insertion in the message.
4351 -----------------------
4352 -- Conformance_Error --
4353 -----------------------
4355 procedure Conformance_Error (Msg : String; N : Node_Id) is
4357 Error_Msg_Sloc := Sloc (Prev_Loc);
4358 Error_Msg_N -- CODEFIX
4359 ("not fully conformant with declaration#!", N);
4360 Error_Msg_NE (Msg, N, N);
4361 end Conformance_Error;
4363 -- Start of processing for Check_Discriminant_Conformance
4366 while Present (Old_Discr) and then Present (New_Discr) loop
4368 New_Discr_Id := Defining_Identifier (New_Discr);
4370 -- The subtype mark of the discriminant on the full type has not
4371 -- been analyzed so we do it here. For an access discriminant a new
4374 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4376 Access_Definition (N, Discriminant_Type (New_Discr));
4379 Analyze (Discriminant_Type (New_Discr));
4380 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4382 -- Ada 2005: if the discriminant definition carries a null
4383 -- exclusion, create an itype to check properly for consistency
4384 -- with partial declaration.
4386 if Is_Access_Type (New_Discr_Type)
4387 and then Null_Exclusion_Present (New_Discr)
4390 Create_Null_Excluding_Itype
4391 (T => New_Discr_Type,
4392 Related_Nod => New_Discr,
4393 Scope_Id => Current_Scope);
4397 if not Conforming_Types
4398 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4400 Conformance_Error ("type of & does not match!", New_Discr_Id);
4403 -- Treat the new discriminant as an occurrence of the old one,
4404 -- for navigation purposes, and fill in some semantic
4405 -- information, for completeness.
4407 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4408 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4409 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4414 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4415 Conformance_Error ("name & does not match!", New_Discr_Id);
4419 -- Default expressions must match
4422 NewD : constant Boolean :=
4423 Present (Expression (New_Discr));
4424 OldD : constant Boolean :=
4425 Present (Expression (Parent (Old_Discr)));
4428 if NewD or OldD then
4430 -- The old default value has been analyzed and expanded,
4431 -- because the current full declaration will have frozen
4432 -- everything before. The new default values have not been
4433 -- expanded, so expand now to check conformance.
4436 Preanalyze_Spec_Expression
4437 (Expression (New_Discr), New_Discr_Type);
4440 if not (NewD and OldD)
4441 or else not Fully_Conformant_Expressions
4442 (Expression (Parent (Old_Discr)),
4443 Expression (New_Discr))
4447 ("default expression for & does not match!",
4454 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4456 if Ada_Version = Ada_83 then
4458 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4461 -- Grouping (use of comma in param lists) must be the same
4462 -- This is where we catch a misconformance like:
4465 -- A : Integer; B : Integer
4467 -- which are represented identically in the tree except
4468 -- for the setting of the flags More_Ids and Prev_Ids.
4470 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4471 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4474 ("grouping of & does not match!", New_Discr_Id);
4480 Next_Discriminant (Old_Discr);
4484 if Present (Old_Discr) then
4485 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4488 elsif Present (New_Discr) then
4490 ("too many discriminants!", Defining_Identifier (New_Discr));
4493 end Check_Discriminant_Conformance;
4495 ----------------------------
4496 -- Check_Fully_Conformant --
4497 ----------------------------
4499 procedure Check_Fully_Conformant
4500 (New_Id : Entity_Id;
4502 Err_Loc : Node_Id := Empty)
4505 pragma Warnings (Off, Result);
4508 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4509 end Check_Fully_Conformant;
4511 ---------------------------
4512 -- Check_Mode_Conformant --
4513 ---------------------------
4515 procedure Check_Mode_Conformant
4516 (New_Id : Entity_Id;
4518 Err_Loc : Node_Id := Empty;
4519 Get_Inst : Boolean := False)
4522 pragma Warnings (Off, Result);
4525 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4526 end Check_Mode_Conformant;
4528 --------------------------------
4529 -- Check_Overriding_Indicator --
4530 --------------------------------
4532 procedure Check_Overriding_Indicator
4534 Overridden_Subp : Entity_Id;
4535 Is_Primitive : Boolean)
4541 -- No overriding indicator for literals
4543 if Ekind (Subp) = E_Enumeration_Literal then
4546 elsif Ekind (Subp) = E_Entry then
4547 Decl := Parent (Subp);
4549 -- No point in analyzing a malformed operator
4551 elsif Nkind (Subp) = N_Defining_Operator_Symbol
4552 and then Error_Posted (Subp)
4557 Decl := Unit_Declaration_Node (Subp);
4560 if Nkind_In (Decl, N_Subprogram_Body,
4561 N_Subprogram_Body_Stub,
4562 N_Subprogram_Declaration,
4563 N_Abstract_Subprogram_Declaration,
4564 N_Subprogram_Renaming_Declaration)
4566 Spec := Specification (Decl);
4568 elsif Nkind (Decl) = N_Entry_Declaration then
4575 -- The overriding operation is type conformant with the overridden one,
4576 -- but the names of the formals are not required to match. If the names
4577 -- appear permuted in the overriding operation, this is a possible
4578 -- source of confusion that is worth diagnosing. Controlling formals
4579 -- often carry names that reflect the type, and it is not worthwhile
4580 -- requiring that their names match.
4582 if Present (Overridden_Subp)
4583 and then Nkind (Subp) /= N_Defining_Operator_Symbol
4590 Form1 := First_Formal (Subp);
4591 Form2 := First_Formal (Overridden_Subp);
4593 -- If the overriding operation is a synchronized operation, skip
4594 -- the first parameter of the overridden operation, which is
4595 -- implicit in the new one. If the operation is declared in the
4596 -- body it is not primitive and all formals must match.
4598 if Is_Concurrent_Type (Scope (Subp))
4599 and then Is_Tagged_Type (Scope (Subp))
4600 and then not Has_Completion (Scope (Subp))
4602 Form2 := Next_Formal (Form2);
4605 if Present (Form1) then
4606 Form1 := Next_Formal (Form1);
4607 Form2 := Next_Formal (Form2);
4610 while Present (Form1) loop
4611 if not Is_Controlling_Formal (Form1)
4612 and then Present (Next_Formal (Form2))
4613 and then Chars (Form1) = Chars (Next_Formal (Form2))
4615 Error_Msg_Node_2 := Alias (Overridden_Subp);
4616 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
4618 ("& does not match corresponding formal of&#",
4623 Next_Formal (Form1);
4624 Next_Formal (Form2);
4629 -- If there is an overridden subprogram, then check that there is no
4630 -- "not overriding" indicator, and mark the subprogram as overriding.
4631 -- This is not done if the overridden subprogram is marked as hidden,
4632 -- which can occur for the case of inherited controlled operations
4633 -- (see Derive_Subprogram), unless the inherited subprogram's parent
4634 -- subprogram is not itself hidden. (Note: This condition could probably
4635 -- be simplified, leaving out the testing for the specific controlled
4636 -- cases, but it seems safer and clearer this way, and echoes similar
4637 -- special-case tests of this kind in other places.)
4639 if Present (Overridden_Subp)
4640 and then (not Is_Hidden (Overridden_Subp)
4642 ((Chars (Overridden_Subp) = Name_Initialize
4644 Chars (Overridden_Subp) = Name_Adjust
4646 Chars (Overridden_Subp) = Name_Finalize)
4647 and then Present (Alias (Overridden_Subp))
4648 and then not Is_Hidden (Alias (Overridden_Subp))))
4650 if Must_Not_Override (Spec) then
4651 Error_Msg_Sloc := Sloc (Overridden_Subp);
4653 if Ekind (Subp) = E_Entry then
4655 ("entry & overrides inherited operation #", Spec, Subp);
4658 ("subprogram & overrides inherited operation #", Spec, Subp);
4661 elsif Is_Subprogram (Subp) then
4662 Set_Is_Overriding_Operation (Subp);
4665 -- If primitive flag is set or this is a protected operation, then
4666 -- the operation is overriding at the point of its declaration, so
4667 -- warn if necessary. Otherwise it may have been declared before the
4668 -- operation it overrides and no check is required.
4671 and then not Must_Override (Spec)
4672 and then (Is_Primitive
4673 or else Ekind (Scope (Subp)) = E_Protected_Type)
4675 Style.Missing_Overriding (Decl, Subp);
4678 -- If Subp is an operator, it may override a predefined operation, if
4679 -- it is defined in the same scope as the type to which it applies.
4680 -- In that case Overridden_Subp is empty because of our implicit
4681 -- representation for predefined operators. We have to check whether the
4682 -- signature of Subp matches that of a predefined operator. Note that
4683 -- first argument provides the name of the operator, and the second
4684 -- argument the signature that may match that of a standard operation.
4685 -- If the indicator is overriding, then the operator must match a
4686 -- predefined signature, because we know already that there is no
4687 -- explicit overridden operation.
4689 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
4691 Typ : constant Entity_Id :=
4692 Base_Type (Etype (First_Formal (Subp)));
4694 Can_Override : constant Boolean :=
4695 Operator_Matches_Spec (Subp, Subp)
4696 and then Scope (Subp) = Scope (Typ)
4697 and then not Is_Class_Wide_Type (Typ);
4700 if Must_Not_Override (Spec) then
4702 -- If this is not a primitive or a protected subprogram, then
4703 -- "not overriding" is illegal.
4706 and then Ekind (Scope (Subp)) /= E_Protected_Type
4709 ("overriding indicator only allowed "
4710 & "if subprogram is primitive", Subp);
4712 elsif Can_Override then
4714 ("subprogram& overrides predefined operator ", Spec, Subp);
4717 elsif Must_Override (Spec) then
4718 if Is_Overriding_Operation (Subp) then
4721 elsif not Can_Override then
4722 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4725 elsif not Error_Posted (Subp)
4726 and then Style_Check
4727 and then Can_Override
4729 not Is_Predefined_File_Name
4730 (Unit_File_Name (Get_Source_Unit (Subp)))
4732 Set_Is_Overriding_Operation (Subp);
4734 -- If style checks are enabled, indicate that the indicator is
4735 -- missing. However, at the point of declaration, the type of
4736 -- which this is a primitive operation may be private, in which
4737 -- case the indicator would be premature.
4739 if Has_Private_Declaration (Etype (Subp))
4740 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
4744 Style.Missing_Overriding (Decl, Subp);
4749 elsif Must_Override (Spec) then
4750 if Ekind (Subp) = E_Entry then
4751 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
4753 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
4756 -- If the operation is marked "not overriding" and it's not primitive
4757 -- then an error is issued, unless this is an operation of a task or
4758 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
4759 -- has been specified have already been checked above.
4761 elsif Must_Not_Override (Spec)
4762 and then not Is_Primitive
4763 and then Ekind (Subp) /= E_Entry
4764 and then Ekind (Scope (Subp)) /= E_Protected_Type
4767 ("overriding indicator only allowed if subprogram is primitive",
4771 end Check_Overriding_Indicator;
4777 -- Note: this procedure needs to know far too much about how the expander
4778 -- messes with exceptions. The use of the flag Exception_Junk and the
4779 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
4780 -- works, but is not very clean. It would be better if the expansion
4781 -- routines would leave Original_Node working nicely, and we could use
4782 -- Original_Node here to ignore all the peculiar expander messing ???
4784 procedure Check_Returns
4788 Proc : Entity_Id := Empty)
4792 procedure Check_Statement_Sequence (L : List_Id);
4793 -- Internal recursive procedure to check a list of statements for proper
4794 -- termination by a return statement (or a transfer of control or a
4795 -- compound statement that is itself internally properly terminated).
4797 ------------------------------
4798 -- Check_Statement_Sequence --
4799 ------------------------------
4801 procedure Check_Statement_Sequence (L : List_Id) is
4806 Raise_Exception_Call : Boolean;
4807 -- Set True if statement sequence terminated by Raise_Exception call
4808 -- or a Reraise_Occurrence call.
4811 Raise_Exception_Call := False;
4813 -- Get last real statement
4815 Last_Stm := Last (L);
4817 -- Deal with digging out exception handler statement sequences that
4818 -- have been transformed by the local raise to goto optimization.
4819 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
4820 -- optimization has occurred, we are looking at something like:
4823 -- original stmts in block
4827 -- goto L1; | omitted if No_Exception_Propagation
4832 -- goto L3; -- skip handler when exception not raised
4834 -- <<L1>> -- target label for local exception
4848 -- and what we have to do is to dig out the estmts1 and estmts2
4849 -- sequences (which were the original sequences of statements in
4850 -- the exception handlers) and check them.
4852 if Nkind (Last_Stm) = N_Label
4853 and then Exception_Junk (Last_Stm)
4859 exit when Nkind (Stm) /= N_Block_Statement;
4860 exit when not Exception_Junk (Stm);
4863 exit when Nkind (Stm) /= N_Label;
4864 exit when not Exception_Junk (Stm);
4865 Check_Statement_Sequence
4866 (Statements (Handled_Statement_Sequence (Next (Stm))));
4871 exit when Nkind (Stm) /= N_Goto_Statement;
4872 exit when not Exception_Junk (Stm);
4876 -- Don't count pragmas
4878 while Nkind (Last_Stm) = N_Pragma
4880 -- Don't count call to SS_Release (can happen after Raise_Exception)
4883 (Nkind (Last_Stm) = N_Procedure_Call_Statement
4885 Nkind (Name (Last_Stm)) = N_Identifier
4887 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
4889 -- Don't count exception junk
4892 (Nkind_In (Last_Stm, N_Goto_Statement,
4894 N_Object_Declaration)
4895 and then Exception_Junk (Last_Stm))
4896 or else Nkind (Last_Stm) in N_Push_xxx_Label
4897 or else Nkind (Last_Stm) in N_Pop_xxx_Label
4902 -- Here we have the "real" last statement
4904 Kind := Nkind (Last_Stm);
4906 -- Transfer of control, OK. Note that in the No_Return procedure
4907 -- case, we already diagnosed any explicit return statements, so
4908 -- we can treat them as OK in this context.
4910 if Is_Transfer (Last_Stm) then
4913 -- Check cases of explicit non-indirect procedure calls
4915 elsif Kind = N_Procedure_Call_Statement
4916 and then Is_Entity_Name (Name (Last_Stm))
4918 -- Check call to Raise_Exception procedure which is treated
4919 -- specially, as is a call to Reraise_Occurrence.
4921 -- We suppress the warning in these cases since it is likely that
4922 -- the programmer really does not expect to deal with the case
4923 -- of Null_Occurrence, and thus would find a warning about a
4924 -- missing return curious, and raising Program_Error does not
4925 -- seem such a bad behavior if this does occur.
4927 -- Note that in the Ada 2005 case for Raise_Exception, the actual
4928 -- behavior will be to raise Constraint_Error (see AI-329).
4930 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
4932 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
4934 Raise_Exception_Call := True;
4936 -- For Raise_Exception call, test first argument, if it is
4937 -- an attribute reference for a 'Identity call, then we know
4938 -- that the call cannot possibly return.
4941 Arg : constant Node_Id :=
4942 Original_Node (First_Actual (Last_Stm));
4944 if Nkind (Arg) = N_Attribute_Reference
4945 and then Attribute_Name (Arg) = Name_Identity
4952 -- If statement, need to look inside if there is an else and check
4953 -- each constituent statement sequence for proper termination.
4955 elsif Kind = N_If_Statement
4956 and then Present (Else_Statements (Last_Stm))
4958 Check_Statement_Sequence (Then_Statements (Last_Stm));
4959 Check_Statement_Sequence (Else_Statements (Last_Stm));
4961 if Present (Elsif_Parts (Last_Stm)) then
4963 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
4966 while Present (Elsif_Part) loop
4967 Check_Statement_Sequence (Then_Statements (Elsif_Part));
4975 -- Case statement, check each case for proper termination
4977 elsif Kind = N_Case_Statement then
4981 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
4982 while Present (Case_Alt) loop
4983 Check_Statement_Sequence (Statements (Case_Alt));
4984 Next_Non_Pragma (Case_Alt);
4990 -- Block statement, check its handled sequence of statements
4992 elsif Kind = N_Block_Statement then
4998 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
5007 -- Loop statement. If there is an iteration scheme, we can definitely
5008 -- fall out of the loop. Similarly if there is an exit statement, we
5009 -- can fall out. In either case we need a following return.
5011 elsif Kind = N_Loop_Statement then
5012 if Present (Iteration_Scheme (Last_Stm))
5013 or else Has_Exit (Entity (Identifier (Last_Stm)))
5017 -- A loop with no exit statement or iteration scheme is either
5018 -- an infinite loop, or it has some other exit (raise/return).
5019 -- In either case, no warning is required.
5025 -- Timed entry call, check entry call and delay alternatives
5027 -- Note: in expanded code, the timed entry call has been converted
5028 -- to a set of expanded statements on which the check will work
5029 -- correctly in any case.
5031 elsif Kind = N_Timed_Entry_Call then
5033 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5034 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
5037 -- If statement sequence of entry call alternative is missing,
5038 -- then we can definitely fall through, and we post the error
5039 -- message on the entry call alternative itself.
5041 if No (Statements (ECA)) then
5044 -- If statement sequence of delay alternative is missing, then
5045 -- we can definitely fall through, and we post the error
5046 -- message on the delay alternative itself.
5048 -- Note: if both ECA and DCA are missing the return, then we
5049 -- post only one message, should be enough to fix the bugs.
5050 -- If not we will get a message next time on the DCA when the
5053 elsif No (Statements (DCA)) then
5056 -- Else check both statement sequences
5059 Check_Statement_Sequence (Statements (ECA));
5060 Check_Statement_Sequence (Statements (DCA));
5065 -- Conditional entry call, check entry call and else part
5067 -- Note: in expanded code, the conditional entry call has been
5068 -- converted to a set of expanded statements on which the check
5069 -- will work correctly in any case.
5071 elsif Kind = N_Conditional_Entry_Call then
5073 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5076 -- If statement sequence of entry call alternative is missing,
5077 -- then we can definitely fall through, and we post the error
5078 -- message on the entry call alternative itself.
5080 if No (Statements (ECA)) then
5083 -- Else check statement sequence and else part
5086 Check_Statement_Sequence (Statements (ECA));
5087 Check_Statement_Sequence (Else_Statements (Last_Stm));
5093 -- If we fall through, issue appropriate message
5096 if not Raise_Exception_Call then
5098 ("?RETURN statement missing following this statement!",
5101 ("\?Program_Error may be raised at run time!",
5105 -- Note: we set Err even though we have not issued a warning
5106 -- because we still have a case of a missing return. This is
5107 -- an extremely marginal case, probably will never be noticed
5108 -- but we might as well get it right.
5112 -- Otherwise we have the case of a procedure marked No_Return
5115 if not Raise_Exception_Call then
5117 ("?implied return after this statement " &
5118 "will raise Program_Error",
5121 ("\?procedure & is marked as No_Return!",
5126 RE : constant Node_Id :=
5127 Make_Raise_Program_Error (Sloc (Last_Stm),
5128 Reason => PE_Implicit_Return);
5130 Insert_After (Last_Stm, RE);
5134 end Check_Statement_Sequence;
5136 -- Start of processing for Check_Returns
5140 Check_Statement_Sequence (Statements (HSS));
5142 if Present (Exception_Handlers (HSS)) then
5143 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5144 while Present (Handler) loop
5145 Check_Statement_Sequence (Statements (Handler));
5146 Next_Non_Pragma (Handler);
5151 ----------------------------
5152 -- Check_Subprogram_Order --
5153 ----------------------------
5155 procedure Check_Subprogram_Order (N : Node_Id) is
5157 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5158 -- This is used to check if S1 > S2 in the sense required by this
5159 -- test, for example nameab < namec, but name2 < name10.
5161 -----------------------------
5162 -- Subprogram_Name_Greater --
5163 -----------------------------
5165 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5170 -- Remove trailing numeric parts
5173 while S1 (L1) in '0' .. '9' loop
5178 while S2 (L2) in '0' .. '9' loop
5182 -- If non-numeric parts non-equal, that's decisive
5184 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5187 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5190 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5191 -- that a missing suffix is treated as numeric zero in this test.
5195 while L1 < S1'Last loop
5197 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5201 while L2 < S2'Last loop
5203 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5208 end Subprogram_Name_Greater;
5210 -- Start of processing for Check_Subprogram_Order
5213 -- Check body in alpha order if this is option
5216 and then Style_Check_Order_Subprograms
5217 and then Nkind (N) = N_Subprogram_Body
5218 and then Comes_From_Source (N)
5219 and then In_Extended_Main_Source_Unit (N)
5223 renames Scope_Stack.Table
5224 (Scope_Stack.Last).Last_Subprogram_Name;
5226 Body_Id : constant Entity_Id :=
5227 Defining_Entity (Specification (N));
5230 Get_Decoded_Name_String (Chars (Body_Id));
5233 if Subprogram_Name_Greater
5234 (LSN.all, Name_Buffer (1 .. Name_Len))
5236 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5242 LSN := new String'(Name_Buffer (1 .. Name_Len));
5245 end Check_Subprogram_Order;
5247 ------------------------------
5248 -- Check_Subtype_Conformant --
5249 ------------------------------
5251 procedure Check_Subtype_Conformant
5252 (New_Id : Entity_Id;
5254 Err_Loc : Node_Id := Empty;
5255 Skip_Controlling_Formals : Boolean := False)
5258 pragma Warnings (Off, Result);
5261 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5262 Skip_Controlling_Formals => Skip_Controlling_Formals);
5263 end Check_Subtype_Conformant;
5265 ---------------------------
5266 -- Check_Type_Conformant --
5267 ---------------------------
5269 procedure Check_Type_Conformant
5270 (New_Id : Entity_Id;
5272 Err_Loc : Node_Id := Empty)
5275 pragma Warnings (Off, Result);
5278 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5279 end Check_Type_Conformant;
5281 ----------------------
5282 -- Conforming_Types --
5283 ----------------------
5285 function Conforming_Types
5288 Ctype : Conformance_Type;
5289 Get_Inst : Boolean := False) return Boolean
5291 Type_1 : Entity_Id := T1;
5292 Type_2 : Entity_Id := T2;
5293 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
5295 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
5296 -- If neither T1 nor T2 are generic actual types, or if they are in
5297 -- different scopes (e.g. parent and child instances), then verify that
5298 -- the base types are equal. Otherwise T1 and T2 must be on the same
5299 -- subtype chain. The whole purpose of this procedure is to prevent
5300 -- spurious ambiguities in an instantiation that may arise if two
5301 -- distinct generic types are instantiated with the same actual.
5303 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
5304 -- An access parameter can designate an incomplete type. If the
5305 -- incomplete type is the limited view of a type from a limited_
5306 -- with_clause, check whether the non-limited view is available. If
5307 -- it is a (non-limited) incomplete type, get the full view.
5309 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
5310 -- Returns True if and only if either T1 denotes a limited view of T2
5311 -- or T2 denotes a limited view of T1. This can arise when the limited
5312 -- with view of a type is used in a subprogram declaration and the
5313 -- subprogram body is in the scope of a regular with clause for the
5314 -- same unit. In such a case, the two type entities can be considered
5315 -- identical for purposes of conformance checking.
5317 ----------------------
5318 -- Base_Types_Match --
5319 ----------------------
5321 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
5326 elsif Base_Type (T1) = Base_Type (T2) then
5328 -- The following is too permissive. A more precise test should
5329 -- check that the generic actual is an ancestor subtype of the
5332 return not Is_Generic_Actual_Type (T1)
5333 or else not Is_Generic_Actual_Type (T2)
5334 or else Scope (T1) /= Scope (T2);
5339 end Base_Types_Match;
5341 --------------------------
5342 -- Find_Designated_Type --
5343 --------------------------
5345 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
5349 Desig := Directly_Designated_Type (T);
5351 if Ekind (Desig) = E_Incomplete_Type then
5353 -- If regular incomplete type, get full view if available
5355 if Present (Full_View (Desig)) then
5356 Desig := Full_View (Desig);
5358 -- If limited view of a type, get non-limited view if available,
5359 -- and check again for a regular incomplete type.
5361 elsif Present (Non_Limited_View (Desig)) then
5362 Desig := Get_Full_View (Non_Limited_View (Desig));
5367 end Find_Designated_Type;
5369 -------------------------------
5370 -- Matches_Limited_With_View --
5371 -------------------------------
5373 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
5375 -- In some cases a type imported through a limited_with clause, and
5376 -- its nonlimited view are both visible, for example in an anonymous
5377 -- access-to-class-wide type in a formal. Both entities designate the
5380 if From_With_Type (T1)
5381 and then T2 = Available_View (T1)
5385 elsif From_With_Type (T2)
5386 and then T1 = Available_View (T2)
5393 end Matches_Limited_With_View;
5395 -- Start of processing for Conforming_Types
5398 -- The context is an instance association for a formal
5399 -- access-to-subprogram type; the formal parameter types require
5400 -- mapping because they may denote other formal parameters of the
5404 Type_1 := Get_Instance_Of (T1);
5405 Type_2 := Get_Instance_Of (T2);
5408 -- If one of the types is a view of the other introduced by a limited
5409 -- with clause, treat these as conforming for all purposes.
5411 if Matches_Limited_With_View (T1, T2) then
5414 elsif Base_Types_Match (Type_1, Type_2) then
5415 return Ctype <= Mode_Conformant
5416 or else Subtypes_Statically_Match (Type_1, Type_2);
5418 elsif Is_Incomplete_Or_Private_Type (Type_1)
5419 and then Present (Full_View (Type_1))
5420 and then Base_Types_Match (Full_View (Type_1), Type_2)
5422 return Ctype <= Mode_Conformant
5423 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
5425 elsif Ekind (Type_2) = E_Incomplete_Type
5426 and then Present (Full_View (Type_2))
5427 and then Base_Types_Match (Type_1, Full_View (Type_2))
5429 return Ctype <= Mode_Conformant
5430 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5432 elsif Is_Private_Type (Type_2)
5433 and then In_Instance
5434 and then Present (Full_View (Type_2))
5435 and then Base_Types_Match (Type_1, Full_View (Type_2))
5437 return Ctype <= Mode_Conformant
5438 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
5441 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
5442 -- treated recursively because they carry a signature.
5444 Are_Anonymous_Access_To_Subprogram_Types :=
5445 Ekind (Type_1) = Ekind (Type_2)
5447 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
5449 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
5451 -- Test anonymous access type case. For this case, static subtype
5452 -- matching is required for mode conformance (RM 6.3.1(15)). We check
5453 -- the base types because we may have built internal subtype entities
5454 -- to handle null-excluding types (see Process_Formals).
5456 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
5458 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
5459 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
5462 Desig_1 : Entity_Id;
5463 Desig_2 : Entity_Id;
5466 -- In Ada2005, access constant indicators must match for
5467 -- subtype conformance.
5469 if Ada_Version >= Ada_2005
5470 and then Ctype >= Subtype_Conformant
5472 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
5477 Desig_1 := Find_Designated_Type (Type_1);
5478 Desig_2 := Find_Designated_Type (Type_2);
5480 -- If the context is an instance association for a formal
5481 -- access-to-subprogram type; formal access parameter designated
5482 -- types require mapping because they may denote other formal
5483 -- parameters of the generic unit.
5486 Desig_1 := Get_Instance_Of (Desig_1);
5487 Desig_2 := Get_Instance_Of (Desig_2);
5490 -- It is possible for a Class_Wide_Type to be introduced for an
5491 -- incomplete type, in which case there is a separate class_ wide
5492 -- type for the full view. The types conform if their Etypes
5493 -- conform, i.e. one may be the full view of the other. This can
5494 -- only happen in the context of an access parameter, other uses
5495 -- of an incomplete Class_Wide_Type are illegal.
5497 if Is_Class_Wide_Type (Desig_1)
5499 Is_Class_Wide_Type (Desig_2)
5503 (Etype (Base_Type (Desig_1)),
5504 Etype (Base_Type (Desig_2)), Ctype);
5506 elsif Are_Anonymous_Access_To_Subprogram_Types then
5507 if Ada_Version < Ada_2005 then
5508 return Ctype = Type_Conformant
5510 Subtypes_Statically_Match (Desig_1, Desig_2);
5512 -- We must check the conformance of the signatures themselves
5516 Conformant : Boolean;
5519 (Desig_1, Desig_2, Ctype, False, Conformant);
5525 return Base_Type (Desig_1) = Base_Type (Desig_2)
5526 and then (Ctype = Type_Conformant
5528 Subtypes_Statically_Match (Desig_1, Desig_2));
5532 -- Otherwise definitely no match
5535 if ((Ekind (Type_1) = E_Anonymous_Access_Type
5536 and then Is_Access_Type (Type_2))
5537 or else (Ekind (Type_2) = E_Anonymous_Access_Type
5538 and then Is_Access_Type (Type_1)))
5541 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
5543 May_Hide_Profile := True;
5548 end Conforming_Types;
5550 --------------------------
5551 -- Create_Extra_Formals --
5552 --------------------------
5554 procedure Create_Extra_Formals (E : Entity_Id) is
5556 First_Extra : Entity_Id := Empty;
5557 Last_Extra : Entity_Id;
5558 Formal_Type : Entity_Id;
5559 P_Formal : Entity_Id := Empty;
5561 function Add_Extra_Formal
5562 (Assoc_Entity : Entity_Id;
5565 Suffix : String) return Entity_Id;
5566 -- Add an extra formal to the current list of formals and extra formals.
5567 -- The extra formal is added to the end of the list of extra formals,
5568 -- and also returned as the result. These formals are always of mode IN.
5569 -- The new formal has the type Typ, is declared in Scope, and its name
5570 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
5571 -- The following suffixes are currently used. They should not be changed
5572 -- without coordinating with CodePeer, which makes use of these to
5573 -- provide better messages.
5575 -- O denotes the Constrained bit.
5576 -- L denotes the accessibility level.
5577 -- BIP_xxx denotes an extra formal for a build-in-place function. See
5578 -- the full list in exp_ch6.BIP_Formal_Kind.
5580 ----------------------
5581 -- Add_Extra_Formal --
5582 ----------------------
5584 function Add_Extra_Formal
5585 (Assoc_Entity : Entity_Id;
5588 Suffix : String) return Entity_Id
5590 EF : constant Entity_Id :=
5591 Make_Defining_Identifier (Sloc (Assoc_Entity),
5592 Chars => New_External_Name (Chars (Assoc_Entity),
5596 -- A little optimization. Never generate an extra formal for the
5597 -- _init operand of an initialization procedure, since it could
5600 if Chars (Formal) = Name_uInit then
5604 Set_Ekind (EF, E_In_Parameter);
5605 Set_Actual_Subtype (EF, Typ);
5606 Set_Etype (EF, Typ);
5607 Set_Scope (EF, Scope);
5608 Set_Mechanism (EF, Default_Mechanism);
5609 Set_Formal_Validity (EF);
5611 if No (First_Extra) then
5613 Set_Extra_Formals (Scope, First_Extra);
5616 if Present (Last_Extra) then
5617 Set_Extra_Formal (Last_Extra, EF);
5623 end Add_Extra_Formal;
5625 -- Start of processing for Create_Extra_Formals
5628 -- We never generate extra formals if expansion is not active
5629 -- because we don't need them unless we are generating code.
5631 if not Expander_Active then
5635 -- If this is a derived subprogram then the subtypes of the parent
5636 -- subprogram's formal parameters will be used to determine the need
5637 -- for extra formals.
5639 if Is_Overloadable (E) and then Present (Alias (E)) then
5640 P_Formal := First_Formal (Alias (E));
5643 Last_Extra := Empty;
5644 Formal := First_Formal (E);
5645 while Present (Formal) loop
5646 Last_Extra := Formal;
5647 Next_Formal (Formal);
5650 -- If Extra_formals were already created, don't do it again. This
5651 -- situation may arise for subprogram types created as part of
5652 -- dispatching calls (see Expand_Dispatching_Call)
5654 if Present (Last_Extra) and then
5655 Present (Extra_Formal (Last_Extra))
5660 -- If the subprogram is a predefined dispatching subprogram then don't
5661 -- generate any extra constrained or accessibility level formals. In
5662 -- general we suppress these for internal subprograms (by not calling
5663 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
5664 -- generated stream attributes do get passed through because extra
5665 -- build-in-place formals are needed in some cases (limited 'Input).
5667 if Is_Predefined_Internal_Operation (E) then
5668 goto Test_For_BIP_Extras;
5671 Formal := First_Formal (E);
5672 while Present (Formal) loop
5674 -- Create extra formal for supporting the attribute 'Constrained.
5675 -- The case of a private type view without discriminants also
5676 -- requires the extra formal if the underlying type has defaulted
5679 if Ekind (Formal) /= E_In_Parameter then
5680 if Present (P_Formal) then
5681 Formal_Type := Etype (P_Formal);
5683 Formal_Type := Etype (Formal);
5686 -- Do not produce extra formals for Unchecked_Union parameters.
5687 -- Jump directly to the end of the loop.
5689 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
5690 goto Skip_Extra_Formal_Generation;
5693 if not Has_Discriminants (Formal_Type)
5694 and then Ekind (Formal_Type) in Private_Kind
5695 and then Present (Underlying_Type (Formal_Type))
5697 Formal_Type := Underlying_Type (Formal_Type);
5700 if Has_Discriminants (Formal_Type)
5701 and then not Is_Constrained (Formal_Type)
5702 and then not Is_Indefinite_Subtype (Formal_Type)
5704 Set_Extra_Constrained
5705 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
5709 -- Create extra formal for supporting accessibility checking. This
5710 -- is done for both anonymous access formals and formals of named
5711 -- access types that are marked as controlling formals. The latter
5712 -- case can occur when Expand_Dispatching_Call creates a subprogram
5713 -- type and substitutes the types of access-to-class-wide actuals
5714 -- for the anonymous access-to-specific-type of controlling formals.
5715 -- Base_Type is applied because in cases where there is a null
5716 -- exclusion the formal may have an access subtype.
5718 -- This is suppressed if we specifically suppress accessibility
5719 -- checks at the package level for either the subprogram, or the
5720 -- package in which it resides. However, we do not suppress it
5721 -- simply if the scope has accessibility checks suppressed, since
5722 -- this could cause trouble when clients are compiled with a
5723 -- different suppression setting. The explicit checks at the
5724 -- package level are safe from this point of view.
5726 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
5727 or else (Is_Controlling_Formal (Formal)
5728 and then Is_Access_Type (Base_Type (Etype (Formal)))))
5730 (Explicit_Suppress (E, Accessibility_Check)
5732 Explicit_Suppress (Scope (E), Accessibility_Check))
5735 or else Present (Extra_Accessibility (P_Formal)))
5737 Set_Extra_Accessibility
5738 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
5741 -- This label is required when skipping extra formal generation for
5742 -- Unchecked_Union parameters.
5744 <<Skip_Extra_Formal_Generation>>
5746 if Present (P_Formal) then
5747 Next_Formal (P_Formal);
5750 Next_Formal (Formal);
5753 <<Test_For_BIP_Extras>>
5755 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
5756 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
5758 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
5760 Result_Subt : constant Entity_Id := Etype (E);
5762 Discard : Entity_Id;
5763 pragma Warnings (Off, Discard);
5766 -- In the case of functions with unconstrained result subtypes,
5767 -- add a 4-state formal indicating whether the return object is
5768 -- allocated by the caller (1), or should be allocated by the
5769 -- callee on the secondary stack (2), in the global heap (3), or
5770 -- in a user-defined storage pool (4). For the moment we just use
5771 -- Natural for the type of this formal. Note that this formal
5772 -- isn't usually needed in the case where the result subtype is
5773 -- constrained, but it is needed when the function has a tagged
5774 -- result, because generally such functions can be called in a
5775 -- dispatching context and such calls must be handled like calls
5776 -- to a class-wide function.
5778 if not Is_Constrained (Underlying_Type (Result_Subt))
5779 or else Is_Tagged_Type (Underlying_Type (Result_Subt))
5783 (E, Standard_Natural,
5784 E, BIP_Formal_Suffix (BIP_Alloc_Form));
5787 -- For functions whose result type has controlled parts, we have
5788 -- an extra formal of type System.Finalization_Implementation.
5789 -- Finalizable_Ptr_Ptr. That is, we are passing a pointer to a
5790 -- finalization list (which is itself a pointer). This extra
5791 -- formal is then passed along to Move_Final_List in case of
5792 -- successful completion of a return statement. We cannot pass an
5793 -- 'in out' parameter, because we need to update the finalization
5794 -- list during an abort-deferred region, rather than using
5795 -- copy-back after the function returns. This is true even if we
5796 -- are able to get away with having 'in out' parameters, which are
5797 -- normally illegal for functions. This formal is also needed when
5798 -- the function has a tagged result.
5800 if Needs_BIP_Final_List (E) then
5803 (E, RTE (RE_Finalizable_Ptr_Ptr),
5804 E, BIP_Formal_Suffix (BIP_Final_List));
5807 -- If the result type contains tasks, we have two extra formals:
5808 -- the master of the tasks to be created, and the caller's
5809 -- activation chain.
5811 if Has_Task (Result_Subt) then
5814 (E, RTE (RE_Master_Id),
5815 E, BIP_Formal_Suffix (BIP_Master));
5818 (E, RTE (RE_Activation_Chain_Access),
5819 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5822 -- All build-in-place functions get an extra formal that will be
5823 -- passed the address of the return object within the caller.
5826 Formal_Type : constant Entity_Id :=
5828 (E_Anonymous_Access_Type, E,
5829 Scope_Id => Scope (E));
5831 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5832 Set_Etype (Formal_Type, Formal_Type);
5833 Set_Depends_On_Private
5834 (Formal_Type, Has_Private_Component (Formal_Type));
5835 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5836 Set_Is_Access_Constant (Formal_Type, False);
5838 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5839 -- the designated type comes from the limited view (for
5840 -- back-end purposes).
5842 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5844 Layout_Type (Formal_Type);
5848 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5852 end Create_Extra_Formals;
5854 -----------------------------
5855 -- Enter_Overloaded_Entity --
5856 -----------------------------
5858 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5859 E : Entity_Id := Current_Entity_In_Scope (S);
5860 C_E : Entity_Id := Current_Entity (S);
5864 Set_Has_Homonym (E);
5865 Set_Has_Homonym (S);
5868 Set_Is_Immediately_Visible (S);
5869 Set_Scope (S, Current_Scope);
5871 -- Chain new entity if front of homonym in current scope, so that
5872 -- homonyms are contiguous.
5877 while Homonym (C_E) /= E loop
5878 C_E := Homonym (C_E);
5881 Set_Homonym (C_E, S);
5885 Set_Current_Entity (S);
5890 Append_Entity (S, Current_Scope);
5891 Set_Public_Status (S);
5893 if Debug_Flag_E then
5894 Write_Str ("New overloaded entity chain: ");
5895 Write_Name (Chars (S));
5898 while Present (E) loop
5899 Write_Str (" "); Write_Int (Int (E));
5906 -- Generate warning for hiding
5909 and then Comes_From_Source (S)
5910 and then In_Extended_Main_Source_Unit (S)
5917 -- Warn unless genuine overloading. Do not emit warning on
5918 -- hiding predefined operators in Standard (these are either an
5919 -- (artifact of our implicit declarations, or simple noise) but
5920 -- keep warning on a operator defined on a local subtype, because
5921 -- of the real danger that different operators may be applied in
5922 -- various parts of the program.
5924 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5925 and then (Is_Immediately_Visible (E)
5927 Is_Potentially_Use_Visible (S))
5929 if Scope (E) /= Standard_Standard then
5930 Error_Msg_Sloc := Sloc (E);
5931 Error_Msg_N ("declaration of & hides one#?", S);
5933 elsif Nkind (S) = N_Defining_Operator_Symbol
5936 Base_Type (Etype (First_Formal (S)))) /= Scope (S)
5939 ("declaration of & hides predefined operator?", S);
5944 end Enter_Overloaded_Entity;
5946 -----------------------------
5947 -- Check_Untagged_Equality --
5948 -----------------------------
5950 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
5951 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
5952 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
5956 if Nkind (Decl) = N_Subprogram_Declaration
5957 and then Is_Record_Type (Typ)
5958 and then not Is_Tagged_Type (Typ)
5960 -- If the type is not declared in a package, or if we are in the
5961 -- body of the package or in some other scope, the new operation is
5962 -- not primitive, and therefore legal, though suspicious. If the
5963 -- type is a generic actual (sub)type, the operation is not primitive
5964 -- either because the base type is declared elsewhere.
5966 if Is_Frozen (Typ) then
5967 if Ekind (Scope (Typ)) /= E_Package
5968 or else Scope (Typ) /= Current_Scope
5972 elsif Is_Generic_Actual_Type (Typ) then
5975 elsif In_Package_Body (Scope (Typ)) then
5977 ("equality operator must be declared "
5978 & "before type& is frozen", Eq_Op, Typ);
5980 ("\move declaration to package spec", Eq_Op);
5984 ("equality operator must be declared "
5985 & "before type& is frozen", Eq_Op, Typ);
5987 Obj_Decl := Next (Parent (Typ));
5988 while Present (Obj_Decl)
5989 and then Obj_Decl /= Decl
5991 if Nkind (Obj_Decl) = N_Object_Declaration
5992 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
5994 Error_Msg_NE ("type& is frozen by declaration?",
5997 ("\an equality operator cannot be declared after this "
5998 & "point ('R'M 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
6006 elsif not In_Same_List (Parent (Typ), Decl)
6007 and then not Is_Limited_Type (Typ)
6010 -- This makes it illegal to have a primitive equality declared in
6011 -- the private part if the type is visible.
6013 Error_Msg_N ("equality operator appears too late", Eq_Op);
6016 end Check_Untagged_Equality;
6018 -----------------------------
6019 -- Find_Corresponding_Spec --
6020 -----------------------------
6022 function Find_Corresponding_Spec
6024 Post_Error : Boolean := True) return Entity_Id
6026 Spec : constant Node_Id := Specification (N);
6027 Designator : constant Entity_Id := Defining_Entity (Spec);
6032 E := Current_Entity (Designator);
6033 while Present (E) loop
6035 -- We are looking for a matching spec. It must have the same scope,
6036 -- and the same name, and either be type conformant, or be the case
6037 -- of a library procedure spec and its body (which belong to one
6038 -- another regardless of whether they are type conformant or not).
6040 if Scope (E) = Current_Scope then
6041 if Current_Scope = Standard_Standard
6042 or else (Ekind (E) = Ekind (Designator)
6043 and then Type_Conformant (E, Designator))
6045 -- Within an instantiation, we know that spec and body are
6046 -- subtype conformant, because they were subtype conformant
6047 -- in the generic. We choose the subtype-conformant entity
6048 -- here as well, to resolve spurious ambiguities in the
6049 -- instance that were not present in the generic (i.e. when
6050 -- two different types are given the same actual). If we are
6051 -- looking for a spec to match a body, full conformance is
6055 Set_Convention (Designator, Convention (E));
6057 if Nkind (N) = N_Subprogram_Body
6058 and then Present (Homonym (E))
6059 and then not Fully_Conformant (E, Designator)
6063 elsif not Subtype_Conformant (E, Designator) then
6068 if not Has_Completion (E) then
6069 if Nkind (N) /= N_Subprogram_Body_Stub then
6070 Set_Corresponding_Spec (N, E);
6073 Set_Has_Completion (E);
6076 elsif Nkind (Parent (N)) = N_Subunit then
6078 -- If this is the proper body of a subunit, the completion
6079 -- flag is set when analyzing the stub.
6083 -- If E is an internal function with a controlling result
6084 -- that was created for an operation inherited by a null
6085 -- extension, it may be overridden by a body without a previous
6086 -- spec (one more reason why these should be shunned). In that
6087 -- case remove the generated body if present, because the
6088 -- current one is the explicit overriding.
6090 elsif Ekind (E) = E_Function
6091 and then Ada_Version >= Ada_2005
6092 and then not Comes_From_Source (E)
6093 and then Has_Controlling_Result (E)
6094 and then Is_Null_Extension (Etype (E))
6095 and then Comes_From_Source (Spec)
6097 Set_Has_Completion (E, False);
6100 and then Nkind (Parent (E)) = N_Function_Specification
6103 (Unit_Declaration_Node
6104 (Corresponding_Body (Unit_Declaration_Node (E))));
6108 -- If expansion is disabled, or if the wrapper function has
6109 -- not been generated yet, this a late body overriding an
6110 -- inherited operation, or it is an overriding by some other
6111 -- declaration before the controlling result is frozen. In
6112 -- either case this is a declaration of a new entity.
6118 -- If the body already exists, then this is an error unless
6119 -- the previous declaration is the implicit declaration of a
6120 -- derived subprogram, or this is a spurious overloading in an
6123 elsif No (Alias (E))
6124 and then not Is_Intrinsic_Subprogram (E)
6125 and then not In_Instance
6128 Error_Msg_Sloc := Sloc (E);
6130 if Is_Imported (E) then
6132 ("body not allowed for imported subprogram & declared#",
6135 Error_Msg_NE ("duplicate body for & declared#", N, E);
6139 -- Child units cannot be overloaded, so a conformance mismatch
6140 -- between body and a previous spec is an error.
6142 elsif Is_Child_Unit (E)
6144 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
6146 Nkind (Parent (Unit_Declaration_Node (Designator))) =
6151 ("body of child unit does not match previous declaration", N);
6159 -- On exit, we know that no previous declaration of subprogram exists
6162 end Find_Corresponding_Spec;
6164 ----------------------
6165 -- Fully_Conformant --
6166 ----------------------
6168 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6171 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
6173 end Fully_Conformant;
6175 ----------------------------------
6176 -- Fully_Conformant_Expressions --
6177 ----------------------------------
6179 function Fully_Conformant_Expressions
6180 (Given_E1 : Node_Id;
6181 Given_E2 : Node_Id) return Boolean
6183 E1 : constant Node_Id := Original_Node (Given_E1);
6184 E2 : constant Node_Id := Original_Node (Given_E2);
6185 -- We always test conformance on original nodes, since it is possible
6186 -- for analysis and/or expansion to make things look as though they
6187 -- conform when they do not, e.g. by converting 1+2 into 3.
6189 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6190 renames Fully_Conformant_Expressions;
6192 function FCL (L1, L2 : List_Id) return Boolean;
6193 -- Compare elements of two lists for conformance. Elements have to
6194 -- be conformant, and actuals inserted as default parameters do not
6195 -- match explicit actuals with the same value.
6197 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6198 -- Compare an operator node with a function call
6204 function FCL (L1, L2 : List_Id) return Boolean is
6208 if L1 = No_List then
6214 if L2 = No_List then
6220 -- Compare two lists, skipping rewrite insertions (we want to
6221 -- compare the original trees, not the expanded versions!)
6224 if Is_Rewrite_Insertion (N1) then
6226 elsif Is_Rewrite_Insertion (N2) then
6232 elsif not FCE (N1, N2) then
6245 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6246 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6251 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6256 Act := First (Actuals);
6258 if Nkind (Op_Node) in N_Binary_Op then
6259 if not FCE (Left_Opnd (Op_Node), Act) then
6266 return Present (Act)
6267 and then FCE (Right_Opnd (Op_Node), Act)
6268 and then No (Next (Act));
6272 -- Start of processing for Fully_Conformant_Expressions
6275 -- Non-conformant if paren count does not match. Note: if some idiot
6276 -- complains that we don't do this right for more than 3 levels of
6277 -- parentheses, they will be treated with the respect they deserve!
6279 if Paren_Count (E1) /= Paren_Count (E2) then
6282 -- If same entities are referenced, then they are conformant even if
6283 -- they have different forms (RM 8.3.1(19-20)).
6285 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6286 if Present (Entity (E1)) then
6287 return Entity (E1) = Entity (E2)
6288 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6289 and then Ekind (Entity (E1)) = E_Discriminant
6290 and then Ekind (Entity (E2)) = E_In_Parameter);
6292 elsif Nkind (E1) = N_Expanded_Name
6293 and then Nkind (E2) = N_Expanded_Name
6294 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6295 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6297 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6300 -- Identifiers in component associations don't always have
6301 -- entities, but their names must conform.
6303 return Nkind (E1) = N_Identifier
6304 and then Nkind (E2) = N_Identifier
6305 and then Chars (E1) = Chars (E2);
6308 elsif Nkind (E1) = N_Character_Literal
6309 and then Nkind (E2) = N_Expanded_Name
6311 return Nkind (Selector_Name (E2)) = N_Character_Literal
6312 and then Chars (E1) = Chars (Selector_Name (E2));
6314 elsif Nkind (E2) = N_Character_Literal
6315 and then Nkind (E1) = N_Expanded_Name
6317 return Nkind (Selector_Name (E1)) = N_Character_Literal
6318 and then Chars (E2) = Chars (Selector_Name (E1));
6320 elsif Nkind (E1) in N_Op
6321 and then Nkind (E2) = N_Function_Call
6323 return FCO (E1, E2);
6325 elsif Nkind (E2) in N_Op
6326 and then Nkind (E1) = N_Function_Call
6328 return FCO (E2, E1);
6330 -- Otherwise we must have the same syntactic entity
6332 elsif Nkind (E1) /= Nkind (E2) then
6335 -- At this point, we specialize by node type
6342 FCL (Expressions (E1), Expressions (E2))
6344 FCL (Component_Associations (E1),
6345 Component_Associations (E2));
6348 if Nkind (Expression (E1)) = N_Qualified_Expression
6350 Nkind (Expression (E2)) = N_Qualified_Expression
6352 return FCE (Expression (E1), Expression (E2));
6354 -- Check that the subtype marks and any constraints
6359 Indic1 : constant Node_Id := Expression (E1);
6360 Indic2 : constant Node_Id := Expression (E2);
6365 if Nkind (Indic1) /= N_Subtype_Indication then
6367 Nkind (Indic2) /= N_Subtype_Indication
6368 and then Entity (Indic1) = Entity (Indic2);
6370 elsif Nkind (Indic2) /= N_Subtype_Indication then
6372 Nkind (Indic1) /= N_Subtype_Indication
6373 and then Entity (Indic1) = Entity (Indic2);
6376 if Entity (Subtype_Mark (Indic1)) /=
6377 Entity (Subtype_Mark (Indic2))
6382 Elt1 := First (Constraints (Constraint (Indic1)));
6383 Elt2 := First (Constraints (Constraint (Indic2)));
6384 while Present (Elt1) and then Present (Elt2) loop
6385 if not FCE (Elt1, Elt2) then
6398 when N_Attribute_Reference =>
6400 Attribute_Name (E1) = Attribute_Name (E2)
6401 and then FCL (Expressions (E1), Expressions (E2));
6405 Entity (E1) = Entity (E2)
6406 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6407 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6409 when N_Short_Circuit | N_Membership_Test =>
6411 FCE (Left_Opnd (E1), Left_Opnd (E2))
6413 FCE (Right_Opnd (E1), Right_Opnd (E2));
6415 when N_Case_Expression =>
6421 if not FCE (Expression (E1), Expression (E2)) then
6425 Alt1 := First (Alternatives (E1));
6426 Alt2 := First (Alternatives (E2));
6428 if Present (Alt1) /= Present (Alt2) then
6430 elsif No (Alt1) then
6434 if not FCE (Expression (Alt1), Expression (Alt2))
6435 or else not FCL (Discrete_Choices (Alt1),
6436 Discrete_Choices (Alt2))
6447 when N_Character_Literal =>
6449 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6451 when N_Component_Association =>
6453 FCL (Choices (E1), Choices (E2))
6455 FCE (Expression (E1), Expression (E2));
6457 when N_Conditional_Expression =>
6459 FCL (Expressions (E1), Expressions (E2));
6461 when N_Explicit_Dereference =>
6463 FCE (Prefix (E1), Prefix (E2));
6465 when N_Extension_Aggregate =>
6467 FCL (Expressions (E1), Expressions (E2))
6468 and then Null_Record_Present (E1) =
6469 Null_Record_Present (E2)
6470 and then FCL (Component_Associations (E1),
6471 Component_Associations (E2));
6473 when N_Function_Call =>
6475 FCE (Name (E1), Name (E2))
6477 FCL (Parameter_Associations (E1),
6478 Parameter_Associations (E2));
6480 when N_Indexed_Component =>
6482 FCE (Prefix (E1), Prefix (E2))
6484 FCL (Expressions (E1), Expressions (E2));
6486 when N_Integer_Literal =>
6487 return (Intval (E1) = Intval (E2));
6492 when N_Operator_Symbol =>
6494 Chars (E1) = Chars (E2);
6496 when N_Others_Choice =>
6499 when N_Parameter_Association =>
6501 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6502 and then FCE (Explicit_Actual_Parameter (E1),
6503 Explicit_Actual_Parameter (E2));
6505 when N_Qualified_Expression =>
6507 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6509 FCE (Expression (E1), Expression (E2));
6513 FCE (Low_Bound (E1), Low_Bound (E2))
6515 FCE (High_Bound (E1), High_Bound (E2));
6517 when N_Real_Literal =>
6518 return (Realval (E1) = Realval (E2));
6520 when N_Selected_Component =>
6522 FCE (Prefix (E1), Prefix (E2))
6524 FCE (Selector_Name (E1), Selector_Name (E2));
6528 FCE (Prefix (E1), Prefix (E2))
6530 FCE (Discrete_Range (E1), Discrete_Range (E2));
6532 when N_String_Literal =>
6534 S1 : constant String_Id := Strval (E1);
6535 S2 : constant String_Id := Strval (E2);
6536 L1 : constant Nat := String_Length (S1);
6537 L2 : constant Nat := String_Length (S2);
6544 for J in 1 .. L1 loop
6545 if Get_String_Char (S1, J) /=
6546 Get_String_Char (S2, J)
6556 when N_Type_Conversion =>
6558 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6560 FCE (Expression (E1), Expression (E2));
6564 Entity (E1) = Entity (E2)
6566 FCE (Right_Opnd (E1), Right_Opnd (E2));
6568 when N_Unchecked_Type_Conversion =>
6570 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6572 FCE (Expression (E1), Expression (E2));
6574 -- All other node types cannot appear in this context. Strictly
6575 -- we should raise a fatal internal error. Instead we just ignore
6576 -- the nodes. This means that if anyone makes a mistake in the
6577 -- expander and mucks an expression tree irretrievably, the
6578 -- result will be a failure to detect a (probably very obscure)
6579 -- case of non-conformance, which is better than bombing on some
6580 -- case where two expressions do in fact conform.
6587 end Fully_Conformant_Expressions;
6589 ----------------------------------------
6590 -- Fully_Conformant_Discrete_Subtypes --
6591 ----------------------------------------
6593 function Fully_Conformant_Discrete_Subtypes
6594 (Given_S1 : Node_Id;
6595 Given_S2 : Node_Id) return Boolean
6597 S1 : constant Node_Id := Original_Node (Given_S1);
6598 S2 : constant Node_Id := Original_Node (Given_S2);
6600 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6601 -- Special-case for a bound given by a discriminant, which in the body
6602 -- is replaced with the discriminal of the enclosing type.
6604 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6605 -- Check both bounds
6607 -----------------------
6608 -- Conforming_Bounds --
6609 -----------------------
6611 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6613 if Is_Entity_Name (B1)
6614 and then Is_Entity_Name (B2)
6615 and then Ekind (Entity (B1)) = E_Discriminant
6617 return Chars (B1) = Chars (B2);
6620 return Fully_Conformant_Expressions (B1, B2);
6622 end Conforming_Bounds;
6624 -----------------------
6625 -- Conforming_Ranges --
6626 -----------------------
6628 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6631 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6633 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6634 end Conforming_Ranges;
6636 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6639 if Nkind (S1) /= Nkind (S2) then
6642 elsif Is_Entity_Name (S1) then
6643 return Entity (S1) = Entity (S2);
6645 elsif Nkind (S1) = N_Range then
6646 return Conforming_Ranges (S1, S2);
6648 elsif Nkind (S1) = N_Subtype_Indication then
6650 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6653 (Range_Expression (Constraint (S1)),
6654 Range_Expression (Constraint (S2)));
6658 end Fully_Conformant_Discrete_Subtypes;
6660 --------------------
6661 -- Install_Entity --
6662 --------------------
6664 procedure Install_Entity (E : Entity_Id) is
6665 Prev : constant Entity_Id := Current_Entity (E);
6667 Set_Is_Immediately_Visible (E);
6668 Set_Current_Entity (E);
6669 Set_Homonym (E, Prev);
6672 ---------------------
6673 -- Install_Formals --
6674 ---------------------
6676 procedure Install_Formals (Id : Entity_Id) is
6679 F := First_Formal (Id);
6680 while Present (F) loop
6684 end Install_Formals;
6686 -----------------------------
6687 -- Is_Interface_Conformant --
6688 -----------------------------
6690 function Is_Interface_Conformant
6691 (Tagged_Type : Entity_Id;
6692 Iface_Prim : Entity_Id;
6693 Prim : Entity_Id) return Boolean
6695 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6696 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6699 pragma Assert (Is_Subprogram (Iface_Prim)
6700 and then Is_Subprogram (Prim)
6701 and then Is_Dispatching_Operation (Iface_Prim)
6702 and then Is_Dispatching_Operation (Prim));
6704 pragma Assert (Is_Interface (Iface)
6705 or else (Present (Alias (Iface_Prim))
6708 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6710 if Prim = Iface_Prim
6711 or else not Is_Subprogram (Prim)
6712 or else Ekind (Prim) /= Ekind (Iface_Prim)
6713 or else not Is_Dispatching_Operation (Prim)
6714 or else Scope (Prim) /= Scope (Tagged_Type)
6716 or else Base_Type (Typ) /= Tagged_Type
6717 or else not Primitive_Names_Match (Iface_Prim, Prim)
6721 -- Case of a procedure, or a function that does not have a controlling
6722 -- result (I or access I).
6724 elsif Ekind (Iface_Prim) = E_Procedure
6725 or else Etype (Prim) = Etype (Iface_Prim)
6726 or else not Has_Controlling_Result (Prim)
6728 return Type_Conformant
6729 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
6731 -- Case of a function returning an interface, or an access to one.
6732 -- Check that the return types correspond.
6734 elsif Implements_Interface (Typ, Iface) then
6735 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6737 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6742 Type_Conformant (Prim, Iface_Prim,
6743 Skip_Controlling_Formals => True);
6749 end Is_Interface_Conformant;
6751 ---------------------------------
6752 -- Is_Non_Overriding_Operation --
6753 ---------------------------------
6755 function Is_Non_Overriding_Operation
6756 (Prev_E : Entity_Id;
6757 New_E : Entity_Id) return Boolean
6761 G_Typ : Entity_Id := Empty;
6763 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6764 -- If F_Type is a derived type associated with a generic actual subtype,
6765 -- then return its Generic_Parent_Type attribute, else return Empty.
6767 function Types_Correspond
6768 (P_Type : Entity_Id;
6769 N_Type : Entity_Id) return Boolean;
6770 -- Returns true if and only if the types (or designated types in the
6771 -- case of anonymous access types) are the same or N_Type is derived
6772 -- directly or indirectly from P_Type.
6774 -----------------------------
6775 -- Get_Generic_Parent_Type --
6776 -----------------------------
6778 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6783 if Is_Derived_Type (F_Typ)
6784 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6786 -- The tree must be traversed to determine the parent subtype in
6787 -- the generic unit, which unfortunately isn't always available
6788 -- via semantic attributes. ??? (Note: The use of Original_Node
6789 -- is needed for cases where a full derived type has been
6792 Indic := Subtype_Indication
6793 (Type_Definition (Original_Node (Parent (F_Typ))));
6795 if Nkind (Indic) = N_Subtype_Indication then
6796 G_Typ := Entity (Subtype_Mark (Indic));
6798 G_Typ := Entity (Indic);
6801 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6802 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6804 return Generic_Parent_Type (Parent (G_Typ));
6809 end Get_Generic_Parent_Type;
6811 ----------------------
6812 -- Types_Correspond --
6813 ----------------------
6815 function Types_Correspond
6816 (P_Type : Entity_Id;
6817 N_Type : Entity_Id) return Boolean
6819 Prev_Type : Entity_Id := Base_Type (P_Type);
6820 New_Type : Entity_Id := Base_Type (N_Type);
6823 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6824 Prev_Type := Designated_Type (Prev_Type);
6827 if Ekind (New_Type) = E_Anonymous_Access_Type then
6828 New_Type := Designated_Type (New_Type);
6831 if Prev_Type = New_Type then
6834 elsif not Is_Class_Wide_Type (New_Type) then
6835 while Etype (New_Type) /= New_Type loop
6836 New_Type := Etype (New_Type);
6837 if New_Type = Prev_Type then
6843 end Types_Correspond;
6845 -- Start of processing for Is_Non_Overriding_Operation
6848 -- In the case where both operations are implicit derived subprograms
6849 -- then neither overrides the other. This can only occur in certain
6850 -- obscure cases (e.g., derivation from homographs created in a generic
6853 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6856 elsif Ekind (Current_Scope) = E_Package
6857 and then Is_Generic_Instance (Current_Scope)
6858 and then In_Private_Part (Current_Scope)
6859 and then Comes_From_Source (New_E)
6861 -- We examine the formals and result subtype of the inherited
6862 -- operation, to determine whether their type is derived from (the
6863 -- instance of) a generic type.
6865 Formal := First_Formal (Prev_E);
6866 while Present (Formal) loop
6867 F_Typ := Base_Type (Etype (Formal));
6869 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6870 F_Typ := Designated_Type (F_Typ);
6873 G_Typ := Get_Generic_Parent_Type (F_Typ);
6875 Next_Formal (Formal);
6878 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6879 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6886 -- If the generic type is a private type, then the original operation
6887 -- was not overriding in the generic, because there was no primitive
6888 -- operation to override.
6890 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6891 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6892 N_Formal_Private_Type_Definition
6896 -- The generic parent type is the ancestor of a formal derived
6897 -- type declaration. We need to check whether it has a primitive
6898 -- operation that should be overridden by New_E in the generic.
6902 P_Formal : Entity_Id;
6903 N_Formal : Entity_Id;
6907 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6910 while Present (Prim_Elt) loop
6911 P_Prim := Node (Prim_Elt);
6913 if Chars (P_Prim) = Chars (New_E)
6914 and then Ekind (P_Prim) = Ekind (New_E)
6916 P_Formal := First_Formal (P_Prim);
6917 N_Formal := First_Formal (New_E);
6918 while Present (P_Formal) and then Present (N_Formal) loop
6919 P_Typ := Etype (P_Formal);
6920 N_Typ := Etype (N_Formal);
6922 if not Types_Correspond (P_Typ, N_Typ) then
6926 Next_Entity (P_Formal);
6927 Next_Entity (N_Formal);
6930 -- Found a matching primitive operation belonging to the
6931 -- formal ancestor type, so the new subprogram is
6935 and then No (N_Formal)
6936 and then (Ekind (New_E) /= E_Function
6939 (Etype (P_Prim), Etype (New_E)))
6945 Next_Elmt (Prim_Elt);
6948 -- If no match found, then the new subprogram does not
6949 -- override in the generic (nor in the instance).
6957 end Is_Non_Overriding_Operation;
6959 -------------------------------------
6960 -- List_Inherited_Pre_Post_Aspects --
6961 -------------------------------------
6963 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
6965 if Opt.List_Inherited_Aspects
6966 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
6969 Inherited : constant Subprogram_List :=
6970 Inherited_Subprograms (E);
6974 for J in Inherited'Range loop
6975 P := Spec_PPC_List (Inherited (J));
6976 while Present (P) loop
6977 Error_Msg_Sloc := Sloc (P);
6979 if Class_Present (P) and then not Split_PPC (P) then
6980 if Pragma_Name (P) = Name_Precondition then
6982 ("?info: & inherits `Pre''Class` aspect from #", E);
6985 ("?info: & inherits `Post''Class` aspect from #", E);
6989 P := Next_Pragma (P);
6994 end List_Inherited_Pre_Post_Aspects;
6996 ------------------------------
6997 -- Make_Inequality_Operator --
6998 ------------------------------
7000 -- S is the defining identifier of an equality operator. We build a
7001 -- subprogram declaration with the right signature. This operation is
7002 -- intrinsic, because it is always expanded as the negation of the
7003 -- call to the equality function.
7005 procedure Make_Inequality_Operator (S : Entity_Id) is
7006 Loc : constant Source_Ptr := Sloc (S);
7009 Op_Name : Entity_Id;
7011 FF : constant Entity_Id := First_Formal (S);
7012 NF : constant Entity_Id := Next_Formal (FF);
7015 -- Check that equality was properly defined, ignore call if not
7022 A : constant Entity_Id :=
7023 Make_Defining_Identifier (Sloc (FF),
7024 Chars => Chars (FF));
7026 B : constant Entity_Id :=
7027 Make_Defining_Identifier (Sloc (NF),
7028 Chars => Chars (NF));
7031 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
7033 Formals := New_List (
7034 Make_Parameter_Specification (Loc,
7035 Defining_Identifier => A,
7037 New_Reference_To (Etype (First_Formal (S)),
7038 Sloc (Etype (First_Formal (S))))),
7040 Make_Parameter_Specification (Loc,
7041 Defining_Identifier => B,
7043 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
7044 Sloc (Etype (Next_Formal (First_Formal (S)))))));
7047 Make_Subprogram_Declaration (Loc,
7049 Make_Function_Specification (Loc,
7050 Defining_Unit_Name => Op_Name,
7051 Parameter_Specifications => Formals,
7052 Result_Definition =>
7053 New_Reference_To (Standard_Boolean, Loc)));
7055 -- Insert inequality right after equality if it is explicit or after
7056 -- the derived type when implicit. These entities are created only
7057 -- for visibility purposes, and eventually replaced in the course of
7058 -- expansion, so they do not need to be attached to the tree and seen
7059 -- by the back-end. Keeping them internal also avoids spurious
7060 -- freezing problems. The declaration is inserted in the tree for
7061 -- analysis, and removed afterwards. If the equality operator comes
7062 -- from an explicit declaration, attach the inequality immediately
7063 -- after. Else the equality is inherited from a derived type
7064 -- declaration, so insert inequality after that declaration.
7066 if No (Alias (S)) then
7067 Insert_After (Unit_Declaration_Node (S), Decl);
7068 elsif Is_List_Member (Parent (S)) then
7069 Insert_After (Parent (S), Decl);
7071 Insert_After (Parent (Etype (First_Formal (S))), Decl);
7074 Mark_Rewrite_Insertion (Decl);
7075 Set_Is_Intrinsic_Subprogram (Op_Name);
7078 Set_Has_Completion (Op_Name);
7079 Set_Corresponding_Equality (Op_Name, S);
7080 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
7082 end Make_Inequality_Operator;
7084 ----------------------
7085 -- May_Need_Actuals --
7086 ----------------------
7088 procedure May_Need_Actuals (Fun : Entity_Id) is
7093 F := First_Formal (Fun);
7095 while Present (F) loop
7096 if No (Default_Value (F)) then
7104 Set_Needs_No_Actuals (Fun, B);
7105 end May_Need_Actuals;
7107 ---------------------
7108 -- Mode_Conformant --
7109 ---------------------
7111 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7114 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
7116 end Mode_Conformant;
7118 ---------------------------
7119 -- New_Overloaded_Entity --
7120 ---------------------------
7122 procedure New_Overloaded_Entity
7124 Derived_Type : Entity_Id := Empty)
7126 Overridden_Subp : Entity_Id := Empty;
7127 -- Set if the current scope has an operation that is type-conformant
7128 -- with S, and becomes hidden by S.
7130 Is_Primitive_Subp : Boolean;
7131 -- Set to True if the new subprogram is primitive
7134 -- Entity that S overrides
7136 Prev_Vis : Entity_Id := Empty;
7137 -- Predecessor of E in Homonym chain
7139 procedure Check_For_Primitive_Subprogram
7140 (Is_Primitive : out Boolean;
7141 Is_Overriding : Boolean := False);
7142 -- If the subprogram being analyzed is a primitive operation of the type
7143 -- of a formal or result, set the Has_Primitive_Operations flag on the
7144 -- type, and set Is_Primitive to True (otherwise set to False). Set the
7145 -- corresponding flag on the entity itself for later use.
7147 procedure Check_Synchronized_Overriding
7148 (Def_Id : Entity_Id;
7149 Overridden_Subp : out Entity_Id);
7150 -- First determine if Def_Id is an entry or a subprogram either defined
7151 -- in the scope of a task or protected type, or is a primitive of such
7152 -- a type. Check whether Def_Id overrides a subprogram of an interface
7153 -- implemented by the synchronized type, return the overridden entity
7156 function Is_Private_Declaration (E : Entity_Id) return Boolean;
7157 -- Check that E is declared in the private part of the current package,
7158 -- or in the package body, where it may hide a previous declaration.
7159 -- We can't use In_Private_Part by itself because this flag is also
7160 -- set when freezing entities, so we must examine the place of the
7161 -- declaration in the tree, and recognize wrapper packages as well.
7163 function Is_Overriding_Alias
7165 New_E : Entity_Id) return Boolean;
7166 -- Check whether new subprogram and old subprogram are both inherited
7167 -- from subprograms that have distinct dispatch table entries. This can
7168 -- occur with derivations from instances with accidental homonyms.
7169 -- The function is conservative given that the converse is only true
7170 -- within instances that contain accidental overloadings.
7172 ------------------------------------
7173 -- Check_For_Primitive_Subprogram --
7174 ------------------------------------
7176 procedure Check_For_Primitive_Subprogram
7177 (Is_Primitive : out Boolean;
7178 Is_Overriding : Boolean := False)
7184 function Visible_Part_Type (T : Entity_Id) return Boolean;
7185 -- Returns true if T is declared in the visible part of the current
7186 -- package scope; otherwise returns false. Assumes that T is declared
7189 procedure Check_Private_Overriding (T : Entity_Id);
7190 -- Checks that if a primitive abstract subprogram of a visible
7191 -- abstract type is declared in a private part, then it must override
7192 -- an abstract subprogram declared in the visible part. Also checks
7193 -- that if a primitive function with a controlling result is declared
7194 -- in a private part, then it must override a function declared in
7195 -- the visible part.
7197 ------------------------------
7198 -- Check_Private_Overriding --
7199 ------------------------------
7201 procedure Check_Private_Overriding (T : Entity_Id) is
7203 if Is_Package_Or_Generic_Package (Current_Scope)
7204 and then In_Private_Part (Current_Scope)
7205 and then Visible_Part_Type (T)
7206 and then not In_Instance
7208 if Is_Abstract_Type (T)
7209 and then Is_Abstract_Subprogram (S)
7210 and then (not Is_Overriding
7211 or else not Is_Abstract_Subprogram (E))
7214 ("abstract subprograms must be visible "
7215 & "(RM 3.9.3(10))!", S);
7217 elsif Ekind (S) = E_Function
7218 and then not Is_Overriding
7220 if Is_Tagged_Type (T)
7221 and then T = Base_Type (Etype (S))
7224 ("private function with tagged result must"
7225 & " override visible-part function", S);
7227 ("\move subprogram to the visible part"
7228 & " (RM 3.9.3(10))", S);
7230 -- AI05-0073: extend this test to the case of a function
7231 -- with a controlling access result.
7233 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
7234 and then Is_Tagged_Type (Designated_Type (Etype (S)))
7236 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
7237 and then Ada_Version >= Ada_2012
7240 ("private function with controlling access result "
7241 & "must override visible-part function", S);
7243 ("\move subprogram to the visible part"
7244 & " (RM 3.9.3(10))", S);
7248 end Check_Private_Overriding;
7250 -----------------------
7251 -- Visible_Part_Type --
7252 -----------------------
7254 function Visible_Part_Type (T : Entity_Id) return Boolean is
7255 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7259 -- If the entity is a private type, then it must be declared in a
7262 if Ekind (T) in Private_Kind then
7266 -- Otherwise, we traverse the visible part looking for its
7267 -- corresponding declaration. We cannot use the declaration
7268 -- node directly because in the private part the entity of a
7269 -- private type is the one in the full view, which does not
7270 -- indicate that it is the completion of something visible.
7272 N := First (Visible_Declarations (Specification (P)));
7273 while Present (N) loop
7274 if Nkind (N) = N_Full_Type_Declaration
7275 and then Present (Defining_Identifier (N))
7276 and then T = Defining_Identifier (N)
7280 elsif Nkind_In (N, N_Private_Type_Declaration,
7281 N_Private_Extension_Declaration)
7282 and then Present (Defining_Identifier (N))
7283 and then T = Full_View (Defining_Identifier (N))
7292 end Visible_Part_Type;
7294 -- Start of processing for Check_For_Primitive_Subprogram
7297 Is_Primitive := False;
7299 if not Comes_From_Source (S) then
7302 -- If subprogram is at library level, it is not primitive operation
7304 elsif Current_Scope = Standard_Standard then
7307 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7308 and then not In_Package_Body (Current_Scope))
7309 or else Is_Overriding
7311 -- For function, check return type
7313 if Ekind (S) = E_Function then
7314 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7315 F_Typ := Designated_Type (Etype (S));
7320 B_Typ := Base_Type (F_Typ);
7322 if Scope (B_Typ) = Current_Scope
7323 and then not Is_Class_Wide_Type (B_Typ)
7324 and then not Is_Generic_Type (B_Typ)
7326 Is_Primitive := True;
7327 Set_Has_Primitive_Operations (B_Typ);
7328 Set_Is_Primitive (S);
7329 Check_Private_Overriding (B_Typ);
7333 -- For all subprograms, check formals
7335 Formal := First_Formal (S);
7336 while Present (Formal) loop
7337 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7338 F_Typ := Designated_Type (Etype (Formal));
7340 F_Typ := Etype (Formal);
7343 B_Typ := Base_Type (F_Typ);
7345 if Ekind (B_Typ) = E_Access_Subtype then
7346 B_Typ := Base_Type (B_Typ);
7349 if Scope (B_Typ) = Current_Scope
7350 and then not Is_Class_Wide_Type (B_Typ)
7351 and then not Is_Generic_Type (B_Typ)
7353 Is_Primitive := True;
7354 Set_Is_Primitive (S);
7355 Set_Has_Primitive_Operations (B_Typ);
7356 Check_Private_Overriding (B_Typ);
7359 Next_Formal (Formal);
7362 end Check_For_Primitive_Subprogram;
7364 -----------------------------------
7365 -- Check_Synchronized_Overriding --
7366 -----------------------------------
7368 procedure Check_Synchronized_Overriding
7369 (Def_Id : Entity_Id;
7370 Overridden_Subp : out Entity_Id)
7372 Ifaces_List : Elist_Id;
7376 function Matches_Prefixed_View_Profile
7377 (Prim_Params : List_Id;
7378 Iface_Params : List_Id) return Boolean;
7379 -- Determine whether a subprogram's parameter profile Prim_Params
7380 -- matches that of a potentially overridden interface subprogram
7381 -- Iface_Params. Also determine if the type of first parameter of
7382 -- Iface_Params is an implemented interface.
7384 -----------------------------------
7385 -- Matches_Prefixed_View_Profile --
7386 -----------------------------------
7388 function Matches_Prefixed_View_Profile
7389 (Prim_Params : List_Id;
7390 Iface_Params : List_Id) return Boolean
7392 Iface_Id : Entity_Id;
7393 Iface_Param : Node_Id;
7394 Iface_Typ : Entity_Id;
7395 Prim_Id : Entity_Id;
7396 Prim_Param : Node_Id;
7397 Prim_Typ : Entity_Id;
7399 function Is_Implemented
7400 (Ifaces_List : Elist_Id;
7401 Iface : Entity_Id) return Boolean;
7402 -- Determine if Iface is implemented by the current task or
7405 --------------------
7406 -- Is_Implemented --
7407 --------------------
7409 function Is_Implemented
7410 (Ifaces_List : Elist_Id;
7411 Iface : Entity_Id) return Boolean
7413 Iface_Elmt : Elmt_Id;
7416 Iface_Elmt := First_Elmt (Ifaces_List);
7417 while Present (Iface_Elmt) loop
7418 if Node (Iface_Elmt) = Iface then
7422 Next_Elmt (Iface_Elmt);
7428 -- Start of processing for Matches_Prefixed_View_Profile
7431 Iface_Param := First (Iface_Params);
7432 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7434 if Is_Access_Type (Iface_Typ) then
7435 Iface_Typ := Designated_Type (Iface_Typ);
7438 Prim_Param := First (Prim_Params);
7440 -- The first parameter of the potentially overridden subprogram
7441 -- must be an interface implemented by Prim.
7443 if not Is_Interface (Iface_Typ)
7444 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7449 -- The checks on the object parameters are done, move onto the
7450 -- rest of the parameters.
7452 if not In_Scope then
7453 Prim_Param := Next (Prim_Param);
7456 Iface_Param := Next (Iface_Param);
7457 while Present (Iface_Param) and then Present (Prim_Param) loop
7458 Iface_Id := Defining_Identifier (Iface_Param);
7459 Iface_Typ := Find_Parameter_Type (Iface_Param);
7461 Prim_Id := Defining_Identifier (Prim_Param);
7462 Prim_Typ := Find_Parameter_Type (Prim_Param);
7464 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7465 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7466 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7468 Iface_Typ := Designated_Type (Iface_Typ);
7469 Prim_Typ := Designated_Type (Prim_Typ);
7472 -- Case of multiple interface types inside a parameter profile
7474 -- (Obj_Param : in out Iface; ...; Param : Iface)
7476 -- If the interface type is implemented, then the matching type
7477 -- in the primitive should be the implementing record type.
7479 if Ekind (Iface_Typ) = E_Record_Type
7480 and then Is_Interface (Iface_Typ)
7481 and then Is_Implemented (Ifaces_List, Iface_Typ)
7483 if Prim_Typ /= Typ then
7487 -- The two parameters must be both mode and subtype conformant
7489 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7491 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7500 -- One of the two lists contains more parameters than the other
7502 if Present (Iface_Param) or else Present (Prim_Param) then
7507 end Matches_Prefixed_View_Profile;
7509 -- Start of processing for Check_Synchronized_Overriding
7512 Overridden_Subp := Empty;
7514 -- Def_Id must be an entry or a subprogram. We should skip predefined
7515 -- primitives internally generated by the frontend; however at this
7516 -- stage predefined primitives are still not fully decorated. As a
7517 -- minor optimization we skip here internally generated subprograms.
7519 if (Ekind (Def_Id) /= E_Entry
7520 and then Ekind (Def_Id) /= E_Function
7521 and then Ekind (Def_Id) /= E_Procedure)
7522 or else not Comes_From_Source (Def_Id)
7527 -- Search for the concurrent declaration since it contains the list
7528 -- of all implemented interfaces. In this case, the subprogram is
7529 -- declared within the scope of a protected or a task type.
7531 if Present (Scope (Def_Id))
7532 and then Is_Concurrent_Type (Scope (Def_Id))
7533 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7535 Typ := Scope (Def_Id);
7538 -- The enclosing scope is not a synchronized type and the subprogram
7541 elsif No (First_Formal (Def_Id)) then
7544 -- The subprogram has formals and hence it may be a primitive of a
7548 Typ := Etype (First_Formal (Def_Id));
7550 if Is_Access_Type (Typ) then
7551 Typ := Directly_Designated_Type (Typ);
7554 if Is_Concurrent_Type (Typ)
7555 and then not Is_Generic_Actual_Type (Typ)
7559 -- This case occurs when the concurrent type is declared within
7560 -- a generic unit. As a result the corresponding record has been
7561 -- built and used as the type of the first formal, we just have
7562 -- to retrieve the corresponding concurrent type.
7564 elsif Is_Concurrent_Record_Type (Typ)
7565 and then Present (Corresponding_Concurrent_Type (Typ))
7567 Typ := Corresponding_Concurrent_Type (Typ);
7575 -- There is no overriding to check if is an inherited operation in a
7576 -- type derivation on for a generic actual.
7578 Collect_Interfaces (Typ, Ifaces_List);
7580 if Is_Empty_Elmt_List (Ifaces_List) then
7584 -- Determine whether entry or subprogram Def_Id overrides a primitive
7585 -- operation that belongs to one of the interfaces in Ifaces_List.
7588 Candidate : Entity_Id := Empty;
7589 Hom : Entity_Id := Empty;
7590 Iface_Typ : Entity_Id;
7591 Subp : Entity_Id := Empty;
7594 -- Traverse the homonym chain, looking for a potentially
7595 -- overridden subprogram that belongs to an implemented
7598 Hom := Current_Entity_In_Scope (Def_Id);
7599 while Present (Hom) loop
7603 or else not Is_Overloadable (Subp)
7604 or else not Is_Primitive (Subp)
7605 or else not Is_Dispatching_Operation (Subp)
7606 or else not Present (Find_Dispatching_Type (Subp))
7607 or else not Is_Interface (Find_Dispatching_Type (Subp))
7611 -- Entries and procedures can override abstract or null
7612 -- interface procedures.
7614 elsif (Ekind (Def_Id) = E_Procedure
7615 or else Ekind (Def_Id) = E_Entry)
7616 and then Ekind (Subp) = E_Procedure
7617 and then Matches_Prefixed_View_Profile
7618 (Parameter_Specifications (Parent (Def_Id)),
7619 Parameter_Specifications (Parent (Subp)))
7623 -- For an overridden subprogram Subp, check whether the mode
7624 -- of its first parameter is correct depending on the kind
7625 -- of synchronized type.
7628 Formal : constant Node_Id := First_Formal (Candidate);
7631 -- In order for an entry or a protected procedure to
7632 -- override, the first parameter of the overridden
7633 -- routine must be of mode "out", "in out" or
7634 -- access-to-variable.
7636 if (Ekind (Candidate) = E_Entry
7637 or else Ekind (Candidate) = E_Procedure)
7638 and then Is_Protected_Type (Typ)
7639 and then Ekind (Formal) /= E_In_Out_Parameter
7640 and then Ekind (Formal) /= E_Out_Parameter
7641 and then Nkind (Parameter_Type (Parent (Formal)))
7642 /= N_Access_Definition
7646 -- All other cases are OK since a task entry or routine
7647 -- does not have a restriction on the mode of the first
7648 -- parameter of the overridden interface routine.
7651 Overridden_Subp := Candidate;
7656 -- Functions can override abstract interface functions
7658 elsif Ekind (Def_Id) = E_Function
7659 and then Ekind (Subp) = E_Function
7660 and then Matches_Prefixed_View_Profile
7661 (Parameter_Specifications (Parent (Def_Id)),
7662 Parameter_Specifications (Parent (Subp)))
7663 and then Etype (Result_Definition (Parent (Def_Id))) =
7664 Etype (Result_Definition (Parent (Subp)))
7666 Overridden_Subp := Subp;
7670 Hom := Homonym (Hom);
7673 -- After examining all candidates for overriding, we are left with
7674 -- the best match which is a mode incompatible interface routine.
7675 -- Do not emit an error if the Expander is active since this error
7676 -- will be detected later on after all concurrent types are
7677 -- expanded and all wrappers are built. This check is meant for
7678 -- spec-only compilations.
7680 if Present (Candidate) and then not Expander_Active then
7682 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7684 -- Def_Id is primitive of a protected type, declared inside the
7685 -- type, and the candidate is primitive of a limited or
7686 -- synchronized interface.
7689 and then Is_Protected_Type (Typ)
7691 (Is_Limited_Interface (Iface_Typ)
7692 or else Is_Protected_Interface (Iface_Typ)
7693 or else Is_Synchronized_Interface (Iface_Typ)
7694 or else Is_Task_Interface (Iface_Typ))
7697 ("first formal of & must be of mode `OUT`, `IN OUT`"
7698 & " or access-to-variable", Typ, Candidate);
7700 ("\in order to be overridden by protected procedure or "
7701 & "entry (RM 9.4(11.9/2))", Typ);
7705 Overridden_Subp := Candidate;
7708 end Check_Synchronized_Overriding;
7710 ----------------------------
7711 -- Is_Private_Declaration --
7712 ----------------------------
7714 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7715 Priv_Decls : List_Id;
7716 Decl : constant Node_Id := Unit_Declaration_Node (E);
7719 if Is_Package_Or_Generic_Package (Current_Scope)
7720 and then In_Private_Part (Current_Scope)
7723 Private_Declarations (
7724 Specification (Unit_Declaration_Node (Current_Scope)));
7726 return In_Package_Body (Current_Scope)
7728 (Is_List_Member (Decl)
7729 and then List_Containing (Decl) = Priv_Decls)
7730 or else (Nkind (Parent (Decl)) = N_Package_Specification
7733 (Defining_Entity (Parent (Decl)))
7734 and then List_Containing (Parent (Parent (Decl)))
7739 end Is_Private_Declaration;
7741 --------------------------
7742 -- Is_Overriding_Alias --
7743 --------------------------
7745 function Is_Overriding_Alias
7747 New_E : Entity_Id) return Boolean
7749 AO : constant Entity_Id := Alias (Old_E);
7750 AN : constant Entity_Id := Alias (New_E);
7753 return Scope (AO) /= Scope (AN)
7754 or else No (DTC_Entity (AO))
7755 or else No (DTC_Entity (AN))
7756 or else DT_Position (AO) = DT_Position (AN);
7757 end Is_Overriding_Alias;
7759 -- Start of processing for New_Overloaded_Entity
7762 -- We need to look for an entity that S may override. This must be a
7763 -- homonym in the current scope, so we look for the first homonym of
7764 -- S in the current scope as the starting point for the search.
7766 E := Current_Entity_In_Scope (S);
7768 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
7769 -- They are directly added to the list of primitive operations of
7770 -- Derived_Type, unless this is a rederivation in the private part
7771 -- of an operation that was already derived in the visible part of
7772 -- the current package.
7774 if Ada_Version >= Ada_2005
7775 and then Present (Derived_Type)
7776 and then Present (Alias (S))
7777 and then Is_Dispatching_Operation (Alias (S))
7778 and then Present (Find_Dispatching_Type (Alias (S)))
7779 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7781 -- For private types, when the full-view is processed we propagate to
7782 -- the full view the non-overridden entities whose attribute "alias"
7783 -- references an interface primitive. These entities were added by
7784 -- Derive_Subprograms to ensure that interface primitives are
7787 -- Inside_Freeze_Actions is non zero when S corresponds with an
7788 -- internal entity that links an interface primitive with its
7789 -- covering primitive through attribute Interface_Alias (see
7790 -- Add_Internal_Interface_Entities).
7792 if Inside_Freezing_Actions = 0
7793 and then Is_Package_Or_Generic_Package (Current_Scope)
7794 and then In_Private_Part (Current_Scope)
7795 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
7796 and then Nkind (Parent (S)) = N_Full_Type_Declaration
7797 and then Full_View (Defining_Identifier (Parent (E)))
7798 = Defining_Identifier (Parent (S))
7799 and then Alias (E) = Alias (S)
7801 Check_Operation_From_Private_View (S, E);
7802 Set_Is_Dispatching_Operation (S);
7807 Enter_Overloaded_Entity (S);
7808 Check_Dispatching_Operation (S, Empty);
7809 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7815 -- If there is no homonym then this is definitely not overriding
7818 Enter_Overloaded_Entity (S);
7819 Check_Dispatching_Operation (S, Empty);
7820 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7822 -- If subprogram has an explicit declaration, check whether it
7823 -- has an overriding indicator.
7825 if Comes_From_Source (S) then
7826 Check_Synchronized_Overriding (S, Overridden_Subp);
7828 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
7829 -- it may have overridden some hidden inherited primitive. Update
7830 -- Overriden_Subp to avoid spurious errors when checking the
7831 -- overriding indicator.
7833 if Ada_Version >= Ada_2012
7834 and then No (Overridden_Subp)
7835 and then Is_Dispatching_Operation (S)
7836 and then Is_Overriding_Operation (S)
7838 Overridden_Subp := Overridden_Operation (S);
7841 Check_Overriding_Indicator
7842 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7845 -- If there is a homonym that is not overloadable, then we have an
7846 -- error, except for the special cases checked explicitly below.
7848 elsif not Is_Overloadable (E) then
7850 -- Check for spurious conflict produced by a subprogram that has the
7851 -- same name as that of the enclosing generic package. The conflict
7852 -- occurs within an instance, between the subprogram and the renaming
7853 -- declaration for the package. After the subprogram, the package
7854 -- renaming declaration becomes hidden.
7856 if Ekind (E) = E_Package
7857 and then Present (Renamed_Object (E))
7858 and then Renamed_Object (E) = Current_Scope
7859 and then Nkind (Parent (Renamed_Object (E))) =
7860 N_Package_Specification
7861 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7864 Set_Is_Immediately_Visible (E, False);
7865 Enter_Overloaded_Entity (S);
7866 Set_Homonym (S, Homonym (E));
7867 Check_Dispatching_Operation (S, Empty);
7868 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7870 -- If the subprogram is implicit it is hidden by the previous
7871 -- declaration. However if it is dispatching, it must appear in the
7872 -- dispatch table anyway, because it can be dispatched to even if it
7873 -- cannot be called directly.
7875 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
7876 Set_Scope (S, Current_Scope);
7878 if Is_Dispatching_Operation (Alias (S)) then
7879 Check_Dispatching_Operation (S, Empty);
7885 Error_Msg_Sloc := Sloc (E);
7887 -- Generate message, with useful additional warning if in generic
7889 if Is_Generic_Unit (E) then
7890 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7891 Error_Msg_N ("\& conflicts with declaration#", S);
7893 Error_Msg_N ("& conflicts with declaration#", S);
7899 -- E exists and is overloadable
7902 Check_Synchronized_Overriding (S, Overridden_Subp);
7904 -- Loop through E and its homonyms to determine if any of them is
7905 -- the candidate for overriding by S.
7907 while Present (E) loop
7909 -- Definitely not interesting if not in the current scope
7911 if Scope (E) /= Current_Scope then
7914 -- Check if we have type conformance
7916 elsif Type_Conformant (E, S) then
7918 -- If the old and new entities have the same profile and one
7919 -- is not the body of the other, then this is an error, unless
7920 -- one of them is implicitly declared.
7922 -- There are some cases when both can be implicit, for example
7923 -- when both a literal and a function that overrides it are
7924 -- inherited in a derivation, or when an inherited operation
7925 -- of a tagged full type overrides the inherited operation of
7926 -- a private extension. Ada 83 had a special rule for the
7927 -- literal case. In Ada95, the later implicit operation hides
7928 -- the former, and the literal is always the former. In the
7929 -- odd case where both are derived operations declared at the
7930 -- same point, both operations should be declared, and in that
7931 -- case we bypass the following test and proceed to the next
7932 -- part. This can only occur for certain obscure cases in
7933 -- instances, when an operation on a type derived from a formal
7934 -- private type does not override a homograph inherited from
7935 -- the actual. In subsequent derivations of such a type, the
7936 -- DT positions of these operations remain distinct, if they
7939 if Present (Alias (S))
7940 and then (No (Alias (E))
7941 or else Comes_From_Source (E)
7942 or else Is_Abstract_Subprogram (S)
7944 (Is_Dispatching_Operation (E)
7945 and then Is_Overriding_Alias (E, S)))
7946 and then Ekind (E) /= E_Enumeration_Literal
7948 -- When an derived operation is overloaded it may be due to
7949 -- the fact that the full view of a private extension
7950 -- re-inherits. It has to be dealt with.
7952 if Is_Package_Or_Generic_Package (Current_Scope)
7953 and then In_Private_Part (Current_Scope)
7955 Check_Operation_From_Private_View (S, E);
7958 -- In any case the implicit operation remains hidden by
7959 -- the existing declaration, which is overriding.
7961 Set_Is_Overriding_Operation (E);
7963 if Comes_From_Source (E) then
7964 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7966 -- Indicate that E overrides the operation from which
7969 if Present (Alias (S)) then
7970 Set_Overridden_Operation (E, Alias (S));
7972 Set_Overridden_Operation (E, S);
7978 -- Within an instance, the renaming declarations for actual
7979 -- subprograms may become ambiguous, but they do not hide each
7982 elsif Ekind (E) /= E_Entry
7983 and then not Comes_From_Source (E)
7984 and then not Is_Generic_Instance (E)
7985 and then (Present (Alias (E))
7986 or else Is_Intrinsic_Subprogram (E))
7987 and then (not In_Instance
7988 or else No (Parent (E))
7989 or else Nkind (Unit_Declaration_Node (E)) /=
7990 N_Subprogram_Renaming_Declaration)
7992 -- A subprogram child unit is not allowed to override an
7993 -- inherited subprogram (10.1.1(20)).
7995 if Is_Child_Unit (S) then
7997 ("child unit overrides inherited subprogram in parent",
8002 if Is_Non_Overriding_Operation (E, S) then
8003 Enter_Overloaded_Entity (S);
8005 if No (Derived_Type)
8006 or else Is_Tagged_Type (Derived_Type)
8008 Check_Dispatching_Operation (S, Empty);
8014 -- E is a derived operation or an internal operator which
8015 -- is being overridden. Remove E from further visibility.
8016 -- Furthermore, if E is a dispatching operation, it must be
8017 -- replaced in the list of primitive operations of its type
8018 -- (see Override_Dispatching_Operation).
8020 Overridden_Subp := E;
8026 Prev := First_Entity (Current_Scope);
8027 while Present (Prev)
8028 and then Next_Entity (Prev) /= E
8033 -- It is possible for E to be in the current scope and
8034 -- yet not in the entity chain. This can only occur in a
8035 -- generic context where E is an implicit concatenation
8036 -- in the formal part, because in a generic body the
8037 -- entity chain starts with the formals.
8040 (Present (Prev) or else Chars (E) = Name_Op_Concat);
8042 -- E must be removed both from the entity_list of the
8043 -- current scope, and from the visibility chain
8045 if Debug_Flag_E then
8046 Write_Str ("Override implicit operation ");
8047 Write_Int (Int (E));
8051 -- If E is a predefined concatenation, it stands for four
8052 -- different operations. As a result, a single explicit
8053 -- declaration does not hide it. In a possible ambiguous
8054 -- situation, Disambiguate chooses the user-defined op,
8055 -- so it is correct to retain the previous internal one.
8057 if Chars (E) /= Name_Op_Concat
8058 or else Ekind (E) /= E_Operator
8060 -- For nondispatching derived operations that are
8061 -- overridden by a subprogram declared in the private
8062 -- part of a package, we retain the derived subprogram
8063 -- but mark it as not immediately visible. If the
8064 -- derived operation was declared in the visible part
8065 -- then this ensures that it will still be visible
8066 -- outside the package with the proper signature
8067 -- (calls from outside must also be directed to this
8068 -- version rather than the overriding one, unlike the
8069 -- dispatching case). Calls from inside the package
8070 -- will still resolve to the overriding subprogram
8071 -- since the derived one is marked as not visible
8072 -- within the package.
8074 -- If the private operation is dispatching, we achieve
8075 -- the overriding by keeping the implicit operation
8076 -- but setting its alias to be the overriding one. In
8077 -- this fashion the proper body is executed in all
8078 -- cases, but the original signature is used outside
8081 -- If the overriding is not in the private part, we
8082 -- remove the implicit operation altogether.
8084 if Is_Private_Declaration (S) then
8085 if not Is_Dispatching_Operation (E) then
8086 Set_Is_Immediately_Visible (E, False);
8088 -- Work done in Override_Dispatching_Operation,
8089 -- so nothing else need to be done here.
8095 -- Find predecessor of E in Homonym chain
8097 if E = Current_Entity (E) then
8100 Prev_Vis := Current_Entity (E);
8101 while Homonym (Prev_Vis) /= E loop
8102 Prev_Vis := Homonym (Prev_Vis);
8106 if Prev_Vis /= Empty then
8108 -- Skip E in the visibility chain
8110 Set_Homonym (Prev_Vis, Homonym (E));
8113 Set_Name_Entity_Id (Chars (E), Homonym (E));
8116 Set_Next_Entity (Prev, Next_Entity (E));
8118 if No (Next_Entity (Prev)) then
8119 Set_Last_Entity (Current_Scope, Prev);
8125 Enter_Overloaded_Entity (S);
8126 Set_Is_Overriding_Operation (S);
8127 Check_Overriding_Indicator (S, E, Is_Primitive => True);
8129 -- If S is a user-defined subprogram or a null procedure
8130 -- expanded to override an inherited null procedure, or a
8131 -- predefined dispatching primitive then indicate that E
8132 -- overrides the operation from which S is inherited. It
8133 -- seems odd that Overridden_Operation isn't set in all
8134 -- cases where Is_Overriding_Operation is true, but doing
8135 -- so causes infinite loops in the compiler for implicit
8136 -- overriding subprograms. ???
8138 if Comes_From_Source (S)
8140 (Present (Parent (S))
8142 Nkind (Parent (S)) = N_Procedure_Specification
8144 Null_Present (Parent (S)))
8146 (Present (Alias (E))
8148 Is_Predefined_Dispatching_Operation (Alias (E)))
8150 if Present (Alias (E)) then
8151 Set_Overridden_Operation (S, Alias (E));
8153 Set_Overridden_Operation (S, E);
8157 if Is_Dispatching_Operation (E) then
8159 -- An overriding dispatching subprogram inherits the
8160 -- convention of the overridden subprogram (by
8163 Set_Convention (S, Convention (E));
8164 Check_Dispatching_Operation (S, E);
8167 Check_Dispatching_Operation (S, Empty);
8170 Check_For_Primitive_Subprogram
8171 (Is_Primitive_Subp, Is_Overriding => True);
8172 goto Check_Inequality;
8175 -- Apparent redeclarations in instances can occur when two
8176 -- formal types get the same actual type. The subprograms in
8177 -- in the instance are legal, even if not callable from the
8178 -- outside. Calls from within are disambiguated elsewhere.
8179 -- For dispatching operations in the visible part, the usual
8180 -- rules apply, and operations with the same profile are not
8183 elsif (In_Instance_Visible_Part
8184 and then not Is_Dispatching_Operation (E))
8185 or else In_Instance_Not_Visible
8189 -- Here we have a real error (identical profile)
8192 Error_Msg_Sloc := Sloc (E);
8194 -- Avoid cascaded errors if the entity appears in
8195 -- subsequent calls.
8197 Set_Scope (S, Current_Scope);
8199 -- Generate error, with extra useful warning for the case
8200 -- of a generic instance with no completion.
8202 if Is_Generic_Instance (S)
8203 and then not Has_Completion (E)
8206 ("instantiation cannot provide body for&", S);
8207 Error_Msg_N ("\& conflicts with declaration#", S);
8209 Error_Msg_N ("& conflicts with declaration#", S);
8216 -- If one subprogram has an access parameter and the other
8217 -- a parameter of an access type, calls to either might be
8218 -- ambiguous. Verify that parameters match except for the
8219 -- access parameter.
8221 if May_Hide_Profile then
8227 F1 := First_Formal (S);
8228 F2 := First_Formal (E);
8229 while Present (F1) and then Present (F2) loop
8230 if Is_Access_Type (Etype (F1)) then
8231 if not Is_Access_Type (Etype (F2))
8232 or else not Conforming_Types
8233 (Designated_Type (Etype (F1)),
8234 Designated_Type (Etype (F2)),
8237 May_Hide_Profile := False;
8241 not Conforming_Types
8242 (Etype (F1), Etype (F2), Type_Conformant)
8244 May_Hide_Profile := False;
8255 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
8264 -- On exit, we know that S is a new entity
8266 Enter_Overloaded_Entity (S);
8267 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8268 Check_Overriding_Indicator
8269 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8271 -- If S is a derived operation for an untagged type then by
8272 -- definition it's not a dispatching operation (even if the parent
8273 -- operation was dispatching), so we don't call
8274 -- Check_Dispatching_Operation in that case.
8276 if No (Derived_Type)
8277 or else Is_Tagged_Type (Derived_Type)
8279 Check_Dispatching_Operation (S, Empty);
8283 -- If this is a user-defined equality operator that is not a derived
8284 -- subprogram, create the corresponding inequality. If the operation is
8285 -- dispatching, the expansion is done elsewhere, and we do not create
8286 -- an explicit inequality operation.
8288 <<Check_Inequality>>
8289 if Chars (S) = Name_Op_Eq
8290 and then Etype (S) = Standard_Boolean
8291 and then Present (Parent (S))
8292 and then not Is_Dispatching_Operation (S)
8294 Make_Inequality_Operator (S);
8296 if Ada_Version >= Ada_2012 then
8297 Check_Untagged_Equality (S);
8300 end New_Overloaded_Entity;
8302 ---------------------
8303 -- Process_Formals --
8304 ---------------------
8306 procedure Process_Formals
8308 Related_Nod : Node_Id)
8310 Param_Spec : Node_Id;
8312 Formal_Type : Entity_Id;
8316 Num_Out_Params : Nat := 0;
8317 First_Out_Param : Entity_Id := Empty;
8318 -- Used for setting Is_Only_Out_Parameter
8320 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8321 -- Determine whether an access type designates a type coming from a
8324 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8325 -- Check whether the default has a class-wide type. After analysis the
8326 -- default has the type of the formal, so we must also check explicitly
8327 -- for an access attribute.
8329 -------------------------------
8330 -- Designates_From_With_Type --
8331 -------------------------------
8333 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8334 Desig : Entity_Id := Typ;
8337 if Is_Access_Type (Desig) then
8338 Desig := Directly_Designated_Type (Desig);
8341 if Is_Class_Wide_Type (Desig) then
8342 Desig := Root_Type (Desig);
8346 Ekind (Desig) = E_Incomplete_Type
8347 and then From_With_Type (Desig);
8348 end Designates_From_With_Type;
8350 ---------------------------
8351 -- Is_Class_Wide_Default --
8352 ---------------------------
8354 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8356 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8357 or else (Nkind (D) = N_Attribute_Reference
8358 and then Attribute_Name (D) = Name_Access
8359 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8360 end Is_Class_Wide_Default;
8362 -- Start of processing for Process_Formals
8365 -- In order to prevent premature use of the formals in the same formal
8366 -- part, the Ekind is left undefined until all default expressions are
8367 -- analyzed. The Ekind is established in a separate loop at the end.
8369 Param_Spec := First (T);
8370 while Present (Param_Spec) loop
8371 Formal := Defining_Identifier (Param_Spec);
8372 Set_Never_Set_In_Source (Formal, True);
8373 Enter_Name (Formal);
8375 -- Case of ordinary parameters
8377 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8378 Find_Type (Parameter_Type (Param_Spec));
8379 Ptype := Parameter_Type (Param_Spec);
8381 if Ptype = Error then
8385 Formal_Type := Entity (Ptype);
8387 if Is_Incomplete_Type (Formal_Type)
8389 (Is_Class_Wide_Type (Formal_Type)
8390 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8392 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8393 -- primitive operations, as long as their completion is
8394 -- in the same declarative part. If in the private part
8395 -- this means that the type cannot be a Taft-amendment type.
8396 -- Check is done on package exit. For access to subprograms,
8397 -- the use is legal for Taft-amendment types.
8399 if Is_Tagged_Type (Formal_Type) then
8400 if Ekind (Scope (Current_Scope)) = E_Package
8401 and then In_Private_Part (Scope (Current_Scope))
8402 and then not From_With_Type (Formal_Type)
8403 and then not Is_Class_Wide_Type (Formal_Type)
8406 (Parent (T), N_Access_Function_Definition,
8407 N_Access_Procedure_Definition)
8411 Private_Dependents (Base_Type (Formal_Type)));
8415 -- Special handling of Value_Type for CIL case
8417 elsif Is_Value_Type (Formal_Type) then
8420 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8421 N_Access_Procedure_Definition)
8424 -- AI05-0151: Tagged incomplete types are allowed in all
8425 -- formal parts. Untagged incomplete types are not allowed
8428 if Ada_Version >= Ada_2012 then
8429 if Is_Tagged_Type (Formal_Type) then
8432 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
8437 ("invalid use of untagged incomplete type&",
8438 Ptype, Formal_Type);
8443 ("invalid use of incomplete type&",
8444 Param_Spec, Formal_Type);
8446 -- Further checks on the legality of incomplete types
8447 -- in formal parts are delayed until the freeze point
8448 -- of the enclosing subprogram or access to subprogram.
8452 elsif Ekind (Formal_Type) = E_Void then
8454 ("premature use of&",
8455 Parameter_Type (Param_Spec), Formal_Type);
8458 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8459 -- declaration corresponding to the null-excluding type of the
8460 -- formal in the enclosing scope. Finally, replace the parameter
8461 -- type of the formal with the internal subtype.
8463 if Ada_Version >= Ada_2005
8464 and then Null_Exclusion_Present (Param_Spec)
8466 if not Is_Access_Type (Formal_Type) then
8468 ("`NOT NULL` allowed only for an access type", Param_Spec);
8471 if Can_Never_Be_Null (Formal_Type)
8472 and then Comes_From_Source (Related_Nod)
8475 ("`NOT NULL` not allowed (& already excludes null)",
8476 Param_Spec, Formal_Type);
8480 Create_Null_Excluding_Itype
8482 Related_Nod => Related_Nod,
8483 Scope_Id => Scope (Current_Scope));
8485 -- If the designated type of the itype is an itype we
8486 -- decorate it with the Has_Delayed_Freeze attribute to
8487 -- avoid problems with the backend.
8490 -- type T is access procedure;
8491 -- procedure Op (O : not null T);
8493 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8494 Set_Has_Delayed_Freeze (Formal_Type);
8499 -- An access formal type
8503 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8505 -- No need to continue if we already notified errors
8507 if not Present (Formal_Type) then
8511 -- Ada 2005 (AI-254)
8514 AD : constant Node_Id :=
8515 Access_To_Subprogram_Definition
8516 (Parameter_Type (Param_Spec));
8518 if Present (AD) and then Protected_Present (AD) then
8520 Replace_Anonymous_Access_To_Protected_Subprogram
8526 Set_Etype (Formal, Formal_Type);
8527 Default := Expression (Param_Spec);
8529 if Present (Default) then
8530 if Out_Present (Param_Spec) then
8532 ("default initialization only allowed for IN parameters",
8536 -- Do the special preanalysis of the expression (see section on
8537 -- "Handling of Default Expressions" in the spec of package Sem).
8539 Preanalyze_Spec_Expression (Default, Formal_Type);
8541 -- An access to constant cannot be the default for
8542 -- an access parameter that is an access to variable.
8544 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8545 and then not Is_Access_Constant (Formal_Type)
8546 and then Is_Access_Type (Etype (Default))
8547 and then Is_Access_Constant (Etype (Default))
8550 ("formal that is access to variable cannot be initialized " &
8551 "with an access-to-constant expression", Default);
8554 -- Check that the designated type of an access parameter's default
8555 -- is not a class-wide type unless the parameter's designated type
8556 -- is also class-wide.
8558 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8559 and then not Designates_From_With_Type (Formal_Type)
8560 and then Is_Class_Wide_Default (Default)
8561 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8564 ("access to class-wide expression not allowed here", Default);
8567 -- Check incorrect use of dynamically tagged expressions
8569 if Is_Tagged_Type (Formal_Type) then
8570 Check_Dynamically_Tagged_Expression
8573 Related_Nod => Default);
8577 -- Ada 2005 (AI-231): Static checks
8579 if Ada_Version >= Ada_2005
8580 and then Is_Access_Type (Etype (Formal))
8581 and then Can_Never_Be_Null (Etype (Formal))
8583 Null_Exclusion_Static_Checks (Param_Spec);
8590 -- If this is the formal part of a function specification, analyze the
8591 -- subtype mark in the context where the formals are visible but not
8592 -- yet usable, and may hide outer homographs.
8594 if Nkind (Related_Nod) = N_Function_Specification then
8595 Analyze_Return_Type (Related_Nod);
8598 -- Now set the kind (mode) of each formal
8600 Param_Spec := First (T);
8601 while Present (Param_Spec) loop
8602 Formal := Defining_Identifier (Param_Spec);
8603 Set_Formal_Mode (Formal);
8605 if Ekind (Formal) = E_In_Parameter then
8606 Set_Default_Value (Formal, Expression (Param_Spec));
8608 if Present (Expression (Param_Spec)) then
8609 Default := Expression (Param_Spec);
8611 if Is_Scalar_Type (Etype (Default)) then
8613 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8615 Formal_Type := Entity (Parameter_Type (Param_Spec));
8618 Formal_Type := Access_Definition
8619 (Related_Nod, Parameter_Type (Param_Spec));
8622 Apply_Scalar_Range_Check (Default, Formal_Type);
8626 elsif Ekind (Formal) = E_Out_Parameter then
8627 Num_Out_Params := Num_Out_Params + 1;
8629 if Num_Out_Params = 1 then
8630 First_Out_Param := Formal;
8633 elsif Ekind (Formal) = E_In_Out_Parameter then
8634 Num_Out_Params := Num_Out_Params + 1;
8640 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8641 Set_Is_Only_Out_Parameter (First_Out_Param);
8643 end Process_Formals;
8649 procedure Process_PPCs
8651 Spec_Id : Entity_Id;
8652 Body_Id : Entity_Id)
8654 Loc : constant Source_Ptr := Sloc (N);
8658 Designator : Entity_Id;
8659 -- Subprogram designator, set from Spec_Id if present, else Body_Id
8661 Precond : Node_Id := Empty;
8662 -- Set non-Empty if we prepend precondition to the declarations. This
8663 -- is used to hook up inherited preconditions (adding the condition
8664 -- expression with OR ELSE, and adding the message).
8666 Inherited_Precond : Node_Id;
8667 -- Precondition inherited from parent subprogram
8669 Inherited : constant Subprogram_List :=
8670 Inherited_Subprograms (Spec_Id);
8671 -- List of subprograms inherited by this subprogram
8673 Plist : List_Id := No_List;
8674 -- List of generated postconditions
8676 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
8677 -- Prag contains an analyzed precondition or postcondition pragma. This
8678 -- function copies the pragma, changes it to the corresponding Check
8679 -- pragma and returns the Check pragma as the result. If Pspec is non-
8680 -- empty, this is the case of inheriting a PPC, where we must change
8681 -- references to parameters of the inherited subprogram to point to the
8682 -- corresponding parameters of the current subprogram.
8684 function Invariants_Present return Boolean;
8685 -- Determines if any invariants are present for any OUT or IN OUT
8686 -- parameters of the subprogram, or (for a function) for the return.
8692 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
8693 Nam : constant Name_Id := Pragma_Name (Prag);
8698 -- Prepare map if this is the case where we have to map entities of
8699 -- arguments in the overridden subprogram to corresponding entities
8700 -- of the current subprogram.
8711 Map := New_Elmt_List;
8712 PF := First_Formal (Pspec);
8713 CF := First_Formal (Designator);
8714 while Present (PF) loop
8715 Append_Elmt (PF, Map);
8716 Append_Elmt (CF, Map);
8723 -- Now we can copy the tree, doing any required substituations
8725 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
8727 -- Set Analyzed to false, since we want to reanalyze the check
8728 -- procedure. Note that it is only at the outer level that we
8729 -- do this fiddling, for the spec cases, the already preanalyzed
8730 -- parameters are not affected.
8732 Set_Analyzed (CP, False);
8734 -- We also make sure Comes_From_Source is False for the copy
8736 Set_Comes_From_Source (CP, False);
8738 -- For a postcondition pragma within a generic, preserve the pragma
8739 -- for later expansion.
8741 if Nam = Name_Postcondition
8742 and then not Expander_Active
8747 -- Change copy of pragma into corresponding pragma Check
8749 Prepend_To (Pragma_Argument_Associations (CP),
8750 Make_Pragma_Argument_Association (Sloc (Prag),
8752 Make_Identifier (Loc,
8754 Set_Pragma_Identifier (CP,
8755 Make_Identifier (Sloc (Prag),
8756 Chars => Name_Check));
8758 -- If this is inherited case and the current message starts with
8759 -- "failed p", we change it to "failed inherited p...".
8761 if Present (Pspec) then
8763 Msg : constant Node_Id :=
8764 Last (Pragma_Argument_Associations (CP));
8767 if Chars (Msg) = Name_Message then
8768 String_To_Name_Buffer (Strval (Expression (Msg)));
8770 if Name_Buffer (1 .. 8) = "failed p" then
8771 Insert_Str_In_Name_Buffer ("inherited ", 8);
8773 (Expression (Last (Pragma_Argument_Associations (CP))),
8774 String_From_Name_Buffer);
8780 -- Return the check pragma
8785 ------------------------
8786 -- Invariants_Present --
8787 ------------------------
8789 function Invariants_Present return Boolean is
8793 -- Check function return result
8795 if Ekind (Designator) /= E_Procedure
8796 and then Has_Invariants (Etype (Designator))
8803 Formal := First_Formal (Designator);
8804 while Present (Formal) loop
8805 if Ekind (Formal) /= E_In_Parameter
8806 and then Has_Invariants (Etype (Formal))
8811 Next_Formal (Formal);
8815 end Invariants_Present;
8817 -- Start of processing for Process_PPCs
8820 -- Capture designator from spec if present, else from body
8822 if Present (Spec_Id) then
8823 Designator := Spec_Id;
8825 Designator := Body_Id;
8828 -- Grab preconditions from spec
8830 if Present (Spec_Id) then
8832 -- Loop through PPC pragmas from spec. Note that preconditions from
8833 -- the body will be analyzed and converted when we scan the body
8834 -- declarations below.
8836 Prag := Spec_PPC_List (Spec_Id);
8837 while Present (Prag) loop
8838 if Pragma_Name (Prag) = Name_Precondition then
8840 -- For Pre (or Precondition pragma), we simply prepend the
8841 -- pragma to the list of declarations right away so that it
8842 -- will be executed at the start of the procedure. Note that
8843 -- this processing reverses the order of the list, which is
8844 -- what we want since new entries were chained to the head of
8845 -- the list. There can be more then one precondition when we
8846 -- use pragma Precondition
8848 if not Class_Present (Prag) then
8849 Prepend (Grab_PPC, Declarations (N));
8851 -- For Pre'Class there can only be one pragma, and we save
8852 -- it in Precond for now. We will add inherited Pre'Class
8853 -- stuff before inserting this pragma in the declarations.
8855 Precond := Grab_PPC;
8859 Prag := Next_Pragma (Prag);
8862 -- Now deal with inherited preconditions
8864 for J in Inherited'Range loop
8865 Prag := Spec_PPC_List (Inherited (J));
8867 while Present (Prag) loop
8868 if Pragma_Name (Prag) = Name_Precondition
8869 and then Class_Present (Prag)
8871 Inherited_Precond := Grab_PPC (Inherited (J));
8873 -- No precondition so far, so establish this as the first
8875 if No (Precond) then
8876 Precond := Inherited_Precond;
8878 -- Here we already have a precondition, add inherited one
8881 -- Add new precondition to old one using OR ELSE
8884 New_Expr : constant Node_Id :=
8888 (Pragma_Argument_Associations
8889 (Inherited_Precond))));
8890 Old_Expr : constant Node_Id :=
8894 (Pragma_Argument_Associations
8898 if Paren_Count (Old_Expr) = 0 then
8899 Set_Paren_Count (Old_Expr, 1);
8902 if Paren_Count (New_Expr) = 0 then
8903 Set_Paren_Count (New_Expr, 1);
8907 Make_Or_Else (Sloc (Old_Expr),
8908 Left_Opnd => Relocate_Node (Old_Expr),
8909 Right_Opnd => New_Expr));
8912 -- Add new message in the form:
8914 -- failed precondition from bla
8915 -- also failed inherited precondition from bla
8918 -- Skip this if exception locations are suppressed
8920 if not Exception_Locations_Suppressed then
8922 New_Msg : constant Node_Id :=
8925 (Pragma_Argument_Associations
8926 (Inherited_Precond)));
8927 Old_Msg : constant Node_Id :=
8930 (Pragma_Argument_Associations
8933 Start_String (Strval (Old_Msg));
8934 Store_String_Chars (ASCII.LF & " also ");
8935 Store_String_Chars (Strval (New_Msg));
8936 Set_Strval (Old_Msg, End_String);
8942 Prag := Next_Pragma (Prag);
8946 -- If we have built a precondition for Pre'Class (including any
8947 -- Pre'Class aspects inherited from parent subprograms), then we
8948 -- insert this composite precondition at this stage.
8950 if Present (Precond) then
8951 Prepend (Precond, Declarations (N));
8955 -- Build postconditions procedure if needed and prepend the following
8956 -- declaration to the start of the declarations for the subprogram.
8958 -- procedure _postconditions [(_Result : resulttype)] is
8960 -- pragma Check (Postcondition, condition [,message]);
8961 -- pragma Check (Postcondition, condition [,message]);
8963 -- Invariant_Procedure (_Result) ...
8964 -- Invariant_Procedure (Arg1)
8968 -- First we deal with the postconditions in the body
8970 if Is_Non_Empty_List (Declarations (N)) then
8972 -- Loop through declarations
8974 Prag := First (Declarations (N));
8975 while Present (Prag) loop
8976 if Nkind (Prag) = N_Pragma then
8978 -- If pragma, capture if enabled postcondition, else ignore
8980 if Pragma_Name (Prag) = Name_Postcondition
8981 and then Check_Enabled (Name_Postcondition)
8983 if Plist = No_List then
8984 Plist := Empty_List;
8989 -- If expansion is disabled, as in a generic unit, save
8990 -- pragma for later expansion.
8992 if not Expander_Active then
8993 Prepend (Grab_PPC, Declarations (N));
8995 Append (Grab_PPC, Plist);
9001 -- Not a pragma, if comes from source, then end scan
9003 elsif Comes_From_Source (Prag) then
9006 -- Skip stuff not coming from source
9014 -- Now deal with any postconditions from the spec
9016 if Present (Spec_Id) then
9017 Spec_Postconditions : declare
9018 procedure Process_Post_Conditions
9021 -- This processes the Spec_PPC_List from Spec, processing any
9022 -- postconditions from the list. If Class is True, then only
9023 -- postconditions marked with Class_Present are considered.
9024 -- The caller has checked that Spec_PPC_List is non-Empty.
9026 -----------------------------
9027 -- Process_Post_Conditions --
9028 -----------------------------
9030 procedure Process_Post_Conditions
9043 -- Loop through PPC pragmas from spec
9045 Prag := Spec_PPC_List (Spec);
9047 if Pragma_Name (Prag) = Name_Postcondition
9048 and then (not Class or else Class_Present (Prag))
9050 if Plist = No_List then
9051 Plist := Empty_List;
9054 if not Expander_Active then
9056 (Grab_PPC (Pspec), Declarations (N));
9058 Append (Grab_PPC (Pspec), Plist);
9062 Prag := Next_Pragma (Prag);
9063 exit when No (Prag);
9065 end Process_Post_Conditions;
9067 -- Start of processing for Spec_Postconditions
9070 if Present (Spec_PPC_List (Spec_Id)) then
9071 Process_Post_Conditions (Spec_Id, Class => False);
9074 -- Process inherited postconditions
9076 for J in Inherited'Range loop
9077 if Present (Spec_PPC_List (Inherited (J))) then
9078 Process_Post_Conditions (Inherited (J), Class => True);
9081 end Spec_Postconditions;
9084 -- If we had any postconditions and expansion is enabled, or if the
9085 -- procedure has invariants, then build the _Postconditions procedure.
9087 if (Present (Plist) or else Invariants_Present)
9088 and then Expander_Active
9091 Plist := Empty_List;
9094 -- Special processing for function case
9096 if Ekind (Designator) /= E_Procedure then
9098 Rent : constant Entity_Id :=
9099 Make_Defining_Identifier (Loc,
9100 Chars => Name_uResult);
9101 Ftyp : constant Entity_Id := Etype (Designator);
9104 Set_Etype (Rent, Ftyp);
9106 -- Add argument for return
9110 Make_Parameter_Specification (Loc,
9111 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
9112 Defining_Identifier => Rent));
9114 -- Add invariant call if returning type with invariants
9116 if Has_Invariants (Etype (Rent))
9117 and then Present (Invariant_Procedure (Etype (Rent)))
9120 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
9124 -- Procedure rather than a function
9130 -- Add invariant calls for parameters. Note that this is done for
9131 -- functions as well, since in Ada 2012 they can have IN OUT args.
9137 Formal := First_Formal (Designator);
9138 while Present (Formal) loop
9139 if Ekind (Formal) /= E_In_Parameter
9140 and then Has_Invariants (Etype (Formal))
9141 and then Present (Invariant_Procedure (Etype (Formal)))
9144 Make_Invariant_Call (New_Occurrence_Of (Formal, Loc)));
9147 Next_Formal (Formal);
9151 -- Build and insert postcondition procedure
9154 Post_Proc : constant Entity_Id :=
9155 Make_Defining_Identifier (Loc,
9156 Chars => Name_uPostconditions);
9157 -- The entity for the _Postconditions procedure
9160 Prepend_To (Declarations (N),
9161 Make_Subprogram_Body (Loc,
9163 Make_Procedure_Specification (Loc,
9164 Defining_Unit_Name => Post_Proc,
9165 Parameter_Specifications => Parms),
9167 Declarations => Empty_List,
9169 Handled_Statement_Sequence =>
9170 Make_Handled_Sequence_Of_Statements (Loc,
9171 Statements => Plist)));
9173 -- If this is a procedure, set the Postcondition_Proc attribute on
9174 -- the proper defining entity for the subprogram.
9176 if Ekind (Designator) = E_Procedure then
9177 Set_Postcondition_Proc (Designator, Post_Proc);
9181 Set_Has_Postconditions (Designator);
9185 ----------------------------
9186 -- Reference_Body_Formals --
9187 ----------------------------
9189 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
9194 if Error_Posted (Spec) then
9198 -- Iterate over both lists. They may be of different lengths if the two
9199 -- specs are not conformant.
9201 Fs := First_Formal (Spec);
9202 Fb := First_Formal (Bod);
9203 while Present (Fs) and then Present (Fb) loop
9204 Generate_Reference (Fs, Fb, 'b');
9207 Style.Check_Identifier (Fb, Fs);
9210 Set_Spec_Entity (Fb, Fs);
9211 Set_Referenced (Fs, False);
9215 end Reference_Body_Formals;
9217 -------------------------
9218 -- Set_Actual_Subtypes --
9219 -------------------------
9221 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
9225 First_Stmt : Node_Id := Empty;
9226 AS_Needed : Boolean;
9229 -- If this is an empty initialization procedure, no need to create
9230 -- actual subtypes (small optimization).
9232 if Ekind (Subp) = E_Procedure
9233 and then Is_Null_Init_Proc (Subp)
9238 Formal := First_Formal (Subp);
9239 while Present (Formal) loop
9240 T := Etype (Formal);
9242 -- We never need an actual subtype for a constrained formal
9244 if Is_Constrained (T) then
9247 -- If we have unknown discriminants, then we do not need an actual
9248 -- subtype, or more accurately we cannot figure it out! Note that
9249 -- all class-wide types have unknown discriminants.
9251 elsif Has_Unknown_Discriminants (T) then
9254 -- At this stage we have an unconstrained type that may need an
9255 -- actual subtype. For sure the actual subtype is needed if we have
9256 -- an unconstrained array type.
9258 elsif Is_Array_Type (T) then
9261 -- The only other case needing an actual subtype is an unconstrained
9262 -- record type which is an IN parameter (we cannot generate actual
9263 -- subtypes for the OUT or IN OUT case, since an assignment can
9264 -- change the discriminant values. However we exclude the case of
9265 -- initialization procedures, since discriminants are handled very
9266 -- specially in this context, see the section entitled "Handling of
9267 -- Discriminants" in Einfo.
9269 -- We also exclude the case of Discrim_SO_Functions (functions used
9270 -- in front end layout mode for size/offset values), since in such
9271 -- functions only discriminants are referenced, and not only are such
9272 -- subtypes not needed, but they cannot always be generated, because
9273 -- of order of elaboration issues.
9275 elsif Is_Record_Type (T)
9276 and then Ekind (Formal) = E_In_Parameter
9277 and then Chars (Formal) /= Name_uInit
9278 and then not Is_Unchecked_Union (T)
9279 and then not Is_Discrim_SO_Function (Subp)
9283 -- All other cases do not need an actual subtype
9289 -- Generate actual subtypes for unconstrained arrays and
9290 -- unconstrained discriminated records.
9293 if Nkind (N) = N_Accept_Statement then
9295 -- If expansion is active, The formal is replaced by a local
9296 -- variable that renames the corresponding entry of the
9297 -- parameter block, and it is this local variable that may
9298 -- require an actual subtype.
9300 if Expander_Active then
9301 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
9303 Decl := Build_Actual_Subtype (T, Formal);
9306 if Present (Handled_Statement_Sequence (N)) then
9308 First (Statements (Handled_Statement_Sequence (N)));
9309 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
9310 Mark_Rewrite_Insertion (Decl);
9312 -- If the accept statement has no body, there will be no
9313 -- reference to the actuals, so no need to compute actual
9320 Decl := Build_Actual_Subtype (T, Formal);
9321 Prepend (Decl, Declarations (N));
9322 Mark_Rewrite_Insertion (Decl);
9325 -- The declaration uses the bounds of an existing object, and
9326 -- therefore needs no constraint checks.
9328 Analyze (Decl, Suppress => All_Checks);
9330 -- We need to freeze manually the generated type when it is
9331 -- inserted anywhere else than in a declarative part.
9333 if Present (First_Stmt) then
9334 Insert_List_Before_And_Analyze (First_Stmt,
9335 Freeze_Entity (Defining_Identifier (Decl), N));
9338 if Nkind (N) = N_Accept_Statement
9339 and then Expander_Active
9341 Set_Actual_Subtype (Renamed_Object (Formal),
9342 Defining_Identifier (Decl));
9344 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
9348 Next_Formal (Formal);
9350 end Set_Actual_Subtypes;
9352 ---------------------
9353 -- Set_Formal_Mode --
9354 ---------------------
9356 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
9357 Spec : constant Node_Id := Parent (Formal_Id);
9360 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
9361 -- since we ensure that corresponding actuals are always valid at the
9362 -- point of the call.
9364 if Out_Present (Spec) then
9365 if Ekind (Scope (Formal_Id)) = E_Function
9366 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
9369 if Ada_Version >= Ada_2012 then
9370 if In_Present (Spec) then
9371 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9373 Set_Ekind (Formal_Id, E_Out_Parameter);
9377 Error_Msg_N ("functions can only have IN parameters", Spec);
9378 Set_Ekind (Formal_Id, E_In_Parameter);
9381 elsif In_Present (Spec) then
9382 Set_Ekind (Formal_Id, E_In_Out_Parameter);
9385 Set_Ekind (Formal_Id, E_Out_Parameter);
9386 Set_Never_Set_In_Source (Formal_Id, True);
9387 Set_Is_True_Constant (Formal_Id, False);
9388 Set_Current_Value (Formal_Id, Empty);
9392 Set_Ekind (Formal_Id, E_In_Parameter);
9395 -- Set Is_Known_Non_Null for access parameters since the language
9396 -- guarantees that access parameters are always non-null. We also set
9397 -- Can_Never_Be_Null, since there is no way to change the value.
9399 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
9401 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
9402 -- null; In Ada 2005, only if then null_exclusion is explicit.
9404 if Ada_Version < Ada_2005
9405 or else Can_Never_Be_Null (Etype (Formal_Id))
9407 Set_Is_Known_Non_Null (Formal_Id);
9408 Set_Can_Never_Be_Null (Formal_Id);
9411 -- Ada 2005 (AI-231): Null-exclusion access subtype
9413 elsif Is_Access_Type (Etype (Formal_Id))
9414 and then Can_Never_Be_Null (Etype (Formal_Id))
9416 Set_Is_Known_Non_Null (Formal_Id);
9419 Set_Mechanism (Formal_Id, Default_Mechanism);
9420 Set_Formal_Validity (Formal_Id);
9421 end Set_Formal_Mode;
9423 -------------------------
9424 -- Set_Formal_Validity --
9425 -------------------------
9427 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
9429 -- If no validity checking, then we cannot assume anything about the
9430 -- validity of parameters, since we do not know there is any checking
9431 -- of the validity on the call side.
9433 if not Validity_Checks_On then
9436 -- If validity checking for parameters is enabled, this means we are
9437 -- not supposed to make any assumptions about argument values.
9439 elsif Validity_Check_Parameters then
9442 -- If we are checking in parameters, we will assume that the caller is
9443 -- also checking parameters, so we can assume the parameter is valid.
9445 elsif Ekind (Formal_Id) = E_In_Parameter
9446 and then Validity_Check_In_Params
9448 Set_Is_Known_Valid (Formal_Id, True);
9450 -- Similar treatment for IN OUT parameters
9452 elsif Ekind (Formal_Id) = E_In_Out_Parameter
9453 and then Validity_Check_In_Out_Params
9455 Set_Is_Known_Valid (Formal_Id, True);
9457 end Set_Formal_Validity;
9459 ------------------------
9460 -- Subtype_Conformant --
9461 ------------------------
9463 function Subtype_Conformant
9464 (New_Id : Entity_Id;
9466 Skip_Controlling_Formals : Boolean := False) return Boolean
9470 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
9471 Skip_Controlling_Formals => Skip_Controlling_Formals);
9473 end Subtype_Conformant;
9475 ---------------------
9476 -- Type_Conformant --
9477 ---------------------
9479 function Type_Conformant
9480 (New_Id : Entity_Id;
9482 Skip_Controlling_Formals : Boolean := False) return Boolean
9486 May_Hide_Profile := False;
9489 (New_Id, Old_Id, Type_Conformant, False, Result,
9490 Skip_Controlling_Formals => Skip_Controlling_Formals);
9492 end Type_Conformant;
9494 -------------------------------
9495 -- Valid_Operator_Definition --
9496 -------------------------------
9498 procedure Valid_Operator_Definition (Designator : Entity_Id) is
9501 Id : constant Name_Id := Chars (Designator);
9505 F := First_Formal (Designator);
9506 while Present (F) loop
9509 if Present (Default_Value (F)) then
9511 ("default values not allowed for operator parameters",
9518 -- Verify that user-defined operators have proper number of arguments
9519 -- First case of operators which can only be unary
9522 or else Id = Name_Op_Abs
9526 -- Case of operators which can be unary or binary
9528 elsif Id = Name_Op_Add
9529 or Id = Name_Op_Subtract
9531 N_OK := (N in 1 .. 2);
9533 -- All other operators can only be binary
9541 ("incorrect number of arguments for operator", Designator);
9545 and then Base_Type (Etype (Designator)) = Standard_Boolean
9546 and then not Is_Intrinsic_Subprogram (Designator)
9549 ("explicit definition of inequality not allowed", Designator);
9551 end Valid_Operator_Definition;