Add assember CFI directives to millicode division and remainder routines.
[official-gcc.git] / gcc / ada / sem_ch6.adb
blobd4701aed0f7a9aabb7ea91b561c0e056724bd43c
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 Rtsfind; use Rtsfind;
57 with Sem; use Sem;
58 with Sem_Aux; use Sem_Aux;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch3; use Sem_Ch3;
61 with Sem_Ch4; use Sem_Ch4;
62 with Sem_Ch5; use Sem_Ch5;
63 with Sem_Ch8; use Sem_Ch8;
64 with Sem_Ch9; use Sem_Ch9;
65 with Sem_Ch10; use Sem_Ch10;
66 with Sem_Ch12; use Sem_Ch12;
67 with Sem_Ch13; use Sem_Ch13;
68 with Sem_Dim; use Sem_Dim;
69 with Sem_Disp; use Sem_Disp;
70 with Sem_Dist; use Sem_Dist;
71 with Sem_Elim; use Sem_Elim;
72 with Sem_Eval; use Sem_Eval;
73 with Sem_Mech; use Sem_Mech;
74 with Sem_Prag; use Sem_Prag;
75 with Sem_Res; use Sem_Res;
76 with Sem_Util; use Sem_Util;
77 with Sem_Type; use Sem_Type;
78 with Sem_Warn; use Sem_Warn;
79 with Sinput; use Sinput;
80 with Stand; use Stand;
81 with Sinfo; use Sinfo;
82 with Sinfo.Nodes; use Sinfo.Nodes;
83 with Sinfo.Utils; use Sinfo.Utils;
84 with Sinfo.CN; use Sinfo.CN;
85 with Snames; use Snames;
86 with Stringt; use Stringt;
87 with Style;
88 with Stylesw; use Stylesw;
89 with Tbuild; use Tbuild;
90 with Uintp; use Uintp;
91 with Urealp; use Urealp;
92 with Validsw; use Validsw;
93 with Warnsw; use Warnsw;
95 package body Sem_Ch6 is
97 May_Hide_Profile : Boolean := False;
98 -- This flag is used to indicate that two formals in two subprograms being
99 -- checked for conformance differ only in that one is an access parameter
100 -- while the other is of a general access type with the same designated
101 -- type. In this case, if the rest of the signatures match, a call to
102 -- either subprogram may be ambiguous, which is worth a warning. The flag
103 -- is set in Compatible_Types, and the warning emitted in
104 -- New_Overloaded_Entity.
106 -----------------------
107 -- Local Subprograms --
108 -----------------------
110 procedure Analyze_Function_Return (N : Node_Id);
111 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
112 -- applies to a [generic] function.
114 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
115 -- Analyze a generic subprogram body. N is the body to be analyzed, and
116 -- Gen_Id is the defining entity Id for the corresponding spec.
118 procedure Analyze_Null_Procedure
119 (N : Node_Id;
120 Is_Completion : out Boolean);
121 -- A null procedure can be a declaration or (Ada 2012) a completion
123 procedure Analyze_Return_Statement (N : Node_Id);
124 -- Common processing for simple and extended return statements
126 procedure Analyze_Return_Type (N : Node_Id);
127 -- Subsidiary to Process_Formals: analyze subtype mark in function
128 -- specification in a context where the formals are visible and hide
129 -- outer homographs.
131 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
132 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
133 -- that we can use RETURN but not skip the debug output at the end.
135 procedure Check_Conformance
136 (New_Id : Entity_Id;
137 Old_Id : Entity_Id;
138 Ctype : Conformance_Type;
139 Errmsg : Boolean;
140 Conforms : out Boolean;
141 Err_Loc : Node_Id := Empty;
142 Get_Inst : Boolean := False;
143 Skip_Controlling_Formals : Boolean := False);
144 -- Given two entities, this procedure checks that the profiles associated
145 -- with these entities meet the conformance criterion given by the third
146 -- parameter. If they conform, Conforms is set True and control returns
147 -- to the caller. If they do not conform, Conforms is set to False, and
148 -- in addition, if Errmsg is True on the call, proper messages are output
149 -- to complain about the conformance failure. If Err_Loc is non_Empty
150 -- the error messages are placed on Err_Loc, if Err_Loc is empty, then
151 -- error messages are placed on the appropriate part of the construct
152 -- denoted by New_Id. If Get_Inst is true, then this is a mode conformance
153 -- against a formal access-to-subprogram type so Get_Instance_Of must
154 -- be called.
156 procedure Check_Formal_Subprogram_Conformance
157 (New_Id : Entity_Id;
158 Old_Id : Entity_Id;
159 Err_Loc : Node_Id;
160 Errmsg : Boolean;
161 Conforms : out Boolean);
162 -- Core implementation of Check_Formal_Subprogram_Conformance from spec.
163 -- Errmsg can be set to False to not emit error messages.
164 -- Conforms is set to True if there is conformance, False otherwise.
166 procedure Check_Limited_Return
167 (N : Node_Id;
168 Expr : Node_Id;
169 R_Type : Entity_Id);
170 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
171 -- types. Used only for simple return statements. Expr is the expression
172 -- returned.
174 procedure Check_Subprogram_Order (N : Node_Id);
175 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
176 -- the alpha ordering rule for N if this ordering requirement applicable.
178 procedure Check_Returns
179 (HSS : Node_Id;
180 Mode : Character;
181 Err : out Boolean;
182 Proc : Entity_Id := Empty);
183 -- Called to check for missing return statements in a function body, or for
184 -- returns present in a procedure body which has No_Return set. HSS is the
185 -- handled statement sequence for the subprogram body. This procedure
186 -- checks all flow paths to make sure they either have return (Mode = 'F',
187 -- used for functions) or do not have a return (Mode = 'P', used for
188 -- No_Return procedures). The flag Err is set if there are any control
189 -- paths not explicitly terminated by a return in the function case, and is
190 -- True otherwise. Proc is the entity for the procedure case and is used
191 -- in posting the warning message.
193 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
194 -- In Ada 2012, a primitive equality operator for an untagged record type
195 -- must appear before the type is frozen. This procedure checks that this
196 -- rule is met, and otherwise gives an error on the subprogram declaration
197 -- and a warning on the earlier freeze point if it is easy to pinpoint. In
198 -- earlier versions of Ada, the call has not effect, unless compatibility
199 -- warnings are requested by means of Warn_On_Ada_2012_Incompatibility.
201 procedure Enter_Overloaded_Entity (S : Entity_Id);
202 -- This procedure makes S, a new overloaded entity, into the first visible
203 -- entity with that name.
205 function Has_Reliable_Extra_Formals (E : Entity_Id) return Boolean;
206 -- E is the entity for a subprogram spec. Returns False for abstract
207 -- predefined dispatching primitives of Root_Controlled since they
208 -- cannot have extra formals (this is required to build the runtime);
209 -- it also returns False for predefined stream dispatching operations
210 -- not emitted by the frontend. Otherwise returns True.
212 function Is_Non_Overriding_Operation
213 (Prev_E : Entity_Id;
214 New_E : Entity_Id) return Boolean;
215 -- Enforce the rule given in 12.3(18): a private operation in an instance
216 -- overrides an inherited operation only if the corresponding operation
217 -- was overriding in the generic. This needs to be checked for primitive
218 -- operations of types derived (in the generic unit) from formal private
219 -- or formal derived types.
221 procedure Make_Inequality_Operator (S : Entity_Id);
222 -- Create the declaration for an inequality operator that is implicitly
223 -- created by a user-defined equality operator that yields a boolean.
225 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id);
226 -- Preanalysis of default expressions of subprogram formals. N is the
227 -- expression to be analyzed and T is the expected type.
229 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
230 -- Formal_Id is an formal parameter entity. This procedure deals with
231 -- setting the proper validity status for this entity, which depends on
232 -- the kind of parameter and the validity checking mode.
234 ---------------------------------------------
235 -- Analyze_Abstract_Subprogram_Declaration --
236 ---------------------------------------------
238 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
239 Scop : constant Entity_Id := Current_Scope;
240 Subp_Id : constant Entity_Id :=
241 Analyze_Subprogram_Specification (Specification (N));
243 begin
244 Generate_Definition (Subp_Id);
246 -- Set the SPARK mode from the current context (may be overwritten later
247 -- with explicit pragma).
249 Set_SPARK_Pragma (Subp_Id, SPARK_Mode_Pragma);
250 Set_SPARK_Pragma_Inherited (Subp_Id);
252 -- Preserve relevant elaboration-related attributes of the context which
253 -- are no longer available or very expensive to recompute once analysis,
254 -- resolution, and expansion are over.
256 Mark_Elaboration_Attributes
257 (N_Id => Subp_Id,
258 Checks => True,
259 Warnings => True);
261 Set_Is_Abstract_Subprogram (Subp_Id);
262 New_Overloaded_Entity (Subp_Id);
263 Check_Delayed_Subprogram (Subp_Id);
265 Set_Categorization_From_Scope (Subp_Id, Scop);
267 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
268 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
270 -- Issue a warning if the abstract subprogram is neither a dispatching
271 -- operation nor an operation that overrides an inherited subprogram or
272 -- predefined operator, since this most likely indicates a mistake.
274 elsif Warn_On_Redundant_Constructs
275 and then not Is_Dispatching_Operation (Subp_Id)
276 and then No (Overridden_Operation (Subp_Id))
277 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
278 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
279 then
280 Error_Msg_N
281 ("abstract subprogram is not dispatching or overriding?r?", N);
282 end if;
284 Generate_Reference_To_Formals (Subp_Id);
285 Check_Eliminated (Subp_Id);
287 if Has_Aspects (N) then
288 Analyze_Aspect_Specifications (N, Subp_Id);
289 end if;
290 end Analyze_Abstract_Subprogram_Declaration;
292 ---------------------------------
293 -- Analyze_Expression_Function --
294 ---------------------------------
296 procedure Analyze_Expression_Function (N : Node_Id) is
297 Expr : constant Node_Id := Expression (N);
298 Loc : constant Source_Ptr := Sloc (N);
299 LocX : constant Source_Ptr := Sloc (Expr);
300 Spec : constant Node_Id := Specification (N);
302 -- Local variables
304 Asp : Node_Id;
305 New_Body : Node_Id;
306 New_Spec : Node_Id;
307 Orig_N : Node_Id := Empty;
308 Ret : Node_Id;
309 Typ : Entity_Id := Empty;
311 Def_Id : Entity_Id := Empty;
312 Prev : Entity_Id;
313 -- If the expression is a completion, Prev is the entity whose
314 -- declaration is completed. Def_Id is needed to analyze the spec.
316 begin
317 -- This is one of the occasions on which we transform the tree during
318 -- semantic analysis. If this is a completion, transform the expression
319 -- function into an equivalent subprogram body, and analyze it.
321 -- Expression functions are inlined unconditionally. The back-end will
322 -- determine whether this is possible.
324 Inline_Processing_Required := True;
326 -- Create a specification for the generated body. This must be done
327 -- prior to the analysis of the initial declaration.
329 New_Spec := Copy_Subprogram_Spec (Spec);
330 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
332 -- If there are previous overloadable entities with the same name,
333 -- check whether any of them is completed by the expression function.
334 -- In a generic context a formal subprogram has no completion.
336 if Present (Prev)
337 and then Is_Overloadable (Prev)
338 and then not Is_Formal_Subprogram (Prev)
339 then
340 Def_Id := Analyze_Subprogram_Specification (Spec);
341 Prev := Find_Corresponding_Spec (N);
343 Typ := Etype (Def_Id);
345 -- The previous entity may be an expression function as well, in
346 -- which case the redeclaration is illegal.
348 if Present (Prev)
349 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
350 N_Expression_Function
351 then
352 Error_Msg_Sloc := Sloc (Prev);
353 Error_Msg_N ("& conflicts with declaration#", Def_Id);
354 return;
355 end if;
356 end if;
358 Ret := Make_Simple_Return_Statement (LocX, Expr);
360 New_Body :=
361 Make_Subprogram_Body (Loc,
362 Specification => New_Spec,
363 Declarations => Empty_List,
364 Handled_Statement_Sequence =>
365 Make_Handled_Sequence_Of_Statements (LocX,
366 Statements => New_List (Ret)));
367 Set_Was_Expression_Function (New_Body);
369 -- If the expression completes a generic subprogram, we must create a
370 -- separate node for the body, because at instantiation the original
371 -- node of the generic copy must be a generic subprogram body, and
372 -- cannot be a expression function. Otherwise we just rewrite the
373 -- expression with the non-generic body.
375 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
376 Insert_After (N, New_Body);
378 -- Propagate any aspects or pragmas that apply to the expression
379 -- function to the proper body when the expression function acts
380 -- as a completion.
382 if Has_Aspects (N) then
383 Move_Aspects (N, To => New_Body);
384 end if;
386 Relocate_Pragmas_To_Body (New_Body);
388 Rewrite (N, Make_Null_Statement (Loc));
389 Set_Has_Completion (Prev, False);
390 Analyze (N);
391 Analyze (New_Body);
392 Set_Is_Inlined (Prev);
394 elsif Present (Prev)
395 and then Is_Overloadable (Prev)
396 and then not Is_Formal_Subprogram (Prev)
397 then
398 Set_Has_Completion (Prev, False);
399 Set_Is_Inlined (Prev);
401 -- AI12-0103: Expression functions that are a completion freeze their
402 -- expression but don't freeze anything else (unlike regular bodies).
404 -- Note that we cannot defer this freezing to the analysis of the
405 -- expression itself, because a freeze node might appear in a nested
406 -- scope, leading to an elaboration order issue in gigi.
407 -- As elsewhere, we do not emit freeze nodes within a generic unit.
409 if not Inside_A_Generic then
410 Freeze_Expr_Types
411 (Def_Id => Def_Id,
412 Typ => Typ,
413 Expr => Expr,
414 N => N);
415 end if;
417 -- For navigation purposes, indicate that the function is a body
419 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
420 Rewrite (N, New_Body);
422 -- Remove any existing aspects from the original node because the act
423 -- of rewriting causes the list to be shared between the two nodes.
425 Orig_N := Original_Node (N);
426 Remove_Aspects (Orig_N);
428 -- Propagate any pragmas that apply to expression function to the
429 -- proper body when the expression function acts as a completion.
430 -- Aspects are automatically transfered because of node rewriting.
432 Relocate_Pragmas_To_Body (N);
433 Analyze (N);
435 -- Prev is the previous entity with the same name, but it is can
436 -- be an unrelated spec that is not completed by the expression
437 -- function. In that case the relevant entity is the one in the body.
438 -- Not clear that the backend can inline it in this case ???
440 if Has_Completion (Prev) then
442 -- The formals of the expression function are body formals,
443 -- and do not appear in the ali file, which will only contain
444 -- references to the formals of the original subprogram spec.
446 declare
447 F1 : Entity_Id;
448 F2 : Entity_Id;
450 begin
451 F1 := First_Formal (Def_Id);
452 F2 := First_Formal (Prev);
454 while Present (F1) loop
455 Set_Spec_Entity (F1, F2);
456 Next_Formal (F1);
457 Next_Formal (F2);
458 end loop;
459 end;
461 else
462 Set_Is_Inlined (Defining_Entity (New_Body));
463 end if;
465 -- If this is not a completion, create both a declaration and a body, so
466 -- that the expression can be inlined whenever possible.
468 else
469 -- An expression function that is not a completion is not a
470 -- subprogram declaration, and thus cannot appear in a protected
471 -- definition.
473 if Nkind (Parent (N)) = N_Protected_Definition then
474 Error_Msg_N
475 ("an expression function is not a legal protected operation", N);
476 end if;
478 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
480 -- Remove any existing aspects from the original node because the act
481 -- of rewriting causes the list to be shared between the two nodes.
483 Orig_N := Original_Node (N);
484 Remove_Aspects (Orig_N);
486 Analyze (N);
488 -- If aspect SPARK_Mode was specified on the body, it needs to be
489 -- repeated both on the generated spec and the body.
491 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
493 if Present (Asp) then
494 Asp := New_Copy_Tree (Asp);
495 Set_Analyzed (Asp, False);
496 Set_Aspect_Specifications (New_Body, New_List (Asp));
497 end if;
499 Def_Id := Defining_Entity (N);
500 Set_Is_Inlined (Def_Id);
502 Typ := Etype (Def_Id);
504 -- Establish the linkages between the spec and the body. These are
505 -- used when the expression function acts as the prefix of attribute
506 -- 'Access in order to freeze the original expression which has been
507 -- moved to the generated body.
509 Set_Corresponding_Body (N, Defining_Entity (New_Body));
510 Set_Corresponding_Spec (New_Body, Def_Id);
512 -- Within a generic preanalyze the original expression for name
513 -- capture. The body is also generated but plays no role in
514 -- this because it is not part of the original source.
515 -- If this is an ignored Ghost entity, analysis of the generated
516 -- body is needed to hide external references (as is done in
517 -- Analyze_Subprogram_Body) after which the subprogram profile
518 -- can be frozen, which is needed to expand calls to such an ignored
519 -- Ghost subprogram.
521 if Inside_A_Generic then
522 Set_Has_Completion (Def_Id, not Is_Ignored_Ghost_Entity (Def_Id));
523 Push_Scope (Def_Id);
524 Install_Formals (Def_Id);
525 Preanalyze_Spec_Expression (Expr, Typ);
526 End_Scope;
527 else
528 Push_Scope (Def_Id);
529 Install_Formals (Def_Id);
530 Preanalyze_Formal_Expression (Expr, Typ);
531 Check_Limited_Return (Orig_N, Expr, Typ);
532 End_Scope;
533 end if;
535 -- If this is a wrapper created in an instance for a formal
536 -- subprogram, insert body after declaration, to be analyzed when the
537 -- enclosing instance is analyzed.
539 if GNATprove_Mode
540 and then Is_Generic_Actual_Subprogram (Def_Id)
541 then
542 Insert_After (N, New_Body);
544 -- To prevent premature freeze action, insert the new body at the end
545 -- of the current declarations, or at the end of the package spec.
546 -- However, resolve usage names now, to prevent spurious visibility
547 -- on later entities. Note that the function can now be called in
548 -- the current declarative part, which will appear to be prior to the
549 -- presence of the body in the code. There are nevertheless no order
550 -- of elaboration issues because all name resolution has taken place
551 -- at the point of declaration.
553 else
554 declare
555 Decls : List_Id := List_Containing (N);
556 Par : constant Node_Id := Parent (Decls);
558 begin
559 if Nkind (Par) = N_Package_Specification
560 and then Decls = Visible_Declarations (Par)
561 and then not Is_Empty_List (Private_Declarations (Par))
562 then
563 Decls := Private_Declarations (Par);
564 end if;
566 Insert_After (Last (Decls), New_Body);
567 end;
568 end if;
570 -- In the case of an expression function marked with the aspect
571 -- Static, we need to check the requirement that the function's
572 -- expression is a potentially static expression. This is done
573 -- by making a full copy of the expression tree and performing
574 -- a special preanalysis on that tree with the global flag
575 -- Checking_Potentially_Static_Expression enabled. If the
576 -- resulting expression is static, then it's OK, but if not, that
577 -- means the expression violates the requirements of the Ada 2022
578 -- RM in 4.9(3.2/5-3.4/5) and we flag an error.
580 if Is_Static_Function (Def_Id) then
581 declare
582 -- If a potentially static expr like "Parameter / 0"
583 -- is transformed into "(raise Constraint_Error)", then we
584 -- need to copy the Original_Node.
585 function Make_Expr_Copy return Node_Id is
586 (New_Copy_Tree (if Expr in N_Raise_xxx_Error_Id
587 then Original_Node (Expr)
588 else Expr));
589 begin
590 if not Is_Static_Expression (Expr) then
591 declare
592 Exp_Copy : constant Node_Id := Make_Expr_Copy;
593 begin
594 Set_Checking_Potentially_Static_Expression (True);
596 Preanalyze_Formal_Expression (Exp_Copy, Typ);
598 if not Is_Static_Expression (Exp_Copy) then
599 Error_Msg_N
600 ("static expression function requires "
601 & "potentially static expression", Expr);
602 end if;
604 Set_Checking_Potentially_Static_Expression (False);
605 end;
606 end if;
608 -- We also make an additional copy of the expression and
609 -- replace the expression of the expression function with
610 -- this copy, because the currently present expression is
611 -- now associated with the body created for the static
612 -- expression function, which will later be analyzed and
613 -- possibly rewritten, and we need to have the separate
614 -- unanalyzed copy available for use with later static
615 -- calls.
617 Set_Expression
618 (Original_Node (Subprogram_Spec (Def_Id)),
619 Make_Expr_Copy);
621 -- Mark static expression functions as inlined, to ensure
622 -- that even calls with nonstatic actuals will be inlined.
624 Set_Has_Pragma_Inline (Def_Id);
625 Set_Is_Inlined (Def_Id);
626 end;
627 end if;
628 end if;
630 -- Check incorrect use of dynamically tagged expression. This doesn't
631 -- fall out automatically when analyzing the generated function body,
632 -- because Check_Dynamically_Tagged_Expression deliberately ignores
633 -- nodes that don't come from source.
635 if Present (Def_Id)
636 and then Is_Tagged_Type (Typ)
637 then
638 Check_Dynamically_Tagged_Expression
639 (Expr => Expr,
640 Typ => Typ,
641 Related_Nod => Orig_N);
642 end if;
644 -- We must enforce checks for unreferenced formals in our newly
645 -- generated function, so we propagate the referenced flag from the
646 -- original spec to the new spec as well as setting Comes_From_Source.
648 if Present (Parameter_Specifications (New_Spec)) then
649 declare
650 Form_New_Def : Entity_Id;
651 Form_New_Spec : Node_Id;
652 Form_Old_Def : Entity_Id;
653 Form_Old_Spec : Node_Id;
655 begin
656 Form_New_Spec := First (Parameter_Specifications (New_Spec));
657 Form_Old_Spec := First (Parameter_Specifications (Spec));
659 while Present (Form_New_Spec) and then Present (Form_Old_Spec) loop
660 Form_New_Def := Defining_Identifier (Form_New_Spec);
661 Form_Old_Def := Defining_Identifier (Form_Old_Spec);
663 Set_Comes_From_Source (Form_New_Def, True);
665 -- Because of the usefulness of unreferenced controlling
666 -- formals we exempt them from unreferenced warnings by marking
667 -- them as always referenced.
669 Set_Referenced (Form_Old_Def,
670 (Is_Formal (Form_Old_Def)
671 and then Is_Controlling_Formal (Form_Old_Def))
672 or else Referenced (Form_Old_Def));
674 Next (Form_New_Spec);
675 Next (Form_Old_Spec);
676 end loop;
677 end;
678 end if;
679 end Analyze_Expression_Function;
681 ---------------------------------------
682 -- Analyze_Extended_Return_Statement --
683 ---------------------------------------
685 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
686 begin
687 Analyze_Return_Statement (N);
688 end Analyze_Extended_Return_Statement;
690 ----------------------------
691 -- Analyze_Function_Call --
692 ----------------------------
694 procedure Analyze_Function_Call (N : Node_Id) is
695 Actuals : constant List_Id := Parameter_Associations (N);
696 Func_Nam : constant Node_Id := Name (N);
697 Actual : Node_Id;
699 begin
700 Analyze (Func_Nam);
702 -- A call of the form A.B (X) may be an Ada 2005 call, which is
703 -- rewritten as B (A, X). If the rewriting is successful, the call
704 -- has been analyzed and we just return.
706 if Nkind (Func_Nam) = N_Selected_Component
707 and then Name (N) /= Func_Nam
708 and then Is_Rewrite_Substitution (N)
709 and then Present (Etype (N))
710 then
711 return;
712 end if;
714 -- If error analyzing name, then set Any_Type as result type and return
716 if Etype (Func_Nam) = Any_Type then
717 Set_Etype (N, Any_Type);
718 return;
719 end if;
721 -- Otherwise analyze the parameters
723 Actual := First (Actuals);
724 while Present (Actual) loop
725 Analyze (Actual);
726 Check_Parameterless_Call (Actual);
727 Next (Actual);
728 end loop;
730 Analyze_Call (N);
731 end Analyze_Function_Call;
733 -----------------------------
734 -- Analyze_Function_Return --
735 -----------------------------
737 procedure Analyze_Function_Return (N : Node_Id) is
738 Loc : constant Source_Ptr := Sloc (N);
739 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
740 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
742 R_Type : constant Entity_Id := Etype (Scope_Id);
743 -- Function result subtype
745 procedure Check_No_Return_Expression (Return_Expr : Node_Id);
746 -- Ada 2022: Check that the return expression in a No_Return function
747 -- meets the conditions specified by RM 6.5.1(5.1/5).
749 --------------------------------
750 -- Check_No_Return_Expression --
751 --------------------------------
753 procedure Check_No_Return_Expression (Return_Expr : Node_Id) is
754 Kind : constant Node_Kind := Nkind (Return_Expr);
756 begin
757 if Kind = N_Raise_Expression then
758 return;
760 elsif Kind = N_Function_Call
761 and then Is_Entity_Name (Name (Return_Expr))
762 and then Ekind (Entity (Name (Return_Expr))) in
763 E_Function | E_Generic_Function
764 and then No_Return (Entity (Name (Return_Expr)))
765 then
766 return;
767 end if;
769 Error_Msg_N
770 ("illegal expression in RETURN statement of No_Return function",
771 Return_Expr);
772 Error_Msg_N
773 ("\must be raise expression or call to No_Return (RM 6.5.1(5.1/5))",
774 Return_Expr);
775 end Check_No_Return_Expression;
777 ---------------------
778 -- Local Variables --
779 ---------------------
781 Expr : Node_Id;
782 Obj_Decl : Node_Id := Empty;
784 -- Start of processing for Analyze_Function_Return
786 begin
787 Set_Return_Present (Scope_Id);
789 if Nkind (N) = N_Simple_Return_Statement then
790 Expr := Expression (N);
792 -- Guard against a malformed expression. The parser may have tried to
793 -- recover but the node is not analyzable.
795 if Nkind (Expr) = N_Error then
796 Set_Etype (Expr, Any_Type);
797 Expander_Mode_Save_And_Set (False);
798 return;
800 else
801 -- The resolution of a controlled [extension] aggregate associated
802 -- with a return statement creates a temporary which needs to be
803 -- finalized on function exit. Wrap the return statement inside a
804 -- block so that the finalization machinery can detect this case.
805 -- This early expansion is done only when the return statement is
806 -- not part of a handled sequence of statements.
808 if Nkind (Expr) in N_Aggregate | N_Extension_Aggregate
809 and then Needs_Finalization (R_Type)
810 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
811 then
812 Rewrite (N,
813 Make_Block_Statement (Loc,
814 Handled_Statement_Sequence =>
815 Make_Handled_Sequence_Of_Statements (Loc,
816 Statements => New_List (Relocate_Node (N)))));
818 Analyze (N);
819 return;
820 end if;
822 Analyze (Expr);
824 -- Ada 2005 (AI-251): If the type of the returned object is
825 -- an access to an interface type then we add an implicit type
826 -- conversion to force the displacement of the "this" pointer to
827 -- reference the secondary dispatch table. We cannot delay the
828 -- generation of this implicit conversion until the expansion
829 -- because in this case the type resolution changes the decoration
830 -- of the expression node to match R_Type; by contrast, if the
831 -- returned object is a class-wide interface type then it is too
832 -- early to generate here the implicit conversion since the return
833 -- statement may be rewritten by the expander into an extended
834 -- return statement whose expansion takes care of adding the
835 -- implicit type conversion to displace the pointer to the object.
837 if Expander_Active
838 and then Serious_Errors_Detected = 0
839 and then Is_Access_Type (R_Type)
840 and then Nkind (Expr) not in N_Null | N_Raise_Expression
841 and then Is_Interface (Designated_Type (R_Type))
842 and then Is_Progenitor (Designated_Type (R_Type),
843 Designated_Type (Etype (Expr)))
844 then
845 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
846 Analyze (Expr);
847 end if;
849 Resolve (Expr, R_Type);
850 Check_Limited_Return (N, Expr, R_Type);
852 Check_Return_Construct_Accessibility (N, Stm_Entity);
854 -- Ada 2022 (AI12-0269): Any return statement that applies to a
855 -- nonreturning function shall be a simple_return_statement with
856 -- an expression that is a raise_expression, or else a call on a
857 -- nonreturning function, or else a parenthesized expression of
858 -- one of these.
860 if Ada_Version >= Ada_2022
861 and then No_Return (Scope_Id)
862 and then Comes_From_Source (N)
863 then
864 Check_No_Return_Expression (Original_Node (Expr));
865 end if;
866 end if;
867 else
868 Obj_Decl := Last (Return_Object_Declarations (N));
870 -- Analyze parts specific to extended_return_statement:
872 declare
873 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
874 HSS : constant Node_Id := Handled_Statement_Sequence (N);
876 begin
877 Expr := Expression (Obj_Decl);
879 -- Note: The check for OK_For_Limited_Init will happen in
880 -- Analyze_Object_Declaration; we treat it as a normal
881 -- object declaration.
883 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
884 Analyze (Obj_Decl);
886 if Present (HSS) then
887 Analyze (HSS);
889 if Present (Exception_Handlers (HSS)) then
891 -- ???Has_Nested_Block_With_Handler needs to be set.
892 -- Probably by creating an actual N_Block_Statement.
893 -- Probably in Expand.
895 null;
896 end if;
897 end if;
899 -- Mark the return object as referenced, since the return is an
900 -- implicit reference of the object.
902 Set_Referenced (Defining_Identifier (Obj_Decl));
904 Check_References (Stm_Entity);
906 Check_Return_Construct_Accessibility (N, Stm_Entity);
908 -- Check RM 6.5 (5.9/3)
910 if Has_Aliased and then not Is_Immutably_Limited_Type (R_Type) then
911 if Ada_Version < Ada_2012
912 and then Warn_On_Ada_2012_Compatibility
913 then
914 Error_Msg_N
915 ("ALIASED only allowed for immutably limited return " &
916 "objects in Ada 2012?y?", N);
918 else
919 Error_Msg_N
920 ("ALIASED only allowed for immutably limited return " &
921 "objects", N);
922 end if;
923 end if;
925 -- Ada 2022 (AI12-0269): Any return statement that applies to a
926 -- nonreturning function shall be a simple_return_statement.
928 if Ada_Version >= Ada_2022
929 and then No_Return (Scope_Id)
930 and then Comes_From_Source (N)
931 then
932 Error_Msg_N
933 ("extended RETURN statement not allowed in No_Return "
934 & "function", N);
935 end if;
936 end;
937 end if;
939 -- Case of Expr present
941 if Present (Expr) then
943 -- Defend against previous errors
945 if Nkind (Expr) = N_Empty
946 or else No (Etype (Expr))
947 then
948 return;
949 end if;
951 -- Apply constraint check. Note that this is done before the implicit
952 -- conversion of the expression done for anonymous access types to
953 -- ensure correct generation of the null-excluding check associated
954 -- with null-excluding expressions found in return statements. We
955 -- don't need a check if the subtype of the return object is the
956 -- same as the result subtype of the function.
958 if Nkind (N) /= N_Extended_Return_Statement
959 or else Nkind (Obj_Decl) /= N_Object_Declaration
960 or else Nkind (Object_Definition (Obj_Decl)) not in N_Has_Entity
961 or else Entity (Object_Definition (Obj_Decl)) /= R_Type
962 then
963 Apply_Constraint_Check (Expr, R_Type);
964 end if;
966 -- The return value is converted to the return type of the function,
967 -- which implies a predicate check if the return type is predicated.
968 -- We do not apply the check for an extended return statement because
969 -- Analyze_Object_Declaration has already done it on Obj_Decl above.
970 -- We do not apply the check to a case expression because it will
971 -- be expanded into a series of return statements, each of which
972 -- will receive a predicate check.
974 if Nkind (N) /= N_Extended_Return_Statement
975 and then Nkind (Expr) /= N_Case_Expression
976 then
977 Apply_Predicate_Check (Expr, R_Type);
978 end if;
980 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
981 -- type, apply an implicit conversion of the expression to that type
982 -- to force appropriate static and run-time accessibility checks.
983 -- But we want to apply the checks to an extended return statement
984 -- only once, i.e. not to the simple return statement generated at
985 -- the end of its expansion because, prior to leaving the function,
986 -- the accessibility level of the return object changes to be a level
987 -- determined by the point of call (RM 3.10.2(10.8/3)).
989 if Ada_Version >= Ada_2005
990 and then Ekind (R_Type) = E_Anonymous_Access_Type
991 and then (Nkind (N) = N_Extended_Return_Statement
992 or else not Comes_From_Extended_Return_Statement (N))
993 then
994 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
995 Analyze_And_Resolve (Expr, R_Type);
997 -- If this is a local anonymous access to subprogram, the
998 -- accessibility check can be applied statically. The return is
999 -- illegal if the access type of the return expression is declared
1000 -- inside of the subprogram (except if it is the subtype indication
1001 -- of an extended return statement).
1003 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1004 if not Comes_From_Source (Current_Scope)
1005 or else Ekind (Current_Scope) = E_Return_Statement
1006 then
1007 null;
1009 elsif
1010 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1011 then
1012 Error_Msg_N ("cannot return local access to subprogram", N);
1013 end if;
1015 -- The expression cannot be of a formal incomplete type
1017 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1018 and then Is_Generic_Type (Etype (Expr))
1019 then
1020 Error_Msg_N
1021 ("cannot return expression of a formal incomplete type", N);
1022 end if;
1024 -- If the result type is class-wide, then check that the return
1025 -- expression's type is not declared at a deeper level than the
1026 -- function (RM05-6.5(5.6/2)).
1028 if Ada_Version >= Ada_2005
1029 and then Is_Class_Wide_Type (R_Type)
1030 then
1031 if Type_Access_Level (Etype (Expr)) >
1032 Subprogram_Access_Level (Scope_Id)
1033 then
1034 Error_Msg_N
1035 ("level of return expression type is deeper than "
1036 & "class-wide function!", Expr);
1037 end if;
1038 end if;
1040 -- Check incorrect use of dynamically tagged expression
1042 if Is_Tagged_Type (R_Type) then
1043 Check_Dynamically_Tagged_Expression
1044 (Expr => Expr,
1045 Typ => R_Type,
1046 Related_Nod => N);
1047 end if;
1049 -- Perform static accessibility checks for cases involving
1050 -- dereferences of access parameters. Runtime accessibility checks
1051 -- get generated elsewhere.
1053 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1054 and then Is_Limited_View (Etype (Scope_Id))
1055 and then Static_Accessibility_Level (Expr, Zero_On_Dynamic_Level)
1056 > Subprogram_Access_Level (Scope_Id)
1057 then
1058 -- Suppress the message in a generic, where the rewriting
1059 -- is irrelevant.
1061 if Inside_A_Generic then
1062 null;
1064 else
1065 Rewrite (N,
1066 Make_Raise_Program_Error (Loc,
1067 Reason => PE_Accessibility_Check_Failed));
1068 Analyze (N);
1070 Error_Msg_Warn := SPARK_Mode /= On;
1071 Error_Msg_N ("cannot return a local value by reference<<", N);
1072 Error_Msg_N ("\Program_Error [<<", N);
1073 end if;
1074 end if;
1076 if Known_Null (Expr)
1077 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1078 and then Null_Exclusion_Present (Parent (Scope_Id))
1079 then
1080 Apply_Compile_Time_Constraint_Error
1081 (N => Expr,
1082 Msg => "(Ada 2005) null not allowed for "
1083 & "null-excluding return??",
1084 Reason => CE_Null_Not_Allowed);
1085 end if;
1087 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1088 -- has no initializing expression.
1090 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1091 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1092 Subprogram_Access_Level (Scope_Id)
1093 then
1094 Error_Msg_N
1095 ("level of return expression type is deeper than "
1096 & "class-wide function!", Obj_Decl);
1097 end if;
1098 end if;
1099 end Analyze_Function_Return;
1101 -------------------------------------
1102 -- Analyze_Generic_Subprogram_Body --
1103 -------------------------------------
1105 procedure Analyze_Generic_Subprogram_Body
1106 (N : Node_Id;
1107 Gen_Id : Entity_Id)
1109 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1110 Kind : constant Entity_Kind := Ekind (Gen_Id);
1111 Body_Id : Entity_Id;
1112 New_N : Node_Id;
1113 Spec : Node_Id;
1115 begin
1116 -- Copy body and disable expansion while analyzing the generic For a
1117 -- stub, do not copy the stub (which would load the proper body), this
1118 -- will be done when the proper body is analyzed.
1120 if Nkind (N) /= N_Subprogram_Body_Stub then
1121 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1122 Rewrite (N, New_N);
1124 -- Once the contents of the generic copy and the template are
1125 -- swapped, do the same for their respective aspect specifications.
1127 Exchange_Aspects (N, New_N);
1129 -- Collect all contract-related source pragmas found within the
1130 -- template and attach them to the contract of the subprogram body.
1131 -- This contract is used in the capture of global references within
1132 -- annotations.
1134 Create_Generic_Contract (N);
1136 Start_Generic;
1137 end if;
1139 Spec := Specification (N);
1141 -- Within the body of the generic, the subprogram is callable, and
1142 -- behaves like the corresponding non-generic unit.
1144 Body_Id := Defining_Entity (Spec);
1146 if Kind = E_Generic_Procedure
1147 and then Nkind (Spec) /= N_Procedure_Specification
1148 then
1149 Error_Msg_N ("invalid body for generic procedure", Body_Id);
1150 return;
1152 elsif Kind = E_Generic_Function
1153 and then Nkind (Spec) /= N_Function_Specification
1154 then
1155 Error_Msg_N ("invalid body for generic function", Body_Id);
1156 return;
1157 end if;
1159 Set_Corresponding_Body (Gen_Decl, Body_Id);
1161 if Has_Completion (Gen_Id)
1162 and then Nkind (Parent (N)) /= N_Subunit
1163 then
1164 Error_Msg_N ("duplicate generic body", N);
1165 return;
1166 else
1167 Set_Has_Completion (Gen_Id);
1168 end if;
1170 if Nkind (N) = N_Subprogram_Body_Stub then
1171 Mutate_Ekind (Defining_Entity (Specification (N)), Kind);
1172 else
1173 Set_Corresponding_Spec (N, Gen_Id);
1174 end if;
1176 if Nkind (Parent (N)) = N_Compilation_Unit then
1177 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1178 end if;
1180 -- Make generic parameters immediately visible in the body. They are
1181 -- needed to process the formals declarations. Then make the formals
1182 -- visible in a separate step.
1184 Push_Scope (Gen_Id);
1186 declare
1187 E : Entity_Id;
1188 First_Ent : Entity_Id;
1190 begin
1191 First_Ent := First_Entity (Gen_Id);
1193 E := First_Ent;
1194 while Present (E) and then not Is_Formal (E) loop
1195 Install_Entity (E);
1196 Next_Entity (E);
1197 end loop;
1199 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1201 -- Now generic formals are visible, and the specification can be
1202 -- analyzed, for subsequent conformance check.
1204 Body_Id := Analyze_Subprogram_Specification (Spec);
1206 -- Make formal parameters visible
1208 if Present (E) then
1210 -- E is the first formal parameter, we loop through the formals
1211 -- installing them so that they will be visible.
1213 Set_First_Entity (Gen_Id, E);
1214 while Present (E) loop
1215 Install_Entity (E);
1216 Next_Formal (E);
1217 end loop;
1218 end if;
1220 -- Visible generic entity is callable within its own body
1222 Mutate_Ekind (Gen_Id, Ekind (Body_Id));
1223 Reinit_Field_To_Zero (Body_Id, F_Has_Out_Or_In_Out_Parameter,
1224 Old_Ekind =>
1225 (E_Function | E_Procedure |
1226 E_Generic_Function | E_Generic_Procedure => True,
1227 others => False));
1228 Mutate_Ekind (Body_Id, E_Subprogram_Body);
1229 Set_Convention (Body_Id, Convention (Gen_Id));
1230 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1231 Set_Scope (Body_Id, Scope (Gen_Id));
1233 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1235 if Nkind (N) = N_Subprogram_Body_Stub then
1237 -- No body to analyze, so restore state of generic unit
1239 Mutate_Ekind (Gen_Id, Kind);
1240 Mutate_Ekind (Body_Id, Kind);
1242 if Present (First_Ent) then
1243 Set_First_Entity (Gen_Id, First_Ent);
1244 end if;
1246 End_Scope;
1247 return;
1248 end if;
1250 -- If this is a compilation unit, it must be made visible explicitly,
1251 -- because the compilation of the declaration, unlike other library
1252 -- unit declarations, does not. If it is not a unit, the following
1253 -- is redundant but harmless.
1255 Set_Is_Immediately_Visible (Gen_Id);
1256 Reference_Body_Formals (Gen_Id, Body_Id);
1258 if Is_Child_Unit (Gen_Id) then
1259 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1260 end if;
1262 Set_Actual_Subtypes (N, Current_Scope);
1264 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1265 Set_SPARK_Pragma_Inherited (Body_Id);
1267 -- Analyze any aspect specifications that appear on the generic
1268 -- subprogram body.
1270 if Has_Aspects (N) then
1271 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
1272 end if;
1274 -- Process the contract of the subprogram body after analyzing all
1275 -- the contract-related pragmas within the declarations.
1277 Analyze_Pragmas_In_Declarations (Body_Id);
1278 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1280 -- Continue on with analyzing the declarations and statements once
1281 -- contract expansion is done and we are done expanding contract
1282 -- related wrappers.
1284 Analyze_Declarations (Declarations (N));
1285 Check_Completion;
1287 Analyze (Handled_Statement_Sequence (N));
1288 Save_Global_References (Original_Node (N));
1290 -- Prior to exiting the scope, include generic formals again (if any
1291 -- are present) in the set of local entities.
1293 if Present (First_Ent) then
1294 Set_First_Entity (Gen_Id, First_Ent);
1295 end if;
1297 Check_References (Gen_Id);
1298 end;
1300 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1301 Update_Use_Clause_Chain;
1302 Validate_Categorization_Dependency (N, Gen_Id);
1303 End_Scope;
1304 Check_Subprogram_Order (N);
1306 -- Outside of its body, unit is generic again
1308 Reinit_Field_To_Zero (Gen_Id, F_Has_Nested_Subprogram,
1309 Old_Ekind => (E_Function | E_Procedure => True, others => False));
1310 Mutate_Ekind (Gen_Id, Kind);
1311 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1313 if Style_Check then
1314 Style.Check_Identifier (Body_Id, Gen_Id);
1315 end if;
1317 End_Generic;
1318 end Analyze_Generic_Subprogram_Body;
1320 ----------------------------
1321 -- Analyze_Null_Procedure --
1322 ----------------------------
1324 -- WARNING: This routine manages Ghost regions. Return statements must be
1325 -- replaced by gotos that jump to the end of the routine and restore the
1326 -- Ghost mode.
1328 procedure Analyze_Null_Procedure
1329 (N : Node_Id;
1330 Is_Completion : out Boolean)
1332 Loc : constant Source_Ptr := Sloc (N);
1333 Spec : constant Node_Id := Specification (N);
1335 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1336 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1337 Saved_ISMP : constant Boolean :=
1338 Ignore_SPARK_Mode_Pragmas_In_Instance;
1339 -- Save the Ghost and SPARK mode-related data to restore on exit
1341 Designator : Entity_Id;
1342 Form : Node_Id;
1343 Null_Body : Node_Id := Empty;
1344 Null_Stmt : Node_Id := Null_Statement (Spec);
1345 Prev : Entity_Id;
1347 begin
1348 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1350 -- A null procedure is Ghost when it is stand-alone and is subject to
1351 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1352 -- now, to ensure that any nodes generated during analysis and expansion
1353 -- are properly marked as Ghost.
1355 if Present (Prev) then
1356 Mark_And_Set_Ghost_Body (N, Prev);
1357 end if;
1359 -- Capture the profile of the null procedure before analysis, for
1360 -- expansion at the freeze point and at each point of call. The body is
1361 -- used if the procedure has preconditions, or if it is a completion. In
1362 -- the first case the body is analyzed at the freeze point, in the other
1363 -- it replaces the null procedure declaration.
1365 -- For a null procedure that comes from source, a NULL statement is
1366 -- provided by the parser, which carries the source location of the
1367 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1368 -- from expansion, create one now.
1370 if No (Null_Stmt) then
1371 Null_Stmt := Make_Null_Statement (Loc);
1372 end if;
1374 Null_Body :=
1375 Make_Subprogram_Body (Loc,
1376 Specification => New_Copy_Tree (Spec),
1377 Declarations => New_List,
1378 Handled_Statement_Sequence =>
1379 Make_Handled_Sequence_Of_Statements (Loc,
1380 Statements => New_List (Null_Stmt)));
1382 -- Create new entities for body and formals
1384 Set_Defining_Unit_Name (Specification (Null_Body),
1385 Make_Defining_Identifier
1386 (Sloc (Defining_Entity (N)),
1387 Chars (Defining_Entity (N))));
1389 Form := First (Parameter_Specifications (Specification (Null_Body)));
1390 while Present (Form) loop
1391 Set_Defining_Identifier (Form,
1392 Make_Defining_Identifier
1393 (Sloc (Defining_Identifier (Form)),
1394 Chars (Defining_Identifier (Form))));
1395 Next (Form);
1396 end loop;
1398 -- Determine whether the null procedure may be a completion of a generic
1399 -- subprogram, in which case we use the new null body as the completion
1400 -- and set minimal semantic information on the original declaration,
1401 -- which is rewritten as a null statement.
1403 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1404 Insert_Before (N, Null_Body);
1405 Mutate_Ekind (Defining_Entity (N), Ekind (Prev));
1407 Rewrite (N, Make_Null_Statement (Loc));
1408 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1409 Is_Completion := True;
1411 -- Mark the newly generated subprogram body as trivial
1413 Set_Is_Trivial_Subprogram
1414 (Defining_Unit_Name (Specification (Null_Body)));
1416 goto Leave;
1418 else
1419 -- Resolve the types of the formals now, because the freeze point may
1420 -- appear in a different context, e.g. an instantiation.
1422 Form := First (Parameter_Specifications (Specification (Null_Body)));
1423 while Present (Form) loop
1424 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1425 Find_Type (Parameter_Type (Form));
1427 elsif No (Access_To_Subprogram_Definition
1428 (Parameter_Type (Form)))
1429 then
1430 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1432 -- The case of a null procedure with a formal that is an
1433 -- access-to-subprogram type, and that is used as an actual
1434 -- in an instantiation is left to the enthusiastic reader.
1436 else
1437 null;
1438 end if;
1440 Next (Form);
1441 end loop;
1442 end if;
1444 -- If there are previous overloadable entities with the same name, check
1445 -- whether any of them is completed by the null procedure.
1447 if Present (Prev) and then Is_Overloadable (Prev) then
1448 Designator := Analyze_Subprogram_Specification (Spec);
1449 Prev := Find_Corresponding_Spec (N);
1450 end if;
1452 if No (Prev) or else not Comes_From_Source (Prev) then
1453 Designator := Analyze_Subprogram_Specification (Spec);
1454 Set_Has_Completion (Designator);
1456 -- Signal to caller that this is a procedure declaration
1458 Is_Completion := False;
1460 -- Null procedures are always inlined, but generic formal subprograms
1461 -- which appear as such in the internal instance of formal packages,
1462 -- need no completion and are not marked Inline.
1464 if Expander_Active
1465 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1466 then
1467 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1468 Set_Body_To_Inline (N, Null_Body);
1469 Set_Is_Inlined (Designator);
1470 end if;
1472 else
1473 -- The null procedure is a completion. We unconditionally rewrite
1474 -- this as a null body (even if expansion is not active), because
1475 -- there are various error checks that are applied on this body
1476 -- when it is analyzed (e.g. correct aspect placement).
1478 if Has_Completion (Prev) then
1479 Error_Msg_Sloc := Sloc (Prev);
1480 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1481 end if;
1483 Check_Previous_Null_Procedure (N, Prev);
1485 Is_Completion := True;
1486 Rewrite (N, Null_Body);
1487 Analyze (N);
1488 end if;
1490 <<Leave>>
1491 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1492 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1493 end Analyze_Null_Procedure;
1495 -----------------------------
1496 -- Analyze_Operator_Symbol --
1497 -----------------------------
1499 -- An operator symbol such as "+" or "and" may appear in context where the
1500 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1501 -- is just a string, as in (conjunction = "or"). In these cases the parser
1502 -- generates this node, and the semantics does the disambiguation. Other
1503 -- such case are actuals in an instantiation, the generic unit in an
1504 -- instantiation, pragma arguments, and aspect specifications.
1506 procedure Analyze_Operator_Symbol (N : Node_Id) is
1507 Par : constant Node_Id := Parent (N);
1509 Maybe_Aspect_Spec : Node_Id := Par;
1510 begin
1511 if Nkind (Maybe_Aspect_Spec) /= N_Aspect_Specification then
1512 -- deal with N_Aggregate nodes
1513 Maybe_Aspect_Spec := Parent (Maybe_Aspect_Spec);
1514 end if;
1516 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1517 or else Nkind (Par) = N_Function_Instantiation
1518 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1519 or else (Nkind (Par) = N_Pragma_Argument_Association
1520 and then not Is_Pragma_String_Literal (Par))
1521 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1522 or else (Nkind (Par) = N_Attribute_Reference
1523 and then Attribute_Name (Par) /= Name_Value)
1524 or else (Nkind (Maybe_Aspect_Spec) = N_Aspect_Specification
1525 and then Get_Aspect_Id (Maybe_Aspect_Spec)
1527 -- Include aspects that can be specified by a
1528 -- subprogram name, which can be an operator.
1530 in Aspect_Stable_Properties
1531 | Aspect_Integer_Literal
1532 | Aspect_Real_Literal
1533 | Aspect_String_Literal
1534 | Aspect_Aggregate)
1535 then
1536 Find_Direct_Name (N);
1538 else
1539 Change_Operator_Symbol_To_String_Literal (N);
1540 Analyze (N);
1541 end if;
1542 end Analyze_Operator_Symbol;
1544 -----------------------------------
1545 -- Analyze_Parameter_Association --
1546 -----------------------------------
1548 procedure Analyze_Parameter_Association (N : Node_Id) is
1549 begin
1550 Analyze (Explicit_Actual_Parameter (N));
1551 end Analyze_Parameter_Association;
1553 ----------------------------
1554 -- Analyze_Procedure_Call --
1555 ----------------------------
1557 -- WARNING: This routine manages Ghost regions. Return statements must be
1558 -- replaced by gotos that jump to the end of the routine and restore the
1559 -- Ghost mode.
1561 procedure Analyze_Procedure_Call (N : Node_Id) is
1562 procedure Analyze_Call_And_Resolve;
1563 -- Do Analyze and Resolve calls for procedure call. At the end, check
1564 -- for illegal order dependence.
1565 -- ??? where is the check for illegal order dependencies?
1567 ------------------------------
1568 -- Analyze_Call_And_Resolve --
1569 ------------------------------
1571 procedure Analyze_Call_And_Resolve is
1572 begin
1573 if Nkind (N) = N_Procedure_Call_Statement then
1574 Analyze_Call (N);
1575 Resolve (N, Standard_Void_Type);
1576 else
1577 Analyze (N);
1578 end if;
1579 end Analyze_Call_And_Resolve;
1581 -- Local variables
1583 Actuals : constant List_Id := Parameter_Associations (N);
1584 Loc : constant Source_Ptr := Sloc (N);
1585 P : constant Node_Id := Name (N);
1587 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1588 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1589 -- Save the Ghost-related attributes to restore on exit
1591 Actual : Node_Id;
1592 New_N : Node_Id;
1594 -- Start of processing for Analyze_Procedure_Call
1596 begin
1597 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1598 -- a procedure call or an entry call. The prefix may denote an access
1599 -- to subprogram type, in which case an implicit dereference applies.
1600 -- If the prefix is an indexed component (without implicit dereference)
1601 -- then the construct denotes a call to a member of an entire family.
1602 -- If the prefix is a simple name, it may still denote a call to a
1603 -- parameterless member of an entry family. Resolution of these various
1604 -- interpretations is delicate.
1606 -- Do not analyze machine code statements to avoid rejecting them in
1607 -- CodePeer mode.
1609 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1610 Set_Etype (P, Standard_Void_Type);
1611 else
1612 Analyze (P);
1613 end if;
1615 -- If this is a call of the form Obj.Op, the call may have been analyzed
1616 -- and possibly rewritten into a block, in which case we are done.
1618 if Analyzed (N) then
1619 return;
1621 -- If there is an error analyzing the name (which may have been
1622 -- rewritten if the original call was in prefix notation) then error
1623 -- has been emitted already, mark node and return.
1625 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1626 Set_Etype (N, Any_Type);
1627 return;
1628 end if;
1630 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1631 -- Set the mode now to ensure that any nodes generated during analysis
1632 -- and expansion are properly marked as Ghost.
1634 Mark_And_Set_Ghost_Procedure_Call (N);
1636 -- Otherwise analyze the parameters
1638 Actual := First (Actuals);
1640 while Present (Actual) loop
1641 Analyze (Actual);
1642 Check_Parameterless_Call (Actual);
1643 Next (Actual);
1644 end loop;
1646 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1648 if Nkind (P) = N_Attribute_Reference
1649 and then Attribute_Name (P) in Name_Elab_Spec
1650 | Name_Elab_Body
1651 | Name_Elab_Subp_Body
1652 then
1653 if Present (Actuals) then
1654 Error_Msg_N
1655 ("no parameters allowed for this call", First (Actuals));
1656 goto Leave;
1657 end if;
1659 Set_Etype (N, Standard_Void_Type);
1660 Set_Analyzed (N);
1662 elsif Is_Entity_Name (P)
1663 and then Is_Record_Type (Etype (Entity (P)))
1664 and then Remote_AST_I_Dereference (P)
1665 then
1666 goto Leave;
1668 elsif Is_Entity_Name (P)
1669 and then Ekind (Entity (P)) /= E_Entry_Family
1670 then
1671 if Is_Access_Type (Etype (P))
1672 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1673 and then No (Actuals)
1674 and then Comes_From_Source (N)
1675 then
1676 Error_Msg_N ("missing explicit dereference in call", N);
1678 elsif Ekind (Entity (P)) = E_Operator then
1679 Error_Msg_Name_1 := Chars (P);
1680 Error_Msg_N ("operator % cannot be used as a procedure", N);
1681 end if;
1683 Analyze_Call_And_Resolve;
1685 -- If the prefix is the simple name of an entry family, this is a
1686 -- parameterless call from within the task body itself.
1688 elsif Is_Entity_Name (P)
1689 and then Nkind (P) = N_Identifier
1690 and then Ekind (Entity (P)) = E_Entry_Family
1691 and then Present (Actuals)
1692 and then No (Next (First (Actuals)))
1693 then
1694 -- Can be call to parameterless entry family. What appears to be the
1695 -- sole argument is in fact the entry index. Rewrite prefix of node
1696 -- accordingly. Source representation is unchanged by this
1697 -- transformation.
1699 New_N :=
1700 Make_Indexed_Component (Loc,
1701 Prefix =>
1702 Make_Selected_Component (Loc,
1703 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1704 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1705 Expressions => Actuals);
1706 Set_Name (N, New_N);
1707 Set_Etype (New_N, Standard_Void_Type);
1708 Set_Parameter_Associations (N, No_List);
1709 Analyze_Call_And_Resolve;
1711 elsif Nkind (P) = N_Explicit_Dereference then
1712 if Ekind (Etype (P)) = E_Subprogram_Type then
1713 Analyze_Call_And_Resolve;
1714 else
1715 Error_Msg_N ("expect access to procedure in call", P);
1716 end if;
1718 -- The name can be a selected component or an indexed component that
1719 -- yields an access to subprogram. Such a prefix is legal if the call
1720 -- has parameter associations.
1722 elsif Is_Access_Type (Etype (P))
1723 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1724 then
1725 if Present (Actuals) then
1726 Analyze_Call_And_Resolve;
1727 else
1728 Error_Msg_N ("missing explicit dereference in call", N);
1729 end if;
1731 -- If not an access to subprogram, then the prefix must resolve to the
1732 -- name of an entry, entry family, or protected operation.
1734 -- For the case of a simple entry call, P is a selected component where
1735 -- the prefix is the task and the selector name is the entry. A call to
1736 -- a protected procedure will have the same syntax. If the protected
1737 -- object contains overloaded operations, the entity may appear as a
1738 -- function, the context will select the operation whose type is Void.
1740 elsif Nkind (P) = N_Selected_Component
1741 and then Ekind (Entity (Selector_Name (P)))
1742 in E_Entry | E_Function | E_Procedure
1743 then
1744 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1745 -- in prefix notation may still be missing its controlling argument,
1746 -- so perform the transformation now.
1748 if SPARK_Mode = On and then In_Inlined_Body then
1749 declare
1750 Subp : constant Entity_Id := Entity (Selector_Name (P));
1751 Typ : constant Entity_Id := Etype (Prefix (P));
1753 begin
1754 if Is_Tagged_Type (Typ)
1755 and then Present (First_Formal (Subp))
1756 and then (Etype (First_Formal (Subp)) = Typ
1757 or else
1758 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
1759 and then Try_Object_Operation (P)
1760 then
1761 goto Leave;
1763 else
1764 Analyze_Call_And_Resolve;
1765 end if;
1766 end;
1768 else
1769 Analyze_Call_And_Resolve;
1770 end if;
1772 elsif Nkind (P) = N_Selected_Component
1773 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
1774 and then Present (Actuals)
1775 and then No (Next (First (Actuals)))
1776 then
1777 -- Can be call to parameterless entry family. What appears to be the
1778 -- sole argument is in fact the entry index. Rewrite prefix of node
1779 -- accordingly. Source representation is unchanged by this
1780 -- transformation.
1782 New_N :=
1783 Make_Indexed_Component (Loc,
1784 Prefix => New_Copy (P),
1785 Expressions => Actuals);
1786 Set_Name (N, New_N);
1787 Set_Etype (New_N, Standard_Void_Type);
1788 Set_Parameter_Associations (N, No_List);
1789 Analyze_Call_And_Resolve;
1791 -- For the case of a reference to an element of an entry family, P is
1792 -- an indexed component whose prefix is a selected component (task and
1793 -- entry family), and whose index is the entry family index.
1795 elsif Nkind (P) = N_Indexed_Component
1796 and then Nkind (Prefix (P)) = N_Selected_Component
1797 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
1798 then
1799 Analyze_Call_And_Resolve;
1801 -- If the prefix is the name of an entry family, it is a call from
1802 -- within the task body itself.
1804 elsif Nkind (P) = N_Indexed_Component
1805 and then Nkind (Prefix (P)) = N_Identifier
1806 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
1807 then
1808 New_N :=
1809 Make_Selected_Component (Loc,
1810 Prefix =>
1811 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
1812 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
1813 Rewrite (Prefix (P), New_N);
1814 Analyze (P);
1815 Analyze_Call_And_Resolve;
1817 -- In Ada 2012. a qualified expression is a name, but it cannot be a
1818 -- procedure name, so the construct can only be a qualified expression.
1820 elsif Nkind (P) = N_Qualified_Expression
1821 and then Ada_Version >= Ada_2012
1822 then
1823 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
1824 Analyze (N);
1826 -- Anything else is an error
1828 else
1829 Error_Msg_N ("invalid procedure or entry call", N);
1831 -- Specialize the error message in the case where both a primitive
1832 -- operation and a record component are visible at the same time.
1834 if Nkind (P) = N_Selected_Component
1835 and then Is_Entity_Name (Selector_Name (P))
1836 then
1837 declare
1838 Sel : constant Entity_Id := Entity (Selector_Name (P));
1839 begin
1840 if Ekind (Sel) = E_Component
1841 and then Present (Homonym (Sel))
1842 and then Ekind (Homonym (Sel)) = E_Procedure
1843 then
1844 Error_Msg_NE ("\component & conflicts with"
1845 & " homonym procedure (RM 4.1.3 (9.2/3))",
1846 Selector_Name (P), Sel);
1847 end if;
1848 end;
1849 end if;
1850 end if;
1852 <<Leave>>
1853 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1854 end Analyze_Procedure_Call;
1856 ------------------------------
1857 -- Analyze_Return_Statement --
1858 ------------------------------
1860 procedure Analyze_Return_Statement (N : Node_Id) is
1861 pragma Assert
1862 (Nkind (N) in N_Extended_Return_Statement | N_Simple_Return_Statement);
1864 Returns_Object : constant Boolean :=
1865 Nkind (N) = N_Extended_Return_Statement
1866 or else
1867 (Nkind (N) = N_Simple_Return_Statement
1868 and then Present (Expression (N)));
1869 -- True if we're returning something; that is, "return <expression>;"
1870 -- or "return Result : T [:= ...]". False for "return;". Used for error
1871 -- checking: If Returns_Object is True, N should apply to a function
1872 -- body; otherwise N should apply to a procedure body, entry body,
1873 -- accept statement, or extended return statement.
1875 function Find_What_It_Applies_To return Entity_Id;
1876 -- Find the entity representing the innermost enclosing body, accept
1877 -- statement, or extended return statement. If the result is a callable
1878 -- construct or extended return statement, then this will be the value
1879 -- of the Return_Applies_To attribute. Otherwise, the program is
1880 -- illegal. See RM-6.5(4/2).
1882 -----------------------------
1883 -- Find_What_It_Applies_To --
1884 -----------------------------
1886 function Find_What_It_Applies_To return Entity_Id is
1887 Result : Entity_Id := Empty;
1889 begin
1890 -- Loop outward through the Scope_Stack, skipping blocks, and loops
1892 for J in reverse 0 .. Scope_Stack.Last loop
1893 Result := Scope_Stack.Table (J).Entity;
1894 exit when Ekind (Result) not in E_Block | E_Loop;
1895 end loop;
1897 pragma Assert (Present (Result));
1898 return Result;
1899 end Find_What_It_Applies_To;
1901 -- Local declarations
1903 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
1904 Kind : constant Entity_Kind := Ekind (Scope_Id);
1905 Loc : constant Source_Ptr := Sloc (N);
1906 Stm_Entity : constant Entity_Id :=
1907 New_Internal_Entity
1908 (E_Return_Statement, Current_Scope, Loc, 'R');
1910 -- Start of processing for Analyze_Return_Statement
1912 begin
1913 Set_Return_Statement_Entity (N, Stm_Entity);
1915 Set_Etype (Stm_Entity, Standard_Void_Type);
1916 Set_Return_Applies_To (Stm_Entity, Scope_Id);
1918 -- Place Return entity on scope stack, to simplify enforcement of 6.5
1919 -- (4/2): an inner return statement will apply to this extended return.
1921 if Nkind (N) = N_Extended_Return_Statement then
1922 Push_Scope (Stm_Entity);
1923 end if;
1925 -- Check that pragma No_Return is obeyed. Don't complain about the
1926 -- implicitly-generated return that is placed at the end.
1928 if No_Return (Scope_Id)
1929 and then Kind in E_Procedure | E_Generic_Procedure
1930 and then Comes_From_Source (N)
1931 then
1932 Error_Msg_N
1933 ("RETURN statement not allowed in No_Return procedure", N);
1934 end if;
1936 -- Warn on any unassigned OUT parameters if in procedure
1938 if Ekind (Scope_Id) = E_Procedure then
1939 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
1940 end if;
1942 -- Check that functions return objects, and other things do not
1944 if Kind in E_Function | E_Generic_Function then
1945 if not Returns_Object then
1946 Error_Msg_N ("missing expression in return from function", N);
1947 end if;
1949 elsif Kind in E_Procedure | E_Generic_Procedure then
1950 if Returns_Object then
1951 Error_Msg_N ("procedure cannot return value (use function)", N);
1952 end if;
1954 elsif Kind in E_Entry | E_Entry_Family then
1955 if Returns_Object then
1956 if Is_Protected_Type (Scope (Scope_Id)) then
1957 Error_Msg_N ("entry body cannot return value", N);
1958 else
1959 Error_Msg_N ("accept statement cannot return value", N);
1960 end if;
1961 end if;
1963 elsif Kind = E_Return_Statement then
1965 -- We are nested within another return statement, which must be an
1966 -- extended_return_statement.
1968 if Returns_Object then
1969 if Nkind (N) = N_Extended_Return_Statement then
1970 Error_Msg_N
1971 ("extended return statement cannot be nested (use `RETURN;`)",
1974 -- Case of a simple return statement with a value inside extended
1975 -- return statement.
1977 else
1978 Error_Msg_N
1979 ("return nested in extended return statement cannot return "
1980 & "value (use `RETURN;`)", N);
1981 end if;
1982 end if;
1984 else
1985 Error_Msg_N ("illegal context for return statement", N);
1986 end if;
1988 if Kind in E_Function | E_Generic_Function then
1989 Analyze_Function_Return (N);
1991 elsif Kind in E_Procedure | E_Generic_Procedure then
1992 Set_Return_Present (Scope_Id);
1993 end if;
1995 if Nkind (N) = N_Extended_Return_Statement then
1996 End_Scope;
1997 end if;
1999 Kill_Current_Values (Last_Assignment_Only => True);
2000 Check_Unreachable_Code (N);
2002 Analyze_Dimension (N);
2003 end Analyze_Return_Statement;
2005 -----------------------------------
2006 -- Analyze_Return_When_Statement --
2007 -----------------------------------
2009 procedure Analyze_Return_When_Statement (N : Node_Id) is
2010 begin
2011 -- Verify the condition is a Boolean expression
2013 Analyze_And_Resolve (Condition (N), Any_Boolean);
2014 Check_Unset_Reference (Condition (N));
2015 end Analyze_Return_When_Statement;
2017 -------------------------------------
2018 -- Analyze_Simple_Return_Statement --
2019 -------------------------------------
2021 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2022 begin
2023 if Present (Expression (N)) then
2024 Mark_Coextensions (N, Expression (N));
2025 end if;
2027 Analyze_Return_Statement (N);
2028 end Analyze_Simple_Return_Statement;
2030 -------------------------
2031 -- Analyze_Return_Type --
2032 -------------------------
2034 procedure Analyze_Return_Type (N : Node_Id) is
2035 Designator : constant Entity_Id := Defining_Entity (N);
2036 Typ : Entity_Id := Empty;
2038 begin
2039 -- Normal case where result definition does not indicate an error
2041 if Result_Definition (N) /= Error then
2042 if Nkind (Result_Definition (N)) = N_Access_Definition then
2044 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2046 declare
2047 AD : constant Node_Id :=
2048 Access_To_Subprogram_Definition (Result_Definition (N));
2049 begin
2050 if Present (AD) and then Protected_Present (AD) then
2051 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2052 else
2053 Typ := Access_Definition (N, Result_Definition (N));
2054 end if;
2055 end;
2057 Set_Parent (Typ, Result_Definition (N));
2058 Set_Is_Local_Anonymous_Access (Typ);
2059 Set_Etype (Designator, Typ);
2061 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2063 Null_Exclusion_Static_Checks (N);
2065 -- Subtype_Mark case
2067 else
2068 Find_Type (Result_Definition (N));
2069 Typ := Entity (Result_Definition (N));
2070 Set_Etype (Designator, Typ);
2072 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2074 Null_Exclusion_Static_Checks (N);
2076 -- If a null exclusion is imposed on the result type, then create
2077 -- a null-excluding itype (an access subtype) and use it as the
2078 -- function's Etype. Note that the null exclusion checks are done
2079 -- right before this, because they don't get applied to types that
2080 -- do not come from source.
2082 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2083 Set_Etype (Designator,
2084 Create_Null_Excluding_Itype
2085 (T => Typ,
2086 Related_Nod => N,
2087 Scope_Id => Scope (Current_Scope)));
2089 -- The new subtype must be elaborated before use because
2090 -- it is visible outside of the function. However its base
2091 -- type may not be frozen yet, so the reference that will
2092 -- force elaboration must be attached to the freezing of
2093 -- the base type.
2095 -- If the return specification appears on a proper body,
2096 -- the subtype will have been created already on the spec.
2098 if Is_Frozen (Typ) then
2099 if Nkind (Parent (N)) = N_Subprogram_Body
2100 and then Nkind (Parent (Parent (N))) = N_Subunit
2101 then
2102 null;
2103 else
2104 Build_Itype_Reference (Etype (Designator), Parent (N));
2105 end if;
2107 else
2108 declare
2109 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2110 begin
2111 Set_Itype (IR, Etype (Designator));
2112 Append_Freeze_Action (Typ, IR);
2113 end;
2114 end if;
2116 else
2117 Set_Etype (Designator, Typ);
2118 end if;
2120 if Ekind (Typ) = E_Incomplete_Type
2121 or else (Is_Class_Wide_Type (Typ)
2122 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2123 then
2124 -- AI05-0151: Tagged incomplete types are allowed in all formal
2125 -- parts. Untagged incomplete types are not allowed in bodies.
2126 -- As a consequence, limited views cannot appear in a basic
2127 -- declaration that is itself within a body, because there is
2128 -- no point at which the non-limited view will become visible.
2130 if Ada_Version >= Ada_2012 then
2131 if From_Limited_With (Typ) and then In_Package_Body then
2132 Error_Msg_NE
2133 ("invalid use of incomplete type&",
2134 Result_Definition (N), Typ);
2136 -- The return type of a subprogram body cannot be of a
2137 -- formal incomplete type.
2139 elsif Is_Generic_Type (Typ)
2140 and then Nkind (Parent (N)) = N_Subprogram_Body
2141 then
2142 Error_Msg_N
2143 ("return type cannot be a formal incomplete type",
2144 Result_Definition (N));
2146 elsif Is_Class_Wide_Type (Typ)
2147 and then Is_Generic_Type (Root_Type (Typ))
2148 and then Nkind (Parent (N)) = N_Subprogram_Body
2149 then
2150 Error_Msg_N
2151 ("return type cannot be a formal incomplete type",
2152 Result_Definition (N));
2154 elsif Is_Tagged_Type (Typ) then
2155 null;
2157 -- Use is legal in a thunk generated for an operation
2158 -- inherited from a progenitor.
2160 elsif Is_Thunk (Designator)
2161 and then Present (Non_Limited_View (Typ))
2162 then
2163 null;
2165 elsif Nkind (Parent (N)) = N_Subprogram_Body
2166 or else Nkind (Parent (Parent (N))) in
2167 N_Accept_Statement | N_Entry_Body
2168 then
2169 Error_Msg_NE
2170 ("invalid use of untagged incomplete type&",
2171 Designator, Typ);
2172 end if;
2174 -- The type must be completed in the current package. This
2175 -- is checked at the end of the package declaration when
2176 -- Taft-amendment types are identified. If the return type
2177 -- is class-wide, there is no required check, the type can
2178 -- be a bona fide TAT.
2180 if Ekind (Scope (Current_Scope)) = E_Package
2181 and then In_Private_Part (Scope (Current_Scope))
2182 and then not Is_Class_Wide_Type (Typ)
2183 then
2184 Append_Elmt (Designator, Private_Dependents (Typ));
2185 end if;
2187 else
2188 Error_Msg_NE
2189 ("invalid use of incomplete type&", Designator, Typ);
2190 end if;
2191 end if;
2192 end if;
2194 -- Case where result definition does indicate an error
2196 else
2197 Set_Etype (Designator, Any_Type);
2198 end if;
2199 end Analyze_Return_Type;
2201 -----------------------------
2202 -- Analyze_Subprogram_Body --
2203 -----------------------------
2205 procedure Analyze_Subprogram_Body (N : Node_Id) is
2206 Loc : constant Source_Ptr := Sloc (N);
2207 Body_Spec : constant Node_Id := Specification (N);
2208 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2210 begin
2211 if Debug_Flag_C then
2212 Write_Str ("==> subprogram body ");
2213 Write_Name (Chars (Body_Id));
2214 Write_Str (" from ");
2215 Write_Location (Loc);
2216 Write_Eol;
2217 Indent;
2218 end if;
2220 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2222 -- The real work is split out into the helper, so it can do "return;"
2223 -- without skipping the debug output:
2225 Analyze_Subprogram_Body_Helper (N);
2227 if Debug_Flag_C then
2228 Outdent;
2229 Write_Str ("<== subprogram body ");
2230 Write_Name (Chars (Body_Id));
2231 Write_Str (" from ");
2232 Write_Location (Loc);
2233 Write_Eol;
2234 end if;
2235 end Analyze_Subprogram_Body;
2237 ------------------------------------
2238 -- Analyze_Subprogram_Body_Helper --
2239 ------------------------------------
2241 -- This procedure is called for regular subprogram bodies, generic bodies,
2242 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2243 -- specification matters, and is used to create a proper declaration for
2244 -- the subprogram, or to perform conformance checks.
2246 -- WARNING: This routine manages Ghost regions. Return statements must be
2247 -- replaced by gotos that jump to the end of the routine and restore the
2248 -- Ghost mode.
2250 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2251 Body_Spec : Node_Id := Specification (N);
2252 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2253 Loc : constant Source_Ptr := Sloc (N);
2254 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2256 Body_Nod : Node_Id := Empty;
2257 Minimum_Acc_Objs : List_Id := No_List;
2259 Conformant : Boolean;
2260 Desig_View : Entity_Id := Empty;
2261 Exch_Views : Elist_Id := No_Elist;
2262 Mask_Types : Elist_Id := No_Elist;
2263 Prot_Typ : Entity_Id := Empty;
2264 Spec_Decl : Node_Id := Empty;
2265 Spec_Id : Entity_Id;
2267 Last_Real_Spec_Entity : Entity_Id := Empty;
2268 -- When we analyze a separate spec, the entity chain ends up containing
2269 -- the formals, as well as any itypes generated during analysis of the
2270 -- default expressions for parameters, or the arguments of associated
2271 -- precondition/postcondition pragmas (which are analyzed in the context
2272 -- of the spec since they have visibility on formals).
2274 -- These entities belong with the spec and not the body. However we do
2275 -- the analysis of the body in the context of the spec (again to obtain
2276 -- visibility to the formals), and all the entities generated during
2277 -- this analysis end up also chained to the entity chain of the spec.
2278 -- But they really belong to the body, and there is circuitry to move
2279 -- them from the spec to the body.
2281 -- However, when we do this move, we don't want to move the real spec
2282 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2283 -- variable points to the last real spec entity, so we only move those
2284 -- chained beyond that point. It is initialized to Empty to deal with
2285 -- the case where there is no separate spec.
2287 function Body_Has_Contract return Boolean;
2288 -- Check whether unanalyzed body has an aspect or pragma that may
2289 -- generate a SPARK contract.
2291 function Body_Has_SPARK_Mode_On return Boolean;
2292 -- Check whether SPARK_Mode On applies to the subprogram body, either
2293 -- because it is specified directly on the body, or because it is
2294 -- inherited from the enclosing subprogram or package.
2296 function Build_Internal_Protected_Declaration
2297 (N : Node_Id) return Entity_Id;
2298 -- A subprogram body without a previous spec that appears in a protected
2299 -- body must be expanded separately to create a subprogram declaration
2300 -- for it, in order to resolve internal calls to it from other protected
2301 -- operations.
2303 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2305 procedure Build_Subprogram_Declaration;
2306 -- Create a matching subprogram declaration for subprogram body N
2308 procedure Check_Anonymous_Return;
2309 -- Ada 2005: if a function returns an access type that denotes a task,
2310 -- or a type that contains tasks, we must create a master entity for
2311 -- the anonymous type, which typically will be used in an allocator
2312 -- in the body of the function.
2314 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2315 -- Look ahead to recognize a pragma that may appear after the body.
2316 -- If there is a previous spec, check that it appears in the same
2317 -- declarative part. If the pragma is Inline_Always, perform inlining
2318 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2319 -- If the body acts as a spec, and inlining is required, we create a
2320 -- subprogram declaration for it, in order to attach the body to inline.
2321 -- If pragma does not appear after the body, check whether there is
2322 -- an inline pragma before any local declarations.
2324 procedure Check_Missing_Return;
2325 -- Checks for a function with a no return statements, and also performs
2326 -- the warning checks implemented by Check_Returns.
2328 function Disambiguate_Spec return Entity_Id;
2329 -- When a primitive is declared between the private view and the full
2330 -- view of a concurrent type which implements an interface, a special
2331 -- mechanism is used to find the corresponding spec of the primitive
2332 -- body.
2334 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2335 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2336 -- incomplete types coming from a limited context and replace their
2337 -- limited views with the non-limited ones. Return the list of changes
2338 -- to be used to undo the transformation.
2340 procedure Generate_Minimum_Accessibility
2341 (Extra_Access : Entity_Id;
2342 Related_Form : Entity_Id := Empty);
2343 -- Generate a minimum accessibility object for a given extra
2344 -- accessibility formal (Extra_Access) and its related formal if it
2345 -- exists.
2347 function Is_Private_Concurrent_Primitive
2348 (Subp_Id : Entity_Id) return Boolean;
2349 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2350 -- type that implements an interface and has a private view.
2352 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2353 -- N is the body generated for an expression function that is not a
2354 -- completion and Spec_Id the defining entity of its spec. Mark all
2355 -- the not-yet-frozen types referenced by the simple return statement
2356 -- of the function as formally frozen.
2358 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2359 -- Find all suitable source pragmas at the top of subprogram body
2360 -- From's declarations and move them after arbitrary node To.
2361 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2362 -- as it applies to the body too.
2364 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2365 -- Undo the transformation done by Exchange_Limited_Views.
2367 procedure Set_Trivial_Subprogram (N : Node_Id);
2368 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2369 -- subprogram whose body is being analyzed. N is the statement node
2370 -- causing the flag to be set, if the following statement is a return
2371 -- of an entity, we mark the entity as set in source to suppress any
2372 -- warning on the stylized use of function stubs with a dummy return.
2374 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2375 -- Undo the transformation done by Mask_Unfrozen_Types
2377 procedure Verify_Overriding_Indicator;
2378 -- If there was a previous spec, the entity has been entered in the
2379 -- current scope previously. If the body itself carries an overriding
2380 -- indicator, check that it is consistent with the known status of the
2381 -- entity.
2383 -----------------------
2384 -- Body_Has_Contract --
2385 -----------------------
2387 function Body_Has_Contract return Boolean is
2388 Decls : constant List_Id := Declarations (N);
2389 Item : Node_Id;
2391 begin
2392 -- Check for aspects that may generate a contract
2394 Item := First (Aspect_Specifications (N));
2395 while Present (Item) loop
2396 if Is_Subprogram_Contract_Annotation (Item) then
2397 return True;
2398 end if;
2400 Next (Item);
2401 end loop;
2403 -- Check for pragmas that may generate a contract
2405 Item := First (Decls);
2406 while Present (Item) loop
2407 if Nkind (Item) = N_Pragma
2408 and then Is_Subprogram_Contract_Annotation (Item)
2409 then
2410 return True;
2411 end if;
2413 Next (Item);
2414 end loop;
2416 return False;
2417 end Body_Has_Contract;
2419 ----------------------------
2420 -- Body_Has_SPARK_Mode_On --
2421 ----------------------------
2423 function Body_Has_SPARK_Mode_On return Boolean is
2424 Decls : constant List_Id := Declarations (N);
2425 Item : Node_Id;
2427 begin
2428 -- Check for SPARK_Mode aspect
2430 Item := First (Aspect_Specifications (N));
2431 while Present (Item) loop
2432 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2433 return Get_SPARK_Mode_From_Annotation (Item) = On;
2434 end if;
2436 Next (Item);
2437 end loop;
2439 -- Check for SPARK_Mode pragma
2441 Item := First (Decls);
2442 while Present (Item) loop
2444 -- Pragmas that apply to a subprogram body are usually grouped
2445 -- together. Look for a potential pragma SPARK_Mode among them.
2447 if Nkind (Item) = N_Pragma then
2448 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2449 return Get_SPARK_Mode_From_Annotation (Item) = On;
2450 end if;
2452 -- Otherwise the first non-pragma declarative item terminates the
2453 -- region where pragma SPARK_Mode may appear.
2455 else
2456 exit;
2457 end if;
2459 Next (Item);
2460 end loop;
2462 -- Otherwise, the applicable SPARK_Mode is inherited from the
2463 -- enclosing subprogram or package.
2465 return SPARK_Mode = On;
2466 end Body_Has_SPARK_Mode_On;
2468 ------------------------------------------
2469 -- Build_Internal_Protected_Declaration --
2470 ------------------------------------------
2472 function Build_Internal_Protected_Declaration
2473 (N : Node_Id) return Entity_Id
2475 procedure Analyze_Pragmas (From : Node_Id);
2476 -- Analyze all pragmas which follow arbitrary node From
2478 ---------------------
2479 -- Analyze_Pragmas --
2480 ---------------------
2482 procedure Analyze_Pragmas (From : Node_Id) is
2483 Decl : Node_Id;
2485 begin
2486 Decl := Next (From);
2487 while Present (Decl) loop
2488 if Nkind (Decl) = N_Pragma then
2489 Analyze_Pragma (Decl);
2491 -- No candidate pragmas are available for analysis
2493 else
2494 exit;
2495 end if;
2497 Next (Decl);
2498 end loop;
2499 end Analyze_Pragmas;
2501 -- Local variables
2503 Body_Id : constant Entity_Id := Defining_Entity (N);
2504 Loc : constant Source_Ptr := Sloc (N);
2505 Decl : Node_Id;
2506 Formal : Entity_Id;
2507 Formals : List_Id;
2508 Spec : Node_Id;
2509 Spec_Id : Entity_Id;
2511 -- Start of processing for Build_Internal_Protected_Declaration
2513 begin
2514 Formal := First_Formal (Body_Id);
2516 -- The protected operation always has at least one formal, namely the
2517 -- object itself, but it is only placed in the parameter list if
2518 -- expansion is enabled.
2520 if Present (Formal) or else Expander_Active then
2521 Formals := Copy_Parameter_List (Body_Id);
2522 else
2523 Formals := No_List;
2524 end if;
2526 Spec_Id :=
2527 Make_Defining_Identifier (Sloc (Body_Id),
2528 Chars => Chars (Body_Id));
2530 -- Indicate that the entity comes from source, to ensure that cross-
2531 -- reference information is properly generated. The body itself is
2532 -- rewritten during expansion, and the body entity will not appear in
2533 -- calls to the operation.
2535 Set_Comes_From_Source (Spec_Id, True);
2537 if Nkind (Specification (N)) = N_Procedure_Specification then
2538 Spec :=
2539 Make_Procedure_Specification (Loc,
2540 Defining_Unit_Name => Spec_Id,
2541 Parameter_Specifications => Formals);
2542 else
2543 Spec :=
2544 Make_Function_Specification (Loc,
2545 Defining_Unit_Name => Spec_Id,
2546 Parameter_Specifications => Formals,
2547 Result_Definition =>
2548 New_Occurrence_Of (Etype (Body_Id), Loc));
2549 end if;
2551 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2552 Set_Corresponding_Body (Decl, Body_Id);
2553 Set_Corresponding_Spec (N, Spec_Id);
2555 Insert_Before (N, Decl);
2557 -- Associate all aspects and pragmas of the body with the spec. This
2558 -- ensures that these annotations apply to the initial declaration of
2559 -- the subprogram body.
2561 Move_Aspects (From => N, To => Decl);
2562 Move_Pragmas (From => N, To => Decl);
2564 Analyze (Decl);
2566 -- The analysis of the spec may generate pragmas which require manual
2567 -- analysis. Since the generation of the spec and the relocation of
2568 -- the annotations is driven by the expansion of the stand-alone
2569 -- body, the pragmas will not be analyzed in a timely manner. Do this
2570 -- now.
2572 Analyze_Pragmas (Decl);
2574 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
2575 -- ensuring in particular that 'Access is illegal.
2577 Set_Convention (Spec_Id, Convention_Intrinsic);
2578 Set_Has_Completion (Spec_Id);
2580 return Spec_Id;
2581 end Build_Internal_Protected_Declaration;
2583 ----------------------------------
2584 -- Build_Subprogram_Declaration --
2585 ----------------------------------
2587 procedure Build_Subprogram_Declaration is
2588 Decl : Node_Id;
2589 Subp_Decl : Node_Id;
2591 begin
2592 -- Create a matching subprogram spec using the profile of the body.
2593 -- The structure of the tree is identical, but has new entities for
2594 -- the defining unit name and formal parameters.
2596 Subp_Decl :=
2597 Make_Subprogram_Declaration (Loc,
2598 Specification => Copy_Subprogram_Spec (Body_Spec));
2599 Set_Comes_From_Source (Subp_Decl, True);
2601 -- Also mark parameters as coming from source
2603 if Present (Parameter_Specifications (Specification (Subp_Decl))) then
2604 declare
2605 Form : Entity_Id;
2606 begin
2607 Form :=
2608 First (Parameter_Specifications (Specification (Subp_Decl)));
2610 while Present (Form) loop
2611 Set_Comes_From_Source (Defining_Identifier (Form), True);
2612 Next (Form);
2613 end loop;
2614 end;
2615 end if;
2617 -- Relocate the aspects and relevant pragmas from the subprogram body
2618 -- to the generated spec because it acts as the initial declaration.
2620 Insert_Before (N, Subp_Decl);
2621 Move_Aspects (N, To => Subp_Decl);
2622 Move_Pragmas (N, To => Subp_Decl);
2624 -- Ensure that the generated corresponding spec and original body
2625 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2626 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2627 -- correctly set for local subprograms.
2629 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2631 Analyze (Subp_Decl);
2633 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2634 -- the body since the expander may generate calls using that entity.
2635 -- Required to ensure that Expand_Call rewrites calls to this
2636 -- function by calls to the built procedure.
2638 if Transform_Function_Array
2639 and then Nkind (Body_Spec) = N_Function_Specification
2640 and then
2641 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2642 then
2643 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2644 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2645 Corresponding_Procedure
2646 (Defining_Entity (Specification (Subp_Decl))));
2647 end if;
2649 -- Analyze any relocated source pragmas or pragmas created for aspect
2650 -- specifications.
2652 Decl := Next (Subp_Decl);
2653 while Present (Decl) loop
2655 -- Stop the search for pragmas once the body has been reached as
2656 -- this terminates the region where pragmas may appear.
2658 if Decl = N then
2659 exit;
2661 elsif Nkind (Decl) = N_Pragma then
2662 Analyze (Decl);
2663 end if;
2665 Next (Decl);
2666 end loop;
2668 Spec_Id := Defining_Entity (Subp_Decl);
2669 Set_Corresponding_Spec (N, Spec_Id);
2671 -- Mark the generated spec as a source construct to ensure that all
2672 -- calls to it are properly registered in ALI files for GNATprove.
2674 Set_Comes_From_Source (Spec_Id, True);
2676 -- Ensure that the specs of the subprogram declaration and its body
2677 -- are identical, otherwise they will appear non-conformant due to
2678 -- rewritings in the default values of formal parameters.
2680 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2681 Set_Specification (N, Body_Spec);
2682 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2683 end Build_Subprogram_Declaration;
2685 ----------------------------
2686 -- Check_Anonymous_Return --
2687 ----------------------------
2689 procedure Check_Anonymous_Return is
2690 Decl : Node_Id;
2691 Par : Node_Id;
2692 Scop : Entity_Id;
2694 begin
2695 if Present (Spec_Id) then
2696 Scop := Spec_Id;
2697 else
2698 Scop := Body_Id;
2699 end if;
2701 if Ekind (Scop) = E_Function
2702 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2703 and then not Is_Thunk (Scop)
2705 -- Skip internally built functions which handle the case of
2706 -- a null access (see Expand_Interface_Conversion)
2708 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2709 and then not Comes_From_Source (Parent (Scop)))
2711 and then (Has_Task (Designated_Type (Etype (Scop)))
2712 or else
2713 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2714 and then
2715 Is_Limited_Record
2716 (Etype (Designated_Type (Etype (Scop))))))
2717 and then Expander_Active
2718 then
2719 Decl := Build_Master_Declaration (Loc);
2721 if Present (Declarations (N)) then
2722 Prepend (Decl, Declarations (N));
2723 else
2724 Set_Declarations (N, New_List (Decl));
2725 end if;
2727 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2728 Set_Has_Master_Entity (Scop);
2730 -- Now mark the containing scope as a task master
2732 Par := N;
2733 while Nkind (Par) /= N_Compilation_Unit loop
2734 Par := Parent (Par);
2735 pragma Assert (Present (Par));
2737 -- If we fall off the top, we are at the outer level, and
2738 -- the environment task is our effective master, so nothing
2739 -- to mark.
2741 if Nkind (Par)
2742 in N_Task_Body | N_Block_Statement | N_Subprogram_Body
2743 then
2744 Set_Is_Task_Master (Par, True);
2745 exit;
2746 end if;
2747 end loop;
2748 end if;
2749 end Check_Anonymous_Return;
2751 -------------------------
2752 -- Check_Inline_Pragma --
2753 -------------------------
2755 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2756 Prag : Node_Id;
2757 Plist : List_Id;
2759 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2760 -- True when N is a pragma Inline or Inline_Always that applies
2761 -- to this subprogram.
2763 -----------------------
2764 -- Is_Inline_Pragma --
2765 -----------------------
2767 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2768 begin
2769 if Nkind (N) = N_Pragma
2770 and then
2771 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2772 or else (Pragma_Name_Unmapped (N) = Name_Inline
2773 and then
2774 (Front_End_Inlining or else Optimization_Level > 0)))
2775 and then Present (Pragma_Argument_Associations (N))
2776 then
2777 declare
2778 Pragma_Arg : Node_Id :=
2779 Expression (First (Pragma_Argument_Associations (N)));
2780 begin
2781 if Nkind (Pragma_Arg) = N_Selected_Component then
2782 Pragma_Arg := Selector_Name (Pragma_Arg);
2783 end if;
2785 return Chars (Pragma_Arg) = Chars (Body_Id);
2786 end;
2788 else
2789 return False;
2790 end if;
2791 end Is_Inline_Pragma;
2793 -- Start of processing for Check_Inline_Pragma
2795 begin
2796 if not Expander_Active then
2797 return;
2798 end if;
2800 if Is_List_Member (N)
2801 and then Present (Next (N))
2802 and then Is_Inline_Pragma (Next (N))
2803 then
2804 Prag := Next (N);
2806 elsif Nkind (N) /= N_Subprogram_Body_Stub
2807 and then Present (Declarations (N))
2808 and then Is_Inline_Pragma (First (Declarations (N)))
2809 then
2810 Prag := First (Declarations (N));
2812 else
2813 Prag := Empty;
2814 end if;
2816 if Present (Prag) and then Is_List_Member (N) then
2817 if Present (Spec_Id) then
2818 if Is_List_Member (Unit_Declaration_Node (Spec_Id))
2819 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
2820 then
2821 Analyze (Prag);
2822 end if;
2823 else
2824 -- Create a subprogram declaration, to make treatment uniform.
2825 -- Make the sloc of the subprogram name that of the entity in
2826 -- the body, so that style checks find identical strings.
2828 declare
2829 Subp : constant Entity_Id :=
2830 Make_Defining_Identifier
2831 (Sloc (Body_Id), Chars (Body_Id));
2832 Decl : constant Node_Id :=
2833 Make_Subprogram_Declaration (Loc,
2834 Specification =>
2835 New_Copy_Tree (Specification (N)));
2837 begin
2838 -- Link the body and the generated spec
2840 Set_Corresponding_Body (Decl, Body_Id);
2842 if Nkind (N) = N_Subprogram_Body_Stub then
2843 Set_Corresponding_Spec_Of_Stub (N, Subp);
2844 else
2845 Set_Corresponding_Spec (N, Subp);
2846 end if;
2848 Set_Defining_Unit_Name (Specification (Decl), Subp);
2850 -- To ensure proper coverage when body is inlined, indicate
2851 -- whether the subprogram comes from source.
2853 Preserve_Comes_From_Source (Subp, N);
2855 if Present (First_Formal (Body_Id)) then
2856 Plist := Copy_Parameter_List (Body_Id);
2857 Set_Parameter_Specifications
2858 (Specification (Decl), Plist);
2859 end if;
2861 -- Move aspects to the new spec
2863 if Has_Aspects (N) then
2864 Move_Aspects (N, To => Decl);
2865 end if;
2867 Insert_Before (N, Decl);
2868 Analyze (Decl);
2869 Analyze (Prag);
2870 Set_Has_Pragma_Inline (Subp);
2872 if Pragma_Name (Prag) = Name_Inline_Always then
2873 Set_Is_Inlined (Subp);
2874 Set_Has_Pragma_Inline_Always (Subp);
2875 end if;
2877 -- Prior to copying the subprogram body to create a template
2878 -- for it for subsequent inlining, remove the pragma from
2879 -- the current body so that the copy that will produce the
2880 -- new body will start from a completely unanalyzed tree.
2882 if Nkind (Parent (Prag)) = N_Subprogram_Body then
2883 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
2884 end if;
2886 Spec := Subp;
2887 end;
2888 end if;
2889 end if;
2890 end Check_Inline_Pragma;
2892 --------------------------
2893 -- Check_Missing_Return --
2894 --------------------------
2896 procedure Check_Missing_Return is
2897 HSS : constant Node_Id := Handled_Statement_Sequence (N);
2899 Id : Entity_Id;
2900 Missing_Ret : Boolean;
2902 begin
2903 if Nkind (Body_Spec) = N_Function_Specification then
2904 if Present (Spec_Id) then
2905 Id := Spec_Id;
2906 else
2907 Id := Body_Id;
2908 end if;
2910 -- A function body shall contain at least one return statement
2911 -- that applies to the function body, unless the function contains
2912 -- code_statements; RM 6.5(5).
2914 if Return_Present (Id) then
2915 Check_Returns (HSS, 'F', Missing_Ret);
2917 if Missing_Ret then
2918 Set_Has_Missing_Return (Id);
2919 end if;
2921 -- Within a premature instantiation of a package with no body, we
2922 -- build completions of the functions therein, with a Raise
2923 -- statement. No point in complaining about a missing return in
2924 -- this case.
2926 elsif Ekind (Id) = E_Function
2927 and then In_Instance
2928 and then Present (Statements (HSS))
2929 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
2930 then
2931 null;
2933 elsif Is_Generic_Subprogram (Id)
2934 or else not Is_Machine_Code_Subprogram (Id)
2935 then
2936 Error_Msg_N ("missing RETURN statement in function body", N);
2937 end if;
2939 -- If procedure with No_Return, check returns
2941 elsif Nkind (Body_Spec) = N_Procedure_Specification then
2942 if Present (Spec_Id) then
2943 Id := Spec_Id;
2944 else
2945 Id := Body_Id;
2946 end if;
2948 if No_Return (Id) then
2949 Check_Returns (HSS, 'P', Missing_Ret, Id);
2950 end if;
2951 end if;
2952 end Check_Missing_Return;
2954 -----------------------
2955 -- Disambiguate_Spec --
2956 -----------------------
2958 function Disambiguate_Spec return Entity_Id is
2959 Priv_Spec : Entity_Id;
2960 Spec_N : Entity_Id;
2962 procedure Replace_Types (To_Corresponding : Boolean);
2963 -- Depending on the flag, replace the type of formal parameters of
2964 -- Body_Id if it is a concurrent type implementing interfaces with
2965 -- the corresponding record type or the other way around.
2967 procedure Replace_Types (To_Corresponding : Boolean) is
2968 Formal : Entity_Id;
2969 Formal_Typ : Entity_Id;
2971 begin
2972 Formal := First_Formal (Body_Id);
2973 while Present (Formal) loop
2974 Formal_Typ := Etype (Formal);
2976 if Is_Class_Wide_Type (Formal_Typ) then
2977 Formal_Typ := Root_Type (Formal_Typ);
2978 end if;
2980 -- From concurrent type to corresponding record
2982 if To_Corresponding then
2983 if Is_Concurrent_Type (Formal_Typ)
2984 and then Present (Corresponding_Record_Type (Formal_Typ))
2985 and then
2986 Present (Interfaces
2987 (Corresponding_Record_Type (Formal_Typ)))
2988 then
2989 Set_Etype (Formal,
2990 Corresponding_Record_Type (Formal_Typ));
2991 end if;
2993 -- From corresponding record to concurrent type
2995 else
2996 if Is_Concurrent_Record_Type (Formal_Typ)
2997 and then Present (Interfaces (Formal_Typ))
2998 then
2999 Set_Etype (Formal,
3000 Corresponding_Concurrent_Type (Formal_Typ));
3001 end if;
3002 end if;
3004 Next_Formal (Formal);
3005 end loop;
3006 end Replace_Types;
3008 -- Start of processing for Disambiguate_Spec
3010 begin
3011 -- Try to retrieve the specification of the body as is. All error
3012 -- messages are suppressed because the body may not have a spec in
3013 -- its current state.
3015 Spec_N := Find_Corresponding_Spec (N, False);
3017 -- It is possible that this is the body of a primitive declared
3018 -- between a private and a full view of a concurrent type. The
3019 -- controlling parameter of the spec carries the concurrent type,
3020 -- not the corresponding record type as transformed by Analyze_
3021 -- Subprogram_Specification. In such cases, we undo the change
3022 -- made by the analysis of the specification and try to find the
3023 -- spec again.
3025 -- Note that wrappers already have their corresponding specs and
3026 -- bodies set during their creation, so if the candidate spec is
3027 -- a wrapper, then we definitely need to swap all types to their
3028 -- original concurrent status.
3030 if No (Spec_N)
3031 or else Is_Primitive_Wrapper (Spec_N)
3032 then
3033 -- Restore all references of corresponding record types to the
3034 -- original concurrent types.
3036 Replace_Types (To_Corresponding => False);
3037 Priv_Spec := Find_Corresponding_Spec (N, False);
3039 -- The current body truly belongs to a primitive declared between
3040 -- a private and a full view. We leave the modified body as is,
3041 -- and return the true spec.
3043 if Present (Priv_Spec)
3044 and then Is_Private_Primitive (Priv_Spec)
3045 then
3046 return Priv_Spec;
3047 end if;
3049 -- In case that this is some sort of error, restore the original
3050 -- state of the body.
3052 Replace_Types (To_Corresponding => True);
3053 end if;
3055 return Spec_N;
3056 end Disambiguate_Spec;
3058 ----------------------------
3059 -- Exchange_Limited_Views --
3060 ----------------------------
3062 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3063 Result : Elist_Id := No_Elist;
3065 procedure Detect_And_Exchange (Id : Entity_Id);
3066 -- Determine whether Id's type denotes an incomplete type associated
3067 -- with a limited with clause and exchange the limited view with the
3068 -- non-limited one when available. Note that the non-limited view
3069 -- may exist because of a with_clause in another unit in the context,
3070 -- but cannot be used because the current view of the enclosing unit
3071 -- is still a limited view.
3073 -------------------------
3074 -- Detect_And_Exchange --
3075 -------------------------
3077 procedure Detect_And_Exchange (Id : Entity_Id) is
3078 Typ : constant Entity_Id := Etype (Id);
3080 begin
3081 if From_Limited_With (Typ)
3082 and then Has_Non_Limited_View (Typ)
3083 and then not From_Limited_With (Scope (Typ))
3084 then
3085 if No (Result) then
3086 Result := New_Elmt_List;
3087 end if;
3089 Prepend_Elmt (Typ, Result);
3090 Prepend_Elmt (Id, Result);
3091 Set_Etype (Id, Non_Limited_View (Typ));
3092 end if;
3093 end Detect_And_Exchange;
3095 -- Local variables
3097 Formal : Entity_Id;
3099 -- Start of processing for Exchange_Limited_Views
3101 begin
3102 -- Do not process subprogram bodies as they already use the non-
3103 -- limited view of types.
3105 if Ekind (Subp_Id) not in E_Function | E_Procedure then
3106 return No_Elist;
3107 end if;
3109 -- Examine all formals and swap views when applicable
3111 Formal := First_Formal (Subp_Id);
3112 while Present (Formal) loop
3113 Detect_And_Exchange (Formal);
3115 Next_Formal (Formal);
3116 end loop;
3118 -- Process the return type of a function
3120 if Ekind (Subp_Id) = E_Function then
3121 Detect_And_Exchange (Subp_Id);
3122 end if;
3124 return Result;
3125 end Exchange_Limited_Views;
3127 ------------------------------------
3128 -- Generate_Minimum_Accessibility --
3129 ------------------------------------
3131 procedure Generate_Minimum_Accessibility
3132 (Extra_Access : Entity_Id;
3133 Related_Form : Entity_Id := Empty)
3135 Loc : constant Source_Ptr := Sloc (Body_Nod);
3136 Form : Entity_Id;
3137 Obj_Node : Node_Id;
3138 begin
3139 -- When no related formal exists then we are dealing with an
3140 -- extra accessibility formal for a function result.
3142 if No (Related_Form) then
3143 Form := Extra_Access;
3144 else
3145 Form := Related_Form;
3146 end if;
3148 -- Create the minimum accessibility object
3150 Obj_Node :=
3151 Make_Object_Declaration (Loc,
3152 Defining_Identifier =>
3153 Make_Temporary
3154 (Loc, 'A', Extra_Access),
3155 Object_Definition => New_Occurrence_Of
3156 (Standard_Natural, Loc),
3157 Expression =>
3158 Make_Attribute_Reference (Loc,
3159 Prefix => New_Occurrence_Of
3160 (Standard_Natural, Loc),
3161 Attribute_Name => Name_Min,
3162 Expressions => New_List (
3163 Make_Integer_Literal (Loc,
3164 Scope_Depth (Body_Id)),
3165 New_Occurrence_Of
3166 (Extra_Access, Loc))));
3168 -- Add the new local object to the Minimum_Acc_Obj to
3169 -- be later prepended to the subprogram's list of
3170 -- declarations after we are sure all expansion is
3171 -- done.
3173 if Present (Minimum_Acc_Objs) then
3174 Prepend (Obj_Node, Minimum_Acc_Objs);
3175 else
3176 Minimum_Acc_Objs := New_List (Obj_Node);
3177 end if;
3179 -- Register the object and analyze it
3181 Set_Minimum_Accessibility
3182 (Form, Defining_Identifier (Obj_Node));
3184 Analyze (Obj_Node);
3185 end Generate_Minimum_Accessibility;
3187 -------------------------------------
3188 -- Is_Private_Concurrent_Primitive --
3189 -------------------------------------
3191 function Is_Private_Concurrent_Primitive
3192 (Subp_Id : Entity_Id) return Boolean
3194 Formal_Typ : Entity_Id;
3196 begin
3197 if Present (First_Formal (Subp_Id)) then
3198 Formal_Typ := Etype (First_Formal (Subp_Id));
3200 if Is_Concurrent_Record_Type (Formal_Typ) then
3201 if Is_Class_Wide_Type (Formal_Typ) then
3202 Formal_Typ := Root_Type (Formal_Typ);
3203 end if;
3205 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3206 end if;
3208 -- The type of the first formal is a concurrent tagged type with
3209 -- a private view.
3211 return
3212 Is_Concurrent_Type (Formal_Typ)
3213 and then Is_Tagged_Type (Formal_Typ)
3214 and then Has_Private_Declaration (Formal_Typ);
3215 end if;
3217 return False;
3218 end Is_Private_Concurrent_Primitive;
3220 -------------------------
3221 -- Mask_Unfrozen_Types --
3222 -------------------------
3224 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3225 Result : Elist_Id := No_Elist;
3227 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3228 -- Mask all types referenced in the subtree rooted at Node as
3229 -- formally frozen.
3231 --------------------
3232 -- Mask_Type_Refs --
3233 --------------------
3235 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3236 procedure Mask_Type (Typ : Entity_Id);
3237 -- Mask a given type as formally frozen when outside the current
3238 -- scope, or else freeze the type.
3240 ---------------
3241 -- Mask_Type --
3242 ---------------
3244 procedure Mask_Type (Typ : Entity_Id) is
3245 begin
3246 -- Skip Itypes created by the preanalysis
3248 if Is_Itype (Typ)
3249 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3250 then
3251 return;
3252 end if;
3254 if not Is_Frozen (Typ) then
3255 if Scope (Typ) /= Current_Scope then
3256 Set_Is_Frozen (Typ);
3257 Append_New_Elmt (Typ, Result);
3258 else
3259 Freeze_Before (N, Typ);
3260 end if;
3261 end if;
3262 end Mask_Type;
3264 -- Start of processing for Mask_Type_Refs
3266 begin
3267 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3268 Mask_Type (Etype (Entity (Node)));
3270 if Ekind (Entity (Node)) in E_Component | E_Discriminant then
3271 Mask_Type (Scope (Entity (Node)));
3272 end if;
3274 elsif Nkind (Node) in N_Aggregate | N_Null | N_Type_Conversion
3275 and then Present (Etype (Node))
3276 then
3277 Mask_Type (Etype (Node));
3278 end if;
3280 return OK;
3281 end Mask_Type_Refs;
3283 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3285 -- Local variables
3287 Return_Stmt : constant Node_Id :=
3288 First (Statements (Handled_Statement_Sequence (N)));
3290 -- Start of processing for Mask_Unfrozen_Types
3292 begin
3293 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3295 Mask_References (Expression (Return_Stmt));
3297 return Result;
3298 end Mask_Unfrozen_Types;
3300 ------------------
3301 -- Move_Pragmas --
3302 ------------------
3304 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
3305 Decl : Node_Id;
3306 Insert_Nod : Node_Id;
3307 Next_Decl : Node_Id;
3309 begin
3310 pragma Assert (Nkind (From) = N_Subprogram_Body);
3312 -- The pragmas are moved in an order-preserving fashion
3314 Insert_Nod := To;
3316 -- Inspect the declarations of the subprogram body and relocate all
3317 -- candidate pragmas.
3319 Decl := First (Declarations (From));
3320 while Present (Decl) loop
3322 -- Preserve the following declaration for iteration purposes, due
3323 -- to possible relocation of a pragma.
3325 Next_Decl := Next (Decl);
3327 if Nkind (Decl) = N_Pragma then
3328 -- Copy pragma SPARK_Mode if present in the declarative list
3329 -- of subprogram body From and insert it after node To. This
3330 -- pragma should not be moved, as it applies to the body too.
3332 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
3333 Insert_After (Insert_Nod, New_Copy_Tree (Decl));
3335 -- Move relevant pragmas to the spec
3337 elsif
3338 Pragma_Significant_To_Subprograms
3339 (Get_Pragma_Id (Decl))
3340 then
3341 Remove (Decl);
3342 Insert_After (Insert_Nod, Decl);
3343 Insert_Nod := Decl;
3344 end if;
3346 -- Skip internally generated code
3348 elsif not Comes_From_Source (Decl) then
3349 null;
3351 -- No candidate pragmas are available for relocation
3353 else
3354 exit;
3355 end if;
3357 Decl := Next_Decl;
3358 end loop;
3359 end Move_Pragmas;
3361 ---------------------------
3362 -- Restore_Limited_Views --
3363 ---------------------------
3365 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3366 Elmt : Elmt_Id := First_Elmt (Restore_List);
3367 Id : Entity_Id;
3369 begin
3370 while Present (Elmt) loop
3371 Id := Node (Elmt);
3372 Next_Elmt (Elmt);
3373 Set_Etype (Id, Node (Elmt));
3374 Next_Elmt (Elmt);
3375 end loop;
3376 end Restore_Limited_Views;
3378 ----------------------------
3379 -- Set_Trivial_Subprogram --
3380 ----------------------------
3382 procedure Set_Trivial_Subprogram (N : Node_Id) is
3383 Nxt : constant Node_Id := Next (N);
3385 begin
3386 Set_Is_Trivial_Subprogram (Body_Id);
3388 if Present (Spec_Id) then
3389 Set_Is_Trivial_Subprogram (Spec_Id);
3390 end if;
3392 if Present (Nxt)
3393 and then Nkind (Nxt) = N_Simple_Return_Statement
3394 and then No (Next (Nxt))
3395 and then Present (Expression (Nxt))
3396 and then Is_Entity_Name (Expression (Nxt))
3397 then
3398 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3399 end if;
3400 end Set_Trivial_Subprogram;
3402 ---------------------------
3403 -- Unmask_Unfrozen_Types --
3404 ---------------------------
3406 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3407 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3409 begin
3410 while Present (Elmt) loop
3411 Set_Is_Frozen (Node (Elmt), False);
3412 Next_Elmt (Elmt);
3413 end loop;
3414 end Unmask_Unfrozen_Types;
3416 ---------------------------------
3417 -- Verify_Overriding_Indicator --
3418 ---------------------------------
3420 procedure Verify_Overriding_Indicator is
3421 begin
3422 if Must_Override (Body_Spec) then
3423 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3424 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3425 then
3426 null;
3428 -- Overridden controlled primitives may have had their
3429 -- Overridden_Operation field cleared according to the setting of
3430 -- the Is_Hidden flag. An issue arises, however, when analyzing
3431 -- an instance that may have manipulated the flag during
3432 -- expansion. As a result, we add an exception for this case.
3434 elsif No (Overridden_Operation (Spec_Id))
3435 and then not (Chars (Spec_Id) in Name_Adjust
3436 | Name_Finalize
3437 | Name_Initialize
3438 and then In_Instance)
3439 then
3440 Error_Msg_NE
3441 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3443 -- Overriding indicators aren't allowed for protected subprogram
3444 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3445 -- this to a warning if -gnatd.E is enabled.
3447 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3448 Error_Msg_Warn := Error_To_Warning;
3449 Error_Msg_N
3450 ("<<overriding indicator not allowed for protected "
3451 & "subprogram body", Body_Spec);
3452 end if;
3454 elsif Must_Not_Override (Body_Spec) then
3455 if Present (Overridden_Operation (Spec_Id)) then
3456 Error_Msg_NE
3457 ("subprogram& overrides inherited operation",
3458 Body_Spec, Spec_Id);
3460 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3461 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3462 then
3463 Error_Msg_NE
3464 ("subprogram& overrides predefined operator",
3465 Body_Spec, Spec_Id);
3467 -- Overriding indicators aren't allowed for protected subprogram
3468 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3469 -- this to a warning if -gnatd.E is enabled.
3471 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3472 Error_Msg_Warn := Error_To_Warning;
3474 Error_Msg_N
3475 ("<<overriding indicator not allowed "
3476 & "for protected subprogram body", Body_Spec);
3478 -- If this is not a primitive operation, then the overriding
3479 -- indicator is altogether illegal.
3481 elsif not Is_Primitive (Spec_Id) then
3482 Error_Msg_N
3483 ("overriding indicator only allowed "
3484 & "if subprogram is primitive", Body_Spec);
3485 end if;
3487 -- If checking the style rule and the operation overrides, then
3488 -- issue a warning about a missing overriding_indicator. Protected
3489 -- subprogram bodies are excluded from this style checking, since
3490 -- they aren't primitives (even though their declarations can
3491 -- override) and aren't allowed to have an overriding_indicator.
3493 elsif Style_Check
3494 and then Present (Overridden_Operation (Spec_Id))
3495 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3496 then
3497 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3498 Style.Missing_Overriding (N, Body_Id);
3500 elsif Style_Check
3501 and then Can_Override_Operator (Spec_Id)
3502 and then not In_Predefined_Unit (Spec_Id)
3503 then
3504 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3505 Style.Missing_Overriding (N, Body_Id);
3506 end if;
3507 end Verify_Overriding_Indicator;
3509 -- Local variables
3511 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3512 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3513 Saved_EA : constant Boolean := Expander_Active;
3514 Saved_ISMP : constant Boolean :=
3515 Ignore_SPARK_Mode_Pragmas_In_Instance;
3516 -- Save the Ghost and SPARK mode-related data to restore on exit
3518 -- Start of processing for Analyze_Subprogram_Body_Helper
3520 begin
3521 -- A [generic] subprogram body freezes the contract of the nearest
3522 -- enclosing package body and all other contracts encountered in the
3523 -- same declarative part up to and excluding the subprogram body:
3525 -- package body Nearest_Enclosing_Package
3526 -- with Refined_State => (State => Constit)
3527 -- is
3528 -- Constit : ...;
3530 -- procedure Freezes_Enclosing_Package_Body
3531 -- with Refined_Depends => (Input => Constit) ...
3533 -- This ensures that any annotations referenced by the contract of the
3534 -- [generic] subprogram body are available. This form of freezing is
3535 -- decoupled from the usual Freeze_xxx mechanism because it must also
3536 -- work in the context of generics where normal freezing is disabled.
3538 -- Only bodies coming from source should cause this type of freezing.
3539 -- Expression functions that act as bodies and complete an initial
3540 -- declaration must be included in this category, hence the use of
3541 -- Original_Node.
3543 if Comes_From_Source (Original_Node (N)) then
3544 Freeze_Previous_Contracts (N);
3545 end if;
3547 -- Generic subprograms are handled separately. They always have a
3548 -- generic specification. Determine whether current scope has a
3549 -- previous declaration.
3551 -- If the subprogram body is defined within an instance of the same
3552 -- name, the instance appears as a package renaming, and will be hidden
3553 -- within the subprogram.
3555 if Present (Prev_Id)
3556 and then not Is_Overloadable (Prev_Id)
3557 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3558 or else Comes_From_Source (Prev_Id))
3559 then
3560 if Is_Generic_Subprogram (Prev_Id) then
3561 Spec_Id := Prev_Id;
3563 -- A subprogram body is Ghost when it is stand-alone and subject
3564 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3565 -- the mode now to ensure that any nodes generated during analysis
3566 -- and expansion are properly marked as Ghost.
3568 Mark_And_Set_Ghost_Body (N, Spec_Id);
3570 -- If the body completes the initial declaration of a compilation
3571 -- unit which is subject to pragma Elaboration_Checks, set the
3572 -- model specified by the pragma because it applies to all parts
3573 -- of the unit.
3575 Install_Elaboration_Model (Spec_Id);
3577 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3578 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3580 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3582 if Nkind (N) = N_Subprogram_Body then
3583 Check_Missing_Return;
3584 end if;
3586 goto Leave;
3588 -- Otherwise a previous entity conflicts with the subprogram name.
3589 -- Attempting to enter name will post error.
3591 else
3592 Enter_Name (Body_Id);
3593 goto Leave;
3594 end if;
3596 -- Non-generic case, find the subprogram declaration, if one was seen,
3597 -- or enter new overloaded entity in the current scope. If the
3598 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3599 -- part of the context of one of its subunits. No need to redo the
3600 -- analysis.
3602 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3603 goto Leave;
3605 else
3606 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3608 if Nkind (N) = N_Subprogram_Body_Stub
3609 or else No (Corresponding_Spec (N))
3610 then
3611 if Is_Private_Concurrent_Primitive (Body_Id) then
3612 Spec_Id := Disambiguate_Spec;
3614 -- A subprogram body is Ghost when it is stand-alone and
3615 -- subject to pragma Ghost or when the corresponding spec is
3616 -- Ghost. Set the mode now to ensure that any nodes generated
3617 -- during analysis and expansion are properly marked as Ghost.
3619 Mark_And_Set_Ghost_Body (N, Spec_Id);
3621 -- If the body completes a compilation unit which is subject
3622 -- to pragma Elaboration_Checks, set the model specified by
3623 -- the pragma because it applies to all parts of the unit.
3625 Install_Elaboration_Model (Spec_Id);
3627 else
3628 Spec_Id := Find_Corresponding_Spec (N);
3630 -- A subprogram body is Ghost when it is stand-alone and
3631 -- subject to pragma Ghost or when the corresponding spec is
3632 -- Ghost. Set the mode now to ensure that any nodes generated
3633 -- during analysis and expansion are properly marked as Ghost.
3635 Mark_And_Set_Ghost_Body (N, Spec_Id);
3637 -- If the body completes a compilation unit which is subject
3638 -- to pragma Elaboration_Checks, set the model specified by
3639 -- the pragma because it applies to all parts of the unit.
3641 Install_Elaboration_Model (Spec_Id);
3643 -- In GNATprove mode, if the body has no previous spec, create
3644 -- one so that the inlining machinery can operate properly.
3645 -- Transfer aspects, if any, to the new spec, so that they
3646 -- are legal and can be processed ahead of the body.
3647 -- We make two copies of the given spec, one for the new
3648 -- declaration, and one for the body.
3649 -- ??? This should be conditioned on front-end inlining rather
3650 -- than GNATprove_Mode.
3652 if No (Spec_Id) and then GNATprove_Mode
3654 -- Inlining does not apply during preanalysis of code
3656 and then Full_Analysis
3658 -- Inlining only applies to full bodies, not stubs
3660 and then Nkind (N) /= N_Subprogram_Body_Stub
3662 -- Inlining only applies to bodies in the source code, not to
3663 -- those generated by the compiler. In particular, expression
3664 -- functions, whose body is generated by the compiler, are
3665 -- treated specially by GNATprove.
3667 and then Comes_From_Source (Body_Id)
3669 -- This cannot be done for a compilation unit, which is not
3670 -- in a context where we can insert a new spec.
3672 and then Is_List_Member (N)
3674 -- Inlining only applies to subprograms without contracts,
3675 -- as a contract is a sign that GNATprove should perform a
3676 -- modular analysis of the subprogram instead of a contextual
3677 -- analysis at each call site. The same test is performed in
3678 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3679 -- here in another form (because the contract has not been
3680 -- attached to the body) to avoid front-end errors in case
3681 -- pragmas are used instead of aspects, because the
3682 -- corresponding pragmas in the body would not be transferred
3683 -- to the spec, leading to legality errors.
3685 and then not Body_Has_Contract
3686 and then not Inside_A_Generic
3687 then
3688 Build_Subprogram_Declaration;
3690 -- If this is a function that returns a constrained array, and
3691 -- Transform_Function_Array is set, create subprogram
3692 -- declaration to simplify e.g. subsequent C generation.
3694 elsif No (Spec_Id)
3695 and then Transform_Function_Array
3696 and then Nkind (Body_Spec) = N_Function_Specification
3697 and then Is_Array_Type (Etype (Body_Id))
3698 and then Is_Constrained (Etype (Body_Id))
3699 then
3700 Build_Subprogram_Declaration;
3701 end if;
3702 end if;
3704 -- If this is a duplicate body, no point in analyzing it
3706 if Error_Posted (N) then
3707 goto Leave;
3708 end if;
3710 -- A subprogram body should cause freezing of its own declaration,
3711 -- so, if the body and spec are compilation units, we must do it
3712 -- manually here. Moreover, if the return type is anonymous access
3713 -- to protected subprogram, it must be frozen before the body
3714 -- because its expansion has generated an equivalent type that is
3715 -- used when elaborating the body.
3717 if Present (Spec_Id)
3718 and then Nkind (Parent (N)) = N_Compilation_Unit
3719 then
3720 Freeze_Before (N, Spec_Id);
3722 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3723 Freeze_Before (N, Etype (Body_Id));
3724 end if;
3726 else
3727 Spec_Id := Corresponding_Spec (N);
3729 -- A subprogram body is Ghost when it is stand-alone and subject
3730 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3731 -- the mode now to ensure that any nodes generated during analysis
3732 -- and expansion are properly marked as Ghost.
3734 Mark_And_Set_Ghost_Body (N, Spec_Id);
3736 -- If the body completes the initial declaration of a compilation
3737 -- unit which is subject to pragma Elaboration_Checks, set the
3738 -- model specified by the pragma because it applies to all parts
3739 -- of the unit.
3741 Install_Elaboration_Model (Spec_Id);
3742 end if;
3743 end if;
3745 -- Deactivate expansion inside the body of ignored Ghost entities,
3746 -- as this code will ultimately be ignored. This avoids requiring the
3747 -- presence of run-time units which are not needed. Only do this for
3748 -- user entities, as internally generated entitities might still need
3749 -- to be expanded (e.g. those generated for types).
3751 if Present (Ignored_Ghost_Region)
3752 and then Comes_From_Source (Body_Id)
3753 then
3754 Expander_Active := False;
3755 end if;
3757 -- Previously we scanned the body to look for nested subprograms, and
3758 -- rejected an inline directive if nested subprograms were present,
3759 -- because the back-end would generate conflicting symbols for the
3760 -- nested bodies. This is now unnecessary.
3762 -- Look ahead to recognize a pragma Inline that appears after the body
3764 Check_Inline_Pragma (Spec_Id);
3766 -- Deal with special case of a fully private operation in the body of
3767 -- the protected type. We must create a declaration for the subprogram,
3768 -- in order to attach the subprogram that will be used in internal
3769 -- calls. We exclude compiler generated bodies from the expander since
3770 -- the issue does not arise for those cases.
3772 if No (Spec_Id)
3773 and then Comes_From_Source (N)
3774 and then Is_Protected_Type (Current_Scope)
3775 then
3776 Spec_Id := Build_Internal_Protected_Declaration (N);
3777 end if;
3779 -- If Transform_Function_Array is set and this is a function returning a
3780 -- constrained array type for which we must create a procedure with an
3781 -- extra out parameter, build and analyze the body now. The procedure
3782 -- declaration has already been created. We reuse the source body of the
3783 -- function, because in an instance it may contain global references
3784 -- that cannot be reanalyzed. The source function itself is not used any
3785 -- further, so we mark it as having a completion. If the subprogram is a
3786 -- stub the transformation is done later, when the proper body is
3787 -- analyzed.
3789 if Expander_Active
3790 and then Transform_Function_Array
3791 and then Nkind (N) /= N_Subprogram_Body_Stub
3792 then
3793 declare
3794 S : constant Entity_Id :=
3795 (if Present (Spec_Id)
3796 then Spec_Id
3797 else Defining_Unit_Name (Specification (N)));
3798 Proc_Body : Node_Id;
3800 begin
3801 if Ekind (S) = E_Function and then Rewritten_For_C (S) then
3802 Set_Has_Completion (S);
3803 Proc_Body := Build_Procedure_Body_Form (S, N);
3805 if Present (Spec_Id) then
3806 Rewrite (N, Proc_Body);
3807 Analyze (N);
3809 -- The entity for the created procedure must remain
3810 -- invisible, so it does not participate in resolution of
3811 -- subsequent references to the function.
3813 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3815 -- If we do not have a separate spec for N, build one and
3816 -- insert the new body right after.
3818 else
3819 Rewrite (N,
3820 Make_Subprogram_Declaration (Loc,
3821 Specification => Relocate_Node (Specification (N))));
3822 Analyze (N);
3823 Insert_After_And_Analyze (N, Proc_Body);
3824 Set_Is_Immediately_Visible
3825 (Corresponding_Spec (Proc_Body), False);
3826 end if;
3828 goto Leave;
3829 end if;
3830 end;
3831 end if;
3833 -- If a separate spec is present, then deal with freezing issues
3835 if Present (Spec_Id) then
3836 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3837 Verify_Overriding_Indicator;
3839 -- For functions with separate spec, if their return type was visible
3840 -- through a limited-with context clause, their extra formals were
3841 -- not added when the spec was frozen. Now the full view must be
3842 -- available, and the extra formals can be created and Returns_By_Ref
3843 -- computed (required to generate its return statements).
3845 if Ekind (Spec_Id) = E_Function
3846 and then From_Limited_With (Etype (Spec_Id))
3847 and then Is_Build_In_Place_Function (Spec_Id)
3848 and then not Has_BIP_Formals (Spec_Id)
3849 then
3850 Create_Extra_Formals (Spec_Id);
3851 Compute_Returns_By_Ref (Spec_Id);
3852 end if;
3854 -- In general, the spec will be frozen when we start analyzing the
3855 -- body. However, for internally generated operations, such as
3856 -- wrapper functions for inherited operations with controlling
3857 -- results, the spec may not have been frozen by the time we expand
3858 -- the freeze actions that include the bodies. In particular, extra
3859 -- formals for accessibility or for return-in-place may need to be
3860 -- generated. Freeze nodes, if any, are inserted before the current
3861 -- body. These freeze actions are also needed in Compile_Only mode to
3862 -- enable the proper back-end type annotations.
3863 -- They are necessary in any case to ensure proper elaboration order
3864 -- in gigi.
3866 if Nkind (N) = N_Subprogram_Body
3867 and then Was_Expression_Function (N)
3868 and then not Has_Completion (Spec_Id)
3869 and then Serious_Errors_Detected = 0
3870 and then (Expander_Active
3871 or else Operating_Mode = Check_Semantics
3872 or else Is_Ignored_Ghost_Entity (Spec_Id))
3873 then
3874 -- The body generated for an expression function that is not a
3875 -- completion is a freeze point neither for the profile nor for
3876 -- anything else. That's why, in order to prevent any freezing
3877 -- during analysis, we need to mask types declared outside the
3878 -- expression (and in an outer scope) that are not yet frozen.
3879 -- This also needs to be done in the case of an ignored Ghost
3880 -- expression function, where the expander isn't active.
3882 -- A further complication arises if the expression function is
3883 -- a primitive operation of a tagged type: in that case the
3884 -- function entity must be frozen before the dispatch table for
3885 -- the type is constructed, so it will be frozen like other local
3886 -- entities, at the end of the current scope.
3888 if not Is_Dispatching_Operation (Spec_Id) then
3889 Set_Is_Frozen (Spec_Id);
3890 end if;
3892 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
3894 elsif not Is_Frozen (Spec_Id)
3895 and then Serious_Errors_Detected = 0
3896 then
3897 Set_Has_Delayed_Freeze (Spec_Id);
3898 Freeze_Before (N, Spec_Id);
3899 end if;
3900 end if;
3902 -- Place subprogram on scope stack, and make formals visible. If there
3903 -- is a spec, the visible entity remains that of the spec.
3905 if Present (Spec_Id) then
3906 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
3908 if Is_Child_Unit (Spec_Id) then
3909 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
3910 end if;
3912 if Style_Check then
3913 Style.Check_Identifier (Body_Id, Spec_Id);
3914 end if;
3916 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3917 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3919 if Is_Abstract_Subprogram (Spec_Id) then
3920 Error_Msg_N ("an abstract subprogram cannot have a body", N);
3921 goto Leave;
3923 else
3924 Set_Convention (Body_Id, Convention (Spec_Id));
3925 Set_Has_Completion (Spec_Id);
3927 if Is_Protected_Type (Scope (Spec_Id)) then
3928 Prot_Typ := Scope (Spec_Id);
3929 end if;
3931 -- If this is a body generated for a renaming, do not check for
3932 -- full conformance. The check is redundant, because the spec of
3933 -- the body is a copy of the spec in the renaming declaration,
3934 -- and the test can lead to spurious errors on nested defaults.
3936 if Present (Spec_Decl)
3937 and then not Comes_From_Source (N)
3938 and then
3939 (Nkind (Original_Node (Spec_Decl)) =
3940 N_Subprogram_Renaming_Declaration
3941 or else (Present (Corresponding_Body (Spec_Decl))
3942 and then
3943 Nkind (Unit_Declaration_Node
3944 (Corresponding_Body (Spec_Decl))) =
3945 N_Subprogram_Renaming_Declaration))
3946 then
3947 Conformant := True;
3949 -- Conversely, the spec may have been generated for specless body
3950 -- with an inline pragma. The entity comes from source, which is
3951 -- both semantically correct and necessary for proper inlining.
3952 -- The subprogram declaration itself is not in the source.
3954 elsif Comes_From_Source (N)
3955 and then Present (Spec_Decl)
3956 and then not Comes_From_Source (Spec_Decl)
3957 and then Has_Pragma_Inline (Spec_Id)
3958 then
3959 Conformant := True;
3961 -- Finally, a body generated for an expression function copies
3962 -- the profile of the function and no check is needed either.
3963 -- If the body is the completion of a previous function
3964 -- declared elsewhere, the conformance check is required.
3966 elsif Nkind (N) = N_Subprogram_Body
3967 and then Was_Expression_Function (N)
3968 and then Sloc (Spec_Id) = Sloc (Body_Id)
3969 then
3970 Conformant := True;
3972 else
3973 Check_Conformance
3974 (Body_Id, Spec_Id,
3975 Fully_Conformant, True, Conformant, Body_Id);
3976 end if;
3978 -- If the body is not fully conformant, we have to decide if we
3979 -- should analyze it or not. If it has a really messed up profile
3980 -- then we probably should not analyze it, since we will get too
3981 -- many bogus messages.
3983 -- Our decision is to go ahead in the non-fully conformant case
3984 -- only if it is at least mode conformant with the spec. Note
3985 -- that the call to Check_Fully_Conformant has issued the proper
3986 -- error messages to complain about the lack of conformance.
3988 if not Conformant
3989 and then not Mode_Conformant (Body_Id, Spec_Id)
3990 then
3991 goto Leave;
3992 end if;
3993 end if;
3995 -- In the case we are dealing with an expression function we check
3996 -- the formals attached to the spec instead of the body - so we don't
3997 -- reference body formals.
3999 if Spec_Id /= Body_Id
4000 and then not Is_Expression_Function (Spec_Id)
4001 then
4002 Reference_Body_Formals (Spec_Id, Body_Id);
4003 end if;
4005 Reinit_Field_To_Zero (Body_Id, F_Has_Out_Or_In_Out_Parameter);
4006 Reinit_Field_To_Zero (Body_Id, F_Needs_No_Actuals,
4007 Old_Ekind => (E_Function | E_Procedure => True, others => False));
4008 Reinit_Field_To_Zero (Body_Id, F_Is_Predicate_Function,
4009 Old_Ekind => (E_Function | E_Procedure => True, others => False));
4010 Reinit_Field_To_Zero (Body_Id, F_Protected_Subprogram,
4011 Old_Ekind => (E_Function | E_Procedure => True, others => False));
4013 if Ekind (Body_Id) = E_Procedure then
4014 Reinit_Field_To_Zero (Body_Id, F_Receiving_Entry);
4015 end if;
4017 Mutate_Ekind (Body_Id, E_Subprogram_Body);
4019 if Nkind (N) = N_Subprogram_Body_Stub then
4020 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4022 -- Regular body
4024 else
4025 Set_Corresponding_Spec (N, Spec_Id);
4027 -- Ada 2005 (AI-345): If the operation is a primitive operation
4028 -- of a concurrent type, the type of the first parameter has been
4029 -- replaced with the corresponding record, which is the proper
4030 -- run-time structure to use. However, within the body there may
4031 -- be uses of the formals that depend on primitive operations
4032 -- of the type (in particular calls in prefixed form) for which
4033 -- we need the original concurrent type. The operation may have
4034 -- several controlling formals, so the replacement must be done
4035 -- for all of them.
4037 if Comes_From_Source (Spec_Id)
4038 and then Present (First_Entity (Spec_Id))
4039 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4040 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4041 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4042 and then Present (Corresponding_Concurrent_Type
4043 (Etype (First_Entity (Spec_Id))))
4044 then
4045 declare
4046 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4047 Form : Entity_Id;
4049 begin
4050 Form := First_Formal (Spec_Id);
4051 while Present (Form) loop
4052 if Etype (Form) = Typ then
4053 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4054 end if;
4056 Next_Formal (Form);
4057 end loop;
4058 end;
4059 end if;
4061 -- Make the formals visible, and place subprogram on scope stack.
4062 -- This is also the point at which we set Last_Real_Spec_Entity
4063 -- to mark the entities which will not be moved to the body.
4065 Install_Formals (Spec_Id);
4066 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4068 -- Within an instance, add local renaming declarations so that
4069 -- gdb can retrieve the values of actuals more easily. This is
4070 -- only relevant if generating code.
4072 if Is_Generic_Instance (Spec_Id)
4073 and then Is_Wrapper_Package (Current_Scope)
4074 and then Expander_Active
4075 then
4076 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4077 end if;
4079 Push_Scope (Spec_Id);
4081 -- Make sure that the subprogram is immediately visible. For
4082 -- child units that have no separate spec this is indispensable.
4083 -- Otherwise it is safe albeit redundant.
4085 Set_Is_Immediately_Visible (Spec_Id);
4086 end if;
4088 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4089 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4090 Set_Scope (Body_Id, Scope (Spec_Id));
4092 -- Case of subprogram body with no previous spec
4094 else
4095 -- Check for style warning required
4097 if Style_Check
4099 -- Only apply check for source level subprograms for which checks
4100 -- have not been suppressed.
4102 and then Comes_From_Source (Body_Id)
4103 and then not Suppress_Style_Checks (Body_Id)
4105 -- No warnings within an instance
4107 and then not In_Instance
4109 -- No warnings for expression functions
4111 and then (Nkind (N) /= N_Subprogram_Body
4112 or else not Was_Expression_Function (N))
4113 then
4114 Style.Body_With_No_Spec (N);
4115 end if;
4117 -- First set Acts_As_Spec if appropriate
4119 if Nkind (N) /= N_Subprogram_Body_Stub then
4120 Set_Acts_As_Spec (N);
4121 end if;
4123 New_Overloaded_Entity (Body_Id);
4125 -- A subprogram body should cause freezing of its own declaration,
4126 -- but if there was no previous explicit declaration, then the
4127 -- subprogram will get frozen too late (there may be code within
4128 -- the body that depends on the subprogram having been frozen,
4129 -- such as uses of extra formals), so we force it to be frozen here.
4130 -- An exception in Ada 2012 is that the body created for expression
4131 -- functions does not freeze.
4133 if Nkind (N) /= N_Subprogram_Body
4134 or else not Was_Expression_Function (N)
4135 then
4136 -- First clear the Is_Public flag on thunks since they are only
4137 -- referenced locally by dispatch tables and thus never inlined.
4139 if Is_Thunk (Body_Id) then
4140 Set_Is_Public (Body_Id, False);
4141 end if;
4143 Freeze_Before (N, Body_Id);
4144 end if;
4146 if Nkind (N) /= N_Subprogram_Body_Stub then
4147 Generate_Definition (Body_Id);
4148 Generate_Reference
4149 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4151 -- If the body is an entry wrapper created for an entry with
4152 -- preconditions, it must be compiled in the context of the
4153 -- enclosing synchronized object, because it may mention other
4154 -- operations of the type.
4156 if Is_Entry_Wrapper (Body_Id) then
4157 declare
4158 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4159 begin
4160 Push_Scope (Prot);
4161 Install_Declarations (Prot);
4162 end;
4163 end if;
4165 Install_Formals (Body_Id);
4167 Push_Scope (Body_Id);
4168 end if;
4170 -- For stubs and bodies with no previous spec, generate references to
4171 -- formals.
4173 Generate_Reference_To_Formals (Body_Id);
4174 end if;
4176 -- Entry barrier functions are generated outside the protected type and
4177 -- should not carry the SPARK_Mode of the enclosing context.
4179 if Nkind (N) = N_Subprogram_Body
4180 and then Is_Entry_Barrier_Function (N)
4181 then
4182 null;
4184 -- The body is generated as part of expression function expansion. When
4185 -- the expression function appears in the visible declarations of a
4186 -- package, the body is added to the private declarations. Since both
4187 -- declarative lists may be subject to a different SPARK_Mode, inherit
4188 -- the mode of the spec.
4190 -- package P with SPARK_Mode is
4191 -- function Expr_Func ... is (...); -- original
4192 -- [function Expr_Func ...;] -- generated spec
4193 -- -- mode is ON
4194 -- private
4195 -- pragma SPARK_Mode (Off);
4196 -- [function Expr_Func ... is return ...;] -- generated body
4197 -- end P; -- mode is ON
4199 elsif not Comes_From_Source (N)
4200 and then Present (Spec_Id)
4201 and then Is_Expression_Function (Spec_Id)
4202 then
4203 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4204 Set_SPARK_Pragma_Inherited
4205 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4207 -- Set the SPARK_Mode from the current context (may be overwritten later
4208 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4209 -- initially on a stand-alone subprogram body, but is then relocated to
4210 -- a generated corresponding spec. In this scenario the mode is shared
4211 -- between the spec and body.
4213 elsif No (SPARK_Pragma (Body_Id)) then
4214 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4215 Set_SPARK_Pragma_Inherited (Body_Id);
4216 end if;
4218 -- A subprogram body may be instantiated or inlined at a later pass.
4219 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4220 -- applied to the initial declaration of the body.
4222 if Present (Spec_Id) then
4223 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4224 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4225 end if;
4227 else
4228 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4229 -- case the body is instantiated or inlined later and out of context.
4230 -- The body uses this attribute to restore the value of the global
4231 -- flag.
4233 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4234 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4236 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4237 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4238 end if;
4239 end if;
4241 -- Preserve relevant elaboration-related attributes of the context which
4242 -- are no longer available or very expensive to recompute once analysis,
4243 -- resolution, and expansion are over.
4245 if No (Spec_Id) then
4246 Mark_Elaboration_Attributes
4247 (N_Id => Body_Id,
4248 Checks => True,
4249 Warnings => True);
4250 end if;
4252 -- If this is the proper body of a stub, we must verify that the stub
4253 -- conforms to the body, and to the previous spec if one was present.
4254 -- We know already that the body conforms to that spec. This test is
4255 -- only required for subprograms that come from source.
4257 if Nkind (Parent (N)) = N_Subunit
4258 and then Comes_From_Source (N)
4259 and then not Error_Posted (Body_Id)
4260 and then Nkind (Corresponding_Stub (Parent (N))) =
4261 N_Subprogram_Body_Stub
4262 then
4263 declare
4264 Old_Id : constant Entity_Id :=
4265 Defining_Entity
4266 (Specification (Corresponding_Stub (Parent (N))));
4268 Conformant : Boolean := False;
4270 begin
4271 if No (Spec_Id) then
4272 Check_Fully_Conformant (Body_Id, Old_Id);
4274 else
4275 Check_Conformance
4276 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4278 if not Conformant then
4280 -- The stub was taken to be a new declaration. Indicate that
4281 -- it lacks a body.
4283 Set_Has_Completion (Old_Id, False);
4284 end if;
4285 end if;
4286 end;
4287 end if;
4289 Set_Has_Completion (Body_Id);
4290 Check_Eliminated (Body_Id);
4292 -- Analyze any aspect specifications that appear on the subprogram body
4293 -- stub. Stop the analysis now as the stub does not have a declarative
4294 -- or a statement part, and it cannot be inlined.
4296 if Nkind (N) = N_Subprogram_Body_Stub then
4297 if Has_Aspects (N) then
4298 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4299 end if;
4301 goto Leave;
4302 end if;
4304 -- Handle inlining
4306 if Expander_Active
4307 and then Serious_Errors_Detected = 0
4308 and then Present (Spec_Id)
4309 and then Has_Pragma_Inline (Spec_Id)
4310 then
4311 -- Legacy implementation (relying on front-end inlining)
4313 if not Back_End_Inlining then
4314 if Has_Pragma_Inline_Always (Spec_Id)
4315 or else (Front_End_Inlining
4316 and then not Opt.Disable_FE_Inline)
4317 then
4318 Build_Body_To_Inline (N, Spec_Id);
4319 end if;
4321 -- New implementation (relying on back-end inlining)
4323 else
4324 if Has_Pragma_Inline_Always (Spec_Id)
4325 or else Optimization_Level > 0
4326 then
4327 -- Handle function returning an unconstrained type
4329 if Comes_From_Source (Body_Id)
4330 and then Ekind (Spec_Id) = E_Function
4331 and then Returns_Unconstrained_Type (Spec_Id)
4333 -- If function builds in place, i.e. returns a limited type,
4334 -- inlining cannot be done.
4336 and then not Is_Limited_Type (Etype (Spec_Id))
4337 then
4338 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4340 else
4341 declare
4342 Subp_Body : constant Node_Id :=
4343 Unit_Declaration_Node (Body_Id);
4344 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4346 begin
4347 -- Do not pass inlining to the backend if the subprogram
4348 -- has declarations or statements which cannot be inlined
4349 -- by the backend. This check is done here to emit an
4350 -- error instead of the generic warning message reported
4351 -- by the GCC backend (ie. "function might not be
4352 -- inlinable").
4354 if Has_Excluded_Declaration (Spec_Id, Subp_Decl) then
4355 null;
4357 elsif Has_Excluded_Statement
4358 (Spec_Id,
4359 Statements
4360 (Handled_Statement_Sequence (Subp_Body)))
4361 then
4362 null;
4364 -- If the backend inlining is available then at this
4365 -- stage we only have to mark the subprogram as inlined.
4366 -- The expander will take care of registering it in the
4367 -- table of subprograms inlined by the backend a part of
4368 -- processing calls to it (cf. Expand_Call)
4370 else
4371 Set_Is_Inlined (Spec_Id);
4372 end if;
4373 end;
4374 end if;
4375 end if;
4376 end if;
4378 -- In GNATprove mode, inline only when there is a separate subprogram
4379 -- declaration for now, as inlining of subprogram bodies acting as
4380 -- declarations, or subprogram stubs, are not supported by front-end
4381 -- inlining. This inlining should occur after analysis of the body, so
4382 -- that it is known whether the value of SPARK_Mode, which can be
4383 -- defined by a pragma inside the body, is applicable to the body.
4384 -- Inlining can be disabled with switch -gnatdm
4386 elsif GNATprove_Mode
4387 and then Full_Analysis
4388 and then not Inside_A_Generic
4389 and then Present (Spec_Id)
4390 and then
4391 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4392 and then Body_Has_SPARK_Mode_On
4393 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4394 and then not Body_Has_Contract
4395 and then not Debug_Flag_M
4396 then
4397 Build_Body_To_Inline (N, Spec_Id);
4398 end if;
4400 -- When generating code, inherited pre/postconditions are handled when
4401 -- expanding the corresponding contract.
4403 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4404 -- of the specification we have to install the private withed units.
4405 -- This holds for child units as well.
4407 if Is_Compilation_Unit (Body_Id)
4408 or else Nkind (Parent (N)) = N_Compilation_Unit
4409 then
4410 Install_Private_With_Clauses (Body_Id);
4411 end if;
4413 Check_Anonymous_Return;
4415 -- Set the Protected_Formal field of each extra formal of the protected
4416 -- subprogram to reference the corresponding extra formal of the
4417 -- subprogram that implements it. For regular formals this occurs when
4418 -- the protected subprogram's declaration is expanded, but the extra
4419 -- formals don't get created until the subprogram is frozen. We need to
4420 -- do this before analyzing the protected subprogram's body so that any
4421 -- references to the original subprogram's extra formals will be changed
4422 -- refer to the implementing subprogram's formals (see Expand_Formal).
4424 if Present (Spec_Id)
4425 and then Is_Protected_Type (Scope (Spec_Id))
4426 and then Present (Protected_Body_Subprogram (Spec_Id))
4427 then
4428 declare
4429 Impl_Subp : constant Entity_Id :=
4430 Protected_Body_Subprogram (Spec_Id);
4431 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4432 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4434 begin
4435 while Present (Prot_Ext_Formal) loop
4436 pragma Assert (Present (Impl_Ext_Formal));
4437 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4438 Next_Formal_With_Extras (Prot_Ext_Formal);
4439 Next_Formal_With_Extras (Impl_Ext_Formal);
4440 end loop;
4441 end;
4442 end if;
4444 -- Generate minimum accessibility local objects to correspond with
4445 -- any extra formal added for anonymous access types. This new local
4446 -- object can then be used instead of the formal in case it is used
4447 -- in an actual to a call to a nested subprogram.
4449 -- This method is used to supplement our "small integer model" for
4450 -- accessibility-check generation (for more information see
4451 -- Accessibility_Level).
4453 -- Because we allow accessibility values greater than our expected value
4454 -- passing along the same extra accessibility formal as an actual
4455 -- to a nested subprogram becomes a problem because high values mean
4456 -- different things to the callee even though they are the same to the
4457 -- caller. So, as described in the first section, we create a local
4458 -- object representing the minimum of the accessibility level value that
4459 -- is passed in and the accessibility level of the callee's parameter
4460 -- and locals and use it in the case of a call to a nested subprogram.
4461 -- This generated object is referred to as a "minimum accessibility
4462 -- level."
4464 if Present (Spec_Id) or else Present (Body_Id) then
4465 Body_Nod := Unit_Declaration_Node (Body_Id);
4467 declare
4468 Form : Entity_Id;
4469 begin
4470 -- Grab the appropriate formal depending on whether there exists
4471 -- an actual spec for the subprogram or whether we are dealing
4472 -- with a protected subprogram.
4474 if Present (Spec_Id) then
4475 if Present (Protected_Body_Subprogram (Spec_Id)) then
4476 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4477 else
4478 Form := First_Formal (Spec_Id);
4479 end if;
4480 else
4481 Form := First_Formal (Body_Id);
4482 end if;
4484 -- Loop through formals if the subprogram is capable of accepting
4485 -- a generated local object. If it is not then it is also not
4486 -- capable of having local subprograms meaning it would not need
4487 -- a minimum accessibility level object anyway.
4489 if Present (Body_Nod)
4490 and then Has_Declarations (Body_Nod)
4491 and then Nkind (Body_Nod) /= N_Package_Specification
4492 then
4493 while Present (Form) loop
4495 if Present (Extra_Accessibility (Form))
4496 and then No (Minimum_Accessibility (Form))
4497 then
4498 -- Generate the minimum accessibility level object
4500 -- A60b : constant natural := natural'min(1, paramL);
4502 Generate_Minimum_Accessibility
4503 (Extra_Accessibility (Form), Form);
4504 end if;
4506 Next_Formal (Form);
4507 end loop;
4509 -- Generate the minimum accessibility level object for the
4510 -- function's Extra_Accessibility_Of_Result.
4512 -- A31b : constant natural := natural'min (2, funcL);
4514 if Ekind (Body_Id) = E_Function
4515 and then Present (Extra_Accessibility_Of_Result (Body_Id))
4516 then
4517 Generate_Minimum_Accessibility
4518 (Extra_Accessibility_Of_Result (Body_Id));
4520 -- Replace the Extra_Accessibility_Of_Result with the new
4521 -- minimum accessibility object.
4523 Set_Extra_Accessibility_Of_Result
4524 (Body_Id, Minimum_Accessibility
4525 (Extra_Accessibility_Of_Result (Body_Id)));
4526 end if;
4527 end if;
4528 end;
4529 end if;
4531 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4532 -- may now appear in parameter and result profiles. Since the analysis
4533 -- of a subprogram body may use the parameter and result profile of the
4534 -- spec, swap any limited views with their non-limited counterpart.
4536 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4537 Exch_Views := Exchange_Limited_Views (Spec_Id);
4538 end if;
4540 -- Analyze any aspect specifications that appear on the subprogram body
4542 if Has_Aspects (N) then
4543 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4544 end if;
4546 -- Process the contract of the subprogram body after analyzing all the
4547 -- contract-related pragmas within the declarations.
4549 Analyze_Pragmas_In_Declarations (Body_Id);
4550 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4552 Set_Actual_Subtypes (N, Current_Scope);
4554 -- Add a declaration for the Protection object, renaming declarations
4555 -- for discriminals and privals and finally a declaration for the entry
4556 -- family index (if applicable). This form of early expansion is done
4557 -- when the Expander is active because Install_Private_Data_Declarations
4558 -- references entities which were created during regular expansion. The
4559 -- subprogram entity must come from source, and not be an internally
4560 -- generated subprogram.
4562 if Expander_Active
4563 and then Present (Prot_Typ)
4564 and then Present (Spec_Id)
4565 and then Comes_From_Source (Spec_Id)
4566 and then not Is_Eliminated (Spec_Id)
4567 then
4568 Install_Private_Data_Declarations
4569 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4570 end if;
4572 -- If the return type is an anonymous access type whose designated type
4573 -- is the limited view of a class-wide type and the non-limited view is
4574 -- available, update the return type accordingly.
4576 if Ada_Version >= Ada_2005
4577 and then Present (Spec_Id)
4578 and then Ekind (Etype (Spec_Id)) = E_Anonymous_Access_Type
4579 then
4580 declare
4581 Etyp : Entity_Id;
4583 begin
4584 Etyp := Directly_Designated_Type (Etype (Spec_Id));
4586 if Is_Class_Wide_Type (Etyp)
4587 and then From_Limited_With (Etyp)
4588 and then Has_Non_Limited_View (Etyp)
4589 then
4590 Desig_View := Etyp;
4591 Etyp := Non_Limited_View (Etyp);
4593 -- If the class-wide type has been created by the completion of
4594 -- an incomplete tagged type declaration, get the class-wide
4595 -- type of the incomplete tagged type to match Find_Type_Name.
4597 if Nkind (Parent (Etyp)) = N_Full_Type_Declaration
4598 and then Present (Incomplete_View (Parent (Etyp)))
4599 then
4600 Etyp := Class_Wide_Type (Incomplete_View (Parent (Etyp)));
4601 end if;
4603 Set_Directly_Designated_Type (Etype (Spec_Id), Etyp);
4604 end if;
4605 end;
4606 end if;
4608 Analyze_Declarations (Declarations (N));
4610 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4612 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4613 if Present (SPARK_Pragma (Spec_Id)) then
4614 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4615 and then
4616 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4617 then
4618 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4619 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4620 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4621 Error_Msg_NE
4622 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4623 end if;
4625 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4626 null;
4628 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4629 -- as specified in source code, or because SPARK_Mode On is ignored
4630 -- in an instance where the context is SPARK_Mode Off/Auto.
4632 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4633 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4634 then
4635 null;
4637 else
4638 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4639 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4640 Error_Msg_Sloc := Sloc (Spec_Id);
4641 Error_Msg_NE
4642 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4643 end if;
4644 end if;
4646 -- Check completion, and analyze the statements
4648 Check_Completion;
4649 Inspect_Deferred_Constant_Completion (Declarations (N));
4650 Analyze (Handled_Statement_Sequence (N));
4652 -- Add the generated minimum accessibility objects to the subprogram
4653 -- body's list of declarations after analysis of the statements and
4654 -- contracts.
4656 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4657 if Present (Declarations (Body_Nod)) then
4658 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4659 else
4660 Set_Declarations
4661 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4662 end if;
4663 end loop;
4665 -- Deal with end of scope processing for the body
4667 Process_End_Label
4668 (Handled_Statement_Sequence (N), 't', Current_Scope);
4669 Update_Use_Clause_Chain;
4670 End_Scope;
4672 -- If we are compiling an entry wrapper, remove the enclosing
4673 -- synchronized object from the stack.
4675 if Is_Entry_Wrapper (Body_Id) then
4676 End_Scope;
4677 end if;
4679 Check_Subprogram_Order (N);
4680 Set_Analyzed (Body_Id);
4682 -- If we have a separate spec, then the analysis of the declarations
4683 -- caused the entities in the body to be chained to the spec id, but
4684 -- we want them chained to the body id. Only the formal parameters
4685 -- end up chained to the spec id in this case.
4687 if Present (Spec_Id) then
4689 -- We must conform to the categorization of our spec
4691 Validate_Categorization_Dependency (N, Spec_Id);
4693 -- And if this is a child unit, the parent units must conform
4695 if Is_Child_Unit (Spec_Id) then
4696 Validate_Categorization_Dependency
4697 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4698 end if;
4700 -- Here is where we move entities from the spec to the body
4702 -- Case where there are entities that stay with the spec
4704 if Present (Last_Real_Spec_Entity) then
4706 -- No body entities (happens when the only real spec entities come
4707 -- from precondition and postcondition pragmas).
4709 if No (Last_Entity (Body_Id)) then
4710 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4712 -- Body entities present (formals), so chain stuff past them
4714 else
4715 Link_Entities
4716 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4717 end if;
4719 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4720 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4721 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4723 -- Case where there are no spec entities, in this case there can be
4724 -- no body entities either, so just move everything.
4726 -- If the body is generated for an expression function, it may have
4727 -- been preanalyzed already, if 'access was applied to it.
4729 else
4730 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4731 N_Expression_Function
4732 then
4733 pragma Assert (No (Last_Entity (Body_Id)));
4734 null;
4735 end if;
4737 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4738 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4739 Set_First_Entity (Spec_Id, Empty);
4740 Set_Last_Entity (Spec_Id, Empty);
4741 end if;
4743 -- Otherwise the body does not complete a previous declaration. Check
4744 -- the categorization of the body against the units it withs.
4746 else
4747 Validate_Categorization_Dependency (N, Body_Id);
4748 end if;
4750 Check_Missing_Return;
4752 -- Now we are going to check for variables that are never modified in
4753 -- the body of the procedure. But first we deal with a special case
4754 -- where we want to modify this check. If the body of the subprogram
4755 -- starts with a raise statement or its equivalent, or if the body
4756 -- consists entirely of a null statement, then it is pretty obvious that
4757 -- it is OK to not reference the parameters. For example, this might be
4758 -- the following common idiom for a stubbed function: statement of the
4759 -- procedure raises an exception. In particular this deals with the
4760 -- common idiom of a stubbed function, which appears something like:
4762 -- function F (A : Integer) return Some_Type;
4763 -- X : Some_Type;
4764 -- begin
4765 -- raise Program_Error;
4766 -- return X;
4767 -- end F;
4769 -- Here the purpose of X is simply to satisfy the annoying requirement
4770 -- in Ada that there be at least one return, and we certainly do not
4771 -- want to go posting warnings on X that it is not initialized. On
4772 -- the other hand, if X is entirely unreferenced that should still
4773 -- get a warning.
4775 -- What we do is to detect these cases, and if we find them, flag the
4776 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4777 -- suppress unwanted warnings. For the case of the function stub above
4778 -- we have a special test to set X as apparently assigned to suppress
4779 -- the warning.
4781 declare
4782 Stm : Node_Id := First (Statements (Handled_Statement_Sequence (N)));
4783 begin
4784 -- Skip call markers installed by the ABE mechanism, labels, and
4785 -- Push_xxx_Error_Label to find the first real statement.
4787 while Nkind (Stm) in N_Call_Marker | N_Label | N_Push_xxx_Label loop
4788 Next (Stm);
4789 end loop;
4791 -- Do the test on the original statement before expansion
4793 declare
4794 Ostm : constant Node_Id := Original_Node (Stm);
4796 begin
4797 -- If explicit raise statement, turn on flag
4799 if Nkind (Ostm) = N_Raise_Statement then
4800 Set_Trivial_Subprogram (Stm);
4802 -- If null statement, and no following statements, turn on flag
4804 elsif Nkind (Stm) = N_Null_Statement
4805 and then Comes_From_Source (Stm)
4806 and then No (Next (Stm))
4807 then
4808 Set_Trivial_Subprogram (Stm);
4810 -- Check for explicit call cases which likely raise an exception
4812 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4813 if Is_Entity_Name (Name (Ostm)) then
4814 declare
4815 Ent : constant Entity_Id := Entity (Name (Ostm));
4817 begin
4818 -- If the procedure is marked No_Return, then likely it
4819 -- raises an exception, but in any case it is not coming
4820 -- back here, so turn on the flag.
4822 if Present (Ent)
4823 and then Ekind (Ent) = E_Procedure
4824 and then No_Return (Ent)
4825 then
4826 Set_Trivial_Subprogram (Stm);
4827 end if;
4828 end;
4829 end if;
4830 end if;
4831 end;
4832 end;
4834 -- Check if a Body_To_Inline was created, but the subprogram has
4835 -- references to object renamings which will be replaced by the special
4836 -- SPARK expansion into nodes of a different kind, which is not expected
4837 -- by the inlining mechanism. In that case, the Body_To_Inline is
4838 -- deleted prior to being analyzed. This check needs to take place
4839 -- after analysis of the subprogram body.
4841 if GNATprove_Mode
4842 and then Present (Spec_Id)
4843 and then
4844 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4845 and then Present (Body_To_Inline (Unit_Declaration_Node (Spec_Id)))
4846 then
4847 Check_Object_Renaming_In_GNATprove_Mode (Spec_Id);
4848 end if;
4850 -- Check for variables that are never modified
4852 declare
4853 F1 : Entity_Id;
4854 F2 : Entity_Id;
4856 begin
4857 -- If there is a separate spec, then transfer Never_Set_In_Source
4858 -- flags from out parameters to the corresponding entities in the
4859 -- body. The reason we do that is we want to post error flags on
4860 -- the body entities, not the spec entities.
4862 if Present (Spec_Id) then
4863 F1 := First_Formal (Spec_Id);
4864 while Present (F1) loop
4865 if Ekind (F1) = E_Out_Parameter then
4866 F2 := First_Formal (Body_Id);
4867 while Present (F2) loop
4868 exit when Chars (F1) = Chars (F2);
4869 Next_Formal (F2);
4870 end loop;
4872 if Present (F2) then
4873 Set_Never_Set_In_Source (F2, Never_Set_In_Source (F1));
4874 end if;
4875 end if;
4877 Next_Formal (F1);
4878 end loop;
4879 end if;
4881 -- Check references of the subprogram spec when we are dealing with
4882 -- an expression function due to it having a generated body.
4884 if Present (Spec_Id)
4885 and then Is_Expression_Function (Spec_Id)
4886 then
4887 Check_References (Spec_Id);
4889 -- Skip the check for subprograms generated for protected subprograms
4890 -- because it is also done for the protected subprograms themselves.
4892 elsif Present (Spec_Id)
4893 and then Present (Protected_Subprogram (Spec_Id))
4894 then
4895 null;
4897 -- Otherwise, we simply check the formals of the subprogram body.
4899 else
4900 Check_References (Body_Id);
4901 end if;
4902 end;
4904 -- Check for nested subprogram, and mark outer level subprogram if so
4906 declare
4907 Ent : Entity_Id;
4909 begin
4910 if Present (Spec_Id) then
4911 Ent := Spec_Id;
4912 else
4913 Ent := Body_Id;
4914 end if;
4916 loop
4917 Ent := Enclosing_Subprogram (Ent);
4918 exit when No (Ent) or else Is_Subprogram (Ent);
4919 end loop;
4921 if Present (Ent) then
4922 Set_Has_Nested_Subprogram (Ent);
4923 end if;
4924 end;
4926 -- Restore the limited views in the spec, if any, to let the back end
4927 -- process it without running into circularities.
4929 if Present (Exch_Views) then
4930 Restore_Limited_Views (Exch_Views);
4931 end if;
4933 if Present (Mask_Types) then
4934 Unmask_Unfrozen_Types (Mask_Types);
4935 end if;
4937 if Present (Desig_View) then
4938 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
4939 end if;
4941 <<Leave>>
4942 if Present (Ignored_Ghost_Region) then
4943 Expander_Active := Saved_EA;
4944 end if;
4946 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
4947 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4948 end Analyze_Subprogram_Body_Helper;
4950 ------------------------------------
4951 -- Analyze_Subprogram_Declaration --
4952 ------------------------------------
4954 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
4955 Scop : constant Entity_Id := Current_Scope;
4956 Designator : Entity_Id;
4958 Is_Completion : Boolean;
4959 -- Indicates whether a null procedure declaration is a completion
4961 begin
4962 -- Null procedures are not allowed in SPARK
4964 if Nkind (Specification (N)) = N_Procedure_Specification
4965 and then Null_Present (Specification (N))
4966 then
4967 -- Null procedures are allowed in protected types, following the
4968 -- recent AI12-0147.
4970 if Is_Protected_Type (Current_Scope)
4971 and then Ada_Version < Ada_2012
4972 then
4973 Error_Msg_N ("protected operation cannot be a null procedure", N);
4974 end if;
4976 Analyze_Null_Procedure (N, Is_Completion);
4978 -- The null procedure acts as a body, nothing further is needed
4980 if Is_Completion then
4981 return;
4982 end if;
4983 end if;
4985 Designator := Analyze_Subprogram_Specification (Specification (N));
4987 -- A reference may already have been generated for the unit name, in
4988 -- which case the following call is redundant. However it is needed for
4989 -- declarations that are the rewriting of an expression function.
4991 Generate_Definition (Designator);
4993 -- Set the SPARK mode from the current context (may be overwritten later
4994 -- with explicit pragma). This is not done for entry barrier functions
4995 -- because they are generated outside the protected type and should not
4996 -- carry the mode of the enclosing context.
4998 if Nkind (N) = N_Subprogram_Declaration
4999 and then Is_Entry_Barrier_Function (N)
5000 then
5001 null;
5003 else
5004 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5005 Set_SPARK_Pragma_Inherited (Designator);
5006 end if;
5008 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5009 -- the body of this subprogram is instantiated or inlined later and out
5010 -- of context. The body uses this attribute to restore the value of the
5011 -- global flag.
5013 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5014 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5015 end if;
5017 -- Preserve relevant elaboration-related attributes of the context which
5018 -- are no longer available or very expensive to recompute once analysis,
5019 -- resolution, and expansion are over.
5021 Mark_Elaboration_Attributes
5022 (N_Id => Designator,
5023 Checks => True,
5024 Warnings => True);
5026 if Debug_Flag_C then
5027 Write_Str ("==> subprogram spec ");
5028 Write_Name (Chars (Designator));
5029 Write_Str (" from ");
5030 Write_Location (Sloc (N));
5031 Write_Eol;
5032 Indent;
5033 end if;
5035 Validate_RCI_Subprogram_Declaration (N);
5036 New_Overloaded_Entity (Designator);
5037 Check_Delayed_Subprogram (Designator);
5039 -- If the type of the first formal of the current subprogram is a non-
5040 -- generic tagged private type, mark the subprogram as being a private
5041 -- primitive. Ditto if this is a function with controlling result, and
5042 -- the return type is currently private. In both cases, the type of the
5043 -- controlling argument or result must be in the current scope for the
5044 -- operation to be primitive.
5046 if Has_Controlling_Result (Designator)
5047 and then Is_Private_Type (Etype (Designator))
5048 and then Scope (Etype (Designator)) = Current_Scope
5049 and then not Is_Generic_Actual_Type (Etype (Designator))
5050 then
5051 Set_Is_Private_Primitive (Designator);
5053 elsif Present (First_Formal (Designator)) then
5054 declare
5055 Formal_Typ : constant Entity_Id :=
5056 Etype (First_Formal (Designator));
5057 begin
5058 Set_Is_Private_Primitive (Designator,
5059 Is_Tagged_Type (Formal_Typ)
5060 and then Scope (Formal_Typ) = Current_Scope
5061 and then Is_Private_Type (Formal_Typ)
5062 and then not Is_Generic_Actual_Type (Formal_Typ));
5063 end;
5064 end if;
5066 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5067 -- or null.
5069 if Ada_Version >= Ada_2005
5070 and then Comes_From_Source (N)
5071 and then Is_Dispatching_Operation (Designator)
5072 then
5073 declare
5074 E : Entity_Id;
5075 Etyp : Entity_Id;
5077 begin
5078 if Has_Controlling_Result (Designator) then
5079 Etyp := Etype (Designator);
5081 else
5082 E := First_Entity (Designator);
5083 while Present (E)
5084 and then Is_Formal (E)
5085 and then not Is_Controlling_Formal (E)
5086 loop
5087 Next_Entity (E);
5088 end loop;
5090 Etyp := Etype (E);
5091 end if;
5093 if Is_Access_Type (Etyp) then
5094 Etyp := Directly_Designated_Type (Etyp);
5095 end if;
5097 if Is_Interface (Etyp)
5098 and then not Is_Abstract_Subprogram (Designator)
5099 and then not (Ekind (Designator) = E_Procedure
5100 and then Null_Present (Specification (N)))
5101 then
5102 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5104 -- Specialize error message based on procedures vs. functions,
5105 -- since functions can't be null subprograms.
5107 if Ekind (Designator) = E_Procedure then
5108 Error_Msg_N
5109 ("interface procedure % must be abstract or null", N);
5110 else
5111 Error_Msg_N
5112 ("interface function % must be abstract", N);
5113 end if;
5114 end if;
5115 end;
5116 end if;
5118 -- For a compilation unit, set body required. This flag will only be
5119 -- reset if a valid Import or Interface pragma is processed later on.
5121 if Nkind (Parent (N)) = N_Compilation_Unit then
5122 Set_Body_Required (Parent (N), True);
5124 if Ada_Version >= Ada_2005
5125 and then Nkind (Specification (N)) = N_Procedure_Specification
5126 and then Null_Present (Specification (N))
5127 then
5128 Error_Msg_N
5129 ("null procedure cannot be declared at library level", N);
5130 end if;
5131 end if;
5133 Generate_Reference_To_Formals (Designator);
5134 Check_Eliminated (Designator);
5136 if Debug_Flag_C then
5137 Outdent;
5138 Write_Str ("<== subprogram spec ");
5139 Write_Name (Chars (Designator));
5140 Write_Str (" from ");
5141 Write_Location (Sloc (N));
5142 Write_Eol;
5143 end if;
5145 -- Indicate that this is a protected operation, because it may be used
5146 -- in subsequent declarations within the protected type.
5148 if Is_Protected_Type (Current_Scope) then
5149 Set_Convention (Designator, Convention_Protected);
5150 end if;
5152 List_Inherited_Pre_Post_Aspects (Designator);
5154 -- Process the aspects before establishing the proper categorization in
5155 -- case the subprogram is a compilation unit and one of its aspects is
5156 -- converted into a categorization pragma.
5158 if Has_Aspects (N) then
5159 Analyze_Aspect_Specifications (N, Designator);
5160 end if;
5162 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5163 Set_Categorization_From_Scope (Designator, Scop);
5165 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5166 -- proper categorization of the unit based on its pragmas.
5168 else
5169 Push_Scope (Designator);
5170 Set_Categorization_From_Pragmas (N);
5171 Validate_Categorization_Dependency (N, Designator);
5172 Pop_Scope;
5173 end if;
5174 end Analyze_Subprogram_Declaration;
5176 --------------------------------------
5177 -- Analyze_Subprogram_Specification --
5178 --------------------------------------
5180 -- Reminder: N here really is a subprogram specification (not a subprogram
5181 -- declaration). This procedure is called to analyze the specification in
5182 -- both subprogram bodies and subprogram declarations (specs).
5184 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5185 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5186 -- Determine whether entity E denotes the spec or body of an invariant
5187 -- procedure.
5189 ------------------------------------
5190 -- Is_Invariant_Procedure_Or_Body --
5191 ------------------------------------
5193 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5194 Decl : constant Node_Id := Unit_Declaration_Node (E);
5195 Spec : Entity_Id;
5197 begin
5198 if Nkind (Decl) = N_Subprogram_Body then
5199 Spec := Corresponding_Spec (Decl);
5200 else
5201 Spec := E;
5202 end if;
5204 return
5205 Present (Spec)
5206 and then Ekind (Spec) = E_Procedure
5207 and then (Is_Partial_Invariant_Procedure (Spec)
5208 or else Is_Invariant_Procedure (Spec));
5209 end Is_Invariant_Procedure_Or_Body;
5211 -- Local variables
5213 Designator : constant Entity_Id := Defining_Entity (N);
5214 Formals : constant List_Id := Parameter_Specifications (N);
5216 -- Start of processing for Analyze_Subprogram_Specification
5218 begin
5219 -- Proceed with analysis. Do not emit a cross-reference entry if the
5220 -- specification comes from an expression function, because it may be
5221 -- the completion of a previous declaration. If it is not, the cross-
5222 -- reference entry will be emitted for the new subprogram declaration.
5224 if Nkind (Parent (N)) /= N_Expression_Function then
5225 Generate_Definition (Designator);
5226 end if;
5228 if Nkind (N) = N_Function_Specification then
5229 Mutate_Ekind (Designator, E_Function);
5230 Set_Mechanism (Designator, Default_Mechanism);
5231 else
5232 Mutate_Ekind (Designator, E_Procedure);
5233 Set_Etype (Designator, Standard_Void_Type);
5234 end if;
5236 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5237 -- those subprograms which could be inlined in GNATprove mode (because
5238 -- Body_To_Inline is non-Empty) but should not be inlined.
5240 if GNATprove_Mode then
5241 Set_Is_Inlined_Always (Designator);
5242 end if;
5244 -- Introduce new scope for analysis of the formals and the return type
5246 Set_Scope (Designator, Current_Scope);
5248 if Present (Formals) then
5249 Push_Scope (Designator);
5250 Process_Formals (Formals, N);
5252 -- Check dimensions in N for formals with default expression
5254 Analyze_Dimension_Formals (N, Formals);
5256 -- Ada 2005 (AI-345): If this is an overriding operation of an
5257 -- inherited interface operation, and the controlling type is
5258 -- a synchronized type, replace the type with its corresponding
5259 -- record, to match the proper signature of an overriding operation.
5260 -- Same processing for an access parameter whose designated type is
5261 -- derived from a synchronized interface.
5263 -- This modification is not done for invariant procedures because
5264 -- the corresponding record may not necessarely be visible when the
5265 -- concurrent type acts as the full view of a private type.
5267 -- package Pack is
5268 -- type Prot is private with Type_Invariant => ...;
5269 -- procedure ConcInvariant (Obj : Prot);
5270 -- private
5271 -- protected type Prot is ...;
5272 -- type Concurrent_Record_Prot is record ...;
5273 -- procedure ConcInvariant (Obj : Prot) is
5274 -- ...
5275 -- end ConcInvariant;
5276 -- end Pack;
5278 -- In the example above, both the spec and body of the invariant
5279 -- procedure must utilize the private type as the controlling type.
5281 if Ada_Version >= Ada_2005
5282 and then not Is_Invariant_Procedure_Or_Body (Designator)
5283 then
5284 declare
5285 Formal : Entity_Id;
5286 Formal_Typ : Entity_Id;
5287 Rec_Typ : Entity_Id;
5288 Desig_Typ : Entity_Id;
5290 begin
5291 Formal := First_Formal (Designator);
5292 while Present (Formal) loop
5293 Formal_Typ := Etype (Formal);
5295 if Is_Concurrent_Type (Formal_Typ)
5296 and then Present (Corresponding_Record_Type (Formal_Typ))
5297 then
5298 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5300 if Present (Interfaces (Rec_Typ)) then
5301 Set_Etype (Formal, Rec_Typ);
5302 end if;
5304 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5305 Desig_Typ := Designated_Type (Formal_Typ);
5307 if Is_Concurrent_Type (Desig_Typ)
5308 and then Present (Corresponding_Record_Type (Desig_Typ))
5309 then
5310 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5312 if Present (Interfaces (Rec_Typ)) then
5313 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5314 end if;
5315 end if;
5316 end if;
5318 Next_Formal (Formal);
5319 end loop;
5320 end;
5321 end if;
5323 End_Scope;
5325 -- The subprogram scope is pushed and popped around the processing of
5326 -- the return type for consistency with call above to Process_Formals
5327 -- (which itself can call Analyze_Return_Type), and to ensure that any
5328 -- itype created for the return type will be associated with the proper
5329 -- scope.
5331 elsif Nkind (N) = N_Function_Specification then
5332 Push_Scope (Designator);
5333 Analyze_Return_Type (N);
5334 End_Scope;
5335 end if;
5337 -- Function case
5339 if Nkind (N) = N_Function_Specification then
5341 -- Deal with operator symbol case
5343 if Nkind (Designator) = N_Defining_Operator_Symbol then
5344 Valid_Operator_Definition (Designator);
5345 end if;
5347 May_Need_Actuals (Designator);
5349 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5350 -- the subprogram is abstract also. This does not apply to renaming
5351 -- declarations, where abstractness is inherited, and to subprogram
5352 -- bodies generated for stream operations, which become renamings as
5353 -- bodies. We also skip the check for thunks.
5355 -- In case of primitives associated with abstract interface types
5356 -- the check is applied later (see Analyze_Subprogram_Declaration).
5358 if Nkind (Original_Node (Parent (N))) not in
5359 N_Abstract_Subprogram_Declaration |
5360 N_Formal_Abstract_Subprogram_Declaration |
5361 N_Subprogram_Renaming_Declaration
5362 and then not Is_Thunk (Designator)
5363 then
5364 if Is_Abstract_Type (Etype (Designator)) then
5365 Error_Msg_N
5366 ("function that returns abstract type must be abstract", N);
5368 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5369 -- access result whose designated type is abstract.
5371 elsif Ada_Version >= Ada_2012
5372 and then Nkind (Result_Definition (N)) = N_Access_Definition
5373 and then
5374 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5375 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5376 then
5377 Error_Msg_N
5378 ("function whose access result designates abstract type "
5379 & "must be abstract", N);
5380 end if;
5381 end if;
5382 end if;
5384 return Designator;
5385 end Analyze_Subprogram_Specification;
5387 -----------------------
5388 -- Check_Conformance --
5389 -----------------------
5391 procedure Check_Conformance
5392 (New_Id : Entity_Id;
5393 Old_Id : Entity_Id;
5394 Ctype : Conformance_Type;
5395 Errmsg : Boolean;
5396 Conforms : out Boolean;
5397 Err_Loc : Node_Id := Empty;
5398 Get_Inst : Boolean := False;
5399 Skip_Controlling_Formals : Boolean := False)
5401 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5402 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5403 -- If Errmsg is True, then processing continues to post an error message
5404 -- for conformance error on given node. Two messages are output. The
5405 -- first message points to the previous declaration with a general "no
5406 -- conformance" message. The second is the detailed reason, supplied as
5407 -- Msg. The parameter N provide information for a possible & insertion
5408 -- in the message, and also provides the location for posting the
5409 -- message in the absence of a specified Err_Loc location.
5411 function Conventions_Match (Id1, Id2 : Entity_Id) return Boolean;
5412 -- True if the conventions of entities Id1 and Id2 match.
5414 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean;
5415 -- True if the null exclusions of two formals of anonymous access type
5416 -- match.
5418 function Subprogram_Subtypes_Have_Same_Declaration
5419 (Subp : Entity_Id;
5420 Decl_Subtype : Entity_Id;
5421 Body_Subtype : Entity_Id) return Boolean;
5422 -- Checks whether corresponding subtypes named within a subprogram
5423 -- declaration and body originate from the same declaration, and returns
5424 -- True when they do. In the case of anonymous access-to-object types,
5425 -- checks the designated types. Also returns True when GNAT_Mode is
5426 -- enabled, or when the subprogram is marked Is_Internal or occurs
5427 -- within a generic instantiation or internal unit (GNAT library unit).
5429 -----------------------
5430 -- Conformance_Error --
5431 -----------------------
5433 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5434 Enode : Node_Id;
5436 begin
5437 Conforms := False;
5439 if Errmsg then
5440 if No (Err_Loc) then
5441 Enode := N;
5442 else
5443 Enode := Err_Loc;
5444 end if;
5446 Error_Msg_Sloc := Sloc (Old_Id);
5448 case Ctype is
5449 when Type_Conformant =>
5450 Error_Msg_N -- CODEFIX
5451 ("not type conformant with declaration#!", Enode);
5453 when Mode_Conformant =>
5454 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5455 Error_Msg_N
5456 ("not mode conformant with operation inherited#!",
5457 Enode);
5458 else
5459 Error_Msg_N
5460 ("not mode conformant with declaration#!", Enode);
5461 end if;
5463 when Subtype_Conformant =>
5464 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5465 Error_Msg_N
5466 ("not subtype conformant with operation inherited#!",
5467 Enode);
5468 else
5469 Error_Msg_N
5470 ("not subtype conformant with declaration#!", Enode);
5471 end if;
5473 when Fully_Conformant =>
5474 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5475 Error_Msg_N -- CODEFIX
5476 ("not fully conformant with operation inherited#!",
5477 Enode);
5478 else
5479 Error_Msg_N -- CODEFIX
5480 ("not fully conformant with declaration#!", Enode);
5481 end if;
5482 end case;
5484 Error_Msg_NE (Msg, Enode, N);
5485 end if;
5486 end Conformance_Error;
5488 -----------------------
5489 -- Conventions_Match --
5490 -----------------------
5492 function Conventions_Match
5493 (Id1 : Entity_Id;
5494 Id2 : Entity_Id) return Boolean
5496 begin
5497 -- Ignore the conventions of anonymous access-to-subprogram types
5498 -- and subprogram types because these are internally generated and
5499 -- the only way these may receive a convention is if they inherit
5500 -- the convention of a related subprogram.
5502 if Ekind (Id1) in E_Anonymous_Access_Subprogram_Type
5503 | E_Subprogram_Type
5504 or else
5505 Ekind (Id2) in E_Anonymous_Access_Subprogram_Type
5506 | E_Subprogram_Type
5507 then
5508 return True;
5510 -- Otherwise compare the conventions directly
5512 else
5513 return Convention (Id1) = Convention (Id2);
5514 end if;
5515 end Conventions_Match;
5517 ---------------------------
5518 -- Null_Exclusions_Match --
5519 ---------------------------
5521 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean is
5522 begin
5523 if not Is_Anonymous_Access_Type (Etype (F1))
5524 or else not Is_Anonymous_Access_Type (Etype (F2))
5525 then
5526 return True;
5527 end if;
5529 -- AI12-0289-1: Case of controlling access parameter; False if the
5530 -- partial view is untagged, the full view is tagged, and no explicit
5531 -- "not null". Note that at this point, we're processing the package
5532 -- body, so private/full types have been swapped. The Sloc test below
5533 -- is to detect the (legal) case where F1 comes after the full type
5534 -- declaration. This part is disabled pre-2005, because "not null" is
5535 -- not allowed on those language versions.
5537 if Ada_Version >= Ada_2005
5538 and then Is_Controlling_Formal (F1)
5539 and then not Null_Exclusion_Present (Parent (F1))
5540 and then not Null_Exclusion_Present (Parent (F2))
5541 then
5542 declare
5543 D : constant Entity_Id := Directly_Designated_Type (Etype (F1));
5544 Partial_View_Of_Desig : constant Entity_Id :=
5545 Incomplete_Or_Partial_View (D);
5546 begin
5547 return No (Partial_View_Of_Desig)
5548 or else Is_Tagged_Type (Partial_View_Of_Desig)
5549 or else Sloc (D) < Sloc (F1);
5550 end;
5552 -- Not a controlling parameter, or one or both views have an explicit
5553 -- "not null".
5555 else
5556 return Null_Exclusion_Present (Parent (F1)) =
5557 Null_Exclusion_Present (Parent (F2));
5558 end if;
5559 end Null_Exclusions_Match;
5561 function Subprogram_Subtypes_Have_Same_Declaration
5562 (Subp : Entity_Id;
5563 Decl_Subtype : Entity_Id;
5564 Body_Subtype : Entity_Id) return Boolean
5567 function Nonlimited_View_Of_Subtype
5568 (Subt : Entity_Id) return Entity_Id;
5569 -- Returns the nonlimited view of a type or subtype that is an
5570 -- incomplete or class-wide type that comes from a limited view of
5571 -- a package (From_Limited_With is True for the entity), or the
5572 -- full view when the subtype is an incomplete type. Otherwise
5573 -- returns the entity passed in.
5575 function Nonlimited_View_Of_Subtype
5576 (Subt : Entity_Id) return Entity_Id
5578 Subt_Temp : Entity_Id := Subt;
5579 begin
5580 if Ekind (Subt) in Incomplete_Kind | E_Class_Wide_Type
5581 and then From_Limited_With (Subt)
5582 then
5583 Subt_Temp := Non_Limited_View (Subt);
5584 end if;
5586 -- If the subtype is incomplete, return full view if present
5587 -- (and accounts for the case where a type from a limited view
5588 -- is itself an incomplete type).
5590 if Ekind (Subt_Temp) in Incomplete_Kind
5591 and then Present (Full_View (Subt_Temp))
5592 then
5593 Subt_Temp := Full_View (Subt_Temp);
5594 end if;
5596 return Subt_Temp;
5597 end Nonlimited_View_Of_Subtype;
5599 -- Start of processing for Subprogram_Subtypes_Have_Same_Declaration
5601 begin
5602 if not In_Instance
5603 and then not In_Internal_Unit (Subp)
5604 and then not Is_Internal (Subp)
5605 and then not GNAT_Mode
5606 and then
5607 Ekind (Etype (Decl_Subtype)) not in Access_Subprogram_Kind
5608 then
5609 if Ekind (Etype (Decl_Subtype)) = E_Anonymous_Access_Type then
5610 if Nonlimited_View_Of_Subtype (Designated_Type (Decl_Subtype))
5611 /= Nonlimited_View_Of_Subtype (Designated_Type (Body_Subtype))
5612 then
5613 return False;
5614 end if;
5616 elsif Nonlimited_View_Of_Subtype (Decl_Subtype)
5617 /= Nonlimited_View_Of_Subtype (Body_Subtype)
5618 then
5619 -- Avoid returning False (and a false-positive warning) for
5620 -- the case of "not null" itypes, which will appear to be
5621 -- different subtypes even when the subtype_marks denote
5622 -- the same subtype.
5624 if Ekind (Decl_Subtype) = E_Access_Subtype
5625 and then Ekind (Body_Subtype) = E_Access_Subtype
5626 and then Is_Itype (Body_Subtype)
5627 and then Can_Never_Be_Null (Body_Subtype)
5628 and then Etype (Decl_Subtype) = Etype (Body_Subtype)
5629 then
5630 return True;
5632 else
5633 return False;
5634 end if;
5635 end if;
5636 end if;
5638 return True;
5639 end Subprogram_Subtypes_Have_Same_Declaration;
5641 -- Local Variables
5643 Old_Type : constant Entity_Id := Etype (Old_Id);
5644 New_Type : constant Entity_Id := Etype (New_Id);
5645 Old_Formal : Entity_Id;
5646 New_Formal : Entity_Id;
5647 Old_Formal_Base : Entity_Id;
5648 New_Formal_Base : Entity_Id;
5650 -- Start of processing for Check_Conformance
5652 begin
5653 Conforms := True;
5655 -- We need a special case for operators, since they don't appear
5656 -- explicitly.
5658 if Ctype = Type_Conformant then
5659 if Ekind (New_Id) = E_Operator
5660 and then Operator_Matches_Spec (New_Id, Old_Id)
5661 then
5662 return;
5663 end if;
5664 end if;
5666 -- If both are functions/operators, check return types conform
5668 if Old_Type /= Standard_Void_Type
5669 and then
5670 New_Type /= Standard_Void_Type
5671 then
5672 -- If we are checking interface conformance we omit controlling
5673 -- arguments and result, because we are only checking the conformance
5674 -- of the remaining parameters.
5676 if Has_Controlling_Result (Old_Id)
5677 and then Has_Controlling_Result (New_Id)
5678 and then Skip_Controlling_Formals
5679 then
5680 null;
5682 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5683 if Ctype >= Subtype_Conformant
5684 and then not Predicates_Match (Old_Type, New_Type)
5685 then
5686 Conformance_Error
5687 ("\predicate of return type does not match!", New_Id);
5688 else
5689 Conformance_Error
5690 ("\return type does not match!", New_Id);
5691 end if;
5693 return;
5695 -- If the result subtypes conform and pedantic checks are enabled,
5696 -- check to see whether the subtypes originate from different
5697 -- declarations, and issue a warning when they do.
5699 elsif Ctype = Fully_Conformant
5700 and then Warn_On_Pedantic_Checks
5701 and then not Subprogram_Subtypes_Have_Same_Declaration
5702 (Old_Id, Old_Type, New_Type)
5703 then
5704 Error_Msg_N ("result subtypes conform but come from different "
5705 & "declarations?_p?", New_Id);
5706 end if;
5708 -- Ada 2005 (AI-231): In case of anonymous access types check the
5709 -- null-exclusion and access-to-constant attributes match.
5711 if Ada_Version >= Ada_2005
5712 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5713 and then
5714 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5715 or else Is_Access_Constant (Etype (Old_Type)) /=
5716 Is_Access_Constant (Etype (New_Type)))
5717 then
5718 Conformance_Error ("\return type does not match!", New_Id);
5719 return;
5720 end if;
5722 -- If either is a function/operator and the other isn't, error
5724 elsif Old_Type /= Standard_Void_Type
5725 or else New_Type /= Standard_Void_Type
5726 then
5727 Conformance_Error ("\functions can only match functions!", New_Id);
5728 return;
5729 end if;
5731 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5732 -- If this is a renaming as body, refine error message to indicate that
5733 -- the conflict is with the original declaration. If the entity is not
5734 -- frozen, the conventions don't have to match, the one of the renamed
5735 -- entity is inherited.
5737 if Ctype >= Subtype_Conformant then
5738 if not Conventions_Match (Old_Id, New_Id) then
5739 if not Is_Frozen (New_Id) then
5740 null;
5742 elsif Present (Err_Loc)
5743 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5744 and then Present (Corresponding_Spec (Err_Loc))
5745 then
5746 Error_Msg_Name_1 := Chars (New_Id);
5747 Error_Msg_Name_2 :=
5748 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5749 Conformance_Error ("\prior declaration for% has convention %!");
5750 return;
5752 else
5753 Conformance_Error ("\calling conventions do not match!");
5754 return;
5755 end if;
5756 else
5757 Check_Formal_Subprogram_Conformance
5758 (New_Id, Old_Id, Err_Loc, Errmsg, Conforms);
5760 if not Conforms then
5761 return;
5762 end if;
5763 end if;
5764 end if;
5766 -- Deal with parameters
5768 -- Note: we use the entity information, rather than going directly
5769 -- to the specification in the tree. This is not only simpler, but
5770 -- absolutely necessary for some cases of conformance tests between
5771 -- operators, where the declaration tree simply does not exist.
5773 Old_Formal := First_Formal (Old_Id);
5774 New_Formal := First_Formal (New_Id);
5775 while Present (Old_Formal) and then Present (New_Formal) loop
5776 if Is_Controlling_Formal (Old_Formal)
5777 and then Is_Controlling_Formal (New_Formal)
5778 and then Skip_Controlling_Formals
5779 then
5780 -- The controlling formals will have different types when
5781 -- comparing an interface operation with its match, but both
5782 -- or neither must be access parameters.
5784 if Is_Access_Type (Etype (Old_Formal))
5786 Is_Access_Type (Etype (New_Formal))
5787 then
5788 goto Skip_Controlling_Formal;
5789 else
5790 Conformance_Error
5791 ("\access parameter does not match!", New_Formal);
5792 end if;
5793 end if;
5795 -- Ada 2012: Mode conformance also requires that formal parameters
5796 -- be both aliased, or neither.
5798 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5799 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5800 Conformance_Error
5801 ("\aliased parameter mismatch!", New_Formal);
5802 end if;
5803 end if;
5805 if Ctype = Fully_Conformant then
5807 -- Names must match. Error message is more accurate if we do
5808 -- this before checking that the types of the formals match.
5810 if Chars (Old_Formal) /= Chars (New_Formal) then
5811 Conformance_Error ("\name& does not match!", New_Formal);
5813 -- Set error posted flag on new formal as well to stop
5814 -- junk cascaded messages in some cases.
5816 Set_Error_Posted (New_Formal);
5817 return;
5818 end if;
5820 -- Null exclusion must match
5822 if not Relaxed_RM_Semantics
5823 and then not Null_Exclusions_Match (Old_Formal, New_Formal)
5824 then
5825 Conformance_Error
5826 ("\null exclusion for& does not match", New_Formal);
5828 -- Mark error posted on the new formal to avoid duplicated
5829 -- complaint about types not matching.
5831 Set_Error_Posted (New_Formal);
5832 end if;
5833 end if;
5835 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5836 -- case occurs whenever a subprogram is being renamed and one of its
5837 -- parameters imposes a null exclusion. For example:
5839 -- type T is null record;
5840 -- type Acc_T is access T;
5841 -- subtype Acc_T_Sub is Acc_T;
5843 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5844 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5845 -- renames P;
5847 Old_Formal_Base := Etype (Old_Formal);
5848 New_Formal_Base := Etype (New_Formal);
5850 if Get_Inst then
5851 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5852 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5853 end if;
5855 -- Types must always match. In the visible part of an instance,
5856 -- usual overloading rules for dispatching operations apply, and
5857 -- we check base types (not the actual subtypes).
5859 if In_Instance_Visible_Part
5860 and then Is_Dispatching_Operation (New_Id)
5861 then
5862 if not Conforming_Types
5863 (T1 => Base_Type (Etype (Old_Formal)),
5864 T2 => Base_Type (Etype (New_Formal)),
5865 Ctype => Ctype,
5866 Get_Inst => Get_Inst)
5867 then
5868 Conformance_Error ("\type of & does not match!", New_Formal);
5869 return;
5870 end if;
5872 elsif not Conforming_Types
5873 (T1 => Old_Formal_Base,
5874 T2 => New_Formal_Base,
5875 Ctype => Ctype,
5876 Get_Inst => Get_Inst)
5877 then
5878 -- Don't give error message if old type is Any_Type. This test
5879 -- avoids some cascaded errors, e.g. in case of a bad spec.
5881 if Errmsg and then Old_Formal_Base = Any_Type then
5882 Conforms := False;
5883 else
5884 if Ctype >= Subtype_Conformant
5885 and then
5886 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5887 then
5888 Conformance_Error
5889 ("\predicate of & does not match!", New_Formal);
5890 else
5891 Conformance_Error
5892 ("\type of & does not match!", New_Formal);
5894 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5895 then
5896 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5897 end if;
5898 end if;
5899 end if;
5901 return;
5903 -- If the formals' subtypes conform and pedantic checks are enabled,
5904 -- check to see whether the subtypes originate from different
5905 -- declarations, and issue a warning when they do.
5907 elsif Ctype = Fully_Conformant
5908 and then Warn_On_Pedantic_Checks
5909 and then not Subprogram_Subtypes_Have_Same_Declaration
5910 (Old_Id, Old_Formal_Base, New_Formal_Base)
5911 then
5912 Error_Msg_N ("formal subtypes conform but come from "
5913 & "different declarations?_p?", New_Formal);
5914 end if;
5916 -- For mode conformance, mode must match
5918 if Ctype >= Mode_Conformant then
5919 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5920 if Ekind (New_Id) not in E_Function | E_Procedure
5921 or else not Is_Primitive_Wrapper (New_Id)
5922 then
5923 Conformance_Error ("\mode of & does not match!", New_Formal);
5925 else
5926 declare
5927 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5928 begin
5929 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5930 then
5931 Conforms := False;
5933 if Errmsg then
5934 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5935 end if;
5936 else
5937 Conformance_Error
5938 ("\mode of & does not match!", New_Formal);
5939 end if;
5940 end;
5941 end if;
5943 return;
5945 elsif Is_Access_Type (Old_Formal_Base)
5946 and then Is_Access_Type (New_Formal_Base)
5947 and then Is_Access_Constant (Old_Formal_Base) /=
5948 Is_Access_Constant (New_Formal_Base)
5949 then
5950 Conformance_Error
5951 ("\constant modifier does not match!", New_Formal);
5952 return;
5953 end if;
5954 end if;
5956 if Ctype >= Subtype_Conformant then
5958 -- Ada 2005 (AI-231): In case of anonymous access types check
5959 -- the null-exclusion and access-to-constant attributes must
5960 -- match. For null exclusion, we test the types rather than the
5961 -- formals themselves, since the attribute is only set reliably
5962 -- on the formals in the Ada 95 case, and we exclude the case
5963 -- where Old_Formal is marked as controlling, to avoid errors
5964 -- when matching completing bodies with dispatching declarations
5965 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5967 if Ada_Version >= Ada_2005
5968 and then Is_Anonymous_Access_Type (Etype (Old_Formal))
5969 and then Is_Anonymous_Access_Type (Etype (New_Formal))
5970 and then
5971 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5972 Can_Never_Be_Null (Etype (New_Formal))
5973 and then
5974 not Is_Controlling_Formal (Old_Formal))
5975 or else
5976 Is_Access_Constant (Etype (Old_Formal)) /=
5977 Is_Access_Constant (Etype (New_Formal)))
5979 -- Do not complain if error already posted on New_Formal. This
5980 -- avoids some redundant error messages.
5982 and then not Error_Posted (New_Formal)
5983 then
5984 -- It is allowed to omit the null-exclusion in case of stream
5985 -- attribute subprograms. We recognize stream subprograms
5986 -- through their TSS-generated suffix.
5988 declare
5989 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5991 begin
5992 if TSS_Name /= TSS_Stream_Read
5993 and then TSS_Name /= TSS_Stream_Write
5994 and then TSS_Name /= TSS_Stream_Input
5995 and then TSS_Name /= TSS_Stream_Output
5996 then
5997 -- Here we have a definite conformance error. It is worth
5998 -- special casing the error message for the case of a
5999 -- controlling formal (which excludes null).
6001 if Is_Controlling_Formal (New_Formal) then
6002 Error_Msg_Node_2 := Scope (New_Formal);
6003 Conformance_Error
6004 ("\controlling formal & of & excludes null, "
6005 & "declaration must exclude null as well",
6006 New_Formal);
6008 -- Normal case (couldn't we give more detail here???)
6010 else
6011 Conformance_Error
6012 ("\type of & does not match!", New_Formal);
6013 end if;
6015 return;
6016 end if;
6017 end;
6018 end if;
6019 end if;
6021 -- Full conformance checks
6023 if Ctype = Fully_Conformant then
6025 -- We have checked already that names match
6027 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6029 -- Check default expressions for in parameters
6031 declare
6032 NewD : constant Boolean :=
6033 Present (Default_Value (New_Formal));
6034 OldD : constant Boolean :=
6035 Present (Default_Value (Old_Formal));
6036 begin
6037 if NewD or OldD then
6039 -- The old default value has been analyzed because the
6040 -- current full declaration will have frozen everything
6041 -- before. The new default value has not been analyzed,
6042 -- so analyze it now before we check for conformance.
6044 if NewD then
6045 Push_Scope (New_Id);
6046 Preanalyze_Spec_Expression
6047 (Default_Value (New_Formal), Etype (New_Formal));
6048 End_Scope;
6049 end if;
6051 if not (NewD and OldD)
6052 or else not Fully_Conformant_Expressions
6053 (Default_Value (Old_Formal),
6054 Default_Value (New_Formal))
6055 then
6056 Conformance_Error
6057 ("\default expression for & does not match!",
6058 New_Formal);
6059 return;
6060 end if;
6061 end if;
6062 end;
6063 end if;
6064 end if;
6066 -- A couple of special checks for Ada 83 mode. These checks are
6067 -- skipped if either entity is an operator in package Standard,
6068 -- or if either old or new instance is not from the source program.
6070 if Ada_Version = Ada_83
6071 and then Sloc (Old_Id) > Standard_Location
6072 and then Sloc (New_Id) > Standard_Location
6073 and then Comes_From_Source (Old_Id)
6074 and then Comes_From_Source (New_Id)
6075 then
6076 declare
6077 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6078 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6080 begin
6081 -- Explicit IN must be present or absent in both cases. This
6082 -- test is required only in the full conformance case.
6084 if In_Present (Old_Param) /= In_Present (New_Param)
6085 and then Ctype = Fully_Conformant
6086 then
6087 Conformance_Error
6088 ("\(Ada 83) IN must appear in both declarations",
6089 New_Formal);
6090 return;
6091 end if;
6093 -- Grouping (use of comma in param lists) must be the same
6094 -- This is where we catch a misconformance like:
6096 -- A, B : Integer
6097 -- A : Integer; B : Integer
6099 -- which are represented identically in the tree except
6100 -- for the setting of the flags More_Ids and Prev_Ids.
6102 if More_Ids (Old_Param) /= More_Ids (New_Param)
6103 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6104 then
6105 Conformance_Error
6106 ("\grouping of & does not match!", New_Formal);
6107 return;
6108 end if;
6109 end;
6110 end if;
6112 -- This label is required when skipping controlling formals
6114 <<Skip_Controlling_Formal>>
6116 Next_Formal (Old_Formal);
6117 Next_Formal (New_Formal);
6118 end loop;
6120 if Present (Old_Formal) then
6121 Conformance_Error ("\too few parameters!");
6122 return;
6124 elsif Present (New_Formal) then
6125 Conformance_Error ("\too many parameters!", New_Formal);
6126 return;
6127 end if;
6128 end Check_Conformance;
6130 -----------------------
6131 -- Check_Conventions --
6132 -----------------------
6134 procedure Check_Conventions (Typ : Entity_Id) is
6135 Ifaces_List : Elist_Id;
6137 procedure Check_Convention (Op : Entity_Id);
6138 -- Verify that the convention of inherited dispatching operation Op is
6139 -- consistent among all subprograms it overrides. In order to minimize
6140 -- the search, Search_From is utilized to designate a specific point in
6141 -- the list rather than iterating over the whole list once more.
6143 ----------------------
6144 -- Check_Convention --
6145 ----------------------
6147 procedure Check_Convention (Op : Entity_Id) is
6148 Op_Conv : constant Convention_Id := Convention (Op);
6149 Iface_Conv : Convention_Id;
6150 Iface_Elmt : Elmt_Id;
6151 Iface_Prim_Elmt : Elmt_Id;
6152 Iface_Prim : Entity_Id;
6154 begin
6155 Iface_Elmt := First_Elmt (Ifaces_List);
6156 while Present (Iface_Elmt) loop
6157 Iface_Prim_Elmt :=
6158 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6159 while Present (Iface_Prim_Elmt) loop
6160 Iface_Prim := Node (Iface_Prim_Elmt);
6161 Iface_Conv := Convention (Iface_Prim);
6163 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6164 and then Iface_Conv /= Op_Conv
6165 then
6166 Error_Msg_N
6167 ("inconsistent conventions in primitive operations", Typ);
6169 Error_Msg_Name_1 := Chars (Op);
6170 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6171 Error_Msg_Sloc := Sloc (Op);
6173 if Comes_From_Source (Op) or else No (Alias (Op)) then
6174 if No (Overridden_Operation (Op)) then
6175 Error_Msg_N ("\\primitive % defined #", Typ);
6176 else
6177 Error_Msg_N
6178 ("\\overriding operation % with "
6179 & "convention % defined #", Typ);
6180 end if;
6182 else pragma Assert (Present (Alias (Op)));
6183 Error_Msg_Sloc := Sloc (Alias (Op));
6184 Error_Msg_N ("\\inherited operation % with "
6185 & "convention % defined #", Typ);
6186 end if;
6188 Error_Msg_Name_1 := Chars (Op);
6189 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6190 Error_Msg_Sloc := Sloc (Iface_Prim);
6191 Error_Msg_N ("\\overridden operation % with "
6192 & "convention % defined #", Typ);
6194 -- Avoid cascading errors
6196 return;
6197 end if;
6199 Next_Elmt (Iface_Prim_Elmt);
6200 end loop;
6202 Next_Elmt (Iface_Elmt);
6203 end loop;
6204 end Check_Convention;
6206 -- Local variables
6208 Prim_Op : Entity_Id;
6209 Prim_Op_Elmt : Elmt_Id;
6211 -- Start of processing for Check_Conventions
6213 begin
6214 if not Has_Interfaces (Typ) then
6215 return;
6216 end if;
6218 Collect_Interfaces (Typ, Ifaces_List);
6220 -- The algorithm checks every overriding dispatching operation against
6221 -- all the corresponding overridden dispatching operations, detecting
6222 -- differences in conventions.
6224 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6225 while Present (Prim_Op_Elmt) loop
6226 Prim_Op := Node (Prim_Op_Elmt);
6228 -- A small optimization: skip the predefined dispatching operations
6229 -- since they always have the same convention.
6231 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6232 Check_Convention (Prim_Op);
6233 end if;
6235 Next_Elmt (Prim_Op_Elmt);
6236 end loop;
6237 end Check_Conventions;
6239 ------------------------------
6240 -- Check_Delayed_Subprogram --
6241 ------------------------------
6243 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6244 procedure Possible_Freeze (T : Entity_Id);
6245 -- T is the type of either a formal parameter or of the return type. If
6246 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6247 -- itself must be delayed.
6249 ---------------------
6250 -- Possible_Freeze --
6251 ---------------------
6253 procedure Possible_Freeze (T : Entity_Id) is
6254 Scop : constant Entity_Id := Scope (Designator);
6256 begin
6257 -- If the subprogram appears within a package instance (which may be
6258 -- the wrapper package of a subprogram instance) the freeze node for
6259 -- that package will freeze the subprogram at the proper place, so
6260 -- do not emit a freeze node for the subprogram, given that it may
6261 -- appear in the wrong scope.
6263 if Ekind (Scop) = E_Package
6264 and then not Comes_From_Source (Scop)
6265 and then Is_Generic_Instance (Scop)
6266 then
6267 null;
6269 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6270 Set_Has_Delayed_Freeze (Designator);
6272 elsif Is_Access_Type (T)
6273 and then Has_Delayed_Freeze (Designated_Type (T))
6274 and then not Is_Frozen (Designated_Type (T))
6275 then
6276 Set_Has_Delayed_Freeze (Designator);
6277 end if;
6278 end Possible_Freeze;
6280 -- Local variables
6282 F : Entity_Id;
6284 -- Start of processing for Check_Delayed_Subprogram
6286 begin
6287 -- All subprograms, including abstract subprograms, may need a freeze
6288 -- node if some formal type or the return type needs one.
6290 Possible_Freeze (Etype (Designator));
6291 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6293 -- Need delayed freeze if any of the formal types themselves need a
6294 -- delayed freeze and are not yet frozen.
6296 F := First_Formal (Designator);
6297 while Present (F) loop
6298 Possible_Freeze (Etype (F));
6299 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6300 Next_Formal (F);
6301 end loop;
6303 -- Mark functions that return by reference. Note that it cannot be done
6304 -- for delayed_freeze subprograms because the underlying returned type
6305 -- may not be known yet (for private types).
6307 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6308 Compute_Returns_By_Ref (Designator);
6309 end if;
6310 end Check_Delayed_Subprogram;
6312 ------------------------------------
6313 -- Check_Discriminant_Conformance --
6314 ------------------------------------
6316 procedure Check_Discriminant_Conformance
6317 (N : Node_Id;
6318 Prev : Entity_Id;
6319 Prev_Loc : Node_Id)
6321 Old_Discr : Entity_Id := First_Discriminant (Prev);
6322 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6323 New_Discr_Id : Entity_Id;
6324 New_Discr_Type : Entity_Id;
6326 procedure Conformance_Error (Msg : String; N : Node_Id);
6327 -- Post error message for conformance error on given node. Two messages
6328 -- are output. The first points to the previous declaration with a
6329 -- general "no conformance" message. The second is the detailed reason,
6330 -- supplied as Msg. The parameter N provide information for a possible
6331 -- & insertion in the message.
6333 -----------------------
6334 -- Conformance_Error --
6335 -----------------------
6337 procedure Conformance_Error (Msg : String; N : Node_Id) is
6338 begin
6339 Error_Msg_Sloc := Sloc (Prev_Loc);
6340 Error_Msg_N -- CODEFIX
6341 ("not fully conformant with declaration#!", N);
6342 Error_Msg_NE (Msg, N, N);
6343 end Conformance_Error;
6345 -- Start of processing for Check_Discriminant_Conformance
6347 begin
6348 while Present (Old_Discr) and then Present (New_Discr) loop
6349 New_Discr_Id := Defining_Identifier (New_Discr);
6351 -- The subtype mark of the discriminant on the full type has not
6352 -- been analyzed so we do it here. For an access discriminant a new
6353 -- type is created.
6355 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6356 New_Discr_Type :=
6357 Access_Definition (N, Discriminant_Type (New_Discr));
6359 else
6360 Find_Type (Discriminant_Type (New_Discr));
6361 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6363 -- Ada 2005: if the discriminant definition carries a null
6364 -- exclusion, create an itype to check properly for consistency
6365 -- with partial declaration.
6367 if Is_Access_Type (New_Discr_Type)
6368 and then Null_Exclusion_Present (New_Discr)
6369 then
6370 New_Discr_Type :=
6371 Create_Null_Excluding_Itype
6372 (T => New_Discr_Type,
6373 Related_Nod => New_Discr,
6374 Scope_Id => Current_Scope);
6375 end if;
6376 end if;
6378 if not Conforming_Types
6379 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6380 then
6381 Conformance_Error ("type of & does not match!", New_Discr_Id);
6382 return;
6383 else
6384 -- Treat the new discriminant as an occurrence of the old one,
6385 -- for navigation purposes, and fill in some semantic
6386 -- information, for completeness.
6388 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6389 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6390 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6391 end if;
6393 -- Names must match
6395 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6396 Conformance_Error ("name & does not match!", New_Discr_Id);
6397 return;
6398 end if;
6400 -- Default expressions must match
6402 declare
6403 NewD : constant Boolean :=
6404 Present (Expression (New_Discr));
6405 OldD : constant Boolean :=
6406 Present (Expression (Parent (Old_Discr)));
6408 begin
6409 if NewD or OldD then
6411 -- The old default value has been analyzed and expanded,
6412 -- because the current full declaration will have frozen
6413 -- everything before. The new default values have not been
6414 -- expanded, so expand now to check conformance.
6416 if NewD then
6417 Preanalyze_Spec_Expression
6418 (Expression (New_Discr), New_Discr_Type);
6419 end if;
6421 if not (NewD and OldD)
6422 or else not Fully_Conformant_Expressions
6423 (Expression (Parent (Old_Discr)),
6424 Expression (New_Discr))
6426 then
6427 Conformance_Error
6428 ("default expression for & does not match!",
6429 New_Discr_Id);
6430 return;
6431 end if;
6432 end if;
6433 end;
6435 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6437 if Ada_Version = Ada_83 then
6438 declare
6439 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6441 begin
6442 -- Grouping (use of comma in param lists) must be the same
6443 -- This is where we catch a misconformance like:
6445 -- A, B : Integer
6446 -- A : Integer; B : Integer
6448 -- which are represented identically in the tree except
6449 -- for the setting of the flags More_Ids and Prev_Ids.
6451 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6452 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6453 then
6454 Conformance_Error
6455 ("grouping of & does not match!", New_Discr_Id);
6456 return;
6457 end if;
6458 end;
6459 end if;
6461 Next_Discriminant (Old_Discr);
6462 Next (New_Discr);
6463 end loop;
6465 if Present (Old_Discr) then
6466 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6467 return;
6469 elsif Present (New_Discr) then
6470 Conformance_Error
6471 ("too many discriminants!", Defining_Identifier (New_Discr));
6472 return;
6473 end if;
6474 end Check_Discriminant_Conformance;
6476 -----------------------------------------
6477 -- Check_Formal_Subprogram_Conformance --
6478 -----------------------------------------
6480 procedure Check_Formal_Subprogram_Conformance
6481 (New_Id : Entity_Id;
6482 Old_Id : Entity_Id;
6483 Err_Loc : Node_Id;
6484 Errmsg : Boolean;
6485 Conforms : out Boolean)
6487 N : Node_Id;
6488 begin
6489 Conforms := True;
6491 if Is_Formal_Subprogram (Old_Id)
6492 or else Is_Formal_Subprogram (New_Id)
6493 or else (Is_Subprogram (New_Id)
6494 and then Present (Alias (New_Id))
6495 and then Is_Formal_Subprogram (Alias (New_Id)))
6496 then
6497 if Present (Err_Loc) then
6498 N := Err_Loc;
6499 else
6500 N := New_Id;
6501 end if;
6503 Conforms := False;
6505 if Errmsg then
6506 Error_Msg_Sloc := Sloc (Old_Id);
6507 Error_Msg_N ("not subtype conformant with declaration#!", N);
6508 Error_Msg_NE
6509 ("\formal subprograms are not subtype conformant "
6510 & "(RM 6.3.1 (17/3))", N, New_Id);
6511 end if;
6512 end if;
6513 end Check_Formal_Subprogram_Conformance;
6515 procedure Check_Formal_Subprogram_Conformance
6516 (New_Id : Entity_Id;
6517 Old_Id : Entity_Id;
6518 Err_Loc : Node_Id := Empty)
6520 Ignore : Boolean;
6521 begin
6522 Check_Formal_Subprogram_Conformance
6523 (New_Id, Old_Id, Err_Loc, True, Ignore);
6524 end Check_Formal_Subprogram_Conformance;
6526 ----------------------------
6527 -- Check_Fully_Conformant --
6528 ----------------------------
6530 procedure Check_Fully_Conformant
6531 (New_Id : Entity_Id;
6532 Old_Id : Entity_Id;
6533 Err_Loc : Node_Id := Empty)
6535 Result : Boolean;
6536 pragma Warnings (Off, Result);
6537 begin
6538 Check_Conformance
6539 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6540 end Check_Fully_Conformant;
6542 --------------------------
6543 -- Check_Limited_Return --
6544 --------------------------
6546 procedure Check_Limited_Return
6547 (N : Node_Id;
6548 Expr : Node_Id;
6549 R_Type : Entity_Id)
6551 begin
6552 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6553 -- replaced by anonymous access results. This is an incompatibility with
6554 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6555 -- controllable with special switch. ???
6557 -- A limited interface that is not immutably limited is OK
6559 if Is_Limited_Interface (R_Type)
6560 and then not Is_Concurrent_Interface (R_Type)
6561 then
6562 null;
6564 elsif Is_Limited_Type (R_Type)
6565 and then not Is_Interface (R_Type)
6566 and then not (Nkind (N) = N_Simple_Return_Statement
6567 and then Comes_From_Extended_Return_Statement (N))
6568 and then not In_Instance_Body
6569 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6570 then
6571 -- Error in Ada 2005
6573 if Ada_Version >= Ada_2005
6574 and then not Debug_Flag_Dot_L
6575 and then not GNAT_Mode
6576 then
6577 Error_Msg_N
6578 ("(Ada 2005) cannot copy object of a limited type "
6579 & "(RM-2005 6.5(5.5/2))", Expr);
6581 if Is_Limited_View (R_Type) then
6582 Error_Msg_N
6583 ("\return by reference not permitted in Ada 2005", Expr);
6584 end if;
6586 -- Warn in Ada 95 mode, to give folks a heads up about this
6587 -- incompatibility.
6589 -- In GNAT mode, this is just a warning, to allow it to be evilly
6590 -- turned off. Otherwise it is a real error.
6592 -- In a generic context, simplify the warning because it makes no
6593 -- sense to discuss pass-by-reference or copy.
6595 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6596 if Inside_A_Generic then
6597 Error_Msg_N
6598 ("return of limited object not permitted in Ada 2005 "
6599 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6601 elsif Is_Limited_View (R_Type) then
6602 Error_Msg_N
6603 ("return by reference not permitted in Ada 2005 "
6604 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6605 else
6606 Error_Msg_N
6607 ("cannot copy object of a limited type in Ada 2005 "
6608 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6609 end if;
6611 -- Ada 95 mode, and compatibility warnings disabled
6613 else
6614 pragma Assert (Ada_Version <= Ada_95);
6615 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6616 return; -- skip continuation messages below
6617 end if;
6619 if not Inside_A_Generic then
6620 Error_Msg_N
6621 ("\consider switching to return of access type", Expr);
6622 Explain_Limited_Type (R_Type, Expr);
6623 end if;
6624 end if;
6625 end Check_Limited_Return;
6627 ---------------------------
6628 -- Check_Mode_Conformant --
6629 ---------------------------
6631 procedure Check_Mode_Conformant
6632 (New_Id : Entity_Id;
6633 Old_Id : Entity_Id;
6634 Err_Loc : Node_Id := Empty;
6635 Get_Inst : Boolean := False)
6637 Result : Boolean;
6638 pragma Warnings (Off, Result);
6639 begin
6640 Check_Conformance
6641 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6642 end Check_Mode_Conformant;
6644 --------------------------------
6645 -- Check_Overriding_Indicator --
6646 --------------------------------
6648 procedure Check_Overriding_Indicator
6649 (Subp : Entity_Id;
6650 Overridden_Subp : Entity_Id;
6651 Is_Primitive : Boolean)
6653 Decl : Node_Id;
6654 Spec : Node_Id;
6656 begin
6657 -- No overriding indicator for literals
6659 if Ekind (Subp) = E_Enumeration_Literal then
6660 return;
6662 elsif Ekind (Subp) = E_Entry then
6663 Decl := Parent (Subp);
6665 -- No point in analyzing a malformed operator
6667 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6668 and then Error_Posted (Subp)
6669 then
6670 return;
6672 else
6673 Decl := Unit_Declaration_Node (Subp);
6674 end if;
6676 if Nkind (Decl) in N_Subprogram_Body
6677 | N_Subprogram_Body_Stub
6678 | N_Subprogram_Declaration
6679 | N_Abstract_Subprogram_Declaration
6680 | N_Subprogram_Renaming_Declaration
6681 then
6682 Spec := Specification (Decl);
6684 elsif Nkind (Decl) = N_Entry_Declaration then
6685 Spec := Decl;
6687 else
6688 return;
6689 end if;
6691 -- An overriding indication is illegal on a subprogram declared
6692 -- in a protected body, where there is no operation to override.
6694 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6695 and then Is_List_Member (Decl)
6696 and then Present (Parent (List_Containing (Decl)))
6697 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6698 then
6699 Error_Msg_N
6700 ("illegal overriding indication in protected body", Decl);
6701 return;
6702 end if;
6704 -- The overriding operation is type conformant with the overridden one,
6705 -- but the names of the formals are not required to match. If the names
6706 -- appear permuted in the overriding operation, this is a possible
6707 -- source of confusion that is worth diagnosing. Controlling formals
6708 -- often carry names that reflect the type, and it is not worthwhile
6709 -- requiring that their names match.
6711 if Present (Overridden_Subp)
6712 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6713 then
6714 declare
6715 Form1 : Entity_Id;
6716 Form2 : Entity_Id;
6718 begin
6719 Form1 := First_Formal (Subp);
6720 Form2 := First_Formal (Overridden_Subp);
6722 -- If the overriding operation is a synchronized operation, skip
6723 -- the first parameter of the overridden operation, which is
6724 -- implicit in the new one. If the operation is declared in the
6725 -- body it is not primitive and all formals must match.
6727 if Is_Concurrent_Type (Scope (Subp))
6728 and then Is_Tagged_Type (Scope (Subp))
6729 and then not Has_Completion (Scope (Subp))
6730 then
6731 Form2 := Next_Formal (Form2);
6732 end if;
6734 if Present (Form1) then
6735 Form1 := Next_Formal (Form1);
6736 Form2 := Next_Formal (Form2);
6737 end if;
6739 while Present (Form1) loop
6740 if not Is_Controlling_Formal (Form1)
6741 and then Present (Next_Formal (Form2))
6742 and then Chars (Form1) = Chars (Next_Formal (Form2))
6743 then
6744 Error_Msg_Node_2 := Alias (Overridden_Subp);
6745 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6746 Error_Msg_NE
6747 ("& does not match corresponding formal of&#",
6748 Form1, Form1);
6749 exit;
6750 end if;
6752 Next_Formal (Form1);
6753 Next_Formal (Form2);
6754 end loop;
6755 end;
6756 end if;
6758 -- If there is an overridden subprogram, then check that there is no
6759 -- "not overriding" indicator, and mark the subprogram as overriding.
6761 -- This is not done if the overridden subprogram is marked as hidden,
6762 -- which can occur for the case of inherited controlled operations
6763 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6764 -- subprogram is not itself hidden or we are within a generic instance,
6765 -- in which case the hidden flag may have been modified for the
6766 -- expansion of the instance.
6768 -- (Note: This condition could probably be simplified, leaving out the
6769 -- testing for the specific controlled cases, but it seems safer and
6770 -- clearer this way, and echoes similar special-case tests of this
6771 -- kind in other places.)
6773 if Present (Overridden_Subp)
6774 and then (not Is_Hidden (Overridden_Subp)
6775 or else
6776 (Chars (Overridden_Subp) in Name_Initialize
6777 | Name_Adjust
6778 | Name_Finalize
6779 and then Present (Alias (Overridden_Subp))
6780 and then (not Is_Hidden (Alias (Overridden_Subp))
6781 or else In_Instance)))
6782 then
6783 if Must_Not_Override (Spec) then
6784 Error_Msg_Sloc := Sloc (Overridden_Subp);
6786 if Ekind (Subp) = E_Entry then
6787 Error_Msg_NE
6788 ("entry & overrides inherited operation #", Spec, Subp);
6789 else
6790 Error_Msg_NE
6791 ("subprogram & overrides inherited operation #", Spec, Subp);
6792 end if;
6794 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6795 -- as an extension of Root_Controlled, and thus has a useless Adjust
6796 -- operation. This operation should not be inherited by other limited
6797 -- controlled types. An explicit Adjust for them is not overriding.
6799 elsif Must_Override (Spec)
6800 and then Chars (Overridden_Subp) = Name_Adjust
6801 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6802 and then Present (Alias (Overridden_Subp))
6803 and then In_Predefined_Unit (Alias (Overridden_Subp))
6804 then
6805 Get_Name_String
6806 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6807 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6809 elsif Is_Subprogram (Subp) then
6810 if Is_Init_Proc (Subp) then
6811 null;
6813 elsif No (Overridden_Operation (Subp)) then
6815 -- For entities generated by Derive_Subprograms the overridden
6816 -- operation is the inherited primitive (which is available
6817 -- through the attribute alias)
6819 if (Is_Dispatching_Operation (Subp)
6820 or else Is_Dispatching_Operation (Overridden_Subp))
6821 and then not Comes_From_Source (Overridden_Subp)
6822 and then Find_Dispatching_Type (Overridden_Subp) =
6823 Find_Dispatching_Type (Subp)
6824 and then Present (Alias (Overridden_Subp))
6825 and then Comes_From_Source (Alias (Overridden_Subp))
6826 then
6827 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6828 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6829 Set_Is_Ada_2022_Only (Subp,
6830 Is_Ada_2022_Only (Alias (Overridden_Subp)));
6832 else
6833 Set_Overridden_Operation (Subp, Overridden_Subp);
6834 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6835 Set_Is_Ada_2022_Only (Subp,
6836 Is_Ada_2022_Only (Overridden_Subp));
6837 end if;
6838 end if;
6839 end if;
6841 -- If primitive flag is set or this is a protected operation, then
6842 -- the operation is overriding at the point of its declaration, so
6843 -- warn if necessary. Otherwise it may have been declared before the
6844 -- operation it overrides and no check is required.
6846 if Style_Check
6847 and then not Must_Override (Spec)
6848 and then (Is_Primitive
6849 or else Ekind (Scope (Subp)) = E_Protected_Type)
6850 then
6851 Style.Missing_Overriding (Decl, Subp);
6852 end if;
6854 -- If Subp is an operator, it may override a predefined operation, if
6855 -- it is defined in the same scope as the type to which it applies.
6856 -- In that case Overridden_Subp is empty because of our implicit
6857 -- representation for predefined operators. We have to check whether the
6858 -- signature of Subp matches that of a predefined operator. Note that
6859 -- first argument provides the name of the operator, and the second
6860 -- argument the signature that may match that of a standard operation.
6861 -- If the indicator is overriding, then the operator must match a
6862 -- predefined signature, because we know already that there is no
6863 -- explicit overridden operation.
6865 elsif Chars (Subp) in Any_Operator_Name then
6866 if Must_Not_Override (Spec) then
6868 -- If this is not a primitive or a protected subprogram, then
6869 -- "not overriding" is illegal.
6871 if not Is_Primitive
6872 and then Ekind (Scope (Subp)) /= E_Protected_Type
6873 then
6874 Error_Msg_N ("overriding indicator only allowed "
6875 & "if subprogram is primitive", Subp);
6877 elsif Can_Override_Operator (Subp) then
6878 Error_Msg_NE
6879 ("subprogram& overrides predefined operator", Spec, Subp);
6880 end if;
6882 elsif Must_Override (Spec) then
6883 if No (Overridden_Operation (Subp))
6884 and then not Can_Override_Operator (Subp)
6885 then
6886 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6887 end if;
6889 elsif not Error_Posted (Subp)
6890 and then Style_Check
6891 and then Can_Override_Operator (Subp)
6892 and then not In_Predefined_Unit (Subp)
6893 then
6894 -- If style checks are enabled, indicate that the indicator is
6895 -- missing. However, at the point of declaration, the type of
6896 -- which this is a primitive operation may be private, in which
6897 -- case the indicator would be premature.
6899 if Has_Private_Declaration (Etype (Subp))
6900 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6901 then
6902 null;
6903 else
6904 Style.Missing_Overriding (Decl, Subp);
6905 end if;
6906 end if;
6908 elsif Must_Override (Spec) then
6909 if Ekind (Subp) = E_Entry then
6910 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6911 else
6912 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6913 end if;
6915 -- If the operation is marked "not overriding" and it's not primitive
6916 -- then an error is issued, unless this is an operation of a task or
6917 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6918 -- has been specified have already been checked above.
6920 elsif Must_Not_Override (Spec)
6921 and then not Is_Primitive
6922 and then Ekind (Subp) /= E_Entry
6923 and then Ekind (Scope (Subp)) /= E_Protected_Type
6924 then
6925 Error_Msg_N
6926 ("overriding indicator only allowed if subprogram is primitive",
6927 Subp);
6928 return;
6929 end if;
6930 end Check_Overriding_Indicator;
6932 -------------------
6933 -- Check_Returns --
6934 -------------------
6936 -- Note: this procedure needs to know far too much about how the expander
6937 -- messes with exceptions. The use of the flag Exception_Junk and the
6938 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6939 -- works, but is not very clean. It would be better if the expansion
6940 -- routines would leave Original_Node working nicely, and we could use
6941 -- Original_Node here to ignore all the peculiar expander messing ???
6943 procedure Check_Returns
6944 (HSS : Node_Id;
6945 Mode : Character;
6946 Err : out Boolean;
6947 Proc : Entity_Id := Empty)
6949 pragma Assert (Mode in 'F' | 'P');
6950 pragma Assert (if Mode = 'F' then No (Proc));
6951 Handler : Node_Id;
6953 procedure Check_Statement_Sequence (L : List_Id);
6954 -- Internal recursive procedure to check a list of statements for proper
6955 -- termination by a return statement (or a transfer of control or a
6956 -- compound statement that is itself internally properly terminated).
6958 ------------------------------
6959 -- Check_Statement_Sequence --
6960 ------------------------------
6962 procedure Check_Statement_Sequence (L : List_Id) is
6963 Last_Stm : Node_Id;
6964 Stm : Node_Id;
6965 Kind : Node_Kind;
6967 function Assert_False return Boolean;
6968 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6969 -- rewritten as a null statement when assertions are off. The assert
6970 -- is not active, but it is still enough to kill the warning.
6972 ------------------
6973 -- Assert_False --
6974 ------------------
6976 function Assert_False return Boolean is
6977 Orig : constant Node_Id := Original_Node (Last_Stm);
6979 begin
6980 if Nkind (Orig) = N_Pragma
6981 and then Pragma_Name (Orig) = Name_Assert
6982 and then not Error_Posted (Orig)
6983 then
6984 declare
6985 Arg : constant Node_Id :=
6986 First (Pragma_Argument_Associations (Orig));
6987 Exp : constant Node_Id := Expression (Arg);
6988 begin
6989 return Nkind (Exp) = N_Identifier
6990 and then Chars (Exp) = Name_False;
6991 end;
6993 else
6994 return False;
6995 end if;
6996 end Assert_False;
6998 -- Local variables
7000 Raise_Exception_Call : Boolean := False;
7001 -- Set True if statement sequence terminated by Raise_Exception call
7002 -- or a Reraise_Occurrence call.
7004 -- Start of processing for Check_Statement_Sequence
7006 begin
7007 -- Get last real statement
7009 Last_Stm := Last (L);
7011 -- Deal with digging out exception handler statement sequences that
7012 -- have been transformed by the local raise to goto optimization.
7013 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7014 -- optimization has occurred, we are looking at something like:
7016 -- begin
7017 -- original stmts in block
7019 -- exception \
7020 -- when excep1 => |
7021 -- goto L1; | omitted if No_Exception_Propagation
7022 -- when excep2 => |
7023 -- goto L2; /
7024 -- end;
7026 -- goto L3; -- skip handler when exception not raised
7028 -- <<L1>> -- target label for local exception
7029 -- begin
7030 -- estmts1
7031 -- end;
7033 -- goto L3;
7035 -- <<L2>>
7036 -- begin
7037 -- estmts2
7038 -- end;
7040 -- <<L3>>
7042 -- and what we have to do is to dig out the estmts1 and estmts2
7043 -- sequences (which were the original sequences of statements in
7044 -- the exception handlers) and check them.
7046 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7047 Stm := Last_Stm;
7048 loop
7049 Prev (Stm);
7050 exit when No (Stm);
7051 exit when Nkind (Stm) /= N_Block_Statement;
7052 exit when not Exception_Junk (Stm);
7053 Prev (Stm);
7054 exit when No (Stm);
7055 exit when Nkind (Stm) /= N_Label;
7056 exit when not Exception_Junk (Stm);
7057 Check_Statement_Sequence
7058 (Statements (Handled_Statement_Sequence (Next (Stm))));
7060 Prev (Stm);
7061 Last_Stm := Stm;
7062 exit when No (Stm);
7063 exit when Nkind (Stm) /= N_Goto_Statement;
7064 exit when not Exception_Junk (Stm);
7065 end loop;
7066 end if;
7068 -- Don't count pragmas
7070 while Nkind (Last_Stm) = N_Pragma
7072 -- Don't count call to SS_Release (can happen after
7073 -- Raise_Exception).
7075 or else
7076 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7077 and then
7078 Nkind (Name (Last_Stm)) = N_Identifier
7079 and then
7080 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7082 -- Don't count exception junk
7084 or else
7085 (Nkind (Last_Stm) in
7086 N_Goto_Statement | N_Label | N_Object_Declaration
7087 and then Exception_Junk (Last_Stm))
7088 or else Nkind (Last_Stm) in N_Push_xxx_Label | N_Pop_xxx_Label
7090 -- Inserted code, such as finalization calls, is irrelevant; we
7091 -- only need to check original source. If we see a transfer of
7092 -- control, we stop.
7094 or else (Is_Rewrite_Insertion (Last_Stm)
7095 and then not Is_Transfer (Last_Stm))
7096 loop
7097 Prev (Last_Stm);
7098 end loop;
7100 -- Here we have the "real" last statement
7102 Kind := Nkind (Last_Stm);
7104 -- Transfer of control, OK. Note that in the No_Return procedure
7105 -- case, we already diagnosed any explicit return statements, so
7106 -- we can treat them as OK in this context.
7108 if Is_Transfer (Last_Stm) then
7109 return;
7111 -- Check cases of explicit non-indirect procedure calls
7113 elsif Kind = N_Procedure_Call_Statement
7114 and then Is_Entity_Name (Name (Last_Stm))
7115 then
7116 -- Check call to Raise_Exception procedure which is treated
7117 -- specially, as is a call to Reraise_Occurrence.
7119 -- We suppress the warning in these cases since it is likely that
7120 -- the programmer really does not expect to deal with the case
7121 -- of Null_Occurrence, and thus would find a warning about a
7122 -- missing return curious, and raising Program_Error does not
7123 -- seem such a bad behavior if this does occur.
7125 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7126 -- behavior will be to raise Constraint_Error (see AI-329).
7128 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7129 or else
7130 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7131 then
7132 Raise_Exception_Call := True;
7134 -- For Raise_Exception call, test first argument, if it is
7135 -- an attribute reference for a 'Identity call, then we know
7136 -- that the call cannot possibly return.
7138 declare
7139 Arg : constant Node_Id :=
7140 Original_Node (First_Actual (Last_Stm));
7141 begin
7142 if Nkind (Arg) = N_Attribute_Reference
7143 and then Attribute_Name (Arg) = Name_Identity
7144 then
7145 return;
7146 end if;
7147 end;
7148 end if;
7150 -- If statement, need to look inside if there is an else and check
7151 -- each constituent statement sequence for proper termination.
7153 elsif Kind = N_If_Statement
7154 and then Present (Else_Statements (Last_Stm))
7155 then
7156 Check_Statement_Sequence (Then_Statements (Last_Stm));
7157 Check_Statement_Sequence (Else_Statements (Last_Stm));
7159 declare
7160 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7162 begin
7163 while Present (Elsif_Part) loop
7164 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7165 Next (Elsif_Part);
7166 end loop;
7167 end;
7169 return;
7171 -- Case statement, check each case for proper termination
7173 elsif Kind = N_Case_Statement then
7174 declare
7175 Case_Alt : Node_Id;
7176 begin
7177 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7178 while Present (Case_Alt) loop
7179 Check_Statement_Sequence (Statements (Case_Alt));
7180 Next_Non_Pragma (Case_Alt);
7181 end loop;
7182 end;
7184 return;
7186 -- Block statement, check its handled sequence of statements
7188 elsif Kind = N_Block_Statement then
7189 declare
7190 Err1 : Boolean;
7192 begin
7193 Check_Returns
7194 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7196 if Err1 then
7197 Err := True;
7198 end if;
7200 return;
7201 end;
7203 -- Loop statement. If there is an iteration scheme, we can definitely
7204 -- fall out of the loop. Similarly if there is an exit statement, we
7205 -- can fall out. In either case we need a following return.
7207 elsif Kind = N_Loop_Statement then
7208 if Present (Iteration_Scheme (Last_Stm))
7209 or else Has_Exit (Entity (Identifier (Last_Stm)))
7210 then
7211 null;
7213 -- A loop with no exit statement or iteration scheme is either
7214 -- an infinite loop, or it has some other exit (raise/return).
7215 -- In either case, no warning is required.
7217 else
7218 return;
7219 end if;
7221 -- Timed entry call, check entry call and delay alternatives
7223 -- Note: in expanded code, the timed entry call has been converted
7224 -- to a set of expanded statements on which the check will work
7225 -- correctly in any case.
7227 elsif Kind = N_Timed_Entry_Call then
7228 declare
7229 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7230 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7232 begin
7233 -- If statement sequence of entry call alternative is missing,
7234 -- then we can definitely fall through, and we post the error
7235 -- message on the entry call alternative itself.
7237 if No (Statements (ECA)) then
7238 Last_Stm := ECA;
7240 -- If statement sequence of delay alternative is missing, then
7241 -- we can definitely fall through, and we post the error
7242 -- message on the delay alternative itself.
7244 -- Note: if both ECA and DCA are missing the return, then we
7245 -- post only one message, should be enough to fix the bugs.
7246 -- If not we will get a message next time on the DCA when the
7247 -- ECA is fixed.
7249 elsif No (Statements (DCA)) then
7250 Last_Stm := DCA;
7252 -- Else check both statement sequences
7254 else
7255 Check_Statement_Sequence (Statements (ECA));
7256 Check_Statement_Sequence (Statements (DCA));
7257 return;
7258 end if;
7259 end;
7261 -- Conditional entry call, check entry call and else part
7263 -- Note: in expanded code, the conditional entry call has been
7264 -- converted to a set of expanded statements on which the check
7265 -- will work correctly in any case.
7267 elsif Kind = N_Conditional_Entry_Call then
7268 declare
7269 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7271 begin
7272 -- If statement sequence of entry call alternative is missing,
7273 -- then we can definitely fall through, and we post the error
7274 -- message on the entry call alternative itself.
7276 if No (Statements (ECA)) then
7277 Last_Stm := ECA;
7279 -- Else check statement sequence and else part
7281 else
7282 Check_Statement_Sequence (Statements (ECA));
7283 Check_Statement_Sequence (Else_Statements (Last_Stm));
7284 return;
7285 end if;
7286 end;
7287 end if;
7289 -- If we fall through, issue appropriate message
7291 if Mode = 'F' then
7293 -- Kill warning if last statement is a raise exception call,
7294 -- or a pragma Assert (False). Note that with assertions enabled,
7295 -- such a pragma has been converted into a raise exception call
7296 -- already, so the Assert_False is for the assertions off case.
7298 if not Raise_Exception_Call and then not Assert_False then
7300 -- In GNATprove mode, it is an error to have a missing return
7302 Error_Msg_Warn := SPARK_Mode /= On;
7304 -- Issue error message or warning
7306 Error_Msg_N
7307 ("RETURN statement missing following this statement<<!",
7308 Last_Stm);
7309 Error_Msg_N
7310 ("\Program_Error [<<!", Last_Stm);
7311 end if;
7313 -- Note: we set Err even though we have not issued a warning
7314 -- because we still have a case of a missing return. This is
7315 -- an extremely marginal case, probably will never be noticed
7316 -- but we might as well get it right.
7318 Err := True;
7320 -- Otherwise we have the case of a procedure marked No_Return
7322 else
7323 if not Raise_Exception_Call then
7324 if GNATprove_Mode then
7325 Error_Msg_N
7326 ("implied return after this statement would have raised "
7327 & "Program_Error", Last_Stm);
7329 -- In normal compilation mode, do not warn on a generated call
7330 -- (e.g. in the body of a renaming as completion).
7332 elsif Comes_From_Source (Last_Stm) then
7333 Error_Msg_N
7334 ("implied return after this statement will raise "
7335 & "Program_Error??", Last_Stm);
7336 end if;
7338 Error_Msg_Warn := SPARK_Mode /= On;
7339 Error_Msg_NE
7340 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7341 end if;
7343 declare
7344 RE : constant Node_Id :=
7345 Make_Raise_Program_Error (Sloc (Last_Stm),
7346 Reason => PE_Implicit_Return);
7347 begin
7348 Insert_After (Last_Stm, RE);
7349 Analyze (RE);
7350 end;
7351 end if;
7352 end Check_Statement_Sequence;
7354 -- Start of processing for Check_Returns
7356 begin
7357 Err := False;
7358 Check_Statement_Sequence (Statements (HSS));
7360 if Present (Exception_Handlers (HSS)) then
7361 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7362 while Present (Handler) loop
7363 Check_Statement_Sequence (Statements (Handler));
7364 Next_Non_Pragma (Handler);
7365 end loop;
7366 end if;
7367 end Check_Returns;
7369 ----------------------------
7370 -- Check_Subprogram_Order --
7371 ----------------------------
7373 procedure Check_Subprogram_Order (N : Node_Id) is
7375 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7376 -- This is used to check if S1 > S2 in the sense required by this test,
7377 -- for example nameab < namec, but name2 < name10.
7379 -----------------------------
7380 -- Subprogram_Name_Greater --
7381 -----------------------------
7383 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7384 L1, L2 : Positive;
7385 N1, N2 : Natural;
7387 begin
7388 -- Deal with special case where names are identical except for a
7389 -- numerical suffix. These are handled specially, taking the numeric
7390 -- ordering from the suffix into account.
7392 L1 := S1'Last;
7393 while S1 (L1) in '0' .. '9' loop
7394 L1 := L1 - 1;
7395 end loop;
7397 L2 := S2'Last;
7398 while S2 (L2) in '0' .. '9' loop
7399 L2 := L2 - 1;
7400 end loop;
7402 -- If non-numeric parts non-equal, do straight compare
7404 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7405 return S1 > S2;
7407 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7408 -- that a missing suffix is treated as numeric zero in this test.
7410 else
7411 N1 := 0;
7412 while L1 < S1'Last loop
7413 L1 := L1 + 1;
7414 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7415 end loop;
7417 N2 := 0;
7418 while L2 < S2'Last loop
7419 L2 := L2 + 1;
7420 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7421 end loop;
7423 return N1 > N2;
7424 end if;
7425 end Subprogram_Name_Greater;
7427 -- Start of processing for Check_Subprogram_Order
7429 begin
7430 -- Check body in alpha order if this is option
7432 if Style_Check
7433 and then Style_Check_Order_Subprograms
7434 and then Nkind (N) = N_Subprogram_Body
7435 and then Comes_From_Source (N)
7436 and then In_Extended_Main_Source_Unit (N)
7437 then
7438 declare
7439 LSN : String_Ptr
7440 renames Scope_Stack.Table
7441 (Scope_Stack.Last).Last_Subprogram_Name;
7443 Body_Id : constant Entity_Id :=
7444 Defining_Entity (Specification (N));
7446 begin
7447 Get_Decoded_Name_String (Chars (Body_Id));
7449 if LSN /= null then
7450 if Subprogram_Name_Greater
7451 (LSN.all, Name_Buffer (1 .. Name_Len))
7452 then
7453 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7454 end if;
7456 Free (LSN);
7457 end if;
7459 LSN := new String'(Name_Buffer (1 .. Name_Len));
7460 end;
7461 end if;
7462 end Check_Subprogram_Order;
7464 ------------------------------
7465 -- Check_Subtype_Conformant --
7466 ------------------------------
7468 procedure Check_Subtype_Conformant
7469 (New_Id : Entity_Id;
7470 Old_Id : Entity_Id;
7471 Err_Loc : Node_Id := Empty;
7472 Skip_Controlling_Formals : Boolean := False;
7473 Get_Inst : Boolean := False)
7475 Result : Boolean;
7476 pragma Warnings (Off, Result);
7477 begin
7478 Check_Conformance
7479 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7480 Skip_Controlling_Formals => Skip_Controlling_Formals,
7481 Get_Inst => Get_Inst);
7482 end Check_Subtype_Conformant;
7484 -----------------------------------
7485 -- Check_Synchronized_Overriding --
7486 -----------------------------------
7488 procedure Check_Synchronized_Overriding
7489 (Def_Id : Entity_Id;
7490 Overridden_Subp : out Entity_Id)
7492 Ifaces_List : Elist_Id;
7493 In_Scope : Boolean;
7494 Typ : Entity_Id;
7496 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7497 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7498 -- subprogram is implemented by a protected procedure or entry,
7499 -- its first parameter must be out, in out, or access-to-variable.
7501 function Matches_Prefixed_View_Profile
7502 (Prim_Params : List_Id;
7503 Iface_Params : List_Id) return Boolean;
7504 -- Determine whether a subprogram's parameter profile Prim_Params
7505 -- matches that of a potentially overridden interface subprogram
7506 -- Iface_Params. Also determine if the type of first parameter of
7507 -- Iface_Params is an implemented interface.
7509 ----------------------
7510 -- Is_Valid_Formal --
7511 ----------------------
7513 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7514 begin
7515 return
7516 Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
7517 or else
7518 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7519 and then not Constant_Present (Parameter_Type (Parent (F))));
7520 end Is_Valid_Formal;
7522 -----------------------------------
7523 -- Matches_Prefixed_View_Profile --
7524 -----------------------------------
7526 function Matches_Prefixed_View_Profile
7527 (Prim_Params : List_Id;
7528 Iface_Params : List_Id) return Boolean
7530 function Is_Implemented
7531 (Ifaces_List : Elist_Id;
7532 Iface : Entity_Id) return Boolean;
7533 -- Determine if Iface is implemented by the current task or
7534 -- protected type.
7536 --------------------
7537 -- Is_Implemented --
7538 --------------------
7540 function Is_Implemented
7541 (Ifaces_List : Elist_Id;
7542 Iface : Entity_Id) return Boolean
7544 Iface_Elmt : Elmt_Id;
7546 begin
7547 Iface_Elmt := First_Elmt (Ifaces_List);
7548 while Present (Iface_Elmt) loop
7549 if Node (Iface_Elmt) = Iface then
7550 return True;
7551 end if;
7553 Next_Elmt (Iface_Elmt);
7554 end loop;
7556 return False;
7557 end Is_Implemented;
7559 -- Local variables
7561 Iface_Id : Entity_Id;
7562 Iface_Param : Node_Id;
7563 Iface_Typ : Entity_Id;
7564 Prim_Id : Entity_Id;
7565 Prim_Param : Node_Id;
7566 Prim_Typ : Entity_Id;
7568 -- Start of processing for Matches_Prefixed_View_Profile
7570 begin
7571 Iface_Param := First (Iface_Params);
7572 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7574 if Is_Access_Type (Iface_Typ) then
7575 Iface_Typ := Designated_Type (Iface_Typ);
7576 end if;
7578 Prim_Param := First (Prim_Params);
7580 -- The first parameter of the potentially overridden subprogram must
7581 -- be an interface implemented by Prim.
7583 if not Is_Interface (Iface_Typ)
7584 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7585 then
7586 return False;
7587 end if;
7589 -- The checks on the object parameters are done, so move on to the
7590 -- rest of the parameters.
7592 if not In_Scope then
7593 Next (Prim_Param);
7594 end if;
7596 Next (Iface_Param);
7597 while Present (Iface_Param) and then Present (Prim_Param) loop
7598 Iface_Id := Defining_Identifier (Iface_Param);
7599 Iface_Typ := Find_Parameter_Type (Iface_Param);
7601 Prim_Id := Defining_Identifier (Prim_Param);
7602 Prim_Typ := Find_Parameter_Type (Prim_Param);
7604 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7605 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7606 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7607 then
7608 Iface_Typ := Designated_Type (Iface_Typ);
7609 Prim_Typ := Designated_Type (Prim_Typ);
7610 end if;
7612 -- Case of multiple interface types inside a parameter profile
7614 -- (Obj_Param : in out Iface; ...; Param : Iface)
7616 -- If the interface type is implemented, then the matching type in
7617 -- the primitive should be the implementing record type.
7619 if Ekind (Iface_Typ) = E_Record_Type
7620 and then Is_Interface (Iface_Typ)
7621 and then Is_Implemented (Ifaces_List, Iface_Typ)
7622 then
7623 if Prim_Typ /= Typ then
7624 return False;
7625 end if;
7627 -- The two parameters must be both mode and subtype conformant
7629 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7630 or else not
7631 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7632 then
7633 return False;
7634 end if;
7636 Next (Iface_Param);
7637 Next (Prim_Param);
7638 end loop;
7640 -- One of the two lists contains more parameters than the other
7642 if Present (Iface_Param) or else Present (Prim_Param) then
7643 return False;
7644 end if;
7646 return True;
7647 end Matches_Prefixed_View_Profile;
7649 -- Start of processing for Check_Synchronized_Overriding
7651 begin
7652 Overridden_Subp := Empty;
7654 -- Def_Id must be an entry or a subprogram. We should skip predefined
7655 -- primitives internally generated by the front end; however at this
7656 -- stage predefined primitives are still not fully decorated. As a
7657 -- minor optimization we skip here internally generated subprograms.
7659 if (Ekind (Def_Id) /= E_Entry
7660 and then Ekind (Def_Id) /= E_Function
7661 and then Ekind (Def_Id) /= E_Procedure)
7662 or else not Comes_From_Source (Def_Id)
7663 then
7664 return;
7665 end if;
7667 -- Search for the concurrent declaration since it contains the list of
7668 -- all implemented interfaces. In this case, the subprogram is declared
7669 -- within the scope of a protected or a task type.
7671 if Present (Scope (Def_Id))
7672 and then Is_Concurrent_Type (Scope (Def_Id))
7673 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7674 then
7675 Typ := Scope (Def_Id);
7676 In_Scope := True;
7678 -- The enclosing scope is not a synchronized type and the subprogram
7679 -- has no formals.
7681 elsif No (First_Formal (Def_Id)) then
7682 return;
7684 -- The subprogram has formals and hence it may be a primitive of a
7685 -- concurrent type.
7687 else
7688 Typ := Etype (First_Formal (Def_Id));
7690 if Is_Access_Type (Typ) then
7691 Typ := Directly_Designated_Type (Typ);
7692 end if;
7694 if Is_Concurrent_Type (Typ)
7695 and then not Is_Generic_Actual_Type (Typ)
7696 then
7697 In_Scope := False;
7699 -- This case occurs when the concurrent type is declared within a
7700 -- generic unit. As a result the corresponding record has been built
7701 -- and used as the type of the first formal, we just have to retrieve
7702 -- the corresponding concurrent type.
7704 elsif Is_Concurrent_Record_Type (Typ)
7705 and then not Is_Class_Wide_Type (Typ)
7706 and then Present (Corresponding_Concurrent_Type (Typ))
7707 then
7708 Typ := Corresponding_Concurrent_Type (Typ);
7709 In_Scope := False;
7711 else
7712 return;
7713 end if;
7714 end if;
7716 -- There is no overriding to check if this is an inherited operation in
7717 -- a type derivation for a generic actual.
7719 Collect_Interfaces (Typ, Ifaces_List);
7721 if Is_Empty_Elmt_List (Ifaces_List) then
7722 return;
7723 end if;
7725 -- Determine whether entry or subprogram Def_Id overrides a primitive
7726 -- operation that belongs to one of the interfaces in Ifaces_List.
7728 declare
7729 Candidate : Entity_Id := Empty;
7730 Hom : Entity_Id := Empty;
7731 Subp : Entity_Id := Empty;
7733 begin
7734 -- Traverse the homonym chain, looking for a potentially overridden
7735 -- subprogram that belongs to an implemented interface.
7737 Hom := Current_Entity_In_Scope (Def_Id);
7738 while Present (Hom) loop
7739 Subp := Hom;
7741 if Subp = Def_Id
7742 or else not Is_Overloadable (Subp)
7743 or else not Is_Primitive (Subp)
7744 or else not Is_Dispatching_Operation (Subp)
7745 or else No (Find_Dispatching_Type (Subp))
7746 or else not Is_Interface (Find_Dispatching_Type (Subp))
7747 then
7748 null;
7750 -- Entries and procedures can override abstract or null interface
7751 -- procedures.
7753 elsif Ekind (Def_Id) in E_Entry | E_Procedure
7754 and then Ekind (Subp) = E_Procedure
7755 and then Matches_Prefixed_View_Profile
7756 (Parameter_Specifications (Parent (Def_Id)),
7757 Parameter_Specifications (Parent (Subp)))
7758 then
7759 Candidate := Subp;
7761 -- For an overridden subprogram Subp, check whether the mode
7762 -- of its first parameter is correct depending on the kind of
7763 -- synchronized type.
7765 declare
7766 Formal : constant Node_Id := First_Formal (Candidate);
7768 begin
7769 -- In order for an entry or a protected procedure to
7770 -- override, the first parameter of the overridden routine
7771 -- must be of mode "out", "in out", or access-to-variable.
7773 if Ekind (Candidate) in E_Entry | E_Procedure
7774 and then Is_Protected_Type (Typ)
7775 and then not Is_Valid_Formal (Formal)
7776 then
7777 null;
7779 -- All other cases are OK since a task entry or routine does
7780 -- not have a restriction on the mode of the first parameter
7781 -- of the overridden interface routine.
7783 else
7784 Overridden_Subp := Candidate;
7785 return;
7786 end if;
7787 end;
7789 -- Functions can override abstract interface functions. Return
7790 -- types must be subtype conformant.
7792 elsif Ekind (Def_Id) = E_Function
7793 and then Ekind (Subp) = E_Function
7794 and then Matches_Prefixed_View_Profile
7795 (Parameter_Specifications (Parent (Def_Id)),
7796 Parameter_Specifications (Parent (Subp)))
7797 and then Conforming_Types
7798 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7799 then
7800 Candidate := Subp;
7802 -- If an inherited subprogram is implemented by a protected
7803 -- function, then the first parameter of the inherited
7804 -- subprogram shall be of mode in, but not an access-to-
7805 -- variable parameter (RM 9.4(11/9)).
7807 if Present (First_Formal (Subp))
7808 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7809 and then
7810 (not Is_Access_Type (Etype (First_Formal (Subp)))
7811 or else
7812 Is_Access_Constant (Etype (First_Formal (Subp))))
7813 then
7814 Overridden_Subp := Subp;
7815 return;
7816 end if;
7817 end if;
7819 Hom := Homonym (Hom);
7820 end loop;
7822 -- After examining all candidates for overriding, we are left with
7823 -- the best match, which is a mode-incompatible interface routine.
7825 if In_Scope and then Present (Candidate) then
7826 Error_Msg_PT (Def_Id, Candidate);
7827 end if;
7829 Overridden_Subp := Candidate;
7830 return;
7831 end;
7832 end Check_Synchronized_Overriding;
7834 ---------------------------
7835 -- Check_Type_Conformant --
7836 ---------------------------
7838 procedure Check_Type_Conformant
7839 (New_Id : Entity_Id;
7840 Old_Id : Entity_Id;
7841 Err_Loc : Node_Id := Empty)
7843 Result : Boolean;
7844 pragma Warnings (Off, Result);
7845 begin
7846 Check_Conformance
7847 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7848 end Check_Type_Conformant;
7850 -----------------------------
7851 -- Check_Untagged_Equality --
7852 -----------------------------
7854 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
7855 Eq_Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
7856 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
7858 procedure Freezing_Point_Warning (N : Node_Id; S : String);
7859 -- Output a warning about the freezing point N of Typ
7861 function Is_Actual_Of_Instantiation
7862 (E : Entity_Id;
7863 Inst : Node_Id) return Boolean;
7864 -- Return True if E is an actual parameter of instantiation Inst
7866 -----------------------------------
7867 -- Output_Freezing_Point_Warning --
7868 -----------------------------------
7870 procedure Freezing_Point_Warning (N : Node_Id; S : String) is
7871 begin
7872 Error_Msg_String (1 .. S'Length) := S;
7873 Error_Msg_Strlen := S'Length;
7875 if Ada_Version >= Ada_2012 then
7876 Error_Msg_NE ("type& is frozen by ~??", N, Typ);
7877 Error_Msg_N
7878 ("\an equality operator cannot be declared after this point??",
7881 else
7882 Error_Msg_NE ("type& is frozen by ~ (Ada 2012)?y?", N, Typ);
7883 Error_Msg_N
7884 ("\an equality operator cannot be declared after this point"
7885 & " (Ada 2012)?y?", N);
7886 end if;
7887 end Freezing_Point_Warning;
7889 --------------------------------
7890 -- Is_Actual_Of_Instantiation --
7891 --------------------------------
7893 function Is_Actual_Of_Instantiation
7894 (E : Entity_Id;
7895 Inst : Node_Id) return Boolean
7897 Assoc : Node_Id;
7899 begin
7900 if Present (Generic_Associations (Inst)) then
7901 Assoc := First (Generic_Associations (Inst));
7903 while Present (Assoc) loop
7904 if Present (Explicit_Generic_Actual_Parameter (Assoc))
7905 and then
7906 Is_Entity_Name (Explicit_Generic_Actual_Parameter (Assoc))
7907 and then
7908 Entity (Explicit_Generic_Actual_Parameter (Assoc)) = E
7909 then
7910 return True;
7911 end if;
7913 Next (Assoc);
7914 end loop;
7915 end if;
7917 return False;
7918 end Is_Actual_Of_Instantiation;
7920 -- Local variable
7922 Decl : Node_Id;
7924 -- Start of processing for Check_Untagged_Equality
7926 begin
7927 -- This check applies only if we have a subprogram declaration or a
7928 -- subprogram body that is not a completion, for an untagged record
7929 -- type, and that is conformant with the predefined operator.
7931 if (Nkind (Eq_Decl) /= N_Subprogram_Declaration
7932 and then not (Nkind (Eq_Decl) = N_Subprogram_Body
7933 and then Acts_As_Spec (Eq_Decl)))
7934 or else not Is_Record_Type (Typ)
7935 or else Is_Tagged_Type (Typ)
7936 or else not Is_User_Defined_Equality (Eq_Op)
7937 then
7938 return;
7939 end if;
7941 -- In Ada 2012 case, we will output errors or warnings depending on
7942 -- the setting of debug flag -gnatd.E.
7944 if Ada_Version >= Ada_2012 then
7945 Error_Msg_Warn := Debug_Flag_Dot_EE;
7947 -- In earlier versions of Ada, nothing to do unless we are warning on
7948 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
7950 else
7951 if not Warn_On_Ada_2012_Compatibility then
7952 return;
7953 end if;
7954 end if;
7956 -- Cases where the type has already been frozen
7958 if Is_Frozen (Typ) then
7960 -- The check applies to a primitive operation, so check that type
7961 -- and equality operation are in the same scope.
7963 if Scope (Typ) /= Current_Scope then
7964 return;
7966 -- If the type is a generic actual (sub)type, the operation is not
7967 -- primitive either because the base type is declared elsewhere.
7969 elsif Is_Generic_Actual_Type (Typ) then
7970 return;
7972 -- Here we may have an error of declaration after freezing, but we
7973 -- must make sure not to flag the equality operator itself causing
7974 -- the freezing when it is a subprogram body.
7976 else
7977 Decl := Next (Declaration_Node (Typ));
7979 while Present (Decl) and then Decl /= Eq_Decl loop
7981 -- The declaration of an object of the type
7983 if Nkind (Decl) = N_Object_Declaration
7984 and then Etype (Defining_Identifier (Decl)) = Typ
7985 then
7986 Freezing_Point_Warning (Decl, "declaration");
7987 exit;
7989 -- The instantiation of a generic on the type
7991 elsif Nkind (Decl) in N_Generic_Instantiation
7992 and then Is_Actual_Of_Instantiation (Typ, Decl)
7993 then
7994 Freezing_Point_Warning (Decl, "instantiation");
7995 exit;
7997 -- A noninstance proper body, body stub or entry body
7999 elsif Nkind (Decl) in N_Proper_Body
8000 | N_Body_Stub
8001 | N_Entry_Body
8002 and then not Is_Generic_Instance (Defining_Entity (Decl))
8003 then
8004 Freezing_Point_Warning (Decl, "body");
8005 exit;
8007 -- If we have reached the freeze node and immediately after we
8008 -- have the body or generated code for the body, then it is the
8009 -- body that caused the freezing and this is legal.
8011 elsif Nkind (Decl) = N_Freeze_Entity
8012 and then Entity (Decl) = Typ
8013 and then (Next (Decl) = Eq_Decl
8014 or else
8015 Sloc (Next (Decl)) = Sloc (Eq_Decl))
8016 then
8017 return;
8018 end if;
8020 Next (Decl);
8021 end loop;
8023 -- Here we have a definite error of declaration after freezing
8025 if Ada_Version >= Ada_2012 then
8026 Error_Msg_NE
8027 ("equality operator must be declared before type & is "
8028 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8030 -- In Ada 2012 mode with error turned to warning, output one
8031 -- more warning to warn that the equality operation may not
8032 -- compose. This is the consequence of ignoring the error.
8034 if Error_Msg_Warn then
8035 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8036 end if;
8038 else
8039 Error_Msg_NE
8040 ("equality operator must be declared before type& is "
8041 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8042 end if;
8044 -- If we have found no freezing point and the declaration of the
8045 -- operator could not be reached from that of the type and we are
8046 -- in a package body, this must be because the type is declared
8047 -- in the spec of the package. Add a message tailored to this.
8049 if No (Decl) and then In_Package_Body (Scope (Typ)) then
8050 if Ada_Version >= Ada_2012 then
8051 if Nkind (Eq_Decl) = N_Subprogram_Body then
8052 Error_Msg_N
8053 ("\put declaration in package spec<<", Eq_Op);
8054 else
8055 Error_Msg_N
8056 ("\move declaration to package spec<<", Eq_Op);
8057 end if;
8059 else
8060 if Nkind (Eq_Decl) = N_Subprogram_Body then
8061 Error_Msg_N
8062 ("\put declaration in package spec (Ada 2012)?y?",
8063 Eq_Op);
8064 else
8065 Error_Msg_N
8066 ("\move declaration to package spec (Ada 2012)?y?",
8067 Eq_Op);
8068 end if;
8069 end if;
8070 end if;
8071 end if;
8073 -- Now check for AI12-0352: the declaration of a user-defined primitive
8074 -- equality operation for a record type T is illegal if it occurs after
8075 -- a type has been derived from T.
8077 else
8078 Decl := Next (Declaration_Node (Typ));
8080 while Present (Decl) and then Decl /= Eq_Decl loop
8081 if Nkind (Decl) = N_Full_Type_Declaration
8082 and then Etype (Defining_Identifier (Decl)) = Typ
8083 then
8084 Error_Msg_N
8085 ("equality operator cannot appear after derivation", Eq_Op);
8086 Error_Msg_NE
8087 ("an equality operator for& cannot be declared after "
8088 & "this point??",
8089 Decl, Typ);
8090 end if;
8092 Next (Decl);
8093 end loop;
8094 end if;
8095 end Check_Untagged_Equality;
8097 ---------------------------
8098 -- Can_Override_Operator --
8099 ---------------------------
8101 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
8102 Typ : Entity_Id;
8104 begin
8105 -- Return False if not an operator. We test the name rather than testing
8106 -- that the Nkind is N_Defining_Operator_Symbol, because there are cases
8107 -- where an operator entity can be an N_Defining_Identifier (such as for
8108 -- function instantiations).
8110 if Chars (Subp) not in Any_Operator_Name then
8111 return False;
8113 else
8114 Typ := Base_Type (Etype (First_Formal (Subp)));
8116 -- Check explicitly that the operation is a primitive of the type
8118 return Operator_Matches_Spec (Subp, Subp)
8119 and then not Is_Generic_Type (Typ)
8120 and then Scope (Subp) = Scope (Typ)
8121 and then not Is_Class_Wide_Type (Typ);
8122 end if;
8123 end Can_Override_Operator;
8125 ----------------------
8126 -- Conforming_Types --
8127 ----------------------
8129 function Conforming_Types
8130 (T1 : Entity_Id;
8131 T2 : Entity_Id;
8132 Ctype : Conformance_Type;
8133 Get_Inst : Boolean := False) return Boolean
8135 function Base_Types_Match
8136 (Typ_1 : Entity_Id;
8137 Typ_2 : Entity_Id) return Boolean;
8138 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
8139 -- in different scopes (e.g. parent and child instances), then verify
8140 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
8141 -- the same subtype chain. The whole purpose of this procedure is to
8142 -- prevent spurious ambiguities in an instantiation that may arise if
8143 -- two distinct generic types are instantiated with the same actual.
8145 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
8146 -- An access parameter can designate an incomplete type. If the
8147 -- incomplete type is the limited view of a type from a limited_
8148 -- with_clause, check whether the non-limited view is available.
8149 -- If it is a (non-limited) incomplete type, get the full view.
8151 function Matches_Limited_With_View
8152 (Typ_1 : Entity_Id;
8153 Typ_2 : Entity_Id) return Boolean;
8154 -- Returns True if and only if either Typ_1 denotes a limited view of
8155 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
8156 -- the limited with view of a type is used in a subprogram declaration
8157 -- and the subprogram body is in the scope of a regular with clause for
8158 -- the same unit. In such a case, the two type entities are considered
8159 -- identical for purposes of conformance checking.
8161 ----------------------
8162 -- Base_Types_Match --
8163 ----------------------
8165 function Base_Types_Match
8166 (Typ_1 : Entity_Id;
8167 Typ_2 : Entity_Id) return Boolean
8169 Base_1 : constant Entity_Id := Base_Type (Typ_1);
8170 Base_2 : constant Entity_Id := Base_Type (Typ_2);
8172 begin
8173 if Typ_1 = Typ_2 then
8174 return True;
8176 elsif Base_1 = Base_2 then
8178 -- The following is too permissive. A more precise test should
8179 -- check that the generic actual is an ancestor subtype of the
8180 -- other ???.
8182 -- See code in Find_Corresponding_Spec that applies an additional
8183 -- filter to handle accidental amiguities in instances.
8185 return
8186 not Is_Generic_Actual_Type (Typ_1)
8187 or else not Is_Generic_Actual_Type (Typ_2)
8188 or else Scope (Typ_1) /= Scope (Typ_2);
8190 -- If Typ_2 is a generic actual type it is declared as the subtype of
8191 -- the actual. If that actual is itself a subtype we need to use its
8192 -- own base type to check for compatibility.
8194 elsif Ekind (Base_2) = Ekind (Typ_2)
8195 and then Base_1 = Base_Type (Base_2)
8196 then
8197 return True;
8199 elsif Ekind (Base_1) = Ekind (Typ_1)
8200 and then Base_2 = Base_Type (Base_1)
8201 then
8202 return True;
8204 else
8205 return False;
8206 end if;
8207 end Base_Types_Match;
8209 --------------------------
8210 -- Find_Designated_Type --
8211 --------------------------
8213 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
8214 Desig : Entity_Id;
8216 begin
8217 Desig := Directly_Designated_Type (Typ);
8219 if Ekind (Desig) = E_Incomplete_Type then
8221 -- If regular incomplete type, get full view if available
8223 if Present (Full_View (Desig)) then
8224 Desig := Full_View (Desig);
8226 -- If limited view of a type, get non-limited view if available,
8227 -- and check again for a regular incomplete type.
8229 elsif Present (Non_Limited_View (Desig)) then
8230 Desig := Get_Full_View (Non_Limited_View (Desig));
8231 end if;
8232 end if;
8234 return Desig;
8235 end Find_Designated_Type;
8237 -------------------------------
8238 -- Matches_Limited_With_View --
8239 -------------------------------
8241 function Matches_Limited_With_View
8242 (Typ_1 : Entity_Id;
8243 Typ_2 : Entity_Id) return Boolean
8245 function Is_Matching_Limited_View
8246 (Typ : Entity_Id;
8247 View : Entity_Id) return Boolean;
8248 -- Determine whether non-limited view View denotes type Typ in some
8249 -- conformant fashion.
8251 ------------------------------
8252 -- Is_Matching_Limited_View --
8253 ------------------------------
8255 function Is_Matching_Limited_View
8256 (Typ : Entity_Id;
8257 View : Entity_Id) return Boolean
8259 Root_Typ : Entity_Id;
8260 Root_View : Entity_Id;
8262 begin
8263 -- The non-limited view directly denotes the type
8265 if Typ = View then
8266 return True;
8268 -- The type is a subtype of the non-limited view
8270 elsif Is_Subtype_Of (Typ, View) then
8271 return True;
8273 -- Both the non-limited view and the type denote class-wide types
8275 elsif Is_Class_Wide_Type (Typ)
8276 and then Is_Class_Wide_Type (View)
8277 then
8278 Root_Typ := Root_Type (Typ);
8279 Root_View := Root_Type (View);
8281 if Root_Typ = Root_View then
8282 return True;
8284 -- An incomplete tagged type and its full view may receive two
8285 -- distinct class-wide types when the related package has not
8286 -- been analyzed yet.
8288 -- package Pack is
8289 -- type T is tagged; -- CW_1
8290 -- type T is tagged null record; -- CW_2
8291 -- end Pack;
8293 -- This is because the package lacks any semantic information
8294 -- that may eventually link both views of T. As a consequence,
8295 -- a client of the limited view of Pack will see CW_2 while a
8296 -- client of the non-limited view of Pack will see CW_1.
8298 elsif Is_Incomplete_Type (Root_Typ)
8299 and then Present (Full_View (Root_Typ))
8300 and then Full_View (Root_Typ) = Root_View
8301 then
8302 return True;
8304 elsif Is_Incomplete_Type (Root_View)
8305 and then Present (Full_View (Root_View))
8306 and then Full_View (Root_View) = Root_Typ
8307 then
8308 return True;
8309 end if;
8310 end if;
8312 return False;
8313 end Is_Matching_Limited_View;
8315 -- Start of processing for Matches_Limited_With_View
8317 begin
8318 -- In some cases a type imported through a limited_with clause, and
8319 -- its non-limited view are both visible, for example in an anonymous
8320 -- access-to-class-wide type in a formal, or when building the body
8321 -- for a subprogram renaming after the subprogram has been frozen.
8322 -- In these cases both entities designate the same type. In addition,
8323 -- if one of them is an actual in an instance, it may be a subtype of
8324 -- the non-limited view of the other.
8326 if From_Limited_With (Typ_1)
8327 and then From_Limited_With (Typ_2)
8328 and then Available_View (Typ_1) = Available_View (Typ_2)
8329 then
8330 return True;
8332 elsif From_Limited_With (Typ_1) then
8333 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8335 elsif From_Limited_With (Typ_2) then
8336 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8338 else
8339 return False;
8340 end if;
8341 end Matches_Limited_With_View;
8343 -- Local variables
8345 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8347 Type_1 : Entity_Id := T1;
8348 Type_2 : Entity_Id := T2;
8350 -- Start of processing for Conforming_Types
8352 begin
8353 -- The context is an instance association for a formal access-to-
8354 -- subprogram type; the formal parameter types require mapping because
8355 -- they may denote other formal parameters of the generic unit.
8357 if Get_Inst then
8358 Type_1 := Get_Instance_Of (T1);
8359 Type_2 := Get_Instance_Of (T2);
8360 end if;
8362 -- If one of the types is a view of the other introduced by a limited
8363 -- with clause, treat these as conforming for all purposes.
8365 if Matches_Limited_With_View (T1, T2) then
8366 return True;
8368 elsif Base_Types_Match (Type_1, Type_2) then
8369 if Ctype <= Mode_Conformant then
8370 return True;
8372 else
8373 return
8374 Subtypes_Statically_Match (Type_1, Type_2)
8375 and then Dimensions_Match (Type_1, Type_2);
8376 end if;
8378 elsif Is_Incomplete_Or_Private_Type (Type_1)
8379 and then Present (Full_View (Type_1))
8380 and then Base_Types_Match (Full_View (Type_1), Type_2)
8381 then
8382 return
8383 Ctype <= Mode_Conformant
8384 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8386 elsif Ekind (Type_2) = E_Incomplete_Type
8387 and then Present (Full_View (Type_2))
8388 and then Base_Types_Match (Type_1, Full_View (Type_2))
8389 then
8390 return
8391 Ctype <= Mode_Conformant
8392 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8394 elsif Is_Private_Type (Type_2)
8395 and then In_Instance
8396 and then Present (Full_View (Type_2))
8397 and then Base_Types_Match (Type_1, Full_View (Type_2))
8398 then
8399 return
8400 Ctype <= Mode_Conformant
8401 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8403 -- Another confusion between views in a nested instance with an
8404 -- actual private type whose full view is not in scope.
8406 elsif Ekind (Type_2) = E_Private_Subtype
8407 and then In_Instance
8408 and then Etype (Type_2) = Type_1
8409 then
8410 return True;
8412 -- In Ada 2012, incomplete types (including limited views) can appear
8413 -- as actuals in instantiations, where they are conformant to the
8414 -- corresponding incomplete formal.
8416 elsif Is_Incomplete_Type (Type_1)
8417 and then Is_Incomplete_Type (Type_2)
8418 and then In_Instance
8419 and then (Used_As_Generic_Actual (Type_1)
8420 or else Used_As_Generic_Actual (Type_2))
8421 then
8422 return True;
8423 end if;
8425 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8426 -- treated recursively because they carry a signature. As far as
8427 -- conformance is concerned, convention plays no role, and either
8428 -- or both could be access to protected subprograms.
8430 Are_Anonymous_Access_To_Subprogram_Types :=
8431 Ekind (Type_1) in E_Anonymous_Access_Subprogram_Type
8432 | E_Anonymous_Access_Protected_Subprogram_Type
8433 and then
8434 Ekind (Type_2) in E_Anonymous_Access_Subprogram_Type
8435 | E_Anonymous_Access_Protected_Subprogram_Type;
8437 -- Test anonymous access type case. For this case, static subtype
8438 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8439 -- the base types because we may have built internal subtype entities
8440 -- to handle null-excluding types (see Process_Formals).
8442 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8443 and then
8444 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8446 -- Ada 2005 (AI-254)
8448 or else Are_Anonymous_Access_To_Subprogram_Types
8449 then
8450 declare
8451 Desig_1 : Entity_Id;
8452 Desig_2 : Entity_Id;
8454 begin
8455 -- In Ada 2005, access constant indicators must match for
8456 -- subtype conformance.
8458 if Ada_Version >= Ada_2005
8459 and then Ctype >= Subtype_Conformant
8460 and then
8461 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8462 then
8463 return False;
8464 end if;
8466 Desig_1 := Find_Designated_Type (Type_1);
8467 Desig_2 := Find_Designated_Type (Type_2);
8469 -- If the context is an instance association for a formal
8470 -- access-to-subprogram type; formal access parameter designated
8471 -- types require mapping because they may denote other formal
8472 -- parameters of the generic unit.
8474 if Get_Inst then
8475 Desig_1 := Get_Instance_Of (Desig_1);
8476 Desig_2 := Get_Instance_Of (Desig_2);
8477 end if;
8479 -- It is possible for a Class_Wide_Type to be introduced for an
8480 -- incomplete type, in which case there is a separate class_ wide
8481 -- type for the full view. The types conform if their Etypes
8482 -- conform, i.e. one may be the full view of the other. This can
8483 -- only happen in the context of an access parameter, other uses
8484 -- of an incomplete Class_Wide_Type are illegal.
8486 if Is_Class_Wide_Type (Desig_1)
8487 and then
8488 Is_Class_Wide_Type (Desig_2)
8489 then
8490 return
8491 Conforming_Types
8492 (Etype (Base_Type (Desig_1)),
8493 Etype (Base_Type (Desig_2)), Ctype);
8495 elsif Are_Anonymous_Access_To_Subprogram_Types then
8496 if Ada_Version < Ada_2005 then
8497 return
8498 Ctype = Type_Conformant
8499 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8501 -- We must check the conformance of the signatures themselves
8503 else
8504 declare
8505 Conformant : Boolean;
8506 begin
8507 Check_Conformance
8508 (Desig_1, Desig_2, Ctype, False, Conformant);
8509 return Conformant;
8510 end;
8511 end if;
8513 -- A limited view of an actual matches the corresponding
8514 -- incomplete formal.
8516 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8517 and then From_Limited_With (Desig_2)
8518 and then Used_As_Generic_Actual (Etype (Desig_2))
8519 then
8520 return True;
8522 else
8523 return Base_Type (Desig_1) = Base_Type (Desig_2)
8524 and then (Ctype = Type_Conformant
8525 or else
8526 Subtypes_Statically_Match (Desig_1, Desig_2));
8527 end if;
8528 end;
8530 -- Otherwise definitely no match
8532 else
8533 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8534 and then Is_Access_Type (Type_2))
8535 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8536 and then Is_Access_Type (Type_1)))
8537 and then
8538 Conforming_Types
8539 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8540 then
8541 May_Hide_Profile := True;
8542 end if;
8544 return False;
8545 end if;
8546 end Conforming_Types;
8548 --------------------------
8549 -- Create_Extra_Formals --
8550 --------------------------
8552 procedure Create_Extra_Formals (E : Entity_Id) is
8553 First_Extra : Entity_Id := Empty;
8554 Formal : Entity_Id;
8555 Last_Extra : Entity_Id := Empty;
8557 function Add_Extra_Formal
8558 (Assoc_Entity : Entity_Id;
8559 Typ : Entity_Id;
8560 Scope : Entity_Id;
8561 Suffix : String) return Entity_Id;
8562 -- Add an extra formal to the current list of formals and extra formals.
8563 -- The extra formal is added to the end of the list of extra formals,
8564 -- and also returned as the result. These formals are always of mode IN.
8565 -- The new formal has the type Typ, is declared in Scope, and its name
8566 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8567 -- The following suffixes are currently used. They should not be changed
8568 -- without coordinating with CodePeer, which makes use of these to
8569 -- provide better messages.
8571 -- O denotes the Constrained bit.
8572 -- L denotes the accessibility level.
8573 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8574 -- the full list in exp_ch6.BIP_Formal_Kind.
8576 function Has_Extra_Formals (E : Entity_Id) return Boolean;
8577 -- Determines if E has its extra formals
8579 function Needs_Accessibility_Check_Extra
8580 (E : Entity_Id;
8581 Formal : Node_Id) return Boolean;
8582 -- Determines whether the given formal of E needs an extra formal for
8583 -- supporting accessibility checking. Returns True for both anonymous
8584 -- access formals and formals of named access types that are marked as
8585 -- controlling formals. The latter case can occur when the subprogram
8586 -- Expand_Dispatching_Call creates a subprogram-type and substitutes
8587 -- the types of access-to-class-wide actuals for the anonymous access-
8588 -- to-specific-type of controlling formals.
8590 function Parent_Subprogram (Subp_Id : Entity_Id) return Entity_Id;
8591 -- Subp_Id is a subprogram of a derived type; return its parent
8592 -- subprogram if Subp_Id overrides a parent primitive or derives
8593 -- from a parent primitive, and such parent primitive can have extra
8594 -- formals. Otherwise return Empty.
8596 ----------------------
8597 -- Add_Extra_Formal --
8598 ----------------------
8600 function Add_Extra_Formal
8601 (Assoc_Entity : Entity_Id;
8602 Typ : Entity_Id;
8603 Scope : Entity_Id;
8604 Suffix : String) return Entity_Id
8606 EF : Entity_Id;
8608 begin
8609 -- A little optimization. Never generate an extra formal for the
8610 -- _init operand of an initialization procedure, since it could
8611 -- never be used.
8613 if Chars (Formal) = Name_uInit then
8614 return Empty;
8615 end if;
8617 EF := Make_Defining_Identifier (Sloc (Assoc_Entity),
8618 Chars => New_External_Name (Chars (Assoc_Entity),
8619 Suffix => Suffix));
8621 Mutate_Ekind (EF, E_In_Parameter);
8622 Set_Actual_Subtype (EF, Typ);
8623 Set_Etype (EF, Typ);
8624 Set_Scope (EF, Scope);
8625 Set_Mechanism (EF, Default_Mechanism);
8626 Set_Formal_Validity (EF);
8628 if No (First_Extra) then
8629 First_Extra := EF;
8630 Set_Extra_Formals (Scope, EF);
8631 end if;
8633 if Present (Last_Extra) then
8634 Set_Extra_Formal (Last_Extra, EF);
8635 end if;
8637 Last_Extra := EF;
8639 return EF;
8640 end Add_Extra_Formal;
8642 -----------------------
8643 -- Has_Extra_Formals --
8644 -----------------------
8646 function Has_Extra_Formals (E : Entity_Id) return Boolean is
8647 begin
8648 return Present (Extra_Formals (E))
8649 or else
8650 (Ekind (E) = E_Function
8651 and then Present (Extra_Accessibility_Of_Result (E)));
8652 end Has_Extra_Formals;
8654 -------------------------------------
8655 -- Needs_Accessibility_Check_Extra --
8656 -------------------------------------
8658 function Needs_Accessibility_Check_Extra
8659 (E : Entity_Id;
8660 Formal : Node_Id) return Boolean is
8662 begin
8663 -- For dispatching operations this extra formal is not suppressed
8664 -- since all the derivations must have matching formals.
8666 -- For nondispatching operations it is suppressed if we specifically
8667 -- suppress accessibility checks at the package level for either the
8668 -- subprogram, or the package in which it resides. However, we do
8669 -- not suppress it simply if the scope has accessibility checks
8670 -- suppressed, since this could cause trouble when clients are
8671 -- compiled with a different suppression setting. The explicit checks
8672 -- at the package level are safe from this point of view.
8674 if not Is_Dispatching_Operation (E)
8675 and then
8676 (Explicit_Suppress (E, Accessibility_Check)
8677 or else Explicit_Suppress (Scope (E), Accessibility_Check))
8678 then
8679 return False;
8680 end if;
8682 -- Base_Type is applied to handle cases where there is a null
8683 -- exclusion the formal may have an access subtype.
8685 return
8686 Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8687 or else
8688 (Is_Controlling_Formal (Formal)
8689 and then Is_Access_Type (Base_Type (Etype (Formal))));
8690 end Needs_Accessibility_Check_Extra;
8692 -----------------------
8693 -- Parent_Subprogram --
8694 -----------------------
8696 function Parent_Subprogram (Subp_Id : Entity_Id) return Entity_Id is
8697 pragma Assert (not Is_Thunk (Subp_Id));
8698 Ovr_E : Entity_Id := Overridden_Operation (Subp_Id);
8699 Ovr_Alias : Entity_Id;
8701 begin
8702 if Present (Ovr_E) then
8703 Ovr_Alias := Ultimate_Alias (Ovr_E);
8705 -- There is no real overridden subprogram if there is a mutual
8706 -- reference between the E and its overridden operation. This
8707 -- weird scenery occurs in the following cases:
8709 -- 1) Controlling function wrappers internally built by
8710 -- Make_Controlling_Function_Wrappers.
8712 -- 2) Hidden overridden primitives of type extensions or private
8713 -- extensions (cf. Find_Hidden_Overridden_Primitive). These
8714 -- hidden primitives have suffix 'P'.
8716 -- 3) Overriding primitives of stub types (see the subprogram
8717 -- Add_RACW_Primitive_Declarations_And_Bodies).
8719 if Ovr_Alias = Subp_Id then
8720 pragma Assert
8721 ((Is_Wrapper (Subp_Id)
8722 and then Has_Controlling_Result (Subp_Id))
8723 or else Has_Suffix (Ovr_E, 'P')
8724 or else Is_RACW_Stub_Type
8725 (Find_Dispatching_Type (Subp_Id)));
8727 if Present (Overridden_Operation (Ovr_E)) then
8728 Ovr_E := Overridden_Operation (Ovr_E);
8730 -- Ovr_E is an internal entity built by Derive_Subprogram and
8731 -- we have no direct way to climb to the corresponding parent
8732 -- subprogram but this internal entity has the extra formals
8733 -- (if any) required for the purpose of checking the extra
8734 -- formals of Subp_Id.
8736 else
8737 pragma Assert (not Comes_From_Source (Ovr_E));
8738 end if;
8740 -- Use as our reference entity the ultimate renaming of the
8741 -- overridden subprogram.
8743 elsif Present (Alias (Ovr_E)) then
8744 pragma Assert (No (Overridden_Operation (Ovr_Alias))
8745 or else Overridden_Operation (Ovr_Alias) /= Ovr_E);
8747 Ovr_E := Ovr_Alias;
8748 end if;
8749 end if;
8751 if Present (Ovr_E) and then Has_Reliable_Extra_Formals (Ovr_E) then
8752 return Ovr_E;
8753 else
8754 return Empty;
8755 end if;
8756 end Parent_Subprogram;
8758 -- Local variables
8760 Formal_Type : Entity_Id;
8761 May_Have_Alias : Boolean;
8762 Alias_Formal : Entity_Id := Empty;
8763 Alias_Subp : Entity_Id := Empty;
8764 Parent_Formal : Entity_Id := Empty;
8765 Parent_Subp : Entity_Id := Empty;
8766 Ref_E : Entity_Id;
8768 -- Start of processing for Create_Extra_Formals
8770 begin
8771 pragma Assert (Is_Subprogram_Or_Entry (E)
8772 or else Ekind (E) in E_Subprogram_Type);
8774 -- We never generate extra formals if expansion is not active because we
8775 -- don't need them unless we are generating code.
8777 if not Expander_Active then
8778 return;
8780 -- Enumeration literals have no extra formal; this case occurs when
8781 -- a function renames it.
8783 elsif Ekind (E) = E_Function
8784 and then Ekind (Ultimate_Alias (E)) = E_Enumeration_Literal
8785 then
8786 return;
8788 -- Initialization procedures don't have extra formals
8790 elsif Is_Init_Proc (E) then
8791 return;
8793 -- No need to generate extra formals in thunks whose target has no extra
8794 -- formals, but we can have two of them chained (interface and stack).
8796 elsif Is_Thunk (E) and then No (Extra_Formals (Thunk_Target (E))) then
8797 return;
8799 -- If Extra_Formals were already created, don't do it again. This
8800 -- situation may arise for subprogram types created as part of
8801 -- dispatching calls (see Expand_Dispatching_Call).
8803 elsif Has_Extra_Formals (E) then
8804 return;
8806 -- Extra formals of renamings of generic actual subprograms and
8807 -- renamings of instances of generic subprograms are shared. The
8808 -- check performed on the last formal is required to ensure that
8809 -- this is the renaming built by Analyze_Instance_And_Renamings
8810 -- (which shares all the formals); otherwise this would be wrong.
8812 elsif Ekind (E) in E_Function | E_Procedure
8813 and then Is_Generic_Instance (E)
8814 and then Present (Alias (E))
8815 and then Last_Formal (Ultimate_Alias (E)) = Last_Formal (E)
8816 then
8817 pragma Assert (Is_Generic_Instance (E)
8818 = Is_Generic_Instance (Ultimate_Alias (E)));
8820 Create_Extra_Formals (Ultimate_Alias (E));
8822 -- Share the extra formals
8824 Set_Extra_Formals (E, Extra_Formals (Ultimate_Alias (E)));
8826 if Ekind (E) = E_Function then
8827 Set_Extra_Accessibility_Of_Result (E,
8828 Extra_Accessibility_Of_Result (Ultimate_Alias (E)));
8829 end if;
8831 pragma Assert (Extra_Formals_OK (E));
8832 return;
8833 end if;
8835 -- Locate the last formal; required by Add_Extra_Formal.
8837 Formal := First_Formal (E);
8838 while Present (Formal) loop
8839 Last_Extra := Formal;
8840 Next_Formal (Formal);
8841 end loop;
8843 -- We rely on three entities to ensure consistency of extra formals of
8844 -- entity E:
8846 -- 1. A reference entity (Ref_E). For thunks it is their target
8847 -- primitive since this ensures that they have exactly the
8848 -- same extra formals; otherwise it is the identity.
8850 -- 2. The parent subprogram; only for derived types and references
8851 -- either the overridden subprogram or the internal entity built
8852 -- by Derive_Subprogram that has the extra formals of the parent
8853 -- subprogram; otherwise it is Empty. This entity ensures matching
8854 -- extra formals in derived types.
8856 -- 3. For renamings, their ultimate alias; this ensures taking the
8857 -- same decision in all the renamings (independently of the Ada
8858 -- mode on which they are compiled). For example:
8860 -- pragma Ada_2012;
8861 -- function Id_A (I : access Integer) return access Integer;
8863 -- pragma Ada_2005;
8864 -- function Id_B (I : access Integer) return access Integer
8865 -- renames Id_A;
8867 if Is_Thunk (E) then
8868 Ref_E := Thunk_Target (E);
8869 else
8870 Ref_E := E;
8871 end if;
8873 if Is_Subprogram (Ref_E) then
8874 Parent_Subp := Parent_Subprogram (Ref_E);
8875 end if;
8877 May_Have_Alias :=
8878 (Is_Subprogram (Ref_E) or else Ekind (Ref_E) = E_Subprogram_Type);
8880 -- If the parent subprogram is available then its ultimate alias of
8881 -- Ref_E is not needed since it will not be used to check its extra
8882 -- formals.
8884 if No (Parent_Subp)
8885 and then May_Have_Alias
8886 and then Present (Alias (Ref_E))
8887 and then Has_Reliable_Extra_Formals (Ultimate_Alias (Ref_E))
8888 then
8889 Alias_Subp := Ultimate_Alias (Ref_E);
8890 end if;
8892 -- Cannot add extra formals to subprograms and access types that have
8893 -- foreign convention nor to subprograms overriding primitives that
8894 -- have foreign convention since the foreign language does not know
8895 -- how to handle these extra formals; same for renamings of entities
8896 -- with foreign convention.
8898 if Has_Foreign_Convention (Ref_E)
8899 or else (Present (Alias_Subp)
8900 and then Has_Foreign_Convention (Alias_Subp))
8901 then
8902 return;
8903 end if;
8905 -- If the subprogram is a predefined dispatching subprogram then don't
8906 -- generate any extra constrained or accessibility level formals. In
8907 -- general we suppress these for internal subprograms (by not calling
8908 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8909 -- generated stream attributes do get passed through because extra
8910 -- build-in-place formals are needed in some cases (limited 'Input).
8912 if Is_Predefined_Internal_Operation (E) then
8913 goto Test_For_Func_Result_Extras;
8914 end if;
8916 -- Process the formals relying on the formals of our reference entities:
8917 -- Parent_Formal, Alias_Formal and Formal. Notice that we don't use the
8918 -- formal of Ref_E; we must use the formal of E which is the entity to
8919 -- which we are adding the extra formals.
8921 -- If this is a derived subprogram then the subtypes of the parent
8922 -- subprogram's formal parameters will be used to determine the need
8923 -- for extra formals.
8925 if Present (Parent_Subp) then
8926 Parent_Formal := First_Formal (Parent_Subp);
8928 -- For concurrent types, the controlling argument of a dispatching
8929 -- primitive implementing an interface primitive is implicit. For
8930 -- example:
8932 -- type Iface is protected interface;
8933 -- function Prim
8934 -- (Obj : Iface;
8935 -- Value : Integer) return Natural is abstract;
8937 -- protected type PO is new Iface with
8938 -- function Prim (Value : Integer) return Natural;
8939 -- end PO;
8941 if Convention (Ref_E) = Convention_Protected
8942 and then Is_Abstract_Subprogram (Parent_Subp)
8943 and then Is_Interface (Find_Dispatching_Type (Parent_Subp))
8944 then
8945 Parent_Formal := Next_Formal (Parent_Formal);
8947 -- This is the nondispatching subprogram of a concurrent type
8948 -- that overrides the interface primitive; the expander will
8949 -- create the dispatching primitive (without Convention_Protected)
8950 -- with all the matching formals (see exp_ch9.Build_Wrapper_Specs)
8952 pragma Assert (not Is_Dispatching_Operation (Ref_E));
8953 end if;
8955 -- Ensure that the ultimate alias has all its extra formals
8957 elsif Present (Alias_Subp) then
8958 Create_Extra_Formals (Alias_Subp);
8959 Alias_Formal := First_Formal (Alias_Subp);
8960 end if;
8962 Formal := First_Formal (E);
8963 while Present (Formal) loop
8965 -- Here we establish our priority for deciding on the extra
8966 -- formals: 1) Parent primitive 2) Aliased primitive 3) Identity
8968 if Present (Parent_Formal) then
8969 Formal_Type := Etype (Parent_Formal);
8971 elsif Present (Alias_Formal) then
8972 Formal_Type := Etype (Alias_Formal);
8974 else
8975 Formal_Type := Etype (Formal);
8976 end if;
8978 -- Create extra formal for supporting the attribute 'Constrained.
8979 -- The case of a private type view without discriminants also
8980 -- requires the extra formal if the underlying type has defaulted
8981 -- discriminants.
8983 if Ekind (Formal) /= E_In_Parameter then
8985 -- Do not produce extra formals for Unchecked_Union parameters.
8986 -- Jump directly to the end of the loop.
8988 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8989 goto Skip_Extra_Formal_Generation;
8990 end if;
8992 if not Has_Discriminants (Formal_Type)
8993 and then Is_Private_Type (Formal_Type)
8994 and then Present (Underlying_Type (Formal_Type))
8995 then
8996 Formal_Type := Underlying_Type (Formal_Type);
8997 end if;
8999 -- Suppress the extra formal if formal's subtype is constrained or
9000 -- indefinite, or we're compiling for Ada 2012 and the underlying
9001 -- type is tagged and limited. In Ada 2012, a limited tagged type
9002 -- can have defaulted discriminants, but 'Constrained is required
9003 -- to return True, so the formal is never needed (see AI05-0214).
9004 -- Note that this ensures consistency of calling sequences for
9005 -- dispatching operations when some types in a class have defaults
9006 -- on discriminants and others do not (and requiring the extra
9007 -- formal would introduce distributed overhead).
9009 -- If the type does not have a completion yet, treat as prior to
9010 -- Ada 2012 for consistency.
9012 if Has_Discriminants (Formal_Type)
9013 and then not Is_Constrained (Formal_Type)
9014 and then Is_Definite_Subtype (Formal_Type)
9015 and then (Ada_Version < Ada_2012
9016 or else No (Underlying_Type (Formal_Type))
9017 or else not
9018 (Is_Limited_Type (Formal_Type)
9019 and then
9020 (Is_Tagged_Type
9021 (Underlying_Type (Formal_Type)))))
9022 then
9023 Set_Extra_Constrained
9024 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
9025 end if;
9026 end if;
9028 -- Extra formal for supporting accessibility checking
9030 if Needs_Accessibility_Check_Extra (Ref_E, Formal) then
9031 pragma Assert (No (Parent_Formal)
9032 or else Present (Extra_Accessibility (Parent_Formal)));
9033 pragma Assert (No (Alias_Formal)
9034 or else Present (Extra_Accessibility (Alias_Formal)));
9036 Set_Extra_Accessibility
9037 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
9039 else
9040 pragma Assert (No (Parent_Formal)
9041 or else No (Extra_Accessibility (Parent_Formal)));
9042 pragma Assert (No (Alias_Formal)
9043 or else No (Extra_Accessibility (Alias_Formal)));
9044 end if;
9046 -- This label is required when skipping extra formal generation for
9047 -- Unchecked_Union parameters.
9049 <<Skip_Extra_Formal_Generation>>
9051 if Present (Parent_Formal) then
9052 Next_Formal (Parent_Formal);
9053 end if;
9055 if Present (Alias_Formal) then
9056 Next_Formal (Alias_Formal);
9057 end if;
9059 Next_Formal (Formal);
9060 end loop;
9062 <<Test_For_Func_Result_Extras>>
9064 -- Assume the worst case (Ada 2022) to evaluate this extra formal;
9065 -- required to ensure matching of extra formals between subprograms
9066 -- and access-to-subprogram types in projects with mixed Ada dialects.
9068 declare
9069 Save_Ada_Version : constant Ada_Version_Type := Ada_Version;
9071 begin
9072 Ada_Version := Ada_2022;
9074 if Needs_Result_Accessibility_Level (Ref_E) then
9075 pragma Assert (No (Parent_Subp)
9076 or else Needs_Result_Accessibility_Level (Parent_Subp));
9077 pragma Assert (No (Alias_Subp)
9078 or else Needs_Result_Accessibility_Level (Alias_Subp));
9080 Set_Extra_Accessibility_Of_Result (E,
9081 Add_Extra_Formal (E, Standard_Natural, E, "L"));
9083 else
9084 pragma Assert (No (Parent_Subp)
9085 or else not Needs_Result_Accessibility_Level (Parent_Subp));
9086 pragma Assert (No (Alias_Subp)
9087 or else not Needs_Result_Accessibility_Level (Alias_Subp));
9088 end if;
9090 Ada_Version := Save_Ada_Version;
9091 end;
9093 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
9094 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
9096 if (Present (Parent_Subp) and then Has_BIP_Formals (Parent_Subp))
9097 or else
9098 (Present (Alias_Subp) and then Has_BIP_Formals (Alias_Subp))
9099 or else
9100 (Is_Build_In_Place_Function (Ref_E)
9101 and then Has_Reliable_Extra_Formals (Ref_E))
9102 then
9103 declare
9104 Result_Subt : constant Entity_Id := Etype (Ref_E);
9105 Formal_Typ : Entity_Id;
9106 Subp_Decl : Node_Id;
9107 Discard : Entity_Id;
9109 begin
9110 -- In the case of functions with unconstrained result subtypes,
9111 -- add a 4-state formal indicating whether the return object is
9112 -- allocated by the caller (1), or should be allocated by the
9113 -- callee on the secondary stack (2), in the global heap (3), or
9114 -- in a user-defined storage pool (4). For the moment we just use
9115 -- Natural for the type of this formal. Note that this formal
9116 -- isn't usually needed in the case where the result subtype is
9117 -- constrained, but it is needed when the function has a tagged
9118 -- result, because generally such functions can be called in a
9119 -- dispatching context and such calls must be handled like calls
9120 -- to a class-wide function.
9122 if Needs_BIP_Alloc_Form (Ref_E) then
9123 pragma Assert (No (Parent_Subp)
9124 or else Has_BIP_Extra_Formal (Parent_Subp, BIP_Alloc_Form,
9125 Must_Be_Frozen => False));
9126 pragma Assert (No (Alias_Subp)
9127 or else Has_BIP_Extra_Formal (Alias_Subp, BIP_Alloc_Form,
9128 Must_Be_Frozen => False));
9130 Discard :=
9131 Add_Extra_Formal
9132 (E, Standard_Natural,
9133 E, BIP_Formal_Suffix (BIP_Alloc_Form));
9135 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
9136 -- use a user-defined pool. This formal is not added on
9137 -- ZFP as those targets do not support pools.
9139 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
9140 Discard :=
9141 Add_Extra_Formal
9142 (E, RTE (RE_Root_Storage_Pool_Ptr),
9143 E, BIP_Formal_Suffix (BIP_Storage_Pool));
9144 end if;
9146 else
9147 pragma Assert (No (Parent_Subp)
9148 or else not
9149 Has_BIP_Extra_Formal (Parent_Subp, BIP_Alloc_Form,
9150 Must_Be_Frozen => False));
9151 pragma Assert (No (Alias_Subp)
9152 or else not
9153 Has_BIP_Extra_Formal (Alias_Subp, BIP_Alloc_Form,
9154 Must_Be_Frozen => False));
9155 end if;
9157 -- In the case of functions whose result type needs finalization,
9158 -- add an extra formal which represents the finalization master.
9160 if Needs_BIP_Finalization_Master (Ref_E) then
9161 pragma Assert (No (Parent_Subp)
9162 or else Has_BIP_Extra_Formal (Parent_Subp,
9163 Kind => BIP_Finalization_Master,
9164 Must_Be_Frozen => False));
9165 pragma Assert (No (Alias_Subp)
9166 or else Has_BIP_Extra_Formal (Alias_Subp,
9167 Kind => BIP_Finalization_Master,
9168 Must_Be_Frozen => False));
9170 Discard :=
9171 Add_Extra_Formal
9172 (E, RTE (RE_Finalization_Master_Ptr),
9173 E, BIP_Formal_Suffix (BIP_Finalization_Master));
9175 else
9176 pragma Assert (No (Parent_Subp)
9177 or else not
9178 Has_BIP_Extra_Formal (Parent_Subp,
9179 Kind => BIP_Finalization_Master,
9180 Must_Be_Frozen => False));
9181 pragma Assert (No (Alias_Subp)
9182 or else not
9183 Has_BIP_Extra_Formal (Alias_Subp,
9184 Kind => BIP_Finalization_Master,
9185 Must_Be_Frozen => False));
9186 end if;
9188 -- When the result type contains tasks, add two extra formals: the
9189 -- master of the tasks to be created, and the caller's activation
9190 -- chain.
9192 if Needs_BIP_Task_Actuals (Ref_E) then
9193 pragma Assert (No (Parent_Subp)
9194 or else Has_BIP_Extra_Formal (Parent_Subp, BIP_Task_Master,
9195 Must_Be_Frozen => False));
9196 pragma Assert (No (Alias_Subp)
9197 or else Has_BIP_Extra_Formal (Alias_Subp, BIP_Task_Master,
9198 Must_Be_Frozen => False)
9199 or else
9200 (Is_Abstract_Subprogram (Ref_E)
9201 and then Is_Predefined_Dispatching_Operation (Ref_E)
9202 and then Is_Interface
9203 (Find_Dispatching_Type (Alias_Subp))));
9205 Discard :=
9206 Add_Extra_Formal
9207 (E, Standard_Integer,
9208 E, BIP_Formal_Suffix (BIP_Task_Master));
9210 Set_Has_Master_Entity (E);
9212 Discard :=
9213 Add_Extra_Formal
9214 (E, RTE (RE_Activation_Chain_Access),
9215 E, BIP_Formal_Suffix (BIP_Activation_Chain));
9217 else
9218 pragma Assert (No (Parent_Subp)
9219 or else not
9220 Has_BIP_Extra_Formal (Parent_Subp, BIP_Task_Master,
9221 Must_Be_Frozen => False));
9222 pragma Assert (No (Alias_Subp)
9223 or else not
9224 Has_BIP_Extra_Formal (Alias_Subp, BIP_Task_Master,
9225 Must_Be_Frozen => False));
9226 end if;
9228 -- All build-in-place functions get an extra formal that will be
9229 -- passed the address of the return object within the caller.
9231 Formal_Typ :=
9232 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
9234 -- Incomplete_View_From_Limited_With is needed here because
9235 -- gigi gets confused if the designated type is the full view
9236 -- coming from a limited-with'ed package. In the normal case,
9237 -- (no limited with) Incomplete_View_From_Limited_With
9238 -- returns Result_Subt.
9240 Set_Directly_Designated_Type
9241 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
9242 Set_Etype (Formal_Typ, Formal_Typ);
9243 Set_Depends_On_Private
9244 (Formal_Typ, Has_Private_Component (Formal_Typ));
9245 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
9246 Set_Is_Access_Constant (Formal_Typ, False);
9248 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
9249 -- the designated type comes from the limited view (for back-end
9250 -- purposes).
9252 Set_From_Limited_With
9253 (Formal_Typ, From_Limited_With (Result_Subt));
9255 Layout_Type (Formal_Typ);
9257 -- Force the definition of the Itype in case of internal function
9258 -- calls within the same or nested scope.
9260 if Is_Subprogram_Or_Generic_Subprogram (E)
9261 and then not Is_Compilation_Unit (E)
9262 then
9263 Subp_Decl := Parent (E);
9265 -- The insertion point for an Itype reference should be after
9266 -- the unit declaration node of the subprogram. An exception
9267 -- to this are inherited operations from a parent type in which
9268 -- case the derived type acts as their parent.
9270 if Nkind (Subp_Decl) in N_Function_Specification
9271 | N_Procedure_Specification
9272 then
9273 Subp_Decl := Parent (Subp_Decl);
9274 end if;
9276 Build_Itype_Reference (Formal_Typ, Subp_Decl);
9277 end if;
9279 Discard :=
9280 Add_Extra_Formal
9281 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
9282 end;
9283 end if;
9285 -- If this is an instance of a generic, we need to have extra formals
9286 -- for the Alias.
9288 if Is_Generic_Instance (E) and then Present (Alias (E)) then
9289 Set_Extra_Formals (Alias (E), Extra_Formals (E));
9290 end if;
9292 pragma Assert (No (Alias_Subp)
9293 or else Extra_Formals_Match_OK (E, Alias_Subp));
9295 pragma Assert (No (Parent_Subp)
9296 or else Extra_Formals_Match_OK (E, Parent_Subp));
9298 pragma Assert (Extra_Formals_OK (E));
9299 end Create_Extra_Formals;
9301 -----------------------------
9302 -- Enter_Overloaded_Entity --
9303 -----------------------------
9305 procedure Enter_Overloaded_Entity (S : Entity_Id) is
9306 function Matches_Predefined_Op return Boolean;
9307 -- This returns an approximation of whether S matches a predefined
9308 -- operator, based on the operator symbol, and the parameter and result
9309 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
9311 ---------------------------
9312 -- Matches_Predefined_Op --
9313 ---------------------------
9315 function Matches_Predefined_Op return Boolean is
9316 Formal_1 : constant Entity_Id := First_Formal (S);
9317 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
9318 Op : constant Name_Id := Chars (S);
9319 Result_Type : constant Entity_Id := Base_Type (Etype (S));
9320 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
9322 begin
9323 -- Binary operator
9325 if Present (Formal_2) then
9326 declare
9327 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
9329 begin
9330 -- All but "&" and "**" have same-types parameters
9332 case Op is
9333 when Name_Op_Concat
9334 | Name_Op_Expon
9336 null;
9338 when others =>
9339 if Type_1 /= Type_2 then
9340 return False;
9341 end if;
9342 end case;
9344 -- Check parameter and result types
9346 case Op is
9347 when Name_Op_And
9348 | Name_Op_Or
9349 | Name_Op_Xor
9351 return
9352 Is_Boolean_Type (Result_Type)
9353 and then Result_Type = Type_1;
9355 when Name_Op_Mod
9356 | Name_Op_Rem
9358 return
9359 Is_Integer_Type (Result_Type)
9360 and then Result_Type = Type_1;
9362 when Name_Op_Add
9363 | Name_Op_Divide
9364 | Name_Op_Multiply
9365 | Name_Op_Subtract
9367 return
9368 Is_Numeric_Type (Result_Type)
9369 and then Result_Type = Type_1;
9371 when Name_Op_Eq
9372 | Name_Op_Ne
9374 return
9375 Is_Boolean_Type (Result_Type)
9376 and then not Is_Limited_Type (Type_1);
9378 when Name_Op_Ge
9379 | Name_Op_Gt
9380 | Name_Op_Le
9381 | Name_Op_Lt
9383 return
9384 Is_Boolean_Type (Result_Type)
9385 and then (Is_Array_Type (Type_1)
9386 or else Is_Scalar_Type (Type_1));
9388 when Name_Op_Concat =>
9389 return Is_Array_Type (Result_Type);
9391 when Name_Op_Expon =>
9392 return
9393 (Is_Integer_Type (Result_Type)
9394 or else Is_Floating_Point_Type (Result_Type))
9395 and then Result_Type = Type_1
9396 and then Type_2 = Standard_Integer;
9398 when others =>
9399 raise Program_Error;
9400 end case;
9401 end;
9403 -- Unary operator
9405 else
9406 case Op is
9407 when Name_Op_Abs
9408 | Name_Op_Add
9409 | Name_Op_Subtract
9411 return
9412 Is_Numeric_Type (Result_Type)
9413 and then Result_Type = Type_1;
9415 when Name_Op_Not =>
9416 return
9417 Is_Boolean_Type (Result_Type)
9418 and then Result_Type = Type_1;
9420 when others =>
9421 raise Program_Error;
9422 end case;
9423 end if;
9424 end Matches_Predefined_Op;
9426 -- Local variables
9428 E : Entity_Id := Current_Entity_In_Scope (S);
9429 C_E : Entity_Id := Current_Entity (S);
9431 -- Start of processing for Enter_Overloaded_Entity
9433 begin
9434 if Present (E) then
9435 Set_Has_Homonym (E);
9436 Set_Has_Homonym (S);
9437 end if;
9439 Set_Is_Immediately_Visible (S);
9440 Set_Scope (S, Current_Scope);
9442 -- Chain new entity if front of homonym in current scope, so that
9443 -- homonyms are contiguous.
9445 if Present (E) and then E /= C_E then
9446 while Homonym (C_E) /= E loop
9447 C_E := Homonym (C_E);
9448 end loop;
9450 Set_Homonym (C_E, S);
9452 else
9453 E := C_E;
9454 Set_Current_Entity (S);
9455 end if;
9457 Set_Homonym (S, E);
9459 if Is_Inherited_Operation (S) then
9460 Append_Inherited_Subprogram (S);
9461 else
9462 Append_Entity (S, Current_Scope);
9463 end if;
9465 Set_Public_Status (S);
9467 if Debug_Flag_E then
9468 Write_Str ("New overloaded entity chain: ");
9469 Write_Name (Chars (S));
9471 E := S;
9472 while Present (E) loop
9473 Write_Str (" "); Write_Int (Int (E));
9474 E := Homonym (E);
9475 end loop;
9477 Write_Eol;
9478 end if;
9480 -- Generate warning for hiding
9482 if Warn_On_Hiding
9483 and then Comes_From_Source (S)
9484 and then In_Extended_Main_Source_Unit (S)
9485 then
9486 E := S;
9487 loop
9488 E := Homonym (E);
9489 exit when No (E);
9491 -- Warn unless genuine overloading. Do not emit warning on
9492 -- hiding predefined operators in Standard (these are either an
9493 -- artifact of our implicit declarations, or simple noise) but
9494 -- keep warning on a operator defined on a local subtype, because
9495 -- of the real danger that different operators may be applied in
9496 -- various parts of the program.
9498 -- Note that if E and S have the same scope, there is never any
9499 -- hiding. Either the two conflict, and the program is illegal,
9500 -- or S is overriding an implicit inherited subprogram.
9502 if Scope (E) /= Scope (S)
9503 and then (not Is_Overloadable (E)
9504 or else Subtype_Conformant (E, S))
9505 and then (Is_Immediately_Visible (E)
9506 or else Is_Potentially_Use_Visible (S))
9507 then
9508 if Scope (E) = Standard_Standard then
9509 if Nkind (S) = N_Defining_Operator_Symbol
9510 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
9511 Scope (S)
9512 and then Matches_Predefined_Op
9513 then
9514 Error_Msg_N
9515 ("declaration of & hides predefined operator?h?", S);
9516 end if;
9518 -- E not immediately within Standard
9520 else
9521 Error_Msg_Sloc := Sloc (E);
9522 Error_Msg_N ("declaration of & hides one #?h?", S);
9523 end if;
9524 end if;
9525 end loop;
9526 end if;
9527 end Enter_Overloaded_Entity;
9529 ----------------------------
9530 -- Extra_Formals_Match_OK --
9531 ----------------------------
9533 function Extra_Formals_Match_OK
9534 (E : Entity_Id;
9535 Ref_E : Entity_Id) return Boolean is
9536 begin
9537 pragma Assert (Is_Subprogram (E));
9539 -- Cases where no check can be performed:
9540 -- 1) When expansion is not active (since we never generate extra
9541 -- formals if expansion is not active because we don't need them
9542 -- unless we are generating code).
9543 -- 2) On abstract predefined dispatching operations of Root_Controlled
9544 -- and predefined stream operations not emitted by the frontend.
9545 -- 3) On renamings of abstract predefined dispatching operations of
9546 -- interface types (since limitedness is not inherited in such
9547 -- case (AI-419)).
9548 -- 4) The controlling formal of the nondispatching subprogram of
9549 -- a concurrent type that overrides an interface primitive is
9550 -- implicit and hence we cannot check here if all its extra
9551 -- formals match; the expander will create the dispatching
9552 -- primitive (without Convention_Protected) with the matching
9553 -- formals (see exp_ch9.Build_Wrapper_Specs) which will be
9554 -- checked later.
9556 if Debug_Flag_Underscore_XX
9557 or else not Expander_Active
9558 or else
9559 (Is_Predefined_Dispatching_Operation (E)
9560 and then (not Has_Reliable_Extra_Formals (E)
9561 or else not Has_Reliable_Extra_Formals (Ref_E)))
9562 or else
9563 (Is_Predefined_Dispatching_Operation (E)
9564 and then Is_Abstract_Subprogram (E)
9565 and then Is_Interface (Find_Dispatching_Type (Ref_E)))
9566 then
9567 return True;
9569 elsif Convention (E) = Convention_Protected
9570 and then not Is_Dispatching_Operation (E)
9571 and then Is_Abstract_Subprogram (Ref_E)
9572 and then Is_Interface (Find_Dispatching_Type (Ref_E))
9573 then
9574 return True;
9575 end if;
9577 -- Perform the checks
9579 if No (Extra_Formals (E)) then
9580 return No (Extra_Formals (Ref_E));
9581 end if;
9583 if Ekind (E) in E_Function | E_Subprogram_Type
9584 and then Present (Extra_Accessibility_Of_Result (E))
9585 /= Present (Extra_Accessibility_Of_Result (Ref_E))
9586 then
9587 return False;
9588 end if;
9590 declare
9591 Formal_1 : Entity_Id := Extra_Formals (E);
9592 Formal_2 : Entity_Id := Extra_Formals (Ref_E);
9594 begin
9595 while Present (Formal_1) and then Present (Formal_2) loop
9596 if Has_Suffix (Formal_1, 'L') then
9597 if not Has_Suffix (Formal_2, 'L') then
9598 return False;
9599 end if;
9601 elsif Has_Suffix (Formal_1, 'O') then
9602 if not Has_Suffix (Formal_2, 'O') then
9603 return False;
9604 end if;
9606 elsif BIP_Suffix_Kind (Formal_1) /= BIP_Suffix_Kind (Formal_2) then
9607 return False;
9608 end if;
9610 Formal_1 := Next_Formal_With_Extras (Formal_1);
9611 Formal_2 := Next_Formal_With_Extras (Formal_2);
9612 end loop;
9614 return No (Formal_1) and then No (Formal_2);
9615 end;
9616 end Extra_Formals_Match_OK;
9618 ----------------------
9619 -- Extra_Formals_OK --
9620 ----------------------
9622 function Extra_Formals_OK (E : Entity_Id) return Boolean is
9623 Last_Formal : Entity_Id := Empty;
9624 Formal : Entity_Id;
9625 Has_Extra_Formals : Boolean := False;
9627 begin
9628 -- No check required if explicitly disabled
9630 if Debug_Flag_Underscore_XX then
9631 return True;
9633 -- No check required if expansion is disabled because extra
9634 -- formals are only generated when we are generating code.
9635 -- See Create_Extra_Formals.
9637 elsif not Expander_Active then
9638 return True;
9639 end if;
9641 -- Check attribute Extra_Formal: If available, it must be set only
9642 -- on the last formal of E.
9644 Formal := First_Formal (E);
9645 while Present (Formal) loop
9646 if Present (Extra_Formal (Formal)) then
9647 if Has_Extra_Formals then
9648 return False;
9649 end if;
9651 Has_Extra_Formals := True;
9652 end if;
9654 Last_Formal := Formal;
9655 Next_Formal (Formal);
9656 end loop;
9658 -- Check attribute Extra_Accessibility_Of_Result
9660 if Ekind (E) in E_Function | E_Subprogram_Type
9661 and then Needs_Result_Accessibility_Level (E)
9662 and then No (Extra_Accessibility_Of_Result (E))
9663 then
9664 return False;
9665 end if;
9667 -- Check attribute Extra_Formals: If E has extra formals, then this
9668 -- attribute must point to the first extra formal of E.
9670 if Has_Extra_Formals then
9671 return Present (Extra_Formals (E))
9672 and then Present (Extra_Formal (Last_Formal))
9673 and then Extra_Formal (Last_Formal) = Extra_Formals (E);
9675 -- When E has no formals, the first extra formal is available through
9676 -- the Extra_Formals attribute.
9678 elsif Present (Extra_Formals (E)) then
9679 return No (First_Formal (E));
9681 else
9682 return True;
9683 end if;
9684 end Extra_Formals_OK;
9686 -----------------------------
9687 -- Find_Corresponding_Spec --
9688 -----------------------------
9690 function Find_Corresponding_Spec
9691 (N : Node_Id;
9692 Post_Error : Boolean := True) return Entity_Id
9694 Spec : constant Node_Id := Specification (N);
9695 Designator : constant Entity_Id := Defining_Entity (Spec);
9697 E : Entity_Id;
9699 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9700 -- Even if fully conformant, a body may depend on a generic actual when
9701 -- the spec does not, or vice versa, in which case they were distinct
9702 -- entities in the generic.
9704 -------------------------------
9705 -- Different_Generic_Profile --
9706 -------------------------------
9708 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9709 F1, F2 : Entity_Id;
9711 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9712 -- Check that the types of corresponding formals have the same
9713 -- generic actual if any. We have to account for subtypes of a
9714 -- generic formal, declared between a spec and a body, which may
9715 -- appear distinct in an instance but matched in the generic, and
9716 -- the subtype may be used either in the spec or the body of the
9717 -- subprogram being checked.
9719 -------------------------
9720 -- Same_Generic_Actual --
9721 -------------------------
9723 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9725 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9726 -- Predicate to check whether S1 is a subtype of S2 in the source
9727 -- of the instance.
9729 -------------------------
9730 -- Is_Declared_Subtype --
9731 -------------------------
9733 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9734 begin
9735 return Comes_From_Source (Parent (S1))
9736 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9737 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9738 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9739 end Is_Declared_Subtype;
9741 -- Start of processing for Same_Generic_Actual
9743 begin
9744 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9745 or else Is_Declared_Subtype (T1, T2)
9746 or else Is_Declared_Subtype (T2, T1);
9747 end Same_Generic_Actual;
9749 -- Start of processing for Different_Generic_Profile
9751 begin
9752 if not In_Instance then
9753 return False;
9755 elsif Ekind (E) = E_Function
9756 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9757 then
9758 return True;
9759 end if;
9761 F1 := First_Formal (Designator);
9762 F2 := First_Formal (E);
9763 while Present (F1) loop
9764 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9765 return True;
9766 end if;
9768 Next_Formal (F1);
9769 Next_Formal (F2);
9770 end loop;
9772 return False;
9773 end Different_Generic_Profile;
9775 -- Start of processing for Find_Corresponding_Spec
9777 begin
9778 E := Current_Entity (Designator);
9779 while Present (E) loop
9781 -- We are looking for a matching spec. It must have the same scope,
9782 -- and the same name, and either be type conformant, or be the case
9783 -- of a library procedure spec and its body (which belong to one
9784 -- another regardless of whether they are type conformant or not).
9786 if Scope (E) = Current_Scope then
9787 if Current_Scope = Standard_Standard
9788 or else (Ekind (E) = Ekind (Designator)
9789 and then Type_Conformant (E, Designator))
9790 then
9791 -- Within an instantiation, we know that spec and body are
9792 -- subtype conformant, because they were subtype conformant in
9793 -- the generic. We choose the subtype-conformant entity here as
9794 -- well, to resolve spurious ambiguities in the instance that
9795 -- were not present in the generic (i.e. when two different
9796 -- types are given the same actual). If we are looking for a
9797 -- spec to match a body, full conformance is expected.
9799 if In_Instance then
9801 -- Inherit the convention and "ghostness" of the matching
9802 -- spec to ensure proper full and subtype conformance.
9804 Set_Convention (Designator, Convention (E));
9806 -- Skip past subprogram bodies and subprogram renamings that
9807 -- may appear to have a matching spec, but that aren't fully
9808 -- conformant with it. That can occur in cases where an
9809 -- actual type causes unrelated homographs in the instance.
9811 if Nkind (N) in N_Subprogram_Body
9812 | N_Subprogram_Renaming_Declaration
9813 and then Present (Homonym (E))
9814 and then not Fully_Conformant (Designator, E)
9815 then
9816 goto Next_Entity;
9818 elsif not Subtype_Conformant (Designator, E) then
9819 goto Next_Entity;
9821 elsif Different_Generic_Profile (E) then
9822 goto Next_Entity;
9823 end if;
9824 end if;
9826 -- Ada 2012 (AI05-0165): For internally generated bodies of
9827 -- null procedures locate the internally generated spec. We
9828 -- enforce mode conformance since a tagged type may inherit
9829 -- from interfaces several null primitives which differ only
9830 -- in the mode of the formals.
9832 if not (Comes_From_Source (E))
9833 and then Is_Null_Procedure (E)
9834 and then not Mode_Conformant (Designator, E)
9835 then
9836 null;
9838 -- For null procedures coming from source that are completions,
9839 -- analysis of the generated body will establish the link.
9841 elsif Comes_From_Source (E)
9842 and then Nkind (Spec) = N_Procedure_Specification
9843 and then Null_Present (Spec)
9844 then
9845 return E;
9847 -- Expression functions can be completions, but cannot be
9848 -- completed by an explicit body.
9850 elsif Comes_From_Source (E)
9851 and then Comes_From_Source (N)
9852 and then Nkind (N) = N_Subprogram_Body
9853 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9854 N_Expression_Function
9855 then
9856 Error_Msg_Sloc := Sloc (E);
9857 Error_Msg_N ("body conflicts with expression function#", N);
9858 return Empty;
9860 elsif not Has_Completion (E) then
9861 if Nkind (N) /= N_Subprogram_Body_Stub then
9862 Set_Corresponding_Spec (N, E);
9863 end if;
9865 Set_Has_Completion (E);
9866 return E;
9868 elsif Nkind (Parent (N)) = N_Subunit then
9870 -- If this is the proper body of a subunit, the completion
9871 -- flag is set when analyzing the stub.
9873 return E;
9875 -- If E is an internal function with a controlling result that
9876 -- was created for an operation inherited by a null extension,
9877 -- it may be overridden by a body without a previous spec (one
9878 -- more reason why these should be shunned). In that case we
9879 -- remove the generated body if present, because the current
9880 -- one is the explicit overriding.
9882 elsif Ekind (E) = E_Function
9883 and then Ada_Version >= Ada_2005
9884 and then not Comes_From_Source (E)
9885 and then Has_Controlling_Result (E)
9886 and then (not Is_Class_Wide_Type (Etype (E))
9887 and then Is_Null_Extension (Etype (E)))
9888 and then Comes_From_Source (Spec)
9889 then
9890 Set_Has_Completion (E, False);
9892 if Expander_Active
9893 and then Nkind (Parent (E)) = N_Function_Specification
9894 then
9895 Remove
9896 (Unit_Declaration_Node
9897 (Corresponding_Body (Unit_Declaration_Node (E))));
9899 return E;
9901 -- If expansion is disabled, or if the wrapper function has
9902 -- not been generated yet, this a late body overriding an
9903 -- inherited operation, or it is an overriding by some other
9904 -- declaration before the controlling result is frozen. In
9905 -- either case this is a declaration of a new entity.
9907 else
9908 return Empty;
9909 end if;
9911 -- If the body already exists, then this is an error unless
9912 -- the previous declaration is the implicit declaration of a
9913 -- derived subprogram. It is also legal for an instance to
9914 -- contain type conformant overloadable declarations (but the
9915 -- generic declaration may not), per 8.3(26/2).
9917 elsif No (Alias (E))
9918 and then not Is_Intrinsic_Subprogram (E)
9919 and then not In_Instance
9920 and then Post_Error
9921 then
9922 Error_Msg_Sloc := Sloc (E);
9924 if Is_Imported (E) then
9925 Error_Msg_NE
9926 ("body not allowed for imported subprogram & declared#",
9927 N, E);
9928 else
9929 Error_Msg_NE ("duplicate body for & declared#", N, E);
9930 end if;
9931 end if;
9933 -- Child units cannot be overloaded, so a conformance mismatch
9934 -- between body and a previous spec is an error.
9936 elsif Is_Child_Unit (E)
9937 and then
9938 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9939 and then
9940 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9941 N_Compilation_Unit
9942 and then Post_Error
9943 then
9944 Error_Msg_N
9945 ("body of child unit does not match previous declaration", N);
9946 end if;
9947 end if;
9949 <<Next_Entity>>
9950 E := Homonym (E);
9951 end loop;
9953 -- On exit, we know that no previous declaration of subprogram exists
9955 return Empty;
9956 end Find_Corresponding_Spec;
9958 ----------------------
9959 -- Fully_Conformant --
9960 ----------------------
9962 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9963 Result : Boolean;
9964 begin
9965 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9966 return Result;
9967 end Fully_Conformant;
9969 ----------------------------------
9970 -- Fully_Conformant_Expressions --
9971 ----------------------------------
9973 function Fully_Conformant_Expressions
9974 (Given_E1 : Node_Id;
9975 Given_E2 : Node_Id;
9976 Report : Boolean := False) return Boolean
9978 E1 : constant Node_Id := Original_Node (Given_E1);
9979 E2 : constant Node_Id := Original_Node (Given_E2);
9980 -- We always test conformance on original nodes, since it is possible
9981 -- for analysis and/or expansion to make things look as though they
9982 -- conform when they do not, e.g. by converting 1+2 into 3.
9984 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9985 -- Convenience function to abbreviate recursive calls to
9986 -- Fully_Conformant_Expressions without having to pass Report.
9988 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9989 -- Compare elements of two lists for conformance. Elements have to be
9990 -- conformant, and actuals inserted as default parameters do not match
9991 -- explicit actuals with the same value.
9993 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9994 -- Compare an operator node with a function call
9996 ---------
9997 -- FCE --
9998 ---------
10000 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
10001 begin
10002 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
10003 end FCE;
10005 ---------
10006 -- FCL --
10007 ---------
10009 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
10010 N1 : Node_Id;
10011 N2 : Node_Id;
10013 begin
10014 N1 := First (L1);
10015 N2 := First (L2);
10017 -- Compare two lists, skipping rewrite insertions (we want to compare
10018 -- the original trees, not the expanded versions).
10020 loop
10021 if Is_Rewrite_Insertion (N1) then
10022 Next (N1);
10023 elsif Is_Rewrite_Insertion (N2) then
10024 Next (N2);
10025 elsif No (N1) then
10026 return No (N2);
10027 elsif No (N2) then
10028 return False;
10029 elsif not FCE (N1, N2) then
10030 return False;
10031 else
10032 Next (N1);
10033 Next (N2);
10034 end if;
10035 end loop;
10036 end FCL;
10038 ---------
10039 -- FCO --
10040 ---------
10042 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
10043 Actuals : constant List_Id := Parameter_Associations (Call_Node);
10044 Act : Node_Id;
10046 begin
10047 if No (Actuals)
10048 or else Entity (Op_Node) /= Entity (Name (Call_Node))
10049 then
10050 return False;
10052 else
10053 Act := First (Actuals);
10055 if Nkind (Op_Node) in N_Binary_Op then
10056 if not FCE (Left_Opnd (Op_Node), Act) then
10057 return False;
10058 end if;
10060 Next (Act);
10061 end if;
10063 return Present (Act)
10064 and then FCE (Right_Opnd (Op_Node), Act)
10065 and then No (Next (Act));
10066 end if;
10067 end FCO;
10069 function User_Defined_Numeric_Literal_Mismatch return Boolean;
10070 -- Usually literals with the same value like 12345 and 12_345
10071 -- or 123.0 and 123.00 conform, but not if they are
10072 -- user-defined literals.
10074 -------------------------------------------
10075 -- User_Defined_Numeric_Literal_Mismatch --
10076 -------------------------------------------
10078 function User_Defined_Numeric_Literal_Mismatch return Boolean is
10079 E1_Is_User_Defined : constant Boolean :=
10080 Nkind (Given_E1) not in N_Integer_Literal | N_Real_Literal;
10081 E2_Is_User_Defined : constant Boolean :=
10082 Nkind (Given_E2) not in N_Integer_Literal | N_Real_Literal;
10084 begin
10085 pragma Assert (E1_Is_User_Defined = E2_Is_User_Defined);
10087 return E1_Is_User_Defined and then
10088 not String_Equal (String_From_Numeric_Literal (E1),
10089 String_From_Numeric_Literal (E2));
10090 end User_Defined_Numeric_Literal_Mismatch;
10092 -- Local variables
10094 Result : Boolean;
10096 -- Start of processing for Fully_Conformant_Expressions
10098 begin
10099 Result := True;
10101 -- Nonconformant if paren count does not match. Note: if some idiot
10102 -- complains that we don't do this right for more than 3 levels of
10103 -- parentheses, they will be treated with the respect they deserve.
10105 if Paren_Count (E1) /= Paren_Count (E2) then
10106 return False;
10108 -- If same entities are referenced, then they are conformant even if
10109 -- they have different forms (RM 8.3.1(19-20)).
10111 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
10112 if Present (Entity (E1)) then
10113 Result := Entity (E1) = Entity (E2)
10115 -- One may be a discriminant that has been replaced by the
10116 -- corresponding discriminal.
10118 or else
10119 (Chars (Entity (E1)) = Chars (Entity (E2))
10120 and then Ekind (Entity (E1)) = E_Discriminant
10121 and then Ekind (Entity (E2)) = E_In_Parameter)
10123 -- The discriminant of a protected type is transformed into
10124 -- a local constant and then into a parameter of a protected
10125 -- operation.
10127 or else
10128 (Ekind (Entity (E1)) = E_Constant
10129 and then Ekind (Entity (E2)) = E_In_Parameter
10130 and then Present (Discriminal_Link (Entity (E1)))
10131 and then Discriminal_Link (Entity (E1)) =
10132 Discriminal_Link (Entity (E2)))
10134 -- AI12-050: The entities of quantified expressions match if they
10135 -- have the same identifier, even if they may be distinct nodes.
10137 or else
10138 (Chars (Entity (E1)) = Chars (Entity (E2))
10139 and then Is_Entity_Of_Quantified_Expression (Entity (E1))
10140 and then Is_Entity_Of_Quantified_Expression (Entity (E2)))
10142 -- A call to an instantiation of Unchecked_Conversion is
10143 -- rewritten with the name of the generated function created for
10144 -- the instance, and this must be special-cased.
10146 or else
10147 (Ekind (Entity (E1)) = E_Function
10148 and then Is_Intrinsic_Subprogram (Entity (E1))
10149 and then Is_Generic_Instance (Entity (E1))
10150 and then Entity (E2) = Alias (Entity (E1)));
10151 if Report and not Result then
10152 Error_Msg_Sloc :=
10153 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
10154 Error_Msg_NE
10155 ("meaning of& differs because of declaration#", E1, E2);
10156 end if;
10158 return Result;
10160 elsif Nkind (E1) = N_Expanded_Name
10161 and then Nkind (E2) = N_Expanded_Name
10162 and then Nkind (Selector_Name (E1)) = N_Character_Literal
10163 and then Nkind (Selector_Name (E2)) = N_Character_Literal
10164 then
10165 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
10167 else
10168 -- Identifiers in component associations don't always have
10169 -- entities, but their names must conform.
10171 return Nkind (E1) = N_Identifier
10172 and then Nkind (E2) = N_Identifier
10173 and then Chars (E1) = Chars (E2);
10174 end if;
10176 elsif Nkind (E1) = N_Character_Literal
10177 and then Nkind (E2) = N_Expanded_Name
10178 then
10179 return Nkind (Selector_Name (E2)) = N_Character_Literal
10180 and then Chars (E1) = Chars (Selector_Name (E2));
10182 elsif Nkind (E2) = N_Character_Literal
10183 and then Nkind (E1) = N_Expanded_Name
10184 then
10185 return Nkind (Selector_Name (E1)) = N_Character_Literal
10186 and then Chars (E2) = Chars (Selector_Name (E1));
10188 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
10189 return FCO (E1, E2);
10191 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
10192 return FCO (E2, E1);
10194 -- Otherwise we must have the same syntactic entity
10196 elsif Nkind (E1) /= Nkind (E2) then
10197 return False;
10199 -- At this point, we specialize by node type
10201 else
10202 case Nkind (E1) is
10203 when N_Aggregate =>
10204 return
10205 FCL (Expressions (E1), Expressions (E2))
10206 and then
10207 FCL (Component_Associations (E1),
10208 Component_Associations (E2));
10210 when N_Allocator =>
10211 if Nkind (Expression (E1)) = N_Qualified_Expression
10212 or else
10213 Nkind (Expression (E2)) = N_Qualified_Expression
10214 then
10215 return FCE (Expression (E1), Expression (E2));
10217 -- Check that the subtype marks and any constraints
10218 -- are conformant
10220 else
10221 declare
10222 Indic1 : constant Node_Id := Expression (E1);
10223 Indic2 : constant Node_Id := Expression (E2);
10224 Elt1 : Node_Id;
10225 Elt2 : Node_Id;
10227 begin
10228 if Nkind (Indic1) /= N_Subtype_Indication then
10229 return
10230 Nkind (Indic2) /= N_Subtype_Indication
10231 and then Entity (Indic1) = Entity (Indic2);
10233 elsif Nkind (Indic2) /= N_Subtype_Indication then
10234 return
10235 Nkind (Indic1) /= N_Subtype_Indication
10236 and then Entity (Indic1) = Entity (Indic2);
10238 else
10239 if Entity (Subtype_Mark (Indic1)) /=
10240 Entity (Subtype_Mark (Indic2))
10241 then
10242 return False;
10243 end if;
10245 Elt1 := First (Constraints (Constraint (Indic1)));
10246 Elt2 := First (Constraints (Constraint (Indic2)));
10247 while Present (Elt1) and then Present (Elt2) loop
10248 if not FCE (Elt1, Elt2) then
10249 return False;
10250 end if;
10252 Next (Elt1);
10253 Next (Elt2);
10254 end loop;
10256 return True;
10257 end if;
10258 end;
10259 end if;
10261 when N_Attribute_Reference =>
10262 return
10263 Attribute_Name (E1) = Attribute_Name (E2)
10264 and then FCL (Expressions (E1), Expressions (E2));
10266 when N_Binary_Op =>
10267 return
10268 Entity (E1) = Entity (E2)
10269 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
10270 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
10272 when N_Membership_Test
10273 | N_Short_Circuit
10275 return
10276 FCE (Left_Opnd (E1), Left_Opnd (E2))
10277 and then
10278 FCE (Right_Opnd (E1), Right_Opnd (E2));
10280 when N_Case_Expression =>
10281 declare
10282 Alt1 : Node_Id;
10283 Alt2 : Node_Id;
10285 begin
10286 if not FCE (Expression (E1), Expression (E2)) then
10287 return False;
10289 else
10290 Alt1 := First (Alternatives (E1));
10291 Alt2 := First (Alternatives (E2));
10292 loop
10293 if Present (Alt1) /= Present (Alt2) then
10294 return False;
10295 elsif No (Alt1) then
10296 return True;
10297 end if;
10299 if not FCE (Expression (Alt1), Expression (Alt2))
10300 or else not FCL (Discrete_Choices (Alt1),
10301 Discrete_Choices (Alt2))
10302 then
10303 return False;
10304 end if;
10306 Next (Alt1);
10307 Next (Alt2);
10308 end loop;
10309 end if;
10310 end;
10312 when N_Character_Literal =>
10313 return
10314 Char_Literal_Value (E1) = Char_Literal_Value (E2);
10316 when N_Component_Association =>
10317 return
10318 FCL (Choices (E1), Choices (E2))
10319 and then
10320 FCE (Expression (E1), Expression (E2));
10322 when N_Explicit_Dereference =>
10323 return
10324 FCE (Prefix (E1), Prefix (E2));
10326 when N_Extension_Aggregate =>
10327 return
10328 FCL (Expressions (E1), Expressions (E2))
10329 and then Null_Record_Present (E1) =
10330 Null_Record_Present (E2)
10331 and then FCL (Component_Associations (E1),
10332 Component_Associations (E2));
10334 when N_Function_Call =>
10335 return
10336 FCE (Name (E1), Name (E2))
10337 and then
10338 FCL (Parameter_Associations (E1),
10339 Parameter_Associations (E2));
10341 when N_If_Expression =>
10342 return
10343 FCL (Expressions (E1), Expressions (E2));
10345 when N_Indexed_Component =>
10346 return
10347 FCE (Prefix (E1), Prefix (E2))
10348 and then
10349 FCL (Expressions (E1), Expressions (E2));
10351 when N_Integer_Literal =>
10352 return (Intval (E1) = Intval (E2))
10353 and then not User_Defined_Numeric_Literal_Mismatch;
10355 when N_Null =>
10356 return True;
10358 when N_Operator_Symbol =>
10359 return
10360 Chars (E1) = Chars (E2);
10362 when N_Others_Choice =>
10363 return True;
10365 when N_Parameter_Association =>
10366 return
10367 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
10368 and then FCE (Explicit_Actual_Parameter (E1),
10369 Explicit_Actual_Parameter (E2));
10371 when N_Qualified_Expression
10372 | N_Type_Conversion
10373 | N_Unchecked_Type_Conversion
10375 return
10376 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
10377 and then
10378 FCE (Expression (E1), Expression (E2));
10380 when N_Quantified_Expression =>
10381 if not FCE (Condition (E1), Condition (E2)) then
10382 return False;
10383 end if;
10385 if Present (Loop_Parameter_Specification (E1))
10386 and then Present (Loop_Parameter_Specification (E2))
10387 then
10388 declare
10389 L1 : constant Node_Id :=
10390 Loop_Parameter_Specification (E1);
10391 L2 : constant Node_Id :=
10392 Loop_Parameter_Specification (E2);
10394 begin
10395 return
10396 Reverse_Present (L1) = Reverse_Present (L2)
10397 and then
10398 FCE (Defining_Identifier (L1),
10399 Defining_Identifier (L2))
10400 and then
10401 FCE (Discrete_Subtype_Definition (L1),
10402 Discrete_Subtype_Definition (L2));
10403 end;
10405 elsif Present (Iterator_Specification (E1))
10406 and then Present (Iterator_Specification (E2))
10407 then
10408 declare
10409 I1 : constant Node_Id := Iterator_Specification (E1);
10410 I2 : constant Node_Id := Iterator_Specification (E2);
10412 begin
10413 return
10414 FCE (Defining_Identifier (I1),
10415 Defining_Identifier (I2))
10416 and then
10417 Of_Present (I1) = Of_Present (I2)
10418 and then
10419 Reverse_Present (I1) = Reverse_Present (I2)
10420 and then FCE (Name (I1), Name (I2))
10421 and then FCE (Subtype_Indication (I1),
10422 Subtype_Indication (I2));
10423 end;
10425 -- The quantified expressions used different specifications to
10426 -- walk their respective ranges.
10428 else
10429 return False;
10430 end if;
10432 when N_Range =>
10433 return
10434 FCE (Low_Bound (E1), Low_Bound (E2))
10435 and then
10436 FCE (High_Bound (E1), High_Bound (E2));
10438 when N_Real_Literal =>
10439 return (Realval (E1) = Realval (E2))
10440 and then not User_Defined_Numeric_Literal_Mismatch;
10442 when N_Selected_Component =>
10443 return
10444 FCE (Prefix (E1), Prefix (E2))
10445 and then
10446 FCE (Selector_Name (E1), Selector_Name (E2));
10448 when N_Slice =>
10449 return
10450 FCE (Prefix (E1), Prefix (E2))
10451 and then
10452 FCE (Discrete_Range (E1), Discrete_Range (E2));
10454 when N_String_Literal =>
10455 declare
10456 S1 : constant String_Id := Strval (E1);
10457 S2 : constant String_Id := Strval (E2);
10458 L1 : constant Nat := String_Length (S1);
10459 L2 : constant Nat := String_Length (S2);
10461 begin
10462 if L1 /= L2 then
10463 return False;
10465 else
10466 for J in 1 .. L1 loop
10467 if Get_String_Char (S1, J) /=
10468 Get_String_Char (S2, J)
10469 then
10470 return False;
10471 end if;
10472 end loop;
10474 return True;
10475 end if;
10476 end;
10478 when N_Unary_Op =>
10479 return
10480 Entity (E1) = Entity (E2)
10481 and then
10482 FCE (Right_Opnd (E1), Right_Opnd (E2));
10484 -- All other node types cannot appear in this context. Strictly
10485 -- we should raise a fatal internal error. Instead we just ignore
10486 -- the nodes. This means that if anyone makes a mistake in the
10487 -- expander and mucks an expression tree irretrievably, the result
10488 -- will be a failure to detect a (probably very obscure) case
10489 -- of non-conformance, which is better than bombing on some
10490 -- case where two expressions do in fact conform.
10492 when others =>
10493 return True;
10494 end case;
10495 end if;
10496 end Fully_Conformant_Expressions;
10498 ----------------------------------------
10499 -- Fully_Conformant_Discrete_Subtypes --
10500 ----------------------------------------
10502 function Fully_Conformant_Discrete_Subtypes
10503 (Given_S1 : Node_Id;
10504 Given_S2 : Node_Id) return Boolean
10506 S1 : constant Node_Id := Original_Node (Given_S1);
10507 S2 : constant Node_Id := Original_Node (Given_S2);
10509 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
10510 -- Special-case for a bound given by a discriminant, which in the body
10511 -- is replaced with the discriminal of the enclosing type.
10513 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
10514 -- Check both bounds
10516 -----------------------
10517 -- Conforming_Bounds --
10518 -----------------------
10520 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
10521 begin
10522 if Is_Entity_Name (B1)
10523 and then Is_Entity_Name (B2)
10524 and then Ekind (Entity (B1)) = E_Discriminant
10525 then
10526 return Chars (B1) = Chars (B2);
10528 else
10529 return Fully_Conformant_Expressions (B1, B2);
10530 end if;
10531 end Conforming_Bounds;
10533 -----------------------
10534 -- Conforming_Ranges --
10535 -----------------------
10537 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
10538 begin
10539 return
10540 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
10541 and then
10542 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
10543 end Conforming_Ranges;
10545 -- Start of processing for Fully_Conformant_Discrete_Subtypes
10547 begin
10548 if Nkind (S1) /= Nkind (S2) then
10549 return False;
10551 elsif Is_Entity_Name (S1) then
10552 return Entity (S1) = Entity (S2);
10554 elsif Nkind (S1) = N_Range then
10555 return Conforming_Ranges (S1, S2);
10557 elsif Nkind (S1) = N_Subtype_Indication then
10558 return
10559 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
10560 and then
10561 Conforming_Ranges
10562 (Range_Expression (Constraint (S1)),
10563 Range_Expression (Constraint (S2)));
10564 else
10565 return True;
10566 end if;
10567 end Fully_Conformant_Discrete_Subtypes;
10569 ---------------------
10570 -- Has_BIP_Formals --
10571 ---------------------
10573 function Has_BIP_Formals (E : Entity_Id) return Boolean is
10574 Formal : Entity_Id := First_Formal_With_Extras (E);
10576 begin
10577 while Present (Formal) loop
10578 if Is_Build_In_Place_Entity (Formal) then
10579 return True;
10580 end if;
10582 Next_Formal_With_Extras (Formal);
10583 end loop;
10585 return False;
10586 end Has_BIP_Formals;
10588 --------------------------------
10589 -- Has_Reliable_Extra_Formals --
10590 --------------------------------
10592 function Has_Reliable_Extra_Formals (E : Entity_Id) return Boolean is
10593 Alias_E : Entity_Id;
10595 begin
10596 -- Extra formals are not added if expansion is not active (and hence if
10597 -- available they are not reliable for extra formals check).
10599 if not Expander_Active then
10600 return False;
10602 -- Currently the unique cases where extra formals are not reliable
10603 -- are associated with predefined dispatching operations; otherwise
10604 -- they are properly added when required.
10606 elsif not Is_Predefined_Dispatching_Operation (E) then
10607 return True;
10608 end if;
10610 Alias_E := Ultimate_Alias (E);
10612 -- Abstract predefined primitives of Root_Controlled don't have
10613 -- extra formals; this is required to build the runtime.
10615 if Ekind (Alias_E) = E_Function
10616 and then Is_Abstract_Subprogram (Alias_E)
10617 and then Is_RTE (Underlying_Type (Etype (Alias_E)),
10618 RE_Root_Controlled)
10619 then
10620 return False;
10622 -- Predefined stream dispatching operations that are not emitted by
10623 -- the frontend; they have a renaming of the corresponding primitive
10624 -- of their parent type and hence they don't have extra formals.
10626 else
10627 declare
10628 Typ : constant Entity_Id :=
10629 Underlying_Type (Find_Dispatching_Type (Alias_E));
10631 begin
10632 if (Get_TSS_Name (E) = TSS_Stream_Input
10633 and then not Stream_Operation_OK (Typ, TSS_Stream_Input))
10634 or else
10635 (Get_TSS_Name (E) = TSS_Stream_Output
10636 and then not Stream_Operation_OK (Typ, TSS_Stream_Output))
10637 or else
10638 (Get_TSS_Name (E) = TSS_Stream_Read
10639 and then not Stream_Operation_OK (Typ, TSS_Stream_Read))
10640 or else
10641 (Get_TSS_Name (E) = TSS_Stream_Write
10642 and then not Stream_Operation_OK (Typ, TSS_Stream_Write))
10643 then
10644 return False;
10645 end if;
10646 end;
10647 end if;
10649 return True;
10650 end Has_Reliable_Extra_Formals;
10652 --------------------
10653 -- Install_Entity --
10654 --------------------
10656 procedure Install_Entity (E : Entity_Id) is
10657 Prev : constant Entity_Id := Current_Entity (E);
10658 begin
10659 Set_Is_Immediately_Visible (E);
10660 Set_Current_Entity (E);
10661 pragma Assert (Prev /= E);
10662 Set_Homonym (E, Prev);
10663 end Install_Entity;
10665 ---------------------
10666 -- Install_Formals --
10667 ---------------------
10669 procedure Install_Formals (Id : Entity_Id) is
10670 F : Entity_Id;
10671 begin
10672 F := First_Formal (Id);
10673 while Present (F) loop
10674 Install_Entity (F);
10675 Next_Formal (F);
10676 end loop;
10677 end Install_Formals;
10679 -----------------------------
10680 -- Is_Interface_Conformant --
10681 -----------------------------
10683 function Is_Interface_Conformant
10684 (Tagged_Type : Entity_Id;
10685 Iface_Prim : Entity_Id;
10686 Prim : Entity_Id) return Boolean
10688 -- The operation may in fact be an inherited (implicit) operation
10689 -- rather than the original interface primitive, so retrieve the
10690 -- ultimate ancestor.
10692 Iface : constant Entity_Id :=
10693 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
10694 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
10696 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
10697 -- Return the controlling formal of Prim
10699 ------------------------
10700 -- Controlling_Formal --
10701 ------------------------
10703 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
10704 E : Entity_Id;
10706 begin
10707 E := First_Formal (Prim);
10708 while Present (E) loop
10709 if Is_Controlling_Formal (E) then
10710 return E;
10711 end if;
10713 Next_Formal (E);
10714 end loop;
10716 return Empty;
10717 end Controlling_Formal;
10719 -- Local variables
10721 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
10722 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
10724 -- Start of processing for Is_Interface_Conformant
10726 begin
10727 pragma Assert (Is_Subprogram (Iface_Prim)
10728 and then Is_Subprogram (Prim)
10729 and then Is_Dispatching_Operation (Iface_Prim)
10730 and then Is_Dispatching_Operation (Prim));
10732 pragma Assert (Is_Interface (Iface)
10733 or else (Present (Alias (Iface_Prim))
10734 and then
10735 Is_Interface
10736 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
10738 if Prim = Iface_Prim
10739 or else not Is_Subprogram (Prim)
10740 or else Ekind (Prim) /= Ekind (Iface_Prim)
10741 or else not Is_Dispatching_Operation (Prim)
10742 or else Scope (Prim) /= Scope (Tagged_Type)
10743 or else No (Typ)
10744 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
10745 or else not Primitive_Names_Match (Iface_Prim, Prim)
10746 then
10747 return False;
10749 -- The mode of the controlling formals must match
10751 elsif Present (Iface_Ctrl_F)
10752 and then Present (Prim_Ctrl_F)
10753 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
10754 then
10755 return False;
10757 -- Case of a procedure, or a function whose result type matches the
10758 -- result type of the interface primitive, or a function that has no
10759 -- controlling result (I or access I).
10761 elsif Ekind (Iface_Prim) = E_Procedure
10762 or else Etype (Prim) = Etype (Iface_Prim)
10763 or else not Has_Controlling_Result (Prim)
10764 then
10765 return Type_Conformant
10766 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
10768 -- Case of a function returning an interface, or an access to one. Check
10769 -- that the return types correspond.
10771 elsif Implements_Interface (Typ, Iface) then
10772 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10774 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10775 then
10776 return False;
10777 else
10778 return
10779 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10780 Skip_Controlling_Formals => True);
10781 end if;
10783 else
10784 return False;
10785 end if;
10786 end Is_Interface_Conformant;
10788 ---------------------------------
10789 -- Is_Non_Overriding_Operation --
10790 ---------------------------------
10792 function Is_Non_Overriding_Operation
10793 (Prev_E : Entity_Id;
10794 New_E : Entity_Id) return Boolean
10796 Formal : Entity_Id;
10797 F_Typ : Entity_Id;
10798 G_Typ : Entity_Id := Empty;
10800 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10801 -- If F_Type is a derived type associated with a generic actual subtype,
10802 -- then return its Generic_Parent_Type attribute, else return Empty.
10804 function Types_Correspond
10805 (P_Type : Entity_Id;
10806 N_Type : Entity_Id) return Boolean;
10807 -- Returns true if and only if the types (or designated types in the
10808 -- case of anonymous access types) are the same or N_Type is derived
10809 -- directly or indirectly from P_Type.
10811 -----------------------------
10812 -- Get_Generic_Parent_Type --
10813 -----------------------------
10815 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10816 G_Typ : Entity_Id;
10817 Defn : Node_Id;
10818 Indic : Node_Id;
10820 begin
10821 if Is_Derived_Type (F_Typ)
10822 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10823 then
10824 -- The tree must be traversed to determine the parent subtype in
10825 -- the generic unit, which unfortunately isn't always available
10826 -- via semantic attributes. ??? (Note: The use of Original_Node
10827 -- is needed for cases where a full derived type has been
10828 -- rewritten.)
10830 -- If the parent type is a scalar type, the derivation creates
10831 -- an anonymous base type for it, and the source type is its
10832 -- first subtype.
10834 if Is_Scalar_Type (F_Typ)
10835 and then not Comes_From_Source (F_Typ)
10836 then
10837 Defn :=
10838 Type_Definition
10839 (Original_Node (Parent (First_Subtype (F_Typ))));
10840 else
10841 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10842 end if;
10843 if Nkind (Defn) = N_Derived_Type_Definition then
10844 Indic := Subtype_Indication (Defn);
10846 if Nkind (Indic) = N_Subtype_Indication then
10847 G_Typ := Entity (Subtype_Mark (Indic));
10848 else
10849 G_Typ := Entity (Indic);
10850 end if;
10852 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10853 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10854 then
10855 return Generic_Parent_Type (Parent (G_Typ));
10856 end if;
10857 end if;
10858 end if;
10860 return Empty;
10861 end Get_Generic_Parent_Type;
10863 ----------------------
10864 -- Types_Correspond --
10865 ----------------------
10867 function Types_Correspond
10868 (P_Type : Entity_Id;
10869 N_Type : Entity_Id) return Boolean
10871 Prev_Type : Entity_Id := Base_Type (P_Type);
10872 New_Type : Entity_Id := Base_Type (N_Type);
10874 begin
10875 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10876 Prev_Type := Designated_Type (Prev_Type);
10877 end if;
10879 if Ekind (New_Type) = E_Anonymous_Access_Type then
10880 New_Type := Designated_Type (New_Type);
10881 end if;
10883 if Prev_Type = New_Type then
10884 return True;
10886 elsif not Is_Class_Wide_Type (New_Type) then
10887 while Etype (New_Type) /= New_Type loop
10888 New_Type := Etype (New_Type);
10890 if New_Type = Prev_Type then
10891 return True;
10892 end if;
10893 end loop;
10894 end if;
10895 return False;
10896 end Types_Correspond;
10898 -- Start of processing for Is_Non_Overriding_Operation
10900 begin
10901 -- In the case where both operations are implicit derived subprograms
10902 -- then neither overrides the other. This can only occur in certain
10903 -- obscure cases (e.g., derivation from homographs created in a generic
10904 -- instantiation).
10906 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10907 return True;
10909 elsif Ekind (Current_Scope) = E_Package
10910 and then Is_Generic_Instance (Current_Scope)
10911 and then In_Private_Part (Current_Scope)
10912 and then Comes_From_Source (New_E)
10913 then
10914 -- We examine the formals and result type of the inherited operation,
10915 -- to determine whether their type is derived from (the instance of)
10916 -- a generic type. The first such formal or result type is the one
10917 -- tested.
10919 Formal := First_Formal (Prev_E);
10920 F_Typ := Empty;
10921 while Present (Formal) loop
10922 F_Typ := Base_Type (Etype (Formal));
10924 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10925 F_Typ := Designated_Type (F_Typ);
10926 end if;
10928 G_Typ := Get_Generic_Parent_Type (F_Typ);
10929 exit when Present (G_Typ);
10931 Next_Formal (Formal);
10932 end loop;
10934 -- If the function dispatches on result check the result type
10936 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10937 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10938 end if;
10940 if No (G_Typ) then
10941 return False;
10942 end if;
10944 -- If the generic type is a private type, then the original operation
10945 -- was not overriding in the generic, because there was no primitive
10946 -- operation to override.
10948 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10949 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10950 N_Formal_Private_Type_Definition
10951 then
10952 return True;
10954 -- The generic parent type is the ancestor of a formal derived
10955 -- type declaration. We need to check whether it has a primitive
10956 -- operation that should be overridden by New_E in the generic.
10958 else
10959 declare
10960 P_Formal : Entity_Id;
10961 N_Formal : Entity_Id;
10962 P_Typ : Entity_Id;
10963 N_Typ : Entity_Id;
10964 P_Prim : Entity_Id;
10965 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10967 begin
10968 while Present (Prim_Elt) loop
10969 P_Prim := Node (Prim_Elt);
10971 if Chars (P_Prim) = Chars (New_E)
10972 and then Ekind (P_Prim) = Ekind (New_E)
10973 then
10974 P_Formal := First_Formal (P_Prim);
10975 N_Formal := First_Formal (New_E);
10976 while Present (P_Formal) and then Present (N_Formal) loop
10977 P_Typ := Etype (P_Formal);
10978 N_Typ := Etype (N_Formal);
10980 if not Types_Correspond (P_Typ, N_Typ) then
10981 exit;
10982 end if;
10984 Next_Formal (P_Formal);
10985 Next_Formal (N_Formal);
10986 end loop;
10988 -- Found a matching primitive operation belonging to the
10989 -- formal ancestor type, so the new subprogram is
10990 -- overriding.
10992 if No (P_Formal)
10993 and then No (N_Formal)
10994 and then (Ekind (New_E) /= E_Function
10995 or else
10996 Types_Correspond
10997 (Etype (P_Prim), Etype (New_E)))
10998 then
10999 return False;
11000 end if;
11001 end if;
11003 Next_Elmt (Prim_Elt);
11004 end loop;
11006 -- If no match found, then the new subprogram does not override
11007 -- in the generic (nor in the instance).
11009 -- If the type in question is not abstract, and the subprogram
11010 -- is, this will be an error if the new operation is in the
11011 -- private part of the instance. Emit a warning now, which will
11012 -- make the subsequent error message easier to understand.
11014 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
11015 and then Is_Abstract_Subprogram (Prev_E)
11016 and then In_Private_Part (Current_Scope)
11017 then
11018 Error_Msg_Node_2 := F_Typ;
11019 Error_Msg_NE
11020 ("private operation& in generic unit does not override "
11021 & "any primitive operation of& (RM 12.3(18))??",
11022 New_E, New_E);
11023 end if;
11025 return True;
11026 end;
11027 end if;
11028 else
11029 return False;
11030 end if;
11031 end Is_Non_Overriding_Operation;
11033 -------------------------------------
11034 -- List_Inherited_Pre_Post_Aspects --
11035 -------------------------------------
11037 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
11038 begin
11039 if List_Inherited_Aspects
11040 and then Is_Subprogram_Or_Generic_Subprogram (E)
11041 then
11042 declare
11043 Subps : constant Subprogram_List := Inherited_Subprograms (E);
11044 Items : Node_Id;
11045 Prag : Node_Id;
11047 begin
11048 for Index in Subps'Range loop
11049 Items := Contract (Subps (Index));
11051 if Present (Items) then
11052 Prag := Pre_Post_Conditions (Items);
11053 while Present (Prag) loop
11054 Error_Msg_Sloc := Sloc (Prag);
11056 if Class_Present (Prag)
11057 and then not Split_PPC (Prag)
11058 then
11059 if Pragma_Name (Prag) = Name_Precondition then
11060 Error_Msg_N
11061 ("info: & inherits `Pre''Class` aspect from "
11062 & "#?.l?", E);
11063 else
11064 Error_Msg_N
11065 ("info: & inherits `Post''Class` aspect from "
11066 & "#?.l?", E);
11067 end if;
11068 end if;
11070 Prag := Next_Pragma (Prag);
11071 end loop;
11072 end if;
11073 end loop;
11074 end;
11075 end if;
11076 end List_Inherited_Pre_Post_Aspects;
11078 ------------------------------
11079 -- Make_Inequality_Operator --
11080 ------------------------------
11082 -- S is the defining identifier of an equality operator. We build a
11083 -- subprogram declaration with the right signature. This operation is
11084 -- intrinsic, because it is always expanded as the negation of the
11085 -- call to the equality function.
11087 procedure Make_Inequality_Operator (S : Entity_Id) is
11088 Loc : constant Source_Ptr := Sloc (S);
11089 Decl : Node_Id;
11090 Formals : List_Id;
11091 Op_Name : Entity_Id;
11093 FF : constant Entity_Id := First_Formal (S);
11094 NF : constant Entity_Id := Next_Formal (FF);
11096 begin
11097 -- Check that equality was properly defined, ignore call if not
11099 if No (NF) then
11100 return;
11101 end if;
11103 declare
11104 A : constant Entity_Id :=
11105 Make_Defining_Identifier (Sloc (FF),
11106 Chars => Chars (FF));
11108 B : constant Entity_Id :=
11109 Make_Defining_Identifier (Sloc (NF),
11110 Chars => Chars (NF));
11112 begin
11113 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
11115 Formals := New_List (
11116 Make_Parameter_Specification (Loc,
11117 Defining_Identifier => A,
11118 Parameter_Type =>
11119 New_Occurrence_Of (Etype (First_Formal (S)),
11120 Sloc (Etype (First_Formal (S))))),
11122 Make_Parameter_Specification (Loc,
11123 Defining_Identifier => B,
11124 Parameter_Type =>
11125 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
11126 Sloc (Etype (Next_Formal (First_Formal (S)))))));
11128 Decl :=
11129 Make_Subprogram_Declaration (Loc,
11130 Specification =>
11131 Make_Function_Specification (Loc,
11132 Defining_Unit_Name => Op_Name,
11133 Parameter_Specifications => Formals,
11134 Result_Definition =>
11135 New_Occurrence_Of (Standard_Boolean, Loc)));
11137 -- Insert inequality right after equality if it is explicit or after
11138 -- the derived type when implicit. These entities are created only
11139 -- for visibility purposes, and eventually replaced in the course
11140 -- of expansion, so they do not need to be attached to the tree and
11141 -- seen by the back-end. Keeping them internal also avoids spurious
11142 -- freezing problems. The declaration is inserted in the tree for
11143 -- analysis, and removed afterwards. If the equality operator comes
11144 -- from an explicit declaration, attach the inequality immediately
11145 -- after. Else the equality is inherited from a derived type
11146 -- declaration, so insert inequality after that declaration.
11148 if No (Alias (S)) then
11149 Insert_After (Unit_Declaration_Node (S), Decl);
11150 elsif Is_List_Member (Parent (S)) then
11151 Insert_After (Parent (S), Decl);
11152 else
11153 Insert_After (Parent (Etype (First_Formal (S))), Decl);
11154 end if;
11156 Mark_Rewrite_Insertion (Decl);
11157 Set_Is_Intrinsic_Subprogram (Op_Name);
11158 Analyze (Decl);
11159 Remove (Decl);
11160 Set_Has_Completion (Op_Name);
11161 Set_Corresponding_Equality (Op_Name, S);
11162 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
11163 end;
11164 end Make_Inequality_Operator;
11166 ----------------------
11167 -- May_Need_Actuals --
11168 ----------------------
11170 procedure May_Need_Actuals (Fun : Entity_Id) is
11171 F : Entity_Id;
11172 B : Boolean;
11174 begin
11175 F := First_Formal (Fun);
11176 B := True;
11177 while Present (F) loop
11178 if No (Default_Value (F)) then
11179 B := False;
11180 exit;
11181 end if;
11183 Next_Formal (F);
11184 end loop;
11186 Set_Needs_No_Actuals (Fun, B);
11187 end May_Need_Actuals;
11189 ---------------------
11190 -- Mode_Conformant --
11191 ---------------------
11193 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
11194 Result : Boolean;
11195 begin
11196 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
11197 return Result;
11198 end Mode_Conformant;
11200 ---------------------------
11201 -- New_Overloaded_Entity --
11202 ---------------------------
11204 procedure New_Overloaded_Entity
11205 (S : Entity_Id;
11206 Derived_Type : Entity_Id := Empty)
11208 Overridden_Subp : Entity_Id := Empty;
11209 -- Set if the current scope has an operation that is type-conformant
11210 -- with S, and becomes hidden by S.
11212 Is_Primitive_Subp : Boolean;
11213 -- Set to True if the new subprogram is primitive
11215 E : Entity_Id;
11216 -- Entity that S overrides
11218 procedure Check_For_Primitive_Subprogram
11219 (Is_Primitive : out Boolean;
11220 Is_Overriding : Boolean := False);
11221 -- If the subprogram being analyzed is a primitive operation of the type
11222 -- of a formal or result, set the Has_Primitive_Operations flag on the
11223 -- type, and set Is_Primitive to True (otherwise set to False). Set the
11224 -- corresponding flag on the entity itself for later use.
11226 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
11227 -- True if a) E is a subprogram whose first formal is a concurrent type
11228 -- defined in the scope of E that has some entry or subprogram whose
11229 -- profile matches E, or b) E is an internally built dispatching
11230 -- subprogram of a protected type and there is a matching subprogram
11231 -- defined in the enclosing scope of the protected type, or c) E is
11232 -- an entry of a synchronized type and a matching procedure has been
11233 -- previously defined in the enclosing scope of the synchronized type.
11235 function Is_Private_Declaration (E : Entity_Id) return Boolean;
11236 -- Check that E is declared in the private part of the current package,
11237 -- or in the package body, where it may hide a previous declaration.
11238 -- We can't use In_Private_Part by itself because this flag is also
11239 -- set when freezing entities, so we must examine the place of the
11240 -- declaration in the tree, and recognize wrapper packages as well.
11242 function Is_Overriding_Alias
11243 (Old_E : Entity_Id;
11244 New_E : Entity_Id) return Boolean;
11245 -- Check whether new subprogram and old subprogram are both inherited
11246 -- from subprograms that have distinct dispatch table entries. This can
11247 -- occur with derivations from instances with accidental homonyms. The
11248 -- function is conservative given that the converse is only true within
11249 -- instances that contain accidental overloadings.
11251 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
11252 -- Report conflict between entities S and E
11254 ------------------------------------
11255 -- Check_For_Primitive_Subprogram --
11256 ------------------------------------
11258 procedure Check_For_Primitive_Subprogram
11259 (Is_Primitive : out Boolean;
11260 Is_Overriding : Boolean := False)
11262 procedure Add_Or_Replace_Untagged_Primitive (Typ : Entity_Id);
11263 -- Either add the new subprogram to the list of primitives for
11264 -- untagged type Typ, or if it overrides a primitive of Typ, then
11265 -- replace the overridden primitive in Typ's primitives list with
11266 -- the new subprogram.
11268 function Visible_Part_Type (T : Entity_Id) return Boolean;
11269 -- Returns true if T is declared in the visible part of the current
11270 -- package scope; otherwise returns false. Assumes that T is declared
11271 -- in a package.
11273 procedure Check_Private_Overriding (T : Entity_Id);
11274 -- Checks that if a primitive abstract subprogram of a visible
11275 -- abstract type is declared in a private part, then it must override
11276 -- an abstract subprogram declared in the visible part. Also checks
11277 -- that if a primitive function with a controlling result is declared
11278 -- in a private part, then it must override a function declared in
11279 -- the visible part.
11281 ---------------------------------------
11282 -- Add_Or_Replace_Untagged_Primitive --
11283 ---------------------------------------
11285 procedure Add_Or_Replace_Untagged_Primitive (Typ : Entity_Id) is
11286 Replaced_Overridden_Subp : Boolean := False;
11288 begin
11289 pragma Assert (not Is_Tagged_Type (Typ));
11291 -- Anonymous access types don't have a primitives list. Normally
11292 -- such types wouldn't make it here, but the case of anonymous
11293 -- access-to-subprogram types can.
11295 if not Is_Anonymous_Access_Type (Typ) then
11297 -- If S overrides a subprogram that's a primitive of
11298 -- the formal's type, then replace the overridden
11299 -- subprogram with the new subprogram in the type's
11300 -- list of primitives.
11302 if Is_Overriding then
11303 pragma Assert (Present (Overridden_Subp)
11304 and then Overridden_Subp = E); -- Added for now
11306 declare
11307 Prim_Ops : constant Elist_Id :=
11308 Primitive_Operations (Typ);
11309 Elmt : Elmt_Id;
11310 begin
11311 if Present (Prim_Ops) then
11312 Elmt := First_Elmt (Prim_Ops);
11314 while Present (Elmt)
11315 and then Node (Elmt) /= Overridden_Subp
11316 loop
11317 Next_Elmt (Elmt);
11318 end loop;
11320 if Present (Elmt) then
11321 Replace_Elmt (Elmt, S);
11322 Replaced_Overridden_Subp := True;
11323 end if;
11324 end if;
11325 end;
11326 end if;
11328 -- If the new subprogram did not override an operation
11329 -- of the formal's type, then add it to the primitives
11330 -- list of the type.
11332 if not Replaced_Overridden_Subp then
11333 Append_Unique_Elmt (S, Primitive_Operations (Typ));
11334 end if;
11335 end if;
11336 end Add_Or_Replace_Untagged_Primitive;
11338 ------------------------------
11339 -- Check_Private_Overriding --
11340 ------------------------------
11342 procedure Check_Private_Overriding (T : Entity_Id) is
11343 function Overrides_Private_Part_Op return Boolean;
11344 -- This detects the special case where the overriding subprogram
11345 -- is overriding a subprogram that was declared in the same
11346 -- private part. That case is illegal by 3.9.3(10).
11348 function Overrides_Visible_Function
11349 (Partial_View : Entity_Id) return Boolean;
11350 -- True if S overrides a function in the visible part. The
11351 -- overridden function could be explicitly or implicitly declared.
11353 -------------------------------
11354 -- Overrides_Private_Part_Op --
11355 -------------------------------
11357 function Overrides_Private_Part_Op return Boolean is
11358 Over_Decl : constant Node_Id :=
11359 Unit_Declaration_Node
11360 (Ultimate_Alias (Overridden_Operation (S)));
11361 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
11363 begin
11364 pragma Assert (Is_Overriding);
11365 pragma Assert
11366 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
11367 pragma Assert
11368 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
11370 return In_Same_List (Over_Decl, Subp_Decl);
11371 end Overrides_Private_Part_Op;
11373 --------------------------------
11374 -- Overrides_Visible_Function --
11375 --------------------------------
11377 function Overrides_Visible_Function
11378 (Partial_View : Entity_Id) return Boolean
11380 begin
11381 if not Is_Overriding or else not Has_Homonym (S) then
11382 return False;
11383 end if;
11385 if No (Partial_View) then
11386 return True;
11387 end if;
11389 -- Search through all the homonyms H of S in the current
11390 -- package spec, and return True if we find one that matches.
11391 -- Note that Parent (H) will be the declaration of the
11392 -- partial view of T for a match.
11394 declare
11395 H : Entity_Id := S;
11396 begin
11397 loop
11398 H := Homonym (H);
11399 exit when No (H) or else Scope (H) /= Scope (S);
11401 if Nkind (Parent (H)) in
11402 N_Private_Extension_Declaration |
11403 N_Private_Type_Declaration
11404 and then Defining_Identifier (Parent (H)) = Partial_View
11405 then
11406 return True;
11407 end if;
11408 end loop;
11409 end;
11411 return False;
11412 end Overrides_Visible_Function;
11414 -- Start of processing for Check_Private_Overriding
11416 begin
11417 if Is_Package_Or_Generic_Package (Current_Scope)
11418 and then In_Private_Part (Current_Scope)
11419 and then Visible_Part_Type (T)
11420 and then not In_Instance
11421 then
11422 if Is_Abstract_Type (T)
11423 and then Is_Abstract_Subprogram (S)
11424 and then (not Is_Overriding
11425 or else not Is_Abstract_Subprogram (E)
11426 or else Overrides_Private_Part_Op)
11427 then
11428 Error_Msg_N
11429 ("abstract subprograms must be visible (RM 3.9.3(10))!",
11432 elsif Ekind (S) = E_Function then
11433 declare
11434 Partial_View : constant Entity_Id :=
11435 Incomplete_Or_Partial_View (T);
11437 begin
11438 if not Overrides_Visible_Function (Partial_View) then
11440 -- Here, S is "function ... return T;" declared in
11441 -- the private part, not overriding some visible
11442 -- operation. That's illegal in the tagged case
11443 -- (but not if the private type is untagged).
11445 if ((Present (Partial_View)
11446 and then Is_Tagged_Type (Partial_View))
11447 or else (No (Partial_View)
11448 and then Is_Tagged_Type (T)))
11449 and then T = Base_Type (Etype (S))
11450 then
11451 Error_Msg_N
11452 ("private function with tagged result must"
11453 & " override visible-part function", S);
11454 Error_Msg_N
11455 ("\move subprogram to the visible part"
11456 & " (RM 3.9.3(10))", S);
11458 -- Ada 2012 (AI05-0073): Extend this check to the case
11459 -- of a function whose result subtype is defined by an
11460 -- access_definition designating specific tagged type.
11462 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
11463 and then Is_Tagged_Type (Designated_Type (Etype (S)))
11464 and then
11465 not Is_Class_Wide_Type
11466 (Designated_Type (Etype (S)))
11467 and then Ada_Version >= Ada_2012
11468 then
11469 Error_Msg_N
11470 ("private function with controlling access "
11471 & "result must override visible-part function",
11473 Error_Msg_N
11474 ("\move subprogram to the visible part"
11475 & " (RM 3.9.3(10))", S);
11476 end if;
11477 end if;
11478 end;
11479 end if;
11480 end if;
11481 end Check_Private_Overriding;
11483 -----------------------
11484 -- Visible_Part_Type --
11485 -----------------------
11487 function Visible_Part_Type (T : Entity_Id) return Boolean is
11488 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
11490 begin
11491 -- If the entity is a private type, then it must be declared in a
11492 -- visible part.
11494 if Is_Private_Type (T) then
11495 return True;
11497 elsif Is_Type (T) and then Has_Private_Declaration (T) then
11498 return True;
11500 elsif Is_List_Member (Declaration_Node (T))
11501 and then List_Containing (Declaration_Node (T)) =
11502 Visible_Declarations (Specification (P))
11503 then
11504 return True;
11506 else
11507 return False;
11508 end if;
11509 end Visible_Part_Type;
11511 -- Local variables
11513 Formal : Entity_Id;
11514 F_Typ : Entity_Id;
11515 B_Typ : Entity_Id;
11517 -- Start of processing for Check_For_Primitive_Subprogram
11519 begin
11520 Is_Primitive := False;
11522 if not Comes_From_Source (S) then
11524 -- Add an inherited primitive for an untagged derived type to
11525 -- Derived_Type's list of primitives. Tagged primitives are
11526 -- dealt with in Check_Dispatching_Operation. Do this even when
11527 -- Extensions_Allowed is False to issue better error messages.
11529 if Present (Derived_Type)
11530 and then not Is_Tagged_Type (Derived_Type)
11531 then
11532 Append_Unique_Elmt (S, Primitive_Operations (Derived_Type));
11533 end if;
11535 -- If subprogram is at library level, it is not primitive operation
11537 elsif Current_Scope = Standard_Standard then
11538 null;
11540 elsif (Is_Package_Or_Generic_Package (Current_Scope)
11541 and then not In_Package_Body (Current_Scope))
11542 or else Is_Overriding
11543 then
11544 -- For function, check return type
11546 if Ekind (S) = E_Function then
11547 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
11548 F_Typ := Designated_Type (Etype (S));
11549 else
11550 F_Typ := Etype (S);
11551 end if;
11553 B_Typ := Base_Type (F_Typ);
11555 if Scope (B_Typ) = Current_Scope
11556 and then not Is_Class_Wide_Type (B_Typ)
11557 and then not Is_Generic_Type (B_Typ)
11558 then
11559 Is_Primitive := True;
11560 Set_Has_Primitive_Operations (B_Typ);
11561 Set_Is_Primitive (S);
11563 -- Add a primitive for an untagged type to B_Typ's
11564 -- list of primitives. Tagged primitives are dealt with
11565 -- in Check_Dispatching_Operation. Do this even when
11566 -- Extensions_Allowed is False to issue better error
11567 -- messages.
11569 if not Is_Tagged_Type (B_Typ) then
11570 Add_Or_Replace_Untagged_Primitive (B_Typ);
11571 end if;
11573 Check_Private_Overriding (B_Typ);
11574 -- The Ghost policy in effect at the point of declaration
11575 -- or a tagged type and a primitive operation must match
11576 -- (SPARK RM 6.9(16)).
11578 Check_Ghost_Primitive (S, B_Typ);
11579 end if;
11580 end if;
11582 -- For all subprograms, check formals
11584 Formal := First_Formal (S);
11585 while Present (Formal) loop
11586 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
11587 F_Typ := Designated_Type (Etype (Formal));
11588 else
11589 F_Typ := Etype (Formal);
11590 end if;
11592 B_Typ := Base_Type (F_Typ);
11594 if Ekind (B_Typ) = E_Access_Subtype then
11595 B_Typ := Base_Type (B_Typ);
11596 end if;
11598 if Scope (B_Typ) = Current_Scope
11599 and then not Is_Class_Wide_Type (B_Typ)
11600 and then not Is_Generic_Type (B_Typ)
11601 then
11602 Is_Primitive := True;
11603 Set_Is_Primitive (S);
11604 Set_Has_Primitive_Operations (B_Typ);
11606 -- Add a primitive for an untagged type to B_Typ's list
11607 -- of primitives. Tagged primitives are dealt with in
11608 -- Check_Dispatching_Operation. Do this even when
11609 -- Extensions_Allowed is False to issue better error
11610 -- messages.
11612 if not Is_Tagged_Type (B_Typ) then
11613 Add_Or_Replace_Untagged_Primitive (B_Typ);
11614 end if;
11616 Check_Private_Overriding (B_Typ);
11618 -- The Ghost policy in effect at the point of declaration
11619 -- of a tagged type and a primitive operation must match
11620 -- (SPARK RM 6.9(16)).
11622 Check_Ghost_Primitive (S, B_Typ);
11623 end if;
11625 Next_Formal (Formal);
11626 end loop;
11628 -- Special case: An equality function can be redefined for a type
11629 -- occurring in a declarative part, and won't otherwise be treated as
11630 -- a primitive because it doesn't occur in a package spec and doesn't
11631 -- override an inherited subprogram. It's important that we mark it
11632 -- primitive so it can be returned by Collect_Primitive_Operations
11633 -- and be used in composing the equality operation of later types
11634 -- that have a component of the type.
11636 elsif Chars (S) = Name_Op_Eq
11637 and then Etype (S) = Standard_Boolean
11638 then
11639 B_Typ := Base_Type (Etype (First_Formal (S)));
11641 if Scope (B_Typ) = Current_Scope
11642 and then
11643 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
11644 and then not Is_Limited_Type (B_Typ)
11645 then
11646 Is_Primitive := True;
11647 Set_Is_Primitive (S);
11648 Set_Has_Primitive_Operations (B_Typ);
11649 Check_Private_Overriding (B_Typ);
11651 -- The Ghost policy in effect at the point of declaration of a
11652 -- tagged type and a primitive operation must match
11653 -- (SPARK RM 6.9(16)).
11655 Check_Ghost_Primitive (S, B_Typ);
11656 end if;
11657 end if;
11658 end Check_For_Primitive_Subprogram;
11660 --------------------------------------
11661 -- Has_Matching_Entry_Or_Subprogram --
11662 --------------------------------------
11664 function Has_Matching_Entry_Or_Subprogram
11665 (E : Entity_Id) return Boolean
11667 function Check_Conforming_Parameters
11668 (E1_Param : Node_Id;
11669 E2_Param : Node_Id;
11670 Ctype : Conformance_Type) return Boolean;
11671 -- Starting from the given parameters, check that all the parameters
11672 -- of two entries or subprograms are conformant. Used to skip
11673 -- the check on the controlling argument.
11675 function Matching_Entry_Or_Subprogram
11676 (Conc_Typ : Entity_Id;
11677 Subp : Entity_Id) return Entity_Id;
11678 -- Return the first entry or subprogram of the given concurrent type
11679 -- whose name matches the name of Subp and has a profile conformant
11680 -- with Subp; return Empty if not found.
11682 function Matching_Dispatching_Subprogram
11683 (Conc_Typ : Entity_Id;
11684 Ent : Entity_Id) return Entity_Id;
11685 -- Return the first dispatching primitive of Conc_Type defined in the
11686 -- enclosing scope of Conc_Type (i.e. before the full definition of
11687 -- this concurrent type) whose name matches the entry Ent and has a
11688 -- profile conformant with the profile of the corresponding (not yet
11689 -- built) dispatching primitive of Ent; return Empty if not found.
11691 function Matching_Original_Protected_Subprogram
11692 (Prot_Typ : Entity_Id;
11693 Subp : Entity_Id) return Entity_Id;
11694 -- Return the first subprogram defined in the enclosing scope of
11695 -- Prot_Typ (before the full definition of this protected type)
11696 -- whose name matches the original name of Subp and has a profile
11697 -- conformant with the profile of Subp; return Empty if not found.
11699 function Normalized_First_Parameter_Type
11700 (E : Entity_Id) return Entity_Id;
11701 -- Return the type of the first parameter unless that type
11702 -- is an anonymous access type, in which case return the
11703 -- designated type. Used to treat anonymous-access-to-synchronized
11704 -- the same as synchronized for purposes of checking for
11705 -- prefixed view profile conflicts.
11707 ---------------------------------
11708 -- Check_Conforming_Parameters --
11709 ---------------------------------
11711 function Check_Conforming_Parameters
11712 (E1_Param : Node_Id;
11713 E2_Param : Node_Id;
11714 Ctype : Conformance_Type) return Boolean
11716 Param_E1 : Node_Id := E1_Param;
11717 Param_E2 : Node_Id := E2_Param;
11719 begin
11720 while Present (Param_E1) and then Present (Param_E2) loop
11721 if (Ctype >= Mode_Conformant) and then
11722 Ekind (Defining_Identifier (Param_E1)) /=
11723 Ekind (Defining_Identifier (Param_E2))
11724 then
11725 return False;
11726 elsif not
11727 Conforming_Types
11728 (Find_Parameter_Type (Param_E1),
11729 Find_Parameter_Type (Param_E2),
11730 Ctype)
11731 then
11732 return False;
11733 end if;
11735 Next (Param_E1);
11736 Next (Param_E2);
11737 end loop;
11739 -- The candidate is not valid if one of the two lists contains
11740 -- more parameters than the other
11742 return No (Param_E1) and then No (Param_E2);
11743 end Check_Conforming_Parameters;
11745 ----------------------------------
11746 -- Matching_Entry_Or_Subprogram --
11747 ----------------------------------
11749 function Matching_Entry_Or_Subprogram
11750 (Conc_Typ : Entity_Id;
11751 Subp : Entity_Id) return Entity_Id
11753 E : Entity_Id;
11755 begin
11756 E := First_Entity (Conc_Typ);
11757 while Present (E) loop
11758 if Chars (Subp) = Chars (E)
11759 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
11760 and then
11761 Check_Conforming_Parameters
11762 (First (Parameter_Specifications (Parent (E))),
11763 Next (First (Parameter_Specifications (Parent (Subp)))),
11764 Type_Conformant)
11765 then
11766 return E;
11767 end if;
11769 Next_Entity (E);
11770 end loop;
11772 return Empty;
11773 end Matching_Entry_Or_Subprogram;
11775 -------------------------------------
11776 -- Matching_Dispatching_Subprogram --
11777 -------------------------------------
11779 function Matching_Dispatching_Subprogram
11780 (Conc_Typ : Entity_Id;
11781 Ent : Entity_Id) return Entity_Id
11783 E : Entity_Id;
11785 begin
11786 -- Search for entities in the enclosing scope of this synchronized
11787 -- type.
11789 pragma Assert (Is_Concurrent_Type (Conc_Typ));
11790 Push_Scope (Scope (Conc_Typ));
11791 E := Current_Entity_In_Scope (Ent);
11792 Pop_Scope;
11794 while Present (E) loop
11795 if Scope (E) = Scope (Conc_Typ)
11796 and then Comes_From_Source (E)
11797 and then Ekind (E) = E_Procedure
11798 and then Present (First_Entity (E))
11799 and then Is_Controlling_Formal (First_Entity (E))
11800 and then Etype (First_Entity (E)) = Conc_Typ
11801 and then
11802 Check_Conforming_Parameters
11803 (First (Parameter_Specifications (Parent (Ent))),
11804 Next (First (Parameter_Specifications (Parent (E)))),
11805 Subtype_Conformant)
11806 then
11807 return E;
11808 end if;
11810 E := Homonym (E);
11811 end loop;
11813 return Empty;
11814 end Matching_Dispatching_Subprogram;
11816 --------------------------------------------
11817 -- Matching_Original_Protected_Subprogram --
11818 --------------------------------------------
11820 function Matching_Original_Protected_Subprogram
11821 (Prot_Typ : Entity_Id;
11822 Subp : Entity_Id) return Entity_Id
11824 ICF : constant Boolean :=
11825 Is_Controlling_Formal (First_Entity (Subp));
11826 E : Entity_Id;
11828 begin
11829 -- Temporarily decorate the first parameter of Subp as controlling
11830 -- formal, required to invoke Subtype_Conformant.
11832 Set_Is_Controlling_Formal (First_Entity (Subp));
11834 E :=
11835 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
11837 while Present (E) loop
11838 if Scope (E) = Scope (Prot_Typ)
11839 and then Comes_From_Source (E)
11840 and then Ekind (Subp) = Ekind (E)
11841 and then Present (First_Entity (E))
11842 and then Is_Controlling_Formal (First_Entity (E))
11843 and then Etype (First_Entity (E)) = Prot_Typ
11844 and then Subtype_Conformant (Subp, E,
11845 Skip_Controlling_Formals => True)
11846 then
11847 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11848 return E;
11849 end if;
11851 E := Homonym (E);
11852 end loop;
11854 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11856 return Empty;
11857 end Matching_Original_Protected_Subprogram;
11859 -------------------------------------
11860 -- Normalized_First_Parameter_Type --
11861 -------------------------------------
11863 function Normalized_First_Parameter_Type
11864 (E : Entity_Id) return Entity_Id
11866 Result : Entity_Id := Etype (First_Entity (E));
11867 begin
11868 if Ekind (Result) = E_Anonymous_Access_Type then
11869 Result := Designated_Type (Result);
11870 end if;
11871 return Result;
11872 end Normalized_First_Parameter_Type;
11874 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11876 begin
11877 -- Case 1: E is a subprogram whose first formal is a concurrent type
11878 -- defined in the scope of E that has an entry or subprogram whose
11879 -- profile matches E.
11881 if Comes_From_Source (E)
11882 and then Is_Subprogram (E)
11883 and then Present (First_Entity (E))
11884 and then Is_Concurrent_Record_Type
11885 (Normalized_First_Parameter_Type (E))
11886 then
11887 if Scope (E) =
11888 Scope (Corresponding_Concurrent_Type
11889 (Normalized_First_Parameter_Type (E)))
11890 and then
11891 Present
11892 (Matching_Entry_Or_Subprogram
11893 (Corresponding_Concurrent_Type
11894 (Normalized_First_Parameter_Type (E)),
11895 Subp => E))
11896 then
11897 Report_Conflict (E,
11898 Matching_Entry_Or_Subprogram
11899 (Corresponding_Concurrent_Type
11900 (Normalized_First_Parameter_Type (E)),
11901 Subp => E));
11902 return True;
11903 end if;
11905 -- Case 2: E is an internally built dispatching subprogram of a
11906 -- protected type and there is a subprogram defined in the enclosing
11907 -- scope of the protected type that has the original name of E and
11908 -- its profile is conformant with the profile of E. We check the
11909 -- name of the original protected subprogram associated with E since
11910 -- the expander builds dispatching primitives of protected functions
11911 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11913 elsif not Comes_From_Source (E)
11914 and then Is_Subprogram (E)
11915 and then Present (First_Entity (E))
11916 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11917 and then Present (Original_Protected_Subprogram (E))
11918 and then
11919 Present
11920 (Matching_Original_Protected_Subprogram
11921 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11922 Subp => E))
11923 then
11924 Report_Conflict (E,
11925 Matching_Original_Protected_Subprogram
11926 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11927 Subp => E));
11928 return True;
11930 -- Case 3: E is an entry of a synchronized type and a matching
11931 -- procedure has been previously defined in the enclosing scope
11932 -- of the synchronized type.
11934 elsif Comes_From_Source (E)
11935 and then Ekind (E) = E_Entry
11936 and then
11937 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11938 then
11939 Report_Conflict (E,
11940 Matching_Dispatching_Subprogram (Current_Scope, E));
11941 return True;
11942 end if;
11944 return False;
11945 end Has_Matching_Entry_Or_Subprogram;
11947 ----------------------------
11948 -- Is_Private_Declaration --
11949 ----------------------------
11951 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11952 Decl : constant Node_Id := Unit_Declaration_Node (E);
11953 Priv_Decls : List_Id;
11955 begin
11956 if Is_Package_Or_Generic_Package (Current_Scope)
11957 and then In_Private_Part (Current_Scope)
11958 then
11959 Priv_Decls :=
11960 Private_Declarations (Package_Specification (Current_Scope));
11962 return In_Package_Body (Current_Scope)
11963 or else
11964 (Is_List_Member (Decl)
11965 and then List_Containing (Decl) = Priv_Decls)
11966 or else (Nkind (Parent (Decl)) = N_Package_Specification
11967 and then not
11968 Is_Compilation_Unit
11969 (Defining_Entity (Parent (Decl)))
11970 and then List_Containing (Parent (Parent (Decl))) =
11971 Priv_Decls);
11972 else
11973 return False;
11974 end if;
11975 end Is_Private_Declaration;
11977 --------------------------
11978 -- Is_Overriding_Alias --
11979 --------------------------
11981 function Is_Overriding_Alias
11982 (Old_E : Entity_Id;
11983 New_E : Entity_Id) return Boolean
11985 AO : constant Entity_Id := Alias (Old_E);
11986 AN : constant Entity_Id := Alias (New_E);
11988 begin
11989 return Scope (AO) /= Scope (AN)
11990 or else No (DTC_Entity (AO))
11991 or else No (DTC_Entity (AN))
11992 or else DT_Position (AO) = DT_Position (AN);
11993 end Is_Overriding_Alias;
11995 ---------------------
11996 -- Report_Conflict --
11997 ---------------------
11999 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
12000 begin
12001 Error_Msg_Sloc := Sloc (E);
12003 -- Generate message, with useful additional warning if in generic
12005 if Is_Generic_Unit (E) then
12006 Error_Msg_N ("previous generic unit cannot be overloaded", S);
12007 Error_Msg_N ("\& conflicts with declaration#", S);
12008 else
12009 Error_Msg_N ("& conflicts with declaration#", S);
12010 end if;
12011 end Report_Conflict;
12013 -- Start of processing for New_Overloaded_Entity
12015 begin
12016 -- We need to look for an entity that S may override. This must be a
12017 -- homonym in the current scope, so we look for the first homonym of
12018 -- S in the current scope as the starting point for the search.
12020 E := Current_Entity_In_Scope (S);
12022 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
12023 -- They are directly added to the list of primitive operations of
12024 -- Derived_Type, unless this is a rederivation in the private part
12025 -- of an operation that was already derived in the visible part of
12026 -- the current package.
12028 if Ada_Version >= Ada_2005
12029 and then Present (Derived_Type)
12030 and then Present (Alias (S))
12031 and then Is_Dispatching_Operation (Alias (S))
12032 and then Present (Find_Dispatching_Type (Alias (S)))
12033 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
12034 then
12035 -- For private types, when the full-view is processed we propagate to
12036 -- the full view the non-overridden entities whose attribute "alias"
12037 -- references an interface primitive. These entities were added by
12038 -- Derive_Subprograms to ensure that interface primitives are
12039 -- covered.
12041 -- Inside_Freeze_Actions is non zero when S corresponds with an
12042 -- internal entity that links an interface primitive with its
12043 -- covering primitive through attribute Interface_Alias (see
12044 -- Add_Internal_Interface_Entities).
12046 if Inside_Freezing_Actions = 0
12047 and then Is_Package_Or_Generic_Package (Current_Scope)
12048 and then In_Private_Part (Current_Scope)
12049 and then Parent_Kind (E) = N_Private_Extension_Declaration
12050 and then Nkind (Parent (S)) = N_Full_Type_Declaration
12051 and then Full_View (Defining_Identifier (Parent (E)))
12052 = Defining_Identifier (Parent (S))
12053 and then Alias (E) = Alias (S)
12054 then
12055 Check_Operation_From_Private_View (S, E);
12056 Set_Is_Dispatching_Operation (S);
12058 -- Common case
12060 else
12061 Enter_Overloaded_Entity (S);
12062 Check_Dispatching_Operation (S, Empty);
12063 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
12064 end if;
12066 return;
12067 end if;
12069 -- For synchronized types check conflicts of this entity with previously
12070 -- defined entities.
12072 if Ada_Version >= Ada_2005
12073 and then Has_Matching_Entry_Or_Subprogram (S)
12074 then
12075 return;
12076 end if;
12078 -- If there is no homonym then this is definitely not overriding
12080 if No (E) then
12081 Enter_Overloaded_Entity (S);
12082 Check_Dispatching_Operation (S, Empty);
12083 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
12085 -- If subprogram has an explicit declaration, check whether it has an
12086 -- overriding indicator.
12088 if Comes_From_Source (S) then
12089 Check_Synchronized_Overriding (S, Overridden_Subp);
12091 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
12092 -- it may have overridden some hidden inherited primitive. Update
12093 -- Overridden_Subp to avoid spurious errors when checking the
12094 -- overriding indicator.
12096 if Ada_Version >= Ada_2012
12097 and then No (Overridden_Subp)
12098 and then Is_Dispatching_Operation (S)
12099 and then Present (Overridden_Operation (S))
12100 then
12101 Overridden_Subp := Overridden_Operation (S);
12102 end if;
12104 Check_Overriding_Indicator
12105 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
12107 -- The Ghost policy in effect at the point of declaration of a
12108 -- parent subprogram and an overriding subprogram must match
12109 -- (SPARK RM 6.9(17)).
12111 Check_Ghost_Overriding (S, Overridden_Subp);
12112 end if;
12114 -- If there is a homonym that is not overloadable, then we have an
12115 -- error, except for the special cases checked explicitly below.
12117 elsif not Is_Overloadable (E) then
12119 -- Check for spurious conflict produced by a subprogram that has the
12120 -- same name as that of the enclosing generic package. The conflict
12121 -- occurs within an instance, between the subprogram and the renaming
12122 -- declaration for the package. After the subprogram, the package
12123 -- renaming declaration becomes hidden.
12125 if Ekind (E) = E_Package
12126 and then Present (Renamed_Entity (E))
12127 and then Renamed_Entity (E) = Current_Scope
12128 and then Nkind (Parent (Renamed_Entity (E))) =
12129 N_Package_Specification
12130 and then Present (Generic_Parent (Parent (Renamed_Entity (E))))
12131 then
12132 Set_Is_Hidden (E);
12133 Set_Is_Immediately_Visible (E, False);
12134 Enter_Overloaded_Entity (S);
12135 Set_Homonym (S, Homonym (E));
12136 Check_Dispatching_Operation (S, Empty);
12137 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
12139 -- If the subprogram is implicit it is hidden by the previous
12140 -- declaration. However if it is dispatching, it must appear in the
12141 -- dispatch table anyway, because it can be dispatched to even if it
12142 -- cannot be called directly.
12144 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
12145 Set_Scope (S, Current_Scope);
12147 if Is_Dispatching_Operation (Alias (S)) then
12148 Check_Dispatching_Operation (S, Empty);
12149 end if;
12151 return;
12153 else
12154 Report_Conflict (S, E);
12155 return;
12156 end if;
12158 -- E exists and is overloadable
12160 else
12161 Check_Synchronized_Overriding (S, Overridden_Subp);
12163 -- Loop through E and its homonyms to determine if any of them is
12164 -- the candidate for overriding by S.
12166 while Present (E) loop
12168 -- Definitely not interesting if not in the current scope
12170 if Scope (E) /= Current_Scope then
12171 null;
12173 -- A function can overload the name of an abstract state. The
12174 -- state can be viewed as a function with a profile that cannot
12175 -- be matched by anything.
12177 elsif Ekind (S) = E_Function
12178 and then Ekind (E) = E_Abstract_State
12179 then
12180 Enter_Overloaded_Entity (S);
12181 return;
12183 -- Ada 2012 (AI05-0165): For internally generated bodies of null
12184 -- procedures locate the internally generated spec. We enforce
12185 -- mode conformance since a tagged type may inherit from
12186 -- interfaces several null primitives which differ only in
12187 -- the mode of the formals.
12189 elsif not Comes_From_Source (S)
12190 and then Is_Null_Procedure (S)
12191 and then not Mode_Conformant (E, S)
12192 then
12193 null;
12195 -- Check if we have type conformance
12197 elsif Type_Conformant (E, S) then
12199 -- If the old and new entities have the same profile and one
12200 -- is not the body of the other, then this is an error, unless
12201 -- one of them is implicitly declared.
12203 -- There are some cases when both can be implicit, for example
12204 -- when both a literal and a function that overrides it are
12205 -- inherited in a derivation, or when an inherited operation
12206 -- of a tagged full type overrides the inherited operation of
12207 -- a private extension. Ada 83 had a special rule for the
12208 -- literal case. In Ada 95, the later implicit operation hides
12209 -- the former, and the literal is always the former. In the
12210 -- odd case where both are derived operations declared at the
12211 -- same point, both operations should be declared, and in that
12212 -- case we bypass the following test and proceed to the next
12213 -- part. This can only occur for certain obscure cases in
12214 -- instances, when an operation on a type derived from a formal
12215 -- private type does not override a homograph inherited from
12216 -- the actual. In subsequent derivations of such a type, the
12217 -- DT positions of these operations remain distinct, if they
12218 -- have been set.
12220 if Present (Alias (S))
12221 and then (No (Alias (E))
12222 or else Comes_From_Source (E)
12223 or else Is_Abstract_Subprogram (S)
12224 or else
12225 (Is_Dispatching_Operation (E)
12226 and then Is_Overriding_Alias (E, S)))
12227 and then Ekind (E) /= E_Enumeration_Literal
12228 then
12229 -- When an derived operation is overloaded it may be due to
12230 -- the fact that the full view of a private extension
12231 -- re-inherits. It has to be dealt with.
12233 if Is_Package_Or_Generic_Package (Current_Scope)
12234 and then In_Private_Part (Current_Scope)
12235 then
12236 Check_Operation_From_Private_View (S, E);
12237 end if;
12239 -- In any case the implicit operation remains hidden by the
12240 -- existing declaration, which is overriding. Indicate that
12241 -- E overrides the operation from which S is inherited.
12243 if Present (Alias (S)) then
12244 Set_Overridden_Operation (E, Alias (S));
12245 Inherit_Subprogram_Contract (E, Alias (S));
12246 Set_Is_Ada_2022_Only (E,
12247 Is_Ada_2022_Only (Alias (S)));
12249 else
12250 Set_Overridden_Operation (E, S);
12251 Inherit_Subprogram_Contract (E, S);
12252 Set_Is_Ada_2022_Only (E, Is_Ada_2022_Only (S));
12253 end if;
12255 -- When a dispatching operation overrides an inherited
12256 -- subprogram, it shall be subtype conformant with the
12257 -- inherited subprogram (RM 3.9.2 (10.2)).
12259 if Comes_From_Source (E)
12260 and then Is_Dispatching_Operation (E)
12261 and then Find_Dispatching_Type (S)
12262 = Find_Dispatching_Type (E)
12263 then
12264 Check_Subtype_Conformant (E, S);
12265 end if;
12267 if Comes_From_Source (E) then
12268 Check_Overriding_Indicator (E, S, Is_Primitive => False);
12270 -- The Ghost policy in effect at the point of declaration
12271 -- of a parent subprogram and an overriding subprogram
12272 -- must match (SPARK RM 6.9(17)).
12274 Check_Ghost_Overriding (E, S);
12275 end if;
12277 return;
12279 -- Within an instance, the renaming declarations for actual
12280 -- subprograms may become ambiguous, but they do not hide each
12281 -- other.
12283 elsif Ekind (E) /= E_Entry
12284 and then not Comes_From_Source (E)
12285 and then not Is_Generic_Instance (E)
12286 and then (Present (Alias (E))
12287 or else Is_Intrinsic_Subprogram (E))
12288 and then (not In_Instance
12289 or else No (Parent (E))
12290 or else Nkind (Unit_Declaration_Node (E)) /=
12291 N_Subprogram_Renaming_Declaration)
12292 then
12293 -- A subprogram child unit is not allowed to override an
12294 -- inherited subprogram (10.1.1(20)).
12296 if Is_Child_Unit (S) then
12297 Error_Msg_N
12298 ("child unit overrides inherited subprogram in parent",
12300 return;
12301 end if;
12303 if Is_Non_Overriding_Operation (E, S) then
12304 Enter_Overloaded_Entity (S);
12306 if No (Derived_Type)
12307 or else Is_Tagged_Type (Derived_Type)
12308 then
12309 Check_Dispatching_Operation (S, Empty);
12310 end if;
12312 return;
12313 end if;
12315 -- E is a derived operation or an internal operator which
12316 -- is being overridden. Remove E from further visibility.
12317 -- Furthermore, if E is a dispatching operation, it must be
12318 -- replaced in the list of primitive operations of its type
12319 -- (see Override_Dispatching_Operation).
12321 Overridden_Subp := E;
12323 -- It is possible for E to be in the current scope and
12324 -- yet not in the entity chain. This can only occur in a
12325 -- generic context where E is an implicit concatenation
12326 -- in the formal part, because in a generic body the
12327 -- entity chain starts with the formals.
12329 -- In GNATprove mode, a wrapper for an operation with
12330 -- axiomatization may be a homonym of another declaration
12331 -- for an actual subprogram (needs refinement ???).
12333 if No (Prev_Entity (E)) then
12334 if In_Instance
12335 and then GNATprove_Mode
12336 and then
12337 Nkind (Original_Node (Unit_Declaration_Node (S))) =
12338 N_Subprogram_Renaming_Declaration
12339 then
12340 return;
12341 else
12342 pragma Assert (Chars (E) = Name_Op_Concat);
12343 null;
12344 end if;
12345 end if;
12347 -- E must be removed both from the entity_list of the
12348 -- current scope, and from the visibility chain.
12350 if Debug_Flag_E then
12351 Write_Str ("Override implicit operation ");
12352 Write_Int (Int (E));
12353 Write_Eol;
12354 end if;
12356 -- If E is a predefined concatenation, it stands for four
12357 -- different operations. As a result, a single explicit
12358 -- declaration does not hide it. In a possible ambiguous
12359 -- situation, Disambiguate chooses the user-defined op,
12360 -- so it is correct to retain the previous internal one.
12362 if Chars (E) /= Name_Op_Concat
12363 or else Ekind (E) /= E_Operator
12364 then
12365 -- For nondispatching derived operations that are
12366 -- overridden by a subprogram declared in the private
12367 -- part of a package, we retain the derived subprogram
12368 -- but mark it as not immediately visible. If the
12369 -- derived operation was declared in the visible part
12370 -- then this ensures that it will still be visible
12371 -- outside the package with the proper signature
12372 -- (calls from outside must also be directed to this
12373 -- version rather than the overriding one, unlike the
12374 -- dispatching case). Calls from inside the package
12375 -- will still resolve to the overriding subprogram
12376 -- since the derived one is marked as not visible
12377 -- within the package.
12379 -- If the private operation is dispatching, we achieve
12380 -- the overriding by keeping the implicit operation
12381 -- but setting its alias to be the overriding one. In
12382 -- this fashion the proper body is executed in all
12383 -- cases, but the original signature is used outside
12384 -- of the package.
12386 -- If the overriding is not in the private part, we
12387 -- remove the implicit operation altogether.
12389 if Is_Private_Declaration (S) then
12390 if not Is_Dispatching_Operation (E) then
12391 Set_Is_Immediately_Visible (E, False);
12392 else
12393 -- Work done in Override_Dispatching_Operation, so
12394 -- nothing else needs to be done here.
12396 -- ??? Special case to keep supporting the hiding
12397 -- of the predefined "=" operator for a nonlimited
12398 -- tagged type by a user-defined "=" operator for
12399 -- its class-wide type when the type is private.
12401 if Chars (E) = Name_Op_Eq then
12402 declare
12403 Typ : constant Entity_Id
12404 := Etype (First_Entity (E));
12405 H : Entity_Id := Homonym (E);
12407 begin
12408 while Present (H)
12409 and then Scope (H) = Scope (E)
12410 loop
12411 if Is_User_Defined_Equality (H)
12412 and then Is_Immediately_Visible (H)
12413 and then Etype (First_Entity (H))
12414 = Class_Wide_Type (Typ)
12415 then
12416 Remove_Entity_And_Homonym (E);
12417 exit;
12418 end if;
12420 H := Homonym (H);
12421 end loop;
12422 end;
12423 end if;
12424 end if;
12426 else
12427 Remove_Entity_And_Homonym (E);
12428 end if;
12429 end if;
12431 Enter_Overloaded_Entity (S);
12433 -- For entities generated by Derive_Subprograms the
12434 -- overridden operation is the inherited primitive
12435 -- (which is available through the attribute alias).
12437 if not (Comes_From_Source (E))
12438 and then Is_Dispatching_Operation (E)
12439 and then Find_Dispatching_Type (E) =
12440 Find_Dispatching_Type (S)
12441 and then Present (Alias (E))
12442 and then Comes_From_Source (Alias (E))
12443 then
12444 Set_Overridden_Operation (S, Alias (E));
12445 Inherit_Subprogram_Contract (S, Alias (E));
12446 Set_Is_Ada_2022_Only (S,
12447 Is_Ada_2022_Only (Alias (E)));
12449 -- Normal case of setting entity as overridden
12451 -- Note: Static_Initialization and Overridden_Operation
12452 -- attributes use the same field in subprogram entities.
12453 -- Static_Initialization is only defined for internal
12454 -- initialization procedures, where Overridden_Operation
12455 -- is irrelevant. Therefore the setting of this attribute
12456 -- must check whether the target is an init_proc.
12458 elsif not Is_Init_Proc (S) then
12460 -- LSP wrappers must override the ultimate alias of their
12461 -- wrapped dispatching primitive E; required to traverse
12462 -- the chain of ancestor primitives (c.f. Map_Primitives)
12463 -- They don't inherit contracts.
12465 if Is_Wrapper (S)
12466 and then Present (LSP_Subprogram (S))
12467 then
12468 Set_Overridden_Operation (S, Ultimate_Alias (E));
12469 else
12470 Set_Overridden_Operation (S, E);
12471 Inherit_Subprogram_Contract (S, E);
12472 end if;
12474 Set_Is_Ada_2022_Only (S, Is_Ada_2022_Only (E));
12475 end if;
12477 Check_Overriding_Indicator (S, E, Is_Primitive => True);
12479 -- The Ghost policy in effect at the point of declaration
12480 -- of a parent subprogram and an overriding subprogram
12481 -- must match (SPARK RM 6.9(17)).
12483 Check_Ghost_Overriding (S, E);
12485 -- If S is a user-defined subprogram or a null procedure
12486 -- expanded to override an inherited null procedure, or a
12487 -- predefined dispatching primitive then indicate that E
12488 -- overrides the operation from which S is inherited.
12490 if Comes_From_Source (S)
12491 or else
12492 (Present (Parent (S))
12493 and then Nkind (Parent (S)) = N_Procedure_Specification
12494 and then Null_Present (Parent (S)))
12495 or else
12496 (Present (Alias (E))
12497 and then
12498 Is_Predefined_Dispatching_Operation (Alias (E)))
12499 then
12500 if Present (Alias (E)) then
12502 -- LSP wrappers must override the ultimate alias of
12503 -- their wrapped dispatching primitive E; required to
12504 -- traverse the chain of ancestor primitives (see
12505 -- Map_Primitives). They don't inherit contracts.
12507 if Is_Wrapper (S)
12508 and then Present (LSP_Subprogram (S))
12509 then
12510 Set_Overridden_Operation (S, Ultimate_Alias (E));
12511 else
12512 Set_Overridden_Operation (S, Alias (E));
12513 Inherit_Subprogram_Contract (S, Alias (E));
12514 end if;
12516 Set_Is_Ada_2022_Only (S, Is_Ada_2022_Only (Alias (E)));
12517 end if;
12518 end if;
12520 if Is_Dispatching_Operation (E) then
12522 -- An overriding dispatching subprogram inherits the
12523 -- convention of the overridden subprogram (AI95-117).
12525 Set_Convention (S, Convention (E));
12526 Check_Dispatching_Operation (S, E);
12528 else
12529 Check_Dispatching_Operation (S, Empty);
12530 end if;
12532 Check_For_Primitive_Subprogram
12533 (Is_Primitive_Subp, Is_Overriding => True);
12534 goto Check_Inequality;
12536 -- Apparent redeclarations in instances can occur when two
12537 -- formal types get the same actual type. The subprograms in
12538 -- in the instance are legal, even if not callable from the
12539 -- outside. Calls from within are disambiguated elsewhere.
12540 -- For dispatching operations in the visible part, the usual
12541 -- rules apply, and operations with the same profile are not
12542 -- legal (B830001).
12544 elsif (In_Instance_Visible_Part
12545 and then not Is_Dispatching_Operation (E))
12546 or else In_Instance_Not_Visible
12547 then
12548 null;
12550 -- Here we have a real error (identical profile)
12552 else
12553 Error_Msg_Sloc := Sloc (E);
12555 -- Avoid cascaded errors if the entity appears in
12556 -- subsequent calls.
12558 Set_Scope (S, Current_Scope);
12560 -- Generate error, with extra useful warning for the case
12561 -- of a generic instance with no completion.
12563 if Is_Generic_Instance (S)
12564 and then not Has_Completion (E)
12565 then
12566 Error_Msg_N
12567 ("instantiation cannot provide body for&", S);
12568 Error_Msg_N ("\& conflicts with declaration#", S);
12569 else
12570 Error_Msg_N ("& conflicts with declaration#", S);
12571 end if;
12573 return;
12574 end if;
12576 else
12577 -- If one subprogram has an access parameter and the other
12578 -- a parameter of an access type, calls to either might be
12579 -- ambiguous. Verify that parameters match except for the
12580 -- access parameter.
12582 if May_Hide_Profile then
12583 declare
12584 F1 : Entity_Id;
12585 F2 : Entity_Id;
12587 begin
12588 F1 := First_Formal (S);
12589 F2 := First_Formal (E);
12590 while Present (F1) and then Present (F2) loop
12591 if Is_Access_Type (Etype (F1)) then
12592 if not Is_Access_Type (Etype (F2))
12593 or else not Conforming_Types
12594 (Designated_Type (Etype (F1)),
12595 Designated_Type (Etype (F2)),
12596 Type_Conformant)
12597 then
12598 May_Hide_Profile := False;
12599 end if;
12601 elsif
12602 not Conforming_Types
12603 (Etype (F1), Etype (F2), Type_Conformant)
12604 then
12605 May_Hide_Profile := False;
12606 end if;
12608 Next_Formal (F1);
12609 Next_Formal (F2);
12610 end loop;
12612 if May_Hide_Profile
12613 and then No (F1)
12614 and then No (F2)
12615 then
12616 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
12617 end if;
12618 end;
12619 end if;
12620 end if;
12622 E := Homonym (E);
12623 end loop;
12625 -- On exit, we know that S is a new entity
12627 Enter_Overloaded_Entity (S);
12628 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
12629 Check_Overriding_Indicator
12630 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
12632 -- The Ghost policy in effect at the point of declaration of a parent
12633 -- subprogram and an overriding subprogram must match
12634 -- (SPARK RM 6.9(17)).
12636 Check_Ghost_Overriding (S, Overridden_Subp);
12638 -- If S is a derived operation for an untagged type then by
12639 -- definition it's not a dispatching operation (even if the parent
12640 -- operation was dispatching), so Check_Dispatching_Operation is not
12641 -- called in that case.
12643 if No (Derived_Type)
12644 or else Is_Tagged_Type (Derived_Type)
12645 then
12646 Check_Dispatching_Operation (S, Empty);
12647 end if;
12648 end if;
12650 -- If this is a user-defined equality operator that is not a derived
12651 -- subprogram, create the corresponding inequality. If the operation is
12652 -- dispatching, the expansion is done elsewhere, and we do not create
12653 -- an explicit inequality operation.
12655 <<Check_Inequality>>
12656 if Chars (S) = Name_Op_Eq
12657 and then Etype (S) = Standard_Boolean
12658 and then Present (Parent (S))
12659 and then not Is_Dispatching_Operation (S)
12660 then
12661 Make_Inequality_Operator (S);
12662 Check_Untagged_Equality (S);
12663 end if;
12664 end New_Overloaded_Entity;
12666 ----------------------------------
12667 -- Preanalyze_Formal_Expression --
12668 ----------------------------------
12670 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
12671 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
12672 begin
12673 In_Spec_Expression := True;
12674 Preanalyze_With_Freezing_And_Resolve (N, T);
12675 In_Spec_Expression := Save_In_Spec_Expression;
12676 end Preanalyze_Formal_Expression;
12678 ---------------------
12679 -- Process_Formals --
12680 ---------------------
12682 procedure Process_Formals
12683 (T : List_Id;
12684 Related_Nod : Node_Id)
12686 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
12687 -- Determine whether an access type designates a type coming from a
12688 -- limited view.
12690 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
12691 -- Check whether the default has a class-wide type. After analysis the
12692 -- default has the type of the formal, so we must also check explicitly
12693 -- for an access attribute.
12695 ----------------------------------
12696 -- Designates_From_Limited_With --
12697 ----------------------------------
12699 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
12700 Desig : Entity_Id := Typ;
12702 begin
12703 if Is_Access_Type (Desig) then
12704 Desig := Directly_Designated_Type (Desig);
12705 end if;
12707 if Is_Class_Wide_Type (Desig) then
12708 Desig := Root_Type (Desig);
12709 end if;
12711 return
12712 Ekind (Desig) = E_Incomplete_Type
12713 and then From_Limited_With (Desig);
12714 end Designates_From_Limited_With;
12716 ---------------------------
12717 -- Is_Class_Wide_Default --
12718 ---------------------------
12720 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
12721 begin
12722 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
12723 or else (Nkind (D) = N_Attribute_Reference
12724 and then Attribute_Name (D) = Name_Access
12725 and then Is_Class_Wide_Type (Etype (Prefix (D))));
12726 end Is_Class_Wide_Default;
12728 -- Local variables
12730 Context : constant Node_Id := Parent (Parent (T));
12731 Default : Node_Id;
12732 Formal : Entity_Id;
12733 Formal_Type : Entity_Id;
12734 Param_Spec : Node_Id;
12735 Ptype : Entity_Id;
12737 Num_Out_Params : Nat := 0;
12738 First_Out_Param : Entity_Id := Empty;
12739 -- Used for setting Is_Only_Out_Parameter
12741 -- Start of processing for Process_Formals
12743 begin
12744 -- In order to prevent premature use of the formals in the same formal
12745 -- part, the Ekind is left undefined until all default expressions are
12746 -- analyzed. The Ekind is established in a separate loop at the end.
12748 Param_Spec := First (T);
12749 while Present (Param_Spec) loop
12750 Formal := Defining_Identifier (Param_Spec);
12751 Set_Never_Set_In_Source (Formal, True);
12752 Enter_Name (Formal);
12754 -- Case of ordinary parameters
12756 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
12757 Find_Type (Parameter_Type (Param_Spec));
12758 Ptype := Parameter_Type (Param_Spec);
12760 if Ptype = Error then
12761 goto Continue;
12762 end if;
12764 -- Protect against malformed parameter types
12766 if Nkind (Ptype) not in N_Has_Entity then
12767 Formal_Type := Any_Type;
12768 else
12769 Formal_Type := Entity (Ptype);
12770 end if;
12772 if Is_Incomplete_Type (Formal_Type)
12773 or else
12774 (Is_Class_Wide_Type (Formal_Type)
12775 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
12776 then
12777 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
12778 -- primitive operations, as long as their completion is
12779 -- in the same declarative part. If in the private part
12780 -- this means that the type cannot be a Taft-amendment type.
12781 -- Check is done on package exit. For access to subprograms,
12782 -- the use is legal for Taft-amendment types.
12784 -- Ada 2012: tagged incomplete types are allowed as generic
12785 -- formal types. They do not introduce dependencies and the
12786 -- corresponding generic subprogram does not have a delayed
12787 -- freeze, because it does not need a freeze node. However,
12788 -- it is still the case that untagged incomplete types cannot
12789 -- be Taft-amendment types and must be completed in private
12790 -- part, so the subprogram must appear in the list of private
12791 -- dependents of the type.
12793 if Is_Tagged_Type (Formal_Type)
12794 or else (Ada_Version >= Ada_2012
12795 and then not From_Limited_With (Formal_Type)
12796 and then not Is_Generic_Type (Formal_Type))
12797 then
12798 if Ekind (Scope (Current_Scope)) = E_Package
12799 and then not Is_Generic_Type (Formal_Type)
12800 and then not Is_Class_Wide_Type (Formal_Type)
12801 then
12802 if Nkind (Parent (T)) not in
12803 N_Access_Function_Definition |
12804 N_Access_Procedure_Definition
12805 then
12806 Append_Elmt (Current_Scope,
12807 Private_Dependents (Base_Type (Formal_Type)));
12809 -- Freezing is delayed to ensure that Register_Prim
12810 -- will get called for this operation, which is needed
12811 -- in cases where static dispatch tables aren't built.
12812 -- (Note that the same is done for controlling access
12813 -- parameter cases in function Access_Definition.)
12815 if not Is_Thunk (Current_Scope) then
12816 Set_Has_Delayed_Freeze (Current_Scope);
12817 end if;
12818 end if;
12819 end if;
12821 elsif Nkind (Parent (T)) not in N_Access_Function_Definition
12822 | N_Access_Procedure_Definition
12823 then
12824 -- AI05-0151: Tagged incomplete types are allowed in all
12825 -- formal parts. Untagged incomplete types are not allowed
12826 -- in bodies. Limited views of either kind are not allowed
12827 -- if there is no place at which the non-limited view can
12828 -- become available.
12830 -- Incomplete formal untagged types are not allowed in
12831 -- subprogram bodies (but are legal in their declarations).
12832 -- This excludes bodies created for null procedures, which
12833 -- are basic declarations.
12835 if Is_Generic_Type (Formal_Type)
12836 and then not Is_Tagged_Type (Formal_Type)
12837 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
12838 then
12839 Error_Msg_N
12840 ("invalid use of formal incomplete type", Param_Spec);
12842 elsif Ada_Version >= Ada_2012 then
12843 if Is_Tagged_Type (Formal_Type)
12844 and then (not From_Limited_With (Formal_Type)
12845 or else not In_Package_Body)
12846 then
12847 null;
12849 elsif Nkind (Context) in N_Accept_Statement
12850 | N_Accept_Alternative
12851 | N_Entry_Body
12852 or else (Nkind (Context) = N_Subprogram_Body
12853 and then Comes_From_Source (Context))
12854 then
12855 Error_Msg_NE
12856 ("invalid use of untagged incomplete type &",
12857 Ptype, Formal_Type);
12858 end if;
12860 else
12861 Error_Msg_NE
12862 ("invalid use of incomplete type&",
12863 Param_Spec, Formal_Type);
12865 -- Further checks on the legality of incomplete types
12866 -- in formal parts are delayed until the freeze point
12867 -- of the enclosing subprogram or access to subprogram.
12868 end if;
12869 end if;
12871 elsif Ekind (Formal_Type) = E_Void then
12872 Error_Msg_NE
12873 ("premature use of&",
12874 Parameter_Type (Param_Spec), Formal_Type);
12875 end if;
12877 -- Ada 2012 (AI-142): Handle aliased parameters
12879 if Ada_Version >= Ada_2012
12880 and then Aliased_Present (Param_Spec)
12881 then
12882 Set_Is_Aliased (Formal);
12884 -- AI12-001: All aliased objects are considered to be specified
12885 -- as independently addressable (RM C.6(8.1/4)).
12887 Set_Is_Independent (Formal);
12888 end if;
12890 -- Ada 2005 (AI-231): Create and decorate an internal subtype
12891 -- declaration corresponding to the null-excluding type of the
12892 -- formal in the enclosing scope. Finally, replace the parameter
12893 -- type of the formal with the internal subtype.
12895 if Ada_Version >= Ada_2005
12896 and then Null_Exclusion_Present (Param_Spec)
12897 then
12898 if not Is_Access_Type (Formal_Type) then
12899 Error_Msg_N
12900 ("`NOT NULL` allowed only for an access type", Param_Spec);
12902 else
12903 if Can_Never_Be_Null (Formal_Type)
12904 and then Comes_From_Source (Related_Nod)
12905 then
12906 Error_Msg_NE
12907 ("`NOT NULL` not allowed (& already excludes null)",
12908 Param_Spec, Formal_Type);
12909 end if;
12911 Formal_Type :=
12912 Create_Null_Excluding_Itype
12913 (T => Formal_Type,
12914 Related_Nod => Related_Nod,
12915 Scope_Id => Scope (Current_Scope));
12917 -- If the designated type of the itype is an itype that is
12918 -- not frozen yet, we set the Has_Delayed_Freeze attribute
12919 -- on the access subtype, to prevent order-of-elaboration
12920 -- issues in the backend.
12922 -- Example:
12923 -- type T is access procedure;
12924 -- procedure Op (O : not null T);
12926 if Is_Itype (Directly_Designated_Type (Formal_Type))
12927 and then
12928 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12929 then
12930 Set_Has_Delayed_Freeze (Formal_Type);
12931 end if;
12932 end if;
12933 end if;
12935 -- An access formal type
12937 else
12938 Formal_Type :=
12939 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12941 -- No need to continue if we already notified errors
12943 if No (Formal_Type) then
12944 return;
12945 end if;
12947 -- Ada 2005 (AI-254)
12949 declare
12950 AD : constant Node_Id :=
12951 Access_To_Subprogram_Definition
12952 (Parameter_Type (Param_Spec));
12953 begin
12954 if Present (AD) and then Protected_Present (AD) then
12955 Formal_Type :=
12956 Replace_Anonymous_Access_To_Protected_Subprogram
12957 (Param_Spec);
12958 end if;
12959 end;
12960 end if;
12962 Set_Etype (Formal, Formal_Type);
12964 -- Deal with default expression if present
12966 Default := Expression (Param_Spec);
12968 if Present (Default) then
12969 if Out_Present (Param_Spec) then
12970 Error_Msg_N
12971 ("default initialization only allowed for IN parameters",
12972 Param_Spec);
12973 end if;
12975 -- Do the special preanalysis of the expression (see section on
12976 -- "Handling of Default Expressions" in the spec of package Sem).
12978 Preanalyze_Formal_Expression (Default, Formal_Type);
12980 -- An access to constant cannot be the default for
12981 -- an access parameter that is an access to variable.
12983 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12984 and then not Is_Access_Constant (Formal_Type)
12985 and then Is_Access_Type (Etype (Default))
12986 and then Is_Access_Constant (Etype (Default))
12987 then
12988 Error_Msg_N
12989 ("formal that is access to variable cannot be initialized "
12990 & "with an access-to-constant expression", Default);
12991 end if;
12993 -- Check that the designated type of an access parameter's default
12994 -- is not a class-wide type unless the parameter's designated type
12995 -- is also class-wide.
12997 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12998 and then not Designates_From_Limited_With (Formal_Type)
12999 and then Is_Class_Wide_Default (Default)
13000 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
13001 then
13002 Error_Msg_N
13003 ("access to class-wide expression not allowed here", Default);
13004 end if;
13006 -- Check incorrect use of dynamically tagged expressions
13008 if Is_Tagged_Type (Formal_Type) then
13009 Check_Dynamically_Tagged_Expression
13010 (Expr => Default,
13011 Typ => Formal_Type,
13012 Related_Nod => Default);
13013 end if;
13014 end if;
13016 -- Ada 2005 (AI-231): Static checks
13018 if Ada_Version >= Ada_2005
13019 and then Is_Access_Type (Etype (Formal))
13020 and then Can_Never_Be_Null (Etype (Formal))
13021 then
13022 Null_Exclusion_Static_Checks (Param_Spec);
13023 end if;
13025 -- The following checks are relevant only when SPARK_Mode is on as
13026 -- these are not standard Ada legality rules.
13028 if SPARK_Mode = On then
13029 if Ekind (Scope (Formal)) in E_Function | E_Generic_Function then
13031 -- A function cannot have a parameter of mode IN OUT or OUT
13032 -- (SPARK RM 6.1).
13034 if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then
13035 Error_Msg_N
13036 ("function cannot have parameter of mode `OUT` or "
13037 & "`IN OUT`", Formal);
13038 end if;
13040 -- A procedure cannot have an effectively volatile formal
13041 -- parameter of mode IN because it behaves as a constant
13042 -- (SPARK RM 7.1.3(4)).
13044 elsif Ekind (Scope (Formal)) = E_Procedure
13045 and then Ekind (Formal) = E_In_Parameter
13046 and then Is_Effectively_Volatile (Formal)
13047 then
13048 Error_Msg_N
13049 ("formal parameter of mode `IN` cannot be volatile", Formal);
13050 end if;
13051 end if;
13053 -- Deal with aspects on formal parameters. Only Unreferenced is
13054 -- supported for the time being.
13056 if Has_Aspects (Param_Spec) then
13057 declare
13058 Aspect : Node_Id := First (Aspect_Specifications (Param_Spec));
13059 begin
13060 while Present (Aspect) loop
13061 if Chars (Identifier (Aspect)) = Name_Unreferenced then
13062 Set_Has_Pragma_Unreferenced (Formal);
13063 else
13064 Error_Msg_NE
13065 ("unsupported aspect& on parameter",
13066 Aspect, Identifier (Aspect));
13067 end if;
13069 Next (Aspect);
13070 end loop;
13071 end;
13072 end if;
13074 <<Continue>>
13075 Next (Param_Spec);
13076 end loop;
13078 -- If this is the formal part of a function specification, analyze the
13079 -- subtype mark in the context where the formals are visible but not
13080 -- yet usable, and may hide outer homographs.
13082 if Nkind (Related_Nod) = N_Function_Specification then
13083 Analyze_Return_Type (Related_Nod);
13084 end if;
13086 -- Now set the kind (mode) of each formal
13088 Param_Spec := First (T);
13089 while Present (Param_Spec) loop
13090 Formal := Defining_Identifier (Param_Spec);
13091 Set_Formal_Mode (Formal);
13093 if Ekind (Formal) = E_In_Parameter then
13094 Default := Expression (Param_Spec);
13096 if Present (Default) then
13097 Set_Default_Value (Formal, Default);
13099 if Is_Scalar_Type (Etype (Default)) then
13100 if Nkind (Parameter_Type (Param_Spec)) /=
13101 N_Access_Definition
13102 then
13103 Formal_Type := Entity (Parameter_Type (Param_Spec));
13104 else
13105 Formal_Type :=
13106 Access_Definition
13107 (Related_Nod, Parameter_Type (Param_Spec));
13108 end if;
13110 Apply_Scalar_Range_Check (Default, Formal_Type);
13111 end if;
13112 end if;
13114 elsif Ekind (Formal) = E_Out_Parameter then
13115 Num_Out_Params := Num_Out_Params + 1;
13117 if Num_Out_Params = 1 then
13118 First_Out_Param := Formal;
13119 end if;
13121 elsif Ekind (Formal) = E_In_Out_Parameter then
13122 Num_Out_Params := Num_Out_Params + 1;
13123 end if;
13125 -- Skip remaining processing if formal type was in error
13127 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
13128 goto Next_Parameter;
13129 end if;
13131 -- Force call by reference if aliased
13133 declare
13134 Conv : constant Convention_Id := Convention (Etype (Formal));
13135 begin
13136 if Is_Aliased (Formal) then
13137 Set_Mechanism (Formal, By_Reference);
13139 -- Warn if user asked this to be passed by copy
13141 if Conv = Convention_Ada_Pass_By_Copy then
13142 Error_Msg_N
13143 ("cannot pass aliased parameter & by copy??", Formal);
13144 end if;
13146 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
13148 elsif Conv = Convention_Ada_Pass_By_Copy then
13149 Set_Mechanism (Formal, By_Copy);
13151 elsif Conv = Convention_Ada_Pass_By_Reference then
13152 Set_Mechanism (Formal, By_Reference);
13153 end if;
13154 end;
13156 <<Next_Parameter>>
13157 Next (Param_Spec);
13158 end loop;
13160 if Present (First_Out_Param) and then Num_Out_Params = 1 then
13161 Set_Is_Only_Out_Parameter (First_Out_Param);
13162 end if;
13163 end Process_Formals;
13165 ----------------------------
13166 -- Reference_Body_Formals --
13167 ----------------------------
13169 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
13170 Fs : Entity_Id;
13171 Fb : Entity_Id;
13173 begin
13174 if Error_Posted (Spec) then
13175 return;
13176 end if;
13178 -- Iterate over both lists. They may be of different lengths if the two
13179 -- specs are not conformant.
13181 Fs := First_Formal (Spec);
13182 Fb := First_Formal (Bod);
13183 while Present (Fs) and then Present (Fb) loop
13184 Generate_Reference (Fs, Fb, 'b');
13186 if Style_Check then
13187 Style.Check_Identifier (Fb, Fs);
13188 end if;
13190 Set_Spec_Entity (Fb, Fs);
13191 Set_Referenced (Fs, False);
13192 Next_Formal (Fs);
13193 Next_Formal (Fb);
13194 end loop;
13195 end Reference_Body_Formals;
13197 -------------------------
13198 -- Set_Actual_Subtypes --
13199 -------------------------
13201 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
13202 Decl : Node_Id;
13203 Formal : Entity_Id;
13204 T : Entity_Id;
13205 First_Stmt : Node_Id := Empty;
13206 AS_Needed : Boolean;
13208 begin
13209 -- If this is an empty initialization procedure, no need to create
13210 -- actual subtypes (small optimization).
13212 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
13213 return;
13215 -- Within a predicate function we do not want to generate local
13216 -- subtypes that may generate nested predicate functions.
13218 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
13219 return;
13220 end if;
13222 -- The subtype declarations may freeze the formals. The body generated
13223 -- for an expression function is not a freeze point, so do not emit
13224 -- these declarations (small loss of efficiency in rare cases).
13226 if Nkind (N) = N_Subprogram_Body
13227 and then Was_Expression_Function (N)
13228 then
13229 return;
13230 end if;
13232 Formal := First_Formal (Subp);
13233 while Present (Formal) loop
13234 T := Etype (Formal);
13236 -- We never need an actual subtype for a constrained formal
13238 if Is_Constrained (T) then
13239 AS_Needed := False;
13241 -- If we have unknown discriminants, then we do not need an actual
13242 -- subtype, or more accurately we cannot figure it out. Note that
13243 -- all class-wide types have unknown discriminants.
13245 elsif Has_Unknown_Discriminants (T) then
13246 AS_Needed := False;
13248 -- At this stage we have an unconstrained type that may need an
13249 -- actual subtype. For sure the actual subtype is needed if we have
13250 -- an unconstrained array type. However, in an instance, the type
13251 -- may appear as a subtype of the full view, while the actual is
13252 -- in fact private (in which case no actual subtype is needed) so
13253 -- check the kind of the base type.
13255 elsif Is_Array_Type (Base_Type (T)) then
13256 AS_Needed := True;
13258 -- The only other case needing an actual subtype is an unconstrained
13259 -- record type which is an IN parameter (we cannot generate actual
13260 -- subtypes for the OUT or IN OUT case, since an assignment can
13261 -- change the discriminant values. However we exclude the case of
13262 -- initialization procedures, since discriminants are handled very
13263 -- specially in this context, see the section entitled "Handling of
13264 -- Discriminants" in Einfo.
13266 -- We also exclude the case of Discrim_SO_Functions (functions used
13267 -- in front-end layout mode for size/offset values), since in such
13268 -- functions only discriminants are referenced, and not only are such
13269 -- subtypes not needed, but they cannot always be generated, because
13270 -- of order of elaboration issues.
13272 elsif Is_Record_Type (T)
13273 and then Ekind (Formal) = E_In_Parameter
13274 and then Chars (Formal) /= Name_uInit
13275 and then not Is_Unchecked_Union (T)
13276 and then not Is_Discrim_SO_Function (Subp)
13277 then
13278 AS_Needed := True;
13280 -- All other cases do not need an actual subtype
13282 else
13283 AS_Needed := False;
13284 end if;
13286 -- Generate actual subtypes for unconstrained arrays and
13287 -- unconstrained discriminated records.
13289 if AS_Needed then
13290 if Nkind (N) = N_Accept_Statement then
13292 -- If expansion is active, the formal is replaced by a local
13293 -- variable that renames the corresponding entry of the
13294 -- parameter block, and it is this local variable that may
13295 -- require an actual subtype.
13297 if Expander_Active then
13298 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
13299 else
13300 Decl := Build_Actual_Subtype (T, Formal);
13301 end if;
13303 if Present (Handled_Statement_Sequence (N)) then
13304 First_Stmt :=
13305 First (Statements (Handled_Statement_Sequence (N)));
13306 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
13307 Mark_Rewrite_Insertion (Decl);
13308 else
13309 -- If the accept statement has no body, there will be no
13310 -- reference to the actuals, so no need to compute actual
13311 -- subtypes.
13313 return;
13314 end if;
13316 else
13317 Decl := Build_Actual_Subtype (T, Formal);
13318 Prepend (Decl, Declarations (N));
13319 Mark_Rewrite_Insertion (Decl);
13320 end if;
13322 -- The declaration uses the bounds of an existing object, and
13323 -- therefore needs no constraint checks.
13325 Analyze (Decl, Suppress => All_Checks);
13326 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
13328 -- We need to freeze manually the generated type when it is
13329 -- inserted anywhere else than in a declarative part.
13331 if Present (First_Stmt) then
13332 Insert_List_Before_And_Analyze (First_Stmt,
13333 Freeze_Entity (Defining_Identifier (Decl), N));
13335 -- Ditto if the type has a dynamic predicate, because the
13336 -- generated function will mention the actual subtype. The
13337 -- predicate may come from an explicit aspect of be inherited.
13339 elsif Has_Predicates (T) then
13340 Insert_List_After_And_Analyze (Decl,
13341 Freeze_Entity (Defining_Identifier (Decl), N));
13342 end if;
13344 if Nkind (N) = N_Accept_Statement
13345 and then Expander_Active
13346 then
13347 Set_Actual_Subtype (Renamed_Object (Formal),
13348 Defining_Identifier (Decl));
13349 else
13350 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
13351 end if;
13352 end if;
13354 Next_Formal (Formal);
13355 end loop;
13356 end Set_Actual_Subtypes;
13358 ---------------------
13359 -- Set_Formal_Mode --
13360 ---------------------
13362 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
13363 Spec : constant Node_Id := Parent (Formal_Id);
13364 Id : constant Entity_Id := Scope (Formal_Id);
13366 begin
13367 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
13368 -- since we ensure that corresponding actuals are always valid at the
13369 -- point of the call.
13371 if Out_Present (Spec) then
13372 if Is_Entry (Id)
13373 or else Is_Subprogram_Or_Generic_Subprogram (Id)
13374 then
13375 Set_Has_Out_Or_In_Out_Parameter (Id, True);
13376 end if;
13378 if Ekind (Id) in E_Function | E_Generic_Function then
13380 -- [IN] OUT parameters allowed for functions in Ada 2012
13382 if Ada_Version >= Ada_2012 then
13384 -- Even in Ada 2012 operators can only have IN parameters
13386 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
13387 Error_Msg_N ("operators can only have IN parameters", Spec);
13388 end if;
13390 if In_Present (Spec) then
13391 Mutate_Ekind (Formal_Id, E_In_Out_Parameter);
13392 else
13393 Mutate_Ekind (Formal_Id, E_Out_Parameter);
13394 end if;
13396 -- But not in earlier versions of Ada
13398 else
13399 Error_Msg_N ("functions can only have IN parameters", Spec);
13400 Mutate_Ekind (Formal_Id, E_In_Parameter);
13401 end if;
13403 elsif In_Present (Spec) then
13404 Mutate_Ekind (Formal_Id, E_In_Out_Parameter);
13406 else
13407 Mutate_Ekind (Formal_Id, E_Out_Parameter);
13408 Set_Is_True_Constant (Formal_Id, False);
13409 Set_Current_Value (Formal_Id, Empty);
13410 end if;
13412 else
13413 Mutate_Ekind (Formal_Id, E_In_Parameter);
13414 end if;
13416 -- Set Is_Known_Non_Null for access parameters since the language
13417 -- guarantees that access parameters are always non-null. We also set
13418 -- Can_Never_Be_Null, since there is no way to change the value.
13420 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
13422 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
13423 -- null; In Ada 2005, only if then null_exclusion is explicit.
13425 if Ada_Version < Ada_2005
13426 or else Can_Never_Be_Null (Etype (Formal_Id))
13427 then
13428 Set_Is_Known_Non_Null (Formal_Id);
13429 Set_Can_Never_Be_Null (Formal_Id);
13430 end if;
13432 -- Ada 2005 (AI-231): Null-exclusion access subtype
13434 elsif Is_Access_Type (Etype (Formal_Id))
13435 and then Can_Never_Be_Null (Etype (Formal_Id))
13436 then
13437 Set_Is_Known_Non_Null (Formal_Id);
13439 -- We can also set Can_Never_Be_Null (thus preventing some junk
13440 -- access checks) for the case of an IN parameter, which cannot
13441 -- be changed, or for an IN OUT parameter, which can be changed but
13442 -- not to a null value. But for an OUT parameter, the initial value
13443 -- passed in can be null, so we can't set this flag in that case.
13445 if Ekind (Formal_Id) /= E_Out_Parameter then
13446 Set_Can_Never_Be_Null (Formal_Id);
13447 end if;
13448 end if;
13450 Set_Mechanism (Formal_Id, Default_Mechanism);
13451 Set_Formal_Validity (Formal_Id);
13452 end Set_Formal_Mode;
13454 -------------------------
13455 -- Set_Formal_Validity --
13456 -------------------------
13458 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
13459 begin
13460 -- If no validity checking, then we cannot assume anything about the
13461 -- validity of parameters, since we do not know there is any checking
13462 -- of the validity on the call side.
13464 if not Validity_Checks_On then
13465 return;
13467 -- If validity checking for parameters is enabled, this means we are
13468 -- not supposed to make any assumptions about argument values.
13470 elsif Validity_Check_Parameters then
13471 return;
13473 -- If we are checking in parameters, we will assume that the caller is
13474 -- also checking parameters, so we can assume the parameter is valid.
13476 elsif Ekind (Formal_Id) = E_In_Parameter
13477 and then Validity_Check_In_Params
13478 then
13479 Set_Is_Known_Valid (Formal_Id, True);
13481 -- Similar treatment for IN OUT parameters
13483 elsif Ekind (Formal_Id) = E_In_Out_Parameter
13484 and then Validity_Check_In_Out_Params
13485 then
13486 Set_Is_Known_Valid (Formal_Id, True);
13487 end if;
13488 end Set_Formal_Validity;
13490 ------------------------
13491 -- Subtype_Conformant --
13492 ------------------------
13494 function Subtype_Conformant
13495 (New_Id : Entity_Id;
13496 Old_Id : Entity_Id;
13497 Skip_Controlling_Formals : Boolean := False) return Boolean
13499 Result : Boolean;
13500 begin
13501 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
13502 Skip_Controlling_Formals => Skip_Controlling_Formals);
13503 return Result;
13504 end Subtype_Conformant;
13506 ---------------------
13507 -- Type_Conformant --
13508 ---------------------
13510 function Type_Conformant
13511 (New_Id : Entity_Id;
13512 Old_Id : Entity_Id;
13513 Skip_Controlling_Formals : Boolean := False) return Boolean
13515 Result : Boolean;
13516 begin
13517 May_Hide_Profile := False;
13518 Check_Conformance
13519 (New_Id, Old_Id, Type_Conformant, False, Result,
13520 Skip_Controlling_Formals => Skip_Controlling_Formals);
13521 return Result;
13522 end Type_Conformant;
13524 -------------------------------
13525 -- Valid_Operator_Definition --
13526 -------------------------------
13528 procedure Valid_Operator_Definition (Designator : Entity_Id) is
13529 N : Integer := 0;
13530 F : Entity_Id;
13531 Id : constant Name_Id := Chars (Designator);
13532 N_OK : Boolean;
13534 begin
13535 F := First_Formal (Designator);
13536 while Present (F) loop
13537 N := N + 1;
13539 if Present (Default_Value (F)) then
13540 Error_Msg_N
13541 ("default values not allowed for operator parameters",
13542 Parent (F));
13544 -- For function instantiations that are operators, we must check
13545 -- separately that the corresponding generic only has in-parameters.
13546 -- For subprogram declarations this is done in Set_Formal_Mode. Such
13547 -- an error could not arise in earlier versions of the language.
13549 elsif Ekind (F) /= E_In_Parameter then
13550 Error_Msg_N ("operators can only have IN parameters", F);
13551 end if;
13553 Next_Formal (F);
13554 end loop;
13556 -- Verify that user-defined operators have proper number of arguments
13557 -- First case of operators which can only be unary
13559 if Id in Name_Op_Not | Name_Op_Abs then
13560 N_OK := (N = 1);
13562 -- Case of operators which can be unary or binary
13564 elsif Id in Name_Op_Add | Name_Op_Subtract then
13565 N_OK := (N in 1 .. 2);
13567 -- All other operators can only be binary
13569 else
13570 N_OK := (N = 2);
13571 end if;
13573 if not N_OK then
13574 Error_Msg_N
13575 ("incorrect number of arguments for operator", Designator);
13576 end if;
13578 if Id = Name_Op_Ne
13579 and then Base_Type (Etype (Designator)) = Standard_Boolean
13580 and then not Is_Intrinsic_Subprogram (Designator)
13581 then
13582 Error_Msg_N
13583 ("explicit definition of inequality not allowed", Designator);
13584 end if;
13585 end Valid_Operator_Definition;
13587 end Sem_Ch6;