MATCH: Improve `A CMP 0 ? A : -A` set of patterns to use bitwise_equal_p.
[official-gcc.git] / gcc / ada / sem_ch6.adb
bloba0dad86149f65523984d95ebdd7367a903f94a64
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- S E M _ C H 6 --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 1992-2023, Free Software Foundation, Inc. --
10 -- --
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. --
20 -- --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
23 -- --
24 ------------------------------------------------------------------------------
26 with Accessibility; use Accessibility;
27 with Aspects; use Aspects;
28 with Atree; use Atree;
29 with Checks; use Checks;
30 with Contracts; use Contracts;
31 with Debug; use Debug;
32 with Einfo; use Einfo;
33 with Einfo.Entities; use Einfo.Entities;
34 with Einfo.Utils; use Einfo.Utils;
35 with Elists; use Elists;
36 with Errout; use Errout;
37 with Expander; use Expander;
38 with Exp_Ch3; use Exp_Ch3;
39 with Exp_Ch6; use Exp_Ch6;
40 with Exp_Ch9; use Exp_Ch9;
41 with Exp_Dbug; use Exp_Dbug;
42 with Exp_Tss; use Exp_Tss;
43 with Exp_Util; use Exp_Util;
44 with Freeze; use Freeze;
45 with Ghost; use Ghost;
46 with Inline; use Inline;
47 with Itypes; use Itypes;
48 with Lib.Xref; use Lib.Xref;
49 with Layout; use Layout;
50 with Namet; use Namet;
51 with Lib; use Lib;
52 with Nlists; use Nlists;
53 with Nmake; use Nmake;
54 with Opt; use Opt;
55 with Output; use Output;
56 with Restrict; use Restrict;
57 with Rtsfind; use Rtsfind;
58 with Sem; use Sem;
59 with Sem_Aux; use Sem_Aux;
60 with Sem_Cat; use Sem_Cat;
61 with Sem_Ch3; use Sem_Ch3;
62 with Sem_Ch4; use Sem_Ch4;
63 with Sem_Ch5; use Sem_Ch5;
64 with Sem_Ch8; use Sem_Ch8;
65 with Sem_Ch9; use Sem_Ch9;
66 with Sem_Ch10; use Sem_Ch10;
67 with Sem_Ch12; use Sem_Ch12;
68 with Sem_Ch13; use Sem_Ch13;
69 with Sem_Dim; use Sem_Dim;
70 with Sem_Disp; use Sem_Disp;
71 with Sem_Dist; use Sem_Dist;
72 with Sem_Elim; use Sem_Elim;
73 with Sem_Eval; use Sem_Eval;
74 with Sem_Mech; use Sem_Mech;
75 with Sem_Prag; use Sem_Prag;
76 with Sem_Res; use Sem_Res;
77 with Sem_Util; use Sem_Util;
78 with Sem_Type; use Sem_Type;
79 with Sem_Warn; use Sem_Warn;
80 with Sinput; use Sinput;
81 with Stand; use Stand;
82 with Sinfo; use Sinfo;
83 with Sinfo.Nodes; use Sinfo.Nodes;
84 with Sinfo.Utils; use Sinfo.Utils;
85 with Sinfo.CN; use Sinfo.CN;
86 with Snames; use Snames;
87 with Stringt; use Stringt;
88 with Style;
89 with Stylesw; use Stylesw;
90 with Tbuild; use Tbuild;
91 with Uintp; use Uintp;
92 with Urealp; use Urealp;
93 with Validsw; use Validsw;
94 with Warnsw; use Warnsw;
96 package body Sem_Ch6 is
98 May_Hide_Profile : Boolean := False;
99 -- This flag is used to indicate that two formals in two subprograms being
100 -- checked for conformance differ only in that one is an access parameter
101 -- while the other is of a general access type with the same designated
102 -- type. In this case, if the rest of the signatures match, a call to
103 -- either subprogram may be ambiguous, which is worth a warning. The flag
104 -- is set in Compatible_Types, and the warning emitted in
105 -- New_Overloaded_Entity.
107 -----------------------
108 -- Local Subprograms --
109 -----------------------
111 procedure Analyze_Function_Return (N : Node_Id);
112 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
113 -- applies to a [generic] function.
115 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
116 -- Analyze a generic subprogram body. N is the body to be analyzed, and
117 -- Gen_Id is the defining entity Id for the corresponding spec.
119 procedure Analyze_Null_Procedure
120 (N : Node_Id;
121 Is_Completion : out Boolean);
122 -- A null procedure can be a declaration or (Ada 2012) a completion
124 procedure Analyze_Return_Statement (N : Node_Id);
125 -- Common processing for simple and extended return statements
127 procedure Analyze_Return_Type (N : Node_Id);
128 -- Subsidiary to Process_Formals: analyze subtype mark in function
129 -- specification in a context where the formals are visible and hide
130 -- outer homographs.
132 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
133 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
134 -- that we can use RETURN but not skip the debug output at the end.
136 procedure Check_Conformance
137 (New_Id : Entity_Id;
138 Old_Id : Entity_Id;
139 Ctype : Conformance_Type;
140 Errmsg : Boolean;
141 Conforms : out Boolean;
142 Err_Loc : Node_Id := Empty;
143 Get_Inst : Boolean := False;
144 Skip_Controlling_Formals : Boolean := False);
145 -- Given two entities, this procedure checks that the profiles associated
146 -- with these entities meet the conformance criterion given by the third
147 -- parameter. If they conform, Conforms is set True and control returns
148 -- to the caller. If they do not conform, Conforms is set to False, and
149 -- in addition, if Errmsg is True on the call, proper messages are output
150 -- to complain about the conformance failure. If Err_Loc is non_Empty
151 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
152 -- error messages are placed on the appropriate part of the construct
153 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
154 -- against a formal access-to-subprogram type so Get_Instance_Of must
155 -- be called.
157 procedure Check_Formal_Subprogram_Conformance
158 (New_Id : Entity_Id;
159 Old_Id : Entity_Id;
160 Err_Loc : Node_Id;
161 Errmsg : Boolean;
162 Conforms : out Boolean);
163 -- Core implementation of Check_Formal_Subprogram_Conformance from spec.
164 -- Errmsg can be set to False to not emit error messages.
165 -- Conforms is set to True if there is conformance, False otherwise.
167 procedure Check_Limited_Return
168 (N : Node_Id;
169 Expr : Node_Id;
170 R_Type : Entity_Id);
171 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
172 -- types. Used only for simple return statements. Expr is the expression
173 -- returned.
175 procedure Check_Subprogram_Order (N : Node_Id);
176 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
177 -- the alpha ordering rule for N if this ordering requirement applicable.
179 procedure Check_Returns
180 (HSS : Node_Id;
181 Mode : Character;
182 Err : out Boolean;
183 Proc : Entity_Id := Empty);
184 -- Called to check for missing return statements in a function body, or for
185 -- returns present in a procedure body which has No_Return set. HSS is the
186 -- handled statement sequence for the subprogram body. This procedure
187 -- checks all flow paths to make sure they either have return (Mode = 'F',
188 -- used for functions) or do not have a return (Mode = 'P', used for
189 -- No_Return procedures). The flag Err is set if there are any control
190 -- paths not explicitly terminated by a return in the function case, and is
191 -- True otherwise. Proc is the entity for the procedure case and is used
192 -- in posting the warning message.
194 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
195 -- In Ada 2012, a primitive equality operator for an untagged record type
196 -- must appear before the type is frozen. This procedure checks that this
197 -- rule is met, and otherwise gives an error on the subprogram declaration
198 -- and a warning on the earlier freeze point if it is easy to pinpoint. In
199 -- earlier versions of Ada, the call has not effect, unless compatibility
200 -- warnings are requested by means of Warn_On_Ada_2012_Incompatibility.
202 procedure Enter_Overloaded_Entity (S : Entity_Id);
203 -- This procedure makes S, a new overloaded entity, into the first visible
204 -- entity with that name.
206 function Has_Reliable_Extra_Formals (E : Entity_Id) return Boolean;
207 -- E is the entity for a subprogram spec. Returns False for abstract
208 -- predefined dispatching primitives of Root_Controlled since they
209 -- cannot have extra formals (this is required to build the runtime);
210 -- it also returns False for predefined stream dispatching operations
211 -- not emitted by the frontend. Otherwise returns True.
213 function Is_Non_Overriding_Operation
214 (Prev_E : Entity_Id;
215 New_E : Entity_Id) return Boolean;
216 -- Enforce the rule given in 12.3(18): a private operation in an instance
217 -- overrides an inherited operation only if the corresponding operation
218 -- was overriding in the generic. This needs to be checked for primitive
219 -- operations of types derived (in the generic unit) from formal private
220 -- or formal derived types.
222 procedure Make_Inequality_Operator (S : Entity_Id);
223 -- Create the declaration for an inequality operator that is implicitly
224 -- created by a user-defined equality operator that yields a boolean.
226 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id);
227 -- Preanalysis of default expressions of subprogram formals. N is the
228 -- expression to be analyzed and T is the expected type.
230 procedure Set_Formal_Mode (Formal_Id : Entity_Id);
231 -- Set proper Ekind to reflect formal mode (in, out, in out), and set
232 -- miscellaneous other attributes.
234 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
235 -- Formal_Id is an formal parameter entity. This procedure deals with
236 -- setting the proper validity status for this entity, which depends on
237 -- the kind of parameter and the validity checking mode.
239 ---------------------------------------------
240 -- Analyze_Abstract_Subprogram_Declaration --
241 ---------------------------------------------
243 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
244 Scop : constant Entity_Id := Current_Scope;
245 Subp_Id : constant Entity_Id :=
246 Analyze_Subprogram_Specification (Specification (N));
248 begin
249 Generate_Definition (Subp_Id);
251 -- Set the SPARK mode from the current context (may be overwritten later
252 -- with explicit pragma).
254 Set_SPARK_Pragma (Subp_Id, SPARK_Mode_Pragma);
255 Set_SPARK_Pragma_Inherited (Subp_Id);
257 -- Preserve relevant elaboration-related attributes of the context which
258 -- are no longer available or very expensive to recompute once analysis,
259 -- resolution, and expansion are over.
261 Mark_Elaboration_Attributes
262 (N_Id => Subp_Id,
263 Checks => True,
264 Warnings => True);
266 Set_Is_Abstract_Subprogram (Subp_Id);
267 New_Overloaded_Entity (Subp_Id);
268 Check_Delayed_Subprogram (Subp_Id);
270 Set_Categorization_From_Scope (Subp_Id, Scop);
272 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
273 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
275 -- Issue a warning if the abstract subprogram is neither a dispatching
276 -- operation nor an operation that overrides an inherited subprogram or
277 -- predefined operator, since this most likely indicates a mistake.
279 elsif Warn_On_Redundant_Constructs
280 and then not Is_Dispatching_Operation (Subp_Id)
281 and then No (Overridden_Operation (Subp_Id))
282 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
283 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
284 then
285 Error_Msg_N
286 ("abstract subprogram is not dispatching or overriding?r?", N);
287 end if;
289 Generate_Reference_To_Formals (Subp_Id);
290 Check_Eliminated (Subp_Id);
292 if Has_Aspects (N) then
293 Analyze_Aspect_Specifications (N, Subp_Id);
294 end if;
295 end Analyze_Abstract_Subprogram_Declaration;
297 ---------------------------------
298 -- Analyze_Expression_Function --
299 ---------------------------------
301 procedure Analyze_Expression_Function (N : Node_Id) is
302 Expr : constant Node_Id := Expression (N);
303 Loc : constant Source_Ptr := Sloc (N);
304 LocX : constant Source_Ptr := Sloc (Expr);
305 Spec : constant Node_Id := Specification (N);
307 -- Local variables
309 Asp : Node_Id;
310 New_Body : Node_Id;
311 New_Spec : Node_Id;
312 Orig_N : Node_Id := Empty;
313 Ret : Node_Id;
314 Typ : Entity_Id := Empty;
316 Def_Id : Entity_Id := Empty;
317 Prev : Entity_Id;
318 -- If the expression is a completion, Prev is the entity whose
319 -- declaration is completed. Def_Id is needed to analyze the spec.
321 begin
322 -- This is one of the occasions on which we transform the tree during
323 -- semantic analysis. If this is a completion, transform the expression
324 -- function into an equivalent subprogram body, and analyze it.
326 -- Expression functions are inlined unconditionally. The back-end will
327 -- determine whether this is possible.
329 Inline_Processing_Required := True;
331 -- Create a specification for the generated body. This must be done
332 -- prior to the analysis of the initial declaration.
334 New_Spec := Copy_Subprogram_Spec (Spec);
335 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
337 -- If there are previous overloadable entities with the same name,
338 -- check whether any of them is completed by the expression function.
339 -- In a generic context a formal subprogram has no completion.
341 if Present (Prev)
342 and then Is_Overloadable (Prev)
343 and then not Is_Formal_Subprogram (Prev)
344 then
345 Def_Id := Analyze_Subprogram_Specification (Spec);
346 Prev := Find_Corresponding_Spec (N);
348 Typ := Etype (Def_Id);
350 -- The previous entity may be an expression function as well, in
351 -- which case the redeclaration is illegal.
353 if Present (Prev)
354 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
355 N_Expression_Function
356 then
357 Error_Msg_Sloc := Sloc (Prev);
358 Error_Msg_N ("& conflicts with declaration#", Def_Id);
359 return;
360 end if;
361 end if;
363 Ret := Make_Simple_Return_Statement (LocX, Expr);
365 -- Remove parens around the expression, so that if the expression will
366 -- appear without them when pretty-printed in error messages.
368 if Paren_Count (Expr) > 0 then
369 Set_Paren_Count (Expr, Paren_Count (Expr) - 1);
370 end if;
372 New_Body :=
373 Make_Subprogram_Body (Loc,
374 Specification => New_Spec,
375 Declarations => Empty_List,
376 Handled_Statement_Sequence =>
377 Make_Handled_Sequence_Of_Statements (LocX,
378 Statements => New_List (Ret)));
379 Set_Was_Expression_Function (New_Body);
381 -- If the expression completes a generic subprogram, we must create a
382 -- separate node for the body, because at instantiation the original
383 -- node of the generic copy must be a generic subprogram body, and
384 -- cannot be a expression function. Otherwise we just rewrite the
385 -- expression with the non-generic body.
387 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
388 Insert_After (N, New_Body);
390 -- Propagate any aspects or pragmas that apply to the expression
391 -- function to the proper body when the expression function acts
392 -- as a completion.
394 Move_Aspects (N, To => New_Body);
396 Relocate_Pragmas_To_Body (New_Body);
398 Rewrite (N, Make_Null_Statement (Loc));
399 Set_Has_Completion (Prev, False);
400 Analyze (N);
401 Analyze (New_Body);
402 Set_Is_Inlined (Prev);
404 elsif Present (Prev)
405 and then Is_Overloadable (Prev)
406 and then not Is_Formal_Subprogram (Prev)
407 then
408 Set_Has_Completion (Prev, False);
409 Set_Is_Inlined (Prev);
411 -- AI12-0103: Expression functions that are a completion freeze their
412 -- expression but don't freeze anything else (unlike regular bodies).
414 -- Note that we cannot defer this freezing to the analysis of the
415 -- expression itself, because a freeze node might appear in a nested
416 -- scope, leading to an elaboration order issue in gigi.
417 -- As elsewhere, we do not emit freeze nodes within a generic unit.
419 if not Inside_A_Generic then
420 Freeze_Expr_Types
421 (Def_Id => Def_Id,
422 Typ => Typ,
423 Expr => Expr,
424 N => N);
425 end if;
427 -- For navigation purposes, indicate that the function is a body
429 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
430 Rewrite (N, New_Body);
432 -- Remove any existing aspects from the original node because the act
433 -- of rewriting causes the list to be shared between the two nodes.
435 Orig_N := Original_Node (N);
436 Remove_Aspects (Orig_N);
438 -- Propagate any pragmas that apply to expression function to the
439 -- proper body when the expression function acts as a completion.
440 -- Aspects are automatically transfered because of node rewriting.
442 Relocate_Pragmas_To_Body (N);
443 Analyze (N);
445 -- Prev is the previous entity with the same name, but it is can
446 -- be an unrelated spec that is not completed by the expression
447 -- function. In that case the relevant entity is the one in the body.
448 -- Not clear that the backend can inline it in this case ???
450 if Has_Completion (Prev) then
452 -- The formals of the expression function are body formals,
453 -- and do not appear in the ali file, which will only contain
454 -- references to the formals of the original subprogram spec.
456 declare
457 F1 : Entity_Id;
458 F2 : Entity_Id;
460 begin
461 F1 := First_Formal (Def_Id);
462 F2 := First_Formal (Prev);
464 while Present (F1) loop
465 Set_Spec_Entity (F1, F2);
466 Next_Formal (F1);
467 Next_Formal (F2);
468 end loop;
469 end;
471 else
472 Set_Is_Inlined (Defining_Entity (New_Body));
473 end if;
475 -- If this is not a completion, create both a declaration and a body, so
476 -- that the expression can be inlined whenever possible.
478 else
479 -- An expression function that is not a completion is not a
480 -- subprogram declaration, and thus cannot appear in a protected
481 -- definition.
483 if Nkind (Parent (N)) = N_Protected_Definition then
484 Error_Msg_N
485 ("an expression function is not a legal protected operation", N);
486 end if;
488 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
490 -- Remove any existing aspects from the original node because the act
491 -- of rewriting causes the list to be shared between the two nodes.
493 Orig_N := Original_Node (N);
494 Remove_Aspects (Orig_N);
496 Analyze (N);
498 -- If aspect SPARK_Mode was specified on the body, it needs to be
499 -- repeated both on the generated spec and the body.
501 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
503 if Present (Asp) then
504 Asp := New_Copy_Tree (Asp);
505 Set_Analyzed (Asp, False);
506 Set_Aspect_Specifications (New_Body, New_List (Asp));
507 end if;
509 Def_Id := Defining_Entity (N);
510 Set_Is_Inlined (Def_Id);
512 Typ := Etype (Def_Id);
514 -- Establish the linkages between the spec and the body. These are
515 -- used when the expression function acts as the prefix of attribute
516 -- 'Access in order to freeze the original expression which has been
517 -- moved to the generated body.
519 Set_Corresponding_Body (N, Defining_Entity (New_Body));
520 Set_Corresponding_Spec (New_Body, Def_Id);
522 -- Within a generic preanalyze the original expression for name
523 -- capture. The body is also generated but plays no role in
524 -- this because it is not part of the original source.
525 -- If this is an ignored Ghost entity, analysis of the generated
526 -- body is needed to hide external references (as is done in
527 -- Analyze_Subprogram_Body) after which the subprogram profile
528 -- can be frozen, which is needed to expand calls to such an ignored
529 -- Ghost subprogram.
531 if Inside_A_Generic then
532 Set_Has_Completion (Def_Id, not Is_Ignored_Ghost_Entity (Def_Id));
533 Push_Scope (Def_Id);
534 Install_Formals (Def_Id);
535 Preanalyze_Spec_Expression (Expr, Typ);
536 End_Scope;
537 else
538 Push_Scope (Def_Id);
539 Install_Formals (Def_Id);
540 Preanalyze_Formal_Expression (Expr, Typ);
541 Check_Limited_Return (Orig_N, Expr, Typ);
542 End_Scope;
543 end if;
545 -- If this is a wrapper created in an instance for a formal
546 -- subprogram, insert body after declaration, to be analyzed when the
547 -- enclosing instance is analyzed.
549 if GNATprove_Mode
550 and then Is_Generic_Actual_Subprogram (Def_Id)
551 then
552 Insert_After (N, New_Body);
554 -- To prevent premature freeze action, insert the new body at the end
555 -- of the current declarations, or at the end of the package spec.
556 -- However, resolve usage names now, to prevent spurious visibility
557 -- on later entities. Note that the function can now be called in
558 -- the current declarative part, which will appear to be prior to the
559 -- presence of the body in the code. There are nevertheless no order
560 -- of elaboration issues because all name resolution has taken place
561 -- at the point of declaration.
563 else
564 declare
565 Decls : List_Id := List_Containing (N);
566 Par : constant Node_Id := Parent (Decls);
568 begin
569 if Nkind (Par) = N_Package_Specification
570 and then Decls = Visible_Declarations (Par)
571 and then not Is_Empty_List (Private_Declarations (Par))
572 then
573 Decls := Private_Declarations (Par);
574 end if;
576 Insert_After (Last (Decls), New_Body);
577 end;
578 end if;
580 -- In the case of an expression function marked with the aspect
581 -- Static, we need to check the requirement that the function's
582 -- expression is a potentially static expression. This is done
583 -- by making a full copy of the expression tree and performing
584 -- a special preanalysis on that tree with the global flag
585 -- Checking_Potentially_Static_Expression enabled. If the
586 -- resulting expression is static, then it's OK, but if not, that
587 -- means the expression violates the requirements of the Ada 2022
588 -- RM in 4.9(3.2/5-3.4/5) and we flag an error.
590 if Is_Static_Function (Def_Id) then
591 declare
592 -- If a potentially static expr like "Parameter / 0"
593 -- is transformed into "(raise Constraint_Error)", then we
594 -- need to copy the Original_Node.
595 function Make_Expr_Copy return Node_Id is
596 (New_Copy_Tree (if Expr in N_Raise_xxx_Error_Id
597 then Original_Node (Expr)
598 else Expr));
599 begin
600 if not Is_Static_Expression (Expr) then
601 declare
602 Exp_Copy : constant Node_Id := Make_Expr_Copy;
603 begin
604 Set_Checking_Potentially_Static_Expression (True);
606 Preanalyze_Formal_Expression (Exp_Copy, Typ);
608 if not Is_Static_Expression (Exp_Copy) then
609 Error_Msg_N
610 ("static expression function requires "
611 & "potentially static expression", Expr);
612 end if;
614 Set_Checking_Potentially_Static_Expression (False);
615 end;
616 end if;
618 -- We also make an additional copy of the expression and
619 -- replace the expression of the expression function with
620 -- this copy, because the currently present expression is
621 -- now associated with the body created for the static
622 -- expression function, which will later be analyzed and
623 -- possibly rewritten, and we need to have the separate
624 -- unanalyzed copy available for use with later static
625 -- calls.
627 Set_Expression
628 (Original_Node (Subprogram_Spec (Def_Id)),
629 Make_Expr_Copy);
631 -- Mark static expression functions as inlined, to ensure
632 -- that even calls with nonstatic actuals will be inlined.
634 Set_Has_Pragma_Inline (Def_Id);
635 Set_Is_Inlined (Def_Id);
636 end;
637 end if;
638 end if;
640 -- Check incorrect use of dynamically tagged expression. This doesn't
641 -- fall out automatically when analyzing the generated function body,
642 -- because Check_Dynamically_Tagged_Expression deliberately ignores
643 -- nodes that don't come from source.
645 if Present (Def_Id)
646 and then Is_Tagged_Type (Typ)
647 then
648 Check_Dynamically_Tagged_Expression
649 (Expr => Expr,
650 Typ => Typ,
651 Related_Nod => Orig_N);
652 end if;
654 -- We must enforce checks for unreferenced formals in our newly
655 -- generated function, so we propagate the referenced flag from the
656 -- original spec to the new spec as well as setting Comes_From_Source.
658 if Present (Parameter_Specifications (New_Spec)) then
659 declare
660 Form_New_Def : Entity_Id;
661 Form_New_Spec : Node_Id;
662 Form_Old_Def : Entity_Id;
663 Form_Old_Spec : Node_Id;
665 begin
666 Form_New_Spec := First (Parameter_Specifications (New_Spec));
667 Form_Old_Spec := First (Parameter_Specifications (Spec));
669 while Present (Form_New_Spec) and then Present (Form_Old_Spec) loop
670 Form_New_Def := Defining_Identifier (Form_New_Spec);
671 Form_Old_Def := Defining_Identifier (Form_Old_Spec);
673 Set_Comes_From_Source (Form_New_Def, True);
675 -- Because of the usefulness of unreferenced controlling
676 -- formals we exempt them from unreferenced warnings by marking
677 -- them as always referenced.
679 Set_Referenced (Form_Old_Def,
680 (Is_Formal (Form_Old_Def)
681 and then Is_Controlling_Formal (Form_Old_Def))
682 or else Referenced (Form_Old_Def));
684 Next (Form_New_Spec);
685 Next (Form_Old_Spec);
686 end loop;
687 end;
688 end if;
689 end Analyze_Expression_Function;
691 ---------------------------------------
692 -- Analyze_Extended_Return_Statement --
693 ---------------------------------------
695 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
696 begin
697 Analyze_Return_Statement (N);
698 end Analyze_Extended_Return_Statement;
700 ----------------------------
701 -- Analyze_Function_Call --
702 ----------------------------
704 procedure Analyze_Function_Call (N : Node_Id) is
705 Actuals : constant List_Id := Parameter_Associations (N);
706 Func_Nam : constant Node_Id := Name (N);
707 Actual : Node_Id;
709 begin
710 Analyze (Func_Nam);
712 -- A call of the form A.B (X) may be an Ada 2005 call, which is
713 -- rewritten as B (A, X). If the rewriting is successful, the call
714 -- has been analyzed and we just return.
716 if Nkind (Func_Nam) = N_Selected_Component
717 and then Name (N) /= Func_Nam
718 and then Is_Rewrite_Substitution (N)
719 and then Present (Etype (N))
720 then
721 return;
722 end if;
724 -- If error analyzing name, then set Any_Type as result type and return
726 if Etype (Func_Nam) = Any_Type then
727 Set_Etype (N, Any_Type);
728 return;
729 end if;
731 -- Otherwise analyze the parameters
733 Actual := First (Actuals);
734 while Present (Actual) loop
735 Analyze (Actual);
736 Check_Parameterless_Call (Actual);
737 Next (Actual);
738 end loop;
740 Analyze_Call (N);
741 end Analyze_Function_Call;
743 -----------------------------
744 -- Analyze_Function_Return --
745 -----------------------------
747 procedure Analyze_Function_Return (N : Node_Id) is
748 Loc : constant Source_Ptr := Sloc (N);
749 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
750 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
752 R_Type : constant Entity_Id := Etype (Scope_Id);
753 -- Function result subtype
755 procedure Check_No_Return_Expression (Return_Expr : Node_Id);
756 -- Ada 2022: Check that the return expression in a No_Return function
757 -- meets the conditions specified by RM 6.5.1(5.1/5).
759 --------------------------------
760 -- Check_No_Return_Expression --
761 --------------------------------
763 procedure Check_No_Return_Expression (Return_Expr : Node_Id) is
764 Kind : constant Node_Kind := Nkind (Return_Expr);
766 begin
767 if Kind = N_Raise_Expression then
768 return;
770 elsif Kind = N_Function_Call
771 and then Is_Entity_Name (Name (Return_Expr))
772 and then Ekind (Entity (Name (Return_Expr))) in
773 E_Function | E_Generic_Function
774 and then No_Return (Entity (Name (Return_Expr)))
775 then
776 return;
777 end if;
779 Error_Msg_N
780 ("illegal expression in RETURN statement of No_Return function",
781 Return_Expr);
782 Error_Msg_N
783 ("\must be raise expression or call to No_Return (RM 6.5.1(5.1/5))",
784 Return_Expr);
785 end Check_No_Return_Expression;
787 ---------------------
788 -- Local Variables --
789 ---------------------
791 Expr : Node_Id;
792 Obj_Decl : Node_Id := Empty;
794 -- Start of processing for Analyze_Function_Return
796 begin
797 Set_Return_Present (Scope_Id);
799 if Nkind (N) = N_Simple_Return_Statement then
800 Expr := Expression (N);
802 -- Guard against a malformed expression. The parser may have tried to
803 -- recover but the node is not analyzable.
805 if Nkind (Expr) = N_Error then
806 Set_Etype (Expr, Any_Type);
807 Expander_Mode_Save_And_Set (False);
808 return;
810 else
811 -- The resolution of a controlled [extension] aggregate associated
812 -- with a return statement creates a temporary which needs to be
813 -- finalized on function exit. Wrap the return statement inside a
814 -- block so that the finalization machinery can detect this case.
815 -- This early expansion is done only when the return statement is
816 -- not part of a handled sequence of statements.
818 if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
819 and then Needs_Finalization (R_Type)
820 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
821 then
822 Rewrite (N,
823 Make_Block_Statement (Loc,
824 Handled_Statement_Sequence =>
825 Make_Handled_Sequence_Of_Statements (Loc,
826 Statements => New_List (Relocate_Node (N)))));
828 Analyze (N);
829 return;
830 end if;
832 Analyze (Expr);
834 -- Ada 2005 (AI-251): If the type of the returned object is
835 -- an access to an interface type then we add an implicit type
836 -- conversion to force the displacement of the "this" pointer to
837 -- reference the secondary dispatch table. We cannot delay the
838 -- generation of this implicit conversion until the expansion
839 -- because in this case the type resolution changes the decoration
840 -- of the expression node to match R_Type; by contrast, if the
841 -- returned object is a class-wide interface type then it is too
842 -- early to generate here the implicit conversion since the return
843 -- statement may be rewritten by the expander into an extended
844 -- return statement whose expansion takes care of adding the
845 -- implicit type conversion to displace the pointer to the object.
847 if Expander_Active
848 and then Serious_Errors_Detected = 0
849 and then Is_Access_Type (R_Type)
850 and then Nkind (Expr) not in N_Null | N_Raise_Expression
851 and then Is_Access_Type (Etype (Expr))
852 and then Is_Interface (Designated_Type (R_Type))
853 and then Is_Progenitor (Designated_Type (R_Type),
854 Designated_Type (Etype (Expr)))
855 then
856 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
857 Analyze (Expr);
858 end if;
860 Resolve (Expr, R_Type);
862 -- The expansion of the expression may have rewritten the return
863 -- statement itself, e.g. when it is a conditional expression.
865 if Nkind (N) /= N_Simple_Return_Statement then
866 return;
867 end if;
869 Check_Limited_Return (N, Expr, R_Type);
871 Check_Return_Construct_Accessibility (N, Stm_Entity);
873 -- Ada 2022 (AI12-0269): Any return statement that applies to a
874 -- nonreturning function shall be a simple_return_statement with
875 -- an expression that is a raise_expression, or else a call on a
876 -- nonreturning function, or else a parenthesized expression of
877 -- one of these.
879 if Ada_Version >= Ada_2022
880 and then No_Return (Scope_Id)
881 and then Comes_From_Source (N)
882 then
883 Check_No_Return_Expression (Original_Node (Expr));
884 end if;
885 end if;
886 else
887 Obj_Decl := Last (Return_Object_Declarations (N));
889 -- Analyze parts specific to extended_return_statement:
891 declare
892 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
893 HSS : constant Node_Id := Handled_Statement_Sequence (N);
895 begin
896 Expr := Expression (Obj_Decl);
898 -- Note: The check for OK_For_Limited_Init will happen in
899 -- Analyze_Object_Declaration; we treat it as a normal
900 -- object declaration.
902 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
903 Analyze (Obj_Decl);
905 if Present (HSS) then
906 Analyze (HSS);
908 if Present (Exception_Handlers (HSS)) then
910 -- ???Has_Nested_Block_With_Handler needs to be set.
911 -- Probably by creating an actual N_Block_Statement.
912 -- Probably in Expand.
914 null;
915 end if;
916 end if;
918 -- Mark the return object as referenced, since the return is an
919 -- implicit reference of the object.
921 Set_Referenced (Defining_Identifier (Obj_Decl));
923 Check_References (Stm_Entity);
925 Check_Return_Construct_Accessibility (N, Stm_Entity);
927 -- Check RM 6.5 (5.9/3)
929 if Has_Aliased and then not Is_Immutably_Limited_Type (R_Type) then
930 if Ada_Version < Ada_2012
931 and then Warn_On_Ada_2012_Compatibility
932 then
933 Error_Msg_N
934 ("ALIASED only allowed for immutably limited return " &
935 "objects in Ada 2012?y?", N);
937 else
938 Error_Msg_N
939 ("ALIASED only allowed for immutably limited return " &
940 "objects", N);
941 end if;
942 end if;
944 -- Ada 2022 (AI12-0269): Any return statement that applies to a
945 -- nonreturning function shall be a simple_return_statement.
947 if Ada_Version >= Ada_2022
948 and then No_Return (Scope_Id)
949 and then Comes_From_Source (N)
950 then
951 Error_Msg_N
952 ("extended RETURN statement not allowed in No_Return "
953 & "function", N);
954 end if;
955 end;
956 end if;
958 -- Case of Expr present
960 if Present (Expr) then
962 -- Defend against previous errors
964 if Nkind (Expr) = N_Empty or else No (Etype (Expr)) then
965 return;
966 end if;
968 -- Apply constraint check. Note that this is done before the implicit
969 -- conversion of the expression done for anonymous access types to
970 -- ensure correct generation of the null-excluding check associated
971 -- with null-excluding expressions found in return statements. We
972 -- don't need a check if the subtype of the return object is the
973 -- same as the result subtype of the function.
975 if Nkind (N) /= N_Extended_Return_Statement
976 or else Nkind (Obj_Decl) /= N_Object_Declaration
977 or else Nkind (Object_Definition (Obj_Decl)) not in N_Has_Entity
978 or else Entity (Object_Definition (Obj_Decl)) /= R_Type
979 then
980 Apply_Constraint_Check (Expr, R_Type);
981 end if;
983 -- The return value is converted to the return type of the function,
984 -- which implies a predicate check if the return type is predicated.
985 -- We do not apply the check for an extended return statement because
986 -- Analyze_Object_Declaration has already done it on Obj_Decl above.
987 -- We do not apply the check to a case expression because it will
988 -- be expanded into a series of return statements, each of which
989 -- will receive a predicate check.
991 if Nkind (N) /= N_Extended_Return_Statement
992 and then Nkind (Expr) /= N_Case_Expression
993 then
994 Apply_Predicate_Check (Expr, R_Type);
995 end if;
997 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
998 -- type, apply an implicit conversion of the expression to that type
999 -- to force appropriate static and run-time accessibility checks.
1000 -- But we want to apply the checks to an extended return statement
1001 -- only once, i.e. not to the simple return statement generated at
1002 -- the end of its expansion because, prior to leaving the function,
1003 -- the accessibility level of the return object changes to be a level
1004 -- determined by the point of call (RM 3.10.2(10.8/3)).
1006 if Ada_Version >= Ada_2005
1007 and then Ekind (R_Type) = E_Anonymous_Access_Type
1008 and then (Nkind (N) = N_Extended_Return_Statement
1009 or else not Comes_From_Extended_Return_Statement (N))
1010 then
1011 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1012 Analyze_And_Resolve (Expr, R_Type);
1014 -- If this is a local anonymous access to subprogram, the
1015 -- accessibility check can be applied statically. The return is
1016 -- illegal if the access type of the return expression is declared
1017 -- inside of the subprogram (except if it is the subtype indication
1018 -- of an extended return statement).
1020 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1021 if not Comes_From_Source (Current_Scope)
1022 or else Ekind (Current_Scope) = E_Return_Statement
1023 then
1024 null;
1026 elsif
1027 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1028 then
1029 Error_Msg_N ("cannot return local access to subprogram", N);
1030 end if;
1032 -- The expression cannot be of a formal incomplete type
1034 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1035 and then Is_Generic_Type (Etype (Expr))
1036 then
1037 Error_Msg_N
1038 ("cannot return expression of a formal incomplete type", N);
1039 end if;
1041 -- If the result type is class-wide, then check that the return
1042 -- expression's type is not declared at a deeper level than the
1043 -- function (RM05-6.5(5.6/2)).
1045 if Ada_Version >= Ada_2005
1046 and then Is_Class_Wide_Type (R_Type)
1047 then
1048 if Type_Access_Level (Etype (Expr)) >
1049 Subprogram_Access_Level (Scope_Id)
1050 then
1051 Error_Msg_N
1052 ("level of return expression type is deeper than "
1053 & "class-wide function!", Expr);
1054 end if;
1055 end if;
1057 -- Check incorrect use of dynamically tagged expression
1059 if Is_Tagged_Type (R_Type) then
1060 Check_Dynamically_Tagged_Expression
1061 (Expr => Expr,
1062 Typ => R_Type,
1063 Related_Nod => N);
1064 end if;
1066 -- Perform static accessibility checks for cases involving
1067 -- dereferences of access parameters. Runtime accessibility checks
1068 -- get generated elsewhere.
1070 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1071 and then Is_Limited_View (Etype (Scope_Id))
1072 and then Static_Accessibility_Level (Expr, Zero_On_Dynamic_Level)
1073 > Subprogram_Access_Level (Scope_Id)
1074 then
1075 -- Suppress the message in a generic, where the rewriting
1076 -- is irrelevant.
1078 if Inside_A_Generic then
1079 null;
1081 else
1082 Rewrite (N,
1083 Make_Raise_Program_Error (Loc,
1084 Reason => PE_Accessibility_Check_Failed));
1085 Analyze (N);
1087 Error_Msg_Warn := SPARK_Mode /= On;
1088 Error_Msg_N ("cannot return a local value by reference<<", N);
1089 Error_Msg_N ("\Program_Error [<<", N);
1090 end if;
1091 end if;
1093 if Known_Null (Expr)
1094 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1095 and then Null_Exclusion_Present (Parent (Scope_Id))
1096 then
1097 Apply_Compile_Time_Constraint_Error
1098 (N => Expr,
1099 Msg => "(Ada 2005) null not allowed for "
1100 & "null-excluding return??",
1101 Reason => CE_Null_Not_Allowed);
1102 end if;
1104 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1105 -- has no initializing expression.
1107 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1108 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1109 Subprogram_Access_Level (Scope_Id)
1110 then
1111 Error_Msg_N
1112 ("level of return expression type is deeper than "
1113 & "class-wide function!", Obj_Decl);
1114 end if;
1115 end if;
1116 end Analyze_Function_Return;
1118 -------------------------------------
1119 -- Analyze_Generic_Subprogram_Body --
1120 -------------------------------------
1122 procedure Analyze_Generic_Subprogram_Body
1123 (N : Node_Id;
1124 Gen_Id : Entity_Id)
1126 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1127 Kind : constant Entity_Kind := Ekind (Gen_Id);
1128 Body_Id : Entity_Id;
1129 New_N : Node_Id;
1130 Spec : Node_Id;
1132 begin
1133 -- Copy body and disable expansion while analyzing the generic For a
1134 -- stub, do not copy the stub (which would load the proper body), this
1135 -- will be done when the proper body is analyzed.
1137 if Nkind (N) /= N_Subprogram_Body_Stub then
1138 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1139 Rewrite (N, New_N);
1141 -- Once the contents of the generic copy and the template are
1142 -- swapped, do the same for their respective aspect specifications.
1144 Exchange_Aspects (N, New_N);
1146 -- Collect all contract-related source pragmas found within the
1147 -- template and attach them to the contract of the subprogram body.
1148 -- This contract is used in the capture of global references within
1149 -- annotations.
1151 Create_Generic_Contract (N);
1153 Start_Generic;
1154 end if;
1156 Spec := Specification (N);
1158 -- Within the body of the generic, the subprogram is callable, and
1159 -- behaves like the corresponding non-generic unit.
1161 Body_Id := Defining_Entity (Spec);
1163 if Kind = E_Generic_Procedure
1164 and then Nkind (Spec) /= N_Procedure_Specification
1165 then
1166 Error_Msg_N ("invalid body for generic procedure", Body_Id);
1167 return;
1169 elsif Kind = E_Generic_Function
1170 and then Nkind (Spec) /= N_Function_Specification
1171 then
1172 Error_Msg_N ("invalid body for generic function", Body_Id);
1173 return;
1174 end if;
1176 Set_Corresponding_Body (Gen_Decl, Body_Id);
1178 if Has_Completion (Gen_Id)
1179 and then Nkind (Parent (N)) /= N_Subunit
1180 then
1181 Error_Msg_N ("duplicate generic body", N);
1182 return;
1183 else
1184 Set_Has_Completion (Gen_Id);
1185 end if;
1187 if Nkind (N) = N_Subprogram_Body_Stub then
1188 Mutate_Ekind (Defining_Entity (Specification (N)), Kind);
1189 else
1190 Set_Corresponding_Spec (N, Gen_Id);
1191 end if;
1193 if Nkind (Parent (N)) = N_Compilation_Unit then
1194 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1195 end if;
1197 -- Make generic parameters immediately visible in the body. They are
1198 -- needed to process the formals declarations. Then make the formals
1199 -- visible in a separate step.
1201 Push_Scope (Gen_Id);
1203 declare
1204 E : Entity_Id;
1205 First_Ent : Entity_Id;
1207 begin
1208 First_Ent := First_Entity (Gen_Id);
1210 E := First_Ent;
1211 while Present (E) and then not Is_Formal (E) loop
1212 Install_Entity (E);
1213 Next_Entity (E);
1214 end loop;
1216 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1218 -- Now generic formals are visible, and the specification can be
1219 -- analyzed, for subsequent conformance check.
1221 Body_Id := Analyze_Subprogram_Specification (Spec);
1223 -- Make formal parameters visible
1225 if Present (E) then
1227 -- E is the first formal parameter, we loop through the formals
1228 -- installing them so that they will be visible.
1230 Set_First_Entity (Gen_Id, E);
1231 while Present (E) loop
1232 Install_Entity (E);
1233 Next_Formal (E);
1234 end loop;
1235 end if;
1237 -- Visible generic entity is callable within its own body
1239 Mutate_Ekind (Gen_Id, Ekind (Body_Id));
1240 Reinit_Field_To_Zero (Body_Id, F_Has_Out_Or_In_Out_Parameter,
1241 Old_Ekind =>
1242 (E_Function | E_Procedure |
1243 E_Generic_Function | E_Generic_Procedure => True,
1244 others => False));
1245 Reinit_Field_To_Zero (Body_Id, F_Needs_No_Actuals);
1246 if Ekind (Body_Id) in E_Function | E_Procedure then
1247 Reinit_Field_To_Zero (Body_Id, F_Is_Inlined_Always);
1248 end if;
1249 Mutate_Ekind (Body_Id, E_Subprogram_Body);
1250 Set_Convention (Body_Id, Convention (Gen_Id));
1251 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1252 Set_Scope (Body_Id, Scope (Gen_Id));
1254 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1256 if Nkind (N) = N_Subprogram_Body_Stub then
1258 -- No body to analyze, so restore state of generic unit
1260 Mutate_Ekind (Gen_Id, Kind);
1261 Mutate_Ekind (Body_Id, Kind);
1263 if Present (First_Ent) then
1264 Set_First_Entity (Gen_Id, First_Ent);
1265 end if;
1267 End_Scope;
1268 return;
1269 end if;
1271 -- If this is a compilation unit, it must be made visible explicitly,
1272 -- because the compilation of the declaration, unlike other library
1273 -- unit declarations, does not. If it is not a unit, the following
1274 -- is redundant but harmless.
1276 Set_Is_Immediately_Visible (Gen_Id);
1277 Reference_Body_Formals (Gen_Id, Body_Id);
1279 if Is_Child_Unit (Gen_Id) then
1280 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1281 end if;
1283 Set_Actual_Subtypes (N, Current_Scope);
1285 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1286 Set_SPARK_Pragma_Inherited (Body_Id);
1288 -- Analyze any aspect specifications that appear on the generic
1289 -- subprogram body.
1291 if Has_Aspects (N) then
1292 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
1293 end if;
1295 -- Process the contract of the subprogram body after analyzing all
1296 -- the contract-related pragmas within the declarations.
1298 Analyze_Pragmas_In_Declarations (Body_Id);
1299 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1301 -- Continue on with analyzing the declarations and statements once
1302 -- contract expansion is done and we are done expanding contract
1303 -- related wrappers.
1305 Analyze_Declarations (Declarations (N));
1306 Check_Completion;
1308 Analyze (Handled_Statement_Sequence (N));
1309 Save_Global_References (Original_Node (N));
1311 -- Prior to exiting the scope, include generic formals again (if any
1312 -- are present) in the set of local entities.
1314 if Present (First_Ent) then
1315 Set_First_Entity (Gen_Id, First_Ent);
1316 end if;
1318 Check_References (Gen_Id);
1319 end;
1321 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1322 Update_Use_Clause_Chain;
1323 Validate_Categorization_Dependency (N, Gen_Id);
1324 End_Scope;
1325 Check_Subprogram_Order (N);
1327 -- Outside of its body, unit is generic again
1329 Reinit_Field_To_Zero (Gen_Id, F_Has_Nested_Subprogram,
1330 Old_Ekind => (E_Function | E_Procedure => True, others => False));
1331 Mutate_Ekind (Gen_Id, Kind);
1332 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1334 if Style_Check then
1335 Style.Check_Identifier (Body_Id, Gen_Id);
1336 end if;
1338 End_Generic;
1339 end Analyze_Generic_Subprogram_Body;
1341 ----------------------------
1342 -- Analyze_Null_Procedure --
1343 ----------------------------
1345 -- WARNING: This routine manages Ghost regions. Return statements must be
1346 -- replaced by gotos that jump to the end of the routine and restore the
1347 -- Ghost mode.
1349 procedure Analyze_Null_Procedure
1350 (N : Node_Id;
1351 Is_Completion : out Boolean)
1353 Loc : constant Source_Ptr := Sloc (N);
1354 Spec : constant Node_Id := Specification (N);
1356 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1357 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1358 Saved_ISMP : constant Boolean :=
1359 Ignore_SPARK_Mode_Pragmas_In_Instance;
1360 -- Save the Ghost and SPARK mode-related data to restore on exit
1362 Designator : Entity_Id;
1363 Form : Node_Id;
1364 Null_Body : Node_Id := Empty;
1365 Null_Stmt : Node_Id := Null_Statement (Spec);
1366 Prev : Entity_Id;
1368 begin
1369 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1371 -- A null procedure is Ghost when it is stand-alone and is subject to
1372 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1373 -- now, to ensure that any nodes generated during analysis and expansion
1374 -- are properly marked as Ghost.
1376 if Present (Prev) then
1377 Mark_And_Set_Ghost_Body (N, Prev);
1378 end if;
1380 -- Capture the profile of the null procedure before analysis, for
1381 -- expansion at the freeze point and at each point of call. The body is
1382 -- used if the procedure has preconditions, or if it is a completion. In
1383 -- the first case the body is analyzed at the freeze point, in the other
1384 -- it replaces the null procedure declaration.
1386 -- For a null procedure that comes from source, a NULL statement is
1387 -- provided by the parser, which carries the source location of the
1388 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1389 -- from expansion, create one now.
1391 if No (Null_Stmt) then
1392 Null_Stmt := Make_Null_Statement (Loc);
1393 end if;
1395 Null_Body :=
1396 Make_Subprogram_Body (Loc,
1397 Specification => New_Copy_Tree (Spec),
1398 Declarations => New_List,
1399 Handled_Statement_Sequence =>
1400 Make_Handled_Sequence_Of_Statements (Loc,
1401 Statements => New_List (Null_Stmt)));
1403 -- Create new entities for body and formals
1405 Set_Defining_Unit_Name (Specification (Null_Body),
1406 Make_Defining_Identifier
1407 (Sloc (Defining_Entity (N)),
1408 Chars (Defining_Entity (N))));
1410 Form := First (Parameter_Specifications (Specification (Null_Body)));
1411 while Present (Form) loop
1412 Set_Defining_Identifier (Form,
1413 Make_Defining_Identifier
1414 (Sloc (Defining_Identifier (Form)),
1415 Chars (Defining_Identifier (Form))));
1416 Next (Form);
1417 end loop;
1419 -- Determine whether the null procedure may be a completion of a generic
1420 -- subprogram, in which case we use the new null body as the completion
1421 -- and set minimal semantic information on the original declaration,
1422 -- which is rewritten as a null statement.
1424 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1425 Insert_Before (N, Null_Body);
1426 Mutate_Ekind (Defining_Entity (N), Ekind (Prev));
1428 Rewrite (N, Make_Null_Statement (Loc));
1429 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1430 Is_Completion := True;
1432 -- Mark the newly generated subprogram body as trivial
1434 Set_Is_Trivial_Subprogram
1435 (Defining_Unit_Name (Specification (Null_Body)));
1437 goto Leave;
1439 else
1440 -- Resolve the types of the formals now, because the freeze point may
1441 -- appear in a different context, e.g. an instantiation.
1443 Form := First (Parameter_Specifications (Specification (Null_Body)));
1444 while Present (Form) loop
1445 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1446 Find_Type (Parameter_Type (Form));
1448 elsif No (Access_To_Subprogram_Definition
1449 (Parameter_Type (Form)))
1450 then
1451 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1453 -- The case of a null procedure with a formal that is an
1454 -- access-to-subprogram type, and that is used as an actual
1455 -- in an instantiation is left to the enthusiastic reader.
1457 else
1458 null;
1459 end if;
1461 Next (Form);
1462 end loop;
1463 end if;
1465 -- If there are previous overloadable entities with the same name, check
1466 -- whether any of them is completed by the null procedure.
1468 if Present (Prev) and then Is_Overloadable (Prev) then
1469 Designator := Analyze_Subprogram_Specification (Spec);
1470 Prev := Find_Corresponding_Spec (N);
1471 end if;
1473 if No (Prev) or else not Comes_From_Source (Prev) then
1474 Designator := Analyze_Subprogram_Specification (Spec);
1475 Set_Has_Completion (Designator);
1477 -- Signal to caller that this is a procedure declaration
1479 Is_Completion := False;
1481 -- Null procedures are always inlined, but generic formal subprograms
1482 -- which appear as such in the internal instance of formal packages,
1483 -- need no completion and are not marked Inline.
1485 if Expander_Active
1486 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1487 then
1488 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1489 Set_Body_To_Inline (N, Null_Body);
1490 Set_Is_Inlined (Designator);
1491 end if;
1493 else
1494 -- The null procedure is a completion. We unconditionally rewrite
1495 -- this as a null body (even if expansion is not active), because
1496 -- there are various error checks that are applied on this body
1497 -- when it is analyzed (e.g. correct aspect placement).
1499 if Has_Completion (Prev) then
1500 Error_Msg_Sloc := Sloc (Prev);
1501 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1502 end if;
1504 Check_Previous_Null_Procedure (N, Prev);
1506 Is_Completion := True;
1507 Rewrite (N, Null_Body);
1508 Analyze (N);
1509 end if;
1511 <<Leave>>
1512 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1513 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1514 end Analyze_Null_Procedure;
1516 -----------------------------
1517 -- Analyze_Operator_Symbol --
1518 -----------------------------
1520 -- An operator symbol such as "+" or "and" may appear in context where the
1521 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1522 -- is just a string, as in (conjunction = "or"). In these cases the parser
1523 -- generates this node, and the semantics does the disambiguation. Other
1524 -- such case are actuals in an instantiation, the generic unit in an
1525 -- instantiation, pragma arguments, and aspect specifications.
1527 procedure Analyze_Operator_Symbol (N : Node_Id) is
1528 Par : constant Node_Id := Parent (N);
1530 Maybe_Aspect_Spec : Node_Id := Par;
1531 begin
1532 if Nkind (Maybe_Aspect_Spec) /= N_Aspect_Specification then
1533 -- deal with N_Aggregate nodes
1534 Maybe_Aspect_Spec := Parent (Maybe_Aspect_Spec);
1535 end if;
1537 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1538 or else Nkind (Par) = N_Function_Instantiation
1539 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1540 or else (Nkind (Par) = N_Pragma_Argument_Association
1541 and then not Is_Pragma_String_Literal (Par))
1542 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1543 or else (Nkind (Par) = N_Attribute_Reference
1544 and then Attribute_Name (Par) /= Name_Value)
1545 or else (Nkind (Maybe_Aspect_Spec) = N_Aspect_Specification
1546 and then Get_Aspect_Id (Maybe_Aspect_Spec)
1548 -- Include aspects that can be specified by a
1549 -- subprogram name, which can be an operator.
1551 in Aspect_Stable_Properties
1552 | Aspect_Integer_Literal
1553 | Aspect_Real_Literal
1554 | Aspect_String_Literal
1555 | Aspect_Aggregate)
1556 then
1557 Find_Direct_Name (N);
1559 else
1560 Change_Operator_Symbol_To_String_Literal (N);
1561 Analyze (N);
1562 end if;
1563 end Analyze_Operator_Symbol;
1565 -----------------------------------
1566 -- Analyze_Parameter_Association --
1567 -----------------------------------
1569 procedure Analyze_Parameter_Association (N : Node_Id) is
1570 begin
1571 Analyze (Explicit_Actual_Parameter (N));
1572 end Analyze_Parameter_Association;
1574 ----------------------------
1575 -- Analyze_Procedure_Call --
1576 ----------------------------
1578 -- WARNING: This routine manages Ghost regions. Return statements must be
1579 -- replaced by gotos that jump to the end of the routine and restore the
1580 -- Ghost mode.
1582 procedure Analyze_Procedure_Call (N : Node_Id) is
1583 procedure Analyze_Call_And_Resolve;
1584 -- Do Analyze and Resolve calls for procedure call. At the end, check
1585 -- for illegal order dependence.
1586 -- ??? where is the check for illegal order dependencies?
1588 ------------------------------
1589 -- Analyze_Call_And_Resolve --
1590 ------------------------------
1592 procedure Analyze_Call_And_Resolve is
1593 begin
1594 if Nkind (N) = N_Procedure_Call_Statement then
1595 Analyze_Call (N);
1596 Resolve (N, Standard_Void_Type);
1597 else
1598 Analyze (N);
1599 end if;
1600 end Analyze_Call_And_Resolve;
1602 -- Local variables
1604 Actuals : constant List_Id := Parameter_Associations (N);
1605 Loc : constant Source_Ptr := Sloc (N);
1606 P : constant Node_Id := Name (N);
1608 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1609 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1610 -- Save the Ghost-related attributes to restore on exit
1612 Actual : Node_Id;
1613 New_N : Node_Id;
1615 -- Start of processing for Analyze_Procedure_Call
1617 begin
1618 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1619 -- a procedure call or an entry call. The prefix may denote an access
1620 -- to subprogram type, in which case an implicit dereference applies.
1621 -- If the prefix is an indexed component (without implicit dereference)
1622 -- then the construct denotes a call to a member of an entire family.
1623 -- If the prefix is a simple name, it may still denote a call to a
1624 -- parameterless member of an entry family. Resolution of these various
1625 -- interpretations is delicate.
1627 -- Do not analyze machine code statements to avoid rejecting them in
1628 -- CodePeer mode.
1630 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1631 Set_Etype (P, Standard_Void_Type);
1632 else
1633 Analyze (P);
1634 end if;
1636 -- If this is a call of the form Obj.Op, the call may have been analyzed
1637 -- and possibly rewritten into a block, in which case we are done.
1639 if Analyzed (N) then
1640 return;
1642 -- If there is an error analyzing the name (which may have been
1643 -- rewritten if the original call was in prefix notation) then error
1644 -- has been emitted already, mark node and return.
1646 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1647 Set_Etype (N, Any_Type);
1648 return;
1649 end if;
1651 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1652 -- Set the mode now to ensure that any nodes generated during analysis
1653 -- and expansion are properly marked as Ghost.
1655 Mark_And_Set_Ghost_Procedure_Call (N);
1657 -- Otherwise analyze the parameters
1659 Actual := First (Actuals);
1661 while Present (Actual) loop
1662 Analyze (Actual);
1663 Check_Parameterless_Call (Actual);
1664 Next (Actual);
1665 end loop;
1667 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1669 if Nkind (P) = N_Attribute_Reference
1670 and then Attribute_Name (P) in Name_Elab_Spec
1671 | Name_Elab_Body
1672 | Name_Elab_Subp_Body
1673 then
1674 if Present (Actuals) then
1675 Error_Msg_N
1676 ("no parameters allowed for this call", First (Actuals));
1677 goto Leave;
1678 end if;
1680 Set_Etype (N, Standard_Void_Type);
1681 Set_Analyzed (N);
1683 elsif Is_Entity_Name (P)
1684 and then Is_Record_Type (Etype (Entity (P)))
1685 and then Remote_AST_I_Dereference (P)
1686 then
1687 goto Leave;
1689 elsif Is_Entity_Name (P)
1690 and then Ekind (Entity (P)) /= E_Entry_Family
1691 then
1692 if Is_Access_Type (Etype (P))
1693 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1694 and then No (Actuals)
1695 and then Comes_From_Source (N)
1696 then
1697 Error_Msg_N ("missing explicit dereference in call", N);
1699 elsif Ekind (Entity (P)) = E_Operator then
1700 Error_Msg_Name_1 := Chars (P);
1701 Error_Msg_N ("operator % cannot be used as a procedure", N);
1702 end if;
1704 Analyze_Call_And_Resolve;
1706 -- If the prefix is the simple name of an entry family, this is a
1707 -- parameterless call from within the task body itself.
1709 elsif Is_Entity_Name (P)
1710 and then Nkind (P) = N_Identifier
1711 and then Ekind (Entity (P)) = E_Entry_Family
1712 and then Present (Actuals)
1713 and then No (Next (First (Actuals)))
1714 then
1715 -- Can be call to parameterless entry family. What appears to be the
1716 -- sole argument is in fact the entry index. Rewrite prefix of node
1717 -- accordingly. Source representation is unchanged by this
1718 -- transformation.
1720 New_N :=
1721 Make_Indexed_Component (Loc,
1722 Prefix =>
1723 Make_Selected_Component (Loc,
1724 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1725 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1726 Expressions => Actuals);
1727 Set_Name (N, New_N);
1728 Set_Etype (New_N, Standard_Void_Type);
1729 Set_Parameter_Associations (N, No_List);
1730 Analyze_Call_And_Resolve;
1732 elsif Nkind (P) = N_Explicit_Dereference then
1733 if Ekind (Etype (P)) = E_Subprogram_Type then
1734 Analyze_Call_And_Resolve;
1735 else
1736 Error_Msg_N ("expect access to procedure in call", P);
1737 end if;
1739 -- The name can be a selected component or an indexed component that
1740 -- yields an access to subprogram. Such a prefix is legal if the call
1741 -- has parameter associations.
1743 elsif Is_Access_Type (Etype (P))
1744 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1745 then
1746 if Present (Actuals) then
1747 Analyze_Call_And_Resolve;
1748 else
1749 Error_Msg_N ("missing explicit dereference in call", N);
1750 end if;
1752 -- If not an access to subprogram, then the prefix must resolve to the
1753 -- name of an entry, entry family, or protected operation.
1755 -- For the case of a simple entry call, P is a selected component where
1756 -- the prefix is the task and the selector name is the entry. A call to
1757 -- a protected procedure will have the same syntax. If the protected
1758 -- object contains overloaded operations, the entity may appear as a
1759 -- function, the context will select the operation whose type is Void.
1761 elsif Nkind (P) = N_Selected_Component
1762 and then Ekind (Entity (Selector_Name (P)))
1763 in E_Entry | E_Function | E_Procedure
1764 then
1765 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1766 -- in prefix notation may still be missing its controlling argument,
1767 -- so perform the transformation now.
1769 if SPARK_Mode = On and then In_Inlined_Body then
1770 declare
1771 Subp : constant Entity_Id := Entity (Selector_Name (P));
1772 Typ : constant Entity_Id := Etype (Prefix (P));
1774 begin
1775 if Is_Tagged_Type (Typ)
1776 and then Present (First_Formal (Subp))
1777 and then (Etype (First_Formal (Subp)) = Typ
1778 or else
1779 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
1780 and then Try_Object_Operation (P)
1781 then
1782 goto Leave;
1784 else
1785 Analyze_Call_And_Resolve;
1786 end if;
1787 end;
1789 else
1790 Analyze_Call_And_Resolve;
1791 end if;
1793 elsif Nkind (P) = N_Selected_Component
1794 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1795 and then Present (Actuals)
1796 and then No (Next (First (Actuals)))
1797 then
1798 -- Can be call to parameterless entry family. What appears to be the
1799 -- sole argument is in fact the entry index. Rewrite prefix of node
1800 -- accordingly. Source representation is unchanged by this
1801 -- transformation.
1803 New_N :=
1804 Make_Indexed_Component (Loc,
1805 Prefix => New_Copy (P),
1806 Expressions => Actuals);
1807 Set_Name (N, New_N);
1808 Set_Etype (New_N, Standard_Void_Type);
1809 Set_Parameter_Associations (N, No_List);
1810 Analyze_Call_And_Resolve;
1812 -- For the case of a reference to an element of an entry family, P is
1813 -- an indexed component whose prefix is a selected component (task and
1814 -- entry family), and whose index is the entry family index.
1816 elsif Nkind (P) = N_Indexed_Component
1817 and then Nkind (Prefix (P)) = N_Selected_Component
1818 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1819 then
1820 Analyze_Call_And_Resolve;
1822 -- If the prefix is the name of an entry family, it is a call from
1823 -- within the task body itself.
1825 elsif Nkind (P) = N_Indexed_Component
1826 and then Nkind (Prefix (P)) = N_Identifier
1827 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1828 then
1829 New_N :=
1830 Make_Selected_Component (Loc,
1831 Prefix =>
1832 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1833 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1834 Rewrite (Prefix (P), New_N);
1835 Analyze (P);
1836 Analyze_Call_And_Resolve;
1838 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1839 -- procedure name, so the construct can only be a qualified expression.
1841 elsif Nkind (P) = N_Qualified_Expression
1842 and then Ada_Version >= Ada_2012
1843 then
1844 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1845 Analyze (N);
1847 -- Anything else is an error
1849 else
1850 Error_Msg_N ("invalid procedure or entry call", N);
1852 -- Specialize the error message in the case where both a primitive
1853 -- operation and a record component are visible at the same time.
1855 if Nkind (P) = N_Selected_Component
1856 and then Is_Entity_Name (Selector_Name (P))
1857 then
1858 declare
1859 Sel : constant Entity_Id := Entity (Selector_Name (P));
1860 begin
1861 if Ekind (Sel) = E_Component
1862 and then Present (Homonym (Sel))
1863 and then Ekind (Homonym (Sel)) = E_Procedure
1864 then
1865 Error_Msg_NE ("\component & conflicts with"
1866 & " homonym procedure (RM 4.1.3 (9.2/3))",
1867 Selector_Name (P), Sel);
1868 end if;
1869 end;
1870 end if;
1871 end if;
1873 <<Leave>>
1874 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1875 end Analyze_Procedure_Call;
1877 ------------------------------
1878 -- Analyze_Return_Statement --
1879 ------------------------------
1881 procedure Analyze_Return_Statement (N : Node_Id) is
1882 pragma Assert
1883 (Nkind (N) in N_Extended_Return_Statement | N_Simple_Return_Statement);
1885 Returns_Object : constant Boolean :=
1886 Nkind (N) = N_Extended_Return_Statement
1887 or else
1888 (Nkind (N) = N_Simple_Return_Statement
1889 and then Present (Expression (N)));
1890 -- True if we're returning something; that is, "return <expression>;"
1891 -- or "return Result : T [:= ...]". False for "return;". Used for error
1892 -- checking: If Returns_Object is True, N should apply to a function
1893 -- body; otherwise N should apply to a procedure body, entry body,
1894 -- accept statement, or extended return statement.
1896 function Find_What_It_Applies_To return Entity_Id;
1897 -- Find the entity representing the innermost enclosing body, accept
1898 -- statement, or extended return statement. If the result is a callable
1899 -- construct or extended return statement, then this will be the value
1900 -- of the Return_Applies_To attribute. Otherwise, the program is
1901 -- illegal. See RM-6.5(4/2).
1903 -----------------------------
1904 -- Find_What_It_Applies_To --
1905 -----------------------------
1907 function Find_What_It_Applies_To return Entity_Id is
1908 Result : Entity_Id := Empty;
1910 begin
1911 -- Loop outward through the Scope_Stack, skipping blocks, and loops
1913 for J in reverse 0 .. Scope_Stack.Last loop
1914 Result := Scope_Stack.Table (J).Entity;
1915 exit when Ekind (Result) not in E_Block | E_Loop;
1916 end loop;
1918 pragma Assert (Present (Result));
1919 return Result;
1920 end Find_What_It_Applies_To;
1922 -- Local declarations
1924 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1925 Kind : constant Entity_Kind := Ekind (Scope_Id);
1926 Loc : constant Source_Ptr := Sloc (N);
1927 Stm_Entity : constant Entity_Id :=
1928 New_Internal_Entity
1929 (E_Return_Statement, Current_Scope, Loc, 'R');
1931 -- Start of processing for Analyze_Return_Statement
1933 begin
1934 Set_Return_Statement_Entity (N, Stm_Entity);
1936 Set_Etype (Stm_Entity, Standard_Void_Type);
1937 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1939 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1940 -- (4/2): an inner return statement will apply to this extended return.
1942 if Nkind (N) = N_Extended_Return_Statement then
1943 Push_Scope (Stm_Entity);
1944 end if;
1946 -- Check that pragma No_Return is obeyed. Don't complain about the
1947 -- implicitly-generated return that is placed at the end.
1949 if No_Return (Scope_Id)
1950 and then Kind in E_Procedure | E_Generic_Procedure
1951 and then Comes_From_Source (N)
1952 then
1953 Error_Msg_N
1954 ("RETURN statement not allowed in No_Return procedure", N);
1955 end if;
1957 -- Warn on any unassigned OUT parameters if in procedure
1959 if Ekind (Scope_Id) = E_Procedure then
1960 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1961 end if;
1963 -- Check that functions return objects, and other things do not
1965 if Kind in E_Function | E_Generic_Function then
1966 if not Returns_Object then
1967 Error_Msg_N ("missing expression in return from function", N);
1968 end if;
1970 elsif Kind in E_Procedure | E_Generic_Procedure then
1971 if Returns_Object then
1972 Error_Msg_N ("procedure cannot return value (use function)", N);
1973 end if;
1975 elsif Kind in E_Entry | E_Entry_Family then
1976 if Returns_Object then
1977 if Is_Protected_Type (Scope (Scope_Id)) then
1978 Error_Msg_N ("entry body cannot return value", N);
1979 else
1980 Error_Msg_N ("accept statement cannot return value", N);
1981 end if;
1982 end if;
1984 elsif Kind = E_Return_Statement then
1986 -- We are nested within another return statement, which must be an
1987 -- extended_return_statement.
1989 if Returns_Object then
1990 if Nkind (N) = N_Extended_Return_Statement then
1991 Error_Msg_N
1992 ("extended return statement cannot be nested (use `RETURN;`)",
1995 -- Case of a simple return statement with a value inside extended
1996 -- return statement.
1998 else
1999 Error_Msg_N
2000 ("return nested in extended return statement cannot return "
2001 & "value (use `RETURN;`)", N);
2002 end if;
2003 end if;
2005 else
2006 Error_Msg_N ("illegal context for return statement", N);
2007 end if;
2009 if Kind in E_Function | E_Generic_Function then
2010 Analyze_Function_Return (N);
2012 elsif Kind in E_Procedure | E_Generic_Procedure then
2013 Set_Return_Present (Scope_Id);
2014 end if;
2016 if Nkind (N) = N_Extended_Return_Statement then
2017 End_Scope;
2018 end if;
2020 Kill_Current_Values (Last_Assignment_Only => True);
2021 Check_Unreachable_Code (N);
2023 Analyze_Dimension (N);
2024 end Analyze_Return_Statement;
2026 -----------------------------------
2027 -- Analyze_Return_When_Statement --
2028 -----------------------------------
2030 procedure Analyze_Return_When_Statement (N : Node_Id) is
2031 begin
2032 -- Verify the condition is a Boolean expression
2034 Analyze_And_Resolve (Condition (N), Any_Boolean);
2035 Check_Unset_Reference (Condition (N));
2036 end Analyze_Return_When_Statement;
2038 -------------------------------------
2039 -- Analyze_Simple_Return_Statement --
2040 -------------------------------------
2042 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2043 begin
2044 if Present (Expression (N)) then
2045 Mark_Coextensions (N, Expression (N));
2046 end if;
2048 Analyze_Return_Statement (N);
2049 end Analyze_Simple_Return_Statement;
2051 -------------------------
2052 -- Analyze_Return_Type --
2053 -------------------------
2055 procedure Analyze_Return_Type (N : Node_Id) is
2056 Designator : constant Entity_Id := Defining_Entity (N);
2057 Typ : Entity_Id;
2059 begin
2060 -- Normal case where result definition does not indicate an error
2062 if Result_Definition (N) /= Error then
2063 if Nkind (Result_Definition (N)) = N_Access_Definition then
2065 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2067 declare
2068 AD : constant Node_Id :=
2069 Access_To_Subprogram_Definition (Result_Definition (N));
2070 begin
2071 if Present (AD) and then Protected_Present (AD) then
2072 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2073 else
2074 Typ := Access_Definition (N, Result_Definition (N));
2075 end if;
2076 end;
2078 Set_Parent (Typ, Result_Definition (N));
2079 Set_Is_Local_Anonymous_Access (Typ);
2080 Set_Etype (Designator, Typ);
2082 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2084 Null_Exclusion_Static_Checks (N);
2086 -- Subtype_Mark case
2088 else
2089 Find_Type (Result_Definition (N));
2090 Typ := Entity (Result_Definition (N));
2091 Set_Etype (Designator, Typ);
2093 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2095 Null_Exclusion_Static_Checks (N);
2097 -- If a null exclusion is imposed on the result type, then create
2098 -- a null-excluding itype (an access subtype) and use it as the
2099 -- function's Etype. Note that the null exclusion checks are done
2100 -- right before this, because they don't get applied to types that
2101 -- do not come from source.
2103 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2104 Set_Etype (Designator,
2105 Create_Null_Excluding_Itype
2106 (T => Typ,
2107 Related_Nod => N,
2108 Scope_Id => Scope (Current_Scope)));
2110 -- The new subtype must be elaborated before use because
2111 -- it is visible outside of the function. However its base
2112 -- type may not be frozen yet, so the reference that will
2113 -- force elaboration must be attached to the freezing of
2114 -- the base type.
2116 -- If the return specification appears on a proper body,
2117 -- the subtype will have been created already on the spec.
2119 if Is_Frozen (Typ) then
2120 if Nkind (Parent (N)) = N_Subprogram_Body
2121 and then Nkind (Parent (Parent (N))) = N_Subunit
2122 then
2123 null;
2124 else
2125 Build_Itype_Reference (Etype (Designator), Parent (N));
2126 end if;
2128 else
2129 declare
2130 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2131 begin
2132 Set_Itype (IR, Etype (Designator));
2133 Append_Freeze_Action (Typ, IR);
2134 end;
2135 end if;
2137 else
2138 Set_Etype (Designator, Typ);
2139 end if;
2141 if Ekind (Typ) = E_Incomplete_Type
2142 or else (Is_Class_Wide_Type (Typ)
2143 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2144 then
2145 -- AI05-0151: Tagged incomplete types are allowed in all formal
2146 -- parts. Untagged incomplete types are not allowed in bodies.
2147 -- As a consequence, limited views cannot appear in a basic
2148 -- declaration that is itself within a body, because there is
2149 -- no point at which the non-limited view will become visible.
2151 if Ada_Version >= Ada_2012 then
2152 if From_Limited_With (Typ) and then In_Package_Body then
2153 Error_Msg_NE
2154 ("invalid use of incomplete type&",
2155 Result_Definition (N), Typ);
2157 -- The return type of a subprogram body cannot be of a
2158 -- formal incomplete type.
2160 elsif Is_Generic_Type (Typ)
2161 and then Nkind (Parent (N)) = N_Subprogram_Body
2162 then
2163 Error_Msg_N
2164 ("return type cannot be a formal incomplete type",
2165 Result_Definition (N));
2167 elsif Is_Class_Wide_Type (Typ)
2168 and then Is_Generic_Type (Root_Type (Typ))
2169 and then Nkind (Parent (N)) = N_Subprogram_Body
2170 then
2171 Error_Msg_N
2172 ("return type cannot be a formal incomplete type",
2173 Result_Definition (N));
2175 elsif Is_Tagged_Type (Typ) then
2176 null;
2178 -- Use is legal in a thunk generated for an operation
2179 -- inherited from a progenitor.
2181 elsif Is_Thunk (Designator)
2182 and then Present (Non_Limited_View (Typ))
2183 then
2184 null;
2186 elsif Nkind (Parent (N)) = N_Subprogram_Body
2187 or else Nkind (Parent (Parent (N))) in
2188 N_Accept_Statement | N_Entry_Body
2189 then
2190 Error_Msg_NE
2191 ("invalid use of untagged incomplete type&",
2192 Designator, Typ);
2193 end if;
2195 -- The type must be completed in the current package. This
2196 -- is checked at the end of the package declaration when
2197 -- Taft-amendment types are identified. If the return type
2198 -- is class-wide, there is no required check, the type can
2199 -- be a bona fide TAT.
2201 if Ekind (Scope (Current_Scope)) = E_Package
2202 and then In_Private_Part (Scope (Current_Scope))
2203 and then not Is_Class_Wide_Type (Typ)
2204 then
2205 Append_Elmt (Designator, Private_Dependents (Typ));
2206 end if;
2208 else
2209 Error_Msg_NE
2210 ("invalid use of incomplete type&", Designator, Typ);
2211 end if;
2212 end if;
2213 end if;
2215 -- Case where result definition does indicate an error
2217 else
2218 Set_Etype (Designator, Any_Type);
2219 end if;
2220 end Analyze_Return_Type;
2222 -----------------------------
2223 -- Analyze_Subprogram_Body --
2224 -----------------------------
2226 procedure Analyze_Subprogram_Body (N : Node_Id) is
2227 Loc : constant Source_Ptr := Sloc (N);
2228 Body_Spec : constant Node_Id := Specification (N);
2229 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2231 begin
2232 if Debug_Flag_C then
2233 Write_Str ("==> subprogram body ");
2234 Write_Name (Chars (Body_Id));
2235 Write_Str (" from ");
2236 Write_Location (Loc);
2237 Write_Eol;
2238 Indent;
2239 end if;
2241 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2243 -- The real work is split out into the helper, so it can do "return;"
2244 -- without skipping the debug output:
2246 Analyze_Subprogram_Body_Helper (N);
2248 if Debug_Flag_C then
2249 Outdent;
2250 Write_Str ("<== subprogram body ");
2251 Write_Name (Chars (Body_Id));
2252 Write_Str (" from ");
2253 Write_Location (Loc);
2254 Write_Eol;
2255 end if;
2256 end Analyze_Subprogram_Body;
2258 ------------------------------------
2259 -- Analyze_Subprogram_Body_Helper --
2260 ------------------------------------
2262 -- This procedure is called for regular subprogram bodies, generic bodies,
2263 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2264 -- specification matters, and is used to create a proper declaration for
2265 -- the subprogram, or to perform conformance checks.
2267 -- WARNING: This routine manages Ghost regions. Return statements must be
2268 -- replaced by gotos that jump to the end of the routine and restore the
2269 -- Ghost mode.
2271 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2272 Body_Spec : Node_Id := Specification (N);
2273 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2274 Loc : constant Source_Ptr := Sloc (N);
2275 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2277 Body_Nod : Node_Id := Empty;
2278 Minimum_Acc_Objs : List_Id := No_List;
2280 Conformant : Boolean;
2281 Desig_View : Entity_Id := Empty;
2282 Exch_Views : Elist_Id := No_Elist;
2283 Mask_Types : Elist_Id := No_Elist;
2284 Prot_Typ : Entity_Id := Empty;
2285 Spec_Decl : Node_Id := Empty;
2286 Spec_Id : Entity_Id := Empty;
2288 Last_Real_Spec_Entity : Entity_Id := Empty;
2289 -- When we analyze a separate spec, the entity chain ends up containing
2290 -- the formals, as well as any itypes generated during analysis of the
2291 -- default expressions for parameters, or the arguments of associated
2292 -- precondition/postcondition pragmas (which are analyzed in the context
2293 -- of the spec since they have visibility on formals).
2295 -- These entities belong with the spec and not the body. However we do
2296 -- the analysis of the body in the context of the spec (again to obtain
2297 -- visibility to the formals), and all the entities generated during
2298 -- this analysis end up also chained to the entity chain of the spec.
2299 -- But they really belong to the body, and there is circuitry to move
2300 -- them from the spec to the body.
2302 -- However, when we do this move, we don't want to move the real spec
2303 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2304 -- variable points to the last real spec entity, so we only move those
2305 -- chained beyond that point. It is initialized to Empty to deal with
2306 -- the case where there is no separate spec.
2308 function Body_Has_Contract return Boolean;
2309 -- Check whether unanalyzed body has an aspect or pragma that may
2310 -- generate a SPARK contract.
2312 function Body_Has_SPARK_Mode_On return Boolean;
2313 -- Check whether SPARK_Mode On applies to the subprogram body, either
2314 -- because it is specified directly on the body, or because it is
2315 -- inherited from the enclosing subprogram or package.
2317 function Build_Internal_Protected_Declaration
2318 (N : Node_Id) return Entity_Id;
2319 -- A subprogram body without a previous spec that appears in a protected
2320 -- body must be expanded separately to create a subprogram declaration
2321 -- for it, in order to resolve internal calls to it from other protected
2322 -- operations.
2324 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2326 procedure Build_Subprogram_Declaration;
2327 -- Create a matching subprogram declaration for subprogram body N
2329 procedure Check_Anonymous_Return;
2330 -- Ada 2005: if a function returns an access type that denotes a task,
2331 -- or a type that contains tasks, we must create a master entity for
2332 -- the anonymous type, which typically will be used in an allocator
2333 -- in the body of the function.
2335 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2336 -- Look ahead to recognize a pragma that may appear after the body.
2337 -- If there is a previous spec, check that it appears in the same
2338 -- declarative part. If the pragma is Inline_Always, perform inlining
2339 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2340 -- If the body acts as a spec, and inlining is required, we create a
2341 -- subprogram declaration for it, in order to attach the body to inline.
2342 -- If pragma does not appear after the body, check whether there is
2343 -- an inline pragma before any local declarations.
2345 procedure Check_Missing_Return;
2346 -- Checks for a function with a no return statements, and also performs
2347 -- the warning checks implemented by Check_Returns.
2349 function Disambiguate_Spec return Entity_Id;
2350 -- When a primitive is declared between the private view and the full
2351 -- view of a concurrent type which implements an interface, a special
2352 -- mechanism is used to find the corresponding spec of the primitive
2353 -- body.
2355 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2356 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2357 -- incomplete types coming from a limited context and replace their
2358 -- limited views with the non-limited ones. Return the list of changes
2359 -- to be used to undo the transformation.
2361 procedure Generate_Minimum_Accessibility
2362 (Extra_Access : Entity_Id;
2363 Related_Form : Entity_Id := Empty);
2364 -- Generate a minimum accessibility object for a given extra
2365 -- accessibility formal (Extra_Access) and its related formal if it
2366 -- exists.
2368 function Is_Private_Concurrent_Primitive
2369 (Subp_Id : Entity_Id) return Boolean;
2370 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2371 -- type that implements an interface and has a private view.
2373 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2374 -- N is the body generated for an expression function that is not a
2375 -- completion and Spec_Id the defining entity of its spec. Mark all
2376 -- the not-yet-frozen types referenced by the simple return statement
2377 -- of the function as formally frozen.
2379 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2380 -- Find all suitable source pragmas at the top of subprogram body
2381 -- From's declarations and move them after arbitrary node To.
2382 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2383 -- as it applies to the body too.
2385 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2386 -- Undo the transformation done by Exchange_Limited_Views.
2388 procedure Set_Trivial_Subprogram (N : Node_Id);
2389 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2390 -- subprogram whose body is being analyzed. N is the statement node
2391 -- causing the flag to be set, if the following statement is a return
2392 -- of an entity, we mark the entity as set in source to suppress any
2393 -- warning on the stylized use of function stubs with a dummy return.
2395 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2396 -- Undo the transformation done by Mask_Unfrozen_Types
2398 procedure Verify_Overriding_Indicator;
2399 -- If there was a previous spec, the entity has been entered in the
2400 -- current scope previously. If the body itself carries an overriding
2401 -- indicator, check that it is consistent with the known status of the
2402 -- entity.
2404 -----------------------
2405 -- Body_Has_Contract --
2406 -----------------------
2408 function Body_Has_Contract return Boolean is
2409 Decls : constant List_Id := Declarations (N);
2410 Item : Node_Id;
2412 begin
2413 -- Check for aspects that may generate a contract
2415 Item := First (Aspect_Specifications (N));
2416 while Present (Item) loop
2417 if Is_Subprogram_Contract_Annotation (Item) then
2418 return True;
2419 end if;
2421 Next (Item);
2422 end loop;
2424 -- Check for pragmas that may generate a contract
2426 Item := First (Decls);
2427 while Present (Item) loop
2428 if Nkind (Item) = N_Pragma
2429 and then Is_Subprogram_Contract_Annotation (Item)
2430 then
2431 return True;
2432 end if;
2434 Next (Item);
2435 end loop;
2437 return False;
2438 end Body_Has_Contract;
2440 ----------------------------
2441 -- Body_Has_SPARK_Mode_On --
2442 ----------------------------
2444 function Body_Has_SPARK_Mode_On return Boolean is
2445 Decls : constant List_Id := Declarations (N);
2446 Item : Node_Id;
2448 begin
2449 -- Check for SPARK_Mode aspect
2451 Item := First (Aspect_Specifications (N));
2452 while Present (Item) loop
2453 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2454 return Get_SPARK_Mode_From_Annotation (Item) = On;
2455 end if;
2457 Next (Item);
2458 end loop;
2460 -- Check for SPARK_Mode pragma
2462 Item := First (Decls);
2463 while Present (Item) loop
2465 -- Pragmas that apply to a subprogram body are usually grouped
2466 -- together. Look for a potential pragma SPARK_Mode among them.
2468 if Nkind (Item) = N_Pragma then
2469 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2470 return Get_SPARK_Mode_From_Annotation (Item) = On;
2471 end if;
2473 -- Otherwise the first non-pragma declarative item terminates the
2474 -- region where pragma SPARK_Mode may appear.
2476 else
2477 exit;
2478 end if;
2480 Next (Item);
2481 end loop;
2483 -- Otherwise, the applicable SPARK_Mode is inherited from the
2484 -- enclosing subprogram or package.
2486 return SPARK_Mode = On;
2487 end Body_Has_SPARK_Mode_On;
2489 ------------------------------------------
2490 -- Build_Internal_Protected_Declaration --
2491 ------------------------------------------
2493 function Build_Internal_Protected_Declaration
2494 (N : Node_Id) return Entity_Id
2496 procedure Analyze_Pragmas (From : Node_Id);
2497 -- Analyze all pragmas which follow arbitrary node From
2499 ---------------------
2500 -- Analyze_Pragmas --
2501 ---------------------
2503 procedure Analyze_Pragmas (From : Node_Id) is
2504 Decl : Node_Id;
2506 begin
2507 Decl := Next (From);
2508 while Present (Decl) loop
2509 if Nkind (Decl) = N_Pragma then
2510 Analyze_Pragma (Decl);
2512 -- No candidate pragmas are available for analysis
2514 else
2515 exit;
2516 end if;
2518 Next (Decl);
2519 end loop;
2520 end Analyze_Pragmas;
2522 -- Local variables
2524 Body_Id : constant Entity_Id := Defining_Entity (N);
2525 Loc : constant Source_Ptr := Sloc (N);
2526 Decl : Node_Id;
2527 Formal : Entity_Id;
2528 Formals : List_Id;
2529 Spec : Node_Id;
2530 Spec_Id : Entity_Id;
2532 -- Start of processing for Build_Internal_Protected_Declaration
2534 begin
2535 Formal := First_Formal (Body_Id);
2537 -- The protected operation always has at least one formal, namely the
2538 -- object itself, but it is only placed in the parameter list if
2539 -- expansion is enabled.
2541 if Present (Formal) or else Expander_Active then
2542 Formals := Copy_Parameter_List (Body_Id);
2543 else
2544 Formals := No_List;
2545 end if;
2547 Spec_Id :=
2548 Make_Defining_Identifier (Sloc (Body_Id),
2549 Chars => Chars (Body_Id));
2551 -- Indicate that the entity comes from source, to ensure that cross-
2552 -- reference information is properly generated. The body itself is
2553 -- rewritten during expansion, and the body entity will not appear in
2554 -- calls to the operation.
2556 Set_Comes_From_Source (Spec_Id, True);
2558 if Nkind (Specification (N)) = N_Procedure_Specification then
2559 Spec :=
2560 Make_Procedure_Specification (Loc,
2561 Defining_Unit_Name => Spec_Id,
2562 Parameter_Specifications => Formals);
2563 else
2564 Spec :=
2565 Make_Function_Specification (Loc,
2566 Defining_Unit_Name => Spec_Id,
2567 Parameter_Specifications => Formals,
2568 Result_Definition =>
2569 New_Occurrence_Of (Etype (Body_Id), Loc));
2570 end if;
2572 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2573 Set_Corresponding_Body (Decl, Body_Id);
2574 Set_Corresponding_Spec (N, Spec_Id);
2576 Insert_Before (N, Decl);
2578 -- Associate all aspects and pragmas of the body with the spec. This
2579 -- ensures that these annotations apply to the initial declaration of
2580 -- the subprogram body.
2582 Move_Aspects (From => N, To => Decl);
2583 Move_Pragmas (From => N, To => Decl);
2585 Analyze (Decl);
2587 -- The analysis of the spec may generate pragmas which require manual
2588 -- analysis. Since the generation of the spec and the relocation of
2589 -- the annotations is driven by the expansion of the stand-alone
2590 -- body, the pragmas will not be analyzed in a timely manner. Do this
2591 -- now.
2593 Analyze_Pragmas (Decl);
2595 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
2596 -- ensuring in particular that 'Access is illegal.
2598 Set_Convention (Spec_Id, Convention_Intrinsic);
2599 Set_Has_Completion (Spec_Id);
2601 return Spec_Id;
2602 end Build_Internal_Protected_Declaration;
2604 ----------------------------------
2605 -- Build_Subprogram_Declaration --
2606 ----------------------------------
2608 procedure Build_Subprogram_Declaration is
2609 Decl : Node_Id;
2610 Subp_Decl : Node_Id;
2612 begin
2613 -- Create a matching subprogram spec using the profile of the body.
2614 -- The structure of the tree is identical, but has new entities for
2615 -- the defining unit name and formal parameters.
2617 Subp_Decl :=
2618 Make_Subprogram_Declaration (Loc,
2619 Specification => Copy_Subprogram_Spec (Body_Spec));
2620 Set_Comes_From_Source (Subp_Decl, True);
2622 -- Also mark parameters as coming from source
2624 if Present (Parameter_Specifications (Specification (Subp_Decl))) then
2625 declare
2626 Form : Entity_Id;
2627 begin
2628 Form :=
2629 First (Parameter_Specifications (Specification (Subp_Decl)));
2631 while Present (Form) loop
2632 Set_Comes_From_Source (Defining_Identifier (Form), True);
2633 Next (Form);
2634 end loop;
2635 end;
2636 end if;
2638 -- Relocate the aspects and relevant pragmas from the subprogram body
2639 -- to the generated spec because it acts as the initial declaration.
2641 Insert_Before (N, Subp_Decl);
2642 Move_Aspects (N, To => Subp_Decl);
2643 Move_Pragmas (N, To => Subp_Decl);
2645 -- Ensure that the generated corresponding spec and original body
2646 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2647 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2648 -- correctly set for local subprograms.
2650 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2652 Analyze (Subp_Decl);
2654 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2655 -- the body since the expander may generate calls using that entity.
2656 -- Required to ensure that Expand_Call rewrites calls to this
2657 -- function by calls to the built procedure.
2659 if Transform_Function_Array
2660 and then Nkind (Body_Spec) = N_Function_Specification
2661 and then
2662 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2663 then
2664 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2665 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2666 Corresponding_Procedure
2667 (Defining_Entity (Specification (Subp_Decl))));
2668 end if;
2670 -- Analyze any relocated source pragmas or pragmas created for aspect
2671 -- specifications.
2673 Decl := Next (Subp_Decl);
2674 while Present (Decl) loop
2676 -- Stop the search for pragmas once the body has been reached as
2677 -- this terminates the region where pragmas may appear.
2679 if Decl = N then
2680 exit;
2682 elsif Nkind (Decl) = N_Pragma then
2683 Analyze (Decl);
2684 end if;
2686 Next (Decl);
2687 end loop;
2689 Spec_Id := Defining_Entity (Subp_Decl);
2690 Set_Corresponding_Spec (N, Spec_Id);
2692 -- Mark the generated spec as a source construct to ensure that all
2693 -- calls to it are properly registered in ALI files for GNATprove.
2695 Set_Comes_From_Source (Spec_Id, True);
2697 -- Ensure that the specs of the subprogram declaration and its body
2698 -- are identical, otherwise they will appear non-conformant due to
2699 -- rewritings in the default values of formal parameters.
2701 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2702 Set_Specification (N, Body_Spec);
2703 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2704 end Build_Subprogram_Declaration;
2706 ----------------------------
2707 -- Check_Anonymous_Return --
2708 ----------------------------
2710 procedure Check_Anonymous_Return is
2711 Decl : Node_Id;
2712 Par : Node_Id;
2713 Scop : Entity_Id;
2715 begin
2716 if Present (Spec_Id) then
2717 Scop := Spec_Id;
2718 else
2719 Scop := Body_Id;
2720 end if;
2722 if Ekind (Scop) = E_Function
2723 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2724 and then not Is_Thunk (Scop)
2726 -- Skip internally built functions which handle the case of
2727 -- a null access (see Expand_Interface_Conversion)
2729 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2730 and then not Comes_From_Source (Parent (Scop)))
2732 and then (Has_Task (Designated_Type (Etype (Scop)))
2733 or else
2734 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2735 and then
2736 Is_Limited_Record
2737 (Etype (Designated_Type (Etype (Scop))))))
2738 and then Expander_Active
2739 then
2740 Decl := Build_Master_Declaration (Loc);
2742 if Present (Declarations (N)) then
2743 Prepend (Decl, Declarations (N));
2744 else
2745 Set_Declarations (N, New_List (Decl));
2746 end if;
2748 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2749 Set_Has_Master_Entity (Scop);
2751 -- Now mark the containing scope as a task master
2753 Par := N;
2754 while Nkind (Par) /= N_Compilation_Unit loop
2755 Par := Parent (Par);
2756 pragma Assert (Present (Par));
2758 -- If we fall off the top, we are at the outer level, and
2759 -- the environment task is our effective master, so nothing
2760 -- to mark.
2762 if Nkind (Par)
2763 in N_Task_Body | N_Block_Statement | N_Subprogram_Body
2764 then
2765 Set_Is_Task_Master (Par, True);
2766 exit;
2767 end if;
2768 end loop;
2769 end if;
2770 end Check_Anonymous_Return;
2772 -------------------------
2773 -- Check_Inline_Pragma --
2774 -------------------------
2776 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2777 Prag : Node_Id;
2778 Plist : List_Id;
2780 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2781 -- True when N is a pragma Inline or Inline_Always that applies
2782 -- to this subprogram.
2784 -----------------------
2785 -- Is_Inline_Pragma --
2786 -----------------------
2788 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2789 begin
2790 if Nkind (N) = N_Pragma
2791 and then
2792 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2793 or else (Pragma_Name_Unmapped (N) = Name_Inline
2794 and then
2795 (Front_End_Inlining or else Optimization_Level > 0)))
2796 and then Present (Pragma_Argument_Associations (N))
2797 then
2798 declare
2799 Pragma_Arg : Node_Id :=
2800 Expression (First (Pragma_Argument_Associations (N)));
2801 begin
2802 if Nkind (Pragma_Arg) = N_Selected_Component then
2803 Pragma_Arg := Selector_Name (Pragma_Arg);
2804 end if;
2806 return Chars (Pragma_Arg) = Chars (Body_Id);
2807 end;
2809 else
2810 return False;
2811 end if;
2812 end Is_Inline_Pragma;
2814 -- Start of processing for Check_Inline_Pragma
2816 begin
2817 if not Expander_Active then
2818 return;
2819 end if;
2821 if Is_List_Member (N)
2822 and then Present (Next (N))
2823 and then Is_Inline_Pragma (Next (N))
2824 then
2825 Prag := Next (N);
2827 elsif Nkind (N) /= N_Subprogram_Body_Stub
2828 and then Present (Declarations (N))
2829 and then Is_Inline_Pragma (First (Declarations (N)))
2830 then
2831 Prag := First (Declarations (N));
2833 else
2834 Prag := Empty;
2835 end if;
2837 if Present (Prag) and then Is_List_Member (N) then
2838 if Present (Spec_Id) then
2839 if Is_List_Member (Unit_Declaration_Node (Spec_Id))
2840 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2841 then
2842 Analyze (Prag);
2843 end if;
2844 else
2845 -- Create a subprogram declaration, to make treatment uniform.
2846 -- Make the sloc of the subprogram name that of the entity in
2847 -- the body, so that style checks find identical strings.
2849 declare
2850 Subp : constant Entity_Id :=
2851 Make_Defining_Identifier
2852 (Sloc (Body_Id), Chars (Body_Id));
2853 Decl : constant Node_Id :=
2854 Make_Subprogram_Declaration (Loc,
2855 Specification =>
2856 New_Copy_Tree (Specification (N)));
2858 begin
2859 -- Link the body and the generated spec
2861 Set_Corresponding_Body (Decl, Body_Id);
2863 if Nkind (N) = N_Subprogram_Body_Stub then
2864 Set_Corresponding_Spec_Of_Stub (N, Subp);
2865 else
2866 Set_Corresponding_Spec (N, Subp);
2867 end if;
2869 Set_Defining_Unit_Name (Specification (Decl), Subp);
2871 -- To ensure proper coverage when body is inlined, indicate
2872 -- whether the subprogram comes from source.
2874 Preserve_Comes_From_Source (Subp, N);
2876 if Present (First_Formal (Body_Id)) then
2877 Plist := Copy_Parameter_List (Body_Id);
2878 Set_Parameter_Specifications
2879 (Specification (Decl), Plist);
2880 end if;
2882 -- Move aspects to the new spec
2884 Move_Aspects (N, To => Decl);
2886 Insert_Before (N, Decl);
2887 Analyze (Decl);
2888 Analyze (Prag);
2889 Set_Has_Pragma_Inline (Subp);
2891 if Pragma_Name (Prag) = Name_Inline_Always then
2892 Set_Is_Inlined (Subp);
2893 Set_Has_Pragma_Inline_Always (Subp);
2894 end if;
2896 -- Prior to copying the subprogram body to create a template
2897 -- for it for subsequent inlining, remove the pragma from
2898 -- the current body so that the copy that will produce the
2899 -- new body will start from a completely unanalyzed tree.
2901 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2902 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2903 end if;
2905 Spec := Subp;
2906 end;
2907 end if;
2908 end if;
2909 end Check_Inline_Pragma;
2911 --------------------------
2912 -- Check_Missing_Return --
2913 --------------------------
2915 procedure Check_Missing_Return is
2916 HSS : constant Node_Id := Handled_Statement_Sequence (N);
2918 Id : Entity_Id;
2919 Missing_Ret : Boolean;
2921 begin
2922 if Nkind (Body_Spec) = N_Function_Specification then
2923 if Present (Spec_Id) then
2924 Id := Spec_Id;
2925 else
2926 Id := Body_Id;
2927 end if;
2929 -- A function body shall contain at least one return statement
2930 -- that applies to the function body, unless the function contains
2931 -- code_statements; RM 6.5(5).
2933 if Return_Present (Id) then
2934 Check_Returns (HSS, 'F', Missing_Ret);
2936 if Missing_Ret then
2937 Set_Has_Missing_Return (Id);
2938 end if;
2940 -- Within a premature instantiation of a package with no body, we
2941 -- build completions of the functions therein, with a Raise
2942 -- statement. No point in complaining about a missing return in
2943 -- this case.
2945 elsif Ekind (Id) = E_Function
2946 and then In_Instance
2947 and then Present (Statements (HSS))
2948 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2949 then
2950 null;
2952 elsif Is_Generic_Subprogram (Id)
2953 or else not Is_Machine_Code_Subprogram (Id)
2954 then
2955 Error_Msg_N ("missing RETURN statement in function body", N);
2956 end if;
2958 -- If procedure with No_Return, check returns
2960 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2961 if Present (Spec_Id) then
2962 Id := Spec_Id;
2963 else
2964 Id := Body_Id;
2965 end if;
2967 if No_Return (Id) then
2968 Check_Returns (HSS, 'P', Missing_Ret, Id);
2969 end if;
2970 end if;
2971 end Check_Missing_Return;
2973 -----------------------
2974 -- Disambiguate_Spec --
2975 -----------------------
2977 function Disambiguate_Spec return Entity_Id is
2978 Priv_Spec : Entity_Id;
2979 Spec_N : Entity_Id;
2981 procedure Replace_Types (To_Corresponding : Boolean);
2982 -- Depending on the flag, replace the type of formal parameters of
2983 -- Body_Id if it is a concurrent type implementing interfaces with
2984 -- the corresponding record type or the other way around.
2986 procedure Replace_Types (To_Corresponding : Boolean) is
2987 Formal : Entity_Id;
2988 Formal_Typ : Entity_Id;
2990 begin
2991 Formal := First_Formal (Body_Id);
2992 while Present (Formal) loop
2993 Formal_Typ := Etype (Formal);
2995 if Is_Class_Wide_Type (Formal_Typ) then
2996 Formal_Typ := Root_Type (Formal_Typ);
2997 end if;
2999 -- From concurrent type to corresponding record
3001 if To_Corresponding then
3002 if Is_Concurrent_Type (Formal_Typ)
3003 and then Present (Corresponding_Record_Type (Formal_Typ))
3004 and then
3005 Present (Interfaces
3006 (Corresponding_Record_Type (Formal_Typ)))
3007 then
3008 Set_Etype (Formal,
3009 Corresponding_Record_Type (Formal_Typ));
3010 end if;
3012 -- From corresponding record to concurrent type
3014 else
3015 if Is_Concurrent_Record_Type (Formal_Typ)
3016 and then Present (Interfaces (Formal_Typ))
3017 then
3018 Set_Etype (Formal,
3019 Corresponding_Concurrent_Type (Formal_Typ));
3020 end if;
3021 end if;
3023 Next_Formal (Formal);
3024 end loop;
3025 end Replace_Types;
3027 -- Start of processing for Disambiguate_Spec
3029 begin
3030 -- Try to retrieve the specification of the body as is. All error
3031 -- messages are suppressed because the body may not have a spec in
3032 -- its current state.
3034 Spec_N := Find_Corresponding_Spec (N, False);
3036 -- It is possible that this is the body of a primitive declared
3037 -- between a private and a full view of a concurrent type. The
3038 -- controlling parameter of the spec carries the concurrent type,
3039 -- not the corresponding record type as transformed by Analyze_
3040 -- Subprogram_Specification. In such cases, we undo the change
3041 -- made by the analysis of the specification and try to find the
3042 -- spec again.
3044 -- Note that wrappers already have their corresponding specs and
3045 -- bodies set during their creation, so if the candidate spec is
3046 -- a wrapper, then we definitely need to swap all types to their
3047 -- original concurrent status.
3049 if No (Spec_N)
3050 or else Is_Primitive_Wrapper (Spec_N)
3051 then
3052 -- Restore all references of corresponding record types to the
3053 -- original concurrent types.
3055 Replace_Types (To_Corresponding => False);
3056 Priv_Spec := Find_Corresponding_Spec (N, False);
3058 -- The current body truly belongs to a primitive declared between
3059 -- a private and a full view. We leave the modified body as is,
3060 -- and return the true spec.
3062 if Present (Priv_Spec)
3063 and then Is_Private_Primitive (Priv_Spec)
3064 then
3065 return Priv_Spec;
3066 end if;
3068 -- In case that this is some sort of error, restore the original
3069 -- state of the body.
3071 Replace_Types (To_Corresponding => True);
3072 end if;
3074 return Spec_N;
3075 end Disambiguate_Spec;
3077 ----------------------------
3078 -- Exchange_Limited_Views --
3079 ----------------------------
3081 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3082 Result : Elist_Id := No_Elist;
3084 procedure Detect_And_Exchange (Id : Entity_Id);
3085 -- Determine whether Id's type denotes an incomplete type associated
3086 -- with a limited with clause and exchange the limited view with the
3087 -- non-limited one when available. Note that the non-limited view
3088 -- may exist because of a with_clause in another unit in the context,
3089 -- but cannot be used because the current view of the enclosing unit
3090 -- is still a limited view.
3092 -------------------------
3093 -- Detect_And_Exchange --
3094 -------------------------
3096 procedure Detect_And_Exchange (Id : Entity_Id) is
3097 Typ : constant Entity_Id := Etype (Id);
3099 begin
3100 if From_Limited_With (Typ)
3101 and then Has_Non_Limited_View (Typ)
3102 and then not From_Limited_With (Scope (Typ))
3103 then
3104 if No (Result) then
3105 Result := New_Elmt_List;
3106 end if;
3108 Prepend_Elmt (Typ, Result);
3109 Prepend_Elmt (Id, Result);
3110 Set_Etype (Id, Non_Limited_View (Typ));
3111 end if;
3112 end Detect_And_Exchange;
3114 -- Local variables
3116 Formal : Entity_Id;
3118 -- Start of processing for Exchange_Limited_Views
3120 begin
3121 -- Do not process subprogram bodies as they already use the non-
3122 -- limited view of types.
3124 if Ekind (Subp_Id) not in E_Function | E_Procedure then
3125 return No_Elist;
3126 end if;
3128 -- Examine all formals and swap views when applicable
3130 Formal := First_Formal (Subp_Id);
3131 while Present (Formal) loop
3132 Detect_And_Exchange (Formal);
3134 Next_Formal (Formal);
3135 end loop;
3137 -- Process the return type of a function
3139 if Ekind (Subp_Id) = E_Function then
3140 Detect_And_Exchange (Subp_Id);
3141 end if;
3143 return Result;
3144 end Exchange_Limited_Views;
3146 ------------------------------------
3147 -- Generate_Minimum_Accessibility --
3148 ------------------------------------
3150 procedure Generate_Minimum_Accessibility
3151 (Extra_Access : Entity_Id;
3152 Related_Form : Entity_Id := Empty)
3154 Loc : constant Source_Ptr := Sloc (Body_Nod);
3155 Form : Entity_Id;
3156 Obj_Node : Node_Id;
3157 begin
3158 -- When no related formal exists then we are dealing with an
3159 -- extra accessibility formal for a function result.
3161 if No (Related_Form) then
3162 Form := Extra_Access;
3163 else
3164 Form := Related_Form;
3165 end if;
3167 -- Create the minimum accessibility object
3169 Obj_Node :=
3170 Make_Object_Declaration (Loc,
3171 Defining_Identifier =>
3172 Make_Temporary
3173 (Loc, 'A', Extra_Access),
3174 Object_Definition => New_Occurrence_Of
3175 (Standard_Natural, Loc),
3176 Expression =>
3177 Make_Attribute_Reference (Loc,
3178 Prefix => New_Occurrence_Of
3179 (Standard_Natural, Loc),
3180 Attribute_Name => Name_Min,
3181 Expressions => New_List (
3182 Make_Integer_Literal (Loc,
3183 Scope_Depth (Body_Id)),
3184 New_Occurrence_Of
3185 (Extra_Access, Loc))));
3187 -- Add the new local object to the Minimum_Acc_Obj to
3188 -- be later prepended to the subprogram's list of
3189 -- declarations after we are sure all expansion is
3190 -- done.
3192 if Present (Minimum_Acc_Objs) then
3193 Prepend (Obj_Node, Minimum_Acc_Objs);
3194 else
3195 Minimum_Acc_Objs := New_List (Obj_Node);
3196 end if;
3198 -- Register the object and analyze it
3200 Set_Minimum_Accessibility
3201 (Form, Defining_Identifier (Obj_Node));
3203 Analyze (Obj_Node);
3204 end Generate_Minimum_Accessibility;
3206 -------------------------------------
3207 -- Is_Private_Concurrent_Primitive --
3208 -------------------------------------
3210 function Is_Private_Concurrent_Primitive
3211 (Subp_Id : Entity_Id) return Boolean
3213 Formal_Typ : Entity_Id;
3215 begin
3216 if Present (First_Formal (Subp_Id)) then
3217 Formal_Typ := Etype (First_Formal (Subp_Id));
3219 if Is_Concurrent_Record_Type (Formal_Typ) then
3220 if Is_Class_Wide_Type (Formal_Typ) then
3221 Formal_Typ := Root_Type (Formal_Typ);
3222 end if;
3224 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3225 end if;
3227 -- The type of the first formal is a concurrent tagged type with
3228 -- a private view.
3230 return
3231 Is_Concurrent_Type (Formal_Typ)
3232 and then Is_Tagged_Type (Formal_Typ)
3233 and then Has_Private_Declaration (Formal_Typ);
3234 end if;
3236 return False;
3237 end Is_Private_Concurrent_Primitive;
3239 -------------------------
3240 -- Mask_Unfrozen_Types --
3241 -------------------------
3243 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3244 Result : Elist_Id := No_Elist;
3246 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3247 -- Mask all types referenced in the subtree rooted at Node as
3248 -- formally frozen.
3250 --------------------
3251 -- Mask_Type_Refs --
3252 --------------------
3254 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3255 procedure Mask_Type (Typ : Entity_Id);
3256 -- Mask a given type as formally frozen when outside the current
3257 -- scope, or else freeze the type.
3259 ---------------
3260 -- Mask_Type --
3261 ---------------
3263 procedure Mask_Type (Typ : Entity_Id) is
3264 begin
3265 -- Skip Itypes created by the preanalysis
3267 if Is_Itype (Typ)
3268 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3269 then
3270 return;
3271 end if;
3273 if not Is_Frozen (Typ) then
3274 if Scope (Typ) /= Current_Scope then
3275 Set_Is_Frozen (Typ);
3276 Append_New_Elmt (Typ, Result);
3277 else
3278 Freeze_Before (N, Typ);
3279 end if;
3280 end if;
3281 end Mask_Type;
3283 -- Start of processing for Mask_Type_Refs
3285 begin
3286 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3287 Mask_Type (Etype (Entity (Node)));
3289 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
3290 Mask_Type (Scope (Entity (Node)));
3291 end if;
3293 elsif Nkind (Node) in N_Aggregate | N_Null | N_Type_Conversion
3294 and then Present (Etype (Node))
3295 then
3296 Mask_Type (Etype (Node));
3297 end if;
3299 return OK;
3300 end Mask_Type_Refs;
3302 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3304 -- Local variables
3306 Return_Stmt : constant Node_Id :=
3307 First (Statements (Handled_Statement_Sequence (N)));
3309 -- Start of processing for Mask_Unfrozen_Types
3311 begin
3312 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3314 Mask_References (Expression (Return_Stmt));
3316 return Result;
3317 end Mask_Unfrozen_Types;
3319 ------------------
3320 -- Move_Pragmas --
3321 ------------------
3323 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
3324 Decl : Node_Id;
3325 Insert_Nod : Node_Id;
3326 Next_Decl : Node_Id;
3328 begin
3329 pragma Assert (Nkind (From) = N_Subprogram_Body);
3331 -- The pragmas are moved in an order-preserving fashion
3333 Insert_Nod := To;
3335 -- Inspect the declarations of the subprogram body and relocate all
3336 -- candidate pragmas.
3338 Decl := First (Declarations (From));
3339 while Present (Decl) loop
3341 -- Preserve the following declaration for iteration purposes, due
3342 -- to possible relocation of a pragma.
3344 Next_Decl := Next (Decl);
3346 if Nkind (Decl) = N_Pragma then
3347 -- Copy pragma SPARK_Mode if present in the declarative list
3348 -- of subprogram body From and insert it after node To. This
3349 -- pragma should not be moved, as it applies to the body too.
3351 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
3352 Insert_After (Insert_Nod, New_Copy_Tree (Decl));
3354 -- Move relevant pragmas to the spec
3356 elsif
3357 Pragma_Significant_To_Subprograms
3358 (Get_Pragma_Id (Decl))
3359 then
3360 Remove (Decl);
3361 Insert_After (Insert_Nod, Decl);
3362 Insert_Nod := Decl;
3363 end if;
3365 -- Skip internally generated code
3367 elsif not Comes_From_Source (Decl) then
3368 null;
3370 -- No candidate pragmas are available for relocation
3372 else
3373 exit;
3374 end if;
3376 Decl := Next_Decl;
3377 end loop;
3378 end Move_Pragmas;
3380 ---------------------------
3381 -- Restore_Limited_Views --
3382 ---------------------------
3384 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3385 Elmt : Elmt_Id := First_Elmt (Restore_List);
3386 Id : Entity_Id;
3388 begin
3389 while Present (Elmt) loop
3390 Id := Node (Elmt);
3391 Next_Elmt (Elmt);
3392 Set_Etype (Id, Node (Elmt));
3393 Next_Elmt (Elmt);
3394 end loop;
3395 end Restore_Limited_Views;
3397 ----------------------------
3398 -- Set_Trivial_Subprogram --
3399 ----------------------------
3401 procedure Set_Trivial_Subprogram (N : Node_Id) is
3402 Nxt : constant Node_Id := Next (N);
3404 begin
3405 Set_Is_Trivial_Subprogram (Body_Id);
3407 if Present (Spec_Id) then
3408 Set_Is_Trivial_Subprogram (Spec_Id);
3409 end if;
3411 if Present (Nxt)
3412 and then Nkind (Nxt) = N_Simple_Return_Statement
3413 and then No (Next (Nxt))
3414 and then Present (Expression (Nxt))
3415 and then Is_Entity_Name (Expression (Nxt))
3416 then
3417 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3418 end if;
3419 end Set_Trivial_Subprogram;
3421 ---------------------------
3422 -- Unmask_Unfrozen_Types --
3423 ---------------------------
3425 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3426 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3428 begin
3429 while Present (Elmt) loop
3430 Set_Is_Frozen (Node (Elmt), False);
3431 Next_Elmt (Elmt);
3432 end loop;
3433 end Unmask_Unfrozen_Types;
3435 ---------------------------------
3436 -- Verify_Overriding_Indicator --
3437 ---------------------------------
3439 procedure Verify_Overriding_Indicator is
3440 begin
3441 if Must_Override (Body_Spec) then
3442 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3443 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3444 then
3445 null;
3447 -- Overridden controlled primitives may have had their
3448 -- Overridden_Operation field cleared according to the setting of
3449 -- the Is_Hidden flag. An issue arises, however, when analyzing
3450 -- an instance that may have manipulated the flag during
3451 -- expansion. As a result, we add an exception for this case.
3453 elsif No (Overridden_Operation (Spec_Id))
3454 and then not (Chars (Spec_Id) in Name_Adjust
3455 | Name_Finalize
3456 | Name_Initialize
3457 and then In_Instance)
3458 then
3459 Error_Msg_NE
3460 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3462 -- Overriding indicators aren't allowed for protected subprogram
3463 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3464 -- this to a warning if -gnatd.E is enabled.
3466 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3467 Error_Msg_Warn := Error_To_Warning;
3468 Error_Msg_N
3469 ("<<overriding indicator not allowed for protected "
3470 & "subprogram body", Body_Spec);
3471 end if;
3473 elsif Must_Not_Override (Body_Spec) then
3474 if Present (Overridden_Operation (Spec_Id)) then
3475 Error_Msg_NE
3476 ("subprogram& overrides inherited operation",
3477 Body_Spec, Spec_Id);
3479 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3480 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3481 then
3482 Error_Msg_NE
3483 ("subprogram& overrides predefined operator",
3484 Body_Spec, Spec_Id);
3486 -- Overriding indicators aren't allowed for protected subprogram
3487 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3488 -- this to a warning if -gnatd.E is enabled.
3490 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3491 Error_Msg_Warn := Error_To_Warning;
3493 Error_Msg_N
3494 ("<<overriding indicator not allowed "
3495 & "for protected subprogram body", Body_Spec);
3497 -- If this is not a primitive operation, then the overriding
3498 -- indicator is altogether illegal.
3500 elsif not Is_Primitive (Spec_Id) then
3501 Error_Msg_N
3502 ("overriding indicator only allowed "
3503 & "if subprogram is primitive", Body_Spec);
3504 end if;
3506 -- If checking the style rule and the operation overrides, then
3507 -- issue a warning about a missing overriding_indicator. Protected
3508 -- subprogram bodies are excluded from this style checking, since
3509 -- they aren't primitives (even though their declarations can
3510 -- override) and aren't allowed to have an overriding_indicator.
3512 elsif Style_Check
3513 and then Present (Overridden_Operation (Spec_Id))
3514 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3515 then
3516 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3517 Style.Missing_Overriding (N, Body_Id);
3519 elsif Style_Check
3520 and then Can_Override_Operator (Spec_Id)
3521 and then not In_Predefined_Unit (Spec_Id)
3522 then
3523 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3524 Style.Missing_Overriding (N, Body_Id);
3525 end if;
3526 end Verify_Overriding_Indicator;
3528 -- Local variables
3530 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3531 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3532 Saved_EA : constant Boolean := Expander_Active;
3533 Saved_ISMP : constant Boolean :=
3534 Ignore_SPARK_Mode_Pragmas_In_Instance;
3535 -- Save the Ghost and SPARK mode-related data to restore on exit
3537 -- Start of processing for Analyze_Subprogram_Body_Helper
3539 begin
3540 -- A [generic] subprogram body freezes the contract of the nearest
3541 -- enclosing package body and all other contracts encountered in the
3542 -- same declarative part up to and excluding the subprogram body:
3544 -- package body Nearest_Enclosing_Package
3545 -- with Refined_State => (State => Constit)
3546 -- is
3547 -- Constit : ...;
3549 -- procedure Freezes_Enclosing_Package_Body
3550 -- with Refined_Depends => (Input => Constit) ...
3552 -- This ensures that any annotations referenced by the contract of the
3553 -- [generic] subprogram body are available. This form of freezing is
3554 -- decoupled from the usual Freeze_xxx mechanism because it must also
3555 -- work in the context of generics where normal freezing is disabled.
3557 -- Only bodies coming from source should cause this type of freezing.
3558 -- Expression functions that act as bodies and complete an initial
3559 -- declaration must be included in this category, hence the use of
3560 -- Original_Node.
3562 if Comes_From_Source (Original_Node (N)) then
3563 Freeze_Previous_Contracts (N);
3564 end if;
3566 -- Generic subprograms are handled separately. They always have a
3567 -- generic specification. Determine whether current scope has a
3568 -- previous declaration.
3570 -- If the subprogram body is defined within an instance of the same
3571 -- name, the instance appears as a package renaming, and will be hidden
3572 -- within the subprogram.
3574 if Present (Prev_Id)
3575 and then not Is_Overloadable (Prev_Id)
3576 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3577 or else Comes_From_Source (Prev_Id))
3578 then
3579 if Is_Generic_Subprogram (Prev_Id) then
3580 Spec_Id := Prev_Id;
3582 -- A subprogram body is Ghost when it is stand-alone and subject
3583 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3584 -- the mode now to ensure that any nodes generated during analysis
3585 -- and expansion are properly marked as Ghost.
3587 Mark_And_Set_Ghost_Body (N, Spec_Id);
3589 -- If the body completes the initial declaration of a compilation
3590 -- unit which is subject to pragma Elaboration_Checks, set the
3591 -- model specified by the pragma because it applies to all parts
3592 -- of the unit.
3594 Install_Elaboration_Model (Spec_Id);
3596 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3597 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3599 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3601 if Nkind (N) = N_Subprogram_Body then
3602 Check_Missing_Return;
3603 end if;
3605 goto Leave;
3607 -- Otherwise a previous entity conflicts with the subprogram name.
3608 -- Attempting to enter name will post error.
3610 else
3611 Enter_Name (Body_Id);
3612 goto Leave;
3613 end if;
3615 -- Non-generic case, find the subprogram declaration, if one was seen,
3616 -- or enter new overloaded entity in the current scope. If the
3617 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3618 -- part of the context of one of its subunits. No need to redo the
3619 -- analysis.
3621 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3622 goto Leave;
3624 else
3625 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3627 if Nkind (N) = N_Subprogram_Body_Stub
3628 or else No (Corresponding_Spec (N))
3629 then
3630 if Is_Private_Concurrent_Primitive (Body_Id) then
3631 Spec_Id := Disambiguate_Spec;
3633 -- A subprogram body is Ghost when it is stand-alone and
3634 -- subject to pragma Ghost or when the corresponding spec is
3635 -- Ghost. Set the mode now to ensure that any nodes generated
3636 -- during analysis and expansion are properly marked as Ghost.
3638 Mark_And_Set_Ghost_Body (N, Spec_Id);
3640 -- If the body completes a compilation unit which is subject
3641 -- to pragma Elaboration_Checks, set the model specified by
3642 -- the pragma because it applies to all parts of the unit.
3644 Install_Elaboration_Model (Spec_Id);
3646 else
3647 Spec_Id := Find_Corresponding_Spec (N);
3649 -- A subprogram body is Ghost when it is stand-alone and
3650 -- subject to pragma Ghost or when the corresponding spec is
3651 -- Ghost. Set the mode now to ensure that any nodes generated
3652 -- during analysis and expansion are properly marked as Ghost.
3654 Mark_And_Set_Ghost_Body (N, Spec_Id);
3656 -- If the body completes a compilation unit which is subject
3657 -- to pragma Elaboration_Checks, set the model specified by
3658 -- the pragma because it applies to all parts of the unit.
3660 Install_Elaboration_Model (Spec_Id);
3662 -- In GNATprove mode, if the body has no previous spec, create
3663 -- one so that the inlining machinery can operate properly.
3664 -- Transfer aspects, if any, to the new spec, so that they
3665 -- are legal and can be processed ahead of the body.
3666 -- We make two copies of the given spec, one for the new
3667 -- declaration, and one for the body.
3668 -- ??? This should be conditioned on front-end inlining rather
3669 -- than GNATprove_Mode.
3671 if No (Spec_Id) and then GNATprove_Mode
3673 -- Inlining does not apply during preanalysis of code
3675 and then Full_Analysis
3677 -- Inlining only applies to full bodies, not stubs
3679 and then Nkind (N) /= N_Subprogram_Body_Stub
3681 -- Inlining only applies to bodies in the source code, not to
3682 -- those generated by the compiler. In particular, expression
3683 -- functions, whose body is generated by the compiler, are
3684 -- treated specially by GNATprove.
3686 and then Comes_From_Source (Body_Id)
3688 -- This cannot be done for a compilation unit, which is not
3689 -- in a context where we can insert a new spec.
3691 and then Is_List_Member (N)
3693 -- Inlining only applies to subprograms without contracts,
3694 -- as a contract is a sign that GNATprove should perform a
3695 -- modular analysis of the subprogram instead of a contextual
3696 -- analysis at each call site. The same test is performed in
3697 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3698 -- here in another form (because the contract has not been
3699 -- attached to the body) to avoid front-end errors in case
3700 -- pragmas are used instead of aspects, because the
3701 -- corresponding pragmas in the body would not be transferred
3702 -- to the spec, leading to legality errors.
3704 and then not Body_Has_Contract
3705 and then not Inside_A_Generic
3706 then
3707 Build_Subprogram_Declaration;
3709 -- If this is a function that returns a constrained array, and
3710 -- Transform_Function_Array is set, create subprogram
3711 -- declaration to simplify e.g. subsequent C generation.
3713 elsif No (Spec_Id)
3714 and then Transform_Function_Array
3715 and then Nkind (Body_Spec) = N_Function_Specification
3716 and then Is_Array_Type (Etype (Body_Id))
3717 and then Is_Constrained (Etype (Body_Id))
3718 then
3719 Build_Subprogram_Declaration;
3720 end if;
3721 end if;
3723 -- If this is a duplicate body, no point in analyzing it
3725 if Error_Posted (N) then
3726 goto Leave;
3727 end if;
3729 -- A subprogram body should cause freezing of its own declaration,
3730 -- so, if the body and spec are compilation units, we must do it
3731 -- manually here. Moreover, if the return type is anonymous access
3732 -- to protected subprogram, it must be frozen before the body
3733 -- because its expansion has generated an equivalent type that is
3734 -- used when elaborating the body.
3736 if Present (Spec_Id)
3737 and then Nkind (Parent (N)) = N_Compilation_Unit
3738 then
3739 Freeze_Before (N, Spec_Id);
3741 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3742 Freeze_Before (N, Etype (Body_Id));
3743 end if;
3745 else
3746 Spec_Id := Corresponding_Spec (N);
3748 -- A subprogram body is Ghost when it is stand-alone and subject
3749 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3750 -- the mode now to ensure that any nodes generated during analysis
3751 -- and expansion are properly marked as Ghost.
3753 Mark_And_Set_Ghost_Body (N, Spec_Id);
3755 -- If the body completes the initial declaration of a compilation
3756 -- unit which is subject to pragma Elaboration_Checks, set the
3757 -- model specified by the pragma because it applies to all parts
3758 -- of the unit.
3760 Install_Elaboration_Model (Spec_Id);
3761 end if;
3762 end if;
3764 -- Deactivate expansion inside the body of ignored Ghost entities,
3765 -- as this code will ultimately be ignored. This avoids requiring the
3766 -- presence of run-time units which are not needed. Only do this for
3767 -- user entities, as internally generated entitities might still need
3768 -- to be expanded (e.g. those generated for types).
3770 if Present (Ignored_Ghost_Region)
3771 and then Comes_From_Source (Body_Id)
3772 then
3773 Expander_Active := False;
3774 end if;
3776 -- Previously we scanned the body to look for nested subprograms, and
3777 -- rejected an inline directive if nested subprograms were present,
3778 -- because the back-end would generate conflicting symbols for the
3779 -- nested bodies. This is now unnecessary.
3781 -- Look ahead to recognize a pragma Inline that appears after the body
3783 Check_Inline_Pragma (Spec_Id);
3785 -- Deal with special case of a fully private operation in the body of
3786 -- the protected type. We must create a declaration for the subprogram,
3787 -- in order to attach the subprogram that will be used in internal
3788 -- calls. We exclude compiler generated bodies from the expander since
3789 -- the issue does not arise for those cases.
3791 if No (Spec_Id)
3792 and then Comes_From_Source (N)
3793 and then Is_Protected_Type (Current_Scope)
3794 then
3795 Spec_Id := Build_Internal_Protected_Declaration (N);
3796 end if;
3798 -- If Transform_Function_Array is set and this is a function returning a
3799 -- constrained array type for which we must create a procedure with an
3800 -- extra out parameter, build and analyze the body now. The procedure
3801 -- declaration has already been created. We reuse the source body of the
3802 -- function, because in an instance it may contain global references
3803 -- that cannot be reanalyzed. The source function itself is not used any
3804 -- further, so we mark it as having a completion. If the subprogram is a
3805 -- stub the transformation is done later, when the proper body is
3806 -- analyzed.
3808 if Expander_Active
3809 and then Transform_Function_Array
3810 and then Nkind (N) /= N_Subprogram_Body_Stub
3811 then
3812 declare
3813 S : constant Entity_Id :=
3814 (if Present (Spec_Id)
3815 then Spec_Id
3816 else Defining_Unit_Name (Specification (N)));
3817 Proc_Body : Node_Id;
3819 begin
3820 if Ekind (S) = E_Function and then Rewritten_For_C (S) then
3821 Set_Has_Completion (S);
3822 Proc_Body := Build_Procedure_Body_Form (S, N);
3824 if Present (Spec_Id) then
3825 Rewrite (N, Proc_Body);
3826 Analyze (N);
3828 -- The entity for the created procedure must remain
3829 -- invisible, so it does not participate in resolution of
3830 -- subsequent references to the function.
3832 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3834 -- If we do not have a separate spec for N, build one and
3835 -- insert the new body right after.
3837 else
3838 Rewrite (N,
3839 Make_Subprogram_Declaration (Loc,
3840 Specification => Relocate_Node (Specification (N))));
3841 Analyze (N);
3842 Insert_After_And_Analyze (N, Proc_Body);
3843 Set_Is_Immediately_Visible
3844 (Corresponding_Spec (Proc_Body), False);
3845 end if;
3847 goto Leave;
3848 end if;
3849 end;
3850 end if;
3852 -- If a separate spec is present, then deal with freezing issues
3854 if Present (Spec_Id) then
3855 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3856 Verify_Overriding_Indicator;
3858 -- For functions with separate spec, if their return type was visible
3859 -- through a limited-with context clause, their extra formals were
3860 -- not added when the spec was frozen. Now the full view must be
3861 -- available, and the extra formals can be created and Returns_By_Ref
3862 -- computed (required to generate its return statements).
3864 if Ekind (Spec_Id) = E_Function
3865 and then From_Limited_With (Etype (Spec_Id))
3866 and then Is_Build_In_Place_Function (Spec_Id)
3867 and then not Has_BIP_Formals (Spec_Id)
3868 then
3869 Create_Extra_Formals (Spec_Id);
3870 Compute_Returns_By_Ref (Spec_Id);
3871 end if;
3873 -- In general, the spec will be frozen when we start analyzing the
3874 -- body. However, for internally generated operations, such as
3875 -- wrapper functions for inherited operations with controlling
3876 -- results, the spec may not have been frozen by the time we expand
3877 -- the freeze actions that include the bodies. In particular, extra
3878 -- formals for accessibility or for return-in-place may need to be
3879 -- generated. Freeze nodes, if any, are inserted before the current
3880 -- body. These freeze actions are also needed in Compile_Only mode to
3881 -- enable the proper back-end type annotations.
3882 -- They are necessary in any case to ensure proper elaboration order
3883 -- in gigi.
3885 if Nkind (N) = N_Subprogram_Body
3886 and then Was_Expression_Function (N)
3887 and then not Has_Completion (Spec_Id)
3888 and then Serious_Errors_Detected = 0
3889 and then (Expander_Active
3890 or else Operating_Mode = Check_Semantics
3891 or else Is_Ignored_Ghost_Entity (Spec_Id))
3892 then
3893 -- The body generated for an expression function that is not a
3894 -- completion is a freeze point neither for the profile nor for
3895 -- anything else. That's why, in order to prevent any freezing
3896 -- during analysis, we need to mask types declared outside the
3897 -- expression (and in an outer scope) that are not yet frozen.
3898 -- This also needs to be done in the case of an ignored Ghost
3899 -- expression function, where the expander isn't active.
3901 -- A further complication arises if the expression function is
3902 -- a primitive operation of a tagged type: in that case the
3903 -- function entity must be frozen before the dispatch table for
3904 -- the type is constructed, so it will be frozen like other local
3905 -- entities, at the end of the current scope.
3907 if not Is_Dispatching_Operation (Spec_Id) then
3908 Set_Is_Frozen (Spec_Id);
3909 end if;
3911 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
3913 elsif not Is_Frozen (Spec_Id)
3914 and then Serious_Errors_Detected = 0
3915 then
3916 Set_Has_Delayed_Freeze (Spec_Id);
3917 Create_Extra_Formals (Spec_Id);
3918 Freeze_Before (N, Spec_Id);
3919 end if;
3920 end if;
3922 -- Place subprogram on scope stack, and make formals visible. If there
3923 -- is a spec, the visible entity remains that of the spec.
3925 if Present (Spec_Id) then
3926 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3928 if Is_Child_Unit (Spec_Id) then
3929 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3930 end if;
3932 if Style_Check then
3933 Style.Check_Identifier (Body_Id, Spec_Id);
3934 end if;
3936 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3937 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3939 if Is_Abstract_Subprogram (Spec_Id) then
3940 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3941 goto Leave;
3943 else
3944 Set_Convention (Body_Id, Convention (Spec_Id));
3945 Set_Has_Completion (Spec_Id);
3947 if Is_Protected_Type (Scope (Spec_Id)) then
3948 Prot_Typ := Scope (Spec_Id);
3949 end if;
3951 -- If this is a body generated for a renaming, do not check for
3952 -- full conformance. The check is redundant, because the spec of
3953 -- the body is a copy of the spec in the renaming declaration,
3954 -- and the test can lead to spurious errors on nested defaults.
3956 if Present (Spec_Decl)
3957 and then not Comes_From_Source (N)
3958 and then
3959 (Nkind (Original_Node (Spec_Decl)) =
3960 N_Subprogram_Renaming_Declaration
3961 or else (Present (Corresponding_Body (Spec_Decl))
3962 and then
3963 Nkind (Unit_Declaration_Node
3964 (Corresponding_Body (Spec_Decl))) =
3965 N_Subprogram_Renaming_Declaration))
3966 then
3967 Conformant := True;
3969 -- Conversely, the spec may have been generated for specless body
3970 -- with an inline pragma. The entity comes from source, which is
3971 -- both semantically correct and necessary for proper inlining.
3972 -- The subprogram declaration itself is not in the source.
3974 elsif Comes_From_Source (N)
3975 and then Present (Spec_Decl)
3976 and then not Comes_From_Source (Spec_Decl)
3977 and then Has_Pragma_Inline (Spec_Id)
3978 then
3979 Conformant := True;
3981 -- Finally, a body generated for an expression function copies
3982 -- the profile of the function and no check is needed either.
3983 -- If the body is the completion of a previous function
3984 -- declared elsewhere, the conformance check is required.
3986 elsif Nkind (N) = N_Subprogram_Body
3987 and then Was_Expression_Function (N)
3988 and then Sloc (Spec_Id) = Sloc (Body_Id)
3989 then
3990 Conformant := True;
3992 else
3993 Check_Conformance
3994 (Body_Id, Spec_Id,
3995 Fully_Conformant, True, Conformant, Body_Id);
3996 end if;
3998 -- If the body is not fully conformant, we have to decide if we
3999 -- should analyze it or not. If it has a really messed up profile
4000 -- then we probably should not analyze it, since we will get too
4001 -- many bogus messages.
4003 -- Our decision is to go ahead in the non-fully conformant case
4004 -- only if it is at least mode conformant with the spec. Note
4005 -- that the call to Check_Fully_Conformant has issued the proper
4006 -- error messages to complain about the lack of conformance.
4008 if not Conformant
4009 and then not Mode_Conformant (Body_Id, Spec_Id)
4010 then
4011 goto Leave;
4012 end if;
4013 end if;
4015 -- In the case we are dealing with an expression function we check
4016 -- the formals attached to the spec instead of the body - so we don't
4017 -- reference body formals.
4019 if Spec_Id /= Body_Id
4020 and then not Is_Expression_Function (Spec_Id)
4021 then
4022 Reference_Body_Formals (Spec_Id, Body_Id);
4023 end if;
4025 Reinit_Field_To_Zero (Body_Id, F_Has_Out_Or_In_Out_Parameter,
4026 Old_Ekind => (E_Function | E_Procedure => True, others => False));
4027 Reinit_Field_To_Zero (Body_Id, F_Needs_No_Actuals);
4028 Reinit_Field_To_Zero (Body_Id, F_Is_Predicate_Function);
4029 Reinit_Field_To_Zero (Body_Id, F_Protected_Subprogram);
4030 Reinit_Field_To_Zero (Body_Id, F_Is_Inlined_Always);
4031 Reinit_Field_To_Zero (Body_Id, F_Is_Generic_Actual_Subprogram);
4032 Reinit_Field_To_Zero (Body_Id, F_Is_Primitive_Wrapper);
4033 Reinit_Field_To_Zero (Body_Id, F_Is_Private_Primitive);
4034 Reinit_Field_To_Zero (Body_Id, F_Original_Protected_Subprogram);
4035 Reinit_Field_To_Zero (Body_Id, F_Wrapped_Entity);
4037 if Ekind (Body_Id) = E_Procedure then
4038 Reinit_Field_To_Zero (Body_Id, F_Receiving_Entry);
4039 end if;
4041 Mutate_Ekind (Body_Id, E_Subprogram_Body);
4043 if Nkind (N) = N_Subprogram_Body_Stub then
4044 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4046 -- Regular body
4048 else
4049 Set_Corresponding_Spec (N, Spec_Id);
4051 -- Ada 2005 (AI-345): If the operation is a primitive operation
4052 -- of a concurrent type, the type of the first parameter has been
4053 -- replaced with the corresponding record, which is the proper
4054 -- run-time structure to use. However, within the body there may
4055 -- be uses of the formals that depend on primitive operations
4056 -- of the type (in particular calls in prefixed form) for which
4057 -- we need the original concurrent type. The operation may have
4058 -- several controlling formals, so the replacement must be done
4059 -- for all of them.
4061 if Comes_From_Source (Spec_Id)
4062 and then Present (First_Entity (Spec_Id))
4063 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4064 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4065 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4066 and then Present (Corresponding_Concurrent_Type
4067 (Etype (First_Entity (Spec_Id))))
4068 then
4069 declare
4070 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4071 Form : Entity_Id;
4073 begin
4074 Form := First_Formal (Spec_Id);
4075 while Present (Form) loop
4076 if Etype (Form) = Typ then
4077 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4078 end if;
4080 Next_Formal (Form);
4081 end loop;
4082 end;
4083 end if;
4085 -- Make the formals visible, and place subprogram on scope stack.
4086 -- This is also the point at which we set Last_Real_Spec_Entity
4087 -- to mark the entities which will not be moved to the body.
4089 Install_Formals (Spec_Id);
4090 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4092 -- Within an instance, add local renaming declarations so that
4093 -- gdb can retrieve the values of actuals more easily. This is
4094 -- only relevant if generating code.
4096 if Is_Generic_Instance (Spec_Id)
4097 and then Is_Wrapper_Package (Current_Scope)
4098 and then Expander_Active
4099 then
4100 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4101 end if;
4103 Push_Scope (Spec_Id);
4105 -- Make sure that the subprogram is immediately visible. For
4106 -- child units that have no separate spec this is indispensable.
4107 -- Otherwise it is safe albeit redundant.
4109 Set_Is_Immediately_Visible (Spec_Id);
4110 end if;
4112 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4113 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4114 Set_Scope (Body_Id, Scope (Spec_Id));
4116 -- Case of subprogram body with no previous spec
4118 else
4119 -- Check for style warning required
4121 if Style_Check
4123 -- Only apply check for source level subprograms for which checks
4124 -- have not been suppressed.
4126 and then Comes_From_Source (Body_Id)
4127 and then not Suppress_Style_Checks (Body_Id)
4129 -- No warnings within an instance
4131 and then not In_Instance
4133 -- No warnings for expression functions
4135 and then (Nkind (N) /= N_Subprogram_Body
4136 or else not Was_Expression_Function (N))
4137 then
4138 Style.Body_With_No_Spec (N);
4139 end if;
4141 -- First set Acts_As_Spec if appropriate
4143 if Nkind (N) /= N_Subprogram_Body_Stub then
4144 Set_Acts_As_Spec (N);
4145 end if;
4147 New_Overloaded_Entity (Body_Id);
4149 -- A subprogram body should cause freezing of its own declaration,
4150 -- but if there was no previous explicit declaration, then the
4151 -- subprogram will get frozen too late (there may be code within
4152 -- the body that depends on the subprogram having been frozen,
4153 -- such as uses of extra formals), so we force it to be frozen here.
4154 -- An exception in Ada 2012 is that the body created for expression
4155 -- functions does not freeze.
4157 if Nkind (N) /= N_Subprogram_Body
4158 or else not Was_Expression_Function (N)
4159 then
4160 -- First clear the Is_Public flag on thunks since they are only
4161 -- referenced locally by dispatch tables and thus never inlined.
4163 if Is_Thunk (Body_Id) then
4164 Set_Is_Public (Body_Id, False);
4165 end if;
4167 Freeze_Before (N, Body_Id);
4168 end if;
4170 if Nkind (N) /= N_Subprogram_Body_Stub then
4171 Generate_Definition (Body_Id);
4172 Generate_Reference
4173 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4175 -- If the body is an entry wrapper created for an entry with
4176 -- preconditions, it must be compiled in the context of the
4177 -- enclosing synchronized object, because it may mention other
4178 -- operations of the type.
4180 if Is_Entry_Wrapper (Body_Id) then
4181 declare
4182 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4183 begin
4184 Push_Scope (Prot);
4185 Install_Declarations (Prot);
4186 end;
4187 end if;
4189 Install_Formals (Body_Id);
4191 Push_Scope (Body_Id);
4192 end if;
4194 -- For stubs and bodies with no previous spec, generate references to
4195 -- formals.
4197 Generate_Reference_To_Formals (Body_Id);
4198 end if;
4200 -- Entry barrier functions are generated outside the protected type and
4201 -- should not carry the SPARK_Mode of the enclosing context.
4203 if Nkind (N) = N_Subprogram_Body
4204 and then Is_Entry_Barrier_Function (N)
4205 then
4206 null;
4208 -- The body is generated as part of expression function expansion. When
4209 -- the expression function appears in the visible declarations of a
4210 -- package, the body is added to the private declarations. Since both
4211 -- declarative lists may be subject to a different SPARK_Mode, inherit
4212 -- the mode of the spec.
4214 -- package P with SPARK_Mode is
4215 -- function Expr_Func ... is (...); -- original
4216 -- [function Expr_Func ...;] -- generated spec
4217 -- -- mode is ON
4218 -- private
4219 -- pragma SPARK_Mode (Off);
4220 -- [function Expr_Func ... is return ...;] -- generated body
4221 -- end P; -- mode is ON
4223 elsif not Comes_From_Source (N)
4224 and then Present (Spec_Id)
4225 and then Is_Expression_Function (Spec_Id)
4226 then
4227 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4228 Set_SPARK_Pragma_Inherited
4229 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4231 -- Set the SPARK_Mode from the current context (may be overwritten later
4232 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4233 -- initially on a stand-alone subprogram body, but is then relocated to
4234 -- a generated corresponding spec. In this scenario the mode is shared
4235 -- between the spec and body.
4237 elsif No (SPARK_Pragma (Body_Id)) then
4238 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4239 Set_SPARK_Pragma_Inherited (Body_Id);
4240 end if;
4242 -- A subprogram body may be instantiated or inlined at a later pass.
4243 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4244 -- applied to the initial declaration of the body.
4246 if Present (Spec_Id) then
4247 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4248 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4249 end if;
4251 else
4252 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4253 -- case the body is instantiated or inlined later and out of context.
4254 -- The body uses this attribute to restore the value of the global
4255 -- flag.
4257 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4258 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4260 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4261 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4262 end if;
4263 end if;
4265 -- Preserve relevant elaboration-related attributes of the context which
4266 -- are no longer available or very expensive to recompute once analysis,
4267 -- resolution, and expansion are over.
4269 if No (Spec_Id) then
4270 Mark_Elaboration_Attributes
4271 (N_Id => Body_Id,
4272 Checks => True,
4273 Warnings => True);
4274 end if;
4276 -- If this is the proper body of a stub, we must verify that the stub
4277 -- conforms to the body, and to the previous spec if one was present.
4278 -- We know already that the body conforms to that spec. This test is
4279 -- only required for subprograms that come from source.
4281 if Nkind (Parent (N)) = N_Subunit
4282 and then Comes_From_Source (N)
4283 and then not Error_Posted (Body_Id)
4284 and then Nkind (Corresponding_Stub (Parent (N))) =
4285 N_Subprogram_Body_Stub
4286 then
4287 declare
4288 Old_Id : constant Entity_Id :=
4289 Defining_Entity
4290 (Specification (Corresponding_Stub (Parent (N))));
4292 Conformant : Boolean := False;
4294 begin
4295 if No (Spec_Id) then
4296 Check_Fully_Conformant (Body_Id, Old_Id);
4298 else
4299 Check_Conformance
4300 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4302 if not Conformant then
4304 -- The stub was taken to be a new declaration. Indicate that
4305 -- it lacks a body.
4307 Set_Has_Completion (Old_Id, False);
4308 end if;
4309 end if;
4310 end;
4311 end if;
4313 Set_Has_Completion (Body_Id);
4314 Check_Eliminated (Body_Id);
4316 -- Analyze any aspect specifications that appear on the subprogram body
4317 -- stub. Stop the analysis now as the stub does not have a declarative
4318 -- or a statement part, and it cannot be inlined.
4320 if Nkind (N) = N_Subprogram_Body_Stub then
4321 if Has_Aspects (N) then
4322 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4323 end if;
4325 goto Leave;
4326 end if;
4328 -- Handle inlining
4330 if Expander_Active
4331 and then Serious_Errors_Detected = 0
4332 and then Present (Spec_Id)
4333 and then Has_Pragma_Inline (Spec_Id)
4334 then
4335 -- Legacy implementation (relying on front-end inlining)
4337 if not Back_End_Inlining then
4338 if Has_Pragma_Inline_Always (Spec_Id)
4339 or else (Front_End_Inlining
4340 and then not Opt.Disable_FE_Inline)
4341 then
4342 Build_Body_To_Inline (N, Spec_Id);
4343 end if;
4345 -- New implementation (relying on back-end inlining)
4347 else
4348 if Has_Pragma_Inline_Always (Spec_Id)
4349 or else Optimization_Level > 0
4350 then
4351 -- Handle function returning an unconstrained type
4353 if Comes_From_Source (Body_Id)
4354 and then Ekind (Spec_Id) = E_Function
4355 and then Returns_Unconstrained_Type (Spec_Id)
4357 -- If function builds in place, i.e. returns a limited type,
4358 -- inlining cannot be done.
4360 and then not Is_Limited_Type (Etype (Spec_Id))
4361 then
4362 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4364 else
4365 declare
4366 Subp_Body : constant Node_Id :=
4367 Unit_Declaration_Node (Body_Id);
4368 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4370 begin
4371 -- Do not pass inlining to the backend if the subprogram
4372 -- has declarations or statements which cannot be inlined
4373 -- by the backend. This check is done here to emit an
4374 -- error instead of the generic warning message reported
4375 -- by the GCC backend (ie. "function might not be
4376 -- inlinable").
4378 if Has_Excluded_Declaration (Spec_Id, Subp_Decl) then
4379 null;
4381 elsif Has_Excluded_Statement
4382 (Spec_Id,
4383 Statements
4384 (Handled_Statement_Sequence (Subp_Body)))
4385 then
4386 null;
4388 -- If the backend inlining is available then at this
4389 -- stage we only have to mark the subprogram as inlined.
4390 -- The expander will take care of registering it in the
4391 -- table of subprograms inlined by the backend a part of
4392 -- processing calls to it (cf. Expand_Call)
4394 else
4395 Set_Is_Inlined (Spec_Id);
4396 end if;
4397 end;
4398 end if;
4399 end if;
4400 end if;
4402 -- In GNATprove mode, inline only when there is a separate subprogram
4403 -- declaration for now, as inlining of subprogram bodies acting as
4404 -- declarations, or subprogram stubs, are not supported by front-end
4405 -- inlining. This inlining should occur after analysis of the body, so
4406 -- that it is known whether the value of SPARK_Mode, which can be
4407 -- defined by a pragma inside the body, is applicable to the body.
4408 -- Inlining can be disabled with switch -gnatdm
4410 elsif GNATprove_Mode
4411 and then Full_Analysis
4412 and then not Inside_A_Generic
4413 and then Present (Spec_Id)
4414 and then
4415 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4416 and then Body_Has_SPARK_Mode_On
4417 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4418 and then not Body_Has_Contract
4419 and then not Debug_Flag_M
4420 then
4421 Build_Body_To_Inline (N, Spec_Id);
4422 end if;
4424 -- When generating code, inherited pre/postconditions are handled when
4425 -- expanding the corresponding contract.
4427 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4428 -- of the specification we have to install the private withed units.
4429 -- This holds for child units as well.
4431 if Is_Compilation_Unit (Body_Id)
4432 or else Nkind (Parent (N)) = N_Compilation_Unit
4433 then
4434 Install_Private_With_Clauses (Body_Id);
4435 end if;
4437 Check_Anonymous_Return;
4439 -- Set the Protected_Formal field of each extra formal of the protected
4440 -- subprogram to reference the corresponding extra formal of the
4441 -- subprogram that implements it. For regular formals this occurs when
4442 -- the protected subprogram's declaration is expanded, but the extra
4443 -- formals don't get created until the subprogram is frozen. We need to
4444 -- do this before analyzing the protected subprogram's body so that any
4445 -- references to the original subprogram's extra formals will be changed
4446 -- refer to the implementing subprogram's formals (see Expand_Formal).
4448 if Present (Spec_Id)
4449 and then Is_Protected_Type (Scope (Spec_Id))
4450 and then Present (Protected_Body_Subprogram (Spec_Id))
4451 then
4452 declare
4453 Impl_Subp : constant Entity_Id :=
4454 Protected_Body_Subprogram (Spec_Id);
4455 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4456 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4458 begin
4459 while Present (Prot_Ext_Formal) loop
4460 pragma Assert (Present (Impl_Ext_Formal));
4461 pragma Assert (not Is_Build_In_Place_Entity (Prot_Ext_Formal)
4462 or else BIP_Suffix_Kind (Impl_Ext_Formal)
4463 = BIP_Suffix_Kind (Prot_Ext_Formal));
4465 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4466 Next_Formal_With_Extras (Prot_Ext_Formal);
4467 Next_Formal_With_Extras (Impl_Ext_Formal);
4468 end loop;
4469 end;
4470 end if;
4472 -- Generate minimum accessibility local objects to correspond with
4473 -- any extra formal added for anonymous access types. This new local
4474 -- object can then be used instead of the formal in case it is used
4475 -- in an actual to a call to a nested subprogram.
4477 -- This method is used to supplement our "small integer model" for
4478 -- accessibility-check generation (for more information see
4479 -- Accessibility_Level).
4481 -- Because we allow accessibility values greater than our expected value
4482 -- passing along the same extra accessibility formal as an actual
4483 -- to a nested subprogram becomes a problem because high values mean
4484 -- different things to the callee even though they are the same to the
4485 -- caller. So, as described in the first section, we create a local
4486 -- object representing the minimum of the accessibility level value that
4487 -- is passed in and the accessibility level of the callee's parameter
4488 -- and locals and use it in the case of a call to a nested subprogram.
4489 -- This generated object is referred to as a "minimum accessibility
4490 -- level."
4492 if Present (Spec_Id) or else Present (Body_Id) then
4493 Body_Nod := Unit_Declaration_Node (Body_Id);
4495 declare
4496 Form : Entity_Id;
4497 begin
4498 -- Grab the appropriate formal depending on whether there exists
4499 -- an actual spec for the subprogram or whether we are dealing
4500 -- with a protected subprogram.
4502 if Present (Spec_Id) then
4503 if Present (Protected_Body_Subprogram (Spec_Id)) then
4504 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4505 else
4506 Form := First_Formal (Spec_Id);
4507 end if;
4508 else
4509 Form := First_Formal (Body_Id);
4510 end if;
4512 -- Loop through formals if the subprogram is capable of accepting
4513 -- a generated local object. If it is not then it is also not
4514 -- capable of having local subprograms meaning it would not need
4515 -- a minimum accessibility level object anyway.
4517 if Present (Body_Nod)
4518 and then Has_Declarations (Body_Nod)
4519 and then Nkind (Body_Nod) /= N_Package_Specification
4520 then
4521 while Present (Form) loop
4523 if Present (Extra_Accessibility (Form))
4524 and then No (Minimum_Accessibility (Form))
4525 then
4526 -- Generate the minimum accessibility level object
4528 -- A60b : constant natural := natural'min(1, paramL);
4530 Generate_Minimum_Accessibility
4531 (Extra_Accessibility (Form), Form);
4532 end if;
4534 Next_Formal (Form);
4535 end loop;
4537 -- Generate the minimum accessibility level object for the
4538 -- function's Extra_Accessibility_Of_Result.
4540 -- A31b : constant natural := natural'min (2, funcL);
4542 if Ekind (Body_Id) = E_Function
4543 and then Present (Extra_Accessibility_Of_Result (Body_Id))
4544 then
4545 Generate_Minimum_Accessibility
4546 (Extra_Accessibility_Of_Result (Body_Id));
4548 -- Replace the Extra_Accessibility_Of_Result with the new
4549 -- minimum accessibility object.
4551 Set_Extra_Accessibility_Of_Result
4552 (Body_Id, Minimum_Accessibility
4553 (Extra_Accessibility_Of_Result (Body_Id)));
4554 end if;
4555 end if;
4556 end;
4557 end if;
4559 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4560 -- may now appear in parameter and result profiles. Since the analysis
4561 -- of a subprogram body may use the parameter and result profile of the
4562 -- spec, swap any limited views with their non-limited counterpart.
4564 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4565 Exch_Views := Exchange_Limited_Views (Spec_Id);
4566 end if;
4568 -- Analyze any aspect specifications that appear on the subprogram body
4570 if Has_Aspects (N) then
4571 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4572 end if;
4574 -- Process the contract of the subprogram body after analyzing all the
4575 -- contract-related pragmas within the declarations.
4577 Analyze_Pragmas_In_Declarations (Body_Id);
4578 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4580 Set_Actual_Subtypes (N, Current_Scope);
4582 -- Add a declaration for the Protection object, renaming declarations
4583 -- for discriminals and privals and finally a declaration for the entry
4584 -- family index (if applicable). This form of early expansion is done
4585 -- when the Expander is active because Install_Private_Data_Declarations
4586 -- references entities which were created during regular expansion. The
4587 -- subprogram entity must come from source, and not be an internally
4588 -- generated subprogram.
4590 if Expander_Active
4591 and then Present (Prot_Typ)
4592 and then Present (Spec_Id)
4593 and then Comes_From_Source (Spec_Id)
4594 and then not Is_Eliminated (Spec_Id)
4595 then
4596 Install_Private_Data_Declarations
4597 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4598 end if;
4600 -- If the return type is an anonymous access type whose designated type
4601 -- is the limited view of a class-wide type and the non-limited view is
4602 -- available, update the return type accordingly.
4604 if Ada_Version >= Ada_2005
4605 and then Present (Spec_Id)
4606 and then Ekind (Etype (Spec_Id)) = E_Anonymous_Access_Type
4607 then
4608 declare
4609 Etyp : Entity_Id;
4611 begin
4612 Etyp := Directly_Designated_Type (Etype (Spec_Id));
4614 if Is_Class_Wide_Type (Etyp)
4615 and then From_Limited_With (Etyp)
4616 and then Has_Non_Limited_View (Etyp)
4617 then
4618 Desig_View := Etyp;
4619 Etyp := Non_Limited_View (Etyp);
4621 -- If the class-wide type has been created by the completion of
4622 -- an incomplete tagged type declaration, get the class-wide
4623 -- type of the incomplete tagged type to match Find_Type_Name.
4625 if Nkind (Parent (Etyp)) = N_Full_Type_Declaration
4626 and then Present (Incomplete_View (Parent (Etyp)))
4627 then
4628 Etyp := Class_Wide_Type (Incomplete_View (Parent (Etyp)));
4629 end if;
4631 Set_Directly_Designated_Type (Etype (Spec_Id), Etyp);
4632 end if;
4633 end;
4634 end if;
4636 Analyze_Declarations (Declarations (N));
4638 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4640 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4641 if Present (SPARK_Pragma (Spec_Id)) then
4642 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4643 and then
4644 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4645 then
4646 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4647 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4648 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4649 Error_Msg_NE
4650 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4651 end if;
4653 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4654 null;
4656 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4657 -- as specified in source code, or because SPARK_Mode On is ignored
4658 -- in an instance where the context is SPARK_Mode Off/Auto.
4660 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4661 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4662 then
4663 null;
4665 else
4666 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4667 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4668 Error_Msg_Sloc := Sloc (Spec_Id);
4669 Error_Msg_NE
4670 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4671 end if;
4672 end if;
4674 -- Check completion, and analyze the statements
4676 Check_Completion;
4677 Inspect_Deferred_Constant_Completion (Declarations (N));
4678 Analyze (Handled_Statement_Sequence (N));
4680 -- Add the generated minimum accessibility objects to the subprogram
4681 -- body's list of declarations after analysis of the statements and
4682 -- contracts.
4684 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4685 if Present (Declarations (Body_Nod)) then
4686 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4687 else
4688 Set_Declarations
4689 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4690 end if;
4691 end loop;
4693 -- Deal with end of scope processing for the body
4695 Process_End_Label
4696 (Handled_Statement_Sequence (N), 't', Current_Scope);
4697 Update_Use_Clause_Chain;
4698 End_Scope;
4700 -- If we are compiling an entry wrapper, remove the enclosing
4701 -- synchronized object from the stack.
4703 if Is_Entry_Wrapper (Body_Id) then
4704 End_Scope;
4705 end if;
4707 Check_Subprogram_Order (N);
4708 Set_Analyzed (Body_Id);
4710 -- If we have a separate spec, then the analysis of the declarations
4711 -- caused the entities in the body to be chained to the spec id, but
4712 -- we want them chained to the body id. Only the formal parameters
4713 -- end up chained to the spec id in this case.
4715 if Present (Spec_Id) then
4717 -- We must conform to the categorization of our spec
4719 Validate_Categorization_Dependency (N, Spec_Id);
4721 -- And if this is a child unit, the parent units must conform
4723 if Is_Child_Unit (Spec_Id) then
4724 Validate_Categorization_Dependency
4725 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4726 end if;
4728 -- Here is where we move entities from the spec to the body
4730 -- Case where there are entities that stay with the spec
4732 if Present (Last_Real_Spec_Entity) then
4734 -- No body entities (happens when the only real spec entities come
4735 -- from precondition and postcondition pragmas).
4737 if No (Last_Entity (Body_Id)) then
4738 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4740 -- Body entities present (formals), so chain stuff past them
4742 else
4743 Link_Entities
4744 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4745 end if;
4747 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4748 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4749 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4751 -- Case where there are no spec entities, in this case there can be
4752 -- no body entities either, so just move everything.
4754 -- If the body is generated for an expression function, it may have
4755 -- been preanalyzed already, if 'access was applied to it.
4757 else
4758 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4759 N_Expression_Function
4760 then
4761 pragma Assert (No (Last_Entity (Body_Id)));
4762 null;
4763 end if;
4765 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4766 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4767 Set_First_Entity (Spec_Id, Empty);
4768 Set_Last_Entity (Spec_Id, Empty);
4769 end if;
4771 -- Otherwise the body does not complete a previous declaration. Check
4772 -- the categorization of the body against the units it withs.
4774 else
4775 Validate_Categorization_Dependency (N, Body_Id);
4776 end if;
4778 Check_Missing_Return;
4780 -- Now we are going to check for variables that are never modified in
4781 -- the body of the procedure. But first we deal with a special case
4782 -- where we want to modify this check. If the body of the subprogram
4783 -- starts with a raise statement or its equivalent, or if the body
4784 -- consists entirely of a null statement, then it is pretty obvious that
4785 -- it is OK to not reference the parameters. For example, this might be
4786 -- the following common idiom for a stubbed function: statement of the
4787 -- procedure raises an exception. In particular this deals with the
4788 -- common idiom of a stubbed function, which appears something like:
4790 -- function F (A : Integer) return Some_Type;
4791 -- X : Some_Type;
4792 -- begin
4793 -- raise Program_Error;
4794 -- return X;
4795 -- end F;
4797 -- Here the purpose of X is simply to satisfy the annoying requirement
4798 -- in Ada that there be at least one return, and we certainly do not
4799 -- want to go posting warnings on X that it is not initialized. On
4800 -- the other hand, if X is entirely unreferenced that should still
4801 -- get a warning.
4803 -- What we do is to detect these cases, and if we find them, flag the
4804 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4805 -- suppress unwanted warnings. For the case of the function stub above
4806 -- we have a special test to set X as apparently assigned to suppress
4807 -- the warning.
4809 declare
4810 Stm : Node_Id := First (Statements (Handled_Statement_Sequence (N)));
4811 begin
4812 -- Skip call markers installed by the ABE mechanism, labels, and
4813 -- Push_xxx_Error_Label to find the first real statement.
4815 while Nkind (Stm) in N_Call_Marker | N_Label | N_Push_xxx_Label loop
4816 Next (Stm);
4817 end loop;
4819 -- Do the test on the original statement before expansion
4821 declare
4822 Ostm : constant Node_Id := Original_Node (Stm);
4824 begin
4825 -- If explicit raise statement, turn on flag
4827 if Nkind (Ostm) = N_Raise_Statement then
4828 Set_Trivial_Subprogram (Stm);
4830 -- If null statement, and no following statements, turn on flag
4832 elsif Nkind (Stm) = N_Null_Statement
4833 and then Comes_From_Source (Stm)
4834 and then No (Next (Stm))
4835 then
4836 Set_Trivial_Subprogram (Stm);
4838 -- Check for explicit call cases which likely raise an exception
4840 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4841 if Is_Entity_Name (Name (Ostm)) then
4842 declare
4843 Ent : constant Entity_Id := Entity (Name (Ostm));
4845 begin
4846 -- If the procedure is marked No_Return, then likely it
4847 -- raises an exception, but in any case it is not coming
4848 -- back here, so turn on the flag.
4850 if Present (Ent)
4851 and then Ekind (Ent) = E_Procedure
4852 and then No_Return (Ent)
4853 then
4854 Set_Trivial_Subprogram (Stm);
4855 end if;
4856 end;
4857 end if;
4858 end if;
4859 end;
4860 end;
4862 -- Check if a Body_To_Inline was created, but the subprogram has
4863 -- references to object renamings which will be replaced by the special
4864 -- SPARK expansion into nodes of a different kind, which is not expected
4865 -- by the inlining mechanism. In that case, the Body_To_Inline is
4866 -- deleted prior to being analyzed. This check needs to take place
4867 -- after analysis of the subprogram body.
4869 if GNATprove_Mode
4870 and then Present (Spec_Id)
4871 and then
4872 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4873 and then Present (Body_To_Inline (Unit_Declaration_Node (Spec_Id)))
4874 then
4875 Check_Object_Renaming_In_GNATprove_Mode (Spec_Id);
4876 end if;
4878 -- Check for variables that are never modified
4880 declare
4881 F1 : Entity_Id;
4882 F2 : Entity_Id;
4884 begin
4885 -- If there is a separate spec, then transfer Never_Set_In_Source
4886 -- flags from out parameters to the corresponding entities in the
4887 -- body. The reason we do that is we want to post error flags on
4888 -- the body entities, not the spec entities.
4890 if Present (Spec_Id) then
4891 F1 := First_Formal (Spec_Id);
4892 while Present (F1) loop
4893 if Ekind (F1) = E_Out_Parameter then
4894 F2 := First_Formal (Body_Id);
4895 while Present (F2) loop
4896 exit when Chars (F1) = Chars (F2);
4897 Next_Formal (F2);
4898 end loop;
4900 if Present (F2) then
4901 Set_Never_Set_In_Source (F2, Never_Set_In_Source (F1));
4902 end if;
4903 end if;
4905 Next_Formal (F1);
4906 end loop;
4907 end if;
4909 -- Check references of the subprogram spec when we are dealing with
4910 -- an expression function due to it having a generated body.
4912 if Present (Spec_Id)
4913 and then Is_Expression_Function (Spec_Id)
4914 then
4915 Check_References (Spec_Id);
4917 -- Skip the check for subprograms generated for protected subprograms
4918 -- because it is also done for the protected subprograms themselves.
4920 elsif Present (Spec_Id)
4921 and then Present (Protected_Subprogram (Spec_Id))
4922 then
4923 null;
4925 -- Otherwise, we simply check the formals of the subprogram body.
4927 else
4928 Check_References (Body_Id);
4929 end if;
4930 end;
4932 -- Check for nested subprogram, and mark outer level subprogram if so
4934 declare
4935 Ent : Entity_Id;
4937 begin
4938 if Present (Spec_Id) then
4939 Ent := Spec_Id;
4940 else
4941 Ent := Body_Id;
4942 end if;
4944 loop
4945 Ent := Enclosing_Subprogram (Ent);
4946 exit when No (Ent) or else Is_Subprogram (Ent);
4947 end loop;
4949 if Present (Ent) then
4950 Set_Has_Nested_Subprogram (Ent);
4951 end if;
4952 end;
4954 -- Restore the limited views in the spec, if any, to let the back end
4955 -- process it without running into circularities.
4957 if Present (Exch_Views) then
4958 Restore_Limited_Views (Exch_Views);
4959 end if;
4961 if Present (Mask_Types) then
4962 Unmask_Unfrozen_Types (Mask_Types);
4963 end if;
4965 if Present (Desig_View) then
4966 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
4967 end if;
4969 <<Leave>>
4970 if Present (Ignored_Ghost_Region) then
4971 Expander_Active := Saved_EA;
4972 end if;
4974 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4975 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4976 end Analyze_Subprogram_Body_Helper;
4978 ------------------------------------
4979 -- Analyze_Subprogram_Declaration --
4980 ------------------------------------
4982 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4983 Scop : constant Entity_Id := Current_Scope;
4984 Designator : Entity_Id;
4986 Is_Completion : Boolean;
4987 -- Indicates whether a null procedure declaration is a completion
4989 begin
4990 -- Null procedures are not allowed in SPARK
4992 if Nkind (Specification (N)) = N_Procedure_Specification
4993 and then Null_Present (Specification (N))
4994 then
4995 -- Null procedures are allowed in protected types, following the
4996 -- recent AI12-0147.
4998 if Is_Protected_Type (Current_Scope)
4999 and then Ada_Version < Ada_2012
5000 then
5001 Error_Msg_N ("protected operation cannot be a null procedure", N);
5002 end if;
5004 Analyze_Null_Procedure (N, Is_Completion);
5006 -- The null procedure acts as a body, nothing further is needed
5008 if Is_Completion then
5009 return;
5010 end if;
5011 end if;
5013 Designator := Analyze_Subprogram_Specification (Specification (N));
5015 -- A reference may already have been generated for the unit name, in
5016 -- which case the following call is redundant. However it is needed for
5017 -- declarations that are the rewriting of an expression function.
5019 Generate_Definition (Designator);
5021 -- Set the SPARK mode from the current context (may be overwritten later
5022 -- with explicit pragma). This is not done for entry barrier functions
5023 -- because they are generated outside the protected type and should not
5024 -- carry the mode of the enclosing context.
5026 if Nkind (N) = N_Subprogram_Declaration
5027 and then Is_Entry_Barrier_Function (N)
5028 then
5029 null;
5031 else
5032 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5033 Set_SPARK_Pragma_Inherited (Designator);
5034 end if;
5036 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5037 -- the body of this subprogram is instantiated or inlined later and out
5038 -- of context. The body uses this attribute to restore the value of the
5039 -- global flag.
5041 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5042 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5043 end if;
5045 -- Preserve relevant elaboration-related attributes of the context which
5046 -- are no longer available or very expensive to recompute once analysis,
5047 -- resolution, and expansion are over.
5049 Mark_Elaboration_Attributes
5050 (N_Id => Designator,
5051 Checks => True,
5052 Warnings => True);
5054 if Debug_Flag_C then
5055 Write_Str ("==> subprogram spec ");
5056 Write_Name (Chars (Designator));
5057 Write_Str (" from ");
5058 Write_Location (Sloc (N));
5059 Write_Eol;
5060 Indent;
5061 end if;
5063 Validate_RCI_Subprogram_Declaration (N);
5064 New_Overloaded_Entity (Designator);
5065 Check_Delayed_Subprogram (Designator);
5067 -- If the type of the first formal of the current subprogram is a non-
5068 -- generic tagged private type, mark the subprogram as being a private
5069 -- primitive. Ditto if this is a function with controlling result, and
5070 -- the return type is currently private. In both cases, the type of the
5071 -- controlling argument or result must be in the current scope for the
5072 -- operation to be primitive.
5074 if Has_Controlling_Result (Designator)
5075 and then Is_Private_Type (Etype (Designator))
5076 and then Scope (Etype (Designator)) = Current_Scope
5077 and then not Is_Generic_Actual_Type (Etype (Designator))
5078 then
5079 Set_Is_Private_Primitive (Designator);
5081 elsif Present (First_Formal (Designator)) then
5082 declare
5083 Formal_Typ : constant Entity_Id :=
5084 Etype (First_Formal (Designator));
5085 begin
5086 Set_Is_Private_Primitive (Designator,
5087 Is_Tagged_Type (Formal_Typ)
5088 and then Scope (Formal_Typ) = Current_Scope
5089 and then Is_Private_Type (Formal_Typ)
5090 and then not Is_Generic_Actual_Type (Formal_Typ));
5091 end;
5092 end if;
5094 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5095 -- or null.
5097 if Ada_Version >= Ada_2005
5098 and then Comes_From_Source (N)
5099 and then Is_Dispatching_Operation (Designator)
5100 then
5101 declare
5102 E : Entity_Id;
5103 Etyp : Entity_Id;
5105 begin
5106 if Has_Controlling_Result (Designator) then
5107 Etyp := Etype (Designator);
5109 else
5110 E := First_Entity (Designator);
5111 while Present (E)
5112 and then Is_Formal (E)
5113 and then not Is_Controlling_Formal (E)
5114 loop
5115 Next_Entity (E);
5116 end loop;
5118 Etyp := Etype (E);
5119 end if;
5121 if Is_Access_Type (Etyp) then
5122 Etyp := Directly_Designated_Type (Etyp);
5123 end if;
5125 if Is_Interface (Etyp)
5126 and then not Is_Abstract_Subprogram (Designator)
5127 and then not (Ekind (Designator) = E_Procedure
5128 and then Null_Present (Specification (N)))
5129 then
5130 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5132 -- Specialize error message based on procedures vs. functions,
5133 -- since functions can't be null subprograms.
5135 if Ekind (Designator) = E_Procedure then
5136 Error_Msg_N
5137 ("interface procedure % must be abstract or null", N);
5138 else
5139 Error_Msg_N
5140 ("interface function % must be abstract", N);
5141 end if;
5142 end if;
5143 end;
5144 end if;
5146 -- For a compilation unit, set body required. This flag will only be
5147 -- reset if a valid Import or Interface pragma is processed later on.
5149 if Nkind (Parent (N)) = N_Compilation_Unit then
5150 Set_Body_Required (Parent (N), True);
5152 if Ada_Version >= Ada_2005
5153 and then Nkind (Specification (N)) = N_Procedure_Specification
5154 and then Null_Present (Specification (N))
5155 then
5156 Error_Msg_N
5157 ("null procedure cannot be declared at library level", N);
5158 end if;
5159 end if;
5161 Generate_Reference_To_Formals (Designator);
5162 Check_Eliminated (Designator);
5164 if Debug_Flag_C then
5165 Outdent;
5166 Write_Str ("<== subprogram spec ");
5167 Write_Name (Chars (Designator));
5168 Write_Str (" from ");
5169 Write_Location (Sloc (N));
5170 Write_Eol;
5171 end if;
5173 -- Indicate that this is a protected operation, because it may be used
5174 -- in subsequent declarations within the protected type.
5176 if Is_Protected_Type (Current_Scope) then
5177 Set_Convention (Designator, Convention_Protected);
5178 end if;
5180 List_Inherited_Pre_Post_Aspects (Designator);
5182 -- Process the aspects before establishing the proper categorization in
5183 -- case the subprogram is a compilation unit and one of its aspects is
5184 -- converted into a categorization pragma.
5186 if Has_Aspects (N) then
5187 Analyze_Aspect_Specifications (N, Designator);
5188 end if;
5190 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5191 Set_Categorization_From_Scope (Designator, Scop);
5193 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5194 -- proper categorization of the unit based on its pragmas.
5196 else
5197 Push_Scope (Designator);
5198 Set_Categorization_From_Pragmas (N);
5199 Validate_Categorization_Dependency (N, Designator);
5200 Pop_Scope;
5201 end if;
5202 end Analyze_Subprogram_Declaration;
5204 --------------------------------------
5205 -- Analyze_Subprogram_Specification --
5206 --------------------------------------
5208 -- Reminder: N here really is a subprogram specification (not a subprogram
5209 -- declaration). This procedure is called to analyze the specification in
5210 -- both subprogram bodies and subprogram declarations (specs).
5212 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5213 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5214 -- Determine whether entity E denotes the spec or body of an invariant
5215 -- procedure.
5217 ------------------------------------
5218 -- Is_Invariant_Procedure_Or_Body --
5219 ------------------------------------
5221 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5222 Decl : constant Node_Id := Unit_Declaration_Node (E);
5223 Spec : Entity_Id;
5225 begin
5226 if Nkind (Decl) = N_Subprogram_Body then
5227 Spec := Corresponding_Spec (Decl);
5228 else
5229 Spec := E;
5230 end if;
5232 return
5233 Present (Spec)
5234 and then Ekind (Spec) = E_Procedure
5235 and then (Is_Partial_Invariant_Procedure (Spec)
5236 or else Is_Invariant_Procedure (Spec));
5237 end Is_Invariant_Procedure_Or_Body;
5239 -- Local variables
5241 Designator : constant Entity_Id := Defining_Entity (N);
5242 Formals : constant List_Id := Parameter_Specifications (N);
5244 -- Start of processing for Analyze_Subprogram_Specification
5246 begin
5247 -- Proceed with analysis. Do not emit a cross-reference entry if the
5248 -- specification comes from an expression function, because it may be
5249 -- the completion of a previous declaration. If it is not, the cross-
5250 -- reference entry will be emitted for the new subprogram declaration.
5252 if Nkind (Parent (N)) /= N_Expression_Function then
5253 Generate_Definition (Designator);
5254 end if;
5256 if Nkind (N) = N_Function_Specification then
5257 Mutate_Ekind (Designator, E_Function);
5258 Set_Mechanism (Designator, Default_Mechanism);
5259 else
5260 Mutate_Ekind (Designator, E_Procedure);
5261 Set_Etype (Designator, Standard_Void_Type);
5262 end if;
5264 Set_Is_Not_Self_Hidden (Designator);
5266 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5267 -- those subprograms which could be inlined in GNATprove mode (because
5268 -- Body_To_Inline is non-Empty) but should not be inlined.
5270 if GNATprove_Mode then
5271 Set_Is_Inlined_Always (Designator);
5272 end if;
5274 -- Introduce new scope for analysis of the formals and the return type
5276 Set_Scope (Designator, Current_Scope);
5278 if Present (Formals) then
5279 Push_Scope (Designator);
5280 Process_Formals (Formals, N);
5282 -- Check dimensions in N for formals with default expression
5284 Analyze_Dimension_Formals (N, Formals);
5286 -- Ada 2005 (AI-345): If this is an overriding operation of an
5287 -- inherited interface operation, and the controlling type is
5288 -- a synchronized type, replace the type with its corresponding
5289 -- record, to match the proper signature of an overriding operation.
5290 -- Same processing for an access parameter whose designated type is
5291 -- derived from a synchronized interface.
5293 -- This modification is not done for invariant procedures because
5294 -- the corresponding record may not necessarely be visible when the
5295 -- concurrent type acts as the full view of a private type.
5297 -- package Pack is
5298 -- type Prot is private with Type_Invariant => ...;
5299 -- procedure ConcInvariant (Obj : Prot);
5300 -- private
5301 -- protected type Prot is ...;
5302 -- type Concurrent_Record_Prot is record ...;
5303 -- procedure ConcInvariant (Obj : Prot) is
5304 -- ...
5305 -- end ConcInvariant;
5306 -- end Pack;
5308 -- In the example above, both the spec and body of the invariant
5309 -- procedure must utilize the private type as the controlling type.
5311 if Ada_Version >= Ada_2005
5312 and then not Is_Invariant_Procedure_Or_Body (Designator)
5313 then
5314 declare
5315 Formal : Entity_Id;
5316 Formal_Typ : Entity_Id;
5317 Rec_Typ : Entity_Id;
5318 Desig_Typ : Entity_Id;
5320 begin
5321 Formal := First_Formal (Designator);
5322 while Present (Formal) loop
5323 Formal_Typ := Etype (Formal);
5325 if Is_Concurrent_Type (Formal_Typ)
5326 and then Present (Corresponding_Record_Type (Formal_Typ))
5327 then
5328 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5330 if Present (Interfaces (Rec_Typ)) then
5331 Set_Etype (Formal, Rec_Typ);
5332 end if;
5334 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5335 Desig_Typ := Designated_Type (Formal_Typ);
5337 if Is_Concurrent_Type (Desig_Typ)
5338 and then Present (Corresponding_Record_Type (Desig_Typ))
5339 then
5340 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5342 if Present (Interfaces (Rec_Typ)) then
5343 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5344 end if;
5345 end if;
5346 end if;
5348 Next_Formal (Formal);
5349 end loop;
5350 end;
5351 end if;
5353 End_Scope;
5355 -- The subprogram scope is pushed and popped around the processing of
5356 -- the return type for consistency with call above to Process_Formals
5357 -- (which itself can call Analyze_Return_Type), and to ensure that any
5358 -- itype created for the return type will be associated with the proper
5359 -- scope.
5361 elsif Nkind (N) = N_Function_Specification then
5362 Push_Scope (Designator);
5363 Analyze_Return_Type (N);
5364 End_Scope;
5365 end if;
5367 -- Function case
5369 if Nkind (N) = N_Function_Specification then
5371 -- Deal with operator symbol case
5373 if Nkind (Designator) = N_Defining_Operator_Symbol then
5374 Valid_Operator_Definition (Designator);
5375 end if;
5377 May_Need_Actuals (Designator);
5379 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5380 -- the subprogram is abstract also. This does not apply to renaming
5381 -- declarations, where abstractness is inherited, and to subprogram
5382 -- bodies generated for stream operations, which become renamings as
5383 -- bodies. We also skip the check for thunks.
5385 -- In case of primitives associated with abstract interface types
5386 -- the check is applied later (see Analyze_Subprogram_Declaration).
5388 if Nkind (Original_Node (Parent (N))) not in
5389 N_Abstract_Subprogram_Declaration |
5390 N_Formal_Abstract_Subprogram_Declaration |
5391 N_Subprogram_Renaming_Declaration
5392 and then not Is_Thunk (Designator)
5393 then
5394 if Is_Abstract_Type (Etype (Designator)) then
5395 Error_Msg_N
5396 ("function that returns abstract type must be abstract", N);
5398 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5399 -- access result whose designated type is abstract.
5401 elsif Ada_Version >= Ada_2012
5402 and then Nkind (Result_Definition (N)) = N_Access_Definition
5403 and then
5404 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5405 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5406 then
5407 Error_Msg_N
5408 ("function whose access result designates abstract type "
5409 & "must be abstract", N);
5410 end if;
5411 end if;
5412 end if;
5414 return Designator;
5415 end Analyze_Subprogram_Specification;
5417 -----------------------
5418 -- Check_Conformance --
5419 -----------------------
5421 procedure Check_Conformance
5422 (New_Id : Entity_Id;
5423 Old_Id : Entity_Id;
5424 Ctype : Conformance_Type;
5425 Errmsg : Boolean;
5426 Conforms : out Boolean;
5427 Err_Loc : Node_Id := Empty;
5428 Get_Inst : Boolean := False;
5429 Skip_Controlling_Formals : Boolean := False)
5431 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5432 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5433 -- If Errmsg is True, then processing continues to post an error message
5434 -- for conformance error on given node. Two messages are output. The
5435 -- first message points to the previous declaration with a general "no
5436 -- conformance" message. The second is the detailed reason, supplied as
5437 -- Msg. The parameter N provide information for a possible & insertion
5438 -- in the message, and also provides the location for posting the
5439 -- message in the absence of a specified Err_Loc location.
5441 function Conventions_Match (Id1, Id2 : Entity_Id) return Boolean;
5442 -- True if the conventions of entities Id1 and Id2 match.
5444 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean;
5445 -- True if the null exclusions of two formals of anonymous access type
5446 -- match.
5448 function Subprogram_Subtypes_Have_Same_Declaration
5449 (Subp : Entity_Id;
5450 Decl_Subtype : Entity_Id;
5451 Body_Subtype : Entity_Id) return Boolean;
5452 -- Checks whether corresponding subtypes named within a subprogram
5453 -- declaration and body originate from the same declaration, and returns
5454 -- True when they do. In the case of anonymous access-to-object types,
5455 -- checks the designated types. Also returns True when GNAT_Mode is
5456 -- enabled, or when the subprogram is marked Is_Internal or occurs
5457 -- within a generic instantiation or internal unit (GNAT library unit).
5459 -----------------------
5460 -- Conformance_Error --
5461 -----------------------
5463 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5464 Enode : Node_Id;
5466 begin
5467 Conforms := False;
5469 if Errmsg then
5470 if No (Err_Loc) then
5471 Enode := N;
5472 else
5473 Enode := Err_Loc;
5474 end if;
5476 Error_Msg_Sloc := Sloc (Old_Id);
5478 case Ctype is
5479 when Type_Conformant =>
5480 Error_Msg_N -- CODEFIX
5481 ("not type conformant with declaration#!", Enode);
5483 when Mode_Conformant =>
5484 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5485 Error_Msg_N
5486 ("not mode conformant with operation inherited#!",
5487 Enode);
5488 else
5489 Error_Msg_N
5490 ("not mode conformant with declaration#!", Enode);
5491 end if;
5493 when Subtype_Conformant =>
5494 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5495 Error_Msg_N
5496 ("not subtype conformant with operation inherited#!",
5497 Enode);
5498 else
5499 Error_Msg_N
5500 ("not subtype conformant with declaration#!", Enode);
5501 end if;
5503 when Fully_Conformant =>
5504 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5505 Error_Msg_N -- CODEFIX
5506 ("not fully conformant with operation inherited#!",
5507 Enode);
5508 else
5509 Error_Msg_N -- CODEFIX
5510 ("not fully conformant with declaration#!", Enode);
5511 end if;
5512 end case;
5514 Error_Msg_NE (Msg, Enode, N);
5515 end if;
5516 end Conformance_Error;
5518 -----------------------
5519 -- Conventions_Match --
5520 -----------------------
5522 function Conventions_Match
5523 (Id1 : Entity_Id;
5524 Id2 : Entity_Id) return Boolean
5526 begin
5527 -- Ignore the conventions of anonymous access-to-subprogram types
5528 -- and subprogram types because these are internally generated and
5529 -- the only way these may receive a convention is if they inherit
5530 -- the convention of a related subprogram.
5532 if Ekind (Id1) in E_Anonymous_Access_Subprogram_Type
5533 | E_Subprogram_Type
5534 or else
5535 Ekind (Id2) in E_Anonymous_Access_Subprogram_Type
5536 | E_Subprogram_Type
5537 then
5538 return True;
5540 -- Otherwise compare the conventions directly
5542 else
5543 return Convention (Id1) = Convention (Id2);
5544 end if;
5545 end Conventions_Match;
5547 ---------------------------
5548 -- Null_Exclusions_Match --
5549 ---------------------------
5551 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean is
5552 begin
5553 if not Is_Anonymous_Access_Type (Etype (F1))
5554 or else not Is_Anonymous_Access_Type (Etype (F2))
5555 then
5556 return True;
5557 end if;
5559 -- AI12-0289-1: Case of controlling access parameter; False if the
5560 -- partial view is untagged, the full view is tagged, and no explicit
5561 -- "not null". Note that at this point, we're processing the package
5562 -- body, so private/full types have been swapped. The Sloc test below
5563 -- is to detect the (legal) case where F1 comes after the full type
5564 -- declaration. This part is disabled pre-2005, because "not null" is
5565 -- not allowed on those language versions.
5567 if Ada_Version >= Ada_2005
5568 and then Is_Controlling_Formal (F1)
5569 and then not Null_Exclusion_Present (Parent (F1))
5570 and then not Null_Exclusion_Present (Parent (F2))
5571 then
5572 declare
5573 D : constant Entity_Id := Directly_Designated_Type (Etype (F1));
5574 Partial_View_Of_Desig : constant Entity_Id :=
5575 Incomplete_Or_Partial_View (D);
5576 begin
5577 return No (Partial_View_Of_Desig)
5578 or else Is_Tagged_Type (Partial_View_Of_Desig)
5579 or else Sloc (D) < Sloc (F1);
5580 end;
5582 -- Not a controlling parameter, or one or both views have an explicit
5583 -- "not null".
5585 else
5586 return Null_Exclusion_Present (Parent (F1)) =
5587 Null_Exclusion_Present (Parent (F2));
5588 end if;
5589 end Null_Exclusions_Match;
5591 function Subprogram_Subtypes_Have_Same_Declaration
5592 (Subp : Entity_Id;
5593 Decl_Subtype : Entity_Id;
5594 Body_Subtype : Entity_Id) return Boolean
5597 function Nonlimited_View_Of_Subtype
5598 (Subt : Entity_Id) return Entity_Id;
5599 -- Returns the nonlimited view of a type or subtype that is an
5600 -- incomplete or class-wide type that comes from a limited view of
5601 -- a package (From_Limited_With is True for the entity), or the
5602 -- full view when the subtype is an incomplete type. Otherwise
5603 -- returns the entity passed in.
5605 function Nonlimited_View_Of_Subtype
5606 (Subt : Entity_Id) return Entity_Id
5608 Subt_Temp : Entity_Id := Subt;
5609 begin
5610 if Ekind (Subt) in Incomplete_Kind | E_Class_Wide_Type
5611 and then From_Limited_With (Subt)
5612 then
5613 Subt_Temp := Non_Limited_View (Subt);
5614 end if;
5616 -- If the subtype is incomplete, return full view if present
5617 -- (and accounts for the case where a type from a limited view
5618 -- is itself an incomplete type).
5620 if Ekind (Subt_Temp) in Incomplete_Kind
5621 and then Present (Full_View (Subt_Temp))
5622 then
5623 Subt_Temp := Full_View (Subt_Temp);
5624 end if;
5626 return Subt_Temp;
5627 end Nonlimited_View_Of_Subtype;
5629 -- Start of processing for Subprogram_Subtypes_Have_Same_Declaration
5631 begin
5632 if not In_Instance
5633 and then not In_Internal_Unit (Subp)
5634 and then not Is_Internal (Subp)
5635 and then not GNAT_Mode
5636 and then
5637 Ekind (Etype (Decl_Subtype)) not in Access_Subprogram_Kind
5638 then
5639 if Ekind (Etype (Decl_Subtype)) = E_Anonymous_Access_Type then
5640 if Nonlimited_View_Of_Subtype (Designated_Type (Decl_Subtype))
5641 /= Nonlimited_View_Of_Subtype (Designated_Type (Body_Subtype))
5642 then
5643 return False;
5644 end if;
5646 elsif Nonlimited_View_Of_Subtype (Decl_Subtype)
5647 /= Nonlimited_View_Of_Subtype (Body_Subtype)
5648 then
5649 -- Avoid returning False (and a false-positive warning) for
5650 -- the case of "not null" itypes, which will appear to be
5651 -- different subtypes even when the subtype_marks denote
5652 -- the same subtype.
5654 if Ekind (Decl_Subtype) = E_Access_Subtype
5655 and then Ekind (Body_Subtype) = E_Access_Subtype
5656 and then Is_Itype (Body_Subtype)
5657 and then Can_Never_Be_Null (Body_Subtype)
5658 and then Etype (Decl_Subtype) = Etype (Body_Subtype)
5659 then
5660 return True;
5662 else
5663 return False;
5664 end if;
5665 end if;
5666 end if;
5668 return True;
5669 end Subprogram_Subtypes_Have_Same_Declaration;
5671 -- Local Variables
5673 Old_Type : constant Entity_Id := Etype (Old_Id);
5674 New_Type : constant Entity_Id := Etype (New_Id);
5675 Old_Formal : Entity_Id;
5676 New_Formal : Entity_Id;
5677 Old_Formal_Base : Entity_Id;
5678 New_Formal_Base : Entity_Id;
5680 -- Start of processing for Check_Conformance
5682 begin
5683 Conforms := True;
5685 -- We need a special case for operators, since they don't appear
5686 -- explicitly.
5688 if Ctype = Type_Conformant then
5689 if Ekind (New_Id) = E_Operator
5690 and then Operator_Matches_Spec (New_Id, Old_Id)
5691 then
5692 return;
5693 end if;
5694 end if;
5696 -- If both are functions/operators, check return types conform
5698 if Old_Type /= Standard_Void_Type
5699 and then
5700 New_Type /= Standard_Void_Type
5701 then
5702 -- If we are checking interface conformance we omit controlling
5703 -- arguments and result, because we are only checking the conformance
5704 -- of the remaining parameters.
5706 if Has_Controlling_Result (Old_Id)
5707 and then Has_Controlling_Result (New_Id)
5708 and then Skip_Controlling_Formals
5709 then
5710 null;
5712 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5713 if Ctype >= Subtype_Conformant
5714 and then not Predicates_Match (Old_Type, New_Type)
5715 then
5716 Conformance_Error
5717 ("\predicate of return type does not match!", New_Id);
5718 else
5719 Conformance_Error
5720 ("\return type does not match!", New_Id);
5721 end if;
5723 return;
5725 -- If the result subtypes conform and pedantic checks are enabled,
5726 -- check to see whether the subtypes originate from different
5727 -- declarations, and issue a warning when they do.
5729 elsif Ctype = Fully_Conformant
5730 and then Warn_On_Pedantic_Checks
5731 and then not Subprogram_Subtypes_Have_Same_Declaration
5732 (Old_Id, Old_Type, New_Type)
5733 then
5734 Error_Msg_N ("result subtypes conform but come from different "
5735 & "declarations?_p?", New_Id);
5736 end if;
5738 -- Ada 2005 (AI-231): In case of anonymous access types check the
5739 -- null-exclusion and access-to-constant attributes match.
5741 if Ada_Version >= Ada_2005
5742 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5743 and then
5744 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5745 or else Is_Access_Constant (Etype (Old_Type)) /=
5746 Is_Access_Constant (Etype (New_Type)))
5747 then
5748 Conformance_Error ("\return type does not match!", New_Id);
5749 return;
5750 end if;
5752 -- If either is a function/operator and the other isn't, error
5754 elsif Old_Type /= Standard_Void_Type
5755 or else New_Type /= Standard_Void_Type
5756 then
5757 Conformance_Error ("\functions can only match functions!", New_Id);
5758 return;
5759 end if;
5761 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5762 -- If this is a renaming as body, refine error message to indicate that
5763 -- the conflict is with the original declaration. If the entity is not
5764 -- frozen, the conventions don't have to match, the one of the renamed
5765 -- entity is inherited.
5767 if Ctype >= Subtype_Conformant then
5768 if not Conventions_Match (Old_Id, New_Id) then
5769 if not Is_Frozen (New_Id) then
5770 null;
5772 elsif Present (Err_Loc)
5773 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5774 and then Present (Corresponding_Spec (Err_Loc))
5775 then
5776 Error_Msg_Name_1 := Chars (New_Id);
5777 Error_Msg_Name_2 :=
5778 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5779 Conformance_Error ("\prior declaration for% has convention %!");
5780 return;
5782 else
5783 Conformance_Error ("\calling conventions do not match!");
5784 return;
5785 end if;
5786 else
5787 Check_Formal_Subprogram_Conformance
5788 (New_Id, Old_Id, Err_Loc, Errmsg, Conforms);
5790 if not Conforms then
5791 return;
5792 end if;
5793 end if;
5794 end if;
5796 -- Deal with parameters
5798 -- Note: we use the entity information, rather than going directly
5799 -- to the specification in the tree. This is not only simpler, but
5800 -- absolutely necessary for some cases of conformance tests between
5801 -- operators, where the declaration tree simply does not exist.
5803 Old_Formal := First_Formal (Old_Id);
5804 New_Formal := First_Formal (New_Id);
5805 while Present (Old_Formal) and then Present (New_Formal) loop
5806 if Is_Controlling_Formal (Old_Formal)
5807 and then Is_Controlling_Formal (New_Formal)
5808 and then Skip_Controlling_Formals
5809 then
5810 -- The controlling formals will have different types when
5811 -- comparing an interface operation with its match, but both
5812 -- or neither must be access parameters.
5814 if Is_Access_Type (Etype (Old_Formal))
5816 Is_Access_Type (Etype (New_Formal))
5817 then
5818 goto Skip_Controlling_Formal;
5819 else
5820 Conformance_Error
5821 ("\access parameter does not match!", New_Formal);
5822 end if;
5823 end if;
5825 -- Ada 2012: Mode conformance also requires that formal parameters
5826 -- be both aliased, or neither.
5828 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5829 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5830 Conformance_Error
5831 ("\aliased parameter mismatch!", New_Formal);
5832 end if;
5833 end if;
5835 if Ctype = Fully_Conformant then
5837 -- Names must match. Error message is more accurate if we do
5838 -- this before checking that the types of the formals match.
5840 if Chars (Old_Formal) /= Chars (New_Formal) then
5841 Conformance_Error ("\name& does not match!", New_Formal);
5843 -- Set error posted flag on new formal as well to stop
5844 -- junk cascaded messages in some cases.
5846 Set_Error_Posted (New_Formal);
5847 return;
5848 end if;
5850 -- Null exclusion must match
5852 if not Relaxed_RM_Semantics
5853 and then not Null_Exclusions_Match (Old_Formal, New_Formal)
5854 then
5855 Conformance_Error
5856 ("\null exclusion for& does not match", New_Formal);
5858 -- Mark error posted on the new formal to avoid duplicated
5859 -- complaint about types not matching.
5861 Set_Error_Posted (New_Formal);
5862 end if;
5863 end if;
5865 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5866 -- case occurs whenever a subprogram is being renamed and one of its
5867 -- parameters imposes a null exclusion. For example:
5869 -- type T is null record;
5870 -- type Acc_T is access T;
5871 -- subtype Acc_T_Sub is Acc_T;
5873 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5874 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5875 -- renames P;
5877 Old_Formal_Base := Etype (Old_Formal);
5878 New_Formal_Base := Etype (New_Formal);
5880 if Get_Inst then
5881 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5882 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5883 end if;
5885 -- Types must always match. In the visible part of an instance,
5886 -- usual overloading rules for dispatching operations apply, and
5887 -- we check base types (not the actual subtypes).
5889 if In_Instance_Visible_Part
5890 and then Is_Dispatching_Operation (New_Id)
5891 then
5892 if not Conforming_Types
5893 (T1 => Base_Type (Etype (Old_Formal)),
5894 T2 => Base_Type (Etype (New_Formal)),
5895 Ctype => Ctype,
5896 Get_Inst => Get_Inst)
5897 then
5898 Conformance_Error ("\type of & does not match!", New_Formal);
5899 return;
5900 end if;
5902 elsif not Conforming_Types
5903 (T1 => Old_Formal_Base,
5904 T2 => New_Formal_Base,
5905 Ctype => Ctype,
5906 Get_Inst => Get_Inst)
5907 then
5908 -- Don't give error message if old type is Any_Type. This test
5909 -- avoids some cascaded errors, e.g. in case of a bad spec.
5911 if Errmsg and then Old_Formal_Base = Any_Type then
5912 Conforms := False;
5913 else
5914 if Ctype >= Subtype_Conformant
5915 and then
5916 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5917 then
5918 Conformance_Error
5919 ("\predicate of & does not match!", New_Formal);
5920 else
5921 Conformance_Error
5922 ("\type of & does not match!", New_Formal);
5924 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5925 then
5926 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5927 end if;
5928 end if;
5929 end if;
5931 return;
5933 -- If the formals' subtypes conform and pedantic checks are enabled,
5934 -- check to see whether the subtypes originate from different
5935 -- declarations, and issue a warning when they do.
5937 elsif Ctype = Fully_Conformant
5938 and then Warn_On_Pedantic_Checks
5939 and then not Subprogram_Subtypes_Have_Same_Declaration
5940 (Old_Id, Old_Formal_Base, New_Formal_Base)
5941 then
5942 Error_Msg_N ("formal subtypes conform but come from "
5943 & "different declarations?_p?", New_Formal);
5944 end if;
5946 -- For mode conformance, mode must match
5948 if Ctype >= Mode_Conformant then
5949 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5950 if Ekind (New_Id) not in E_Function | E_Procedure
5951 or else not Is_Primitive_Wrapper (New_Id)
5952 then
5953 Conformance_Error ("\mode of & does not match!", New_Formal);
5955 else
5956 declare
5957 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5958 begin
5959 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5960 then
5961 Conforms := False;
5963 if Errmsg then
5964 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5965 end if;
5966 else
5967 Conformance_Error
5968 ("\mode of & does not match!", New_Formal);
5969 end if;
5970 end;
5971 end if;
5973 return;
5975 elsif Is_Access_Type (Old_Formal_Base)
5976 and then Is_Access_Type (New_Formal_Base)
5977 and then Is_Access_Constant (Old_Formal_Base) /=
5978 Is_Access_Constant (New_Formal_Base)
5979 then
5980 Conformance_Error
5981 ("\constant modifier does not match!", New_Formal);
5982 return;
5983 end if;
5984 end if;
5986 if Ctype >= Subtype_Conformant then
5988 -- Ada 2005 (AI-231): In case of anonymous access types check
5989 -- the null-exclusion and access-to-constant attributes must
5990 -- match. For null exclusion, we test the types rather than the
5991 -- formals themselves, since the attribute is only set reliably
5992 -- on the formals in the Ada 95 case, and we exclude the case
5993 -- where Old_Formal is marked as controlling, to avoid errors
5994 -- when matching completing bodies with dispatching declarations
5995 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5997 if Ada_Version >= Ada_2005
5998 and then Is_Anonymous_Access_Type (Etype (Old_Formal))
5999 and then Is_Anonymous_Access_Type (Etype (New_Formal))
6000 and then
6001 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6002 Can_Never_Be_Null (Etype (New_Formal))
6003 and then
6004 not Is_Controlling_Formal (Old_Formal))
6005 or else
6006 Is_Access_Constant (Etype (Old_Formal)) /=
6007 Is_Access_Constant (Etype (New_Formal)))
6009 -- Do not complain if error already posted on New_Formal. This
6010 -- avoids some redundant error messages.
6012 and then not Error_Posted (New_Formal)
6014 -- It is allowed to omit the null-exclusion in case of stream
6015 -- attribute subprograms. We recognize stream subprograms
6016 -- through their TSS-generated suffix.
6018 and then Get_TSS_Name (New_Id) not in TSS_Stream_Read
6019 | TSS_Stream_Write
6020 | TSS_Stream_Input
6021 | TSS_Stream_Output
6022 then
6023 -- Here we have a definite conformance error. It is worth
6024 -- special casing the error message for the case of a
6025 -- controlling formal (which excludes null).
6027 if Is_Controlling_Formal (New_Formal) then
6028 Error_Msg_Node_2 := Scope (New_Formal);
6029 Conformance_Error
6030 ("\controlling formal & of & excludes null, "
6031 & "declaration must exclude null as well",
6032 New_Formal);
6034 -- Normal case (couldn't we give more detail here???)
6036 else
6037 Conformance_Error
6038 ("\type of & does not match!", New_Formal);
6039 end if;
6041 return;
6042 end if;
6043 end if;
6045 -- Full conformance checks
6047 if Ctype = Fully_Conformant then
6049 -- We have checked already that names match
6051 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6053 -- Check default expressions for in parameters
6055 declare
6056 NewD : constant Boolean :=
6057 Present (Default_Value (New_Formal));
6058 OldD : constant Boolean :=
6059 Present (Default_Value (Old_Formal));
6060 begin
6061 if NewD or OldD then
6063 -- The old default value has been analyzed because the
6064 -- current full declaration will have frozen everything
6065 -- before. The new default value has not been analyzed,
6066 -- so analyze it now before we check for conformance.
6068 if NewD then
6069 Push_Scope (New_Id);
6070 Preanalyze_Spec_Expression
6071 (Default_Value (New_Formal), Etype (New_Formal));
6072 End_Scope;
6073 end if;
6075 if not (NewD and OldD)
6076 or else not Fully_Conformant_Expressions
6077 (Default_Value (Old_Formal),
6078 Default_Value (New_Formal))
6079 then
6080 Conformance_Error
6081 ("\default expression for & does not match!",
6082 New_Formal);
6083 return;
6084 end if;
6085 end if;
6086 end;
6087 end if;
6088 end if;
6090 -- A couple of special checks for Ada 83 mode. These checks are
6091 -- skipped if either entity is an operator in package Standard,
6092 -- or if either old or new instance is not from the source program.
6094 if Ada_Version = Ada_83
6095 and then Sloc (Old_Id) > Standard_Location
6096 and then Sloc (New_Id) > Standard_Location
6097 and then Comes_From_Source (Old_Id)
6098 and then Comes_From_Source (New_Id)
6099 then
6100 declare
6101 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6102 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6104 begin
6105 -- Explicit IN must be present or absent in both cases. This
6106 -- test is required only in the full conformance case.
6108 if In_Present (Old_Param) /= In_Present (New_Param)
6109 and then Ctype = Fully_Conformant
6110 then
6111 Conformance_Error
6112 ("\(Ada 83) IN must appear in both declarations",
6113 New_Formal);
6114 return;
6115 end if;
6117 -- Grouping (use of comma in param lists) must be the same
6118 -- This is where we catch a misconformance like:
6120 -- A, B : Integer
6121 -- A : Integer; B : Integer
6123 -- which are represented identically in the tree except
6124 -- for the setting of the flags More_Ids and Prev_Ids.
6126 if More_Ids (Old_Param) /= More_Ids (New_Param)
6127 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6128 then
6129 Conformance_Error
6130 ("\grouping of & does not match!", New_Formal);
6131 return;
6132 end if;
6133 end;
6134 end if;
6136 -- This label is required when skipping controlling formals
6138 <<Skip_Controlling_Formal>>
6140 Next_Formal (Old_Formal);
6141 Next_Formal (New_Formal);
6142 end loop;
6144 if Present (Old_Formal) then
6145 Conformance_Error ("\too few parameters!");
6146 return;
6148 elsif Present (New_Formal) then
6149 Conformance_Error ("\too many parameters!", New_Formal);
6150 return;
6151 end if;
6152 end Check_Conformance;
6154 -----------------------
6155 -- Check_Conventions --
6156 -----------------------
6158 procedure Check_Conventions (Typ : Entity_Id) is
6159 Ifaces_List : Elist_Id;
6161 procedure Check_Convention (Op : Entity_Id);
6162 -- Verify that the convention of inherited dispatching operation Op is
6163 -- consistent among all subprograms it overrides. In order to minimize
6164 -- the search, Search_From is utilized to designate a specific point in
6165 -- the list rather than iterating over the whole list once more.
6167 ----------------------
6168 -- Check_Convention --
6169 ----------------------
6171 procedure Check_Convention (Op : Entity_Id) is
6172 Op_Conv : constant Convention_Id := Convention (Op);
6173 Iface_Conv : Convention_Id;
6174 Iface_Elmt : Elmt_Id;
6175 Iface_Prim_Elmt : Elmt_Id;
6176 Iface_Prim : Entity_Id;
6178 begin
6179 Iface_Elmt := First_Elmt (Ifaces_List);
6180 while Present (Iface_Elmt) loop
6181 Iface_Prim_Elmt :=
6182 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6183 while Present (Iface_Prim_Elmt) loop
6184 Iface_Prim := Node (Iface_Prim_Elmt);
6185 Iface_Conv := Convention (Iface_Prim);
6187 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6188 and then Iface_Conv /= Op_Conv
6189 then
6190 Error_Msg_N
6191 ("inconsistent conventions in primitive operations", Typ);
6193 Error_Msg_Name_1 := Chars (Op);
6194 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6195 Error_Msg_Sloc := Sloc (Op);
6197 if Comes_From_Source (Op) or else No (Alias (Op)) then
6198 if No (Overridden_Operation (Op)) then
6199 Error_Msg_N ("\\primitive % defined #", Typ);
6200 else
6201 Error_Msg_N
6202 ("\\overriding operation % with "
6203 & "convention % defined #", Typ);
6204 end if;
6206 else pragma Assert (Present (Alias (Op)));
6207 Error_Msg_Sloc := Sloc (Alias (Op));
6208 Error_Msg_N ("\\inherited operation % with "
6209 & "convention % defined #", Typ);
6210 end if;
6212 Error_Msg_Name_1 := Chars (Op);
6213 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6214 Error_Msg_Sloc := Sloc (Iface_Prim);
6215 Error_Msg_N ("\\overridden operation % with "
6216 & "convention % defined #", Typ);
6218 -- Avoid cascading errors
6220 return;
6221 end if;
6223 Next_Elmt (Iface_Prim_Elmt);
6224 end loop;
6226 Next_Elmt (Iface_Elmt);
6227 end loop;
6228 end Check_Convention;
6230 -- Local variables
6232 Prim_Op : Entity_Id;
6233 Prim_Op_Elmt : Elmt_Id;
6235 -- Start of processing for Check_Conventions
6237 begin
6238 if not Has_Interfaces (Typ) then
6239 return;
6240 end if;
6242 Collect_Interfaces (Typ, Ifaces_List);
6244 -- The algorithm checks every overriding dispatching operation against
6245 -- all the corresponding overridden dispatching operations, detecting
6246 -- differences in conventions.
6248 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6249 while Present (Prim_Op_Elmt) loop
6250 Prim_Op := Node (Prim_Op_Elmt);
6252 -- A small optimization: skip the predefined dispatching operations
6253 -- since they always have the same convention.
6255 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6256 Check_Convention (Prim_Op);
6257 end if;
6259 Next_Elmt (Prim_Op_Elmt);
6260 end loop;
6261 end Check_Conventions;
6263 ------------------------------
6264 -- Check_Delayed_Subprogram --
6265 ------------------------------
6267 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6268 procedure Possible_Freeze (T : Entity_Id);
6269 -- T is the type of either a formal parameter or of the return type. If
6270 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6271 -- itself must be delayed.
6273 ---------------------
6274 -- Possible_Freeze --
6275 ---------------------
6277 procedure Possible_Freeze (T : Entity_Id) is
6278 Scop : constant Entity_Id := Scope (Designator);
6280 begin
6281 -- If the subprogram appears within a package instance (which may be
6282 -- the wrapper package of a subprogram instance) the freeze node for
6283 -- that package will freeze the subprogram at the proper place, so
6284 -- do not emit a freeze node for the subprogram, given that it may
6285 -- appear in the wrong scope.
6287 if Ekind (Scop) = E_Package
6288 and then not Comes_From_Source (Scop)
6289 and then Is_Generic_Instance (Scop)
6290 then
6291 null;
6293 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6294 Set_Has_Delayed_Freeze (Designator);
6296 elsif Is_Access_Type (T)
6297 and then Has_Delayed_Freeze (Designated_Type (T))
6298 and then not Is_Frozen (Designated_Type (T))
6299 then
6300 Set_Has_Delayed_Freeze (Designator);
6301 end if;
6302 end Possible_Freeze;
6304 -- Local variables
6306 F : Entity_Id;
6308 -- Start of processing for Check_Delayed_Subprogram
6310 begin
6311 -- All subprograms, including abstract subprograms, may need a freeze
6312 -- node if some formal type or the return type needs one.
6314 Possible_Freeze (Etype (Designator));
6315 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6317 -- Need delayed freeze if any of the formal types themselves need a
6318 -- delayed freeze and are not yet frozen.
6320 F := First_Formal (Designator);
6321 while Present (F) loop
6322 Possible_Freeze (Etype (F));
6323 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6324 Next_Formal (F);
6325 end loop;
6327 -- Mark functions that return by reference. Note that it cannot be done
6328 -- for delayed_freeze subprograms because the underlying returned type
6329 -- may not be known yet (for private types).
6331 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6332 Compute_Returns_By_Ref (Designator);
6333 end if;
6334 end Check_Delayed_Subprogram;
6336 ------------------------------------
6337 -- Check_Discriminant_Conformance --
6338 ------------------------------------
6340 procedure Check_Discriminant_Conformance
6341 (N : Node_Id;
6342 Prev : Entity_Id;
6343 Prev_Loc : Node_Id)
6345 Old_Discr : Entity_Id := First_Discriminant (Prev);
6346 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6347 New_Discr_Id : Entity_Id;
6348 New_Discr_Type : Entity_Id;
6350 procedure Conformance_Error (Msg : String; N : Node_Id);
6351 -- Post error message for conformance error on given node. Two messages
6352 -- are output. The first points to the previous declaration with a
6353 -- general "no conformance" message. The second is the detailed reason,
6354 -- supplied as Msg. The parameter N provide information for a possible
6355 -- & insertion in the message.
6357 -----------------------
6358 -- Conformance_Error --
6359 -----------------------
6361 procedure Conformance_Error (Msg : String; N : Node_Id) is
6362 begin
6363 Error_Msg_Sloc := Sloc (Prev_Loc);
6364 Error_Msg_N -- CODEFIX
6365 ("not fully conformant with declaration#!", N);
6366 Error_Msg_NE (Msg, N, N);
6367 end Conformance_Error;
6369 -- Start of processing for Check_Discriminant_Conformance
6371 begin
6372 while Present (Old_Discr) and then Present (New_Discr) loop
6373 New_Discr_Id := Defining_Identifier (New_Discr);
6375 -- The subtype mark of the discriminant on the full type has not
6376 -- been analyzed so we do it here. For an access discriminant a new
6377 -- type is created.
6379 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6380 New_Discr_Type :=
6381 Access_Definition (N, Discriminant_Type (New_Discr));
6383 else
6384 Find_Type (Discriminant_Type (New_Discr));
6385 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6387 -- Ada 2005: if the discriminant definition carries a null
6388 -- exclusion, create an itype to check properly for consistency
6389 -- with partial declaration.
6391 if Is_Access_Type (New_Discr_Type)
6392 and then Null_Exclusion_Present (New_Discr)
6393 then
6394 New_Discr_Type :=
6395 Create_Null_Excluding_Itype
6396 (T => New_Discr_Type,
6397 Related_Nod => New_Discr,
6398 Scope_Id => Current_Scope);
6399 end if;
6400 end if;
6402 if not Conforming_Types
6403 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6404 then
6405 Conformance_Error ("type of & does not match!", New_Discr_Id);
6406 return;
6407 else
6408 -- Treat the new discriminant as an occurrence of the old one,
6409 -- for navigation purposes, and fill in some semantic
6410 -- information, for completeness.
6412 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6413 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6414 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6415 end if;
6417 -- Names must match
6419 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6420 Conformance_Error ("name & does not match!", New_Discr_Id);
6421 return;
6422 end if;
6424 -- Default expressions must match
6426 declare
6427 NewD : constant Boolean :=
6428 Present (Expression (New_Discr));
6429 OldD : constant Boolean :=
6430 Present (Expression (Parent (Old_Discr)));
6432 begin
6433 if NewD or OldD then
6435 -- The old default value has been analyzed and expanded,
6436 -- because the current full declaration will have frozen
6437 -- everything before. The new default values have not been
6438 -- expanded, so expand now to check conformance.
6440 if NewD then
6441 Preanalyze_Spec_Expression
6442 (Expression (New_Discr), New_Discr_Type);
6443 end if;
6445 if not (NewD and OldD)
6446 or else not Fully_Conformant_Expressions
6447 (Expression (Parent (Old_Discr)),
6448 Expression (New_Discr))
6450 then
6451 Conformance_Error
6452 ("default expression for & does not match!",
6453 New_Discr_Id);
6454 return;
6455 end if;
6456 end if;
6457 end;
6459 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6461 if Ada_Version = Ada_83 then
6462 declare
6463 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6465 begin
6466 -- Grouping (use of comma in param lists) must be the same
6467 -- This is where we catch a misconformance like:
6469 -- A, B : Integer
6470 -- A : Integer; B : Integer
6472 -- which are represented identically in the tree except
6473 -- for the setting of the flags More_Ids and Prev_Ids.
6475 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6476 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6477 then
6478 Conformance_Error
6479 ("grouping of & does not match!", New_Discr_Id);
6480 return;
6481 end if;
6482 end;
6483 end if;
6485 Next_Discriminant (Old_Discr);
6486 Next (New_Discr);
6487 end loop;
6489 if Present (Old_Discr) then
6490 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6491 return;
6493 elsif Present (New_Discr) then
6494 Conformance_Error
6495 ("too many discriminants!", Defining_Identifier (New_Discr));
6496 return;
6497 end if;
6498 end Check_Discriminant_Conformance;
6500 -----------------------------------------
6501 -- Check_Formal_Subprogram_Conformance --
6502 -----------------------------------------
6504 procedure Check_Formal_Subprogram_Conformance
6505 (New_Id : Entity_Id;
6506 Old_Id : Entity_Id;
6507 Err_Loc : Node_Id;
6508 Errmsg : Boolean;
6509 Conforms : out Boolean)
6511 N : Node_Id;
6512 begin
6513 Conforms := True;
6515 if Is_Formal_Subprogram (Old_Id)
6516 or else Is_Formal_Subprogram (New_Id)
6517 or else (Is_Subprogram (New_Id)
6518 and then Present (Alias (New_Id))
6519 and then Is_Formal_Subprogram (Alias (New_Id)))
6520 then
6521 if Present (Err_Loc) then
6522 N := Err_Loc;
6523 else
6524 N := New_Id;
6525 end if;
6527 Conforms := False;
6529 if Errmsg then
6530 Error_Msg_Sloc := Sloc (Old_Id);
6531 Error_Msg_N ("not subtype conformant with declaration#!", N);
6532 Error_Msg_NE
6533 ("\formal subprograms are not subtype conformant "
6534 & "(RM 6.3.1 (17/3))", N, New_Id);
6535 end if;
6536 end if;
6537 end Check_Formal_Subprogram_Conformance;
6539 procedure Check_Formal_Subprogram_Conformance
6540 (New_Id : Entity_Id;
6541 Old_Id : Entity_Id;
6542 Err_Loc : Node_Id := Empty)
6544 Ignore : Boolean;
6545 begin
6546 Check_Formal_Subprogram_Conformance
6547 (New_Id, Old_Id, Err_Loc, True, Ignore);
6548 end Check_Formal_Subprogram_Conformance;
6550 ----------------------------
6551 -- Check_Fully_Conformant --
6552 ----------------------------
6554 procedure Check_Fully_Conformant
6555 (New_Id : Entity_Id;
6556 Old_Id : Entity_Id;
6557 Err_Loc : Node_Id := Empty)
6559 Result : Boolean;
6560 pragma Warnings (Off, Result);
6561 begin
6562 Check_Conformance
6563 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6564 end Check_Fully_Conformant;
6566 --------------------------
6567 -- Check_Limited_Return --
6568 --------------------------
6570 procedure Check_Limited_Return
6571 (N : Node_Id;
6572 Expr : Node_Id;
6573 R_Type : Entity_Id)
6575 begin
6576 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6577 -- replaced by anonymous access results. This is an incompatibility with
6578 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6579 -- controllable with special switch. ???
6581 -- A limited interface that is not immutably limited is OK
6583 if Is_Limited_Interface (R_Type)
6584 and then not Is_Concurrent_Interface (R_Type)
6585 then
6586 null;
6588 elsif Is_Limited_Type (R_Type)
6589 and then not Is_Interface (R_Type)
6590 and then not (Nkind (N) = N_Simple_Return_Statement
6591 and then Comes_From_Extended_Return_Statement (N))
6592 and then not In_Instance_Body
6593 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6594 then
6595 -- Error in Ada 2005
6597 if Ada_Version >= Ada_2005
6598 and then not Debug_Flag_Dot_L
6599 and then not GNAT_Mode
6600 then
6601 Error_Msg_N
6602 ("(Ada 2005) cannot copy object of a limited type "
6603 & "(RM-2005 6.5(5.5/2))", Expr);
6605 if Is_Limited_View (R_Type) then
6606 Error_Msg_N
6607 ("\return by reference not permitted in Ada 2005", Expr);
6608 end if;
6610 -- Warn in Ada 95 mode, to give folks a heads up about this
6611 -- incompatibility.
6613 -- In GNAT mode, this is just a warning, to allow it to be evilly
6614 -- turned off. Otherwise it is a real error.
6616 -- In a generic context, simplify the warning because it makes no
6617 -- sense to discuss pass-by-reference or copy.
6619 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6620 if Inside_A_Generic then
6621 Error_Msg_N
6622 ("return of limited object not permitted in Ada 2005 "
6623 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6625 elsif Is_Limited_View (R_Type) then
6626 Error_Msg_N
6627 ("return by reference not permitted in Ada 2005 "
6628 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6629 else
6630 Error_Msg_N
6631 ("cannot copy object of a limited type in Ada 2005 "
6632 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6633 end if;
6635 -- Ada 95 mode, and compatibility warnings disabled
6637 else
6638 pragma Assert (Ada_Version <= Ada_95);
6639 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6640 return; -- skip continuation messages below
6641 end if;
6643 if not Inside_A_Generic then
6644 Error_Msg_N
6645 ("\consider switching to return of access type", Expr);
6646 Explain_Limited_Type (R_Type, Expr);
6647 end if;
6648 end if;
6649 end Check_Limited_Return;
6651 ---------------------------
6652 -- Check_Mode_Conformant --
6653 ---------------------------
6655 procedure Check_Mode_Conformant
6656 (New_Id : Entity_Id;
6657 Old_Id : Entity_Id;
6658 Err_Loc : Node_Id := Empty;
6659 Get_Inst : Boolean := False)
6661 Result : Boolean;
6662 pragma Warnings (Off, Result);
6663 begin
6664 Check_Conformance
6665 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6666 end Check_Mode_Conformant;
6668 --------------------------------
6669 -- Check_Overriding_Indicator --
6670 --------------------------------
6672 procedure Check_Overriding_Indicator
6673 (Subp : Entity_Id;
6674 Overridden_Subp : Entity_Id;
6675 Is_Primitive : Boolean)
6677 Decl : Node_Id;
6678 Spec : Node_Id;
6680 begin
6681 -- No overriding indicator for literals
6683 if Ekind (Subp) = E_Enumeration_Literal then
6684 return;
6686 elsif Ekind (Subp) = E_Entry then
6687 Decl := Parent (Subp);
6689 -- No point in analyzing a malformed operator
6691 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6692 and then Error_Posted (Subp)
6693 then
6694 return;
6696 else
6697 Decl := Unit_Declaration_Node (Subp);
6698 end if;
6700 if Nkind (Decl) in N_Subprogram_Body
6701 | N_Subprogram_Body_Stub
6702 | N_Subprogram_Declaration
6703 | N_Abstract_Subprogram_Declaration
6704 | N_Subprogram_Renaming_Declaration
6705 then
6706 Spec := Specification (Decl);
6708 elsif Nkind (Decl) = N_Entry_Declaration then
6709 Spec := Decl;
6711 else
6712 return;
6713 end if;
6715 -- An overriding indication is illegal on a subprogram declared
6716 -- in a protected body, where there is no operation to override.
6718 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6719 and then Is_List_Member (Decl)
6720 and then Present (Parent (List_Containing (Decl)))
6721 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6722 then
6723 Error_Msg_N
6724 ("illegal overriding indication in protected body", Decl);
6725 return;
6726 end if;
6728 -- The overriding operation is type conformant with the overridden one,
6729 -- but the names of the formals are not required to match. If the names
6730 -- appear permuted in the overriding operation, this is a possible
6731 -- source of confusion that is worth diagnosing. Controlling formals
6732 -- often carry names that reflect the type, and it is not worthwhile
6733 -- requiring that their names match.
6735 if Present (Overridden_Subp)
6736 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6737 then
6738 declare
6739 Form1 : Entity_Id;
6740 Form2 : Entity_Id;
6742 begin
6743 Form1 := First_Formal (Subp);
6744 Form2 := First_Formal (Overridden_Subp);
6746 -- If the overriding operation is a synchronized operation, skip
6747 -- the first parameter of the overridden operation, which is
6748 -- implicit in the new one. If the operation is declared in the
6749 -- body it is not primitive and all formals must match.
6751 if Is_Concurrent_Type (Scope (Subp))
6752 and then Is_Tagged_Type (Scope (Subp))
6753 and then not Has_Completion (Scope (Subp))
6754 then
6755 Form2 := Next_Formal (Form2);
6756 end if;
6758 if Present (Form1) then
6759 Form1 := Next_Formal (Form1);
6760 Form2 := Next_Formal (Form2);
6761 end if;
6763 while Present (Form1) loop
6764 if not Is_Controlling_Formal (Form1)
6765 and then Present (Next_Formal (Form2))
6766 and then Chars (Form1) = Chars (Next_Formal (Form2))
6767 then
6768 Error_Msg_Node_2 := Alias (Overridden_Subp);
6769 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6770 Error_Msg_NE
6771 ("& does not match corresponding formal of&#",
6772 Form1, Form1);
6773 exit;
6774 end if;
6776 Next_Formal (Form1);
6777 Next_Formal (Form2);
6778 end loop;
6779 end;
6780 end if;
6782 -- If there is an overridden subprogram, then check that there is no
6783 -- "not overriding" indicator, and mark the subprogram as overriding.
6785 -- This is not done if the overridden subprogram is marked as hidden,
6786 -- which can occur for the case of inherited controlled operations
6787 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6788 -- subprogram is not itself hidden or we are within a generic instance,
6789 -- in which case the hidden flag may have been modified for the
6790 -- expansion of the instance.
6792 -- (Note: This condition could probably be simplified, leaving out the
6793 -- testing for the specific controlled cases, but it seems safer and
6794 -- clearer this way, and echoes similar special-case tests of this
6795 -- kind in other places.)
6797 if Present (Overridden_Subp)
6798 and then (not Is_Hidden (Overridden_Subp)
6799 or else
6800 (Chars (Overridden_Subp) in Name_Initialize
6801 | Name_Adjust
6802 | Name_Finalize
6803 and then Present (Alias (Overridden_Subp))
6804 and then (not Is_Hidden (Alias (Overridden_Subp))
6805 or else In_Instance)))
6806 then
6807 if Must_Not_Override (Spec) then
6808 Error_Msg_Sloc := Sloc (Overridden_Subp);
6810 if Ekind (Subp) = E_Entry then
6811 Error_Msg_NE
6812 ("entry & overrides inherited operation #", Spec, Subp);
6813 else
6814 Error_Msg_NE
6815 ("subprogram & overrides inherited operation #", Spec, Subp);
6816 end if;
6818 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6819 -- as an extension of Root_Controlled, and thus has a useless Adjust
6820 -- operation. This operation should not be inherited by other limited
6821 -- controlled types. An explicit Adjust for them is not overriding.
6823 elsif Must_Override (Spec)
6824 and then Chars (Overridden_Subp) = Name_Adjust
6825 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6826 and then Present (Alias (Overridden_Subp))
6827 and then In_Predefined_Unit (Alias (Overridden_Subp))
6828 then
6829 Get_Name_String
6830 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6831 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6833 elsif Is_Subprogram (Subp) then
6834 if Is_Init_Proc (Subp) then
6835 null;
6837 elsif No (Overridden_Operation (Subp)) then
6839 -- For entities generated by Derive_Subprograms the overridden
6840 -- operation is the inherited primitive (which is available
6841 -- through the attribute alias)
6843 if (Is_Dispatching_Operation (Subp)
6844 or else Is_Dispatching_Operation (Overridden_Subp))
6845 and then not Comes_From_Source (Overridden_Subp)
6846 and then Find_Dispatching_Type (Overridden_Subp) =
6847 Find_Dispatching_Type (Subp)
6848 and then Present (Alias (Overridden_Subp))
6849 and then Comes_From_Source (Alias (Overridden_Subp))
6850 then
6851 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6852 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6853 Set_Is_Ada_2022_Only (Subp,
6854 Is_Ada_2022_Only (Alias (Overridden_Subp)));
6856 else
6857 Set_Overridden_Operation (Subp, Overridden_Subp);
6858 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6859 Set_Is_Ada_2022_Only (Subp,
6860 Is_Ada_2022_Only (Overridden_Subp));
6861 end if;
6862 end if;
6863 end if;
6865 -- If primitive flag is set or this is a protected operation, then
6866 -- the operation is overriding at the point of its declaration, so
6867 -- warn if necessary. Otherwise it may have been declared before the
6868 -- operation it overrides and no check is required.
6870 if Style_Check
6871 and then not Must_Override (Spec)
6872 and then (Is_Primitive
6873 or else Ekind (Scope (Subp)) = E_Protected_Type)
6874 then
6875 Style.Missing_Overriding (Decl, Subp);
6876 end if;
6878 -- If Subp is an operator, it may override a predefined operation, if
6879 -- it is defined in the same scope as the type to which it applies.
6880 -- In that case Overridden_Subp is empty because of our implicit
6881 -- representation for predefined operators. We have to check whether the
6882 -- signature of Subp matches that of a predefined operator. Note that
6883 -- first argument provides the name of the operator, and the second
6884 -- argument the signature that may match that of a standard operation.
6885 -- If the indicator is overriding, then the operator must match a
6886 -- predefined signature, because we know already that there is no
6887 -- explicit overridden operation.
6889 elsif Chars (Subp) in Any_Operator_Name then
6890 if Must_Not_Override (Spec) then
6892 -- If this is not a primitive or a protected subprogram, then
6893 -- "not overriding" is illegal.
6895 if not Is_Primitive
6896 and then Ekind (Scope (Subp)) /= E_Protected_Type
6897 then
6898 Error_Msg_N ("overriding indicator only allowed "
6899 & "if subprogram is primitive", Subp);
6901 elsif Can_Override_Operator (Subp) then
6902 Error_Msg_NE
6903 ("subprogram& overrides predefined operator", Spec, Subp);
6904 end if;
6906 elsif Must_Override (Spec) then
6907 if No (Overridden_Operation (Subp))
6908 and then not Can_Override_Operator (Subp)
6909 then
6910 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6911 end if;
6913 elsif not Error_Posted (Subp)
6914 and then Style_Check
6915 and then Can_Override_Operator (Subp)
6916 and then not In_Predefined_Unit (Subp)
6917 then
6918 -- If style checks are enabled, indicate that the indicator is
6919 -- missing. However, at the point of declaration, the type of
6920 -- which this is a primitive operation may be private, in which
6921 -- case the indicator would be premature.
6923 if Has_Private_Declaration (Etype (Subp))
6924 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6925 then
6926 null;
6927 else
6928 Style.Missing_Overriding (Decl, Subp);
6929 end if;
6930 end if;
6932 elsif Must_Override (Spec) then
6933 if Ekind (Subp) = E_Entry then
6934 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6935 else
6936 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6937 end if;
6939 -- If the operation is marked "not overriding" and it's not primitive
6940 -- then an error is issued, unless this is an operation of a task or
6941 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6942 -- has been specified have already been checked above.
6944 elsif Must_Not_Override (Spec)
6945 and then not Is_Primitive
6946 and then Ekind (Subp) /= E_Entry
6947 and then Ekind (Scope (Subp)) /= E_Protected_Type
6948 then
6949 Error_Msg_N
6950 ("overriding indicator only allowed if subprogram is primitive",
6951 Subp);
6952 return;
6953 end if;
6954 end Check_Overriding_Indicator;
6956 -------------------
6957 -- Check_Returns --
6958 -------------------
6960 -- Note: this procedure needs to know far too much about how the expander
6961 -- messes with exceptions. The use of the flag Exception_Junk and the
6962 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6963 -- works, but is not very clean. It would be better if the expansion
6964 -- routines would leave Original_Node working nicely, and we could use
6965 -- Original_Node here to ignore all the peculiar expander messing ???
6967 procedure Check_Returns
6968 (HSS : Node_Id;
6969 Mode : Character;
6970 Err : out Boolean;
6971 Proc : Entity_Id := Empty)
6973 pragma Assert (Mode in 'F' | 'P');
6974 pragma Assert (if Mode = 'F' then No (Proc));
6975 Handler : Node_Id;
6977 procedure Check_Statement_Sequence (L : List_Id);
6978 -- Internal recursive procedure to check a list of statements for proper
6979 -- termination by a return statement (or a transfer of control or a
6980 -- compound statement that is itself internally properly terminated).
6982 ------------------------------
6983 -- Check_Statement_Sequence --
6984 ------------------------------
6986 procedure Check_Statement_Sequence (L : List_Id) is
6987 Last_Stm : Node_Id;
6988 Stm : Node_Id;
6989 Kind : Node_Kind;
6991 function Assert_False return Boolean;
6992 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6993 -- rewritten as a null statement when assertions are off. The assert
6994 -- is not active, but it is still enough to kill the warning.
6996 ------------------
6997 -- Assert_False --
6998 ------------------
7000 function Assert_False return Boolean is
7001 Orig : constant Node_Id := Original_Node (Last_Stm);
7003 begin
7004 if Nkind (Orig) = N_Pragma
7005 and then Pragma_Name (Orig) = Name_Assert
7006 and then not Error_Posted (Orig)
7007 then
7008 declare
7009 Arg : constant Node_Id :=
7010 First (Pragma_Argument_Associations (Orig));
7011 Exp : constant Node_Id := Expression (Arg);
7012 begin
7013 return Nkind (Exp) = N_Identifier
7014 and then Chars (Exp) = Name_False;
7015 end;
7017 else
7018 return False;
7019 end if;
7020 end Assert_False;
7022 -- Local variables
7024 Raise_Exception_Call : Boolean := False;
7025 -- Set True if statement sequence terminated by Raise_Exception call
7026 -- or a Reraise_Occurrence call.
7028 -- Start of processing for Check_Statement_Sequence
7030 begin
7031 -- Get last real statement
7033 Last_Stm := Last (L);
7035 -- Deal with digging out exception handler statement sequences that
7036 -- have been transformed by the local raise to goto optimization.
7037 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7038 -- optimization has occurred, we are looking at something like:
7040 -- begin
7041 -- original stmts in block
7043 -- exception \
7044 -- when excep1 => |
7045 -- goto L1; | omitted if No_Exception_Propagation
7046 -- when excep2 => |
7047 -- goto L2; /
7048 -- end;
7050 -- goto L3; -- skip handler when exception not raised
7052 -- <<L1>> -- target label for local exception
7053 -- begin
7054 -- estmts1
7055 -- end;
7057 -- goto L3;
7059 -- <<L2>>
7060 -- begin
7061 -- estmts2
7062 -- end;
7064 -- <<L3>>
7066 -- and what we have to do is to dig out the estmts1 and estmts2
7067 -- sequences (which were the original sequences of statements in
7068 -- the exception handlers) and check them.
7070 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7071 Stm := Last_Stm;
7072 loop
7073 Prev (Stm);
7074 exit when No (Stm);
7075 exit when Nkind (Stm) /= N_Block_Statement;
7076 exit when not Exception_Junk (Stm);
7077 Prev (Stm);
7078 exit when No (Stm);
7079 exit when Nkind (Stm) /= N_Label;
7080 exit when not Exception_Junk (Stm);
7081 Check_Statement_Sequence
7082 (Statements (Handled_Statement_Sequence (Next (Stm))));
7084 Prev (Stm);
7085 Last_Stm := Stm;
7086 exit when No (Stm);
7087 exit when Nkind (Stm) /= N_Goto_Statement;
7088 exit when not Exception_Junk (Stm);
7089 end loop;
7090 end if;
7092 -- Don't count pragmas
7094 while Nkind (Last_Stm) = N_Pragma
7096 -- Don't count call to SS_Release (can happen after
7097 -- Raise_Exception).
7099 or else
7100 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7101 and then
7102 Nkind (Name (Last_Stm)) = N_Identifier
7103 and then
7104 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7106 -- Don't count exception junk
7108 or else
7109 (Nkind (Last_Stm) in
7110 N_Goto_Statement | N_Label | N_Object_Declaration
7111 and then Exception_Junk (Last_Stm))
7112 or else Nkind (Last_Stm) in N_Push_xxx_Label | N_Pop_xxx_Label
7114 -- Inserted code, such as finalization calls, is irrelevant; we
7115 -- only need to check original source. If we see a transfer of
7116 -- control, we stop.
7118 or else (Is_Rewrite_Insertion (Last_Stm)
7119 and then not Is_Transfer (Last_Stm))
7120 loop
7121 Prev (Last_Stm);
7122 end loop;
7124 -- Here we have the "real" last statement
7126 Kind := Nkind (Last_Stm);
7128 -- Transfer of control, OK. Note that in the No_Return procedure
7129 -- case, we already diagnosed any explicit return statements, so
7130 -- we can treat them as OK in this context.
7132 if Is_Transfer (Last_Stm) then
7133 return;
7135 -- Check cases of explicit non-indirect procedure calls
7137 elsif Kind = N_Procedure_Call_Statement
7138 and then Is_Entity_Name (Name (Last_Stm))
7139 then
7140 -- Check call to Raise_Exception procedure which is treated
7141 -- specially, as is a call to Reraise_Occurrence.
7143 -- We suppress the warning in these cases since it is likely that
7144 -- the programmer really does not expect to deal with the case
7145 -- of Null_Occurrence, and thus would find a warning about a
7146 -- missing return curious, and raising Program_Error does not
7147 -- seem such a bad behavior if this does occur.
7149 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7150 -- behavior will be to raise Constraint_Error (see AI-329).
7152 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7153 or else
7154 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7155 then
7156 Raise_Exception_Call := True;
7158 -- For Raise_Exception call, test first argument, if it is
7159 -- an attribute reference for a 'Identity call, then we know
7160 -- that the call cannot possibly return.
7162 declare
7163 Arg : constant Node_Id :=
7164 Original_Node (First_Actual (Last_Stm));
7165 begin
7166 if Nkind (Arg) = N_Attribute_Reference
7167 and then Attribute_Name (Arg) = Name_Identity
7168 then
7169 return;
7170 end if;
7171 end;
7172 end if;
7174 -- If statement, need to look inside if there is an else and check
7175 -- each constituent statement sequence for proper termination.
7177 elsif Kind = N_If_Statement
7178 and then Present (Else_Statements (Last_Stm))
7179 then
7180 Check_Statement_Sequence (Then_Statements (Last_Stm));
7181 Check_Statement_Sequence (Else_Statements (Last_Stm));
7183 declare
7184 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7186 begin
7187 while Present (Elsif_Part) loop
7188 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7189 Next (Elsif_Part);
7190 end loop;
7191 end;
7193 return;
7195 -- Case statement, check each case for proper termination
7197 elsif Kind = N_Case_Statement then
7198 declare
7199 Case_Alt : Node_Id;
7200 begin
7201 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7202 while Present (Case_Alt) loop
7203 Check_Statement_Sequence (Statements (Case_Alt));
7204 Next_Non_Pragma (Case_Alt);
7205 end loop;
7206 end;
7208 return;
7210 -- Block statement, check its handled sequence of statements
7212 elsif Kind = N_Block_Statement then
7213 declare
7214 Err1 : Boolean;
7216 begin
7217 Check_Returns
7218 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7220 if Err1 then
7221 Err := True;
7222 end if;
7224 return;
7225 end;
7227 -- Loop statement. If there is an iteration scheme, we can definitely
7228 -- fall out of the loop. Similarly if there is an exit statement, we
7229 -- can fall out. In either case we need a following return.
7231 elsif Kind = N_Loop_Statement then
7232 if Present (Iteration_Scheme (Last_Stm))
7233 or else Has_Exit (Entity (Identifier (Last_Stm)))
7234 then
7235 null;
7237 -- A loop with no exit statement or iteration scheme is either
7238 -- an infinite loop, or it has some other exit (raise/return).
7239 -- In either case, no warning is required.
7241 else
7242 return;
7243 end if;
7245 -- Timed entry call, check entry call and delay alternatives
7247 -- Note: in expanded code, the timed entry call has been converted
7248 -- to a set of expanded statements on which the check will work
7249 -- correctly in any case.
7251 elsif Kind = N_Timed_Entry_Call then
7252 declare
7253 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7254 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7256 begin
7257 -- If statement sequence of entry call alternative is missing,
7258 -- then we can definitely fall through, and we post the error
7259 -- message on the entry call alternative itself.
7261 if No (Statements (ECA)) then
7262 Last_Stm := ECA;
7264 -- If statement sequence of delay alternative is missing, then
7265 -- we can definitely fall through, and we post the error
7266 -- message on the delay alternative itself.
7268 -- Note: if both ECA and DCA are missing the return, then we
7269 -- post only one message, should be enough to fix the bugs.
7270 -- If not we will get a message next time on the DCA when the
7271 -- ECA is fixed.
7273 elsif No (Statements (DCA)) then
7274 Last_Stm := DCA;
7276 -- Else check both statement sequences
7278 else
7279 Check_Statement_Sequence (Statements (ECA));
7280 Check_Statement_Sequence (Statements (DCA));
7281 return;
7282 end if;
7283 end;
7285 -- Conditional entry call, check entry call and else part
7287 -- Note: in expanded code, the conditional entry call has been
7288 -- converted to a set of expanded statements on which the check
7289 -- will work correctly in any case.
7291 elsif Kind = N_Conditional_Entry_Call then
7292 declare
7293 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7295 begin
7296 -- If statement sequence of entry call alternative is missing,
7297 -- then we can definitely fall through, and we post the error
7298 -- message on the entry call alternative itself.
7300 if No (Statements (ECA)) then
7301 Last_Stm := ECA;
7303 -- Else check statement sequence and else part
7305 else
7306 Check_Statement_Sequence (Statements (ECA));
7307 Check_Statement_Sequence (Else_Statements (Last_Stm));
7308 return;
7309 end if;
7310 end;
7311 end if;
7313 -- If we fall through, issue appropriate message
7315 if Mode = 'F' then
7317 -- Kill warning if last statement is a raise exception call,
7318 -- or a pragma Assert (False). Note that with assertions enabled,
7319 -- such a pragma has been converted into a raise exception call
7320 -- already, so the Assert_False is for the assertions off case.
7322 if not Raise_Exception_Call and then not Assert_False then
7323 Error_Msg_N
7324 ("RETURN statement missing following this statement??!",
7325 Last_Stm);
7326 Error_Msg_N
7327 ("\Program_Error [??!", Last_Stm);
7328 end if;
7330 -- Note: we set Err even though we have not issued a warning
7331 -- because we still have a case of a missing return. This is
7332 -- an extremely marginal case, probably will never be noticed
7333 -- but we might as well get it right.
7335 Err := True;
7337 -- Otherwise we have the case of a procedure marked No_Return
7339 else
7340 if not Raise_Exception_Call then
7341 if GNATprove_Mode then
7342 Error_Msg_N
7343 ("implied return after this statement would have raised "
7344 & "Program_Error", Last_Stm);
7346 -- In normal compilation mode, do not warn on a generated call
7347 -- (e.g. in the body of a renaming as completion).
7349 elsif Comes_From_Source (Last_Stm) then
7350 Error_Msg_N
7351 ("implied return after this statement will raise "
7352 & "Program_Error??", Last_Stm);
7353 end if;
7355 Error_Msg_Warn := SPARK_Mode /= On;
7356 Error_Msg_NE
7357 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7358 end if;
7360 declare
7361 RE : constant Node_Id :=
7362 Make_Raise_Program_Error (Sloc (Last_Stm),
7363 Reason => PE_Implicit_Return);
7364 begin
7365 Insert_After (Last_Stm, RE);
7366 Analyze (RE);
7367 end;
7368 end if;
7369 end Check_Statement_Sequence;
7371 -- Start of processing for Check_Returns
7373 begin
7374 Err := False;
7375 Check_Statement_Sequence (Statements (HSS));
7377 if Present (Exception_Handlers (HSS)) then
7378 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7379 while Present (Handler) loop
7380 Check_Statement_Sequence (Statements (Handler));
7381 Next_Non_Pragma (Handler);
7382 end loop;
7383 end if;
7384 end Check_Returns;
7386 ----------------------------
7387 -- Check_Subprogram_Order --
7388 ----------------------------
7390 procedure Check_Subprogram_Order (N : Node_Id) is
7392 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7393 -- This is used to check if S1 > S2 in the sense required by this test,
7394 -- for example nameab < namec, but name2 < name10.
7396 -----------------------------
7397 -- Subprogram_Name_Greater --
7398 -----------------------------
7400 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7401 L1, L2 : Positive;
7402 N1, N2 : Natural;
7404 begin
7405 -- Deal with special case where names are identical except for a
7406 -- numerical suffix. These are handled specially, taking the numeric
7407 -- ordering from the suffix into account.
7409 L1 := S1'Last;
7410 while S1 (L1) in '0' .. '9' loop
7411 L1 := L1 - 1;
7412 end loop;
7414 L2 := S2'Last;
7415 while S2 (L2) in '0' .. '9' loop
7416 L2 := L2 - 1;
7417 end loop;
7419 -- If non-numeric parts non-equal, do straight compare
7421 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7422 return S1 > S2;
7424 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7425 -- that a missing suffix is treated as numeric zero in this test.
7427 else
7428 N1 := 0;
7429 while L1 < S1'Last loop
7430 L1 := L1 + 1;
7431 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7432 end loop;
7434 N2 := 0;
7435 while L2 < S2'Last loop
7436 L2 := L2 + 1;
7437 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7438 end loop;
7440 return N1 > N2;
7441 end if;
7442 end Subprogram_Name_Greater;
7444 -- Start of processing for Check_Subprogram_Order
7446 begin
7447 -- Check body in alpha order if this is option
7449 if Style_Check
7450 and then Style_Check_Order_Subprograms
7451 and then Nkind (N) = N_Subprogram_Body
7452 and then Comes_From_Source (N)
7453 and then In_Extended_Main_Source_Unit (N)
7454 then
7455 declare
7456 LSN : String_Ptr
7457 renames Scope_Stack.Table
7458 (Scope_Stack.Last).Last_Subprogram_Name;
7460 Body_Id : constant Entity_Id :=
7461 Defining_Entity (Specification (N));
7463 begin
7464 Get_Decoded_Name_String (Chars (Body_Id));
7466 if LSN /= null then
7467 if Subprogram_Name_Greater
7468 (LSN.all, Name_Buffer (1 .. Name_Len))
7469 then
7470 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7471 end if;
7473 Free (LSN);
7474 end if;
7476 LSN := new String'(Name_Buffer (1 .. Name_Len));
7477 end;
7478 end if;
7479 end Check_Subprogram_Order;
7481 ------------------------------
7482 -- Check_Subtype_Conformant --
7483 ------------------------------
7485 procedure Check_Subtype_Conformant
7486 (New_Id : Entity_Id;
7487 Old_Id : Entity_Id;
7488 Err_Loc : Node_Id := Empty;
7489 Skip_Controlling_Formals : Boolean := False;
7490 Get_Inst : Boolean := False)
7492 Result : Boolean;
7493 pragma Warnings (Off, Result);
7494 begin
7495 Check_Conformance
7496 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7497 Skip_Controlling_Formals => Skip_Controlling_Formals,
7498 Get_Inst => Get_Inst);
7499 end Check_Subtype_Conformant;
7501 -----------------------------------
7502 -- Check_Synchronized_Overriding --
7503 -----------------------------------
7505 procedure Check_Synchronized_Overriding
7506 (Def_Id : Entity_Id;
7507 Overridden_Subp : out Entity_Id)
7509 Ifaces_List : Elist_Id;
7510 In_Scope : Boolean;
7511 Typ : Entity_Id;
7513 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7514 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7515 -- subprogram is implemented by a protected procedure or entry,
7516 -- its first parameter must be out, in out, or access-to-variable.
7518 function Matches_Prefixed_View_Profile
7519 (Prim_Params : List_Id;
7520 Iface_Params : List_Id) return Boolean;
7521 -- Determine whether a subprogram's parameter profile Prim_Params
7522 -- matches that of a potentially overridden interface subprogram
7523 -- Iface_Params. Also determine if the type of first parameter of
7524 -- Iface_Params is an implemented interface.
7526 ----------------------
7527 -- Is_Valid_Formal --
7528 ----------------------
7530 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7531 begin
7532 return
7533 Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
7534 or else
7535 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7536 and then not Constant_Present (Parameter_Type (Parent (F))));
7537 end Is_Valid_Formal;
7539 -----------------------------------
7540 -- Matches_Prefixed_View_Profile --
7541 -----------------------------------
7543 function Matches_Prefixed_View_Profile
7544 (Prim_Params : List_Id;
7545 Iface_Params : List_Id) return Boolean
7547 function Is_Implemented
7548 (Ifaces_List : Elist_Id;
7549 Iface : Entity_Id) return Boolean;
7550 -- Determine if Iface is implemented by the current task or
7551 -- protected type.
7553 --------------------
7554 -- Is_Implemented --
7555 --------------------
7557 function Is_Implemented
7558 (Ifaces_List : Elist_Id;
7559 Iface : Entity_Id) return Boolean
7561 Iface_Elmt : Elmt_Id;
7563 begin
7564 Iface_Elmt := First_Elmt (Ifaces_List);
7565 while Present (Iface_Elmt) loop
7566 if Node (Iface_Elmt) = Iface then
7567 return True;
7568 end if;
7570 Next_Elmt (Iface_Elmt);
7571 end loop;
7573 return False;
7574 end Is_Implemented;
7576 -- Local variables
7578 Iface_Id : Entity_Id;
7579 Iface_Param : Node_Id;
7580 Iface_Typ : Entity_Id;
7581 Prim_Id : Entity_Id;
7582 Prim_Param : Node_Id;
7583 Prim_Typ : Entity_Id;
7585 -- Start of processing for Matches_Prefixed_View_Profile
7587 begin
7588 Iface_Param := First (Iface_Params);
7589 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7591 if Is_Access_Type (Iface_Typ) then
7592 Iface_Typ := Designated_Type (Iface_Typ);
7593 end if;
7595 Prim_Param := First (Prim_Params);
7597 -- The first parameter of the potentially overridden subprogram must
7598 -- be an interface implemented by Prim.
7600 if not Is_Interface (Iface_Typ)
7601 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7602 then
7603 return False;
7604 end if;
7606 -- The checks on the object parameters are done, so move on to the
7607 -- rest of the parameters.
7609 if not In_Scope then
7610 Next (Prim_Param);
7611 end if;
7613 Next (Iface_Param);
7614 while Present (Iface_Param) and then Present (Prim_Param) loop
7615 Iface_Id := Defining_Identifier (Iface_Param);
7616 Iface_Typ := Find_Parameter_Type (Iface_Param);
7618 Prim_Id := Defining_Identifier (Prim_Param);
7619 Prim_Typ := Find_Parameter_Type (Prim_Param);
7621 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7622 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7623 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7624 then
7625 Iface_Typ := Designated_Type (Iface_Typ);
7626 Prim_Typ := Designated_Type (Prim_Typ);
7627 end if;
7629 -- Case of multiple interface types inside a parameter profile
7631 -- (Obj_Param : in out Iface; ...; Param : Iface)
7633 -- If the interface type is implemented, then the matching type in
7634 -- the primitive should be the implementing record type.
7636 if Ekind (Iface_Typ) = E_Record_Type
7637 and then Is_Interface (Iface_Typ)
7638 and then Is_Implemented (Ifaces_List, Iface_Typ)
7639 then
7640 if Prim_Typ /= Typ then
7641 return False;
7642 end if;
7644 -- The two parameters must be both mode and subtype conformant
7646 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7647 or else not
7648 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7649 then
7650 return False;
7651 end if;
7653 Next (Iface_Param);
7654 Next (Prim_Param);
7655 end loop;
7657 -- One of the two lists contains more parameters than the other
7659 if Present (Iface_Param) or else Present (Prim_Param) then
7660 return False;
7661 end if;
7663 return True;
7664 end Matches_Prefixed_View_Profile;
7666 -- Start of processing for Check_Synchronized_Overriding
7668 begin
7669 Overridden_Subp := Empty;
7671 -- Def_Id must be an entry or a subprogram. We should skip predefined
7672 -- primitives internally generated by the front end; however at this
7673 -- stage predefined primitives are still not fully decorated. As a
7674 -- minor optimization we skip here internally generated subprograms.
7676 if (Ekind (Def_Id) /= E_Entry
7677 and then Ekind (Def_Id) /= E_Function
7678 and then Ekind (Def_Id) /= E_Procedure)
7679 or else not Comes_From_Source (Def_Id)
7680 then
7681 return;
7682 end if;
7684 -- Search for the concurrent declaration since it contains the list of
7685 -- all implemented interfaces. In this case, the subprogram is declared
7686 -- within the scope of a protected or a task type.
7688 if Present (Scope (Def_Id))
7689 and then Is_Concurrent_Type (Scope (Def_Id))
7690 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7691 then
7692 Typ := Scope (Def_Id);
7693 In_Scope := True;
7695 -- The enclosing scope is not a synchronized type and the subprogram
7696 -- has no formals.
7698 elsif No (First_Formal (Def_Id)) then
7699 return;
7701 -- The subprogram has formals and hence it may be a primitive of a
7702 -- concurrent type.
7704 else
7705 Typ := Etype (First_Formal (Def_Id));
7707 if Is_Access_Type (Typ) then
7708 Typ := Directly_Designated_Type (Typ);
7709 end if;
7711 if Is_Concurrent_Type (Typ)
7712 and then not Is_Generic_Actual_Type (Typ)
7713 then
7714 In_Scope := False;
7716 -- This case occurs when the concurrent type is declared within a
7717 -- generic unit. As a result the corresponding record has been built
7718 -- and used as the type of the first formal, we just have to retrieve
7719 -- the corresponding concurrent type.
7721 elsif Is_Concurrent_Record_Type (Typ)
7722 and then not Is_Class_Wide_Type (Typ)
7723 and then Present (Corresponding_Concurrent_Type (Typ))
7724 then
7725 Typ := Corresponding_Concurrent_Type (Typ);
7726 In_Scope := False;
7728 else
7729 return;
7730 end if;
7731 end if;
7733 -- There is no overriding to check if this is an inherited operation in
7734 -- a type derivation for a generic actual.
7736 Collect_Interfaces (Typ, Ifaces_List);
7738 if Is_Empty_Elmt_List (Ifaces_List) then
7739 return;
7740 end if;
7742 -- Determine whether entry or subprogram Def_Id overrides a primitive
7743 -- operation that belongs to one of the interfaces in Ifaces_List.
7745 declare
7746 Candidate : Entity_Id := Empty;
7747 Hom : Entity_Id := Empty;
7748 Subp : Entity_Id := Empty;
7750 begin
7751 -- Traverse the homonym chain, looking for a potentially overridden
7752 -- subprogram that belongs to an implemented interface.
7754 Hom := Current_Entity_In_Scope (Def_Id);
7755 while Present (Hom) loop
7756 Subp := Hom;
7758 if Subp = Def_Id
7759 or else not Is_Overloadable (Subp)
7760 or else not Is_Primitive (Subp)
7761 or else not Is_Dispatching_Operation (Subp)
7762 or else No (Find_Dispatching_Type (Subp))
7763 or else not Is_Interface (Find_Dispatching_Type (Subp))
7764 then
7765 null;
7767 -- Entries and procedures can override abstract or null interface
7768 -- procedures.
7770 elsif Ekind (Def_Id) in E_Entry | E_Procedure
7771 and then Ekind (Subp) = E_Procedure
7772 and then Matches_Prefixed_View_Profile
7773 (Parameter_Specifications (Parent (Def_Id)),
7774 Parameter_Specifications (Parent (Subp)))
7775 then
7776 Candidate := Subp;
7778 -- For an overridden subprogram Subp, check whether the mode
7779 -- of its first parameter is correct depending on the kind of
7780 -- synchronized type.
7782 declare
7783 Formal : constant Node_Id := First_Formal (Candidate);
7785 begin
7786 -- In order for an entry or a protected procedure to
7787 -- override, the first parameter of the overridden routine
7788 -- must be of mode "out", "in out", or access-to-variable.
7790 if Ekind (Candidate) in E_Entry | E_Procedure
7791 and then Is_Protected_Type (Typ)
7792 and then not Is_Valid_Formal (Formal)
7793 then
7794 null;
7796 -- All other cases are OK since a task entry or routine does
7797 -- not have a restriction on the mode of the first parameter
7798 -- of the overridden interface routine.
7800 else
7801 Overridden_Subp := Candidate;
7802 return;
7803 end if;
7804 end;
7806 -- Functions can override abstract interface functions. Return
7807 -- types must be subtype conformant.
7809 elsif Ekind (Def_Id) = E_Function
7810 and then Ekind (Subp) = E_Function
7811 and then Matches_Prefixed_View_Profile
7812 (Parameter_Specifications (Parent (Def_Id)),
7813 Parameter_Specifications (Parent (Subp)))
7814 and then Conforming_Types
7815 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7816 then
7817 Candidate := Subp;
7819 -- If an inherited subprogram is implemented by a protected
7820 -- function, then the first parameter of the inherited
7821 -- subprogram shall be of mode in, but not an access-to-
7822 -- variable parameter (RM 9.4(11/9)).
7824 if Present (First_Formal (Subp))
7825 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7826 and then
7827 (not Is_Access_Type (Etype (First_Formal (Subp)))
7828 or else
7829 Is_Access_Constant (Etype (First_Formal (Subp))))
7830 then
7831 Overridden_Subp := Subp;
7832 return;
7833 end if;
7834 end if;
7836 Hom := Homonym (Hom);
7837 end loop;
7839 -- After examining all candidates for overriding, we are left with
7840 -- the best match, which is a mode-incompatible interface routine.
7842 if In_Scope and then Present (Candidate) then
7843 Error_Msg_PT (Def_Id, Candidate);
7844 end if;
7846 Overridden_Subp := Candidate;
7847 return;
7848 end;
7849 end Check_Synchronized_Overriding;
7851 ---------------------------
7852 -- Check_Type_Conformant --
7853 ---------------------------
7855 procedure Check_Type_Conformant
7856 (New_Id : Entity_Id;
7857 Old_Id : Entity_Id;
7858 Err_Loc : Node_Id := Empty)
7860 Result : Boolean;
7861 pragma Warnings (Off, Result);
7862 begin
7863 Check_Conformance
7864 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7865 end Check_Type_Conformant;
7867 -----------------------------
7868 -- Check_Untagged_Equality --
7869 -----------------------------
7871 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7872 Eq_Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7873 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7875 procedure Freezing_Point_Warning (N : Node_Id; S : String);
7876 -- Output a warning about the freezing point N of Typ
7878 function Is_Actual_Of_Instantiation
7879 (E : Entity_Id;
7880 Inst : Node_Id) return Boolean;
7881 -- Return True if E is an actual parameter of instantiation Inst
7883 -----------------------------------
7884 -- Output_Freezing_Point_Warning --
7885 -----------------------------------
7887 procedure Freezing_Point_Warning (N : Node_Id; S : String) is
7888 begin
7889 Error_Msg_String (1 .. S'Length) := S;
7890 Error_Msg_Strlen := S'Length;
7892 if Ada_Version >= Ada_2012 then
7893 Error_Msg_NE ("type& is frozen by ~??", N, Typ);
7894 Error_Msg_N
7895 ("\an equality operator cannot be declared after this point??",
7898 else
7899 Error_Msg_NE ("type& is frozen by ~ (Ada 2012)?y?", N, Typ);
7900 Error_Msg_N
7901 ("\an equality operator cannot be declared after this point"
7902 & " (Ada 2012)?y?", N);
7903 end if;
7904 end Freezing_Point_Warning;
7906 --------------------------------
7907 -- Is_Actual_Of_Instantiation --
7908 --------------------------------
7910 function Is_Actual_Of_Instantiation
7911 (E : Entity_Id;
7912 Inst : Node_Id) return Boolean
7914 Assoc : Node_Id;
7916 begin
7917 if Present (Generic_Associations (Inst)) then
7918 Assoc := First (Generic_Associations (Inst));
7920 while Present (Assoc) loop
7921 if Present (Explicit_Generic_Actual_Parameter (Assoc))
7922 and then
7923 Is_Entity_Name (Explicit_Generic_Actual_Parameter (Assoc))
7924 and then
7925 Entity (Explicit_Generic_Actual_Parameter (Assoc)) = E
7926 then
7927 return True;
7928 end if;
7930 Next (Assoc);
7931 end loop;
7932 end if;
7934 return False;
7935 end Is_Actual_Of_Instantiation;
7937 -- Local variable
7939 Decl : Node_Id;
7941 -- Start of processing for Check_Untagged_Equality
7943 begin
7944 -- This check applies only if we have a subprogram declaration or a
7945 -- subprogram body that is not a completion, for an untagged record
7946 -- type, and that is conformant with the predefined operator.
7948 if (Nkind (Eq_Decl) /= N_Subprogram_Declaration
7949 and then not (Nkind (Eq_Decl) = N_Subprogram_Body
7950 and then Acts_As_Spec (Eq_Decl)))
7951 or else not Is_Record_Type (Typ)
7952 or else Is_Tagged_Type (Typ)
7953 or else not Is_User_Defined_Equality (Eq_Op)
7954 then
7955 return;
7956 end if;
7958 -- In Ada 2012 case, we will output errors or warnings depending on
7959 -- the setting of debug flag -gnatd.E.
7961 if Ada_Version >= Ada_2012 then
7962 Error_Msg_Warn := Debug_Flag_Dot_EE;
7964 -- In earlier versions of Ada, nothing to do unless we are warning on
7965 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7967 else
7968 if not Warn_On_Ada_2012_Compatibility then
7969 return;
7970 end if;
7971 end if;
7973 -- Cases where the type has already been frozen
7975 if Is_Frozen (Typ) then
7977 -- The check applies to a primitive operation, so check that type
7978 -- and equality operation are in the same scope.
7980 if Scope (Typ) /= Current_Scope then
7981 return;
7983 -- If the type is a generic actual (sub)type, the operation is not
7984 -- primitive either because the base type is declared elsewhere.
7986 elsif Is_Generic_Actual_Type (Typ) then
7987 return;
7989 -- Here we may have an error of declaration after freezing, but we
7990 -- must make sure not to flag the equality operator itself causing
7991 -- the freezing when it is a subprogram body.
7993 else
7994 Decl := Next (Declaration_Node (Typ));
7996 while Present (Decl) and then Decl /= Eq_Decl loop
7998 -- The declaration of an object of the type
8000 if Nkind (Decl) = N_Object_Declaration
8001 and then Etype (Defining_Identifier (Decl)) = Typ
8002 then
8003 Freezing_Point_Warning (Decl, "declaration");
8004 exit;
8006 -- The instantiation of a generic on the type
8008 elsif Nkind (Decl) in N_Generic_Instantiation
8009 and then Is_Actual_Of_Instantiation (Typ, Decl)
8010 then
8011 Freezing_Point_Warning (Decl, "instantiation");
8012 exit;
8014 -- A noninstance proper body, body stub or entry body
8016 elsif Nkind (Decl) in N_Proper_Body
8017 | N_Body_Stub
8018 | N_Entry_Body
8019 and then not Is_Generic_Instance (Defining_Entity (Decl))
8020 then
8021 Freezing_Point_Warning (Decl, "body");
8022 exit;
8024 -- If we have reached the freeze node and immediately after we
8025 -- have the body or generated code for the body, then it is the
8026 -- body that caused the freezing and this is legal.
8028 elsif Nkind (Decl) = N_Freeze_Entity
8029 and then Entity (Decl) = Typ
8030 and then (Next (Decl) = Eq_Decl
8031 or else
8032 Sloc (Next (Decl)) = Sloc (Eq_Decl))
8033 then
8034 return;
8035 end if;
8037 Next (Decl);
8038 end loop;
8040 -- Here we have a definite error of declaration after freezing
8042 if Ada_Version >= Ada_2012 then
8043 Error_Msg_NE
8044 ("equality operator must be declared before type & is "
8045 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8047 -- In Ada 2012 mode with error turned to warning, output one
8048 -- more warning to warn that the equality operation may not
8049 -- compose. This is the consequence of ignoring the error.
8051 if Error_Msg_Warn then
8052 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8053 end if;
8055 else
8056 Error_Msg_NE
8057 ("equality operator must be declared before type& is "
8058 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8059 end if;
8061 -- If we have found no freezing point and the declaration of the
8062 -- operator could not be reached from that of the type and we are
8063 -- in a package body, this must be because the type is declared
8064 -- in the spec of the package. Add a message tailored to this.
8066 if No (Decl) and then In_Package_Body (Scope (Typ)) then
8067 if Ada_Version >= Ada_2012 then
8068 if Nkind (Eq_Decl) = N_Subprogram_Body then
8069 Error_Msg_N
8070 ("\put declaration in package spec<<", Eq_Op);
8071 else
8072 Error_Msg_N
8073 ("\move declaration to package spec<<", Eq_Op);
8074 end if;
8076 else
8077 if Nkind (Eq_Decl) = N_Subprogram_Body then
8078 Error_Msg_N
8079 ("\put declaration in package spec (Ada 2012)?y?",
8080 Eq_Op);
8081 else
8082 Error_Msg_N
8083 ("\move declaration to package spec (Ada 2012)?y?",
8084 Eq_Op);
8085 end if;
8086 end if;
8087 end if;
8088 end if;
8090 -- Now check for AI12-0352: the declaration of a user-defined primitive
8091 -- equality operation for a record type T is illegal if it occurs after
8092 -- a type has been derived from T.
8094 else
8095 Decl := Next (Declaration_Node (Typ));
8097 while Present (Decl) and then Decl /= Eq_Decl loop
8098 if Nkind (Decl) = N_Full_Type_Declaration
8099 and then Etype (Defining_Identifier (Decl)) = Typ
8100 then
8101 Error_Msg_N
8102 ("equality operator cannot appear after derivation", Eq_Op);
8103 Error_Msg_NE
8104 ("an equality operator for& cannot be declared after "
8105 & "this point??",
8106 Decl, Typ);
8107 end if;
8109 Next (Decl);
8110 end loop;
8111 end if;
8112 end Check_Untagged_Equality;
8114 ---------------------------
8115 -- Can_Override_Operator --
8116 ---------------------------
8118 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
8119 Typ : Entity_Id;
8121 begin
8122 -- Return False if not an operator. We test the name rather than testing
8123 -- that the Nkind is N_Defining_Operator_Symbol, because there are cases
8124 -- where an operator entity can be an N_Defining_Identifier (such as for
8125 -- function instantiations).
8127 if Chars (Subp) not in Any_Operator_Name then
8128 return False;
8130 else
8131 Typ := Base_Type (Etype (First_Formal (Subp)));
8133 -- Check explicitly that the operation is a primitive of the type
8135 return Operator_Matches_Spec (Subp, Subp)
8136 and then not Is_Generic_Type (Typ)
8137 and then Scope (Subp) = Scope (Typ)
8138 and then not Is_Class_Wide_Type (Typ);
8139 end if;
8140 end Can_Override_Operator;
8142 ----------------------
8143 -- Conforming_Types --
8144 ----------------------
8146 function Conforming_Types
8147 (T1 : Entity_Id;
8148 T2 : Entity_Id;
8149 Ctype : Conformance_Type;
8150 Get_Inst : Boolean := False) return Boolean
8152 function Base_Types_Match
8153 (Typ_1 : Entity_Id;
8154 Typ_2 : Entity_Id) return Boolean;
8155 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
8156 -- in different scopes (e.g. parent and child instances), then verify
8157 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
8158 -- the same subtype chain. The whole purpose of this procedure is to
8159 -- prevent spurious ambiguities in an instantiation that may arise if
8160 -- two distinct generic types are instantiated with the same actual.
8162 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
8163 -- An access parameter can designate an incomplete type. If the
8164 -- incomplete type is the limited view of a type from a limited_
8165 -- with_clause, check whether the non-limited view is available.
8166 -- If it is a (non-limited) incomplete type, get the full view.
8168 function Matches_Limited_With_View
8169 (Typ_1 : Entity_Id;
8170 Typ_2 : Entity_Id) return Boolean;
8171 -- Returns True if and only if either Typ_1 denotes a limited view of
8172 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
8173 -- the limited with view of a type is used in a subprogram declaration
8174 -- and the subprogram body is in the scope of a regular with clause for
8175 -- the same unit. In such a case, the two type entities are considered
8176 -- identical for purposes of conformance checking.
8178 ----------------------
8179 -- Base_Types_Match --
8180 ----------------------
8182 function Base_Types_Match
8183 (Typ_1 : Entity_Id;
8184 Typ_2 : Entity_Id) return Boolean
8186 Base_1 : constant Entity_Id := Base_Type (Typ_1);
8187 Base_2 : constant Entity_Id := Base_Type (Typ_2);
8189 begin
8190 if Typ_1 = Typ_2 then
8191 return True;
8193 elsif Base_1 = Base_2 then
8195 -- The following is too permissive. A more precise test should
8196 -- check that the generic actual is an ancestor subtype of the
8197 -- other ???.
8199 -- See code in Find_Corresponding_Spec that applies an additional
8200 -- filter to handle accidental amiguities in instances.
8202 return
8203 not Is_Generic_Actual_Type (Typ_1)
8204 or else not Is_Generic_Actual_Type (Typ_2)
8205 or else Scope (Typ_1) /= Scope (Typ_2);
8207 -- If Typ_2 is a generic actual type it is declared as the subtype of
8208 -- the actual. If that actual is itself a subtype we need to use its
8209 -- own base type to check for compatibility.
8211 elsif Ekind (Base_2) = Ekind (Typ_2)
8212 and then Base_1 = Base_Type (Base_2)
8213 then
8214 return True;
8216 elsif Ekind (Base_1) = Ekind (Typ_1)
8217 and then Base_2 = Base_Type (Base_1)
8218 then
8219 return True;
8221 else
8222 return False;
8223 end if;
8224 end Base_Types_Match;
8226 --------------------------
8227 -- Find_Designated_Type --
8228 --------------------------
8230 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
8231 Desig : Entity_Id;
8233 begin
8234 Desig := Directly_Designated_Type (Typ);
8236 if Ekind (Desig) = E_Incomplete_Type then
8238 -- If regular incomplete type, get full view if available
8240 if Present (Full_View (Desig)) then
8241 Desig := Full_View (Desig);
8243 -- If limited view of a type, get non-limited view if available,
8244 -- and check again for a regular incomplete type.
8246 elsif Present (Non_Limited_View (Desig)) then
8247 Desig := Get_Full_View (Non_Limited_View (Desig));
8248 end if;
8249 end if;
8251 return Desig;
8252 end Find_Designated_Type;
8254 -------------------------------
8255 -- Matches_Limited_With_View --
8256 -------------------------------
8258 function Matches_Limited_With_View
8259 (Typ_1 : Entity_Id;
8260 Typ_2 : Entity_Id) return Boolean
8262 function Is_Matching_Limited_View
8263 (Typ : Entity_Id;
8264 View : Entity_Id) return Boolean;
8265 -- Determine whether non-limited view View denotes type Typ in some
8266 -- conformant fashion.
8268 ------------------------------
8269 -- Is_Matching_Limited_View --
8270 ------------------------------
8272 function Is_Matching_Limited_View
8273 (Typ : Entity_Id;
8274 View : Entity_Id) return Boolean
8276 Root_Typ : Entity_Id;
8277 Root_View : Entity_Id;
8279 begin
8280 -- The non-limited view directly denotes the type
8282 if Typ = View then
8283 return True;
8285 -- The type is a subtype of the non-limited view
8287 elsif Is_Subtype_Of (Typ, View) then
8288 return True;
8290 -- Both the non-limited view and the type denote class-wide types
8292 elsif Is_Class_Wide_Type (Typ)
8293 and then Is_Class_Wide_Type (View)
8294 then
8295 Root_Typ := Root_Type (Typ);
8296 Root_View := Root_Type (View);
8298 if Root_Typ = Root_View then
8299 return True;
8301 -- An incomplete tagged type and its full view may receive two
8302 -- distinct class-wide types when the related package has not
8303 -- been analyzed yet.
8305 -- package Pack is
8306 -- type T is tagged; -- CW_1
8307 -- type T is tagged null record; -- CW_2
8308 -- end Pack;
8310 -- This is because the package lacks any semantic information
8311 -- that may eventually link both views of T. As a consequence,
8312 -- a client of the limited view of Pack will see CW_2 while a
8313 -- client of the non-limited view of Pack will see CW_1.
8315 elsif Is_Incomplete_Type (Root_Typ)
8316 and then Present (Full_View (Root_Typ))
8317 and then Full_View (Root_Typ) = Root_View
8318 then
8319 return True;
8321 elsif Is_Incomplete_Type (Root_View)
8322 and then Present (Full_View (Root_View))
8323 and then Full_View (Root_View) = Root_Typ
8324 then
8325 return True;
8326 end if;
8327 end if;
8329 return False;
8330 end Is_Matching_Limited_View;
8332 -- Start of processing for Matches_Limited_With_View
8334 begin
8335 -- In some cases a type imported through a limited_with clause, and
8336 -- its non-limited view are both visible, for example in an anonymous
8337 -- access-to-class-wide type in a formal, or when building the body
8338 -- for a subprogram renaming after the subprogram has been frozen.
8339 -- In these cases both entities designate the same type. In addition,
8340 -- if one of them is an actual in an instance, it may be a subtype of
8341 -- the non-limited view of the other.
8343 if From_Limited_With (Typ_1)
8344 and then From_Limited_With (Typ_2)
8345 and then Available_View (Typ_1) = Available_View (Typ_2)
8346 then
8347 return True;
8349 elsif From_Limited_With (Typ_1) then
8350 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8352 elsif From_Limited_With (Typ_2) then
8353 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8355 else
8356 return False;
8357 end if;
8358 end Matches_Limited_With_View;
8360 -- Local variables
8362 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8364 Type_1 : Entity_Id := T1;
8365 Type_2 : Entity_Id := T2;
8367 -- Start of processing for Conforming_Types
8369 begin
8370 -- The context is an instance association for a formal access-to-
8371 -- subprogram type; the formal parameter types require mapping because
8372 -- they may denote other formal parameters of the generic unit.
8374 if Get_Inst then
8375 Type_1 := Get_Instance_Of (T1);
8376 Type_2 := Get_Instance_Of (T2);
8377 end if;
8379 -- If one of the types is a view of the other introduced by a limited
8380 -- with clause, treat these as conforming for all purposes.
8382 if Matches_Limited_With_View (T1, T2) then
8383 return True;
8385 elsif Base_Types_Match (Type_1, Type_2) then
8386 if Ctype <= Mode_Conformant then
8387 return True;
8389 else
8390 return
8391 Subtypes_Statically_Match (Type_1, Type_2)
8392 and then Dimensions_Match (Type_1, Type_2);
8393 end if;
8395 elsif Is_Incomplete_Or_Private_Type (Type_1)
8396 and then Present (Full_View (Type_1))
8397 and then Base_Types_Match (Full_View (Type_1), Type_2)
8398 then
8399 return
8400 Ctype <= Mode_Conformant
8401 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8403 elsif Ekind (Type_2) = E_Incomplete_Type
8404 and then Present (Full_View (Type_2))
8405 and then Base_Types_Match (Type_1, Full_View (Type_2))
8406 then
8407 return
8408 Ctype <= Mode_Conformant
8409 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8411 -- The subtype declared for the formal type in an instantiation and the
8412 -- actual type are conforming. Note that testing Is_Generic_Actual_Type
8413 -- here is not sufficient because the flag is only set in the bodies of
8414 -- instances, which is too late for formal subprograms.
8416 elsif Ekind (Type_2) = E_Private_Subtype
8417 and then Etype (Type_2) = Type_1
8418 and then Present (Generic_Parent_Type (Declaration_Node (Type_2)))
8419 then
8420 return True;
8422 -- In Ada 2012, incomplete types (including limited views) can appear
8423 -- as actuals in instantiations, where they are conformant to the
8424 -- corresponding incomplete formal.
8426 elsif Is_Incomplete_Type (Type_1)
8427 and then Is_Incomplete_Type (Type_2)
8428 and then In_Instance
8429 and then (Used_As_Generic_Actual (Type_1)
8430 or else Used_As_Generic_Actual (Type_2))
8431 then
8432 return True;
8433 end if;
8435 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8436 -- treated recursively because they carry a signature. As far as
8437 -- conformance is concerned, convention plays no role, and either
8438 -- or both could be access to protected subprograms.
8440 Are_Anonymous_Access_To_Subprogram_Types :=
8441 Ekind (Type_1) in E_Anonymous_Access_Subprogram_Type
8442 | E_Anonymous_Access_Protected_Subprogram_Type
8443 and then
8444 Ekind (Type_2) in E_Anonymous_Access_Subprogram_Type
8445 | E_Anonymous_Access_Protected_Subprogram_Type;
8447 -- Test anonymous access type case. For this case, static subtype
8448 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8449 -- the base types because we may have built internal subtype entities
8450 -- to handle null-excluding types (see Process_Formals).
8452 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8453 and then
8454 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8456 -- Ada 2005 (AI-254)
8458 or else Are_Anonymous_Access_To_Subprogram_Types
8459 then
8460 declare
8461 Desig_1 : Entity_Id;
8462 Desig_2 : Entity_Id;
8464 begin
8465 -- In Ada 2005, access constant indicators must match for
8466 -- subtype conformance.
8468 if Ada_Version >= Ada_2005
8469 and then Ctype >= Subtype_Conformant
8470 and then
8471 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8472 then
8473 return False;
8474 end if;
8476 Desig_1 := Find_Designated_Type (Type_1);
8477 Desig_2 := Find_Designated_Type (Type_2);
8479 -- If the context is an instance association for a formal
8480 -- access-to-subprogram type; formal access parameter designated
8481 -- types require mapping because they may denote other formal
8482 -- parameters of the generic unit.
8484 if Get_Inst then
8485 Desig_1 := Get_Instance_Of (Desig_1);
8486 Desig_2 := Get_Instance_Of (Desig_2);
8487 end if;
8489 -- It is possible for a Class_Wide_Type to be introduced for an
8490 -- incomplete type, in which case there is a separate class_ wide
8491 -- type for the full view. The types conform if their Etypes
8492 -- conform, i.e. one may be the full view of the other. This can
8493 -- only happen in the context of an access parameter, other uses
8494 -- of an incomplete Class_Wide_Type are illegal.
8496 if Is_Class_Wide_Type (Desig_1)
8497 and then
8498 Is_Class_Wide_Type (Desig_2)
8499 then
8500 return
8501 Conforming_Types
8502 (Etype (Base_Type (Desig_1)),
8503 Etype (Base_Type (Desig_2)), Ctype);
8505 elsif Are_Anonymous_Access_To_Subprogram_Types then
8506 if Ada_Version < Ada_2005 then
8507 return
8508 Ctype = Type_Conformant
8509 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8511 -- We must check the conformance of the signatures themselves
8513 else
8514 declare
8515 Conformant : Boolean;
8516 begin
8517 Check_Conformance
8518 (Desig_1, Desig_2, Ctype, False, Conformant);
8519 return Conformant;
8520 end;
8521 end if;
8523 -- A limited view of an actual matches the corresponding
8524 -- incomplete formal.
8526 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8527 and then From_Limited_With (Desig_2)
8528 and then Used_As_Generic_Actual (Etype (Desig_2))
8529 then
8530 return True;
8532 else
8533 return Base_Type (Desig_1) = Base_Type (Desig_2)
8534 and then (Ctype = Type_Conformant
8535 or else
8536 Subtypes_Statically_Match (Desig_1, Desig_2));
8537 end if;
8538 end;
8540 -- Otherwise definitely no match
8542 else
8543 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8544 and then Is_Access_Type (Type_2))
8545 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8546 and then Is_Access_Type (Type_1)))
8547 and then
8548 Conforming_Types
8549 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8550 then
8551 May_Hide_Profile := True;
8552 end if;
8554 return False;
8555 end if;
8556 end Conforming_Types;
8558 --------------------------
8559 -- Create_Extra_Formals --
8560 --------------------------
8562 procedure Create_Extra_Formals (E : Entity_Id) is
8563 First_Extra : Entity_Id := Empty;
8564 Formal : Entity_Id;
8565 Last_Extra : Entity_Id := Empty;
8567 function Add_Extra_Formal
8568 (Assoc_Entity : Entity_Id;
8569 Typ : Entity_Id;
8570 Scope : Entity_Id;
8571 Suffix : String) return Entity_Id;
8572 -- Add an extra formal to the current list of formals and extra formals.
8573 -- The extra formal is added to the end of the list of extra formals,
8574 -- and also returned as the result. These formals are always of mode IN.
8575 -- The new formal has the type Typ, is declared in Scope, and its name
8576 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8577 -- The following suffixes are currently used. They should not be changed
8578 -- without coordinating with CodePeer, which makes use of these to
8579 -- provide better messages.
8581 -- O denotes the Constrained bit.
8582 -- L denotes the accessibility level.
8583 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8584 -- the full list in exp_ch6.BIP_Formal_Kind.
8586 function Has_Extra_Formals (E : Entity_Id) return Boolean;
8587 -- Determines if E has its extra formals
8589 function Might_Need_BIP_Task_Actuals (E : Entity_Id) return Boolean;
8590 -- Determines if E is a dispatching primitive returning a limited tagged
8591 -- type object since some descendant might return an object with tasks
8592 -- (and therefore need the BIP task extra actuals).
8594 function Needs_Accessibility_Check_Extra
8595 (E : Entity_Id;
8596 Formal : Node_Id) return Boolean;
8597 -- Determines whether the given formal of E needs an extra formal for
8598 -- supporting accessibility checking. Returns True for both anonymous
8599 -- access formals and formals of named access types that are marked as
8600 -- controlling formals. The latter case can occur when the subprogram
8601 -- Expand_Dispatching_Call creates a subprogram-type and substitutes
8602 -- the types of access-to-class-wide actuals for the anonymous access-
8603 -- to-specific-type of controlling formals.
8605 function Parent_Subprogram (Subp_Id : Entity_Id) return Entity_Id;
8606 -- Subp_Id is a subprogram of a derived type; return its parent
8607 -- subprogram if Subp_Id overrides a parent primitive or derives
8608 -- from a parent primitive, and such parent primitive can have extra
8609 -- formals. Otherwise return Empty.
8611 ----------------------
8612 -- Add_Extra_Formal --
8613 ----------------------
8615 function Add_Extra_Formal
8616 (Assoc_Entity : Entity_Id;
8617 Typ : Entity_Id;
8618 Scope : Entity_Id;
8619 Suffix : String) return Entity_Id
8621 EF : Entity_Id;
8623 begin
8624 -- A little optimization. Never generate an extra formal for the
8625 -- _init operand of an initialization procedure, since it could
8626 -- never be used.
8628 if Chars (Formal) = Name_uInit then
8629 return Empty;
8630 end if;
8632 EF := Make_Defining_Identifier (Sloc (Assoc_Entity),
8633 Chars => New_External_Name (Chars (Assoc_Entity),
8634 Suffix => Suffix));
8636 Mutate_Ekind (EF, E_In_Parameter);
8637 Set_Actual_Subtype (EF, Typ);
8638 Set_Etype (EF, Typ);
8639 Set_Scope (EF, Scope);
8640 Set_Mechanism (EF, Default_Mechanism);
8641 Set_Formal_Validity (EF);
8643 if No (First_Extra) then
8644 First_Extra := EF;
8645 Set_Extra_Formals (Scope, EF);
8646 end if;
8648 if Present (Last_Extra) then
8649 Set_Extra_Formal (Last_Extra, EF);
8650 end if;
8652 Last_Extra := EF;
8654 return EF;
8655 end Add_Extra_Formal;
8657 -----------------------
8658 -- Has_Extra_Formals --
8659 -----------------------
8661 function Has_Extra_Formals (E : Entity_Id) return Boolean is
8662 begin
8663 return Present (Extra_Formals (E))
8664 or else
8665 (Ekind (E) = E_Function
8666 and then Present (Extra_Accessibility_Of_Result (E)));
8667 end Has_Extra_Formals;
8669 ---------------------------------
8670 -- Might_Need_BIP_Task_Actuals --
8671 ---------------------------------
8673 function Might_Need_BIP_Task_Actuals (E : Entity_Id) return Boolean is
8674 Subp_Id : Entity_Id;
8675 Func_Typ : Entity_Id;
8677 begin
8678 if Global_No_Tasking or else No_Run_Time_Mode then
8679 return False;
8680 end if;
8682 -- No further check needed if we know that BIP task actuals are
8683 -- required.
8685 if Needs_BIP_Task_Actuals (E) then
8686 return True;
8687 end if;
8689 -- For thunks we must rely on their target entity
8691 if Is_Thunk (E) then
8692 Subp_Id := Thunk_Target (E);
8694 -- For protected subprograms we rely on the subprogram which
8695 -- implements the body of the operation (since it is the entity
8696 -- that may be a dispatching operation).
8698 elsif Is_Protected_Type (Scope (E))
8699 and then Present (Protected_Body_Subprogram (E))
8700 then
8701 Subp_Id := Protected_Body_Subprogram (E);
8703 else
8704 Subp_Id := E;
8705 end if;
8707 -- We check the root type of the return type since the same
8708 -- decision must be taken for all descendants overriding a
8709 -- dispatching operation.
8711 Func_Typ := Root_Type (Underlying_Type (Etype (Subp_Id)));
8713 return Ekind (Subp_Id) = E_Function
8714 and then not Has_Foreign_Convention (Func_Typ)
8715 and then Is_Dispatching_Operation (Subp_Id)
8716 and then Is_Tagged_Type (Func_Typ)
8717 and then Is_Limited_Type (Func_Typ)
8718 and then not Has_Aspect (Func_Typ, Aspect_No_Task_Parts);
8719 end Might_Need_BIP_Task_Actuals;
8721 -------------------------------------
8722 -- Needs_Accessibility_Check_Extra --
8723 -------------------------------------
8725 function Needs_Accessibility_Check_Extra
8726 (E : Entity_Id;
8727 Formal : Node_Id) return Boolean is
8729 begin
8730 -- For dispatching operations this extra formal is not suppressed
8731 -- since all the derivations must have matching formals.
8733 -- For nondispatching operations it is suppressed if we specifically
8734 -- suppress accessibility checks at the package level for either the
8735 -- subprogram, or the package in which it resides. However, we do
8736 -- not suppress it simply if the scope has accessibility checks
8737 -- suppressed, since this could cause trouble when clients are
8738 -- compiled with a different suppression setting. The explicit checks
8739 -- at the package level are safe from this point of view.
8741 if not Is_Dispatching_Operation (E)
8742 and then
8743 (Explicit_Suppress (E, Accessibility_Check)
8744 or else Explicit_Suppress (Scope (E), Accessibility_Check))
8745 then
8746 return False;
8747 end if;
8749 -- Base_Type is applied to handle cases where there is a null
8750 -- exclusion the formal may have an access subtype.
8752 return
8753 Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8754 or else
8755 (Is_Controlling_Formal (Formal)
8756 and then Is_Access_Type (Base_Type (Etype (Formal))));
8757 end Needs_Accessibility_Check_Extra;
8759 -----------------------
8760 -- Parent_Subprogram --
8761 -----------------------
8763 function Parent_Subprogram (Subp_Id : Entity_Id) return Entity_Id is
8764 pragma Assert (not Is_Thunk (Subp_Id));
8765 Ovr_E : Entity_Id := Overridden_Operation (Subp_Id);
8766 Ovr_Alias : Entity_Id;
8768 begin
8769 if Present (Ovr_E)
8770 and then Ekind (Ovr_E) = E_Enumeration_Literal
8771 then
8772 Ovr_E := Empty;
8774 elsif Present (Ovr_E) then
8775 Ovr_Alias := Ultimate_Alias (Ovr_E);
8777 -- There is no real overridden subprogram if there is a mutual
8778 -- reference between the E and its overridden operation. This
8779 -- weird scenery occurs in the following cases:
8781 -- 1) Controlling function wrappers internally built by
8782 -- Make_Controlling_Function_Wrappers.
8784 -- 2) Hidden overridden primitives of type extensions or private
8785 -- extensions (cf. Find_Hidden_Overridden_Primitive). These
8786 -- hidden primitives have suffix 'P'.
8788 -- 3) Overriding primitives of stub types (see the subprogram
8789 -- Add_RACW_Primitive_Declarations_And_Bodies).
8791 if Ovr_Alias = Subp_Id then
8792 pragma Assert
8793 ((Is_Wrapper (Subp_Id)
8794 and then Has_Controlling_Result (Subp_Id))
8795 or else Has_Suffix (Ovr_E, 'P')
8796 or else Is_RACW_Stub_Type
8797 (Find_Dispatching_Type (Subp_Id))
8798 or else No (Overridden_Operation (Ovr_E)));
8800 if Present (Overridden_Operation (Ovr_E)) then
8801 Ovr_E := Overridden_Operation (Ovr_E);
8803 -- Ovr_E is an internal entity built by Derive_Subprogram and
8804 -- we have no direct way to climb to the corresponding parent
8805 -- subprogram but this internal entity has the extra formals
8806 -- (if any) required for the purpose of checking the extra
8807 -- formals of Subp_Id.
8809 else
8810 pragma Assert (not Comes_From_Source (Ovr_E));
8811 end if;
8813 -- Use as our reference entity the ultimate renaming of the
8814 -- overridden subprogram.
8816 elsif Present (Alias (Ovr_E)) then
8817 pragma Assert (No (Overridden_Operation (Ovr_Alias))
8818 or else Overridden_Operation (Ovr_Alias) /= Ovr_E);
8820 Ovr_E := Ovr_Alias;
8821 end if;
8822 end if;
8824 if Present (Ovr_E) and then Has_Reliable_Extra_Formals (Ovr_E) then
8825 return Ovr_E;
8826 else
8827 return Empty;
8828 end if;
8829 end Parent_Subprogram;
8831 -- Local variables
8833 Formal_Type : Entity_Id;
8834 May_Have_Alias : Boolean;
8835 Alias_Formal : Entity_Id := Empty;
8836 Alias_Subp : Entity_Id := Empty;
8837 Parent_Formal : Entity_Id := Empty;
8838 Parent_Subp : Entity_Id := Empty;
8839 Ref_E : Entity_Id;
8841 -- Start of processing for Create_Extra_Formals
8843 begin
8844 pragma Assert (Is_Subprogram_Or_Entry (E)
8845 or else Ekind (E) in E_Subprogram_Type);
8847 -- We never generate extra formals if expansion is not active because we
8848 -- don't need them unless we are generating code.
8850 if not Expander_Active then
8851 return;
8853 -- Enumeration literals have no extra formal; this case occurs when
8854 -- a function renames it.
8856 elsif Ekind (E) = E_Function
8857 and then Ekind (Ultimate_Alias (E)) = E_Enumeration_Literal
8858 then
8859 return;
8861 -- Extra formals of Initialization procedures are added by the function
8862 -- Exp_Ch3.Init_Formals
8864 elsif Is_Init_Proc (E) then
8865 return;
8867 -- No need to generate extra formals in thunks whose target has no extra
8868 -- formals, but we can have two of them chained (interface and stack).
8870 elsif Is_Thunk (E) and then No (Extra_Formals (Thunk_Target (E))) then
8871 return;
8873 -- If Extra_Formals were already created, don't do it again. This
8874 -- situation may arise for subprogram types created as part of
8875 -- dispatching calls (see Expand_Dispatching_Call).
8877 elsif Has_Extra_Formals (E) then
8878 return;
8880 -- Extra formals of renamings of generic actual subprograms and
8881 -- renamings of instances of generic subprograms are shared. The
8882 -- check performed on the last formal is required to ensure that
8883 -- this is the renaming built by Analyze_Instance_And_Renamings
8884 -- (which shares all the formals); otherwise this would be wrong.
8886 elsif Ekind (E) in E_Function | E_Procedure
8887 and then Is_Generic_Instance (E)
8888 and then Present (Alias (E))
8889 and then Last_Formal (Ultimate_Alias (E)) = Last_Formal (E)
8890 then
8891 pragma Assert (Is_Generic_Instance (E)
8892 = Is_Generic_Instance (Ultimate_Alias (E)));
8894 Create_Extra_Formals (Ultimate_Alias (E));
8896 -- Share the extra formals
8898 Set_Extra_Formals (E, Extra_Formals (Ultimate_Alias (E)));
8900 if Ekind (E) = E_Function then
8901 Set_Extra_Accessibility_Of_Result (E,
8902 Extra_Accessibility_Of_Result (Ultimate_Alias (E)));
8903 end if;
8905 pragma Assert (Extra_Formals_OK (E));
8906 return;
8907 end if;
8909 -- Locate the last formal; required by Add_Extra_Formal.
8911 Formal := First_Formal (E);
8912 while Present (Formal) loop
8913 Last_Extra := Formal;
8914 Next_Formal (Formal);
8915 end loop;
8917 -- We rely on three entities to ensure consistency of extra formals of
8918 -- entity E:
8920 -- 1. A reference entity (Ref_E). For thunks it is their target
8921 -- primitive since this ensures that they have exactly the
8922 -- same extra formals; otherwise it is the identity.
8924 -- 2. The parent subprogram; only for derived types and references
8925 -- either the overridden subprogram or the internal entity built
8926 -- by Derive_Subprogram that has the extra formals of the parent
8927 -- subprogram; otherwise it is Empty. This entity ensures matching
8928 -- extra formals in derived types.
8930 -- 3. For renamings, their ultimate alias; this ensures taking the
8931 -- same decision in all the renamings (independently of the Ada
8932 -- mode on which they are compiled). For example:
8934 -- pragma Ada_2012;
8935 -- function Id_A (I : access Integer) return access Integer;
8937 -- pragma Ada_2005;
8938 -- function Id_B (I : access Integer) return access Integer
8939 -- renames Id_A;
8941 if Is_Thunk (E) then
8942 Ref_E := Thunk_Target (E);
8943 else
8944 Ref_E := E;
8945 end if;
8947 if Is_Subprogram (Ref_E) then
8948 Parent_Subp := Parent_Subprogram (Ref_E);
8949 end if;
8951 May_Have_Alias :=
8952 (Is_Subprogram (Ref_E) or else Ekind (Ref_E) = E_Subprogram_Type);
8954 -- If the parent subprogram is available then its ultimate alias of
8955 -- Ref_E is not needed since it will not be used to check its extra
8956 -- formals.
8958 if No (Parent_Subp)
8959 and then May_Have_Alias
8960 and then Present (Alias (Ref_E))
8961 and then Has_Reliable_Extra_Formals (Ultimate_Alias (Ref_E))
8962 then
8963 Alias_Subp := Ultimate_Alias (Ref_E);
8964 end if;
8966 -- Cannot add extra formals to subprograms and access types that have
8967 -- foreign convention nor to subprograms overriding primitives that
8968 -- have foreign convention since the foreign language does not know
8969 -- how to handle these extra formals; same for renamings of entities
8970 -- with foreign convention.
8972 if Has_Foreign_Convention (Ref_E)
8973 or else (Present (Alias_Subp)
8974 and then Has_Foreign_Convention (Alias_Subp))
8975 then
8976 return;
8977 end if;
8979 -- If the subprogram is a predefined dispatching subprogram then don't
8980 -- generate any extra constrained or accessibility level formals. In
8981 -- general we suppress these for internal subprograms (by not calling
8982 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8983 -- generated stream attributes do get passed through because extra
8984 -- build-in-place formals are needed in some cases (limited 'Input).
8986 if Is_Predefined_Internal_Operation (E) then
8987 goto Test_For_Func_Result_Extras;
8988 end if;
8990 -- Process the formals relying on the formals of our reference entities:
8991 -- Parent_Formal, Alias_Formal and Formal. Notice that we don't use the
8992 -- formal of Ref_E; we must use the formal of E which is the entity to
8993 -- which we are adding the extra formals.
8995 -- If this is a derived subprogram then the subtypes of the parent
8996 -- subprogram's formal parameters will be used to determine the need
8997 -- for extra formals.
8999 if Present (Parent_Subp) then
9001 -- Ensure that the parent subprogram has all its extra formals.
9002 -- Required because its return type may have been a private or
9003 -- an incomplete type, and the extra formals were not added. We
9004 -- protect this call against the weird cases where the parent subp
9005 -- renames this primitive (documented in the body of the local
9006 -- function Parent_Subprogram).
9008 if Ultimate_Alias (Parent_Subp) /= Ref_E then
9009 Create_Extra_Formals (Parent_Subp);
9010 end if;
9012 Parent_Formal := First_Formal (Parent_Subp);
9014 -- For concurrent types, the controlling argument of a dispatching
9015 -- primitive implementing an interface primitive is implicit. For
9016 -- example:
9018 -- type Iface is protected interface;
9019 -- function Prim
9020 -- (Obj : Iface;
9021 -- Value : Integer) return Natural is abstract;
9023 -- protected type PO is new Iface with
9024 -- function Prim (Value : Integer) return Natural;
9025 -- end PO;
9027 if Convention (Ref_E) = Convention_Protected
9028 and then Is_Abstract_Subprogram (Parent_Subp)
9029 and then Is_Interface (Find_Dispatching_Type (Parent_Subp))
9030 then
9031 Parent_Formal := Next_Formal (Parent_Formal);
9033 -- This is the nondispatching subprogram of a concurrent type
9034 -- that overrides the interface primitive; the expander will
9035 -- create the dispatching primitive (without Convention_Protected)
9036 -- with all the matching formals (see exp_ch9.Build_Wrapper_Specs)
9038 pragma Assert (not Is_Dispatching_Operation (Ref_E));
9039 end if;
9041 -- Ensure that the ultimate alias has all its extra formals
9043 elsif Present (Alias_Subp) then
9044 Create_Extra_Formals (Alias_Subp);
9045 Alias_Formal := First_Formal (Alias_Subp);
9046 end if;
9048 Formal := First_Formal (E);
9049 while Present (Formal) loop
9051 -- Here we establish our priority for deciding on the extra
9052 -- formals: 1) Parent primitive 2) Aliased primitive 3) Identity
9054 if Present (Parent_Formal) then
9055 Formal_Type := Etype (Parent_Formal);
9057 elsif Present (Alias_Formal) then
9058 Formal_Type := Etype (Alias_Formal);
9060 else
9061 Formal_Type := Etype (Formal);
9062 end if;
9064 -- Create extra formal for supporting the attribute 'Constrained.
9065 -- The case of a private type view without discriminants also
9066 -- requires the extra formal if the underlying type has defaulted
9067 -- discriminants.
9069 if Ekind (Formal) /= E_In_Parameter then
9071 -- Do not produce extra formals for Unchecked_Union parameters.
9072 -- Jump directly to the end of the loop.
9074 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
9075 goto Skip_Extra_Formal_Generation;
9076 end if;
9078 if not Has_Discriminants (Formal_Type)
9079 and then Is_Private_Type (Formal_Type)
9080 and then Present (Underlying_Type (Formal_Type))
9081 then
9082 Formal_Type := Underlying_Type (Formal_Type);
9083 end if;
9085 -- Suppress the extra formal if formal's subtype is constrained or
9086 -- indefinite, or we're compiling for Ada 2012 and the underlying
9087 -- type is tagged and limited. In Ada 2012, a limited tagged type
9088 -- can have defaulted discriminants, but 'Constrained is required
9089 -- to return True, so the formal is never needed (see AI05-0214).
9090 -- Note that this ensures consistency of calling sequences for
9091 -- dispatching operations when some types in a class have defaults
9092 -- on discriminants and others do not (and requiring the extra
9093 -- formal would introduce distributed overhead).
9095 -- If the type does not have a completion yet, treat as prior to
9096 -- Ada 2012 for consistency.
9098 if Has_Discriminants (Formal_Type)
9099 and then not Is_Constrained (Formal_Type)
9100 and then Is_Definite_Subtype (Formal_Type)
9101 and then (Ada_Version < Ada_2012
9102 or else No (Underlying_Type (Formal_Type))
9103 or else not
9104 (Is_Limited_Type (Formal_Type)
9105 and then
9106 Is_Tagged_Type
9107 (Underlying_Type (Formal_Type))))
9108 then
9109 Set_Extra_Constrained
9110 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
9111 end if;
9112 end if;
9114 -- Extra formal for supporting accessibility checking
9116 if Needs_Accessibility_Check_Extra (Ref_E, Formal) then
9117 pragma Assert (No (Parent_Formal)
9118 or else Present (Extra_Accessibility (Parent_Formal)));
9119 pragma Assert (No (Alias_Formal)
9120 or else Present (Extra_Accessibility (Alias_Formal)));
9122 Set_Extra_Accessibility
9123 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
9125 else
9126 pragma Assert (No (Parent_Formal)
9127 or else No (Extra_Accessibility (Parent_Formal)));
9128 pragma Assert (No (Alias_Formal)
9129 or else No (Extra_Accessibility (Alias_Formal)));
9130 end if;
9132 -- This label is required when skipping extra formal generation for
9133 -- Unchecked_Union parameters.
9135 <<Skip_Extra_Formal_Generation>>
9137 if Present (Parent_Formal) then
9138 Next_Formal (Parent_Formal);
9139 end if;
9141 if Present (Alias_Formal) then
9142 Next_Formal (Alias_Formal);
9143 end if;
9145 Next_Formal (Formal);
9146 end loop;
9148 <<Test_For_Func_Result_Extras>>
9150 -- Assume the worst case (Ada 2022) to evaluate this extra formal;
9151 -- required to ensure matching of extra formals between subprograms
9152 -- and access-to-subprogram types in projects with mixed Ada dialects.
9154 declare
9155 Save_Ada_Version : constant Ada_Version_Type := Ada_Version;
9157 begin
9158 Ada_Version := Ada_2022;
9160 if Needs_Result_Accessibility_Level (Ref_E)
9161 or else
9162 (Present (Parent_Subp)
9163 and then Needs_Result_Accessibility_Level (Parent_Subp))
9164 or else
9165 (Present (Alias_Subp)
9166 and then Needs_Result_Accessibility_Level (Alias_Subp))
9167 then
9168 Set_Extra_Accessibility_Of_Result (E,
9169 Add_Extra_Formal (E, Standard_Natural, E, "L"));
9170 end if;
9172 Ada_Version := Save_Ada_Version;
9173 end;
9175 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
9176 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
9178 if (Present (Parent_Subp) and then Has_BIP_Formals (Parent_Subp))
9179 or else
9180 (Present (Alias_Subp) and then Has_BIP_Formals (Alias_Subp))
9181 or else
9182 (Is_Build_In_Place_Function (Ref_E)
9183 and then Has_Reliable_Extra_Formals (Ref_E))
9184 then
9185 declare
9186 Result_Subt : constant Entity_Id := Etype (Ref_E);
9187 Formal_Typ : Entity_Id;
9188 Subp_Decl : Node_Id;
9189 Discard : Entity_Id;
9191 begin
9192 -- In the case of functions with unconstrained result subtypes,
9193 -- add a 4-state formal indicating whether the return object is
9194 -- allocated by the caller (1), or should be allocated by the
9195 -- callee on the secondary stack (2), in the global heap (3), or
9196 -- in a user-defined storage pool (4). For the moment we just use
9197 -- Natural for the type of this formal. Note that this formal
9198 -- isn't usually needed in the case where the result subtype is
9199 -- constrained, but it is needed when the function has a tagged
9200 -- result, because generally such functions can be called in a
9201 -- dispatching context and such calls must be handled like calls
9202 -- to a class-wide function.
9204 if Needs_BIP_Alloc_Form (Ref_E)
9205 or else
9206 (Present (Parent_Subp)
9207 and then Has_BIP_Extra_Formal (Parent_Subp, BIP_Alloc_Form,
9208 Must_Be_Frozen => False))
9209 or else
9210 (Present (Alias_Subp)
9211 and then Has_BIP_Extra_Formal (Alias_Subp, BIP_Alloc_Form,
9212 Must_Be_Frozen => False))
9213 then
9214 Discard :=
9215 Add_Extra_Formal
9216 (E, Standard_Natural,
9217 E, BIP_Formal_Suffix (BIP_Alloc_Form));
9219 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
9220 -- use a user-defined pool. This formal is not added on
9221 -- ZFP as those targets do not support pools.
9223 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
9224 Discard :=
9225 Add_Extra_Formal
9226 (E, RTE (RE_Root_Storage_Pool_Ptr),
9227 E, BIP_Formal_Suffix (BIP_Storage_Pool));
9228 end if;
9229 end if;
9231 -- In the case of functions whose result type needs finalization,
9232 -- add an extra formal which represents the finalization master.
9234 if Needs_BIP_Finalization_Master (Ref_E)
9235 or else
9236 (Present (Parent_Subp)
9237 and then Has_BIP_Extra_Formal (Parent_Subp,
9238 Kind => BIP_Finalization_Master,
9239 Must_Be_Frozen => False))
9240 or else
9241 (Present (Alias_Subp)
9242 and then Has_BIP_Extra_Formal (Alias_Subp,
9243 Kind => BIP_Finalization_Master,
9244 Must_Be_Frozen => False))
9245 then
9246 Discard :=
9247 Add_Extra_Formal
9248 (E, RTE (RE_Finalization_Master_Ptr),
9249 E, BIP_Formal_Suffix (BIP_Finalization_Master));
9250 end if;
9252 -- When the result type contains tasks, add two extra formals: the
9253 -- master of the tasks to be created, and the caller's activation
9254 -- chain.
9256 if Needs_BIP_Task_Actuals (Ref_E)
9257 or else Might_Need_BIP_Task_Actuals (Ref_E)
9258 or else
9259 (Present (Parent_Subp)
9260 and then Has_BIP_Extra_Formal (Parent_Subp, BIP_Task_Master,
9261 Must_Be_Frozen => False))
9262 or else
9263 (Present (Alias_Subp)
9264 and then Has_BIP_Extra_Formal (Alias_Subp, BIP_Task_Master,
9265 Must_Be_Frozen => False))
9266 then
9267 Discard :=
9268 Add_Extra_Formal
9269 (E, Standard_Integer,
9270 E, BIP_Formal_Suffix (BIP_Task_Master));
9272 if Needs_BIP_Task_Actuals (Ref_E) then
9273 Set_Has_Master_Entity (E);
9274 end if;
9276 Discard :=
9277 Add_Extra_Formal
9278 (E, RTE (RE_Activation_Chain_Access),
9279 E, BIP_Formal_Suffix (BIP_Activation_Chain));
9280 end if;
9282 -- All build-in-place functions get an extra formal that will be
9283 -- passed the address of the return object within the caller.
9285 Formal_Typ :=
9286 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
9288 -- Incomplete_View_From_Limited_With is needed here because
9289 -- gigi gets confused if the designated type is the full view
9290 -- coming from a limited-with'ed package. In the normal case,
9291 -- (no limited with) Incomplete_View_From_Limited_With
9292 -- returns Result_Subt.
9294 Set_Directly_Designated_Type
9295 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
9296 Set_Etype (Formal_Typ, Formal_Typ);
9297 Set_Depends_On_Private
9298 (Formal_Typ, Has_Private_Component (Formal_Typ));
9299 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
9300 Set_Is_Access_Constant (Formal_Typ, False);
9302 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
9303 -- the designated type comes from the limited view (for back-end
9304 -- purposes).
9306 Set_From_Limited_With
9307 (Formal_Typ, From_Limited_With (Result_Subt));
9309 Layout_Type (Formal_Typ);
9311 -- Force the definition of the Itype in case of internal function
9312 -- calls within the same or nested scope.
9314 if Is_Subprogram_Or_Generic_Subprogram (E)
9315 and then not Is_Compilation_Unit (E)
9316 then
9317 Subp_Decl := Parent (E);
9319 -- The insertion point for an Itype reference should be after
9320 -- the unit declaration node of the subprogram. An exception
9321 -- to this are inherited operations from a parent type in which
9322 -- case the derived type acts as their parent.
9324 if Nkind (Subp_Decl) in N_Function_Specification
9325 | N_Procedure_Specification
9326 then
9327 Subp_Decl := Parent (Subp_Decl);
9328 end if;
9330 Build_Itype_Reference (Formal_Typ, Subp_Decl);
9331 end if;
9333 Discard :=
9334 Add_Extra_Formal
9335 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
9336 end;
9337 end if;
9339 -- If this is an instance of a generic, we need to have extra formals
9340 -- for the Alias.
9342 if Is_Generic_Instance (E) and then Present (Alias (E)) then
9343 Set_Extra_Formals (Alias (E), Extra_Formals (E));
9344 end if;
9346 pragma Assert (No (Alias_Subp)
9347 or else Extra_Formals_Match_OK (E, Alias_Subp));
9349 pragma Assert (No (Parent_Subp)
9350 or else Extra_Formals_Match_OK (E, Parent_Subp));
9352 pragma Assert (Extra_Formals_OK (E));
9353 end Create_Extra_Formals;
9355 -----------------------------
9356 -- Enter_Overloaded_Entity --
9357 -----------------------------
9359 procedure Enter_Overloaded_Entity (S : Entity_Id) is
9360 function Matches_Predefined_Op return Boolean;
9361 -- This returns an approximation of whether S matches a predefined
9362 -- operator, based on the operator symbol, and the parameter and result
9363 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
9365 ---------------------------
9366 -- Matches_Predefined_Op --
9367 ---------------------------
9369 function Matches_Predefined_Op return Boolean is
9370 Formal_1 : constant Entity_Id := First_Formal (S);
9371 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
9372 Op : constant Name_Id := Chars (S);
9373 Result_Type : constant Entity_Id := Base_Type (Etype (S));
9374 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
9376 begin
9377 -- Binary operator
9379 if Present (Formal_2) then
9380 declare
9381 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
9383 begin
9384 -- All but "&" and "**" have same-types parameters
9386 case Op is
9387 when Name_Op_Concat
9388 | Name_Op_Expon
9390 null;
9392 when others =>
9393 if Type_1 /= Type_2 then
9394 return False;
9395 end if;
9396 end case;
9398 -- Check parameter and result types
9400 case Op is
9401 when Name_Op_And
9402 | Name_Op_Or
9403 | Name_Op_Xor
9405 return
9406 Is_Boolean_Type (Result_Type)
9407 and then Result_Type = Type_1;
9409 when Name_Op_Mod
9410 | Name_Op_Rem
9412 return
9413 Is_Integer_Type (Result_Type)
9414 and then Result_Type = Type_1;
9416 when Name_Op_Add
9417 | Name_Op_Divide
9418 | Name_Op_Multiply
9419 | Name_Op_Subtract
9421 return
9422 Is_Numeric_Type (Result_Type)
9423 and then Result_Type = Type_1;
9425 when Name_Op_Eq
9426 | Name_Op_Ne
9428 return
9429 Is_Boolean_Type (Result_Type)
9430 and then not Is_Limited_Type (Type_1);
9432 when Name_Op_Ge
9433 | Name_Op_Gt
9434 | Name_Op_Le
9435 | Name_Op_Lt
9437 return
9438 Is_Boolean_Type (Result_Type)
9439 and then (Is_Array_Type (Type_1)
9440 or else Is_Scalar_Type (Type_1));
9442 when Name_Op_Concat =>
9443 return Is_Array_Type (Result_Type);
9445 when Name_Op_Expon =>
9446 return
9447 (Is_Integer_Type (Result_Type)
9448 or else Is_Floating_Point_Type (Result_Type))
9449 and then Result_Type = Type_1
9450 and then Type_2 = Standard_Integer;
9452 when others =>
9453 raise Program_Error;
9454 end case;
9455 end;
9457 -- Unary operator
9459 else
9460 case Op is
9461 when Name_Op_Abs
9462 | Name_Op_Add
9463 | Name_Op_Subtract
9465 return
9466 Is_Numeric_Type (Result_Type)
9467 and then Result_Type = Type_1;
9469 when Name_Op_Not =>
9470 return
9471 Is_Boolean_Type (Result_Type)
9472 and then Result_Type = Type_1;
9474 when others =>
9475 raise Program_Error;
9476 end case;
9477 end if;
9478 end Matches_Predefined_Op;
9480 -- Local variables
9482 E : Entity_Id := Current_Entity_In_Scope (S);
9483 C_E : Entity_Id := Current_Entity (S);
9485 -- Start of processing for Enter_Overloaded_Entity
9487 begin
9488 if Present (E) then
9489 Set_Has_Homonym (E);
9490 Set_Has_Homonym (S);
9491 end if;
9493 Set_Is_Immediately_Visible (S);
9494 Set_Scope (S, Current_Scope);
9496 -- Chain new entity if front of homonym in current scope, so that
9497 -- homonyms are contiguous.
9499 if Present (E) and then E /= C_E then
9500 while Homonym (C_E) /= E loop
9501 C_E := Homonym (C_E);
9502 end loop;
9504 Set_Homonym (C_E, S);
9506 else
9507 E := C_E;
9508 Set_Current_Entity (S);
9509 end if;
9511 Set_Homonym (S, E);
9513 if Is_Inherited_Operation (S) then
9514 Append_Inherited_Subprogram (S);
9515 else
9516 Append_Entity (S, Current_Scope);
9517 end if;
9519 Set_Public_Status (S);
9521 if Debug_Flag_E then
9522 Write_Str ("New overloaded entity chain: ");
9523 Write_Name (Chars (S));
9525 E := S;
9526 while Present (E) loop
9527 Write_Str (" "); Write_Int (Int (E));
9528 E := Homonym (E);
9529 end loop;
9531 Write_Eol;
9532 end if;
9534 -- Generate warning for hiding
9536 if Warn_On_Hiding
9537 and then Comes_From_Source (S)
9538 and then In_Extended_Main_Source_Unit (S)
9539 then
9540 E := S;
9541 loop
9542 E := Homonym (E);
9543 exit when No (E);
9545 -- Warn unless genuine overloading. Do not emit warning on
9546 -- hiding predefined operators in Standard (these are either an
9547 -- artifact of our implicit declarations, or simple noise) but
9548 -- keep warning on a operator defined on a local subtype, because
9549 -- of the real danger that different operators may be applied in
9550 -- various parts of the program.
9552 -- Note that if E and S have the same scope, there is never any
9553 -- hiding. Either the two conflict, and the program is illegal,
9554 -- or S is overriding an implicit inherited subprogram.
9556 if Scope (E) /= Scope (S)
9557 and then (not Is_Overloadable (E)
9558 or else Subtype_Conformant (E, S))
9559 and then (Is_Immediately_Visible (E)
9560 or else Is_Potentially_Use_Visible (S))
9561 then
9562 if Scope (E) = Standard_Standard then
9563 if Nkind (S) = N_Defining_Operator_Symbol
9564 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
9565 Scope (S)
9566 and then Matches_Predefined_Op
9567 then
9568 Error_Msg_N
9569 ("declaration of & hides predefined operator?h?", S);
9570 end if;
9572 -- E not immediately within Standard
9574 else
9575 Error_Msg_Sloc := Sloc (E);
9576 Error_Msg_N ("declaration of & hides one #?h?", S);
9577 end if;
9578 end if;
9579 end loop;
9580 end if;
9581 end Enter_Overloaded_Entity;
9583 ----------------------------
9584 -- Extra_Formals_Match_OK --
9585 ----------------------------
9587 function Extra_Formals_Match_OK
9588 (E : Entity_Id;
9589 Ref_E : Entity_Id) return Boolean is
9590 begin
9591 pragma Assert (Is_Subprogram (E));
9593 -- Cases where no check can be performed:
9594 -- 1) When expansion is not active (since we never generate extra
9595 -- formals if expansion is not active because we don't need them
9596 -- unless we are generating code).
9597 -- 2) On abstract predefined dispatching operations of Root_Controlled
9598 -- and predefined stream operations not emitted by the frontend.
9599 -- 3) On renamings of abstract predefined dispatching operations of
9600 -- interface types (since limitedness is not inherited in such
9601 -- case (AI-419)).
9602 -- 4) The controlling formal of the nondispatching subprogram of
9603 -- a concurrent type that overrides an interface primitive is
9604 -- implicit and hence we cannot check here if all its extra
9605 -- formals match; the expander will create the dispatching
9606 -- primitive (without Convention_Protected) with the matching
9607 -- formals (see exp_ch9.Build_Wrapper_Specs) which will be
9608 -- checked later.
9610 if Debug_Flag_Underscore_XX
9611 or else not Expander_Active
9612 or else
9613 (Is_Predefined_Dispatching_Operation (E)
9614 and then (not Has_Reliable_Extra_Formals (E)
9615 or else not Has_Reliable_Extra_Formals (Ref_E)))
9616 or else
9617 (Is_Predefined_Dispatching_Operation (E)
9618 and then Is_Abstract_Subprogram (E)
9619 and then Is_Interface (Find_Dispatching_Type (Ref_E)))
9620 then
9621 return True;
9623 elsif Convention (E) = Convention_Protected
9624 and then not Is_Dispatching_Operation (E)
9625 and then Is_Abstract_Subprogram (Ref_E)
9626 and then Is_Interface (Find_Dispatching_Type (Ref_E))
9627 then
9628 return True;
9629 end if;
9631 -- Perform the checks
9633 if No (Extra_Formals (E)) then
9634 return No (Extra_Formals (Ref_E));
9635 end if;
9637 if Ekind (E) in E_Function | E_Subprogram_Type
9638 and then Present (Extra_Accessibility_Of_Result (E))
9639 /= Present (Extra_Accessibility_Of_Result (Ref_E))
9640 then
9641 return False;
9642 end if;
9644 declare
9645 Formal_1 : Entity_Id := Extra_Formals (E);
9646 Formal_2 : Entity_Id := Extra_Formals (Ref_E);
9648 begin
9649 while Present (Formal_1) and then Present (Formal_2) loop
9650 if Has_Suffix (Formal_1, 'L') then
9651 if not Has_Suffix (Formal_2, 'L') then
9652 return False;
9653 end if;
9655 elsif Has_Suffix (Formal_1, 'O') then
9656 if not Has_Suffix (Formal_2, 'O') then
9657 return False;
9658 end if;
9660 elsif BIP_Suffix_Kind (Formal_1) /= BIP_Suffix_Kind (Formal_2) then
9661 return False;
9662 end if;
9664 Formal_1 := Next_Formal_With_Extras (Formal_1);
9665 Formal_2 := Next_Formal_With_Extras (Formal_2);
9666 end loop;
9668 return No (Formal_1) and then No (Formal_2);
9669 end;
9670 end Extra_Formals_Match_OK;
9672 ----------------------
9673 -- Extra_Formals_OK --
9674 ----------------------
9676 function Extra_Formals_OK (E : Entity_Id) return Boolean is
9677 Last_Formal : Entity_Id := Empty;
9678 Formal : Entity_Id;
9679 Has_Extra_Formals : Boolean := False;
9681 begin
9682 -- No check required if explicitly disabled
9684 if Debug_Flag_Underscore_XX then
9685 return True;
9687 -- No check required if expansion is disabled because extra
9688 -- formals are only generated when we are generating code.
9689 -- See Create_Extra_Formals.
9691 elsif not Expander_Active then
9692 return True;
9693 end if;
9695 -- Check attribute Extra_Formal: If available, it must be set only
9696 -- on the last formal of E.
9698 Formal := First_Formal (E);
9699 while Present (Formal) loop
9700 if Present (Extra_Formal (Formal)) then
9701 if Has_Extra_Formals then
9702 return False;
9703 end if;
9705 Has_Extra_Formals := True;
9706 end if;
9708 Last_Formal := Formal;
9709 Next_Formal (Formal);
9710 end loop;
9712 -- Check attribute Extra_Accessibility_Of_Result
9714 if Ekind (E) in E_Function | E_Subprogram_Type
9715 and then Needs_Result_Accessibility_Level (E)
9716 and then No (Extra_Accessibility_Of_Result (E))
9717 then
9718 return False;
9719 end if;
9721 -- Check attribute Extra_Formals: If E has extra formals, then this
9722 -- attribute must point to the first extra formal of E.
9724 if Has_Extra_Formals then
9725 return Present (Extra_Formals (E))
9726 and then Present (Extra_Formal (Last_Formal))
9727 and then Extra_Formal (Last_Formal) = Extra_Formals (E);
9729 -- When E has no formals, the first extra formal is available through
9730 -- the Extra_Formals attribute.
9732 elsif Present (Extra_Formals (E)) then
9733 return No (First_Formal (E));
9735 else
9736 return True;
9737 end if;
9738 end Extra_Formals_OK;
9740 -----------------------------
9741 -- Find_Corresponding_Spec --
9742 -----------------------------
9744 function Find_Corresponding_Spec
9745 (N : Node_Id;
9746 Post_Error : Boolean := True) return Entity_Id
9748 Spec : constant Node_Id := Specification (N);
9749 Designator : constant Entity_Id := Defining_Entity (Spec);
9751 E : Entity_Id;
9753 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9754 -- Even if fully conformant, a body may depend on a generic actual when
9755 -- the spec does not, or vice versa, in which case they were distinct
9756 -- entities in the generic.
9758 -------------------------------
9759 -- Different_Generic_Profile --
9760 -------------------------------
9762 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9763 F1, F2 : Entity_Id;
9765 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9766 -- Check that the types of corresponding formals have the same
9767 -- generic actual if any. We have to account for subtypes of a
9768 -- generic formal, declared between a spec and a body, which may
9769 -- appear distinct in an instance but matched in the generic, and
9770 -- the subtype may be used either in the spec or the body of the
9771 -- subprogram being checked.
9773 -------------------------
9774 -- Same_Generic_Actual --
9775 -------------------------
9777 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9779 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9780 -- Predicate to check whether S1 is a subtype of S2 in the source
9781 -- of the instance.
9783 -------------------------
9784 -- Is_Declared_Subtype --
9785 -------------------------
9787 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9788 begin
9789 return Comes_From_Source (Parent (S1))
9790 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9791 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9792 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9793 end Is_Declared_Subtype;
9795 -- Start of processing for Same_Generic_Actual
9797 begin
9798 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9799 or else Is_Declared_Subtype (T1, T2)
9800 or else Is_Declared_Subtype (T2, T1);
9801 end Same_Generic_Actual;
9803 -- Start of processing for Different_Generic_Profile
9805 begin
9806 if not In_Instance then
9807 return False;
9809 elsif Ekind (E) = E_Function
9810 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9811 then
9812 return True;
9813 end if;
9815 F1 := First_Formal (Designator);
9816 F2 := First_Formal (E);
9817 while Present (F1) loop
9818 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9819 return True;
9820 end if;
9822 Next_Formal (F1);
9823 Next_Formal (F2);
9824 end loop;
9826 return False;
9827 end Different_Generic_Profile;
9829 -- Start of processing for Find_Corresponding_Spec
9831 begin
9832 E := Current_Entity (Designator);
9833 while Present (E) loop
9835 -- We are looking for a matching spec. It must have the same scope,
9836 -- and the same name, and either be type conformant, or be the case
9837 -- of a library procedure spec and its body (which belong to one
9838 -- another regardless of whether they are type conformant or not).
9840 if Scope (E) = Current_Scope then
9841 if Current_Scope = Standard_Standard
9842 or else (Ekind (E) = Ekind (Designator)
9843 and then Type_Conformant (E, Designator))
9844 then
9845 -- Within an instantiation, we know that spec and body are
9846 -- subtype conformant, because they were subtype conformant in
9847 -- the generic. We choose the subtype-conformant entity here as
9848 -- well, to resolve spurious ambiguities in the instance that
9849 -- were not present in the generic (i.e. when two different
9850 -- types are given the same actual). If we are looking for a
9851 -- spec to match a body, full conformance is expected.
9853 if In_Instance then
9855 -- Inherit the convention and "ghostness" of the matching
9856 -- spec to ensure proper full and subtype conformance.
9858 Set_Convention (Designator, Convention (E));
9860 -- Skip past subprogram bodies and subprogram renamings that
9861 -- may appear to have a matching spec, but that aren't fully
9862 -- conformant with it. That can occur in cases where an
9863 -- actual type causes unrelated homographs in the instance.
9865 if Nkind (N) in N_Subprogram_Body
9866 | N_Subprogram_Renaming_Declaration
9867 and then Present (Homonym (E))
9868 and then not Fully_Conformant (Designator, E)
9869 then
9870 goto Next_Entity;
9872 elsif not Subtype_Conformant (Designator, E) then
9873 goto Next_Entity;
9875 elsif Different_Generic_Profile (E) then
9876 goto Next_Entity;
9877 end if;
9878 end if;
9880 -- Ada 2012 (AI05-0165): For internally generated bodies of
9881 -- null procedures locate the internally generated spec. We
9882 -- enforce mode conformance since a tagged type may inherit
9883 -- from interfaces several null primitives which differ only
9884 -- in the mode of the formals.
9886 if not (Comes_From_Source (E))
9887 and then Is_Null_Procedure (E)
9888 and then not Mode_Conformant (Designator, E)
9889 then
9890 null;
9892 -- For null procedures coming from source that are completions,
9893 -- analysis of the generated body will establish the link.
9895 elsif Comes_From_Source (E)
9896 and then Nkind (Spec) = N_Procedure_Specification
9897 and then Null_Present (Spec)
9898 then
9899 return E;
9901 -- Expression functions can be completions, but cannot be
9902 -- completed by an explicit body.
9904 elsif Comes_From_Source (E)
9905 and then Comes_From_Source (N)
9906 and then Nkind (N) = N_Subprogram_Body
9907 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9908 N_Expression_Function
9909 then
9910 Error_Msg_Sloc := Sloc (E);
9911 Error_Msg_N ("body conflicts with expression function#", N);
9912 return Empty;
9914 elsif not Has_Completion (E) then
9915 if Nkind (N) /= N_Subprogram_Body_Stub then
9916 Set_Corresponding_Spec (N, E);
9917 end if;
9919 Set_Has_Completion (E);
9920 return E;
9922 elsif Nkind (Parent (N)) = N_Subunit then
9924 -- If this is the proper body of a subunit, the completion
9925 -- flag is set when analyzing the stub.
9927 return E;
9929 -- If E is an internal function with a controlling result that
9930 -- was created for an operation inherited by a null extension,
9931 -- it may be overridden by a body without a previous spec (one
9932 -- more reason why these should be shunned). In that case we
9933 -- remove the generated body if present, because the current
9934 -- one is the explicit overriding.
9936 elsif Ekind (E) = E_Function
9937 and then Ada_Version >= Ada_2005
9938 and then not Comes_From_Source (E)
9939 and then Has_Controlling_Result (E)
9940 and then (not Is_Class_Wide_Type (Etype (E))
9941 and then Is_Null_Extension (Etype (E)))
9942 and then Comes_From_Source (Spec)
9943 then
9944 Set_Has_Completion (E, False);
9946 if Expander_Active
9947 and then Nkind (Parent (E)) = N_Function_Specification
9948 then
9949 Remove
9950 (Unit_Declaration_Node
9951 (Corresponding_Body (Unit_Declaration_Node (E))));
9953 return E;
9955 -- If expansion is disabled, or if the wrapper function has
9956 -- not been generated yet, this a late body overriding an
9957 -- inherited operation, or it is an overriding by some other
9958 -- declaration before the controlling result is frozen. In
9959 -- either case this is a declaration of a new entity.
9961 else
9962 return Empty;
9963 end if;
9965 -- If the body already exists, then this is an error unless
9966 -- the previous declaration is the implicit declaration of a
9967 -- derived subprogram. It is also legal for an instance to
9968 -- contain type conformant overloadable declarations (but the
9969 -- generic declaration may not), per 8.3(26/2).
9971 elsif No (Alias (E))
9972 and then not Is_Intrinsic_Subprogram (E)
9973 and then not In_Instance
9974 and then Post_Error
9975 then
9976 Error_Msg_Sloc := Sloc (E);
9978 if Is_Imported (E) then
9979 Error_Msg_NE
9980 ("body not allowed for imported subprogram & declared#",
9981 N, E);
9982 else
9983 Error_Msg_NE ("duplicate body for & declared#", N, E);
9984 end if;
9985 end if;
9987 -- Child units cannot be overloaded, so a conformance mismatch
9988 -- between body and a previous spec is an error.
9990 elsif Is_Child_Unit (E)
9991 and then
9992 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9993 and then
9994 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9995 N_Compilation_Unit
9996 and then Post_Error
9997 then
9998 Error_Msg_N
9999 ("body of child unit does not match previous declaration", N);
10000 end if;
10001 end if;
10003 <<Next_Entity>>
10004 E := Homonym (E);
10005 end loop;
10007 -- On exit, we know that no previous declaration of subprogram exists
10009 return Empty;
10010 end Find_Corresponding_Spec;
10012 ----------------------
10013 -- Fully_Conformant --
10014 ----------------------
10016 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10017 Result : Boolean;
10018 begin
10019 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
10020 return Result;
10021 end Fully_Conformant;
10023 ----------------------------------
10024 -- Fully_Conformant_Expressions --
10025 ----------------------------------
10027 function Fully_Conformant_Expressions
10028 (Given_E1 : Node_Id;
10029 Given_E2 : Node_Id;
10030 Report : Boolean := False) return Boolean
10032 E1 : constant Node_Id := Original_Node (Given_E1);
10033 E2 : constant Node_Id := Original_Node (Given_E2);
10034 -- We always test conformance on original nodes, since it is possible
10035 -- for analysis and/or expansion to make things look as though they
10036 -- conform when they do not, e.g. by converting 1+2 into 3.
10038 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
10039 -- Convenience function to abbreviate recursive calls to
10040 -- Fully_Conformant_Expressions without having to pass Report.
10042 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
10043 -- Compare elements of two lists for conformance. Elements have to be
10044 -- conformant, and actuals inserted as default parameters do not match
10045 -- explicit actuals with the same value.
10047 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
10048 -- Compare an operator node with a function call
10050 ---------
10051 -- FCE --
10052 ---------
10054 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
10055 begin
10056 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
10057 end FCE;
10059 ---------
10060 -- FCL --
10061 ---------
10063 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
10064 N1 : Node_Id;
10065 N2 : Node_Id;
10067 begin
10068 N1 := First (L1);
10069 N2 := First (L2);
10071 -- Compare two lists, skipping rewrite insertions (we want to compare
10072 -- the original trees, not the expanded versions).
10074 loop
10075 if Is_Rewrite_Insertion (N1) then
10076 Next (N1);
10077 elsif Is_Rewrite_Insertion (N2) then
10078 Next (N2);
10079 elsif No (N1) then
10080 return No (N2);
10081 elsif No (N2) then
10082 return False;
10083 elsif not FCE (N1, N2) then
10084 return False;
10085 else
10086 Next (N1);
10087 Next (N2);
10088 end if;
10089 end loop;
10090 end FCL;
10092 ---------
10093 -- FCO --
10094 ---------
10096 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
10097 Actuals : constant List_Id := Parameter_Associations (Call_Node);
10098 Act : Node_Id;
10100 begin
10101 if No (Actuals)
10102 or else Entity (Op_Node) /= Entity (Name (Call_Node))
10103 then
10104 return False;
10106 else
10107 Act := First (Actuals);
10109 if Nkind (Op_Node) in N_Binary_Op then
10110 if not FCE (Left_Opnd (Op_Node), Act) then
10111 return False;
10112 end if;
10114 Next (Act);
10115 end if;
10117 return Present (Act)
10118 and then FCE (Right_Opnd (Op_Node), Act)
10119 and then No (Next (Act));
10120 end if;
10121 end FCO;
10123 function User_Defined_Numeric_Literal_Mismatch return Boolean;
10124 -- Usually literals with the same value like 12345 and 12_345
10125 -- or 123.0 and 123.00 conform, but not if they are
10126 -- user-defined literals.
10128 -------------------------------------------
10129 -- User_Defined_Numeric_Literal_Mismatch --
10130 -------------------------------------------
10132 function User_Defined_Numeric_Literal_Mismatch return Boolean is
10133 E1_Is_User_Defined : constant Boolean :=
10134 Nkind (Given_E1) not in N_Integer_Literal | N_Real_Literal;
10135 E2_Is_User_Defined : constant Boolean :=
10136 Nkind (Given_E2) not in N_Integer_Literal | N_Real_Literal;
10138 begin
10139 pragma Assert (E1_Is_User_Defined = E2_Is_User_Defined);
10141 return E1_Is_User_Defined and then
10142 not String_Equal (String_From_Numeric_Literal (E1),
10143 String_From_Numeric_Literal (E2));
10144 end User_Defined_Numeric_Literal_Mismatch;
10146 -- Local variables
10148 Result : Boolean;
10150 -- Start of processing for Fully_Conformant_Expressions
10152 begin
10153 Result := True;
10155 -- Nonconformant if paren count does not match. Note: if some idiot
10156 -- complains that we don't do this right for more than 3 levels of
10157 -- parentheses, they will be treated with the respect they deserve.
10159 if Paren_Count (E1) /= Paren_Count (E2) then
10160 return False;
10162 -- If same entities are referenced, then they are conformant even if
10163 -- they have different forms (RM 8.3.1(19-20)).
10165 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
10166 if Present (Entity (E1)) then
10167 Result := Entity (E1) = Entity (E2)
10169 -- One may be a discriminant that has been replaced by the
10170 -- corresponding discriminal.
10172 or else
10173 (Chars (Entity (E1)) = Chars (Entity (E2))
10174 and then Ekind (Entity (E1)) = E_Discriminant
10175 and then Ekind (Entity (E2)) = E_In_Parameter)
10177 -- The discriminant of a protected type is transformed into
10178 -- a local constant and then into a parameter of a protected
10179 -- operation.
10181 or else
10182 (Ekind (Entity (E1)) = E_Constant
10183 and then Ekind (Entity (E2)) = E_In_Parameter
10184 and then Present (Discriminal_Link (Entity (E1)))
10185 and then Discriminal_Link (Entity (E1)) =
10186 Discriminal_Link (Entity (E2)))
10188 -- AI12-050: The entities of quantified expressions match if they
10189 -- have the same identifier, even if they may be distinct nodes.
10191 or else
10192 (Chars (Entity (E1)) = Chars (Entity (E2))
10193 and then Is_Entity_Of_Quantified_Expression (Entity (E1))
10194 and then Is_Entity_Of_Quantified_Expression (Entity (E2)))
10196 -- A call to an instantiation of Unchecked_Conversion is
10197 -- rewritten with the name of the generated function created for
10198 -- the instance, and this must be special-cased.
10200 or else
10201 (Ekind (Entity (E1)) = E_Function
10202 and then Is_Intrinsic_Subprogram (Entity (E1))
10203 and then Is_Generic_Instance (Entity (E1))
10204 and then Entity (E2) = Alias (Entity (E1)));
10205 if Report and not Result then
10206 Error_Msg_Sloc :=
10207 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
10208 Error_Msg_NE
10209 ("meaning of& differs because of declaration#", E1, E2);
10210 end if;
10212 return Result;
10214 elsif Nkind (E1) = N_Expanded_Name
10215 and then Nkind (E2) = N_Expanded_Name
10216 and then Nkind (Selector_Name (E1)) = N_Character_Literal
10217 and then Nkind (Selector_Name (E2)) = N_Character_Literal
10218 then
10219 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
10221 else
10222 -- Identifiers in component associations don't always have
10223 -- entities, but their names must conform.
10225 return Nkind (E1) = N_Identifier
10226 and then Nkind (E2) = N_Identifier
10227 and then Chars (E1) = Chars (E2);
10228 end if;
10230 elsif Nkind (E1) = N_Character_Literal
10231 and then Nkind (E2) = N_Expanded_Name
10232 then
10233 return Nkind (Selector_Name (E2)) = N_Character_Literal
10234 and then Chars (E1) = Chars (Selector_Name (E2));
10236 elsif Nkind (E2) = N_Character_Literal
10237 and then Nkind (E1) = N_Expanded_Name
10238 then
10239 return Nkind (Selector_Name (E1)) = N_Character_Literal
10240 and then Chars (E2) = Chars (Selector_Name (E1));
10242 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
10243 return FCO (E1, E2);
10245 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
10246 return FCO (E2, E1);
10248 -- Otherwise we must have the same syntactic entity
10250 elsif Nkind (E1) /= Nkind (E2) then
10251 return False;
10253 -- At this point, we specialize by node type
10255 else
10256 case Nkind (E1) is
10257 when N_Aggregate =>
10258 return
10259 FCL (Expressions (E1), Expressions (E2))
10260 and then
10261 FCL (Component_Associations (E1),
10262 Component_Associations (E2));
10264 when N_Allocator =>
10265 if Nkind (Expression (E1)) = N_Qualified_Expression
10266 or else
10267 Nkind (Expression (E2)) = N_Qualified_Expression
10268 then
10269 return FCE (Expression (E1), Expression (E2));
10271 -- Check that the subtype marks and any constraints
10272 -- are conformant
10274 else
10275 declare
10276 Indic1 : constant Node_Id := Expression (E1);
10277 Indic2 : constant Node_Id := Expression (E2);
10278 Elt1 : Node_Id;
10279 Elt2 : Node_Id;
10281 begin
10282 if Nkind (Indic1) /= N_Subtype_Indication then
10283 return
10284 Nkind (Indic2) /= N_Subtype_Indication
10285 and then Entity (Indic1) = Entity (Indic2);
10287 elsif Nkind (Indic2) /= N_Subtype_Indication then
10288 return
10289 Nkind (Indic1) /= N_Subtype_Indication
10290 and then Entity (Indic1) = Entity (Indic2);
10292 else
10293 if Entity (Subtype_Mark (Indic1)) /=
10294 Entity (Subtype_Mark (Indic2))
10295 then
10296 return False;
10297 end if;
10299 Elt1 := First (Constraints (Constraint (Indic1)));
10300 Elt2 := First (Constraints (Constraint (Indic2)));
10301 while Present (Elt1) and then Present (Elt2) loop
10302 if not FCE (Elt1, Elt2) then
10303 return False;
10304 end if;
10306 Next (Elt1);
10307 Next (Elt2);
10308 end loop;
10310 return True;
10311 end if;
10312 end;
10313 end if;
10315 when N_Attribute_Reference =>
10316 return
10317 Attribute_Name (E1) = Attribute_Name (E2)
10318 and then FCL (Expressions (E1), Expressions (E2));
10320 when N_Binary_Op =>
10321 return
10322 Entity (E1) = Entity (E2)
10323 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
10324 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
10326 when N_Membership_Test
10327 | N_Short_Circuit
10329 return
10330 FCE (Left_Opnd (E1), Left_Opnd (E2))
10331 and then
10332 FCE (Right_Opnd (E1), Right_Opnd (E2));
10334 when N_Case_Expression =>
10335 declare
10336 Alt1 : Node_Id;
10337 Alt2 : Node_Id;
10339 begin
10340 if not FCE (Expression (E1), Expression (E2)) then
10341 return False;
10343 else
10344 Alt1 := First (Alternatives (E1));
10345 Alt2 := First (Alternatives (E2));
10346 loop
10347 if Present (Alt1) /= Present (Alt2) then
10348 return False;
10349 elsif No (Alt1) then
10350 return True;
10351 end if;
10353 if not FCE (Expression (Alt1), Expression (Alt2))
10354 or else not FCL (Discrete_Choices (Alt1),
10355 Discrete_Choices (Alt2))
10356 then
10357 return False;
10358 end if;
10360 Next (Alt1);
10361 Next (Alt2);
10362 end loop;
10363 end if;
10364 end;
10366 when N_Character_Literal =>
10367 return
10368 Char_Literal_Value (E1) = Char_Literal_Value (E2);
10370 when N_Component_Association =>
10371 return
10372 FCL (Choices (E1), Choices (E2))
10373 and then
10374 FCE (Expression (E1), Expression (E2));
10376 when N_Explicit_Dereference =>
10377 return
10378 FCE (Prefix (E1), Prefix (E2));
10380 when N_Extension_Aggregate =>
10381 return
10382 FCL (Expressions (E1), Expressions (E2))
10383 and then Null_Record_Present (E1) =
10384 Null_Record_Present (E2)
10385 and then FCL (Component_Associations (E1),
10386 Component_Associations (E2));
10388 when N_Function_Call =>
10389 return
10390 FCE (Name (E1), Name (E2))
10391 and then
10392 FCL (Parameter_Associations (E1),
10393 Parameter_Associations (E2));
10395 when N_If_Expression =>
10396 return
10397 FCL (Expressions (E1), Expressions (E2));
10399 when N_Indexed_Component =>
10400 return
10401 FCE (Prefix (E1), Prefix (E2))
10402 and then
10403 FCL (Expressions (E1), Expressions (E2));
10405 when N_Integer_Literal =>
10406 return Intval (E1) = Intval (E2)
10407 and then not User_Defined_Numeric_Literal_Mismatch;
10409 when N_Null =>
10410 return True;
10412 when N_Operator_Symbol =>
10413 return
10414 Chars (E1) = Chars (E2);
10416 when N_Others_Choice =>
10417 return True;
10419 when N_Parameter_Association =>
10420 return
10421 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
10422 and then FCE (Explicit_Actual_Parameter (E1),
10423 Explicit_Actual_Parameter (E2));
10425 when N_Qualified_Expression
10426 | N_Type_Conversion
10427 | N_Unchecked_Type_Conversion
10429 return
10430 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
10431 and then
10432 FCE (Expression (E1), Expression (E2));
10434 when N_Quantified_Expression =>
10435 if not FCE (Condition (E1), Condition (E2)) then
10436 return False;
10437 end if;
10439 if Present (Loop_Parameter_Specification (E1))
10440 and then Present (Loop_Parameter_Specification (E2))
10441 then
10442 declare
10443 L1 : constant Node_Id :=
10444 Loop_Parameter_Specification (E1);
10445 L2 : constant Node_Id :=
10446 Loop_Parameter_Specification (E2);
10448 begin
10449 return
10450 Reverse_Present (L1) = Reverse_Present (L2)
10451 and then
10452 FCE (Defining_Identifier (L1),
10453 Defining_Identifier (L2))
10454 and then
10455 FCE (Discrete_Subtype_Definition (L1),
10456 Discrete_Subtype_Definition (L2));
10457 end;
10459 elsif Present (Iterator_Specification (E1))
10460 and then Present (Iterator_Specification (E2))
10461 then
10462 declare
10463 I1 : constant Node_Id := Iterator_Specification (E1);
10464 I2 : constant Node_Id := Iterator_Specification (E2);
10466 begin
10467 return
10468 FCE (Defining_Identifier (I1),
10469 Defining_Identifier (I2))
10470 and then
10471 Of_Present (I1) = Of_Present (I2)
10472 and then
10473 Reverse_Present (I1) = Reverse_Present (I2)
10474 and then FCE (Name (I1), Name (I2))
10475 and then FCE (Subtype_Indication (I1),
10476 Subtype_Indication (I2));
10477 end;
10479 -- The quantified expressions used different specifications to
10480 -- walk their respective ranges.
10482 else
10483 return False;
10484 end if;
10486 when N_Range =>
10487 return
10488 FCE (Low_Bound (E1), Low_Bound (E2))
10489 and then
10490 FCE (High_Bound (E1), High_Bound (E2));
10492 when N_Real_Literal =>
10493 return Realval (E1) = Realval (E2)
10494 and then not User_Defined_Numeric_Literal_Mismatch;
10496 when N_Selected_Component =>
10497 return
10498 FCE (Prefix (E1), Prefix (E2))
10499 and then
10500 FCE (Selector_Name (E1), Selector_Name (E2));
10502 when N_Slice =>
10503 return
10504 FCE (Prefix (E1), Prefix (E2))
10505 and then
10506 FCE (Discrete_Range (E1), Discrete_Range (E2));
10508 when N_String_Literal =>
10509 declare
10510 S1 : constant String_Id := Strval (E1);
10511 S2 : constant String_Id := Strval (E2);
10512 L1 : constant Nat := String_Length (S1);
10513 L2 : constant Nat := String_Length (S2);
10515 begin
10516 if L1 /= L2 then
10517 return False;
10519 else
10520 for J in 1 .. L1 loop
10521 if Get_String_Char (S1, J) /=
10522 Get_String_Char (S2, J)
10523 then
10524 return False;
10525 end if;
10526 end loop;
10528 return True;
10529 end if;
10530 end;
10532 when N_Unary_Op =>
10533 return
10534 Entity (E1) = Entity (E2)
10535 and then
10536 FCE (Right_Opnd (E1), Right_Opnd (E2));
10538 -- All other node types cannot appear in this context. Strictly
10539 -- we should raise a fatal internal error. Instead we just ignore
10540 -- the nodes. This means that if anyone makes a mistake in the
10541 -- expander and mucks an expression tree irretrievably, the result
10542 -- will be a failure to detect a (probably very obscure) case
10543 -- of non-conformance, which is better than bombing on some
10544 -- case where two expressions do in fact conform.
10546 when others =>
10547 return True;
10548 end case;
10549 end if;
10550 end Fully_Conformant_Expressions;
10552 ----------------------------------------
10553 -- Fully_Conformant_Discrete_Subtypes --
10554 ----------------------------------------
10556 function Fully_Conformant_Discrete_Subtypes
10557 (Given_S1 : Node_Id;
10558 Given_S2 : Node_Id) return Boolean
10560 S1 : constant Node_Id := Original_Node (Given_S1);
10561 S2 : constant Node_Id := Original_Node (Given_S2);
10563 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
10564 -- Special-case for a bound given by a discriminant, which in the body
10565 -- is replaced with the discriminal of the enclosing type.
10567 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
10568 -- Check both bounds
10570 -----------------------
10571 -- Conforming_Bounds --
10572 -----------------------
10574 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
10575 begin
10576 if Is_Entity_Name (B1)
10577 and then Is_Entity_Name (B2)
10578 and then Ekind (Entity (B1)) = E_Discriminant
10579 then
10580 return Chars (B1) = Chars (B2);
10582 else
10583 return Fully_Conformant_Expressions (B1, B2);
10584 end if;
10585 end Conforming_Bounds;
10587 -----------------------
10588 -- Conforming_Ranges --
10589 -----------------------
10591 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
10592 begin
10593 return
10594 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
10595 and then
10596 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
10597 end Conforming_Ranges;
10599 -- Start of processing for Fully_Conformant_Discrete_Subtypes
10601 begin
10602 if Nkind (S1) /= Nkind (S2) then
10603 return False;
10605 elsif Is_Entity_Name (S1) then
10606 return Entity (S1) = Entity (S2);
10608 elsif Nkind (S1) = N_Range then
10609 return Conforming_Ranges (S1, S2);
10611 elsif Nkind (S1) = N_Subtype_Indication then
10612 return
10613 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
10614 and then
10615 Conforming_Ranges
10616 (Range_Expression (Constraint (S1)),
10617 Range_Expression (Constraint (S2)));
10618 else
10619 return True;
10620 end if;
10621 end Fully_Conformant_Discrete_Subtypes;
10623 ---------------------
10624 -- Has_BIP_Formals --
10625 ---------------------
10627 function Has_BIP_Formals (E : Entity_Id) return Boolean is
10628 Formal : Entity_Id := First_Formal_With_Extras (E);
10630 begin
10631 while Present (Formal) loop
10632 if Is_Build_In_Place_Entity (Formal) then
10633 return True;
10634 end if;
10636 Next_Formal_With_Extras (Formal);
10637 end loop;
10639 return False;
10640 end Has_BIP_Formals;
10642 --------------------------------
10643 -- Has_Reliable_Extra_Formals --
10644 --------------------------------
10646 function Has_Reliable_Extra_Formals (E : Entity_Id) return Boolean is
10647 Alias_E : Entity_Id;
10649 begin
10650 -- Extra formals are not added if expansion is not active (and hence if
10651 -- available they are not reliable for extra formals check).
10653 if not Expander_Active then
10654 return False;
10656 -- Currently the unique cases where extra formals are not reliable
10657 -- are associated with predefined dispatching operations; otherwise
10658 -- they are properly added when required.
10660 elsif not Is_Predefined_Dispatching_Operation (E) then
10661 return True;
10662 end if;
10664 Alias_E := Ultimate_Alias (E);
10666 -- Abstract predefined primitives of Root_Controlled don't have
10667 -- extra formals; this is required to build the runtime.
10669 if Ekind (Alias_E) = E_Function
10670 and then Is_Abstract_Subprogram (Alias_E)
10671 and then Is_RTE (Underlying_Type (Etype (Alias_E)),
10672 RE_Root_Controlled)
10673 then
10674 return False;
10676 -- Predefined stream dispatching operations that are not emitted by
10677 -- the frontend; they have a renaming of the corresponding primitive
10678 -- of their parent type and hence they don't have extra formals.
10680 else
10681 declare
10682 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (E);
10683 Typ : constant Entity_Id :=
10684 Underlying_Type (Find_Dispatching_Type (Alias_E));
10686 begin
10687 if TSS_Name in TSS_Stream_Input
10688 | TSS_Stream_Output
10689 | TSS_Stream_Read
10690 | TSS_Stream_Write
10691 and then not Stream_Operation_OK (Typ, TSS_Name)
10692 then
10693 return False;
10694 end if;
10695 end;
10696 end if;
10698 return True;
10699 end Has_Reliable_Extra_Formals;
10701 --------------------
10702 -- Install_Entity --
10703 --------------------
10705 procedure Install_Entity (E : Entity_Id) is
10706 Prev : constant Entity_Id := Current_Entity (E);
10707 begin
10708 Set_Is_Immediately_Visible (E);
10709 Set_Current_Entity (E);
10710 pragma Assert (Prev /= E);
10711 Set_Homonym (E, Prev);
10712 end Install_Entity;
10714 ---------------------
10715 -- Install_Formals --
10716 ---------------------
10718 procedure Install_Formals (Id : Entity_Id) is
10719 F : Entity_Id;
10720 begin
10721 F := First_Formal (Id);
10722 while Present (F) loop
10723 Install_Entity (F);
10724 Next_Formal (F);
10725 end loop;
10726 end Install_Formals;
10728 -----------------------------
10729 -- Is_Interface_Conformant --
10730 -----------------------------
10732 function Is_Interface_Conformant
10733 (Tagged_Type : Entity_Id;
10734 Iface_Prim : Entity_Id;
10735 Prim : Entity_Id) return Boolean
10737 -- The operation may in fact be an inherited (implicit) operation
10738 -- rather than the original interface primitive, so retrieve the
10739 -- ultimate ancestor.
10741 Iface : constant Entity_Id :=
10742 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
10743 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
10745 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
10746 -- Return the controlling formal of Prim
10748 ------------------------
10749 -- Controlling_Formal --
10750 ------------------------
10752 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
10753 E : Entity_Id;
10755 begin
10756 E := First_Formal (Prim);
10757 while Present (E) loop
10758 if Is_Controlling_Formal (E) then
10759 return E;
10760 end if;
10762 Next_Formal (E);
10763 end loop;
10765 return Empty;
10766 end Controlling_Formal;
10768 -- Local variables
10770 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
10771 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
10773 -- Start of processing for Is_Interface_Conformant
10775 begin
10776 pragma Assert (Is_Subprogram (Iface_Prim)
10777 and then Is_Subprogram (Prim)
10778 and then Is_Dispatching_Operation (Iface_Prim)
10779 and then Is_Dispatching_Operation (Prim));
10781 pragma Assert (Is_Interface (Iface)
10782 or else (Present (Alias (Iface_Prim))
10783 and then
10784 Is_Interface
10785 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
10787 if Prim = Iface_Prim
10788 or else not Is_Subprogram (Prim)
10789 or else Ekind (Prim) /= Ekind (Iface_Prim)
10790 or else not Is_Dispatching_Operation (Prim)
10791 or else Scope (Prim) /= Scope (Tagged_Type)
10792 or else No (Typ)
10793 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
10794 or else not Primitive_Names_Match (Iface_Prim, Prim)
10795 then
10796 return False;
10798 -- The mode of the controlling formals must match
10800 elsif Present (Iface_Ctrl_F)
10801 and then Present (Prim_Ctrl_F)
10802 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
10803 then
10804 return False;
10806 -- Case of a procedure, or a function whose result type matches the
10807 -- result type of the interface primitive, or a function that has no
10808 -- controlling result (I or access I).
10810 elsif Ekind (Iface_Prim) = E_Procedure
10811 or else Etype (Prim) = Etype (Iface_Prim)
10812 or else not Has_Controlling_Result (Prim)
10813 then
10814 return Type_Conformant
10815 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
10817 -- Case of a function returning an interface, or an access to one. Check
10818 -- that the return types correspond.
10820 elsif Implements_Interface (Typ, Iface) then
10821 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10823 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10824 then
10825 return False;
10826 else
10827 return
10828 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10829 Skip_Controlling_Formals => True);
10830 end if;
10832 else
10833 return False;
10834 end if;
10835 end Is_Interface_Conformant;
10837 ---------------------------------
10838 -- Is_Non_Overriding_Operation --
10839 ---------------------------------
10841 function Is_Non_Overriding_Operation
10842 (Prev_E : Entity_Id;
10843 New_E : Entity_Id) return Boolean
10845 Formal : Entity_Id;
10846 F_Typ : Entity_Id;
10847 G_Typ : Entity_Id := Empty;
10849 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10850 -- If F_Type is a derived type associated with a generic actual subtype,
10851 -- then return its Generic_Parent_Type attribute, else return Empty.
10853 function Types_Correspond
10854 (P_Type : Entity_Id;
10855 N_Type : Entity_Id) return Boolean;
10856 -- Returns true if and only if the types (or designated types in the
10857 -- case of anonymous access types) are the same or N_Type is derived
10858 -- directly or indirectly from P_Type.
10860 -----------------------------
10861 -- Get_Generic_Parent_Type --
10862 -----------------------------
10864 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10865 G_Typ : Entity_Id;
10866 Defn : Node_Id;
10867 Indic : Node_Id;
10869 begin
10870 if Is_Derived_Type (F_Typ)
10871 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10872 then
10873 -- The tree must be traversed to determine the parent subtype in
10874 -- the generic unit, which unfortunately isn't always available
10875 -- via semantic attributes. ??? (Note: The use of Original_Node
10876 -- is needed for cases where a full derived type has been
10877 -- rewritten.)
10879 -- If the parent type is a scalar type, the derivation creates
10880 -- an anonymous base type for it, and the source type is its
10881 -- first subtype.
10883 if Is_Scalar_Type (F_Typ)
10884 and then not Comes_From_Source (F_Typ)
10885 then
10886 Defn :=
10887 Type_Definition
10888 (Original_Node (Parent (First_Subtype (F_Typ))));
10889 else
10890 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10891 end if;
10892 if Nkind (Defn) = N_Derived_Type_Definition then
10893 Indic := Subtype_Indication (Defn);
10895 if Nkind (Indic) = N_Subtype_Indication then
10896 G_Typ := Entity (Subtype_Mark (Indic));
10897 else
10898 G_Typ := Entity (Indic);
10899 end if;
10901 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10902 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10903 then
10904 return Generic_Parent_Type (Parent (G_Typ));
10905 end if;
10906 end if;
10907 end if;
10909 return Empty;
10910 end Get_Generic_Parent_Type;
10912 ----------------------
10913 -- Types_Correspond --
10914 ----------------------
10916 function Types_Correspond
10917 (P_Type : Entity_Id;
10918 N_Type : Entity_Id) return Boolean
10920 Prev_Type : Entity_Id := Base_Type (P_Type);
10921 New_Type : Entity_Id := Base_Type (N_Type);
10923 begin
10924 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10925 Prev_Type := Designated_Type (Prev_Type);
10926 end if;
10928 if Ekind (New_Type) = E_Anonymous_Access_Type then
10929 New_Type := Designated_Type (New_Type);
10930 end if;
10932 if Prev_Type = New_Type then
10933 return True;
10935 elsif not Is_Class_Wide_Type (New_Type) then
10936 while Etype (New_Type) /= New_Type loop
10937 New_Type := Etype (New_Type);
10939 if New_Type = Prev_Type then
10940 return True;
10941 end if;
10942 end loop;
10943 end if;
10944 return False;
10945 end Types_Correspond;
10947 -- Start of processing for Is_Non_Overriding_Operation
10949 begin
10950 -- In the case where both operations are implicit derived subprograms
10951 -- then neither overrides the other. This can only occur in certain
10952 -- obscure cases (e.g., derivation from homographs created in a generic
10953 -- instantiation).
10955 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10956 return True;
10958 elsif Ekind (Current_Scope) = E_Package
10959 and then Is_Generic_Instance (Current_Scope)
10960 and then In_Private_Part (Current_Scope)
10961 and then Comes_From_Source (New_E)
10962 then
10963 -- We examine the formals and result type of the inherited operation,
10964 -- to determine whether their type is derived from (the instance of)
10965 -- a generic type. The first such formal or result type is the one
10966 -- tested.
10968 Formal := First_Formal (Prev_E);
10969 F_Typ := Empty;
10970 while Present (Formal) loop
10971 F_Typ := Base_Type (Etype (Formal));
10973 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10974 F_Typ := Designated_Type (F_Typ);
10975 end if;
10977 G_Typ := Get_Generic_Parent_Type (F_Typ);
10978 exit when Present (G_Typ);
10980 Next_Formal (Formal);
10981 end loop;
10983 -- If the function dispatches on result check the result type
10985 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10986 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10987 end if;
10989 if No (G_Typ) then
10990 return False;
10991 end if;
10993 -- If the generic type is a private type, then the original operation
10994 -- was not overriding in the generic, because there was no primitive
10995 -- operation to override.
10997 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10998 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10999 N_Formal_Private_Type_Definition
11000 then
11001 return True;
11003 -- The generic parent type is the ancestor of a formal derived
11004 -- type declaration. We need to check whether it has a primitive
11005 -- operation that should be overridden by New_E in the generic.
11007 else
11008 declare
11009 P_Formal : Entity_Id;
11010 N_Formal : Entity_Id;
11011 P_Typ : Entity_Id;
11012 N_Typ : Entity_Id;
11013 P_Prim : Entity_Id;
11014 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
11016 begin
11017 while Present (Prim_Elt) loop
11018 P_Prim := Node (Prim_Elt);
11020 if Chars (P_Prim) = Chars (New_E)
11021 and then Ekind (P_Prim) = Ekind (New_E)
11022 then
11023 P_Formal := First_Formal (P_Prim);
11024 N_Formal := First_Formal (New_E);
11025 while Present (P_Formal) and then Present (N_Formal) loop
11026 P_Typ := Etype (P_Formal);
11027 N_Typ := Etype (N_Formal);
11029 if not Types_Correspond (P_Typ, N_Typ) then
11030 exit;
11031 end if;
11033 Next_Formal (P_Formal);
11034 Next_Formal (N_Formal);
11035 end loop;
11037 -- Found a matching primitive operation belonging to the
11038 -- formal ancestor type, so the new subprogram is
11039 -- overriding.
11041 if No (P_Formal)
11042 and then No (N_Formal)
11043 and then (Ekind (New_E) /= E_Function
11044 or else
11045 Types_Correspond
11046 (Etype (P_Prim), Etype (New_E)))
11047 then
11048 return False;
11049 end if;
11050 end if;
11052 Next_Elmt (Prim_Elt);
11053 end loop;
11055 -- If no match found, then the new subprogram does not override
11056 -- in the generic (nor in the instance).
11058 -- If the type in question is not abstract, and the subprogram
11059 -- is, this will be an error if the new operation is in the
11060 -- private part of the instance. Emit a warning now, which will
11061 -- make the subsequent error message easier to understand.
11063 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
11064 and then Is_Abstract_Subprogram (Prev_E)
11065 and then In_Private_Part (Current_Scope)
11066 then
11067 Error_Msg_Node_2 := F_Typ;
11068 Error_Msg_NE
11069 ("private operation& in generic unit does not override "
11070 & "any primitive operation of& (RM 12.3(18))??",
11071 New_E, New_E);
11072 end if;
11074 return True;
11075 end;
11076 end if;
11077 else
11078 return False;
11079 end if;
11080 end Is_Non_Overriding_Operation;
11082 -------------------------------------
11083 -- List_Inherited_Pre_Post_Aspects --
11084 -------------------------------------
11086 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
11087 begin
11088 if List_Inherited_Aspects
11089 and then Is_Subprogram_Or_Generic_Subprogram (E)
11090 then
11091 declare
11092 Subps : constant Subprogram_List := Inherited_Subprograms (E);
11093 Items : Node_Id;
11094 Prag : Node_Id;
11096 begin
11097 for Index in Subps'Range loop
11098 Items := Contract (Subps (Index));
11100 if Present (Items) then
11101 Prag := Pre_Post_Conditions (Items);
11102 while Present (Prag) loop
11103 Error_Msg_Sloc := Sloc (Prag);
11105 if Class_Present (Prag)
11106 and then not Split_PPC (Prag)
11107 then
11108 if Pragma_Name (Prag) = Name_Precondition then
11109 Error_Msg_N
11110 ("info: & inherits `Pre''Class` aspect from "
11111 & "#?.l?", E);
11112 else
11113 Error_Msg_N
11114 ("info: & inherits `Post''Class` aspect from "
11115 & "#?.l?", E);
11116 end if;
11117 end if;
11119 Prag := Next_Pragma (Prag);
11120 end loop;
11121 end if;
11122 end loop;
11123 end;
11124 end if;
11125 end List_Inherited_Pre_Post_Aspects;
11127 ------------------------------
11128 -- Make_Inequality_Operator --
11129 ------------------------------
11131 -- S is the defining identifier of an equality operator. We build a
11132 -- subprogram declaration with the right signature. This operation is
11133 -- intrinsic, because it is always expanded as the negation of the
11134 -- call to the equality function.
11136 procedure Make_Inequality_Operator (S : Entity_Id) is
11137 Loc : constant Source_Ptr := Sloc (S);
11138 Decl : Node_Id;
11139 Formals : List_Id;
11140 Op_Name : Entity_Id;
11142 FF : constant Entity_Id := First_Formal (S);
11143 NF : constant Entity_Id := Next_Formal (FF);
11145 begin
11146 -- Check that equality was properly defined, ignore call if not
11148 if No (NF) then
11149 return;
11150 end if;
11152 declare
11153 A : constant Entity_Id :=
11154 Make_Defining_Identifier (Sloc (FF),
11155 Chars => Chars (FF));
11157 B : constant Entity_Id :=
11158 Make_Defining_Identifier (Sloc (NF),
11159 Chars => Chars (NF));
11161 begin
11162 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
11164 Formals := New_List (
11165 Make_Parameter_Specification (Loc,
11166 Defining_Identifier => A,
11167 Parameter_Type =>
11168 New_Occurrence_Of (Etype (First_Formal (S)),
11169 Sloc (Etype (First_Formal (S))))),
11171 Make_Parameter_Specification (Loc,
11172 Defining_Identifier => B,
11173 Parameter_Type =>
11174 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
11175 Sloc (Etype (Next_Formal (First_Formal (S)))))));
11177 Decl :=
11178 Make_Subprogram_Declaration (Loc,
11179 Specification =>
11180 Make_Function_Specification (Loc,
11181 Defining_Unit_Name => Op_Name,
11182 Parameter_Specifications => Formals,
11183 Result_Definition =>
11184 New_Occurrence_Of (Standard_Boolean, Loc)));
11186 -- Insert inequality right after equality if it is explicit or after
11187 -- the derived type when implicit. These entities are created only
11188 -- for visibility purposes, and eventually replaced in the course
11189 -- of expansion, so they do not need to be attached to the tree and
11190 -- seen by the back-end. Keeping them internal also avoids spurious
11191 -- freezing problems. The declaration is inserted in the tree for
11192 -- analysis, and removed afterwards. If the equality operator comes
11193 -- from an explicit declaration, attach the inequality immediately
11194 -- after. Else the equality is inherited from a derived type
11195 -- declaration, so insert inequality after that declaration.
11197 if No (Alias (S)) then
11198 Insert_After (Unit_Declaration_Node (S), Decl);
11199 elsif Is_List_Member (Parent (S)) then
11200 Insert_After (Parent (S), Decl);
11201 else
11202 Insert_After (Parent (Etype (First_Formal (S))), Decl);
11203 end if;
11205 Mark_Rewrite_Insertion (Decl);
11206 Set_Is_Intrinsic_Subprogram (Op_Name);
11207 Analyze (Decl);
11208 Remove (Decl);
11209 Set_Has_Completion (Op_Name);
11210 Set_Corresponding_Equality (Op_Name, S);
11211 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
11212 end;
11213 end Make_Inequality_Operator;
11215 ----------------------
11216 -- May_Need_Actuals --
11217 ----------------------
11219 procedure May_Need_Actuals (Fun : Entity_Id) is
11220 F : Entity_Id;
11221 B : Boolean;
11223 begin
11224 F := First_Formal (Fun);
11225 B := True;
11226 while Present (F) loop
11227 if No (Default_Value (F)) then
11228 B := False;
11229 exit;
11230 end if;
11232 Next_Formal (F);
11233 end loop;
11235 Set_Needs_No_Actuals (Fun, B);
11236 end May_Need_Actuals;
11238 ---------------------
11239 -- Mode_Conformant --
11240 ---------------------
11242 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
11243 Result : Boolean;
11244 begin
11245 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
11246 return Result;
11247 end Mode_Conformant;
11249 ---------------------------
11250 -- New_Overloaded_Entity --
11251 ---------------------------
11253 procedure New_Overloaded_Entity
11254 (S : Entity_Id;
11255 Derived_Type : Entity_Id := Empty)
11257 Overridden_Subp : Entity_Id := Empty;
11258 -- Set if the current scope has an operation that is type-conformant
11259 -- with S, and becomes hidden by S.
11261 Is_Primitive_Subp : Boolean;
11262 -- Set to True if the new subprogram is primitive
11264 E : Entity_Id;
11265 -- Entity that S overrides
11267 procedure Check_For_Primitive_Subprogram
11268 (Is_Primitive : out Boolean;
11269 Is_Overriding : Boolean := False);
11270 -- If the subprogram being analyzed is a primitive operation of the type
11271 -- of a formal or result, set the Has_Primitive_Operations flag on the
11272 -- type, and set Is_Primitive to True (otherwise set to False). Set the
11273 -- corresponding flag on the entity itself for later use.
11275 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
11276 -- True if a) E is a subprogram whose first formal is a concurrent type
11277 -- defined in the scope of E that has some entry or subprogram whose
11278 -- profile matches E, or b) E is an internally built dispatching
11279 -- subprogram of a protected type and there is a matching subprogram
11280 -- defined in the enclosing scope of the protected type, or c) E is
11281 -- an entry of a synchronized type and a matching procedure has been
11282 -- previously defined in the enclosing scope of the synchronized type.
11284 function Is_Private_Declaration (E : Entity_Id) return Boolean;
11285 -- Check that E is declared in the private part of the current package,
11286 -- or in the package body, where it may hide a previous declaration.
11287 -- We can't use In_Private_Part by itself because this flag is also
11288 -- set when freezing entities, so we must examine the place of the
11289 -- declaration in the tree, and recognize wrapper packages as well.
11291 function Is_Overriding_Alias
11292 (Old_E : Entity_Id;
11293 New_E : Entity_Id) return Boolean;
11294 -- Check whether new subprogram and old subprogram are both inherited
11295 -- from subprograms that have distinct dispatch table entries. This can
11296 -- occur with derivations from instances with accidental homonyms. The
11297 -- function is conservative given that the converse is only true within
11298 -- instances that contain accidental overloadings.
11300 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
11301 -- Report conflict between entities S and E
11303 ------------------------------------
11304 -- Check_For_Primitive_Subprogram --
11305 ------------------------------------
11307 procedure Check_For_Primitive_Subprogram
11308 (Is_Primitive : out Boolean;
11309 Is_Overriding : Boolean := False)
11311 procedure Add_Or_Replace_Untagged_Primitive (Typ : Entity_Id);
11312 -- Either add the new subprogram to the list of primitives for
11313 -- untagged type Typ, or if it overrides a primitive of Typ, then
11314 -- replace the overridden primitive in Typ's primitives list with
11315 -- the new subprogram.
11317 function Visible_Part_Type (T : Entity_Id) return Boolean;
11318 -- Returns true if T is declared in the visible part of the current
11319 -- package scope; otherwise returns false. Assumes that T is declared
11320 -- in a package.
11322 procedure Check_Private_Overriding (T : Entity_Id);
11323 -- Checks that if a primitive abstract subprogram of a visible
11324 -- abstract type is declared in a private part, then it must override
11325 -- an abstract subprogram declared in the visible part. Also checks
11326 -- that if a primitive function with a controlling result is declared
11327 -- in a private part, then it must override a function declared in
11328 -- the visible part.
11330 ---------------------------------------
11331 -- Add_Or_Replace_Untagged_Primitive --
11332 ---------------------------------------
11334 procedure Add_Or_Replace_Untagged_Primitive (Typ : Entity_Id) is
11335 Replaced_Overridden_Subp : Boolean := False;
11337 begin
11338 pragma Assert (not Is_Tagged_Type (Typ));
11340 -- Anonymous access types don't have a primitives list. Normally
11341 -- such types wouldn't make it here, but the case of anonymous
11342 -- access-to-subprogram types can.
11344 if not Is_Anonymous_Access_Type (Typ) then
11346 -- If S overrides a subprogram that's a primitive of
11347 -- the formal's type, then replace the overridden
11348 -- subprogram with the new subprogram in the type's
11349 -- list of primitives.
11351 if Is_Overriding then
11352 pragma Assert (Present (Overridden_Subp)
11353 and then Overridden_Subp = E); -- Added for now
11355 declare
11356 Prim_Ops : constant Elist_Id :=
11357 Primitive_Operations (Typ);
11358 Elmt : Elmt_Id;
11359 begin
11360 if Present (Prim_Ops) then
11361 Elmt := First_Elmt (Prim_Ops);
11363 while Present (Elmt)
11364 and then Node (Elmt) /= Overridden_Subp
11365 loop
11366 Next_Elmt (Elmt);
11367 end loop;
11369 if Present (Elmt) then
11370 Replace_Elmt (Elmt, S);
11371 Replaced_Overridden_Subp := True;
11372 end if;
11373 end if;
11374 end;
11375 end if;
11377 -- If the new subprogram did not override an operation
11378 -- of the formal's type, then add it to the primitives
11379 -- list of the type.
11381 if not Replaced_Overridden_Subp then
11382 Append_Unique_Elmt (S, Primitive_Operations (Typ));
11383 end if;
11384 end if;
11385 end Add_Or_Replace_Untagged_Primitive;
11387 ------------------------------
11388 -- Check_Private_Overriding --
11389 ------------------------------
11391 procedure Check_Private_Overriding (T : Entity_Id) is
11392 function Overrides_Private_Part_Op return Boolean;
11393 -- This detects the special case where the overriding subprogram
11394 -- is overriding a subprogram that was declared in the same
11395 -- private part. That case is illegal by 3.9.3(10).
11397 function Overrides_Visible_Function
11398 (Partial_View : Entity_Id) return Boolean;
11399 -- True if S overrides a function in the visible part. The
11400 -- overridden function could be explicitly or implicitly declared.
11402 -------------------------------
11403 -- Overrides_Private_Part_Op --
11404 -------------------------------
11406 function Overrides_Private_Part_Op return Boolean is
11407 Over_Decl : constant Node_Id :=
11408 Unit_Declaration_Node
11409 (Ultimate_Alias (Overridden_Operation (S)));
11410 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
11412 begin
11413 pragma Assert (Is_Overriding);
11414 pragma Assert
11415 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
11416 pragma Assert
11417 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
11419 return In_Same_List (Over_Decl, Subp_Decl);
11420 end Overrides_Private_Part_Op;
11422 --------------------------------
11423 -- Overrides_Visible_Function --
11424 --------------------------------
11426 function Overrides_Visible_Function
11427 (Partial_View : Entity_Id) return Boolean
11429 begin
11430 if not Is_Overriding or else not Has_Homonym (S) then
11431 return False;
11432 end if;
11434 if No (Partial_View) then
11435 return True;
11436 end if;
11438 -- Search through all the homonyms H of S in the current
11439 -- package spec, and return True if we find one that matches.
11440 -- Note that Parent (H) will be the declaration of the
11441 -- partial view of T for a match.
11443 declare
11444 H : Entity_Id := S;
11445 begin
11446 loop
11447 H := Homonym (H);
11448 exit when No (H) or else Scope (H) /= Scope (S);
11450 if Nkind (Parent (H)) in
11451 N_Private_Extension_Declaration |
11452 N_Private_Type_Declaration
11453 and then Defining_Identifier (Parent (H)) = Partial_View
11454 then
11455 return True;
11456 end if;
11457 end loop;
11458 end;
11460 return False;
11461 end Overrides_Visible_Function;
11463 -- Start of processing for Check_Private_Overriding
11465 begin
11466 if Is_Package_Or_Generic_Package (Current_Scope)
11467 and then In_Private_Part (Current_Scope)
11468 and then Visible_Part_Type (T)
11469 and then not In_Instance
11470 then
11471 if Is_Abstract_Type (T)
11472 and then Is_Abstract_Subprogram (S)
11473 and then (not Is_Overriding
11474 or else not Is_Abstract_Subprogram (E)
11475 or else Overrides_Private_Part_Op)
11476 then
11477 Error_Msg_N
11478 ("abstract subprograms must be visible (RM 3.9.3(10))!",
11481 elsif Ekind (S) = E_Function then
11482 declare
11483 Partial_View : constant Entity_Id :=
11484 Incomplete_Or_Partial_View (T);
11486 begin
11487 if not Overrides_Visible_Function (Partial_View) then
11489 -- Here, S is "function ... return T;" declared in
11490 -- the private part, not overriding some visible
11491 -- operation. That's illegal in the tagged case
11492 -- (but not if the private type is untagged).
11494 if ((Present (Partial_View)
11495 and then Is_Tagged_Type (Partial_View))
11496 or else (No (Partial_View)
11497 and then Is_Tagged_Type (T)))
11498 and then T = Base_Type (Etype (S))
11499 then
11500 Error_Msg_N
11501 ("private function with tagged result must"
11502 & " override visible-part function", S);
11503 Error_Msg_N
11504 ("\move subprogram to the visible part"
11505 & " (RM 3.9.3(10))", S);
11507 -- Ada 2012 (AI05-0073): Extend this check to the case
11508 -- of a function whose result subtype is defined by an
11509 -- access_definition designating specific tagged type.
11511 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
11512 and then Is_Tagged_Type (Designated_Type (Etype (S)))
11513 and then
11514 not Is_Class_Wide_Type
11515 (Designated_Type (Etype (S)))
11516 and then Ada_Version >= Ada_2012
11517 then
11518 Error_Msg_N
11519 ("private function with controlling access "
11520 & "result must override visible-part function",
11522 Error_Msg_N
11523 ("\move subprogram to the visible part"
11524 & " (RM 3.9.3(10))", S);
11525 end if;
11526 end if;
11527 end;
11528 end if;
11529 end if;
11530 end Check_Private_Overriding;
11532 -----------------------
11533 -- Visible_Part_Type --
11534 -----------------------
11536 function Visible_Part_Type (T : Entity_Id) return Boolean is
11537 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
11539 begin
11540 -- If the entity is a private type, then it must be declared in a
11541 -- visible part.
11543 if Is_Private_Type (T) then
11544 return True;
11546 elsif Is_Type (T) and then Has_Private_Declaration (T) then
11547 return True;
11549 elsif Is_List_Member (Declaration_Node (T))
11550 and then List_Containing (Declaration_Node (T)) =
11551 Visible_Declarations (Specification (P))
11552 then
11553 return True;
11555 else
11556 return False;
11557 end if;
11558 end Visible_Part_Type;
11560 -- Local variables
11562 Formal : Entity_Id;
11563 F_Typ : Entity_Id;
11564 B_Typ : Entity_Id;
11566 -- Start of processing for Check_For_Primitive_Subprogram
11568 begin
11569 Is_Primitive := False;
11571 if not Comes_From_Source (S) then
11573 -- Add an inherited primitive for an untagged derived type to
11574 -- Derived_Type's list of primitives. Tagged primitives are
11575 -- dealt with in Check_Dispatching_Operation. Do this even when
11576 -- Extensions_Allowed is False to issue better error messages.
11578 if Present (Derived_Type)
11579 and then not Is_Tagged_Type (Derived_Type)
11580 then
11581 Append_Unique_Elmt (S, Primitive_Operations (Derived_Type));
11582 end if;
11584 -- If subprogram is at library level, it is not primitive operation
11586 elsif Current_Scope = Standard_Standard then
11587 null;
11589 elsif (Is_Package_Or_Generic_Package (Current_Scope)
11590 and then not In_Package_Body (Current_Scope))
11591 or else Is_Overriding
11592 then
11593 -- For function, check return type
11595 if Ekind (S) = E_Function then
11596 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
11597 F_Typ := Designated_Type (Etype (S));
11598 else
11599 F_Typ := Etype (S);
11600 end if;
11602 B_Typ := Base_Type (F_Typ);
11604 if Scope (B_Typ) = Current_Scope
11605 and then not Is_Class_Wide_Type (B_Typ)
11606 and then not Is_Generic_Type (B_Typ)
11607 then
11608 Is_Primitive := True;
11609 Set_Has_Primitive_Operations (B_Typ);
11610 Set_Is_Primitive (S);
11612 -- Add a primitive for an untagged type to B_Typ's
11613 -- list of primitives. Tagged primitives are dealt with
11614 -- in Check_Dispatching_Operation. Do this even when
11615 -- Extensions_Allowed is False to issue better error
11616 -- messages.
11618 if not Is_Tagged_Type (B_Typ) then
11619 Add_Or_Replace_Untagged_Primitive (B_Typ);
11620 end if;
11622 Check_Private_Overriding (B_Typ);
11623 -- The Ghost policy in effect at the point of declaration
11624 -- or a tagged type and a primitive operation must match
11625 -- (SPARK RM 6.9(16)).
11627 Check_Ghost_Primitive (S, B_Typ);
11628 end if;
11629 end if;
11631 -- For all subprograms, check formals
11633 Formal := First_Formal (S);
11634 while Present (Formal) loop
11635 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
11636 F_Typ := Designated_Type (Etype (Formal));
11637 else
11638 F_Typ := Etype (Formal);
11639 end if;
11641 B_Typ := Base_Type (F_Typ);
11643 if Ekind (B_Typ) = E_Access_Subtype then
11644 B_Typ := Base_Type (B_Typ);
11645 end if;
11647 if Scope (B_Typ) = Current_Scope
11648 and then not Is_Class_Wide_Type (B_Typ)
11649 and then not Is_Generic_Type (B_Typ)
11650 then
11651 Is_Primitive := True;
11652 Set_Is_Primitive (S);
11653 Set_Has_Primitive_Operations (B_Typ);
11655 -- Add a primitive for an untagged type to B_Typ's list
11656 -- of primitives. Tagged primitives are dealt with in
11657 -- Check_Dispatching_Operation. Do this even when
11658 -- Extensions_Allowed is False to issue better error
11659 -- messages.
11661 if not Is_Tagged_Type (B_Typ) then
11662 Add_Or_Replace_Untagged_Primitive (B_Typ);
11663 end if;
11665 Check_Private_Overriding (B_Typ);
11667 -- The Ghost policy in effect at the point of declaration
11668 -- of a tagged type and a primitive operation must match
11669 -- (SPARK RM 6.9(16)).
11671 Check_Ghost_Primitive (S, B_Typ);
11672 end if;
11674 Next_Formal (Formal);
11675 end loop;
11677 -- Special case: An equality function can be redefined for a type
11678 -- occurring in a declarative part, and won't otherwise be treated as
11679 -- a primitive because it doesn't occur in a package spec and doesn't
11680 -- override an inherited subprogram. It's important that we mark it
11681 -- primitive so it can be returned by Collect_Primitive_Operations
11682 -- and be used in composing the equality operation of later types
11683 -- that have a component of the type.
11685 elsif Chars (S) = Name_Op_Eq
11686 and then Etype (S) = Standard_Boolean
11687 then
11688 B_Typ := Base_Type (Etype (First_Formal (S)));
11690 if Scope (B_Typ) = Current_Scope
11691 and then
11692 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
11693 and then not Is_Limited_Type (B_Typ)
11694 then
11695 Is_Primitive := True;
11696 Set_Is_Primitive (S);
11697 Set_Has_Primitive_Operations (B_Typ);
11698 Check_Private_Overriding (B_Typ);
11700 -- The Ghost policy in effect at the point of declaration of a
11701 -- tagged type and a primitive operation must match
11702 -- (SPARK RM 6.9(16)).
11704 Check_Ghost_Primitive (S, B_Typ);
11705 end if;
11706 end if;
11707 end Check_For_Primitive_Subprogram;
11709 --------------------------------------
11710 -- Has_Matching_Entry_Or_Subprogram --
11711 --------------------------------------
11713 function Has_Matching_Entry_Or_Subprogram
11714 (E : Entity_Id) return Boolean
11716 function Check_Conforming_Parameters
11717 (E1_Param : Node_Id;
11718 E2_Param : Node_Id;
11719 Ctype : Conformance_Type) return Boolean;
11720 -- Starting from the given parameters, check that all the parameters
11721 -- of two entries or subprograms are conformant. Used to skip
11722 -- the check on the controlling argument.
11724 function Matching_Entry_Or_Subprogram
11725 (Conc_Typ : Entity_Id;
11726 Subp : Entity_Id) return Entity_Id;
11727 -- Return the first entry or subprogram of the given concurrent type
11728 -- whose name matches the name of Subp and has a profile conformant
11729 -- with Subp; return Empty if not found.
11731 function Matching_Dispatching_Subprogram
11732 (Conc_Typ : Entity_Id;
11733 Ent : Entity_Id) return Entity_Id;
11734 -- Return the first dispatching primitive of Conc_Type defined in the
11735 -- enclosing scope of Conc_Type (i.e. before the full definition of
11736 -- this concurrent type) whose name matches the entry Ent and has a
11737 -- profile conformant with the profile of the corresponding (not yet
11738 -- built) dispatching primitive of Ent; return Empty if not found.
11740 function Matching_Original_Protected_Subprogram
11741 (Prot_Typ : Entity_Id;
11742 Subp : Entity_Id) return Entity_Id;
11743 -- Return the first subprogram defined in the enclosing scope of
11744 -- Prot_Typ (before the full definition of this protected type)
11745 -- whose name matches the original name of Subp and has a profile
11746 -- conformant with the profile of Subp; return Empty if not found.
11748 function Normalized_First_Parameter_Type
11749 (E : Entity_Id) return Entity_Id;
11750 -- Return the type of the first parameter unless that type
11751 -- is an anonymous access type, in which case return the
11752 -- designated type. Used to treat anonymous-access-to-synchronized
11753 -- the same as synchronized for purposes of checking for
11754 -- prefixed view profile conflicts.
11756 ---------------------------------
11757 -- Check_Conforming_Parameters --
11758 ---------------------------------
11760 function Check_Conforming_Parameters
11761 (E1_Param : Node_Id;
11762 E2_Param : Node_Id;
11763 Ctype : Conformance_Type) return Boolean
11765 Param_E1 : Node_Id := E1_Param;
11766 Param_E2 : Node_Id := E2_Param;
11768 begin
11769 while Present (Param_E1) and then Present (Param_E2) loop
11770 if Ctype >= Mode_Conformant and then
11771 Ekind (Defining_Identifier (Param_E1)) /=
11772 Ekind (Defining_Identifier (Param_E2))
11773 then
11774 return False;
11775 elsif not
11776 Conforming_Types
11777 (Find_Parameter_Type (Param_E1),
11778 Find_Parameter_Type (Param_E2),
11779 Ctype)
11780 then
11781 return False;
11782 end if;
11784 Next (Param_E1);
11785 Next (Param_E2);
11786 end loop;
11788 -- The candidate is not valid if one of the two lists contains
11789 -- more parameters than the other
11791 return No (Param_E1) and then No (Param_E2);
11792 end Check_Conforming_Parameters;
11794 ----------------------------------
11795 -- Matching_Entry_Or_Subprogram --
11796 ----------------------------------
11798 function Matching_Entry_Or_Subprogram
11799 (Conc_Typ : Entity_Id;
11800 Subp : Entity_Id) return Entity_Id
11802 E : Entity_Id;
11804 begin
11805 E := First_Entity (Conc_Typ);
11806 while Present (E) loop
11807 if Chars (Subp) = Chars (E)
11808 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
11809 and then
11810 Check_Conforming_Parameters
11811 (First (Parameter_Specifications (Parent (E))),
11812 Next (First (Parameter_Specifications (Parent (Subp)))),
11813 Type_Conformant)
11814 then
11815 return E;
11816 end if;
11818 Next_Entity (E);
11819 end loop;
11821 return Empty;
11822 end Matching_Entry_Or_Subprogram;
11824 -------------------------------------
11825 -- Matching_Dispatching_Subprogram --
11826 -------------------------------------
11828 function Matching_Dispatching_Subprogram
11829 (Conc_Typ : Entity_Id;
11830 Ent : Entity_Id) return Entity_Id
11832 E : Entity_Id;
11834 begin
11835 -- Search for entities in the enclosing scope of this synchronized
11836 -- type.
11838 pragma Assert (Is_Concurrent_Type (Conc_Typ));
11839 Push_Scope (Scope (Conc_Typ));
11840 E := Current_Entity_In_Scope (Ent);
11841 Pop_Scope;
11843 while Present (E) loop
11844 if Scope (E) = Scope (Conc_Typ)
11845 and then Comes_From_Source (E)
11846 and then Ekind (E) = E_Procedure
11847 and then Present (First_Entity (E))
11848 and then Is_Controlling_Formal (First_Entity (E))
11849 and then Etype (First_Entity (E)) = Conc_Typ
11850 and then
11851 Check_Conforming_Parameters
11852 (First (Parameter_Specifications (Parent (Ent))),
11853 Next (First (Parameter_Specifications (Parent (E)))),
11854 Subtype_Conformant)
11855 then
11856 return E;
11857 end if;
11859 E := Homonym (E);
11860 end loop;
11862 return Empty;
11863 end Matching_Dispatching_Subprogram;
11865 --------------------------------------------
11866 -- Matching_Original_Protected_Subprogram --
11867 --------------------------------------------
11869 function Matching_Original_Protected_Subprogram
11870 (Prot_Typ : Entity_Id;
11871 Subp : Entity_Id) return Entity_Id
11873 ICF : constant Boolean :=
11874 Is_Controlling_Formal (First_Entity (Subp));
11875 E : Entity_Id;
11877 begin
11878 -- Temporarily decorate the first parameter of Subp as controlling
11879 -- formal, required to invoke Subtype_Conformant.
11881 Set_Is_Controlling_Formal (First_Entity (Subp));
11883 E :=
11884 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
11886 while Present (E) loop
11887 if Scope (E) = Scope (Prot_Typ)
11888 and then Comes_From_Source (E)
11889 and then Ekind (Subp) = Ekind (E)
11890 and then Present (First_Entity (E))
11891 and then Is_Controlling_Formal (First_Entity (E))
11892 and then Etype (First_Entity (E)) = Prot_Typ
11893 and then Subtype_Conformant (Subp, E,
11894 Skip_Controlling_Formals => True)
11895 then
11896 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11897 return E;
11898 end if;
11900 E := Homonym (E);
11901 end loop;
11903 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11905 return Empty;
11906 end Matching_Original_Protected_Subprogram;
11908 -------------------------------------
11909 -- Normalized_First_Parameter_Type --
11910 -------------------------------------
11912 function Normalized_First_Parameter_Type
11913 (E : Entity_Id) return Entity_Id
11915 Result : Entity_Id := Etype (First_Entity (E));
11916 begin
11917 if Ekind (Result) = E_Anonymous_Access_Type then
11918 Result := Designated_Type (Result);
11919 end if;
11920 return Result;
11921 end Normalized_First_Parameter_Type;
11923 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11925 begin
11926 -- Case 1: E is a subprogram whose first formal is a concurrent type
11927 -- defined in the scope of E that has an entry or subprogram whose
11928 -- profile matches E.
11930 if Comes_From_Source (E)
11931 and then Is_Subprogram (E)
11932 and then Present (First_Entity (E))
11933 and then Is_Concurrent_Record_Type
11934 (Normalized_First_Parameter_Type (E))
11935 then
11936 if Scope (E) =
11937 Scope (Corresponding_Concurrent_Type
11938 (Normalized_First_Parameter_Type (E)))
11939 and then
11940 Present
11941 (Matching_Entry_Or_Subprogram
11942 (Corresponding_Concurrent_Type
11943 (Normalized_First_Parameter_Type (E)),
11944 Subp => E))
11945 then
11946 Report_Conflict (E,
11947 Matching_Entry_Or_Subprogram
11948 (Corresponding_Concurrent_Type
11949 (Normalized_First_Parameter_Type (E)),
11950 Subp => E));
11951 return True;
11952 end if;
11954 -- Case 2: E is an internally built dispatching subprogram of a
11955 -- protected type and there is a subprogram defined in the enclosing
11956 -- scope of the protected type that has the original name of E and
11957 -- its profile is conformant with the profile of E. We check the
11958 -- name of the original protected subprogram associated with E since
11959 -- the expander builds dispatching primitives of protected functions
11960 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11962 elsif not Comes_From_Source (E)
11963 and then Is_Subprogram (E)
11964 and then Present (First_Entity (E))
11965 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11966 and then Present (Original_Protected_Subprogram (E))
11967 and then
11968 Present
11969 (Matching_Original_Protected_Subprogram
11970 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11971 Subp => E))
11972 then
11973 Report_Conflict (E,
11974 Matching_Original_Protected_Subprogram
11975 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11976 Subp => E));
11977 return True;
11979 -- Case 3: E is an entry of a synchronized type and a matching
11980 -- procedure has been previously defined in the enclosing scope
11981 -- of the synchronized type.
11983 elsif Comes_From_Source (E)
11984 and then Ekind (E) = E_Entry
11985 and then
11986 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11987 then
11988 Report_Conflict (E,
11989 Matching_Dispatching_Subprogram (Current_Scope, E));
11990 return True;
11991 end if;
11993 return False;
11994 end Has_Matching_Entry_Or_Subprogram;
11996 ----------------------------
11997 -- Is_Private_Declaration --
11998 ----------------------------
12000 function Is_Private_Declaration (E : Entity_Id) return Boolean is
12001 Decl : constant Node_Id := Unit_Declaration_Node (E);
12002 Priv_Decls : List_Id;
12004 begin
12005 if Is_Package_Or_Generic_Package (Current_Scope)
12006 and then In_Private_Part (Current_Scope)
12007 then
12008 Priv_Decls :=
12009 Private_Declarations (Package_Specification (Current_Scope));
12011 return In_Package_Body (Current_Scope)
12012 or else
12013 (Is_List_Member (Decl)
12014 and then List_Containing (Decl) = Priv_Decls)
12015 or else (Nkind (Parent (Decl)) = N_Package_Specification
12016 and then not
12017 Is_Compilation_Unit
12018 (Defining_Entity (Parent (Decl)))
12019 and then List_Containing (Parent (Parent (Decl))) =
12020 Priv_Decls);
12021 else
12022 return False;
12023 end if;
12024 end Is_Private_Declaration;
12026 --------------------------
12027 -- Is_Overriding_Alias --
12028 --------------------------
12030 function Is_Overriding_Alias
12031 (Old_E : Entity_Id;
12032 New_E : Entity_Id) return Boolean
12034 AO : constant Entity_Id := Alias (Old_E);
12035 AN : constant Entity_Id := Alias (New_E);
12037 begin
12038 return Scope (AO) /= Scope (AN)
12039 or else No (DTC_Entity (AO))
12040 or else No (DTC_Entity (AN))
12041 or else DT_Position (AO) = DT_Position (AN);
12042 end Is_Overriding_Alias;
12044 ---------------------
12045 -- Report_Conflict --
12046 ---------------------
12048 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
12049 begin
12050 Error_Msg_Sloc := Sloc (E);
12052 -- Generate message, with useful additional warning if in generic
12054 if Is_Generic_Unit (E) then
12055 Error_Msg_N ("previous generic unit cannot be overloaded", S);
12056 Error_Msg_N ("\& conflicts with declaration#", S);
12057 else
12058 Error_Msg_N ("& conflicts with declaration#", S);
12059 end if;
12060 end Report_Conflict;
12062 -- Start of processing for New_Overloaded_Entity
12064 begin
12065 -- We need to look for an entity that S may override. This must be a
12066 -- homonym in the current scope, so we look for the first homonym of
12067 -- S in the current scope as the starting point for the search.
12069 E := Current_Entity_In_Scope (S);
12071 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
12072 -- They are directly added to the list of primitive operations of
12073 -- Derived_Type, unless this is a rederivation in the private part
12074 -- of an operation that was already derived in the visible part of
12075 -- the current package.
12077 if Ada_Version >= Ada_2005
12078 and then Present (Derived_Type)
12079 and then Present (Alias (S))
12080 and then Is_Dispatching_Operation (Alias (S))
12081 and then Present (Find_Dispatching_Type (Alias (S)))
12082 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
12083 then
12084 -- For private types, when the full-view is processed we propagate to
12085 -- the full view the non-overridden entities whose attribute "alias"
12086 -- references an interface primitive. These entities were added by
12087 -- Derive_Subprograms to ensure that interface primitives are
12088 -- covered.
12090 -- Inside_Freeze_Actions is non zero when S corresponds with an
12091 -- internal entity that links an interface primitive with its
12092 -- covering primitive through attribute Interface_Alias (see
12093 -- Add_Internal_Interface_Entities).
12095 if Inside_Freezing_Actions = 0
12096 and then Is_Package_Or_Generic_Package (Current_Scope)
12097 and then In_Private_Part (Current_Scope)
12098 and then Parent_Kind (E) = N_Private_Extension_Declaration
12099 and then Nkind (Parent (S)) = N_Full_Type_Declaration
12100 and then Full_View (Defining_Identifier (Parent (E)))
12101 = Defining_Identifier (Parent (S))
12102 and then Alias (E) = Alias (S)
12103 then
12104 Check_Operation_From_Private_View (S, E);
12105 Set_Is_Dispatching_Operation (S);
12107 -- Common case
12109 else
12110 Enter_Overloaded_Entity (S);
12111 Check_Dispatching_Operation (S, Empty);
12112 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
12113 end if;
12115 return;
12116 end if;
12118 -- For synchronized types check conflicts of this entity with previously
12119 -- defined entities.
12121 if Ada_Version >= Ada_2005
12122 and then Has_Matching_Entry_Or_Subprogram (S)
12123 then
12124 return;
12125 end if;
12127 -- If there is no homonym then this is definitely not overriding
12129 if No (E) then
12130 Enter_Overloaded_Entity (S);
12131 Check_Dispatching_Operation (S, Empty);
12132 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
12134 -- If subprogram has an explicit declaration, check whether it has an
12135 -- overriding indicator.
12137 if Comes_From_Source (S) then
12138 Check_Synchronized_Overriding (S, Overridden_Subp);
12140 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
12141 -- it may have overridden some hidden inherited primitive. Update
12142 -- Overridden_Subp to avoid spurious errors when checking the
12143 -- overriding indicator.
12145 if Ada_Version >= Ada_2012
12146 and then No (Overridden_Subp)
12147 and then Is_Dispatching_Operation (S)
12148 and then Present (Overridden_Operation (S))
12149 then
12150 Overridden_Subp := Overridden_Operation (S);
12151 end if;
12153 Check_Overriding_Indicator
12154 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
12156 -- The Ghost policy in effect at the point of declaration of a
12157 -- parent subprogram and an overriding subprogram must match
12158 -- (SPARK RM 6.9(17)).
12160 Check_Ghost_Overriding (S, Overridden_Subp);
12161 end if;
12163 -- If there is a homonym that is not overloadable, then we have an
12164 -- error, except for the special cases checked explicitly below.
12166 elsif not Is_Overloadable (E) then
12168 -- Check for spurious conflict produced by a subprogram that has the
12169 -- same name as that of the enclosing generic package. The conflict
12170 -- occurs within an instance, between the subprogram and the renaming
12171 -- declaration for the package. After the subprogram, the package
12172 -- renaming declaration becomes hidden.
12174 if Ekind (E) = E_Package
12175 and then Present (Renamed_Entity (E))
12176 and then Renamed_Entity (E) = Current_Scope
12177 and then Nkind (Parent (Renamed_Entity (E))) =
12178 N_Package_Specification
12179 and then Present (Generic_Parent (Parent (Renamed_Entity (E))))
12180 then
12181 Set_Is_Hidden (E);
12182 Set_Is_Immediately_Visible (E, False);
12183 Enter_Overloaded_Entity (S);
12184 Set_Homonym (S, Homonym (E));
12185 Check_Dispatching_Operation (S, Empty);
12186 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
12188 -- If the subprogram is implicit it is hidden by the previous
12189 -- declaration. However if it is dispatching, it must appear in the
12190 -- dispatch table anyway, because it can be dispatched to even if it
12191 -- cannot be called directly.
12193 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
12194 Set_Scope (S, Current_Scope);
12196 if Is_Dispatching_Operation (Alias (S)) then
12197 Check_Dispatching_Operation (S, Empty);
12198 end if;
12200 return;
12202 else
12203 Report_Conflict (S, E);
12204 return;
12205 end if;
12207 -- E exists and is overloadable
12209 else
12210 Check_Synchronized_Overriding (S, Overridden_Subp);
12212 -- Loop through E and its homonyms to determine if any of them is
12213 -- the candidate for overriding by S.
12215 while Present (E) loop
12217 -- Definitely not interesting if not in the current scope
12219 if Scope (E) /= Current_Scope then
12220 null;
12222 -- A function can overload the name of an abstract state. The
12223 -- state can be viewed as a function with a profile that cannot
12224 -- be matched by anything.
12226 elsif Ekind (S) = E_Function
12227 and then Ekind (E) = E_Abstract_State
12228 then
12229 Enter_Overloaded_Entity (S);
12230 return;
12232 -- Ada 2012 (AI05-0165): For internally generated bodies of null
12233 -- procedures locate the internally generated spec. We enforce
12234 -- mode conformance since a tagged type may inherit from
12235 -- interfaces several null primitives which differ only in
12236 -- the mode of the formals.
12238 elsif not Comes_From_Source (S)
12239 and then Is_Null_Procedure (S)
12240 and then not Mode_Conformant (E, S)
12241 then
12242 null;
12244 -- Check if we have type conformance
12246 elsif Type_Conformant (E, S) then
12248 -- If the old and new entities have the same profile and one
12249 -- is not the body of the other, then this is an error, unless
12250 -- one of them is implicitly declared.
12252 -- There are some cases when both can be implicit, for example
12253 -- when both a literal and a function that overrides it are
12254 -- inherited in a derivation, or when an inherited operation
12255 -- of a tagged full type overrides the inherited operation of
12256 -- a private extension. Ada 83 had a special rule for the
12257 -- literal case. In Ada 95, the later implicit operation hides
12258 -- the former, and the literal is always the former. In the
12259 -- odd case where both are derived operations declared at the
12260 -- same point, both operations should be declared, and in that
12261 -- case we bypass the following test and proceed to the next
12262 -- part. This can only occur for certain obscure cases in
12263 -- instances, when an operation on a type derived from a formal
12264 -- private type does not override a homograph inherited from
12265 -- the actual. In subsequent derivations of such a type, the
12266 -- DT positions of these operations remain distinct, if they
12267 -- have been set.
12269 if Present (Alias (S))
12270 and then (No (Alias (E))
12271 or else Comes_From_Source (E)
12272 or else Is_Abstract_Subprogram (S)
12273 or else
12274 (Is_Dispatching_Operation (E)
12275 and then Is_Overriding_Alias (E, S)))
12276 and then Ekind (E) /= E_Enumeration_Literal
12277 then
12278 -- When an derived operation is overloaded it may be due to
12279 -- the fact that the full view of a private extension
12280 -- re-inherits. It has to be dealt with.
12282 if Is_Package_Or_Generic_Package (Current_Scope)
12283 and then In_Private_Part (Current_Scope)
12284 then
12285 Check_Operation_From_Private_View (S, E);
12286 end if;
12288 -- In any case the implicit operation remains hidden by the
12289 -- existing declaration, which is overriding. Indicate that
12290 -- E overrides the operation from which S is inherited.
12292 if Present (Alias (S)) then
12293 Set_Overridden_Operation (E, Alias (S));
12294 Inherit_Subprogram_Contract (E, Alias (S));
12295 Set_Is_Ada_2022_Only (E,
12296 Is_Ada_2022_Only (Alias (S)));
12298 else
12299 Set_Overridden_Operation (E, S);
12300 Inherit_Subprogram_Contract (E, S);
12301 Set_Is_Ada_2022_Only (E, Is_Ada_2022_Only (S));
12302 end if;
12304 -- When a dispatching operation overrides an inherited
12305 -- subprogram, it shall be subtype conformant with the
12306 -- inherited subprogram (RM 3.9.2 (10.2)).
12308 if Comes_From_Source (E)
12309 and then Is_Dispatching_Operation (E)
12310 and then Find_Dispatching_Type (S)
12311 = Find_Dispatching_Type (E)
12312 then
12313 Check_Subtype_Conformant (E, S);
12314 end if;
12316 if Comes_From_Source (E) then
12317 Check_Overriding_Indicator (E, S, Is_Primitive => False);
12319 -- The Ghost policy in effect at the point of declaration
12320 -- of a parent subprogram and an overriding subprogram
12321 -- must match (SPARK RM 6.9(17)).
12323 Check_Ghost_Overriding (E, S);
12324 end if;
12326 return;
12328 -- Within an instance, the renaming declarations for actual
12329 -- subprograms may become ambiguous, but they do not hide each
12330 -- other.
12332 elsif Ekind (E) /= E_Entry
12333 and then not Comes_From_Source (E)
12334 and then not Is_Generic_Instance (E)
12335 and then (Present (Alias (E))
12336 or else Is_Intrinsic_Subprogram (E))
12337 and then (not In_Instance
12338 or else No (Parent (E))
12339 or else Nkind (Unit_Declaration_Node (E)) /=
12340 N_Subprogram_Renaming_Declaration)
12341 then
12342 -- A subprogram child unit is not allowed to override an
12343 -- inherited subprogram (10.1.1(20)).
12345 if Is_Child_Unit (S) then
12346 Error_Msg_N
12347 ("child unit overrides inherited subprogram in parent",
12349 return;
12350 end if;
12352 if Is_Non_Overriding_Operation (E, S) then
12353 Enter_Overloaded_Entity (S);
12355 if No (Derived_Type)
12356 or else Is_Tagged_Type (Derived_Type)
12357 then
12358 Check_Dispatching_Operation (S, Empty);
12359 end if;
12361 return;
12362 end if;
12364 -- E is a derived operation or an internal operator which
12365 -- is being overridden. Remove E from further visibility.
12366 -- Furthermore, if E is a dispatching operation, it must be
12367 -- replaced in the list of primitive operations of its type
12368 -- (see Override_Dispatching_Operation).
12370 Overridden_Subp := E;
12372 -- It is possible for E to be in the current scope and
12373 -- yet not in the entity chain. This can only occur in a
12374 -- generic context where E is an implicit concatenation
12375 -- in the formal part, because in a generic body the
12376 -- entity chain starts with the formals.
12378 -- In GNATprove mode, a wrapper for an operation with
12379 -- axiomatization may be a homonym of another declaration
12380 -- for an actual subprogram (needs refinement ???).
12382 if No (Prev_Entity (E)) then
12383 if In_Instance
12384 and then GNATprove_Mode
12385 and then
12386 Nkind (Original_Node (Unit_Declaration_Node (S))) =
12387 N_Subprogram_Renaming_Declaration
12388 then
12389 return;
12390 else
12391 pragma Assert (Chars (E) = Name_Op_Concat);
12392 null;
12393 end if;
12394 end if;
12396 -- E must be removed both from the entity_list of the
12397 -- current scope, and from the visibility chain.
12399 if Debug_Flag_E then
12400 Write_Str ("Override implicit operation ");
12401 Write_Int (Int (E));
12402 Write_Eol;
12403 end if;
12405 -- If E is a predefined concatenation, it stands for four
12406 -- different operations. As a result, a single explicit
12407 -- declaration does not hide it. In a possible ambiguous
12408 -- situation, Disambiguate chooses the user-defined op,
12409 -- so it is correct to retain the previous internal one.
12411 if Chars (E) /= Name_Op_Concat
12412 or else Ekind (E) /= E_Operator
12413 then
12414 -- For nondispatching derived operations that are
12415 -- overridden by a subprogram declared in the private
12416 -- part of a package, we retain the derived subprogram
12417 -- but mark it as not immediately visible. If the
12418 -- derived operation was declared in the visible part
12419 -- then this ensures that it will still be visible
12420 -- outside the package with the proper signature
12421 -- (calls from outside must also be directed to this
12422 -- version rather than the overriding one, unlike the
12423 -- dispatching case). Calls from inside the package
12424 -- will still resolve to the overriding subprogram
12425 -- since the derived one is marked as not visible
12426 -- within the package.
12428 -- If the private operation is dispatching, we achieve
12429 -- the overriding by keeping the implicit operation
12430 -- but setting its alias to be the overriding one. In
12431 -- this fashion the proper body is executed in all
12432 -- cases, but the original signature is used outside
12433 -- of the package.
12435 -- If the overriding is not in the private part, we
12436 -- remove the implicit operation altogether.
12438 if Is_Private_Declaration (S) then
12439 if not Is_Dispatching_Operation (E) then
12440 Set_Is_Immediately_Visible (E, False);
12441 else
12442 -- Work done in Override_Dispatching_Operation, so
12443 -- nothing else needs to be done here.
12445 -- ??? Special case to keep supporting the hiding
12446 -- of the predefined "=" operator for a nonlimited
12447 -- tagged type by a user-defined "=" operator for
12448 -- its class-wide type when the type is private.
12450 if Chars (E) = Name_Op_Eq then
12451 declare
12452 Typ : constant Entity_Id
12453 := Etype (First_Entity (E));
12454 H : Entity_Id := Homonym (E);
12456 begin
12457 while Present (H)
12458 and then Scope (H) = Scope (E)
12459 loop
12460 if Is_User_Defined_Equality (H)
12461 and then Is_Immediately_Visible (H)
12462 and then Etype (First_Entity (H))
12463 = Class_Wide_Type (Typ)
12464 then
12465 Remove_Entity_And_Homonym (E);
12466 exit;
12467 end if;
12469 H := Homonym (H);
12470 end loop;
12471 end;
12472 end if;
12473 end if;
12475 else
12476 Remove_Entity_And_Homonym (E);
12477 end if;
12478 end if;
12480 Enter_Overloaded_Entity (S);
12482 -- For entities generated by Derive_Subprograms the
12483 -- overridden operation is the inherited primitive
12484 -- (which is available through the attribute alias).
12486 if not (Comes_From_Source (E))
12487 and then Is_Dispatching_Operation (E)
12488 and then Find_Dispatching_Type (E) =
12489 Find_Dispatching_Type (S)
12490 and then Present (Alias (E))
12491 and then Comes_From_Source (Alias (E))
12492 then
12493 Set_Overridden_Operation (S, Alias (E));
12494 Inherit_Subprogram_Contract (S, Alias (E));
12495 Set_Is_Ada_2022_Only (S,
12496 Is_Ada_2022_Only (Alias (E)));
12498 -- Normal case of setting entity as overridden
12500 -- Note: Static_Initialization and Overridden_Operation
12501 -- attributes use the same field in subprogram entities.
12502 -- Static_Initialization is only defined for internal
12503 -- initialization procedures, where Overridden_Operation
12504 -- is irrelevant. Therefore the setting of this attribute
12505 -- must check whether the target is an init_proc.
12507 elsif not Is_Init_Proc (S) then
12509 -- LSP wrappers must override the ultimate alias of their
12510 -- wrapped dispatching primitive E; required to traverse
12511 -- the chain of ancestor primitives (c.f. Map_Primitives)
12512 -- They don't inherit contracts.
12514 if Is_Wrapper (S)
12515 and then Present (LSP_Subprogram (S))
12516 then
12517 Set_Overridden_Operation (S, Ultimate_Alias (E));
12518 else
12519 Set_Overridden_Operation (S, E);
12520 Inherit_Subprogram_Contract (S, E);
12521 end if;
12523 Set_Is_Ada_2022_Only (S, Is_Ada_2022_Only (E));
12524 end if;
12526 Check_Overriding_Indicator (S, E, Is_Primitive => True);
12528 -- The Ghost policy in effect at the point of declaration
12529 -- of a parent subprogram and an overriding subprogram
12530 -- must match (SPARK RM 6.9(17)).
12532 Check_Ghost_Overriding (S, E);
12534 -- If S is a user-defined subprogram or a null procedure
12535 -- expanded to override an inherited null procedure, or a
12536 -- predefined dispatching primitive then indicate that E
12537 -- overrides the operation from which S is inherited.
12539 if Comes_From_Source (S)
12540 or else
12541 (Present (Parent (S))
12542 and then Nkind (Parent (S)) = N_Procedure_Specification
12543 and then Null_Present (Parent (S)))
12544 or else
12545 (Present (Alias (E))
12546 and then
12547 Is_Predefined_Dispatching_Operation (Alias (E)))
12548 then
12549 if Present (Alias (E)) then
12551 -- LSP wrappers must override the ultimate alias of
12552 -- their wrapped dispatching primitive E; required to
12553 -- traverse the chain of ancestor primitives (see
12554 -- Map_Primitives). They don't inherit contracts.
12556 if Is_Wrapper (S)
12557 and then Present (LSP_Subprogram (S))
12558 then
12559 Set_Overridden_Operation (S, Ultimate_Alias (E));
12560 else
12561 Set_Overridden_Operation (S, Alias (E));
12562 Inherit_Subprogram_Contract (S, Alias (E));
12563 end if;
12565 Set_Is_Ada_2022_Only (S, Is_Ada_2022_Only (Alias (E)));
12566 end if;
12567 end if;
12569 if Is_Dispatching_Operation (E) then
12571 -- An overriding dispatching subprogram inherits the
12572 -- convention of the overridden subprogram (AI95-117).
12574 Set_Convention (S, Convention (E));
12575 Check_Dispatching_Operation (S, E);
12577 else
12578 Check_Dispatching_Operation (S, Empty);
12579 end if;
12581 Check_For_Primitive_Subprogram
12582 (Is_Primitive_Subp, Is_Overriding => True);
12583 goto Check_Inequality;
12585 -- Apparent redeclarations in instances can occur when two
12586 -- formal types get the same actual type. The subprograms in
12587 -- in the instance are legal, even if not callable from the
12588 -- outside. Calls from within are disambiguated elsewhere.
12589 -- For dispatching operations in the visible part, the usual
12590 -- rules apply, and operations with the same profile are not
12591 -- legal (B830001).
12593 elsif (In_Instance_Visible_Part
12594 and then not Is_Dispatching_Operation (E))
12595 or else In_Instance_Not_Visible
12596 then
12597 null;
12599 -- Here we have a real error (identical profile)
12601 else
12602 Error_Msg_Sloc := Sloc (E);
12604 -- Avoid cascaded errors if the entity appears in
12605 -- subsequent calls.
12607 Set_Scope (S, Current_Scope);
12609 -- Generate error, with extra useful warning for the case
12610 -- of a generic instance with no completion.
12612 if Is_Generic_Instance (S)
12613 and then not Has_Completion (E)
12614 then
12615 Error_Msg_N
12616 ("instantiation cannot provide body for&", S);
12617 Error_Msg_N ("\& conflicts with declaration#", S);
12618 else
12619 Error_Msg_N ("& conflicts with declaration#", S);
12620 end if;
12622 return;
12623 end if;
12625 else
12626 -- If one subprogram has an access parameter and the other
12627 -- a parameter of an access type, calls to either might be
12628 -- ambiguous. Verify that parameters match except for the
12629 -- access parameter.
12631 if May_Hide_Profile then
12632 declare
12633 F1 : Entity_Id;
12634 F2 : Entity_Id;
12636 begin
12637 F1 := First_Formal (S);
12638 F2 := First_Formal (E);
12639 while Present (F1) and then Present (F2) loop
12640 if Is_Access_Type (Etype (F1)) then
12641 if not Is_Access_Type (Etype (F2))
12642 or else not Conforming_Types
12643 (Designated_Type (Etype (F1)),
12644 Designated_Type (Etype (F2)),
12645 Type_Conformant)
12646 then
12647 May_Hide_Profile := False;
12648 end if;
12650 elsif
12651 not Conforming_Types
12652 (Etype (F1), Etype (F2), Type_Conformant)
12653 then
12654 May_Hide_Profile := False;
12655 end if;
12657 Next_Formal (F1);
12658 Next_Formal (F2);
12659 end loop;
12661 if May_Hide_Profile
12662 and then No (F1)
12663 and then No (F2)
12664 then
12665 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
12666 end if;
12667 end;
12668 end if;
12669 end if;
12671 E := Homonym (E);
12672 end loop;
12674 -- On exit, we know that S is a new entity
12676 Enter_Overloaded_Entity (S);
12677 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
12678 Check_Overriding_Indicator
12679 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
12681 -- The Ghost policy in effect at the point of declaration of a parent
12682 -- subprogram and an overriding subprogram must match
12683 -- (SPARK RM 6.9(17)).
12685 Check_Ghost_Overriding (S, Overridden_Subp);
12687 -- If S is a derived operation for an untagged type then by
12688 -- definition it's not a dispatching operation (even if the parent
12689 -- operation was dispatching), so Check_Dispatching_Operation is not
12690 -- called in that case.
12692 if No (Derived_Type)
12693 or else Is_Tagged_Type (Derived_Type)
12694 then
12695 Check_Dispatching_Operation (S, Empty);
12696 end if;
12697 end if;
12699 -- If this is a user-defined equality operator that is not a derived
12700 -- subprogram, create the corresponding inequality. If the operation is
12701 -- dispatching, the expansion is done elsewhere, and we do not create
12702 -- an explicit inequality operation.
12704 <<Check_Inequality>>
12705 if Chars (S) = Name_Op_Eq
12706 and then Etype (S) = Standard_Boolean
12707 and then Present (Parent (S))
12708 and then not Is_Dispatching_Operation (S)
12709 then
12710 Make_Inequality_Operator (S);
12711 Check_Untagged_Equality (S);
12712 end if;
12713 end New_Overloaded_Entity;
12715 ----------------------------------
12716 -- Preanalyze_Formal_Expression --
12717 ----------------------------------
12719 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
12720 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
12721 begin
12722 In_Spec_Expression := True;
12723 Preanalyze_With_Freezing_And_Resolve (N, T);
12724 In_Spec_Expression := Save_In_Spec_Expression;
12725 end Preanalyze_Formal_Expression;
12727 ---------------------
12728 -- Process_Formals --
12729 ---------------------
12731 procedure Process_Formals
12732 (T : List_Id;
12733 Related_Nod : Node_Id)
12735 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
12736 -- Determine whether an access type designates a type coming from a
12737 -- limited view.
12739 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
12740 -- Check whether the default has a class-wide type. After analysis the
12741 -- default has the type of the formal, so we must also check explicitly
12742 -- for an access attribute.
12744 ----------------------------------
12745 -- Designates_From_Limited_With --
12746 ----------------------------------
12748 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
12749 Desig : Entity_Id := Typ;
12751 begin
12752 if Is_Access_Type (Desig) then
12753 Desig := Directly_Designated_Type (Desig);
12754 end if;
12756 if Is_Class_Wide_Type (Desig) then
12757 Desig := Root_Type (Desig);
12758 end if;
12760 return
12761 Ekind (Desig) = E_Incomplete_Type
12762 and then From_Limited_With (Desig);
12763 end Designates_From_Limited_With;
12765 ---------------------------
12766 -- Is_Class_Wide_Default --
12767 ---------------------------
12769 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
12770 begin
12771 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
12772 or else (Nkind (D) = N_Attribute_Reference
12773 and then Attribute_Name (D) = Name_Access
12774 and then Is_Class_Wide_Type (Etype (Prefix (D))));
12775 end Is_Class_Wide_Default;
12777 -- Local variables
12779 Context : constant Node_Id := Parent (Parent (T));
12780 Default : Node_Id;
12781 Formal : Entity_Id;
12782 Formal_Type : Entity_Id;
12783 Param_Spec : Node_Id;
12784 Ptype : Entity_Id;
12786 Num_Out_Params : Nat := 0;
12787 First_Out_Param : Entity_Id := Empty;
12788 -- Used for setting Is_Only_Out_Parameter
12790 -- Start of processing for Process_Formals
12792 begin
12793 -- In order to prevent premature use of the formals in the same formal
12794 -- part, the Ekind is left undefined until all default expressions are
12795 -- analyzed. The Ekind is established in a separate loop at the end.
12797 Param_Spec := First (T);
12798 while Present (Param_Spec) loop
12799 Formal := Defining_Identifier (Param_Spec);
12800 Set_Never_Set_In_Source (Formal, True);
12801 Enter_Name (Formal);
12803 -- Case of ordinary parameters
12805 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
12806 Find_Type (Parameter_Type (Param_Spec));
12807 Ptype := Parameter_Type (Param_Spec);
12809 if Ptype = Error then
12810 goto Continue;
12811 end if;
12813 -- Protect against malformed parameter types
12815 if Nkind (Ptype) not in N_Has_Entity then
12816 Formal_Type := Any_Type;
12817 else
12818 Formal_Type := Entity (Ptype);
12819 end if;
12821 if Is_Incomplete_Type (Formal_Type)
12822 or else
12823 (Is_Class_Wide_Type (Formal_Type)
12824 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
12825 then
12826 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
12827 -- primitive operations, as long as their completion is
12828 -- in the same declarative part. If in the private part
12829 -- this means that the type cannot be a Taft-amendment type.
12830 -- Check is done on package exit. For access to subprograms,
12831 -- the use is legal for Taft-amendment types.
12833 -- Ada 2012: tagged incomplete types are allowed as generic
12834 -- formal types. They do not introduce dependencies and the
12835 -- corresponding generic subprogram does not have a delayed
12836 -- freeze, because it does not need a freeze node. However,
12837 -- it is still the case that untagged incomplete types cannot
12838 -- be Taft-amendment types and must be completed in private
12839 -- part, so the subprogram must appear in the list of private
12840 -- dependents of the type.
12842 if Is_Tagged_Type (Formal_Type)
12843 or else (Ada_Version >= Ada_2012
12844 and then not From_Limited_With (Formal_Type)
12845 and then not Is_Generic_Type (Formal_Type))
12846 then
12847 if Ekind (Scope (Current_Scope)) = E_Package
12848 and then not Is_Generic_Type (Formal_Type)
12849 and then not Is_Class_Wide_Type (Formal_Type)
12850 then
12851 if Nkind (Parent (T)) not in
12852 N_Access_Function_Definition |
12853 N_Access_Procedure_Definition
12854 then
12855 Append_Elmt (Current_Scope,
12856 Private_Dependents (Base_Type (Formal_Type)));
12858 -- Freezing is delayed to ensure that Register_Prim
12859 -- will get called for this operation, which is needed
12860 -- in cases where static dispatch tables aren't built.
12861 -- (Note that the same is done for controlling access
12862 -- parameter cases in function Access_Definition.)
12864 if not Is_Thunk (Current_Scope) then
12865 Set_Has_Delayed_Freeze (Current_Scope);
12866 end if;
12867 end if;
12868 end if;
12870 elsif Nkind (Parent (T)) not in N_Access_Function_Definition
12871 | N_Access_Procedure_Definition
12872 then
12873 -- AI05-0151: Tagged incomplete types are allowed in all
12874 -- formal parts. Untagged incomplete types are not allowed
12875 -- in bodies. Limited views of either kind are not allowed
12876 -- if there is no place at which the non-limited view can
12877 -- become available.
12879 -- Incomplete formal untagged types are not allowed in
12880 -- subprogram bodies (but are legal in their declarations).
12881 -- This excludes bodies created for null procedures, which
12882 -- are basic declarations.
12884 if Is_Generic_Type (Formal_Type)
12885 and then not Is_Tagged_Type (Formal_Type)
12886 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
12887 then
12888 Error_Msg_N
12889 ("invalid use of formal incomplete type", Param_Spec);
12891 elsif Ada_Version >= Ada_2012 then
12892 if Is_Tagged_Type (Formal_Type)
12893 and then (not From_Limited_With (Formal_Type)
12894 or else not In_Package_Body)
12895 then
12896 null;
12898 elsif Nkind (Context) in N_Accept_Statement
12899 | N_Accept_Alternative
12900 | N_Entry_Body
12901 or else (Nkind (Context) = N_Subprogram_Body
12902 and then Comes_From_Source (Context))
12903 then
12904 Error_Msg_NE
12905 ("invalid use of untagged incomplete type &",
12906 Ptype, Formal_Type);
12907 end if;
12909 else
12910 Error_Msg_NE
12911 ("invalid use of incomplete type&",
12912 Param_Spec, Formal_Type);
12914 -- Further checks on the legality of incomplete types
12915 -- in formal parts are delayed until the freeze point
12916 -- of the enclosing subprogram or access to subprogram.
12917 end if;
12918 end if;
12920 elsif Ekind (Formal_Type) = E_Void then
12921 Error_Msg_NE
12922 ("premature use of&",
12923 Parameter_Type (Param_Spec), Formal_Type);
12924 end if;
12926 -- Ada 2012 (AI-142): Handle aliased parameters
12928 if Ada_Version >= Ada_2012
12929 and then Aliased_Present (Param_Spec)
12930 then
12931 Set_Is_Aliased (Formal);
12933 -- AI12-001: All aliased objects are considered to be specified
12934 -- as independently addressable (RM C.6(8.1/4)).
12936 Set_Is_Independent (Formal);
12937 end if;
12939 -- Ada 2005 (AI-231): Create and decorate an internal subtype
12940 -- declaration corresponding to the null-excluding type of the
12941 -- formal in the enclosing scope. Finally, replace the parameter
12942 -- type of the formal with the internal subtype.
12944 if Ada_Version >= Ada_2005
12945 and then Null_Exclusion_Present (Param_Spec)
12946 then
12947 if not Is_Access_Type (Formal_Type) then
12948 Error_Msg_N
12949 ("`NOT NULL` allowed only for an access type", Param_Spec);
12951 else
12952 if Can_Never_Be_Null (Formal_Type)
12953 and then Comes_From_Source (Related_Nod)
12954 then
12955 Error_Msg_NE
12956 ("`NOT NULL` not allowed (& already excludes null)",
12957 Param_Spec, Formal_Type);
12958 end if;
12960 Formal_Type :=
12961 Create_Null_Excluding_Itype
12962 (T => Formal_Type,
12963 Related_Nod => Related_Nod,
12964 Scope_Id => Scope (Current_Scope));
12966 -- If the designated type of the itype is an itype that is
12967 -- not frozen yet, we set the Has_Delayed_Freeze attribute
12968 -- on the access subtype, to prevent order-of-elaboration
12969 -- issues in the backend.
12971 -- Example:
12972 -- type T is access procedure;
12973 -- procedure Op (O : not null T);
12975 if Is_Itype (Directly_Designated_Type (Formal_Type))
12976 and then
12977 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12978 then
12979 Set_Has_Delayed_Freeze (Formal_Type);
12980 end if;
12981 end if;
12982 end if;
12984 -- An access formal type
12986 else
12987 Formal_Type :=
12988 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12990 -- No need to continue if we already notified errors
12992 if No (Formal_Type) then
12993 return;
12994 end if;
12996 -- Ada 2005 (AI-254)
12998 declare
12999 AD : constant Node_Id :=
13000 Access_To_Subprogram_Definition
13001 (Parameter_Type (Param_Spec));
13002 begin
13003 if Present (AD) and then Protected_Present (AD) then
13004 Formal_Type :=
13005 Replace_Anonymous_Access_To_Protected_Subprogram
13006 (Param_Spec);
13007 end if;
13008 end;
13009 end if;
13011 Set_Etype (Formal, Formal_Type);
13013 -- Deal with default expression if present
13015 Default := Expression (Param_Spec);
13017 if Present (Default) then
13018 if Out_Present (Param_Spec) then
13019 Error_Msg_N
13020 ("default initialization only allowed for IN parameters",
13021 Param_Spec);
13022 end if;
13024 -- Do the special preanalysis of the expression (see section on
13025 -- "Handling of Default Expressions" in the spec of package Sem).
13027 Preanalyze_Formal_Expression (Default, Formal_Type);
13029 -- An access to constant cannot be the default for
13030 -- an access parameter that is an access to variable.
13032 if Ekind (Formal_Type) = E_Anonymous_Access_Type
13033 and then not Is_Access_Constant (Formal_Type)
13034 and then Is_Access_Type (Etype (Default))
13035 and then Is_Access_Constant (Etype (Default))
13036 then
13037 Error_Msg_N
13038 ("formal that is access to variable cannot be initialized "
13039 & "with an access-to-constant expression", Default);
13040 end if;
13042 -- Check that the designated type of an access parameter's default
13043 -- is not a class-wide type unless the parameter's designated type
13044 -- is also class-wide.
13046 if Ekind (Formal_Type) = E_Anonymous_Access_Type
13047 and then not Designates_From_Limited_With (Formal_Type)
13048 and then Is_Class_Wide_Default (Default)
13049 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
13050 then
13051 Error_Msg_N
13052 ("access to class-wide expression not allowed here", Default);
13053 end if;
13055 -- Check incorrect use of dynamically tagged expressions
13057 if Is_Tagged_Type (Formal_Type) then
13058 Check_Dynamically_Tagged_Expression
13059 (Expr => Default,
13060 Typ => Formal_Type,
13061 Related_Nod => Default);
13062 end if;
13063 end if;
13065 -- Ada 2005 (AI-231): Static checks
13067 if Ada_Version >= Ada_2005
13068 and then Is_Access_Type (Etype (Formal))
13069 and then Can_Never_Be_Null (Etype (Formal))
13070 then
13071 Null_Exclusion_Static_Checks (Param_Spec);
13072 end if;
13074 -- The following checks are relevant only when SPARK_Mode is on as
13075 -- these are not standard Ada legality rules.
13077 if SPARK_Mode = On then
13078 if Ekind (Scope (Formal)) in E_Function | E_Generic_Function then
13080 -- A function cannot have a parameter of mode IN OUT or OUT
13081 -- (SPARK RM 6.1).
13083 if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then
13084 Error_Msg_N
13085 ("function cannot have parameter of mode `OUT` or "
13086 & "`IN OUT`", Formal);
13087 end if;
13089 -- A procedure cannot have an effectively volatile formal
13090 -- parameter of mode IN because it behaves as a constant
13091 -- (SPARK RM 7.1.3(4)).
13093 elsif Ekind (Scope (Formal)) = E_Procedure
13094 and then Ekind (Formal) = E_In_Parameter
13095 and then Is_Effectively_Volatile (Formal)
13096 then
13097 Error_Msg_N
13098 ("formal parameter of mode `IN` cannot be volatile", Formal);
13099 end if;
13100 end if;
13102 -- Deal with aspects on formal parameters. Only Unreferenced is
13103 -- supported for the time being.
13105 if Has_Aspects (Param_Spec) then
13106 declare
13107 Aspect : Node_Id := First (Aspect_Specifications (Param_Spec));
13108 begin
13109 while Present (Aspect) loop
13110 if Chars (Identifier (Aspect)) = Name_Unreferenced then
13111 Set_Has_Pragma_Unreferenced (Formal);
13112 else
13113 Error_Msg_NE
13114 ("unsupported aspect& on parameter",
13115 Aspect, Identifier (Aspect));
13116 end if;
13118 Next (Aspect);
13119 end loop;
13120 end;
13121 end if;
13123 <<Continue>>
13124 Next (Param_Spec);
13125 end loop;
13127 -- If this is the formal part of a function specification, analyze the
13128 -- subtype mark in the context where the formals are visible but not
13129 -- yet usable, and may hide outer homographs.
13131 if Nkind (Related_Nod) = N_Function_Specification then
13132 Analyze_Return_Type (Related_Nod);
13133 end if;
13135 -- Now set the kind (mode) of each formal
13137 Param_Spec := First (T);
13138 while Present (Param_Spec) loop
13139 Formal := Defining_Identifier (Param_Spec);
13140 Set_Formal_Mode (Formal);
13142 if Ekind (Formal) = E_In_Parameter then
13143 Default := Expression (Param_Spec);
13145 if Present (Default) then
13146 Set_Default_Value (Formal, Default);
13148 if Is_Scalar_Type (Etype (Default)) then
13149 if Nkind (Parameter_Type (Param_Spec)) /=
13150 N_Access_Definition
13151 then
13152 Formal_Type := Entity (Parameter_Type (Param_Spec));
13153 else
13154 Formal_Type :=
13155 Access_Definition
13156 (Related_Nod, Parameter_Type (Param_Spec));
13157 end if;
13159 Apply_Scalar_Range_Check (Default, Formal_Type);
13160 end if;
13161 end if;
13163 elsif Ekind (Formal) = E_Out_Parameter then
13164 Num_Out_Params := Num_Out_Params + 1;
13166 if Num_Out_Params = 1 then
13167 First_Out_Param := Formal;
13168 end if;
13170 elsif Ekind (Formal) = E_In_Out_Parameter then
13171 Num_Out_Params := Num_Out_Params + 1;
13172 end if;
13174 -- Skip remaining processing if formal type was in error
13176 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
13177 goto Next_Parameter;
13178 end if;
13180 -- Force call by reference if aliased
13182 declare
13183 Conv : constant Convention_Id := Convention (Etype (Formal));
13184 begin
13185 if Is_Aliased (Formal) then
13186 Set_Mechanism (Formal, By_Reference);
13188 -- Warn if user asked this to be passed by copy
13190 if Conv = Convention_Ada_Pass_By_Copy then
13191 Error_Msg_N
13192 ("cannot pass aliased parameter & by copy??", Formal);
13193 end if;
13195 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
13197 elsif Conv = Convention_Ada_Pass_By_Copy then
13198 Set_Mechanism (Formal, By_Copy);
13200 elsif Conv = Convention_Ada_Pass_By_Reference then
13201 Set_Mechanism (Formal, By_Reference);
13202 end if;
13203 end;
13205 <<Next_Parameter>>
13206 Next (Param_Spec);
13207 end loop;
13209 if Present (First_Out_Param) and then Num_Out_Params = 1 then
13210 Set_Is_Only_Out_Parameter (First_Out_Param);
13211 end if;
13212 end Process_Formals;
13214 ----------------------------
13215 -- Reference_Body_Formals --
13216 ----------------------------
13218 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
13219 Fs : Entity_Id;
13220 Fb : Entity_Id;
13222 begin
13223 if Error_Posted (Spec) then
13224 return;
13225 end if;
13227 -- Iterate over both lists. They may be of different lengths if the two
13228 -- specs are not conformant.
13230 Fs := First_Formal (Spec);
13231 Fb := First_Formal (Bod);
13232 while Present (Fs) and then Present (Fb) loop
13233 Generate_Reference (Fs, Fb, 'b');
13235 if Style_Check then
13236 Style.Check_Identifier (Fb, Fs);
13237 end if;
13239 Set_Spec_Entity (Fb, Fs);
13240 Set_Referenced (Fs, False);
13241 Next_Formal (Fs);
13242 Next_Formal (Fb);
13243 end loop;
13244 end Reference_Body_Formals;
13246 -------------------------
13247 -- Set_Actual_Subtypes --
13248 -------------------------
13250 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
13251 Decl : Node_Id;
13252 Formal : Entity_Id;
13253 T : Entity_Id;
13254 First_Stmt : Node_Id := Empty;
13255 AS_Needed : Boolean;
13257 begin
13258 -- If this is an empty initialization procedure, no need to create
13259 -- actual subtypes (small optimization).
13261 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
13262 return;
13264 -- Within a predicate function we do not want to generate local
13265 -- subtypes that may generate nested predicate functions.
13267 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
13268 return;
13269 end if;
13271 -- The subtype declarations may freeze the formals. The body generated
13272 -- for an expression function is not a freeze point, so do not emit
13273 -- these declarations (small loss of efficiency in rare cases).
13275 if Nkind (N) = N_Subprogram_Body
13276 and then Was_Expression_Function (N)
13277 then
13278 return;
13279 end if;
13281 Formal := First_Formal (Subp);
13282 while Present (Formal) loop
13283 T := Etype (Formal);
13285 -- We never need an actual subtype for a constrained formal
13287 if Is_Constrained (T) then
13288 AS_Needed := False;
13290 -- If we have unknown discriminants, then we do not need an actual
13291 -- subtype, or more accurately we cannot figure it out. Note that
13292 -- all class-wide types have unknown discriminants.
13294 elsif Has_Unknown_Discriminants (T) then
13295 AS_Needed := False;
13297 -- At this stage we have an unconstrained type that may need an
13298 -- actual subtype. For sure the actual subtype is needed if we have
13299 -- an unconstrained array type. However, in an instance, the type
13300 -- may appear as a subtype of the full view, while the actual is
13301 -- in fact private (in which case no actual subtype is needed) so
13302 -- check the kind of the base type.
13304 elsif Is_Array_Type (Base_Type (T)) then
13305 AS_Needed := True;
13307 -- The only other case needing an actual subtype is an unconstrained
13308 -- record type which is an IN parameter (we cannot generate actual
13309 -- subtypes for the OUT or IN OUT case, since an assignment can
13310 -- change the discriminant values. However we exclude the case of
13311 -- initialization procedures, since discriminants are handled very
13312 -- specially in this context, see the section entitled "Handling of
13313 -- Discriminants" in Einfo.
13315 -- We also exclude the case of Discrim_SO_Functions (functions used
13316 -- in front-end layout mode for size/offset values), since in such
13317 -- functions only discriminants are referenced, and not only are such
13318 -- subtypes not needed, but they cannot always be generated, because
13319 -- of order of elaboration issues.
13321 elsif Is_Record_Type (T)
13322 and then Ekind (Formal) = E_In_Parameter
13323 and then Chars (Formal) /= Name_uInit
13324 and then not Is_Unchecked_Union (T)
13325 and then not Is_Discrim_SO_Function (Subp)
13326 then
13327 AS_Needed := True;
13329 -- All other cases do not need an actual subtype
13331 else
13332 AS_Needed := False;
13333 end if;
13335 -- Generate actual subtypes for unconstrained arrays and
13336 -- unconstrained discriminated records.
13338 if AS_Needed then
13339 if Nkind (N) = N_Accept_Statement then
13341 -- If expansion is active, the formal is replaced by a local
13342 -- variable that renames the corresponding entry of the
13343 -- parameter block, and it is this local variable that may
13344 -- require an actual subtype.
13346 if Expander_Active then
13347 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
13348 else
13349 Decl := Build_Actual_Subtype (T, Formal);
13350 end if;
13352 if Present (Handled_Statement_Sequence (N)) then
13353 First_Stmt :=
13354 First (Statements (Handled_Statement_Sequence (N)));
13355 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
13356 Mark_Rewrite_Insertion (Decl);
13357 else
13358 -- If the accept statement has no body, there will be no
13359 -- reference to the actuals, so no need to compute actual
13360 -- subtypes.
13362 return;
13363 end if;
13365 else
13366 Decl := Build_Actual_Subtype (T, Formal);
13367 Prepend (Decl, Declarations (N));
13368 Mark_Rewrite_Insertion (Decl);
13369 end if;
13371 -- The declaration uses the bounds of an existing object, and
13372 -- therefore needs no constraint checks.
13374 Analyze (Decl, Suppress => All_Checks);
13375 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
13377 -- We need to freeze manually the generated type when it is
13378 -- inserted anywhere else than in a declarative part.
13380 if Present (First_Stmt) then
13381 Insert_List_Before_And_Analyze (First_Stmt,
13382 Freeze_Entity (Defining_Identifier (Decl), N));
13384 -- Ditto if the type has a dynamic predicate, because the
13385 -- generated function will mention the actual subtype. The
13386 -- predicate may come from an explicit aspect of be inherited.
13388 elsif Has_Predicates (T) then
13389 Insert_List_After_And_Analyze (Decl,
13390 Freeze_Entity (Defining_Identifier (Decl), N));
13391 end if;
13393 if Nkind (N) = N_Accept_Statement
13394 and then Expander_Active
13395 then
13396 Set_Actual_Subtype (Renamed_Object (Formal),
13397 Defining_Identifier (Decl));
13398 else
13399 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
13400 end if;
13401 end if;
13403 Next_Formal (Formal);
13404 end loop;
13405 end Set_Actual_Subtypes;
13407 ---------------------
13408 -- Set_Formal_Mode --
13409 ---------------------
13411 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
13412 Spec : constant Node_Id := Parent (Formal_Id);
13413 Id : constant Entity_Id := Scope (Formal_Id);
13415 begin
13416 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
13417 -- since we ensure that corresponding actuals are always valid at the
13418 -- point of the call.
13420 if Out_Present (Spec) then
13421 if Is_Entry (Id)
13422 or else Is_Subprogram_Or_Generic_Subprogram (Id)
13423 then
13424 Set_Has_Out_Or_In_Out_Parameter (Id, True);
13425 end if;
13427 if Ekind (Id) in E_Function | E_Generic_Function then
13429 -- [IN] OUT parameters allowed for functions in Ada 2012
13431 if Ada_Version >= Ada_2012 then
13433 -- Even in Ada 2012 operators can only have IN parameters
13435 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
13436 Error_Msg_N ("operators can only have IN parameters", Spec);
13437 end if;
13439 if In_Present (Spec) then
13440 Mutate_Ekind (Formal_Id, E_In_Out_Parameter);
13441 else
13442 Mutate_Ekind (Formal_Id, E_Out_Parameter);
13443 end if;
13445 -- But not in earlier versions of Ada
13447 else
13448 Error_Msg_N ("functions can only have IN parameters", Spec);
13449 Mutate_Ekind (Formal_Id, E_In_Parameter);
13450 end if;
13452 elsif In_Present (Spec) then
13453 Mutate_Ekind (Formal_Id, E_In_Out_Parameter);
13455 else
13456 Mutate_Ekind (Formal_Id, E_Out_Parameter);
13457 Set_Is_True_Constant (Formal_Id, False);
13458 Set_Current_Value (Formal_Id, Empty);
13459 end if;
13461 else
13462 Mutate_Ekind (Formal_Id, E_In_Parameter);
13463 end if;
13465 Set_Is_Not_Self_Hidden (Formal_Id);
13467 -- Set Is_Known_Non_Null for access parameters since the language
13468 -- guarantees that access parameters are always non-null. We also set
13469 -- Can_Never_Be_Null, since there is no way to change the value.
13471 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
13473 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
13474 -- null; In Ada 2005, only if then null_exclusion is explicit.
13476 if Ada_Version < Ada_2005
13477 or else Can_Never_Be_Null (Etype (Formal_Id))
13478 then
13479 Set_Is_Known_Non_Null (Formal_Id);
13480 Set_Can_Never_Be_Null (Formal_Id);
13481 end if;
13483 -- Ada 2005 (AI-231): Null-exclusion access subtype
13485 elsif Is_Access_Type (Etype (Formal_Id))
13486 and then Can_Never_Be_Null (Etype (Formal_Id))
13487 then
13488 Set_Is_Known_Non_Null (Formal_Id);
13490 -- We can also set Can_Never_Be_Null (thus preventing some junk
13491 -- access checks) for the case of an IN parameter, which cannot
13492 -- be changed, or for an IN OUT parameter, which can be changed but
13493 -- not to a null value. But for an OUT parameter, the initial value
13494 -- passed in can be null, so we can't set this flag in that case.
13496 if Ekind (Formal_Id) /= E_Out_Parameter then
13497 Set_Can_Never_Be_Null (Formal_Id);
13498 end if;
13499 end if;
13501 Set_Mechanism (Formal_Id, Default_Mechanism);
13502 Set_Formal_Validity (Formal_Id);
13503 end Set_Formal_Mode;
13505 -------------------------
13506 -- Set_Formal_Validity --
13507 -------------------------
13509 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
13510 begin
13511 -- If no validity checking, then we cannot assume anything about the
13512 -- validity of parameters, since we do not know there is any checking
13513 -- of the validity on the call side.
13515 if not Validity_Checks_On then
13516 return;
13518 -- If validity checking for parameters is enabled, this means we are
13519 -- not supposed to make any assumptions about argument values.
13521 elsif Validity_Check_Parameters then
13522 return;
13524 -- If we are checking in parameters, we will assume that the caller is
13525 -- also checking parameters, so we can assume the parameter is valid.
13527 elsif Ekind (Formal_Id) = E_In_Parameter
13528 and then Validity_Check_In_Params
13529 then
13530 Set_Is_Known_Valid (Formal_Id, True);
13532 -- Similar treatment for IN OUT parameters
13534 elsif Ekind (Formal_Id) = E_In_Out_Parameter
13535 and then Validity_Check_In_Out_Params
13536 then
13537 Set_Is_Known_Valid (Formal_Id, True);
13538 end if;
13539 end Set_Formal_Validity;
13541 ------------------------
13542 -- Subtype_Conformant --
13543 ------------------------
13545 function Subtype_Conformant
13546 (New_Id : Entity_Id;
13547 Old_Id : Entity_Id;
13548 Skip_Controlling_Formals : Boolean := False) return Boolean
13550 Result : Boolean;
13551 begin
13552 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
13553 Skip_Controlling_Formals => Skip_Controlling_Formals);
13554 return Result;
13555 end Subtype_Conformant;
13557 ---------------------
13558 -- Type_Conformant --
13559 ---------------------
13561 function Type_Conformant
13562 (New_Id : Entity_Id;
13563 Old_Id : Entity_Id;
13564 Skip_Controlling_Formals : Boolean := False) return Boolean
13566 Result : Boolean;
13567 begin
13568 May_Hide_Profile := False;
13569 Check_Conformance
13570 (New_Id, Old_Id, Type_Conformant, False, Result,
13571 Skip_Controlling_Formals => Skip_Controlling_Formals);
13572 return Result;
13573 end Type_Conformant;
13575 -------------------------------
13576 -- Valid_Operator_Definition --
13577 -------------------------------
13579 procedure Valid_Operator_Definition (Designator : Entity_Id) is
13580 N : Integer := 0;
13581 F : Entity_Id;
13582 Id : constant Name_Id := Chars (Designator);
13583 N_OK : Boolean;
13585 begin
13586 F := First_Formal (Designator);
13587 while Present (F) loop
13588 N := N + 1;
13590 if Present (Default_Value (F)) then
13591 Error_Msg_N
13592 ("default values not allowed for operator parameters",
13593 Parent (F));
13595 -- For function instantiations that are operators, we must check
13596 -- separately that the corresponding generic only has in-parameters.
13597 -- For subprogram declarations this is done in Set_Formal_Mode. Such
13598 -- an error could not arise in earlier versions of the language.
13600 elsif Ekind (F) /= E_In_Parameter then
13601 Error_Msg_N ("operators can only have IN parameters", F);
13602 end if;
13604 Next_Formal (F);
13605 end loop;
13607 -- Verify that user-defined operators have proper number of arguments
13608 -- First case of operators which can only be unary
13610 if Id in Name_Op_Not | Name_Op_Abs then
13611 N_OK := (N = 1);
13613 -- Case of operators which can be unary or binary
13615 elsif Id in Name_Op_Add | Name_Op_Subtract then
13616 N_OK := (N in 1 .. 2);
13618 -- All other operators can only be binary
13620 else
13621 N_OK := (N = 2);
13622 end if;
13624 if not N_OK then
13625 Error_Msg_N
13626 ("incorrect number of arguments for operator", Designator);
13627 end if;
13629 if Id = Name_Op_Ne
13630 and then Base_Type (Etype (Designator)) = Standard_Boolean
13631 and then not Is_Intrinsic_Subprogram (Designator)
13632 then
13633 Error_Msg_N
13634 ("explicit definition of inequality not allowed", Designator);
13635 end if;
13636 end Valid_Operator_Definition;
13638 end Sem_Ch6;