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