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