exp_prag.adb (Expand_Pragma_Loop_Variant): Rewrite pragma as null statement if ignored.
[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 -- True if 'Result used in a non-trivial postcondition or contract-cases
7067
7068 No_Warning_On_Some_Postcondition : Boolean := False;
7069 -- True if there is a non-trivial postcondition or contract-cases
7070 -- without a corresponding warning.
7071
7072 Post_State_Mentioned : Boolean := False;
7073 -- True if expression mentioned in a postcondition or contract-cases
7074 -- can have a different value in the post-state than in the pre-state.
7075
7076 function Check_Attr_Result (N : Node_Id) return Traverse_Result;
7077 -- Check if N is a reference to the attribute 'Result, and if so set
7078 -- Attribute_Result_Mentioned and return Abandon. Otherwise return OK.
7079
7080 function Check_Post_State (N : Node_Id) return Traverse_Result;
7081 -- Check whether the value of evaluating N can be different in the
7082 -- post-state, compared to the same evaluation in the pre-state, and
7083 -- if so set Post_State_Mentioned and return Abandon. Return Skip on
7084 -- reference to attribute 'Old, in order to ignore its prefix, which
7085 -- is precisely evaluated in the pre-state. Otherwise return OK.
7086
7087 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean;
7088 -- Return True if node N is trivially "True" or "False", and it comes
7089 -- from source. In particular, nodes that are statically known "True" or
7090 -- "False" by the compiler but not written as such in source code are
7091 -- not considered as trivial.
7092
7093 procedure Process_Contract_Cases (Spec : Node_Id);
7094 -- This processes the Spec_CTC_List from Spec, processing any contract
7095 -- case from the list. The caller has checked that Spec_CTC_List is
7096 -- non-Empty.
7097
7098 procedure Process_Post_Conditions (Spec : Node_Id; Class : Boolean);
7099 -- This processes the Spec_PPC_List from Spec, processing any
7100 -- postcondition from the list. If Class is True, then only
7101 -- postconditions marked with Class_Present are considered. The
7102 -- caller has checked that Spec_PPC_List is non-Empty.
7103
7104 function Find_Attribute_Result is new Traverse_Func (Check_Attr_Result);
7105
7106 function Find_Post_State is new Traverse_Func (Check_Post_State);
7107
7108 -----------------------
7109 -- Check_Attr_Result --
7110 -----------------------
7111
7112 function Check_Attr_Result (N : Node_Id) return Traverse_Result is
7113 begin
7114 if Nkind (N) = N_Attribute_Reference
7115 and then Get_Attribute_Id (Attribute_Name (N)) = Attribute_Result
7116 then
7117 Attribute_Result_Mentioned := True;
7118 return Abandon;
7119 else
7120 return OK;
7121 end if;
7122 end Check_Attr_Result;
7123
7124 ----------------------
7125 -- Check_Post_State --
7126 ----------------------
7127
7128 function Check_Post_State (N : Node_Id) return Traverse_Result is
7129 Found : Boolean := False;
7130
7131 begin
7132 case Nkind (N) is
7133 when N_Function_Call |
7134 N_Explicit_Dereference =>
7135 Found := True;
7136
7137 when N_Identifier |
7138 N_Expanded_Name =>
7139
7140 declare
7141 E : constant Entity_Id := Entity (N);
7142
7143 begin
7144 -- ???Quantified expressions get analyzed later, so E can
7145 -- be empty at this point. In this case, we suppress the
7146 -- warning, just in case E is assignable. It seems better to
7147 -- have false negatives than false positives. At some point,
7148 -- we should make the warning more accurate, either by
7149 -- analyzing quantified expressions earlier, or moving
7150 -- this processing later.
7151
7152 if No (E)
7153 or else
7154 (Is_Entity_Name (N)
7155 and then Ekind (E) in Assignable_Kind)
7156 then
7157 Found := True;
7158 end if;
7159 end;
7160
7161 when N_Attribute_Reference =>
7162 case Get_Attribute_Id (Attribute_Name (N)) is
7163 when Attribute_Old =>
7164 return Skip;
7165 when Attribute_Result =>
7166 Found := True;
7167 when others =>
7168 null;
7169 end case;
7170
7171 when others =>
7172 null;
7173 end case;
7174
7175 if Found then
7176 Post_State_Mentioned := True;
7177 return Abandon;
7178 else
7179 return OK;
7180 end if;
7181 end Check_Post_State;
7182
7183 --------------------------------
7184 -- Is_Trivial_Post_Or_Ensures --
7185 --------------------------------
7186
7187 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean is
7188 begin
7189 return Is_Entity_Name (N)
7190 and then (Entity (N) = Standard_True
7191 or else
7192 Entity (N) = Standard_False)
7193 and then Comes_From_Source (N);
7194 end Is_Trivial_Post_Or_Ensures;
7195
7196 ----------------------------
7197 -- Process_Contract_Cases --
7198 ----------------------------
7199
7200 procedure Process_Contract_Cases (Spec : Node_Id) is
7201 Prag : Node_Id;
7202 Aggr : Node_Id;
7203 Conseq : Node_Id;
7204 Post_Case : Node_Id;
7205
7206 Ignored : Traverse_Final_Result;
7207 pragma Unreferenced (Ignored);
7208
7209 begin
7210 Prag := Spec_CTC_List (Contract (Spec));
7211 loop
7212 if Pragma_Name (Prag) = Name_Contract_Cases then
7213 Aggr :=
7214 Expression (First (Pragma_Argument_Associations (Prag)));
7215
7216 Post_Case := First (Component_Associations (Aggr));
7217 while Present (Post_Case) loop
7218 Conseq := Expression (Post_Case);
7219
7220 -- Ignore trivial contract-cases when consequence is "True"
7221 -- or "False".
7222
7223 if not Is_Trivial_Post_Or_Ensures (Conseq) then
7224 Last_Contract_Cases := Prag;
7225
7226 -- For functions, look for presence of 'Result in
7227 -- consequence expression.
7228
7229 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7230 Ignored := Find_Attribute_Result (Conseq);
7231 end if;
7232
7233 -- For each individual case, look for presence of an
7234 -- expression that could be evaluated differently in
7235 -- post-state.
7236
7237 Post_State_Mentioned := False;
7238 Ignored := Find_Post_State (Conseq);
7239
7240 if Post_State_Mentioned then
7241 No_Warning_On_Some_Postcondition := True;
7242 else
7243 Error_Msg_N
7244 ("contract case refers only to pre-state?T?",
7245 Conseq);
7246 end if;
7247 end if;
7248
7249 Next (Post_Case);
7250 end loop;
7251 end if;
7252
7253 Prag := Next_Pragma (Prag);
7254 exit when No (Prag);
7255 end loop;
7256 end Process_Contract_Cases;
7257
7258 -----------------------------
7259 -- Process_Post_Conditions --
7260 -----------------------------
7261
7262 procedure Process_Post_Conditions
7263 (Spec : Node_Id;
7264 Class : Boolean)
7265 is
7266 Prag : Node_Id;
7267 Arg : Node_Id;
7268 Ignored : Traverse_Final_Result;
7269 pragma Unreferenced (Ignored);
7270
7271 begin
7272 Prag := Spec_PPC_List (Contract (Spec));
7273 loop
7274 Arg := First (Pragma_Argument_Associations (Prag));
7275
7276 -- Ignore trivial postcondition of "True" or "False"
7277
7278 if Pragma_Name (Prag) = Name_Postcondition
7279 and then not Is_Trivial_Post_Or_Ensures (Expression (Arg))
7280 then
7281 -- Since pre- and post-conditions are listed in reverse order,
7282 -- the first postcondition in the list is last in the source.
7283
7284 if not Class and then No (Last_Postcondition) then
7285 Last_Postcondition := Prag;
7286 end if;
7287
7288 -- For functions, look for presence of 'Result in postcondition
7289
7290 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7291 Ignored := Find_Attribute_Result (Arg);
7292 end if;
7293
7294 -- For each individual non-inherited postcondition, look
7295 -- for presence of an expression that could be evaluated
7296 -- differently in post-state.
7297
7298 if not Class then
7299 Post_State_Mentioned := False;
7300 Ignored := Find_Post_State (Arg);
7301
7302 if Post_State_Mentioned then
7303 No_Warning_On_Some_Postcondition := True;
7304 else
7305 Error_Msg_N
7306 ("postcondition refers only to pre-state?T?", Prag);
7307 end if;
7308 end if;
7309 end if;
7310
7311 Prag := Next_Pragma (Prag);
7312 exit when No (Prag);
7313 end loop;
7314 end Process_Post_Conditions;
7315
7316 -- Start of processing for Check_Subprogram_Contract
7317
7318 begin
7319 if not Warn_On_Suspicious_Contract then
7320 return;
7321 end if;
7322
7323 -- Process spec postconditions
7324
7325 if Present (Spec_PPC_List (Contract (Spec_Id))) then
7326 Process_Post_Conditions (Spec_Id, Class => False);
7327 end if;
7328
7329 -- Process inherited postconditions
7330
7331 -- Code is currently commented out as, in some cases, it causes crashes
7332 -- because Direct_Primitive_Operations is not available for a private
7333 -- type. This may cause more warnings to be issued than necessary. ???
7334
7335 -- for J in Inherited'Range loop
7336 -- if Present (Spec_PPC_List (Contract (Inherited (J)))) then
7337 -- Process_Post_Conditions (Inherited (J), Class => True);
7338 -- end if;
7339 -- end loop;
7340
7341 -- Process contract cases
7342
7343 if Present (Spec_CTC_List (Contract (Spec_Id))) then
7344 Process_Contract_Cases (Spec_Id);
7345 end if;
7346
7347 -- Issue warning for functions whose postcondition does not mention
7348 -- 'Result after all postconditions have been processed, and provided
7349 -- all postconditions do not already get a warning that they only refer
7350 -- to pre-state.
7351
7352 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
7353 and then (Present (Last_Postcondition)
7354 or else Present (Last_Contract_Cases))
7355 and then not Attribute_Result_Mentioned
7356 and then No_Warning_On_Some_Postcondition
7357 then
7358 if Present (Last_Postcondition) then
7359 if Present (Last_Contract_Cases) then
7360 Error_Msg_N
7361 ("neither function postcondition nor "
7362 & "contract cases mention result?T?", Last_Postcondition);
7363
7364 else
7365 Error_Msg_N
7366 ("function postcondition does not mention result?T?",
7367 Last_Postcondition);
7368 end if;
7369 else
7370 Error_Msg_N
7371 ("contract cases do not mention result?T?", Last_Contract_Cases);
7372 end if;
7373 end if;
7374 end Check_Subprogram_Contract;
7375
7376 ----------------------------
7377 -- Check_Subprogram_Order --
7378 ----------------------------
7379
7380 procedure Check_Subprogram_Order (N : Node_Id) is
7381
7382 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7383 -- This is used to check if S1 > S2 in the sense required by this test,
7384 -- for example nameab < namec, but name2 < name10.
7385
7386 -----------------------------
7387 -- Subprogram_Name_Greater --
7388 -----------------------------
7389
7390 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7391 L1, L2 : Positive;
7392 N1, N2 : Natural;
7393
7394 begin
7395 -- Deal with special case where names are identical except for a
7396 -- numerical suffix. These are handled specially, taking the numeric
7397 -- ordering from the suffix into account.
7398
7399 L1 := S1'Last;
7400 while S1 (L1) in '0' .. '9' loop
7401 L1 := L1 - 1;
7402 end loop;
7403
7404 L2 := S2'Last;
7405 while S2 (L2) in '0' .. '9' loop
7406 L2 := L2 - 1;
7407 end loop;
7408
7409 -- If non-numeric parts non-equal, do straight compare
7410
7411 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7412 return S1 > S2;
7413
7414 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7415 -- that a missing suffix is treated as numeric zero in this test.
7416
7417 else
7418 N1 := 0;
7419 while L1 < S1'Last loop
7420 L1 := L1 + 1;
7421 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7422 end loop;
7423
7424 N2 := 0;
7425 while L2 < S2'Last loop
7426 L2 := L2 + 1;
7427 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7428 end loop;
7429
7430 return N1 > N2;
7431 end if;
7432 end Subprogram_Name_Greater;
7433
7434 -- Start of processing for Check_Subprogram_Order
7435
7436 begin
7437 -- Check body in alpha order if this is option
7438
7439 if Style_Check
7440 and then Style_Check_Order_Subprograms
7441 and then Nkind (N) = N_Subprogram_Body
7442 and then Comes_From_Source (N)
7443 and then In_Extended_Main_Source_Unit (N)
7444 then
7445 declare
7446 LSN : String_Ptr
7447 renames Scope_Stack.Table
7448 (Scope_Stack.Last).Last_Subprogram_Name;
7449
7450 Body_Id : constant Entity_Id :=
7451 Defining_Entity (Specification (N));
7452
7453 begin
7454 Get_Decoded_Name_String (Chars (Body_Id));
7455
7456 if LSN /= null then
7457 if Subprogram_Name_Greater
7458 (LSN.all, Name_Buffer (1 .. Name_Len))
7459 then
7460 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7461 end if;
7462
7463 Free (LSN);
7464 end if;
7465
7466 LSN := new String'(Name_Buffer (1 .. Name_Len));
7467 end;
7468 end if;
7469 end Check_Subprogram_Order;
7470
7471 ------------------------------
7472 -- Check_Subtype_Conformant --
7473 ------------------------------
7474
7475 procedure Check_Subtype_Conformant
7476 (New_Id : Entity_Id;
7477 Old_Id : Entity_Id;
7478 Err_Loc : Node_Id := Empty;
7479 Skip_Controlling_Formals : Boolean := False;
7480 Get_Inst : Boolean := False)
7481 is
7482 Result : Boolean;
7483 pragma Warnings (Off, Result);
7484 begin
7485 Check_Conformance
7486 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7487 Skip_Controlling_Formals => Skip_Controlling_Formals,
7488 Get_Inst => Get_Inst);
7489 end Check_Subtype_Conformant;
7490
7491 ---------------------------
7492 -- Check_Type_Conformant --
7493 ---------------------------
7494
7495 procedure Check_Type_Conformant
7496 (New_Id : Entity_Id;
7497 Old_Id : Entity_Id;
7498 Err_Loc : Node_Id := Empty)
7499 is
7500 Result : Boolean;
7501 pragma Warnings (Off, Result);
7502 begin
7503 Check_Conformance
7504 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7505 end Check_Type_Conformant;
7506
7507 ---------------------------
7508 -- Can_Override_Operator --
7509 ---------------------------
7510
7511 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7512 Typ : Entity_Id;
7513
7514 begin
7515 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7516 return False;
7517
7518 else
7519 Typ := Base_Type (Etype (First_Formal (Subp)));
7520
7521 -- Check explicitly that the operation is a primitive of the type
7522
7523 return Operator_Matches_Spec (Subp, Subp)
7524 and then not Is_Generic_Type (Typ)
7525 and then Scope (Subp) = Scope (Typ)
7526 and then not Is_Class_Wide_Type (Typ);
7527 end if;
7528 end Can_Override_Operator;
7529
7530 ----------------------
7531 -- Conforming_Types --
7532 ----------------------
7533
7534 function Conforming_Types
7535 (T1 : Entity_Id;
7536 T2 : Entity_Id;
7537 Ctype : Conformance_Type;
7538 Get_Inst : Boolean := False) return Boolean
7539 is
7540 Type_1 : Entity_Id := T1;
7541 Type_2 : Entity_Id := T2;
7542 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
7543
7544 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
7545 -- If neither T1 nor T2 are generic actual types, or if they are in
7546 -- different scopes (e.g. parent and child instances), then verify that
7547 -- the base types are equal. Otherwise T1 and T2 must be on the same
7548 -- subtype chain. The whole purpose of this procedure is to prevent
7549 -- spurious ambiguities in an instantiation that may arise if two
7550 -- distinct generic types are instantiated with the same actual.
7551
7552 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7553 -- An access parameter can designate an incomplete type. If the
7554 -- incomplete type is the limited view of a type from a limited_
7555 -- with_clause, check whether the non-limited view is available. If
7556 -- it is a (non-limited) incomplete type, get the full view.
7557
7558 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7559 -- Returns True if and only if either T1 denotes a limited view of T2
7560 -- or T2 denotes a limited view of T1. This can arise when the limited
7561 -- with view of a type is used in a subprogram declaration and the
7562 -- subprogram body is in the scope of a regular with clause for the
7563 -- same unit. In such a case, the two type entities can be considered
7564 -- identical for purposes of conformance checking.
7565
7566 ----------------------
7567 -- Base_Types_Match --
7568 ----------------------
7569
7570 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7571 BT1 : constant Entity_Id := Base_Type (T1);
7572 BT2 : constant Entity_Id := Base_Type (T2);
7573
7574 begin
7575 if T1 = T2 then
7576 return True;
7577
7578 elsif BT1 = BT2 then
7579
7580 -- The following is too permissive. A more precise test should
7581 -- check that the generic actual is an ancestor subtype of the
7582 -- other ???.
7583
7584 -- See code in Find_Corresponding_Spec that applies an additional
7585 -- filter to handle accidental amiguities in instances.
7586
7587 return not Is_Generic_Actual_Type (T1)
7588 or else not Is_Generic_Actual_Type (T2)
7589 or else Scope (T1) /= Scope (T2);
7590
7591 -- If T2 is a generic actual type it is declared as the subtype of
7592 -- the actual. If that actual is itself a subtype we need to use
7593 -- its own base type to check for compatibility.
7594
7595 elsif Ekind (BT2) = Ekind (T2) and then BT1 = Base_Type (BT2) then
7596 return True;
7597
7598 elsif Ekind (BT1) = Ekind (T1) and then BT2 = Base_Type (BT1) then
7599 return True;
7600
7601 else
7602 return False;
7603 end if;
7604 end Base_Types_Match;
7605
7606 --------------------------
7607 -- Find_Designated_Type --
7608 --------------------------
7609
7610 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7611 Desig : Entity_Id;
7612
7613 begin
7614 Desig := Directly_Designated_Type (T);
7615
7616 if Ekind (Desig) = E_Incomplete_Type then
7617
7618 -- If regular incomplete type, get full view if available
7619
7620 if Present (Full_View (Desig)) then
7621 Desig := Full_View (Desig);
7622
7623 -- If limited view of a type, get non-limited view if available,
7624 -- and check again for a regular incomplete type.
7625
7626 elsif Present (Non_Limited_View (Desig)) then
7627 Desig := Get_Full_View (Non_Limited_View (Desig));
7628 end if;
7629 end if;
7630
7631 return Desig;
7632 end Find_Designated_Type;
7633
7634 -------------------------------
7635 -- Matches_Limited_With_View --
7636 -------------------------------
7637
7638 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7639 begin
7640 -- In some cases a type imported through a limited_with clause, and
7641 -- its nonlimited view are both visible, for example in an anonymous
7642 -- access-to-class-wide type in a formal. Both entities designate the
7643 -- same type.
7644
7645 if From_With_Type (T1) and then T2 = Available_View (T1) then
7646 return True;
7647
7648 elsif From_With_Type (T2) and then T1 = Available_View (T2) then
7649 return True;
7650
7651 elsif From_With_Type (T1)
7652 and then From_With_Type (T2)
7653 and then Available_View (T1) = Available_View (T2)
7654 then
7655 return True;
7656
7657 else
7658 return False;
7659 end if;
7660 end Matches_Limited_With_View;
7661
7662 -- Start of processing for Conforming_Types
7663
7664 begin
7665 -- The context is an instance association for a formal access-to-
7666 -- subprogram type; the formal parameter types require mapping because
7667 -- they may denote other formal parameters of the generic unit.
7668
7669 if Get_Inst then
7670 Type_1 := Get_Instance_Of (T1);
7671 Type_2 := Get_Instance_Of (T2);
7672 end if;
7673
7674 -- If one of the types is a view of the other introduced by a limited
7675 -- with clause, treat these as conforming for all purposes.
7676
7677 if Matches_Limited_With_View (T1, T2) then
7678 return True;
7679
7680 elsif Base_Types_Match (Type_1, Type_2) then
7681 return Ctype <= Mode_Conformant
7682 or else Subtypes_Statically_Match (Type_1, Type_2);
7683
7684 elsif Is_Incomplete_Or_Private_Type (Type_1)
7685 and then Present (Full_View (Type_1))
7686 and then Base_Types_Match (Full_View (Type_1), Type_2)
7687 then
7688 return Ctype <= Mode_Conformant
7689 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7690
7691 elsif Ekind (Type_2) = E_Incomplete_Type
7692 and then Present (Full_View (Type_2))
7693 and then Base_Types_Match (Type_1, Full_View (Type_2))
7694 then
7695 return Ctype <= Mode_Conformant
7696 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7697
7698 elsif Is_Private_Type (Type_2)
7699 and then In_Instance
7700 and then Present (Full_View (Type_2))
7701 and then Base_Types_Match (Type_1, Full_View (Type_2))
7702 then
7703 return Ctype <= Mode_Conformant
7704 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
7705 end if;
7706
7707 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
7708 -- treated recursively because they carry a signature.
7709
7710 Are_Anonymous_Access_To_Subprogram_Types :=
7711 Ekind (Type_1) = Ekind (Type_2)
7712 and then
7713 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
7714 E_Anonymous_Access_Protected_Subprogram_Type);
7715
7716 -- Test anonymous access type case. For this case, static subtype
7717 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7718 -- the base types because we may have built internal subtype entities
7719 -- to handle null-excluding types (see Process_Formals).
7720
7721 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7722 and then
7723 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
7724
7725 -- Ada 2005 (AI-254)
7726
7727 or else Are_Anonymous_Access_To_Subprogram_Types
7728 then
7729 declare
7730 Desig_1 : Entity_Id;
7731 Desig_2 : Entity_Id;
7732
7733 begin
7734 -- In Ada 2005, access constant indicators must match for
7735 -- subtype conformance.
7736
7737 if Ada_Version >= Ada_2005
7738 and then Ctype >= Subtype_Conformant
7739 and then
7740 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7741 then
7742 return False;
7743 end if;
7744
7745 Desig_1 := Find_Designated_Type (Type_1);
7746 Desig_2 := Find_Designated_Type (Type_2);
7747
7748 -- If the context is an instance association for a formal
7749 -- access-to-subprogram type; formal access parameter designated
7750 -- types require mapping because they may denote other formal
7751 -- parameters of the generic unit.
7752
7753 if Get_Inst then
7754 Desig_1 := Get_Instance_Of (Desig_1);
7755 Desig_2 := Get_Instance_Of (Desig_2);
7756 end if;
7757
7758 -- It is possible for a Class_Wide_Type to be introduced for an
7759 -- incomplete type, in which case there is a separate class_ wide
7760 -- type for the full view. The types conform if their Etypes
7761 -- conform, i.e. one may be the full view of the other. This can
7762 -- only happen in the context of an access parameter, other uses
7763 -- of an incomplete Class_Wide_Type are illegal.
7764
7765 if Is_Class_Wide_Type (Desig_1)
7766 and then
7767 Is_Class_Wide_Type (Desig_2)
7768 then
7769 return
7770 Conforming_Types
7771 (Etype (Base_Type (Desig_1)),
7772 Etype (Base_Type (Desig_2)), Ctype);
7773
7774 elsif Are_Anonymous_Access_To_Subprogram_Types then
7775 if Ada_Version < Ada_2005 then
7776 return Ctype = Type_Conformant
7777 or else
7778 Subtypes_Statically_Match (Desig_1, Desig_2);
7779
7780 -- We must check the conformance of the signatures themselves
7781
7782 else
7783 declare
7784 Conformant : Boolean;
7785 begin
7786 Check_Conformance
7787 (Desig_1, Desig_2, Ctype, False, Conformant);
7788 return Conformant;
7789 end;
7790 end if;
7791
7792 else
7793 return Base_Type (Desig_1) = Base_Type (Desig_2)
7794 and then (Ctype = Type_Conformant
7795 or else
7796 Subtypes_Statically_Match (Desig_1, Desig_2));
7797 end if;
7798 end;
7799
7800 -- Otherwise definitely no match
7801
7802 else
7803 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7804 and then Is_Access_Type (Type_2))
7805 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7806 and then Is_Access_Type (Type_1)))
7807 and then
7808 Conforming_Types
7809 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7810 then
7811 May_Hide_Profile := True;
7812 end if;
7813
7814 return False;
7815 end if;
7816 end Conforming_Types;
7817
7818 --------------------------
7819 -- Create_Extra_Formals --
7820 --------------------------
7821
7822 procedure Create_Extra_Formals (E : Entity_Id) is
7823 Formal : Entity_Id;
7824 First_Extra : Entity_Id := Empty;
7825 Last_Extra : Entity_Id;
7826 Formal_Type : Entity_Id;
7827 P_Formal : Entity_Id := Empty;
7828
7829 function Add_Extra_Formal
7830 (Assoc_Entity : Entity_Id;
7831 Typ : Entity_Id;
7832 Scope : Entity_Id;
7833 Suffix : String) return Entity_Id;
7834 -- Add an extra formal to the current list of formals and extra formals.
7835 -- The extra formal is added to the end of the list of extra formals,
7836 -- and also returned as the result. These formals are always of mode IN.
7837 -- The new formal has the type Typ, is declared in Scope, and its name
7838 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
7839 -- The following suffixes are currently used. They should not be changed
7840 -- without coordinating with CodePeer, which makes use of these to
7841 -- provide better messages.
7842
7843 -- O denotes the Constrained bit.
7844 -- L denotes the accessibility level.
7845 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7846 -- the full list in exp_ch6.BIP_Formal_Kind.
7847
7848 ----------------------
7849 -- Add_Extra_Formal --
7850 ----------------------
7851
7852 function Add_Extra_Formal
7853 (Assoc_Entity : Entity_Id;
7854 Typ : Entity_Id;
7855 Scope : Entity_Id;
7856 Suffix : String) return Entity_Id
7857 is
7858 EF : constant Entity_Id :=
7859 Make_Defining_Identifier (Sloc (Assoc_Entity),
7860 Chars => New_External_Name (Chars (Assoc_Entity),
7861 Suffix => Suffix));
7862
7863 begin
7864 -- A little optimization. Never generate an extra formal for the
7865 -- _init operand of an initialization procedure, since it could
7866 -- never be used.
7867
7868 if Chars (Formal) = Name_uInit then
7869 return Empty;
7870 end if;
7871
7872 Set_Ekind (EF, E_In_Parameter);
7873 Set_Actual_Subtype (EF, Typ);
7874 Set_Etype (EF, Typ);
7875 Set_Scope (EF, Scope);
7876 Set_Mechanism (EF, Default_Mechanism);
7877 Set_Formal_Validity (EF);
7878
7879 if No (First_Extra) then
7880 First_Extra := EF;
7881 Set_Extra_Formals (Scope, First_Extra);
7882 end if;
7883
7884 if Present (Last_Extra) then
7885 Set_Extra_Formal (Last_Extra, EF);
7886 end if;
7887
7888 Last_Extra := EF;
7889
7890 return EF;
7891 end Add_Extra_Formal;
7892
7893 -- Start of processing for Create_Extra_Formals
7894
7895 begin
7896 -- We never generate extra formals if expansion is not active because we
7897 -- don't need them unless we are generating code.
7898
7899 if not Expander_Active then
7900 return;
7901 end if;
7902
7903 -- No need to generate extra formals in interface thunks whose target
7904 -- primitive has no extra formals.
7905
7906 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
7907 return;
7908 end if;
7909
7910 -- If this is a derived subprogram then the subtypes of the parent
7911 -- subprogram's formal parameters will be used to determine the need
7912 -- for extra formals.
7913
7914 if Is_Overloadable (E) and then Present (Alias (E)) then
7915 P_Formal := First_Formal (Alias (E));
7916 end if;
7917
7918 Last_Extra := Empty;
7919 Formal := First_Formal (E);
7920 while Present (Formal) loop
7921 Last_Extra := Formal;
7922 Next_Formal (Formal);
7923 end loop;
7924
7925 -- If Extra_formals were already created, don't do it again. This
7926 -- situation may arise for subprogram types created as part of
7927 -- dispatching calls (see Expand_Dispatching_Call)
7928
7929 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
7930 return;
7931 end if;
7932
7933 -- If the subprogram is a predefined dispatching subprogram then don't
7934 -- generate any extra constrained or accessibility level formals. In
7935 -- general we suppress these for internal subprograms (by not calling
7936 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7937 -- generated stream attributes do get passed through because extra
7938 -- build-in-place formals are needed in some cases (limited 'Input).
7939
7940 if Is_Predefined_Internal_Operation (E) then
7941 goto Test_For_Func_Result_Extras;
7942 end if;
7943
7944 Formal := First_Formal (E);
7945 while Present (Formal) loop
7946
7947 -- Create extra formal for supporting the attribute 'Constrained.
7948 -- The case of a private type view without discriminants also
7949 -- requires the extra formal if the underlying type has defaulted
7950 -- discriminants.
7951
7952 if Ekind (Formal) /= E_In_Parameter then
7953 if Present (P_Formal) then
7954 Formal_Type := Etype (P_Formal);
7955 else
7956 Formal_Type := Etype (Formal);
7957 end if;
7958
7959 -- Do not produce extra formals for Unchecked_Union parameters.
7960 -- Jump directly to the end of the loop.
7961
7962 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7963 goto Skip_Extra_Formal_Generation;
7964 end if;
7965
7966 if not Has_Discriminants (Formal_Type)
7967 and then Ekind (Formal_Type) in Private_Kind
7968 and then Present (Underlying_Type (Formal_Type))
7969 then
7970 Formal_Type := Underlying_Type (Formal_Type);
7971 end if;
7972
7973 -- Suppress the extra formal if formal's subtype is constrained or
7974 -- indefinite, or we're compiling for Ada 2012 and the underlying
7975 -- type is tagged and limited. In Ada 2012, a limited tagged type
7976 -- can have defaulted discriminants, but 'Constrained is required
7977 -- to return True, so the formal is never needed (see AI05-0214).
7978 -- Note that this ensures consistency of calling sequences for
7979 -- dispatching operations when some types in a class have defaults
7980 -- on discriminants and others do not (and requiring the extra
7981 -- formal would introduce distributed overhead).
7982
7983 if Has_Discriminants (Formal_Type)
7984 and then not Is_Constrained (Formal_Type)
7985 and then not Is_Indefinite_Subtype (Formal_Type)
7986 and then (Ada_Version < Ada_2012
7987 or else
7988 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
7989 and then Is_Limited_Type (Formal_Type)))
7990 then
7991 Set_Extra_Constrained
7992 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
7993 end if;
7994 end if;
7995
7996 -- Create extra formal for supporting accessibility checking. This
7997 -- is done for both anonymous access formals and formals of named
7998 -- access types that are marked as controlling formals. The latter
7999 -- case can occur when Expand_Dispatching_Call creates a subprogram
8000 -- type and substitutes the types of access-to-class-wide actuals
8001 -- for the anonymous access-to-specific-type of controlling formals.
8002 -- Base_Type is applied because in cases where there is a null
8003 -- exclusion the formal may have an access subtype.
8004
8005 -- This is suppressed if we specifically suppress accessibility
8006 -- checks at the package level for either the subprogram, or the
8007 -- package in which it resides. However, we do not suppress it
8008 -- simply if the scope has accessibility checks suppressed, since
8009 -- this could cause trouble when clients are compiled with a
8010 -- different suppression setting. The explicit checks at the
8011 -- package level are safe from this point of view.
8012
8013 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8014 or else (Is_Controlling_Formal (Formal)
8015 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8016 and then not
8017 (Explicit_Suppress (E, Accessibility_Check)
8018 or else
8019 Explicit_Suppress (Scope (E), Accessibility_Check))
8020 and then
8021 (No (P_Formal)
8022 or else Present (Extra_Accessibility (P_Formal)))
8023 then
8024 Set_Extra_Accessibility
8025 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8026 end if;
8027
8028 -- This label is required when skipping extra formal generation for
8029 -- Unchecked_Union parameters.
8030
8031 <<Skip_Extra_Formal_Generation>>
8032
8033 if Present (P_Formal) then
8034 Next_Formal (P_Formal);
8035 end if;
8036
8037 Next_Formal (Formal);
8038 end loop;
8039
8040 <<Test_For_Func_Result_Extras>>
8041
8042 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8043 -- function call is ... determined by the point of call ...".
8044
8045 if Needs_Result_Accessibility_Level (E) then
8046 Set_Extra_Accessibility_Of_Result
8047 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8048 end if;
8049
8050 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8051 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8052
8053 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
8054 declare
8055 Result_Subt : constant Entity_Id := Etype (E);
8056 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
8057 Formal_Typ : Entity_Id;
8058
8059 Discard : Entity_Id;
8060 pragma Warnings (Off, Discard);
8061
8062 begin
8063 -- In the case of functions with unconstrained result subtypes,
8064 -- add a 4-state formal indicating whether the return object is
8065 -- allocated by the caller (1), or should be allocated by the
8066 -- callee on the secondary stack (2), in the global heap (3), or
8067 -- in a user-defined storage pool (4). For the moment we just use
8068 -- Natural for the type of this formal. Note that this formal
8069 -- isn't usually needed in the case where the result subtype is
8070 -- constrained, but it is needed when the function has a tagged
8071 -- result, because generally such functions can be called in a
8072 -- dispatching context and such calls must be handled like calls
8073 -- to a class-wide function.
8074
8075 if Needs_BIP_Alloc_Form (E) then
8076 Discard :=
8077 Add_Extra_Formal
8078 (E, Standard_Natural,
8079 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8080
8081 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8082 -- use a user-defined pool. This formal is not added on
8083 -- .NET/JVM/ZFP as those targets do not support pools.
8084
8085 if VM_Target = No_VM
8086 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
8087 then
8088 Discard :=
8089 Add_Extra_Formal
8090 (E, RTE (RE_Root_Storage_Pool_Ptr),
8091 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8092 end if;
8093 end if;
8094
8095 -- In the case of functions whose result type needs finalization,
8096 -- add an extra formal which represents the finalization master.
8097
8098 if Needs_BIP_Finalization_Master (E) then
8099 Discard :=
8100 Add_Extra_Formal
8101 (E, RTE (RE_Finalization_Master_Ptr),
8102 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8103 end if;
8104
8105 -- When the result type contains tasks, add two extra formals: the
8106 -- master of the tasks to be created, and the caller's activation
8107 -- chain.
8108
8109 if Has_Task (Full_Subt) then
8110 Discard :=
8111 Add_Extra_Formal
8112 (E, RTE (RE_Master_Id),
8113 E, BIP_Formal_Suffix (BIP_Task_Master));
8114 Discard :=
8115 Add_Extra_Formal
8116 (E, RTE (RE_Activation_Chain_Access),
8117 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8118 end if;
8119
8120 -- All build-in-place functions get an extra formal that will be
8121 -- passed the address of the return object within the caller.
8122
8123 Formal_Typ :=
8124 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8125
8126 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
8127 Set_Etype (Formal_Typ, Formal_Typ);
8128 Set_Depends_On_Private
8129 (Formal_Typ, Has_Private_Component (Formal_Typ));
8130 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8131 Set_Is_Access_Constant (Formal_Typ, False);
8132
8133 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8134 -- the designated type comes from the limited view (for back-end
8135 -- purposes).
8136
8137 Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt));
8138
8139 Layout_Type (Formal_Typ);
8140
8141 Discard :=
8142 Add_Extra_Formal
8143 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8144 end;
8145 end if;
8146 end Create_Extra_Formals;
8147
8148 -----------------------------
8149 -- Enter_Overloaded_Entity --
8150 -----------------------------
8151
8152 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8153 E : Entity_Id := Current_Entity_In_Scope (S);
8154 C_E : Entity_Id := Current_Entity (S);
8155
8156 begin
8157 if Present (E) then
8158 Set_Has_Homonym (E);
8159 Set_Has_Homonym (S);
8160 end if;
8161
8162 Set_Is_Immediately_Visible (S);
8163 Set_Scope (S, Current_Scope);
8164
8165 -- Chain new entity if front of homonym in current scope, so that
8166 -- homonyms are contiguous.
8167
8168 if Present (E) and then E /= C_E then
8169 while Homonym (C_E) /= E loop
8170 C_E := Homonym (C_E);
8171 end loop;
8172
8173 Set_Homonym (C_E, S);
8174
8175 else
8176 E := C_E;
8177 Set_Current_Entity (S);
8178 end if;
8179
8180 Set_Homonym (S, E);
8181
8182 if Is_Inherited_Operation (S) then
8183 Append_Inherited_Subprogram (S);
8184 else
8185 Append_Entity (S, Current_Scope);
8186 end if;
8187
8188 Set_Public_Status (S);
8189
8190 if Debug_Flag_E then
8191 Write_Str ("New overloaded entity chain: ");
8192 Write_Name (Chars (S));
8193
8194 E := S;
8195 while Present (E) loop
8196 Write_Str (" "); Write_Int (Int (E));
8197 E := Homonym (E);
8198 end loop;
8199
8200 Write_Eol;
8201 end if;
8202
8203 -- Generate warning for hiding
8204
8205 if Warn_On_Hiding
8206 and then Comes_From_Source (S)
8207 and then In_Extended_Main_Source_Unit (S)
8208 then
8209 E := S;
8210 loop
8211 E := Homonym (E);
8212 exit when No (E);
8213
8214 -- Warn unless genuine overloading. Do not emit warning on
8215 -- hiding predefined operators in Standard (these are either an
8216 -- (artifact of our implicit declarations, or simple noise) but
8217 -- keep warning on a operator defined on a local subtype, because
8218 -- of the real danger that different operators may be applied in
8219 -- various parts of the program.
8220
8221 -- Note that if E and S have the same scope, there is never any
8222 -- hiding. Either the two conflict, and the program is illegal,
8223 -- or S is overriding an implicit inherited subprogram.
8224
8225 if Scope (E) /= Scope (S)
8226 and then (not Is_Overloadable (E)
8227 or else Subtype_Conformant (E, S))
8228 and then (Is_Immediately_Visible (E)
8229 or else
8230 Is_Potentially_Use_Visible (S))
8231 then
8232 if Scope (E) /= Standard_Standard then
8233 Error_Msg_Sloc := Sloc (E);
8234 Error_Msg_N ("declaration of & hides one#?h?", S);
8235
8236 elsif Nkind (S) = N_Defining_Operator_Symbol
8237 and then
8238 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
8239 then
8240 Error_Msg_N
8241 ("declaration of & hides predefined operator?h?", S);
8242 end if;
8243 end if;
8244 end loop;
8245 end if;
8246 end Enter_Overloaded_Entity;
8247
8248 -----------------------------
8249 -- Check_Untagged_Equality --
8250 -----------------------------
8251
8252 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8253 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8254 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8255 Obj_Decl : Node_Id;
8256
8257 begin
8258 if Nkind (Decl) = N_Subprogram_Declaration
8259 and then Is_Record_Type (Typ)
8260 and then not Is_Tagged_Type (Typ)
8261 then
8262 -- If the type is not declared in a package, or if we are in the
8263 -- body of the package or in some other scope, the new operation is
8264 -- not primitive, and therefore legal, though suspicious. If the
8265 -- type is a generic actual (sub)type, the operation is not primitive
8266 -- either because the base type is declared elsewhere.
8267
8268 if Is_Frozen (Typ) then
8269 if Ekind (Scope (Typ)) /= E_Package
8270 or else Scope (Typ) /= Current_Scope
8271 then
8272 null;
8273
8274 elsif Is_Generic_Actual_Type (Typ) then
8275 null;
8276
8277 elsif In_Package_Body (Scope (Typ)) then
8278 Error_Msg_NE
8279 ("equality operator must be declared "
8280 & "before type& is frozen", Eq_Op, Typ);
8281 Error_Msg_N
8282 ("\move declaration to package spec", Eq_Op);
8283
8284 else
8285 Error_Msg_NE
8286 ("equality operator must be declared "
8287 & "before type& is frozen", Eq_Op, Typ);
8288
8289 Obj_Decl := Next (Parent (Typ));
8290 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8291 if Nkind (Obj_Decl) = N_Object_Declaration
8292 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8293 then
8294 Error_Msg_NE
8295 ("type& is frozen by declaration??", Obj_Decl, Typ);
8296 Error_Msg_N
8297 ("\an equality operator cannot be declared after this "
8298 & "point (RM 4.5.2 (9.8)) (Ada 2012))??", Obj_Decl);
8299 exit;
8300 end if;
8301
8302 Next (Obj_Decl);
8303 end loop;
8304 end if;
8305
8306 elsif not In_Same_List (Parent (Typ), Decl)
8307 and then not Is_Limited_Type (Typ)
8308 then
8309
8310 -- This makes it illegal to have a primitive equality declared in
8311 -- the private part if the type is visible.
8312
8313 Error_Msg_N ("equality operator appears too late", Eq_Op);
8314 end if;
8315 end if;
8316 end Check_Untagged_Equality;
8317
8318 -----------------------------
8319 -- Find_Corresponding_Spec --
8320 -----------------------------
8321
8322 function Find_Corresponding_Spec
8323 (N : Node_Id;
8324 Post_Error : Boolean := True) return Entity_Id
8325 is
8326 Spec : constant Node_Id := Specification (N);
8327 Designator : constant Entity_Id := Defining_Entity (Spec);
8328
8329 E : Entity_Id;
8330
8331 function Different_Generic_Profile (E : Entity_Id) return Boolean;
8332 -- Even if fully conformant, a body may depend on a generic actual when
8333 -- the spec does not, or vice versa, in which case they were distinct
8334 -- entities in the generic.
8335
8336 -------------------------------
8337 -- Different_Generic_Profile --
8338 -------------------------------
8339
8340 function Different_Generic_Profile (E : Entity_Id) return Boolean is
8341 F1, F2 : Entity_Id;
8342
8343 begin
8344 if Ekind (E) = E_Function
8345 and then Is_Generic_Actual_Type (Etype (E)) /=
8346 Is_Generic_Actual_Type (Etype (Designator))
8347 then
8348 return True;
8349 end if;
8350
8351 F1 := First_Formal (Designator);
8352 F2 := First_Formal (E);
8353 while Present (F1) loop
8354 if Is_Generic_Actual_Type (Etype (F1)) /=
8355 Is_Generic_Actual_Type (Etype (F2))
8356 then
8357 return True;
8358 end if;
8359
8360 Next_Formal (F1);
8361 Next_Formal (F2);
8362 end loop;
8363
8364 return False;
8365 end Different_Generic_Profile;
8366
8367 -- Start of processing for Find_Corresponding_Spec
8368
8369 begin
8370 E := Current_Entity (Designator);
8371 while Present (E) loop
8372
8373 -- We are looking for a matching spec. It must have the same scope,
8374 -- and the same name, and either be type conformant, or be the case
8375 -- of a library procedure spec and its body (which belong to one
8376 -- another regardless of whether they are type conformant or not).
8377
8378 if Scope (E) = Current_Scope then
8379 if Current_Scope = Standard_Standard
8380 or else (Ekind (E) = Ekind (Designator)
8381 and then Type_Conformant (E, Designator))
8382 then
8383 -- Within an instantiation, we know that spec and body are
8384 -- subtype conformant, because they were subtype conformant in
8385 -- the generic. We choose the subtype-conformant entity here as
8386 -- well, to resolve spurious ambiguities in the instance that
8387 -- were not present in the generic (i.e. when two different
8388 -- types are given the same actual). If we are looking for a
8389 -- spec to match a body, full conformance is expected.
8390
8391 if In_Instance then
8392 Set_Convention (Designator, Convention (E));
8393
8394 -- Skip past subprogram bodies and subprogram renamings that
8395 -- may appear to have a matching spec, but that aren't fully
8396 -- conformant with it. That can occur in cases where an
8397 -- actual type causes unrelated homographs in the instance.
8398
8399 if Nkind_In (N, N_Subprogram_Body,
8400 N_Subprogram_Renaming_Declaration)
8401 and then Present (Homonym (E))
8402 and then not Fully_Conformant (Designator, E)
8403 then
8404 goto Next_Entity;
8405
8406 elsif not Subtype_Conformant (Designator, E) then
8407 goto Next_Entity;
8408
8409 elsif Different_Generic_Profile (E) then
8410 goto Next_Entity;
8411 end if;
8412 end if;
8413
8414 -- Ada 2012 (AI05-0165): For internally generated bodies of
8415 -- null procedures locate the internally generated spec. We
8416 -- enforce mode conformance since a tagged type may inherit
8417 -- from interfaces several null primitives which differ only
8418 -- in the mode of the formals.
8419
8420 if not (Comes_From_Source (E))
8421 and then Is_Null_Procedure (E)
8422 and then not Mode_Conformant (Designator, E)
8423 then
8424 null;
8425
8426 -- For null procedures coming from source that are completions,
8427 -- analysis of the generated body will establish the link.
8428
8429 elsif Comes_From_Source (E)
8430 and then Nkind (Spec) = N_Procedure_Specification
8431 and then Null_Present (Spec)
8432 then
8433 return E;
8434
8435 elsif not Has_Completion (E) then
8436 if Nkind (N) /= N_Subprogram_Body_Stub then
8437 Set_Corresponding_Spec (N, E);
8438 end if;
8439
8440 Set_Has_Completion (E);
8441 return E;
8442
8443 elsif Nkind (Parent (N)) = N_Subunit then
8444
8445 -- If this is the proper body of a subunit, the completion
8446 -- flag is set when analyzing the stub.
8447
8448 return E;
8449
8450 -- If E is an internal function with a controlling result that
8451 -- was created for an operation inherited by a null extension,
8452 -- it may be overridden by a body without a previous spec (one
8453 -- more reason why these should be shunned). In that case
8454 -- remove the generated body if present, because the current
8455 -- one is the explicit overriding.
8456
8457 elsif Ekind (E) = E_Function
8458 and then Ada_Version >= Ada_2005
8459 and then not Comes_From_Source (E)
8460 and then Has_Controlling_Result (E)
8461 and then Is_Null_Extension (Etype (E))
8462 and then Comes_From_Source (Spec)
8463 then
8464 Set_Has_Completion (E, False);
8465
8466 if Expander_Active
8467 and then Nkind (Parent (E)) = N_Function_Specification
8468 then
8469 Remove
8470 (Unit_Declaration_Node
8471 (Corresponding_Body (Unit_Declaration_Node (E))));
8472
8473 return E;
8474
8475 -- If expansion is disabled, or if the wrapper function has
8476 -- not been generated yet, this a late body overriding an
8477 -- inherited operation, or it is an overriding by some other
8478 -- declaration before the controlling result is frozen. In
8479 -- either case this is a declaration of a new entity.
8480
8481 else
8482 return Empty;
8483 end if;
8484
8485 -- If the body already exists, then this is an error unless
8486 -- the previous declaration is the implicit declaration of a
8487 -- derived subprogram. It is also legal for an instance to
8488 -- contain type conformant overloadable declarations (but the
8489 -- generic declaration may not), per 8.3(26/2).
8490
8491 elsif No (Alias (E))
8492 and then not Is_Intrinsic_Subprogram (E)
8493 and then not In_Instance
8494 and then Post_Error
8495 then
8496 Error_Msg_Sloc := Sloc (E);
8497
8498 if Is_Imported (E) then
8499 Error_Msg_NE
8500 ("body not allowed for imported subprogram & declared#",
8501 N, E);
8502 else
8503 Error_Msg_NE ("duplicate body for & declared#", N, E);
8504 end if;
8505 end if;
8506
8507 -- Child units cannot be overloaded, so a conformance mismatch
8508 -- between body and a previous spec is an error.
8509
8510 elsif Is_Child_Unit (E)
8511 and then
8512 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8513 and then
8514 Nkind (Parent (Unit_Declaration_Node (Designator))) =
8515 N_Compilation_Unit
8516 and then Post_Error
8517 then
8518 Error_Msg_N
8519 ("body of child unit does not match previous declaration", N);
8520 end if;
8521 end if;
8522
8523 <<Next_Entity>>
8524 E := Homonym (E);
8525 end loop;
8526
8527 -- On exit, we know that no previous declaration of subprogram exists
8528
8529 return Empty;
8530 end Find_Corresponding_Spec;
8531
8532 ----------------------
8533 -- Fully_Conformant --
8534 ----------------------
8535
8536 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8537 Result : Boolean;
8538 begin
8539 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8540 return Result;
8541 end Fully_Conformant;
8542
8543 ----------------------------------
8544 -- Fully_Conformant_Expressions --
8545 ----------------------------------
8546
8547 function Fully_Conformant_Expressions
8548 (Given_E1 : Node_Id;
8549 Given_E2 : Node_Id) return Boolean
8550 is
8551 E1 : constant Node_Id := Original_Node (Given_E1);
8552 E2 : constant Node_Id := Original_Node (Given_E2);
8553 -- We always test conformance on original nodes, since it is possible
8554 -- for analysis and/or expansion to make things look as though they
8555 -- conform when they do not, e.g. by converting 1+2 into 3.
8556
8557 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8558 renames Fully_Conformant_Expressions;
8559
8560 function FCL (L1, L2 : List_Id) return Boolean;
8561 -- Compare elements of two lists for conformance. Elements have to be
8562 -- conformant, and actuals inserted as default parameters do not match
8563 -- explicit actuals with the same value.
8564
8565 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
8566 -- Compare an operator node with a function call
8567
8568 ---------
8569 -- FCL --
8570 ---------
8571
8572 function FCL (L1, L2 : List_Id) return Boolean is
8573 N1, N2 : Node_Id;
8574
8575 begin
8576 if L1 = No_List then
8577 N1 := Empty;
8578 else
8579 N1 := First (L1);
8580 end if;
8581
8582 if L2 = No_List then
8583 N2 := Empty;
8584 else
8585 N2 := First (L2);
8586 end if;
8587
8588 -- Compare two lists, skipping rewrite insertions (we want to compare
8589 -- the original trees, not the expanded versions!)
8590
8591 loop
8592 if Is_Rewrite_Insertion (N1) then
8593 Next (N1);
8594 elsif Is_Rewrite_Insertion (N2) then
8595 Next (N2);
8596 elsif No (N1) then
8597 return No (N2);
8598 elsif No (N2) then
8599 return False;
8600 elsif not FCE (N1, N2) then
8601 return False;
8602 else
8603 Next (N1);
8604 Next (N2);
8605 end if;
8606 end loop;
8607 end FCL;
8608
8609 ---------
8610 -- FCO --
8611 ---------
8612
8613 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8614 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8615 Act : Node_Id;
8616
8617 begin
8618 if No (Actuals)
8619 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8620 then
8621 return False;
8622
8623 else
8624 Act := First (Actuals);
8625
8626 if Nkind (Op_Node) in N_Binary_Op then
8627 if not FCE (Left_Opnd (Op_Node), Act) then
8628 return False;
8629 end if;
8630
8631 Next (Act);
8632 end if;
8633
8634 return Present (Act)
8635 and then FCE (Right_Opnd (Op_Node), Act)
8636 and then No (Next (Act));
8637 end if;
8638 end FCO;
8639
8640 -- Start of processing for Fully_Conformant_Expressions
8641
8642 begin
8643 -- Non-conformant if paren count does not match. Note: if some idiot
8644 -- complains that we don't do this right for more than 3 levels of
8645 -- parentheses, they will be treated with the respect they deserve!
8646
8647 if Paren_Count (E1) /= Paren_Count (E2) then
8648 return False;
8649
8650 -- If same entities are referenced, then they are conformant even if
8651 -- they have different forms (RM 8.3.1(19-20)).
8652
8653 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8654 if Present (Entity (E1)) then
8655 return Entity (E1) = Entity (E2)
8656 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8657 and then Ekind (Entity (E1)) = E_Discriminant
8658 and then Ekind (Entity (E2)) = E_In_Parameter);
8659
8660 elsif Nkind (E1) = N_Expanded_Name
8661 and then Nkind (E2) = N_Expanded_Name
8662 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8663 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8664 then
8665 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8666
8667 else
8668 -- Identifiers in component associations don't always have
8669 -- entities, but their names must conform.
8670
8671 return Nkind (E1) = N_Identifier
8672 and then Nkind (E2) = N_Identifier
8673 and then Chars (E1) = Chars (E2);
8674 end if;
8675
8676 elsif Nkind (E1) = N_Character_Literal
8677 and then Nkind (E2) = N_Expanded_Name
8678 then
8679 return Nkind (Selector_Name (E2)) = N_Character_Literal
8680 and then Chars (E1) = Chars (Selector_Name (E2));
8681
8682 elsif Nkind (E2) = N_Character_Literal
8683 and then Nkind (E1) = N_Expanded_Name
8684 then
8685 return Nkind (Selector_Name (E1)) = N_Character_Literal
8686 and then Chars (E2) = Chars (Selector_Name (E1));
8687
8688 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
8689 return FCO (E1, E2);
8690
8691 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
8692 return FCO (E2, E1);
8693
8694 -- Otherwise we must have the same syntactic entity
8695
8696 elsif Nkind (E1) /= Nkind (E2) then
8697 return False;
8698
8699 -- At this point, we specialize by node type
8700
8701 else
8702 case Nkind (E1) is
8703
8704 when N_Aggregate =>
8705 return
8706 FCL (Expressions (E1), Expressions (E2))
8707 and then
8708 FCL (Component_Associations (E1),
8709 Component_Associations (E2));
8710
8711 when N_Allocator =>
8712 if Nkind (Expression (E1)) = N_Qualified_Expression
8713 or else
8714 Nkind (Expression (E2)) = N_Qualified_Expression
8715 then
8716 return FCE (Expression (E1), Expression (E2));
8717
8718 -- Check that the subtype marks and any constraints
8719 -- are conformant
8720
8721 else
8722 declare
8723 Indic1 : constant Node_Id := Expression (E1);
8724 Indic2 : constant Node_Id := Expression (E2);
8725 Elt1 : Node_Id;
8726 Elt2 : Node_Id;
8727
8728 begin
8729 if Nkind (Indic1) /= N_Subtype_Indication then
8730 return
8731 Nkind (Indic2) /= N_Subtype_Indication
8732 and then Entity (Indic1) = Entity (Indic2);
8733
8734 elsif Nkind (Indic2) /= N_Subtype_Indication then
8735 return
8736 Nkind (Indic1) /= N_Subtype_Indication
8737 and then Entity (Indic1) = Entity (Indic2);
8738
8739 else
8740 if Entity (Subtype_Mark (Indic1)) /=
8741 Entity (Subtype_Mark (Indic2))
8742 then
8743 return False;
8744 end if;
8745
8746 Elt1 := First (Constraints (Constraint (Indic1)));
8747 Elt2 := First (Constraints (Constraint (Indic2)));
8748 while Present (Elt1) and then Present (Elt2) loop
8749 if not FCE (Elt1, Elt2) then
8750 return False;
8751 end if;
8752
8753 Next (Elt1);
8754 Next (Elt2);
8755 end loop;
8756
8757 return True;
8758 end if;
8759 end;
8760 end if;
8761
8762 when N_Attribute_Reference =>
8763 return
8764 Attribute_Name (E1) = Attribute_Name (E2)
8765 and then FCL (Expressions (E1), Expressions (E2));
8766
8767 when N_Binary_Op =>
8768 return
8769 Entity (E1) = Entity (E2)
8770 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8771 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8772
8773 when N_Short_Circuit | N_Membership_Test =>
8774 return
8775 FCE (Left_Opnd (E1), Left_Opnd (E2))
8776 and then
8777 FCE (Right_Opnd (E1), Right_Opnd (E2));
8778
8779 when N_Case_Expression =>
8780 declare
8781 Alt1 : Node_Id;
8782 Alt2 : Node_Id;
8783
8784 begin
8785 if not FCE (Expression (E1), Expression (E2)) then
8786 return False;
8787
8788 else
8789 Alt1 := First (Alternatives (E1));
8790 Alt2 := First (Alternatives (E2));
8791 loop
8792 if Present (Alt1) /= Present (Alt2) then
8793 return False;
8794 elsif No (Alt1) then
8795 return True;
8796 end if;
8797
8798 if not FCE (Expression (Alt1), Expression (Alt2))
8799 or else not FCL (Discrete_Choices (Alt1),
8800 Discrete_Choices (Alt2))
8801 then
8802 return False;
8803 end if;
8804
8805 Next (Alt1);
8806 Next (Alt2);
8807 end loop;
8808 end if;
8809 end;
8810
8811 when N_Character_Literal =>
8812 return
8813 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8814
8815 when N_Component_Association =>
8816 return
8817 FCL (Choices (E1), Choices (E2))
8818 and then
8819 FCE (Expression (E1), Expression (E2));
8820
8821 when N_Explicit_Dereference =>
8822 return
8823 FCE (Prefix (E1), Prefix (E2));
8824
8825 when N_Extension_Aggregate =>
8826 return
8827 FCL (Expressions (E1), Expressions (E2))
8828 and then Null_Record_Present (E1) =
8829 Null_Record_Present (E2)
8830 and then FCL (Component_Associations (E1),
8831 Component_Associations (E2));
8832
8833 when N_Function_Call =>
8834 return
8835 FCE (Name (E1), Name (E2))
8836 and then
8837 FCL (Parameter_Associations (E1),
8838 Parameter_Associations (E2));
8839
8840 when N_If_Expression =>
8841 return
8842 FCL (Expressions (E1), Expressions (E2));
8843
8844 when N_Indexed_Component =>
8845 return
8846 FCE (Prefix (E1), Prefix (E2))
8847 and then
8848 FCL (Expressions (E1), Expressions (E2));
8849
8850 when N_Integer_Literal =>
8851 return (Intval (E1) = Intval (E2));
8852
8853 when N_Null =>
8854 return True;
8855
8856 when N_Operator_Symbol =>
8857 return
8858 Chars (E1) = Chars (E2);
8859
8860 when N_Others_Choice =>
8861 return True;
8862
8863 when N_Parameter_Association =>
8864 return
8865 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8866 and then FCE (Explicit_Actual_Parameter (E1),
8867 Explicit_Actual_Parameter (E2));
8868
8869 when N_Qualified_Expression =>
8870 return
8871 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8872 and then
8873 FCE (Expression (E1), Expression (E2));
8874
8875 when N_Quantified_Expression =>
8876 if not FCE (Condition (E1), Condition (E2)) then
8877 return False;
8878 end if;
8879
8880 if Present (Loop_Parameter_Specification (E1))
8881 and then Present (Loop_Parameter_Specification (E2))
8882 then
8883 declare
8884 L1 : constant Node_Id :=
8885 Loop_Parameter_Specification (E1);
8886 L2 : constant Node_Id :=
8887 Loop_Parameter_Specification (E2);
8888
8889 begin
8890 return
8891 Reverse_Present (L1) = Reverse_Present (L2)
8892 and then
8893 FCE (Defining_Identifier (L1),
8894 Defining_Identifier (L2))
8895 and then
8896 FCE (Discrete_Subtype_Definition (L1),
8897 Discrete_Subtype_Definition (L2));
8898 end;
8899
8900 elsif Present (Iterator_Specification (E1))
8901 and then Present (Iterator_Specification (E2))
8902 then
8903 declare
8904 I1 : constant Node_Id := Iterator_Specification (E1);
8905 I2 : constant Node_Id := Iterator_Specification (E2);
8906
8907 begin
8908 return
8909 FCE (Defining_Identifier (I1),
8910 Defining_Identifier (I2))
8911 and then
8912 Of_Present (I1) = Of_Present (I2)
8913 and then
8914 Reverse_Present (I1) = Reverse_Present (I2)
8915 and then FCE (Name (I1), Name (I2))
8916 and then FCE (Subtype_Indication (I1),
8917 Subtype_Indication (I2));
8918 end;
8919
8920 -- The quantified expressions used different specifications to
8921 -- walk their respective ranges.
8922
8923 else
8924 return False;
8925 end if;
8926
8927 when N_Range =>
8928 return
8929 FCE (Low_Bound (E1), Low_Bound (E2))
8930 and then
8931 FCE (High_Bound (E1), High_Bound (E2));
8932
8933 when N_Real_Literal =>
8934 return (Realval (E1) = Realval (E2));
8935
8936 when N_Selected_Component =>
8937 return
8938 FCE (Prefix (E1), Prefix (E2))
8939 and then
8940 FCE (Selector_Name (E1), Selector_Name (E2));
8941
8942 when N_Slice =>
8943 return
8944 FCE (Prefix (E1), Prefix (E2))
8945 and then
8946 FCE (Discrete_Range (E1), Discrete_Range (E2));
8947
8948 when N_String_Literal =>
8949 declare
8950 S1 : constant String_Id := Strval (E1);
8951 S2 : constant String_Id := Strval (E2);
8952 L1 : constant Nat := String_Length (S1);
8953 L2 : constant Nat := String_Length (S2);
8954
8955 begin
8956 if L1 /= L2 then
8957 return False;
8958
8959 else
8960 for J in 1 .. L1 loop
8961 if Get_String_Char (S1, J) /=
8962 Get_String_Char (S2, J)
8963 then
8964 return False;
8965 end if;
8966 end loop;
8967
8968 return True;
8969 end if;
8970 end;
8971
8972 when N_Type_Conversion =>
8973 return
8974 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8975 and then
8976 FCE (Expression (E1), Expression (E2));
8977
8978 when N_Unary_Op =>
8979 return
8980 Entity (E1) = Entity (E2)
8981 and then
8982 FCE (Right_Opnd (E1), Right_Opnd (E2));
8983
8984 when N_Unchecked_Type_Conversion =>
8985 return
8986 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
8987 and then
8988 FCE (Expression (E1), Expression (E2));
8989
8990 -- All other node types cannot appear in this context. Strictly
8991 -- we should raise a fatal internal error. Instead we just ignore
8992 -- the nodes. This means that if anyone makes a mistake in the
8993 -- expander and mucks an expression tree irretrievably, the
8994 -- result will be a failure to detect a (probably very obscure)
8995 -- case of non-conformance, which is better than bombing on some
8996 -- case where two expressions do in fact conform.
8997
8998 when others =>
8999 return True;
9000
9001 end case;
9002 end if;
9003 end Fully_Conformant_Expressions;
9004
9005 ----------------------------------------
9006 -- Fully_Conformant_Discrete_Subtypes --
9007 ----------------------------------------
9008
9009 function Fully_Conformant_Discrete_Subtypes
9010 (Given_S1 : Node_Id;
9011 Given_S2 : Node_Id) return Boolean
9012 is
9013 S1 : constant Node_Id := Original_Node (Given_S1);
9014 S2 : constant Node_Id := Original_Node (Given_S2);
9015
9016 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9017 -- Special-case for a bound given by a discriminant, which in the body
9018 -- is replaced with the discriminal of the enclosing type.
9019
9020 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9021 -- Check both bounds
9022
9023 -----------------------
9024 -- Conforming_Bounds --
9025 -----------------------
9026
9027 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9028 begin
9029 if Is_Entity_Name (B1)
9030 and then Is_Entity_Name (B2)
9031 and then Ekind (Entity (B1)) = E_Discriminant
9032 then
9033 return Chars (B1) = Chars (B2);
9034
9035 else
9036 return Fully_Conformant_Expressions (B1, B2);
9037 end if;
9038 end Conforming_Bounds;
9039
9040 -----------------------
9041 -- Conforming_Ranges --
9042 -----------------------
9043
9044 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9045 begin
9046 return
9047 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9048 and then
9049 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9050 end Conforming_Ranges;
9051
9052 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9053
9054 begin
9055 if Nkind (S1) /= Nkind (S2) then
9056 return False;
9057
9058 elsif Is_Entity_Name (S1) then
9059 return Entity (S1) = Entity (S2);
9060
9061 elsif Nkind (S1) = N_Range then
9062 return Conforming_Ranges (S1, S2);
9063
9064 elsif Nkind (S1) = N_Subtype_Indication then
9065 return
9066 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9067 and then
9068 Conforming_Ranges
9069 (Range_Expression (Constraint (S1)),
9070 Range_Expression (Constraint (S2)));
9071 else
9072 return True;
9073 end if;
9074 end Fully_Conformant_Discrete_Subtypes;
9075
9076 --------------------
9077 -- Install_Entity --
9078 --------------------
9079
9080 procedure Install_Entity (E : Entity_Id) is
9081 Prev : constant Entity_Id := Current_Entity (E);
9082 begin
9083 Set_Is_Immediately_Visible (E);
9084 Set_Current_Entity (E);
9085 Set_Homonym (E, Prev);
9086 end Install_Entity;
9087
9088 ---------------------
9089 -- Install_Formals --
9090 ---------------------
9091
9092 procedure Install_Formals (Id : Entity_Id) is
9093 F : Entity_Id;
9094 begin
9095 F := First_Formal (Id);
9096 while Present (F) loop
9097 Install_Entity (F);
9098 Next_Formal (F);
9099 end loop;
9100 end Install_Formals;
9101
9102 -----------------------------
9103 -- Is_Interface_Conformant --
9104 -----------------------------
9105
9106 function Is_Interface_Conformant
9107 (Tagged_Type : Entity_Id;
9108 Iface_Prim : Entity_Id;
9109 Prim : Entity_Id) return Boolean
9110 is
9111 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
9112 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9113
9114 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9115 -- Return the controlling formal of Prim
9116
9117 ------------------------
9118 -- Controlling_Formal --
9119 ------------------------
9120
9121 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9122 E : Entity_Id := First_Entity (Prim);
9123
9124 begin
9125 while Present (E) loop
9126 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9127 return E;
9128 end if;
9129
9130 Next_Entity (E);
9131 end loop;
9132
9133 return Empty;
9134 end Controlling_Formal;
9135
9136 -- Local variables
9137
9138 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9139 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9140
9141 -- Start of processing for Is_Interface_Conformant
9142
9143 begin
9144 pragma Assert (Is_Subprogram (Iface_Prim)
9145 and then Is_Subprogram (Prim)
9146 and then Is_Dispatching_Operation (Iface_Prim)
9147 and then Is_Dispatching_Operation (Prim));
9148
9149 pragma Assert (Is_Interface (Iface)
9150 or else (Present (Alias (Iface_Prim))
9151 and then
9152 Is_Interface
9153 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9154
9155 if Prim = Iface_Prim
9156 or else not Is_Subprogram (Prim)
9157 or else Ekind (Prim) /= Ekind (Iface_Prim)
9158 or else not Is_Dispatching_Operation (Prim)
9159 or else Scope (Prim) /= Scope (Tagged_Type)
9160 or else No (Typ)
9161 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9162 or else not Primitive_Names_Match (Iface_Prim, Prim)
9163 then
9164 return False;
9165
9166 -- The mode of the controlling formals must match
9167
9168 elsif Present (Iface_Ctrl_F)
9169 and then Present (Prim_Ctrl_F)
9170 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9171 then
9172 return False;
9173
9174 -- Case of a procedure, or a function whose result type matches the
9175 -- result type of the interface primitive, or a function that has no
9176 -- controlling result (I or access I).
9177
9178 elsif Ekind (Iface_Prim) = E_Procedure
9179 or else Etype (Prim) = Etype (Iface_Prim)
9180 or else not Has_Controlling_Result (Prim)
9181 then
9182 return Type_Conformant
9183 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9184
9185 -- Case of a function returning an interface, or an access to one.
9186 -- Check that the return types correspond.
9187
9188 elsif Implements_Interface (Typ, Iface) then
9189 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9190 /=
9191 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
9192 then
9193 return False;
9194 else
9195 return
9196 Type_Conformant (Prim, Iface_Prim,
9197 Skip_Controlling_Formals => True);
9198 end if;
9199
9200 else
9201 return False;
9202 end if;
9203 end Is_Interface_Conformant;
9204
9205 ---------------------------------
9206 -- Is_Non_Overriding_Operation --
9207 ---------------------------------
9208
9209 function Is_Non_Overriding_Operation
9210 (Prev_E : Entity_Id;
9211 New_E : Entity_Id) return Boolean
9212 is
9213 Formal : Entity_Id;
9214 F_Typ : Entity_Id;
9215 G_Typ : Entity_Id := Empty;
9216
9217 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
9218 -- If F_Type is a derived type associated with a generic actual subtype,
9219 -- then return its Generic_Parent_Type attribute, else return Empty.
9220
9221 function Types_Correspond
9222 (P_Type : Entity_Id;
9223 N_Type : Entity_Id) return Boolean;
9224 -- Returns true if and only if the types (or designated types in the
9225 -- case of anonymous access types) are the same or N_Type is derived
9226 -- directly or indirectly from P_Type.
9227
9228 -----------------------------
9229 -- Get_Generic_Parent_Type --
9230 -----------------------------
9231
9232 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9233 G_Typ : Entity_Id;
9234 Defn : Node_Id;
9235 Indic : Node_Id;
9236
9237 begin
9238 if Is_Derived_Type (F_Typ)
9239 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9240 then
9241 -- The tree must be traversed to determine the parent subtype in
9242 -- the generic unit, which unfortunately isn't always available
9243 -- via semantic attributes. ??? (Note: The use of Original_Node
9244 -- is needed for cases where a full derived type has been
9245 -- rewritten.)
9246
9247 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9248 if Nkind (Defn) = N_Derived_Type_Definition then
9249 Indic := Subtype_Indication (Defn);
9250
9251 if Nkind (Indic) = N_Subtype_Indication then
9252 G_Typ := Entity (Subtype_Mark (Indic));
9253 else
9254 G_Typ := Entity (Indic);
9255 end if;
9256
9257 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9258 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9259 then
9260 return Generic_Parent_Type (Parent (G_Typ));
9261 end if;
9262 end if;
9263 end if;
9264
9265 return Empty;
9266 end Get_Generic_Parent_Type;
9267
9268 ----------------------
9269 -- Types_Correspond --
9270 ----------------------
9271
9272 function Types_Correspond
9273 (P_Type : Entity_Id;
9274 N_Type : Entity_Id) return Boolean
9275 is
9276 Prev_Type : Entity_Id := Base_Type (P_Type);
9277 New_Type : Entity_Id := Base_Type (N_Type);
9278
9279 begin
9280 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9281 Prev_Type := Designated_Type (Prev_Type);
9282 end if;
9283
9284 if Ekind (New_Type) = E_Anonymous_Access_Type then
9285 New_Type := Designated_Type (New_Type);
9286 end if;
9287
9288 if Prev_Type = New_Type then
9289 return True;
9290
9291 elsif not Is_Class_Wide_Type (New_Type) then
9292 while Etype (New_Type) /= New_Type loop
9293 New_Type := Etype (New_Type);
9294 if New_Type = Prev_Type then
9295 return True;
9296 end if;
9297 end loop;
9298 end if;
9299 return False;
9300 end Types_Correspond;
9301
9302 -- Start of processing for Is_Non_Overriding_Operation
9303
9304 begin
9305 -- In the case where both operations are implicit derived subprograms
9306 -- then neither overrides the other. This can only occur in certain
9307 -- obscure cases (e.g., derivation from homographs created in a generic
9308 -- instantiation).
9309
9310 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9311 return True;
9312
9313 elsif Ekind (Current_Scope) = E_Package
9314 and then Is_Generic_Instance (Current_Scope)
9315 and then In_Private_Part (Current_Scope)
9316 and then Comes_From_Source (New_E)
9317 then
9318 -- We examine the formals and result type of the inherited operation,
9319 -- to determine whether their type is derived from (the instance of)
9320 -- a generic type. The first such formal or result type is the one
9321 -- tested.
9322
9323 Formal := First_Formal (Prev_E);
9324 while Present (Formal) loop
9325 F_Typ := Base_Type (Etype (Formal));
9326
9327 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9328 F_Typ := Designated_Type (F_Typ);
9329 end if;
9330
9331 G_Typ := Get_Generic_Parent_Type (F_Typ);
9332 exit when Present (G_Typ);
9333
9334 Next_Formal (Formal);
9335 end loop;
9336
9337 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
9338 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9339 end if;
9340
9341 if No (G_Typ) then
9342 return False;
9343 end if;
9344
9345 -- If the generic type is a private type, then the original operation
9346 -- was not overriding in the generic, because there was no primitive
9347 -- operation to override.
9348
9349 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9350 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
9351 N_Formal_Private_Type_Definition
9352 then
9353 return True;
9354
9355 -- The generic parent type is the ancestor of a formal derived
9356 -- type declaration. We need to check whether it has a primitive
9357 -- operation that should be overridden by New_E in the generic.
9358
9359 else
9360 declare
9361 P_Formal : Entity_Id;
9362 N_Formal : Entity_Id;
9363 P_Typ : Entity_Id;
9364 N_Typ : Entity_Id;
9365 P_Prim : Entity_Id;
9366 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9367
9368 begin
9369 while Present (Prim_Elt) loop
9370 P_Prim := Node (Prim_Elt);
9371
9372 if Chars (P_Prim) = Chars (New_E)
9373 and then Ekind (P_Prim) = Ekind (New_E)
9374 then
9375 P_Formal := First_Formal (P_Prim);
9376 N_Formal := First_Formal (New_E);
9377 while Present (P_Formal) and then Present (N_Formal) loop
9378 P_Typ := Etype (P_Formal);
9379 N_Typ := Etype (N_Formal);
9380
9381 if not Types_Correspond (P_Typ, N_Typ) then
9382 exit;
9383 end if;
9384
9385 Next_Entity (P_Formal);
9386 Next_Entity (N_Formal);
9387 end loop;
9388
9389 -- Found a matching primitive operation belonging to the
9390 -- formal ancestor type, so the new subprogram is
9391 -- overriding.
9392
9393 if No (P_Formal)
9394 and then No (N_Formal)
9395 and then (Ekind (New_E) /= E_Function
9396 or else
9397 Types_Correspond
9398 (Etype (P_Prim), Etype (New_E)))
9399 then
9400 return False;
9401 end if;
9402 end if;
9403
9404 Next_Elmt (Prim_Elt);
9405 end loop;
9406
9407 -- If no match found, then the new subprogram does not
9408 -- override in the generic (nor in the instance).
9409
9410 -- If the type in question is not abstract, and the subprogram
9411 -- is, this will be an error if the new operation is in the
9412 -- private part of the instance. Emit a warning now, which will
9413 -- make the subsequent error message easier to understand.
9414
9415 if not Is_Abstract_Type (F_Typ)
9416 and then Is_Abstract_Subprogram (Prev_E)
9417 and then In_Private_Part (Current_Scope)
9418 then
9419 Error_Msg_Node_2 := F_Typ;
9420 Error_Msg_NE
9421 ("private operation& in generic unit does not override " &
9422 "any primitive operation of& (RM 12.3 (18))??",
9423 New_E, New_E);
9424 end if;
9425
9426 return True;
9427 end;
9428 end if;
9429 else
9430 return False;
9431 end if;
9432 end Is_Non_Overriding_Operation;
9433
9434 -------------------------------------
9435 -- List_Inherited_Pre_Post_Aspects --
9436 -------------------------------------
9437
9438 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9439 begin
9440 if Opt.List_Inherited_Aspects
9441 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9442 then
9443 declare
9444 Inherited : constant Subprogram_List := Inherited_Subprograms (E);
9445 P : Node_Id;
9446
9447 begin
9448 for J in Inherited'Range loop
9449 P := Spec_PPC_List (Contract (Inherited (J)));
9450 while Present (P) loop
9451 Error_Msg_Sloc := Sloc (P);
9452
9453 if Class_Present (P) and then not Split_PPC (P) then
9454 if Pragma_Name (P) = Name_Precondition then
9455 Error_Msg_N
9456 ("info: & inherits `Pre''Class` aspect from #?L?",
9457 E);
9458 else
9459 Error_Msg_N
9460 ("info: & inherits `Post''Class` aspect from #?L?",
9461 E);
9462 end if;
9463 end if;
9464
9465 P := Next_Pragma (P);
9466 end loop;
9467 end loop;
9468 end;
9469 end if;
9470 end List_Inherited_Pre_Post_Aspects;
9471
9472 ------------------------------
9473 -- Make_Inequality_Operator --
9474 ------------------------------
9475
9476 -- S is the defining identifier of an equality operator. We build a
9477 -- subprogram declaration with the right signature. This operation is
9478 -- intrinsic, because it is always expanded as the negation of the
9479 -- call to the equality function.
9480
9481 procedure Make_Inequality_Operator (S : Entity_Id) is
9482 Loc : constant Source_Ptr := Sloc (S);
9483 Decl : Node_Id;
9484 Formals : List_Id;
9485 Op_Name : Entity_Id;
9486
9487 FF : constant Entity_Id := First_Formal (S);
9488 NF : constant Entity_Id := Next_Formal (FF);
9489
9490 begin
9491 -- Check that equality was properly defined, ignore call if not
9492
9493 if No (NF) then
9494 return;
9495 end if;
9496
9497 declare
9498 A : constant Entity_Id :=
9499 Make_Defining_Identifier (Sloc (FF),
9500 Chars => Chars (FF));
9501
9502 B : constant Entity_Id :=
9503 Make_Defining_Identifier (Sloc (NF),
9504 Chars => Chars (NF));
9505
9506 begin
9507 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9508
9509 Formals := New_List (
9510 Make_Parameter_Specification (Loc,
9511 Defining_Identifier => A,
9512 Parameter_Type =>
9513 New_Reference_To (Etype (First_Formal (S)),
9514 Sloc (Etype (First_Formal (S))))),
9515
9516 Make_Parameter_Specification (Loc,
9517 Defining_Identifier => B,
9518 Parameter_Type =>
9519 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9520 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9521
9522 Decl :=
9523 Make_Subprogram_Declaration (Loc,
9524 Specification =>
9525 Make_Function_Specification (Loc,
9526 Defining_Unit_Name => Op_Name,
9527 Parameter_Specifications => Formals,
9528 Result_Definition =>
9529 New_Reference_To (Standard_Boolean, Loc)));
9530
9531 -- Insert inequality right after equality if it is explicit or after
9532 -- the derived type when implicit. These entities are created only
9533 -- for visibility purposes, and eventually replaced in the course of
9534 -- expansion, so they do not need to be attached to the tree and seen
9535 -- by the back-end. Keeping them internal also avoids spurious
9536 -- freezing problems. The declaration is inserted in the tree for
9537 -- analysis, and removed afterwards. If the equality operator comes
9538 -- from an explicit declaration, attach the inequality immediately
9539 -- after. Else the equality is inherited from a derived type
9540 -- declaration, so insert inequality after that declaration.
9541
9542 if No (Alias (S)) then
9543 Insert_After (Unit_Declaration_Node (S), Decl);
9544 elsif Is_List_Member (Parent (S)) then
9545 Insert_After (Parent (S), Decl);
9546 else
9547 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9548 end if;
9549
9550 Mark_Rewrite_Insertion (Decl);
9551 Set_Is_Intrinsic_Subprogram (Op_Name);
9552 Analyze (Decl);
9553 Remove (Decl);
9554 Set_Has_Completion (Op_Name);
9555 Set_Corresponding_Equality (Op_Name, S);
9556 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
9557 end;
9558 end Make_Inequality_Operator;
9559
9560 ----------------------
9561 -- May_Need_Actuals --
9562 ----------------------
9563
9564 procedure May_Need_Actuals (Fun : Entity_Id) is
9565 F : Entity_Id;
9566 B : Boolean;
9567
9568 begin
9569 F := First_Formal (Fun);
9570 B := True;
9571 while Present (F) loop
9572 if No (Default_Value (F)) then
9573 B := False;
9574 exit;
9575 end if;
9576
9577 Next_Formal (F);
9578 end loop;
9579
9580 Set_Needs_No_Actuals (Fun, B);
9581 end May_Need_Actuals;
9582
9583 ---------------------
9584 -- Mode_Conformant --
9585 ---------------------
9586
9587 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9588 Result : Boolean;
9589 begin
9590 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9591 return Result;
9592 end Mode_Conformant;
9593
9594 ---------------------------
9595 -- New_Overloaded_Entity --
9596 ---------------------------
9597
9598 procedure New_Overloaded_Entity
9599 (S : Entity_Id;
9600 Derived_Type : Entity_Id := Empty)
9601 is
9602 Overridden_Subp : Entity_Id := Empty;
9603 -- Set if the current scope has an operation that is type-conformant
9604 -- with S, and becomes hidden by S.
9605
9606 Is_Primitive_Subp : Boolean;
9607 -- Set to True if the new subprogram is primitive
9608
9609 E : Entity_Id;
9610 -- Entity that S overrides
9611
9612 Prev_Vis : Entity_Id := Empty;
9613 -- Predecessor of E in Homonym chain
9614
9615 procedure Check_For_Primitive_Subprogram
9616 (Is_Primitive : out Boolean;
9617 Is_Overriding : Boolean := False);
9618 -- If the subprogram being analyzed is a primitive operation of the type
9619 -- of a formal or result, set the Has_Primitive_Operations flag on the
9620 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9621 -- corresponding flag on the entity itself for later use.
9622
9623 procedure Check_Synchronized_Overriding
9624 (Def_Id : Entity_Id;
9625 Overridden_Subp : out Entity_Id);
9626 -- First determine if Def_Id is an entry or a subprogram either defined
9627 -- in the scope of a task or protected type, or is a primitive of such
9628 -- a type. Check whether Def_Id overrides a subprogram of an interface
9629 -- implemented by the synchronized type, return the overridden entity
9630 -- or Empty.
9631
9632 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9633 -- Check that E is declared in the private part of the current package,
9634 -- or in the package body, where it may hide a previous declaration.
9635 -- We can't use In_Private_Part by itself because this flag is also
9636 -- set when freezing entities, so we must examine the place of the
9637 -- declaration in the tree, and recognize wrapper packages as well.
9638
9639 function Is_Overriding_Alias
9640 (Old_E : Entity_Id;
9641 New_E : Entity_Id) return Boolean;
9642 -- Check whether new subprogram and old subprogram are both inherited
9643 -- from subprograms that have distinct dispatch table entries. This can
9644 -- occur with derivations from instances with accidental homonyms.
9645 -- The function is conservative given that the converse is only true
9646 -- within instances that contain accidental overloadings.
9647
9648 ------------------------------------
9649 -- Check_For_Primitive_Subprogram --
9650 ------------------------------------
9651
9652 procedure Check_For_Primitive_Subprogram
9653 (Is_Primitive : out Boolean;
9654 Is_Overriding : Boolean := False)
9655 is
9656 Formal : Entity_Id;
9657 F_Typ : Entity_Id;
9658 B_Typ : Entity_Id;
9659
9660 function Visible_Part_Type (T : Entity_Id) return Boolean;
9661 -- Returns true if T is declared in the visible part of the current
9662 -- package scope; otherwise returns false. Assumes that T is declared
9663 -- in a package.
9664
9665 procedure Check_Private_Overriding (T : Entity_Id);
9666 -- Checks that if a primitive abstract subprogram of a visible
9667 -- abstract type is declared in a private part, then it must override
9668 -- an abstract subprogram declared in the visible part. Also checks
9669 -- that if a primitive function with a controlling result is declared
9670 -- in a private part, then it must override a function declared in
9671 -- the visible part.
9672
9673 ------------------------------
9674 -- Check_Private_Overriding --
9675 ------------------------------
9676
9677 procedure Check_Private_Overriding (T : Entity_Id) is
9678 begin
9679 if Is_Package_Or_Generic_Package (Current_Scope)
9680 and then In_Private_Part (Current_Scope)
9681 and then Visible_Part_Type (T)
9682 and then not In_Instance
9683 then
9684 if Is_Abstract_Type (T)
9685 and then Is_Abstract_Subprogram (S)
9686 and then (not Is_Overriding
9687 or else not Is_Abstract_Subprogram (E))
9688 then
9689 Error_Msg_N
9690 ("abstract subprograms must be visible "
9691 & "(RM 3.9.3(10))!", S);
9692
9693 elsif Ekind (S) = E_Function and then not Is_Overriding then
9694 if Is_Tagged_Type (T) and then T = Base_Type (Etype (S)) then
9695 Error_Msg_N
9696 ("private function with tagged result must"
9697 & " override visible-part function", S);
9698 Error_Msg_N
9699 ("\move subprogram to the visible part"
9700 & " (RM 3.9.3(10))", S);
9701
9702 -- AI05-0073: extend this test to the case of a function
9703 -- with a controlling access result.
9704
9705 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9706 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9707 and then
9708 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
9709 and then Ada_Version >= Ada_2012
9710 then
9711 Error_Msg_N
9712 ("private function with controlling access result "
9713 & "must override visible-part function", S);
9714 Error_Msg_N
9715 ("\move subprogram to the visible part"
9716 & " (RM 3.9.3(10))", S);
9717 end if;
9718 end if;
9719 end if;
9720 end Check_Private_Overriding;
9721
9722 -----------------------
9723 -- Visible_Part_Type --
9724 -----------------------
9725
9726 function Visible_Part_Type (T : Entity_Id) return Boolean is
9727 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9728 N : Node_Id;
9729
9730 begin
9731 -- If the entity is a private type, then it must be declared in a
9732 -- visible part.
9733
9734 if Ekind (T) in Private_Kind then
9735 return True;
9736 end if;
9737
9738 -- Otherwise, we traverse the visible part looking for its
9739 -- corresponding declaration. We cannot use the declaration
9740 -- node directly because in the private part the entity of a
9741 -- private type is the one in the full view, which does not
9742 -- indicate that it is the completion of something visible.
9743
9744 N := First (Visible_Declarations (Specification (P)));
9745 while Present (N) loop
9746 if Nkind (N) = N_Full_Type_Declaration
9747 and then Present (Defining_Identifier (N))
9748 and then T = Defining_Identifier (N)
9749 then
9750 return True;
9751
9752 elsif Nkind_In (N, N_Private_Type_Declaration,
9753 N_Private_Extension_Declaration)
9754 and then Present (Defining_Identifier (N))
9755 and then T = Full_View (Defining_Identifier (N))
9756 then
9757 return True;
9758 end if;
9759
9760 Next (N);
9761 end loop;
9762
9763 return False;
9764 end Visible_Part_Type;
9765
9766 -- Start of processing for Check_For_Primitive_Subprogram
9767
9768 begin
9769 Is_Primitive := False;
9770
9771 if not Comes_From_Source (S) then
9772 null;
9773
9774 -- If subprogram is at library level, it is not primitive operation
9775
9776 elsif Current_Scope = Standard_Standard then
9777 null;
9778
9779 elsif (Is_Package_Or_Generic_Package (Current_Scope)
9780 and then not In_Package_Body (Current_Scope))
9781 or else Is_Overriding
9782 then
9783 -- For function, check return type
9784
9785 if Ekind (S) = E_Function then
9786 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9787 F_Typ := Designated_Type (Etype (S));
9788 else
9789 F_Typ := Etype (S);
9790 end if;
9791
9792 B_Typ := Base_Type (F_Typ);
9793
9794 if Scope (B_Typ) = Current_Scope
9795 and then not Is_Class_Wide_Type (B_Typ)
9796 and then not Is_Generic_Type (B_Typ)
9797 then
9798 Is_Primitive := True;
9799 Set_Has_Primitive_Operations (B_Typ);
9800 Set_Is_Primitive (S);
9801 Check_Private_Overriding (B_Typ);
9802 end if;
9803 end if;
9804
9805 -- For all subprograms, check formals
9806
9807 Formal := First_Formal (S);
9808 while Present (Formal) loop
9809 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9810 F_Typ := Designated_Type (Etype (Formal));
9811 else
9812 F_Typ := Etype (Formal);
9813 end if;
9814
9815 B_Typ := Base_Type (F_Typ);
9816
9817 if Ekind (B_Typ) = E_Access_Subtype then
9818 B_Typ := Base_Type (B_Typ);
9819 end if;
9820
9821 if Scope (B_Typ) = Current_Scope
9822 and then not Is_Class_Wide_Type (B_Typ)
9823 and then not Is_Generic_Type (B_Typ)
9824 then
9825 Is_Primitive := True;
9826 Set_Is_Primitive (S);
9827 Set_Has_Primitive_Operations (B_Typ);
9828 Check_Private_Overriding (B_Typ);
9829 end if;
9830
9831 Next_Formal (Formal);
9832 end loop;
9833
9834 -- Special case: An equality function can be redefined for a type
9835 -- occurring in a declarative part, and won't otherwise be treated as
9836 -- a primitive because it doesn't occur in a package spec and doesn't
9837 -- override an inherited subprogram. It's important that we mark it
9838 -- primitive so it can be returned by Collect_Primitive_Operations
9839 -- and be used in composing the equality operation of later types
9840 -- that have a component of the type.
9841
9842 elsif Chars (S) = Name_Op_Eq
9843 and then Etype (S) = Standard_Boolean
9844 then
9845 B_Typ := Base_Type (Etype (First_Formal (S)));
9846
9847 if Scope (B_Typ) = Current_Scope
9848 and then
9849 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
9850 and then not Is_Limited_Type (B_Typ)
9851 then
9852 Is_Primitive := True;
9853 Set_Is_Primitive (S);
9854 Set_Has_Primitive_Operations (B_Typ);
9855 Check_Private_Overriding (B_Typ);
9856 end if;
9857 end if;
9858 end Check_For_Primitive_Subprogram;
9859
9860 -----------------------------------
9861 -- Check_Synchronized_Overriding --
9862 -----------------------------------
9863
9864 procedure Check_Synchronized_Overriding
9865 (Def_Id : Entity_Id;
9866 Overridden_Subp : out Entity_Id)
9867 is
9868 Ifaces_List : Elist_Id;
9869 In_Scope : Boolean;
9870 Typ : Entity_Id;
9871
9872 function Matches_Prefixed_View_Profile
9873 (Prim_Params : List_Id;
9874 Iface_Params : List_Id) return Boolean;
9875 -- Determine whether a subprogram's parameter profile Prim_Params
9876 -- matches that of a potentially overridden interface subprogram
9877 -- Iface_Params. Also determine if the type of first parameter of
9878 -- Iface_Params is an implemented interface.
9879
9880 -----------------------------------
9881 -- Matches_Prefixed_View_Profile --
9882 -----------------------------------
9883
9884 function Matches_Prefixed_View_Profile
9885 (Prim_Params : List_Id;
9886 Iface_Params : List_Id) return Boolean
9887 is
9888 Iface_Id : Entity_Id;
9889 Iface_Param : Node_Id;
9890 Iface_Typ : Entity_Id;
9891 Prim_Id : Entity_Id;
9892 Prim_Param : Node_Id;
9893 Prim_Typ : Entity_Id;
9894
9895 function Is_Implemented
9896 (Ifaces_List : Elist_Id;
9897 Iface : Entity_Id) return Boolean;
9898 -- Determine if Iface is implemented by the current task or
9899 -- protected type.
9900
9901 --------------------
9902 -- Is_Implemented --
9903 --------------------
9904
9905 function Is_Implemented
9906 (Ifaces_List : Elist_Id;
9907 Iface : Entity_Id) return Boolean
9908 is
9909 Iface_Elmt : Elmt_Id;
9910
9911 begin
9912 Iface_Elmt := First_Elmt (Ifaces_List);
9913 while Present (Iface_Elmt) loop
9914 if Node (Iface_Elmt) = Iface then
9915 return True;
9916 end if;
9917
9918 Next_Elmt (Iface_Elmt);
9919 end loop;
9920
9921 return False;
9922 end Is_Implemented;
9923
9924 -- Start of processing for Matches_Prefixed_View_Profile
9925
9926 begin
9927 Iface_Param := First (Iface_Params);
9928 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9929
9930 if Is_Access_Type (Iface_Typ) then
9931 Iface_Typ := Designated_Type (Iface_Typ);
9932 end if;
9933
9934 Prim_Param := First (Prim_Params);
9935
9936 -- The first parameter of the potentially overridden subprogram
9937 -- must be an interface implemented by Prim.
9938
9939 if not Is_Interface (Iface_Typ)
9940 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9941 then
9942 return False;
9943 end if;
9944
9945 -- The checks on the object parameters are done, move onto the
9946 -- rest of the parameters.
9947
9948 if not In_Scope then
9949 Prim_Param := Next (Prim_Param);
9950 end if;
9951
9952 Iface_Param := Next (Iface_Param);
9953 while Present (Iface_Param) and then Present (Prim_Param) loop
9954 Iface_Id := Defining_Identifier (Iface_Param);
9955 Iface_Typ := Find_Parameter_Type (Iface_Param);
9956
9957 Prim_Id := Defining_Identifier (Prim_Param);
9958 Prim_Typ := Find_Parameter_Type (Prim_Param);
9959
9960 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9961 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9962 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9963 then
9964 Iface_Typ := Designated_Type (Iface_Typ);
9965 Prim_Typ := Designated_Type (Prim_Typ);
9966 end if;
9967
9968 -- Case of multiple interface types inside a parameter profile
9969
9970 -- (Obj_Param : in out Iface; ...; Param : Iface)
9971
9972 -- If the interface type is implemented, then the matching type
9973 -- in the primitive should be the implementing record type.
9974
9975 if Ekind (Iface_Typ) = E_Record_Type
9976 and then Is_Interface (Iface_Typ)
9977 and then Is_Implemented (Ifaces_List, Iface_Typ)
9978 then
9979 if Prim_Typ /= Typ then
9980 return False;
9981 end if;
9982
9983 -- The two parameters must be both mode and subtype conformant
9984
9985 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9986 or else not
9987 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9988 then
9989 return False;
9990 end if;
9991
9992 Next (Iface_Param);
9993 Next (Prim_Param);
9994 end loop;
9995
9996 -- One of the two lists contains more parameters than the other
9997
9998 if Present (Iface_Param) or else Present (Prim_Param) then
9999 return False;
10000 end if;
10001
10002 return True;
10003 end Matches_Prefixed_View_Profile;
10004
10005 -- Start of processing for Check_Synchronized_Overriding
10006
10007 begin
10008 Overridden_Subp := Empty;
10009
10010 -- Def_Id must be an entry or a subprogram. We should skip predefined
10011 -- primitives internally generated by the frontend; however at this
10012 -- stage predefined primitives are still not fully decorated. As a
10013 -- minor optimization we skip here internally generated subprograms.
10014
10015 if (Ekind (Def_Id) /= E_Entry
10016 and then Ekind (Def_Id) /= E_Function
10017 and then Ekind (Def_Id) /= E_Procedure)
10018 or else not Comes_From_Source (Def_Id)
10019 then
10020 return;
10021 end if;
10022
10023 -- Search for the concurrent declaration since it contains the list
10024 -- of all implemented interfaces. In this case, the subprogram is
10025 -- declared within the scope of a protected or a task type.
10026
10027 if Present (Scope (Def_Id))
10028 and then Is_Concurrent_Type (Scope (Def_Id))
10029 and then not Is_Generic_Actual_Type (Scope (Def_Id))
10030 then
10031 Typ := Scope (Def_Id);
10032 In_Scope := True;
10033
10034 -- The enclosing scope is not a synchronized type and the subprogram
10035 -- has no formals.
10036
10037 elsif No (First_Formal (Def_Id)) then
10038 return;
10039
10040 -- The subprogram has formals and hence it may be a primitive of a
10041 -- concurrent type.
10042
10043 else
10044 Typ := Etype (First_Formal (Def_Id));
10045
10046 if Is_Access_Type (Typ) then
10047 Typ := Directly_Designated_Type (Typ);
10048 end if;
10049
10050 if Is_Concurrent_Type (Typ)
10051 and then not Is_Generic_Actual_Type (Typ)
10052 then
10053 In_Scope := False;
10054
10055 -- This case occurs when the concurrent type is declared within
10056 -- a generic unit. As a result the corresponding record has been
10057 -- built and used as the type of the first formal, we just have
10058 -- to retrieve the corresponding concurrent type.
10059
10060 elsif Is_Concurrent_Record_Type (Typ)
10061 and then not Is_Class_Wide_Type (Typ)
10062 and then Present (Corresponding_Concurrent_Type (Typ))
10063 then
10064 Typ := Corresponding_Concurrent_Type (Typ);
10065 In_Scope := False;
10066
10067 else
10068 return;
10069 end if;
10070 end if;
10071
10072 -- There is no overriding to check if is an inherited operation in a
10073 -- type derivation on for a generic actual.
10074
10075 Collect_Interfaces (Typ, Ifaces_List);
10076
10077 if Is_Empty_Elmt_List (Ifaces_List) then
10078 return;
10079 end if;
10080
10081 -- Determine whether entry or subprogram Def_Id overrides a primitive
10082 -- operation that belongs to one of the interfaces in Ifaces_List.
10083
10084 declare
10085 Candidate : Entity_Id := Empty;
10086 Hom : Entity_Id := Empty;
10087 Iface_Typ : Entity_Id;
10088 Subp : Entity_Id := Empty;
10089
10090 begin
10091 -- Traverse the homonym chain, looking for a potentially
10092 -- overridden subprogram that belongs to an implemented
10093 -- interface.
10094
10095 Hom := Current_Entity_In_Scope (Def_Id);
10096 while Present (Hom) loop
10097 Subp := Hom;
10098
10099 if Subp = Def_Id
10100 or else not Is_Overloadable (Subp)
10101 or else not Is_Primitive (Subp)
10102 or else not Is_Dispatching_Operation (Subp)
10103 or else not Present (Find_Dispatching_Type (Subp))
10104 or else not Is_Interface (Find_Dispatching_Type (Subp))
10105 then
10106 null;
10107
10108 -- Entries and procedures can override abstract or null
10109 -- interface procedures.
10110
10111 elsif (Ekind (Def_Id) = E_Procedure
10112 or else Ekind (Def_Id) = E_Entry)
10113 and then Ekind (Subp) = E_Procedure
10114 and then Matches_Prefixed_View_Profile
10115 (Parameter_Specifications (Parent (Def_Id)),
10116 Parameter_Specifications (Parent (Subp)))
10117 then
10118 Candidate := Subp;
10119
10120 -- For an overridden subprogram Subp, check whether the mode
10121 -- of its first parameter is correct depending on the kind
10122 -- of synchronized type.
10123
10124 declare
10125 Formal : constant Node_Id := First_Formal (Candidate);
10126
10127 begin
10128 -- In order for an entry or a protected procedure to
10129 -- override, the first parameter of the overridden
10130 -- routine must be of mode "out", "in out" or
10131 -- access-to-variable.
10132
10133 if Ekind_In (Candidate, E_Entry, E_Procedure)
10134 and then Is_Protected_Type (Typ)
10135 and then Ekind (Formal) /= E_In_Out_Parameter
10136 and then Ekind (Formal) /= E_Out_Parameter
10137 and then Nkind (Parameter_Type (Parent (Formal))) /=
10138 N_Access_Definition
10139 then
10140 null;
10141
10142 -- All other cases are OK since a task entry or routine
10143 -- does not have a restriction on the mode of the first
10144 -- parameter of the overridden interface routine.
10145
10146 else
10147 Overridden_Subp := Candidate;
10148 return;
10149 end if;
10150 end;
10151
10152 -- Functions can override abstract interface functions
10153
10154 elsif Ekind (Def_Id) = E_Function
10155 and then Ekind (Subp) = E_Function
10156 and then Matches_Prefixed_View_Profile
10157 (Parameter_Specifications (Parent (Def_Id)),
10158 Parameter_Specifications (Parent (Subp)))
10159 and then Etype (Result_Definition (Parent (Def_Id))) =
10160 Etype (Result_Definition (Parent (Subp)))
10161 then
10162 Overridden_Subp := Subp;
10163 return;
10164 end if;
10165
10166 Hom := Homonym (Hom);
10167 end loop;
10168
10169 -- After examining all candidates for overriding, we are left with
10170 -- the best match which is a mode incompatible interface routine.
10171 -- Do not emit an error if the Expander is active since this error
10172 -- will be detected later on after all concurrent types are
10173 -- expanded and all wrappers are built. This check is meant for
10174 -- spec-only compilations.
10175
10176 if Present (Candidate) and then not Expander_Active then
10177 Iface_Typ :=
10178 Find_Parameter_Type (Parent (First_Formal (Candidate)));
10179
10180 -- Def_Id is primitive of a protected type, declared inside the
10181 -- type, and the candidate is primitive of a limited or
10182 -- synchronized interface.
10183
10184 if In_Scope
10185 and then Is_Protected_Type (Typ)
10186 and then
10187 (Is_Limited_Interface (Iface_Typ)
10188 or else Is_Protected_Interface (Iface_Typ)
10189 or else Is_Synchronized_Interface (Iface_Typ)
10190 or else Is_Task_Interface (Iface_Typ))
10191 then
10192 Error_Msg_PT (Parent (Typ), Candidate);
10193 end if;
10194 end if;
10195
10196 Overridden_Subp := Candidate;
10197 return;
10198 end;
10199 end Check_Synchronized_Overriding;
10200
10201 ----------------------------
10202 -- Is_Private_Declaration --
10203 ----------------------------
10204
10205 function Is_Private_Declaration (E : Entity_Id) return Boolean is
10206 Priv_Decls : List_Id;
10207 Decl : constant Node_Id := Unit_Declaration_Node (E);
10208
10209 begin
10210 if Is_Package_Or_Generic_Package (Current_Scope)
10211 and then In_Private_Part (Current_Scope)
10212 then
10213 Priv_Decls :=
10214 Private_Declarations
10215 (Specification (Unit_Declaration_Node (Current_Scope)));
10216
10217 return In_Package_Body (Current_Scope)
10218 or else
10219 (Is_List_Member (Decl)
10220 and then List_Containing (Decl) = Priv_Decls)
10221 or else (Nkind (Parent (Decl)) = N_Package_Specification
10222 and then not
10223 Is_Compilation_Unit
10224 (Defining_Entity (Parent (Decl)))
10225 and then List_Containing (Parent (Parent (Decl))) =
10226 Priv_Decls);
10227 else
10228 return False;
10229 end if;
10230 end Is_Private_Declaration;
10231
10232 --------------------------
10233 -- Is_Overriding_Alias --
10234 --------------------------
10235
10236 function Is_Overriding_Alias
10237 (Old_E : Entity_Id;
10238 New_E : Entity_Id) return Boolean
10239 is
10240 AO : constant Entity_Id := Alias (Old_E);
10241 AN : constant Entity_Id := Alias (New_E);
10242
10243 begin
10244 return Scope (AO) /= Scope (AN)
10245 or else No (DTC_Entity (AO))
10246 or else No (DTC_Entity (AN))
10247 or else DT_Position (AO) = DT_Position (AN);
10248 end Is_Overriding_Alias;
10249
10250 -- Start of processing for New_Overloaded_Entity
10251
10252 begin
10253 -- We need to look for an entity that S may override. This must be a
10254 -- homonym in the current scope, so we look for the first homonym of
10255 -- S in the current scope as the starting point for the search.
10256
10257 E := Current_Entity_In_Scope (S);
10258
10259 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10260 -- They are directly added to the list of primitive operations of
10261 -- Derived_Type, unless this is a rederivation in the private part
10262 -- of an operation that was already derived in the visible part of
10263 -- the current package.
10264
10265 if Ada_Version >= Ada_2005
10266 and then Present (Derived_Type)
10267 and then Present (Alias (S))
10268 and then Is_Dispatching_Operation (Alias (S))
10269 and then Present (Find_Dispatching_Type (Alias (S)))
10270 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10271 then
10272 -- For private types, when the full-view is processed we propagate to
10273 -- the full view the non-overridden entities whose attribute "alias"
10274 -- references an interface primitive. These entities were added by
10275 -- Derive_Subprograms to ensure that interface primitives are
10276 -- covered.
10277
10278 -- Inside_Freeze_Actions is non zero when S corresponds with an
10279 -- internal entity that links an interface primitive with its
10280 -- covering primitive through attribute Interface_Alias (see
10281 -- Add_Internal_Interface_Entities).
10282
10283 if Inside_Freezing_Actions = 0
10284 and then Is_Package_Or_Generic_Package (Current_Scope)
10285 and then In_Private_Part (Current_Scope)
10286 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10287 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10288 and then Full_View (Defining_Identifier (Parent (E)))
10289 = Defining_Identifier (Parent (S))
10290 and then Alias (E) = Alias (S)
10291 then
10292 Check_Operation_From_Private_View (S, E);
10293 Set_Is_Dispatching_Operation (S);
10294
10295 -- Common case
10296
10297 else
10298 Enter_Overloaded_Entity (S);
10299 Check_Dispatching_Operation (S, Empty);
10300 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10301 end if;
10302
10303 return;
10304 end if;
10305
10306 -- If there is no homonym then this is definitely not overriding
10307
10308 if No (E) then
10309 Enter_Overloaded_Entity (S);
10310 Check_Dispatching_Operation (S, Empty);
10311 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10312
10313 -- If subprogram has an explicit declaration, check whether it
10314 -- has an overriding indicator.
10315
10316 if Comes_From_Source (S) then
10317 Check_Synchronized_Overriding (S, Overridden_Subp);
10318
10319 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10320 -- it may have overridden some hidden inherited primitive. Update
10321 -- Overridden_Subp to avoid spurious errors when checking the
10322 -- overriding indicator.
10323
10324 if Ada_Version >= Ada_2012
10325 and then No (Overridden_Subp)
10326 and then Is_Dispatching_Operation (S)
10327 and then Present (Overridden_Operation (S))
10328 then
10329 Overridden_Subp := Overridden_Operation (S);
10330 end if;
10331
10332 Check_Overriding_Indicator
10333 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10334 end if;
10335
10336 -- If there is a homonym that is not overloadable, then we have an
10337 -- error, except for the special cases checked explicitly below.
10338
10339 elsif not Is_Overloadable (E) then
10340
10341 -- Check for spurious conflict produced by a subprogram that has the
10342 -- same name as that of the enclosing generic package. The conflict
10343 -- occurs within an instance, between the subprogram and the renaming
10344 -- declaration for the package. After the subprogram, the package
10345 -- renaming declaration becomes hidden.
10346
10347 if Ekind (E) = E_Package
10348 and then Present (Renamed_Object (E))
10349 and then Renamed_Object (E) = Current_Scope
10350 and then Nkind (Parent (Renamed_Object (E))) =
10351 N_Package_Specification
10352 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10353 then
10354 Set_Is_Hidden (E);
10355 Set_Is_Immediately_Visible (E, False);
10356 Enter_Overloaded_Entity (S);
10357 Set_Homonym (S, Homonym (E));
10358 Check_Dispatching_Operation (S, Empty);
10359 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
10360
10361 -- If the subprogram is implicit it is hidden by the previous
10362 -- declaration. However if it is dispatching, it must appear in the
10363 -- dispatch table anyway, because it can be dispatched to even if it
10364 -- cannot be called directly.
10365
10366 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
10367 Set_Scope (S, Current_Scope);
10368
10369 if Is_Dispatching_Operation (Alias (S)) then
10370 Check_Dispatching_Operation (S, Empty);
10371 end if;
10372
10373 return;
10374
10375 else
10376 Error_Msg_Sloc := Sloc (E);
10377
10378 -- Generate message, with useful additional warning if in generic
10379
10380 if Is_Generic_Unit (E) then
10381 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10382 Error_Msg_N ("\& conflicts with declaration#", S);
10383 else
10384 Error_Msg_N ("& conflicts with declaration#", S);
10385 end if;
10386
10387 return;
10388 end if;
10389
10390 -- E exists and is overloadable
10391
10392 else
10393 Check_Synchronized_Overriding (S, Overridden_Subp);
10394
10395 -- Loop through E and its homonyms to determine if any of them is
10396 -- the candidate for overriding by S.
10397
10398 while Present (E) loop
10399
10400 -- Definitely not interesting if not in the current scope
10401
10402 if Scope (E) /= Current_Scope then
10403 null;
10404
10405 -- Ada 2012 (AI05-0165): For internally generated bodies of
10406 -- null procedures locate the internally generated spec. We
10407 -- enforce mode conformance since a tagged type may inherit
10408 -- from interfaces several null primitives which differ only
10409 -- in the mode of the formals.
10410
10411 elsif not Comes_From_Source (S)
10412 and then Is_Null_Procedure (S)
10413 and then not Mode_Conformant (E, S)
10414 then
10415 null;
10416
10417 -- Check if we have type conformance
10418
10419 elsif Type_Conformant (E, S) then
10420
10421 -- If the old and new entities have the same profile and one
10422 -- is not the body of the other, then this is an error, unless
10423 -- one of them is implicitly declared.
10424
10425 -- There are some cases when both can be implicit, for example
10426 -- when both a literal and a function that overrides it are
10427 -- inherited in a derivation, or when an inherited operation
10428 -- of a tagged full type overrides the inherited operation of
10429 -- a private extension. Ada 83 had a special rule for the
10430 -- literal case. In Ada 95, the later implicit operation hides
10431 -- the former, and the literal is always the former. In the
10432 -- odd case where both are derived operations declared at the
10433 -- same point, both operations should be declared, and in that
10434 -- case we bypass the following test and proceed to the next
10435 -- part. This can only occur for certain obscure cases in
10436 -- instances, when an operation on a type derived from a formal
10437 -- private type does not override a homograph inherited from
10438 -- the actual. In subsequent derivations of such a type, the
10439 -- DT positions of these operations remain distinct, if they
10440 -- have been set.
10441
10442 if Present (Alias (S))
10443 and then (No (Alias (E))
10444 or else Comes_From_Source (E)
10445 or else Is_Abstract_Subprogram (S)
10446 or else
10447 (Is_Dispatching_Operation (E)
10448 and then Is_Overriding_Alias (E, S)))
10449 and then Ekind (E) /= E_Enumeration_Literal
10450 then
10451 -- When an derived operation is overloaded it may be due to
10452 -- the fact that the full view of a private extension
10453 -- re-inherits. It has to be dealt with.
10454
10455 if Is_Package_Or_Generic_Package (Current_Scope)
10456 and then In_Private_Part (Current_Scope)
10457 then
10458 Check_Operation_From_Private_View (S, E);
10459 end if;
10460
10461 -- In any case the implicit operation remains hidden by the
10462 -- existing declaration, which is overriding. Indicate that
10463 -- E overrides the operation from which S is inherited.
10464
10465 if Present (Alias (S)) then
10466 Set_Overridden_Operation (E, Alias (S));
10467 else
10468 Set_Overridden_Operation (E, S);
10469 end if;
10470
10471 if Comes_From_Source (E) then
10472 Check_Overriding_Indicator (E, S, Is_Primitive => False);
10473 end if;
10474
10475 return;
10476
10477 -- Within an instance, the renaming declarations for actual
10478 -- subprograms may become ambiguous, but they do not hide each
10479 -- other.
10480
10481 elsif Ekind (E) /= E_Entry
10482 and then not Comes_From_Source (E)
10483 and then not Is_Generic_Instance (E)
10484 and then (Present (Alias (E))
10485 or else Is_Intrinsic_Subprogram (E))
10486 and then (not In_Instance
10487 or else No (Parent (E))
10488 or else Nkind (Unit_Declaration_Node (E)) /=
10489 N_Subprogram_Renaming_Declaration)
10490 then
10491 -- A subprogram child unit is not allowed to override an
10492 -- inherited subprogram (10.1.1(20)).
10493
10494 if Is_Child_Unit (S) then
10495 Error_Msg_N
10496 ("child unit overrides inherited subprogram in parent",
10497 S);
10498 return;
10499 end if;
10500
10501 if Is_Non_Overriding_Operation (E, S) then
10502 Enter_Overloaded_Entity (S);
10503
10504 if No (Derived_Type)
10505 or else Is_Tagged_Type (Derived_Type)
10506 then
10507 Check_Dispatching_Operation (S, Empty);
10508 end if;
10509
10510 return;
10511 end if;
10512
10513 -- E is a derived operation or an internal operator which
10514 -- is being overridden. Remove E from further visibility.
10515 -- Furthermore, if E is a dispatching operation, it must be
10516 -- replaced in the list of primitive operations of its type
10517 -- (see Override_Dispatching_Operation).
10518
10519 Overridden_Subp := E;
10520
10521 declare
10522 Prev : Entity_Id;
10523
10524 begin
10525 Prev := First_Entity (Current_Scope);
10526 while Present (Prev) and then Next_Entity (Prev) /= E loop
10527 Next_Entity (Prev);
10528 end loop;
10529
10530 -- It is possible for E to be in the current scope and
10531 -- yet not in the entity chain. This can only occur in a
10532 -- generic context where E is an implicit concatenation
10533 -- in the formal part, because in a generic body the
10534 -- entity chain starts with the formals.
10535
10536 pragma Assert
10537 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10538
10539 -- E must be removed both from the entity_list of the
10540 -- current scope, and from the visibility chain
10541
10542 if Debug_Flag_E then
10543 Write_Str ("Override implicit operation ");
10544 Write_Int (Int (E));
10545 Write_Eol;
10546 end if;
10547
10548 -- If E is a predefined concatenation, it stands for four
10549 -- different operations. As a result, a single explicit
10550 -- declaration does not hide it. In a possible ambiguous
10551 -- situation, Disambiguate chooses the user-defined op,
10552 -- so it is correct to retain the previous internal one.
10553
10554 if Chars (E) /= Name_Op_Concat
10555 or else Ekind (E) /= E_Operator
10556 then
10557 -- For nondispatching derived operations that are
10558 -- overridden by a subprogram declared in the private
10559 -- part of a package, we retain the derived subprogram
10560 -- but mark it as not immediately visible. If the
10561 -- derived operation was declared in the visible part
10562 -- then this ensures that it will still be visible
10563 -- outside the package with the proper signature
10564 -- (calls from outside must also be directed to this
10565 -- version rather than the overriding one, unlike the
10566 -- dispatching case). Calls from inside the package
10567 -- will still resolve to the overriding subprogram
10568 -- since the derived one is marked as not visible
10569 -- within the package.
10570
10571 -- If the private operation is dispatching, we achieve
10572 -- the overriding by keeping the implicit operation
10573 -- but setting its alias to be the overriding one. In
10574 -- this fashion the proper body is executed in all
10575 -- cases, but the original signature is used outside
10576 -- of the package.
10577
10578 -- If the overriding is not in the private part, we
10579 -- remove the implicit operation altogether.
10580
10581 if Is_Private_Declaration (S) then
10582 if not Is_Dispatching_Operation (E) then
10583 Set_Is_Immediately_Visible (E, False);
10584 else
10585 -- Work done in Override_Dispatching_Operation,
10586 -- so nothing else needs to be done here.
10587
10588 null;
10589 end if;
10590
10591 else
10592 -- Find predecessor of E in Homonym chain
10593
10594 if E = Current_Entity (E) then
10595 Prev_Vis := Empty;
10596 else
10597 Prev_Vis := Current_Entity (E);
10598 while Homonym (Prev_Vis) /= E loop
10599 Prev_Vis := Homonym (Prev_Vis);
10600 end loop;
10601 end if;
10602
10603 if Prev_Vis /= Empty then
10604
10605 -- Skip E in the visibility chain
10606
10607 Set_Homonym (Prev_Vis, Homonym (E));
10608
10609 else
10610 Set_Name_Entity_Id (Chars (E), Homonym (E));
10611 end if;
10612
10613 Set_Next_Entity (Prev, Next_Entity (E));
10614
10615 if No (Next_Entity (Prev)) then
10616 Set_Last_Entity (Current_Scope, Prev);
10617 end if;
10618 end if;
10619 end if;
10620
10621 Enter_Overloaded_Entity (S);
10622
10623 -- For entities generated by Derive_Subprograms the
10624 -- overridden operation is the inherited primitive
10625 -- (which is available through the attribute alias).
10626
10627 if not (Comes_From_Source (E))
10628 and then Is_Dispatching_Operation (E)
10629 and then Find_Dispatching_Type (E) =
10630 Find_Dispatching_Type (S)
10631 and then Present (Alias (E))
10632 and then Comes_From_Source (Alias (E))
10633 then
10634 Set_Overridden_Operation (S, Alias (E));
10635
10636 -- Normal case of setting entity as overridden
10637
10638 -- Note: Static_Initialization and Overridden_Operation
10639 -- attributes use the same field in subprogram entities.
10640 -- Static_Initialization is only defined for internal
10641 -- initialization procedures, where Overridden_Operation
10642 -- is irrelevant. Therefore the setting of this attribute
10643 -- must check whether the target is an init_proc.
10644
10645 elsif not Is_Init_Proc (S) then
10646 Set_Overridden_Operation (S, E);
10647 end if;
10648
10649 Check_Overriding_Indicator (S, E, Is_Primitive => True);
10650
10651 -- If S is a user-defined subprogram or a null procedure
10652 -- expanded to override an inherited null procedure, or a
10653 -- predefined dispatching primitive then indicate that E
10654 -- overrides the operation from which S is inherited.
10655
10656 if Comes_From_Source (S)
10657 or else
10658 (Present (Parent (S))
10659 and then
10660 Nkind (Parent (S)) = N_Procedure_Specification
10661 and then
10662 Null_Present (Parent (S)))
10663 or else
10664 (Present (Alias (E))
10665 and then
10666 Is_Predefined_Dispatching_Operation (Alias (E)))
10667 then
10668 if Present (Alias (E)) then
10669 Set_Overridden_Operation (S, Alias (E));
10670 end if;
10671 end if;
10672
10673 if Is_Dispatching_Operation (E) then
10674
10675 -- An overriding dispatching subprogram inherits the
10676 -- convention of the overridden subprogram (AI-117).
10677
10678 Set_Convention (S, Convention (E));
10679 Check_Dispatching_Operation (S, E);
10680
10681 else
10682 Check_Dispatching_Operation (S, Empty);
10683 end if;
10684
10685 Check_For_Primitive_Subprogram
10686 (Is_Primitive_Subp, Is_Overriding => True);
10687 goto Check_Inequality;
10688 end;
10689
10690 -- Apparent redeclarations in instances can occur when two
10691 -- formal types get the same actual type. The subprograms in
10692 -- in the instance are legal, even if not callable from the
10693 -- outside. Calls from within are disambiguated elsewhere.
10694 -- For dispatching operations in the visible part, the usual
10695 -- rules apply, and operations with the same profile are not
10696 -- legal (B830001).
10697
10698 elsif (In_Instance_Visible_Part
10699 and then not Is_Dispatching_Operation (E))
10700 or else In_Instance_Not_Visible
10701 then
10702 null;
10703
10704 -- Here we have a real error (identical profile)
10705
10706 else
10707 Error_Msg_Sloc := Sloc (E);
10708
10709 -- Avoid cascaded errors if the entity appears in
10710 -- subsequent calls.
10711
10712 Set_Scope (S, Current_Scope);
10713
10714 -- Generate error, with extra useful warning for the case
10715 -- of a generic instance with no completion.
10716
10717 if Is_Generic_Instance (S)
10718 and then not Has_Completion (E)
10719 then
10720 Error_Msg_N
10721 ("instantiation cannot provide body for&", S);
10722 Error_Msg_N ("\& conflicts with declaration#", S);
10723 else
10724 Error_Msg_N ("& conflicts with declaration#", S);
10725 end if;
10726
10727 return;
10728 end if;
10729
10730 else
10731 -- If one subprogram has an access parameter and the other
10732 -- a parameter of an access type, calls to either might be
10733 -- ambiguous. Verify that parameters match except for the
10734 -- access parameter.
10735
10736 if May_Hide_Profile then
10737 declare
10738 F1 : Entity_Id;
10739 F2 : Entity_Id;
10740
10741 begin
10742 F1 := First_Formal (S);
10743 F2 := First_Formal (E);
10744 while Present (F1) and then Present (F2) loop
10745 if Is_Access_Type (Etype (F1)) then
10746 if not Is_Access_Type (Etype (F2))
10747 or else not Conforming_Types
10748 (Designated_Type (Etype (F1)),
10749 Designated_Type (Etype (F2)),
10750 Type_Conformant)
10751 then
10752 May_Hide_Profile := False;
10753 end if;
10754
10755 elsif
10756 not Conforming_Types
10757 (Etype (F1), Etype (F2), Type_Conformant)
10758 then
10759 May_Hide_Profile := False;
10760 end if;
10761
10762 Next_Formal (F1);
10763 Next_Formal (F2);
10764 end loop;
10765
10766 if May_Hide_Profile
10767 and then No (F1)
10768 and then No (F2)
10769 then
10770 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
10771 end if;
10772 end;
10773 end if;
10774 end if;
10775
10776 E := Homonym (E);
10777 end loop;
10778
10779 -- On exit, we know that S is a new entity
10780
10781 Enter_Overloaded_Entity (S);
10782 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10783 Check_Overriding_Indicator
10784 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
10785
10786 -- Overloading is not allowed in SPARK, except for operators
10787
10788 if Nkind (S) /= N_Defining_Operator_Symbol then
10789 Error_Msg_Sloc := Sloc (Homonym (S));
10790 Check_SPARK_Restriction
10791 ("overloading not allowed with entity#", S);
10792 end if;
10793
10794 -- If S is a derived operation for an untagged type then by
10795 -- definition it's not a dispatching operation (even if the parent
10796 -- operation was dispatching), so Check_Dispatching_Operation is not
10797 -- called in that case.
10798
10799 if No (Derived_Type)
10800 or else Is_Tagged_Type (Derived_Type)
10801 then
10802 Check_Dispatching_Operation (S, Empty);
10803 end if;
10804 end if;
10805
10806 -- If this is a user-defined equality operator that is not a derived
10807 -- subprogram, create the corresponding inequality. If the operation is
10808 -- dispatching, the expansion is done elsewhere, and we do not create
10809 -- an explicit inequality operation.
10810
10811 <<Check_Inequality>>
10812 if Chars (S) = Name_Op_Eq
10813 and then Etype (S) = Standard_Boolean
10814 and then Present (Parent (S))
10815 and then not Is_Dispatching_Operation (S)
10816 then
10817 Make_Inequality_Operator (S);
10818
10819 if Ada_Version >= Ada_2012 then
10820 Check_Untagged_Equality (S);
10821 end if;
10822 end if;
10823 end New_Overloaded_Entity;
10824
10825 ---------------------
10826 -- Process_Formals --
10827 ---------------------
10828
10829 procedure Process_Formals
10830 (T : List_Id;
10831 Related_Nod : Node_Id)
10832 is
10833 Param_Spec : Node_Id;
10834 Formal : Entity_Id;
10835 Formal_Type : Entity_Id;
10836 Default : Node_Id;
10837 Ptype : Entity_Id;
10838
10839 Num_Out_Params : Nat := 0;
10840 First_Out_Param : Entity_Id := Empty;
10841 -- Used for setting Is_Only_Out_Parameter
10842
10843 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
10844 -- Determine whether an access type designates a type coming from a
10845 -- limited view.
10846
10847 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
10848 -- Check whether the default has a class-wide type. After analysis the
10849 -- default has the type of the formal, so we must also check explicitly
10850 -- for an access attribute.
10851
10852 -------------------------------
10853 -- Designates_From_With_Type --
10854 -------------------------------
10855
10856 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
10857 Desig : Entity_Id := Typ;
10858
10859 begin
10860 if Is_Access_Type (Desig) then
10861 Desig := Directly_Designated_Type (Desig);
10862 end if;
10863
10864 if Is_Class_Wide_Type (Desig) then
10865 Desig := Root_Type (Desig);
10866 end if;
10867
10868 return
10869 Ekind (Desig) = E_Incomplete_Type and then From_With_Type (Desig);
10870 end Designates_From_With_Type;
10871
10872 ---------------------------
10873 -- Is_Class_Wide_Default --
10874 ---------------------------
10875
10876 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10877 begin
10878 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10879 or else (Nkind (D) = N_Attribute_Reference
10880 and then Attribute_Name (D) = Name_Access
10881 and then Is_Class_Wide_Type (Etype (Prefix (D))));
10882 end Is_Class_Wide_Default;
10883
10884 -- Start of processing for Process_Formals
10885
10886 begin
10887 -- In order to prevent premature use of the formals in the same formal
10888 -- part, the Ekind is left undefined until all default expressions are
10889 -- analyzed. The Ekind is established in a separate loop at the end.
10890
10891 Param_Spec := First (T);
10892 while Present (Param_Spec) loop
10893 Formal := Defining_Identifier (Param_Spec);
10894 Set_Never_Set_In_Source (Formal, True);
10895 Enter_Name (Formal);
10896
10897 -- Case of ordinary parameters
10898
10899 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10900 Find_Type (Parameter_Type (Param_Spec));
10901 Ptype := Parameter_Type (Param_Spec);
10902
10903 if Ptype = Error then
10904 goto Continue;
10905 end if;
10906
10907 Formal_Type := Entity (Ptype);
10908
10909 if Is_Incomplete_Type (Formal_Type)
10910 or else
10911 (Is_Class_Wide_Type (Formal_Type)
10912 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
10913 then
10914 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10915 -- primitive operations, as long as their completion is
10916 -- in the same declarative part. If in the private part
10917 -- this means that the type cannot be a Taft-amendment type.
10918 -- Check is done on package exit. For access to subprograms,
10919 -- the use is legal for Taft-amendment types.
10920
10921 -- Ada 2012: tagged incomplete types are allowed as generic
10922 -- formal types. They do not introduce dependencies and the
10923 -- corresponding generic subprogram does not have a delayed
10924 -- freeze, because it does not need a freeze node.
10925
10926 if Is_Tagged_Type (Formal_Type) then
10927 if Ekind (Scope (Current_Scope)) = E_Package
10928 and then not From_With_Type (Formal_Type)
10929 and then not Is_Generic_Type (Formal_Type)
10930 and then not Is_Class_Wide_Type (Formal_Type)
10931 then
10932 if not Nkind_In
10933 (Parent (T), N_Access_Function_Definition,
10934 N_Access_Procedure_Definition)
10935 then
10936 Append_Elmt
10937 (Current_Scope,
10938 Private_Dependents (Base_Type (Formal_Type)));
10939
10940 -- Freezing is delayed to ensure that Register_Prim
10941 -- will get called for this operation, which is needed
10942 -- in cases where static dispatch tables aren't built.
10943 -- (Note that the same is done for controlling access
10944 -- parameter cases in function Access_Definition.)
10945
10946 Set_Has_Delayed_Freeze (Current_Scope);
10947 end if;
10948 end if;
10949
10950 -- Special handling of Value_Type for CIL case
10951
10952 elsif Is_Value_Type (Formal_Type) then
10953 null;
10954
10955 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10956 N_Access_Procedure_Definition)
10957 then
10958 -- AI05-0151: Tagged incomplete types are allowed in all
10959 -- formal parts. Untagged incomplete types are not allowed
10960 -- in bodies.
10961
10962 if Ada_Version >= Ada_2012 then
10963 if Is_Tagged_Type (Formal_Type) then
10964 null;
10965
10966 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10967 N_Entry_Body,
10968 N_Subprogram_Body)
10969 then
10970 Error_Msg_NE
10971 ("invalid use of untagged incomplete type&",
10972 Ptype, Formal_Type);
10973 end if;
10974
10975 else
10976 Error_Msg_NE
10977 ("invalid use of incomplete type&",
10978 Param_Spec, Formal_Type);
10979
10980 -- Further checks on the legality of incomplete types
10981 -- in formal parts are delayed until the freeze point
10982 -- of the enclosing subprogram or access to subprogram.
10983 end if;
10984 end if;
10985
10986 elsif Ekind (Formal_Type) = E_Void then
10987 Error_Msg_NE
10988 ("premature use of&",
10989 Parameter_Type (Param_Spec), Formal_Type);
10990 end if;
10991
10992 -- Ada 2012 (AI-142): Handle aliased parameters
10993
10994 if Ada_Version >= Ada_2012
10995 and then Aliased_Present (Param_Spec)
10996 then
10997 Set_Is_Aliased (Formal);
10998 end if;
10999
11000 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11001 -- declaration corresponding to the null-excluding type of the
11002 -- formal in the enclosing scope. Finally, replace the parameter
11003 -- type of the formal with the internal subtype.
11004
11005 if Ada_Version >= Ada_2005
11006 and then Null_Exclusion_Present (Param_Spec)
11007 then
11008 if not Is_Access_Type (Formal_Type) then
11009 Error_Msg_N
11010 ("`NOT NULL` allowed only for an access type", Param_Spec);
11011
11012 else
11013 if Can_Never_Be_Null (Formal_Type)
11014 and then Comes_From_Source (Related_Nod)
11015 then
11016 Error_Msg_NE
11017 ("`NOT NULL` not allowed (& already excludes null)",
11018 Param_Spec, Formal_Type);
11019 end if;
11020
11021 Formal_Type :=
11022 Create_Null_Excluding_Itype
11023 (T => Formal_Type,
11024 Related_Nod => Related_Nod,
11025 Scope_Id => Scope (Current_Scope));
11026
11027 -- If the designated type of the itype is an itype that is
11028 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11029 -- on the access subtype, to prevent order-of-elaboration
11030 -- issues in the backend.
11031
11032 -- Example:
11033 -- type T is access procedure;
11034 -- procedure Op (O : not null T);
11035
11036 if Is_Itype (Directly_Designated_Type (Formal_Type))
11037 and then
11038 not Is_Frozen (Directly_Designated_Type (Formal_Type))
11039 then
11040 Set_Has_Delayed_Freeze (Formal_Type);
11041 end if;
11042 end if;
11043 end if;
11044
11045 -- An access formal type
11046
11047 else
11048 Formal_Type :=
11049 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
11050
11051 -- No need to continue if we already notified errors
11052
11053 if not Present (Formal_Type) then
11054 return;
11055 end if;
11056
11057 -- Ada 2005 (AI-254)
11058
11059 declare
11060 AD : constant Node_Id :=
11061 Access_To_Subprogram_Definition
11062 (Parameter_Type (Param_Spec));
11063 begin
11064 if Present (AD) and then Protected_Present (AD) then
11065 Formal_Type :=
11066 Replace_Anonymous_Access_To_Protected_Subprogram
11067 (Param_Spec);
11068 end if;
11069 end;
11070 end if;
11071
11072 Set_Etype (Formal, Formal_Type);
11073
11074 -- Deal with default expression if present
11075
11076 Default := Expression (Param_Spec);
11077
11078 if Present (Default) then
11079 Check_SPARK_Restriction
11080 ("default expression is not allowed", Default);
11081
11082 if Out_Present (Param_Spec) then
11083 Error_Msg_N
11084 ("default initialization only allowed for IN parameters",
11085 Param_Spec);
11086 end if;
11087
11088 -- Do the special preanalysis of the expression (see section on
11089 -- "Handling of Default Expressions" in the spec of package Sem).
11090
11091 Preanalyze_Spec_Expression (Default, Formal_Type);
11092
11093 -- An access to constant cannot be the default for
11094 -- an access parameter that is an access to variable.
11095
11096 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11097 and then not Is_Access_Constant (Formal_Type)
11098 and then Is_Access_Type (Etype (Default))
11099 and then Is_Access_Constant (Etype (Default))
11100 then
11101 Error_Msg_N
11102 ("formal that is access to variable cannot be initialized " &
11103 "with an access-to-constant expression", Default);
11104 end if;
11105
11106 -- Check that the designated type of an access parameter's default
11107 -- is not a class-wide type unless the parameter's designated type
11108 -- is also class-wide.
11109
11110 if Ekind (Formal_Type) = E_Anonymous_Access_Type
11111 and then not Designates_From_With_Type (Formal_Type)
11112 and then Is_Class_Wide_Default (Default)
11113 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
11114 then
11115 Error_Msg_N
11116 ("access to class-wide expression not allowed here", Default);
11117 end if;
11118
11119 -- Check incorrect use of dynamically tagged expressions
11120
11121 if Is_Tagged_Type (Formal_Type) then
11122 Check_Dynamically_Tagged_Expression
11123 (Expr => Default,
11124 Typ => Formal_Type,
11125 Related_Nod => Default);
11126 end if;
11127 end if;
11128
11129 -- Ada 2005 (AI-231): Static checks
11130
11131 if Ada_Version >= Ada_2005
11132 and then Is_Access_Type (Etype (Formal))
11133 and then Can_Never_Be_Null (Etype (Formal))
11134 then
11135 Null_Exclusion_Static_Checks (Param_Spec);
11136 end if;
11137
11138 <<Continue>>
11139 Next (Param_Spec);
11140 end loop;
11141
11142 -- If this is the formal part of a function specification, analyze the
11143 -- subtype mark in the context where the formals are visible but not
11144 -- yet usable, and may hide outer homographs.
11145
11146 if Nkind (Related_Nod) = N_Function_Specification then
11147 Analyze_Return_Type (Related_Nod);
11148 end if;
11149
11150 -- Now set the kind (mode) of each formal
11151
11152 Param_Spec := First (T);
11153 while Present (Param_Spec) loop
11154 Formal := Defining_Identifier (Param_Spec);
11155 Set_Formal_Mode (Formal);
11156
11157 if Ekind (Formal) = E_In_Parameter then
11158 Set_Default_Value (Formal, Expression (Param_Spec));
11159
11160 if Present (Expression (Param_Spec)) then
11161 Default := Expression (Param_Spec);
11162
11163 if Is_Scalar_Type (Etype (Default)) then
11164 if Nkind (Parameter_Type (Param_Spec)) /=
11165 N_Access_Definition
11166 then
11167 Formal_Type := Entity (Parameter_Type (Param_Spec));
11168 else
11169 Formal_Type :=
11170 Access_Definition
11171 (Related_Nod, Parameter_Type (Param_Spec));
11172 end if;
11173
11174 Apply_Scalar_Range_Check (Default, Formal_Type);
11175 end if;
11176 end if;
11177
11178 elsif Ekind (Formal) = E_Out_Parameter then
11179 Num_Out_Params := Num_Out_Params + 1;
11180
11181 if Num_Out_Params = 1 then
11182 First_Out_Param := Formal;
11183 end if;
11184
11185 elsif Ekind (Formal) = E_In_Out_Parameter then
11186 Num_Out_Params := Num_Out_Params + 1;
11187 end if;
11188
11189 -- Skip remaining processing if formal type was in error
11190
11191 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
11192 goto Next_Parameter;
11193 end if;
11194
11195 -- Force call by reference if aliased
11196
11197 if Is_Aliased (Formal) then
11198 Set_Mechanism (Formal, By_Reference);
11199
11200 -- Warn if user asked this to be passed by copy
11201
11202 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11203 Error_Msg_N
11204 ("cannot pass aliased parameter & by copy?", Formal);
11205 end if;
11206
11207 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
11208
11209 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
11210 Set_Mechanism (Formal, By_Copy);
11211
11212 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
11213 Set_Mechanism (Formal, By_Reference);
11214 end if;
11215
11216 <<Next_Parameter>>
11217 Next (Param_Spec);
11218 end loop;
11219
11220 if Present (First_Out_Param) and then Num_Out_Params = 1 then
11221 Set_Is_Only_Out_Parameter (First_Out_Param);
11222 end if;
11223 end Process_Formals;
11224
11225 ------------------
11226 -- Process_PPCs --
11227 ------------------
11228
11229 procedure Process_PPCs
11230 (N : Node_Id;
11231 Spec_Id : Entity_Id;
11232 Body_Id : Entity_Id)
11233 is
11234 Loc : constant Source_Ptr := Sloc (N);
11235 Prag : Node_Id;
11236 Parms : List_Id;
11237
11238 Designator : Entity_Id;
11239 -- Subprogram designator, set from Spec_Id if present, else Body_Id
11240
11241 Precond : Node_Id := Empty;
11242 -- Set non-Empty if we prepend precondition to the declarations. This
11243 -- is used to hook up inherited preconditions (adding the condition
11244 -- expression with OR ELSE, and adding the message).
11245
11246 Inherited_Precond : Node_Id;
11247 -- Precondition inherited from parent subprogram
11248
11249 Inherited : constant Subprogram_List :=
11250 Inherited_Subprograms (Spec_Id);
11251 -- List of subprograms inherited by this subprogram
11252
11253 Plist : List_Id := No_List;
11254 -- List of generated postconditions
11255
11256 procedure Check_Access_Invariants (E : Entity_Id);
11257 -- If the subprogram returns an access to a type with invariants, or
11258 -- has access parameters whose designated type has an invariant, then
11259 -- under the same visibility conditions as for other invariant checks,
11260 -- the type invariant must be applied to the returned value.
11261
11262 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id);
11263 -- Given pragma Contract_Cases CCs, create the circuitry needed to
11264 -- evaluate case guards and trigger consequence expressions. Subp_Id
11265 -- denotes the related subprogram.
11266
11267 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
11268 -- Prag contains an analyzed precondition or postcondition pragma. This
11269 -- function copies the pragma, changes it to the corresponding Check
11270 -- pragma and returns the Check pragma as the result. If Pspec is non-
11271 -- empty, this is the case of inheriting a PPC, where we must change
11272 -- references to parameters of the inherited subprogram to point to the
11273 -- corresponding parameters of the current subprogram.
11274
11275 procedure Insert_After_Last_Declaration (Nod : Node_Id);
11276 -- Insert node Nod after the last declaration of the context
11277
11278 function Invariants_Or_Predicates_Present return Boolean;
11279 -- Determines if any invariants or predicates are present for any OUT
11280 -- or IN OUT parameters of the subprogram, or (for a function) if the
11281 -- return value has an invariant.
11282
11283 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean;
11284 -- T is the entity for a private type for which invariants are defined.
11285 -- This function returns True if the procedure corresponding to the
11286 -- value of Designator is a public procedure from the point of view of
11287 -- this type (i.e. its spec is in the visible part of the package that
11288 -- contains the declaration of the private type). A True value means
11289 -- that an invariant check is required (for an IN OUT parameter, or
11290 -- the returned value of a function.
11291
11292 -----------------------------
11293 -- Check_Access_Invariants --
11294 -----------------------------
11295
11296 procedure Check_Access_Invariants (E : Entity_Id) is
11297 Call : Node_Id;
11298 Obj : Node_Id;
11299 Typ : Entity_Id;
11300
11301 begin
11302 if Is_Access_Type (Etype (E))
11303 and then not Is_Access_Constant (Etype (E))
11304 then
11305 Typ := Designated_Type (Etype (E));
11306
11307 if Has_Invariants (Typ)
11308 and then Present (Invariant_Procedure (Typ))
11309 and then Is_Public_Subprogram_For (Typ)
11310 then
11311 Obj :=
11312 Make_Explicit_Dereference (Loc,
11313 Prefix => New_Occurrence_Of (E, Loc));
11314 Set_Etype (Obj, Typ);
11315
11316 Call := Make_Invariant_Call (Obj);
11317
11318 Append_To (Plist,
11319 Make_If_Statement (Loc,
11320 Condition =>
11321 Make_Op_Ne (Loc,
11322 Left_Opnd => Make_Null (Loc),
11323 Right_Opnd => New_Occurrence_Of (E, Loc)),
11324 Then_Statements => New_List (Call)));
11325 end if;
11326 end if;
11327 end Check_Access_Invariants;
11328
11329 ---------------------------
11330 -- Expand_Contract_Cases --
11331 ---------------------------
11332
11333 -- Pragma Contract_Cases is expanded in the following manner:
11334
11335 -- subprogram S is
11336 -- Flag_1 : Boolean := False;
11337 -- . . .
11338 -- Flag_N : Boolean := False;
11339 -- Flag_N+1 : Boolean := False; -- when "others" present
11340 -- Count : Natural := 0;
11341
11342 -- <preconditions (if any)>
11343
11344 -- if Case_Guard_1 then
11345 -- Flag_1 := True;
11346 -- Count := Count + 1;
11347 -- end if;
11348 -- . . .
11349 -- if Case_Guard_N then
11350 -- Flag_N := True;
11351 -- Count := Count + 1;
11352 -- end if;
11353
11354 -- if Count = 0 then
11355 -- raise Assertion_Error with "xxx contract cases incomplete";
11356 -- <or>
11357 -- Flag_N+1 := True; -- when "others" present
11358
11359 -- elsif Count > 1 then
11360 -- declare
11361 -- Str0 : constant String :=
11362 -- "contract cases overlap for subprogram ABC";
11363 -- Str1 : constant String :=
11364 -- (if Flag_1 then
11365 -- Str0 & "case guard at xxx evaluates to True"
11366 -- else Str0);
11367 -- StrN : constant String :=
11368 -- (if Flag_N then
11369 -- StrN-1 & "case guard at xxx evaluates to True"
11370 -- else StrN-1);
11371 -- begin
11372 -- raise Assertion_Error with StrN;
11373 -- end;
11374 -- end if;
11375
11376 -- procedure _Postconditions is
11377 -- begin
11378 -- <postconditions (if any)>
11379
11380 -- if Flag_1 and then not Consequence_1 then
11381 -- raise Assertion_Error with "failed contract case at xxx";
11382 -- end if;
11383 -- . . .
11384 -- if Flag_N[+1] and then not Consequence_N[+1] then
11385 -- raise Assertion_Error with "failed contract case at xxx";
11386 -- end if;
11387 -- end _Postconditions;
11388 -- begin
11389 -- . . .
11390 -- end S;
11391
11392 procedure Expand_Contract_Cases (CCs : Node_Id; Subp_Id : Entity_Id) is
11393 Loc : constant Source_Ptr := Sloc (CCs);
11394
11395 procedure Case_Guard_Error
11396 (Decls : List_Id;
11397 Flag : Entity_Id;
11398 Error_Loc : Source_Ptr;
11399 Msg : in out Entity_Id);
11400 -- Given a declarative list Decls, status flag Flag, the location of
11401 -- the error and a string Msg, construct the following check:
11402 -- Msg : constant String :=
11403 -- (if Flag then
11404 -- Msg & "case guard at Error_Loc evaluates to True"
11405 -- else Msg);
11406 -- The resulting code is added to Decls
11407
11408 procedure Consequence_Error
11409 (Checks : in out Node_Id;
11410 Flag : Entity_Id;
11411 Conseq : Node_Id);
11412 -- Given an if statement Checks, status flag Flag and a consequence
11413 -- Conseq, construct the following check:
11414 -- [els]if Flag and then not Conseq then
11415 -- raise Assertion_Error
11416 -- with "failed contract case at Sloc (Conseq)";
11417 -- [end if;]
11418 -- The resulting code is added to Checks
11419
11420 function Declaration_Of (Id : Entity_Id) return Node_Id;
11421 -- Given the entity Id of a boolean flag, generate:
11422 -- Id : Boolean := False;
11423
11424 function Increment (Id : Entity_Id) return Node_Id;
11425 -- Given the entity Id of a numerical variable, generate:
11426 -- Id := Id + 1;
11427
11428 function Set (Id : Entity_Id) return Node_Id;
11429 -- Given the entity Id of a boolean variable, generate:
11430 -- Id := True;
11431
11432 ----------------------
11433 -- Case_Guard_Error --
11434 ----------------------
11435
11436 procedure Case_Guard_Error
11437 (Decls : List_Id;
11438 Flag : Entity_Id;
11439 Error_Loc : Source_Ptr;
11440 Msg : in out Entity_Id)
11441 is
11442 New_Line : constant Character := Character'Val (10);
11443 New_Msg : constant Entity_Id := Make_Temporary (Loc, 'S');
11444
11445 begin
11446 Start_String;
11447 Store_String_Char (New_Line);
11448 Store_String_Chars (" case guard at ");
11449 Store_String_Chars (Build_Location_String (Error_Loc));
11450 Store_String_Chars (" evaluates to True");
11451
11452 -- Generate:
11453 -- New_Msg : constant String :=
11454 -- (if Flag then
11455 -- Msg & "case guard at Error_Loc evaluates to True"
11456 -- else Msg);
11457
11458 Append_To (Decls,
11459 Make_Object_Declaration (Loc,
11460 Defining_Identifier => New_Msg,
11461 Constant_Present => True,
11462 Object_Definition => New_Reference_To (Standard_String, Loc),
11463 Expression =>
11464 Make_If_Expression (Loc,
11465 Expressions => New_List (
11466 New_Reference_To (Flag, Loc),
11467
11468 Make_Op_Concat (Loc,
11469 Left_Opnd => New_Reference_To (Msg, Loc),
11470 Right_Opnd => Make_String_Literal (Loc, End_String)),
11471
11472 New_Reference_To (Msg, Loc)))));
11473
11474 Msg := New_Msg;
11475 end Case_Guard_Error;
11476
11477 -----------------------
11478 -- Consequence_Error --
11479 -----------------------
11480
11481 procedure Consequence_Error
11482 (Checks : in out Node_Id;
11483 Flag : Entity_Id;
11484 Conseq : Node_Id)
11485 is
11486 Cond : Node_Id;
11487 Error : Node_Id;
11488
11489 begin
11490 -- Generate:
11491 -- Flag and then not Conseq
11492
11493 Cond :=
11494 Make_And_Then (Loc,
11495 Left_Opnd => New_Reference_To (Flag, Loc),
11496 Right_Opnd =>
11497 Make_Op_Not (Loc,
11498 Right_Opnd => Relocate_Node (Conseq)));
11499
11500 -- Generate:
11501 -- raise Assertion_Error
11502 -- with "failed contract case at Sloc (Conseq)";
11503
11504 Start_String;
11505 Store_String_Chars ("failed contract case at ");
11506 Store_String_Chars (Build_Location_String (Sloc (Conseq)));
11507
11508 Error :=
11509 Make_Procedure_Call_Statement (Loc,
11510 Name =>
11511 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11512 Parameter_Associations => New_List (
11513 Make_String_Literal (Loc, End_String)));
11514
11515 if No (Checks) then
11516 Checks :=
11517 Make_If_Statement (Loc,
11518 Condition => Cond,
11519 Then_Statements => New_List (Error));
11520
11521 else
11522 if No (Elsif_Parts (Checks)) then
11523 Set_Elsif_Parts (Checks, New_List);
11524 end if;
11525
11526 Append_To (Elsif_Parts (Checks),
11527 Make_Elsif_Part (Loc,
11528 Condition => Cond,
11529 Then_Statements => New_List (Error)));
11530 end if;
11531 end Consequence_Error;
11532
11533 --------------------
11534 -- Declaration_Of --
11535 --------------------
11536
11537 function Declaration_Of (Id : Entity_Id) return Node_Id is
11538 begin
11539 return
11540 Make_Object_Declaration (Loc,
11541 Defining_Identifier => Id,
11542 Object_Definition =>
11543 New_Reference_To (Standard_Boolean, Loc),
11544 Expression =>
11545 New_Reference_To (Standard_False, Loc));
11546 end Declaration_Of;
11547
11548 ---------------
11549 -- Increment --
11550 ---------------
11551
11552 function Increment (Id : Entity_Id) return Node_Id is
11553 begin
11554 return
11555 Make_Assignment_Statement (Loc,
11556 Name => New_Reference_To (Id, Loc),
11557 Expression =>
11558 Make_Op_Add (Loc,
11559 Left_Opnd => New_Reference_To (Id, Loc),
11560 Right_Opnd => Make_Integer_Literal (Loc, 1)));
11561 end Increment;
11562
11563 ---------
11564 -- Set --
11565 ---------
11566
11567 function Set (Id : Entity_Id) return Node_Id is
11568 begin
11569 return
11570 Make_Assignment_Statement (Loc,
11571 Name => New_Reference_To (Id, Loc),
11572 Expression => New_Reference_To (Standard_True, Loc));
11573 end Set;
11574
11575 -- Local variables
11576
11577 Aggr : constant Node_Id :=
11578 Expression (First
11579 (Pragma_Argument_Associations (CCs)));
11580 Decls : constant List_Id := Declarations (N);
11581 Multiple_PCs : constant Boolean :=
11582 List_Length (Component_Associations (Aggr)) > 1;
11583 Case_Guard : Node_Id;
11584 CG_Checks : Node_Id;
11585 CG_Stmts : List_Id;
11586 Conseq : Node_Id;
11587 Conseq_Checks : Node_Id := Empty;
11588 Count : Entity_Id;
11589 Error_Decls : List_Id;
11590 Flag : Entity_Id;
11591 Msg_Str : Entity_Id;
11592 Others_Flag : Entity_Id := Empty;
11593 Post_Case : Node_Id;
11594
11595 -- Start of processing for Expand_Contract_Cases
11596
11597 begin
11598 -- Do nothing if pragma is not enabled. If pragma is disabled, it has
11599 -- already been rewritten as a Null statement.
11600
11601 if Is_Ignored (CCs) then
11602 return;
11603 end if;
11604
11605 -- Create the counter which tracks the number of case guards that
11606 -- evaluate to True.
11607
11608 -- Count : Natural := 0;
11609
11610 Count := Make_Temporary (Loc, 'C');
11611
11612 Prepend_To (Decls,
11613 Make_Object_Declaration (Loc,
11614 Defining_Identifier => Count,
11615 Object_Definition => New_Reference_To (Standard_Natural, Loc),
11616 Expression => Make_Integer_Literal (Loc, 0)));
11617
11618 -- Create the base error message for multiple overlapping case
11619 -- guards.
11620
11621 -- Msg_Str : constant String :=
11622 -- "contract cases overlap for subprogram Subp_Id";
11623
11624 if Multiple_PCs then
11625 Msg_Str := Make_Temporary (Loc, 'S');
11626
11627 Start_String;
11628 Store_String_Chars ("contract cases overlap for subprogram ");
11629 Store_String_Chars (Get_Name_String (Chars (Subp_Id)));
11630
11631 Error_Decls := New_List (
11632 Make_Object_Declaration (Loc,
11633 Defining_Identifier => Msg_Str,
11634 Constant_Present => True,
11635 Object_Definition => New_Reference_To (Standard_String, Loc),
11636 Expression => Make_String_Literal (Loc, End_String)));
11637 end if;
11638
11639 -- Process individual post cases
11640
11641 Post_Case := First (Component_Associations (Aggr));
11642 while Present (Post_Case) loop
11643 Case_Guard := First (Choices (Post_Case));
11644 Conseq := Expression (Post_Case);
11645
11646 -- The "others" choice requires special processing
11647
11648 if Nkind (Case_Guard) = N_Others_Choice then
11649 Others_Flag := Make_Temporary (Loc, 'F');
11650 Prepend_To (Decls, Declaration_Of (Others_Flag));
11651
11652 -- Check possible overlap between a case guard and "others"
11653
11654 if Multiple_PCs then
11655 Case_Guard_Error
11656 (Decls => Error_Decls,
11657 Flag => Others_Flag,
11658 Error_Loc => Sloc (Case_Guard),
11659 Msg => Msg_Str);
11660 end if;
11661
11662 -- Check the corresponding consequence of "others"
11663
11664 Consequence_Error
11665 (Checks => Conseq_Checks,
11666 Flag => Others_Flag,
11667 Conseq => Conseq);
11668
11669 -- Regular post case
11670
11671 else
11672 -- Create the flag which tracks the state of its associated
11673 -- case guard.
11674
11675 Flag := Make_Temporary (Loc, 'F');
11676 Prepend_To (Decls, Declaration_Of (Flag));
11677
11678 -- The flag is set when the case guard is evaluated to True
11679 -- if Case_Guard then
11680 -- Flag := True;
11681 -- Count := Count + 1;
11682 -- end if;
11683
11684 Append_To (Decls,
11685 Make_If_Statement (Loc,
11686 Condition => Relocate_Node (Case_Guard),
11687 Then_Statements => New_List (
11688 Set (Flag),
11689 Increment (Count))));
11690
11691 -- Check whether this case guard overlaps with another case
11692 -- guard.
11693
11694 if Multiple_PCs then
11695 Case_Guard_Error
11696 (Decls => Error_Decls,
11697 Flag => Flag,
11698 Error_Loc => Sloc (Case_Guard),
11699 Msg => Msg_Str);
11700 end if;
11701
11702 -- The corresponding consequence of the case guard which
11703 -- evaluated to True must hold on exit from the subprogram.
11704
11705 Consequence_Error (Conseq_Checks, Flag, Conseq);
11706 end if;
11707
11708 Next (Post_Case);
11709 end loop;
11710
11711 -- Raise Assertion_Error when none of the case guards evaluate to
11712 -- True. The only exception is when we have "others", in which case
11713 -- there is no error because "others" acts as a default True.
11714
11715 -- Generate:
11716 -- Flag := True;
11717
11718 if Present (Others_Flag) then
11719 CG_Stmts := New_List (Set (Others_Flag));
11720
11721 -- Generate:
11722 -- raise Assertion_Error with "xxx contract cases incomplete";
11723
11724 else
11725 Start_String;
11726 Store_String_Chars (Build_Location_String (Loc));
11727 Store_String_Chars (" contract cases incomplete");
11728
11729 CG_Stmts := New_List (
11730 Make_Procedure_Call_Statement (Loc,
11731 Name =>
11732 New_Reference_To (RTE (RE_Raise_Assert_Failure), Loc),
11733 Parameter_Associations => New_List (
11734 Make_String_Literal (Loc, End_String))));
11735 end if;
11736
11737 CG_Checks :=
11738 Make_If_Statement (Loc,
11739 Condition =>
11740 Make_Op_Eq (Loc,
11741 Left_Opnd => New_Reference_To (Count, Loc),
11742 Right_Opnd => Make_Integer_Literal (Loc, 0)),
11743 Then_Statements => CG_Stmts);
11744
11745 -- Detect a possible failure due to several case guards evaluating to
11746 -- True.
11747
11748 -- Generate:
11749 -- elsif Count > 0 then
11750 -- declare
11751 -- <Error_Decls>
11752 -- begin
11753 -- raise Assertion_Error with <Msg_Str>;
11754 -- end if;
11755
11756 if Multiple_PCs then
11757 Set_Elsif_Parts (CG_Checks, New_List (
11758 Make_Elsif_Part (Loc,
11759 Condition =>
11760 Make_Op_Gt (Loc,
11761 Left_Opnd => New_Reference_To (Count, Loc),
11762 Right_Opnd => Make_Integer_Literal (Loc, 1)),
11763
11764 Then_Statements => New_List (
11765 Make_Block_Statement (Loc,
11766 Declarations => Error_Decls,
11767 Handled_Statement_Sequence =>
11768 Make_Handled_Sequence_Of_Statements (Loc,
11769 Statements => New_List (
11770 Make_Procedure_Call_Statement (Loc,
11771 Name =>
11772 New_Reference_To
11773 (RTE (RE_Raise_Assert_Failure), Loc),
11774 Parameter_Associations => New_List (
11775 New_Reference_To (Msg_Str, Loc))))))))));
11776 end if;
11777
11778 Append_To (Decls, CG_Checks);
11779
11780 -- Raise Assertion_Error when the corresponding consequence of a case
11781 -- guard that evaluated to True fails.
11782
11783 if No (Plist) then
11784 Plist := New_List;
11785 end if;
11786
11787 Append_To (Plist, Conseq_Checks);
11788 end Expand_Contract_Cases;
11789
11790 --------------
11791 -- Grab_PPC --
11792 --------------
11793
11794 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
11795 Nam : constant Name_Id := Pragma_Name (Prag);
11796 Map : Elist_Id;
11797 CP : Node_Id;
11798
11799 Ename : Name_Id;
11800 -- Effective name of pragma (maybe Pre/Post rather than Precondition/
11801 -- Postcodition if the pragma came from a Pre/Post aspect). We need
11802 -- the name right when we generate the Check pragma, since we want
11803 -- the right set of check policies to apply.
11804
11805 begin
11806 -- Prepare map if this is the case where we have to map entities of
11807 -- arguments in the overridden subprogram to corresponding entities
11808 -- of the current subprogram.
11809
11810 if No (Pspec) then
11811 Map := No_Elist;
11812
11813 else
11814 declare
11815 PF : Entity_Id;
11816 CF : Entity_Id;
11817
11818 begin
11819 Map := New_Elmt_List;
11820 PF := First_Formal (Pspec);
11821 CF := First_Formal (Designator);
11822 while Present (PF) loop
11823 Append_Elmt (PF, Map);
11824 Append_Elmt (CF, Map);
11825 Next_Formal (PF);
11826 Next_Formal (CF);
11827 end loop;
11828 end;
11829 end if;
11830
11831 -- Now we can copy the tree, doing any required substitutions
11832
11833 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
11834
11835 -- Set Analyzed to false, since we want to reanalyze the check
11836 -- procedure. Note that it is only at the outer level that we
11837 -- do this fiddling, for the spec cases, the already preanalyzed
11838 -- parameters are not affected.
11839
11840 Set_Analyzed (CP, False);
11841
11842 -- We also make sure Comes_From_Source is False for the copy
11843
11844 Set_Comes_From_Source (CP, False);
11845
11846 -- For a postcondition pragma within a generic, preserve the pragma
11847 -- for later expansion. This is also used when an error was detected,
11848 -- thus setting Expander_Active to False.
11849
11850 if Nam = Name_Postcondition
11851 and then not Expander_Active
11852 then
11853 return CP;
11854 end if;
11855
11856 -- Get effective name of aspect
11857
11858 if Present (Corresponding_Aspect (Prag)) then
11859 Ename := Chars (Identifier (Corresponding_Aspect (Prag)));
11860 else
11861 Ename := Nam;
11862 end if;
11863
11864 -- Change copy of pragma into corresponding pragma Check
11865
11866 Prepend_To (Pragma_Argument_Associations (CP),
11867 Make_Pragma_Argument_Association (Sloc (Prag),
11868 Expression => Make_Identifier (Loc, Ename)));
11869 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
11870
11871 -- If this is inherited case and the current message starts with
11872 -- "failed p", we change it to "failed inherited p...".
11873
11874 if Present (Pspec) then
11875 declare
11876 Msg : constant Node_Id :=
11877 Last (Pragma_Argument_Associations (CP));
11878
11879 begin
11880 if Chars (Msg) = Name_Message then
11881 String_To_Name_Buffer (Strval (Expression (Msg)));
11882
11883 if Name_Buffer (1 .. 8) = "failed p" then
11884 Insert_Str_In_Name_Buffer ("inherited ", 8);
11885 Set_Strval
11886 (Expression (Last (Pragma_Argument_Associations (CP))),
11887 String_From_Name_Buffer);
11888 end if;
11889 end if;
11890 end;
11891 end if;
11892
11893 -- Return the check pragma
11894
11895 return CP;
11896 end Grab_PPC;
11897
11898 -----------------------------------
11899 -- Insert_After_Last_Declaration --
11900 -----------------------------------
11901
11902 procedure Insert_After_Last_Declaration (Nod : Node_Id) is
11903 Decls : constant List_Id := Declarations (N);
11904
11905 begin
11906 if No (Decls) then
11907 Set_Declarations (N, New_List (Nod));
11908 else
11909 Append_To (Decls, Nod);
11910 end if;
11911 end Insert_After_Last_Declaration;
11912
11913 --------------------------------------
11914 -- Invariants_Or_Predicates_Present --
11915 --------------------------------------
11916
11917 function Invariants_Or_Predicates_Present return Boolean is
11918 Formal : Entity_Id;
11919
11920 begin
11921 -- Check function return result. If result is an access type there
11922 -- may be invariants on the designated type.
11923
11924 if Ekind (Designator) /= E_Procedure
11925 and then Has_Invariants (Etype (Designator))
11926 then
11927 return True;
11928
11929 elsif Ekind (Designator) /= E_Procedure
11930 and then Is_Access_Type (Etype (Designator))
11931 and then Has_Invariants (Designated_Type (Etype (Designator)))
11932 then
11933 return True;
11934 end if;
11935
11936 -- Check parameters
11937
11938 Formal := First_Formal (Designator);
11939 while Present (Formal) loop
11940 if Ekind (Formal) /= E_In_Parameter
11941 and then (Has_Invariants (Etype (Formal))
11942 or else Present (Predicate_Function (Etype (Formal))))
11943 then
11944 return True;
11945
11946 elsif Is_Access_Type (Etype (Formal))
11947 and then Has_Invariants (Designated_Type (Etype (Formal)))
11948 then
11949 return True;
11950 end if;
11951
11952 Next_Formal (Formal);
11953 end loop;
11954
11955 return False;
11956 end Invariants_Or_Predicates_Present;
11957
11958 ------------------------------
11959 -- Is_Public_Subprogram_For --
11960 ------------------------------
11961
11962 -- The type T is a private type, its declaration is therefore in
11963 -- the list of public declarations of some package. The test for a
11964 -- public subprogram is that its declaration is in this same list
11965 -- of declarations for the same package (note that all the public
11966 -- declarations are in one list, and all the private declarations
11967 -- in another, so this deals with the public/private distinction).
11968
11969 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is
11970 DD : constant Node_Id := Unit_Declaration_Node (Designator);
11971 -- The subprogram declaration for the subprogram in question
11972
11973 TL : constant List_Id :=
11974 Visible_Declarations
11975 (Specification (Unit_Declaration_Node (Scope (T))));
11976 -- The list of declarations containing the private declaration of
11977 -- the type. We know it is a private type, so we know its scope is
11978 -- the package in question, and we know it must be in the visible
11979 -- declarations of this package.
11980
11981 begin
11982 -- If the subprogram declaration is not a list member, it must be
11983 -- an Init_Proc, in which case we want to consider it to be a
11984 -- public subprogram, since we do get initializations to deal with.
11985 -- Other internally generated subprograms are not public.
11986
11987 if not Is_List_Member (DD)
11988 and then Is_Init_Proc (Defining_Entity (DD))
11989 then
11990 return True;
11991
11992 -- The declaration may have been generated for an expression function
11993 -- so check whether that function comes from source.
11994
11995 elsif not Comes_From_Source (DD)
11996 and then
11997 (Nkind (Original_Node (DD)) /= N_Expression_Function
11998 or else not Comes_From_Source (Defining_Entity (DD)))
11999 then
12000 return False;
12001
12002 -- Otherwise we test whether the subprogram is declared in the
12003 -- visible declarations of the package containing the type.
12004
12005 else
12006 return TL = List_Containing (DD);
12007 end if;
12008 end Is_Public_Subprogram_For;
12009
12010 -- Start of processing for Process_PPCs
12011
12012 begin
12013 -- Capture designator from spec if present, else from body
12014
12015 if Present (Spec_Id) then
12016 Designator := Spec_Id;
12017 else
12018 Designator := Body_Id;
12019 end if;
12020
12021 -- Internally generated subprograms, such as type-specific functions,
12022 -- don't get assertion checks.
12023
12024 if Get_TSS_Name (Designator) /= TSS_Null then
12025 return;
12026 end if;
12027
12028 -- Grab preconditions from spec
12029
12030 if Present (Spec_Id) then
12031
12032 -- Loop through PPC pragmas from spec. Note that preconditions from
12033 -- the body will be analyzed and converted when we scan the body
12034 -- declarations below.
12035
12036 Prag := Spec_PPC_List (Contract (Spec_Id));
12037 while Present (Prag) loop
12038 if Pragma_Name (Prag) = Name_Precondition then
12039
12040 -- For Pre (or Precondition pragma), we simply prepend the
12041 -- pragma to the list of declarations right away so that it
12042 -- will be executed at the start of the procedure. Note that
12043 -- this processing reverses the order of the list, which is
12044 -- what we want since new entries were chained to the head of
12045 -- the list. There can be more than one precondition when we
12046 -- use pragma Precondition.
12047
12048 if not Class_Present (Prag) then
12049 Prepend (Grab_PPC, Declarations (N));
12050
12051 -- For Pre'Class there can only be one pragma, and we save
12052 -- it in Precond for now. We will add inherited Pre'Class
12053 -- stuff before inserting this pragma in the declarations.
12054 else
12055 Precond := Grab_PPC;
12056 end if;
12057 end if;
12058
12059 Prag := Next_Pragma (Prag);
12060 end loop;
12061
12062 -- Now deal with inherited preconditions
12063
12064 for J in Inherited'Range loop
12065 Prag := Spec_PPC_List (Contract (Inherited (J)));
12066
12067 while Present (Prag) loop
12068 if Pragma_Name (Prag) = Name_Precondition
12069 and then Class_Present (Prag)
12070 then
12071 Inherited_Precond := Grab_PPC (Inherited (J));
12072
12073 -- No precondition so far, so establish this as the first
12074
12075 if No (Precond) then
12076 Precond := Inherited_Precond;
12077
12078 -- Here we already have a precondition, add inherited one
12079
12080 else
12081 -- Add new precondition to old one using OR ELSE
12082
12083 declare
12084 New_Expr : constant Node_Id :=
12085 Get_Pragma_Arg
12086 (Next
12087 (First
12088 (Pragma_Argument_Associations
12089 (Inherited_Precond))));
12090 Old_Expr : constant Node_Id :=
12091 Get_Pragma_Arg
12092 (Next
12093 (First
12094 (Pragma_Argument_Associations
12095 (Precond))));
12096
12097 begin
12098 if Paren_Count (Old_Expr) = 0 then
12099 Set_Paren_Count (Old_Expr, 1);
12100 end if;
12101
12102 if Paren_Count (New_Expr) = 0 then
12103 Set_Paren_Count (New_Expr, 1);
12104 end if;
12105
12106 Rewrite (Old_Expr,
12107 Make_Or_Else (Sloc (Old_Expr),
12108 Left_Opnd => Relocate_Node (Old_Expr),
12109 Right_Opnd => New_Expr));
12110 end;
12111
12112 -- Add new message in the form:
12113
12114 -- failed precondition from bla
12115 -- also failed inherited precondition from bla
12116 -- ...
12117
12118 -- Skip this if exception locations are suppressed
12119
12120 if not Exception_Locations_Suppressed then
12121 declare
12122 New_Msg : constant Node_Id :=
12123 Get_Pragma_Arg
12124 (Last
12125 (Pragma_Argument_Associations
12126 (Inherited_Precond)));
12127 Old_Msg : constant Node_Id :=
12128 Get_Pragma_Arg
12129 (Last
12130 (Pragma_Argument_Associations
12131 (Precond)));
12132 begin
12133 Start_String (Strval (Old_Msg));
12134 Store_String_Chars (ASCII.LF & " also ");
12135 Store_String_Chars (Strval (New_Msg));
12136 Set_Strval (Old_Msg, End_String);
12137 end;
12138 end if;
12139 end if;
12140 end if;
12141
12142 Prag := Next_Pragma (Prag);
12143 end loop;
12144 end loop;
12145
12146 -- If we have built a precondition for Pre'Class (including any
12147 -- Pre'Class aspects inherited from parent subprograms), then we
12148 -- insert this composite precondition at this stage.
12149
12150 if Present (Precond) then
12151 Prepend (Precond, Declarations (N));
12152 end if;
12153 end if;
12154
12155 -- Build postconditions procedure if needed and prepend the following
12156 -- declaration to the start of the declarations for the subprogram.
12157
12158 -- procedure _postconditions [(_Result : resulttype)] is
12159 -- begin
12160 -- pragma Check (Postcondition, condition [,message]);
12161 -- pragma Check (Postcondition, condition [,message]);
12162 -- ...
12163 -- Invariant_Procedure (_Result) ...
12164 -- Invariant_Procedure (Arg1)
12165 -- ...
12166 -- end;
12167
12168 -- First we deal with the postconditions in the body
12169
12170 if Is_Non_Empty_List (Declarations (N)) then
12171
12172 -- Loop through declarations
12173
12174 Prag := First (Declarations (N));
12175 while Present (Prag) loop
12176 if Nkind (Prag) = N_Pragma then
12177 Check_Applicable_Policy (Prag);
12178
12179 -- If pragma, capture if postconditions enabled, else ignore
12180
12181 if Pragma_Name (Prag) = Name_Postcondition
12182 and then not Is_Ignored (Prag)
12183 then
12184 if Plist = No_List then
12185 Plist := Empty_List;
12186 end if;
12187
12188 Analyze (Prag);
12189
12190 -- If expansion is disabled, as in a generic unit, save
12191 -- pragma for later expansion.
12192
12193 if not Expander_Active then
12194 Prepend (Grab_PPC, Declarations (N));
12195 else
12196 Append (Grab_PPC, Plist);
12197 end if;
12198 end if;
12199
12200 Next (Prag);
12201
12202 -- Not a pragma, if comes from source, then end scan
12203
12204 elsif Comes_From_Source (Prag) then
12205 exit;
12206
12207 -- Skip stuff not coming from source
12208
12209 else
12210 Next (Prag);
12211 end if;
12212 end loop;
12213 end if;
12214
12215 -- Now deal with any postconditions from the spec
12216
12217 if Present (Spec_Id) then
12218 Spec_Postconditions : declare
12219 procedure Process_Contract_Cases (Spec : Node_Id);
12220 -- This processes the Spec_CTC_List from Spec, processing any
12221 -- contract-cases from the list. The caller has checked that
12222 -- Spec_CTC_List is non-Empty.
12223
12224 procedure Process_Post_Conditions
12225 (Spec : Node_Id;
12226 Class : Boolean);
12227 -- This processes the Spec_PPC_List from Spec, processing any
12228 -- postconditions from the list. If Class is True, then only
12229 -- postconditions marked with Class_Present are considered.
12230 -- The caller has checked that Spec_PPC_List is non-Empty.
12231
12232 ----------------------------
12233 -- Process_Contract_Cases --
12234 ----------------------------
12235
12236 procedure Process_Contract_Cases (Spec : Node_Id) is
12237 begin
12238 -- Loop through Contract_Cases pragmas from spec
12239
12240 Prag := Spec_CTC_List (Contract (Spec));
12241 loop
12242 if Pragma_Name (Prag) = Name_Contract_Cases then
12243 Expand_Contract_Cases (Prag, Spec_Id);
12244 end if;
12245
12246 Prag := Next_Pragma (Prag);
12247 exit when No (Prag);
12248 end loop;
12249 end Process_Contract_Cases;
12250
12251 -----------------------------
12252 -- Process_Post_Conditions --
12253 -----------------------------
12254
12255 procedure Process_Post_Conditions
12256 (Spec : Node_Id;
12257 Class : Boolean)
12258 is
12259 Pspec : Node_Id;
12260
12261 begin
12262 if Class then
12263 Pspec := Spec;
12264 else
12265 Pspec := Empty;
12266 end if;
12267
12268 -- Loop through PPC pragmas from spec
12269
12270 Prag := Spec_PPC_List (Contract (Spec));
12271 loop
12272 if Pragma_Name (Prag) = Name_Postcondition
12273 and then (not Class or else Class_Present (Prag))
12274 then
12275 if Plist = No_List then
12276 Plist := Empty_List;
12277 end if;
12278
12279 if not Expander_Active then
12280 Prepend (Grab_PPC (Pspec), Declarations (N));
12281 else
12282 Append (Grab_PPC (Pspec), Plist);
12283 end if;
12284 end if;
12285
12286 Prag := Next_Pragma (Prag);
12287 exit when No (Prag);
12288 end loop;
12289 end Process_Post_Conditions;
12290
12291 -- Start of processing for Spec_Postconditions
12292
12293 begin
12294 -- Process postconditions expressed as contract-cases
12295
12296 if Present (Spec_CTC_List (Contract (Spec_Id))) then
12297 Process_Contract_Cases (Spec_Id);
12298 end if;
12299
12300 -- Process spec postconditions
12301
12302 if Present (Spec_PPC_List (Contract (Spec_Id))) then
12303 Process_Post_Conditions (Spec_Id, Class => False);
12304 end if;
12305
12306 -- Process inherited postconditions
12307
12308 for J in Inherited'Range loop
12309 if Present (Spec_PPC_List (Contract (Inherited (J)))) then
12310 Process_Post_Conditions (Inherited (J), Class => True);
12311 end if;
12312 end loop;
12313 end Spec_Postconditions;
12314 end if;
12315
12316 -- If we had any postconditions and expansion is enabled, or if the
12317 -- subprogram has invariants, then build the _Postconditions procedure.
12318
12319 if (Present (Plist) or else Invariants_Or_Predicates_Present)
12320 and then Expander_Active
12321 then
12322 if No (Plist) then
12323 Plist := Empty_List;
12324 end if;
12325
12326 -- Special processing for function return
12327
12328 if Ekind (Designator) /= E_Procedure then
12329 declare
12330 Rent : constant Entity_Id :=
12331 Make_Defining_Identifier (Loc, Name_uResult);
12332 Ftyp : constant Entity_Id := Etype (Designator);
12333
12334 begin
12335 Set_Etype (Rent, Ftyp);
12336
12337 -- Add argument for return
12338
12339 Parms :=
12340 New_List (
12341 Make_Parameter_Specification (Loc,
12342 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
12343 Defining_Identifier => Rent));
12344
12345 -- Add invariant call if returning type with invariants and
12346 -- this is a public function, i.e. a function declared in the
12347 -- visible part of the package defining the private type.
12348
12349 if Has_Invariants (Etype (Rent))
12350 and then Present (Invariant_Procedure (Etype (Rent)))
12351 and then Is_Public_Subprogram_For (Etype (Rent))
12352 then
12353 Append_To (Plist,
12354 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
12355 end if;
12356
12357 -- Same if return value is an access to type with invariants
12358
12359 Check_Access_Invariants (Rent);
12360 end;
12361
12362 -- Procedure rather than a function
12363
12364 else
12365 Parms := No_List;
12366 end if;
12367
12368 -- Add invariant calls and predicate calls for parameters. Note that
12369 -- this is done for functions as well, since in Ada 2012 they can
12370 -- have IN OUT args.
12371
12372 declare
12373 Formal : Entity_Id;
12374 Ftype : Entity_Id;
12375
12376 begin
12377 Formal := First_Formal (Designator);
12378 while Present (Formal) loop
12379 if Ekind (Formal) /= E_In_Parameter
12380 or else Is_Access_Type (Etype (Formal))
12381 then
12382 Ftype := Etype (Formal);
12383
12384 if Has_Invariants (Ftype)
12385 and then Present (Invariant_Procedure (Ftype))
12386 and then Is_Public_Subprogram_For (Ftype)
12387 then
12388 Append_To (Plist,
12389 Make_Invariant_Call
12390 (New_Occurrence_Of (Formal, Loc)));
12391 end if;
12392
12393 Check_Access_Invariants (Formal);
12394
12395 if Present (Predicate_Function (Ftype)) then
12396 Append_To (Plist,
12397 Make_Predicate_Check
12398 (Ftype, New_Occurrence_Of (Formal, Loc)));
12399 end if;
12400 end if;
12401
12402 Next_Formal (Formal);
12403 end loop;
12404 end;
12405
12406 -- Build and insert postcondition procedure
12407
12408 declare
12409 Post_Proc : constant Entity_Id :=
12410 Make_Defining_Identifier (Loc,
12411 Chars => Name_uPostconditions);
12412 -- The entity for the _Postconditions procedure
12413
12414 begin
12415 -- Insert the corresponding body of a post condition pragma after
12416 -- the last declaration of the context. This ensures that the body
12417 -- will not cause any premature freezing as it may mention types:
12418
12419 -- procedure Proc (Obj : Array_Typ) is
12420 -- procedure _postconditions is
12421 -- begin
12422 -- ... Obj ...
12423 -- end _postconditions;
12424
12425 -- subtype T is Array_Typ (Obj'First (1) .. Obj'Last (1));
12426 -- begin
12427
12428 -- In the example above, Obj is of type T but the incorrect
12429 -- placement of _postconditions will cause a crash in gigi due to
12430 -- an out of order reference. The body of _postconditions must be
12431 -- placed after the declaration of Temp to preserve correct
12432 -- visibility.
12433
12434 Insert_After_Last_Declaration (
12435 Make_Subprogram_Body (Loc,
12436 Specification =>
12437 Make_Procedure_Specification (Loc,
12438 Defining_Unit_Name => Post_Proc,
12439 Parameter_Specifications => Parms),
12440
12441 Declarations => Empty_List,
12442
12443 Handled_Statement_Sequence =>
12444 Make_Handled_Sequence_Of_Statements (Loc,
12445 Statements => Plist)));
12446
12447 Set_Ekind (Post_Proc, E_Procedure);
12448
12449 -- If this is a procedure, set the Postcondition_Proc attribute on
12450 -- the proper defining entity for the subprogram.
12451
12452 if Ekind (Designator) = E_Procedure then
12453 Set_Postcondition_Proc (Designator, Post_Proc);
12454 end if;
12455 end;
12456
12457 Set_Has_Postconditions (Designator);
12458 end if;
12459 end Process_PPCs;
12460
12461 ----------------------------
12462 -- Reference_Body_Formals --
12463 ----------------------------
12464
12465 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12466 Fs : Entity_Id;
12467 Fb : Entity_Id;
12468
12469 begin
12470 if Error_Posted (Spec) then
12471 return;
12472 end if;
12473
12474 -- Iterate over both lists. They may be of different lengths if the two
12475 -- specs are not conformant.
12476
12477 Fs := First_Formal (Spec);
12478 Fb := First_Formal (Bod);
12479 while Present (Fs) and then Present (Fb) loop
12480 Generate_Reference (Fs, Fb, 'b');
12481
12482 if Style_Check then
12483 Style.Check_Identifier (Fb, Fs);
12484 end if;
12485
12486 Set_Spec_Entity (Fb, Fs);
12487 Set_Referenced (Fs, False);
12488 Next_Formal (Fs);
12489 Next_Formal (Fb);
12490 end loop;
12491 end Reference_Body_Formals;
12492
12493 -------------------------
12494 -- Set_Actual_Subtypes --
12495 -------------------------
12496
12497 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12498 Decl : Node_Id;
12499 Formal : Entity_Id;
12500 T : Entity_Id;
12501 First_Stmt : Node_Id := Empty;
12502 AS_Needed : Boolean;
12503
12504 begin
12505 -- If this is an empty initialization procedure, no need to create
12506 -- actual subtypes (small optimization).
12507
12508 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12509 return;
12510 end if;
12511
12512 Formal := First_Formal (Subp);
12513 while Present (Formal) loop
12514 T := Etype (Formal);
12515
12516 -- We never need an actual subtype for a constrained formal
12517
12518 if Is_Constrained (T) then
12519 AS_Needed := False;
12520
12521 -- If we have unknown discriminants, then we do not need an actual
12522 -- subtype, or more accurately we cannot figure it out! Note that
12523 -- all class-wide types have unknown discriminants.
12524
12525 elsif Has_Unknown_Discriminants (T) then
12526 AS_Needed := False;
12527
12528 -- At this stage we have an unconstrained type that may need an
12529 -- actual subtype. For sure the actual subtype is needed if we have
12530 -- an unconstrained array type.
12531
12532 elsif Is_Array_Type (T) then
12533 AS_Needed := True;
12534
12535 -- The only other case needing an actual subtype is an unconstrained
12536 -- record type which is an IN parameter (we cannot generate actual
12537 -- subtypes for the OUT or IN OUT case, since an assignment can
12538 -- change the discriminant values. However we exclude the case of
12539 -- initialization procedures, since discriminants are handled very
12540 -- specially in this context, see the section entitled "Handling of
12541 -- Discriminants" in Einfo.
12542
12543 -- We also exclude the case of Discrim_SO_Functions (functions used
12544 -- in front end layout mode for size/offset values), since in such
12545 -- functions only discriminants are referenced, and not only are such
12546 -- subtypes not needed, but they cannot always be generated, because
12547 -- of order of elaboration issues.
12548
12549 elsif Is_Record_Type (T)
12550 and then Ekind (Formal) = E_In_Parameter
12551 and then Chars (Formal) /= Name_uInit
12552 and then not Is_Unchecked_Union (T)
12553 and then not Is_Discrim_SO_Function (Subp)
12554 then
12555 AS_Needed := True;
12556
12557 -- All other cases do not need an actual subtype
12558
12559 else
12560 AS_Needed := False;
12561 end if;
12562
12563 -- Generate actual subtypes for unconstrained arrays and
12564 -- unconstrained discriminated records.
12565
12566 if AS_Needed then
12567 if Nkind (N) = N_Accept_Statement then
12568
12569 -- If expansion is active, the formal is replaced by a local
12570 -- variable that renames the corresponding entry of the
12571 -- parameter block, and it is this local variable that may
12572 -- require an actual subtype.
12573
12574 if Full_Expander_Active then
12575 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12576 else
12577 Decl := Build_Actual_Subtype (T, Formal);
12578 end if;
12579
12580 if Present (Handled_Statement_Sequence (N)) then
12581 First_Stmt :=
12582 First (Statements (Handled_Statement_Sequence (N)));
12583 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12584 Mark_Rewrite_Insertion (Decl);
12585 else
12586 -- If the accept statement has no body, there will be no
12587 -- reference to the actuals, so no need to compute actual
12588 -- subtypes.
12589
12590 return;
12591 end if;
12592
12593 else
12594 Decl := Build_Actual_Subtype (T, Formal);
12595 Prepend (Decl, Declarations (N));
12596 Mark_Rewrite_Insertion (Decl);
12597 end if;
12598
12599 -- The declaration uses the bounds of an existing object, and
12600 -- therefore needs no constraint checks.
12601
12602 Analyze (Decl, Suppress => All_Checks);
12603
12604 -- We need to freeze manually the generated type when it is
12605 -- inserted anywhere else than in a declarative part.
12606
12607 if Present (First_Stmt) then
12608 Insert_List_Before_And_Analyze (First_Stmt,
12609 Freeze_Entity (Defining_Identifier (Decl), N));
12610 end if;
12611
12612 if Nkind (N) = N_Accept_Statement
12613 and then Full_Expander_Active
12614 then
12615 Set_Actual_Subtype (Renamed_Object (Formal),
12616 Defining_Identifier (Decl));
12617 else
12618 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12619 end if;
12620 end if;
12621
12622 Next_Formal (Formal);
12623 end loop;
12624 end Set_Actual_Subtypes;
12625
12626 ---------------------
12627 -- Set_Formal_Mode --
12628 ---------------------
12629
12630 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12631 Spec : constant Node_Id := Parent (Formal_Id);
12632
12633 begin
12634 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12635 -- since we ensure that corresponding actuals are always valid at the
12636 -- point of the call.
12637
12638 if Out_Present (Spec) then
12639 if Ekind (Scope (Formal_Id)) = E_Function
12640 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
12641 then
12642 -- [IN] OUT parameters allowed for functions in Ada 2012
12643
12644 if Ada_Version >= Ada_2012 then
12645
12646 -- Even in Ada 2012 operators can only have IN parameters
12647
12648 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12649 Error_Msg_N ("operators can only have IN parameters", Spec);
12650 end if;
12651
12652 if In_Present (Spec) then
12653 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12654 else
12655 Set_Ekind (Formal_Id, E_Out_Parameter);
12656 end if;
12657
12658 -- But not in earlier versions of Ada
12659
12660 else
12661 Error_Msg_N ("functions can only have IN parameters", Spec);
12662 Set_Ekind (Formal_Id, E_In_Parameter);
12663 end if;
12664
12665 elsif In_Present (Spec) then
12666 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12667
12668 else
12669 Set_Ekind (Formal_Id, E_Out_Parameter);
12670 Set_Never_Set_In_Source (Formal_Id, True);
12671 Set_Is_True_Constant (Formal_Id, False);
12672 Set_Current_Value (Formal_Id, Empty);
12673 end if;
12674
12675 else
12676 Set_Ekind (Formal_Id, E_In_Parameter);
12677 end if;
12678
12679 -- Set Is_Known_Non_Null for access parameters since the language
12680 -- guarantees that access parameters are always non-null. We also set
12681 -- Can_Never_Be_Null, since there is no way to change the value.
12682
12683 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12684
12685 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12686 -- null; In Ada 2005, only if then null_exclusion is explicit.
12687
12688 if Ada_Version < Ada_2005
12689 or else Can_Never_Be_Null (Etype (Formal_Id))
12690 then
12691 Set_Is_Known_Non_Null (Formal_Id);
12692 Set_Can_Never_Be_Null (Formal_Id);
12693 end if;
12694
12695 -- Ada 2005 (AI-231): Null-exclusion access subtype
12696
12697 elsif Is_Access_Type (Etype (Formal_Id))
12698 and then Can_Never_Be_Null (Etype (Formal_Id))
12699 then
12700 Set_Is_Known_Non_Null (Formal_Id);
12701
12702 -- We can also set Can_Never_Be_Null (thus preventing some junk
12703 -- access checks) for the case of an IN parameter, which cannot
12704 -- be changed, or for an IN OUT parameter, which can be changed but
12705 -- not to a null value. But for an OUT parameter, the initial value
12706 -- passed in can be null, so we can't set this flag in that case.
12707
12708 if Ekind (Formal_Id) /= E_Out_Parameter then
12709 Set_Can_Never_Be_Null (Formal_Id);
12710 end if;
12711 end if;
12712
12713 Set_Mechanism (Formal_Id, Default_Mechanism);
12714 Set_Formal_Validity (Formal_Id);
12715 end Set_Formal_Mode;
12716
12717 -------------------------
12718 -- Set_Formal_Validity --
12719 -------------------------
12720
12721 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12722 begin
12723 -- If no validity checking, then we cannot assume anything about the
12724 -- validity of parameters, since we do not know there is any checking
12725 -- of the validity on the call side.
12726
12727 if not Validity_Checks_On then
12728 return;
12729
12730 -- If validity checking for parameters is enabled, this means we are
12731 -- not supposed to make any assumptions about argument values.
12732
12733 elsif Validity_Check_Parameters then
12734 return;
12735
12736 -- If we are checking in parameters, we will assume that the caller is
12737 -- also checking parameters, so we can assume the parameter is valid.
12738
12739 elsif Ekind (Formal_Id) = E_In_Parameter
12740 and then Validity_Check_In_Params
12741 then
12742 Set_Is_Known_Valid (Formal_Id, True);
12743
12744 -- Similar treatment for IN OUT parameters
12745
12746 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12747 and then Validity_Check_In_Out_Params
12748 then
12749 Set_Is_Known_Valid (Formal_Id, True);
12750 end if;
12751 end Set_Formal_Validity;
12752
12753 ------------------------
12754 -- Subtype_Conformant --
12755 ------------------------
12756
12757 function Subtype_Conformant
12758 (New_Id : Entity_Id;
12759 Old_Id : Entity_Id;
12760 Skip_Controlling_Formals : Boolean := False) return Boolean
12761 is
12762 Result : Boolean;
12763 begin
12764 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12765 Skip_Controlling_Formals => Skip_Controlling_Formals);
12766 return Result;
12767 end Subtype_Conformant;
12768
12769 ---------------------
12770 -- Type_Conformant --
12771 ---------------------
12772
12773 function Type_Conformant
12774 (New_Id : Entity_Id;
12775 Old_Id : Entity_Id;
12776 Skip_Controlling_Formals : Boolean := False) return Boolean
12777 is
12778 Result : Boolean;
12779 begin
12780 May_Hide_Profile := False;
12781
12782 Check_Conformance
12783 (New_Id, Old_Id, Type_Conformant, False, Result,
12784 Skip_Controlling_Formals => Skip_Controlling_Formals);
12785 return Result;
12786 end Type_Conformant;
12787
12788 -------------------------------
12789 -- Valid_Operator_Definition --
12790 -------------------------------
12791
12792 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12793 N : Integer := 0;
12794 F : Entity_Id;
12795 Id : constant Name_Id := Chars (Designator);
12796 N_OK : Boolean;
12797
12798 begin
12799 F := First_Formal (Designator);
12800 while Present (F) loop
12801 N := N + 1;
12802
12803 if Present (Default_Value (F)) then
12804 Error_Msg_N
12805 ("default values not allowed for operator parameters",
12806 Parent (F));
12807 end if;
12808
12809 Next_Formal (F);
12810 end loop;
12811
12812 -- Verify that user-defined operators have proper number of arguments
12813 -- First case of operators which can only be unary
12814
12815 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12816 N_OK := (N = 1);
12817
12818 -- Case of operators which can be unary or binary
12819
12820 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12821 N_OK := (N in 1 .. 2);
12822
12823 -- All other operators can only be binary
12824
12825 else
12826 N_OK := (N = 2);
12827 end if;
12828
12829 if not N_OK then
12830 Error_Msg_N
12831 ("incorrect number of arguments for operator", Designator);
12832 end if;
12833
12834 if Id = Name_Op_Ne
12835 and then Base_Type (Etype (Designator)) = Standard_Boolean
12836 and then not Is_Intrinsic_Subprogram (Designator)
12837 then
12838 Error_Msg_N
12839 ("explicit definition of inequality not allowed", Designator);
12840 end if;
12841 end Valid_Operator_Definition;
12842
12843 end Sem_Ch6;