Update comments.
[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 -- For functions whose result type has controlled parts, we have
5656 -- an extra formal of type System.Finalization_Implementation.
5657 -- Finalizable_Ptr_Ptr. That is, we are passing a pointer to a
5658 -- finalization list (which is itself a pointer). This extra
5659 -- formal is then passed along to Move_Final_List in case of
5660 -- successful completion of a return statement. We cannot pass an
5661 -- 'in out' parameter, because we need to update the finalization
5662 -- list during an abort-deferred region, rather than using
5663 -- copy-back after the function returns. This is true even if we
5664 -- are able to get away with having 'in out' parameters, which are
5665 -- normally illegal for functions. This formal is also needed when
5666 -- the function has a tagged result.
5667
5668 if Needs_BIP_Final_List (E) then
5669 Discard :=
5670 Add_Extra_Formal
5671 (E, RTE (RE_Finalizable_Ptr_Ptr),
5672 E, BIP_Formal_Suffix (BIP_Final_List));
5673 end if;
5674
5675 -- If the result type contains tasks, we have two extra formals:
5676 -- the master of the tasks to be created, and the caller's
5677 -- activation chain.
5678
5679 if Has_Task (Result_Subt) then
5680 Discard :=
5681 Add_Extra_Formal
5682 (E, RTE (RE_Master_Id),
5683 E, BIP_Formal_Suffix (BIP_Master));
5684 Discard :=
5685 Add_Extra_Formal
5686 (E, RTE (RE_Activation_Chain_Access),
5687 E, BIP_Formal_Suffix (BIP_Activation_Chain));
5688 end if;
5689
5690 -- All build-in-place functions get an extra formal that will be
5691 -- passed the address of the return object within the caller.
5692
5693 declare
5694 Formal_Type : constant Entity_Id :=
5695 Create_Itype
5696 (E_Anonymous_Access_Type, E,
5697 Scope_Id => Scope (E));
5698 begin
5699 Set_Directly_Designated_Type (Formal_Type, Result_Subt);
5700 Set_Etype (Formal_Type, Formal_Type);
5701 Set_Depends_On_Private
5702 (Formal_Type, Has_Private_Component (Formal_Type));
5703 Set_Is_Public (Formal_Type, Is_Public (Scope (Formal_Type)));
5704 Set_Is_Access_Constant (Formal_Type, False);
5705
5706 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
5707 -- the designated type comes from the limited view (for
5708 -- back-end purposes).
5709
5710 Set_From_With_Type (Formal_Type, From_With_Type (Result_Subt));
5711
5712 Layout_Type (Formal_Type);
5713
5714 Discard :=
5715 Add_Extra_Formal
5716 (E, Formal_Type, E, BIP_Formal_Suffix (BIP_Object_Access));
5717 end;
5718 end;
5719 end if;
5720 end Create_Extra_Formals;
5721
5722 -----------------------------
5723 -- Enter_Overloaded_Entity --
5724 -----------------------------
5725
5726 procedure Enter_Overloaded_Entity (S : Entity_Id) is
5727 E : Entity_Id := Current_Entity_In_Scope (S);
5728 C_E : Entity_Id := Current_Entity (S);
5729
5730 begin
5731 if Present (E) then
5732 Set_Has_Homonym (E);
5733 Set_Has_Homonym (S);
5734 end if;
5735
5736 Set_Is_Immediately_Visible (S);
5737 Set_Scope (S, Current_Scope);
5738
5739 -- Chain new entity if front of homonym in current scope, so that
5740 -- homonyms are contiguous.
5741
5742 if Present (E)
5743 and then E /= C_E
5744 then
5745 while Homonym (C_E) /= E loop
5746 C_E := Homonym (C_E);
5747 end loop;
5748
5749 Set_Homonym (C_E, S);
5750
5751 else
5752 E := C_E;
5753 Set_Current_Entity (S);
5754 end if;
5755
5756 Set_Homonym (S, E);
5757
5758 Append_Entity (S, Current_Scope);
5759 Set_Public_Status (S);
5760
5761 if Debug_Flag_E then
5762 Write_Str ("New overloaded entity chain: ");
5763 Write_Name (Chars (S));
5764
5765 E := S;
5766 while Present (E) loop
5767 Write_Str (" "); Write_Int (Int (E));
5768 E := Homonym (E);
5769 end loop;
5770
5771 Write_Eol;
5772 end if;
5773
5774 -- Generate warning for hiding
5775
5776 if Warn_On_Hiding
5777 and then Comes_From_Source (S)
5778 and then In_Extended_Main_Source_Unit (S)
5779 then
5780 E := S;
5781 loop
5782 E := Homonym (E);
5783 exit when No (E);
5784
5785 -- Warn unless genuine overloading
5786
5787 if (not Is_Overloadable (E) or else Subtype_Conformant (E, S))
5788 and then (Is_Immediately_Visible (E)
5789 or else
5790 Is_Potentially_Use_Visible (S))
5791 then
5792 Error_Msg_Sloc := Sloc (E);
5793 Error_Msg_N ("declaration of & hides one#?", S);
5794 end if;
5795 end loop;
5796 end if;
5797 end Enter_Overloaded_Entity;
5798
5799 -----------------------------
5800 -- Check_Untagged_Equality --
5801 -----------------------------
5802
5803 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
5804 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
5805 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
5806 Obj_Decl : Node_Id;
5807
5808 begin
5809 if Nkind (Decl) = N_Subprogram_Declaration
5810 and then Is_Record_Type (Typ)
5811 and then not Is_Tagged_Type (Typ)
5812 then
5813 if Is_Frozen (Typ) then
5814 Error_Msg_NE
5815 ("equality operator must be declared "
5816 & "before type& is frozen", Eq_Op, Typ);
5817
5818 Obj_Decl := Next (Parent (Typ));
5819 while Present (Obj_Decl)
5820 and then Obj_Decl /= Decl
5821 loop
5822 if Nkind (Obj_Decl) = N_Object_Declaration
5823 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
5824 then
5825 Error_Msg_NE ("type& is frozen by declaration?",
5826 Obj_Decl, Typ);
5827 Error_Msg_N
5828 ("\an equality operator cannot be declared after this "
5829 & "point ('R'M 4.5.2 (9.8)) (Ada2012))?", Obj_Decl);
5830 exit;
5831 end if;
5832
5833 Next (Obj_Decl);
5834 end loop;
5835
5836 elsif not In_Same_List (Parent (Typ), Decl)
5837 and then not Is_Limited_Type (Typ)
5838 then
5839 Error_Msg_N ("equality operator appears too late", Eq_Op);
5840 end if;
5841 end if;
5842 end Check_Untagged_Equality;
5843
5844 -----------------------------
5845 -- Find_Corresponding_Spec --
5846 -----------------------------
5847
5848 function Find_Corresponding_Spec
5849 (N : Node_Id;
5850 Post_Error : Boolean := True) return Entity_Id
5851 is
5852 Spec : constant Node_Id := Specification (N);
5853 Designator : constant Entity_Id := Defining_Entity (Spec);
5854
5855 E : Entity_Id;
5856
5857 begin
5858 E := Current_Entity (Designator);
5859 while Present (E) loop
5860
5861 -- We are looking for a matching spec. It must have the same scope,
5862 -- and the same name, and either be type conformant, or be the case
5863 -- of a library procedure spec and its body (which belong to one
5864 -- another regardless of whether they are type conformant or not).
5865
5866 if Scope (E) = Current_Scope then
5867 if Current_Scope = Standard_Standard
5868 or else (Ekind (E) = Ekind (Designator)
5869 and then Type_Conformant (E, Designator))
5870 then
5871 -- Within an instantiation, we know that spec and body are
5872 -- subtype conformant, because they were subtype conformant
5873 -- in the generic. We choose the subtype-conformant entity
5874 -- here as well, to resolve spurious ambiguities in the
5875 -- instance that were not present in the generic (i.e. when
5876 -- two different types are given the same actual). If we are
5877 -- looking for a spec to match a body, full conformance is
5878 -- expected.
5879
5880 if In_Instance then
5881 Set_Convention (Designator, Convention (E));
5882
5883 if Nkind (N) = N_Subprogram_Body
5884 and then Present (Homonym (E))
5885 and then not Fully_Conformant (E, Designator)
5886 then
5887 goto Next_Entity;
5888
5889 elsif not Subtype_Conformant (E, Designator) then
5890 goto Next_Entity;
5891 end if;
5892 end if;
5893
5894 if not Has_Completion (E) then
5895 if Nkind (N) /= N_Subprogram_Body_Stub then
5896 Set_Corresponding_Spec (N, E);
5897 end if;
5898
5899 Set_Has_Completion (E);
5900 return E;
5901
5902 elsif Nkind (Parent (N)) = N_Subunit then
5903
5904 -- If this is the proper body of a subunit, the completion
5905 -- flag is set when analyzing the stub.
5906
5907 return E;
5908
5909 -- If E is an internal function with a controlling result
5910 -- that was created for an operation inherited by a null
5911 -- extension, it may be overridden by a body without a previous
5912 -- spec (one more reason why these should be shunned). In that
5913 -- case remove the generated body, because the current one is
5914 -- the explicit overriding.
5915
5916 elsif Ekind (E) = E_Function
5917 and then Ada_Version >= Ada_05
5918 and then not Comes_From_Source (E)
5919 and then Has_Controlling_Result (E)
5920 and then Is_Null_Extension (Etype (E))
5921 and then Comes_From_Source (Spec)
5922 then
5923 Set_Has_Completion (E, False);
5924
5925 if Expander_Active then
5926 Remove
5927 (Unit_Declaration_Node
5928 (Corresponding_Body (Unit_Declaration_Node (E))));
5929 return E;
5930
5931 -- If expansion is disabled, the wrapper function has not
5932 -- been generated, and this is the standard case of a late
5933 -- body overriding an inherited operation.
5934
5935 else
5936 return Empty;
5937 end if;
5938
5939 -- If the body already exists, then this is an error unless
5940 -- the previous declaration is the implicit declaration of a
5941 -- derived subprogram, or this is a spurious overloading in an
5942 -- instance.
5943
5944 elsif No (Alias (E))
5945 and then not Is_Intrinsic_Subprogram (E)
5946 and then not In_Instance
5947 and then Post_Error
5948 then
5949 Error_Msg_Sloc := Sloc (E);
5950
5951 if Is_Imported (E) then
5952 Error_Msg_NE
5953 ("body not allowed for imported subprogram & declared#",
5954 N, E);
5955 else
5956 Error_Msg_NE ("duplicate body for & declared#", N, E);
5957 end if;
5958 end if;
5959
5960 -- Child units cannot be overloaded, so a conformance mismatch
5961 -- between body and a previous spec is an error.
5962
5963 elsif Is_Child_Unit (E)
5964 and then
5965 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
5966 and then
5967 Nkind (Parent (Unit_Declaration_Node (Designator))) =
5968 N_Compilation_Unit
5969 and then Post_Error
5970 then
5971 Error_Msg_N
5972 ("body of child unit does not match previous declaration", N);
5973 end if;
5974 end if;
5975
5976 <<Next_Entity>>
5977 E := Homonym (E);
5978 end loop;
5979
5980 -- On exit, we know that no previous declaration of subprogram exists
5981
5982 return Empty;
5983 end Find_Corresponding_Spec;
5984
5985 ----------------------
5986 -- Fully_Conformant --
5987 ----------------------
5988
5989 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
5990 Result : Boolean;
5991 begin
5992 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
5993 return Result;
5994 end Fully_Conformant;
5995
5996 ----------------------------------
5997 -- Fully_Conformant_Expressions --
5998 ----------------------------------
5999
6000 function Fully_Conformant_Expressions
6001 (Given_E1 : Node_Id;
6002 Given_E2 : Node_Id) return Boolean
6003 is
6004 E1 : constant Node_Id := Original_Node (Given_E1);
6005 E2 : constant Node_Id := Original_Node (Given_E2);
6006 -- We always test conformance on original nodes, since it is possible
6007 -- for analysis and/or expansion to make things look as though they
6008 -- conform when they do not, e.g. by converting 1+2 into 3.
6009
6010 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
6011 renames Fully_Conformant_Expressions;
6012
6013 function FCL (L1, L2 : List_Id) return Boolean;
6014 -- Compare elements of two lists for conformance. Elements have to
6015 -- be conformant, and actuals inserted as default parameters do not
6016 -- match explicit actuals with the same value.
6017
6018 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
6019 -- Compare an operator node with a function call
6020
6021 ---------
6022 -- FCL --
6023 ---------
6024
6025 function FCL (L1, L2 : List_Id) return Boolean is
6026 N1, N2 : Node_Id;
6027
6028 begin
6029 if L1 = No_List then
6030 N1 := Empty;
6031 else
6032 N1 := First (L1);
6033 end if;
6034
6035 if L2 = No_List then
6036 N2 := Empty;
6037 else
6038 N2 := First (L2);
6039 end if;
6040
6041 -- Compare two lists, skipping rewrite insertions (we want to
6042 -- compare the original trees, not the expanded versions!)
6043
6044 loop
6045 if Is_Rewrite_Insertion (N1) then
6046 Next (N1);
6047 elsif Is_Rewrite_Insertion (N2) then
6048 Next (N2);
6049 elsif No (N1) then
6050 return No (N2);
6051 elsif No (N2) then
6052 return False;
6053 elsif not FCE (N1, N2) then
6054 return False;
6055 else
6056 Next (N1);
6057 Next (N2);
6058 end if;
6059 end loop;
6060 end FCL;
6061
6062 ---------
6063 -- FCO --
6064 ---------
6065
6066 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
6067 Actuals : constant List_Id := Parameter_Associations (Call_Node);
6068 Act : Node_Id;
6069
6070 begin
6071 if No (Actuals)
6072 or else Entity (Op_Node) /= Entity (Name (Call_Node))
6073 then
6074 return False;
6075
6076 else
6077 Act := First (Actuals);
6078
6079 if Nkind (Op_Node) in N_Binary_Op then
6080 if not FCE (Left_Opnd (Op_Node), Act) then
6081 return False;
6082 end if;
6083
6084 Next (Act);
6085 end if;
6086
6087 return Present (Act)
6088 and then FCE (Right_Opnd (Op_Node), Act)
6089 and then No (Next (Act));
6090 end if;
6091 end FCO;
6092
6093 -- Start of processing for Fully_Conformant_Expressions
6094
6095 begin
6096 -- Non-conformant if paren count does not match. Note: if some idiot
6097 -- complains that we don't do this right for more than 3 levels of
6098 -- parentheses, they will be treated with the respect they deserve!
6099
6100 if Paren_Count (E1) /= Paren_Count (E2) then
6101 return False;
6102
6103 -- If same entities are referenced, then they are conformant even if
6104 -- they have different forms (RM 8.3.1(19-20)).
6105
6106 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
6107 if Present (Entity (E1)) then
6108 return Entity (E1) = Entity (E2)
6109 or else (Chars (Entity (E1)) = Chars (Entity (E2))
6110 and then Ekind (Entity (E1)) = E_Discriminant
6111 and then Ekind (Entity (E2)) = E_In_Parameter);
6112
6113 elsif Nkind (E1) = N_Expanded_Name
6114 and then Nkind (E2) = N_Expanded_Name
6115 and then Nkind (Selector_Name (E1)) = N_Character_Literal
6116 and then Nkind (Selector_Name (E2)) = N_Character_Literal
6117 then
6118 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
6119
6120 else
6121 -- Identifiers in component associations don't always have
6122 -- entities, but their names must conform.
6123
6124 return Nkind (E1) = N_Identifier
6125 and then Nkind (E2) = N_Identifier
6126 and then Chars (E1) = Chars (E2);
6127 end if;
6128
6129 elsif Nkind (E1) = N_Character_Literal
6130 and then Nkind (E2) = N_Expanded_Name
6131 then
6132 return Nkind (Selector_Name (E2)) = N_Character_Literal
6133 and then Chars (E1) = Chars (Selector_Name (E2));
6134
6135 elsif Nkind (E2) = N_Character_Literal
6136 and then Nkind (E1) = N_Expanded_Name
6137 then
6138 return Nkind (Selector_Name (E1)) = N_Character_Literal
6139 and then Chars (E2) = Chars (Selector_Name (E1));
6140
6141 elsif Nkind (E1) in N_Op
6142 and then Nkind (E2) = N_Function_Call
6143 then
6144 return FCO (E1, E2);
6145
6146 elsif Nkind (E2) in N_Op
6147 and then Nkind (E1) = N_Function_Call
6148 then
6149 return FCO (E2, E1);
6150
6151 -- Otherwise we must have the same syntactic entity
6152
6153 elsif Nkind (E1) /= Nkind (E2) then
6154 return False;
6155
6156 -- At this point, we specialize by node type
6157
6158 else
6159 case Nkind (E1) is
6160
6161 when N_Aggregate =>
6162 return
6163 FCL (Expressions (E1), Expressions (E2))
6164 and then
6165 FCL (Component_Associations (E1),
6166 Component_Associations (E2));
6167
6168 when N_Allocator =>
6169 if Nkind (Expression (E1)) = N_Qualified_Expression
6170 or else
6171 Nkind (Expression (E2)) = N_Qualified_Expression
6172 then
6173 return FCE (Expression (E1), Expression (E2));
6174
6175 -- Check that the subtype marks and any constraints
6176 -- are conformant
6177
6178 else
6179 declare
6180 Indic1 : constant Node_Id := Expression (E1);
6181 Indic2 : constant Node_Id := Expression (E2);
6182 Elt1 : Node_Id;
6183 Elt2 : Node_Id;
6184
6185 begin
6186 if Nkind (Indic1) /= N_Subtype_Indication then
6187 return
6188 Nkind (Indic2) /= N_Subtype_Indication
6189 and then Entity (Indic1) = Entity (Indic2);
6190
6191 elsif Nkind (Indic2) /= N_Subtype_Indication then
6192 return
6193 Nkind (Indic1) /= N_Subtype_Indication
6194 and then Entity (Indic1) = Entity (Indic2);
6195
6196 else
6197 if Entity (Subtype_Mark (Indic1)) /=
6198 Entity (Subtype_Mark (Indic2))
6199 then
6200 return False;
6201 end if;
6202
6203 Elt1 := First (Constraints (Constraint (Indic1)));
6204 Elt2 := First (Constraints (Constraint (Indic2)));
6205 while Present (Elt1) and then Present (Elt2) loop
6206 if not FCE (Elt1, Elt2) then
6207 return False;
6208 end if;
6209
6210 Next (Elt1);
6211 Next (Elt2);
6212 end loop;
6213
6214 return True;
6215 end if;
6216 end;
6217 end if;
6218
6219 when N_Attribute_Reference =>
6220 return
6221 Attribute_Name (E1) = Attribute_Name (E2)
6222 and then FCL (Expressions (E1), Expressions (E2));
6223
6224 when N_Binary_Op =>
6225 return
6226 Entity (E1) = Entity (E2)
6227 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
6228 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
6229
6230 when N_Short_Circuit | N_Membership_Test =>
6231 return
6232 FCE (Left_Opnd (E1), Left_Opnd (E2))
6233 and then
6234 FCE (Right_Opnd (E1), Right_Opnd (E2));
6235
6236 when N_Case_Expression =>
6237 declare
6238 Alt1 : Node_Id;
6239 Alt2 : Node_Id;
6240
6241 begin
6242 if not FCE (Expression (E1), Expression (E2)) then
6243 return False;
6244
6245 else
6246 Alt1 := First (Alternatives (E1));
6247 Alt2 := First (Alternatives (E2));
6248 loop
6249 if Present (Alt1) /= Present (Alt2) then
6250 return False;
6251 elsif No (Alt1) then
6252 return True;
6253 end if;
6254
6255 if not FCE (Expression (Alt1), Expression (Alt2))
6256 or else not FCL (Discrete_Choices (Alt1),
6257 Discrete_Choices (Alt2))
6258 then
6259 return False;
6260 end if;
6261
6262 Next (Alt1);
6263 Next (Alt2);
6264 end loop;
6265 end if;
6266 end;
6267
6268 when N_Character_Literal =>
6269 return
6270 Char_Literal_Value (E1) = Char_Literal_Value (E2);
6271
6272 when N_Component_Association =>
6273 return
6274 FCL (Choices (E1), Choices (E2))
6275 and then
6276 FCE (Expression (E1), Expression (E2));
6277
6278 when N_Conditional_Expression =>
6279 return
6280 FCL (Expressions (E1), Expressions (E2));
6281
6282 when N_Explicit_Dereference =>
6283 return
6284 FCE (Prefix (E1), Prefix (E2));
6285
6286 when N_Extension_Aggregate =>
6287 return
6288 FCL (Expressions (E1), Expressions (E2))
6289 and then Null_Record_Present (E1) =
6290 Null_Record_Present (E2)
6291 and then FCL (Component_Associations (E1),
6292 Component_Associations (E2));
6293
6294 when N_Function_Call =>
6295 return
6296 FCE (Name (E1), Name (E2))
6297 and then
6298 FCL (Parameter_Associations (E1),
6299 Parameter_Associations (E2));
6300
6301 when N_Indexed_Component =>
6302 return
6303 FCE (Prefix (E1), Prefix (E2))
6304 and then
6305 FCL (Expressions (E1), Expressions (E2));
6306
6307 when N_Integer_Literal =>
6308 return (Intval (E1) = Intval (E2));
6309
6310 when N_Null =>
6311 return True;
6312
6313 when N_Operator_Symbol =>
6314 return
6315 Chars (E1) = Chars (E2);
6316
6317 when N_Others_Choice =>
6318 return True;
6319
6320 when N_Parameter_Association =>
6321 return
6322 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
6323 and then FCE (Explicit_Actual_Parameter (E1),
6324 Explicit_Actual_Parameter (E2));
6325
6326 when N_Qualified_Expression =>
6327 return
6328 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6329 and then
6330 FCE (Expression (E1), Expression (E2));
6331
6332 when N_Range =>
6333 return
6334 FCE (Low_Bound (E1), Low_Bound (E2))
6335 and then
6336 FCE (High_Bound (E1), High_Bound (E2));
6337
6338 when N_Real_Literal =>
6339 return (Realval (E1) = Realval (E2));
6340
6341 when N_Selected_Component =>
6342 return
6343 FCE (Prefix (E1), Prefix (E2))
6344 and then
6345 FCE (Selector_Name (E1), Selector_Name (E2));
6346
6347 when N_Slice =>
6348 return
6349 FCE (Prefix (E1), Prefix (E2))
6350 and then
6351 FCE (Discrete_Range (E1), Discrete_Range (E2));
6352
6353 when N_String_Literal =>
6354 declare
6355 S1 : constant String_Id := Strval (E1);
6356 S2 : constant String_Id := Strval (E2);
6357 L1 : constant Nat := String_Length (S1);
6358 L2 : constant Nat := String_Length (S2);
6359
6360 begin
6361 if L1 /= L2 then
6362 return False;
6363
6364 else
6365 for J in 1 .. L1 loop
6366 if Get_String_Char (S1, J) /=
6367 Get_String_Char (S2, J)
6368 then
6369 return False;
6370 end if;
6371 end loop;
6372
6373 return True;
6374 end if;
6375 end;
6376
6377 when N_Type_Conversion =>
6378 return
6379 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6380 and then
6381 FCE (Expression (E1), Expression (E2));
6382
6383 when N_Unary_Op =>
6384 return
6385 Entity (E1) = Entity (E2)
6386 and then
6387 FCE (Right_Opnd (E1), Right_Opnd (E2));
6388
6389 when N_Unchecked_Type_Conversion =>
6390 return
6391 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
6392 and then
6393 FCE (Expression (E1), Expression (E2));
6394
6395 -- All other node types cannot appear in this context. Strictly
6396 -- we should raise a fatal internal error. Instead we just ignore
6397 -- the nodes. This means that if anyone makes a mistake in the
6398 -- expander and mucks an expression tree irretrievably, the
6399 -- result will be a failure to detect a (probably very obscure)
6400 -- case of non-conformance, which is better than bombing on some
6401 -- case where two expressions do in fact conform.
6402
6403 when others =>
6404 return True;
6405
6406 end case;
6407 end if;
6408 end Fully_Conformant_Expressions;
6409
6410 ----------------------------------------
6411 -- Fully_Conformant_Discrete_Subtypes --
6412 ----------------------------------------
6413
6414 function Fully_Conformant_Discrete_Subtypes
6415 (Given_S1 : Node_Id;
6416 Given_S2 : Node_Id) return Boolean
6417 is
6418 S1 : constant Node_Id := Original_Node (Given_S1);
6419 S2 : constant Node_Id := Original_Node (Given_S2);
6420
6421 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
6422 -- Special-case for a bound given by a discriminant, which in the body
6423 -- is replaced with the discriminal of the enclosing type.
6424
6425 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
6426 -- Check both bounds
6427
6428 -----------------------
6429 -- Conforming_Bounds --
6430 -----------------------
6431
6432 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
6433 begin
6434 if Is_Entity_Name (B1)
6435 and then Is_Entity_Name (B2)
6436 and then Ekind (Entity (B1)) = E_Discriminant
6437 then
6438 return Chars (B1) = Chars (B2);
6439
6440 else
6441 return Fully_Conformant_Expressions (B1, B2);
6442 end if;
6443 end Conforming_Bounds;
6444
6445 -----------------------
6446 -- Conforming_Ranges --
6447 -----------------------
6448
6449 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
6450 begin
6451 return
6452 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
6453 and then
6454 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
6455 end Conforming_Ranges;
6456
6457 -- Start of processing for Fully_Conformant_Discrete_Subtypes
6458
6459 begin
6460 if Nkind (S1) /= Nkind (S2) then
6461 return False;
6462
6463 elsif Is_Entity_Name (S1) then
6464 return Entity (S1) = Entity (S2);
6465
6466 elsif Nkind (S1) = N_Range then
6467 return Conforming_Ranges (S1, S2);
6468
6469 elsif Nkind (S1) = N_Subtype_Indication then
6470 return
6471 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
6472 and then
6473 Conforming_Ranges
6474 (Range_Expression (Constraint (S1)),
6475 Range_Expression (Constraint (S2)));
6476 else
6477 return True;
6478 end if;
6479 end Fully_Conformant_Discrete_Subtypes;
6480
6481 --------------------
6482 -- Install_Entity --
6483 --------------------
6484
6485 procedure Install_Entity (E : Entity_Id) is
6486 Prev : constant Entity_Id := Current_Entity (E);
6487 begin
6488 Set_Is_Immediately_Visible (E);
6489 Set_Current_Entity (E);
6490 Set_Homonym (E, Prev);
6491 end Install_Entity;
6492
6493 ---------------------
6494 -- Install_Formals --
6495 ---------------------
6496
6497 procedure Install_Formals (Id : Entity_Id) is
6498 F : Entity_Id;
6499 begin
6500 F := First_Formal (Id);
6501 while Present (F) loop
6502 Install_Entity (F);
6503 Next_Formal (F);
6504 end loop;
6505 end Install_Formals;
6506
6507 -----------------------------
6508 -- Is_Interface_Conformant --
6509 -----------------------------
6510
6511 function Is_Interface_Conformant
6512 (Tagged_Type : Entity_Id;
6513 Iface_Prim : Entity_Id;
6514 Prim : Entity_Id) return Boolean
6515 is
6516 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
6517 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
6518
6519 begin
6520 pragma Assert (Is_Subprogram (Iface_Prim)
6521 and then Is_Subprogram (Prim)
6522 and then Is_Dispatching_Operation (Iface_Prim)
6523 and then Is_Dispatching_Operation (Prim));
6524
6525 pragma Assert (Is_Interface (Iface)
6526 or else (Present (Alias (Iface_Prim))
6527 and then
6528 Is_Interface
6529 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
6530
6531 if Prim = Iface_Prim
6532 or else not Is_Subprogram (Prim)
6533 or else Ekind (Prim) /= Ekind (Iface_Prim)
6534 or else not Is_Dispatching_Operation (Prim)
6535 or else Scope (Prim) /= Scope (Tagged_Type)
6536 or else No (Typ)
6537 or else Base_Type (Typ) /= Tagged_Type
6538 or else not Primitive_Names_Match (Iface_Prim, Prim)
6539 then
6540 return False;
6541
6542 -- Case of a procedure, or a function that does not have a controlling
6543 -- result (I or access I).
6544
6545 elsif Ekind (Iface_Prim) = E_Procedure
6546 or else Etype (Prim) = Etype (Iface_Prim)
6547 or else not Has_Controlling_Result (Prim)
6548 then
6549 return Type_Conformant
6550 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
6551
6552 -- Case of a function returning an interface, or an access to one.
6553 -- Check that the return types correspond.
6554
6555 elsif Implements_Interface (Typ, Iface) then
6556 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
6557 /=
6558 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
6559 then
6560 return False;
6561 else
6562 return
6563 Type_Conformant (Prim, Iface_Prim,
6564 Skip_Controlling_Formals => True);
6565 end if;
6566
6567 else
6568 return False;
6569 end if;
6570 end Is_Interface_Conformant;
6571
6572 ---------------------------------
6573 -- Is_Non_Overriding_Operation --
6574 ---------------------------------
6575
6576 function Is_Non_Overriding_Operation
6577 (Prev_E : Entity_Id;
6578 New_E : Entity_Id) return Boolean
6579 is
6580 Formal : Entity_Id;
6581 F_Typ : Entity_Id;
6582 G_Typ : Entity_Id := Empty;
6583
6584 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
6585 -- If F_Type is a derived type associated with a generic actual subtype,
6586 -- then return its Generic_Parent_Type attribute, else return Empty.
6587
6588 function Types_Correspond
6589 (P_Type : Entity_Id;
6590 N_Type : Entity_Id) return Boolean;
6591 -- Returns true if and only if the types (or designated types in the
6592 -- case of anonymous access types) are the same or N_Type is derived
6593 -- directly or indirectly from P_Type.
6594
6595 -----------------------------
6596 -- Get_Generic_Parent_Type --
6597 -----------------------------
6598
6599 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
6600 G_Typ : Entity_Id;
6601 Indic : Node_Id;
6602
6603 begin
6604 if Is_Derived_Type (F_Typ)
6605 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
6606 then
6607 -- The tree must be traversed to determine the parent subtype in
6608 -- the generic unit, which unfortunately isn't always available
6609 -- via semantic attributes. ??? (Note: The use of Original_Node
6610 -- is needed for cases where a full derived type has been
6611 -- rewritten.)
6612
6613 Indic := Subtype_Indication
6614 (Type_Definition (Original_Node (Parent (F_Typ))));
6615
6616 if Nkind (Indic) = N_Subtype_Indication then
6617 G_Typ := Entity (Subtype_Mark (Indic));
6618 else
6619 G_Typ := Entity (Indic);
6620 end if;
6621
6622 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
6623 and then Present (Generic_Parent_Type (Parent (G_Typ)))
6624 then
6625 return Generic_Parent_Type (Parent (G_Typ));
6626 end if;
6627 end if;
6628
6629 return Empty;
6630 end Get_Generic_Parent_Type;
6631
6632 ----------------------
6633 -- Types_Correspond --
6634 ----------------------
6635
6636 function Types_Correspond
6637 (P_Type : Entity_Id;
6638 N_Type : Entity_Id) return Boolean
6639 is
6640 Prev_Type : Entity_Id := Base_Type (P_Type);
6641 New_Type : Entity_Id := Base_Type (N_Type);
6642
6643 begin
6644 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
6645 Prev_Type := Designated_Type (Prev_Type);
6646 end if;
6647
6648 if Ekind (New_Type) = E_Anonymous_Access_Type then
6649 New_Type := Designated_Type (New_Type);
6650 end if;
6651
6652 if Prev_Type = New_Type then
6653 return True;
6654
6655 elsif not Is_Class_Wide_Type (New_Type) then
6656 while Etype (New_Type) /= New_Type loop
6657 New_Type := Etype (New_Type);
6658 if New_Type = Prev_Type then
6659 return True;
6660 end if;
6661 end loop;
6662 end if;
6663 return False;
6664 end Types_Correspond;
6665
6666 -- Start of processing for Is_Non_Overriding_Operation
6667
6668 begin
6669 -- In the case where both operations are implicit derived subprograms
6670 -- then neither overrides the other. This can only occur in certain
6671 -- obscure cases (e.g., derivation from homographs created in a generic
6672 -- instantiation).
6673
6674 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
6675 return True;
6676
6677 elsif Ekind (Current_Scope) = E_Package
6678 and then Is_Generic_Instance (Current_Scope)
6679 and then In_Private_Part (Current_Scope)
6680 and then Comes_From_Source (New_E)
6681 then
6682 -- We examine the formals and result subtype of the inherited
6683 -- operation, to determine whether their type is derived from (the
6684 -- instance of) a generic type.
6685
6686 Formal := First_Formal (Prev_E);
6687 while Present (Formal) loop
6688 F_Typ := Base_Type (Etype (Formal));
6689
6690 if Ekind (F_Typ) = E_Anonymous_Access_Type then
6691 F_Typ := Designated_Type (F_Typ);
6692 end if;
6693
6694 G_Typ := Get_Generic_Parent_Type (F_Typ);
6695
6696 Next_Formal (Formal);
6697 end loop;
6698
6699 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
6700 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
6701 end if;
6702
6703 if No (G_Typ) then
6704 return False;
6705 end if;
6706
6707 -- If the generic type is a private type, then the original operation
6708 -- was not overriding in the generic, because there was no primitive
6709 -- operation to override.
6710
6711 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
6712 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
6713 N_Formal_Private_Type_Definition
6714 then
6715 return True;
6716
6717 -- The generic parent type is the ancestor of a formal derived
6718 -- type declaration. We need to check whether it has a primitive
6719 -- operation that should be overridden by New_E in the generic.
6720
6721 else
6722 declare
6723 P_Formal : Entity_Id;
6724 N_Formal : Entity_Id;
6725 P_Typ : Entity_Id;
6726 N_Typ : Entity_Id;
6727 P_Prim : Entity_Id;
6728 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
6729
6730 begin
6731 while Present (Prim_Elt) loop
6732 P_Prim := Node (Prim_Elt);
6733
6734 if Chars (P_Prim) = Chars (New_E)
6735 and then Ekind (P_Prim) = Ekind (New_E)
6736 then
6737 P_Formal := First_Formal (P_Prim);
6738 N_Formal := First_Formal (New_E);
6739 while Present (P_Formal) and then Present (N_Formal) loop
6740 P_Typ := Etype (P_Formal);
6741 N_Typ := Etype (N_Formal);
6742
6743 if not Types_Correspond (P_Typ, N_Typ) then
6744 exit;
6745 end if;
6746
6747 Next_Entity (P_Formal);
6748 Next_Entity (N_Formal);
6749 end loop;
6750
6751 -- Found a matching primitive operation belonging to the
6752 -- formal ancestor type, so the new subprogram is
6753 -- overriding.
6754
6755 if No (P_Formal)
6756 and then No (N_Formal)
6757 and then (Ekind (New_E) /= E_Function
6758 or else
6759 Types_Correspond
6760 (Etype (P_Prim), Etype (New_E)))
6761 then
6762 return False;
6763 end if;
6764 end if;
6765
6766 Next_Elmt (Prim_Elt);
6767 end loop;
6768
6769 -- If no match found, then the new subprogram does not
6770 -- override in the generic (nor in the instance).
6771
6772 return True;
6773 end;
6774 end if;
6775 else
6776 return False;
6777 end if;
6778 end Is_Non_Overriding_Operation;
6779
6780 ------------------------------
6781 -- Make_Inequality_Operator --
6782 ------------------------------
6783
6784 -- S is the defining identifier of an equality operator. We build a
6785 -- subprogram declaration with the right signature. This operation is
6786 -- intrinsic, because it is always expanded as the negation of the
6787 -- call to the equality function.
6788
6789 procedure Make_Inequality_Operator (S : Entity_Id) is
6790 Loc : constant Source_Ptr := Sloc (S);
6791 Decl : Node_Id;
6792 Formals : List_Id;
6793 Op_Name : Entity_Id;
6794
6795 FF : constant Entity_Id := First_Formal (S);
6796 NF : constant Entity_Id := Next_Formal (FF);
6797
6798 begin
6799 -- Check that equality was properly defined, ignore call if not
6800
6801 if No (NF) then
6802 return;
6803 end if;
6804
6805 declare
6806 A : constant Entity_Id :=
6807 Make_Defining_Identifier (Sloc (FF),
6808 Chars => Chars (FF));
6809
6810 B : constant Entity_Id :=
6811 Make_Defining_Identifier (Sloc (NF),
6812 Chars => Chars (NF));
6813
6814 begin
6815 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
6816
6817 Formals := New_List (
6818 Make_Parameter_Specification (Loc,
6819 Defining_Identifier => A,
6820 Parameter_Type =>
6821 New_Reference_To (Etype (First_Formal (S)),
6822 Sloc (Etype (First_Formal (S))))),
6823
6824 Make_Parameter_Specification (Loc,
6825 Defining_Identifier => B,
6826 Parameter_Type =>
6827 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
6828 Sloc (Etype (Next_Formal (First_Formal (S)))))));
6829
6830 Decl :=
6831 Make_Subprogram_Declaration (Loc,
6832 Specification =>
6833 Make_Function_Specification (Loc,
6834 Defining_Unit_Name => Op_Name,
6835 Parameter_Specifications => Formals,
6836 Result_Definition =>
6837 New_Reference_To (Standard_Boolean, Loc)));
6838
6839 -- Insert inequality right after equality if it is explicit or after
6840 -- the derived type when implicit. These entities are created only
6841 -- for visibility purposes, and eventually replaced in the course of
6842 -- expansion, so they do not need to be attached to the tree and seen
6843 -- by the back-end. Keeping them internal also avoids spurious
6844 -- freezing problems. The declaration is inserted in the tree for
6845 -- analysis, and removed afterwards. If the equality operator comes
6846 -- from an explicit declaration, attach the inequality immediately
6847 -- after. Else the equality is inherited from a derived type
6848 -- declaration, so insert inequality after that declaration.
6849
6850 if No (Alias (S)) then
6851 Insert_After (Unit_Declaration_Node (S), Decl);
6852 elsif Is_List_Member (Parent (S)) then
6853 Insert_After (Parent (S), Decl);
6854 else
6855 Insert_After (Parent (Etype (First_Formal (S))), Decl);
6856 end if;
6857
6858 Mark_Rewrite_Insertion (Decl);
6859 Set_Is_Intrinsic_Subprogram (Op_Name);
6860 Analyze (Decl);
6861 Remove (Decl);
6862 Set_Has_Completion (Op_Name);
6863 Set_Corresponding_Equality (Op_Name, S);
6864 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
6865 end;
6866 end Make_Inequality_Operator;
6867
6868 ----------------------
6869 -- May_Need_Actuals --
6870 ----------------------
6871
6872 procedure May_Need_Actuals (Fun : Entity_Id) is
6873 F : Entity_Id;
6874 B : Boolean;
6875
6876 begin
6877 F := First_Formal (Fun);
6878 B := True;
6879 while Present (F) loop
6880 if No (Default_Value (F)) then
6881 B := False;
6882 exit;
6883 end if;
6884
6885 Next_Formal (F);
6886 end loop;
6887
6888 Set_Needs_No_Actuals (Fun, B);
6889 end May_Need_Actuals;
6890
6891 ---------------------
6892 -- Mode_Conformant --
6893 ---------------------
6894
6895 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
6896 Result : Boolean;
6897 begin
6898 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
6899 return Result;
6900 end Mode_Conformant;
6901
6902 ---------------------------
6903 -- New_Overloaded_Entity --
6904 ---------------------------
6905
6906 procedure New_Overloaded_Entity
6907 (S : Entity_Id;
6908 Derived_Type : Entity_Id := Empty)
6909 is
6910 Overridden_Subp : Entity_Id := Empty;
6911 -- Set if the current scope has an operation that is type-conformant
6912 -- with S, and becomes hidden by S.
6913
6914 Is_Primitive_Subp : Boolean;
6915 -- Set to True if the new subprogram is primitive
6916
6917 E : Entity_Id;
6918 -- Entity that S overrides
6919
6920 Prev_Vis : Entity_Id := Empty;
6921 -- Predecessor of E in Homonym chain
6922
6923 procedure Check_For_Primitive_Subprogram
6924 (Is_Primitive : out Boolean;
6925 Is_Overriding : Boolean := False);
6926 -- If the subprogram being analyzed is a primitive operation of the type
6927 -- of a formal or result, set the Has_Primitive_Operations flag on the
6928 -- type, and set Is_Primitive to True (otherwise set to False). Set the
6929 -- corresponding flag on the entity itself for later use.
6930
6931 procedure Check_Synchronized_Overriding
6932 (Def_Id : Entity_Id;
6933 Overridden_Subp : out Entity_Id);
6934 -- First determine if Def_Id is an entry or a subprogram either defined
6935 -- in the scope of a task or protected type, or is a primitive of such
6936 -- a type. Check whether Def_Id overrides a subprogram of an interface
6937 -- implemented by the synchronized type, return the overridden entity
6938 -- or Empty.
6939
6940 function Is_Private_Declaration (E : Entity_Id) return Boolean;
6941 -- Check that E is declared in the private part of the current package,
6942 -- or in the package body, where it may hide a previous declaration.
6943 -- We can't use In_Private_Part by itself because this flag is also
6944 -- set when freezing entities, so we must examine the place of the
6945 -- declaration in the tree, and recognize wrapper packages as well.
6946
6947 function Is_Overriding_Alias
6948 (Old_E : Entity_Id;
6949 New_E : Entity_Id) return Boolean;
6950 -- Check whether new subprogram and old subprogram are both inherited
6951 -- from subprograms that have distinct dispatch table entries. This can
6952 -- occur with derivations from instances with accidental homonyms.
6953 -- The function is conservative given that the converse is only true
6954 -- within instances that contain accidental overloadings.
6955
6956 ------------------------------------
6957 -- Check_For_Primitive_Subprogram --
6958 ------------------------------------
6959
6960 procedure Check_For_Primitive_Subprogram
6961 (Is_Primitive : out Boolean;
6962 Is_Overriding : Boolean := False)
6963 is
6964 Formal : Entity_Id;
6965 F_Typ : Entity_Id;
6966 B_Typ : Entity_Id;
6967
6968 function Visible_Part_Type (T : Entity_Id) return Boolean;
6969 -- Returns true if T is declared in the visible part of the current
6970 -- package scope; otherwise returns false. Assumes that T is declared
6971 -- in a package.
6972
6973 procedure Check_Private_Overriding (T : Entity_Id);
6974 -- Checks that if a primitive abstract subprogram of a visible
6975 -- abstract type is declared in a private part, then it must override
6976 -- an abstract subprogram declared in the visible part. Also checks
6977 -- that if a primitive function with a controlling result is declared
6978 -- in a private part, then it must override a function declared in
6979 -- the visible part.
6980
6981 ------------------------------
6982 -- Check_Private_Overriding --
6983 ------------------------------
6984
6985 procedure Check_Private_Overriding (T : Entity_Id) is
6986 begin
6987 if Is_Package_Or_Generic_Package (Current_Scope)
6988 and then In_Private_Part (Current_Scope)
6989 and then Visible_Part_Type (T)
6990 and then not In_Instance
6991 then
6992 if Is_Abstract_Type (T)
6993 and then Is_Abstract_Subprogram (S)
6994 and then (not Is_Overriding
6995 or else not Is_Abstract_Subprogram (E))
6996 then
6997 Error_Msg_N
6998 ("abstract subprograms must be visible "
6999 & "(RM 3.9.3(10))!", S);
7000
7001 elsif Ekind (S) = E_Function
7002 and then Is_Tagged_Type (T)
7003 and then T = Base_Type (Etype (S))
7004 and then not Is_Overriding
7005 then
7006 Error_Msg_N
7007 ("private function with tagged result must"
7008 & " override visible-part function", S);
7009 Error_Msg_N
7010 ("\move subprogram to the visible part"
7011 & " (RM 3.9.3(10))", S);
7012 end if;
7013 end if;
7014 end Check_Private_Overriding;
7015
7016 -----------------------
7017 -- Visible_Part_Type --
7018 -----------------------
7019
7020 function Visible_Part_Type (T : Entity_Id) return Boolean is
7021 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
7022 N : Node_Id;
7023
7024 begin
7025 -- If the entity is a private type, then it must be declared in a
7026 -- visible part.
7027
7028 if Ekind (T) in Private_Kind then
7029 return True;
7030 end if;
7031
7032 -- Otherwise, we traverse the visible part looking for its
7033 -- corresponding declaration. We cannot use the declaration
7034 -- node directly because in the private part the entity of a
7035 -- private type is the one in the full view, which does not
7036 -- indicate that it is the completion of something visible.
7037
7038 N := First (Visible_Declarations (Specification (P)));
7039 while Present (N) loop
7040 if Nkind (N) = N_Full_Type_Declaration
7041 and then Present (Defining_Identifier (N))
7042 and then T = Defining_Identifier (N)
7043 then
7044 return True;
7045
7046 elsif Nkind_In (N, N_Private_Type_Declaration,
7047 N_Private_Extension_Declaration)
7048 and then Present (Defining_Identifier (N))
7049 and then T = Full_View (Defining_Identifier (N))
7050 then
7051 return True;
7052 end if;
7053
7054 Next (N);
7055 end loop;
7056
7057 return False;
7058 end Visible_Part_Type;
7059
7060 -- Start of processing for Check_For_Primitive_Subprogram
7061
7062 begin
7063 Is_Primitive := False;
7064
7065 if not Comes_From_Source (S) then
7066 null;
7067
7068 -- If subprogram is at library level, it is not primitive operation
7069
7070 elsif Current_Scope = Standard_Standard then
7071 null;
7072
7073 elsif (Is_Package_Or_Generic_Package (Current_Scope)
7074 and then not In_Package_Body (Current_Scope))
7075 or else Is_Overriding
7076 then
7077 -- For function, check return type
7078
7079 if Ekind (S) = E_Function then
7080 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
7081 F_Typ := Designated_Type (Etype (S));
7082 else
7083 F_Typ := Etype (S);
7084 end if;
7085
7086 B_Typ := Base_Type (F_Typ);
7087
7088 if Scope (B_Typ) = Current_Scope
7089 and then not Is_Class_Wide_Type (B_Typ)
7090 and then not Is_Generic_Type (B_Typ)
7091 then
7092 Is_Primitive := True;
7093 Set_Has_Primitive_Operations (B_Typ);
7094 Set_Is_Primitive (S);
7095 Check_Private_Overriding (B_Typ);
7096 end if;
7097 end if;
7098
7099 -- For all subprograms, check formals
7100
7101 Formal := First_Formal (S);
7102 while Present (Formal) loop
7103 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
7104 F_Typ := Designated_Type (Etype (Formal));
7105 else
7106 F_Typ := Etype (Formal);
7107 end if;
7108
7109 B_Typ := Base_Type (F_Typ);
7110
7111 if Ekind (B_Typ) = E_Access_Subtype then
7112 B_Typ := Base_Type (B_Typ);
7113 end if;
7114
7115 if Scope (B_Typ) = Current_Scope
7116 and then not Is_Class_Wide_Type (B_Typ)
7117 and then not Is_Generic_Type (B_Typ)
7118 then
7119 Is_Primitive := True;
7120 Set_Is_Primitive (S);
7121 Set_Has_Primitive_Operations (B_Typ);
7122 Check_Private_Overriding (B_Typ);
7123 end if;
7124
7125 Next_Formal (Formal);
7126 end loop;
7127 end if;
7128 end Check_For_Primitive_Subprogram;
7129
7130 -----------------------------------
7131 -- Check_Synchronized_Overriding --
7132 -----------------------------------
7133
7134 procedure Check_Synchronized_Overriding
7135 (Def_Id : Entity_Id;
7136 Overridden_Subp : out Entity_Id)
7137 is
7138 Ifaces_List : Elist_Id;
7139 In_Scope : Boolean;
7140 Typ : Entity_Id;
7141
7142 function Matches_Prefixed_View_Profile
7143 (Prim_Params : List_Id;
7144 Iface_Params : List_Id) return Boolean;
7145 -- Determine whether a subprogram's parameter profile Prim_Params
7146 -- matches that of a potentially overridden interface subprogram
7147 -- Iface_Params. Also determine if the type of first parameter of
7148 -- Iface_Params is an implemented interface.
7149
7150 -----------------------------------
7151 -- Matches_Prefixed_View_Profile --
7152 -----------------------------------
7153
7154 function Matches_Prefixed_View_Profile
7155 (Prim_Params : List_Id;
7156 Iface_Params : List_Id) return Boolean
7157 is
7158 Iface_Id : Entity_Id;
7159 Iface_Param : Node_Id;
7160 Iface_Typ : Entity_Id;
7161 Prim_Id : Entity_Id;
7162 Prim_Param : Node_Id;
7163 Prim_Typ : Entity_Id;
7164
7165 function Is_Implemented
7166 (Ifaces_List : Elist_Id;
7167 Iface : Entity_Id) return Boolean;
7168 -- Determine if Iface is implemented by the current task or
7169 -- protected type.
7170
7171 --------------------
7172 -- Is_Implemented --
7173 --------------------
7174
7175 function Is_Implemented
7176 (Ifaces_List : Elist_Id;
7177 Iface : Entity_Id) return Boolean
7178 is
7179 Iface_Elmt : Elmt_Id;
7180
7181 begin
7182 Iface_Elmt := First_Elmt (Ifaces_List);
7183 while Present (Iface_Elmt) loop
7184 if Node (Iface_Elmt) = Iface then
7185 return True;
7186 end if;
7187
7188 Next_Elmt (Iface_Elmt);
7189 end loop;
7190
7191 return False;
7192 end Is_Implemented;
7193
7194 -- Start of processing for Matches_Prefixed_View_Profile
7195
7196 begin
7197 Iface_Param := First (Iface_Params);
7198 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7199
7200 if Is_Access_Type (Iface_Typ) then
7201 Iface_Typ := Designated_Type (Iface_Typ);
7202 end if;
7203
7204 Prim_Param := First (Prim_Params);
7205
7206 -- The first parameter of the potentially overridden subprogram
7207 -- must be an interface implemented by Prim.
7208
7209 if not Is_Interface (Iface_Typ)
7210 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7211 then
7212 return False;
7213 end if;
7214
7215 -- The checks on the object parameters are done, move onto the
7216 -- rest of the parameters.
7217
7218 if not In_Scope then
7219 Prim_Param := Next (Prim_Param);
7220 end if;
7221
7222 Iface_Param := Next (Iface_Param);
7223 while Present (Iface_Param) and then Present (Prim_Param) loop
7224 Iface_Id := Defining_Identifier (Iface_Param);
7225 Iface_Typ := Find_Parameter_Type (Iface_Param);
7226
7227 Prim_Id := Defining_Identifier (Prim_Param);
7228 Prim_Typ := Find_Parameter_Type (Prim_Param);
7229
7230 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7231 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7232 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7233 then
7234 Iface_Typ := Designated_Type (Iface_Typ);
7235 Prim_Typ := Designated_Type (Prim_Typ);
7236 end if;
7237
7238 -- Case of multiple interface types inside a parameter profile
7239
7240 -- (Obj_Param : in out Iface; ...; Param : Iface)
7241
7242 -- If the interface type is implemented, then the matching type
7243 -- in the primitive should be the implementing record type.
7244
7245 if Ekind (Iface_Typ) = E_Record_Type
7246 and then Is_Interface (Iface_Typ)
7247 and then Is_Implemented (Ifaces_List, Iface_Typ)
7248 then
7249 if Prim_Typ /= Typ then
7250 return False;
7251 end if;
7252
7253 -- The two parameters must be both mode and subtype conformant
7254
7255 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7256 or else not
7257 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7258 then
7259 return False;
7260 end if;
7261
7262 Next (Iface_Param);
7263 Next (Prim_Param);
7264 end loop;
7265
7266 -- One of the two lists contains more parameters than the other
7267
7268 if Present (Iface_Param) or else Present (Prim_Param) then
7269 return False;
7270 end if;
7271
7272 return True;
7273 end Matches_Prefixed_View_Profile;
7274
7275 -- Start of processing for Check_Synchronized_Overriding
7276
7277 begin
7278 Overridden_Subp := Empty;
7279
7280 -- Def_Id must be an entry or a subprogram. We should skip predefined
7281 -- primitives internally generated by the frontend; however at this
7282 -- stage predefined primitives are still not fully decorated. As a
7283 -- minor optimization we skip here internally generated subprograms.
7284
7285 if (Ekind (Def_Id) /= E_Entry
7286 and then Ekind (Def_Id) /= E_Function
7287 and then Ekind (Def_Id) /= E_Procedure)
7288 or else not Comes_From_Source (Def_Id)
7289 then
7290 return;
7291 end if;
7292
7293 -- Search for the concurrent declaration since it contains the list
7294 -- of all implemented interfaces. In this case, the subprogram is
7295 -- declared within the scope of a protected or a task type.
7296
7297 if Present (Scope (Def_Id))
7298 and then Is_Concurrent_Type (Scope (Def_Id))
7299 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7300 then
7301 Typ := Scope (Def_Id);
7302 In_Scope := True;
7303
7304 -- The enclosing scope is not a synchronized type and the subprogram
7305 -- has no formals
7306
7307 elsif No (First_Formal (Def_Id)) then
7308 return;
7309
7310 -- The subprogram has formals and hence it may be a primitive of a
7311 -- concurrent type
7312
7313 else
7314 Typ := Etype (First_Formal (Def_Id));
7315
7316 if Is_Access_Type (Typ) then
7317 Typ := Directly_Designated_Type (Typ);
7318 end if;
7319
7320 if Is_Concurrent_Type (Typ)
7321 and then not Is_Generic_Actual_Type (Typ)
7322 then
7323 In_Scope := False;
7324
7325 -- This case occurs when the concurrent type is declared within
7326 -- a generic unit. As a result the corresponding record has been
7327 -- built and used as the type of the first formal, we just have
7328 -- to retrieve the corresponding concurrent type.
7329
7330 elsif Is_Concurrent_Record_Type (Typ)
7331 and then Present (Corresponding_Concurrent_Type (Typ))
7332 then
7333 Typ := Corresponding_Concurrent_Type (Typ);
7334 In_Scope := False;
7335
7336 else
7337 return;
7338 end if;
7339 end if;
7340
7341 -- There is no overriding to check if is an inherited operation in a
7342 -- type derivation on for a generic actual.
7343
7344 Collect_Interfaces (Typ, Ifaces_List);
7345
7346 if Is_Empty_Elmt_List (Ifaces_List) then
7347 return;
7348 end if;
7349
7350 -- Determine whether entry or subprogram Def_Id overrides a primitive
7351 -- operation that belongs to one of the interfaces in Ifaces_List.
7352
7353 declare
7354 Candidate : Entity_Id := Empty;
7355 Hom : Entity_Id := Empty;
7356 Iface_Typ : Entity_Id;
7357 Subp : Entity_Id := Empty;
7358
7359 begin
7360 -- Traverse the homonym chain, looking at a potentially
7361 -- overridden subprogram that belongs to an implemented
7362 -- interface.
7363
7364 Hom := Current_Entity_In_Scope (Def_Id);
7365 while Present (Hom) loop
7366 Subp := Hom;
7367
7368 if Subp = Def_Id
7369 or else not Is_Overloadable (Subp)
7370 or else not Is_Primitive (Subp)
7371 or else not Is_Dispatching_Operation (Subp)
7372 or else not Present (Find_Dispatching_Type (Subp))
7373 or else not Is_Interface (Find_Dispatching_Type (Subp))
7374 then
7375 null;
7376
7377 -- Entries and procedures can override abstract or null
7378 -- interface procedures
7379
7380 elsif (Ekind (Def_Id) = E_Procedure
7381 or else Ekind (Def_Id) = E_Entry)
7382 and then Ekind (Subp) = E_Procedure
7383 and then Matches_Prefixed_View_Profile
7384 (Parameter_Specifications (Parent (Def_Id)),
7385 Parameter_Specifications (Parent (Subp)))
7386 then
7387 Candidate := Subp;
7388
7389 -- For an overridden subprogram Subp, check whether the mode
7390 -- of its first parameter is correct depending on the kind
7391 -- of synchronized type.
7392
7393 declare
7394 Formal : constant Node_Id := First_Formal (Candidate);
7395
7396 begin
7397 -- In order for an entry or a protected procedure to
7398 -- override, the first parameter of the overridden
7399 -- routine must be of mode "out", "in out" or
7400 -- access-to-variable.
7401
7402 if (Ekind (Candidate) = E_Entry
7403 or else Ekind (Candidate) = E_Procedure)
7404 and then Is_Protected_Type (Typ)
7405 and then Ekind (Formal) /= E_In_Out_Parameter
7406 and then Ekind (Formal) /= E_Out_Parameter
7407 and then Nkind (Parameter_Type (Parent (Formal)))
7408 /= N_Access_Definition
7409 then
7410 null;
7411
7412 -- All other cases are OK since a task entry or routine
7413 -- does not have a restriction on the mode of the first
7414 -- parameter of the overridden interface routine.
7415
7416 else
7417 Overridden_Subp := Candidate;
7418 return;
7419 end if;
7420 end;
7421
7422 -- Functions can override abstract interface functions
7423
7424 elsif Ekind (Def_Id) = E_Function
7425 and then Ekind (Subp) = E_Function
7426 and then Matches_Prefixed_View_Profile
7427 (Parameter_Specifications (Parent (Def_Id)),
7428 Parameter_Specifications (Parent (Subp)))
7429 and then Etype (Result_Definition (Parent (Def_Id))) =
7430 Etype (Result_Definition (Parent (Subp)))
7431 then
7432 Overridden_Subp := Subp;
7433 return;
7434 end if;
7435
7436 Hom := Homonym (Hom);
7437 end loop;
7438
7439 -- After examining all candidates for overriding, we are
7440 -- left with the best match which is a mode incompatible
7441 -- interface routine. Do not emit an error if the Expander
7442 -- is active since this error will be detected later on
7443 -- after all concurrent types are expanded and all wrappers
7444 -- are built. This check is meant for spec-only
7445 -- compilations.
7446
7447 if Present (Candidate)
7448 and then not Expander_Active
7449 then
7450 Iface_Typ :=
7451 Find_Parameter_Type (Parent (First_Formal (Candidate)));
7452
7453 -- Def_Id is primitive of a protected type, declared
7454 -- inside the type, and the candidate is primitive of a
7455 -- limited or synchronized interface.
7456
7457 if In_Scope
7458 and then Is_Protected_Type (Typ)
7459 and then
7460 (Is_Limited_Interface (Iface_Typ)
7461 or else Is_Protected_Interface (Iface_Typ)
7462 or else Is_Synchronized_Interface (Iface_Typ)
7463 or else Is_Task_Interface (Iface_Typ))
7464 then
7465 -- Must reword this message, comma before to in -gnatj
7466 -- mode ???
7467
7468 Error_Msg_NE
7469 ("first formal of & must be of mode `OUT`, `IN OUT`"
7470 & " or access-to-variable", Typ, Candidate);
7471 Error_Msg_N
7472 ("\to be overridden by protected procedure or entry "
7473 & "(RM 9.4(11.9/2))", Typ);
7474 end if;
7475 end if;
7476
7477 Overridden_Subp := Candidate;
7478 return;
7479 end;
7480 end Check_Synchronized_Overriding;
7481
7482 ----------------------------
7483 -- Is_Private_Declaration --
7484 ----------------------------
7485
7486 function Is_Private_Declaration (E : Entity_Id) return Boolean is
7487 Priv_Decls : List_Id;
7488 Decl : constant Node_Id := Unit_Declaration_Node (E);
7489
7490 begin
7491 if Is_Package_Or_Generic_Package (Current_Scope)
7492 and then In_Private_Part (Current_Scope)
7493 then
7494 Priv_Decls :=
7495 Private_Declarations (
7496 Specification (Unit_Declaration_Node (Current_Scope)));
7497
7498 return In_Package_Body (Current_Scope)
7499 or else
7500 (Is_List_Member (Decl)
7501 and then List_Containing (Decl) = Priv_Decls)
7502 or else (Nkind (Parent (Decl)) = N_Package_Specification
7503 and then not
7504 Is_Compilation_Unit
7505 (Defining_Entity (Parent (Decl)))
7506 and then List_Containing (Parent (Parent (Decl)))
7507 = Priv_Decls);
7508 else
7509 return False;
7510 end if;
7511 end Is_Private_Declaration;
7512
7513 --------------------------
7514 -- Is_Overriding_Alias --
7515 --------------------------
7516
7517 function Is_Overriding_Alias
7518 (Old_E : Entity_Id;
7519 New_E : Entity_Id) return Boolean
7520 is
7521 AO : constant Entity_Id := Alias (Old_E);
7522 AN : constant Entity_Id := Alias (New_E);
7523
7524 begin
7525 return Scope (AO) /= Scope (AN)
7526 or else No (DTC_Entity (AO))
7527 or else No (DTC_Entity (AN))
7528 or else DT_Position (AO) = DT_Position (AN);
7529 end Is_Overriding_Alias;
7530
7531 -- Start of processing for New_Overloaded_Entity
7532
7533 begin
7534 -- We need to look for an entity that S may override. This must be a
7535 -- homonym in the current scope, so we look for the first homonym of
7536 -- S in the current scope as the starting point for the search.
7537
7538 E := Current_Entity_In_Scope (S);
7539
7540 -- If there is no homonym then this is definitely not overriding
7541
7542 if No (E) then
7543 Enter_Overloaded_Entity (S);
7544 Check_Dispatching_Operation (S, Empty);
7545 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
7546
7547 -- If subprogram has an explicit declaration, check whether it
7548 -- has an overriding indicator.
7549
7550 if Comes_From_Source (S) then
7551 Check_Synchronized_Overriding (S, Overridden_Subp);
7552 Check_Overriding_Indicator
7553 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
7554 end if;
7555
7556 -- If there is a homonym that is not overloadable, then we have an
7557 -- error, except for the special cases checked explicitly below.
7558
7559 elsif not Is_Overloadable (E) then
7560
7561 -- Check for spurious conflict produced by a subprogram that has the
7562 -- same name as that of the enclosing generic package. The conflict
7563 -- occurs within an instance, between the subprogram and the renaming
7564 -- declaration for the package. After the subprogram, the package
7565 -- renaming declaration becomes hidden.
7566
7567 if Ekind (E) = E_Package
7568 and then Present (Renamed_Object (E))
7569 and then Renamed_Object (E) = Current_Scope
7570 and then Nkind (Parent (Renamed_Object (E))) =
7571 N_Package_Specification
7572 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
7573 then
7574 Set_Is_Hidden (E);
7575 Set_Is_Immediately_Visible (E, False);
7576 Enter_Overloaded_Entity (S);
7577 Set_Homonym (S, Homonym (E));
7578 Check_Dispatching_Operation (S, Empty);
7579 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
7580
7581 -- If the subprogram is implicit it is hidden by the previous
7582 -- declaration. However if it is dispatching, it must appear in the
7583 -- dispatch table anyway, because it can be dispatched to even if it
7584 -- cannot be called directly.
7585
7586 elsif Present (Alias (S))
7587 and then not Comes_From_Source (S)
7588 then
7589 Set_Scope (S, Current_Scope);
7590
7591 if Is_Dispatching_Operation (Alias (S)) then
7592 Check_Dispatching_Operation (S, Empty);
7593 end if;
7594
7595 return;
7596
7597 else
7598 Error_Msg_Sloc := Sloc (E);
7599
7600 -- Generate message, with useful additional warning if in generic
7601
7602 if Is_Generic_Unit (E) then
7603 Error_Msg_N ("previous generic unit cannot be overloaded", S);
7604 Error_Msg_N ("\& conflicts with declaration#", S);
7605 else
7606 Error_Msg_N ("& conflicts with declaration#", S);
7607 end if;
7608
7609 return;
7610 end if;
7611
7612 -- E exists and is overloadable
7613
7614 else
7615 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
7616 -- They are directly added to the list of primitive operations of
7617 -- Derived_Type, unless this is a rederivation in the private part
7618 -- of an operation that was already derived in the visible part of
7619 -- the current package.
7620
7621 if Ada_Version >= Ada_05
7622 and then Present (Derived_Type)
7623 and then Is_Dispatching_Operation (Alias (S))
7624 and then Present (Find_Dispatching_Type (Alias (S)))
7625 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
7626 then
7627 if Type_Conformant (E, S)
7628 and then Is_Package_Or_Generic_Package (Current_Scope)
7629 and then In_Private_Part (Current_Scope)
7630 and then Parent (E) /= Parent (S)
7631 and then Alias (E) = Alias (S)
7632 then
7633 Check_Operation_From_Private_View (S, E);
7634 else
7635 goto Add_New_Entity;
7636 end if;
7637 end if;
7638
7639 Check_Synchronized_Overriding (S, Overridden_Subp);
7640
7641 -- Loop through E and its homonyms to determine if any of them is
7642 -- the candidate for overriding by S.
7643
7644 while Present (E) loop
7645
7646 -- Definitely not interesting if not in the current scope
7647
7648 if Scope (E) /= Current_Scope then
7649 null;
7650
7651 -- Check if we have type conformance
7652
7653 elsif Type_Conformant (E, S) then
7654
7655 -- If the old and new entities have the same profile and one
7656 -- is not the body of the other, then this is an error, unless
7657 -- one of them is implicitly declared.
7658
7659 -- There are some cases when both can be implicit, for example
7660 -- when both a literal and a function that overrides it are
7661 -- inherited in a derivation, or when an inherited operation
7662 -- of a tagged full type overrides the inherited operation of
7663 -- a private extension. Ada 83 had a special rule for the
7664 -- literal case. In Ada95, the later implicit operation hides
7665 -- the former, and the literal is always the former. In the
7666 -- odd case where both are derived operations declared at the
7667 -- same point, both operations should be declared, and in that
7668 -- case we bypass the following test and proceed to the next
7669 -- part. This can only occur for certain obscure cases in
7670 -- instances, when an operation on a type derived from a formal
7671 -- private type does not override a homograph inherited from
7672 -- the actual. In subsequent derivations of such a type, the
7673 -- DT positions of these operations remain distinct, if they
7674 -- have been set.
7675
7676 if Present (Alias (S))
7677 and then (No (Alias (E))
7678 or else Comes_From_Source (E)
7679 or else Is_Abstract_Subprogram (S)
7680 or else
7681 (Is_Dispatching_Operation (E)
7682 and then Is_Overriding_Alias (E, S)))
7683 and then Ekind (E) /= E_Enumeration_Literal
7684 then
7685 -- When an derived operation is overloaded it may be due to
7686 -- the fact that the full view of a private extension
7687 -- re-inherits. It has to be dealt with.
7688
7689 if Is_Package_Or_Generic_Package (Current_Scope)
7690 and then In_Private_Part (Current_Scope)
7691 then
7692 Check_Operation_From_Private_View (S, E);
7693 end if;
7694
7695 -- In any case the implicit operation remains hidden by
7696 -- the existing declaration, which is overriding.
7697
7698 Set_Is_Overriding_Operation (E);
7699
7700 if Comes_From_Source (E) then
7701 Check_Overriding_Indicator (E, S, Is_Primitive => False);
7702
7703 -- Indicate that E overrides the operation from which
7704 -- S is inherited.
7705
7706 if Present (Alias (S)) then
7707 Set_Overridden_Operation (E, Alias (S));
7708 else
7709 Set_Overridden_Operation (E, S);
7710 end if;
7711 end if;
7712
7713 return;
7714
7715 -- Within an instance, the renaming declarations for actual
7716 -- subprograms may become ambiguous, but they do not hide each
7717 -- other.
7718
7719 elsif Ekind (E) /= E_Entry
7720 and then not Comes_From_Source (E)
7721 and then not Is_Generic_Instance (E)
7722 and then (Present (Alias (E))
7723 or else Is_Intrinsic_Subprogram (E))
7724 and then (not In_Instance
7725 or else No (Parent (E))
7726 or else Nkind (Unit_Declaration_Node (E)) /=
7727 N_Subprogram_Renaming_Declaration)
7728 then
7729 -- A subprogram child unit is not allowed to override an
7730 -- inherited subprogram (10.1.1(20)).
7731
7732 if Is_Child_Unit (S) then
7733 Error_Msg_N
7734 ("child unit overrides inherited subprogram in parent",
7735 S);
7736 return;
7737 end if;
7738
7739 if Is_Non_Overriding_Operation (E, S) then
7740 Enter_Overloaded_Entity (S);
7741
7742 if No (Derived_Type)
7743 or else Is_Tagged_Type (Derived_Type)
7744 then
7745 Check_Dispatching_Operation (S, Empty);
7746 end if;
7747
7748 return;
7749 end if;
7750
7751 -- E is a derived operation or an internal operator which
7752 -- is being overridden. Remove E from further visibility.
7753 -- Furthermore, if E is a dispatching operation, it must be
7754 -- replaced in the list of primitive operations of its type
7755 -- (see Override_Dispatching_Operation).
7756
7757 Overridden_Subp := E;
7758
7759 declare
7760 Prev : Entity_Id;
7761
7762 begin
7763 Prev := First_Entity (Current_Scope);
7764 while Present (Prev)
7765 and then Next_Entity (Prev) /= E
7766 loop
7767 Next_Entity (Prev);
7768 end loop;
7769
7770 -- It is possible for E to be in the current scope and
7771 -- yet not in the entity chain. This can only occur in a
7772 -- generic context where E is an implicit concatenation
7773 -- in the formal part, because in a generic body the
7774 -- entity chain starts with the formals.
7775
7776 pragma Assert
7777 (Present (Prev) or else Chars (E) = Name_Op_Concat);
7778
7779 -- E must be removed both from the entity_list of the
7780 -- current scope, and from the visibility chain
7781
7782 if Debug_Flag_E then
7783 Write_Str ("Override implicit operation ");
7784 Write_Int (Int (E));
7785 Write_Eol;
7786 end if;
7787
7788 -- If E is a predefined concatenation, it stands for four
7789 -- different operations. As a result, a single explicit
7790 -- declaration does not hide it. In a possible ambiguous
7791 -- situation, Disambiguate chooses the user-defined op,
7792 -- so it is correct to retain the previous internal one.
7793
7794 if Chars (E) /= Name_Op_Concat
7795 or else Ekind (E) /= E_Operator
7796 then
7797 -- For nondispatching derived operations that are
7798 -- overridden by a subprogram declared in the private
7799 -- part of a package, we retain the derived subprogram
7800 -- but mark it as not immediately visible. If the
7801 -- derived operation was declared in the visible part
7802 -- then this ensures that it will still be visible
7803 -- outside the package with the proper signature
7804 -- (calls from outside must also be directed to this
7805 -- version rather than the overriding one, unlike the
7806 -- dispatching case). Calls from inside the package
7807 -- will still resolve to the overriding subprogram
7808 -- since the derived one is marked as not visible
7809 -- within the package.
7810
7811 -- If the private operation is dispatching, we achieve
7812 -- the overriding by keeping the implicit operation
7813 -- but setting its alias to be the overriding one. In
7814 -- this fashion the proper body is executed in all
7815 -- cases, but the original signature is used outside
7816 -- of the package.
7817
7818 -- If the overriding is not in the private part, we
7819 -- remove the implicit operation altogether.
7820
7821 if Is_Private_Declaration (S) then
7822 if not Is_Dispatching_Operation (E) then
7823 Set_Is_Immediately_Visible (E, False);
7824 else
7825 -- Work done in Override_Dispatching_Operation,
7826 -- so nothing else need to be done here.
7827
7828 null;
7829 end if;
7830
7831 else
7832 -- Find predecessor of E in Homonym chain
7833
7834 if E = Current_Entity (E) then
7835 Prev_Vis := Empty;
7836 else
7837 Prev_Vis := Current_Entity (E);
7838 while Homonym (Prev_Vis) /= E loop
7839 Prev_Vis := Homonym (Prev_Vis);
7840 end loop;
7841 end if;
7842
7843 if Prev_Vis /= Empty then
7844
7845 -- Skip E in the visibility chain
7846
7847 Set_Homonym (Prev_Vis, Homonym (E));
7848
7849 else
7850 Set_Name_Entity_Id (Chars (E), Homonym (E));
7851 end if;
7852
7853 Set_Next_Entity (Prev, Next_Entity (E));
7854
7855 if No (Next_Entity (Prev)) then
7856 Set_Last_Entity (Current_Scope, Prev);
7857 end if;
7858
7859 end if;
7860 end if;
7861
7862 Enter_Overloaded_Entity (S);
7863 Set_Is_Overriding_Operation (S);
7864 Check_Overriding_Indicator (S, E, Is_Primitive => True);
7865
7866 -- If S is a user-defined subprogram or a null procedure
7867 -- expanded to override an inherited null procedure, then
7868 -- indicate that E overrides the operation from which S
7869 -- is inherited. It seems odd that Overridden_Operation
7870 -- isn't set in all cases where Is_Overriding_Operation
7871 -- is true, but doing so causes infinite loops in the
7872 -- compiler for implicit overriding subprograms. ???
7873
7874 if Comes_From_Source (S)
7875 or else
7876 (Present (Parent (S))
7877 and then
7878 Nkind (Parent (S)) = N_Procedure_Specification
7879 and then
7880 Null_Present (Parent (S)))
7881 then
7882 if Present (Alias (E)) then
7883 Set_Overridden_Operation (S, Alias (E));
7884 else
7885 Set_Overridden_Operation (S, E);
7886 end if;
7887 end if;
7888
7889 if Is_Dispatching_Operation (E) then
7890
7891 -- An overriding dispatching subprogram inherits the
7892 -- convention of the overridden subprogram (by
7893 -- AI-117).
7894
7895 Set_Convention (S, Convention (E));
7896 Check_Dispatching_Operation (S, E);
7897
7898 else
7899 Check_Dispatching_Operation (S, Empty);
7900 end if;
7901
7902 Check_For_Primitive_Subprogram
7903 (Is_Primitive_Subp, Is_Overriding => True);
7904 goto Check_Inequality;
7905 end;
7906
7907 -- Apparent redeclarations in instances can occur when two
7908 -- formal types get the same actual type. The subprograms in
7909 -- in the instance are legal, even if not callable from the
7910 -- outside. Calls from within are disambiguated elsewhere.
7911 -- For dispatching operations in the visible part, the usual
7912 -- rules apply, and operations with the same profile are not
7913 -- legal (B830001).
7914
7915 elsif (In_Instance_Visible_Part
7916 and then not Is_Dispatching_Operation (E))
7917 or else In_Instance_Not_Visible
7918 then
7919 null;
7920
7921 -- Here we have a real error (identical profile)
7922
7923 else
7924 Error_Msg_Sloc := Sloc (E);
7925
7926 -- Avoid cascaded errors if the entity appears in
7927 -- subsequent calls.
7928
7929 Set_Scope (S, Current_Scope);
7930
7931 -- Generate error, with extra useful warning for the case
7932 -- of a generic instance with no completion.
7933
7934 if Is_Generic_Instance (S)
7935 and then not Has_Completion (E)
7936 then
7937 Error_Msg_N
7938 ("instantiation cannot provide body for&", S);
7939 Error_Msg_N ("\& conflicts with declaration#", S);
7940 else
7941 Error_Msg_N ("& conflicts with declaration#", S);
7942 end if;
7943
7944 return;
7945 end if;
7946
7947 else
7948 -- If one subprogram has an access parameter and the other
7949 -- a parameter of an access type, calls to either might be
7950 -- ambiguous. Verify that parameters match except for the
7951 -- access parameter.
7952
7953 if May_Hide_Profile then
7954 declare
7955 F1 : Entity_Id;
7956 F2 : Entity_Id;
7957
7958 begin
7959 F1 := First_Formal (S);
7960 F2 := First_Formal (E);
7961 while Present (F1) and then Present (F2) loop
7962 if Is_Access_Type (Etype (F1)) then
7963 if not Is_Access_Type (Etype (F2))
7964 or else not Conforming_Types
7965 (Designated_Type (Etype (F1)),
7966 Designated_Type (Etype (F2)),
7967 Type_Conformant)
7968 then
7969 May_Hide_Profile := False;
7970 end if;
7971
7972 elsif
7973 not Conforming_Types
7974 (Etype (F1), Etype (F2), Type_Conformant)
7975 then
7976 May_Hide_Profile := False;
7977 end if;
7978
7979 Next_Formal (F1);
7980 Next_Formal (F2);
7981 end loop;
7982
7983 if May_Hide_Profile
7984 and then No (F1)
7985 and then No (F2)
7986 then
7987 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
7988 end if;
7989 end;
7990 end if;
7991 end if;
7992
7993 E := Homonym (E);
7994 end loop;
7995
7996 <<Add_New_Entity>>
7997
7998 -- On exit, we know that S is a new entity
7999
8000 Enter_Overloaded_Entity (S);
8001 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
8002 Check_Overriding_Indicator
8003 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
8004
8005 -- If S is a derived operation for an untagged type then by
8006 -- definition it's not a dispatching operation (even if the parent
8007 -- operation was dispatching), so we don't call
8008 -- Check_Dispatching_Operation in that case.
8009
8010 if No (Derived_Type)
8011 or else Is_Tagged_Type (Derived_Type)
8012 then
8013 Check_Dispatching_Operation (S, Empty);
8014 end if;
8015 end if;
8016
8017 -- If this is a user-defined equality operator that is not a derived
8018 -- subprogram, create the corresponding inequality. If the operation is
8019 -- dispatching, the expansion is done elsewhere, and we do not create
8020 -- an explicit inequality operation.
8021
8022 <<Check_Inequality>>
8023 if Chars (S) = Name_Op_Eq
8024 and then Etype (S) = Standard_Boolean
8025 and then Present (Parent (S))
8026 and then not Is_Dispatching_Operation (S)
8027 then
8028 Make_Inequality_Operator (S);
8029
8030 if Ada_Version >= Ada_12 then
8031 Check_Untagged_Equality (S);
8032 end if;
8033 end if;
8034 end New_Overloaded_Entity;
8035
8036 ---------------------
8037 -- Process_Formals --
8038 ---------------------
8039
8040 procedure Process_Formals
8041 (T : List_Id;
8042 Related_Nod : Node_Id)
8043 is
8044 Param_Spec : Node_Id;
8045 Formal : Entity_Id;
8046 Formal_Type : Entity_Id;
8047 Default : Node_Id;
8048 Ptype : Entity_Id;
8049
8050 Num_Out_Params : Nat := 0;
8051 First_Out_Param : Entity_Id := Empty;
8052 -- Used for setting Is_Only_Out_Parameter
8053
8054 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
8055 -- Determine whether an access type designates a type coming from a
8056 -- limited view.
8057
8058 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
8059 -- Check whether the default has a class-wide type. After analysis the
8060 -- default has the type of the formal, so we must also check explicitly
8061 -- for an access attribute.
8062
8063 -------------------------------
8064 -- Designates_From_With_Type --
8065 -------------------------------
8066
8067 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
8068 Desig : Entity_Id := Typ;
8069
8070 begin
8071 if Is_Access_Type (Desig) then
8072 Desig := Directly_Designated_Type (Desig);
8073 end if;
8074
8075 if Is_Class_Wide_Type (Desig) then
8076 Desig := Root_Type (Desig);
8077 end if;
8078
8079 return
8080 Ekind (Desig) = E_Incomplete_Type
8081 and then From_With_Type (Desig);
8082 end Designates_From_With_Type;
8083
8084 ---------------------------
8085 -- Is_Class_Wide_Default --
8086 ---------------------------
8087
8088 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
8089 begin
8090 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
8091 or else (Nkind (D) = N_Attribute_Reference
8092 and then Attribute_Name (D) = Name_Access
8093 and then Is_Class_Wide_Type (Etype (Prefix (D))));
8094 end Is_Class_Wide_Default;
8095
8096 -- Start of processing for Process_Formals
8097
8098 begin
8099 -- In order to prevent premature use of the formals in the same formal
8100 -- part, the Ekind is left undefined until all default expressions are
8101 -- analyzed. The Ekind is established in a separate loop at the end.
8102
8103 Param_Spec := First (T);
8104 while Present (Param_Spec) loop
8105 Formal := Defining_Identifier (Param_Spec);
8106 Set_Never_Set_In_Source (Formal, True);
8107 Enter_Name (Formal);
8108
8109 -- Case of ordinary parameters
8110
8111 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
8112 Find_Type (Parameter_Type (Param_Spec));
8113 Ptype := Parameter_Type (Param_Spec);
8114
8115 if Ptype = Error then
8116 goto Continue;
8117 end if;
8118
8119 Formal_Type := Entity (Ptype);
8120
8121 if Is_Incomplete_Type (Formal_Type)
8122 or else
8123 (Is_Class_Wide_Type (Formal_Type)
8124 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
8125 then
8126 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
8127 -- primitive operations, as long as their completion is
8128 -- in the same declarative part. If in the private part
8129 -- this means that the type cannot be a Taft-amendment type.
8130 -- Check is done on package exit. For access to subprograms,
8131 -- the use is legal for Taft-amendment types.
8132
8133 if Is_Tagged_Type (Formal_Type) then
8134 if Ekind (Scope (Current_Scope)) = E_Package
8135 and then In_Private_Part (Scope (Current_Scope))
8136 and then not From_With_Type (Formal_Type)
8137 and then not Is_Class_Wide_Type (Formal_Type)
8138 then
8139 if not Nkind_In
8140 (Parent (T), N_Access_Function_Definition,
8141 N_Access_Procedure_Definition)
8142 then
8143 Append_Elmt
8144 (Current_Scope,
8145 Private_Dependents (Base_Type (Formal_Type)));
8146 end if;
8147 end if;
8148
8149 -- Special handling of Value_Type for CIL case
8150
8151 elsif Is_Value_Type (Formal_Type) then
8152 null;
8153
8154 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
8155 N_Access_Procedure_Definition)
8156 then
8157 Error_Msg_NE
8158 ("invalid use of incomplete type&",
8159 Param_Spec, Formal_Type);
8160
8161 -- Further checks on the legality of incomplete types
8162 -- in formal parts must be delayed until the freeze point
8163 -- of the enclosing subprogram or access to subprogram.
8164 end if;
8165
8166 elsif Ekind (Formal_Type) = E_Void then
8167 Error_Msg_NE ("premature use of&",
8168 Parameter_Type (Param_Spec), Formal_Type);
8169 end if;
8170
8171 -- Ada 2005 (AI-231): Create and decorate an internal subtype
8172 -- declaration corresponding to the null-excluding type of the
8173 -- formal in the enclosing scope. Finally, replace the parameter
8174 -- type of the formal with the internal subtype.
8175
8176 if Ada_Version >= Ada_05
8177 and then Null_Exclusion_Present (Param_Spec)
8178 then
8179 if not Is_Access_Type (Formal_Type) then
8180 Error_Msg_N
8181 ("`NOT NULL` allowed only for an access type", Param_Spec);
8182
8183 else
8184 if Can_Never_Be_Null (Formal_Type)
8185 and then Comes_From_Source (Related_Nod)
8186 then
8187 Error_Msg_NE
8188 ("`NOT NULL` not allowed (& already excludes null)",
8189 Param_Spec,
8190 Formal_Type);
8191 end if;
8192
8193 Formal_Type :=
8194 Create_Null_Excluding_Itype
8195 (T => Formal_Type,
8196 Related_Nod => Related_Nod,
8197 Scope_Id => Scope (Current_Scope));
8198
8199 -- If the designated type of the itype is an itype we
8200 -- decorate it with the Has_Delayed_Freeze attribute to
8201 -- avoid problems with the backend.
8202
8203 -- Example:
8204 -- type T is access procedure;
8205 -- procedure Op (O : not null T);
8206
8207 if Is_Itype (Directly_Designated_Type (Formal_Type)) then
8208 Set_Has_Delayed_Freeze (Formal_Type);
8209 end if;
8210 end if;
8211 end if;
8212
8213 -- An access formal type
8214
8215 else
8216 Formal_Type :=
8217 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
8218
8219 -- No need to continue if we already notified errors
8220
8221 if not Present (Formal_Type) then
8222 return;
8223 end if;
8224
8225 -- Ada 2005 (AI-254)
8226
8227 declare
8228 AD : constant Node_Id :=
8229 Access_To_Subprogram_Definition
8230 (Parameter_Type (Param_Spec));
8231 begin
8232 if Present (AD) and then Protected_Present (AD) then
8233 Formal_Type :=
8234 Replace_Anonymous_Access_To_Protected_Subprogram
8235 (Param_Spec);
8236 end if;
8237 end;
8238 end if;
8239
8240 Set_Etype (Formal, Formal_Type);
8241 Default := Expression (Param_Spec);
8242
8243 if Present (Default) then
8244 if Out_Present (Param_Spec) then
8245 Error_Msg_N
8246 ("default initialization only allowed for IN parameters",
8247 Param_Spec);
8248 end if;
8249
8250 -- Do the special preanalysis of the expression (see section on
8251 -- "Handling of Default Expressions" in the spec of package Sem).
8252
8253 Preanalyze_Spec_Expression (Default, Formal_Type);
8254
8255 -- An access to constant cannot be the default for
8256 -- an access parameter that is an access to variable.
8257
8258 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8259 and then not Is_Access_Constant (Formal_Type)
8260 and then Is_Access_Type (Etype (Default))
8261 and then Is_Access_Constant (Etype (Default))
8262 then
8263 Error_Msg_N
8264 ("formal that is access to variable cannot be initialized " &
8265 "with an access-to-constant expression", Default);
8266 end if;
8267
8268 -- Check that the designated type of an access parameter's default
8269 -- is not a class-wide type unless the parameter's designated type
8270 -- is also class-wide.
8271
8272 if Ekind (Formal_Type) = E_Anonymous_Access_Type
8273 and then not Designates_From_With_Type (Formal_Type)
8274 and then Is_Class_Wide_Default (Default)
8275 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
8276 then
8277 Error_Msg_N
8278 ("access to class-wide expression not allowed here", Default);
8279 end if;
8280
8281 -- Check incorrect use of dynamically tagged expressions
8282
8283 if Is_Tagged_Type (Formal_Type) then
8284 Check_Dynamically_Tagged_Expression
8285 (Expr => Default,
8286 Typ => Formal_Type,
8287 Related_Nod => Default);
8288 end if;
8289 end if;
8290
8291 -- Ada 2005 (AI-231): Static checks
8292
8293 if Ada_Version >= Ada_05
8294 and then Is_Access_Type (Etype (Formal))
8295 and then Can_Never_Be_Null (Etype (Formal))
8296 then
8297 Null_Exclusion_Static_Checks (Param_Spec);
8298 end if;
8299
8300 <<Continue>>
8301 Next (Param_Spec);
8302 end loop;
8303
8304 -- If this is the formal part of a function specification, analyze the
8305 -- subtype mark in the context where the formals are visible but not
8306 -- yet usable, and may hide outer homographs.
8307
8308 if Nkind (Related_Nod) = N_Function_Specification then
8309 Analyze_Return_Type (Related_Nod);
8310 end if;
8311
8312 -- Now set the kind (mode) of each formal
8313
8314 Param_Spec := First (T);
8315
8316 while Present (Param_Spec) loop
8317 Formal := Defining_Identifier (Param_Spec);
8318 Set_Formal_Mode (Formal);
8319
8320 if Ekind (Formal) = E_In_Parameter then
8321 Set_Default_Value (Formal, Expression (Param_Spec));
8322
8323 if Present (Expression (Param_Spec)) then
8324 Default := Expression (Param_Spec);
8325
8326 if Is_Scalar_Type (Etype (Default)) then
8327 if Nkind
8328 (Parameter_Type (Param_Spec)) /= N_Access_Definition
8329 then
8330 Formal_Type := Entity (Parameter_Type (Param_Spec));
8331
8332 else
8333 Formal_Type := Access_Definition
8334 (Related_Nod, Parameter_Type (Param_Spec));
8335 end if;
8336
8337 Apply_Scalar_Range_Check (Default, Formal_Type);
8338 end if;
8339 end if;
8340
8341 elsif Ekind (Formal) = E_Out_Parameter then
8342 Num_Out_Params := Num_Out_Params + 1;
8343
8344 if Num_Out_Params = 1 then
8345 First_Out_Param := Formal;
8346 end if;
8347
8348 elsif Ekind (Formal) = E_In_Out_Parameter then
8349 Num_Out_Params := Num_Out_Params + 1;
8350 end if;
8351
8352 Next (Param_Spec);
8353 end loop;
8354
8355 if Present (First_Out_Param) and then Num_Out_Params = 1 then
8356 Set_Is_Only_Out_Parameter (First_Out_Param);
8357 end if;
8358 end Process_Formals;
8359
8360 ------------------
8361 -- Process_PPCs --
8362 ------------------
8363
8364 procedure Process_PPCs
8365 (N : Node_Id;
8366 Spec_Id : Entity_Id;
8367 Body_Id : Entity_Id)
8368 is
8369 Loc : constant Source_Ptr := Sloc (N);
8370 Prag : Node_Id;
8371 Plist : List_Id := No_List;
8372 Subp : Entity_Id;
8373 Parms : List_Id;
8374
8375 function Grab_PPC (Nam : Name_Id) return Node_Id;
8376 -- Prag contains an analyzed precondition or postcondition pragma.
8377 -- This function copies the pragma, changes it to the corresponding
8378 -- Check pragma and returns the Check pragma as the result. The
8379 -- argument Nam is either Name_Precondition or Name_Postcondition.
8380
8381 --------------
8382 -- Grab_PPC --
8383 --------------
8384
8385 function Grab_PPC (Nam : Name_Id) return Node_Id is
8386 CP : constant Node_Id := New_Copy_Tree (Prag);
8387
8388 begin
8389 -- Set Analyzed to false, since we want to reanalyze the check
8390 -- procedure. Note that it is only at the outer level that we
8391 -- do this fiddling, for the spec cases, the already preanalyzed
8392 -- parameters are not affected.
8393
8394 -- For a postcondition pragma within a generic, preserve the pragma
8395 -- for later expansion.
8396
8397 Set_Analyzed (CP, False);
8398
8399 if Nam = Name_Postcondition
8400 and then not Expander_Active
8401 then
8402 return CP;
8403 end if;
8404
8405 -- Change pragma into corresponding pragma Check
8406
8407 Prepend_To (Pragma_Argument_Associations (CP),
8408 Make_Pragma_Argument_Association (Sloc (Prag),
8409 Expression =>
8410 Make_Identifier (Loc,
8411 Chars => Nam)));
8412 Set_Pragma_Identifier (CP,
8413 Make_Identifier (Sloc (Prag),
8414 Chars => Name_Check));
8415
8416 return CP;
8417 end Grab_PPC;
8418
8419 -- Start of processing for Process_PPCs
8420
8421 begin
8422 -- Nothing to do if we are not generating code
8423
8424 if Operating_Mode /= Generate_Code then
8425 return;
8426 end if;
8427
8428 -- Grab preconditions from spec
8429
8430 if Present (Spec_Id) then
8431
8432 -- Loop through PPC pragmas from spec. Note that preconditions from
8433 -- the body will be analyzed and converted when we scan the body
8434 -- declarations below.
8435
8436 Prag := Spec_PPC_List (Spec_Id);
8437 while Present (Prag) loop
8438 if Pragma_Name (Prag) = Name_Precondition
8439 and then Pragma_Enabled (Prag)
8440 then
8441 -- Add pragma Check at the start of the declarations of N.
8442 -- Note that this processing reverses the order of the list,
8443 -- which is what we want since new entries were chained to
8444 -- the head of the list.
8445
8446 Prepend (Grab_PPC (Name_Precondition), Declarations (N));
8447 end if;
8448
8449 Prag := Next_Pragma (Prag);
8450 end loop;
8451 end if;
8452
8453 -- Build postconditions procedure if needed and prepend the following
8454 -- declaration to the start of the declarations for the subprogram.
8455
8456 -- procedure _postconditions [(_Result : resulttype)] is
8457 -- begin
8458 -- pragma Check (Postcondition, condition [,message]);
8459 -- pragma Check (Postcondition, condition [,message]);
8460 -- ...
8461 -- end;
8462
8463 -- First we deal with the postconditions in the body
8464
8465 if Is_Non_Empty_List (Declarations (N)) then
8466
8467 -- Loop through declarations
8468
8469 Prag := First (Declarations (N));
8470 while Present (Prag) loop
8471 if Nkind (Prag) = N_Pragma then
8472
8473 -- If pragma, capture if enabled postcondition, else ignore
8474
8475 if Pragma_Name (Prag) = Name_Postcondition
8476 and then Check_Enabled (Name_Postcondition)
8477 then
8478 if Plist = No_List then
8479 Plist := Empty_List;
8480 end if;
8481
8482 Analyze (Prag);
8483
8484 -- If expansion is disabled, as in a generic unit,
8485 -- save pragma for later expansion.
8486
8487 if not Expander_Active then
8488 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8489 else
8490 Append (Grab_PPC (Name_Postcondition), Plist);
8491 end if;
8492 end if;
8493
8494 Next (Prag);
8495
8496 -- Not a pragma, if comes from source, then end scan
8497
8498 elsif Comes_From_Source (Prag) then
8499 exit;
8500
8501 -- Skip stuff not coming from source
8502
8503 else
8504 Next (Prag);
8505 end if;
8506 end loop;
8507 end if;
8508
8509 -- Now deal with any postconditions from the spec
8510
8511 if Present (Spec_Id) then
8512
8513 -- Loop through PPC pragmas from spec
8514
8515 Prag := Spec_PPC_List (Spec_Id);
8516 while Present (Prag) loop
8517 if Pragma_Name (Prag) = Name_Postcondition
8518 and then Pragma_Enabled (Prag)
8519 then
8520 if Plist = No_List then
8521 Plist := Empty_List;
8522 end if;
8523
8524 if not Expander_Active then
8525 Prepend (Grab_PPC (Name_Postcondition), Declarations (N));
8526 else
8527 Append (Grab_PPC (Name_Postcondition), Plist);
8528 end if;
8529 end if;
8530
8531 Prag := Next_Pragma (Prag);
8532 end loop;
8533 end if;
8534
8535 -- If we had any postconditions and expansion is enabled, build
8536 -- the _Postconditions procedure.
8537
8538 if Present (Plist)
8539 and then Expander_Active
8540 then
8541 Subp := Defining_Entity (N);
8542
8543 if Etype (Subp) /= Standard_Void_Type then
8544 Parms := New_List (
8545 Make_Parameter_Specification (Loc,
8546 Defining_Identifier =>
8547 Make_Defining_Identifier (Loc,
8548 Chars => Name_uResult),
8549 Parameter_Type => New_Occurrence_Of (Etype (Subp), Loc)));
8550 else
8551 Parms := No_List;
8552 end if;
8553
8554 declare
8555 Post_Proc : constant Entity_Id :=
8556 Make_Defining_Identifier (Loc,
8557 Chars => Name_uPostconditions);
8558 -- The entity for the _Postconditions procedure
8559 begin
8560 Prepend_To (Declarations (N),
8561 Make_Subprogram_Body (Loc,
8562 Specification =>
8563 Make_Procedure_Specification (Loc,
8564 Defining_Unit_Name => Post_Proc,
8565 Parameter_Specifications => Parms),
8566
8567 Declarations => Empty_List,
8568
8569 Handled_Statement_Sequence =>
8570 Make_Handled_Sequence_Of_Statements (Loc,
8571 Statements => Plist)));
8572
8573 -- If this is a procedure, set the Postcondition_Proc attribute on
8574 -- the proper defining entity for the subprogram.
8575
8576 if Etype (Subp) = Standard_Void_Type then
8577 if Present (Spec_Id) then
8578 Set_Postcondition_Proc (Spec_Id, Post_Proc);
8579 else
8580 Set_Postcondition_Proc (Body_Id, Post_Proc);
8581 end if;
8582 end if;
8583 end;
8584
8585 if Present (Spec_Id) then
8586 Set_Has_Postconditions (Spec_Id);
8587 else
8588 Set_Has_Postconditions (Body_Id);
8589 end if;
8590 end if;
8591 end Process_PPCs;
8592
8593 ----------------------------
8594 -- Reference_Body_Formals --
8595 ----------------------------
8596
8597 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
8598 Fs : Entity_Id;
8599 Fb : Entity_Id;
8600
8601 begin
8602 if Error_Posted (Spec) then
8603 return;
8604 end if;
8605
8606 -- Iterate over both lists. They may be of different lengths if the two
8607 -- specs are not conformant.
8608
8609 Fs := First_Formal (Spec);
8610 Fb := First_Formal (Bod);
8611 while Present (Fs) and then Present (Fb) loop
8612 Generate_Reference (Fs, Fb, 'b');
8613
8614 if Style_Check then
8615 Style.Check_Identifier (Fb, Fs);
8616 end if;
8617
8618 Set_Spec_Entity (Fb, Fs);
8619 Set_Referenced (Fs, False);
8620 Next_Formal (Fs);
8621 Next_Formal (Fb);
8622 end loop;
8623 end Reference_Body_Formals;
8624
8625 -------------------------
8626 -- Set_Actual_Subtypes --
8627 -------------------------
8628
8629 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
8630 Loc : constant Source_Ptr := Sloc (N);
8631 Decl : Node_Id;
8632 Formal : Entity_Id;
8633 T : Entity_Id;
8634 First_Stmt : Node_Id := Empty;
8635 AS_Needed : Boolean;
8636
8637 begin
8638 -- If this is an empty initialization procedure, no need to create
8639 -- actual subtypes (small optimization).
8640
8641 if Ekind (Subp) = E_Procedure
8642 and then Is_Null_Init_Proc (Subp)
8643 then
8644 return;
8645 end if;
8646
8647 Formal := First_Formal (Subp);
8648 while Present (Formal) loop
8649 T := Etype (Formal);
8650
8651 -- We never need an actual subtype for a constrained formal
8652
8653 if Is_Constrained (T) then
8654 AS_Needed := False;
8655
8656 -- If we have unknown discriminants, then we do not need an actual
8657 -- subtype, or more accurately we cannot figure it out! Note that
8658 -- all class-wide types have unknown discriminants.
8659
8660 elsif Has_Unknown_Discriminants (T) then
8661 AS_Needed := False;
8662
8663 -- At this stage we have an unconstrained type that may need an
8664 -- actual subtype. For sure the actual subtype is needed if we have
8665 -- an unconstrained array type.
8666
8667 elsif Is_Array_Type (T) then
8668 AS_Needed := True;
8669
8670 -- The only other case needing an actual subtype is an unconstrained
8671 -- record type which is an IN parameter (we cannot generate actual
8672 -- subtypes for the OUT or IN OUT case, since an assignment can
8673 -- change the discriminant values. However we exclude the case of
8674 -- initialization procedures, since discriminants are handled very
8675 -- specially in this context, see the section entitled "Handling of
8676 -- Discriminants" in Einfo.
8677
8678 -- We also exclude the case of Discrim_SO_Functions (functions used
8679 -- in front end layout mode for size/offset values), since in such
8680 -- functions only discriminants are referenced, and not only are such
8681 -- subtypes not needed, but they cannot always be generated, because
8682 -- of order of elaboration issues.
8683
8684 elsif Is_Record_Type (T)
8685 and then Ekind (Formal) = E_In_Parameter
8686 and then Chars (Formal) /= Name_uInit
8687 and then not Is_Unchecked_Union (T)
8688 and then not Is_Discrim_SO_Function (Subp)
8689 then
8690 AS_Needed := True;
8691
8692 -- All other cases do not need an actual subtype
8693
8694 else
8695 AS_Needed := False;
8696 end if;
8697
8698 -- Generate actual subtypes for unconstrained arrays and
8699 -- unconstrained discriminated records.
8700
8701 if AS_Needed then
8702 if Nkind (N) = N_Accept_Statement then
8703
8704 -- If expansion is active, The formal is replaced by a local
8705 -- variable that renames the corresponding entry of the
8706 -- parameter block, and it is this local variable that may
8707 -- require an actual subtype.
8708
8709 if Expander_Active then
8710 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
8711 else
8712 Decl := Build_Actual_Subtype (T, Formal);
8713 end if;
8714
8715 if Present (Handled_Statement_Sequence (N)) then
8716 First_Stmt :=
8717 First (Statements (Handled_Statement_Sequence (N)));
8718 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
8719 Mark_Rewrite_Insertion (Decl);
8720 else
8721 -- If the accept statement has no body, there will be no
8722 -- reference to the actuals, so no need to compute actual
8723 -- subtypes.
8724
8725 return;
8726 end if;
8727
8728 else
8729 Decl := Build_Actual_Subtype (T, Formal);
8730 Prepend (Decl, Declarations (N));
8731 Mark_Rewrite_Insertion (Decl);
8732 end if;
8733
8734 -- The declaration uses the bounds of an existing object, and
8735 -- therefore needs no constraint checks.
8736
8737 Analyze (Decl, Suppress => All_Checks);
8738
8739 -- We need to freeze manually the generated type when it is
8740 -- inserted anywhere else than in a declarative part.
8741
8742 if Present (First_Stmt) then
8743 Insert_List_Before_And_Analyze (First_Stmt,
8744 Freeze_Entity (Defining_Identifier (Decl), Loc));
8745 end if;
8746
8747 if Nkind (N) = N_Accept_Statement
8748 and then Expander_Active
8749 then
8750 Set_Actual_Subtype (Renamed_Object (Formal),
8751 Defining_Identifier (Decl));
8752 else
8753 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
8754 end if;
8755 end if;
8756
8757 Next_Formal (Formal);
8758 end loop;
8759 end Set_Actual_Subtypes;
8760
8761 ---------------------
8762 -- Set_Formal_Mode --
8763 ---------------------
8764
8765 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
8766 Spec : constant Node_Id := Parent (Formal_Id);
8767
8768 begin
8769 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
8770 -- since we ensure that corresponding actuals are always valid at the
8771 -- point of the call.
8772
8773 if Out_Present (Spec) then
8774 if Ekind (Scope (Formal_Id)) = E_Function
8775 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
8776 then
8777 Error_Msg_N ("functions can only have IN parameters", Spec);
8778 Set_Ekind (Formal_Id, E_In_Parameter);
8779
8780 elsif In_Present (Spec) then
8781 Set_Ekind (Formal_Id, E_In_Out_Parameter);
8782
8783 else
8784 Set_Ekind (Formal_Id, E_Out_Parameter);
8785 Set_Never_Set_In_Source (Formal_Id, True);
8786 Set_Is_True_Constant (Formal_Id, False);
8787 Set_Current_Value (Formal_Id, Empty);
8788 end if;
8789
8790 else
8791 Set_Ekind (Formal_Id, E_In_Parameter);
8792 end if;
8793
8794 -- Set Is_Known_Non_Null for access parameters since the language
8795 -- guarantees that access parameters are always non-null. We also set
8796 -- Can_Never_Be_Null, since there is no way to change the value.
8797
8798 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
8799
8800 -- Ada 2005 (AI-231): In Ada95, access parameters are always non-
8801 -- null; In Ada 2005, only if then null_exclusion is explicit.
8802
8803 if Ada_Version < Ada_05
8804 or else Can_Never_Be_Null (Etype (Formal_Id))
8805 then
8806 Set_Is_Known_Non_Null (Formal_Id);
8807 Set_Can_Never_Be_Null (Formal_Id);
8808 end if;
8809
8810 -- Ada 2005 (AI-231): Null-exclusion access subtype
8811
8812 elsif Is_Access_Type (Etype (Formal_Id))
8813 and then Can_Never_Be_Null (Etype (Formal_Id))
8814 then
8815 Set_Is_Known_Non_Null (Formal_Id);
8816 end if;
8817
8818 Set_Mechanism (Formal_Id, Default_Mechanism);
8819 Set_Formal_Validity (Formal_Id);
8820 end Set_Formal_Mode;
8821
8822 -------------------------
8823 -- Set_Formal_Validity --
8824 -------------------------
8825
8826 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
8827 begin
8828 -- If no validity checking, then we cannot assume anything about the
8829 -- validity of parameters, since we do not know there is any checking
8830 -- of the validity on the call side.
8831
8832 if not Validity_Checks_On then
8833 return;
8834
8835 -- If validity checking for parameters is enabled, this means we are
8836 -- not supposed to make any assumptions about argument values.
8837
8838 elsif Validity_Check_Parameters then
8839 return;
8840
8841 -- If we are checking in parameters, we will assume that the caller is
8842 -- also checking parameters, so we can assume the parameter is valid.
8843
8844 elsif Ekind (Formal_Id) = E_In_Parameter
8845 and then Validity_Check_In_Params
8846 then
8847 Set_Is_Known_Valid (Formal_Id, True);
8848
8849 -- Similar treatment for IN OUT parameters
8850
8851 elsif Ekind (Formal_Id) = E_In_Out_Parameter
8852 and then Validity_Check_In_Out_Params
8853 then
8854 Set_Is_Known_Valid (Formal_Id, True);
8855 end if;
8856 end Set_Formal_Validity;
8857
8858 ------------------------
8859 -- Subtype_Conformant --
8860 ------------------------
8861
8862 function Subtype_Conformant
8863 (New_Id : Entity_Id;
8864 Old_Id : Entity_Id;
8865 Skip_Controlling_Formals : Boolean := False) return Boolean
8866 is
8867 Result : Boolean;
8868 begin
8869 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
8870 Skip_Controlling_Formals => Skip_Controlling_Formals);
8871 return Result;
8872 end Subtype_Conformant;
8873
8874 ---------------------
8875 -- Type_Conformant --
8876 ---------------------
8877
8878 function Type_Conformant
8879 (New_Id : Entity_Id;
8880 Old_Id : Entity_Id;
8881 Skip_Controlling_Formals : Boolean := False) return Boolean
8882 is
8883 Result : Boolean;
8884 begin
8885 May_Hide_Profile := False;
8886
8887 Check_Conformance
8888 (New_Id, Old_Id, Type_Conformant, False, Result,
8889 Skip_Controlling_Formals => Skip_Controlling_Formals);
8890 return Result;
8891 end Type_Conformant;
8892
8893 -------------------------------
8894 -- Valid_Operator_Definition --
8895 -------------------------------
8896
8897 procedure Valid_Operator_Definition (Designator : Entity_Id) is
8898 N : Integer := 0;
8899 F : Entity_Id;
8900 Id : constant Name_Id := Chars (Designator);
8901 N_OK : Boolean;
8902
8903 begin
8904 F := First_Formal (Designator);
8905 while Present (F) loop
8906 N := N + 1;
8907
8908 if Present (Default_Value (F)) then
8909 Error_Msg_N
8910 ("default values not allowed for operator parameters",
8911 Parent (F));
8912 end if;
8913
8914 Next_Formal (F);
8915 end loop;
8916
8917 -- Verify that user-defined operators have proper number of arguments
8918 -- First case of operators which can only be unary
8919
8920 if Id = Name_Op_Not
8921 or else Id = Name_Op_Abs
8922 then
8923 N_OK := (N = 1);
8924
8925 -- Case of operators which can be unary or binary
8926
8927 elsif Id = Name_Op_Add
8928 or Id = Name_Op_Subtract
8929 then
8930 N_OK := (N in 1 .. 2);
8931
8932 -- All other operators can only be binary
8933
8934 else
8935 N_OK := (N = 2);
8936 end if;
8937
8938 if not N_OK then
8939 Error_Msg_N
8940 ("incorrect number of arguments for operator", Designator);
8941 end if;
8942
8943 if Id = Name_Op_Ne
8944 and then Base_Type (Etype (Designator)) = Standard_Boolean
8945 and then not Is_Intrinsic_Subprogram (Designator)
8946 then
8947 Error_Msg_N
8948 ("explicit definition of inequality not allowed", Designator);
8949 end if;
8950 end Valid_Operator_Definition;
8951
8952 end Sem_Ch6;