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