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