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