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