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