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