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