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