1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2011, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
29 with Einfo; use Einfo;
30 with Elists; use Elists;
31 with Errout; use Errout;
32 with Expander; use Expander;
33 with Exp_Ch6; use Exp_Ch6;
34 with Exp_Ch7; use Exp_Ch7;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Lib.Xref; use Lib.Xref;
43 with Layout; use Layout;
44 with Namet; use Namet;
46 with Nlists; use Nlists;
47 with Nmake; use Nmake;
49 with Output; use Output;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Cat; use Sem_Cat;
56 with Sem_Ch3; use Sem_Ch3;
57 with Sem_Ch4; use Sem_Ch4;
58 with Sem_Ch5; use Sem_Ch5;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Util; use Sem_Util;
71 with Sem_Type; use Sem_Type;
72 with Sem_Warn; use Sem_Warn;
73 with Sinput; use Sinput;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinfo.CN; use Sinfo.CN;
77 with Snames; use Snames;
78 with Stringt; use Stringt;
80 with Stylesw; use Stylesw;
81 with Targparm; use Targparm;
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 function Can_Override_Operator (Subp : Entity_Id) return Boolean;
132 -- Returns true if Subp can override a predefined operator.
134 procedure Check_Conformance
137 Ctype : Conformance_Type;
139 Conforms : out Boolean;
140 Err_Loc : Node_Id := Empty;
141 Get_Inst : Boolean := False;
142 Skip_Controlling_Formals : Boolean := False);
143 -- Given two entities, this procedure checks that the profiles associated
144 -- with these entities meet the conformance criterion given by the third
145 -- parameter. If they conform, Conforms is set True and control returns
146 -- to the caller. If they do not conform, Conforms is set to False, and
147 -- in addition, if Errmsg is True on the call, proper messages are output
148 -- to complain about the conformance failure. If Err_Loc is non_Empty
149 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
150 -- error messages are placed on the appropriate part of the construct
151 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
152 -- against a formal access-to-subprogram type so Get_Instance_Of must
155 procedure Check_Subprogram_Order (N : Node_Id);
156 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
157 -- the alpha ordering rule for N if this ordering requirement applicable.
159 procedure Check_Returns
163 Proc : Entity_Id := Empty);
164 -- Called to check for missing return statements in a function body, or for
165 -- returns present in a procedure body which has No_Return set. HSS is the
166 -- handled statement sequence for the subprogram body. This procedure
167 -- checks all flow paths to make sure they either have return (Mode = 'F',
168 -- used for functions) or do not have a return (Mode = 'P', used for
169 -- No_Return procedures). The flag Err is set if there are any control
170 -- paths not explicitly terminated by a return in the function case, and is
171 -- True otherwise. Proc is the entity for the procedure case and is used
172 -- in posting the warning message.
174 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
175 -- In Ada 2012, a primitive equality operator on an untagged record type
176 -- must appear before the type is frozen, and have the same visibility as
177 -- that of the type. This procedure checks that this rule is met, and
178 -- otherwise emits an error on the subprogram declaration and a warning
179 -- on the earlier freeze point if it is easy to locate.
181 procedure Enter_Overloaded_Entity (S : Entity_Id);
182 -- This procedure makes S, a new overloaded entity, into the first visible
183 -- entity with that name.
185 function Is_Non_Overriding_Operation
187 New_E : Entity_Id) return Boolean;
188 -- Enforce the rule given in 12.3(18): a private operation in an instance
189 -- overrides an inherited operation only if the corresponding operation
190 -- was overriding in the generic. This can happen for primitive operations
191 -- of types derived (in the generic unit) from formal private or formal
194 procedure Make_Inequality_Operator (S : Entity_Id);
195 -- Create the declaration for an inequality operator that is implicitly
196 -- created by a user-defined equality operator that yields a boolean.
198 procedure May_Need_Actuals (Fun : Entity_Id);
199 -- Flag functions that can be called without parameters, i.e. those that
200 -- have no parameters, or those for which defaults exist for all parameters
202 procedure Process_PPCs
205 Body_Id : Entity_Id);
206 -- Called from Analyze[_Generic]_Subprogram_Body to deal with scanning post
207 -- conditions for the body and assembling and inserting the _postconditions
208 -- procedure. N is the node for the subprogram body and Body_Id/Spec_Id are
209 -- the entities for the body and separate spec (if there is no separate
210 -- spec, Spec_Id is Empty). Note that invariants and predicates may also
211 -- provide postconditions, and are also handled in this procedure.
213 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
214 -- Formal_Id is an formal parameter entity. This procedure deals with
215 -- setting the proper validity status for this entity, which depends on
216 -- the kind of parameter and the validity checking mode.
218 ---------------------------------------------
219 -- Analyze_Abstract_Subprogram_Declaration --
220 ---------------------------------------------
222 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
223 Designator : constant Entity_Id :=
224 Analyze_Subprogram_Specification (Specification (N));
225 Scop : constant Entity_Id := Current_Scope;
228 Check_SPARK_Restriction ("abstract subprogram is not allowed", N);
230 Generate_Definition (Designator);
231 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
232 Set_Is_Abstract_Subprogram (Designator);
233 New_Overloaded_Entity (Designator);
234 Check_Delayed_Subprogram (Designator);
236 Set_Categorization_From_Scope (Designator, Scop);
238 if Ekind (Scope (Designator)) = E_Protected_Type then
240 ("abstract subprogram not allowed in protected type", N);
242 -- Issue a warning if the abstract subprogram is neither a dispatching
243 -- operation nor an operation that overrides an inherited subprogram or
244 -- predefined operator, since this most likely indicates a mistake.
246 elsif Warn_On_Redundant_Constructs
247 and then not Is_Dispatching_Operation (Designator)
248 and then not Present (Overridden_Operation (Designator))
249 and then (not Is_Operator_Symbol_Name (Chars (Designator))
250 or else Scop /= Scope (Etype (First_Formal (Designator))))
253 ("?abstract subprogram is not dispatching or overriding", N);
256 Generate_Reference_To_Formals (Designator);
257 Check_Eliminated (Designator);
259 if Has_Aspects (N) then
260 Analyze_Aspect_Specifications (N, Designator);
262 end Analyze_Abstract_Subprogram_Declaration;
264 ---------------------------------
265 -- Analyze_Expression_Function --
266 ---------------------------------
268 procedure Analyze_Expression_Function (N : Node_Id) is
269 Loc : constant Source_Ptr := Sloc (N);
270 LocX : constant Source_Ptr := Sloc (Expression (N));
271 Expr : constant Node_Id := Expression (N);
272 Spec : constant Node_Id := Specification (N);
275 pragma Unreferenced (Def_Id);
278 -- If the expression is a completion, Prev is the entity whose
279 -- declaration is completed. Def_Id is needed to analyze the spec.
286 -- This is one of the occasions on which we transform the tree during
287 -- semantic analysis. If this is a completion, transform the expression
288 -- function into an equivalent subprogram body, and analyze it.
290 -- Expression functions are inlined unconditionally. The back-end will
291 -- determine whether this is possible.
293 Inline_Processing_Required := True;
294 New_Spec := Copy_Separate_Tree (Spec);
295 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
297 -- If there are previous overloadable entities with the same name,
298 -- check whether any of them is completed by the expression function.
300 if Present (Prev) and then Is_Overloadable (Prev) then
301 Def_Id := Analyze_Subprogram_Specification (Spec);
302 Prev := Find_Corresponding_Spec (N);
306 Make_Subprogram_Body (Loc,
307 Specification => New_Spec,
308 Declarations => Empty_List,
309 Handled_Statement_Sequence =>
310 Make_Handled_Sequence_Of_Statements (LocX,
311 Statements => New_List (
312 Make_Simple_Return_Statement (LocX,
313 Expression => Expression (N)))));
315 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
317 -- If the expression completes a generic subprogram, we must create a
318 -- separate node for the body, because at instantiation the original
319 -- node of the generic copy must be a generic subprogram body, and
320 -- cannot be a expression function. Otherwise we just rewrite the
321 -- expression with the non-generic body.
323 Insert_After (N, New_Body);
324 Rewrite (N, Make_Null_Statement (Loc));
325 Set_Has_Completion (Prev, False);
328 Set_Is_Inlined (Prev);
331 and then Comes_From_Source (Prev)
333 Set_Has_Completion (Prev, False);
334 Rewrite (N, New_Body);
337 -- Prev is the previous entity with the same name, but it is can
338 -- be an unrelated spec that is not completed by the expression
339 -- function. In that case the relevant entity is the one in the body.
340 -- Not clear that the backend can inline it in this case ???
342 if Has_Completion (Prev) then
343 Set_Is_Inlined (Prev);
345 Set_Is_Inlined (Defining_Entity (New_Body));
348 -- If this is not a completion, create both a declaration and a body, so
349 -- that the expression can be inlined whenever possible.
353 Make_Subprogram_Declaration (Loc, Specification => Spec);
355 Rewrite (N, New_Decl);
357 Set_Is_Inlined (Defining_Entity (New_Decl));
359 -- To prevent premature freeze action, insert the new body at the end
360 -- of the current declarations, or at the end of the package spec.
363 Decls : List_Id := List_Containing (N);
364 Par : constant Node_Id := Parent (Decls);
367 if Nkind (Par) = N_Package_Specification
368 and then Decls = Visible_Declarations (Par)
369 and then Present (Private_Declarations (Par))
370 and then not Is_Empty_List (Private_Declarations (Par))
372 Decls := Private_Declarations (Par);
375 Insert_After (Last (Decls), New_Body);
379 -- If the return expression is a static constant, we suppress warning
380 -- messages on unused formals, which in most cases will be noise.
382 Set_Is_Trivial_Subprogram (Defining_Entity (New_Body),
383 Is_OK_Static_Expression (Expr));
384 end Analyze_Expression_Function;
386 ----------------------------------------
387 -- Analyze_Extended_Return_Statement --
388 ----------------------------------------
390 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
392 Analyze_Return_Statement (N);
393 end Analyze_Extended_Return_Statement;
395 ----------------------------
396 -- Analyze_Function_Call --
397 ----------------------------
399 procedure Analyze_Function_Call (N : Node_Id) is
400 P : constant Node_Id := Name (N);
401 Actuals : constant List_Id := Parameter_Associations (N);
407 -- A call of the form A.B (X) may be an Ada 2005 call, which is
408 -- rewritten as B (A, X). If the rewriting is successful, the call
409 -- has been analyzed and we just return.
411 if Nkind (P) = N_Selected_Component
412 and then Name (N) /= P
413 and then Is_Rewrite_Substitution (N)
414 and then Present (Etype (N))
419 -- If error analyzing name, then set Any_Type as result type and return
421 if Etype (P) = Any_Type then
422 Set_Etype (N, Any_Type);
426 -- Otherwise analyze the parameters
428 if Present (Actuals) then
429 Actual := First (Actuals);
430 while Present (Actual) loop
432 Check_Parameterless_Call (Actual);
438 end Analyze_Function_Call;
440 -----------------------------
441 -- Analyze_Function_Return --
442 -----------------------------
444 procedure Analyze_Function_Return (N : Node_Id) is
445 Loc : constant Source_Ptr := Sloc (N);
446 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
447 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
449 R_Type : constant Entity_Id := Etype (Scope_Id);
450 -- Function result subtype
452 procedure Check_Limited_Return (Expr : Node_Id);
453 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning
454 -- limited types. Used only for simple return statements.
455 -- Expr is the expression returned.
457 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
458 -- Check that the return_subtype_indication properly matches the result
459 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
461 --------------------------
462 -- Check_Limited_Return --
463 --------------------------
465 procedure Check_Limited_Return (Expr : Node_Id) is
467 -- Ada 2005 (AI-318-02): Return-by-reference types have been
468 -- removed and replaced by anonymous access results. This is an
469 -- incompatibility with Ada 95. Not clear whether this should be
470 -- enforced yet or perhaps controllable with special switch. ???
472 -- A limited interface that is not immutably limited is OK.
474 if Is_Limited_Interface (R_Type)
476 not (Is_Task_Interface (R_Type)
477 or else Is_Protected_Interface (R_Type)
478 or else Is_Synchronized_Interface (R_Type))
482 elsif Is_Limited_Type (R_Type)
483 and then not Is_Interface (R_Type)
484 and then Comes_From_Source (N)
485 and then not In_Instance_Body
486 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
490 if Ada_Version >= Ada_2005
491 and then not Debug_Flag_Dot_L
492 and then not GNAT_Mode
495 ("(Ada 2005) cannot copy object of a limited type " &
496 "(RM-2005 6.5(5.5/2))", Expr);
498 if Is_Immutably_Limited_Type (R_Type) then
500 ("\return by reference not permitted in Ada 2005", Expr);
503 -- Warn in Ada 95 mode, to give folks a heads up about this
506 -- In GNAT mode, this is just a warning, to allow it to be
507 -- evilly turned off. Otherwise it is a real error.
509 -- In a generic context, simplify the warning because it makes
510 -- no sense to discuss pass-by-reference or copy.
512 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
513 if Inside_A_Generic then
515 ("return of limited object not permitted in Ada 2005 "
516 & "(RM-2005 6.5(5.5/2))?", Expr);
518 elsif Is_Immutably_Limited_Type (R_Type) then
520 ("return by reference not permitted in Ada 2005 "
521 & "(RM-2005 6.5(5.5/2))?", Expr);
524 ("cannot copy object of a limited type in Ada 2005 "
525 & "(RM-2005 6.5(5.5/2))?", Expr);
528 -- Ada 95 mode, compatibility warnings disabled
531 return; -- skip continuation messages below
534 if not Inside_A_Generic then
536 ("\consider switching to return of access type", Expr);
537 Explain_Limited_Type (R_Type, Expr);
540 end Check_Limited_Return;
542 -------------------------------------
543 -- Check_Return_Subtype_Indication --
544 -------------------------------------
546 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
547 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
549 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
550 -- Subtype given in the extended return statement (must match R_Type)
552 Subtype_Ind : constant Node_Id :=
553 Object_Definition (Original_Node (Obj_Decl));
555 R_Type_Is_Anon_Access :
557 Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type
559 Ekind (R_Type) = E_Anonymous_Access_Protected_Subprogram_Type
561 Ekind (R_Type) = E_Anonymous_Access_Type;
562 -- True if return type of the function is an anonymous access type
563 -- Can't we make Is_Anonymous_Access_Type in einfo ???
565 R_Stm_Type_Is_Anon_Access :
567 Ekind (R_Stm_Type) = E_Anonymous_Access_Subprogram_Type
569 Ekind (R_Stm_Type) = E_Anonymous_Access_Protected_Subprogram_Type
571 Ekind (R_Stm_Type) = E_Anonymous_Access_Type;
572 -- True if type of the return object is an anonymous access type
575 -- First, avoid cascaded errors
577 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
581 -- "return access T" case; check that the return statement also has
582 -- "access T", and that the subtypes statically match:
583 -- if this is an access to subprogram the signatures must match.
585 if R_Type_Is_Anon_Access then
586 if R_Stm_Type_Is_Anon_Access then
588 Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
590 if Base_Type (Designated_Type (R_Stm_Type)) /=
591 Base_Type (Designated_Type (R_Type))
592 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
595 ("subtype must statically match function result subtype",
596 Subtype_Mark (Subtype_Ind));
600 -- For two anonymous access to subprogram types, the
601 -- types themselves must be type conformant.
603 if not Conforming_Types
604 (R_Stm_Type, R_Type, Fully_Conformant)
607 ("subtype must statically match function result subtype",
613 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
616 -- If the return object is of an anonymous access type, then report
617 -- an error if the function's result type is not also anonymous.
619 elsif R_Stm_Type_Is_Anon_Access
620 and then not R_Type_Is_Anon_Access
622 Error_Msg_N ("anonymous access not allowed for function with " &
623 "named access result", Subtype_Ind);
625 -- Subtype indication case: check that the return object's type is
626 -- covered by the result type, and that the subtypes statically match
627 -- when the result subtype is constrained. Also handle record types
628 -- with unknown discriminants for which we have built the underlying
629 -- record view. Coverage is needed to allow specific-type return
630 -- objects when the result type is class-wide (see AI05-32).
632 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
633 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
637 Underlying_Record_View (Base_Type (R_Stm_Type))))
639 -- A null exclusion may be present on the return type, on the
640 -- function specification, on the object declaration or on the
643 if Is_Access_Type (R_Type)
645 (Can_Never_Be_Null (R_Type)
646 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
647 Can_Never_Be_Null (R_Stm_Type)
650 ("subtype must statically match function result subtype",
654 -- AI05-103: for elementary types, subtypes must statically match
656 if Is_Constrained (R_Type)
657 or else Is_Access_Type (R_Type)
659 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
661 ("subtype must statically match function result subtype",
666 elsif Etype (Base_Type (R_Type)) = R_Stm_Type
667 and then Is_Null_Extension (Base_Type (R_Type))
673 ("wrong type for return_subtype_indication", Subtype_Ind);
675 end Check_Return_Subtype_Indication;
677 ---------------------
678 -- Local Variables --
679 ---------------------
683 -- Start of processing for Analyze_Function_Return
686 Set_Return_Present (Scope_Id);
688 if Nkind (N) = N_Simple_Return_Statement then
689 Expr := Expression (N);
691 -- Guard against a malformed expression. The parser may have tried to
692 -- recover but the node is not analyzable.
694 if Nkind (Expr) = N_Error then
695 Set_Etype (Expr, Any_Type);
696 Expander_Mode_Save_And_Set (False);
700 -- The resolution of a controlled [extension] aggregate associated
701 -- with a return statement creates a temporary which needs to be
702 -- finalized on function exit. Wrap the return statement inside a
703 -- block so that the finalization machinery can detect this case.
704 -- This early expansion is done only when the return statement is
705 -- not part of a handled sequence of statements.
707 if Nkind_In (Expr, N_Aggregate,
708 N_Extension_Aggregate)
709 and then Needs_Finalization (R_Type)
710 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
713 Make_Block_Statement (Loc,
714 Handled_Statement_Sequence =>
715 Make_Handled_Sequence_Of_Statements (Loc,
716 Statements => New_List (Relocate_Node (N)))));
722 Analyze_And_Resolve (Expr, R_Type);
723 Check_Limited_Return (Expr);
726 -- RETURN only allowed in SPARK as the last statement in function
728 if Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
730 (Nkind (Parent (Parent (N))) /= N_Subprogram_Body
731 or else Present (Next (N)))
733 Check_SPARK_Restriction
734 ("RETURN should be the last statement in function", N);
738 Check_SPARK_Restriction ("extended RETURN is not allowed", N);
740 -- Analyze parts specific to extended_return_statement:
743 Obj_Decl : constant Node_Id :=
744 Last (Return_Object_Declarations (N));
746 HSS : constant Node_Id := Handled_Statement_Sequence (N);
749 Expr := Expression (Obj_Decl);
751 -- Note: The check for OK_For_Limited_Init will happen in
752 -- Analyze_Object_Declaration; we treat it as a normal
753 -- object declaration.
755 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
758 Check_Return_Subtype_Indication (Obj_Decl);
760 if Present (HSS) then
763 if Present (Exception_Handlers (HSS)) then
765 -- ???Has_Nested_Block_With_Handler needs to be set.
766 -- Probably by creating an actual N_Block_Statement.
767 -- Probably in Expand.
773 -- Mark the return object as referenced, since the return is an
774 -- implicit reference of the object.
776 Set_Referenced (Defining_Identifier (Obj_Decl));
778 Check_References (Stm_Entity);
782 -- Case of Expr present
786 -- Defend against previous errors
788 and then Nkind (Expr) /= N_Empty
789 and then Present (Etype (Expr))
791 -- Apply constraint check. Note that this is done before the implicit
792 -- conversion of the expression done for anonymous access types to
793 -- ensure correct generation of the null-excluding check associated
794 -- with null-excluding expressions found in return statements.
796 Apply_Constraint_Check (Expr, R_Type);
798 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
799 -- type, apply an implicit conversion of the expression to that type
800 -- to force appropriate static and run-time accessibility checks.
802 if Ada_Version >= Ada_2005
803 and then Ekind (R_Type) = E_Anonymous_Access_Type
805 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
806 Analyze_And_Resolve (Expr, R_Type);
809 -- If the result type is class-wide, then check that the return
810 -- expression's type is not declared at a deeper level than the
811 -- function (RM05-6.5(5.6/2)).
813 if Ada_Version >= Ada_2005
814 and then Is_Class_Wide_Type (R_Type)
816 if Type_Access_Level (Etype (Expr)) >
817 Subprogram_Access_Level (Scope_Id)
820 ("level of return expression type is deeper than " &
821 "class-wide function!", Expr);
825 -- Check incorrect use of dynamically tagged expression
827 if Is_Tagged_Type (R_Type) then
828 Check_Dynamically_Tagged_Expression
834 -- ??? A real run-time accessibility check is needed in cases
835 -- involving dereferences of access parameters. For now we just
836 -- check the static cases.
838 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
839 and then Is_Immutably_Limited_Type (Etype (Scope_Id))
840 and then Object_Access_Level (Expr) >
841 Subprogram_Access_Level (Scope_Id)
844 -- Suppress the message in a generic, where the rewriting
847 if Inside_A_Generic then
852 Make_Raise_Program_Error (Loc,
853 Reason => PE_Accessibility_Check_Failed));
857 ("cannot return a local value by reference?", N);
859 ("\& will be raised at run time?",
860 N, Standard_Program_Error);
865 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
866 and then Null_Exclusion_Present (Parent (Scope_Id))
868 Apply_Compile_Time_Constraint_Error
870 Msg => "(Ada 2005) null not allowed for "
871 & "null-excluding return?",
872 Reason => CE_Null_Not_Allowed);
875 -- Apply checks suggested by AI05-0144 (dangerous order dependence)
877 Check_Order_Dependence;
879 end Analyze_Function_Return;
881 -------------------------------------
882 -- Analyze_Generic_Subprogram_Body --
883 -------------------------------------
885 procedure Analyze_Generic_Subprogram_Body
889 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
890 Kind : constant Entity_Kind := Ekind (Gen_Id);
896 -- Copy body and disable expansion while analyzing the generic For a
897 -- stub, do not copy the stub (which would load the proper body), this
898 -- will be done when the proper body is analyzed.
900 if Nkind (N) /= N_Subprogram_Body_Stub then
901 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
906 Spec := Specification (N);
908 -- Within the body of the generic, the subprogram is callable, and
909 -- behaves like the corresponding non-generic unit.
911 Body_Id := Defining_Entity (Spec);
913 if Kind = E_Generic_Procedure
914 and then Nkind (Spec) /= N_Procedure_Specification
916 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
919 elsif Kind = E_Generic_Function
920 and then Nkind (Spec) /= N_Function_Specification
922 Error_Msg_N ("invalid body for generic function ", Body_Id);
926 Set_Corresponding_Body (Gen_Decl, Body_Id);
928 if Has_Completion (Gen_Id)
929 and then Nkind (Parent (N)) /= N_Subunit
931 Error_Msg_N ("duplicate generic body", N);
934 Set_Has_Completion (Gen_Id);
937 if Nkind (N) = N_Subprogram_Body_Stub then
938 Set_Ekind (Defining_Entity (Specification (N)), Kind);
940 Set_Corresponding_Spec (N, Gen_Id);
943 if Nkind (Parent (N)) = N_Compilation_Unit then
944 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
947 -- Make generic parameters immediately visible in the body. They are
948 -- needed to process the formals declarations. Then make the formals
949 -- visible in a separate step.
955 First_Ent : Entity_Id;
958 First_Ent := First_Entity (Gen_Id);
961 while Present (E) and then not Is_Formal (E) loop
966 Set_Use (Generic_Formal_Declarations (Gen_Decl));
968 -- Now generic formals are visible, and the specification can be
969 -- analyzed, for subsequent conformance check.
971 Body_Id := Analyze_Subprogram_Specification (Spec);
973 -- Make formal parameters visible
977 -- E is the first formal parameter, we loop through the formals
978 -- installing them so that they will be visible.
980 Set_First_Entity (Gen_Id, E);
981 while Present (E) loop
987 -- Visible generic entity is callable within its own body
989 Set_Ekind (Gen_Id, Ekind (Body_Id));
990 Set_Ekind (Body_Id, E_Subprogram_Body);
991 Set_Convention (Body_Id, Convention (Gen_Id));
992 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
993 Set_Scope (Body_Id, Scope (Gen_Id));
994 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
996 if Nkind (N) = N_Subprogram_Body_Stub then
998 -- No body to analyze, so restore state of generic unit
1000 Set_Ekind (Gen_Id, Kind);
1001 Set_Ekind (Body_Id, Kind);
1003 if Present (First_Ent) then
1004 Set_First_Entity (Gen_Id, First_Ent);
1011 -- If this is a compilation unit, it must be made visible explicitly,
1012 -- because the compilation of the declaration, unlike other library
1013 -- unit declarations, does not. If it is not a unit, the following
1014 -- is redundant but harmless.
1016 Set_Is_Immediately_Visible (Gen_Id);
1017 Reference_Body_Formals (Gen_Id, Body_Id);
1019 if Is_Child_Unit (Gen_Id) then
1020 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1023 Set_Actual_Subtypes (N, Current_Scope);
1025 -- Deal with preconditions and postconditions. In formal verification
1026 -- mode, we keep pre- and postconditions attached to entities rather
1027 -- than inserted in the code, in order to facilitate a distinct
1028 -- treatment for them.
1030 if not Alfa_Mode then
1031 Process_PPCs (N, Gen_Id, Body_Id);
1034 -- If the generic unit carries pre- or post-conditions, copy them
1035 -- to the original generic tree, so that they are properly added
1036 -- to any instantiation.
1039 Orig : constant Node_Id := Original_Node (N);
1043 Cond := First (Declarations (N));
1044 while Present (Cond) loop
1045 if Nkind (Cond) = N_Pragma
1046 and then Pragma_Name (Cond) = Name_Check
1048 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1050 elsif Nkind (Cond) = N_Pragma
1051 and then Pragma_Name (Cond) = Name_Postcondition
1053 Set_Ekind (Defining_Entity (Orig), Ekind (Gen_Id));
1054 Prepend (New_Copy_Tree (Cond), Declarations (Orig));
1063 Analyze_Declarations (Declarations (N));
1065 Analyze (Handled_Statement_Sequence (N));
1067 Save_Global_References (Original_Node (N));
1069 -- Prior to exiting the scope, include generic formals again (if any
1070 -- are present) in the set of local entities.
1072 if Present (First_Ent) then
1073 Set_First_Entity (Gen_Id, First_Ent);
1076 Check_References (Gen_Id);
1079 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1081 Check_Subprogram_Order (N);
1083 -- Outside of its body, unit is generic again
1085 Set_Ekind (Gen_Id, Kind);
1086 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1089 Style.Check_Identifier (Body_Id, Gen_Id);
1093 end Analyze_Generic_Subprogram_Body;
1095 -----------------------------
1096 -- Analyze_Operator_Symbol --
1097 -----------------------------
1099 -- An operator symbol such as "+" or "and" may appear in context where the
1100 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1101 -- is just a string, as in (conjunction = "or"). In these cases the parser
1102 -- generates this node, and the semantics does the disambiguation. Other
1103 -- such case are actuals in an instantiation, the generic unit in an
1104 -- instantiation, and pragma arguments.
1106 procedure Analyze_Operator_Symbol (N : Node_Id) is
1107 Par : constant Node_Id := Parent (N);
1110 if (Nkind (Par) = N_Function_Call
1111 and then N = Name (Par))
1112 or else Nkind (Par) = N_Function_Instantiation
1113 or else (Nkind (Par) = N_Indexed_Component
1114 and then N = Prefix (Par))
1115 or else (Nkind (Par) = N_Pragma_Argument_Association
1116 and then not Is_Pragma_String_Literal (Par))
1117 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1118 or else (Nkind (Par) = N_Attribute_Reference
1119 and then Attribute_Name (Par) /= Name_Value)
1121 Find_Direct_Name (N);
1124 Change_Operator_Symbol_To_String_Literal (N);
1127 end Analyze_Operator_Symbol;
1129 -----------------------------------
1130 -- Analyze_Parameter_Association --
1131 -----------------------------------
1133 procedure Analyze_Parameter_Association (N : Node_Id) is
1135 Analyze (Explicit_Actual_Parameter (N));
1136 end Analyze_Parameter_Association;
1138 ----------------------------
1139 -- Analyze_Procedure_Call --
1140 ----------------------------
1142 procedure Analyze_Procedure_Call (N : Node_Id) is
1143 Loc : constant Source_Ptr := Sloc (N);
1144 P : constant Node_Id := Name (N);
1145 Actuals : constant List_Id := Parameter_Associations (N);
1149 procedure Analyze_Call_And_Resolve;
1150 -- Do Analyze and Resolve calls for procedure call
1151 -- At end, check illegal order dependence.
1153 ------------------------------
1154 -- Analyze_Call_And_Resolve --
1155 ------------------------------
1157 procedure Analyze_Call_And_Resolve is
1159 if Nkind (N) = N_Procedure_Call_Statement then
1161 Resolve (N, Standard_Void_Type);
1163 -- Apply checks suggested by AI05-0144
1165 Check_Order_Dependence;
1170 end Analyze_Call_And_Resolve;
1172 -- Start of processing for Analyze_Procedure_Call
1175 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1176 -- a procedure call or an entry call. The prefix may denote an access
1177 -- to subprogram type, in which case an implicit dereference applies.
1178 -- If the prefix is an indexed component (without implicit dereference)
1179 -- then the construct denotes a call to a member of an entire family.
1180 -- If the prefix is a simple name, it may still denote a call to a
1181 -- parameterless member of an entry family. Resolution of these various
1182 -- interpretations is delicate.
1186 -- If this is a call of the form Obj.Op, the call may have been
1187 -- analyzed and possibly rewritten into a block, in which case
1190 if Analyzed (N) then
1194 -- If there is an error analyzing the name (which may have been
1195 -- rewritten if the original call was in prefix notation) then error
1196 -- has been emitted already, mark node and return.
1199 or else Etype (Name (N)) = Any_Type
1201 Set_Etype (N, Any_Type);
1205 -- Otherwise analyze the parameters
1207 if Present (Actuals) then
1208 Actual := First (Actuals);
1210 while Present (Actual) loop
1212 Check_Parameterless_Call (Actual);
1217 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1219 if Nkind (P) = N_Attribute_Reference
1220 and then (Attribute_Name (P) = Name_Elab_Spec
1221 or else Attribute_Name (P) = Name_Elab_Body
1222 or else Attribute_Name (P) = Name_Elab_Subp_Body)
1224 if Present (Actuals) then
1226 ("no parameters allowed for this call", First (Actuals));
1230 Set_Etype (N, Standard_Void_Type);
1233 elsif Is_Entity_Name (P)
1234 and then Is_Record_Type (Etype (Entity (P)))
1235 and then Remote_AST_I_Dereference (P)
1239 elsif Is_Entity_Name (P)
1240 and then Ekind (Entity (P)) /= E_Entry_Family
1242 if Is_Access_Type (Etype (P))
1243 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1244 and then No (Actuals)
1245 and then Comes_From_Source (N)
1247 Error_Msg_N ("missing explicit dereference in call", N);
1250 Analyze_Call_And_Resolve;
1252 -- If the prefix is the simple name of an entry family, this is
1253 -- a parameterless call from within the task body itself.
1255 elsif Is_Entity_Name (P)
1256 and then Nkind (P) = N_Identifier
1257 and then Ekind (Entity (P)) = E_Entry_Family
1258 and then Present (Actuals)
1259 and then No (Next (First (Actuals)))
1261 -- Can be call to parameterless entry family. What appears to be the
1262 -- sole argument is in fact the entry index. Rewrite prefix of node
1263 -- accordingly. Source representation is unchanged by this
1267 Make_Indexed_Component (Loc,
1269 Make_Selected_Component (Loc,
1270 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1271 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1272 Expressions => Actuals);
1273 Set_Name (N, New_N);
1274 Set_Etype (New_N, Standard_Void_Type);
1275 Set_Parameter_Associations (N, No_List);
1276 Analyze_Call_And_Resolve;
1278 elsif Nkind (P) = N_Explicit_Dereference then
1279 if Ekind (Etype (P)) = E_Subprogram_Type then
1280 Analyze_Call_And_Resolve;
1282 Error_Msg_N ("expect access to procedure in call", P);
1285 -- The name can be a selected component or an indexed component that
1286 -- yields an access to subprogram. Such a prefix is legal if the call
1287 -- has parameter associations.
1289 elsif Is_Access_Type (Etype (P))
1290 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1292 if Present (Actuals) then
1293 Analyze_Call_And_Resolve;
1295 Error_Msg_N ("missing explicit dereference in call ", N);
1298 -- If not an access to subprogram, then the prefix must resolve to the
1299 -- name of an entry, entry family, or protected operation.
1301 -- For the case of a simple entry call, P is a selected component where
1302 -- the prefix is the task and the selector name is the entry. A call to
1303 -- a protected procedure will have the same syntax. If the protected
1304 -- object contains overloaded operations, the entity may appear as a
1305 -- function, the context will select the operation whose type is Void.
1307 elsif Nkind (P) = N_Selected_Component
1308 and then (Ekind (Entity (Selector_Name (P))) = E_Entry
1310 Ekind (Entity (Selector_Name (P))) = E_Procedure
1312 Ekind (Entity (Selector_Name (P))) = E_Function)
1314 Analyze_Call_And_Resolve;
1316 elsif Nkind (P) = N_Selected_Component
1317 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1318 and then Present (Actuals)
1319 and then No (Next (First (Actuals)))
1321 -- Can be call to parameterless entry family. What appears to be the
1322 -- sole argument is in fact the entry index. Rewrite prefix of node
1323 -- accordingly. Source representation is unchanged by this
1327 Make_Indexed_Component (Loc,
1328 Prefix => New_Copy (P),
1329 Expressions => Actuals);
1330 Set_Name (N, New_N);
1331 Set_Etype (New_N, Standard_Void_Type);
1332 Set_Parameter_Associations (N, No_List);
1333 Analyze_Call_And_Resolve;
1335 -- For the case of a reference to an element of an entry family, P is
1336 -- an indexed component whose prefix is a selected component (task and
1337 -- entry family), and whose index is the entry family index.
1339 elsif Nkind (P) = N_Indexed_Component
1340 and then Nkind (Prefix (P)) = N_Selected_Component
1341 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1343 Analyze_Call_And_Resolve;
1345 -- If the prefix is the name of an entry family, it is a call from
1346 -- within the task body itself.
1348 elsif Nkind (P) = N_Indexed_Component
1349 and then Nkind (Prefix (P)) = N_Identifier
1350 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1353 Make_Selected_Component (Loc,
1354 Prefix => New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1355 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1356 Rewrite (Prefix (P), New_N);
1358 Analyze_Call_And_Resolve;
1360 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1361 -- procedure name, so the construct can only be a qualified expression.
1363 elsif Nkind (P) = N_Qualified_Expression
1364 and then Ada_Version >= Ada_2012
1366 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1369 -- Anything else is an error
1372 Error_Msg_N ("invalid procedure or entry call", N);
1374 end Analyze_Procedure_Call;
1376 ------------------------------
1377 -- Analyze_Return_Statement --
1378 ------------------------------
1380 procedure Analyze_Return_Statement (N : Node_Id) is
1382 pragma Assert (Nkind_In (N, N_Simple_Return_Statement,
1383 N_Extended_Return_Statement));
1385 Returns_Object : constant Boolean :=
1386 Nkind (N) = N_Extended_Return_Statement
1388 (Nkind (N) = N_Simple_Return_Statement
1389 and then Present (Expression (N)));
1390 -- True if we're returning something; that is, "return <expression>;"
1391 -- or "return Result : T [:= ...]". False for "return;". Used for error
1392 -- checking: If Returns_Object is True, N should apply to a function
1393 -- body; otherwise N should apply to a procedure body, entry body,
1394 -- accept statement, or extended return statement.
1396 function Find_What_It_Applies_To return Entity_Id;
1397 -- Find the entity representing the innermost enclosing body, accept
1398 -- statement, or extended return statement. If the result is a callable
1399 -- construct or extended return statement, then this will be the value
1400 -- of the Return_Applies_To attribute. Otherwise, the program is
1401 -- illegal. See RM-6.5(4/2).
1403 -----------------------------
1404 -- Find_What_It_Applies_To --
1405 -----------------------------
1407 function Find_What_It_Applies_To return Entity_Id is
1408 Result : Entity_Id := Empty;
1411 -- Loop outward through the Scope_Stack, skipping blocks, loops,
1412 -- and postconditions.
1414 for J in reverse 0 .. Scope_Stack.Last loop
1415 Result := Scope_Stack.Table (J).Entity;
1416 exit when not Ekind_In (Result, E_Block, E_Loop)
1417 and then Chars (Result) /= Name_uPostconditions;
1420 pragma Assert (Present (Result));
1422 end Find_What_It_Applies_To;
1424 -- Local declarations
1426 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1427 Kind : constant Entity_Kind := Ekind (Scope_Id);
1428 Loc : constant Source_Ptr := Sloc (N);
1429 Stm_Entity : constant Entity_Id :=
1431 (E_Return_Statement, Current_Scope, Loc, 'R');
1433 -- Start of processing for Analyze_Return_Statement
1436 Set_Return_Statement_Entity (N, Stm_Entity);
1438 Set_Etype (Stm_Entity, Standard_Void_Type);
1439 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1441 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1442 -- (4/2): an inner return statement will apply to this extended return.
1444 if Nkind (N) = N_Extended_Return_Statement then
1445 Push_Scope (Stm_Entity);
1448 -- Check that pragma No_Return is obeyed. Don't complain about the
1449 -- implicitly-generated return that is placed at the end.
1451 if No_Return (Scope_Id) and then Comes_From_Source (N) then
1452 Error_Msg_N ("RETURN statement not allowed (No_Return)", N);
1455 -- Warn on any unassigned OUT parameters if in procedure
1457 if Ekind (Scope_Id) = E_Procedure then
1458 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1461 -- Check that functions return objects, and other things do not
1463 if Kind = E_Function or else Kind = E_Generic_Function then
1464 if not Returns_Object then
1465 Error_Msg_N ("missing expression in return from function", N);
1468 elsif Kind = E_Procedure or else Kind = E_Generic_Procedure then
1469 if Returns_Object then
1470 Error_Msg_N ("procedure cannot return value (use function)", N);
1473 elsif Kind = E_Entry or else Kind = E_Entry_Family then
1474 if Returns_Object then
1475 if Is_Protected_Type (Scope (Scope_Id)) then
1476 Error_Msg_N ("entry body cannot return value", N);
1478 Error_Msg_N ("accept statement cannot return value", N);
1482 elsif Kind = E_Return_Statement then
1484 -- We are nested within another return statement, which must be an
1485 -- extended_return_statement.
1487 if Returns_Object then
1488 if Nkind (N) = N_Extended_Return_Statement then
1490 ("extended return statement cannot be nested (use `RETURN;`)",
1493 -- Case of a simple return statement with a value inside extended
1494 -- return statement.
1498 ("return nested in extended return statement cannot return " &
1499 "value (use `RETURN;`)", N);
1504 Error_Msg_N ("illegal context for return statement", N);
1507 if Ekind_In (Kind, E_Function, E_Generic_Function) then
1508 Analyze_Function_Return (N);
1510 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
1511 Set_Return_Present (Scope_Id);
1514 if Nkind (N) = N_Extended_Return_Statement then
1518 Kill_Current_Values (Last_Assignment_Only => True);
1519 Check_Unreachable_Code (N);
1520 end Analyze_Return_Statement;
1522 -------------------------------------
1523 -- Analyze_Simple_Return_Statement --
1524 -------------------------------------
1526 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
1528 if Present (Expression (N)) then
1529 Mark_Coextensions (N, Expression (N));
1532 Analyze_Return_Statement (N);
1533 end Analyze_Simple_Return_Statement;
1535 -------------------------
1536 -- Analyze_Return_Type --
1537 -------------------------
1539 procedure Analyze_Return_Type (N : Node_Id) is
1540 Designator : constant Entity_Id := Defining_Entity (N);
1541 Typ : Entity_Id := Empty;
1544 -- Normal case where result definition does not indicate an error
1546 if Result_Definition (N) /= Error then
1547 if Nkind (Result_Definition (N)) = N_Access_Definition then
1548 Check_SPARK_Restriction
1549 ("access result is not allowed", Result_Definition (N));
1551 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
1554 AD : constant Node_Id :=
1555 Access_To_Subprogram_Definition (Result_Definition (N));
1557 if Present (AD) and then Protected_Present (AD) then
1558 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1560 Typ := Access_Definition (N, Result_Definition (N));
1564 Set_Parent (Typ, Result_Definition (N));
1565 Set_Is_Local_Anonymous_Access (Typ);
1566 Set_Etype (Designator, Typ);
1568 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1570 Null_Exclusion_Static_Checks (N);
1572 -- Subtype_Mark case
1575 Find_Type (Result_Definition (N));
1576 Typ := Entity (Result_Definition (N));
1577 Set_Etype (Designator, Typ);
1579 -- Unconstrained array as result is not allowed in SPARK
1581 if Is_Array_Type (Typ)
1582 and then not Is_Constrained (Typ)
1584 Check_SPARK_Restriction
1585 ("returning an unconstrained array is not allowed",
1586 Result_Definition (N));
1589 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
1591 Null_Exclusion_Static_Checks (N);
1593 -- If a null exclusion is imposed on the result type, then create
1594 -- a null-excluding itype (an access subtype) and use it as the
1595 -- function's Etype. Note that the null exclusion checks are done
1596 -- right before this, because they don't get applied to types that
1597 -- do not come from source.
1599 if Is_Access_Type (Typ)
1600 and then Null_Exclusion_Present (N)
1602 Set_Etype (Designator,
1603 Create_Null_Excluding_Itype
1606 Scope_Id => Scope (Current_Scope)));
1608 -- The new subtype must be elaborated before use because
1609 -- it is visible outside of the function. However its base
1610 -- type may not be frozen yet, so the reference that will
1611 -- force elaboration must be attached to the freezing of
1614 -- If the return specification appears on a proper body,
1615 -- the subtype will have been created already on the spec.
1617 if Is_Frozen (Typ) then
1618 if Nkind (Parent (N)) = N_Subprogram_Body
1619 and then Nkind (Parent (Parent (N))) = N_Subunit
1623 Build_Itype_Reference (Etype (Designator), Parent (N));
1627 Ensure_Freeze_Node (Typ);
1630 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
1632 Set_Itype (IR, Etype (Designator));
1633 Append_Freeze_Actions (Typ, New_List (IR));
1638 Set_Etype (Designator, Typ);
1641 if Ekind (Typ) = E_Incomplete_Type
1642 and then Is_Value_Type (Typ)
1646 elsif Ekind (Typ) = E_Incomplete_Type
1647 or else (Is_Class_Wide_Type (Typ)
1649 Ekind (Root_Type (Typ)) = E_Incomplete_Type)
1651 -- AI05-0151: Tagged incomplete types are allowed in all formal
1652 -- parts. Untagged incomplete types are not allowed in bodies.
1654 if Ada_Version >= Ada_2012 then
1655 if Is_Tagged_Type (Typ) then
1658 elsif Nkind_In (Parent (Parent (N)),
1664 ("invalid use of untagged incomplete type&",
1668 -- The type must be completed in the current package. This
1669 -- is checked at the end of the package declaraton, when
1670 -- Taft-amendment types are identified. If the return type
1671 -- is class-wide, there is no required check, the type can
1672 -- be a bona fide TAT.
1674 if Ekind (Scope (Current_Scope)) = E_Package
1675 and then In_Private_Part (Scope (Current_Scope))
1676 and then not Is_Class_Wide_Type (Typ)
1678 Append_Elmt (Designator, Private_Dependents (Typ));
1683 ("invalid use of incomplete type&", Designator, Typ);
1688 -- Case where result definition does indicate an error
1691 Set_Etype (Designator, Any_Type);
1693 end Analyze_Return_Type;
1695 -----------------------------
1696 -- Analyze_Subprogram_Body --
1697 -----------------------------
1699 procedure Analyze_Subprogram_Body (N : Node_Id) is
1700 Loc : constant Source_Ptr := Sloc (N);
1701 Body_Spec : constant Node_Id := Specification (N);
1702 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
1705 if Debug_Flag_C then
1706 Write_Str ("==> subprogram body ");
1707 Write_Name (Chars (Body_Id));
1708 Write_Str (" from ");
1709 Write_Location (Loc);
1714 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
1716 -- The real work is split out into the helper, so it can do "return;"
1717 -- without skipping the debug output:
1719 Analyze_Subprogram_Body_Helper (N);
1721 if Debug_Flag_C then
1723 Write_Str ("<== subprogram body ");
1724 Write_Name (Chars (Body_Id));
1725 Write_Str (" from ");
1726 Write_Location (Loc);
1729 end Analyze_Subprogram_Body;
1731 ------------------------------------
1732 -- Analyze_Subprogram_Body_Helper --
1733 ------------------------------------
1735 -- This procedure is called for regular subprogram bodies, generic bodies,
1736 -- and for subprogram stubs of both kinds. In the case of stubs, only the
1737 -- specification matters, and is used to create a proper declaration for
1738 -- the subprogram, or to perform conformance checks.
1740 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
1741 Loc : constant Source_Ptr := Sloc (N);
1742 Body_Deleted : constant Boolean := False;
1743 Body_Spec : constant Node_Id := Specification (N);
1744 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
1745 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
1746 Conformant : Boolean;
1749 Prot_Typ : Entity_Id := Empty;
1750 Spec_Id : Entity_Id;
1751 Spec_Decl : Node_Id := Empty;
1753 Last_Real_Spec_Entity : Entity_Id := Empty;
1754 -- When we analyze a separate spec, the entity chain ends up containing
1755 -- the formals, as well as any itypes generated during analysis of the
1756 -- default expressions for parameters, or the arguments of associated
1757 -- precondition/postcondition pragmas (which are analyzed in the context
1758 -- of the spec since they have visibility on formals).
1760 -- These entities belong with the spec and not the body. However we do
1761 -- the analysis of the body in the context of the spec (again to obtain
1762 -- visibility to the formals), and all the entities generated during
1763 -- this analysis end up also chained to the entity chain of the spec.
1764 -- But they really belong to the body, and there is circuitry to move
1765 -- them from the spec to the body.
1767 -- However, when we do this move, we don't want to move the real spec
1768 -- entities (first para above) to the body. The Last_Real_Spec_Entity
1769 -- variable points to the last real spec entity, so we only move those
1770 -- chained beyond that point. It is initialized to Empty to deal with
1771 -- the case where there is no separate spec.
1773 procedure Check_Anonymous_Return;
1774 -- Ada 2005: if a function returns an access type that denotes a task,
1775 -- or a type that contains tasks, we must create a master entity for
1776 -- the anonymous type, which typically will be used in an allocator
1777 -- in the body of the function.
1779 procedure Check_Inline_Pragma (Spec : in out Node_Id);
1780 -- Look ahead to recognize a pragma that may appear after the body.
1781 -- If there is a previous spec, check that it appears in the same
1782 -- declarative part. If the pragma is Inline_Always, perform inlining
1783 -- unconditionally, otherwise only if Front_End_Inlining is requested.
1784 -- If the body acts as a spec, and inlining is required, we create a
1785 -- subprogram declaration for it, in order to attach the body to inline.
1786 -- If pragma does not appear after the body, check whether there is
1787 -- an inline pragma before any local declarations.
1789 procedure Check_Missing_Return;
1790 -- Checks for a function with a no return statements, and also performs
1791 -- the warning checks implemented by Check_Returns. In formal mode, also
1792 -- verify that a function ends with a RETURN and that a procedure does
1793 -- not contain any RETURN.
1795 function Disambiguate_Spec return Entity_Id;
1796 -- When a primitive is declared between the private view and the full
1797 -- view of a concurrent type which implements an interface, a special
1798 -- mechanism is used to find the corresponding spec of the primitive
1801 procedure Exchange_Limited_Views (Subp_Id : Entity_Id);
1802 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
1803 -- incomplete types coming from a limited context and swap their limited
1804 -- views with the non-limited ones.
1806 function Is_Private_Concurrent_Primitive
1807 (Subp_Id : Entity_Id) return Boolean;
1808 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
1809 -- type that implements an interface and has a private view.
1811 procedure Set_Trivial_Subprogram (N : Node_Id);
1812 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
1813 -- subprogram whose body is being analyzed. N is the statement node
1814 -- causing the flag to be set, if the following statement is a return
1815 -- of an entity, we mark the entity as set in source to suppress any
1816 -- warning on the stylized use of function stubs with a dummy return.
1818 procedure Verify_Overriding_Indicator;
1819 -- If there was a previous spec, the entity has been entered in the
1820 -- current scope previously. If the body itself carries an overriding
1821 -- indicator, check that it is consistent with the known status of the
1824 ----------------------------
1825 -- Check_Anonymous_Return --
1826 ----------------------------
1828 procedure Check_Anonymous_Return is
1834 if Present (Spec_Id) then
1840 if Ekind (Scop) = E_Function
1841 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
1842 and then not Is_Thunk (Scop)
1843 and then (Has_Task (Designated_Type (Etype (Scop)))
1845 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
1847 Is_Limited_Record (Designated_Type (Etype (Scop)))))
1848 and then Expander_Active
1850 -- Avoid cases with no tasking support
1852 and then RTE_Available (RE_Current_Master)
1853 and then not Restriction_Active (No_Task_Hierarchy)
1856 Make_Object_Declaration (Loc,
1857 Defining_Identifier =>
1858 Make_Defining_Identifier (Loc, Name_uMaster),
1859 Constant_Present => True,
1860 Object_Definition =>
1861 New_Reference_To (RTE (RE_Master_Id), Loc),
1863 Make_Explicit_Dereference (Loc,
1864 New_Reference_To (RTE (RE_Current_Master), Loc)));
1866 if Present (Declarations (N)) then
1867 Prepend (Decl, Declarations (N));
1869 Set_Declarations (N, New_List (Decl));
1872 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
1873 Set_Has_Master_Entity (Scop);
1875 -- Now mark the containing scope as a task master
1878 while Nkind (Par) /= N_Compilation_Unit loop
1879 Par := Parent (Par);
1880 pragma Assert (Present (Par));
1882 -- If we fall off the top, we are at the outer level, and
1883 -- the environment task is our effective master, so nothing
1887 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
1889 Set_Is_Task_Master (Par, True);
1894 end Check_Anonymous_Return;
1896 -------------------------
1897 -- Check_Inline_Pragma --
1898 -------------------------
1900 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
1904 function Is_Inline_Pragma (N : Node_Id) return Boolean;
1905 -- True when N is a pragma Inline or Inline_Always that applies
1906 -- to this subprogram.
1908 -----------------------
1909 -- Is_Inline_Pragma --
1910 -----------------------
1912 function Is_Inline_Pragma (N : Node_Id) return Boolean is
1915 Nkind (N) = N_Pragma
1917 (Pragma_Name (N) = Name_Inline_Always
1920 and then Pragma_Name (N) = Name_Inline))
1923 (Expression (First (Pragma_Argument_Associations (N))))
1925 end Is_Inline_Pragma;
1927 -- Start of processing for Check_Inline_Pragma
1930 if not Expander_Active then
1934 if Is_List_Member (N)
1935 and then Present (Next (N))
1936 and then Is_Inline_Pragma (Next (N))
1940 elsif Nkind (N) /= N_Subprogram_Body_Stub
1941 and then Present (Declarations (N))
1942 and then Is_Inline_Pragma (First (Declarations (N)))
1944 Prag := First (Declarations (N));
1950 if Present (Prag) then
1951 if Present (Spec_Id) then
1952 if In_Same_List (N, Unit_Declaration_Node (Spec_Id)) then
1957 -- Create a subprogram declaration, to make treatment uniform
1960 Subp : constant Entity_Id :=
1961 Make_Defining_Identifier (Loc, Chars (Body_Id));
1962 Decl : constant Node_Id :=
1963 Make_Subprogram_Declaration (Loc,
1965 New_Copy_Tree (Specification (N)));
1968 Set_Defining_Unit_Name (Specification (Decl), Subp);
1970 if Present (First_Formal (Body_Id)) then
1971 Plist := Copy_Parameter_List (Body_Id);
1972 Set_Parameter_Specifications
1973 (Specification (Decl), Plist);
1976 Insert_Before (N, Decl);
1979 Set_Has_Pragma_Inline (Subp);
1981 if Pragma_Name (Prag) = Name_Inline_Always then
1982 Set_Is_Inlined (Subp);
1983 Set_Has_Pragma_Inline_Always (Subp);
1990 end Check_Inline_Pragma;
1992 --------------------------
1993 -- Check_Missing_Return --
1994 --------------------------
1996 procedure Check_Missing_Return is
1998 Missing_Ret : Boolean;
2001 if Nkind (Body_Spec) = N_Function_Specification then
2002 if Present (Spec_Id) then
2008 if Return_Present (Id) then
2009 Check_Returns (HSS, 'F', Missing_Ret);
2012 Set_Has_Missing_Return (Id);
2015 elsif (Is_Generic_Subprogram (Id)
2016 or else not Is_Machine_Code_Subprogram (Id))
2017 and then not Body_Deleted
2019 Error_Msg_N ("missing RETURN statement in function body", N);
2022 -- If procedure with No_Return, check returns
2024 elsif Nkind (Body_Spec) = N_Procedure_Specification
2025 and then Present (Spec_Id)
2026 and then No_Return (Spec_Id)
2028 Check_Returns (HSS, 'P', Missing_Ret, Spec_Id);
2031 -- Special checks in SPARK mode
2033 if Nkind (Body_Spec) = N_Function_Specification then
2035 -- In SPARK mode, last statement of a function should be a return
2038 Stat : constant Node_Id := Last_Source_Statement (HSS);
2041 and then not Nkind_In (Stat, N_Simple_Return_Statement,
2042 N_Extended_Return_Statement)
2044 Check_SPARK_Restriction
2045 ("last statement in function should be RETURN", Stat);
2049 -- In SPARK mode, verify that a procedure has no return
2051 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2052 if Present (Spec_Id) then
2058 -- Would be nice to point to return statement here, can we
2059 -- borrow the Check_Returns procedure here ???
2061 if Return_Present (Id) then
2062 Check_SPARK_Restriction
2063 ("procedure should not have RETURN", N);
2066 end Check_Missing_Return;
2068 -----------------------
2069 -- Disambiguate_Spec --
2070 -----------------------
2072 function Disambiguate_Spec return Entity_Id is
2073 Priv_Spec : Entity_Id;
2076 procedure Replace_Types (To_Corresponding : Boolean);
2077 -- Depending on the flag, replace the type of formal parameters of
2078 -- Body_Id if it is a concurrent type implementing interfaces with
2079 -- the corresponding record type or the other way around.
2081 procedure Replace_Types (To_Corresponding : Boolean) is
2083 Formal_Typ : Entity_Id;
2086 Formal := First_Formal (Body_Id);
2087 while Present (Formal) loop
2088 Formal_Typ := Etype (Formal);
2090 if Is_Class_Wide_Type (Formal_Typ) then
2091 Formal_Typ := Root_Type (Formal_Typ);
2094 -- From concurrent type to corresponding record
2096 if To_Corresponding then
2097 if Is_Concurrent_Type (Formal_Typ)
2098 and then Present (Corresponding_Record_Type (Formal_Typ))
2099 and then Present (Interfaces (
2100 Corresponding_Record_Type (Formal_Typ)))
2103 Corresponding_Record_Type (Formal_Typ));
2106 -- From corresponding record to concurrent type
2109 if Is_Concurrent_Record_Type (Formal_Typ)
2110 and then Present (Interfaces (Formal_Typ))
2113 Corresponding_Concurrent_Type (Formal_Typ));
2117 Next_Formal (Formal);
2121 -- Start of processing for Disambiguate_Spec
2124 -- Try to retrieve the specification of the body as is. All error
2125 -- messages are suppressed because the body may not have a spec in
2126 -- its current state.
2128 Spec_N := Find_Corresponding_Spec (N, False);
2130 -- It is possible that this is the body of a primitive declared
2131 -- between a private and a full view of a concurrent type. The
2132 -- controlling parameter of the spec carries the concurrent type,
2133 -- not the corresponding record type as transformed by Analyze_
2134 -- Subprogram_Specification. In such cases, we undo the change
2135 -- made by the analysis of the specification and try to find the
2138 -- Note that wrappers already have their corresponding specs and
2139 -- bodies set during their creation, so if the candidate spec is
2140 -- a wrapper, then we definitely need to swap all types to their
2141 -- original concurrent status.
2144 or else Is_Primitive_Wrapper (Spec_N)
2146 -- Restore all references of corresponding record types to the
2147 -- original concurrent types.
2149 Replace_Types (To_Corresponding => False);
2150 Priv_Spec := Find_Corresponding_Spec (N, False);
2152 -- The current body truly belongs to a primitive declared between
2153 -- a private and a full view. We leave the modified body as is,
2154 -- and return the true spec.
2156 if Present (Priv_Spec)
2157 and then Is_Private_Primitive (Priv_Spec)
2162 -- In case that this is some sort of error, restore the original
2163 -- state of the body.
2165 Replace_Types (To_Corresponding => True);
2169 end Disambiguate_Spec;
2171 ----------------------------
2172 -- Exchange_Limited_Views --
2173 ----------------------------
2175 procedure Exchange_Limited_Views (Subp_Id : Entity_Id) is
2176 procedure Detect_And_Exchange (Id : Entity_Id);
2177 -- Determine whether Id's type denotes an incomplete type associated
2178 -- with a limited with clause and exchange the limited view with the
2181 -------------------------
2182 -- Detect_And_Exchange --
2183 -------------------------
2185 procedure Detect_And_Exchange (Id : Entity_Id) is
2186 Typ : constant Entity_Id := Etype (Id);
2189 if Ekind (Typ) = E_Incomplete_Type
2190 and then From_With_Type (Typ)
2191 and then Present (Non_Limited_View (Typ))
2193 Set_Etype (Id, Non_Limited_View (Typ));
2195 end Detect_And_Exchange;
2201 -- Start of processing for Exchange_Limited_Views
2204 if No (Subp_Id) then
2207 -- Do not process subprogram bodies as they already use the non-
2208 -- limited view of types.
2210 elsif not Ekind_In (Subp_Id, E_Function, E_Procedure) then
2214 -- Examine all formals and swap views when applicable
2216 Formal := First_Formal (Subp_Id);
2217 while Present (Formal) loop
2218 Detect_And_Exchange (Formal);
2220 Next_Formal (Formal);
2223 -- Process the return type of a function
2225 if Ekind (Subp_Id) = E_Function then
2226 Detect_And_Exchange (Subp_Id);
2228 end Exchange_Limited_Views;
2230 -------------------------------------
2231 -- Is_Private_Concurrent_Primitive --
2232 -------------------------------------
2234 function Is_Private_Concurrent_Primitive
2235 (Subp_Id : Entity_Id) return Boolean
2237 Formal_Typ : Entity_Id;
2240 if Present (First_Formal (Subp_Id)) then
2241 Formal_Typ := Etype (First_Formal (Subp_Id));
2243 if Is_Concurrent_Record_Type (Formal_Typ) then
2244 if Is_Class_Wide_Type (Formal_Typ) then
2245 Formal_Typ := Root_Type (Formal_Typ);
2248 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
2251 -- The type of the first formal is a concurrent tagged type with
2255 Is_Concurrent_Type (Formal_Typ)
2256 and then Is_Tagged_Type (Formal_Typ)
2257 and then Has_Private_Declaration (Formal_Typ);
2261 end Is_Private_Concurrent_Primitive;
2263 ----------------------------
2264 -- Set_Trivial_Subprogram --
2265 ----------------------------
2267 procedure Set_Trivial_Subprogram (N : Node_Id) is
2268 Nxt : constant Node_Id := Next (N);
2271 Set_Is_Trivial_Subprogram (Body_Id);
2273 if Present (Spec_Id) then
2274 Set_Is_Trivial_Subprogram (Spec_Id);
2278 and then Nkind (Nxt) = N_Simple_Return_Statement
2279 and then No (Next (Nxt))
2280 and then Present (Expression (Nxt))
2281 and then Is_Entity_Name (Expression (Nxt))
2283 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
2285 end Set_Trivial_Subprogram;
2287 ---------------------------------
2288 -- Verify_Overriding_Indicator --
2289 ---------------------------------
2291 procedure Verify_Overriding_Indicator is
2293 if Must_Override (Body_Spec) then
2294 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
2295 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2299 elsif not Present (Overridden_Operation (Spec_Id)) then
2301 ("subprogram& is not overriding", Body_Spec, Spec_Id);
2304 elsif Must_Not_Override (Body_Spec) then
2305 if Present (Overridden_Operation (Spec_Id)) then
2307 ("subprogram& overrides inherited operation",
2308 Body_Spec, Spec_Id);
2310 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
2311 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
2314 ("subprogram & overrides predefined operator ",
2315 Body_Spec, Spec_Id);
2317 -- If this is not a primitive operation or protected subprogram,
2318 -- then the overriding indicator is altogether illegal.
2320 elsif not Is_Primitive (Spec_Id)
2321 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
2324 ("overriding indicator only allowed " &
2325 "if subprogram is primitive",
2330 and then Present (Overridden_Operation (Spec_Id))
2332 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2333 Style.Missing_Overriding (N, Body_Id);
2336 and then Can_Override_Operator (Spec_Id)
2337 and then not Is_Predefined_File_Name
2338 (Unit_File_Name (Get_Source_Unit (Spec_Id)))
2340 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
2341 Style.Missing_Overriding (N, Body_Id);
2343 end Verify_Overriding_Indicator;
2345 -- Start of processing for Analyze_Subprogram_Body_Helper
2348 -- Generic subprograms are handled separately. They always have a
2349 -- generic specification. Determine whether current scope has a
2350 -- previous declaration.
2352 -- If the subprogram body is defined within an instance of the same
2353 -- name, the instance appears as a package renaming, and will be hidden
2354 -- within the subprogram.
2356 if Present (Prev_Id)
2357 and then not Is_Overloadable (Prev_Id)
2358 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
2359 or else Comes_From_Source (Prev_Id))
2361 if Is_Generic_Subprogram (Prev_Id) then
2363 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2364 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2366 Analyze_Generic_Subprogram_Body (N, Spec_Id);
2368 if Nkind (N) = N_Subprogram_Body then
2369 HSS := Handled_Statement_Sequence (N);
2370 Check_Missing_Return;
2376 -- Previous entity conflicts with subprogram name. Attempting to
2377 -- enter name will post error.
2379 Enter_Name (Body_Id);
2383 -- Non-generic case, find the subprogram declaration, if one was seen,
2384 -- or enter new overloaded entity in the current scope. If the
2385 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
2386 -- part of the context of one of its subunits. No need to redo the
2389 elsif Prev_Id = Body_Id
2390 and then Has_Completion (Body_Id)
2395 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2397 if Nkind (N) = N_Subprogram_Body_Stub
2398 or else No (Corresponding_Spec (N))
2400 if Is_Private_Concurrent_Primitive (Body_Id) then
2401 Spec_Id := Disambiguate_Spec;
2403 Spec_Id := Find_Corresponding_Spec (N);
2406 -- If this is a duplicate body, no point in analyzing it
2408 if Error_Posted (N) then
2412 -- A subprogram body should cause freezing of its own declaration,
2413 -- but if there was no previous explicit declaration, then the
2414 -- subprogram will get frozen too late (there may be code within
2415 -- the body that depends on the subprogram having been frozen,
2416 -- such as uses of extra formals), so we force it to be frozen
2417 -- here. Same holds if the body and spec are compilation units.
2418 -- Finally, if the return type is an anonymous access to protected
2419 -- subprogram, it must be frozen before the body because its
2420 -- expansion has generated an equivalent type that is used when
2421 -- elaborating the body.
2423 -- An exception in the case of Ada 2012, AI05-177: The bodies
2424 -- created for expression functions do not freeze.
2427 and then Nkind (Original_Node (N)) /= N_Expression_Function
2429 Freeze_Before (N, Body_Id);
2431 elsif Nkind (Parent (N)) = N_Compilation_Unit then
2432 Freeze_Before (N, Spec_Id);
2434 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
2435 Freeze_Before (N, Etype (Body_Id));
2439 Spec_Id := Corresponding_Spec (N);
2443 -- Do not inline any subprogram that contains nested subprograms, since
2444 -- the backend inlining circuit seems to generate uninitialized
2445 -- references in this case. We know this happens in the case of front
2446 -- end ZCX support, but it also appears it can happen in other cases as
2447 -- well. The backend often rejects attempts to inline in the case of
2448 -- nested procedures anyway, so little if anything is lost by this.
2449 -- Note that this is test is for the benefit of the back-end. There is
2450 -- a separate test for front-end inlining that also rejects nested
2453 -- Do not do this test if errors have been detected, because in some
2454 -- error cases, this code blows up, and we don't need it anyway if
2455 -- there have been errors, since we won't get to the linker anyway.
2457 if Comes_From_Source (Body_Id)
2458 and then Serious_Errors_Detected = 0
2462 P_Ent := Scope (P_Ent);
2463 exit when No (P_Ent) or else P_Ent = Standard_Standard;
2465 if Is_Subprogram (P_Ent) then
2466 Set_Is_Inlined (P_Ent, False);
2468 if Comes_From_Source (P_Ent)
2469 and then Has_Pragma_Inline (P_Ent)
2472 ("cannot inline& (nested subprogram)?",
2479 Check_Inline_Pragma (Spec_Id);
2481 -- Deal with special case of a fully private operation in the body of
2482 -- the protected type. We must create a declaration for the subprogram,
2483 -- in order to attach the protected subprogram that will be used in
2484 -- internal calls. We exclude compiler generated bodies from the
2485 -- expander since the issue does not arise for those cases.
2488 and then Comes_From_Source (N)
2489 and then Is_Protected_Type (Current_Scope)
2491 Spec_Id := Build_Private_Protected_Declaration (N);
2494 -- If a separate spec is present, then deal with freezing issues
2496 if Present (Spec_Id) then
2497 Spec_Decl := Unit_Declaration_Node (Spec_Id);
2498 Verify_Overriding_Indicator;
2500 -- In general, the spec will be frozen when we start analyzing the
2501 -- body. However, for internally generated operations, such as
2502 -- wrapper functions for inherited operations with controlling
2503 -- results, the spec may not have been frozen by the time we expand
2504 -- the freeze actions that include the bodies. In particular, extra
2505 -- formals for accessibility or for return-in-place may need to be
2506 -- generated. Freeze nodes, if any, are inserted before the current
2507 -- body. These freeze actions are also needed in ASIS mode to enable
2508 -- the proper back-annotations.
2510 if not Is_Frozen (Spec_Id)
2511 and then (Expander_Active or ASIS_Mode)
2513 -- Force the generation of its freezing node to ensure proper
2514 -- management of access types in the backend.
2516 -- This is definitely needed for some cases, but it is not clear
2517 -- why, to be investigated further???
2519 Set_Has_Delayed_Freeze (Spec_Id);
2520 Freeze_Before (N, Spec_Id);
2524 -- Mark presence of postcondition procedure in current scope and mark
2525 -- the procedure itself as needing debug info. The latter is important
2526 -- when analyzing decision coverage (for example, for MC/DC coverage).
2528 if Chars (Body_Id) = Name_uPostconditions then
2529 Set_Has_Postconditions (Current_Scope);
2530 Set_Debug_Info_Needed (Body_Id);
2533 -- Place subprogram on scope stack, and make formals visible. If there
2534 -- is a spec, the visible entity remains that of the spec.
2536 if Present (Spec_Id) then
2537 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
2539 if Is_Child_Unit (Spec_Id) then
2540 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
2544 Style.Check_Identifier (Body_Id, Spec_Id);
2547 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
2548 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
2550 if Is_Abstract_Subprogram (Spec_Id) then
2551 Error_Msg_N ("an abstract subprogram cannot have a body", N);
2555 Set_Convention (Body_Id, Convention (Spec_Id));
2556 Set_Has_Completion (Spec_Id);
2558 if Is_Protected_Type (Scope (Spec_Id)) then
2559 Prot_Typ := Scope (Spec_Id);
2562 -- If this is a body generated for a renaming, do not check for
2563 -- full conformance. The check is redundant, because the spec of
2564 -- the body is a copy of the spec in the renaming declaration,
2565 -- and the test can lead to spurious errors on nested defaults.
2567 if Present (Spec_Decl)
2568 and then not Comes_From_Source (N)
2570 (Nkind (Original_Node (Spec_Decl)) =
2571 N_Subprogram_Renaming_Declaration
2572 or else (Present (Corresponding_Body (Spec_Decl))
2574 Nkind (Unit_Declaration_Node
2575 (Corresponding_Body (Spec_Decl))) =
2576 N_Subprogram_Renaming_Declaration))
2580 -- Conversely, the spec may have been generated for specless body
2581 -- with an inline pragma.
2583 elsif Comes_From_Source (N)
2584 and then not Comes_From_Source (Spec_Id)
2585 and then Has_Pragma_Inline (Spec_Id)
2592 Fully_Conformant, True, Conformant, Body_Id);
2595 -- If the body is not fully conformant, we have to decide if we
2596 -- should analyze it or not. If it has a really messed up profile
2597 -- then we probably should not analyze it, since we will get too
2598 -- many bogus messages.
2600 -- Our decision is to go ahead in the non-fully conformant case
2601 -- only if it is at least mode conformant with the spec. Note
2602 -- that the call to Check_Fully_Conformant has issued the proper
2603 -- error messages to complain about the lack of conformance.
2606 and then not Mode_Conformant (Body_Id, Spec_Id)
2612 if Spec_Id /= Body_Id then
2613 Reference_Body_Formals (Spec_Id, Body_Id);
2616 if Nkind (N) /= N_Subprogram_Body_Stub then
2617 Set_Corresponding_Spec (N, Spec_Id);
2619 -- Ada 2005 (AI-345): If the operation is a primitive operation
2620 -- of a concurrent type, the type of the first parameter has been
2621 -- replaced with the corresponding record, which is the proper
2622 -- run-time structure to use. However, within the body there may
2623 -- be uses of the formals that depend on primitive operations
2624 -- of the type (in particular calls in prefixed form) for which
2625 -- we need the original concurrent type. The operation may have
2626 -- several controlling formals, so the replacement must be done
2629 if Comes_From_Source (Spec_Id)
2630 and then Present (First_Entity (Spec_Id))
2631 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
2632 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
2634 Present (Interfaces (Etype (First_Entity (Spec_Id))))
2637 (Corresponding_Concurrent_Type
2638 (Etype (First_Entity (Spec_Id))))
2641 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
2645 Form := First_Formal (Spec_Id);
2646 while Present (Form) loop
2647 if Etype (Form) = Typ then
2648 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
2656 -- Make the formals visible, and place subprogram on scope stack.
2657 -- This is also the point at which we set Last_Real_Spec_Entity
2658 -- to mark the entities which will not be moved to the body.
2660 Install_Formals (Spec_Id);
2661 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
2662 Push_Scope (Spec_Id);
2664 -- Make sure that the subprogram is immediately visible. For
2665 -- child units that have no separate spec this is indispensable.
2666 -- Otherwise it is safe albeit redundant.
2668 Set_Is_Immediately_Visible (Spec_Id);
2671 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
2672 Set_Ekind (Body_Id, E_Subprogram_Body);
2673 Set_Scope (Body_Id, Scope (Spec_Id));
2674 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
2676 -- Case of subprogram body with no previous spec
2679 -- Check for style warning required
2683 -- Only apply check for source level subprograms for which checks
2684 -- have not been suppressed.
2686 and then Comes_From_Source (Body_Id)
2687 and then not Suppress_Style_Checks (Body_Id)
2689 -- No warnings within an instance
2691 and then not In_Instance
2693 -- No warnings for expression functions
2695 and then Nkind (Original_Node (N)) /= N_Expression_Function
2697 Style.Body_With_No_Spec (N);
2700 New_Overloaded_Entity (Body_Id);
2702 if Nkind (N) /= N_Subprogram_Body_Stub then
2703 Set_Acts_As_Spec (N);
2704 Generate_Definition (Body_Id);
2705 Set_Contract (Body_Id, Make_Contract (Sloc (Body_Id)));
2707 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
2708 Install_Formals (Body_Id);
2709 Push_Scope (Body_Id);
2712 -- For stubs and bodies with no previous spec, generate references to
2715 Generate_Reference_To_Formals (Body_Id);
2718 -- If the return type is an anonymous access type whose designated type
2719 -- is the limited view of a class-wide type and the non-limited view is
2720 -- available, update the return type accordingly.
2722 if Ada_Version >= Ada_2005
2723 and then Comes_From_Source (N)
2730 Rtyp := Etype (Current_Scope);
2732 if Ekind (Rtyp) = E_Anonymous_Access_Type then
2733 Etyp := Directly_Designated_Type (Rtyp);
2735 if Is_Class_Wide_Type (Etyp)
2736 and then From_With_Type (Etyp)
2738 Set_Directly_Designated_Type
2739 (Etype (Current_Scope), Available_View (Etyp));
2745 -- If this is the proper body of a stub, we must verify that the stub
2746 -- conforms to the body, and to the previous spec if one was present.
2747 -- We know already that the body conforms to that spec. This test is
2748 -- only required for subprograms that come from source.
2750 if Nkind (Parent (N)) = N_Subunit
2751 and then Comes_From_Source (N)
2752 and then not Error_Posted (Body_Id)
2753 and then Nkind (Corresponding_Stub (Parent (N))) =
2754 N_Subprogram_Body_Stub
2757 Old_Id : constant Entity_Id :=
2759 (Specification (Corresponding_Stub (Parent (N))));
2761 Conformant : Boolean := False;
2764 if No (Spec_Id) then
2765 Check_Fully_Conformant (Body_Id, Old_Id);
2769 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
2771 if not Conformant then
2773 -- The stub was taken to be a new declaration. Indicate that
2776 Set_Has_Completion (Old_Id, False);
2782 Set_Has_Completion (Body_Id);
2783 Check_Eliminated (Body_Id);
2785 if Nkind (N) = N_Subprogram_Body_Stub then
2788 elsif Present (Spec_Id)
2789 and then Expander_Active
2791 (Has_Pragma_Inline_Always (Spec_Id)
2792 or else (Has_Pragma_Inline (Spec_Id) and Front_End_Inlining))
2794 Build_Body_To_Inline (N, Spec_Id);
2797 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
2798 -- of the specification we have to install the private withed units.
2799 -- This holds for child units as well.
2801 if Is_Compilation_Unit (Body_Id)
2802 or else Nkind (Parent (N)) = N_Compilation_Unit
2804 Install_Private_With_Clauses (Body_Id);
2807 Check_Anonymous_Return;
2809 -- Set the Protected_Formal field of each extra formal of the protected
2810 -- subprogram to reference the corresponding extra formal of the
2811 -- subprogram that implements it. For regular formals this occurs when
2812 -- the protected subprogram's declaration is expanded, but the extra
2813 -- formals don't get created until the subprogram is frozen. We need to
2814 -- do this before analyzing the protected subprogram's body so that any
2815 -- references to the original subprogram's extra formals will be changed
2816 -- refer to the implementing subprogram's formals (see Expand_Formal).
2818 if Present (Spec_Id)
2819 and then Is_Protected_Type (Scope (Spec_Id))
2820 and then Present (Protected_Body_Subprogram (Spec_Id))
2823 Impl_Subp : constant Entity_Id :=
2824 Protected_Body_Subprogram (Spec_Id);
2825 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
2826 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
2828 while Present (Prot_Ext_Formal) loop
2829 pragma Assert (Present (Impl_Ext_Formal));
2830 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
2831 Next_Formal_With_Extras (Prot_Ext_Formal);
2832 Next_Formal_With_Extras (Impl_Ext_Formal);
2837 -- Now we can go on to analyze the body
2839 HSS := Handled_Statement_Sequence (N);
2840 Set_Actual_Subtypes (N, Current_Scope);
2842 -- Deal with preconditions and postconditions. In formal verification
2843 -- mode, we keep pre- and postconditions attached to entities rather
2844 -- than inserted in the code, in order to facilitate a distinct
2845 -- treatment for them.
2847 if not Alfa_Mode then
2848 Process_PPCs (N, Spec_Id, Body_Id);
2851 -- Add a declaration for the Protection object, renaming declarations
2852 -- for discriminals and privals and finally a declaration for the entry
2853 -- family index (if applicable). This form of early expansion is done
2854 -- when the Expander is active because Install_Private_Data_Declarations
2855 -- references entities which were created during regular expansion. The
2856 -- body may be the rewritting of an expression function, and we need to
2857 -- verify that the original node is in the source.
2859 if Full_Expander_Active
2860 and then Comes_From_Source (Original_Node (N))
2861 and then Present (Prot_Typ)
2862 and then Present (Spec_Id)
2863 and then not Is_Eliminated (Spec_Id)
2865 Install_Private_Data_Declarations
2866 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
2869 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
2870 -- may now appear in parameter and result profiles. Since the analysis
2871 -- of a subprogram body may use the parameter and result profile of the
2872 -- spec, swap any limited views with their non-limited counterpart.
2874 if Ada_Version >= Ada_2012 then
2875 Exchange_Limited_Views (Spec_Id);
2878 -- Analyze the declarations (this call will analyze the precondition
2879 -- Check pragmas we prepended to the list, as well as the declaration
2880 -- of the _Postconditions procedure).
2882 Analyze_Declarations (Declarations (N));
2884 -- Check completion, and analyze the statements
2887 Inspect_Deferred_Constant_Completion (Declarations (N));
2890 -- Deal with end of scope processing for the body
2892 Process_End_Label (HSS, 't', Current_Scope);
2894 Check_Subprogram_Order (N);
2895 Set_Analyzed (Body_Id);
2897 -- If we have a separate spec, then the analysis of the declarations
2898 -- caused the entities in the body to be chained to the spec id, but
2899 -- we want them chained to the body id. Only the formal parameters
2900 -- end up chained to the spec id in this case.
2902 if Present (Spec_Id) then
2904 -- We must conform to the categorization of our spec
2906 Validate_Categorization_Dependency (N, Spec_Id);
2908 -- And if this is a child unit, the parent units must conform
2910 if Is_Child_Unit (Spec_Id) then
2911 Validate_Categorization_Dependency
2912 (Unit_Declaration_Node (Spec_Id), Spec_Id);
2915 -- Here is where we move entities from the spec to the body
2917 -- Case where there are entities that stay with the spec
2919 if Present (Last_Real_Spec_Entity) then
2921 -- No body entities (happens when the only real spec entities come
2922 -- from precondition and postcondition pragmas).
2924 if No (Last_Entity (Body_Id)) then
2926 (Body_Id, Next_Entity (Last_Real_Spec_Entity));
2928 -- Body entities present (formals), so chain stuff past them
2932 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
2935 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
2936 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2937 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
2939 -- Case where there are no spec entities, in this case there can be
2940 -- no body entities either, so just move everything.
2943 pragma Assert (No (Last_Entity (Body_Id)));
2944 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
2945 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
2946 Set_First_Entity (Spec_Id, Empty);
2947 Set_Last_Entity (Spec_Id, Empty);
2951 Check_Missing_Return;
2953 -- Now we are going to check for variables that are never modified in
2954 -- the body of the procedure. But first we deal with a special case
2955 -- where we want to modify this check. If the body of the subprogram
2956 -- starts with a raise statement or its equivalent, or if the body
2957 -- consists entirely of a null statement, then it is pretty obvious
2958 -- that it is OK to not reference the parameters. For example, this
2959 -- might be the following common idiom for a stubbed function:
2960 -- statement of the procedure raises an exception. In particular this
2961 -- deals with the common idiom of a stubbed function, which might
2962 -- appear as something like:
2964 -- function F (A : Integer) return Some_Type;
2967 -- raise Program_Error;
2971 -- Here the purpose of X is simply to satisfy the annoying requirement
2972 -- in Ada that there be at least one return, and we certainly do not
2973 -- want to go posting warnings on X that it is not initialized! On
2974 -- the other hand, if X is entirely unreferenced that should still
2977 -- What we do is to detect these cases, and if we find them, flag the
2978 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
2979 -- suppress unwanted warnings. For the case of the function stub above
2980 -- we have a special test to set X as apparently assigned to suppress
2987 -- Skip initial labels (for one thing this occurs when we are in
2988 -- front end ZCX mode, but in any case it is irrelevant), and also
2989 -- initial Push_xxx_Error_Label nodes, which are also irrelevant.
2991 Stm := First (Statements (HSS));
2992 while Nkind (Stm) = N_Label
2993 or else Nkind (Stm) in N_Push_xxx_Label
2998 -- Do the test on the original statement before expansion
3001 Ostm : constant Node_Id := Original_Node (Stm);
3004 -- If explicit raise statement, turn on flag
3006 if Nkind (Ostm) = N_Raise_Statement then
3007 Set_Trivial_Subprogram (Stm);
3009 -- If null statement, and no following statements, turn on flag
3011 elsif Nkind (Stm) = N_Null_Statement
3012 and then Comes_From_Source (Stm)
3013 and then No (Next (Stm))
3015 Set_Trivial_Subprogram (Stm);
3017 -- Check for explicit call cases which likely raise an exception
3019 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
3020 if Is_Entity_Name (Name (Ostm)) then
3022 Ent : constant Entity_Id := Entity (Name (Ostm));
3025 -- If the procedure is marked No_Return, then likely it
3026 -- raises an exception, but in any case it is not coming
3027 -- back here, so turn on the flag.
3030 and then Ekind (Ent) = E_Procedure
3031 and then No_Return (Ent)
3033 Set_Trivial_Subprogram (Stm);
3041 -- Check for variables that are never modified
3047 -- If there is a separate spec, then transfer Never_Set_In_Source
3048 -- flags from out parameters to the corresponding entities in the
3049 -- body. The reason we do that is we want to post error flags on
3050 -- the body entities, not the spec entities.
3052 if Present (Spec_Id) then
3053 E1 := First_Entity (Spec_Id);
3054 while Present (E1) loop
3055 if Ekind (E1) = E_Out_Parameter then
3056 E2 := First_Entity (Body_Id);
3057 while Present (E2) loop
3058 exit when Chars (E1) = Chars (E2);
3062 if Present (E2) then
3063 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
3071 -- Check references in body unless it was deleted. Note that the
3072 -- check of Body_Deleted here is not just for efficiency, it is
3073 -- necessary to avoid junk warnings on formal parameters.
3075 if not Body_Deleted then
3076 Check_References (Body_Id);
3079 end Analyze_Subprogram_Body_Helper;
3081 ------------------------------------
3082 -- Analyze_Subprogram_Declaration --
3083 ------------------------------------
3085 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
3086 Loc : constant Source_Ptr := Sloc (N);
3087 Scop : constant Entity_Id := Current_Scope;
3088 Designator : Entity_Id;
3090 Null_Body : Node_Id := Empty;
3092 -- Start of processing for Analyze_Subprogram_Declaration
3095 -- Null procedures are not allowed in SPARK
3097 if Nkind (Specification (N)) = N_Procedure_Specification
3098 and then Null_Present (Specification (N))
3100 Check_SPARK_Restriction ("null procedure is not allowed", N);
3103 -- For a null procedure, capture the profile before analysis, for
3104 -- expansion at the freeze point and at each point of call. The body
3105 -- will only be used if the procedure has preconditions. In that case
3106 -- the body is analyzed at the freeze point.
3108 if Nkind (Specification (N)) = N_Procedure_Specification
3109 and then Null_Present (Specification (N))
3110 and then Expander_Active
3113 Make_Subprogram_Body (Loc,
3115 New_Copy_Tree (Specification (N)),
3118 Handled_Statement_Sequence =>
3119 Make_Handled_Sequence_Of_Statements (Loc,
3120 Statements => New_List (Make_Null_Statement (Loc))));
3122 -- Create new entities for body and formals
3124 Set_Defining_Unit_Name (Specification (Null_Body),
3125 Make_Defining_Identifier (Loc, Chars (Defining_Entity (N))));
3126 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
3128 Form := First (Parameter_Specifications (Specification (Null_Body)));
3129 while Present (Form) loop
3130 Set_Defining_Identifier (Form,
3131 Make_Defining_Identifier (Loc,
3132 Chars (Defining_Identifier (Form))));
3134 -- Resolve the types of the formals now, because the freeze point
3135 -- may appear in a different context, e.g. an instantiation.
3137 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
3138 Find_Type (Parameter_Type (Form));
3141 No (Access_To_Subprogram_Definition (Parameter_Type (Form)))
3143 Find_Type (Subtype_Mark (Parameter_Type (Form)));
3147 -- the case of a null procedure with a formal that is an
3148 -- access_to_subprogram type, and that is used as an actual
3149 -- in an instantiation is left to the enthusiastic reader.
3157 if Is_Protected_Type (Current_Scope) then
3158 Error_Msg_N ("protected operation cannot be a null procedure", N);
3162 Designator := Analyze_Subprogram_Specification (Specification (N));
3163 Generate_Definition (Designator);
3164 -- ??? why this call, already in Analyze_Subprogram_Specification
3166 if Debug_Flag_C then
3167 Write_Str ("==> subprogram spec ");
3168 Write_Name (Chars (Designator));
3169 Write_Str (" from ");
3170 Write_Location (Sloc (N));
3175 if Nkind (Specification (N)) = N_Procedure_Specification
3176 and then Null_Present (Specification (N))
3178 Set_Has_Completion (Designator);
3180 if Present (Null_Body) then
3181 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
3182 Set_Body_To_Inline (N, Null_Body);
3183 Set_Is_Inlined (Designator);
3187 Validate_RCI_Subprogram_Declaration (N);
3188 New_Overloaded_Entity (Designator);
3189 Check_Delayed_Subprogram (Designator);
3191 -- If the type of the first formal of the current subprogram is a
3192 -- nongeneric tagged private type, mark the subprogram as being a
3193 -- private primitive. Ditto if this is a function with controlling
3194 -- result, and the return type is currently private. In both cases,
3195 -- the type of the controlling argument or result must be in the
3196 -- current scope for the operation to be primitive.
3198 if Has_Controlling_Result (Designator)
3199 and then Is_Private_Type (Etype (Designator))
3200 and then Scope (Etype (Designator)) = Current_Scope
3201 and then not Is_Generic_Actual_Type (Etype (Designator))
3203 Set_Is_Private_Primitive (Designator);
3205 elsif Present (First_Formal (Designator)) then
3207 Formal_Typ : constant Entity_Id :=
3208 Etype (First_Formal (Designator));
3210 Set_Is_Private_Primitive (Designator,
3211 Is_Tagged_Type (Formal_Typ)
3212 and then Scope (Formal_Typ) = Current_Scope
3213 and then Is_Private_Type (Formal_Typ)
3214 and then not Is_Generic_Actual_Type (Formal_Typ));
3218 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
3221 if Ada_Version >= Ada_2005
3222 and then Comes_From_Source (N)
3223 and then Is_Dispatching_Operation (Designator)
3230 if Has_Controlling_Result (Designator) then
3231 Etyp := Etype (Designator);
3234 E := First_Entity (Designator);
3236 and then Is_Formal (E)
3237 and then not Is_Controlling_Formal (E)
3245 if Is_Access_Type (Etyp) then
3246 Etyp := Directly_Designated_Type (Etyp);
3249 if Is_Interface (Etyp)
3250 and then not Is_Abstract_Subprogram (Designator)
3251 and then not (Ekind (Designator) = E_Procedure
3252 and then Null_Present (Specification (N)))
3254 Error_Msg_Name_1 := Chars (Defining_Entity (N));
3256 ("(Ada 2005) interface subprogram % must be abstract or null",
3262 -- What is the following code for, it used to be
3264 -- ??? Set_Suppress_Elaboration_Checks
3265 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
3267 -- The following seems equivalent, but a bit dubious
3269 if Elaboration_Checks_Suppressed (Designator) then
3270 Set_Kill_Elaboration_Checks (Designator);
3273 if Scop /= Standard_Standard
3274 and then not Is_Child_Unit (Designator)
3276 Set_Categorization_From_Scope (Designator, Scop);
3278 -- For a compilation unit, check for library-unit pragmas
3280 Push_Scope (Designator);
3281 Set_Categorization_From_Pragmas (N);
3282 Validate_Categorization_Dependency (N, Designator);
3286 -- For a compilation unit, set body required. This flag will only be
3287 -- reset if a valid Import or Interface pragma is processed later on.
3289 if Nkind (Parent (N)) = N_Compilation_Unit then
3290 Set_Body_Required (Parent (N), True);
3292 if Ada_Version >= Ada_2005
3293 and then Nkind (Specification (N)) = N_Procedure_Specification
3294 and then Null_Present (Specification (N))
3297 ("null procedure cannot be declared at library level", N);
3301 Generate_Reference_To_Formals (Designator);
3302 Check_Eliminated (Designator);
3304 if Debug_Flag_C then
3306 Write_Str ("<== subprogram spec ");
3307 Write_Name (Chars (Designator));
3308 Write_Str (" from ");
3309 Write_Location (Sloc (N));
3313 if Is_Protected_Type (Current_Scope) then
3315 -- Indicate that this is a protected operation, because it may be
3316 -- used in subsequent declarations within the protected type.
3318 Set_Convention (Designator, Convention_Protected);
3321 List_Inherited_Pre_Post_Aspects (Designator);
3323 if Has_Aspects (N) then
3324 Analyze_Aspect_Specifications (N, Designator);
3326 end Analyze_Subprogram_Declaration;
3328 --------------------------------------
3329 -- Analyze_Subprogram_Specification --
3330 --------------------------------------
3332 -- Reminder: N here really is a subprogram specification (not a subprogram
3333 -- declaration). This procedure is called to analyze the specification in
3334 -- both subprogram bodies and subprogram declarations (specs).
3336 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
3337 Designator : constant Entity_Id := Defining_Entity (N);
3338 Formals : constant List_Id := Parameter_Specifications (N);
3340 -- Start of processing for Analyze_Subprogram_Specification
3343 -- User-defined operator is not allowed in SPARK, except as a renaming
3345 if Nkind (Defining_Unit_Name (N)) = N_Defining_Operator_Symbol
3346 and then Nkind (Parent (N)) /= N_Subprogram_Renaming_Declaration
3348 Check_SPARK_Restriction ("user-defined operator is not allowed", N);
3351 -- Proceed with analysis
3353 Generate_Definition (Designator);
3354 Set_Contract (Designator, Make_Contract (Sloc (Designator)));
3356 if Nkind (N) = N_Function_Specification then
3357 Set_Ekind (Designator, E_Function);
3358 Set_Mechanism (Designator, Default_Mechanism);
3360 Set_Ekind (Designator, E_Procedure);
3361 Set_Etype (Designator, Standard_Void_Type);
3364 -- Introduce new scope for analysis of the formals and the return type
3366 Set_Scope (Designator, Current_Scope);
3368 if Present (Formals) then
3369 Push_Scope (Designator);
3370 Process_Formals (Formals, N);
3372 -- Ada 2005 (AI-345): If this is an overriding operation of an
3373 -- inherited interface operation, and the controlling type is
3374 -- a synchronized type, replace the type with its corresponding
3375 -- record, to match the proper signature of an overriding operation.
3376 -- Same processing for an access parameter whose designated type is
3377 -- derived from a synchronized interface.
3379 if Ada_Version >= Ada_2005 then
3382 Formal_Typ : Entity_Id;
3383 Rec_Typ : Entity_Id;
3384 Desig_Typ : Entity_Id;
3387 Formal := First_Formal (Designator);
3388 while Present (Formal) loop
3389 Formal_Typ := Etype (Formal);
3391 if Is_Concurrent_Type (Formal_Typ)
3392 and then Present (Corresponding_Record_Type (Formal_Typ))
3394 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
3396 if Present (Interfaces (Rec_Typ)) then
3397 Set_Etype (Formal, Rec_Typ);
3400 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
3401 Desig_Typ := Designated_Type (Formal_Typ);
3403 if Is_Concurrent_Type (Desig_Typ)
3404 and then Present (Corresponding_Record_Type (Desig_Typ))
3406 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
3408 if Present (Interfaces (Rec_Typ)) then
3409 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
3414 Next_Formal (Formal);
3421 -- The subprogram scope is pushed and popped around the processing of
3422 -- the return type for consistency with call above to Process_Formals
3423 -- (which itself can call Analyze_Return_Type), and to ensure that any
3424 -- itype created for the return type will be associated with the proper
3427 elsif Nkind (N) = N_Function_Specification then
3428 Push_Scope (Designator);
3429 Analyze_Return_Type (N);
3435 if Nkind (N) = N_Function_Specification then
3437 -- Deal with operator symbol case
3439 if Nkind (Designator) = N_Defining_Operator_Symbol then
3440 Valid_Operator_Definition (Designator);
3443 May_Need_Actuals (Designator);
3445 -- Ada 2005 (AI-251): If the return type is abstract, verify that
3446 -- the subprogram is abstract also. This does not apply to renaming
3447 -- declarations, where abstractness is inherited, and to subprogram
3448 -- bodies generated for stream operations, which become renamings as
3451 -- In case of primitives associated with abstract interface types
3452 -- the check is applied later (see Analyze_Subprogram_Declaration).
3454 if not Nkind_In (Original_Node (Parent (N)),
3455 N_Subprogram_Renaming_Declaration,
3456 N_Abstract_Subprogram_Declaration,
3457 N_Formal_Abstract_Subprogram_Declaration)
3459 if Is_Abstract_Type (Etype (Designator))
3460 and then not Is_Interface (Etype (Designator))
3463 ("function that returns abstract type must be abstract", N);
3465 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
3466 -- access result whose designated type is abstract.
3468 elsif Nkind (Result_Definition (N)) = N_Access_Definition
3470 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
3471 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
3472 and then Ada_Version >= Ada_2012
3474 Error_Msg_N ("function whose access result designates "
3475 & "abstract type must be abstract", N);
3481 end Analyze_Subprogram_Specification;
3483 --------------------------
3484 -- Build_Body_To_Inline --
3485 --------------------------
3487 procedure Build_Body_To_Inline (N : Node_Id; Subp : Entity_Id) is
3488 Decl : constant Node_Id := Unit_Declaration_Node (Subp);
3489 Original_Body : Node_Id;
3490 Body_To_Analyze : Node_Id;
3491 Max_Size : constant := 10;
3492 Stat_Count : Integer := 0;
3494 function Has_Excluded_Declaration (Decls : List_Id) return Boolean;
3495 -- Check for declarations that make inlining not worthwhile
3497 function Has_Excluded_Statement (Stats : List_Id) return Boolean;
3498 -- Check for statements that make inlining not worthwhile: any tasking
3499 -- statement, nested at any level. Keep track of total number of
3500 -- elementary statements, as a measure of acceptable size.
3502 function Has_Pending_Instantiation return Boolean;
3503 -- If some enclosing body contains instantiations that appear before the
3504 -- corresponding generic body, the enclosing body has a freeze node so
3505 -- that it can be elaborated after the generic itself. This might
3506 -- conflict with subsequent inlinings, so that it is unsafe to try to
3507 -- inline in such a case.
3509 function Has_Single_Return return Boolean;
3510 -- In general we cannot inline functions that return unconstrained type.
3511 -- However, we can handle such functions if all return statements return
3512 -- a local variable that is the only declaration in the body of the
3513 -- function. In that case the call can be replaced by that local
3514 -- variable as is done for other inlined calls.
3516 procedure Remove_Pragmas;
3517 -- A pragma Unreferenced or pragma Unmodified that mentions a formal
3518 -- parameter has no meaning when the body is inlined and the formals
3519 -- are rewritten. Remove it from body to inline. The analysis of the
3520 -- non-inlined body will handle the pragma properly.
3522 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean;
3523 -- If the body of the subprogram includes a call that returns an
3524 -- unconstrained type, the secondary stack is involved, and it
3525 -- is not worth inlining.
3527 ------------------------------
3528 -- Has_Excluded_Declaration --
3529 ------------------------------
3531 function Has_Excluded_Declaration (Decls : List_Id) return Boolean is
3534 function Is_Unchecked_Conversion (D : Node_Id) return Boolean;
3535 -- Nested subprograms make a given body ineligible for inlining, but
3536 -- we make an exception for instantiations of unchecked conversion.
3537 -- The body has not been analyzed yet, so check the name, and verify
3538 -- that the visible entity with that name is the predefined unit.
3540 -----------------------------
3541 -- Is_Unchecked_Conversion --
3542 -----------------------------
3544 function Is_Unchecked_Conversion (D : Node_Id) return Boolean is
3545 Id : constant Node_Id := Name (D);
3549 if Nkind (Id) = N_Identifier
3550 and then Chars (Id) = Name_Unchecked_Conversion
3552 Conv := Current_Entity (Id);
3554 elsif Nkind_In (Id, N_Selected_Component, N_Expanded_Name)
3555 and then Chars (Selector_Name (Id)) = Name_Unchecked_Conversion
3557 Conv := Current_Entity (Selector_Name (Id));
3562 return Present (Conv)
3563 and then Is_Predefined_File_Name
3564 (Unit_File_Name (Get_Source_Unit (Conv)))
3565 and then Is_Intrinsic_Subprogram (Conv);
3566 end Is_Unchecked_Conversion;
3568 -- Start of processing for Has_Excluded_Declaration
3572 while Present (D) loop
3573 if (Nkind (D) = N_Function_Instantiation
3574 and then not Is_Unchecked_Conversion (D))
3575 or else Nkind_In (D, N_Protected_Type_Declaration,
3576 N_Package_Declaration,
3577 N_Package_Instantiation,
3579 N_Procedure_Instantiation,
3580 N_Task_Type_Declaration)
3583 ("cannot inline & (non-allowed declaration)?", D, Subp);
3591 end Has_Excluded_Declaration;
3593 ----------------------------
3594 -- Has_Excluded_Statement --
3595 ----------------------------
3597 function Has_Excluded_Statement (Stats : List_Id) return Boolean is
3603 while Present (S) loop
3604 Stat_Count := Stat_Count + 1;
3606 if Nkind_In (S, N_Abort_Statement,
3607 N_Asynchronous_Select,
3608 N_Conditional_Entry_Call,
3609 N_Delay_Relative_Statement,
3610 N_Delay_Until_Statement,
3615 ("cannot inline & (non-allowed statement)?", S, Subp);
3618 elsif Nkind (S) = N_Block_Statement then
3619 if Present (Declarations (S))
3620 and then Has_Excluded_Declaration (Declarations (S))
3624 elsif Present (Handled_Statement_Sequence (S))
3627 (Exception_Handlers (Handled_Statement_Sequence (S)))
3629 Has_Excluded_Statement
3630 (Statements (Handled_Statement_Sequence (S))))
3635 elsif Nkind (S) = N_Case_Statement then
3636 E := First (Alternatives (S));
3637 while Present (E) loop
3638 if Has_Excluded_Statement (Statements (E)) then
3645 elsif Nkind (S) = N_If_Statement then
3646 if Has_Excluded_Statement (Then_Statements (S)) then
3650 if Present (Elsif_Parts (S)) then
3651 E := First (Elsif_Parts (S));
3652 while Present (E) loop
3653 if Has_Excluded_Statement (Then_Statements (E)) then
3660 if Present (Else_Statements (S))
3661 and then Has_Excluded_Statement (Else_Statements (S))
3666 elsif Nkind (S) = N_Loop_Statement
3667 and then Has_Excluded_Statement (Statements (S))
3671 elsif Nkind (S) = N_Extended_Return_Statement then
3672 if Has_Excluded_Statement
3673 (Statements (Handled_Statement_Sequence (S)))
3675 (Exception_Handlers (Handled_Statement_Sequence (S)))
3685 end Has_Excluded_Statement;
3687 -------------------------------
3688 -- Has_Pending_Instantiation --
3689 -------------------------------
3691 function Has_Pending_Instantiation return Boolean is
3696 while Present (S) loop
3697 if Is_Compilation_Unit (S)
3698 or else Is_Child_Unit (S)
3702 elsif Ekind (S) = E_Package
3703 and then Has_Forward_Instantiation (S)
3712 end Has_Pending_Instantiation;
3714 ------------------------
3715 -- Has_Single_Return --
3716 ------------------------
3718 function Has_Single_Return return Boolean is
3719 Return_Statement : Node_Id := Empty;
3721 function Check_Return (N : Node_Id) return Traverse_Result;
3727 function Check_Return (N : Node_Id) return Traverse_Result is
3729 if Nkind (N) = N_Simple_Return_Statement then
3730 if Present (Expression (N))
3731 and then Is_Entity_Name (Expression (N))
3733 if No (Return_Statement) then
3734 Return_Statement := N;
3737 elsif Chars (Expression (N)) =
3738 Chars (Expression (Return_Statement))
3746 -- A return statement within an extended return is a noop
3749 elsif No (Expression (N))
3750 and then Nkind (Parent (Parent (N))) =
3751 N_Extended_Return_Statement
3756 -- Expression has wrong form
3761 -- We can only inline a build-in-place function if
3762 -- it has a single extended return.
3764 elsif Nkind (N) = N_Extended_Return_Statement then
3765 if No (Return_Statement) then
3766 Return_Statement := N;
3778 function Check_All_Returns is new Traverse_Func (Check_Return);
3780 -- Start of processing for Has_Single_Return
3783 if Check_All_Returns (N) /= OK then
3786 elsif Nkind (Return_Statement) = N_Extended_Return_Statement then
3790 return Present (Declarations (N))
3791 and then Present (First (Declarations (N)))
3792 and then Chars (Expression (Return_Statement)) =
3793 Chars (Defining_Identifier (First (Declarations (N))));
3795 end Has_Single_Return;
3797 --------------------
3798 -- Remove_Pragmas --
3799 --------------------
3801 procedure Remove_Pragmas is
3806 Decl := First (Declarations (Body_To_Analyze));
3807 while Present (Decl) loop
3810 if Nkind (Decl) = N_Pragma
3811 and then (Pragma_Name (Decl) = Name_Unreferenced
3813 Pragma_Name (Decl) = Name_Unmodified)
3822 --------------------------
3823 -- Uses_Secondary_Stack --
3824 --------------------------
3826 function Uses_Secondary_Stack (Bod : Node_Id) return Boolean is
3827 function Check_Call (N : Node_Id) return Traverse_Result;
3828 -- Look for function calls that return an unconstrained type
3834 function Check_Call (N : Node_Id) return Traverse_Result is
3836 if Nkind (N) = N_Function_Call
3837 and then Is_Entity_Name (Name (N))
3838 and then Is_Composite_Type (Etype (Entity (Name (N))))
3839 and then not Is_Constrained (Etype (Entity (Name (N))))
3842 ("cannot inline & (call returns unconstrained type)?",
3850 function Check_Calls is new Traverse_Func (Check_Call);
3853 return Check_Calls (Bod) = Abandon;
3854 end Uses_Secondary_Stack;
3856 -- Start of processing for Build_Body_To_Inline
3859 -- Return immediately if done already
3861 if Nkind (Decl) = N_Subprogram_Declaration
3862 and then Present (Body_To_Inline (Decl))
3866 -- Functions that return unconstrained composite types require
3867 -- secondary stack handling, and cannot currently be inlined, unless
3868 -- all return statements return a local variable that is the first
3869 -- local declaration in the body.
3871 elsif Ekind (Subp) = E_Function
3872 and then not Is_Scalar_Type (Etype (Subp))
3873 and then not Is_Access_Type (Etype (Subp))
3874 and then not Is_Constrained (Etype (Subp))
3876 if not Has_Single_Return then
3878 ("cannot inline & (unconstrained return type)?", N, Subp);
3882 -- Ditto for functions that return controlled types, where controlled
3883 -- actions interfere in complex ways with inlining.
3885 elsif Ekind (Subp) = E_Function
3886 and then Needs_Finalization (Etype (Subp))
3889 ("cannot inline & (controlled return type)?", N, Subp);
3893 if Present (Declarations (N))
3894 and then Has_Excluded_Declaration (Declarations (N))
3899 if Present (Handled_Statement_Sequence (N)) then
3900 if Present (Exception_Handlers (Handled_Statement_Sequence (N))) then
3902 ("cannot inline& (exception handler)?",
3903 First (Exception_Handlers (Handled_Statement_Sequence (N))),
3907 Has_Excluded_Statement
3908 (Statements (Handled_Statement_Sequence (N)))
3914 -- We do not inline a subprogram that is too large, unless it is
3915 -- marked Inline_Always. This pragma does not suppress the other
3916 -- checks on inlining (forbidden declarations, handlers, etc).
3918 if Stat_Count > Max_Size
3919 and then not Has_Pragma_Inline_Always (Subp)
3921 Cannot_Inline ("cannot inline& (body too large)?", N, Subp);
3925 if Has_Pending_Instantiation then
3927 ("cannot inline& (forward instance within enclosing body)?",
3932 -- Within an instance, the body to inline must be treated as a nested
3933 -- generic, so that the proper global references are preserved.
3935 -- Note that we do not do this at the library level, because it is not
3936 -- needed, and furthermore this causes trouble if front end inlining
3937 -- is activated (-gnatN).
3939 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3940 Save_Env (Scope (Current_Scope), Scope (Current_Scope));
3941 Original_Body := Copy_Generic_Node (N, Empty, True);
3943 Original_Body := Copy_Separate_Tree (N);
3946 -- We need to capture references to the formals in order to substitute
3947 -- the actuals at the point of inlining, i.e. instantiation. To treat
3948 -- the formals as globals to the body to inline, we nest it within
3949 -- a dummy parameterless subprogram, declared within the real one.
3950 -- To avoid generating an internal name (which is never public, and
3951 -- which affects serial numbers of other generated names), we use
3952 -- an internal symbol that cannot conflict with user declarations.
3954 Set_Parameter_Specifications (Specification (Original_Body), No_List);
3955 Set_Defining_Unit_Name
3956 (Specification (Original_Body),
3957 Make_Defining_Identifier (Sloc (N), Name_uParent));
3958 Set_Corresponding_Spec (Original_Body, Empty);
3960 Body_To_Analyze := Copy_Generic_Node (Original_Body, Empty, False);
3962 -- Set return type of function, which is also global and does not need
3965 if Ekind (Subp) = E_Function then
3966 Set_Result_Definition (Specification (Body_To_Analyze),
3967 New_Occurrence_Of (Etype (Subp), Sloc (N)));
3970 if No (Declarations (N)) then
3971 Set_Declarations (N, New_List (Body_To_Analyze));
3973 Append (Body_To_Analyze, Declarations (N));
3976 Expander_Mode_Save_And_Set (False);
3979 Analyze (Body_To_Analyze);
3980 Push_Scope (Defining_Entity (Body_To_Analyze));
3981 Save_Global_References (Original_Body);
3983 Remove (Body_To_Analyze);
3985 Expander_Mode_Restore;
3987 -- Restore environment if previously saved
3989 if In_Instance and then Scope (Current_Scope) /= Standard_Standard then
3993 -- If secondary stk used there is no point in inlining. We have
3994 -- already issued the warning in this case, so nothing to do.
3996 if Uses_Secondary_Stack (Body_To_Analyze) then
4000 Set_Body_To_Inline (Decl, Original_Body);
4001 Set_Ekind (Defining_Entity (Original_Body), Ekind (Subp));
4002 Set_Is_Inlined (Subp);
4003 end Build_Body_To_Inline;
4009 procedure Cannot_Inline (Msg : String; N : Node_Id; Subp : Entity_Id) is
4011 -- Do not emit warning if this is a predefined unit which is not the
4012 -- main unit. With validity checks enabled, some predefined subprograms
4013 -- may contain nested subprograms and become ineligible for inlining.
4015 if Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (Subp)))
4016 and then not In_Extended_Main_Source_Unit (Subp)
4020 elsif Has_Pragma_Inline_Always (Subp) then
4022 -- Remove last character (question mark) to make this into an error,
4023 -- because the Inline_Always pragma cannot be obeyed.
4025 Error_Msg_NE (Msg (Msg'First .. Msg'Last - 1), N, Subp);
4027 elsif Ineffective_Inline_Warnings then
4028 Error_Msg_NE (Msg, N, Subp);
4032 -----------------------
4033 -- Check_Conformance --
4034 -----------------------
4036 procedure Check_Conformance
4037 (New_Id : Entity_Id;
4039 Ctype : Conformance_Type;
4041 Conforms : out Boolean;
4042 Err_Loc : Node_Id := Empty;
4043 Get_Inst : Boolean := False;
4044 Skip_Controlling_Formals : Boolean := False)
4046 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
4047 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
4048 -- If Errmsg is True, then processing continues to post an error message
4049 -- for conformance error on given node. Two messages are output. The
4050 -- first message points to the previous declaration with a general "no
4051 -- conformance" message. The second is the detailed reason, supplied as
4052 -- Msg. The parameter N provide information for a possible & insertion
4053 -- in the message, and also provides the location for posting the
4054 -- message in the absence of a specified Err_Loc location.
4056 -----------------------
4057 -- Conformance_Error --
4058 -----------------------
4060 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
4067 if No (Err_Loc) then
4073 Error_Msg_Sloc := Sloc (Old_Id);
4076 when Type_Conformant =>
4077 Error_Msg_N -- CODEFIX
4078 ("not type conformant with declaration#!", Enode);
4080 when Mode_Conformant =>
4081 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4083 ("not mode conformant with operation inherited#!",
4087 ("not mode conformant with declaration#!", Enode);
4090 when Subtype_Conformant =>
4091 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4093 ("not subtype conformant with operation inherited#!",
4097 ("not subtype conformant with declaration#!", Enode);
4100 when Fully_Conformant =>
4101 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
4102 Error_Msg_N -- CODEFIX
4103 ("not fully conformant with operation inherited#!",
4106 Error_Msg_N -- CODEFIX
4107 ("not fully conformant with declaration#!", Enode);
4111 Error_Msg_NE (Msg, Enode, N);
4113 end Conformance_Error;
4117 Old_Type : constant Entity_Id := Etype (Old_Id);
4118 New_Type : constant Entity_Id := Etype (New_Id);
4119 Old_Formal : Entity_Id;
4120 New_Formal : Entity_Id;
4121 Access_Types_Match : Boolean;
4122 Old_Formal_Base : Entity_Id;
4123 New_Formal_Base : Entity_Id;
4125 -- Start of processing for Check_Conformance
4130 -- We need a special case for operators, since they don't appear
4133 if Ctype = Type_Conformant then
4134 if Ekind (New_Id) = E_Operator
4135 and then Operator_Matches_Spec (New_Id, Old_Id)
4141 -- If both are functions/operators, check return types conform
4143 if Old_Type /= Standard_Void_Type
4144 and then New_Type /= Standard_Void_Type
4147 -- If we are checking interface conformance we omit controlling
4148 -- arguments and result, because we are only checking the conformance
4149 -- of the remaining parameters.
4151 if Has_Controlling_Result (Old_Id)
4152 and then Has_Controlling_Result (New_Id)
4153 and then Skip_Controlling_Formals
4157 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
4158 Conformance_Error ("\return type does not match!", New_Id);
4162 -- Ada 2005 (AI-231): In case of anonymous access types check the
4163 -- null-exclusion and access-to-constant attributes match.
4165 if Ada_Version >= Ada_2005
4166 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
4168 (Can_Never_Be_Null (Old_Type)
4169 /= Can_Never_Be_Null (New_Type)
4170 or else Is_Access_Constant (Etype (Old_Type))
4171 /= Is_Access_Constant (Etype (New_Type)))
4173 Conformance_Error ("\return type does not match!", New_Id);
4177 -- If either is a function/operator and the other isn't, error
4179 elsif Old_Type /= Standard_Void_Type
4180 or else New_Type /= Standard_Void_Type
4182 Conformance_Error ("\functions can only match functions!", New_Id);
4186 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
4187 -- If this is a renaming as body, refine error message to indicate that
4188 -- the conflict is with the original declaration. If the entity is not
4189 -- frozen, the conventions don't have to match, the one of the renamed
4190 -- entity is inherited.
4192 if Ctype >= Subtype_Conformant then
4193 if Convention (Old_Id) /= Convention (New_Id) then
4195 if not Is_Frozen (New_Id) then
4198 elsif Present (Err_Loc)
4199 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
4200 and then Present (Corresponding_Spec (Err_Loc))
4202 Error_Msg_Name_1 := Chars (New_Id);
4204 Name_Ada + Convention_Id'Pos (Convention (New_Id));
4205 Conformance_Error ("\prior declaration for% has convention %!");
4208 Conformance_Error ("\calling conventions do not match!");
4213 elsif Is_Formal_Subprogram (Old_Id)
4214 or else Is_Formal_Subprogram (New_Id)
4216 Conformance_Error ("\formal subprograms not allowed!");
4221 -- Deal with parameters
4223 -- Note: we use the entity information, rather than going directly
4224 -- to the specification in the tree. This is not only simpler, but
4225 -- absolutely necessary for some cases of conformance tests between
4226 -- operators, where the declaration tree simply does not exist!
4228 Old_Formal := First_Formal (Old_Id);
4229 New_Formal := First_Formal (New_Id);
4230 while Present (Old_Formal) and then Present (New_Formal) loop
4231 if Is_Controlling_Formal (Old_Formal)
4232 and then Is_Controlling_Formal (New_Formal)
4233 and then Skip_Controlling_Formals
4235 -- The controlling formals will have different types when
4236 -- comparing an interface operation with its match, but both
4237 -- or neither must be access parameters.
4239 if Is_Access_Type (Etype (Old_Formal))
4241 Is_Access_Type (Etype (New_Formal))
4243 goto Skip_Controlling_Formal;
4246 ("\access parameter does not match!", New_Formal);
4250 if Ctype = Fully_Conformant then
4252 -- Names must match. Error message is more accurate if we do
4253 -- this before checking that the types of the formals match.
4255 if Chars (Old_Formal) /= Chars (New_Formal) then
4256 Conformance_Error ("\name & does not match!", New_Formal);
4258 -- Set error posted flag on new formal as well to stop
4259 -- junk cascaded messages in some cases.
4261 Set_Error_Posted (New_Formal);
4265 -- Null exclusion must match
4267 if Null_Exclusion_Present (Parent (Old_Formal))
4269 Null_Exclusion_Present (Parent (New_Formal))
4271 -- Only give error if both come from source. This should be
4272 -- investigated some time, since it should not be needed ???
4274 if Comes_From_Source (Old_Formal)
4276 Comes_From_Source (New_Formal)
4279 ("\null exclusion for & does not match", New_Formal);
4281 -- Mark error posted on the new formal to avoid duplicated
4282 -- complaint about types not matching.
4284 Set_Error_Posted (New_Formal);
4289 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
4290 -- case occurs whenever a subprogram is being renamed and one of its
4291 -- parameters imposes a null exclusion. For example:
4293 -- type T is null record;
4294 -- type Acc_T is access T;
4295 -- subtype Acc_T_Sub is Acc_T;
4297 -- procedure P (Obj : not null Acc_T_Sub); -- itype
4298 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
4301 Old_Formal_Base := Etype (Old_Formal);
4302 New_Formal_Base := Etype (New_Formal);
4305 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
4306 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
4309 Access_Types_Match := Ada_Version >= Ada_2005
4311 -- Ensure that this rule is only applied when New_Id is a
4312 -- renaming of Old_Id.
4314 and then Nkind (Parent (Parent (New_Id))) =
4315 N_Subprogram_Renaming_Declaration
4316 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
4317 and then Present (Entity (Name (Parent (Parent (New_Id)))))
4318 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
4320 -- Now handle the allowed access-type case
4322 and then Is_Access_Type (Old_Formal_Base)
4323 and then Is_Access_Type (New_Formal_Base)
4325 -- The type kinds must match. The only exception occurs with
4326 -- multiple generics of the form:
4329 -- type F is private; type A is private;
4330 -- type F_Ptr is access F; type A_Ptr is access A;
4331 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
4332 -- package F_Pack is ... package A_Pack is
4333 -- package F_Inst is
4334 -- new F_Pack (A, A_Ptr, A_P);
4336 -- When checking for conformance between the parameters of A_P
4337 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
4338 -- because the compiler has transformed A_Ptr into a subtype of
4339 -- F_Ptr. We catch this case in the code below.
4341 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
4343 (Is_Generic_Type (Old_Formal_Base)
4344 and then Is_Generic_Type (New_Formal_Base)
4345 and then Is_Internal (New_Formal_Base)
4346 and then Etype (Etype (New_Formal_Base)) =
4348 and then Directly_Designated_Type (Old_Formal_Base) =
4349 Directly_Designated_Type (New_Formal_Base)
4350 and then ((Is_Itype (Old_Formal_Base)
4351 and then Can_Never_Be_Null (Old_Formal_Base))
4353 (Is_Itype (New_Formal_Base)
4354 and then Can_Never_Be_Null (New_Formal_Base)));
4356 -- Types must always match. In the visible part of an instance,
4357 -- usual overloading rules for dispatching operations apply, and
4358 -- we check base types (not the actual subtypes).
4360 if In_Instance_Visible_Part
4361 and then Is_Dispatching_Operation (New_Id)
4363 if not Conforming_Types
4364 (T1 => Base_Type (Etype (Old_Formal)),
4365 T2 => Base_Type (Etype (New_Formal)),
4367 Get_Inst => Get_Inst)
4368 and then not Access_Types_Match
4370 Conformance_Error ("\type of & does not match!", New_Formal);
4374 elsif not Conforming_Types
4375 (T1 => Old_Formal_Base,
4376 T2 => New_Formal_Base,
4378 Get_Inst => Get_Inst)
4379 and then not Access_Types_Match
4381 -- Don't give error message if old type is Any_Type. This test
4382 -- avoids some cascaded errors, e.g. in case of a bad spec.
4384 if Errmsg and then Old_Formal_Base = Any_Type then
4387 Conformance_Error ("\type of & does not match!", New_Formal);
4393 -- For mode conformance, mode must match
4395 if Ctype >= Mode_Conformant then
4396 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
4397 if not Ekind_In (New_Id, E_Function, E_Procedure)
4398 or else not Is_Primitive_Wrapper (New_Id)
4400 Conformance_Error ("\mode of & does not match!", New_Formal);
4404 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
4406 if Is_Protected_Type
4407 (Corresponding_Concurrent_Type (T))
4409 Error_Msg_PT (T, New_Id);
4412 ("\mode of & does not match!", New_Formal);
4419 -- Part of mode conformance for access types is having the same
4420 -- constant modifier.
4422 elsif Access_Types_Match
4423 and then Is_Access_Constant (Old_Formal_Base) /=
4424 Is_Access_Constant (New_Formal_Base)
4427 ("\constant modifier does not match!", New_Formal);
4432 if Ctype >= Subtype_Conformant then
4434 -- Ada 2005 (AI-231): In case of anonymous access types check
4435 -- the null-exclusion and access-to-constant attributes must
4436 -- match. For null exclusion, we test the types rather than the
4437 -- formals themselves, since the attribute is only set reliably
4438 -- on the formals in the Ada 95 case, and we exclude the case
4439 -- where Old_Formal is marked as controlling, to avoid errors
4440 -- when matching completing bodies with dispatching declarations
4441 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
4443 if Ada_Version >= Ada_2005
4444 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
4445 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
4447 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
4448 Can_Never_Be_Null (Etype (New_Formal))
4450 not Is_Controlling_Formal (Old_Formal))
4452 Is_Access_Constant (Etype (Old_Formal)) /=
4453 Is_Access_Constant (Etype (New_Formal)))
4455 -- Do not complain if error already posted on New_Formal. This
4456 -- avoids some redundant error messages.
4458 and then not Error_Posted (New_Formal)
4460 -- It is allowed to omit the null-exclusion in case of stream
4461 -- attribute subprograms. We recognize stream subprograms
4462 -- through their TSS-generated suffix.
4465 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
4467 if TSS_Name /= TSS_Stream_Read
4468 and then TSS_Name /= TSS_Stream_Write
4469 and then TSS_Name /= TSS_Stream_Input
4470 and then TSS_Name /= TSS_Stream_Output
4473 ("\type of & does not match!", New_Formal);
4480 -- Full conformance checks
4482 if Ctype = Fully_Conformant then
4484 -- We have checked already that names match
4486 if Parameter_Mode (Old_Formal) = E_In_Parameter then
4488 -- Check default expressions for in parameters
4491 NewD : constant Boolean :=
4492 Present (Default_Value (New_Formal));
4493 OldD : constant Boolean :=
4494 Present (Default_Value (Old_Formal));
4496 if NewD or OldD then
4498 -- The old default value has been analyzed because the
4499 -- current full declaration will have frozen everything
4500 -- before. The new default value has not been analyzed,
4501 -- so analyze it now before we check for conformance.
4504 Push_Scope (New_Id);
4505 Preanalyze_Spec_Expression
4506 (Default_Value (New_Formal), Etype (New_Formal));
4510 if not (NewD and OldD)
4511 or else not Fully_Conformant_Expressions
4512 (Default_Value (Old_Formal),
4513 Default_Value (New_Formal))
4516 ("\default expression for & does not match!",
4525 -- A couple of special checks for Ada 83 mode. These checks are
4526 -- skipped if either entity is an operator in package Standard,
4527 -- or if either old or new instance is not from the source program.
4529 if Ada_Version = Ada_83
4530 and then Sloc (Old_Id) > Standard_Location
4531 and then Sloc (New_Id) > Standard_Location
4532 and then Comes_From_Source (Old_Id)
4533 and then Comes_From_Source (New_Id)
4536 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
4537 New_Param : constant Node_Id := Declaration_Node (New_Formal);
4540 -- Explicit IN must be present or absent in both cases. This
4541 -- test is required only in the full conformance case.
4543 if In_Present (Old_Param) /= In_Present (New_Param)
4544 and then Ctype = Fully_Conformant
4547 ("\(Ada 83) IN must appear in both declarations",
4552 -- Grouping (use of comma in param lists) must be the same
4553 -- This is where we catch a misconformance like:
4556 -- A : Integer; B : Integer
4558 -- which are represented identically in the tree except
4559 -- for the setting of the flags More_Ids and Prev_Ids.
4561 if More_Ids (Old_Param) /= More_Ids (New_Param)
4562 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
4565 ("\grouping of & does not match!", New_Formal);
4571 -- This label is required when skipping controlling formals
4573 <<Skip_Controlling_Formal>>
4575 Next_Formal (Old_Formal);
4576 Next_Formal (New_Formal);
4579 if Present (Old_Formal) then
4580 Conformance_Error ("\too few parameters!");
4583 elsif Present (New_Formal) then
4584 Conformance_Error ("\too many parameters!", New_Formal);
4587 end Check_Conformance;
4589 -----------------------
4590 -- Check_Conventions --
4591 -----------------------
4593 procedure Check_Conventions (Typ : Entity_Id) is
4594 Ifaces_List : Elist_Id;
4596 procedure Check_Convention (Op : Entity_Id);
4597 -- Verify that the convention of inherited dispatching operation Op is
4598 -- consistent among all subprograms it overrides. In order to minimize
4599 -- the search, Search_From is utilized to designate a specific point in
4600 -- the list rather than iterating over the whole list once more.
4602 ----------------------
4603 -- Check_Convention --
4604 ----------------------
4606 procedure Check_Convention (Op : Entity_Id) is
4607 Iface_Elmt : Elmt_Id;
4608 Iface_Prim_Elmt : Elmt_Id;
4609 Iface_Prim : Entity_Id;
4612 Iface_Elmt := First_Elmt (Ifaces_List);
4613 while Present (Iface_Elmt) loop
4615 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
4616 while Present (Iface_Prim_Elmt) loop
4617 Iface_Prim := Node (Iface_Prim_Elmt);
4619 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
4620 and then Convention (Iface_Prim) /= Convention (Op)
4623 ("inconsistent conventions in primitive operations", Typ);
4625 Error_Msg_Name_1 := Chars (Op);
4626 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
4627 Error_Msg_Sloc := Sloc (Op);
4629 if Comes_From_Source (Op) or else No (Alias (Op)) then
4630 if not Present (Overridden_Operation (Op)) then
4631 Error_Msg_N ("\\primitive % defined #", Typ);
4634 ("\\overriding operation % with " &
4635 "convention % defined #", Typ);
4638 else pragma Assert (Present (Alias (Op)));
4639 Error_Msg_Sloc := Sloc (Alias (Op));
4641 ("\\inherited operation % with " &
4642 "convention % defined #", Typ);
4645 Error_Msg_Name_1 := Chars (Op);
4647 Get_Convention_Name (Convention (Iface_Prim));
4648 Error_Msg_Sloc := Sloc (Iface_Prim);
4650 ("\\overridden operation % with " &
4651 "convention % defined #", Typ);
4653 -- Avoid cascading errors
4658 Next_Elmt (Iface_Prim_Elmt);
4661 Next_Elmt (Iface_Elmt);
4663 end Check_Convention;
4667 Prim_Op : Entity_Id;
4668 Prim_Op_Elmt : Elmt_Id;
4670 -- Start of processing for Check_Conventions
4673 if not Has_Interfaces (Typ) then
4677 Collect_Interfaces (Typ, Ifaces_List);
4679 -- The algorithm checks every overriding dispatching operation against
4680 -- all the corresponding overridden dispatching operations, detecting
4681 -- differences in conventions.
4683 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
4684 while Present (Prim_Op_Elmt) loop
4685 Prim_Op := Node (Prim_Op_Elmt);
4687 -- A small optimization: skip the predefined dispatching operations
4688 -- since they always have the same convention.
4690 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
4691 Check_Convention (Prim_Op);
4694 Next_Elmt (Prim_Op_Elmt);
4696 end Check_Conventions;
4698 ------------------------------
4699 -- Check_Delayed_Subprogram --
4700 ------------------------------
4702 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
4705 procedure Possible_Freeze (T : Entity_Id);
4706 -- T is the type of either a formal parameter or of the return type.
4707 -- If T is not yet frozen and needs a delayed freeze, then the
4708 -- subprogram itself must be delayed. If T is the limited view of an
4709 -- incomplete type the subprogram must be frozen as well, because
4710 -- T may depend on local types that have not been frozen yet.
4712 ---------------------
4713 -- Possible_Freeze --
4714 ---------------------
4716 procedure Possible_Freeze (T : Entity_Id) is
4718 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
4719 Set_Has_Delayed_Freeze (Designator);
4721 elsif Is_Access_Type (T)
4722 and then Has_Delayed_Freeze (Designated_Type (T))
4723 and then not Is_Frozen (Designated_Type (T))
4725 Set_Has_Delayed_Freeze (Designator);
4727 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
4728 Set_Has_Delayed_Freeze (Designator);
4730 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
4731 -- of a subprogram or entry declaration.
4733 elsif Ekind (T) = E_Incomplete_Type
4734 and then Ada_Version >= Ada_2012
4736 Set_Has_Delayed_Freeze (Designator);
4739 end Possible_Freeze;
4741 -- Start of processing for Check_Delayed_Subprogram
4744 -- All subprograms, including abstract subprograms, may need a freeze
4745 -- node if some formal type or the return type needs one.
4747 Possible_Freeze (Etype (Designator));
4748 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
4750 -- Need delayed freeze if any of the formal types themselves need
4751 -- a delayed freeze and are not yet frozen.
4753 F := First_Formal (Designator);
4754 while Present (F) loop
4755 Possible_Freeze (Etype (F));
4756 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
4760 -- Mark functions that return by reference. Note that it cannot be
4761 -- done for delayed_freeze subprograms because the underlying
4762 -- returned type may not be known yet (for private types)
4764 if not Has_Delayed_Freeze (Designator)
4765 and then Expander_Active
4768 Typ : constant Entity_Id := Etype (Designator);
4769 Utyp : constant Entity_Id := Underlying_Type (Typ);
4772 if Is_Immutably_Limited_Type (Typ) then
4773 Set_Returns_By_Ref (Designator);
4775 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
4776 Set_Returns_By_Ref (Designator);
4780 end Check_Delayed_Subprogram;
4782 ------------------------------------
4783 -- Check_Discriminant_Conformance --
4784 ------------------------------------
4786 procedure Check_Discriminant_Conformance
4791 Old_Discr : Entity_Id := First_Discriminant (Prev);
4792 New_Discr : Node_Id := First (Discriminant_Specifications (N));
4793 New_Discr_Id : Entity_Id;
4794 New_Discr_Type : Entity_Id;
4796 procedure Conformance_Error (Msg : String; N : Node_Id);
4797 -- Post error message for conformance error on given node. Two messages
4798 -- are output. The first points to the previous declaration with a
4799 -- general "no conformance" message. The second is the detailed reason,
4800 -- supplied as Msg. The parameter N provide information for a possible
4801 -- & insertion in the message.
4803 -----------------------
4804 -- Conformance_Error --
4805 -----------------------
4807 procedure Conformance_Error (Msg : String; N : Node_Id) is
4809 Error_Msg_Sloc := Sloc (Prev_Loc);
4810 Error_Msg_N -- CODEFIX
4811 ("not fully conformant with declaration#!", N);
4812 Error_Msg_NE (Msg, N, N);
4813 end Conformance_Error;
4815 -- Start of processing for Check_Discriminant_Conformance
4818 while Present (Old_Discr) and then Present (New_Discr) loop
4820 New_Discr_Id := Defining_Identifier (New_Discr);
4822 -- The subtype mark of the discriminant on the full type has not
4823 -- been analyzed so we do it here. For an access discriminant a new
4826 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
4828 Access_Definition (N, Discriminant_Type (New_Discr));
4831 Analyze (Discriminant_Type (New_Discr));
4832 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
4834 -- Ada 2005: if the discriminant definition carries a null
4835 -- exclusion, create an itype to check properly for consistency
4836 -- with partial declaration.
4838 if Is_Access_Type (New_Discr_Type)
4839 and then Null_Exclusion_Present (New_Discr)
4842 Create_Null_Excluding_Itype
4843 (T => New_Discr_Type,
4844 Related_Nod => New_Discr,
4845 Scope_Id => Current_Scope);
4849 if not Conforming_Types
4850 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
4852 Conformance_Error ("type of & does not match!", New_Discr_Id);
4855 -- Treat the new discriminant as an occurrence of the old one,
4856 -- for navigation purposes, and fill in some semantic
4857 -- information, for completeness.
4859 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
4860 Set_Etype (New_Discr_Id, Etype (Old_Discr));
4861 Set_Scope (New_Discr_Id, Scope (Old_Discr));
4866 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
4867 Conformance_Error ("name & does not match!", New_Discr_Id);
4871 -- Default expressions must match
4874 NewD : constant Boolean :=
4875 Present (Expression (New_Discr));
4876 OldD : constant Boolean :=
4877 Present (Expression (Parent (Old_Discr)));
4880 if NewD or OldD then
4882 -- The old default value has been analyzed and expanded,
4883 -- because the current full declaration will have frozen
4884 -- everything before. The new default values have not been
4885 -- expanded, so expand now to check conformance.
4888 Preanalyze_Spec_Expression
4889 (Expression (New_Discr), New_Discr_Type);
4892 if not (NewD and OldD)
4893 or else not Fully_Conformant_Expressions
4894 (Expression (Parent (Old_Discr)),
4895 Expression (New_Discr))
4899 ("default expression for & does not match!",
4906 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
4908 if Ada_Version = Ada_83 then
4910 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
4913 -- Grouping (use of comma in param lists) must be the same
4914 -- This is where we catch a misconformance like:
4917 -- A : Integer; B : Integer
4919 -- which are represented identically in the tree except
4920 -- for the setting of the flags More_Ids and Prev_Ids.
4922 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
4923 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
4926 ("grouping of & does not match!", New_Discr_Id);
4932 Next_Discriminant (Old_Discr);
4936 if Present (Old_Discr) then
4937 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
4940 elsif Present (New_Discr) then
4942 ("too many discriminants!", Defining_Identifier (New_Discr));
4945 end Check_Discriminant_Conformance;
4947 ----------------------------
4948 -- Check_Fully_Conformant --
4949 ----------------------------
4951 procedure Check_Fully_Conformant
4952 (New_Id : Entity_Id;
4954 Err_Loc : Node_Id := Empty)
4957 pragma Warnings (Off, Result);
4960 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
4961 end Check_Fully_Conformant;
4963 ---------------------------
4964 -- Check_Mode_Conformant --
4965 ---------------------------
4967 procedure Check_Mode_Conformant
4968 (New_Id : Entity_Id;
4970 Err_Loc : Node_Id := Empty;
4971 Get_Inst : Boolean := False)
4974 pragma Warnings (Off, Result);
4977 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
4978 end Check_Mode_Conformant;
4980 --------------------------------
4981 -- Check_Overriding_Indicator --
4982 --------------------------------
4984 procedure Check_Overriding_Indicator
4986 Overridden_Subp : Entity_Id;
4987 Is_Primitive : Boolean)
4993 -- No overriding indicator for literals
4995 if Ekind (Subp) = E_Enumeration_Literal then
4998 elsif Ekind (Subp) = E_Entry then
4999 Decl := Parent (Subp);
5001 -- No point in analyzing a malformed operator
5003 elsif Nkind (Subp) = N_Defining_Operator_Symbol
5004 and then Error_Posted (Subp)
5009 Decl := Unit_Declaration_Node (Subp);
5012 if Nkind_In (Decl, N_Subprogram_Body,
5013 N_Subprogram_Body_Stub,
5014 N_Subprogram_Declaration,
5015 N_Abstract_Subprogram_Declaration,
5016 N_Subprogram_Renaming_Declaration)
5018 Spec := Specification (Decl);
5020 elsif Nkind (Decl) = N_Entry_Declaration then
5027 -- The overriding operation is type conformant with the overridden one,
5028 -- but the names of the formals are not required to match. If the names
5029 -- appear permuted in the overriding operation, this is a possible
5030 -- source of confusion that is worth diagnosing. Controlling formals
5031 -- often carry names that reflect the type, and it is not worthwhile
5032 -- requiring that their names match.
5034 if Present (Overridden_Subp)
5035 and then Nkind (Subp) /= N_Defining_Operator_Symbol
5042 Form1 := First_Formal (Subp);
5043 Form2 := First_Formal (Overridden_Subp);
5045 -- If the overriding operation is a synchronized operation, skip
5046 -- the first parameter of the overridden operation, which is
5047 -- implicit in the new one. If the operation is declared in the
5048 -- body it is not primitive and all formals must match.
5050 if Is_Concurrent_Type (Scope (Subp))
5051 and then Is_Tagged_Type (Scope (Subp))
5052 and then not Has_Completion (Scope (Subp))
5054 Form2 := Next_Formal (Form2);
5057 if Present (Form1) then
5058 Form1 := Next_Formal (Form1);
5059 Form2 := Next_Formal (Form2);
5062 while Present (Form1) loop
5063 if not Is_Controlling_Formal (Form1)
5064 and then Present (Next_Formal (Form2))
5065 and then Chars (Form1) = Chars (Next_Formal (Form2))
5067 Error_Msg_Node_2 := Alias (Overridden_Subp);
5068 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
5070 ("& does not match corresponding formal of&#",
5075 Next_Formal (Form1);
5076 Next_Formal (Form2);
5081 -- If there is an overridden subprogram, then check that there is no
5082 -- "not overriding" indicator, and mark the subprogram as overriding.
5083 -- This is not done if the overridden subprogram is marked as hidden,
5084 -- which can occur for the case of inherited controlled operations
5085 -- (see Derive_Subprogram), unless the inherited subprogram's parent
5086 -- subprogram is not itself hidden. (Note: This condition could probably
5087 -- be simplified, leaving out the testing for the specific controlled
5088 -- cases, but it seems safer and clearer this way, and echoes similar
5089 -- special-case tests of this kind in other places.)
5091 if Present (Overridden_Subp)
5092 and then (not Is_Hidden (Overridden_Subp)
5094 ((Chars (Overridden_Subp) = Name_Initialize
5096 Chars (Overridden_Subp) = Name_Adjust
5098 Chars (Overridden_Subp) = Name_Finalize)
5099 and then Present (Alias (Overridden_Subp))
5100 and then not Is_Hidden (Alias (Overridden_Subp))))
5102 if Must_Not_Override (Spec) then
5103 Error_Msg_Sloc := Sloc (Overridden_Subp);
5105 if Ekind (Subp) = E_Entry then
5107 ("entry & overrides inherited operation #", Spec, Subp);
5110 ("subprogram & overrides inherited operation #", Spec, Subp);
5113 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
5114 -- as an extension of Root_Controlled, and thus has a useless Adjust
5115 -- operation. This operation should not be inherited by other limited
5116 -- controlled types. An explicit Adjust for them is not overriding.
5118 elsif Must_Override (Spec)
5119 and then Chars (Overridden_Subp) = Name_Adjust
5120 and then Is_Limited_Type (Etype (First_Formal (Subp)))
5121 and then Present (Alias (Overridden_Subp))
5123 Is_Predefined_File_Name
5124 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
5126 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5128 elsif Is_Subprogram (Subp) then
5129 if Is_Init_Proc (Subp) then
5132 elsif No (Overridden_Operation (Subp)) then
5134 -- For entities generated by Derive_Subprograms the overridden
5135 -- operation is the inherited primitive (which is available
5136 -- through the attribute alias)
5138 if (Is_Dispatching_Operation (Subp)
5139 or else Is_Dispatching_Operation (Overridden_Subp))
5140 and then not Comes_From_Source (Overridden_Subp)
5141 and then Find_Dispatching_Type (Overridden_Subp) =
5142 Find_Dispatching_Type (Subp)
5143 and then Present (Alias (Overridden_Subp))
5144 and then Comes_From_Source (Alias (Overridden_Subp))
5146 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
5149 Set_Overridden_Operation (Subp, Overridden_Subp);
5154 -- If primitive flag is set or this is a protected operation, then
5155 -- the operation is overriding at the point of its declaration, so
5156 -- warn if necessary. Otherwise it may have been declared before the
5157 -- operation it overrides and no check is required.
5160 and then not Must_Override (Spec)
5161 and then (Is_Primitive
5162 or else Ekind (Scope (Subp)) = E_Protected_Type)
5164 Style.Missing_Overriding (Decl, Subp);
5167 -- If Subp is an operator, it may override a predefined operation, if
5168 -- it is defined in the same scope as the type to which it applies.
5169 -- In that case Overridden_Subp is empty because of our implicit
5170 -- representation for predefined operators. We have to check whether the
5171 -- signature of Subp matches that of a predefined operator. Note that
5172 -- first argument provides the name of the operator, and the second
5173 -- argument the signature that may match that of a standard operation.
5174 -- If the indicator is overriding, then the operator must match a
5175 -- predefined signature, because we know already that there is no
5176 -- explicit overridden operation.
5178 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
5179 if Must_Not_Override (Spec) then
5181 -- If this is not a primitive or a protected subprogram, then
5182 -- "not overriding" is illegal.
5185 and then Ekind (Scope (Subp)) /= E_Protected_Type
5188 ("overriding indicator only allowed "
5189 & "if subprogram is primitive", Subp);
5191 elsif Can_Override_Operator (Subp) then
5193 ("subprogram& overrides predefined operator ", Spec, Subp);
5196 elsif Must_Override (Spec) then
5197 if No (Overridden_Operation (Subp))
5198 and then not Can_Override_Operator (Subp)
5200 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5203 elsif not Error_Posted (Subp)
5204 and then Style_Check
5205 and then Can_Override_Operator (Subp)
5207 not Is_Predefined_File_Name
5208 (Unit_File_Name (Get_Source_Unit (Subp)))
5210 -- If style checks are enabled, indicate that the indicator is
5211 -- missing. However, at the point of declaration, the type of
5212 -- which this is a primitive operation may be private, in which
5213 -- case the indicator would be premature.
5215 if Has_Private_Declaration (Etype (Subp))
5216 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
5220 Style.Missing_Overriding (Decl, Subp);
5224 elsif Must_Override (Spec) then
5225 if Ekind (Subp) = E_Entry then
5226 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5228 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
5231 -- If the operation is marked "not overriding" and it's not primitive
5232 -- then an error is issued, unless this is an operation of a task or
5233 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
5234 -- has been specified have already been checked above.
5236 elsif Must_Not_Override (Spec)
5237 and then not Is_Primitive
5238 and then Ekind (Subp) /= E_Entry
5239 and then Ekind (Scope (Subp)) /= E_Protected_Type
5242 ("overriding indicator only allowed if subprogram is primitive",
5246 end Check_Overriding_Indicator;
5252 -- Note: this procedure needs to know far too much about how the expander
5253 -- messes with exceptions. The use of the flag Exception_Junk and the
5254 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
5255 -- works, but is not very clean. It would be better if the expansion
5256 -- routines would leave Original_Node working nicely, and we could use
5257 -- Original_Node here to ignore all the peculiar expander messing ???
5259 procedure Check_Returns
5263 Proc : Entity_Id := Empty)
5267 procedure Check_Statement_Sequence (L : List_Id);
5268 -- Internal recursive procedure to check a list of statements for proper
5269 -- termination by a return statement (or a transfer of control or a
5270 -- compound statement that is itself internally properly terminated).
5272 ------------------------------
5273 -- Check_Statement_Sequence --
5274 ------------------------------
5276 procedure Check_Statement_Sequence (L : List_Id) is
5281 Raise_Exception_Call : Boolean;
5282 -- Set True if statement sequence terminated by Raise_Exception call
5283 -- or a Reraise_Occurrence call.
5286 Raise_Exception_Call := False;
5288 -- Get last real statement
5290 Last_Stm := Last (L);
5292 -- Deal with digging out exception handler statement sequences that
5293 -- have been transformed by the local raise to goto optimization.
5294 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
5295 -- optimization has occurred, we are looking at something like:
5298 -- original stmts in block
5302 -- goto L1; | omitted if No_Exception_Propagation
5307 -- goto L3; -- skip handler when exception not raised
5309 -- <<L1>> -- target label for local exception
5323 -- and what we have to do is to dig out the estmts1 and estmts2
5324 -- sequences (which were the original sequences of statements in
5325 -- the exception handlers) and check them.
5327 if Nkind (Last_Stm) = N_Label
5328 and then Exception_Junk (Last_Stm)
5334 exit when Nkind (Stm) /= N_Block_Statement;
5335 exit when not Exception_Junk (Stm);
5338 exit when Nkind (Stm) /= N_Label;
5339 exit when not Exception_Junk (Stm);
5340 Check_Statement_Sequence
5341 (Statements (Handled_Statement_Sequence (Next (Stm))));
5346 exit when Nkind (Stm) /= N_Goto_Statement;
5347 exit when not Exception_Junk (Stm);
5351 -- Don't count pragmas
5353 while Nkind (Last_Stm) = N_Pragma
5355 -- Don't count call to SS_Release (can happen after Raise_Exception)
5358 (Nkind (Last_Stm) = N_Procedure_Call_Statement
5360 Nkind (Name (Last_Stm)) = N_Identifier
5362 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
5364 -- Don't count exception junk
5367 (Nkind_In (Last_Stm, N_Goto_Statement,
5369 N_Object_Declaration)
5370 and then Exception_Junk (Last_Stm))
5371 or else Nkind (Last_Stm) in N_Push_xxx_Label
5372 or else Nkind (Last_Stm) in N_Pop_xxx_Label
5377 -- Here we have the "real" last statement
5379 Kind := Nkind (Last_Stm);
5381 -- Transfer of control, OK. Note that in the No_Return procedure
5382 -- case, we already diagnosed any explicit return statements, so
5383 -- we can treat them as OK in this context.
5385 if Is_Transfer (Last_Stm) then
5388 -- Check cases of explicit non-indirect procedure calls
5390 elsif Kind = N_Procedure_Call_Statement
5391 and then Is_Entity_Name (Name (Last_Stm))
5393 -- Check call to Raise_Exception procedure which is treated
5394 -- specially, as is a call to Reraise_Occurrence.
5396 -- We suppress the warning in these cases since it is likely that
5397 -- the programmer really does not expect to deal with the case
5398 -- of Null_Occurrence, and thus would find a warning about a
5399 -- missing return curious, and raising Program_Error does not
5400 -- seem such a bad behavior if this does occur.
5402 -- Note that in the Ada 2005 case for Raise_Exception, the actual
5403 -- behavior will be to raise Constraint_Error (see AI-329).
5405 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
5407 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
5409 Raise_Exception_Call := True;
5411 -- For Raise_Exception call, test first argument, if it is
5412 -- an attribute reference for a 'Identity call, then we know
5413 -- that the call cannot possibly return.
5416 Arg : constant Node_Id :=
5417 Original_Node (First_Actual (Last_Stm));
5419 if Nkind (Arg) = N_Attribute_Reference
5420 and then Attribute_Name (Arg) = Name_Identity
5427 -- If statement, need to look inside if there is an else and check
5428 -- each constituent statement sequence for proper termination.
5430 elsif Kind = N_If_Statement
5431 and then Present (Else_Statements (Last_Stm))
5433 Check_Statement_Sequence (Then_Statements (Last_Stm));
5434 Check_Statement_Sequence (Else_Statements (Last_Stm));
5436 if Present (Elsif_Parts (Last_Stm)) then
5438 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
5441 while Present (Elsif_Part) loop
5442 Check_Statement_Sequence (Then_Statements (Elsif_Part));
5450 -- Case statement, check each case for proper termination
5452 elsif Kind = N_Case_Statement then
5456 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
5457 while Present (Case_Alt) loop
5458 Check_Statement_Sequence (Statements (Case_Alt));
5459 Next_Non_Pragma (Case_Alt);
5465 -- Block statement, check its handled sequence of statements
5467 elsif Kind = N_Block_Statement then
5473 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
5482 -- Loop statement. If there is an iteration scheme, we can definitely
5483 -- fall out of the loop. Similarly if there is an exit statement, we
5484 -- can fall out. In either case we need a following return.
5486 elsif Kind = N_Loop_Statement then
5487 if Present (Iteration_Scheme (Last_Stm))
5488 or else Has_Exit (Entity (Identifier (Last_Stm)))
5492 -- A loop with no exit statement or iteration scheme is either
5493 -- an infinite loop, or it has some other exit (raise/return).
5494 -- In either case, no warning is required.
5500 -- Timed entry call, check entry call and delay alternatives
5502 -- Note: in expanded code, the timed entry call has been converted
5503 -- to a set of expanded statements on which the check will work
5504 -- correctly in any case.
5506 elsif Kind = N_Timed_Entry_Call then
5508 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5509 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
5512 -- If statement sequence of entry call alternative is missing,
5513 -- then we can definitely fall through, and we post the error
5514 -- message on the entry call alternative itself.
5516 if No (Statements (ECA)) then
5519 -- If statement sequence of delay alternative is missing, then
5520 -- we can definitely fall through, and we post the error
5521 -- message on the delay alternative itself.
5523 -- Note: if both ECA and DCA are missing the return, then we
5524 -- post only one message, should be enough to fix the bugs.
5525 -- If not we will get a message next time on the DCA when the
5528 elsif No (Statements (DCA)) then
5531 -- Else check both statement sequences
5534 Check_Statement_Sequence (Statements (ECA));
5535 Check_Statement_Sequence (Statements (DCA));
5540 -- Conditional entry call, check entry call and else part
5542 -- Note: in expanded code, the conditional entry call has been
5543 -- converted to a set of expanded statements on which the check
5544 -- will work correctly in any case.
5546 elsif Kind = N_Conditional_Entry_Call then
5548 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
5551 -- If statement sequence of entry call alternative is missing,
5552 -- then we can definitely fall through, and we post the error
5553 -- message on the entry call alternative itself.
5555 if No (Statements (ECA)) then
5558 -- Else check statement sequence and else part
5561 Check_Statement_Sequence (Statements (ECA));
5562 Check_Statement_Sequence (Else_Statements (Last_Stm));
5568 -- If we fall through, issue appropriate message
5571 if not Raise_Exception_Call then
5573 ("?RETURN statement missing following this statement!",
5576 ("\?Program_Error may be raised at run time!",
5580 -- Note: we set Err even though we have not issued a warning
5581 -- because we still have a case of a missing return. This is
5582 -- an extremely marginal case, probably will never be noticed
5583 -- but we might as well get it right.
5587 -- Otherwise we have the case of a procedure marked No_Return
5590 if not Raise_Exception_Call then
5592 ("?implied return after this statement " &
5593 "will raise Program_Error",
5596 ("\?procedure & is marked as No_Return!",
5601 RE : constant Node_Id :=
5602 Make_Raise_Program_Error (Sloc (Last_Stm),
5603 Reason => PE_Implicit_Return);
5605 Insert_After (Last_Stm, RE);
5609 end Check_Statement_Sequence;
5611 -- Start of processing for Check_Returns
5615 Check_Statement_Sequence (Statements (HSS));
5617 if Present (Exception_Handlers (HSS)) then
5618 Handler := First_Non_Pragma (Exception_Handlers (HSS));
5619 while Present (Handler) loop
5620 Check_Statement_Sequence (Statements (Handler));
5621 Next_Non_Pragma (Handler);
5626 -------------------------------
5627 -- Check_Subprogram_Contract --
5628 -------------------------------
5630 procedure Check_Subprogram_Contract (Spec_Id : Entity_Id) is
5632 -- Code is currently commented out as, in some cases, it causes crashes
5633 -- because Direct_Primitive_Operations is not available for a private
5634 -- type. This may cause more warnings to be issued than necessary. See
5635 -- below for the intended use of this variable. ???
5637 -- Inherited : constant Subprogram_List :=
5638 -- Inherited_Subprograms (Spec_Id);
5639 -- -- List of subprograms inherited by this subprogram
5641 Last_Postcondition : Node_Id := Empty;
5642 -- Last postcondition on the subprogram, or else Empty if either no
5643 -- postcondition or only inherited postconditions.
5645 Attribute_Result_Mentioned : Boolean := False;
5646 -- Whether attribute 'Result is mentioned in a postcondition
5648 Post_State_Mentioned : Boolean := False;
5649 -- Whether some expression mentioned in a postcondition can have a
5650 -- different value in the post-state than in the pre-state.
5652 function Check_Attr_Result (N : Node_Id) return Traverse_Result;
5653 -- Check if N is a reference to the attribute 'Result, and if so set
5654 -- Attribute_Result_Mentioned and return Abandon. Otherwise return OK.
5656 function Check_Post_State (N : Node_Id) return Traverse_Result;
5657 -- Check whether the value of evaluating N can be different in the
5658 -- post-state, compared to the same evaluation in the pre-state, and
5659 -- if so set Post_State_Mentioned and return Abandon. Return Skip on
5660 -- reference to attribute 'Old, in order to ignore its prefix, which
5661 -- is precisely evaluated in the pre-state. Otherwise return OK.
5663 procedure Process_Post_Conditions (Spec : Node_Id; Class : Boolean);
5664 -- This processes the Spec_PPC_List from Spec, processing any
5665 -- postconditions from the list. If Class is True, then only
5666 -- postconditions marked with Class_Present are considered. The
5667 -- caller has checked that Spec_PPC_List is non-Empty.
5669 function Find_Attribute_Result is new Traverse_Func (Check_Attr_Result);
5671 function Find_Post_State is new Traverse_Func (Check_Post_State);
5673 -----------------------
5674 -- Check_Attr_Result --
5675 -----------------------
5677 function Check_Attr_Result (N : Node_Id) return Traverse_Result is
5679 if Nkind (N) = N_Attribute_Reference
5680 and then Get_Attribute_Id (Attribute_Name (N)) = Attribute_Result
5682 Attribute_Result_Mentioned := True;
5687 end Check_Attr_Result;
5689 ----------------------
5690 -- Check_Post_State --
5691 ----------------------
5693 function Check_Post_State (N : Node_Id) return Traverse_Result is
5694 Found : Boolean := False;
5698 when N_Function_Call |
5699 N_Explicit_Dereference =>
5706 E : constant Entity_Id := Entity (N);
5709 -- ???Quantified expressions get analyzed later, so E can
5710 -- be empty at this point. In this case, we suppress the
5711 -- warning, just in case E is assignable. It seems better to
5712 -- have false negatives than false positives. At some point,
5713 -- we should make the warning more accurate, either by
5714 -- analyzing quantified expressions earlier, or moving
5715 -- this processing later.
5720 and then Ekind (E) in Assignable_Kind)
5726 when N_Attribute_Reference =>
5727 case Get_Attribute_Id (Attribute_Name (N)) is
5728 when Attribute_Old =>
5730 when Attribute_Result =>
5741 Post_State_Mentioned := True;
5746 end Check_Post_State;
5748 -----------------------------
5749 -- Process_Post_Conditions --
5750 -----------------------------
5752 procedure Process_Post_Conditions
5758 Ignored : Traverse_Final_Result;
5759 pragma Unreferenced (Ignored);
5762 Prag := Spec_PPC_List (Contract (Spec));
5765 Arg := First (Pragma_Argument_Associations (Prag));
5767 -- Since pre- and post-conditions are listed in reverse order, the
5768 -- first postcondition in the list is the last in the source.
5770 if Pragma_Name (Prag) = Name_Postcondition
5772 and then No (Last_Postcondition)
5774 Last_Postcondition := Prag;
5777 -- For functions, look for presence of 'Result in postcondition
5779 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
5780 Ignored := Find_Attribute_Result (Arg);
5783 -- For each individual non-inherited postcondition, look for
5784 -- presence of an expression that could be evaluated differently
5787 if Pragma_Name (Prag) = Name_Postcondition
5790 Post_State_Mentioned := False;
5791 Ignored := Find_Post_State (Arg);
5793 if not Post_State_Mentioned then
5794 Error_Msg_N ("?postcondition refers only to pre-state",
5799 Prag := Next_Pragma (Prag);
5800 exit when No (Prag);
5802 end Process_Post_Conditions;
5804 -- Start of processing for Check_Subprogram_Contract
5807 if not Warn_On_Suspicious_Contract then
5811 if Present (Spec_PPC_List (Contract (Spec_Id))) then
5812 Process_Post_Conditions (Spec_Id, Class => False);
5815 -- Process inherited postconditions
5817 -- Code is currently commented out as, in some cases, it causes crashes
5818 -- because Direct_Primitive_Operations is not available for a private
5819 -- type. This may cause more warnings to be issued than necessary. ???
5821 -- for J in Inherited'Range loop
5822 -- if Present (Spec_PPC_List (Contract (Inherited (J)))) then
5823 -- Process_Post_Conditions (Inherited (J), Class => True);
5827 -- Issue warning for functions whose postcondition does not mention
5828 -- 'Result after all postconditions have been processed.
5830 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
5831 and then Present (Last_Postcondition)
5832 and then not Attribute_Result_Mentioned
5834 Error_Msg_N ("?function postcondition does not mention result",
5835 Last_Postcondition);
5837 end Check_Subprogram_Contract;
5839 ----------------------------
5840 -- Check_Subprogram_Order --
5841 ----------------------------
5843 procedure Check_Subprogram_Order (N : Node_Id) is
5845 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
5846 -- This is used to check if S1 > S2 in the sense required by this test,
5847 -- for example nameab < namec, but name2 < name10.
5849 -----------------------------
5850 -- Subprogram_Name_Greater --
5851 -----------------------------
5853 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
5858 -- Remove trailing numeric parts
5861 while S1 (L1) in '0' .. '9' loop
5866 while S2 (L2) in '0' .. '9' loop
5870 -- If non-numeric parts non-equal, that's decisive
5872 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
5875 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
5878 -- If non-numeric parts equal, compare suffixed numeric parts. Note
5879 -- that a missing suffix is treated as numeric zero in this test.
5883 while L1 < S1'Last loop
5885 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
5889 while L2 < S2'Last loop
5891 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
5896 end Subprogram_Name_Greater;
5898 -- Start of processing for Check_Subprogram_Order
5901 -- Check body in alpha order if this is option
5904 and then Style_Check_Order_Subprograms
5905 and then Nkind (N) = N_Subprogram_Body
5906 and then Comes_From_Source (N)
5907 and then In_Extended_Main_Source_Unit (N)
5911 renames Scope_Stack.Table
5912 (Scope_Stack.Last).Last_Subprogram_Name;
5914 Body_Id : constant Entity_Id :=
5915 Defining_Entity (Specification (N));
5918 Get_Decoded_Name_String (Chars (Body_Id));
5921 if Subprogram_Name_Greater
5922 (LSN.all, Name_Buffer (1 .. Name_Len))
5924 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
5930 LSN := new String'(Name_Buffer (1 .. Name_Len));
5933 end Check_Subprogram_Order;
5935 ------------------------------
5936 -- Check_Subtype_Conformant --
5937 ------------------------------
5939 procedure Check_Subtype_Conformant
5940 (New_Id : Entity_Id;
5942 Err_Loc : Node_Id := Empty;
5943 Skip_Controlling_Formals : Boolean := False)
5946 pragma Warnings (Off, Result);
5949 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
5950 Skip_Controlling_Formals => Skip_Controlling_Formals);
5951 end Check_Subtype_Conformant;
5953 ---------------------------
5954 -- Check_Type_Conformant --
5955 ---------------------------
5957 procedure Check_Type_Conformant
5958 (New_Id : Entity_Id;
5960 Err_Loc : Node_Id := Empty)
5963 pragma Warnings (Off, Result);
5966 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
5967 end Check_Type_Conformant;
5969 ---------------------------
5970 -- Can_Override_Operator --
5971 ---------------------------
5973 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
5976 if Nkind (Subp) /= N_Defining_Operator_Symbol then
5980 Typ := Base_Type (Etype (First_Formal (Subp)));
5982 return Operator_Matches_Spec (Subp, Subp)
5983 and then Scope (Subp) = Scope (Typ)
5984 and then not Is_Class_Wide_Type (Typ);
5986 end Can_Override_Operator;
5988 ----------------------
5989 -- Conforming_Types --
5990 ----------------------
5992 function Conforming_Types
5995 Ctype : Conformance_Type;
5996 Get_Inst : Boolean := False) return Boolean
5998 Type_1 : Entity_Id := T1;
5999 Type_2 : Entity_Id := T2;
6000 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
6002 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
6003 -- If neither T1 nor T2 are generic actual types, or if they are in
6004 -- different scopes (e.g. parent and child instances), then verify that
6005 -- the base types are equal. Otherwise T1 and T2 must be on the same
6006 -- subtype chain. The whole purpose of this procedure is to prevent
6007 -- spurious ambiguities in an instantiation that may arise if two
6008 -- distinct generic types are instantiated with the same actual.
6010 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
6011 -- An access parameter can designate an incomplete type. If the
6012 -- incomplete type is the limited view of a type from a limited_
6013 -- with_clause, check whether the non-limited view is available. If
6014 -- it is a (non-limited) incomplete type, get the full view.
6016 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
6017 -- Returns True if and only if either T1 denotes a limited view of T2
6018 -- or T2 denotes a limited view of T1. This can arise when the limited
6019 -- with view of a type is used in a subprogram declaration and the
6020 -- subprogram body is in the scope of a regular with clause for the
6021 -- same unit. In such a case, the two type entities can be considered
6022 -- identical for purposes of conformance checking.
6024 ----------------------
6025 -- Base_Types_Match --
6026 ----------------------
6028 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
6033 elsif Base_Type (T1) = Base_Type (T2) then
6035 -- The following is too permissive. A more precise test should
6036 -- check that the generic actual is an ancestor subtype of the
6039 return not Is_Generic_Actual_Type (T1)
6040 or else not Is_Generic_Actual_Type (T2)
6041 or else Scope (T1) /= Scope (T2);
6046 end Base_Types_Match;
6048 --------------------------
6049 -- Find_Designated_Type --
6050 --------------------------
6052 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
6056 Desig := Directly_Designated_Type (T);
6058 if Ekind (Desig) = E_Incomplete_Type then
6060 -- If regular incomplete type, get full view if available
6062 if Present (Full_View (Desig)) then
6063 Desig := Full_View (Desig);
6065 -- If limited view of a type, get non-limited view if available,
6066 -- and check again for a regular incomplete type.
6068 elsif Present (Non_Limited_View (Desig)) then
6069 Desig := Get_Full_View (Non_Limited_View (Desig));
6074 end Find_Designated_Type;
6076 -------------------------------
6077 -- Matches_Limited_With_View --
6078 -------------------------------
6080 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
6082 -- In some cases a type imported through a limited_with clause, and
6083 -- its nonlimited view are both visible, for example in an anonymous
6084 -- access-to-class-wide type in a formal. Both entities designate the
6087 if From_With_Type (T1)
6088 and then T2 = Available_View (T1)
6092 elsif From_With_Type (T2)
6093 and then T1 = Available_View (T2)
6097 elsif From_With_Type (T1)
6098 and then From_With_Type (T2)
6099 and then Available_View (T1) = Available_View (T2)
6106 end Matches_Limited_With_View;
6108 -- Start of processing for Conforming_Types
6111 -- The context is an instance association for a formal
6112 -- access-to-subprogram type; the formal parameter types require
6113 -- mapping because they may denote other formal parameters of the
6117 Type_1 := Get_Instance_Of (T1);
6118 Type_2 := Get_Instance_Of (T2);
6121 -- If one of the types is a view of the other introduced by a limited
6122 -- with clause, treat these as conforming for all purposes.
6124 if Matches_Limited_With_View (T1, T2) then
6127 elsif Base_Types_Match (Type_1, Type_2) then
6128 return Ctype <= Mode_Conformant
6129 or else Subtypes_Statically_Match (Type_1, Type_2);
6131 elsif Is_Incomplete_Or_Private_Type (Type_1)
6132 and then Present (Full_View (Type_1))
6133 and then Base_Types_Match (Full_View (Type_1), Type_2)
6135 return Ctype <= Mode_Conformant
6136 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
6138 elsif Ekind (Type_2) = E_Incomplete_Type
6139 and then Present (Full_View (Type_2))
6140 and then Base_Types_Match (Type_1, Full_View (Type_2))
6142 return Ctype <= Mode_Conformant
6143 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6145 elsif Is_Private_Type (Type_2)
6146 and then In_Instance
6147 and then Present (Full_View (Type_2))
6148 and then Base_Types_Match (Type_1, Full_View (Type_2))
6150 return Ctype <= Mode_Conformant
6151 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
6154 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
6155 -- treated recursively because they carry a signature.
6157 Are_Anonymous_Access_To_Subprogram_Types :=
6158 Ekind (Type_1) = Ekind (Type_2)
6160 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
6162 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
6164 -- Test anonymous access type case. For this case, static subtype
6165 -- matching is required for mode conformance (RM 6.3.1(15)). We check
6166 -- the base types because we may have built internal subtype entities
6167 -- to handle null-excluding types (see Process_Formals).
6169 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
6171 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
6172 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
6175 Desig_1 : Entity_Id;
6176 Desig_2 : Entity_Id;
6179 -- In Ada 2005, access constant indicators must match for
6180 -- subtype conformance.
6182 if Ada_Version >= Ada_2005
6183 and then Ctype >= Subtype_Conformant
6185 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
6190 Desig_1 := Find_Designated_Type (Type_1);
6191 Desig_2 := Find_Designated_Type (Type_2);
6193 -- If the context is an instance association for a formal
6194 -- access-to-subprogram type; formal access parameter designated
6195 -- types require mapping because they may denote other formal
6196 -- parameters of the generic unit.
6199 Desig_1 := Get_Instance_Of (Desig_1);
6200 Desig_2 := Get_Instance_Of (Desig_2);
6203 -- It is possible for a Class_Wide_Type to be introduced for an
6204 -- incomplete type, in which case there is a separate class_ wide
6205 -- type for the full view. The types conform if their Etypes
6206 -- conform, i.e. one may be the full view of the other. This can
6207 -- only happen in the context of an access parameter, other uses
6208 -- of an incomplete Class_Wide_Type are illegal.
6210 if Is_Class_Wide_Type (Desig_1)
6212 Is_Class_Wide_Type (Desig_2)
6216 (Etype (Base_Type (Desig_1)),
6217 Etype (Base_Type (Desig_2)), Ctype);
6219 elsif Are_Anonymous_Access_To_Subprogram_Types then
6220 if Ada_Version < Ada_2005 then
6221 return Ctype = Type_Conformant
6223 Subtypes_Statically_Match (Desig_1, Desig_2);
6225 -- We must check the conformance of the signatures themselves
6229 Conformant : Boolean;
6232 (Desig_1, Desig_2, Ctype, False, Conformant);
6238 return Base_Type (Desig_1) = Base_Type (Desig_2)
6239 and then (Ctype = Type_Conformant
6241 Subtypes_Statically_Match (Desig_1, Desig_2));
6245 -- Otherwise definitely no match
6248 if ((Ekind (Type_1) = E_Anonymous_Access_Type
6249 and then Is_Access_Type (Type_2))
6250 or else (Ekind (Type_2) = E_Anonymous_Access_Type
6251 and then Is_Access_Type (Type_1)))
6254 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
6256 May_Hide_Profile := True;
6261 end Conforming_Types;
6263 --------------------------
6264 -- Create_Extra_Formals --
6265 --------------------------
6267 procedure Create_Extra_Formals (E : Entity_Id) is
6269 First_Extra : Entity_Id := Empty;
6270 Last_Extra : Entity_Id;
6271 Formal_Type : Entity_Id;
6272 P_Formal : Entity_Id := Empty;
6274 function Add_Extra_Formal
6275 (Assoc_Entity : Entity_Id;
6278 Suffix : String) return Entity_Id;
6279 -- Add an extra formal to the current list of formals and extra formals.
6280 -- The extra formal is added to the end of the list of extra formals,
6281 -- and also returned as the result. These formals are always of mode IN.
6282 -- The new formal has the type Typ, is declared in Scope, and its name
6283 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
6284 -- The following suffixes are currently used. They should not be changed
6285 -- without coordinating with CodePeer, which makes use of these to
6286 -- provide better messages.
6288 -- O denotes the Constrained bit.
6289 -- L denotes the accessibility level.
6290 -- BIP_xxx denotes an extra formal for a build-in-place function. See
6291 -- the full list in exp_ch6.BIP_Formal_Kind.
6293 ----------------------
6294 -- Add_Extra_Formal --
6295 ----------------------
6297 function Add_Extra_Formal
6298 (Assoc_Entity : Entity_Id;
6301 Suffix : String) return Entity_Id
6303 EF : constant Entity_Id :=
6304 Make_Defining_Identifier (Sloc (Assoc_Entity),
6305 Chars => New_External_Name (Chars (Assoc_Entity),
6309 -- A little optimization. Never generate an extra formal for the
6310 -- _init operand of an initialization procedure, since it could
6313 if Chars (Formal) = Name_uInit then
6317 Set_Ekind (EF, E_In_Parameter);
6318 Set_Actual_Subtype (EF, Typ);
6319 Set_Etype (EF, Typ);
6320 Set_Scope (EF, Scope);
6321 Set_Mechanism (EF, Default_Mechanism);
6322 Set_Formal_Validity (EF);
6324 if No (First_Extra) then
6326 Set_Extra_Formals (Scope, First_Extra);
6329 if Present (Last_Extra) then
6330 Set_Extra_Formal (Last_Extra, EF);
6336 end Add_Extra_Formal;
6338 -- Start of processing for Create_Extra_Formals
6341 -- We never generate extra formals if expansion is not active
6342 -- because we don't need them unless we are generating code.
6344 if not Expander_Active then
6348 -- If this is a derived subprogram then the subtypes of the parent
6349 -- subprogram's formal parameters will be used to determine the need
6350 -- for extra formals.
6352 if Is_Overloadable (E) and then Present (Alias (E)) then
6353 P_Formal := First_Formal (Alias (E));
6356 Last_Extra := Empty;
6357 Formal := First_Formal (E);
6358 while Present (Formal) loop
6359 Last_Extra := Formal;
6360 Next_Formal (Formal);
6363 -- If Extra_formals were already created, don't do it again. This
6364 -- situation may arise for subprogram types created as part of
6365 -- dispatching calls (see Expand_Dispatching_Call)
6367 if Present (Last_Extra) and then
6368 Present (Extra_Formal (Last_Extra))
6373 -- If the subprogram is a predefined dispatching subprogram then don't
6374 -- generate any extra constrained or accessibility level formals. In
6375 -- general we suppress these for internal subprograms (by not calling
6376 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
6377 -- generated stream attributes do get passed through because extra
6378 -- build-in-place formals are needed in some cases (limited 'Input).
6380 if Is_Predefined_Internal_Operation (E) then
6381 goto Test_For_Func_Result_Extras;
6384 Formal := First_Formal (E);
6385 while Present (Formal) loop
6387 -- Create extra formal for supporting the attribute 'Constrained.
6388 -- The case of a private type view without discriminants also
6389 -- requires the extra formal if the underlying type has defaulted
6392 if Ekind (Formal) /= E_In_Parameter then
6393 if Present (P_Formal) then
6394 Formal_Type := Etype (P_Formal);
6396 Formal_Type := Etype (Formal);
6399 -- Do not produce extra formals for Unchecked_Union parameters.
6400 -- Jump directly to the end of the loop.
6402 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
6403 goto Skip_Extra_Formal_Generation;
6406 if not Has_Discriminants (Formal_Type)
6407 and then Ekind (Formal_Type) in Private_Kind
6408 and then Present (Underlying_Type (Formal_Type))
6410 Formal_Type := Underlying_Type (Formal_Type);
6413 -- Suppress the extra formal if formal's subtype is constrained or
6414 -- indefinite, or we're compiling for Ada 2012 and the underlying
6415 -- type is tagged and limited. In Ada 2012, a limited tagged type
6416 -- can have defaulted discriminants, but 'Constrained is required
6417 -- to return True, so the formal is never needed (see AI05-0214).
6418 -- Note that this ensures consistency of calling sequences for
6419 -- dispatching operations when some types in a class have defaults
6420 -- on discriminants and others do not (and requiring the extra
6421 -- formal would introduce distributed overhead).
6423 if Has_Discriminants (Formal_Type)
6424 and then not Is_Constrained (Formal_Type)
6425 and then not Is_Indefinite_Subtype (Formal_Type)
6426 and then (Ada_Version < Ada_2012
6428 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
6429 and then Is_Limited_Type (Formal_Type)))
6431 Set_Extra_Constrained
6432 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
6436 -- Create extra formal for supporting accessibility checking. This
6437 -- is done for both anonymous access formals and formals of named
6438 -- access types that are marked as controlling formals. The latter
6439 -- case can occur when Expand_Dispatching_Call creates a subprogram
6440 -- type and substitutes the types of access-to-class-wide actuals
6441 -- for the anonymous access-to-specific-type of controlling formals.
6442 -- Base_Type is applied because in cases where there is a null
6443 -- exclusion the formal may have an access subtype.
6445 -- This is suppressed if we specifically suppress accessibility
6446 -- checks at the package level for either the subprogram, or the
6447 -- package in which it resides. However, we do not suppress it
6448 -- simply if the scope has accessibility checks suppressed, since
6449 -- this could cause trouble when clients are compiled with a
6450 -- different suppression setting. The explicit checks at the
6451 -- package level are safe from this point of view.
6453 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
6454 or else (Is_Controlling_Formal (Formal)
6455 and then Is_Access_Type (Base_Type (Etype (Formal)))))
6457 (Explicit_Suppress (E, Accessibility_Check)
6459 Explicit_Suppress (Scope (E), Accessibility_Check))
6462 or else Present (Extra_Accessibility (P_Formal)))
6464 Set_Extra_Accessibility
6465 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
6468 -- This label is required when skipping extra formal generation for
6469 -- Unchecked_Union parameters.
6471 <<Skip_Extra_Formal_Generation>>
6473 if Present (P_Formal) then
6474 Next_Formal (P_Formal);
6477 Next_Formal (Formal);
6480 <<Test_For_Func_Result_Extras>>
6482 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
6483 -- function call is ... determined by the point of call ...".
6485 if Needs_Result_Accessibility_Level (E) then
6486 Set_Extra_Accessibility_Of_Result
6487 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
6490 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
6491 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
6493 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
6495 Result_Subt : constant Entity_Id := Etype (E);
6496 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
6497 Formal_Typ : Entity_Id;
6499 Discard : Entity_Id;
6500 pragma Warnings (Off, Discard);
6503 -- In the case of functions with unconstrained result subtypes,
6504 -- add a 4-state formal indicating whether the return object is
6505 -- allocated by the caller (1), or should be allocated by the
6506 -- callee on the secondary stack (2), in the global heap (3), or
6507 -- in a user-defined storage pool (4). For the moment we just use
6508 -- Natural for the type of this formal. Note that this formal
6509 -- isn't usually needed in the case where the result subtype is
6510 -- constrained, but it is needed when the function has a tagged
6511 -- result, because generally such functions can be called in a
6512 -- dispatching context and such calls must be handled like calls
6513 -- to a class-wide function.
6515 if Needs_BIP_Alloc_Form (E) then
6518 (E, Standard_Natural,
6519 E, BIP_Formal_Suffix (BIP_Alloc_Form));
6521 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
6522 -- use a user-defined pool. This formal is not added on
6523 -- .NET/JVM/ZFP as those targets do not support pools.
6525 if VM_Target = No_VM
6526 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
6530 (E, RTE (RE_Root_Storage_Pool_Ptr),
6531 E, BIP_Formal_Suffix (BIP_Storage_Pool));
6535 -- In the case of functions whose result type needs finalization,
6536 -- add an extra formal which represents the finalization master.
6538 if Needs_BIP_Finalization_Master (E) then
6541 (E, RTE (RE_Finalization_Master_Ptr),
6542 E, BIP_Formal_Suffix (BIP_Finalization_Master));
6545 -- When the result type contains tasks, add two extra formals: the
6546 -- master of the tasks to be created, and the caller's activation
6549 if Has_Task (Full_Subt) then
6552 (E, RTE (RE_Master_Id),
6553 E, BIP_Formal_Suffix (BIP_Task_Master));
6556 (E, RTE (RE_Activation_Chain_Access),
6557 E, BIP_Formal_Suffix (BIP_Activation_Chain));
6560 -- All build-in-place functions get an extra formal that will be
6561 -- passed the address of the return object within the caller.
6564 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
6566 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
6567 Set_Etype (Formal_Typ, Formal_Typ);
6568 Set_Depends_On_Private
6569 (Formal_Typ, Has_Private_Component (Formal_Typ));
6570 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
6571 Set_Is_Access_Constant (Formal_Typ, False);
6573 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
6574 -- the designated type comes from the limited view (for back-end
6577 Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt));
6579 Layout_Type (Formal_Typ);
6583 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
6586 end Create_Extra_Formals;
6588 -----------------------------
6589 -- Enter_Overloaded_Entity --
6590 -----------------------------
6592 procedure Enter_Overloaded_Entity (S : Entity_Id) is
6593 E : Entity_Id := Current_Entity_In_Scope (S);
6594 C_E : Entity_Id := Current_Entity (S);
6598 Set_Has_Homonym (E);
6599 Set_Has_Homonym (S);
6602 Set_Is_Immediately_Visible (S);
6603 Set_Scope (S, Current_Scope);
6605 -- Chain new entity if front of homonym in current scope, so that
6606 -- homonyms are contiguous.
6611 while Homonym (C_E) /= E loop
6612 C_E := Homonym (C_E);
6615 Set_Homonym (C_E, S);
6619 Set_Current_Entity (S);
6624 Append_Entity (S, Current_Scope);
6625 Set_Public_Status (S);
6627 if Debug_Flag_E then
6628 Write_Str ("New overloaded entity chain: ");
6629 Write_Name (Chars (S));
6632 while Present (E) loop
6633 Write_Str (" "); Write_Int (Int (E));
6640 -- Generate warning for hiding
6643 and then Comes_From_Source (S)
6644 and then In_Extended_Main_Source_Unit (S)
6651 -- Warn unless genuine overloading. Do not emit warning on
6652 -- hiding predefined operators in Standard (these are either an
6653 -- (artifact of our implicit declarations, or simple noise) but
6654 -- keep warning on a operator defined on a local subtype, because
6655 -- of the real danger that different operators may be applied in
6656 -- various parts of the program.
6658 -- Note that if E and S have the same scope, there is never any
6659 -- hiding. Either the two conflict, and the program is illegal,
6660 -- or S is overriding an implicit inherited subprogram.
6662 if Scope (E) /= Scope (S)
6663 and then (not Is_Overloadable (E)
6664 or else Subtype_Conformant (E, S))
6665 and then (Is_Immediately_Visible (E)
6667 Is_Potentially_Use_Visible (S))
6669 if Scope (E) /= Standard_Standard then
6670 Error_Msg_Sloc := Sloc (E);
6671 Error_Msg_N ("declaration of & hides one#?", S);
6673 elsif Nkind (S) = N_Defining_Operator_Symbol
6675 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
6678 ("declaration of & hides predefined operator?", S);
6683 end Enter_Overloaded_Entity;
6685 -----------------------------
6686 -- Check_Untagged_Equality --
6687 -----------------------------
6689 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
6690 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
6691 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
6695 if Nkind (Decl) = N_Subprogram_Declaration
6696 and then Is_Record_Type (Typ)
6697 and then not Is_Tagged_Type (Typ)
6699 -- If the type is not declared in a package, or if we are in the
6700 -- body of the package or in some other scope, the new operation is
6701 -- not primitive, and therefore legal, though suspicious. If the
6702 -- type is a generic actual (sub)type, the operation is not primitive
6703 -- either because the base type is declared elsewhere.
6705 if Is_Frozen (Typ) then
6706 if Ekind (Scope (Typ)) /= E_Package
6707 or else Scope (Typ) /= Current_Scope
6711 elsif Is_Generic_Actual_Type (Typ) then
6714 elsif In_Package_Body (Scope (Typ)) then
6716 ("equality operator must be declared "
6717 & "before type& is frozen", Eq_Op, Typ);
6719 ("\move declaration to package spec", Eq_Op);
6723 ("equality operator must be declared "
6724 & "before type& is frozen", Eq_Op, Typ);
6726 Obj_Decl := Next (Parent (Typ));
6727 while Present (Obj_Decl)
6728 and then Obj_Decl /= Decl
6730 if Nkind (Obj_Decl) = N_Object_Declaration
6731 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
6733 Error_Msg_NE ("type& is frozen by declaration?",
6736 ("\an equality operator cannot be declared after this "
6737 & "point (RM 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
6745 elsif not In_Same_List (Parent (Typ), Decl)
6746 and then not Is_Limited_Type (Typ)
6749 -- This makes it illegal to have a primitive equality declared in
6750 -- the private part if the type is visible.
6752 Error_Msg_N ("equality operator appears too late", Eq_Op);
6755 end Check_Untagged_Equality;
6757 -----------------------------
6758 -- Find_Corresponding_Spec --
6759 -----------------------------
6761 function Find_Corresponding_Spec
6763 Post_Error : Boolean := True) return Entity_Id
6765 Spec : constant Node_Id := Specification (N);
6766 Designator : constant Entity_Id := Defining_Entity (Spec);
6771 E := Current_Entity (Designator);
6772 while Present (E) loop
6774 -- We are looking for a matching spec. It must have the same scope,
6775 -- and the same name, and either be type conformant, or be the case
6776 -- of a library procedure spec and its body (which belong to one
6777 -- another regardless of whether they are type conformant or not).
6779 if Scope (E) = Current_Scope then
6780 if Current_Scope = Standard_Standard
6781 or else (Ekind (E) = Ekind (Designator)
6782 and then Type_Conformant (E, Designator))
6784 -- Within an instantiation, we know that spec and body are
6785 -- subtype conformant, because they were subtype conformant
6786 -- in the generic. We choose the subtype-conformant entity
6787 -- here as well, to resolve spurious ambiguities in the
6788 -- instance that were not present in the generic (i.e. when
6789 -- two different types are given the same actual). If we are
6790 -- looking for a spec to match a body, full conformance is
6794 Set_Convention (Designator, Convention (E));
6796 -- Skip past subprogram bodies and subprogram renamings that
6797 -- may appear to have a matching spec, but that aren't fully
6798 -- conformant with it. That can occur in cases where an
6799 -- actual type causes unrelated homographs in the instance.
6801 if Nkind_In (N, N_Subprogram_Body,
6802 N_Subprogram_Renaming_Declaration)
6803 and then Present (Homonym (E))
6804 and then not Fully_Conformant (Designator, E)
6808 elsif not Subtype_Conformant (Designator, E) then
6813 -- Ada 2012 (AI05-0165): For internally generated bodies of
6814 -- null procedures locate the internally generated spec. We
6815 -- enforce mode conformance since a tagged type may inherit
6816 -- from interfaces several null primitives which differ only
6817 -- in the mode of the formals.
6819 if not (Comes_From_Source (E))
6820 and then Is_Null_Procedure (E)
6821 and then not Mode_Conformant (Designator, E)
6825 elsif not Has_Completion (E) then
6826 if Nkind (N) /= N_Subprogram_Body_Stub then
6827 Set_Corresponding_Spec (N, E);
6830 Set_Has_Completion (E);
6833 elsif Nkind (Parent (N)) = N_Subunit then
6835 -- If this is the proper body of a subunit, the completion
6836 -- flag is set when analyzing the stub.
6840 -- If E is an internal function with a controlling result
6841 -- that was created for an operation inherited by a null
6842 -- extension, it may be overridden by a body without a previous
6843 -- spec (one more reason why these should be shunned). In that
6844 -- case remove the generated body if present, because the
6845 -- current one is the explicit overriding.
6847 elsif Ekind (E) = E_Function
6848 and then Ada_Version >= Ada_2005
6849 and then not Comes_From_Source (E)
6850 and then Has_Controlling_Result (E)
6851 and then Is_Null_Extension (Etype (E))
6852 and then Comes_From_Source (Spec)
6854 Set_Has_Completion (E, False);
6857 and then Nkind (Parent (E)) = N_Function_Specification
6860 (Unit_Declaration_Node
6861 (Corresponding_Body (Unit_Declaration_Node (E))));
6865 -- If expansion is disabled, or if the wrapper function has
6866 -- not been generated yet, this a late body overriding an
6867 -- inherited operation, or it is an overriding by some other
6868 -- declaration before the controlling result is frozen. In
6869 -- either case this is a declaration of a new entity.
6875 -- If the body already exists, then this is an error unless
6876 -- the previous declaration is the implicit declaration of a
6877 -- derived subprogram. It is also legal for an instance to
6878 -- contain type conformant overloadable declarations (but the
6879 -- generic declaration may not), per 8.3(26/2).
6881 elsif No (Alias (E))
6882 and then not Is_Intrinsic_Subprogram (E)
6883 and then not In_Instance
6886 Error_Msg_Sloc := Sloc (E);
6888 if Is_Imported (E) then
6890 ("body not allowed for imported subprogram & declared#",
6893 Error_Msg_NE ("duplicate body for & declared#", N, E);
6897 -- Child units cannot be overloaded, so a conformance mismatch
6898 -- between body and a previous spec is an error.
6900 elsif Is_Child_Unit (E)
6902 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
6904 Nkind (Parent (Unit_Declaration_Node (Designator))) =
6909 ("body of child unit does not match previous declaration", N);
6917 -- On exit, we know that no previous declaration of subprogram exists
6920 end Find_Corresponding_Spec;
6922 ----------------------
6923 -- Fully_Conformant --
6924 ----------------------
6926 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6929 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
6931 end Fully_Conformant;
6933 ----------------------------------
6934 -- Fully_Conformant_Expressions --
6935 ----------------------------------
6937 function Fully_Conformant_Expressions
6938 (Given_E1 : Node_Id;
6939 Given_E2 : Node_Id) return Boolean
6941 E1 : constant Node_Id := Original_Node (Given_E1);
6942 E2 : constant Node_Id := Original_Node (Given_E2);
6943 -- We always test conformance on original nodes, since it is possible
6944 -- for analysis and/or expansion to make things look as though they
6945 -- conform when they do not, e.g. by converting 1+2 into 3.
6947 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6948 renames Fully_Conformant_Expressions;
6950 function FCL (L1, L2 : List_Id) return Boolean;
6951 -- Compare elements of two lists for conformance. Elements have to
6952 -- be conformant, and actuals inserted as default parameters do not
6953 -- match explicit actuals with the same value.
6955 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6956 -- Compare an operator node with a function call
6962 function FCL (L1, L2 : List_Id) return Boolean is
6966 if L1 = No_List then
6972 if L2 = No_List then
6978 -- Compare two lists, skipping rewrite insertions (we want to
6979 -- compare the original trees, not the expanded versions!)
6982 if Is_Rewrite_Insertion (N1) then
6984 elsif Is_Rewrite_Insertion (N2) then
6990 elsif not FCE (N1, N2) then
7003 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
7004 Actuals : constant List_Id := Parameter_Associations (Call_Node);
7009 or else Entity (Op_Node) /= Entity (Name (Call_Node))
7014 Act := First (Actuals);
7016 if Nkind (Op_Node) in N_Binary_Op then
7017 if not FCE (Left_Opnd (Op_Node), Act) then
7024 return Present (Act)
7025 and then FCE (Right_Opnd (Op_Node), Act)
7026 and then No (Next (Act));
7030 -- Start of processing for Fully_Conformant_Expressions
7033 -- Non-conformant if paren count does not match. Note: if some idiot
7034 -- complains that we don't do this right for more than 3 levels of
7035 -- parentheses, they will be treated with the respect they deserve!
7037 if Paren_Count (E1) /= Paren_Count (E2) then
7040 -- If same entities are referenced, then they are conformant even if
7041 -- they have different forms (RM 8.3.1(19-20)).
7043 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
7044 if Present (Entity (E1)) then
7045 return Entity (E1) = Entity (E2)
7046 or else (Chars (Entity (E1)) = Chars (Entity (E2))
7047 and then Ekind (Entity (E1)) = E_Discriminant
7048 and then Ekind (Entity (E2)) = E_In_Parameter);
7050 elsif Nkind (E1) = N_Expanded_Name
7051 and then Nkind (E2) = N_Expanded_Name
7052 and then Nkind (Selector_Name (E1)) = N_Character_Literal
7053 and then Nkind (Selector_Name (E2)) = N_Character_Literal
7055 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
7058 -- Identifiers in component associations don't always have
7059 -- entities, but their names must conform.
7061 return Nkind (E1) = N_Identifier
7062 and then Nkind (E2) = N_Identifier
7063 and then Chars (E1) = Chars (E2);
7066 elsif Nkind (E1) = N_Character_Literal
7067 and then Nkind (E2) = N_Expanded_Name
7069 return Nkind (Selector_Name (E2)) = N_Character_Literal
7070 and then Chars (E1) = Chars (Selector_Name (E2));
7072 elsif Nkind (E2) = N_Character_Literal
7073 and then Nkind (E1) = N_Expanded_Name
7075 return Nkind (Selector_Name (E1)) = N_Character_Literal
7076 and then Chars (E2) = Chars (Selector_Name (E1));
7078 elsif Nkind (E1) in N_Op
7079 and then Nkind (E2) = N_Function_Call
7081 return FCO (E1, E2);
7083 elsif Nkind (E2) in N_Op
7084 and then Nkind (E1) = N_Function_Call
7086 return FCO (E2, E1);
7088 -- Otherwise we must have the same syntactic entity
7090 elsif Nkind (E1) /= Nkind (E2) then
7093 -- At this point, we specialize by node type
7100 FCL (Expressions (E1), Expressions (E2))
7102 FCL (Component_Associations (E1),
7103 Component_Associations (E2));
7106 if Nkind (Expression (E1)) = N_Qualified_Expression
7108 Nkind (Expression (E2)) = N_Qualified_Expression
7110 return FCE (Expression (E1), Expression (E2));
7112 -- Check that the subtype marks and any constraints
7117 Indic1 : constant Node_Id := Expression (E1);
7118 Indic2 : constant Node_Id := Expression (E2);
7123 if Nkind (Indic1) /= N_Subtype_Indication then
7125 Nkind (Indic2) /= N_Subtype_Indication
7126 and then Entity (Indic1) = Entity (Indic2);
7128 elsif Nkind (Indic2) /= N_Subtype_Indication then
7130 Nkind (Indic1) /= N_Subtype_Indication
7131 and then Entity (Indic1) = Entity (Indic2);
7134 if Entity (Subtype_Mark (Indic1)) /=
7135 Entity (Subtype_Mark (Indic2))
7140 Elt1 := First (Constraints (Constraint (Indic1)));
7141 Elt2 := First (Constraints (Constraint (Indic2)));
7142 while Present (Elt1) and then Present (Elt2) loop
7143 if not FCE (Elt1, Elt2) then
7156 when N_Attribute_Reference =>
7158 Attribute_Name (E1) = Attribute_Name (E2)
7159 and then FCL (Expressions (E1), Expressions (E2));
7163 Entity (E1) = Entity (E2)
7164 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
7165 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
7167 when N_Short_Circuit | N_Membership_Test =>
7169 FCE (Left_Opnd (E1), Left_Opnd (E2))
7171 FCE (Right_Opnd (E1), Right_Opnd (E2));
7173 when N_Case_Expression =>
7179 if not FCE (Expression (E1), Expression (E2)) then
7183 Alt1 := First (Alternatives (E1));
7184 Alt2 := First (Alternatives (E2));
7186 if Present (Alt1) /= Present (Alt2) then
7188 elsif No (Alt1) then
7192 if not FCE (Expression (Alt1), Expression (Alt2))
7193 or else not FCL (Discrete_Choices (Alt1),
7194 Discrete_Choices (Alt2))
7205 when N_Character_Literal =>
7207 Char_Literal_Value (E1) = Char_Literal_Value (E2);
7209 when N_Component_Association =>
7211 FCL (Choices (E1), Choices (E2))
7213 FCE (Expression (E1), Expression (E2));
7215 when N_Conditional_Expression =>
7217 FCL (Expressions (E1), Expressions (E2));
7219 when N_Explicit_Dereference =>
7221 FCE (Prefix (E1), Prefix (E2));
7223 when N_Extension_Aggregate =>
7225 FCL (Expressions (E1), Expressions (E2))
7226 and then Null_Record_Present (E1) =
7227 Null_Record_Present (E2)
7228 and then FCL (Component_Associations (E1),
7229 Component_Associations (E2));
7231 when N_Function_Call =>
7233 FCE (Name (E1), Name (E2))
7235 FCL (Parameter_Associations (E1),
7236 Parameter_Associations (E2));
7238 when N_Indexed_Component =>
7240 FCE (Prefix (E1), Prefix (E2))
7242 FCL (Expressions (E1), Expressions (E2));
7244 when N_Integer_Literal =>
7245 return (Intval (E1) = Intval (E2));
7250 when N_Operator_Symbol =>
7252 Chars (E1) = Chars (E2);
7254 when N_Others_Choice =>
7257 when N_Parameter_Association =>
7259 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
7260 and then FCE (Explicit_Actual_Parameter (E1),
7261 Explicit_Actual_Parameter (E2));
7263 when N_Qualified_Expression =>
7265 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7267 FCE (Expression (E1), Expression (E2));
7269 when N_Quantified_Expression =>
7270 if not FCE (Condition (E1), Condition (E2)) then
7274 if Present (Loop_Parameter_Specification (E1))
7275 and then Present (Loop_Parameter_Specification (E2))
7278 L1 : constant Node_Id :=
7279 Loop_Parameter_Specification (E1);
7280 L2 : constant Node_Id :=
7281 Loop_Parameter_Specification (E2);
7285 Reverse_Present (L1) = Reverse_Present (L2)
7287 FCE (Defining_Identifier (L1),
7288 Defining_Identifier (L2))
7290 FCE (Discrete_Subtype_Definition (L1),
7291 Discrete_Subtype_Definition (L2));
7294 else -- quantified expression with an iterator
7296 I1 : constant Node_Id := Iterator_Specification (E1);
7297 I2 : constant Node_Id := Iterator_Specification (E2);
7301 FCE (Defining_Identifier (I1),
7302 Defining_Identifier (I2))
7304 Of_Present (I1) = Of_Present (I2)
7306 Reverse_Present (I1) = Reverse_Present (I2)
7307 and then FCE (Name (I1), Name (I2))
7308 and then FCE (Subtype_Indication (I1),
7309 Subtype_Indication (I2));
7315 FCE (Low_Bound (E1), Low_Bound (E2))
7317 FCE (High_Bound (E1), High_Bound (E2));
7319 when N_Real_Literal =>
7320 return (Realval (E1) = Realval (E2));
7322 when N_Selected_Component =>
7324 FCE (Prefix (E1), Prefix (E2))
7326 FCE (Selector_Name (E1), Selector_Name (E2));
7330 FCE (Prefix (E1), Prefix (E2))
7332 FCE (Discrete_Range (E1), Discrete_Range (E2));
7334 when N_String_Literal =>
7336 S1 : constant String_Id := Strval (E1);
7337 S2 : constant String_Id := Strval (E2);
7338 L1 : constant Nat := String_Length (S1);
7339 L2 : constant Nat := String_Length (S2);
7346 for J in 1 .. L1 loop
7347 if Get_String_Char (S1, J) /=
7348 Get_String_Char (S2, J)
7358 when N_Type_Conversion =>
7360 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7362 FCE (Expression (E1), Expression (E2));
7366 Entity (E1) = Entity (E2)
7368 FCE (Right_Opnd (E1), Right_Opnd (E2));
7370 when N_Unchecked_Type_Conversion =>
7372 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
7374 FCE (Expression (E1), Expression (E2));
7376 -- All other node types cannot appear in this context. Strictly
7377 -- we should raise a fatal internal error. Instead we just ignore
7378 -- the nodes. This means that if anyone makes a mistake in the
7379 -- expander and mucks an expression tree irretrievably, the
7380 -- result will be a failure to detect a (probably very obscure)
7381 -- case of non-conformance, which is better than bombing on some
7382 -- case where two expressions do in fact conform.
7389 end Fully_Conformant_Expressions;
7391 ----------------------------------------
7392 -- Fully_Conformant_Discrete_Subtypes --
7393 ----------------------------------------
7395 function Fully_Conformant_Discrete_Subtypes
7396 (Given_S1 : Node_Id;
7397 Given_S2 : Node_Id) return Boolean
7399 S1 : constant Node_Id := Original_Node (Given_S1);
7400 S2 : constant Node_Id := Original_Node (Given_S2);
7402 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
7403 -- Special-case for a bound given by a discriminant, which in the body
7404 -- is replaced with the discriminal of the enclosing type.
7406 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
7407 -- Check both bounds
7409 -----------------------
7410 -- Conforming_Bounds --
7411 -----------------------
7413 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
7415 if Is_Entity_Name (B1)
7416 and then Is_Entity_Name (B2)
7417 and then Ekind (Entity (B1)) = E_Discriminant
7419 return Chars (B1) = Chars (B2);
7422 return Fully_Conformant_Expressions (B1, B2);
7424 end Conforming_Bounds;
7426 -----------------------
7427 -- Conforming_Ranges --
7428 -----------------------
7430 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
7433 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
7435 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
7436 end Conforming_Ranges;
7438 -- Start of processing for Fully_Conformant_Discrete_Subtypes
7441 if Nkind (S1) /= Nkind (S2) then
7444 elsif Is_Entity_Name (S1) then
7445 return Entity (S1) = Entity (S2);
7447 elsif Nkind (S1) = N_Range then
7448 return Conforming_Ranges (S1, S2);
7450 elsif Nkind (S1) = N_Subtype_Indication then
7452 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
7455 (Range_Expression (Constraint (S1)),
7456 Range_Expression (Constraint (S2)));
7460 end Fully_Conformant_Discrete_Subtypes;
7462 --------------------
7463 -- Install_Entity --
7464 --------------------
7466 procedure Install_Entity (E : Entity_Id) is
7467 Prev : constant Entity_Id := Current_Entity (E);
7469 Set_Is_Immediately_Visible (E);
7470 Set_Current_Entity (E);
7471 Set_Homonym (E, Prev);
7474 ---------------------
7475 -- Install_Formals --
7476 ---------------------
7478 procedure Install_Formals (Id : Entity_Id) is
7481 F := First_Formal (Id);
7482 while Present (F) loop
7486 end Install_Formals;
7488 -----------------------------
7489 -- Is_Interface_Conformant --
7490 -----------------------------
7492 function Is_Interface_Conformant
7493 (Tagged_Type : Entity_Id;
7494 Iface_Prim : Entity_Id;
7495 Prim : Entity_Id) return Boolean
7497 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
7498 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
7500 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
7501 -- Return the controlling formal of Prim
7503 ------------------------
7504 -- Controlling_Formal --
7505 ------------------------
7507 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
7508 E : Entity_Id := First_Entity (Prim);
7511 while Present (E) loop
7512 if Is_Formal (E) and then Is_Controlling_Formal (E) then
7520 end Controlling_Formal;
7524 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
7525 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
7527 -- Start of processing for Is_Interface_Conformant
7530 pragma Assert (Is_Subprogram (Iface_Prim)
7531 and then Is_Subprogram (Prim)
7532 and then Is_Dispatching_Operation (Iface_Prim)
7533 and then Is_Dispatching_Operation (Prim));
7535 pragma Assert (Is_Interface (Iface)
7536 or else (Present (Alias (Iface_Prim))
7539 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
7541 if Prim = Iface_Prim
7542 or else not Is_Subprogram (Prim)
7543 or else Ekind (Prim) /= Ekind (Iface_Prim)
7544 or else not Is_Dispatching_Operation (Prim)
7545 or else Scope (Prim) /= Scope (Tagged_Type)
7547 or else Base_Type (Typ) /= Tagged_Type
7548 or else not Primitive_Names_Match (Iface_Prim, Prim)
7552 -- The mode of the controlling formals must match
7554 elsif Present (Iface_Ctrl_F)
7555 and then Present (Prim_Ctrl_F)
7556 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
7560 -- Case of a procedure, or a function whose result type matches the
7561 -- result type of the interface primitive, or a function that has no
7562 -- controlling result (I or access I).
7564 elsif Ekind (Iface_Prim) = E_Procedure
7565 or else Etype (Prim) = Etype (Iface_Prim)
7566 or else not Has_Controlling_Result (Prim)
7568 return Type_Conformant
7569 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
7571 -- Case of a function returning an interface, or an access to one.
7572 -- Check that the return types correspond.
7574 elsif Implements_Interface (Typ, Iface) then
7575 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
7577 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
7582 Type_Conformant (Prim, Iface_Prim,
7583 Skip_Controlling_Formals => True);
7589 end Is_Interface_Conformant;
7591 ---------------------------------
7592 -- Is_Non_Overriding_Operation --
7593 ---------------------------------
7595 function Is_Non_Overriding_Operation
7596 (Prev_E : Entity_Id;
7597 New_E : Entity_Id) return Boolean
7601 G_Typ : Entity_Id := Empty;
7603 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
7604 -- If F_Type is a derived type associated with a generic actual subtype,
7605 -- then return its Generic_Parent_Type attribute, else return Empty.
7607 function Types_Correspond
7608 (P_Type : Entity_Id;
7609 N_Type : Entity_Id) return Boolean;
7610 -- Returns true if and only if the types (or designated types in the
7611 -- case of anonymous access types) are the same or N_Type is derived
7612 -- directly or indirectly from P_Type.
7614 -----------------------------
7615 -- Get_Generic_Parent_Type --
7616 -----------------------------
7618 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
7624 if Is_Derived_Type (F_Typ)
7625 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
7627 -- The tree must be traversed to determine the parent subtype in
7628 -- the generic unit, which unfortunately isn't always available
7629 -- via semantic attributes. ??? (Note: The use of Original_Node
7630 -- is needed for cases where a full derived type has been
7633 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
7634 if Nkind (Defn) = N_Derived_Type_Definition then
7635 Indic := Subtype_Indication (Defn);
7637 if Nkind (Indic) = N_Subtype_Indication then
7638 G_Typ := Entity (Subtype_Mark (Indic));
7640 G_Typ := Entity (Indic);
7643 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
7644 and then Present (Generic_Parent_Type (Parent (G_Typ)))
7646 return Generic_Parent_Type (Parent (G_Typ));
7652 end Get_Generic_Parent_Type;
7654 ----------------------
7655 -- Types_Correspond --
7656 ----------------------
7658 function Types_Correspond
7659 (P_Type : Entity_Id;
7660 N_Type : Entity_Id) return Boolean
7662 Prev_Type : Entity_Id := Base_Type (P_Type);
7663 New_Type : Entity_Id := Base_Type (N_Type);
7666 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
7667 Prev_Type := Designated_Type (Prev_Type);
7670 if Ekind (New_Type) = E_Anonymous_Access_Type then
7671 New_Type := Designated_Type (New_Type);
7674 if Prev_Type = New_Type then
7677 elsif not Is_Class_Wide_Type (New_Type) then
7678 while Etype (New_Type) /= New_Type loop
7679 New_Type := Etype (New_Type);
7680 if New_Type = Prev_Type then
7686 end Types_Correspond;
7688 -- Start of processing for Is_Non_Overriding_Operation
7691 -- In the case where both operations are implicit derived subprograms
7692 -- then neither overrides the other. This can only occur in certain
7693 -- obscure cases (e.g., derivation from homographs created in a generic
7696 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
7699 elsif Ekind (Current_Scope) = E_Package
7700 and then Is_Generic_Instance (Current_Scope)
7701 and then In_Private_Part (Current_Scope)
7702 and then Comes_From_Source (New_E)
7704 -- We examine the formals and result type of the inherited operation,
7705 -- to determine whether their type is derived from (the instance of)
7706 -- a generic type. The first such formal or result type is the one
7709 Formal := First_Formal (Prev_E);
7710 while Present (Formal) loop
7711 F_Typ := Base_Type (Etype (Formal));
7713 if Ekind (F_Typ) = E_Anonymous_Access_Type then
7714 F_Typ := Designated_Type (F_Typ);
7717 G_Typ := Get_Generic_Parent_Type (F_Typ);
7718 exit when Present (G_Typ);
7720 Next_Formal (Formal);
7723 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
7724 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
7731 -- If the generic type is a private type, then the original operation
7732 -- was not overriding in the generic, because there was no primitive
7733 -- operation to override.
7735 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
7736 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
7737 N_Formal_Private_Type_Definition
7741 -- The generic parent type is the ancestor of a formal derived
7742 -- type declaration. We need to check whether it has a primitive
7743 -- operation that should be overridden by New_E in the generic.
7747 P_Formal : Entity_Id;
7748 N_Formal : Entity_Id;
7752 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
7755 while Present (Prim_Elt) loop
7756 P_Prim := Node (Prim_Elt);
7758 if Chars (P_Prim) = Chars (New_E)
7759 and then Ekind (P_Prim) = Ekind (New_E)
7761 P_Formal := First_Formal (P_Prim);
7762 N_Formal := First_Formal (New_E);
7763 while Present (P_Formal) and then Present (N_Formal) loop
7764 P_Typ := Etype (P_Formal);
7765 N_Typ := Etype (N_Formal);
7767 if not Types_Correspond (P_Typ, N_Typ) then
7771 Next_Entity (P_Formal);
7772 Next_Entity (N_Formal);
7775 -- Found a matching primitive operation belonging to the
7776 -- formal ancestor type, so the new subprogram is
7780 and then No (N_Formal)
7781 and then (Ekind (New_E) /= E_Function
7784 (Etype (P_Prim), Etype (New_E)))
7790 Next_Elmt (Prim_Elt);
7793 -- If no match found, then the new subprogram does not
7794 -- override in the generic (nor in the instance).
7802 end Is_Non_Overriding_Operation;
7804 -------------------------------------
7805 -- List_Inherited_Pre_Post_Aspects --
7806 -------------------------------------
7808 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
7810 if Opt.List_Inherited_Aspects
7811 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
7814 Inherited : constant Subprogram_List :=
7815 Inherited_Subprograms (E);
7819 for J in Inherited'Range loop
7820 P := Spec_PPC_List (Contract (Inherited (J)));
7822 while Present (P) loop
7823 Error_Msg_Sloc := Sloc (P);
7825 if Class_Present (P) and then not Split_PPC (P) then
7826 if Pragma_Name (P) = Name_Precondition then
7828 ("?info: & inherits `Pre''Class` aspect from #", E);
7831 ("?info: & inherits `Post''Class` aspect from #", E);
7835 P := Next_Pragma (P);
7840 end List_Inherited_Pre_Post_Aspects;
7842 ------------------------------
7843 -- Make_Inequality_Operator --
7844 ------------------------------
7846 -- S is the defining identifier of an equality operator. We build a
7847 -- subprogram declaration with the right signature. This operation is
7848 -- intrinsic, because it is always expanded as the negation of the
7849 -- call to the equality function.
7851 procedure Make_Inequality_Operator (S : Entity_Id) is
7852 Loc : constant Source_Ptr := Sloc (S);
7855 Op_Name : Entity_Id;
7857 FF : constant Entity_Id := First_Formal (S);
7858 NF : constant Entity_Id := Next_Formal (FF);
7861 -- Check that equality was properly defined, ignore call if not
7868 A : constant Entity_Id :=
7869 Make_Defining_Identifier (Sloc (FF),
7870 Chars => Chars (FF));
7872 B : constant Entity_Id :=
7873 Make_Defining_Identifier (Sloc (NF),
7874 Chars => Chars (NF));
7877 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
7879 Formals := New_List (
7880 Make_Parameter_Specification (Loc,
7881 Defining_Identifier => A,
7883 New_Reference_To (Etype (First_Formal (S)),
7884 Sloc (Etype (First_Formal (S))))),
7886 Make_Parameter_Specification (Loc,
7887 Defining_Identifier => B,
7889 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
7890 Sloc (Etype (Next_Formal (First_Formal (S)))))));
7893 Make_Subprogram_Declaration (Loc,
7895 Make_Function_Specification (Loc,
7896 Defining_Unit_Name => Op_Name,
7897 Parameter_Specifications => Formals,
7898 Result_Definition =>
7899 New_Reference_To (Standard_Boolean, Loc)));
7901 -- Insert inequality right after equality if it is explicit or after
7902 -- the derived type when implicit. These entities are created only
7903 -- for visibility purposes, and eventually replaced in the course of
7904 -- expansion, so they do not need to be attached to the tree and seen
7905 -- by the back-end. Keeping them internal also avoids spurious
7906 -- freezing problems. The declaration is inserted in the tree for
7907 -- analysis, and removed afterwards. If the equality operator comes
7908 -- from an explicit declaration, attach the inequality immediately
7909 -- after. Else the equality is inherited from a derived type
7910 -- declaration, so insert inequality after that declaration.
7912 if No (Alias (S)) then
7913 Insert_After (Unit_Declaration_Node (S), Decl);
7914 elsif Is_List_Member (Parent (S)) then
7915 Insert_After (Parent (S), Decl);
7917 Insert_After (Parent (Etype (First_Formal (S))), Decl);
7920 Mark_Rewrite_Insertion (Decl);
7921 Set_Is_Intrinsic_Subprogram (Op_Name);
7924 Set_Has_Completion (Op_Name);
7925 Set_Corresponding_Equality (Op_Name, S);
7926 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
7928 end Make_Inequality_Operator;
7930 ----------------------
7931 -- May_Need_Actuals --
7932 ----------------------
7934 procedure May_Need_Actuals (Fun : Entity_Id) is
7939 F := First_Formal (Fun);
7941 while Present (F) loop
7942 if No (Default_Value (F)) then
7950 Set_Needs_No_Actuals (Fun, B);
7951 end May_Need_Actuals;
7953 ---------------------
7954 -- Mode_Conformant --
7955 ---------------------
7957 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
7960 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
7962 end Mode_Conformant;
7964 ---------------------------
7965 -- New_Overloaded_Entity --
7966 ---------------------------
7968 procedure New_Overloaded_Entity
7970 Derived_Type : Entity_Id := Empty)
7972 Overridden_Subp : Entity_Id := Empty;
7973 -- Set if the current scope has an operation that is type-conformant
7974 -- with S, and becomes hidden by S.
7976 Is_Primitive_Subp : Boolean;
7977 -- Set to True if the new subprogram is primitive
7980 -- Entity that S overrides
7982 Prev_Vis : Entity_Id := Empty;
7983 -- Predecessor of E in Homonym chain
7985 procedure Check_For_Primitive_Subprogram
7986 (Is_Primitive : out Boolean;
7987 Is_Overriding : Boolean := False);
7988 -- If the subprogram being analyzed is a primitive operation of the type
7989 -- of a formal or result, set the Has_Primitive_Operations flag on the
7990 -- type, and set Is_Primitive to True (otherwise set to False). Set the
7991 -- corresponding flag on the entity itself for later use.
7993 procedure Check_Synchronized_Overriding
7994 (Def_Id : Entity_Id;
7995 Overridden_Subp : out Entity_Id);
7996 -- First determine if Def_Id is an entry or a subprogram either defined
7997 -- in the scope of a task or protected type, or is a primitive of such
7998 -- a type. Check whether Def_Id overrides a subprogram of an interface
7999 -- implemented by the synchronized type, return the overridden entity
8002 function Is_Private_Declaration (E : Entity_Id) return Boolean;
8003 -- Check that E is declared in the private part of the current package,
8004 -- or in the package body, where it may hide a previous declaration.
8005 -- We can't use In_Private_Part by itself because this flag is also
8006 -- set when freezing entities, so we must examine the place of the
8007 -- declaration in the tree, and recognize wrapper packages as well.
8009 function Is_Overriding_Alias
8011 New_E : Entity_Id) return Boolean;
8012 -- Check whether new subprogram and old subprogram are both inherited
8013 -- from subprograms that have distinct dispatch table entries. This can
8014 -- occur with derivations from instances with accidental homonyms.
8015 -- The function is conservative given that the converse is only true
8016 -- within instances that contain accidental overloadings.
8018 ------------------------------------
8019 -- Check_For_Primitive_Subprogram --
8020 ------------------------------------
8022 procedure Check_For_Primitive_Subprogram
8023 (Is_Primitive : out Boolean;
8024 Is_Overriding : Boolean := False)
8030 function Visible_Part_Type (T : Entity_Id) return Boolean;
8031 -- Returns true if T is declared in the visible part of the current
8032 -- package scope; otherwise returns false. Assumes that T is declared
8035 procedure Check_Private_Overriding (T : Entity_Id);
8036 -- Checks that if a primitive abstract subprogram of a visible
8037 -- abstract type is declared in a private part, then it must override
8038 -- an abstract subprogram declared in the visible part. Also checks
8039 -- that if a primitive function with a controlling result is declared
8040 -- in a private part, then it must override a function declared in
8041 -- the visible part.
8043 ------------------------------
8044 -- Check_Private_Overriding --
8045 ------------------------------
8047 procedure Check_Private_Overriding (T : Entity_Id) is
8049 if Is_Package_Or_Generic_Package (Current_Scope)
8050 and then In_Private_Part (Current_Scope)
8051 and then Visible_Part_Type (T)
8052 and then not In_Instance
8054 if Is_Abstract_Type (T)
8055 and then Is_Abstract_Subprogram (S)
8056 and then (not Is_Overriding
8057 or else not Is_Abstract_Subprogram (E))
8060 ("abstract subprograms must be visible "
8061 & "(RM 3.9.3(10))!", S);
8063 elsif Ekind (S) = E_Function
8064 and then not Is_Overriding
8066 if Is_Tagged_Type (T)
8067 and then T = Base_Type (Etype (S))
8070 ("private function with tagged result must"
8071 & " override visible-part function", S);
8073 ("\move subprogram to the visible part"
8074 & " (RM 3.9.3(10))", S);
8076 -- AI05-0073: extend this test to the case of a function
8077 -- with a controlling access result.
8079 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
8080 and then Is_Tagged_Type (Designated_Type (Etype (S)))
8082 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
8083 and then Ada_Version >= Ada_2012
8086 ("private function with controlling access result "
8087 & "must override visible-part function", S);
8089 ("\move subprogram to the visible part"
8090 & " (RM 3.9.3(10))", S);
8094 end Check_Private_Overriding;
8096 -----------------------
8097 -- Visible_Part_Type --
8098 -----------------------
8100 function Visible_Part_Type (T : Entity_Id) return Boolean is
8101 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
8105 -- If the entity is a private type, then it must be declared in a
8108 if Ekind (T) in Private_Kind then
8112 -- Otherwise, we traverse the visible part looking for its
8113 -- corresponding declaration. We cannot use the declaration
8114 -- node directly because in the private part the entity of a
8115 -- private type is the one in the full view, which does not
8116 -- indicate that it is the completion of something visible.
8118 N := First (Visible_Declarations (Specification (P)));
8119 while Present (N) loop
8120 if Nkind (N) = N_Full_Type_Declaration
8121 and then Present (Defining_Identifier (N))
8122 and then T = Defining_Identifier (N)
8126 elsif Nkind_In (N, N_Private_Type_Declaration,
8127 N_Private_Extension_Declaration)
8128 and then Present (Defining_Identifier (N))
8129 and then T = Full_View (Defining_Identifier (N))
8138 end Visible_Part_Type;
8140 -- Start of processing for Check_For_Primitive_Subprogram
8143 Is_Primitive := False;
8145 if not Comes_From_Source (S) then
8148 -- If subprogram is at library level, it is not primitive operation
8150 elsif Current_Scope = Standard_Standard then
8153 elsif (Is_Package_Or_Generic_Package (Current_Scope)
8154 and then not In_Package_Body (Current_Scope))
8155 or else Is_Overriding
8157 -- For function, check return type
8159 if Ekind (S) = E_Function then
8160 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
8161 F_Typ := Designated_Type (Etype (S));
8166 B_Typ := Base_Type (F_Typ);
8168 if Scope (B_Typ) = Current_Scope
8169 and then not Is_Class_Wide_Type (B_Typ)
8170 and then not Is_Generic_Type (B_Typ)
8172 Is_Primitive := True;
8173 Set_Has_Primitive_Operations (B_Typ);
8174 Set_Is_Primitive (S);
8175 Check_Private_Overriding (B_Typ);
8179 -- For all subprograms, check formals
8181 Formal := First_Formal (S);
8182 while Present (Formal) loop
8183 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
8184 F_Typ := Designated_Type (Etype (Formal));
8186 F_Typ := Etype (Formal);
8189 B_Typ := Base_Type (F_Typ);
8191 if Ekind (B_Typ) = E_Access_Subtype then
8192 B_Typ := Base_Type (B_Typ);
8195 if Scope (B_Typ) = Current_Scope
8196 and then not Is_Class_Wide_Type (B_Typ)
8197 and then not Is_Generic_Type (B_Typ)
8199 Is_Primitive := True;
8200 Set_Is_Primitive (S);
8201 Set_Has_Primitive_Operations (B_Typ);
8202 Check_Private_Overriding (B_Typ);
8205 Next_Formal (Formal);
8208 end Check_For_Primitive_Subprogram;
8210 -----------------------------------
8211 -- Check_Synchronized_Overriding --
8212 -----------------------------------
8214 procedure Check_Synchronized_Overriding
8215 (Def_Id : Entity_Id;
8216 Overridden_Subp : out Entity_Id)
8218 Ifaces_List : Elist_Id;
8222 function Matches_Prefixed_View_Profile
8223 (Prim_Params : List_Id;
8224 Iface_Params : List_Id) return Boolean;
8225 -- Determine whether a subprogram's parameter profile Prim_Params
8226 -- matches that of a potentially overridden interface subprogram
8227 -- Iface_Params. Also determine if the type of first parameter of
8228 -- Iface_Params is an implemented interface.
8230 -----------------------------------
8231 -- Matches_Prefixed_View_Profile --
8232 -----------------------------------
8234 function Matches_Prefixed_View_Profile
8235 (Prim_Params : List_Id;
8236 Iface_Params : List_Id) return Boolean
8238 Iface_Id : Entity_Id;
8239 Iface_Param : Node_Id;
8240 Iface_Typ : Entity_Id;
8241 Prim_Id : Entity_Id;
8242 Prim_Param : Node_Id;
8243 Prim_Typ : Entity_Id;
8245 function Is_Implemented
8246 (Ifaces_List : Elist_Id;
8247 Iface : Entity_Id) return Boolean;
8248 -- Determine if Iface is implemented by the current task or
8251 --------------------
8252 -- Is_Implemented --
8253 --------------------
8255 function Is_Implemented
8256 (Ifaces_List : Elist_Id;
8257 Iface : Entity_Id) return Boolean
8259 Iface_Elmt : Elmt_Id;
8262 Iface_Elmt := First_Elmt (Ifaces_List);
8263 while Present (Iface_Elmt) loop
8264 if Node (Iface_Elmt) = Iface then
8268 Next_Elmt (Iface_Elmt);
8274 -- Start of processing for Matches_Prefixed_View_Profile
8277 Iface_Param := First (Iface_Params);
8278 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
8280 if Is_Access_Type (Iface_Typ) then
8281 Iface_Typ := Designated_Type (Iface_Typ);
8284 Prim_Param := First (Prim_Params);
8286 -- The first parameter of the potentially overridden subprogram
8287 -- must be an interface implemented by Prim.
8289 if not Is_Interface (Iface_Typ)
8290 or else not Is_Implemented (Ifaces_List, Iface_Typ)
8295 -- The checks on the object parameters are done, move onto the
8296 -- rest of the parameters.
8298 if not In_Scope then
8299 Prim_Param := Next (Prim_Param);
8302 Iface_Param := Next (Iface_Param);
8303 while Present (Iface_Param) and then Present (Prim_Param) loop
8304 Iface_Id := Defining_Identifier (Iface_Param);
8305 Iface_Typ := Find_Parameter_Type (Iface_Param);
8307 Prim_Id := Defining_Identifier (Prim_Param);
8308 Prim_Typ := Find_Parameter_Type (Prim_Param);
8310 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
8311 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
8312 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
8314 Iface_Typ := Designated_Type (Iface_Typ);
8315 Prim_Typ := Designated_Type (Prim_Typ);
8318 -- Case of multiple interface types inside a parameter profile
8320 -- (Obj_Param : in out Iface; ...; Param : Iface)
8322 -- If the interface type is implemented, then the matching type
8323 -- in the primitive should be the implementing record type.
8325 if Ekind (Iface_Typ) = E_Record_Type
8326 and then Is_Interface (Iface_Typ)
8327 and then Is_Implemented (Ifaces_List, Iface_Typ)
8329 if Prim_Typ /= Typ then
8333 -- The two parameters must be both mode and subtype conformant
8335 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
8337 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
8346 -- One of the two lists contains more parameters than the other
8348 if Present (Iface_Param) or else Present (Prim_Param) then
8353 end Matches_Prefixed_View_Profile;
8355 -- Start of processing for Check_Synchronized_Overriding
8358 Overridden_Subp := Empty;
8360 -- Def_Id must be an entry or a subprogram. We should skip predefined
8361 -- primitives internally generated by the frontend; however at this
8362 -- stage predefined primitives are still not fully decorated. As a
8363 -- minor optimization we skip here internally generated subprograms.
8365 if (Ekind (Def_Id) /= E_Entry
8366 and then Ekind (Def_Id) /= E_Function
8367 and then Ekind (Def_Id) /= E_Procedure)
8368 or else not Comes_From_Source (Def_Id)
8373 -- Search for the concurrent declaration since it contains the list
8374 -- of all implemented interfaces. In this case, the subprogram is
8375 -- declared within the scope of a protected or a task type.
8377 if Present (Scope (Def_Id))
8378 and then Is_Concurrent_Type (Scope (Def_Id))
8379 and then not Is_Generic_Actual_Type (Scope (Def_Id))
8381 Typ := Scope (Def_Id);
8384 -- The enclosing scope is not a synchronized type and the subprogram
8387 elsif No (First_Formal (Def_Id)) then
8390 -- The subprogram has formals and hence it may be a primitive of a
8394 Typ := Etype (First_Formal (Def_Id));
8396 if Is_Access_Type (Typ) then
8397 Typ := Directly_Designated_Type (Typ);
8400 if Is_Concurrent_Type (Typ)
8401 and then not Is_Generic_Actual_Type (Typ)
8405 -- This case occurs when the concurrent type is declared within
8406 -- a generic unit. As a result the corresponding record has been
8407 -- built and used as the type of the first formal, we just have
8408 -- to retrieve the corresponding concurrent type.
8410 elsif Is_Concurrent_Record_Type (Typ)
8411 and then not Is_Class_Wide_Type (Typ)
8412 and then Present (Corresponding_Concurrent_Type (Typ))
8414 Typ := Corresponding_Concurrent_Type (Typ);
8422 -- There is no overriding to check if is an inherited operation in a
8423 -- type derivation on for a generic actual.
8425 Collect_Interfaces (Typ, Ifaces_List);
8427 if Is_Empty_Elmt_List (Ifaces_List) then
8431 -- Determine whether entry or subprogram Def_Id overrides a primitive
8432 -- operation that belongs to one of the interfaces in Ifaces_List.
8435 Candidate : Entity_Id := Empty;
8436 Hom : Entity_Id := Empty;
8437 Iface_Typ : Entity_Id;
8438 Subp : Entity_Id := Empty;
8441 -- Traverse the homonym chain, looking for a potentially
8442 -- overridden subprogram that belongs to an implemented
8445 Hom := Current_Entity_In_Scope (Def_Id);
8446 while Present (Hom) loop
8450 or else not Is_Overloadable (Subp)
8451 or else not Is_Primitive (Subp)
8452 or else not Is_Dispatching_Operation (Subp)
8453 or else not Present (Find_Dispatching_Type (Subp))
8454 or else not Is_Interface (Find_Dispatching_Type (Subp))
8458 -- Entries and procedures can override abstract or null
8459 -- interface procedures.
8461 elsif (Ekind (Def_Id) = E_Procedure
8462 or else Ekind (Def_Id) = E_Entry)
8463 and then Ekind (Subp) = E_Procedure
8464 and then Matches_Prefixed_View_Profile
8465 (Parameter_Specifications (Parent (Def_Id)),
8466 Parameter_Specifications (Parent (Subp)))
8470 -- For an overridden subprogram Subp, check whether the mode
8471 -- of its first parameter is correct depending on the kind
8472 -- of synchronized type.
8475 Formal : constant Node_Id := First_Formal (Candidate);
8478 -- In order for an entry or a protected procedure to
8479 -- override, the first parameter of the overridden
8480 -- routine must be of mode "out", "in out" or
8481 -- access-to-variable.
8483 if (Ekind (Candidate) = E_Entry
8484 or else Ekind (Candidate) = E_Procedure)
8485 and then Is_Protected_Type (Typ)
8486 and then Ekind (Formal) /= E_In_Out_Parameter
8487 and then Ekind (Formal) /= E_Out_Parameter
8488 and then Nkind (Parameter_Type (Parent (Formal)))
8489 /= N_Access_Definition
8493 -- All other cases are OK since a task entry or routine
8494 -- does not have a restriction on the mode of the first
8495 -- parameter of the overridden interface routine.
8498 Overridden_Subp := Candidate;
8503 -- Functions can override abstract interface functions
8505 elsif Ekind (Def_Id) = E_Function
8506 and then Ekind (Subp) = E_Function
8507 and then Matches_Prefixed_View_Profile
8508 (Parameter_Specifications (Parent (Def_Id)),
8509 Parameter_Specifications (Parent (Subp)))
8510 and then Etype (Result_Definition (Parent (Def_Id))) =
8511 Etype (Result_Definition (Parent (Subp)))
8513 Overridden_Subp := Subp;
8517 Hom := Homonym (Hom);
8520 -- After examining all candidates for overriding, we are left with
8521 -- the best match which is a mode incompatible interface routine.
8522 -- Do not emit an error if the Expander is active since this error
8523 -- will be detected later on after all concurrent types are
8524 -- expanded and all wrappers are built. This check is meant for
8525 -- spec-only compilations.
8527 if Present (Candidate) and then not Expander_Active then
8529 Find_Parameter_Type (Parent (First_Formal (Candidate)));
8531 -- Def_Id is primitive of a protected type, declared inside the
8532 -- type, and the candidate is primitive of a limited or
8533 -- synchronized interface.
8536 and then Is_Protected_Type (Typ)
8538 (Is_Limited_Interface (Iface_Typ)
8539 or else Is_Protected_Interface (Iface_Typ)
8540 or else Is_Synchronized_Interface (Iface_Typ)
8541 or else Is_Task_Interface (Iface_Typ))
8543 Error_Msg_PT (Parent (Typ), Candidate);
8547 Overridden_Subp := Candidate;
8550 end Check_Synchronized_Overriding;
8552 ----------------------------
8553 -- Is_Private_Declaration --
8554 ----------------------------
8556 function Is_Private_Declaration (E : Entity_Id) return Boolean is
8557 Priv_Decls : List_Id;
8558 Decl : constant Node_Id := Unit_Declaration_Node (E);
8561 if Is_Package_Or_Generic_Package (Current_Scope)
8562 and then In_Private_Part (Current_Scope)
8565 Private_Declarations
8566 (Specification (Unit_Declaration_Node (Current_Scope)));
8568 return In_Package_Body (Current_Scope)
8570 (Is_List_Member (Decl)
8571 and then List_Containing (Decl) = Priv_Decls)
8572 or else (Nkind (Parent (Decl)) = N_Package_Specification
8575 (Defining_Entity (Parent (Decl)))
8576 and then List_Containing (Parent (Parent (Decl))) =
8581 end Is_Private_Declaration;
8583 --------------------------
8584 -- Is_Overriding_Alias --
8585 --------------------------
8587 function Is_Overriding_Alias
8589 New_E : Entity_Id) return Boolean
8591 AO : constant Entity_Id := Alias (Old_E);
8592 AN : constant Entity_Id := Alias (New_E);
8595 return Scope (AO) /= Scope (AN)
8596 or else No (DTC_Entity (AO))
8597 or else No (DTC_Entity (AN))
8598 or else DT_Position (AO) = DT_Position (AN);
8599 end Is_Overriding_Alias;
8601 -- Start of processing for New_Overloaded_Entity
8604 -- We need to look for an entity that S may override. This must be a
8605 -- homonym in the current scope, so we look for the first homonym of
8606 -- S in the current scope as the starting point for the search.
8608 E := Current_Entity_In_Scope (S);
8610 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
8611 -- They are directly added to the list of primitive operations of
8612 -- Derived_Type, unless this is a rederivation in the private part
8613 -- of an operation that was already derived in the visible part of
8614 -- the current package.
8616 if Ada_Version >= Ada_2005
8617 and then Present (Derived_Type)
8618 and then Present (Alias (S))
8619 and then Is_Dispatching_Operation (Alias (S))
8620 and then Present (Find_Dispatching_Type (Alias (S)))
8621 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
8623 -- For private types, when the full-view is processed we propagate to
8624 -- the full view the non-overridden entities whose attribute "alias"
8625 -- references an interface primitive. These entities were added by
8626 -- Derive_Subprograms to ensure that interface primitives are
8629 -- Inside_Freeze_Actions is non zero when S corresponds with an
8630 -- internal entity that links an interface primitive with its
8631 -- covering primitive through attribute Interface_Alias (see
8632 -- Add_Internal_Interface_Entities).
8634 if Inside_Freezing_Actions = 0
8635 and then Is_Package_Or_Generic_Package (Current_Scope)
8636 and then In_Private_Part (Current_Scope)
8637 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
8638 and then Nkind (Parent (S)) = N_Full_Type_Declaration
8639 and then Full_View (Defining_Identifier (Parent (E)))
8640 = Defining_Identifier (Parent (S))
8641 and then Alias (E) = Alias (S)
8643 Check_Operation_From_Private_View (S, E);
8644 Set_Is_Dispatching_Operation (S);
8649 Enter_Overloaded_Entity (S);
8650 Check_Dispatching_Operation (S, Empty);
8651 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8657 -- If there is no homonym then this is definitely not overriding
8660 Enter_Overloaded_Entity (S);
8661 Check_Dispatching_Operation (S, Empty);
8662 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8664 -- If subprogram has an explicit declaration, check whether it
8665 -- has an overriding indicator.
8667 if Comes_From_Source (S) then
8668 Check_Synchronized_Overriding (S, Overridden_Subp);
8670 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
8671 -- it may have overridden some hidden inherited primitive. Update
8672 -- Overridden_Subp to avoid spurious errors when checking the
8673 -- overriding indicator.
8675 if Ada_Version >= Ada_2012
8676 and then No (Overridden_Subp)
8677 and then Is_Dispatching_Operation (S)
8678 and then Present (Overridden_Operation (S))
8680 Overridden_Subp := Overridden_Operation (S);
8683 Check_Overriding_Indicator
8684 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8687 -- If there is a homonym that is not overloadable, then we have an
8688 -- error, except for the special cases checked explicitly below.
8690 elsif not Is_Overloadable (E) then
8692 -- Check for spurious conflict produced by a subprogram that has the
8693 -- same name as that of the enclosing generic package. The conflict
8694 -- occurs within an instance, between the subprogram and the renaming
8695 -- declaration for the package. After the subprogram, the package
8696 -- renaming declaration becomes hidden.
8698 if Ekind (E) = E_Package
8699 and then Present (Renamed_Object (E))
8700 and then Renamed_Object (E) = Current_Scope
8701 and then Nkind (Parent (Renamed_Object (E))) =
8702 N_Package_Specification
8703 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
8706 Set_Is_Immediately_Visible (E, False);
8707 Enter_Overloaded_Entity (S);
8708 Set_Homonym (S, Homonym (E));
8709 Check_Dispatching_Operation (S, Empty);
8710 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
8712 -- If the subprogram is implicit it is hidden by the previous
8713 -- declaration. However if it is dispatching, it must appear in the
8714 -- dispatch table anyway, because it can be dispatched to even if it
8715 -- cannot be called directly.
8717 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
8718 Set_Scope (S, Current_Scope);
8720 if Is_Dispatching_Operation (Alias (S)) then
8721 Check_Dispatching_Operation (S, Empty);
8727 Error_Msg_Sloc := Sloc (E);
8729 -- Generate message, with useful additional warning if in generic
8731 if Is_Generic_Unit (E) then
8732 Error_Msg_N ("previous generic unit cannot be overloaded", S);
8733 Error_Msg_N ("\& conflicts with declaration#", S);
8735 Error_Msg_N ("& conflicts with declaration#", S);
8741 -- E exists and is overloadable
8744 Check_Synchronized_Overriding (S, Overridden_Subp);
8746 -- Loop through E and its homonyms to determine if any of them is
8747 -- the candidate for overriding by S.
8749 while Present (E) loop
8751 -- Definitely not interesting if not in the current scope
8753 if Scope (E) /= Current_Scope then
8756 -- Ada 2012 (AI05-0165): For internally generated bodies of
8757 -- null procedures locate the internally generated spec. We
8758 -- enforce mode conformance since a tagged type may inherit
8759 -- from interfaces several null primitives which differ only
8760 -- in the mode of the formals.
8762 elsif not Comes_From_Source (S)
8763 and then Is_Null_Procedure (S)
8764 and then not Mode_Conformant (E, S)
8768 -- Check if we have type conformance
8770 elsif Type_Conformant (E, S) then
8772 -- If the old and new entities have the same profile and one
8773 -- is not the body of the other, then this is an error, unless
8774 -- one of them is implicitly declared.
8776 -- There are some cases when both can be implicit, for example
8777 -- when both a literal and a function that overrides it are
8778 -- inherited in a derivation, or when an inherited operation
8779 -- of a tagged full type overrides the inherited operation of
8780 -- a private extension. Ada 83 had a special rule for the
8781 -- literal case. In Ada 95, the later implicit operation hides
8782 -- the former, and the literal is always the former. In the
8783 -- odd case where both are derived operations declared at the
8784 -- same point, both operations should be declared, and in that
8785 -- case we bypass the following test and proceed to the next
8786 -- part. This can only occur for certain obscure cases in
8787 -- instances, when an operation on a type derived from a formal
8788 -- private type does not override a homograph inherited from
8789 -- the actual. In subsequent derivations of such a type, the
8790 -- DT positions of these operations remain distinct, if they
8793 if Present (Alias (S))
8794 and then (No (Alias (E))
8795 or else Comes_From_Source (E)
8796 or else Is_Abstract_Subprogram (S)
8798 (Is_Dispatching_Operation (E)
8799 and then Is_Overriding_Alias (E, S)))
8800 and then Ekind (E) /= E_Enumeration_Literal
8802 -- When an derived operation is overloaded it may be due to
8803 -- the fact that the full view of a private extension
8804 -- re-inherits. It has to be dealt with.
8806 if Is_Package_Or_Generic_Package (Current_Scope)
8807 and then In_Private_Part (Current_Scope)
8809 Check_Operation_From_Private_View (S, E);
8812 -- In any case the implicit operation remains hidden by the
8813 -- existing declaration, which is overriding. Indicate that
8814 -- E overrides the operation from which S is inherited.
8816 if Present (Alias (S)) then
8817 Set_Overridden_Operation (E, Alias (S));
8819 Set_Overridden_Operation (E, S);
8822 if Comes_From_Source (E) then
8823 Check_Overriding_Indicator (E, S, Is_Primitive => False);
8828 -- Within an instance, the renaming declarations for actual
8829 -- subprograms may become ambiguous, but they do not hide each
8832 elsif Ekind (E) /= E_Entry
8833 and then not Comes_From_Source (E)
8834 and then not Is_Generic_Instance (E)
8835 and then (Present (Alias (E))
8836 or else Is_Intrinsic_Subprogram (E))
8837 and then (not In_Instance
8838 or else No (Parent (E))
8839 or else Nkind (Unit_Declaration_Node (E)) /=
8840 N_Subprogram_Renaming_Declaration)
8842 -- A subprogram child unit is not allowed to override an
8843 -- inherited subprogram (10.1.1(20)).
8845 if Is_Child_Unit (S) then
8847 ("child unit overrides inherited subprogram in parent",
8852 if Is_Non_Overriding_Operation (E, S) then
8853 Enter_Overloaded_Entity (S);
8855 if No (Derived_Type)
8856 or else Is_Tagged_Type (Derived_Type)
8858 Check_Dispatching_Operation (S, Empty);
8864 -- E is a derived operation or an internal operator which
8865 -- is being overridden. Remove E from further visibility.
8866 -- Furthermore, if E is a dispatching operation, it must be
8867 -- replaced in the list of primitive operations of its type
8868 -- (see Override_Dispatching_Operation).
8870 Overridden_Subp := E;
8876 Prev := First_Entity (Current_Scope);
8877 while Present (Prev)
8878 and then Next_Entity (Prev) /= E
8883 -- It is possible for E to be in the current scope and
8884 -- yet not in the entity chain. This can only occur in a
8885 -- generic context where E is an implicit concatenation
8886 -- in the formal part, because in a generic body the
8887 -- entity chain starts with the formals.
8890 (Present (Prev) or else Chars (E) = Name_Op_Concat);
8892 -- E must be removed both from the entity_list of the
8893 -- current scope, and from the visibility chain
8895 if Debug_Flag_E then
8896 Write_Str ("Override implicit operation ");
8897 Write_Int (Int (E));
8901 -- If E is a predefined concatenation, it stands for four
8902 -- different operations. As a result, a single explicit
8903 -- declaration does not hide it. In a possible ambiguous
8904 -- situation, Disambiguate chooses the user-defined op,
8905 -- so it is correct to retain the previous internal one.
8907 if Chars (E) /= Name_Op_Concat
8908 or else Ekind (E) /= E_Operator
8910 -- For nondispatching derived operations that are
8911 -- overridden by a subprogram declared in the private
8912 -- part of a package, we retain the derived subprogram
8913 -- but mark it as not immediately visible. If the
8914 -- derived operation was declared in the visible part
8915 -- then this ensures that it will still be visible
8916 -- outside the package with the proper signature
8917 -- (calls from outside must also be directed to this
8918 -- version rather than the overriding one, unlike the
8919 -- dispatching case). Calls from inside the package
8920 -- will still resolve to the overriding subprogram
8921 -- since the derived one is marked as not visible
8922 -- within the package.
8924 -- If the private operation is dispatching, we achieve
8925 -- the overriding by keeping the implicit operation
8926 -- but setting its alias to be the overriding one. In
8927 -- this fashion the proper body is executed in all
8928 -- cases, but the original signature is used outside
8931 -- If the overriding is not in the private part, we
8932 -- remove the implicit operation altogether.
8934 if Is_Private_Declaration (S) then
8935 if not Is_Dispatching_Operation (E) then
8936 Set_Is_Immediately_Visible (E, False);
8938 -- Work done in Override_Dispatching_Operation,
8939 -- so nothing else needs to be done here.
8945 -- Find predecessor of E in Homonym chain
8947 if E = Current_Entity (E) then
8950 Prev_Vis := Current_Entity (E);
8951 while Homonym (Prev_Vis) /= E loop
8952 Prev_Vis := Homonym (Prev_Vis);
8956 if Prev_Vis /= Empty then
8958 -- Skip E in the visibility chain
8960 Set_Homonym (Prev_Vis, Homonym (E));
8963 Set_Name_Entity_Id (Chars (E), Homonym (E));
8966 Set_Next_Entity (Prev, Next_Entity (E));
8968 if No (Next_Entity (Prev)) then
8969 Set_Last_Entity (Current_Scope, Prev);
8974 Enter_Overloaded_Entity (S);
8976 -- For entities generated by Derive_Subprograms the
8977 -- overridden operation is the inherited primitive
8978 -- (which is available through the attribute alias).
8980 if not (Comes_From_Source (E))
8981 and then Is_Dispatching_Operation (E)
8982 and then Find_Dispatching_Type (E) =
8983 Find_Dispatching_Type (S)
8984 and then Present (Alias (E))
8985 and then Comes_From_Source (Alias (E))
8987 Set_Overridden_Operation (S, Alias (E));
8989 -- Normal case of setting entity as overridden
8991 -- Note: Static_Initialization and Overridden_Operation
8992 -- attributes use the same field in subprogram entities.
8993 -- Static_Initialization is only defined for internal
8994 -- initialization procedures, where Overridden_Operation
8995 -- is irrelevant. Therefore the setting of this attribute
8996 -- must check whether the target is an init_proc.
8998 elsif not Is_Init_Proc (S) then
8999 Set_Overridden_Operation (S, E);
9002 Check_Overriding_Indicator (S, E, Is_Primitive => True);
9004 -- If S is a user-defined subprogram or a null procedure
9005 -- expanded to override an inherited null procedure, or a
9006 -- predefined dispatching primitive then indicate that E
9007 -- overrides the operation from which S is inherited.
9009 if Comes_From_Source (S)
9011 (Present (Parent (S))
9013 Nkind (Parent (S)) = N_Procedure_Specification
9015 Null_Present (Parent (S)))
9017 (Present (Alias (E))
9019 Is_Predefined_Dispatching_Operation (Alias (E)))
9021 if Present (Alias (E)) then
9022 Set_Overridden_Operation (S, Alias (E));
9026 if Is_Dispatching_Operation (E) then
9028 -- An overriding dispatching subprogram inherits the
9029 -- convention of the overridden subprogram (AI-117).
9031 Set_Convention (S, Convention (E));
9032 Check_Dispatching_Operation (S, E);
9035 Check_Dispatching_Operation (S, Empty);
9038 Check_For_Primitive_Subprogram
9039 (Is_Primitive_Subp, Is_Overriding => True);
9040 goto Check_Inequality;
9043 -- Apparent redeclarations in instances can occur when two
9044 -- formal types get the same actual type. The subprograms in
9045 -- in the instance are legal, even if not callable from the
9046 -- outside. Calls from within are disambiguated elsewhere.
9047 -- For dispatching operations in the visible part, the usual
9048 -- rules apply, and operations with the same profile are not
9051 elsif (In_Instance_Visible_Part
9052 and then not Is_Dispatching_Operation (E))
9053 or else In_Instance_Not_Visible
9057 -- Here we have a real error (identical profile)
9060 Error_Msg_Sloc := Sloc (E);
9062 -- Avoid cascaded errors if the entity appears in
9063 -- subsequent calls.
9065 Set_Scope (S, Current_Scope);
9067 -- Generate error, with extra useful warning for the case
9068 -- of a generic instance with no completion.
9070 if Is_Generic_Instance (S)
9071 and then not Has_Completion (E)
9074 ("instantiation cannot provide body for&", S);
9075 Error_Msg_N ("\& conflicts with declaration#", S);
9077 Error_Msg_N ("& conflicts with declaration#", S);
9084 -- If one subprogram has an access parameter and the other
9085 -- a parameter of an access type, calls to either might be
9086 -- ambiguous. Verify that parameters match except for the
9087 -- access parameter.
9089 if May_Hide_Profile then
9095 F1 := First_Formal (S);
9096 F2 := First_Formal (E);
9097 while Present (F1) and then Present (F2) loop
9098 if Is_Access_Type (Etype (F1)) then
9099 if not Is_Access_Type (Etype (F2))
9100 or else not Conforming_Types
9101 (Designated_Type (Etype (F1)),
9102 Designated_Type (Etype (F2)),
9105 May_Hide_Profile := False;
9109 not Conforming_Types
9110 (Etype (F1), Etype (F2), Type_Conformant)
9112 May_Hide_Profile := False;
9123 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
9132 -- On exit, we know that S is a new entity
9134 Enter_Overloaded_Entity (S);
9135 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
9136 Check_Overriding_Indicator
9137 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
9139 -- Overloading is not allowed in SPARK, except for operators
9141 if Nkind (S) /= N_Defining_Operator_Symbol then
9142 Error_Msg_Sloc := Sloc (Homonym (S));
9143 Check_SPARK_Restriction
9144 ("overloading not allowed with entity#", S);
9147 -- If S is a derived operation for an untagged type then by
9148 -- definition it's not a dispatching operation (even if the parent
9149 -- operation was dispatching), so Check_Dispatching_Operation is not
9150 -- called in that case.
9152 if No (Derived_Type)
9153 or else Is_Tagged_Type (Derived_Type)
9155 Check_Dispatching_Operation (S, Empty);
9159 -- If this is a user-defined equality operator that is not a derived
9160 -- subprogram, create the corresponding inequality. If the operation is
9161 -- dispatching, the expansion is done elsewhere, and we do not create
9162 -- an explicit inequality operation.
9164 <<Check_Inequality>>
9165 if Chars (S) = Name_Op_Eq
9166 and then Etype (S) = Standard_Boolean
9167 and then Present (Parent (S))
9168 and then not Is_Dispatching_Operation (S)
9170 Make_Inequality_Operator (S);
9172 if Ada_Version >= Ada_2012 then
9173 Check_Untagged_Equality (S);
9176 end New_Overloaded_Entity;
9178 ---------------------
9179 -- Process_Formals --
9180 ---------------------
9182 procedure Process_Formals
9184 Related_Nod : Node_Id)
9186 Param_Spec : Node_Id;
9188 Formal_Type : Entity_Id;
9192 Num_Out_Params : Nat := 0;
9193 First_Out_Param : Entity_Id := Empty;
9194 -- Used for setting Is_Only_Out_Parameter
9196 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
9197 -- Determine whether an access type designates a type coming from a
9200 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
9201 -- Check whether the default has a class-wide type. After analysis the
9202 -- default has the type of the formal, so we must also check explicitly
9203 -- for an access attribute.
9205 -------------------------------
9206 -- Designates_From_With_Type --
9207 -------------------------------
9209 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
9210 Desig : Entity_Id := Typ;
9213 if Is_Access_Type (Desig) then
9214 Desig := Directly_Designated_Type (Desig);
9217 if Is_Class_Wide_Type (Desig) then
9218 Desig := Root_Type (Desig);
9222 Ekind (Desig) = E_Incomplete_Type
9223 and then From_With_Type (Desig);
9224 end Designates_From_With_Type;
9226 ---------------------------
9227 -- Is_Class_Wide_Default --
9228 ---------------------------
9230 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
9232 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
9233 or else (Nkind (D) = N_Attribute_Reference
9234 and then Attribute_Name (D) = Name_Access
9235 and then Is_Class_Wide_Type (Etype (Prefix (D))));
9236 end Is_Class_Wide_Default;
9238 -- Start of processing for Process_Formals
9241 -- In order to prevent premature use of the formals in the same formal
9242 -- part, the Ekind is left undefined until all default expressions are
9243 -- analyzed. The Ekind is established in a separate loop at the end.
9245 Param_Spec := First (T);
9246 while Present (Param_Spec) loop
9247 Formal := Defining_Identifier (Param_Spec);
9248 Set_Never_Set_In_Source (Formal, True);
9249 Enter_Name (Formal);
9251 -- Case of ordinary parameters
9253 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
9254 Find_Type (Parameter_Type (Param_Spec));
9255 Ptype := Parameter_Type (Param_Spec);
9257 if Ptype = Error then
9261 Formal_Type := Entity (Ptype);
9263 if Is_Incomplete_Type (Formal_Type)
9265 (Is_Class_Wide_Type (Formal_Type)
9266 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
9268 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
9269 -- primitive operations, as long as their completion is
9270 -- in the same declarative part. If in the private part
9271 -- this means that the type cannot be a Taft-amendment type.
9272 -- Check is done on package exit. For access to subprograms,
9273 -- the use is legal for Taft-amendment types.
9275 if Is_Tagged_Type (Formal_Type) then
9276 if Ekind (Scope (Current_Scope)) = E_Package
9277 and then not From_With_Type (Formal_Type)
9278 and then not Is_Class_Wide_Type (Formal_Type)
9281 (Parent (T), N_Access_Function_Definition,
9282 N_Access_Procedure_Definition)
9286 Private_Dependents (Base_Type (Formal_Type)));
9288 -- Freezing is delayed to ensure that Register_Prim
9289 -- will get called for this operation, which is needed
9290 -- in cases where static dispatch tables aren't built.
9291 -- (Note that the same is done for controlling access
9292 -- parameter cases in function Access_Definition.)
9294 Set_Has_Delayed_Freeze (Current_Scope);
9298 -- Special handling of Value_Type for CIL case
9300 elsif Is_Value_Type (Formal_Type) then
9303 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
9304 N_Access_Procedure_Definition)
9306 -- AI05-0151: Tagged incomplete types are allowed in all
9307 -- formal parts. Untagged incomplete types are not allowed
9310 if Ada_Version >= Ada_2012 then
9311 if Is_Tagged_Type (Formal_Type) then
9314 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
9319 ("invalid use of untagged incomplete type&",
9320 Ptype, Formal_Type);
9325 ("invalid use of incomplete type&",
9326 Param_Spec, Formal_Type);
9328 -- Further checks on the legality of incomplete types
9329 -- in formal parts are delayed until the freeze point
9330 -- of the enclosing subprogram or access to subprogram.
9334 elsif Ekind (Formal_Type) = E_Void then
9336 ("premature use of&",
9337 Parameter_Type (Param_Spec), Formal_Type);
9340 -- Ada 2012 (AI-142): Handle aliased parameters
9342 if Ada_Version >= Ada_2012
9343 and then Aliased_Present (Param_Spec)
9345 Set_Is_Aliased (Formal);
9348 -- Ada 2005 (AI-231): Create and decorate an internal subtype
9349 -- declaration corresponding to the null-excluding type of the
9350 -- formal in the enclosing scope. Finally, replace the parameter
9351 -- type of the formal with the internal subtype.
9353 if Ada_Version >= Ada_2005
9354 and then Null_Exclusion_Present (Param_Spec)
9356 if not Is_Access_Type (Formal_Type) then
9358 ("`NOT NULL` allowed only for an access type", Param_Spec);
9361 if Can_Never_Be_Null (Formal_Type)
9362 and then Comes_From_Source (Related_Nod)
9365 ("`NOT NULL` not allowed (& already excludes null)",
9366 Param_Spec, Formal_Type);
9370 Create_Null_Excluding_Itype
9372 Related_Nod => Related_Nod,
9373 Scope_Id => Scope (Current_Scope));
9375 -- If the designated type of the itype is an itype we
9376 -- decorate it with the Has_Delayed_Freeze attribute to
9377 -- avoid problems with the backend.
9380 -- type T is access procedure;
9381 -- procedure Op (O : not null T);
9383 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
9384 Set_Has_Delayed_Freeze (Formal_Type);
9389 -- An access formal type
9393 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
9395 -- No need to continue if we already notified errors
9397 if not Present (Formal_Type) then
9401 -- Ada 2005 (AI-254)
9404 AD : constant Node_Id :=
9405 Access_To_Subprogram_Definition
9406 (Parameter_Type (Param_Spec));
9408 if Present (AD) and then Protected_Present (AD) then
9410 Replace_Anonymous_Access_To_Protected_Subprogram
9416 Set_Etype (Formal, Formal_Type);
9418 -- Deal with default expression if present
9420 Default := Expression (Param_Spec);
9422 if Present (Default) then
9423 Check_SPARK_Restriction
9424 ("default expression is not allowed", Default);
9426 if Out_Present (Param_Spec) then
9428 ("default initialization only allowed for IN parameters",
9432 -- Do the special preanalysis of the expression (see section on
9433 -- "Handling of Default Expressions" in the spec of package Sem).
9435 Preanalyze_Spec_Expression (Default, Formal_Type);
9437 -- An access to constant cannot be the default for
9438 -- an access parameter that is an access to variable.
9440 if Ekind (Formal_Type) = E_Anonymous_Access_Type
9441 and then not Is_Access_Constant (Formal_Type)
9442 and then Is_Access_Type (Etype (Default))
9443 and then Is_Access_Constant (Etype (Default))
9446 ("formal that is access to variable cannot be initialized " &
9447 "with an access-to-constant expression", Default);
9450 -- Check that the designated type of an access parameter's default
9451 -- is not a class-wide type unless the parameter's designated type
9452 -- is also class-wide.
9454 if Ekind (Formal_Type) = E_Anonymous_Access_Type
9455 and then not Designates_From_With_Type (Formal_Type)
9456 and then Is_Class_Wide_Default (Default)
9457 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
9460 ("access to class-wide expression not allowed here", Default);
9463 -- Check incorrect use of dynamically tagged expressions
9465 if Is_Tagged_Type (Formal_Type) then
9466 Check_Dynamically_Tagged_Expression
9469 Related_Nod => Default);
9473 -- Ada 2005 (AI-231): Static checks
9475 if Ada_Version >= Ada_2005
9476 and then Is_Access_Type (Etype (Formal))
9477 and then Can_Never_Be_Null (Etype (Formal))
9479 Null_Exclusion_Static_Checks (Param_Spec);
9486 -- If this is the formal part of a function specification, analyze the
9487 -- subtype mark in the context where the formals are visible but not
9488 -- yet usable, and may hide outer homographs.
9490 if Nkind (Related_Nod) = N_Function_Specification then
9491 Analyze_Return_Type (Related_Nod);
9494 -- Now set the kind (mode) of each formal
9496 Param_Spec := First (T);
9497 while Present (Param_Spec) loop
9498 Formal := Defining_Identifier (Param_Spec);
9499 Set_Formal_Mode (Formal);
9501 if Ekind (Formal) = E_In_Parameter then
9502 Set_Default_Value (Formal, Expression (Param_Spec));
9504 if Present (Expression (Param_Spec)) then
9505 Default := Expression (Param_Spec);
9507 if Is_Scalar_Type (Etype (Default)) then
9509 (Parameter_Type (Param_Spec)) /= N_Access_Definition
9511 Formal_Type := Entity (Parameter_Type (Param_Spec));
9514 Formal_Type := Access_Definition
9515 (Related_Nod, Parameter_Type (Param_Spec));
9518 Apply_Scalar_Range_Check (Default, Formal_Type);
9522 elsif Ekind (Formal) = E_Out_Parameter then
9523 Num_Out_Params := Num_Out_Params + 1;
9525 if Num_Out_Params = 1 then
9526 First_Out_Param := Formal;
9529 elsif Ekind (Formal) = E_In_Out_Parameter then
9530 Num_Out_Params := Num_Out_Params + 1;
9533 -- Force call by reference if aliased
9535 if Is_Aliased (Formal) then
9536 Set_Mechanism (Formal, By_Reference);
9542 if Present (First_Out_Param) and then Num_Out_Params = 1 then
9543 Set_Is_Only_Out_Parameter (First_Out_Param);
9545 end Process_Formals;
9551 procedure Process_PPCs
9553 Spec_Id : Entity_Id;
9554 Body_Id : Entity_Id)
9556 Loc : constant Source_Ptr := Sloc (N);
9560 Designator : Entity_Id;
9561 -- Subprogram designator, set from Spec_Id if present, else Body_Id
9563 Precond : Node_Id := Empty;
9564 -- Set non-Empty if we prepend precondition to the declarations. This
9565 -- is used to hook up inherited preconditions (adding the condition
9566 -- expression with OR ELSE, and adding the message).
9568 Inherited_Precond : Node_Id;
9569 -- Precondition inherited from parent subprogram
9571 Inherited : constant Subprogram_List :=
9572 Inherited_Subprograms (Spec_Id);
9573 -- List of subprograms inherited by this subprogram
9575 Plist : List_Id := No_List;
9576 -- List of generated postconditions
9578 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
9579 -- Prag contains an analyzed precondition or postcondition pragma. This
9580 -- function copies the pragma, changes it to the corresponding Check
9581 -- pragma and returns the Check pragma as the result. If Pspec is non-
9582 -- empty, this is the case of inheriting a PPC, where we must change
9583 -- references to parameters of the inherited subprogram to point to the
9584 -- corresponding parameters of the current subprogram.
9586 function Invariants_Or_Predicates_Present return Boolean;
9587 -- Determines if any invariants or predicates are present for any OUT
9588 -- or IN OUT parameters of the subprogram, or (for a function) if the
9589 -- return value has an invariant.
9591 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean;
9592 -- T is the entity for a private type for which invariants are defined.
9593 -- This function returns True if the procedure corresponding to the
9594 -- value of Designator is a public procedure from the point of view of
9595 -- this type (i.e. its spec is in the visible part of the package that
9596 -- contains the declaration of the private type). A True value means
9597 -- that an invariant check is required (for an IN OUT parameter, or
9598 -- the returned value of a function.
9604 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
9605 Nam : constant Name_Id := Pragma_Name (Prag);
9610 -- Prepare map if this is the case where we have to map entities of
9611 -- arguments in the overridden subprogram to corresponding entities
9612 -- of the current subprogram.
9623 Map := New_Elmt_List;
9624 PF := First_Formal (Pspec);
9625 CF := First_Formal (Designator);
9626 while Present (PF) loop
9627 Append_Elmt (PF, Map);
9628 Append_Elmt (CF, Map);
9635 -- Now we can copy the tree, doing any required substitutions
9637 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
9639 -- Set Analyzed to false, since we want to reanalyze the check
9640 -- procedure. Note that it is only at the outer level that we
9641 -- do this fiddling, for the spec cases, the already preanalyzed
9642 -- parameters are not affected.
9644 Set_Analyzed (CP, False);
9646 -- We also make sure Comes_From_Source is False for the copy
9648 Set_Comes_From_Source (CP, False);
9650 -- For a postcondition pragma within a generic, preserve the pragma
9651 -- for later expansion.
9653 if Nam = Name_Postcondition
9654 and then not Expander_Active
9659 -- Change copy of pragma into corresponding pragma Check
9661 Prepend_To (Pragma_Argument_Associations (CP),
9662 Make_Pragma_Argument_Association (Sloc (Prag),
9663 Expression => Make_Identifier (Loc, Nam)));
9664 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
9666 -- If this is inherited case and the current message starts with
9667 -- "failed p", we change it to "failed inherited p...".
9669 if Present (Pspec) then
9671 Msg : constant Node_Id :=
9672 Last (Pragma_Argument_Associations (CP));
9675 if Chars (Msg) = Name_Message then
9676 String_To_Name_Buffer (Strval (Expression (Msg)));
9678 if Name_Buffer (1 .. 8) = "failed p" then
9679 Insert_Str_In_Name_Buffer ("inherited ", 8);
9681 (Expression (Last (Pragma_Argument_Associations (CP))),
9682 String_From_Name_Buffer);
9688 -- Return the check pragma
9693 --------------------------------------
9694 -- Invariants_Or_Predicates_Present --
9695 --------------------------------------
9697 function Invariants_Or_Predicates_Present return Boolean is
9701 -- Check function return result
9703 if Ekind (Designator) /= E_Procedure
9704 and then Has_Invariants (Etype (Designator))
9711 Formal := First_Formal (Designator);
9712 while Present (Formal) loop
9713 if Ekind (Formal) /= E_In_Parameter
9715 (Has_Invariants (Etype (Formal))
9716 or else Present (Predicate_Function (Etype (Formal))))
9721 Next_Formal (Formal);
9725 end Invariants_Or_Predicates_Present;
9727 ------------------------------
9728 -- Is_Public_Subprogram_For --
9729 ------------------------------
9731 -- The type T is a private type, its declaration is therefore in
9732 -- the list of public declarations of some package. The test for a
9733 -- public subprogram is that its declaration is in this same list
9734 -- of declarations for the same package (note that all the public
9735 -- declarations are in one list, and all the private declarations
9736 -- in another, so this deals with the public/private distinction).
9738 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is
9739 DD : constant Node_Id := Unit_Declaration_Node (Designator);
9740 -- The subprogram declaration for the subprogram in question
9742 TL : constant List_Id :=
9743 Visible_Declarations
9744 (Specification (Unit_Declaration_Node (Scope (T))));
9745 -- The list of declarations containing the private declaration of
9746 -- the type. We know it is a private type, so we know its scope is
9747 -- the package in question, and we know it must be in the visible
9748 -- declarations of this package.
9751 -- If the subprogram declaration is not a list member, it must be
9752 -- an Init_Proc, in which case we want to consider it to be a
9753 -- public subprogram, since we do get initializations to deal with.
9755 if not Is_List_Member (DD) then
9758 -- Otherwise we test whether the subprogram is declared in the
9759 -- visible declarations of the package containing the type.
9762 return TL = List_Containing (DD);
9764 end Is_Public_Subprogram_For;
9766 -- Start of processing for Process_PPCs
9769 -- Capture designator from spec if present, else from body
9771 if Present (Spec_Id) then
9772 Designator := Spec_Id;
9774 Designator := Body_Id;
9777 -- Grab preconditions from spec
9779 if Present (Spec_Id) then
9781 -- Loop through PPC pragmas from spec. Note that preconditions from
9782 -- the body will be analyzed and converted when we scan the body
9783 -- declarations below.
9785 Prag := Spec_PPC_List (Contract (Spec_Id));
9786 while Present (Prag) loop
9787 if Pragma_Name (Prag) = Name_Precondition then
9789 -- For Pre (or Precondition pragma), we simply prepend the
9790 -- pragma to the list of declarations right away so that it
9791 -- will be executed at the start of the procedure. Note that
9792 -- this processing reverses the order of the list, which is
9793 -- what we want since new entries were chained to the head of
9794 -- the list. There can be more than one precondition when we
9795 -- use pragma Precondition.
9797 if not Class_Present (Prag) then
9798 Prepend (Grab_PPC, Declarations (N));
9800 -- For Pre'Class there can only be one pragma, and we save
9801 -- it in Precond for now. We will add inherited Pre'Class
9802 -- stuff before inserting this pragma in the declarations.
9804 Precond := Grab_PPC;
9808 Prag := Next_Pragma (Prag);
9811 -- Now deal with inherited preconditions
9813 for J in Inherited'Range loop
9814 Prag := Spec_PPC_List (Contract (Inherited (J)));
9816 while Present (Prag) loop
9817 if Pragma_Name (Prag) = Name_Precondition
9818 and then Class_Present (Prag)
9820 Inherited_Precond := Grab_PPC (Inherited (J));
9822 -- No precondition so far, so establish this as the first
9824 if No (Precond) then
9825 Precond := Inherited_Precond;
9827 -- Here we already have a precondition, add inherited one
9830 -- Add new precondition to old one using OR ELSE
9833 New_Expr : constant Node_Id :=
9837 (Pragma_Argument_Associations
9838 (Inherited_Precond))));
9839 Old_Expr : constant Node_Id :=
9843 (Pragma_Argument_Associations
9847 if Paren_Count (Old_Expr) = 0 then
9848 Set_Paren_Count (Old_Expr, 1);
9851 if Paren_Count (New_Expr) = 0 then
9852 Set_Paren_Count (New_Expr, 1);
9856 Make_Or_Else (Sloc (Old_Expr),
9857 Left_Opnd => Relocate_Node (Old_Expr),
9858 Right_Opnd => New_Expr));
9861 -- Add new message in the form:
9863 -- failed precondition from bla
9864 -- also failed inherited precondition from bla
9867 -- Skip this if exception locations are suppressed
9869 if not Exception_Locations_Suppressed then
9871 New_Msg : constant Node_Id :=
9874 (Pragma_Argument_Associations
9875 (Inherited_Precond)));
9876 Old_Msg : constant Node_Id :=
9879 (Pragma_Argument_Associations
9882 Start_String (Strval (Old_Msg));
9883 Store_String_Chars (ASCII.LF & " also ");
9884 Store_String_Chars (Strval (New_Msg));
9885 Set_Strval (Old_Msg, End_String);
9891 Prag := Next_Pragma (Prag);
9895 -- If we have built a precondition for Pre'Class (including any
9896 -- Pre'Class aspects inherited from parent subprograms), then we
9897 -- insert this composite precondition at this stage.
9899 if Present (Precond) then
9900 Prepend (Precond, Declarations (N));
9904 -- Build postconditions procedure if needed and prepend the following
9905 -- declaration to the start of the declarations for the subprogram.
9907 -- procedure _postconditions [(_Result : resulttype)] is
9909 -- pragma Check (Postcondition, condition [,message]);
9910 -- pragma Check (Postcondition, condition [,message]);
9912 -- Invariant_Procedure (_Result) ...
9913 -- Invariant_Procedure (Arg1)
9917 -- First we deal with the postconditions in the body
9919 if Is_Non_Empty_List (Declarations (N)) then
9921 -- Loop through declarations
9923 Prag := First (Declarations (N));
9924 while Present (Prag) loop
9925 if Nkind (Prag) = N_Pragma then
9927 -- If pragma, capture if enabled postcondition, else ignore
9929 if Pragma_Name (Prag) = Name_Postcondition
9930 and then Check_Enabled (Name_Postcondition)
9932 if Plist = No_List then
9933 Plist := Empty_List;
9938 -- If expansion is disabled, as in a generic unit, save
9939 -- pragma for later expansion.
9941 if not Expander_Active then
9942 Prepend (Grab_PPC, Declarations (N));
9944 Append (Grab_PPC, Plist);
9950 -- Not a pragma, if comes from source, then end scan
9952 elsif Comes_From_Source (Prag) then
9955 -- Skip stuff not coming from source
9963 -- Now deal with any postconditions from the spec
9965 if Present (Spec_Id) then
9966 Spec_Postconditions : declare
9967 procedure Process_Post_Conditions
9970 -- This processes the Spec_PPC_List from Spec, processing any
9971 -- postconditions from the list. If Class is True, then only
9972 -- postconditions marked with Class_Present are considered.
9973 -- The caller has checked that Spec_PPC_List is non-Empty.
9975 -----------------------------
9976 -- Process_Post_Conditions --
9977 -----------------------------
9979 procedure Process_Post_Conditions
9992 -- Loop through PPC pragmas from spec
9994 Prag := Spec_PPC_List (Contract (Spec));
9996 if Pragma_Name (Prag) = Name_Postcondition
9997 and then (not Class or else Class_Present (Prag))
9999 if Plist = No_List then
10000 Plist := Empty_List;
10003 if not Expander_Active then
10005 (Grab_PPC (Pspec), Declarations (N));
10007 Append (Grab_PPC (Pspec), Plist);
10011 Prag := Next_Pragma (Prag);
10012 exit when No (Prag);
10014 end Process_Post_Conditions;
10016 -- Start of processing for Spec_Postconditions
10019 if Present (Spec_PPC_List (Contract (Spec_Id))) then
10020 Process_Post_Conditions (Spec_Id, Class => False);
10023 -- Process inherited postconditions
10025 for J in Inherited'Range loop
10026 if Present (Spec_PPC_List (Contract (Inherited (J)))) then
10027 Process_Post_Conditions (Inherited (J), Class => True);
10030 end Spec_Postconditions;
10033 -- If we had any postconditions and expansion is enabled, or if the
10034 -- procedure has invariants, then build the _Postconditions procedure.
10036 if (Present (Plist) or else Invariants_Or_Predicates_Present)
10037 and then Expander_Active
10040 Plist := Empty_List;
10043 -- Special processing for function case
10045 if Ekind (Designator) /= E_Procedure then
10047 Rent : constant Entity_Id :=
10048 Make_Defining_Identifier (Loc, Name_uResult);
10049 Ftyp : constant Entity_Id := Etype (Designator);
10052 Set_Etype (Rent, Ftyp);
10054 -- Add argument for return
10058 Make_Parameter_Specification (Loc,
10059 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
10060 Defining_Identifier => Rent));
10062 -- Add invariant call if returning type with invariants and
10063 -- this is a public function, i.e. a function declared in the
10064 -- visible part of the package defining the private type.
10066 if Has_Invariants (Etype (Rent))
10067 and then Present (Invariant_Procedure (Etype (Rent)))
10068 and then Is_Public_Subprogram_For (Etype (Rent))
10071 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
10075 -- Procedure rather than a function
10081 -- Add invariant calls and predicate calls for parameters. Note that
10082 -- this is done for functions as well, since in Ada 2012 they can
10083 -- have IN OUT args.
10086 Formal : Entity_Id;
10090 Formal := First_Formal (Designator);
10091 while Present (Formal) loop
10092 if Ekind (Formal) /= E_In_Parameter then
10093 Ftype := Etype (Formal);
10095 if Has_Invariants (Ftype)
10096 and then Present (Invariant_Procedure (Ftype))
10097 and then Is_Public_Subprogram_For (Ftype)
10100 Make_Invariant_Call
10101 (New_Occurrence_Of (Formal, Loc)));
10104 if Present (Predicate_Function (Ftype)) then
10106 Make_Predicate_Check
10107 (Ftype, New_Occurrence_Of (Formal, Loc)));
10111 Next_Formal (Formal);
10115 -- Build and insert postcondition procedure
10118 Post_Proc : constant Entity_Id :=
10119 Make_Defining_Identifier (Loc,
10120 Chars => Name_uPostconditions);
10121 -- The entity for the _Postconditions procedure
10124 Prepend_To (Declarations (N),
10125 Make_Subprogram_Body (Loc,
10127 Make_Procedure_Specification (Loc,
10128 Defining_Unit_Name => Post_Proc,
10129 Parameter_Specifications => Parms),
10131 Declarations => Empty_List,
10133 Handled_Statement_Sequence =>
10134 Make_Handled_Sequence_Of_Statements (Loc,
10135 Statements => Plist)));
10137 Set_Ekind (Post_Proc, E_Procedure);
10139 -- If this is a procedure, set the Postcondition_Proc attribute on
10140 -- the proper defining entity for the subprogram.
10142 if Ekind (Designator) = E_Procedure then
10143 Set_Postcondition_Proc (Designator, Post_Proc);
10147 Set_Has_Postconditions (Designator);
10151 ----------------------------
10152 -- Reference_Body_Formals --
10153 ----------------------------
10155 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
10160 if Error_Posted (Spec) then
10164 -- Iterate over both lists. They may be of different lengths if the two
10165 -- specs are not conformant.
10167 Fs := First_Formal (Spec);
10168 Fb := First_Formal (Bod);
10169 while Present (Fs) and then Present (Fb) loop
10170 Generate_Reference (Fs, Fb, 'b');
10172 if Style_Check then
10173 Style.Check_Identifier (Fb, Fs);
10176 Set_Spec_Entity (Fb, Fs);
10177 Set_Referenced (Fs, False);
10181 end Reference_Body_Formals;
10183 -------------------------
10184 -- Set_Actual_Subtypes --
10185 -------------------------
10187 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
10189 Formal : Entity_Id;
10191 First_Stmt : Node_Id := Empty;
10192 AS_Needed : Boolean;
10195 -- If this is an empty initialization procedure, no need to create
10196 -- actual subtypes (small optimization).
10198 if Ekind (Subp) = E_Procedure
10199 and then Is_Null_Init_Proc (Subp)
10204 Formal := First_Formal (Subp);
10205 while Present (Formal) loop
10206 T := Etype (Formal);
10208 -- We never need an actual subtype for a constrained formal
10210 if Is_Constrained (T) then
10211 AS_Needed := False;
10213 -- If we have unknown discriminants, then we do not need an actual
10214 -- subtype, or more accurately we cannot figure it out! Note that
10215 -- all class-wide types have unknown discriminants.
10217 elsif Has_Unknown_Discriminants (T) then
10218 AS_Needed := False;
10220 -- At this stage we have an unconstrained type that may need an
10221 -- actual subtype. For sure the actual subtype is needed if we have
10222 -- an unconstrained array type.
10224 elsif Is_Array_Type (T) then
10227 -- The only other case needing an actual subtype is an unconstrained
10228 -- record type which is an IN parameter (we cannot generate actual
10229 -- subtypes for the OUT or IN OUT case, since an assignment can
10230 -- change the discriminant values. However we exclude the case of
10231 -- initialization procedures, since discriminants are handled very
10232 -- specially in this context, see the section entitled "Handling of
10233 -- Discriminants" in Einfo.
10235 -- We also exclude the case of Discrim_SO_Functions (functions used
10236 -- in front end layout mode for size/offset values), since in such
10237 -- functions only discriminants are referenced, and not only are such
10238 -- subtypes not needed, but they cannot always be generated, because
10239 -- of order of elaboration issues.
10241 elsif Is_Record_Type (T)
10242 and then Ekind (Formal) = E_In_Parameter
10243 and then Chars (Formal) /= Name_uInit
10244 and then not Is_Unchecked_Union (T)
10245 and then not Is_Discrim_SO_Function (Subp)
10249 -- All other cases do not need an actual subtype
10252 AS_Needed := False;
10255 -- Generate actual subtypes for unconstrained arrays and
10256 -- unconstrained discriminated records.
10259 if Nkind (N) = N_Accept_Statement then
10261 -- If expansion is active, the formal is replaced by a local
10262 -- variable that renames the corresponding entry of the
10263 -- parameter block, and it is this local variable that may
10264 -- require an actual subtype.
10266 if Full_Expander_Active then
10267 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
10269 Decl := Build_Actual_Subtype (T, Formal);
10272 if Present (Handled_Statement_Sequence (N)) then
10274 First (Statements (Handled_Statement_Sequence (N)));
10275 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
10276 Mark_Rewrite_Insertion (Decl);
10278 -- If the accept statement has no body, there will be no
10279 -- reference to the actuals, so no need to compute actual
10286 Decl := Build_Actual_Subtype (T, Formal);
10287 Prepend (Decl, Declarations (N));
10288 Mark_Rewrite_Insertion (Decl);
10291 -- The declaration uses the bounds of an existing object, and
10292 -- therefore needs no constraint checks.
10294 Analyze (Decl, Suppress => All_Checks);
10296 -- We need to freeze manually the generated type when it is
10297 -- inserted anywhere else than in a declarative part.
10299 if Present (First_Stmt) then
10300 Insert_List_Before_And_Analyze (First_Stmt,
10301 Freeze_Entity (Defining_Identifier (Decl), N));
10304 if Nkind (N) = N_Accept_Statement
10305 and then Full_Expander_Active
10307 Set_Actual_Subtype (Renamed_Object (Formal),
10308 Defining_Identifier (Decl));
10310 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
10314 Next_Formal (Formal);
10316 end Set_Actual_Subtypes;
10318 ---------------------
10319 -- Set_Formal_Mode --
10320 ---------------------
10322 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
10323 Spec : constant Node_Id := Parent (Formal_Id);
10326 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
10327 -- since we ensure that corresponding actuals are always valid at the
10328 -- point of the call.
10330 if Out_Present (Spec) then
10331 if Ekind (Scope (Formal_Id)) = E_Function
10332 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
10334 -- [IN] OUT parameters allowed for functions in Ada 2012
10336 if Ada_Version >= Ada_2012 then
10337 if In_Present (Spec) then
10338 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10340 Set_Ekind (Formal_Id, E_Out_Parameter);
10343 -- But not in earlier versions of Ada
10346 Error_Msg_N ("functions can only have IN parameters", Spec);
10347 Set_Ekind (Formal_Id, E_In_Parameter);
10350 elsif In_Present (Spec) then
10351 Set_Ekind (Formal_Id, E_In_Out_Parameter);
10354 Set_Ekind (Formal_Id, E_Out_Parameter);
10355 Set_Never_Set_In_Source (Formal_Id, True);
10356 Set_Is_True_Constant (Formal_Id, False);
10357 Set_Current_Value (Formal_Id, Empty);
10361 Set_Ekind (Formal_Id, E_In_Parameter);
10364 -- Set Is_Known_Non_Null for access parameters since the language
10365 -- guarantees that access parameters are always non-null. We also set
10366 -- Can_Never_Be_Null, since there is no way to change the value.
10368 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
10370 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
10371 -- null; In Ada 2005, only if then null_exclusion is explicit.
10373 if Ada_Version < Ada_2005
10374 or else Can_Never_Be_Null (Etype (Formal_Id))
10376 Set_Is_Known_Non_Null (Formal_Id);
10377 Set_Can_Never_Be_Null (Formal_Id);
10380 -- Ada 2005 (AI-231): Null-exclusion access subtype
10382 elsif Is_Access_Type (Etype (Formal_Id))
10383 and then Can_Never_Be_Null (Etype (Formal_Id))
10385 Set_Is_Known_Non_Null (Formal_Id);
10387 -- We can also set Can_Never_Be_Null (thus preventing some junk
10388 -- access checks) for the case of an IN parameter, which cannot
10389 -- be changed, or for an IN OUT parameter, which can be changed but
10390 -- not to a null value. But for an OUT parameter, the initial value
10391 -- passed in can be null, so we can't set this flag in that case.
10393 if Ekind (Formal_Id) /= E_Out_Parameter then
10394 Set_Can_Never_Be_Null (Formal_Id);
10398 Set_Mechanism (Formal_Id, Default_Mechanism);
10399 Set_Formal_Validity (Formal_Id);
10400 end Set_Formal_Mode;
10402 -------------------------
10403 -- Set_Formal_Validity --
10404 -------------------------
10406 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
10408 -- If no validity checking, then we cannot assume anything about the
10409 -- validity of parameters, since we do not know there is any checking
10410 -- of the validity on the call side.
10412 if not Validity_Checks_On then
10415 -- If validity checking for parameters is enabled, this means we are
10416 -- not supposed to make any assumptions about argument values.
10418 elsif Validity_Check_Parameters then
10421 -- If we are checking in parameters, we will assume that the caller is
10422 -- also checking parameters, so we can assume the parameter is valid.
10424 elsif Ekind (Formal_Id) = E_In_Parameter
10425 and then Validity_Check_In_Params
10427 Set_Is_Known_Valid (Formal_Id, True);
10429 -- Similar treatment for IN OUT parameters
10431 elsif Ekind (Formal_Id) = E_In_Out_Parameter
10432 and then Validity_Check_In_Out_Params
10434 Set_Is_Known_Valid (Formal_Id, True);
10436 end Set_Formal_Validity;
10438 ------------------------
10439 -- Subtype_Conformant --
10440 ------------------------
10442 function Subtype_Conformant
10443 (New_Id : Entity_Id;
10444 Old_Id : Entity_Id;
10445 Skip_Controlling_Formals : Boolean := False) return Boolean
10449 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
10450 Skip_Controlling_Formals => Skip_Controlling_Formals);
10452 end Subtype_Conformant;
10454 ---------------------
10455 -- Type_Conformant --
10456 ---------------------
10458 function Type_Conformant
10459 (New_Id : Entity_Id;
10460 Old_Id : Entity_Id;
10461 Skip_Controlling_Formals : Boolean := False) return Boolean
10465 May_Hide_Profile := False;
10468 (New_Id, Old_Id, Type_Conformant, False, Result,
10469 Skip_Controlling_Formals => Skip_Controlling_Formals);
10471 end Type_Conformant;
10473 -------------------------------
10474 -- Valid_Operator_Definition --
10475 -------------------------------
10477 procedure Valid_Operator_Definition (Designator : Entity_Id) is
10480 Id : constant Name_Id := Chars (Designator);
10484 F := First_Formal (Designator);
10485 while Present (F) loop
10488 if Present (Default_Value (F)) then
10490 ("default values not allowed for operator parameters",
10497 -- Verify that user-defined operators have proper number of arguments
10498 -- First case of operators which can only be unary
10500 if Id = Name_Op_Not
10501 or else Id = Name_Op_Abs
10505 -- Case of operators which can be unary or binary
10507 elsif Id = Name_Op_Add
10508 or Id = Name_Op_Subtract
10510 N_OK := (N in 1 .. 2);
10512 -- All other operators can only be binary
10520 ("incorrect number of arguments for operator", Designator);
10524 and then Base_Type (Etype (Designator)) = Standard_Boolean
10525 and then not Is_Intrinsic_Subprogram (Designator)
10528 ("explicit definition of inequality not allowed", Designator);
10530 end Valid_Operator_Definition;