Debian Codegraph @

gcc/ada/sem_ch6.adb

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