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