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