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