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