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