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