[Ada] Implement AI12-0343 Return Statement Checks
[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-2020, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Einfo; use Einfo;
32 with Elists; use Elists;
33 with Errout; use Errout;
34 with Expander; use Expander;
35 with Exp_Ch6; use Exp_Ch6;
36 with Exp_Ch7; use Exp_Ch7;
37 with Exp_Ch9; use Exp_Ch9;
38 with Exp_Dbug; use Exp_Dbug;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Inline; use Inline;
44 with Itypes; use Itypes;
45 with Lib.Xref; use Lib.Xref;
46 with Layout; use Layout;
47 with Namet; use Namet;
48 with Lib; use Lib;
49 with Nlists; use Nlists;
50 with Nmake; use Nmake;
51 with Opt; use Opt;
52 with Output; use Output;
53 with Restrict; use Restrict;
54 with Rtsfind; use Rtsfind;
55 with Sem; use Sem;
56 with Sem_Aux; use Sem_Aux;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch3; use Sem_Ch3;
59 with Sem_Ch4; use Sem_Ch4;
60 with Sem_Ch5; use Sem_Ch5;
61 with Sem_Ch8; use Sem_Ch8;
62 with Sem_Ch9; use Sem_Ch9;
63 with Sem_Ch10; use Sem_Ch10;
64 with Sem_Ch12; use Sem_Ch12;
65 with Sem_Ch13; use Sem_Ch13;
66 with Sem_Dim; use Sem_Dim;
67 with Sem_Disp; use Sem_Disp;
68 with Sem_Dist; use Sem_Dist;
69 with Sem_Elim; use Sem_Elim;
70 with Sem_Eval; use Sem_Eval;
71 with Sem_Mech; use Sem_Mech;
72 with Sem_Prag; use Sem_Prag;
73 with Sem_Res; use Sem_Res;
74 with Sem_Util; use Sem_Util;
75 with Sem_Type; use Sem_Type;
76 with Sem_Warn; use Sem_Warn;
77 with Sinput; use Sinput;
78 with Stand; use Stand;
79 with Sinfo; use Sinfo;
80 with Sinfo.CN; use Sinfo.CN;
81 with Snames; use Snames;
82 with Stringt; use Stringt;
83 with Style;
84 with Stylesw; use Stylesw;
85 with Tbuild; use Tbuild;
86 with Uintp; use Uintp;
87 with Urealp; use Urealp;
88 with Validsw; use Validsw;
89
90 package body Sem_Ch6 is
91
92 May_Hide_Profile : Boolean := False;
93 -- This flag is used to indicate that two formals in two subprograms being
94 -- checked for conformance differ only in that one is an access parameter
95 -- while the other is of a general access type with the same designated
96 -- type. In this case, if the rest of the signatures match, a call to
97 -- either subprogram may be ambiguous, which is worth a warning. The flag
98 -- is set in Compatible_Types, and the warning emitted in
99 -- New_Overloaded_Entity.
100
101 -----------------------
102 -- Local Subprograms --
103 -----------------------
104
105 procedure Analyze_Function_Return (N : Node_Id);
106 -- Subsidiary to Analyze_Return_Statement. Called when the return statement
107 -- applies to a [generic] function.
108
109 procedure Analyze_Generic_Subprogram_Body (N : Node_Id; Gen_Id : Entity_Id);
110 -- Analyze a generic subprogram body. N is the body to be analyzed, and
111 -- Gen_Id is the defining entity Id for the corresponding spec.
112
113 procedure Analyze_Null_Procedure
114 (N : Node_Id;
115 Is_Completion : out Boolean);
116 -- A null procedure can be a declaration or (Ada 2012) a completion
117
118 procedure Analyze_Return_Statement (N : Node_Id);
119 -- Common processing for simple and extended return statements
120
121 procedure Analyze_Return_Type (N : Node_Id);
122 -- Subsidiary to Process_Formals: analyze subtype mark in function
123 -- specification in a context where the formals are visible and hide
124 -- outer homographs.
125
126 procedure Analyze_Subprogram_Body_Helper (N : Node_Id);
127 -- Does all the real work of Analyze_Subprogram_Body. This is split out so
128 -- that we can use RETURN but not skip the debug output at the end.
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_Limited_Return
155 (N : Node_Id;
156 Expr : Node_Id;
157 R_Type : Entity_Id);
158 -- Check the appropriate (Ada 95 or Ada 2005) rules for returning limited
159 -- types. Used only for simple return statements. Expr is the expression
160 -- returned.
161
162 procedure Check_Subprogram_Order (N : Node_Id);
163 -- N is the N_Subprogram_Body node for a subprogram. This routine applies
164 -- the alpha ordering rule for N if this ordering requirement applicable.
165
166 procedure Check_Returns
167 (HSS : Node_Id;
168 Mode : Character;
169 Err : out Boolean;
170 Proc : Entity_Id := Empty);
171 -- Called to check for missing return statements in a function body, or for
172 -- returns present in a procedure body which has No_Return set. HSS is the
173 -- handled statement sequence for the subprogram body. This procedure
174 -- checks all flow paths to make sure they either have return (Mode = 'F',
175 -- used for functions) or do not have a return (Mode = 'P', used for
176 -- No_Return procedures). The flag Err is set if there are any control
177 -- paths not explicitly terminated by a return in the function case, and is
178 -- True otherwise. Proc is the entity for the procedure case and is used
179 -- in posting the warning message.
180
181 procedure Check_Untagged_Equality (Eq_Op : Entity_Id);
182 -- In Ada 2012, a primitive equality operator on an untagged record type
183 -- must appear before the type is frozen, and have the same visibility as
184 -- that of the type. This procedure checks that this rule is met, and
185 -- otherwise emits an error on the subprogram declaration and a warning
186 -- on the earlier freeze point if it is easy to locate. In Ada 2012 mode,
187 -- this routine outputs errors (or warnings if -gnatd.E is set). In earlier
188 -- versions of Ada, warnings are output if Warn_On_Ada_2012_Incompatibility
189 -- is set, otherwise the call has no effect.
190
191 procedure Enter_Overloaded_Entity (S : Entity_Id);
192 -- This procedure makes S, a new overloaded entity, into the first visible
193 -- entity with that name.
194
195 function Is_Non_Overriding_Operation
196 (Prev_E : Entity_Id;
197 New_E : Entity_Id) return Boolean;
198 -- Enforce the rule given in 12.3(18): a private operation in an instance
199 -- overrides an inherited operation only if the corresponding operation
200 -- was overriding in the generic. This needs to be checked for primitive
201 -- operations of types derived (in the generic unit) from formal private
202 -- or formal derived types.
203
204 procedure Make_Inequality_Operator (S : Entity_Id);
205 -- Create the declaration for an inequality operator that is implicitly
206 -- created by a user-defined equality operator that yields a boolean.
207
208 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id);
209 -- Preanalysis of default expressions of subprogram formals. N is the
210 -- expression to be analyzed and T is the expected type.
211
212 procedure Set_Formal_Validity (Formal_Id : Entity_Id);
213 -- Formal_Id is an formal parameter entity. This procedure deals with
214 -- setting the proper validity status for this entity, which depends on
215 -- the kind of parameter and the validity checking mode.
216
217 ---------------------------------------------
218 -- Analyze_Abstract_Subprogram_Declaration --
219 ---------------------------------------------
220
221 procedure Analyze_Abstract_Subprogram_Declaration (N : Node_Id) is
222 Scop : constant Entity_Id := Current_Scope;
223 Subp_Id : constant Entity_Id :=
224 Analyze_Subprogram_Specification (Specification (N));
225
226 begin
227 Generate_Definition (Subp_Id);
228
229 -- Set the SPARK mode from the current context (may be overwritten later
230 -- with explicit pragma).
231
232 Set_SPARK_Pragma (Subp_Id, SPARK_Mode_Pragma);
233 Set_SPARK_Pragma_Inherited (Subp_Id);
234
235 -- Preserve relevant elaboration-related attributes of the context which
236 -- are no longer available or very expensive to recompute once analysis,
237 -- resolution, and expansion are over.
238
239 Mark_Elaboration_Attributes
240 (N_Id => Subp_Id,
241 Checks => True,
242 Warnings => True);
243
244 Set_Is_Abstract_Subprogram (Subp_Id);
245 New_Overloaded_Entity (Subp_Id);
246 Check_Delayed_Subprogram (Subp_Id);
247
248 Set_Categorization_From_Scope (Subp_Id, Scop);
249
250 if Ekind (Scope (Subp_Id)) = E_Protected_Type then
251 Error_Msg_N ("abstract subprogram not allowed in protected type", N);
252
253 -- Issue a warning if the abstract subprogram is neither a dispatching
254 -- operation nor an operation that overrides an inherited subprogram or
255 -- predefined operator, since this most likely indicates a mistake.
256
257 elsif Warn_On_Redundant_Constructs
258 and then not Is_Dispatching_Operation (Subp_Id)
259 and then not Present (Overridden_Operation (Subp_Id))
260 and then (not Is_Operator_Symbol_Name (Chars (Subp_Id))
261 or else Scop /= Scope (Etype (First_Formal (Subp_Id))))
262 then
263 Error_Msg_N
264 ("abstract subprogram is not dispatching or overriding?r?", N);
265 end if;
266
267 Generate_Reference_To_Formals (Subp_Id);
268 Check_Eliminated (Subp_Id);
269
270 if Has_Aspects (N) then
271 Analyze_Aspect_Specifications (N, Subp_Id);
272 end if;
273 end Analyze_Abstract_Subprogram_Declaration;
274
275 ---------------------------------
276 -- Analyze_Expression_Function --
277 ---------------------------------
278
279 procedure Analyze_Expression_Function (N : Node_Id) is
280 Expr : constant Node_Id := Expression (N);
281 Loc : constant Source_Ptr := Sloc (N);
282 LocX : constant Source_Ptr := Sloc (Expr);
283 Spec : constant Node_Id := Specification (N);
284
285 -- Local variables
286
287 Asp : Node_Id;
288 New_Body : Node_Id;
289 New_Spec : Node_Id;
290 Orig_N : Node_Id;
291 Ret : Node_Id;
292
293 Def_Id : Entity_Id := Empty;
294 Prev : Entity_Id;
295 -- If the expression is a completion, Prev is the entity whose
296 -- declaration is completed. Def_Id is needed to analyze the spec.
297
298 -- Start of processing for Analyze_Expression_Function
299
300 begin
301 -- This is one of the occasions on which we transform the tree during
302 -- semantic analysis. If this is a completion, transform the expression
303 -- function into an equivalent subprogram body, and analyze it.
304
305 -- Expression functions are inlined unconditionally. The back-end will
306 -- determine whether this is possible.
307
308 Inline_Processing_Required := True;
309
310 -- Create a specification for the generated body. This must be done
311 -- prior to the analysis of the initial declaration.
312
313 New_Spec := Copy_Subprogram_Spec (Spec);
314 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
315
316 -- If there are previous overloadable entities with the same name,
317 -- check whether any of them is completed by the expression function.
318 -- In a generic context a formal subprogram has no completion.
319
320 if Present (Prev)
321 and then Is_Overloadable (Prev)
322 and then not Is_Formal_Subprogram (Prev)
323 then
324 Def_Id := Analyze_Subprogram_Specification (Spec);
325 Prev := Find_Corresponding_Spec (N);
326
327 -- The previous entity may be an expression function as well, in
328 -- which case the redeclaration is illegal.
329
330 if Present (Prev)
331 and then Nkind (Original_Node (Unit_Declaration_Node (Prev))) =
332 N_Expression_Function
333 then
334 Error_Msg_Sloc := Sloc (Prev);
335 Error_Msg_N ("& conflicts with declaration#", Def_Id);
336 return;
337 end if;
338 end if;
339
340 Ret := Make_Simple_Return_Statement (LocX, Expr);
341
342 New_Body :=
343 Make_Subprogram_Body (Loc,
344 Specification => New_Spec,
345 Declarations => Empty_List,
346 Handled_Statement_Sequence =>
347 Make_Handled_Sequence_Of_Statements (LocX,
348 Statements => New_List (Ret)));
349 Set_Was_Expression_Function (New_Body);
350
351 -- If the expression completes a generic subprogram, we must create a
352 -- separate node for the body, because at instantiation the original
353 -- node of the generic copy must be a generic subprogram body, and
354 -- cannot be a expression function. Otherwise we just rewrite the
355 -- expression with the non-generic body.
356
357 if Present (Prev) and then Ekind (Prev) = E_Generic_Function then
358 Insert_After (N, New_Body);
359
360 -- Propagate any aspects or pragmas that apply to the expression
361 -- function to the proper body when the expression function acts
362 -- as a completion.
363
364 if Has_Aspects (N) then
365 Move_Aspects (N, To => New_Body);
366 end if;
367
368 Relocate_Pragmas_To_Body (New_Body);
369
370 Rewrite (N, Make_Null_Statement (Loc));
371 Set_Has_Completion (Prev, False);
372 Analyze (N);
373 Analyze (New_Body);
374 Set_Is_Inlined (Prev);
375
376 -- If the expression function is a completion, the previous declaration
377 -- must come from source. We know already that it appears in the current
378 -- scope. The entity itself may be internally created if within a body
379 -- to be inlined.
380
381 elsif Present (Prev)
382 and then Is_Overloadable (Prev)
383 and then not Is_Formal_Subprogram (Prev)
384 and then Comes_From_Source (Parent (Prev))
385 then
386 Set_Has_Completion (Prev, False);
387 Set_Is_Inlined (Prev);
388
389 -- AI12-0103: Expression functions that are a completion freeze their
390 -- expression but don't freeze anything else (unlike regular bodies).
391
392 -- Note that we cannot defer this freezing to the analysis of the
393 -- expression itself, because a freeze node might appear in a nested
394 -- scope, leading to an elaboration order issue in gigi.
395 -- As elsewhere, we do not emit freeze nodes within a generic unit.
396
397 if not Inside_A_Generic then
398 Freeze_Expr_Types
399 (Def_Id => Def_Id,
400 Typ => Etype (Def_Id),
401 Expr => Expr,
402 N => N);
403 end if;
404
405 -- For navigation purposes, indicate that the function is a body
406
407 Generate_Reference (Prev, Defining_Entity (N), 'b', Force => True);
408 Rewrite (N, New_Body);
409
410 -- Remove any existing aspects from the original node because the act
411 -- of rewriting causes the list to be shared between the two nodes.
412
413 Orig_N := Original_Node (N);
414 Remove_Aspects (Orig_N);
415
416 -- Propagate any pragmas that apply to expression function to the
417 -- proper body when the expression function acts as a completion.
418 -- Aspects are automatically transfered because of node rewriting.
419
420 Relocate_Pragmas_To_Body (N);
421 Analyze (N);
422
423 -- Prev is the previous entity with the same name, but it is can
424 -- be an unrelated spec that is not completed by the expression
425 -- function. In that case the relevant entity is the one in the body.
426 -- Not clear that the backend can inline it in this case ???
427
428 if Has_Completion (Prev) then
429
430 -- The formals of the expression function are body formals,
431 -- and do not appear in the ali file, which will only contain
432 -- references to the formals of the original subprogram spec.
433
434 declare
435 F1 : Entity_Id;
436 F2 : Entity_Id;
437
438 begin
439 F1 := First_Formal (Def_Id);
440 F2 := First_Formal (Prev);
441
442 while Present (F1) loop
443 Set_Spec_Entity (F1, F2);
444 Next_Formal (F1);
445 Next_Formal (F2);
446 end loop;
447 end;
448
449 else
450 Set_Is_Inlined (Defining_Entity (New_Body));
451 end if;
452
453 -- If this is not a completion, create both a declaration and a body, so
454 -- that the expression can be inlined whenever possible.
455
456 else
457 -- An expression function that is not a completion is not a
458 -- subprogram declaration, and thus cannot appear in a protected
459 -- definition.
460
461 if Nkind (Parent (N)) = N_Protected_Definition then
462 Error_Msg_N
463 ("an expression function is not a legal protected operation", N);
464 end if;
465
466 Rewrite (N, Make_Subprogram_Declaration (Loc, Specification => Spec));
467
468 -- Remove any existing aspects from the original node because the act
469 -- of rewriting causes the list to be shared between the two nodes.
470
471 Orig_N := Original_Node (N);
472 Remove_Aspects (Orig_N);
473
474 Analyze (N);
475
476 -- If aspect SPARK_Mode was specified on the body, it needs to be
477 -- repeated both on the generated spec and the body.
478
479 Asp := Find_Aspect (Defining_Unit_Name (Spec), Aspect_SPARK_Mode);
480
481 if Present (Asp) then
482 Asp := New_Copy_Tree (Asp);
483 Set_Analyzed (Asp, False);
484 Set_Aspect_Specifications (New_Body, New_List (Asp));
485 end if;
486
487 Def_Id := Defining_Entity (N);
488 Set_Is_Inlined (Def_Id);
489
490 -- Establish the linkages between the spec and the body. These are
491 -- used when the expression function acts as the prefix of attribute
492 -- 'Access in order to freeze the original expression which has been
493 -- moved to the generated body.
494
495 Set_Corresponding_Body (N, Defining_Entity (New_Body));
496 Set_Corresponding_Spec (New_Body, Def_Id);
497
498 -- Within a generic preanalyze the original expression for name
499 -- capture. The body is also generated but plays no role in
500 -- this because it is not part of the original source.
501
502 if Inside_A_Generic then
503 Set_Has_Completion (Def_Id);
504 Push_Scope (Def_Id);
505 Install_Formals (Def_Id);
506 Preanalyze_Spec_Expression (Expr, Etype (Def_Id));
507 End_Scope;
508 end if;
509
510 -- To prevent premature freeze action, insert the new body at the end
511 -- of the current declarations, or at the end of the package spec.
512 -- However, resolve usage names now, to prevent spurious visibility
513 -- on later entities. Note that the function can now be called in
514 -- the current declarative part, which will appear to be prior to
515 -- the presence of the body in the code. There are nevertheless no
516 -- order of elaboration issues because all name resolution has taken
517 -- place at the point of declaration.
518
519 declare
520 Decls : List_Id := List_Containing (N);
521 Expr : constant Node_Id := Expression (Ret);
522 Par : constant Node_Id := Parent (Decls);
523 Typ : constant Entity_Id := Etype (Def_Id);
524
525 begin
526 -- If this is a wrapper created for in an instance for a formal
527 -- subprogram, insert body after declaration, to be analyzed when
528 -- the enclosing instance is analyzed.
529
530 if GNATprove_Mode
531 and then Is_Generic_Actual_Subprogram (Def_Id)
532 then
533 Insert_After (N, New_Body);
534
535 else
536 if Nkind (Par) = N_Package_Specification
537 and then Decls = Visible_Declarations (Par)
538 and then Present (Private_Declarations (Par))
539 and then not Is_Empty_List (Private_Declarations (Par))
540 then
541 Decls := Private_Declarations (Par);
542 end if;
543
544 Insert_After (Last (Decls), New_Body);
545
546 -- Preanalyze the expression if not already done above
547
548 if not Inside_A_Generic then
549 Push_Scope (Def_Id);
550 Install_Formals (Def_Id);
551 Preanalyze_Formal_Expression (Expr, Typ);
552 Check_Limited_Return (Original_Node (N), Expr, Typ);
553 End_Scope;
554 end if;
555 end if;
556 end;
557 end if;
558
559 -- Check incorrect use of dynamically tagged expression. This doesn't
560 -- fall out automatically when analyzing the generated function body,
561 -- because Check_Dynamically_Tagged_Expression deliberately ignores
562 -- nodes that don't come from source.
563
564 if Present (Def_Id)
565 and then Nkind (Def_Id) in N_Has_Etype
566 and then Is_Tagged_Type (Etype (Def_Id))
567 then
568 Check_Dynamically_Tagged_Expression
569 (Expr => Expr,
570 Typ => Etype (Def_Id),
571 Related_Nod => Original_Node (N));
572 end if;
573
574 -- We must enforce checks for unreferenced formals in our newly
575 -- generated function, so we propagate the referenced flag from the
576 -- original spec to the new spec as well as setting Comes_From_Source.
577
578 if Present (Parameter_Specifications (New_Spec)) then
579 declare
580 Form_New_Def : Entity_Id;
581 Form_New_Spec : Entity_Id;
582 Form_Old_Def : Entity_Id;
583 Form_Old_Spec : Entity_Id;
584
585 begin
586 Form_New_Spec := First (Parameter_Specifications (New_Spec));
587 Form_Old_Spec := First (Parameter_Specifications (Spec));
588
589 while Present (Form_New_Spec) and then Present (Form_Old_Spec) loop
590 Form_New_Def := Defining_Identifier (Form_New_Spec);
591 Form_Old_Def := Defining_Identifier (Form_Old_Spec);
592
593 Set_Comes_From_Source (Form_New_Def, True);
594
595 -- Because of the usefulness of unreferenced controlling
596 -- formals we exempt them from unreferenced warnings by marking
597 -- them as always referenced.
598
599 Set_Referenced (Form_Old_Def,
600 (Is_Formal (Form_Old_Def)
601 and then Is_Controlling_Formal (Form_Old_Def))
602 or else Referenced (Form_Old_Def));
603
604 Next (Form_New_Spec);
605 Next (Form_Old_Spec);
606 end loop;
607 end;
608 end if;
609 end Analyze_Expression_Function;
610
611 ----------------------------------------
612 -- Analyze_Extended_Return_Statement --
613 ----------------------------------------
614
615 procedure Analyze_Extended_Return_Statement (N : Node_Id) is
616 begin
617 Check_Compiler_Unit ("extended return statement", N);
618 Analyze_Return_Statement (N);
619 end Analyze_Extended_Return_Statement;
620
621 ----------------------------
622 -- Analyze_Function_Call --
623 ----------------------------
624
625 procedure Analyze_Function_Call (N : Node_Id) is
626 Actuals : constant List_Id := Parameter_Associations (N);
627 Func_Nam : constant Node_Id := Name (N);
628 Actual : Node_Id;
629
630 begin
631 Analyze (Func_Nam);
632
633 -- A call of the form A.B (X) may be an Ada 2005 call, which is
634 -- rewritten as B (A, X). If the rewriting is successful, the call
635 -- has been analyzed and we just return.
636
637 if Nkind (Func_Nam) = N_Selected_Component
638 and then Name (N) /= Func_Nam
639 and then Is_Rewrite_Substitution (N)
640 and then Present (Etype (N))
641 then
642 return;
643 end if;
644
645 -- If error analyzing name, then set Any_Type as result type and return
646
647 if Etype (Func_Nam) = Any_Type then
648 Set_Etype (N, Any_Type);
649 return;
650 end if;
651
652 -- Otherwise analyze the parameters
653
654 if Present (Actuals) then
655 Actual := First (Actuals);
656 while Present (Actual) loop
657 Analyze (Actual);
658 Check_Parameterless_Call (Actual);
659 Next (Actual);
660 end loop;
661 end if;
662
663 Analyze_Call (N);
664 end Analyze_Function_Call;
665
666 -----------------------------
667 -- Analyze_Function_Return --
668 -----------------------------
669
670 procedure Analyze_Function_Return (N : Node_Id) is
671 Loc : constant Source_Ptr := Sloc (N);
672 Stm_Entity : constant Entity_Id := Return_Statement_Entity (N);
673 Scope_Id : constant Entity_Id := Return_Applies_To (Stm_Entity);
674
675 R_Type : constant Entity_Id := Etype (Scope_Id);
676 -- Function result subtype
677
678 procedure Check_No_Return_Expression (Return_Expr : Node_Id);
679 -- Ada 2020: Check that the return expression in a No_Return function
680 -- meets the conditions specified by RM 6.5.1(5.1/5).
681
682 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id);
683 -- Apply legality rule of 6.5 (5.9) to the access discriminants of an
684 -- aggregate in a return statement.
685
686 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id);
687 -- Check that the return_subtype_indication properly matches the result
688 -- subtype of the function, as required by RM-6.5(5.1/2-5.3/2).
689
690 --------------------------------
691 -- Check_No_Return_Expression --
692 --------------------------------
693
694 procedure Check_No_Return_Expression (Return_Expr : Node_Id) is
695 Kind : constant Node_Kind := Nkind (Return_Expr);
696
697 begin
698 if Kind = N_Raise_Expression then
699 return;
700
701 elsif Kind = N_Function_Call
702 and then Is_Entity_Name (Name (Return_Expr))
703 and then Ekind_In (Entity (Name (Return_Expr)), E_Function,
704 E_Generic_Function)
705 and then No_Return (Entity (Name (Return_Expr)))
706 then
707 return;
708 end if;
709
710 Error_Msg_N
711 ("illegal expression in RETURN statement of No_Return function",
712 Return_Expr);
713 Error_Msg_N
714 ("\must be raise expression or call to No_Return (RM 6.5.1(5.1/5))",
715 Return_Expr);
716 end Check_No_Return_Expression;
717
718 ------------------------------------------
719 -- Check_Return_Construct_Accessibility --
720 ------------------------------------------
721
722 procedure Check_Return_Construct_Accessibility (Return_Stmt : Node_Id) is
723 Assoc : Node_Id;
724 Agg : Node_Id := Empty;
725 Discr : Entity_Id;
726 Expr : Node_Id;
727 Obj : Node_Id;
728 Process_Exprs : Boolean := False;
729 Return_Con : Node_Id;
730
731 begin
732 -- Only perform checks on record types with access discriminants and
733 -- non-internally generated functions.
734
735 if not Is_Record_Type (R_Type)
736 or else not Has_Discriminants (R_Type)
737 or else not Comes_From_Source (Return_Stmt)
738 then
739 return;
740 end if;
741
742 -- We are only interested in return statements
743
744 if not Nkind_In (Return_Stmt, N_Extended_Return_Statement,
745 N_Simple_Return_Statement)
746 then
747 return;
748 end if;
749
750 -- Fetch the object from the return statement, in the case of a
751 -- simple return statement the expression is part of the node.
752
753 if Nkind (Return_Stmt) = N_Extended_Return_Statement then
754 -- Obtain the object definition from the expanded extended return
755
756 Return_Con := First (Return_Object_Declarations (Return_Stmt));
757 while Present (Return_Con) loop
758 -- Inspect the original node to avoid object declarations
759 -- expanded into renamings.
760
761 if Nkind (Original_Node (Return_Con)) = N_Object_Declaration
762 and then Comes_From_Source (Original_Node (Return_Con))
763 then
764 exit;
765 end if;
766
767 Nlists.Next (Return_Con);
768 end loop;
769
770 pragma Assert (Present (Return_Con));
771
772 -- Could be dealing with a renaming
773
774 Return_Con := Original_Node (Return_Con);
775 else
776 Return_Con := Return_Stmt;
777 end if;
778
779 -- We may need to check an aggregate or a subtype indication
780 -- depending on how the discriminants were specified and whether
781 -- we are looking at an extended return statement.
782
783 if Nkind (Return_Con) = N_Object_Declaration
784 and then Nkind (Object_Definition (Return_Con))
785 = N_Subtype_Indication
786 then
787 Assoc := Original_Node
788 (First
789 (Constraints
790 (Constraint (Object_Definition (Return_Con)))));
791 else
792 -- Qualified expressions may be nested
793
794 Agg := Original_Node (Expression (Return_Con));
795 while Nkind (Agg) = N_Qualified_Expression loop
796 Agg := Original_Node (Expression (Agg));
797 end loop;
798
799 -- If we are looking at an aggregate instead of a function call we
800 -- can continue checking accessibility for the supplied
801 -- discriminant associations.
802
803 if Nkind (Agg) = N_Aggregate then
804 if Present (Expressions (Agg)) then
805 Assoc := First (Expressions (Agg));
806 Process_Exprs := True;
807 else
808 Assoc := First (Component_Associations (Agg));
809 end if;
810
811 -- Otherwise the expression is not of interest ???
812
813 else
814 return;
815 end if;
816 end if;
817
818 -- Move through the discriminants checking the accessibility level
819 -- of each co-extension's associated expression.
820
821 Discr := First_Discriminant (R_Type);
822 while Present (Discr) loop
823 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type then
824
825 if Nkind (Assoc) = N_Attribute_Reference then
826 Expr := Assoc;
827 elsif Nkind_In (Assoc, N_Component_Association,
828 N_Discriminant_Association)
829 then
830 Expr := Expression (Assoc);
831 else
832 Expr := Empty;
833 end if;
834
835 -- This anonymous access discriminant has an associated
836 -- expression which needs checking.
837
838 if Present (Expr)
839 and then Nkind (Expr) = N_Attribute_Reference
840 and then Attribute_Name (Expr) /= Name_Unrestricted_Access
841 then
842 -- Obtain the object to perform static checks on by moving
843 -- up the prefixes in the expression taking into account
844 -- named access types and renamed objects within the
845 -- expression.
846
847 Obj := Original_Node (Prefix (Expr));
848 loop
849 while Nkind_In (Obj, N_Explicit_Dereference,
850 N_Indexed_Component,
851 N_Selected_Component)
852 loop
853 -- When we encounter a named access type then we can
854 -- ignore accessibility checks on the dereference.
855
856 if Ekind (Etype (Original_Node (Prefix (Obj))))
857 in E_Access_Type ..
858 E_Access_Protected_Subprogram_Type
859 then
860 if Nkind (Obj) = N_Selected_Component then
861 Obj := Selector_Name (Obj);
862 else
863 Obj := Original_Node (Prefix (Obj));
864 end if;
865 exit;
866 end if;
867
868 Obj := Original_Node (Prefix (Obj));
869 end loop;
870
871 if Nkind (Obj) = N_Selected_Component then
872 Obj := Selector_Name (Obj);
873 end if;
874
875 -- Check for renamings
876
877 pragma Assert (Is_Entity_Name (Obj));
878
879 if Present (Renamed_Object (Entity (Obj))) then
880 Obj := Renamed_Object (Entity (Obj));
881 else
882 exit;
883 end if;
884 end loop;
885
886 -- Do not check aliased formals or function calls. A
887 -- run-time check may still be needed ???
888
889 if Is_Formal (Entity (Obj))
890 and then Is_Aliased (Entity (Obj))
891 then
892 null;
893
894 elsif Object_Access_Level (Obj) >
895 Scope_Depth (Scope (Scope_Id))
896 then
897 Error_Msg_N
898 ("access discriminant in return aggregate would "
899 & "be a dangling reference", Obj);
900 end if;
901 end if;
902 end if;
903
904 Next_Discriminant (Discr);
905
906 if not Is_List_Member (Assoc) then
907 Assoc := Empty;
908 else
909 Nlists.Next (Assoc);
910 end if;
911
912 -- After aggregate expressions, examine component associations if
913 -- present.
914
915 if No (Assoc) then
916 if Present (Agg)
917 and then Process_Exprs
918 and then Present (Component_Associations (Agg))
919 then
920 Assoc := First (Component_Associations (Agg));
921 Process_Exprs := False;
922 else
923 exit;
924 end if;
925 end if;
926 end loop;
927 end Check_Return_Construct_Accessibility;
928
929 -------------------------------------
930 -- Check_Return_Subtype_Indication --
931 -------------------------------------
932
933 procedure Check_Return_Subtype_Indication (Obj_Decl : Node_Id) is
934 Return_Obj : constant Node_Id := Defining_Identifier (Obj_Decl);
935
936 R_Stm_Type : constant Entity_Id := Etype (Return_Obj);
937 -- Subtype given in the extended return statement (must match R_Type)
938
939 Subtype_Ind : constant Node_Id :=
940 Object_Definition (Original_Node (Obj_Decl));
941
942 procedure Error_No_Match (N : Node_Id);
943 -- Output error messages for case where types do not statically
944 -- match. N is the location for the messages.
945
946 --------------------
947 -- Error_No_Match --
948 --------------------
949
950 procedure Error_No_Match (N : Node_Id) is
951 begin
952 Error_Msg_N
953 ("subtype must statically match function result subtype", N);
954
955 if not Predicates_Match (R_Stm_Type, R_Type) then
956 Error_Msg_Node_2 := R_Type;
957 Error_Msg_NE
958 ("\predicate of& does not match predicate of&",
959 N, R_Stm_Type);
960 end if;
961 end Error_No_Match;
962
963 -- Start of processing for Check_Return_Subtype_Indication
964
965 begin
966 -- First, avoid cascaded errors
967
968 if Error_Posted (Obj_Decl) or else Error_Posted (Subtype_Ind) then
969 return;
970 end if;
971
972 -- "return access T" case; check that the return statement also has
973 -- "access T", and that the subtypes statically match:
974 -- if this is an access to subprogram the signatures must match.
975
976 if Is_Anonymous_Access_Type (R_Type) then
977 if Is_Anonymous_Access_Type (R_Stm_Type) then
978 if Ekind (Designated_Type (R_Stm_Type)) /= E_Subprogram_Type
979 then
980 if Base_Type (Designated_Type (R_Stm_Type)) /=
981 Base_Type (Designated_Type (R_Type))
982 or else not Subtypes_Statically_Match (R_Stm_Type, R_Type)
983 then
984 Error_No_Match (Subtype_Mark (Subtype_Ind));
985 end if;
986
987 else
988 -- For two anonymous access to subprogram types, the types
989 -- themselves must be type conformant.
990
991 if not Conforming_Types
992 (R_Stm_Type, R_Type, Fully_Conformant)
993 then
994 Error_No_Match (Subtype_Ind);
995 end if;
996 end if;
997
998 else
999 Error_Msg_N ("must use anonymous access type", Subtype_Ind);
1000 end if;
1001
1002 -- If the return object is of an anonymous access type, then report
1003 -- an error if the function's result type is not also anonymous.
1004
1005 elsif Is_Anonymous_Access_Type (R_Stm_Type) then
1006 pragma Assert (not Is_Anonymous_Access_Type (R_Type));
1007 Error_Msg_N
1008 ("anonymous access not allowed for function with named access "
1009 & "result", Subtype_Ind);
1010
1011 -- Subtype indication case: check that the return object's type is
1012 -- covered by the result type, and that the subtypes statically match
1013 -- when the result subtype is constrained. Also handle record types
1014 -- with unknown discriminants for which we have built the underlying
1015 -- record view. Coverage is needed to allow specific-type return
1016 -- objects when the result type is class-wide (see AI05-32).
1017
1018 elsif Covers (Base_Type (R_Type), Base_Type (R_Stm_Type))
1019 or else (Is_Underlying_Record_View (Base_Type (R_Stm_Type))
1020 and then
1021 Covers
1022 (Base_Type (R_Type),
1023 Underlying_Record_View (Base_Type (R_Stm_Type))))
1024 then
1025 -- A null exclusion may be present on the return type, on the
1026 -- function specification, on the object declaration or on the
1027 -- subtype itself.
1028
1029 if Is_Access_Type (R_Type)
1030 and then
1031 (Can_Never_Be_Null (R_Type)
1032 or else Null_Exclusion_Present (Parent (Scope_Id))) /=
1033 Can_Never_Be_Null (R_Stm_Type)
1034 then
1035 Error_No_Match (Subtype_Ind);
1036 end if;
1037
1038 -- AI05-103: for elementary types, subtypes must statically match
1039
1040 if Is_Constrained (R_Type) or else Is_Access_Type (R_Type) then
1041 if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
1042 Error_No_Match (Subtype_Ind);
1043 end if;
1044 end if;
1045
1046 -- All remaining cases are illegal
1047
1048 -- Note: previous versions of this subprogram allowed the return
1049 -- value to be the ancestor of the return type if the return type
1050 -- was a null extension. This was plainly incorrect.
1051
1052 else
1053 Error_Msg_N
1054 ("wrong type for return_subtype_indication", Subtype_Ind);
1055 end if;
1056 end Check_Return_Subtype_Indication;
1057
1058 ---------------------
1059 -- Local Variables --
1060 ---------------------
1061
1062 Expr : Node_Id;
1063 Obj_Decl : Node_Id := Empty;
1064
1065 -- Start of processing for Analyze_Function_Return
1066
1067 begin
1068 Set_Return_Present (Scope_Id);
1069
1070 if Nkind (N) = N_Simple_Return_Statement then
1071 Expr := Expression (N);
1072
1073 -- Guard against a malformed expression. The parser may have tried to
1074 -- recover but the node is not analyzable.
1075
1076 if Nkind (Expr) = N_Error then
1077 Set_Etype (Expr, Any_Type);
1078 Expander_Mode_Save_And_Set (False);
1079 return;
1080
1081 else
1082 -- The resolution of a controlled [extension] aggregate associated
1083 -- with a return statement creates a temporary which needs to be
1084 -- finalized on function exit. Wrap the return statement inside a
1085 -- block so that the finalization machinery can detect this case.
1086 -- This early expansion is done only when the return statement is
1087 -- not part of a handled sequence of statements.
1088
1089 if Nkind_In (Expr, N_Aggregate,
1090 N_Extension_Aggregate)
1091 and then Needs_Finalization (R_Type)
1092 and then Nkind (Parent (N)) /= N_Handled_Sequence_Of_Statements
1093 then
1094 Rewrite (N,
1095 Make_Block_Statement (Loc,
1096 Handled_Statement_Sequence =>
1097 Make_Handled_Sequence_Of_Statements (Loc,
1098 Statements => New_List (Relocate_Node (N)))));
1099
1100 Analyze (N);
1101 return;
1102 end if;
1103
1104 Analyze (Expr);
1105
1106 -- Ada 2005 (AI-251): If the type of the returned object is
1107 -- an access to an interface type then we add an implicit type
1108 -- conversion to force the displacement of the "this" pointer to
1109 -- reference the secondary dispatch table. We cannot delay the
1110 -- generation of this implicit conversion until the expansion
1111 -- because in this case the type resolution changes the decoration
1112 -- of the expression node to match R_Type; by contrast, if the
1113 -- returned object is a class-wide interface type then it is too
1114 -- early to generate here the implicit conversion since the return
1115 -- statement may be rewritten by the expander into an extended
1116 -- return statement whose expansion takes care of adding the
1117 -- implicit type conversion to displace the pointer to the object.
1118
1119 if Expander_Active
1120 and then Serious_Errors_Detected = 0
1121 and then Is_Access_Type (R_Type)
1122 and then not Nkind_In (Expr, N_Null, N_Raise_Expression)
1123 and then Is_Interface (Designated_Type (R_Type))
1124 and then Is_Progenitor (Designated_Type (R_Type),
1125 Designated_Type (Etype (Expr)))
1126 then
1127 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1128 Analyze (Expr);
1129 end if;
1130
1131 Resolve (Expr, R_Type);
1132 Check_Limited_Return (N, Expr, R_Type);
1133
1134 Check_Return_Construct_Accessibility (N);
1135
1136 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1137 -- nonreturning function shall be a simple_return_statement with
1138 -- an expression that is a raise_expression, or else a call on a
1139 -- nonreturning function, or else a parenthesized expression of
1140 -- one of these.
1141
1142 if Ada_Version >= Ada_2020
1143 and then No_Return (Scope_Id)
1144 and then Comes_From_Source (N)
1145 then
1146 Check_No_Return_Expression (Original_Node (Expr));
1147 end if;
1148 end if;
1149 else
1150 Obj_Decl := Last (Return_Object_Declarations (N));
1151
1152 -- Analyze parts specific to extended_return_statement:
1153
1154 declare
1155 Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
1156 HSS : constant Node_Id := Handled_Statement_Sequence (N);
1157
1158 begin
1159 Expr := Expression (Obj_Decl);
1160
1161 -- Note: The check for OK_For_Limited_Init will happen in
1162 -- Analyze_Object_Declaration; we treat it as a normal
1163 -- object declaration.
1164
1165 Set_Is_Return_Object (Defining_Identifier (Obj_Decl));
1166 Analyze (Obj_Decl);
1167
1168 Check_Return_Subtype_Indication (Obj_Decl);
1169
1170 if Present (HSS) then
1171 Analyze (HSS);
1172
1173 if Present (Exception_Handlers (HSS)) then
1174
1175 -- ???Has_Nested_Block_With_Handler needs to be set.
1176 -- Probably by creating an actual N_Block_Statement.
1177 -- Probably in Expand.
1178
1179 null;
1180 end if;
1181 end if;
1182
1183 -- Mark the return object as referenced, since the return is an
1184 -- implicit reference of the object.
1185
1186 Set_Referenced (Defining_Identifier (Obj_Decl));
1187
1188 Check_References (Stm_Entity);
1189
1190 Check_Return_Construct_Accessibility (N);
1191
1192 -- Check RM 6.5 (5.9/3)
1193
1194 if Has_Aliased then
1195 if Ada_Version < Ada_2012 then
1196
1197 -- Shouldn't this test Warn_On_Ada_2012_Compatibility ???
1198 -- Can it really happen (extended return???)
1199
1200 Error_Msg_N
1201 ("aliased only allowed for limited return objects "
1202 & "in Ada 2012??", N);
1203
1204 elsif not Is_Limited_View (R_Type) then
1205 Error_Msg_N
1206 ("aliased only allowed for limited return objects", N);
1207 end if;
1208 end if;
1209
1210 -- Ada 2020 (AI12-0269): Any return statement that applies to a
1211 -- nonreturning function shall be a simple_return_statement.
1212
1213 if Ada_Version >= Ada_2020
1214 and then No_Return (Scope_Id)
1215 and then Comes_From_Source (N)
1216 then
1217 Error_Msg_N
1218 ("extended RETURN statement not allowed in No_Return "
1219 & "function", N);
1220 end if;
1221 end;
1222 end if;
1223
1224 -- Case of Expr present
1225
1226 if Present (Expr) then
1227
1228 -- Defend against previous errors
1229
1230 if Nkind (Expr) = N_Empty
1231 or else No (Etype (Expr))
1232 then
1233 return;
1234 end if;
1235
1236 -- Apply constraint check. Note that this is done before the implicit
1237 -- conversion of the expression done for anonymous access types to
1238 -- ensure correct generation of the null-excluding check associated
1239 -- with null-excluding expressions found in return statements. We
1240 -- don't need a check if the subtype of the return object is the
1241 -- same as the result subtype of the function.
1242
1243 if Nkind (N) /= N_Extended_Return_Statement
1244 or else Nkind (Obj_Decl) /= N_Object_Declaration
1245 or else Nkind (Object_Definition (Obj_Decl)) not in N_Has_Entity
1246 or else Entity (Object_Definition (Obj_Decl)) /= R_Type
1247 then
1248 Apply_Constraint_Check (Expr, R_Type);
1249 end if;
1250
1251 -- The return value is converted to the return type of the function,
1252 -- which implies a predicate check if the return type is predicated.
1253 -- We do not apply the check for an extended return statement because
1254 -- Analyze_Object_Declaration has already done it on Obj_Decl above.
1255 -- We do not apply the check to a case expression because it will
1256 -- be expanded into a series of return statements, each of which
1257 -- will receive a predicate check.
1258
1259 if Nkind (N) /= N_Extended_Return_Statement
1260 and then Nkind (Expr) /= N_Case_Expression
1261 then
1262 Apply_Predicate_Check (Expr, R_Type);
1263 end if;
1264
1265 -- Ada 2005 (AI-318-02): When the result type is an anonymous access
1266 -- type, apply an implicit conversion of the expression to that type
1267 -- to force appropriate static and run-time accessibility checks.
1268 -- But we want to apply the checks to an extended return statement
1269 -- only once, i.e. not to the simple return statement generated at
1270 -- the end of its expansion because, prior to leaving the function,
1271 -- the accessibility level of the return object changes to be a level
1272 -- determined by the point of call (RM 3.10.2(10.8/3).
1273
1274 if Ada_Version >= Ada_2005
1275 and then Ekind (R_Type) = E_Anonymous_Access_Type
1276 and then (Nkind (N) = N_Extended_Return_Statement
1277 or else not Comes_From_Extended_Return_Statement (N))
1278 then
1279 Rewrite (Expr, Convert_To (R_Type, Relocate_Node (Expr)));
1280 Analyze_And_Resolve (Expr, R_Type);
1281
1282 -- If this is a local anonymous access to subprogram, the
1283 -- accessibility check can be applied statically. The return is
1284 -- illegal if the access type of the return expression is declared
1285 -- inside of the subprogram (except if it is the subtype indication
1286 -- of an extended return statement).
1287
1288 elsif Ekind (R_Type) = E_Anonymous_Access_Subprogram_Type then
1289 if not Comes_From_Source (Current_Scope)
1290 or else Ekind (Current_Scope) = E_Return_Statement
1291 then
1292 null;
1293
1294 elsif
1295 Scope_Depth (Scope (Etype (Expr))) >= Scope_Depth (Scope_Id)
1296 then
1297 Error_Msg_N ("cannot return local access to subprogram", N);
1298 end if;
1299
1300 -- The expression cannot be of a formal incomplete type
1301
1302 elsif Ekind (Etype (Expr)) = E_Incomplete_Type
1303 and then Is_Generic_Type (Etype (Expr))
1304 then
1305 Error_Msg_N
1306 ("cannot return expression of a formal incomplete type", N);
1307 end if;
1308
1309 -- If the result type is class-wide, then check that the return
1310 -- expression's type is not declared at a deeper level than the
1311 -- function (RM05-6.5(5.6/2)).
1312
1313 if Ada_Version >= Ada_2005
1314 and then Is_Class_Wide_Type (R_Type)
1315 then
1316 if Type_Access_Level (Etype (Expr)) >
1317 Subprogram_Access_Level (Scope_Id)
1318 then
1319 Error_Msg_N
1320 ("level of return expression type is deeper than "
1321 & "class-wide function!", Expr);
1322 end if;
1323 end if;
1324
1325 -- Check incorrect use of dynamically tagged expression
1326
1327 if Is_Tagged_Type (R_Type) then
1328 Check_Dynamically_Tagged_Expression
1329 (Expr => Expr,
1330 Typ => R_Type,
1331 Related_Nod => N);
1332 end if;
1333
1334 -- ??? A real run-time accessibility check is needed in cases
1335 -- involving dereferences of access parameters. For now we just
1336 -- check the static cases.
1337
1338 if (Ada_Version < Ada_2005 or else Debug_Flag_Dot_L)
1339 and then Is_Limited_View (Etype (Scope_Id))
1340 and then Object_Access_Level (Expr) >
1341 Subprogram_Access_Level (Scope_Id)
1342 then
1343 -- Suppress the message in a generic, where the rewriting
1344 -- is irrelevant.
1345
1346 if Inside_A_Generic then
1347 null;
1348
1349 else
1350 Rewrite (N,
1351 Make_Raise_Program_Error (Loc,
1352 Reason => PE_Accessibility_Check_Failed));
1353 Analyze (N);
1354
1355 Error_Msg_Warn := SPARK_Mode /= On;
1356 Error_Msg_N ("cannot return a local value by reference<<", N);
1357 Error_Msg_NE ("\& [<<", N, Standard_Program_Error);
1358 end if;
1359 end if;
1360
1361 if Known_Null (Expr)
1362 and then Nkind (Parent (Scope_Id)) = N_Function_Specification
1363 and then Null_Exclusion_Present (Parent (Scope_Id))
1364 then
1365 Apply_Compile_Time_Constraint_Error
1366 (N => Expr,
1367 Msg => "(Ada 2005) null not allowed for "
1368 & "null-excluding return??",
1369 Reason => CE_Null_Not_Allowed);
1370 end if;
1371
1372 -- RM 6.5 (5.4/3): accessibility checks also apply if the return object
1373 -- has no initializing expression.
1374
1375 elsif Ada_Version > Ada_2005 and then Is_Class_Wide_Type (R_Type) then
1376 if Type_Access_Level (Etype (Defining_Identifier (Obj_Decl))) >
1377 Subprogram_Access_Level (Scope_Id)
1378 then
1379 Error_Msg_N
1380 ("level of return expression type is deeper than "
1381 & "class-wide function!", Obj_Decl);
1382 end if;
1383 end if;
1384 end Analyze_Function_Return;
1385
1386 -------------------------------------
1387 -- Analyze_Generic_Subprogram_Body --
1388 -------------------------------------
1389
1390 procedure Analyze_Generic_Subprogram_Body
1391 (N : Node_Id;
1392 Gen_Id : Entity_Id)
1393 is
1394 Gen_Decl : constant Node_Id := Unit_Declaration_Node (Gen_Id);
1395 Kind : constant Entity_Kind := Ekind (Gen_Id);
1396 Body_Id : Entity_Id;
1397 New_N : Node_Id;
1398 Spec : Node_Id;
1399
1400 begin
1401 -- Copy body and disable expansion while analyzing the generic For a
1402 -- stub, do not copy the stub (which would load the proper body), this
1403 -- will be done when the proper body is analyzed.
1404
1405 if Nkind (N) /= N_Subprogram_Body_Stub then
1406 New_N := Copy_Generic_Node (N, Empty, Instantiating => False);
1407 Rewrite (N, New_N);
1408
1409 -- Once the contents of the generic copy and the template are
1410 -- swapped, do the same for their respective aspect specifications.
1411
1412 Exchange_Aspects (N, New_N);
1413
1414 -- Collect all contract-related source pragmas found within the
1415 -- template and attach them to the contract of the subprogram body.
1416 -- This contract is used in the capture of global references within
1417 -- annotations.
1418
1419 Create_Generic_Contract (N);
1420
1421 Start_Generic;
1422 end if;
1423
1424 Spec := Specification (N);
1425
1426 -- Within the body of the generic, the subprogram is callable, and
1427 -- behaves like the corresponding non-generic unit.
1428
1429 Body_Id := Defining_Entity (Spec);
1430
1431 if Kind = E_Generic_Procedure
1432 and then Nkind (Spec) /= N_Procedure_Specification
1433 then
1434 Error_Msg_N ("invalid body for generic procedure ", Body_Id);
1435 return;
1436
1437 elsif Kind = E_Generic_Function
1438 and then Nkind (Spec) /= N_Function_Specification
1439 then
1440 Error_Msg_N ("invalid body for generic function ", Body_Id);
1441 return;
1442 end if;
1443
1444 Set_Corresponding_Body (Gen_Decl, Body_Id);
1445
1446 if Has_Completion (Gen_Id)
1447 and then Nkind (Parent (N)) /= N_Subunit
1448 then
1449 Error_Msg_N ("duplicate generic body", N);
1450 return;
1451 else
1452 Set_Has_Completion (Gen_Id);
1453 end if;
1454
1455 if Nkind (N) = N_Subprogram_Body_Stub then
1456 Set_Ekind (Defining_Entity (Specification (N)), Kind);
1457 else
1458 Set_Corresponding_Spec (N, Gen_Id);
1459 end if;
1460
1461 if Nkind (Parent (N)) = N_Compilation_Unit then
1462 Set_Cunit_Entity (Current_Sem_Unit, Defining_Entity (N));
1463 end if;
1464
1465 -- Make generic parameters immediately visible in the body. They are
1466 -- needed to process the formals declarations. Then make the formals
1467 -- visible in a separate step.
1468
1469 Push_Scope (Gen_Id);
1470
1471 declare
1472 E : Entity_Id;
1473 First_Ent : Entity_Id;
1474
1475 begin
1476 First_Ent := First_Entity (Gen_Id);
1477
1478 E := First_Ent;
1479 while Present (E) and then not Is_Formal (E) loop
1480 Install_Entity (E);
1481 Next_Entity (E);
1482 end loop;
1483
1484 Set_Use (Generic_Formal_Declarations (Gen_Decl));
1485
1486 -- Now generic formals are visible, and the specification can be
1487 -- analyzed, for subsequent conformance check.
1488
1489 Body_Id := Analyze_Subprogram_Specification (Spec);
1490
1491 -- Make formal parameters visible
1492
1493 if Present (E) then
1494
1495 -- E is the first formal parameter, we loop through the formals
1496 -- installing them so that they will be visible.
1497
1498 Set_First_Entity (Gen_Id, E);
1499 while Present (E) loop
1500 Install_Entity (E);
1501 Next_Formal (E);
1502 end loop;
1503 end if;
1504
1505 -- Visible generic entity is callable within its own body
1506
1507 Set_Ekind (Gen_Id, Ekind (Body_Id));
1508 Set_Ekind (Body_Id, E_Subprogram_Body);
1509 Set_Convention (Body_Id, Convention (Gen_Id));
1510 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Gen_Id));
1511 Set_Scope (Body_Id, Scope (Gen_Id));
1512
1513 Check_Fully_Conformant (Body_Id, Gen_Id, Body_Id);
1514
1515 if Nkind (N) = N_Subprogram_Body_Stub then
1516
1517 -- No body to analyze, so restore state of generic unit
1518
1519 Set_Ekind (Gen_Id, Kind);
1520 Set_Ekind (Body_Id, Kind);
1521
1522 if Present (First_Ent) then
1523 Set_First_Entity (Gen_Id, First_Ent);
1524 end if;
1525
1526 End_Scope;
1527 return;
1528 end if;
1529
1530 -- If this is a compilation unit, it must be made visible explicitly,
1531 -- because the compilation of the declaration, unlike other library
1532 -- unit declarations, does not. If it is not a unit, the following
1533 -- is redundant but harmless.
1534
1535 Set_Is_Immediately_Visible (Gen_Id);
1536 Reference_Body_Formals (Gen_Id, Body_Id);
1537
1538 if Is_Child_Unit (Gen_Id) then
1539 Generate_Reference (Gen_Id, Scope (Gen_Id), 'k', False);
1540 end if;
1541
1542 Set_Actual_Subtypes (N, Current_Scope);
1543
1544 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
1545 Set_SPARK_Pragma_Inherited (Body_Id);
1546
1547 -- Analyze any aspect specifications that appear on the generic
1548 -- subprogram body.
1549
1550 if Has_Aspects (N) then
1551 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
1552 end if;
1553
1554 Analyze_Declarations (Declarations (N));
1555 Check_Completion;
1556
1557 -- Process the contract of the subprogram body after all declarations
1558 -- have been analyzed. This ensures that any contract-related pragmas
1559 -- are available through the N_Contract node of the body.
1560
1561 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
1562
1563 Analyze (Handled_Statement_Sequence (N));
1564 Save_Global_References (Original_Node (N));
1565
1566 -- Prior to exiting the scope, include generic formals again (if any
1567 -- are present) in the set of local entities.
1568
1569 if Present (First_Ent) then
1570 Set_First_Entity (Gen_Id, First_Ent);
1571 end if;
1572
1573 Check_References (Gen_Id);
1574 end;
1575
1576 Process_End_Label (Handled_Statement_Sequence (N), 't', Current_Scope);
1577 Update_Use_Clause_Chain;
1578 Validate_Categorization_Dependency (N, Gen_Id);
1579 End_Scope;
1580 Check_Subprogram_Order (N);
1581
1582 -- Outside of its body, unit is generic again
1583
1584 Set_Ekind (Gen_Id, Kind);
1585 Generate_Reference (Gen_Id, Body_Id, 'b', Set_Ref => False);
1586
1587 if Style_Check then
1588 Style.Check_Identifier (Body_Id, Gen_Id);
1589 end if;
1590
1591 End_Generic;
1592 end Analyze_Generic_Subprogram_Body;
1593
1594 ----------------------------
1595 -- Analyze_Null_Procedure --
1596 ----------------------------
1597
1598 -- WARNING: This routine manages Ghost regions. Return statements must be
1599 -- replaced by gotos that jump to the end of the routine and restore the
1600 -- Ghost mode.
1601
1602 procedure Analyze_Null_Procedure
1603 (N : Node_Id;
1604 Is_Completion : out Boolean)
1605 is
1606 Loc : constant Source_Ptr := Sloc (N);
1607 Spec : constant Node_Id := Specification (N);
1608
1609 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1610 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1611 Saved_ISMP : constant Boolean :=
1612 Ignore_SPARK_Mode_Pragmas_In_Instance;
1613 -- Save the Ghost and SPARK mode-related data to restore on exit
1614
1615 Designator : Entity_Id;
1616 Form : Node_Id;
1617 Null_Body : Node_Id := Empty;
1618 Null_Stmt : Node_Id := Null_Statement (Spec);
1619 Prev : Entity_Id;
1620
1621 begin
1622 Prev := Current_Entity_In_Scope (Defining_Entity (Spec));
1623
1624 -- A null procedure is Ghost when it is stand-alone and is subject to
1625 -- pragma Ghost, or when the corresponding spec is Ghost. Set the mode
1626 -- now, to ensure that any nodes generated during analysis and expansion
1627 -- are properly marked as Ghost.
1628
1629 if Present (Prev) then
1630 Mark_And_Set_Ghost_Body (N, Prev);
1631 end if;
1632
1633 -- Capture the profile of the null procedure before analysis, for
1634 -- expansion at the freeze point and at each point of call. The body is
1635 -- used if the procedure has preconditions, or if it is a completion. In
1636 -- the first case the body is analyzed at the freeze point, in the other
1637 -- it replaces the null procedure declaration.
1638
1639 -- For a null procedure that comes from source, a NULL statement is
1640 -- provided by the parser, which carries the source location of the
1641 -- NULL keyword, and has Comes_From_Source set. For a null procedure
1642 -- from expansion, create one now.
1643
1644 if No (Null_Stmt) then
1645 Null_Stmt := Make_Null_Statement (Loc);
1646 end if;
1647
1648 Null_Body :=
1649 Make_Subprogram_Body (Loc,
1650 Specification => New_Copy_Tree (Spec),
1651 Declarations => New_List,
1652 Handled_Statement_Sequence =>
1653 Make_Handled_Sequence_Of_Statements (Loc,
1654 Statements => New_List (Null_Stmt)));
1655
1656 -- Create new entities for body and formals
1657
1658 Set_Defining_Unit_Name (Specification (Null_Body),
1659 Make_Defining_Identifier
1660 (Sloc (Defining_Entity (N)),
1661 Chars (Defining_Entity (N))));
1662
1663 Form := First (Parameter_Specifications (Specification (Null_Body)));
1664 while Present (Form) loop
1665 Set_Defining_Identifier (Form,
1666 Make_Defining_Identifier
1667 (Sloc (Defining_Identifier (Form)),
1668 Chars (Defining_Identifier (Form))));
1669 Next (Form);
1670 end loop;
1671
1672 -- Determine whether the null procedure may be a completion of a generic
1673 -- suprogram, in which case we use the new null body as the completion
1674 -- and set minimal semantic information on the original declaration,
1675 -- which is rewritten as a null statement.
1676
1677 if Present (Prev) and then Is_Generic_Subprogram (Prev) then
1678 Insert_Before (N, Null_Body);
1679 Set_Ekind (Defining_Entity (N), Ekind (Prev));
1680
1681 Rewrite (N, Make_Null_Statement (Loc));
1682 Analyze_Generic_Subprogram_Body (Null_Body, Prev);
1683 Is_Completion := True;
1684
1685 goto Leave;
1686
1687 else
1688 -- Resolve the types of the formals now, because the freeze point may
1689 -- appear in a different context, e.g. an instantiation.
1690
1691 Form := First (Parameter_Specifications (Specification (Null_Body)));
1692 while Present (Form) loop
1693 if Nkind (Parameter_Type (Form)) /= N_Access_Definition then
1694 Find_Type (Parameter_Type (Form));
1695
1696 elsif No (Access_To_Subprogram_Definition
1697 (Parameter_Type (Form)))
1698 then
1699 Find_Type (Subtype_Mark (Parameter_Type (Form)));
1700
1701 -- The case of a null procedure with a formal that is an
1702 -- access-to-subprogram type, and that is used as an actual
1703 -- in an instantiation is left to the enthusiastic reader.
1704
1705 else
1706 null;
1707 end if;
1708
1709 Next (Form);
1710 end loop;
1711 end if;
1712
1713 -- If there are previous overloadable entities with the same name, check
1714 -- whether any of them is completed by the null procedure.
1715
1716 if Present (Prev) and then Is_Overloadable (Prev) then
1717 Designator := Analyze_Subprogram_Specification (Spec);
1718 Prev := Find_Corresponding_Spec (N);
1719 end if;
1720
1721 if No (Prev) or else not Comes_From_Source (Prev) then
1722 Designator := Analyze_Subprogram_Specification (Spec);
1723 Set_Has_Completion (Designator);
1724
1725 -- Signal to caller that this is a procedure declaration
1726
1727 Is_Completion := False;
1728
1729 -- Null procedures are always inlined, but generic formal subprograms
1730 -- which appear as such in the internal instance of formal packages,
1731 -- need no completion and are not marked Inline.
1732
1733 if Expander_Active
1734 and then Nkind (N) /= N_Formal_Concrete_Subprogram_Declaration
1735 then
1736 Set_Corresponding_Body (N, Defining_Entity (Null_Body));
1737 Set_Body_To_Inline (N, Null_Body);
1738 Set_Is_Inlined (Designator);
1739 end if;
1740
1741 else
1742 -- The null procedure is a completion. We unconditionally rewrite
1743 -- this as a null body (even if expansion is not active), because
1744 -- there are various error checks that are applied on this body
1745 -- when it is analyzed (e.g. correct aspect placement).
1746
1747 if Has_Completion (Prev) then
1748 Error_Msg_Sloc := Sloc (Prev);
1749 Error_Msg_NE ("duplicate body for & declared#", N, Prev);
1750 end if;
1751
1752 Check_Previous_Null_Procedure (N, Prev);
1753
1754 Is_Completion := True;
1755 Rewrite (N, Null_Body);
1756 Analyze (N);
1757 end if;
1758
1759 <<Leave>>
1760 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
1761 Restore_Ghost_Region (Saved_GM, Saved_IGR);
1762 end Analyze_Null_Procedure;
1763
1764 -----------------------------
1765 -- Analyze_Operator_Symbol --
1766 -----------------------------
1767
1768 -- An operator symbol such as "+" or "and" may appear in context where the
1769 -- literal denotes an entity name, such as "+"(x, y) or in context when it
1770 -- is just a string, as in (conjunction = "or"). In these cases the parser
1771 -- generates this node, and the semantics does the disambiguation. Other
1772 -- such case are actuals in an instantiation, the generic unit in an
1773 -- instantiation, and pragma arguments.
1774
1775 procedure Analyze_Operator_Symbol (N : Node_Id) is
1776 Par : constant Node_Id := Parent (N);
1777
1778 begin
1779 if (Nkind (Par) = N_Function_Call and then N = Name (Par))
1780 or else Nkind (Par) = N_Function_Instantiation
1781 or else (Nkind (Par) = N_Indexed_Component and then N = Prefix (Par))
1782 or else (Nkind (Par) = N_Pragma_Argument_Association
1783 and then not Is_Pragma_String_Literal (Par))
1784 or else Nkind (Par) = N_Subprogram_Renaming_Declaration
1785 or else (Nkind (Par) = N_Attribute_Reference
1786 and then Attribute_Name (Par) /= Name_Value)
1787 then
1788 Find_Direct_Name (N);
1789
1790 else
1791 Change_Operator_Symbol_To_String_Literal (N);
1792 Analyze (N);
1793 end if;
1794 end Analyze_Operator_Symbol;
1795
1796 -----------------------------------
1797 -- Analyze_Parameter_Association --
1798 -----------------------------------
1799
1800 procedure Analyze_Parameter_Association (N : Node_Id) is
1801 begin
1802 Analyze (Explicit_Actual_Parameter (N));
1803 end Analyze_Parameter_Association;
1804
1805 ----------------------------
1806 -- Analyze_Procedure_Call --
1807 ----------------------------
1808
1809 -- WARNING: This routine manages Ghost regions. Return statements must be
1810 -- replaced by gotos that jump to the end of the routine and restore the
1811 -- Ghost mode.
1812
1813 procedure Analyze_Procedure_Call (N : Node_Id) is
1814 procedure Analyze_Call_And_Resolve;
1815 -- Do Analyze and Resolve calls for procedure call. At the end, check
1816 -- for illegal order dependence.
1817 -- ??? where is the check for illegal order dependencies?
1818
1819 ------------------------------
1820 -- Analyze_Call_And_Resolve --
1821 ------------------------------
1822
1823 procedure Analyze_Call_And_Resolve is
1824 begin
1825 if Nkind (N) = N_Procedure_Call_Statement then
1826 Analyze_Call (N);
1827 Resolve (N, Standard_Void_Type);
1828 else
1829 Analyze (N);
1830 end if;
1831 end Analyze_Call_And_Resolve;
1832
1833 -- Local variables
1834
1835 Actuals : constant List_Id := Parameter_Associations (N);
1836 Loc : constant Source_Ptr := Sloc (N);
1837 P : constant Node_Id := Name (N);
1838
1839 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
1840 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
1841 -- Save the Ghost-related attributes to restore on exit
1842
1843 Actual : Node_Id;
1844 New_N : Node_Id;
1845
1846 -- Start of processing for Analyze_Procedure_Call
1847
1848 begin
1849 -- The syntactic construct: PREFIX ACTUAL_PARAMETER_PART can denote
1850 -- a procedure call or an entry call. The prefix may denote an access
1851 -- to subprogram type, in which case an implicit dereference applies.
1852 -- If the prefix is an indexed component (without implicit dereference)
1853 -- then the construct denotes a call to a member of an entire family.
1854 -- If the prefix is a simple name, it may still denote a call to a
1855 -- parameterless member of an entry family. Resolution of these various
1856 -- interpretations is delicate.
1857
1858 -- Do not analyze machine code statements to avoid rejecting them in
1859 -- CodePeer mode.
1860
1861 if CodePeer_Mode and then Nkind (P) = N_Qualified_Expression then
1862 Set_Etype (P, Standard_Void_Type);
1863 else
1864 Analyze (P);
1865 end if;
1866
1867 -- If this is a call of the form Obj.Op, the call may have been analyzed
1868 -- and possibly rewritten into a block, in which case we are done.
1869
1870 if Analyzed (N) then
1871 return;
1872
1873 -- If there is an error analyzing the name (which may have been
1874 -- rewritten if the original call was in prefix notation) then error
1875 -- has been emitted already, mark node and return.
1876
1877 elsif Error_Posted (N) or else Etype (Name (N)) = Any_Type then
1878 Set_Etype (N, Any_Type);
1879 return;
1880 end if;
1881
1882 -- A procedure call is Ghost when its name denotes a Ghost procedure.
1883 -- Set the mode now to ensure that any nodes generated during analysis
1884 -- and expansion are properly marked as Ghost.
1885
1886 Mark_And_Set_Ghost_Procedure_Call (N);
1887
1888 -- Otherwise analyze the parameters
1889
1890 if Present (Actuals) then
1891 Actual := First (Actuals);
1892
1893 while Present (Actual) loop
1894 Analyze (Actual);
1895 Check_Parameterless_Call (Actual);
1896 Next (Actual);
1897 end loop;
1898 end if;
1899
1900 -- Special processing for Elab_Spec, Elab_Body and Elab_Subp_Body calls
1901
1902 if Nkind (P) = N_Attribute_Reference
1903 and then Nam_In (Attribute_Name (P), Name_Elab_Spec,
1904 Name_Elab_Body,
1905 Name_Elab_Subp_Body)
1906 then
1907 if Present (Actuals) then
1908 Error_Msg_N
1909 ("no parameters allowed for this call", First (Actuals));
1910 goto Leave;
1911 end if;
1912
1913 Set_Etype (N, Standard_Void_Type);
1914 Set_Analyzed (N);
1915
1916 elsif Is_Entity_Name (P)
1917 and then Is_Record_Type (Etype (Entity (P)))
1918 and then Remote_AST_I_Dereference (P)
1919 then
1920 goto Leave;
1921
1922 elsif Is_Entity_Name (P)
1923 and then Ekind (Entity (P)) /= E_Entry_Family
1924 then
1925 if Is_Access_Type (Etype (P))
1926 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1927 and then No (Actuals)
1928 and then Comes_From_Source (N)
1929 then
1930 Error_Msg_N ("missing explicit dereference in call", N);
1931 end if;
1932
1933 Analyze_Call_And_Resolve;
1934
1935 -- If the prefix is the simple name of an entry family, this is a
1936 -- parameterless call from within the task body itself.
1937
1938 elsif Is_Entity_Name (P)
1939 and then Nkind (P) = N_Identifier
1940 and then Ekind (Entity (P)) = E_Entry_Family
1941 and then Present (Actuals)
1942 and then No (Next (First (Actuals)))
1943 then
1944 -- Can be call to parameterless entry family. What appears to be the
1945 -- sole argument is in fact the entry index. Rewrite prefix of node
1946 -- accordingly. Source representation is unchanged by this
1947 -- transformation.
1948
1949 New_N :=
1950 Make_Indexed_Component (Loc,
1951 Prefix =>
1952 Make_Selected_Component (Loc,
1953 Prefix => New_Occurrence_Of (Scope (Entity (P)), Loc),
1954 Selector_Name => New_Occurrence_Of (Entity (P), Loc)),
1955 Expressions => Actuals);
1956 Set_Name (N, New_N);
1957 Set_Etype (New_N, Standard_Void_Type);
1958 Set_Parameter_Associations (N, No_List);
1959 Analyze_Call_And_Resolve;
1960
1961 elsif Nkind (P) = N_Explicit_Dereference then
1962 if Ekind (Etype (P)) = E_Subprogram_Type then
1963 Analyze_Call_And_Resolve;
1964 else
1965 Error_Msg_N ("expect access to procedure in call", P);
1966 end if;
1967
1968 -- The name can be a selected component or an indexed component that
1969 -- yields an access to subprogram. Such a prefix is legal if the call
1970 -- has parameter associations.
1971
1972 elsif Is_Access_Type (Etype (P))
1973 and then Ekind (Designated_Type (Etype (P))) = E_Subprogram_Type
1974 then
1975 if Present (Actuals) then
1976 Analyze_Call_And_Resolve;
1977 else
1978 Error_Msg_N ("missing explicit dereference in call ", N);
1979 end if;
1980
1981 -- If not an access to subprogram, then the prefix must resolve to the
1982 -- name of an entry, entry family, or protected operation.
1983
1984 -- For the case of a simple entry call, P is a selected component where
1985 -- the prefix is the task and the selector name is the entry. A call to
1986 -- a protected procedure will have the same syntax. If the protected
1987 -- object contains overloaded operations, the entity may appear as a
1988 -- function, the context will select the operation whose type is Void.
1989
1990 elsif Nkind (P) = N_Selected_Component
1991 and then Ekind_In (Entity (Selector_Name (P)), E_Entry,
1992 E_Function,
1993 E_Procedure)
1994 then
1995 -- When front-end inlining is enabled, as with SPARK_Mode, a call
1996 -- in prefix notation may still be missing its controlling argument,
1997 -- so perform the transformation now.
1998
1999 if SPARK_Mode = On and then In_Inlined_Body then
2000 declare
2001 Subp : constant Entity_Id := Entity (Selector_Name (P));
2002 Typ : constant Entity_Id := Etype (Prefix (P));
2003
2004 begin
2005 if Is_Tagged_Type (Typ)
2006 and then Present (First_Formal (Subp))
2007 and then (Etype (First_Formal (Subp)) = Typ
2008 or else
2009 Class_Wide_Type (Etype (First_Formal (Subp))) = Typ)
2010 and then Try_Object_Operation (P)
2011 then
2012 return;
2013
2014 else
2015 Analyze_Call_And_Resolve;
2016 end if;
2017 end;
2018
2019 else
2020 Analyze_Call_And_Resolve;
2021 end if;
2022
2023 elsif Nkind (P) = N_Selected_Component
2024 and then Ekind (Entity (Selector_Name (P))) = E_Entry_Family
2025 and then Present (Actuals)
2026 and then No (Next (First (Actuals)))
2027 then
2028 -- Can be call to parameterless entry family. What appears to be the
2029 -- sole argument is in fact the entry index. Rewrite prefix of node
2030 -- accordingly. Source representation is unchanged by this
2031 -- transformation.
2032
2033 New_N :=
2034 Make_Indexed_Component (Loc,
2035 Prefix => New_Copy (P),
2036 Expressions => Actuals);
2037 Set_Name (N, New_N);
2038 Set_Etype (New_N, Standard_Void_Type);
2039 Set_Parameter_Associations (N, No_List);
2040 Analyze_Call_And_Resolve;
2041
2042 -- For the case of a reference to an element of an entry family, P is
2043 -- an indexed component whose prefix is a selected component (task and
2044 -- entry family), and whose index is the entry family index.
2045
2046 elsif Nkind (P) = N_Indexed_Component
2047 and then Nkind (Prefix (P)) = N_Selected_Component
2048 and then Ekind (Entity (Selector_Name (Prefix (P)))) = E_Entry_Family
2049 then
2050 Analyze_Call_And_Resolve;
2051
2052 -- If the prefix is the name of an entry family, it is a call from
2053 -- within the task body itself.
2054
2055 elsif Nkind (P) = N_Indexed_Component
2056 and then Nkind (Prefix (P)) = N_Identifier
2057 and then Ekind (Entity (Prefix (P))) = E_Entry_Family
2058 then
2059 New_N :=
2060 Make_Selected_Component (Loc,
2061 Prefix =>
2062 New_Occurrence_Of (Scope (Entity (Prefix (P))), Loc),
2063 Selector_Name => New_Occurrence_Of (Entity (Prefix (P)), Loc));
2064 Rewrite (Prefix (P), New_N);
2065 Analyze (P);
2066 Analyze_Call_And_Resolve;
2067
2068 -- In Ada 2012. a qualified expression is a name, but it cannot be a
2069 -- procedure name, so the construct can only be a qualified expression.
2070
2071 elsif Nkind (P) = N_Qualified_Expression
2072 and then Ada_Version >= Ada_2012
2073 then
2074 Rewrite (N, Make_Code_Statement (Loc, Expression => P));
2075 Analyze (N);
2076
2077 -- Anything else is an error
2078
2079 else
2080 Error_Msg_N ("invalid procedure or entry call", N);
2081 end if;
2082
2083 <<Leave>>
2084 Restore_Ghost_Region (Saved_GM, Saved_IGR);
2085 end Analyze_Procedure_Call;
2086
2087 ------------------------------
2088 -- Analyze_Return_Statement --
2089 ------------------------------
2090
2091 procedure Analyze_Return_Statement (N : Node_Id) is
2092 pragma Assert (Nkind_In (N, N_Extended_Return_Statement,
2093 N_Simple_Return_Statement));
2094
2095 Returns_Object : constant Boolean :=
2096 Nkind (N) = N_Extended_Return_Statement
2097 or else
2098 (Nkind (N) = N_Simple_Return_Statement
2099 and then Present (Expression (N)));
2100 -- True if we're returning something; that is, "return <expression>;"
2101 -- or "return Result : T [:= ...]". False for "return;". Used for error
2102 -- checking: If Returns_Object is True, N should apply to a function
2103 -- body; otherwise N should apply to a procedure body, entry body,
2104 -- accept statement, or extended return statement.
2105
2106 function Find_What_It_Applies_To return Entity_Id;
2107 -- Find the entity representing the innermost enclosing body, accept
2108 -- statement, or extended return statement. If the result is a callable
2109 -- construct or extended return statement, then this will be the value
2110 -- of the Return_Applies_To attribute. Otherwise, the program is
2111 -- illegal. See RM-6.5(4/2).
2112
2113 -----------------------------
2114 -- Find_What_It_Applies_To --
2115 -----------------------------
2116
2117 function Find_What_It_Applies_To return Entity_Id is
2118 Result : Entity_Id := Empty;
2119
2120 begin
2121 -- Loop outward through the Scope_Stack, skipping blocks, loops,
2122 -- and postconditions.
2123
2124 for J in reverse 0 .. Scope_Stack.Last loop
2125 Result := Scope_Stack.Table (J).Entity;
2126 exit when not Ekind_In (Result, E_Block, E_Loop)
2127 and then Chars (Result) /= Name_uPostconditions;
2128 end loop;
2129
2130 pragma Assert (Present (Result));
2131 return Result;
2132 end Find_What_It_Applies_To;
2133
2134 -- Local declarations
2135
2136 Scope_Id : constant Entity_Id := Find_What_It_Applies_To;
2137 Kind : constant Entity_Kind := Ekind (Scope_Id);
2138 Loc : constant Source_Ptr := Sloc (N);
2139 Stm_Entity : constant Entity_Id :=
2140 New_Internal_Entity
2141 (E_Return_Statement, Current_Scope, Loc, 'R');
2142
2143 -- Start of processing for Analyze_Return_Statement
2144
2145 begin
2146 Set_Return_Statement_Entity (N, Stm_Entity);
2147
2148 Set_Etype (Stm_Entity, Standard_Void_Type);
2149 Set_Return_Applies_To (Stm_Entity, Scope_Id);
2150
2151 -- Place Return entity on scope stack, to simplify enforcement of 6.5
2152 -- (4/2): an inner return statement will apply to this extended return.
2153
2154 if Nkind (N) = N_Extended_Return_Statement then
2155 Push_Scope (Stm_Entity);
2156 end if;
2157
2158 -- Check that pragma No_Return is obeyed. Don't complain about the
2159 -- implicitly-generated return that is placed at the end.
2160
2161 if No_Return (Scope_Id)
2162 and then Ekind_In (Kind, E_Procedure, E_Generic_Procedure)
2163 and then Comes_From_Source (N)
2164 then
2165 Error_Msg_N
2166 ("RETURN statement not allowed in No_Return procedure", N);
2167 end if;
2168
2169 -- Warn on any unassigned OUT parameters if in procedure
2170
2171 if Ekind (Scope_Id) = E_Procedure then
2172 Warn_On_Unassigned_Out_Parameter (N, Scope_Id);
2173 end if;
2174
2175 -- Check that functions return objects, and other things do not
2176
2177 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2178 if not Returns_Object then
2179 Error_Msg_N ("missing expression in return from function", N);
2180 end if;
2181
2182 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2183 if Returns_Object then
2184 Error_Msg_N ("procedure cannot return value (use function)", N);
2185 end if;
2186
2187 elsif Ekind_In (Kind, E_Entry, E_Entry_Family) then
2188 if Returns_Object then
2189 if Is_Protected_Type (Scope (Scope_Id)) then
2190 Error_Msg_N ("entry body cannot return value", N);
2191 else
2192 Error_Msg_N ("accept statement cannot return value", N);
2193 end if;
2194 end if;
2195
2196 elsif Kind = E_Return_Statement then
2197
2198 -- We are nested within another return statement, which must be an
2199 -- extended_return_statement.
2200
2201 if Returns_Object then
2202 if Nkind (N) = N_Extended_Return_Statement then
2203 Error_Msg_N
2204 ("extended return statement cannot be nested (use `RETURN;`)",
2205 N);
2206
2207 -- Case of a simple return statement with a value inside extended
2208 -- return statement.
2209
2210 else
2211 Error_Msg_N
2212 ("return nested in extended return statement cannot return "
2213 & "value (use `RETURN;`)", N);
2214 end if;
2215 end if;
2216
2217 else
2218 Error_Msg_N ("illegal context for return statement", N);
2219 end if;
2220
2221 if Ekind_In (Kind, E_Function, E_Generic_Function) then
2222 Analyze_Function_Return (N);
2223
2224 elsif Ekind_In (Kind, E_Procedure, E_Generic_Procedure) then
2225 Set_Return_Present (Scope_Id);
2226 end if;
2227
2228 if Nkind (N) = N_Extended_Return_Statement then
2229 End_Scope;
2230 end if;
2231
2232 Kill_Current_Values (Last_Assignment_Only => True);
2233 Check_Unreachable_Code (N);
2234
2235 Analyze_Dimension (N);
2236 end Analyze_Return_Statement;
2237
2238 -------------------------------------
2239 -- Analyze_Simple_Return_Statement --
2240 -------------------------------------
2241
2242 procedure Analyze_Simple_Return_Statement (N : Node_Id) is
2243 begin
2244 if Present (Expression (N)) then
2245 Mark_Coextensions (N, Expression (N));
2246 end if;
2247
2248 Analyze_Return_Statement (N);
2249 end Analyze_Simple_Return_Statement;
2250
2251 -------------------------
2252 -- Analyze_Return_Type --
2253 -------------------------
2254
2255 procedure Analyze_Return_Type (N : Node_Id) is
2256 Designator : constant Entity_Id := Defining_Entity (N);
2257 Typ : Entity_Id := Empty;
2258
2259 begin
2260 -- Normal case where result definition does not indicate an error
2261
2262 if Result_Definition (N) /= Error then
2263 if Nkind (Result_Definition (N)) = N_Access_Definition then
2264
2265 -- Ada 2005 (AI-254): Handle anonymous access to subprograms
2266
2267 declare
2268 AD : constant Node_Id :=
2269 Access_To_Subprogram_Definition (Result_Definition (N));
2270 begin
2271 if Present (AD) and then Protected_Present (AD) then
2272 Typ := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2273 else
2274 Typ := Access_Definition (N, Result_Definition (N));
2275 end if;
2276 end;
2277
2278 Set_Parent (Typ, Result_Definition (N));
2279 Set_Is_Local_Anonymous_Access (Typ);
2280 Set_Etype (Designator, Typ);
2281
2282 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2283
2284 Null_Exclusion_Static_Checks (N);
2285
2286 -- Subtype_Mark case
2287
2288 else
2289 Find_Type (Result_Definition (N));
2290 Typ := Entity (Result_Definition (N));
2291 Set_Etype (Designator, Typ);
2292
2293 -- Ada 2005 (AI-231): Ensure proper usage of null exclusion
2294
2295 Null_Exclusion_Static_Checks (N);
2296
2297 -- If a null exclusion is imposed on the result type, then create
2298 -- a null-excluding itype (an access subtype) and use it as the
2299 -- function's Etype. Note that the null exclusion checks are done
2300 -- right before this, because they don't get applied to types that
2301 -- do not come from source.
2302
2303 if Is_Access_Type (Typ) and then Null_Exclusion_Present (N) then
2304 Set_Etype (Designator,
2305 Create_Null_Excluding_Itype
2306 (T => Typ,
2307 Related_Nod => N,
2308 Scope_Id => Scope (Current_Scope)));
2309
2310 -- The new subtype must be elaborated before use because
2311 -- it is visible outside of the function. However its base
2312 -- type may not be frozen yet, so the reference that will
2313 -- force elaboration must be attached to the freezing of
2314 -- the base type.
2315
2316 -- If the return specification appears on a proper body,
2317 -- the subtype will have been created already on the spec.
2318
2319 if Is_Frozen (Typ) then
2320 if Nkind (Parent (N)) = N_Subprogram_Body
2321 and then Nkind (Parent (Parent (N))) = N_Subunit
2322 then
2323 null;
2324 else
2325 Build_Itype_Reference (Etype (Designator), Parent (N));
2326 end if;
2327
2328 else
2329 Ensure_Freeze_Node (Typ);
2330
2331 declare
2332 IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
2333 begin
2334 Set_Itype (IR, Etype (Designator));
2335 Append_Freeze_Actions (Typ, New_List (IR));
2336 end;
2337 end if;
2338
2339 else
2340 Set_Etype (Designator, Typ);
2341 end if;
2342
2343 if Ekind (Typ) = E_Incomplete_Type
2344 or else (Is_Class_Wide_Type (Typ)
2345 and then Ekind (Root_Type (Typ)) = E_Incomplete_Type)
2346 then
2347 -- AI05-0151: Tagged incomplete types are allowed in all formal
2348 -- parts. Untagged incomplete types are not allowed in bodies.
2349 -- As a consequence, limited views cannot appear in a basic
2350 -- declaration that is itself within a body, because there is
2351 -- no point at which the non-limited view will become visible.
2352
2353 if Ada_Version >= Ada_2012 then
2354 if From_Limited_With (Typ) and then In_Package_Body then
2355 Error_Msg_NE
2356 ("invalid use of incomplete type&",
2357 Result_Definition (N), Typ);
2358
2359 -- The return type of a subprogram body cannot be of a
2360 -- formal incomplete type.
2361
2362 elsif Is_Generic_Type (Typ)
2363 and then Nkind (Parent (N)) = N_Subprogram_Body
2364 then
2365 Error_Msg_N
2366 ("return type cannot be a formal incomplete type",
2367 Result_Definition (N));
2368
2369 elsif Is_Class_Wide_Type (Typ)
2370 and then Is_Generic_Type (Root_Type (Typ))
2371 and then Nkind (Parent (N)) = N_Subprogram_Body
2372 then
2373 Error_Msg_N
2374 ("return type cannot be a formal incomplete type",
2375 Result_Definition (N));
2376
2377 elsif Is_Tagged_Type (Typ) then
2378 null;
2379
2380 -- Use is legal in a thunk generated for an operation
2381 -- inherited from a progenitor.
2382
2383 elsif Is_Thunk (Designator)
2384 and then Present (Non_Limited_View (Typ))
2385 then
2386 null;
2387
2388 elsif Nkind (Parent (N)) = N_Subprogram_Body
2389 or else Nkind_In (Parent (Parent (N)), N_Accept_Statement,
2390 N_Entry_Body)
2391 then
2392 Error_Msg_NE
2393 ("invalid use of untagged incomplete type&",
2394 Designator, Typ);
2395 end if;
2396
2397 -- The type must be completed in the current package. This
2398 -- is checked at the end of the package declaration when
2399 -- Taft-amendment types are identified. If the return type
2400 -- is class-wide, there is no required check, the type can
2401 -- be a bona fide TAT.
2402
2403 if Ekind (Scope (Current_Scope)) = E_Package
2404 and then In_Private_Part (Scope (Current_Scope))
2405 and then not Is_Class_Wide_Type (Typ)
2406 then
2407 Append_Elmt (Designator, Private_Dependents (Typ));
2408 end if;
2409
2410 else
2411 Error_Msg_NE
2412 ("invalid use of incomplete type&", Designator, Typ);
2413 end if;
2414 end if;
2415 end if;
2416
2417 -- Case where result definition does indicate an error
2418
2419 else
2420 Set_Etype (Designator, Any_Type);
2421 end if;
2422 end Analyze_Return_Type;
2423
2424 -----------------------------
2425 -- Analyze_Subprogram_Body --
2426 -----------------------------
2427
2428 procedure Analyze_Subprogram_Body (N : Node_Id) is
2429 Loc : constant Source_Ptr := Sloc (N);
2430 Body_Spec : constant Node_Id := Specification (N);
2431 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2432
2433 begin
2434 if Debug_Flag_C then
2435 Write_Str ("==> subprogram body ");
2436 Write_Name (Chars (Body_Id));
2437 Write_Str (" from ");
2438 Write_Location (Loc);
2439 Write_Eol;
2440 Indent;
2441 end if;
2442
2443 Trace_Scope (N, Body_Id, " Analyze subprogram: ");
2444
2445 -- The real work is split out into the helper, so it can do "return;"
2446 -- without skipping the debug output:
2447
2448 Analyze_Subprogram_Body_Helper (N);
2449
2450 if Debug_Flag_C then
2451 Outdent;
2452 Write_Str ("<== subprogram body ");
2453 Write_Name (Chars (Body_Id));
2454 Write_Str (" from ");
2455 Write_Location (Loc);
2456 Write_Eol;
2457 end if;
2458 end Analyze_Subprogram_Body;
2459
2460 ------------------------------------
2461 -- Analyze_Subprogram_Body_Helper --
2462 ------------------------------------
2463
2464 -- This procedure is called for regular subprogram bodies, generic bodies,
2465 -- and for subprogram stubs of both kinds. In the case of stubs, only the
2466 -- specification matters, and is used to create a proper declaration for
2467 -- the subprogram, or to perform conformance checks.
2468
2469 -- WARNING: This routine manages Ghost regions. Return statements must be
2470 -- replaced by gotos that jump to the end of the routine and restore the
2471 -- Ghost mode.
2472
2473 procedure Analyze_Subprogram_Body_Helper (N : Node_Id) is
2474 Body_Spec : Node_Id := Specification (N);
2475 Body_Id : Entity_Id := Defining_Entity (Body_Spec);
2476 Loc : constant Source_Ptr := Sloc (N);
2477 Prev_Id : constant Entity_Id := Current_Entity_In_Scope (Body_Id);
2478
2479 Conformant : Boolean;
2480 Desig_View : Entity_Id := Empty;
2481 Exch_Views : Elist_Id := No_Elist;
2482 HSS : Node_Id;
2483 Mask_Types : Elist_Id := No_Elist;
2484 Prot_Typ : Entity_Id := Empty;
2485 Spec_Decl : Node_Id := Empty;
2486 Spec_Id : Entity_Id;
2487
2488 Last_Real_Spec_Entity : Entity_Id := Empty;
2489 -- When we analyze a separate spec, the entity chain ends up containing
2490 -- the formals, as well as any itypes generated during analysis of the
2491 -- default expressions for parameters, or the arguments of associated
2492 -- precondition/postcondition pragmas (which are analyzed in the context
2493 -- of the spec since they have visibility on formals).
2494 --
2495 -- These entities belong with the spec and not the body. However we do
2496 -- the analysis of the body in the context of the spec (again to obtain
2497 -- visibility to the formals), and all the entities generated during
2498 -- this analysis end up also chained to the entity chain of the spec.
2499 -- But they really belong to the body, and there is circuitry to move
2500 -- them from the spec to the body.
2501 --
2502 -- However, when we do this move, we don't want to move the real spec
2503 -- entities (first para above) to the body. The Last_Real_Spec_Entity
2504 -- variable points to the last real spec entity, so we only move those
2505 -- chained beyond that point. It is initialized to Empty to deal with
2506 -- the case where there is no separate spec.
2507
2508 function Body_Has_Contract return Boolean;
2509 -- Check whether unanalyzed body has an aspect or pragma that may
2510 -- generate a SPARK contract.
2511
2512 function Body_Has_SPARK_Mode_On return Boolean;
2513 -- Check whether SPARK_Mode On applies to the subprogram body, either
2514 -- because it is specified directly on the body, or because it is
2515 -- inherited from the enclosing subprogram or package.
2516
2517 function Build_Internal_Protected_Declaration
2518 (N : Node_Id) return Entity_Id;
2519 -- A subprogram body without a previous spec that appears in a protected
2520 -- body must be expanded separately to create a subprogram declaration
2521 -- for it, in order to resolve internal calls to it from other protected
2522 -- operations.
2523 --
2524 -- Possibly factor this with Exp_Dist.Copy_Specification ???
2525
2526 procedure Build_Subprogram_Declaration;
2527 -- Create a matching subprogram declaration for subprogram body N
2528
2529 procedure Check_Anonymous_Return;
2530 -- Ada 2005: if a function returns an access type that denotes a task,
2531 -- or a type that contains tasks, we must create a master entity for
2532 -- the anonymous type, which typically will be used in an allocator
2533 -- in the body of the function.
2534
2535 procedure Check_Inline_Pragma (Spec : in out Node_Id);
2536 -- Look ahead to recognize a pragma that may appear after the body.
2537 -- If there is a previous spec, check that it appears in the same
2538 -- declarative part. If the pragma is Inline_Always, perform inlining
2539 -- unconditionally, otherwise only if Front_End_Inlining is requested.
2540 -- If the body acts as a spec, and inlining is required, we create a
2541 -- subprogram declaration for it, in order to attach the body to inline.
2542 -- If pragma does not appear after the body, check whether there is
2543 -- an inline pragma before any local declarations.
2544
2545 procedure Check_Missing_Return;
2546 -- Checks for a function with a no return statements, and also performs
2547 -- the warning checks implemented by Check_Returns. In formal mode, also
2548 -- verify that a function ends with a RETURN and that a procedure does
2549 -- not contain any RETURN.
2550
2551 function Disambiguate_Spec return Entity_Id;
2552 -- When a primitive is declared between the private view and the full
2553 -- view of a concurrent type which implements an interface, a special
2554 -- mechanism is used to find the corresponding spec of the primitive
2555 -- body.
2556
2557 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id;
2558 -- Ada 2012 (AI05-0151): Detect whether the profile of Subp_Id contains
2559 -- incomplete types coming from a limited context and replace their
2560 -- limited views with the non-limited ones. Return the list of changes
2561 -- to be used to undo the transformation.
2562
2563 function Is_Private_Concurrent_Primitive
2564 (Subp_Id : Entity_Id) return Boolean;
2565 -- Determine whether subprogram Subp_Id is a primitive of a concurrent
2566 -- type that implements an interface and has a private view.
2567
2568 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id;
2569 -- N is the body generated for an expression function that is not a
2570 -- completion and Spec_Id the defining entity of its spec. Mark all
2571 -- the not-yet-frozen types referenced by the simple return statement
2572 -- of the function as formally frozen.
2573
2574 procedure Move_Pragmas (From : Node_Id; To : Node_Id);
2575 -- Find all suitable source pragmas at the top of subprogram body
2576 -- From's declarations and move them after arbitrary node To.
2577 -- One exception is pragma SPARK_Mode which is copied rather than moved,
2578 -- as it applies to the body too.
2579
2580 procedure Restore_Limited_Views (Restore_List : Elist_Id);
2581 -- Undo the transformation done by Exchange_Limited_Views.
2582
2583 procedure Set_Trivial_Subprogram (N : Node_Id);
2584 -- Sets the Is_Trivial_Subprogram flag in both spec and body of the
2585 -- subprogram whose body is being analyzed. N is the statement node
2586 -- causing the flag to be set, if the following statement is a return
2587 -- of an entity, we mark the entity as set in source to suppress any
2588 -- warning on the stylized use of function stubs with a dummy return.
2589
2590 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id);
2591 -- Undo the transformation done by Mask_Unfrozen_Types
2592
2593 procedure Verify_Overriding_Indicator;
2594 -- If there was a previous spec, the entity has been entered in the
2595 -- current scope previously. If the body itself carries an overriding
2596 -- indicator, check that it is consistent with the known status of the
2597 -- entity.
2598
2599 -----------------------
2600 -- Body_Has_Contract --
2601 -----------------------
2602
2603 function Body_Has_Contract return Boolean is
2604 Decls : constant List_Id := Declarations (N);
2605 Item : Node_Id;
2606
2607 begin
2608 -- Check for aspects that may generate a contract
2609
2610 if Present (Aspect_Specifications (N)) then
2611 Item := First (Aspect_Specifications (N));
2612 while Present (Item) loop
2613 if Is_Subprogram_Contract_Annotation (Item) then
2614 return True;
2615 end if;
2616
2617 Next (Item);
2618 end loop;
2619 end if;
2620
2621 -- Check for pragmas that may generate a contract
2622
2623 if Present (Decls) then
2624 Item := First (Decls);
2625 while Present (Item) loop
2626 if Nkind (Item) = N_Pragma
2627 and then Is_Subprogram_Contract_Annotation (Item)
2628 then
2629 return True;
2630 end if;
2631
2632 Next (Item);
2633 end loop;
2634 end if;
2635
2636 return False;
2637 end Body_Has_Contract;
2638
2639 ----------------------------
2640 -- Body_Has_SPARK_Mode_On --
2641 ----------------------------
2642
2643 function Body_Has_SPARK_Mode_On return Boolean is
2644 Decls : constant List_Id := Declarations (N);
2645 Item : Node_Id;
2646
2647 begin
2648 -- Check for SPARK_Mode aspect
2649
2650 if Present (Aspect_Specifications (N)) then
2651 Item := First (Aspect_Specifications (N));
2652 while Present (Item) loop
2653 if Get_Aspect_Id (Item) = Aspect_SPARK_Mode then
2654 return Get_SPARK_Mode_From_Annotation (Item) = On;
2655 end if;
2656
2657 Next (Item);
2658 end loop;
2659 end if;
2660
2661 -- Check for SPARK_Mode pragma
2662
2663 if Present (Decls) then
2664 Item := First (Decls);
2665 while Present (Item) loop
2666
2667 -- Pragmas that apply to a subprogram body are usually grouped
2668 -- together. Look for a potential pragma SPARK_Mode among them.
2669
2670 if Nkind (Item) = N_Pragma then
2671 if Get_Pragma_Id (Item) = Pragma_SPARK_Mode then
2672 return Get_SPARK_Mode_From_Annotation (Item) = On;
2673 end if;
2674
2675 -- Otherwise the first non-pragma declarative item terminates
2676 -- the region where pragma SPARK_Mode may appear.
2677
2678 else
2679 exit;
2680 end if;
2681
2682 Next (Item);
2683 end loop;
2684 end if;
2685
2686 -- Otherwise, the applicable SPARK_Mode is inherited from the
2687 -- enclosing subprogram or package.
2688
2689 return SPARK_Mode = On;
2690 end Body_Has_SPARK_Mode_On;
2691
2692 ------------------------------------------
2693 -- Build_Internal_Protected_Declaration --
2694 ------------------------------------------
2695
2696 function Build_Internal_Protected_Declaration
2697 (N : Node_Id) return Entity_Id
2698 is
2699 procedure Analyze_Pragmas (From : Node_Id);
2700 -- Analyze all pragmas which follow arbitrary node From
2701
2702 ---------------------
2703 -- Analyze_Pragmas --
2704 ---------------------
2705
2706 procedure Analyze_Pragmas (From : Node_Id) is
2707 Decl : Node_Id;
2708
2709 begin
2710 Decl := Next (From);
2711 while Present (Decl) loop
2712 if Nkind (Decl) = N_Pragma then
2713 Analyze_Pragma (Decl);
2714
2715 -- No candidate pragmas are available for analysis
2716
2717 else
2718 exit;
2719 end if;
2720
2721 Next (Decl);
2722 end loop;
2723 end Analyze_Pragmas;
2724
2725 -- Local variables
2726
2727 Body_Id : constant Entity_Id := Defining_Entity (N);
2728 Loc : constant Source_Ptr := Sloc (N);
2729 Decl : Node_Id;
2730 Formal : Entity_Id;
2731 Formals : List_Id;
2732 Spec : Node_Id;
2733 Spec_Id : Entity_Id;
2734
2735 -- Start of processing for Build_Internal_Protected_Declaration
2736
2737 begin
2738 Formal := First_Formal (Body_Id);
2739
2740 -- The protected operation always has at least one formal, namely the
2741 -- object itself, but it is only placed in the parameter list if
2742 -- expansion is enabled.
2743
2744 if Present (Formal) or else Expander_Active then
2745 Formals := Copy_Parameter_List (Body_Id);
2746 else
2747 Formals := No_List;
2748 end if;
2749
2750 Spec_Id :=
2751 Make_Defining_Identifier (Sloc (Body_Id),
2752 Chars => Chars (Body_Id));
2753
2754 -- Indicate that the entity comes from source, to ensure that cross-
2755 -- reference information is properly generated. The body itself is
2756 -- rewritten during expansion, and the body entity will not appear in
2757 -- calls to the operation.
2758
2759 Set_Comes_From_Source (Spec_Id, True);
2760
2761 if Nkind (Specification (N)) = N_Procedure_Specification then
2762 Spec :=
2763 Make_Procedure_Specification (Loc,
2764 Defining_Unit_Name => Spec_Id,
2765 Parameter_Specifications => Formals);
2766 else
2767 Spec :=
2768 Make_Function_Specification (Loc,
2769 Defining_Unit_Name => Spec_Id,
2770 Parameter_Specifications => Formals,
2771 Result_Definition =>
2772 New_Occurrence_Of (Etype (Body_Id), Loc));
2773 end if;
2774
2775 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2776 Set_Corresponding_Body (Decl, Body_Id);
2777 Set_Corresponding_Spec (N, Spec_Id);
2778
2779 Insert_Before (N, Decl);
2780
2781 -- Associate all aspects and pragmas of the body with the spec. This
2782 -- ensures that these annotations apply to the initial declaration of
2783 -- the subprogram body.
2784
2785 Move_Aspects (From => N, To => Decl);
2786 Move_Pragmas (From => N, To => Decl);
2787
2788 Analyze (Decl);
2789
2790 -- The analysis of the spec may generate pragmas which require manual
2791 -- analysis. Since the generation of the spec and the relocation of
2792 -- the annotations is driven by the expansion of the stand-alone
2793 -- body, the pragmas will not be analyzed in a timely manner. Do this
2794 -- now.
2795
2796 Analyze_Pragmas (Decl);
2797
2798 -- This subprogram has convention Intrinsic as per RM 6.3.1(10/2)
2799 -- ensuring in particular that 'Access is illegal.
2800
2801 Set_Convention (Spec_Id, Convention_Intrinsic);
2802 Set_Has_Completion (Spec_Id);
2803
2804 return Spec_Id;
2805 end Build_Internal_Protected_Declaration;
2806
2807 ----------------------------------
2808 -- Build_Subprogram_Declaration --
2809 ----------------------------------
2810
2811 procedure Build_Subprogram_Declaration is
2812 Decl : Node_Id;
2813 Subp_Decl : Node_Id;
2814
2815 begin
2816 -- Create a matching subprogram spec using the profile of the body.
2817 -- The structure of the tree is identical, but has new entities for
2818 -- the defining unit name and formal parameters.
2819
2820 Subp_Decl :=
2821 Make_Subprogram_Declaration (Loc,
2822 Specification => Copy_Subprogram_Spec (Body_Spec));
2823 Set_Comes_From_Source (Subp_Decl, True);
2824
2825 -- Also mark parameters as coming from source
2826
2827 if Present (Parameter_Specifications (Specification (Subp_Decl))) then
2828 declare
2829 Form : Entity_Id;
2830 begin
2831 Form :=
2832 First (Parameter_Specifications (Specification (Subp_Decl)));
2833
2834 while Present (Form) loop
2835 Set_Comes_From_Source (Defining_Identifier (Form), True);
2836 Next (Form);
2837 end loop;
2838 end;
2839 end if;
2840
2841 -- Relocate the aspects and relevant pragmas from the subprogram body
2842 -- to the generated spec because it acts as the initial declaration.
2843
2844 Insert_Before (N, Subp_Decl);
2845 Move_Aspects (N, To => Subp_Decl);
2846 Move_Pragmas (N, To => Subp_Decl);
2847
2848 -- Ensure that the generated corresponding spec and original body
2849 -- share the same SPARK_Mode pragma or aspect. As a result, both have
2850 -- the same SPARK_Mode attributes, and the global SPARK_Mode value is
2851 -- correctly set for local subprograms.
2852
2853 Copy_SPARK_Mode_Aspect (Subp_Decl, To => N);
2854
2855 Analyze (Subp_Decl);
2856
2857 -- Propagate the attributes Rewritten_For_C and Corresponding_Proc to
2858 -- the body since the expander may generate calls using that entity.
2859 -- Required to ensure that Expand_Call rewrites calls to this
2860 -- function by calls to the built procedure.
2861
2862 if Modify_Tree_For_C
2863 and then Nkind (Body_Spec) = N_Function_Specification
2864 and then
2865 Rewritten_For_C (Defining_Entity (Specification (Subp_Decl)))
2866 then
2867 Set_Rewritten_For_C (Defining_Entity (Body_Spec));
2868 Set_Corresponding_Procedure (Defining_Entity (Body_Spec),
2869 Corresponding_Procedure
2870 (Defining_Entity (Specification (Subp_Decl))));
2871 end if;
2872
2873 -- Analyze any relocated source pragmas or pragmas created for aspect
2874 -- specifications.
2875
2876 Decl := Next (Subp_Decl);
2877 while Present (Decl) loop
2878
2879 -- Stop the search for pragmas once the body has been reached as
2880 -- this terminates the region where pragmas may appear.
2881
2882 if Decl = N then
2883 exit;
2884
2885 elsif Nkind (Decl) = N_Pragma then
2886 Analyze (Decl);
2887 end if;
2888
2889 Next (Decl);
2890 end loop;
2891
2892 Spec_Id := Defining_Entity (Subp_Decl);
2893 Set_Corresponding_Spec (N, Spec_Id);
2894
2895 -- Mark the generated spec as a source construct to ensure that all
2896 -- calls to it are properly registered in ALI files for GNATprove.
2897
2898 Set_Comes_From_Source (Spec_Id, True);
2899
2900 -- Ensure that the specs of the subprogram declaration and its body
2901 -- are identical, otherwise they will appear non-conformant due to
2902 -- rewritings in the default values of formal parameters.
2903
2904 Body_Spec := Copy_Subprogram_Spec (Body_Spec);
2905 Set_Specification (N, Body_Spec);
2906 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
2907 end Build_Subprogram_Declaration;
2908
2909 ----------------------------
2910 -- Check_Anonymous_Return --
2911 ----------------------------
2912
2913 procedure Check_Anonymous_Return is
2914 Decl : Node_Id;
2915 Par : Node_Id;
2916 Scop : Entity_Id;
2917
2918 begin
2919 if Present (Spec_Id) then
2920 Scop := Spec_Id;
2921 else
2922 Scop := Body_Id;
2923 end if;
2924
2925 if Ekind (Scop) = E_Function
2926 and then Ekind (Etype (Scop)) = E_Anonymous_Access_Type
2927 and then not Is_Thunk (Scop)
2928
2929 -- Skip internally built functions which handle the case of
2930 -- a null access (see Expand_Interface_Conversion)
2931
2932 and then not (Is_Interface (Designated_Type (Etype (Scop)))
2933 and then not Comes_From_Source (Parent (Scop)))
2934
2935 and then (Has_Task (Designated_Type (Etype (Scop)))
2936 or else
2937 (Is_Class_Wide_Type (Designated_Type (Etype (Scop)))
2938 and then
2939 Is_Limited_Record (Designated_Type (Etype (Scop)))))
2940 and then Expander_Active
2941 then
2942 Decl := Build_Master_Declaration (Loc);
2943
2944 if Present (Declarations (N)) then
2945 Prepend (Decl, Declarations (N));
2946 else
2947 Set_Declarations (N, New_List (Decl));
2948 end if;
2949
2950 Set_Master_Id (Etype (Scop), Defining_Identifier (Decl));
2951 Set_Has_Master_Entity (Scop);
2952
2953 -- Now mark the containing scope as a task master
2954
2955 Par := N;
2956 while Nkind (Par) /= N_Compilation_Unit loop
2957 Par := Parent (Par);
2958 pragma Assert (Present (Par));
2959
2960 -- If we fall off the top, we are at the outer level, and
2961 -- the environment task is our effective master, so nothing
2962 -- to mark.
2963
2964 if Nkind_In
2965 (Par, N_Task_Body, N_Block_Statement, N_Subprogram_Body)
2966 then
2967 Set_Is_Task_Master (Par, True);
2968 exit;
2969 end if;
2970 end loop;
2971 end if;
2972 end Check_Anonymous_Return;
2973
2974 -------------------------
2975 -- Check_Inline_Pragma --
2976 -------------------------
2977
2978 procedure Check_Inline_Pragma (Spec : in out Node_Id) is
2979 Prag : Node_Id;
2980 Plist : List_Id;
2981
2982 function Is_Inline_Pragma (N : Node_Id) return Boolean;
2983 -- True when N is a pragma Inline or Inline_Always that applies
2984 -- to this subprogram.
2985
2986 -----------------------
2987 -- Is_Inline_Pragma --
2988 -----------------------
2989
2990 function Is_Inline_Pragma (N : Node_Id) return Boolean is
2991 begin
2992 if Nkind (N) = N_Pragma
2993 and then
2994 (Pragma_Name_Unmapped (N) = Name_Inline_Always
2995 or else (Pragma_Name_Unmapped (N) = Name_Inline
2996 and then
2997 (Front_End_Inlining or else Optimization_Level > 0)))
2998 and then Present (Pragma_Argument_Associations (N))
2999 then
3000 declare
3001 Pragma_Arg : Node_Id :=
3002 Expression (First (Pragma_Argument_Associations (N)));
3003 begin
3004 if Nkind (Pragma_Arg) = N_Selected_Component then
3005 Pragma_Arg := Selector_Name (Pragma_Arg);
3006 end if;
3007
3008 return Chars (Pragma_Arg) = Chars (Body_Id);
3009 end;
3010
3011 else
3012 return False;
3013 end if;
3014 end Is_Inline_Pragma;
3015
3016 -- Start of processing for Check_Inline_Pragma
3017
3018 begin
3019 if not Expander_Active then
3020 return;
3021 end if;
3022
3023 if Is_List_Member (N)
3024 and then Present (Next (N))
3025 and then Is_Inline_Pragma (Next (N))
3026 then
3027 Prag := Next (N);
3028
3029 elsif Nkind (N) /= N_Subprogram_Body_Stub
3030 and then Present (Declarations (N))
3031 and then Is_Inline_Pragma (First (Declarations (N)))
3032 then
3033 Prag := First (Declarations (N));
3034
3035 else
3036 Prag := Empty;
3037 end if;
3038
3039 if Present (Prag) then
3040 if Present (Spec_Id) then
3041 if Is_List_Member (N)
3042 and then Is_List_Member (Unit_Declaration_Node (Spec_Id))
3043 and then In_Same_List (N, Unit_Declaration_Node (Spec_Id))
3044 then
3045 Analyze (Prag);
3046 end if;
3047
3048 else
3049 -- Create a subprogram declaration, to make treatment uniform.
3050 -- Make the sloc of the subprogram name that of the entity in
3051 -- the body, so that style checks find identical strings.
3052
3053 declare
3054 Subp : constant Entity_Id :=
3055 Make_Defining_Identifier
3056 (Sloc (Body_Id), Chars (Body_Id));
3057 Decl : constant Node_Id :=
3058 Make_Subprogram_Declaration (Loc,
3059 Specification =>
3060 New_Copy_Tree (Specification (N)));
3061
3062 begin
3063 -- Link the body and the generated spec
3064
3065 Set_Corresponding_Body (Decl, Body_Id);
3066 Set_Corresponding_Spec (N, Subp);
3067
3068 Set_Defining_Unit_Name (Specification (Decl), Subp);
3069
3070 -- To ensure proper coverage when body is inlined, indicate
3071 -- whether the subprogram comes from source.
3072
3073 Set_Comes_From_Source (Subp, Comes_From_Source (N));
3074
3075 if Present (First_Formal (Body_Id)) then
3076 Plist := Copy_Parameter_List (Body_Id);
3077 Set_Parameter_Specifications
3078 (Specification (Decl), Plist);
3079 end if;
3080
3081 -- Move aspects to the new spec
3082
3083 if Has_Aspects (N) then
3084 Move_Aspects (N, To => Decl);
3085 end if;
3086
3087 Insert_Before (N, Decl);
3088 Analyze (Decl);
3089 Analyze (Prag);
3090 Set_Has_Pragma_Inline (Subp);
3091
3092 if Pragma_Name (Prag) = Name_Inline_Always then
3093 Set_Is_Inlined (Subp);
3094 Set_Has_Pragma_Inline_Always (Subp);
3095 end if;
3096
3097 -- Prior to copying the subprogram body to create a template
3098 -- for it for subsequent inlining, remove the pragma from
3099 -- the current body so that the copy that will produce the
3100 -- new body will start from a completely unanalyzed tree.
3101
3102 if Nkind (Parent (Prag)) = N_Subprogram_Body then
3103 Rewrite (Prag, Make_Null_Statement (Sloc (Prag)));
3104 end if;
3105
3106 Spec := Subp;
3107 end;
3108 end if;
3109 end if;
3110 end Check_Inline_Pragma;
3111
3112 --------------------------
3113 -- Check_Missing_Return --
3114 --------------------------
3115
3116 procedure Check_Missing_Return is
3117 Id : Entity_Id;
3118 Missing_Ret : Boolean;
3119
3120 begin
3121 if Nkind (Body_Spec) = N_Function_Specification then
3122 if Present (Spec_Id) then
3123 Id := Spec_Id;
3124 else
3125 Id := Body_Id;
3126 end if;
3127
3128 if Return_Present (Id) then
3129 Check_Returns (HSS, 'F', Missing_Ret);
3130
3131 if Missing_Ret then
3132 Set_Has_Missing_Return (Id);
3133 end if;
3134
3135 -- Within a premature instantiation of a package with no body, we
3136 -- build completions of the functions therein, with a Raise
3137 -- statement. No point in complaining about a missing return in
3138 -- this case.
3139
3140 elsif Ekind (Id) = E_Function
3141 and then In_Instance
3142 and then Present (Statements (HSS))
3143 and then Nkind (First (Statements (HSS))) = N_Raise_Program_Error
3144 then
3145 null;
3146
3147 elsif Is_Generic_Subprogram (Id)
3148 or else not Is_Machine_Code_Subprogram (Id)
3149 then
3150 Error_Msg_N ("missing RETURN statement in function body", N);
3151 end if;
3152
3153 -- If procedure with No_Return, check returns
3154
3155 elsif Nkind (Body_Spec) = N_Procedure_Specification then
3156 if Present (Spec_Id) then
3157 Id := Spec_Id;
3158 else
3159 Id := Body_Id;
3160 end if;
3161
3162 if No_Return (Id) then
3163 Check_Returns (HSS, 'P', Missing_Ret, Id);
3164 end if;
3165 end if;
3166 end Check_Missing_Return;
3167
3168 -----------------------
3169 -- Disambiguate_Spec --
3170 -----------------------
3171
3172 function Disambiguate_Spec return Entity_Id is
3173 Priv_Spec : Entity_Id;
3174 Spec_N : Entity_Id;
3175
3176 procedure Replace_Types (To_Corresponding : Boolean);
3177 -- Depending on the flag, replace the type of formal parameters of
3178 -- Body_Id if it is a concurrent type implementing interfaces with
3179 -- the corresponding record type or the other way around.
3180
3181 procedure Replace_Types (To_Corresponding : Boolean) is
3182 Formal : Entity_Id;
3183 Formal_Typ : Entity_Id;
3184
3185 begin
3186 Formal := First_Formal (Body_Id);
3187 while Present (Formal) loop
3188 Formal_Typ := Etype (Formal);
3189
3190 if Is_Class_Wide_Type (Formal_Typ) then
3191 Formal_Typ := Root_Type (Formal_Typ);
3192 end if;
3193
3194 -- From concurrent type to corresponding record
3195
3196 if To_Corresponding then
3197 if Is_Concurrent_Type (Formal_Typ)
3198 and then Present (Corresponding_Record_Type (Formal_Typ))
3199 and then
3200 Present (Interfaces
3201 (Corresponding_Record_Type (Formal_Typ)))
3202 then
3203 Set_Etype (Formal,
3204 Corresponding_Record_Type (Formal_Typ));
3205 end if;
3206
3207 -- From corresponding record to concurrent type
3208
3209 else
3210 if Is_Concurrent_Record_Type (Formal_Typ)
3211 and then Present (Interfaces (Formal_Typ))
3212 then
3213 Set_Etype (Formal,
3214 Corresponding_Concurrent_Type (Formal_Typ));
3215 end if;
3216 end if;
3217
3218 Next_Formal (Formal);
3219 end loop;
3220 end Replace_Types;
3221
3222 -- Start of processing for Disambiguate_Spec
3223
3224 begin
3225 -- Try to retrieve the specification of the body as is. All error
3226 -- messages are suppressed because the body may not have a spec in
3227 -- its current state.
3228
3229 Spec_N := Find_Corresponding_Spec (N, False);
3230
3231 -- It is possible that this is the body of a primitive declared
3232 -- between a private and a full view of a concurrent type. The
3233 -- controlling parameter of the spec carries the concurrent type,
3234 -- not the corresponding record type as transformed by Analyze_
3235 -- Subprogram_Specification. In such cases, we undo the change
3236 -- made by the analysis of the specification and try to find the
3237 -- spec again.
3238
3239 -- Note that wrappers already have their corresponding specs and
3240 -- bodies set during their creation, so if the candidate spec is
3241 -- a wrapper, then we definitely need to swap all types to their
3242 -- original concurrent status.
3243
3244 if No (Spec_N)
3245 or else Is_Primitive_Wrapper (Spec_N)
3246 then
3247 -- Restore all references of corresponding record types to the
3248 -- original concurrent types.
3249
3250 Replace_Types (To_Corresponding => False);
3251 Priv_Spec := Find_Corresponding_Spec (N, False);
3252
3253 -- The current body truly belongs to a primitive declared between
3254 -- a private and a full view. We leave the modified body as is,
3255 -- and return the true spec.
3256
3257 if Present (Priv_Spec)
3258 and then Is_Private_Primitive (Priv_Spec)
3259 then
3260 return Priv_Spec;
3261 end if;
3262
3263 -- In case that this is some sort of error, restore the original
3264 -- state of the body.
3265
3266 Replace_Types (To_Corresponding => True);
3267 end if;
3268
3269 return Spec_N;
3270 end Disambiguate_Spec;
3271
3272 ----------------------------
3273 -- Exchange_Limited_Views --
3274 ----------------------------
3275
3276 function Exchange_Limited_Views (Subp_Id : Entity_Id) return Elist_Id is
3277 Result : Elist_Id := No_Elist;
3278
3279 procedure Detect_And_Exchange (Id : Entity_Id);
3280 -- Determine whether Id's type denotes an incomplete type associated
3281 -- with a limited with clause and exchange the limited view with the
3282 -- non-limited one when available. Note that the non-limited view
3283 -- may exist because of a with_clause in another unit in the context,
3284 -- but cannot be used because the current view of the enclosing unit
3285 -- is still a limited view.
3286
3287 -------------------------
3288 -- Detect_And_Exchange --
3289 -------------------------
3290
3291 procedure Detect_And_Exchange (Id : Entity_Id) is
3292 Typ : constant Entity_Id := Etype (Id);
3293 begin
3294 if From_Limited_With (Typ)
3295 and then Has_Non_Limited_View (Typ)
3296 and then not From_Limited_With (Scope (Typ))
3297 then
3298 if No (Result) then
3299 Result := New_Elmt_List;
3300 end if;
3301
3302 Prepend_Elmt (Typ, Result);
3303 Prepend_Elmt (Id, Result);
3304 Set_Etype (Id, Non_Limited_View (Typ));
3305 end if;
3306 end Detect_And_Exchange;
3307
3308 -- Local variables
3309
3310 Formal : Entity_Id;
3311
3312 -- Start of processing for Exchange_Limited_Views
3313
3314 begin
3315 -- Do not process subprogram bodies as they already use the non-
3316 -- limited view of types.
3317
3318 if not Ekind_In (Subp_Id, E_Function, E_Procedure) then
3319 return No_Elist;
3320 end if;
3321
3322 -- Examine all formals and swap views when applicable
3323
3324 Formal := First_Formal (Subp_Id);
3325 while Present (Formal) loop
3326 Detect_And_Exchange (Formal);
3327
3328 Next_Formal (Formal);
3329 end loop;
3330
3331 -- Process the return type of a function
3332
3333 if Ekind (Subp_Id) = E_Function then
3334 Detect_And_Exchange (Subp_Id);
3335 end if;
3336
3337 return Result;
3338 end Exchange_Limited_Views;
3339
3340 -------------------------------------
3341 -- Is_Private_Concurrent_Primitive --
3342 -------------------------------------
3343
3344 function Is_Private_Concurrent_Primitive
3345 (Subp_Id : Entity_Id) return Boolean
3346 is
3347 Formal_Typ : Entity_Id;
3348
3349 begin
3350 if Present (First_Formal (Subp_Id)) then
3351 Formal_Typ := Etype (First_Formal (Subp_Id));
3352
3353 if Is_Concurrent_Record_Type (Formal_Typ) then
3354 if Is_Class_Wide_Type (Formal_Typ) then
3355 Formal_Typ := Root_Type (Formal_Typ);
3356 end if;
3357
3358 Formal_Typ := Corresponding_Concurrent_Type (Formal_Typ);
3359 end if;
3360
3361 -- The type of the first formal is a concurrent tagged type with
3362 -- a private view.
3363
3364 return
3365 Is_Concurrent_Type (Formal_Typ)
3366 and then Is_Tagged_Type (Formal_Typ)
3367 and then Has_Private_Declaration (Formal_Typ);
3368 end if;
3369
3370 return False;
3371 end Is_Private_Concurrent_Primitive;
3372
3373 -------------------------
3374 -- Mask_Unfrozen_Types --
3375 -------------------------
3376
3377 function Mask_Unfrozen_Types (Spec_Id : Entity_Id) return Elist_Id is
3378 Result : Elist_Id := No_Elist;
3379
3380 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result;
3381 -- Mask all types referenced in the subtree rooted at Node
3382
3383 --------------------
3384 -- Mask_Type_Refs --
3385 --------------------
3386
3387 function Mask_Type_Refs (Node : Node_Id) return Traverse_Result is
3388 procedure Mask_Type (Typ : Entity_Id);
3389 -- ??? what does this do?
3390
3391 ---------------
3392 -- Mask_Type --
3393 ---------------
3394
3395 procedure Mask_Type (Typ : Entity_Id) is
3396 begin
3397 -- Skip Itypes created by the preanalysis
3398
3399 if Is_Itype (Typ)
3400 and then Scope_Within_Or_Same (Scope (Typ), Spec_Id)
3401 then
3402 return;
3403 end if;
3404
3405 if not Is_Frozen (Typ) then
3406 if Scope (Typ) /= Current_Scope then
3407 Set_Is_Frozen (Typ);
3408 Append_New_Elmt (Typ, Result);
3409 else
3410 Freeze_Before (N, Typ);
3411 end if;
3412 end if;
3413 end Mask_Type;
3414
3415 -- Start of processing for Mask_Type_Refs
3416
3417 begin
3418 if Is_Entity_Name (Node) and then Present (Entity (Node)) then
3419 Mask_Type (Etype (Entity (Node)));
3420
3421 if Ekind_In (Entity (Node), E_Component, E_Discriminant) then
3422 Mask_Type (Scope (Entity (Node)));
3423 end if;
3424
3425 elsif Nkind_In (Node, N_Aggregate, N_Null, N_Type_Conversion)
3426 and then Present (Etype (Node))
3427 then
3428 Mask_Type (Etype (Node));
3429 end if;
3430
3431 return OK;
3432 end Mask_Type_Refs;
3433
3434 procedure Mask_References is new Traverse_Proc (Mask_Type_Refs);
3435
3436 -- Local variables
3437
3438 Return_Stmt : constant Node_Id :=
3439 First (Statements (Handled_Statement_Sequence (N)));
3440
3441 -- Start of processing for Mask_Unfrozen_Types
3442
3443 begin
3444 pragma Assert (Nkind (Return_Stmt) = N_Simple_Return_Statement);
3445
3446 Mask_References (Expression (Return_Stmt));
3447
3448 return Result;
3449 end Mask_Unfrozen_Types;
3450
3451 ------------------
3452 -- Move_Pragmas --
3453 ------------------
3454
3455 procedure Move_Pragmas (From : Node_Id; To : Node_Id) is
3456 Decl : Node_Id;
3457 Insert_Nod : Node_Id;
3458 Next_Decl : Node_Id;
3459
3460 begin
3461 pragma Assert (Nkind (From) = N_Subprogram_Body);
3462
3463 -- The pragmas are moved in an order-preserving fashion
3464
3465 Insert_Nod := To;
3466
3467 -- Inspect the declarations of the subprogram body and relocate all
3468 -- candidate pragmas.
3469
3470 Decl := First (Declarations (From));
3471 while Present (Decl) loop
3472
3473 -- Preserve the following declaration for iteration purposes, due
3474 -- to possible relocation of a pragma.
3475
3476 Next_Decl := Next (Decl);
3477
3478 if Nkind (Decl) = N_Pragma then
3479 -- Copy pragma SPARK_Mode if present in the declarative list
3480 -- of subprogram body From and insert it after node To. This
3481 -- pragma should not be moved, as it applies to the body too.
3482
3483 if Pragma_Name_Unmapped (Decl) = Name_SPARK_Mode then
3484 Insert_After (Insert_Nod, New_Copy_Tree (Decl));
3485
3486 -- Move relevant pragmas to the spec
3487
3488 elsif Nam_In (Pragma_Name_Unmapped (Decl),
3489 Name_Depends,
3490 Name_Ghost,
3491 Name_Global,
3492 Name_Pre,
3493 Name_Precondition,
3494 Name_Post,
3495 Name_Refined_Depends,
3496 Name_Refined_Global,
3497 Name_Refined_Post,
3498 Name_Inline,
3499 Name_Pure_Function,
3500 Name_Volatile_Function)
3501 then
3502 Remove (Decl);
3503 Insert_After (Insert_Nod, Decl);
3504 Insert_Nod := Decl;
3505 end if;
3506
3507 -- Skip internally generated code
3508
3509 elsif not Comes_From_Source (Decl) then
3510 null;
3511
3512 -- No candidate pragmas are available for relocation
3513
3514 else
3515 exit;
3516 end if;
3517
3518 Decl := Next_Decl;
3519 end loop;
3520 end Move_Pragmas;
3521
3522 ---------------------------
3523 -- Restore_Limited_Views --
3524 ---------------------------
3525
3526 procedure Restore_Limited_Views (Restore_List : Elist_Id) is
3527 Elmt : Elmt_Id := First_Elmt (Restore_List);
3528 Id : Entity_Id;
3529
3530 begin
3531 while Present (Elmt) loop
3532 Id := Node (Elmt);
3533 Next_Elmt (Elmt);
3534 Set_Etype (Id, Node (Elmt));
3535 Next_Elmt (Elmt);
3536 end loop;
3537 end Restore_Limited_Views;
3538
3539 ----------------------------
3540 -- Set_Trivial_Subprogram --
3541 ----------------------------
3542
3543 procedure Set_Trivial_Subprogram (N : Node_Id) is
3544 Nxt : constant Node_Id := Next (N);
3545
3546 begin
3547 Set_Is_Trivial_Subprogram (Body_Id);
3548
3549 if Present (Spec_Id) then
3550 Set_Is_Trivial_Subprogram (Spec_Id);
3551 end if;
3552
3553 if Present (Nxt)
3554 and then Nkind (Nxt) = N_Simple_Return_Statement
3555 and then No (Next (Nxt))
3556 and then Present (Expression (Nxt))
3557 and then Is_Entity_Name (Expression (Nxt))
3558 then
3559 Set_Never_Set_In_Source (Entity (Expression (Nxt)), False);
3560 end if;
3561 end Set_Trivial_Subprogram;
3562
3563 ---------------------------
3564 -- Unmask_Unfrozen_Types --
3565 ---------------------------
3566
3567 procedure Unmask_Unfrozen_Types (Unmask_List : Elist_Id) is
3568 Elmt : Elmt_Id := First_Elmt (Unmask_List);
3569
3570 begin
3571 while Present (Elmt) loop
3572 Set_Is_Frozen (Node (Elmt), False);
3573 Next_Elmt (Elmt);
3574 end loop;
3575 end Unmask_Unfrozen_Types;
3576
3577 ---------------------------------
3578 -- Verify_Overriding_Indicator --
3579 ---------------------------------
3580
3581 procedure Verify_Overriding_Indicator is
3582 begin
3583 if Must_Override (Body_Spec) then
3584 if Nkind (Spec_Id) = N_Defining_Operator_Symbol
3585 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3586 then
3587 null;
3588
3589 -- Overridden controlled primitives may have had their
3590 -- Overridden_Operation field cleared according to the setting of
3591 -- the Is_Hidden flag. An issue arises, however, when analyzing
3592 -- an instance that may have manipulated the flag during
3593 -- expansion. As a result, we add an exception for this case.
3594
3595 elsif not Present (Overridden_Operation (Spec_Id))
3596 and then not (Nam_In (Chars (Spec_Id), Name_Adjust,
3597 Name_Finalize,
3598 Name_Initialize)
3599 and then In_Instance)
3600 then
3601 Error_Msg_NE
3602 ("subprogram& is not overriding", Body_Spec, Spec_Id);
3603
3604 -- Overriding indicators aren't allowed for protected subprogram
3605 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3606 -- this to a warning if -gnatd.E is enabled.
3607
3608 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3609 Error_Msg_Warn := Error_To_Warning;
3610 Error_Msg_N
3611 ("<<overriding indicator not allowed for protected "
3612 & "subprogram body", Body_Spec);
3613 end if;
3614
3615 elsif Must_Not_Override (Body_Spec) then
3616 if Present (Overridden_Operation (Spec_Id)) then
3617 Error_Msg_NE
3618 ("subprogram& overrides inherited operation",
3619 Body_Spec, Spec_Id);
3620
3621 elsif Nkind (Spec_Id) = N_Defining_Operator_Symbol
3622 and then Operator_Matches_Spec (Spec_Id, Spec_Id)
3623 then
3624 Error_Msg_NE
3625 ("subprogram& overrides predefined operator ",
3626 Body_Spec, Spec_Id);
3627
3628 -- Overriding indicators aren't allowed for protected subprogram
3629 -- bodies (see the Confirmation in Ada Comment AC95-00213). Change
3630 -- this to a warning if -gnatd.E is enabled.
3631
3632 elsif Ekind (Scope (Spec_Id)) = E_Protected_Type then
3633 Error_Msg_Warn := Error_To_Warning;
3634
3635 Error_Msg_N
3636 ("<<overriding indicator not allowed "
3637 & "for protected subprogram body", Body_Spec);
3638
3639 -- If this is not a primitive operation, then the overriding
3640 -- indicator is altogether illegal.
3641
3642 elsif not Is_Primitive (Spec_Id) then
3643 Error_Msg_N
3644 ("overriding indicator only allowed "
3645 & "if subprogram is primitive", Body_Spec);
3646 end if;
3647
3648 -- If checking the style rule and the operation overrides, then
3649 -- issue a warning about a missing overriding_indicator. Protected
3650 -- subprogram bodies are excluded from this style checking, since
3651 -- they aren't primitives (even though their declarations can
3652 -- override) and aren't allowed to have an overriding_indicator.
3653
3654 elsif Style_Check
3655 and then Present (Overridden_Operation (Spec_Id))
3656 and then Ekind (Scope (Spec_Id)) /= E_Protected_Type
3657 then
3658 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3659 Style.Missing_Overriding (N, Body_Id);
3660
3661 elsif Style_Check
3662 and then Can_Override_Operator (Spec_Id)
3663 and then not In_Predefined_Unit (Spec_Id)
3664 then
3665 pragma Assert (Unit_Declaration_Node (Body_Id) = N);
3666 Style.Missing_Overriding (N, Body_Id);
3667 end if;
3668 end Verify_Overriding_Indicator;
3669
3670 -- Local variables
3671
3672 Body_Nod : Node_Id := Empty;
3673 Minimum_Acc_Objs : List_Id := No_List;
3674
3675 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3676 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3677 Saved_EA : constant Boolean := Expander_Active;
3678 Saved_ISMP : constant Boolean :=
3679 Ignore_SPARK_Mode_Pragmas_In_Instance;
3680 -- Save the Ghost and SPARK mode-related data to restore on exit
3681
3682 -- Start of processing for Analyze_Subprogram_Body_Helper
3683
3684 begin
3685 -- A [generic] subprogram body freezes the contract of the nearest
3686 -- enclosing package body and all other contracts encountered in the
3687 -- same declarative part up to and excluding the subprogram body:
3688
3689 -- package body Nearest_Enclosing_Package
3690 -- with Refined_State => (State => Constit)
3691 -- is
3692 -- Constit : ...;
3693
3694 -- procedure Freezes_Enclosing_Package_Body
3695 -- with Refined_Depends => (Input => Constit) ...
3696
3697 -- This ensures that any annotations referenced by the contract of the
3698 -- [generic] subprogram body are available. This form of freezing is
3699 -- decoupled from the usual Freeze_xxx mechanism because it must also
3700 -- work in the context of generics where normal freezing is disabled.
3701
3702 -- Only bodies coming from source should cause this type of freezing.
3703 -- Expression functions that act as bodies and complete an initial
3704 -- declaration must be included in this category, hence the use of
3705 -- Original_Node.
3706
3707 if Comes_From_Source (Original_Node (N)) then
3708 Freeze_Previous_Contracts (N);
3709 end if;
3710
3711 -- Generic subprograms are handled separately. They always have a
3712 -- generic specification. Determine whether current scope has a
3713 -- previous declaration.
3714
3715 -- If the subprogram body is defined within an instance of the same
3716 -- name, the instance appears as a package renaming, and will be hidden
3717 -- within the subprogram.
3718
3719 if Present (Prev_Id)
3720 and then not Is_Overloadable (Prev_Id)
3721 and then (Nkind (Parent (Prev_Id)) /= N_Package_Renaming_Declaration
3722 or else Comes_From_Source (Prev_Id))
3723 then
3724 if Is_Generic_Subprogram (Prev_Id) then
3725 Spec_Id := Prev_Id;
3726
3727 -- A subprogram body is Ghost when it is stand-alone and subject
3728 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3729 -- the mode now to ensure that any nodes generated during analysis
3730 -- and expansion are properly marked as Ghost.
3731
3732 Mark_And_Set_Ghost_Body (N, Spec_Id);
3733
3734 -- If the body completes the initial declaration of a compilation
3735 -- unit which is subject to pragma Elaboration_Checks, set the
3736 -- model specified by the pragma because it applies to all parts
3737 -- of the unit.
3738
3739 Install_Elaboration_Model (Spec_Id);
3740
3741 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
3742 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
3743
3744 Analyze_Generic_Subprogram_Body (N, Spec_Id);
3745
3746 if Nkind (N) = N_Subprogram_Body then
3747 HSS := Handled_Statement_Sequence (N);
3748 Check_Missing_Return;
3749 end if;
3750
3751 goto Leave;
3752
3753 -- Otherwise a previous entity conflicts with the subprogram name.
3754 -- Attempting to enter name will post error.
3755
3756 else
3757 Enter_Name (Body_Id);
3758 goto Leave;
3759 end if;
3760
3761 -- Non-generic case, find the subprogram declaration, if one was seen,
3762 -- or enter new overloaded entity in the current scope. If the
3763 -- Current_Entity is the Body_Id itself, the unit is being analyzed as
3764 -- part of the context of one of its subunits. No need to redo the
3765 -- analysis.
3766
3767 elsif Prev_Id = Body_Id and then Has_Completion (Body_Id) then
3768 goto Leave;
3769
3770 else
3771 Body_Id := Analyze_Subprogram_Specification (Body_Spec);
3772
3773 if Nkind (N) = N_Subprogram_Body_Stub
3774 or else No (Corresponding_Spec (N))
3775 then
3776 if Is_Private_Concurrent_Primitive (Body_Id) then
3777 Spec_Id := Disambiguate_Spec;
3778
3779 -- A subprogram body is Ghost when it is stand-alone and
3780 -- subject to pragma Ghost or when the corresponding spec is
3781 -- Ghost. Set the mode now to ensure that any nodes generated
3782 -- during analysis and expansion are properly marked as Ghost.
3783
3784 Mark_And_Set_Ghost_Body (N, Spec_Id);
3785
3786 -- If the body completes a compilation unit which is subject
3787 -- to pragma Elaboration_Checks, set the model specified by
3788 -- the pragma because it applies to all parts of the unit.
3789
3790 Install_Elaboration_Model (Spec_Id);
3791
3792 else
3793 Spec_Id := Find_Corresponding_Spec (N);
3794
3795 -- A subprogram body is Ghost when it is stand-alone and
3796 -- subject to pragma Ghost or when the corresponding spec is
3797 -- Ghost. Set the mode now to ensure that any nodes generated
3798 -- during analysis and expansion are properly marked as Ghost.
3799
3800 Mark_And_Set_Ghost_Body (N, Spec_Id);
3801
3802 -- If the body completes a compilation unit which is subject
3803 -- to pragma Elaboration_Checks, set the model specified by
3804 -- the pragma because it applies to all parts of the unit.
3805
3806 Install_Elaboration_Model (Spec_Id);
3807
3808 -- In GNATprove mode, if the body has no previous spec, create
3809 -- one so that the inlining machinery can operate properly.
3810 -- Transfer aspects, if any, to the new spec, so that they
3811 -- are legal and can be processed ahead of the body.
3812 -- We make two copies of the given spec, one for the new
3813 -- declaration, and one for the body.
3814 -- ??? This should be conditioned on front-end inlining rather
3815 -- than GNATprove_Mode.
3816
3817 if No (Spec_Id) and then GNATprove_Mode
3818
3819 -- Inlining does not apply during preanalysis of code
3820
3821 and then Full_Analysis
3822
3823 -- Inlining only applies to full bodies, not stubs
3824
3825 and then Nkind (N) /= N_Subprogram_Body_Stub
3826
3827 -- Inlining only applies to bodies in the source code, not to
3828 -- those generated by the compiler. In particular, expression
3829 -- functions, whose body is generated by the compiler, are
3830 -- treated specially by GNATprove.
3831
3832 and then Comes_From_Source (Body_Id)
3833
3834 -- This cannot be done for a compilation unit, which is not
3835 -- in a context where we can insert a new spec.
3836
3837 and then Is_List_Member (N)
3838
3839 -- Inlining only applies to subprograms without contracts,
3840 -- as a contract is a sign that GNATprove should perform a
3841 -- modular analysis of the subprogram instead of a contextual
3842 -- analysis at each call site. The same test is performed in
3843 -- Inline.Can_Be_Inlined_In_GNATprove_Mode. It is repeated
3844 -- here in another form (because the contract has not been
3845 -- attached to the body) to avoid front-end errors in case
3846 -- pragmas are used instead of aspects, because the
3847 -- corresponding pragmas in the body would not be transferred
3848 -- to the spec, leading to legality errors.
3849
3850 and then not Body_Has_Contract
3851 and then not Inside_A_Generic
3852 then
3853 Build_Subprogram_Declaration;
3854
3855 -- If this is a function that returns a constrained array, and
3856 -- we are generating C code, create subprogram declaration
3857 -- to simplify subsequent C generation.
3858
3859 elsif No (Spec_Id)
3860 and then Modify_Tree_For_C
3861 and then Nkind (Body_Spec) = N_Function_Specification
3862 and then Is_Array_Type (Etype (Body_Id))
3863 and then Is_Constrained (Etype (Body_Id))
3864 then
3865 Build_Subprogram_Declaration;
3866 end if;
3867 end if;
3868
3869 -- If this is a duplicate body, no point in analyzing it
3870
3871 if Error_Posted (N) then
3872 goto Leave;
3873 end if;
3874
3875 -- A subprogram body should cause freezing of its own declaration,
3876 -- but if there was no previous explicit declaration, then the
3877 -- subprogram will get frozen too late (there may be code within
3878 -- the body that depends on the subprogram having been frozen,
3879 -- such as uses of extra formals), so we force it to be frozen
3880 -- here. Same holds if the body and spec are compilation units.
3881 -- Finally, if the return type is an anonymous access to protected
3882 -- subprogram, it must be frozen before the body because its
3883 -- expansion has generated an equivalent type that is used when
3884 -- elaborating the body.
3885
3886 -- An exception in the case of Ada 2012, AI05-177: The bodies
3887 -- created for expression functions do not freeze.
3888
3889 if No (Spec_Id)
3890 and then Nkind (Original_Node (N)) /= N_Expression_Function
3891 then
3892 Freeze_Before (N, Body_Id);
3893
3894 elsif Nkind (Parent (N)) = N_Compilation_Unit then
3895 Freeze_Before (N, Spec_Id);
3896
3897 elsif Is_Access_Subprogram_Type (Etype (Body_Id)) then
3898 Freeze_Before (N, Etype (Body_Id));
3899 end if;
3900
3901 else
3902 Spec_Id := Corresponding_Spec (N);
3903
3904 -- A subprogram body is Ghost when it is stand-alone and subject
3905 -- to pragma Ghost or when the corresponding spec is Ghost. Set
3906 -- the mode now to ensure that any nodes generated during analysis
3907 -- and expansion are properly marked as Ghost.
3908
3909 Mark_And_Set_Ghost_Body (N, Spec_Id);
3910
3911 -- If the body completes the initial declaration of a compilation
3912 -- unit which is subject to pragma Elaboration_Checks, set the
3913 -- model specified by the pragma because it applies to all parts
3914 -- of the unit.
3915
3916 Install_Elaboration_Model (Spec_Id);
3917 end if;
3918 end if;
3919
3920 -- Deactivate expansion inside the body of ignored Ghost entities,
3921 -- as this code will ultimately be ignored. This avoids requiring the
3922 -- presence of run-time units which are not needed. Only do this for
3923 -- user entities, as internally generated entitities might still need
3924 -- to be expanded (e.g. those generated for types).
3925
3926 if Present (Ignored_Ghost_Region)
3927 and then Comes_From_Source (Body_Id)
3928 then
3929 Expander_Active := False;
3930 end if;
3931
3932 -- Previously we scanned the body to look for nested subprograms, and
3933 -- rejected an inline directive if nested subprograms were present,
3934 -- because the back-end would generate conflicting symbols for the
3935 -- nested bodies. This is now unnecessary.
3936
3937 -- Look ahead to recognize a pragma Inline that appears after the body
3938
3939 Check_Inline_Pragma (Spec_Id);
3940
3941 -- Deal with special case of a fully private operation in the body of
3942 -- the protected type. We must create a declaration for the subprogram,
3943 -- in order to attach the subprogram that will be used in internal
3944 -- calls. We exclude compiler generated bodies from the expander since
3945 -- the issue does not arise for those cases.
3946
3947 if No (Spec_Id)
3948 and then Comes_From_Source (N)
3949 and then Is_Protected_Type (Current_Scope)
3950 then
3951 Spec_Id := Build_Internal_Protected_Declaration (N);
3952 end if;
3953
3954 -- If we are generating C and this is a function returning a constrained
3955 -- array type for which we must create a procedure with an extra out
3956 -- parameter, build and analyze the body now. The procedure declaration
3957 -- has already been created. We reuse the source body of the function,
3958 -- because in an instance it may contain global references that cannot
3959 -- be reanalyzed. The source function itself is not used any further,
3960 -- so we mark it as having a completion. If the subprogram is a stub the
3961 -- transformation is done later, when the proper body is analyzed.
3962
3963 if Expander_Active
3964 and then Modify_Tree_For_C
3965 and then Present (Spec_Id)
3966 and then Ekind (Spec_Id) = E_Function
3967 and then Nkind (N) /= N_Subprogram_Body_Stub
3968 and then Rewritten_For_C (Spec_Id)
3969 then
3970 Set_Has_Completion (Spec_Id);
3971
3972 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
3973 Analyze (N);
3974
3975 -- The entity for the created procedure must remain invisible, so it
3976 -- does not participate in resolution of subsequent references to the
3977 -- function.
3978
3979 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
3980 goto Leave;
3981 end if;
3982
3983 -- If a separate spec is present, then deal with freezing issues
3984
3985 if Present (Spec_Id) then
3986 Spec_Decl := Unit_Declaration_Node (Spec_Id);
3987 Verify_Overriding_Indicator;
3988
3989 -- In general, the spec will be frozen when we start analyzing the
3990 -- body. However, for internally generated operations, such as
3991 -- wrapper functions for inherited operations with controlling
3992 -- results, the spec may not have been frozen by the time we expand
3993 -- the freeze actions that include the bodies. In particular, extra
3994 -- formals for accessibility or for return-in-place may need to be
3995 -- generated. Freeze nodes, if any, are inserted before the current
3996 -- body. These freeze actions are also needed in Compile_Only mode to
3997 -- enable the proper back-end type annotations.
3998 -- They are necessary in any case to ensure proper elaboration order
3999 -- in gigi.
4000
4001 if Nkind (N) = N_Subprogram_Body
4002 and then Was_Expression_Function (N)
4003 and then not Has_Completion (Spec_Id)
4004 and then Serious_Errors_Detected = 0
4005 and then (Expander_Active
4006 or else Operating_Mode = Check_Semantics
4007 or else Is_Ignored_Ghost_Entity (Spec_Id))
4008 then
4009 -- The body generated for an expression function that is not a
4010 -- completion is a freeze point neither for the profile nor for
4011 -- anything else. That's why, in order to prevent any freezing
4012 -- during analysis, we need to mask types declared outside the
4013 -- expression (and in an outer scope) that are not yet frozen.
4014 -- This also needs to be done in the case of an ignored Ghost
4015 -- expression function, where the expander isn't active.
4016
4017 Set_Is_Frozen (Spec_Id);
4018 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
4019
4020 elsif not Is_Frozen (Spec_Id)
4021 and then Serious_Errors_Detected = 0
4022 then
4023 Set_Has_Delayed_Freeze (Spec_Id);
4024 Freeze_Before (N, Spec_Id);
4025 end if;
4026 end if;
4027
4028 -- If the subprogram has a class-wide clone, build its body as a copy
4029 -- of the original body, and rewrite body of original subprogram as a
4030 -- wrapper that calls the clone. If N is a stub, this construction will
4031 -- take place when the proper body is analyzed. No action needed if this
4032 -- subprogram has been eliminated.
4033
4034 if Present (Spec_Id)
4035 and then Present (Class_Wide_Clone (Spec_Id))
4036 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
4037 and then Nkind (N) /= N_Subprogram_Body_Stub
4038 and then not (Expander_Active and then Is_Eliminated (Spec_Id))
4039 then
4040 Build_Class_Wide_Clone_Body (Spec_Id, N);
4041
4042 -- This is the new body for the existing primitive operation
4043
4044 Rewrite (N, Build_Class_Wide_Clone_Call
4045 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
4046 Set_Has_Completion (Spec_Id, False);
4047 Analyze (N);
4048 return;
4049 end if;
4050
4051 -- Place subprogram on scope stack, and make formals visible. If there
4052 -- is a spec, the visible entity remains that of the spec.
4053
4054 if Present (Spec_Id) then
4055 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
4056
4057 if Is_Child_Unit (Spec_Id) then
4058 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
4059 end if;
4060
4061 if Style_Check then
4062 Style.Check_Identifier (Body_Id, Spec_Id);
4063 end if;
4064
4065 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4066 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4067
4068 if Is_Abstract_Subprogram (Spec_Id) then
4069 Error_Msg_N ("an abstract subprogram cannot have a body", N);
4070 goto Leave;
4071
4072 else
4073 Set_Convention (Body_Id, Convention (Spec_Id));
4074 Set_Has_Completion (Spec_Id);
4075
4076 if Is_Protected_Type (Scope (Spec_Id)) then
4077 Prot_Typ := Scope (Spec_Id);
4078 end if;
4079
4080 -- If this is a body generated for a renaming, do not check for
4081 -- full conformance. The check is redundant, because the spec of
4082 -- the body is a copy of the spec in the renaming declaration,
4083 -- and the test can lead to spurious errors on nested defaults.
4084
4085 if Present (Spec_Decl)
4086 and then not Comes_From_Source (N)
4087 and then
4088 (Nkind (Original_Node (Spec_Decl)) =
4089 N_Subprogram_Renaming_Declaration
4090 or else (Present (Corresponding_Body (Spec_Decl))
4091 and then
4092 Nkind (Unit_Declaration_Node
4093 (Corresponding_Body (Spec_Decl))) =
4094 N_Subprogram_Renaming_Declaration))
4095 then
4096 Conformant := True;
4097
4098 -- Conversely, the spec may have been generated for specless body
4099 -- with an inline pragma. The entity comes from source, which is
4100 -- both semantically correct and necessary for proper inlining.
4101 -- The subprogram declaration itself is not in the source.
4102
4103 elsif Comes_From_Source (N)
4104 and then Present (Spec_Decl)
4105 and then not Comes_From_Source (Spec_Decl)
4106 and then Has_Pragma_Inline (Spec_Id)
4107 then
4108 Conformant := True;
4109
4110 else
4111 Check_Conformance
4112 (Body_Id, Spec_Id,
4113 Fully_Conformant, True, Conformant, Body_Id);
4114 end if;
4115
4116 -- If the body is not fully conformant, we have to decide if we
4117 -- should analyze it or not. If it has a really messed up profile
4118 -- then we probably should not analyze it, since we will get too
4119 -- many bogus messages.
4120
4121 -- Our decision is to go ahead in the non-fully conformant case
4122 -- only if it is at least mode conformant with the spec. Note
4123 -- that the call to Check_Fully_Conformant has issued the proper
4124 -- error messages to complain about the lack of conformance.
4125
4126 if not Conformant
4127 and then not Mode_Conformant (Body_Id, Spec_Id)
4128 then
4129 goto Leave;
4130 end if;
4131 end if;
4132
4133 -- In the case we are dealing with an expression function we check
4134 -- the formals attached to the spec instead of the body - so we don't
4135 -- reference body formals.
4136
4137 if Spec_Id /= Body_Id
4138 and then not Is_Expression_Function (Spec_Id)
4139 then
4140 Reference_Body_Formals (Spec_Id, Body_Id);
4141 end if;
4142
4143 Set_Ekind (Body_Id, E_Subprogram_Body);
4144
4145 if Nkind (N) = N_Subprogram_Body_Stub then
4146 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4147
4148 -- Regular body
4149
4150 else
4151 Set_Corresponding_Spec (N, Spec_Id);
4152
4153 -- Ada 2005 (AI-345): If the operation is a primitive operation
4154 -- of a concurrent type, the type of the first parameter has been
4155 -- replaced with the corresponding record, which is the proper
4156 -- run-time structure to use. However, within the body there may
4157 -- be uses of the formals that depend on primitive operations
4158 -- of the type (in particular calls in prefixed form) for which
4159 -- we need the original concurrent type. The operation may have
4160 -- several controlling formals, so the replacement must be done
4161 -- for all of them.
4162
4163 if Comes_From_Source (Spec_Id)
4164 and then Present (First_Entity (Spec_Id))
4165 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4166 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4167 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4168 and then Present (Corresponding_Concurrent_Type
4169 (Etype (First_Entity (Spec_Id))))
4170 then
4171 declare
4172 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4173 Form : Entity_Id;
4174
4175 begin
4176 Form := First_Formal (Spec_Id);
4177 while Present (Form) loop
4178 if Etype (Form) = Typ then
4179 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4180 end if;
4181
4182 Next_Formal (Form);
4183 end loop;
4184 end;
4185 end if;
4186
4187 -- Make the formals visible, and place subprogram on scope stack.
4188 -- This is also the point at which we set Last_Real_Spec_Entity
4189 -- to mark the entities which will not be moved to the body.
4190
4191 Install_Formals (Spec_Id);
4192 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4193
4194 -- Within an instance, add local renaming declarations so that
4195 -- gdb can retrieve the values of actuals more easily. This is
4196 -- only relevant if generating code.
4197
4198 if Is_Generic_Instance (Spec_Id)
4199 and then Is_Wrapper_Package (Current_Scope)
4200 and then Expander_Active
4201 then
4202 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4203 end if;
4204
4205 Push_Scope (Spec_Id);
4206
4207 -- Make sure that the subprogram is immediately visible. For
4208 -- child units that have no separate spec this is indispensable.
4209 -- Otherwise it is safe albeit redundant.
4210
4211 Set_Is_Immediately_Visible (Spec_Id);
4212 end if;
4213
4214 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4215 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4216 Set_Scope (Body_Id, Scope (Spec_Id));
4217
4218 -- Case of subprogram body with no previous spec
4219
4220 else
4221 -- Check for style warning required
4222
4223 if Style_Check
4224
4225 -- Only apply check for source level subprograms for which checks
4226 -- have not been suppressed.
4227
4228 and then Comes_From_Source (Body_Id)
4229 and then not Suppress_Style_Checks (Body_Id)
4230
4231 -- No warnings within an instance
4232
4233 and then not In_Instance
4234
4235 -- No warnings for expression functions
4236
4237 and then Nkind (Original_Node (N)) /= N_Expression_Function
4238 then
4239 Style.Body_With_No_Spec (N);
4240 end if;
4241
4242 New_Overloaded_Entity (Body_Id);
4243
4244 if Nkind (N) /= N_Subprogram_Body_Stub then
4245 Set_Acts_As_Spec (N);
4246 Generate_Definition (Body_Id);
4247 Generate_Reference
4248 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4249
4250 -- If the body is an entry wrapper created for an entry with
4251 -- preconditions, it must be compiled in the context of the
4252 -- enclosing synchronized object, because it may mention other
4253 -- operations of the type.
4254
4255 if Is_Entry_Wrapper (Body_Id) then
4256 declare
4257 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4258 begin
4259 Push_Scope (Prot);
4260 Install_Declarations (Prot);
4261 end;
4262 end if;
4263
4264 Install_Formals (Body_Id);
4265
4266 Push_Scope (Body_Id);
4267 end if;
4268
4269 -- For stubs and bodies with no previous spec, generate references to
4270 -- formals.
4271
4272 Generate_Reference_To_Formals (Body_Id);
4273 end if;
4274
4275 -- Entry barrier functions are generated outside the protected type and
4276 -- should not carry the SPARK_Mode of the enclosing context.
4277
4278 if Nkind (N) = N_Subprogram_Body
4279 and then Is_Entry_Barrier_Function (N)
4280 then
4281 null;
4282
4283 -- The body is generated as part of expression function expansion. When
4284 -- the expression function appears in the visible declarations of a
4285 -- package, the body is added to the private declarations. Since both
4286 -- declarative lists may be subject to a different SPARK_Mode, inherit
4287 -- the mode of the spec.
4288
4289 -- package P with SPARK_Mode is
4290 -- function Expr_Func ... is (...); -- original
4291 -- [function Expr_Func ...;] -- generated spec
4292 -- -- mode is ON
4293 -- private
4294 -- pragma SPARK_Mode (Off);
4295 -- [function Expr_Func ... is return ...;] -- generated body
4296 -- end P; -- mode is ON
4297
4298 elsif not Comes_From_Source (N)
4299 and then Present (Spec_Id)
4300 and then Is_Expression_Function (Spec_Id)
4301 then
4302 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4303 Set_SPARK_Pragma_Inherited
4304 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4305
4306 -- Set the SPARK_Mode from the current context (may be overwritten later
4307 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4308 -- initially on a stand-alone subprogram body, but is then relocated to
4309 -- a generated corresponding spec. In this scenario the mode is shared
4310 -- between the spec and body.
4311
4312 elsif No (SPARK_Pragma (Body_Id)) then
4313 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4314 Set_SPARK_Pragma_Inherited (Body_Id);
4315 end if;
4316
4317 -- A subprogram body may be instantiated or inlined at a later pass.
4318 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4319 -- applied to the initial declaration of the body.
4320
4321 if Present (Spec_Id) then
4322 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4323 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4324 end if;
4325
4326 else
4327 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4328 -- case the body is instantiated or inlined later and out of context.
4329 -- The body uses this attribute to restore the value of the global
4330 -- flag.
4331
4332 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4333 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4334
4335 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4336 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4337 end if;
4338 end if;
4339
4340 -- Preserve relevant elaboration-related attributes of the context which
4341 -- are no longer available or very expensive to recompute once analysis,
4342 -- resolution, and expansion are over.
4343
4344 if No (Spec_Id) then
4345 Mark_Elaboration_Attributes
4346 (N_Id => Body_Id,
4347 Checks => True,
4348 Warnings => True);
4349 end if;
4350
4351 -- If this is the proper body of a stub, we must verify that the stub
4352 -- conforms to the body, and to the previous spec if one was present.
4353 -- We know already that the body conforms to that spec. This test is
4354 -- only required for subprograms that come from source.
4355
4356 if Nkind (Parent (N)) = N_Subunit
4357 and then Comes_From_Source (N)
4358 and then not Error_Posted (Body_Id)
4359 and then Nkind (Corresponding_Stub (Parent (N))) =
4360 N_Subprogram_Body_Stub
4361 then
4362 declare
4363 Old_Id : constant Entity_Id :=
4364 Defining_Entity
4365 (Specification (Corresponding_Stub (Parent (N))));
4366
4367 Conformant : Boolean := False;
4368
4369 begin
4370 if No (Spec_Id) then
4371 Check_Fully_Conformant (Body_Id, Old_Id);
4372
4373 else
4374 Check_Conformance
4375 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4376
4377 if not Conformant then
4378
4379 -- The stub was taken to be a new declaration. Indicate that
4380 -- it lacks a body.
4381
4382 Set_Has_Completion (Old_Id, False);
4383 end if;
4384 end if;
4385 end;
4386 end if;
4387
4388 Set_Has_Completion (Body_Id);
4389 Check_Eliminated (Body_Id);
4390
4391 -- Analyze any aspect specifications that appear on the subprogram body
4392 -- stub. Stop the analysis now as the stub does not have a declarative
4393 -- or a statement part, and it cannot be inlined.
4394
4395 if Nkind (N) = N_Subprogram_Body_Stub then
4396 if Has_Aspects (N) then
4397 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4398 end if;
4399
4400 goto Leave;
4401 end if;
4402
4403 -- Handle inlining
4404
4405 -- Note: Normally we don't do any inlining if expansion is off, since
4406 -- we won't generate code in any case. An exception arises in GNATprove
4407 -- mode where we want to expand some calls in place, even with expansion
4408 -- disabled, since the inlining eases formal verification.
4409
4410 if not GNATprove_Mode
4411 and then Expander_Active
4412 and then Serious_Errors_Detected = 0
4413 and then Present (Spec_Id)
4414 and then Has_Pragma_Inline (Spec_Id)
4415 then
4416 -- Legacy implementation (relying on front-end inlining)
4417
4418 if not Back_End_Inlining then
4419 if Has_Pragma_Inline_Always (Spec_Id)
4420 or else (Front_End_Inlining
4421 and then not Opt.Disable_FE_Inline)
4422 then
4423 Build_Body_To_Inline (N, Spec_Id);
4424 end if;
4425
4426 -- New implementation (relying on back-end inlining)
4427
4428 else
4429 if Has_Pragma_Inline_Always (Spec_Id)
4430 or else Optimization_Level > 0
4431 then
4432 -- Handle function returning an unconstrained type
4433
4434 if Comes_From_Source (Body_Id)
4435 and then Ekind (Spec_Id) = E_Function
4436 and then Returns_Unconstrained_Type (Spec_Id)
4437
4438 -- If function builds in place, i.e. returns a limited type,
4439 -- inlining cannot be done.
4440
4441 and then not Is_Limited_Type (Etype (Spec_Id))
4442 then
4443 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4444
4445 else
4446 declare
4447 Subp_Body : constant Node_Id :=
4448 Unit_Declaration_Node (Body_Id);
4449 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4450
4451 begin
4452 -- Do not pass inlining to the backend if the subprogram
4453 -- has declarations or statements which cannot be inlined
4454 -- by the backend. This check is done here to emit an
4455 -- error instead of the generic warning message reported
4456 -- by the GCC backend (ie. "function might not be
4457 -- inlinable").
4458
4459 if Present (Subp_Decl)
4460 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4461 then
4462 null;
4463
4464 elsif Has_Excluded_Statement
4465 (Spec_Id,
4466 Statements
4467 (Handled_Statement_Sequence (Subp_Body)))
4468 then
4469 null;
4470
4471 -- If the backend inlining is available then at this
4472 -- stage we only have to mark the subprogram as inlined.
4473 -- The expander will take care of registering it in the
4474 -- table of subprograms inlined by the backend a part of
4475 -- processing calls to it (cf. Expand_Call)
4476
4477 else
4478 Set_Is_Inlined (Spec_Id);
4479 end if;
4480 end;
4481 end if;
4482 end if;
4483 end if;
4484
4485 -- In GNATprove mode, inline only when there is a separate subprogram
4486 -- declaration for now, as inlining of subprogram bodies acting as
4487 -- declarations, or subprogram stubs, are not supported by front-end
4488 -- inlining. This inlining should occur after analysis of the body, so
4489 -- that it is known whether the value of SPARK_Mode, which can be
4490 -- defined by a pragma inside the body, is applicable to the body.
4491 -- Inlining can be disabled with switch -gnatdm
4492
4493 elsif GNATprove_Mode
4494 and then Full_Analysis
4495 and then not Inside_A_Generic
4496 and then Present (Spec_Id)
4497 and then
4498 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4499 and then Body_Has_SPARK_Mode_On
4500 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4501 and then not Body_Has_Contract
4502 and then not Debug_Flag_M
4503 then
4504 Build_Body_To_Inline (N, Spec_Id);
4505 end if;
4506
4507 -- When generating code, inherited pre/postconditions are handled when
4508 -- expanding the corresponding contract.
4509
4510 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4511 -- of the specification we have to install the private withed units.
4512 -- This holds for child units as well.
4513
4514 if Is_Compilation_Unit (Body_Id)
4515 or else Nkind (Parent (N)) = N_Compilation_Unit
4516 then
4517 Install_Private_With_Clauses (Body_Id);
4518 end if;
4519
4520 Check_Anonymous_Return;
4521
4522 -- Set the Protected_Formal field of each extra formal of the protected
4523 -- subprogram to reference the corresponding extra formal of the
4524 -- subprogram that implements it. For regular formals this occurs when
4525 -- the protected subprogram's declaration is expanded, but the extra
4526 -- formals don't get created until the subprogram is frozen. We need to
4527 -- do this before analyzing the protected subprogram's body so that any
4528 -- references to the original subprogram's extra formals will be changed
4529 -- refer to the implementing subprogram's formals (see Expand_Formal).
4530
4531 if Present (Spec_Id)
4532 and then Is_Protected_Type (Scope (Spec_Id))
4533 and then Present (Protected_Body_Subprogram (Spec_Id))
4534 then
4535 declare
4536 Impl_Subp : constant Entity_Id :=
4537 Protected_Body_Subprogram (Spec_Id);
4538 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4539 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4540
4541 begin
4542 while Present (Prot_Ext_Formal) loop
4543 pragma Assert (Present (Impl_Ext_Formal));
4544 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4545 Next_Formal_With_Extras (Prot_Ext_Formal);
4546 Next_Formal_With_Extras (Impl_Ext_Formal);
4547 end loop;
4548 end;
4549 end if;
4550
4551 -- Generate minimum accessibility local objects to correspond with
4552 -- any extra formal added for anonymous access types. This new local
4553 -- object can then be used instead of the formal in case it is used
4554 -- in an actual to a call to a nested subprogram.
4555
4556 -- This method is used to supplement our "small integer model" for
4557 -- accessibility-check generation (for more information see
4558 -- Dynamic_Accessibility_Level).
4559
4560 -- Because we allow accessibility values greater than our expected value
4561 -- passing along the same extra accessibility formal as an actual
4562 -- to a nested subprogram becomes a problem because high values mean
4563 -- different things to the callee even though they are the same to the
4564 -- caller. So, as described in the first section, we create a local
4565 -- object representing the minimum of the accessibility level value that
4566 -- is passed in and the accessibility level of the callee's parameter
4567 -- and locals and use it in the case of a call to a nested subprogram.
4568 -- This generated object is refered to as a "minimum accessiblity
4569 -- level."
4570
4571 if Present (Spec_Id) or else Present (Body_Id) then
4572 Body_Nod := Unit_Declaration_Node (Body_Id);
4573
4574 declare
4575 Form : Entity_Id;
4576 begin
4577 -- Grab the appropriate formal depending on whether there exists
4578 -- an actual spec for the subprogram or whether we are dealing
4579 -- with a protected subprogram.
4580
4581 if Present (Spec_Id) then
4582 if Present (Protected_Body_Subprogram (Spec_Id)) then
4583 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4584 else
4585 Form := First_Formal (Spec_Id);
4586 end if;
4587 else
4588 Form := First_Formal (Body_Id);
4589 end if;
4590
4591 -- Loop through formals if the subprogram is capable of accepting
4592 -- a generated local object. If it is not then it is also not
4593 -- capable of having local subprograms meaning it would not need
4594 -- a minimum accessibility level object anyway.
4595
4596 if Present (Body_Nod)
4597 and then Has_Declarations (Body_Nod)
4598 and then Nkind (Body_Nod) /= N_Package_Specification
4599 then
4600 while Present (Form) loop
4601
4602 if Present (Extra_Accessibility (Form))
4603 and then No (Minimum_Accessibility (Form))
4604 then
4605 -- Generate the minimum accessibility level object
4606
4607 -- A60b : integer := integer'min(2, paramL);
4608
4609 declare
4610 Loc : constant Source_Ptr := Sloc (Body_Nod);
4611 Obj_Node : constant Node_Id :=
4612 Make_Object_Declaration (Loc,
4613 Defining_Identifier =>
4614 Make_Temporary
4615 (Loc, 'A', Extra_Accessibility (Form)),
4616 Object_Definition => New_Occurrence_Of
4617 (Standard_Integer, Loc),
4618 Expression =>
4619 Make_Attribute_Reference (Loc,
4620 Prefix => New_Occurrence_Of
4621 (Standard_Integer, Loc),
4622 Attribute_Name => Name_Min,
4623 Expressions => New_List (
4624 Make_Integer_Literal (Loc,
4625 Object_Access_Level (Form)),
4626 New_Occurrence_Of
4627 (Extra_Accessibility (Form), Loc))));
4628 begin
4629 -- Add the new local object to the Minimum_Acc_Obj to
4630 -- be later prepended to the subprogram's list of
4631 -- declarations after we are sure all expansion is
4632 -- done.
4633
4634 if Present (Minimum_Acc_Objs) then
4635 Prepend (Obj_Node, Minimum_Acc_Objs);
4636 else
4637 Minimum_Acc_Objs := New_List (Obj_Node);
4638 end if;
4639
4640 -- Register the object and analyze it
4641
4642 Set_Minimum_Accessibility
4643 (Form, Defining_Identifier (Obj_Node));
4644
4645 Analyze (Obj_Node);
4646 end;
4647 end if;
4648
4649 Next_Formal (Form);
4650 end loop;
4651 end if;
4652 end;
4653 end if;
4654
4655 -- Now we can go on to analyze the body
4656
4657 HSS := Handled_Statement_Sequence (N);
4658 Set_Actual_Subtypes (N, Current_Scope);
4659
4660 -- Add a declaration for the Protection object, renaming declarations
4661 -- for discriminals and privals and finally a declaration for the entry
4662 -- family index (if applicable). This form of early expansion is done
4663 -- when the Expander is active because Install_Private_Data_Declarations
4664 -- references entities which were created during regular expansion. The
4665 -- subprogram entity must come from source, and not be an internally
4666 -- generated subprogram.
4667
4668 if Expander_Active
4669 and then Present (Prot_Typ)
4670 and then Present (Spec_Id)
4671 and then Comes_From_Source (Spec_Id)
4672 and then not Is_Eliminated (Spec_Id)
4673 then
4674 Install_Private_Data_Declarations
4675 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4676 end if;
4677
4678 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4679 -- may now appear in parameter and result profiles. Since the analysis
4680 -- of a subprogram body may use the parameter and result profile of the
4681 -- spec, swap any limited views with their non-limited counterpart.
4682
4683 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4684 Exch_Views := Exchange_Limited_Views (Spec_Id);
4685 end if;
4686
4687 -- If the return type is an anonymous access type whose designated type
4688 -- is the limited view of a class-wide type and the non-limited view is
4689 -- available, update the return type accordingly.
4690
4691 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4692 declare
4693 Etyp : Entity_Id;
4694 Rtyp : Entity_Id;
4695
4696 begin
4697 Rtyp := Etype (Spec_Id);
4698
4699 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4700 Etyp := Directly_Designated_Type (Rtyp);
4701
4702 if Is_Class_Wide_Type (Etyp)
4703 and then From_Limited_With (Etyp)
4704 then
4705 Desig_View := Etyp;
4706 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4707 end if;
4708 end if;
4709 end;
4710 end if;
4711
4712 -- Analyze any aspect specifications that appear on the subprogram body
4713
4714 if Has_Aspects (N) then
4715 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4716 end if;
4717
4718 Analyze_Declarations (Declarations (N));
4719
4720 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4721
4722 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4723 if Present (SPARK_Pragma (Spec_Id)) then
4724 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4725 and then
4726 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4727 then
4728 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4729 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4730 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4731 Error_Msg_NE
4732 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4733 end if;
4734
4735 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4736 null;
4737
4738 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4739 -- as specified in source code, or because SPARK_Mode On is ignored
4740 -- in an instance where the context is SPARK_Mode Off/Auto.
4741
4742 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4743 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4744 then
4745 null;
4746
4747 else
4748 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4749 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4750 Error_Msg_Sloc := Sloc (Spec_Id);
4751 Error_Msg_NE
4752 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4753 end if;
4754 end if;
4755
4756 -- A subprogram body freezes its own contract. Analyze the contract
4757 -- after the declarations of the body have been processed as pragmas
4758 -- are now chained on the contract of the subprogram body.
4759
4760 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4761
4762 -- Check completion, and analyze the statements
4763
4764 Check_Completion;
4765 Inspect_Deferred_Constant_Completion (Declarations (N));
4766 Analyze (HSS);
4767
4768 -- Add the generated minimum accessibility objects to the subprogram
4769 -- body's list of declarations after analysis of the statements and
4770 -- contracts.
4771
4772 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4773 if Present (Declarations (Body_Nod)) then
4774 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4775 else
4776 Set_Declarations
4777 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4778 end if;
4779 end loop;
4780
4781 -- Deal with end of scope processing for the body
4782
4783 Process_End_Label (HSS, 't', Current_Scope);
4784 Update_Use_Clause_Chain;
4785 End_Scope;
4786
4787 -- If we are compiling an entry wrapper, remove the enclosing
4788 -- synchronized object from the stack.
4789
4790 if Is_Entry_Wrapper (Body_Id) then
4791 End_Scope;
4792 end if;
4793
4794 Check_Subprogram_Order (N);
4795 Set_Analyzed (Body_Id);
4796
4797 -- If we have a separate spec, then the analysis of the declarations
4798 -- caused the entities in the body to be chained to the spec id, but
4799 -- we want them chained to the body id. Only the formal parameters
4800 -- end up chained to the spec id in this case.
4801
4802 if Present (Spec_Id) then
4803
4804 -- We must conform to the categorization of our spec
4805
4806 Validate_Categorization_Dependency (N, Spec_Id);
4807
4808 -- And if this is a child unit, the parent units must conform
4809
4810 if Is_Child_Unit (Spec_Id) then
4811 Validate_Categorization_Dependency
4812 (Unit_Declaration_Node (Spec_Id), Spec_Id);
4813 end if;
4814
4815 -- Here is where we move entities from the spec to the body
4816
4817 -- Case where there are entities that stay with the spec
4818
4819 if Present (Last_Real_Spec_Entity) then
4820
4821 -- No body entities (happens when the only real spec entities come
4822 -- from precondition and postcondition pragmas).
4823
4824 if No (Last_Entity (Body_Id)) then
4825 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
4826
4827 -- Body entities present (formals), so chain stuff past them
4828
4829 else
4830 Link_Entities
4831 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
4832 end if;
4833
4834 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
4835 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4836 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
4837
4838 -- Case where there are no spec entities, in this case there can be
4839 -- no body entities either, so just move everything.
4840
4841 -- If the body is generated for an expression function, it may have
4842 -- been preanalyzed already, if 'access was applied to it.
4843
4844 else
4845 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
4846 N_Expression_Function
4847 then
4848 pragma Assert (No (Last_Entity (Body_Id)));
4849 null;
4850 end if;
4851
4852 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
4853 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
4854 Set_First_Entity (Spec_Id, Empty);
4855 Set_Last_Entity (Spec_Id, Empty);
4856 end if;
4857
4858 -- Otherwise the body does not complete a previous declaration. Check
4859 -- the categorization of the body against the units it withs.
4860
4861 else
4862 Validate_Categorization_Dependency (N, Body_Id);
4863 end if;
4864
4865 Check_Missing_Return;
4866
4867 -- Now we are going to check for variables that are never modified in
4868 -- the body of the procedure. But first we deal with a special case
4869 -- where we want to modify this check. If the body of the subprogram
4870 -- starts with a raise statement or its equivalent, or if the body
4871 -- consists entirely of a null statement, then it is pretty obvious that
4872 -- it is OK to not reference the parameters. For example, this might be
4873 -- the following common idiom for a stubbed function: statement of the
4874 -- procedure raises an exception. In particular this deals with the
4875 -- common idiom of a stubbed function, which appears something like:
4876
4877 -- function F (A : Integer) return Some_Type;
4878 -- X : Some_Type;
4879 -- begin
4880 -- raise Program_Error;
4881 -- return X;
4882 -- end F;
4883
4884 -- Here the purpose of X is simply to satisfy the annoying requirement
4885 -- in Ada that there be at least one return, and we certainly do not
4886 -- want to go posting warnings on X that it is not initialized. On
4887 -- the other hand, if X is entirely unreferenced that should still
4888 -- get a warning.
4889
4890 -- What we do is to detect these cases, and if we find them, flag the
4891 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
4892 -- suppress unwanted warnings. For the case of the function stub above
4893 -- we have a special test to set X as apparently assigned to suppress
4894 -- the warning.
4895
4896 declare
4897 Stm : Node_Id;
4898
4899 begin
4900 -- Skip call markers installed by the ABE mechanism, labels, and
4901 -- Push_xxx_Error_Label to find the first real statement.
4902
4903 Stm := First (Statements (HSS));
4904 while Nkind_In (Stm, N_Call_Marker, N_Label)
4905 or else Nkind (Stm) in N_Push_xxx_Label
4906 loop
4907 Next (Stm);
4908 end loop;
4909
4910 -- Do the test on the original statement before expansion
4911
4912 declare
4913 Ostm : constant Node_Id := Original_Node (Stm);
4914
4915 begin
4916 -- If explicit raise statement, turn on flag
4917
4918 if Nkind (Ostm) = N_Raise_Statement then
4919 Set_Trivial_Subprogram (Stm);
4920
4921 -- If null statement, and no following statements, turn on flag
4922
4923 elsif Nkind (Stm) = N_Null_Statement
4924 and then Comes_From_Source (Stm)
4925 and then No (Next (Stm))
4926 then
4927 Set_Trivial_Subprogram (Stm);
4928
4929 -- Check for explicit call cases which likely raise an exception
4930
4931 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
4932 if Is_Entity_Name (Name (Ostm)) then
4933 declare
4934 Ent : constant Entity_Id := Entity (Name (Ostm));
4935
4936 begin
4937 -- If the procedure is marked No_Return, then likely it
4938 -- raises an exception, but in any case it is not coming
4939 -- back here, so turn on the flag.
4940
4941 if Present (Ent)
4942 and then Ekind (Ent) = E_Procedure
4943 and then No_Return (Ent)
4944 then
4945 Set_Trivial_Subprogram (Stm);
4946 end if;
4947 end;
4948 end if;
4949 end if;
4950 end;
4951 end;
4952
4953 -- Check for variables that are never modified
4954
4955 declare
4956 E1 : Entity_Id;
4957 E2 : Entity_Id;
4958
4959 begin
4960 -- If there is a separate spec, then transfer Never_Set_In_Source
4961 -- flags from out parameters to the corresponding entities in the
4962 -- body. The reason we do that is we want to post error flags on
4963 -- the body entities, not the spec entities.
4964
4965 if Present (Spec_Id) then
4966 E1 := First_Entity (Spec_Id);
4967 while Present (E1) loop
4968 if Ekind (E1) = E_Out_Parameter then
4969 E2 := First_Entity (Body_Id);
4970 while Present (E2) loop
4971 exit when Chars (E1) = Chars (E2);
4972 Next_Entity (E2);
4973 end loop;
4974
4975 if Present (E2) then
4976 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
4977 end if;
4978 end if;
4979
4980 Next_Entity (E1);
4981 end loop;
4982 end if;
4983
4984 -- Check references of the subprogram spec when we are dealing with
4985 -- an expression function due to it having a generated body.
4986 -- Otherwise, we simply check the formals of the subprogram body.
4987
4988 if Present (Spec_Id)
4989 and then Is_Expression_Function (Spec_Id)
4990 then
4991 Check_References (Spec_Id);
4992 else
4993 Check_References (Body_Id);
4994 end if;
4995 end;
4996
4997 -- Check for nested subprogram, and mark outer level subprogram if so
4998
4999 declare
5000 Ent : Entity_Id;
5001
5002 begin
5003 if Present (Spec_Id) then
5004 Ent := Spec_Id;
5005 else
5006 Ent := Body_Id;
5007 end if;
5008
5009 loop
5010 Ent := Enclosing_Subprogram (Ent);
5011 exit when No (Ent) or else Is_Subprogram (Ent);
5012 end loop;
5013
5014 if Present (Ent) then
5015 Set_Has_Nested_Subprogram (Ent);
5016 end if;
5017 end;
5018
5019 -- Restore the limited views in the spec, if any, to let the back end
5020 -- process it without running into circularities.
5021
5022 if Exch_Views /= No_Elist then
5023 Restore_Limited_Views (Exch_Views);
5024 end if;
5025
5026 if Mask_Types /= No_Elist then
5027 Unmask_Unfrozen_Types (Mask_Types);
5028 end if;
5029
5030 if Present (Desig_View) then
5031 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
5032 end if;
5033
5034 <<Leave>>
5035 if Present (Ignored_Ghost_Region) then
5036 Expander_Active := Saved_EA;
5037 end if;
5038
5039 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5040 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5041 end Analyze_Subprogram_Body_Helper;
5042
5043 ------------------------------------
5044 -- Analyze_Subprogram_Declaration --
5045 ------------------------------------
5046
5047 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
5048 Scop : constant Entity_Id := Current_Scope;
5049 Designator : Entity_Id;
5050
5051 Is_Completion : Boolean;
5052 -- Indicates whether a null procedure declaration is a completion
5053
5054 begin
5055 -- Null procedures are not allowed in SPARK
5056
5057 if Nkind (Specification (N)) = N_Procedure_Specification
5058 and then Null_Present (Specification (N))
5059 then
5060 -- Null procedures are allowed in protected types, following the
5061 -- recent AI12-0147.
5062
5063 if Is_Protected_Type (Current_Scope)
5064 and then Ada_Version < Ada_2012
5065 then
5066 Error_Msg_N ("protected operation cannot be a null procedure", N);
5067 end if;
5068
5069 Analyze_Null_Procedure (N, Is_Completion);
5070
5071 -- The null procedure acts as a body, nothing further is needed
5072
5073 if Is_Completion then
5074 return;
5075 end if;
5076 end if;
5077
5078 Designator := Analyze_Subprogram_Specification (Specification (N));
5079
5080 -- A reference may already have been generated for the unit name, in
5081 -- which case the following call is redundant. However it is needed for
5082 -- declarations that are the rewriting of an expression function.
5083
5084 Generate_Definition (Designator);
5085
5086 -- Set the SPARK mode from the current context (may be overwritten later
5087 -- with explicit pragma). This is not done for entry barrier functions
5088 -- because they are generated outside the protected type and should not
5089 -- carry the mode of the enclosing context.
5090
5091 if Nkind (N) = N_Subprogram_Declaration
5092 and then Is_Entry_Barrier_Function (N)
5093 then
5094 null;
5095
5096 else
5097 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5098 Set_SPARK_Pragma_Inherited (Designator);
5099 end if;
5100
5101 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5102 -- the body of this subprogram is instantiated or inlined later and out
5103 -- of context. The body uses this attribute to restore the value of the
5104 -- global flag.
5105
5106 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5107 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5108 end if;
5109
5110 -- Preserve relevant elaboration-related attributes of the context which
5111 -- are no longer available or very expensive to recompute once analysis,
5112 -- resolution, and expansion are over.
5113
5114 Mark_Elaboration_Attributes
5115 (N_Id => Designator,
5116 Checks => True,
5117 Warnings => True);
5118
5119 if Debug_Flag_C then
5120 Write_Str ("==> subprogram spec ");
5121 Write_Name (Chars (Designator));
5122 Write_Str (" from ");
5123 Write_Location (Sloc (N));
5124 Write_Eol;
5125 Indent;
5126 end if;
5127
5128 Validate_RCI_Subprogram_Declaration (N);
5129 New_Overloaded_Entity (Designator);
5130 Check_Delayed_Subprogram (Designator);
5131
5132 -- If the type of the first formal of the current subprogram is a non-
5133 -- generic tagged private type, mark the subprogram as being a private
5134 -- primitive. Ditto if this is a function with controlling result, and
5135 -- the return type is currently private. In both cases, the type of the
5136 -- controlling argument or result must be in the current scope for the
5137 -- operation to be primitive.
5138
5139 if Has_Controlling_Result (Designator)
5140 and then Is_Private_Type (Etype (Designator))
5141 and then Scope (Etype (Designator)) = Current_Scope
5142 and then not Is_Generic_Actual_Type (Etype (Designator))
5143 then
5144 Set_Is_Private_Primitive (Designator);
5145
5146 elsif Present (First_Formal (Designator)) then
5147 declare
5148 Formal_Typ : constant Entity_Id :=
5149 Etype (First_Formal (Designator));
5150 begin
5151 Set_Is_Private_Primitive (Designator,
5152 Is_Tagged_Type (Formal_Typ)
5153 and then Scope (Formal_Typ) = Current_Scope
5154 and then Is_Private_Type (Formal_Typ)
5155 and then not Is_Generic_Actual_Type (Formal_Typ));
5156 end;
5157 end if;
5158
5159 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5160 -- or null.
5161
5162 if Ada_Version >= Ada_2005
5163 and then Comes_From_Source (N)
5164 and then Is_Dispatching_Operation (Designator)
5165 then
5166 declare
5167 E : Entity_Id;
5168 Etyp : Entity_Id;
5169
5170 begin
5171 if Has_Controlling_Result (Designator) then
5172 Etyp := Etype (Designator);
5173
5174 else
5175 E := First_Entity (Designator);
5176 while Present (E)
5177 and then Is_Formal (E)
5178 and then not Is_Controlling_Formal (E)
5179 loop
5180 Next_Entity (E);
5181 end loop;
5182
5183 Etyp := Etype (E);
5184 end if;
5185
5186 if Is_Access_Type (Etyp) then
5187 Etyp := Directly_Designated_Type (Etyp);
5188 end if;
5189
5190 if Is_Interface (Etyp)
5191 and then not Is_Abstract_Subprogram (Designator)
5192 and then not (Ekind (Designator) = E_Procedure
5193 and then Null_Present (Specification (N)))
5194 then
5195 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5196
5197 -- Specialize error message based on procedures vs. functions,
5198 -- since functions can't be null subprograms.
5199
5200 if Ekind (Designator) = E_Procedure then
5201 Error_Msg_N
5202 ("interface procedure % must be abstract or null", N);
5203 else
5204 Error_Msg_N
5205 ("interface function % must be abstract", N);
5206 end if;
5207 end if;
5208 end;
5209 end if;
5210
5211 -- What is the following code for, it used to be
5212
5213 -- ??? Set_Suppress_Elaboration_Checks
5214 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
5215
5216 -- The following seems equivalent, but a bit dubious
5217
5218 if Elaboration_Checks_Suppressed (Designator) then
5219 Set_Kill_Elaboration_Checks (Designator);
5220 end if;
5221
5222 -- For a compilation unit, set body required. This flag will only be
5223 -- reset if a valid Import or Interface pragma is processed later on.
5224
5225 if Nkind (Parent (N)) = N_Compilation_Unit then
5226 Set_Body_Required (Parent (N), True);
5227
5228 if Ada_Version >= Ada_2005
5229 and then Nkind (Specification (N)) = N_Procedure_Specification
5230 and then Null_Present (Specification (N))
5231 then
5232 Error_Msg_N
5233 ("null procedure cannot be declared at library level", N);
5234 end if;
5235 end if;
5236
5237 Generate_Reference_To_Formals (Designator);
5238 Check_Eliminated (Designator);
5239
5240 if Debug_Flag_C then
5241 Outdent;
5242 Write_Str ("<== subprogram spec ");
5243 Write_Name (Chars (Designator));
5244 Write_Str (" from ");
5245 Write_Location (Sloc (N));
5246 Write_Eol;
5247 end if;
5248
5249 -- Indicate that this is a protected operation, because it may be used
5250 -- in subsequent declarations within the protected type.
5251
5252 if Is_Protected_Type (Current_Scope) then
5253 Set_Convention (Designator, Convention_Protected);
5254 end if;
5255
5256 List_Inherited_Pre_Post_Aspects (Designator);
5257
5258 -- Process the aspects before establishing the proper categorization in
5259 -- case the subprogram is a compilation unit and one of its aspects is
5260 -- converted into a categorization pragma.
5261
5262 if Has_Aspects (N) then
5263 Analyze_Aspect_Specifications (N, Designator);
5264 end if;
5265
5266 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5267 Set_Categorization_From_Scope (Designator, Scop);
5268
5269 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5270 -- proper categorization of the unit based on its pragmas.
5271
5272 else
5273 Push_Scope (Designator);
5274 Set_Categorization_From_Pragmas (N);
5275 Validate_Categorization_Dependency (N, Designator);
5276 Pop_Scope;
5277 end if;
5278 end Analyze_Subprogram_Declaration;
5279
5280 --------------------------------------
5281 -- Analyze_Subprogram_Specification --
5282 --------------------------------------
5283
5284 -- Reminder: N here really is a subprogram specification (not a subprogram
5285 -- declaration). This procedure is called to analyze the specification in
5286 -- both subprogram bodies and subprogram declarations (specs).
5287
5288 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5289 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5290 -- Determine whether entity E denotes the spec or body of an invariant
5291 -- procedure.
5292
5293 ------------------------------------
5294 -- Is_Invariant_Procedure_Or_Body --
5295 ------------------------------------
5296
5297 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5298 Decl : constant Node_Id := Unit_Declaration_Node (E);
5299 Spec : Entity_Id;
5300
5301 begin
5302 if Nkind (Decl) = N_Subprogram_Body then
5303 Spec := Corresponding_Spec (Decl);
5304 else
5305 Spec := E;
5306 end if;
5307
5308 return
5309 Present (Spec)
5310 and then Ekind (Spec) = E_Procedure
5311 and then (Is_Partial_Invariant_Procedure (Spec)
5312 or else Is_Invariant_Procedure (Spec));
5313 end Is_Invariant_Procedure_Or_Body;
5314
5315 -- Local variables
5316
5317 Designator : constant Entity_Id := Defining_Entity (N);
5318 Formals : constant List_Id := Parameter_Specifications (N);
5319
5320 -- Start of processing for Analyze_Subprogram_Specification
5321
5322 begin
5323 -- Proceed with analysis. Do not emit a cross-reference entry if the
5324 -- specification comes from an expression function, because it may be
5325 -- the completion of a previous declaration. If it is not, the cross-
5326 -- reference entry will be emitted for the new subprogram declaration.
5327
5328 if Nkind (Parent (N)) /= N_Expression_Function then
5329 Generate_Definition (Designator);
5330 end if;
5331
5332 if Nkind (N) = N_Function_Specification then
5333 Set_Ekind (Designator, E_Function);
5334 Set_Mechanism (Designator, Default_Mechanism);
5335 else
5336 Set_Ekind (Designator, E_Procedure);
5337 Set_Etype (Designator, Standard_Void_Type);
5338 end if;
5339
5340 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5341 -- those subprograms which could be inlined in GNATprove mode (because
5342 -- Body_To_Inline is non-Empty) but should not be inlined.
5343
5344 if GNATprove_Mode then
5345 Set_Is_Inlined_Always (Designator);
5346 end if;
5347
5348 -- Introduce new scope for analysis of the formals and the return type
5349
5350 Set_Scope (Designator, Current_Scope);
5351
5352 if Present (Formals) then
5353 Push_Scope (Designator);
5354 Process_Formals (Formals, N);
5355
5356 -- Check dimensions in N for formals with default expression
5357
5358 Analyze_Dimension_Formals (N, Formals);
5359
5360 -- Ada 2005 (AI-345): If this is an overriding operation of an
5361 -- inherited interface operation, and the controlling type is
5362 -- a synchronized type, replace the type with its corresponding
5363 -- record, to match the proper signature of an overriding operation.
5364 -- Same processing for an access parameter whose designated type is
5365 -- derived from a synchronized interface.
5366
5367 -- This modification is not done for invariant procedures because
5368 -- the corresponding record may not necessarely be visible when the
5369 -- concurrent type acts as the full view of a private type.
5370
5371 -- package Pack is
5372 -- type Prot is private with Type_Invariant => ...;
5373 -- procedure ConcInvariant (Obj : Prot);
5374 -- private
5375 -- protected type Prot is ...;
5376 -- type Concurrent_Record_Prot is record ...;
5377 -- procedure ConcInvariant (Obj : Prot) is
5378 -- ...
5379 -- end ConcInvariant;
5380 -- end Pack;
5381
5382 -- In the example above, both the spec and body of the invariant
5383 -- procedure must utilize the private type as the controlling type.
5384
5385 if Ada_Version >= Ada_2005
5386 and then not Is_Invariant_Procedure_Or_Body (Designator)
5387 then
5388 declare
5389 Formal : Entity_Id;
5390 Formal_Typ : Entity_Id;
5391 Rec_Typ : Entity_Id;
5392 Desig_Typ : Entity_Id;
5393
5394 begin
5395 Formal := First_Formal (Designator);
5396 while Present (Formal) loop
5397 Formal_Typ := Etype (Formal);
5398
5399 if Is_Concurrent_Type (Formal_Typ)
5400 and then Present (Corresponding_Record_Type (Formal_Typ))
5401 then
5402 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5403
5404 if Present (Interfaces (Rec_Typ)) then
5405 Set_Etype (Formal, Rec_Typ);
5406 end if;
5407
5408 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5409 Desig_Typ := Designated_Type (Formal_Typ);
5410
5411 if Is_Concurrent_Type (Desig_Typ)
5412 and then Present (Corresponding_Record_Type (Desig_Typ))
5413 then
5414 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5415
5416 if Present (Interfaces (Rec_Typ)) then
5417 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5418 end if;
5419 end if;
5420 end if;
5421
5422 Next_Formal (Formal);
5423 end loop;
5424 end;
5425 end if;
5426
5427 End_Scope;
5428
5429 -- The subprogram scope is pushed and popped around the processing of
5430 -- the return type for consistency with call above to Process_Formals
5431 -- (which itself can call Analyze_Return_Type), and to ensure that any
5432 -- itype created for the return type will be associated with the proper
5433 -- scope.
5434
5435 elsif Nkind (N) = N_Function_Specification then
5436 Push_Scope (Designator);
5437 Analyze_Return_Type (N);
5438 End_Scope;
5439 end if;
5440
5441 -- Function case
5442
5443 if Nkind (N) = N_Function_Specification then
5444
5445 -- Deal with operator symbol case
5446
5447 if Nkind (Designator) = N_Defining_Operator_Symbol then
5448 Valid_Operator_Definition (Designator);
5449 end if;
5450
5451 May_Need_Actuals (Designator);
5452
5453 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5454 -- the subprogram is abstract also. This does not apply to renaming
5455 -- declarations, where abstractness is inherited, and to subprogram
5456 -- bodies generated for stream operations, which become renamings as
5457 -- bodies.
5458
5459 -- In case of primitives associated with abstract interface types
5460 -- the check is applied later (see Analyze_Subprogram_Declaration).
5461
5462 if not Nkind_In (Original_Node (Parent (N)),
5463 N_Abstract_Subprogram_Declaration,
5464 N_Formal_Abstract_Subprogram_Declaration,
5465 N_Subprogram_Renaming_Declaration)
5466 then
5467 if Is_Abstract_Type (Etype (Designator))
5468 and then not Is_Interface (Etype (Designator))
5469 then
5470 Error_Msg_N
5471 ("function that returns abstract type must be abstract", N);
5472
5473 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5474 -- access result whose designated type is abstract.
5475
5476 elsif Ada_Version >= Ada_2012
5477 and then Nkind (Result_Definition (N)) = N_Access_Definition
5478 and then
5479 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5480 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5481 then
5482 Error_Msg_N
5483 ("function whose access result designates abstract type "
5484 & "must be abstract", N);
5485 end if;
5486 end if;
5487 end if;
5488
5489 return Designator;
5490 end Analyze_Subprogram_Specification;
5491
5492 -----------------------
5493 -- Check_Conformance --
5494 -----------------------
5495
5496 procedure Check_Conformance
5497 (New_Id : Entity_Id;
5498 Old_Id : Entity_Id;
5499 Ctype : Conformance_Type;
5500 Errmsg : Boolean;
5501 Conforms : out Boolean;
5502 Err_Loc : Node_Id := Empty;
5503 Get_Inst : Boolean := False;
5504 Skip_Controlling_Formals : Boolean := False)
5505 is
5506 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5507 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5508 -- If Errmsg is True, then processing continues to post an error message
5509 -- for conformance error on given node. Two messages are output. The
5510 -- first message points to the previous declaration with a general "no
5511 -- conformance" message. The second is the detailed reason, supplied as
5512 -- Msg. The parameter N provide information for a possible & insertion
5513 -- in the message, and also provides the location for posting the
5514 -- message in the absence of a specified Err_Loc location.
5515
5516 function Conventions_Match
5517 (Id1 : Entity_Id;
5518 Id2 : Entity_Id) return Boolean;
5519 -- Determine whether the conventions of arbitrary entities Id1 and Id2
5520 -- match.
5521
5522 -----------------------
5523 -- Conformance_Error --
5524 -----------------------
5525
5526 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5527 Enode : Node_Id;
5528
5529 begin
5530 Conforms := False;
5531
5532 if Errmsg then
5533 if No (Err_Loc) then
5534 Enode := N;
5535 else
5536 Enode := Err_Loc;
5537 end if;
5538
5539 Error_Msg_Sloc := Sloc (Old_Id);
5540
5541 case Ctype is
5542 when Type_Conformant =>
5543 Error_Msg_N -- CODEFIX
5544 ("not type conformant with declaration#!", Enode);
5545
5546 when Mode_Conformant =>
5547 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5548 Error_Msg_N
5549 ("not mode conformant with operation inherited#!",
5550 Enode);
5551 else
5552 Error_Msg_N
5553 ("not mode conformant with declaration#!", Enode);
5554 end if;
5555
5556 when Subtype_Conformant =>
5557 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5558 Error_Msg_N
5559 ("not subtype conformant with operation inherited#!",
5560 Enode);
5561 else
5562 Error_Msg_N
5563 ("not subtype conformant with declaration#!", Enode);
5564 end if;
5565
5566 when Fully_Conformant =>
5567 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5568 Error_Msg_N -- CODEFIX
5569 ("not fully conformant with operation inherited#!",
5570 Enode);
5571 else
5572 Error_Msg_N -- CODEFIX
5573 ("not fully conformant with declaration#!", Enode);
5574 end if;
5575 end case;
5576
5577 Error_Msg_NE (Msg, Enode, N);
5578 end if;
5579 end Conformance_Error;
5580
5581 -----------------------
5582 -- Conventions_Match --
5583 -----------------------
5584
5585 function Conventions_Match
5586 (Id1 : Entity_Id;
5587 Id2 : Entity_Id) return Boolean
5588 is
5589 begin
5590 -- Ignore the conventions of anonymous access-to-subprogram types
5591 -- and subprogram types because these are internally generated and
5592 -- the only way these may receive a convention is if they inherit
5593 -- the convention of a related subprogram.
5594
5595 if Ekind_In (Id1, E_Anonymous_Access_Subprogram_Type,
5596 E_Subprogram_Type)
5597 or else
5598 Ekind_In (Id2, E_Anonymous_Access_Subprogram_Type,
5599 E_Subprogram_Type)
5600 then
5601 return True;
5602
5603 -- Otherwise compare the conventions directly
5604
5605 else
5606 return Convention (Id1) = Convention (Id2);
5607 end if;
5608 end Conventions_Match;
5609
5610 -- Local Variables
5611
5612 Old_Type : constant Entity_Id := Etype (Old_Id);
5613 New_Type : constant Entity_Id := Etype (New_Id);
5614 Old_Formal : Entity_Id;
5615 New_Formal : Entity_Id;
5616 Access_Types_Match : Boolean;
5617 Old_Formal_Base : Entity_Id;
5618 New_Formal_Base : Entity_Id;
5619
5620 -- Start of processing for Check_Conformance
5621
5622 begin
5623 Conforms := True;
5624
5625 -- We need a special case for operators, since they don't appear
5626 -- explicitly.
5627
5628 if Ctype = Type_Conformant then
5629 if Ekind (New_Id) = E_Operator
5630 and then Operator_Matches_Spec (New_Id, Old_Id)
5631 then
5632 return;
5633 end if;
5634 end if;
5635
5636 -- If both are functions/operators, check return types conform
5637
5638 if Old_Type /= Standard_Void_Type
5639 and then
5640 New_Type /= Standard_Void_Type
5641 then
5642 -- If we are checking interface conformance we omit controlling
5643 -- arguments and result, because we are only checking the conformance
5644 -- of the remaining parameters.
5645
5646 if Has_Controlling_Result (Old_Id)
5647 and then Has_Controlling_Result (New_Id)
5648 and then Skip_Controlling_Formals
5649 then
5650 null;
5651
5652 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5653 if Ctype >= Subtype_Conformant
5654 and then not Predicates_Match (Old_Type, New_Type)
5655 then
5656 Conformance_Error
5657 ("\predicate of return type does not match!", New_Id);
5658 else
5659 Conformance_Error
5660 ("\return type does not match!", New_Id);
5661 end if;
5662
5663 return;
5664 end if;
5665
5666 -- Ada 2005 (AI-231): In case of anonymous access types check the
5667 -- null-exclusion and access-to-constant attributes match.
5668
5669 if Ada_Version >= Ada_2005
5670 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5671 and then
5672 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5673 or else Is_Access_Constant (Etype (Old_Type)) /=
5674 Is_Access_Constant (Etype (New_Type)))
5675 then
5676 Conformance_Error ("\return type does not match!", New_Id);
5677 return;
5678 end if;
5679
5680 -- If either is a function/operator and the other isn't, error
5681
5682 elsif Old_Type /= Standard_Void_Type
5683 or else New_Type /= Standard_Void_Type
5684 then
5685 Conformance_Error ("\functions can only match functions!", New_Id);
5686 return;
5687 end if;
5688
5689 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5690 -- If this is a renaming as body, refine error message to indicate that
5691 -- the conflict is with the original declaration. If the entity is not
5692 -- frozen, the conventions don't have to match, the one of the renamed
5693 -- entity is inherited.
5694
5695 if Ctype >= Subtype_Conformant then
5696 if not Conventions_Match (Old_Id, New_Id) then
5697 if not Is_Frozen (New_Id) then
5698 null;
5699
5700 elsif Present (Err_Loc)
5701 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5702 and then Present (Corresponding_Spec (Err_Loc))
5703 then
5704 Error_Msg_Name_1 := Chars (New_Id);
5705 Error_Msg_Name_2 :=
5706 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5707 Conformance_Error ("\prior declaration for% has convention %!");
5708
5709 else
5710 Conformance_Error ("\calling conventions do not match!");
5711 end if;
5712
5713 return;
5714
5715 elsif Is_Formal_Subprogram (Old_Id)
5716 or else Is_Formal_Subprogram (New_Id)
5717 or else (Is_Subprogram (New_Id)
5718 and then Present (Alias (New_Id))
5719 and then Is_Formal_Subprogram (Alias (New_Id)))
5720 then
5721 Conformance_Error
5722 ("\formal subprograms are not subtype conformant "
5723 & "(RM 6.3.1 (17/3))");
5724 end if;
5725 end if;
5726
5727 -- Deal with parameters
5728
5729 -- Note: we use the entity information, rather than going directly
5730 -- to the specification in the tree. This is not only simpler, but
5731 -- absolutely necessary for some cases of conformance tests between
5732 -- operators, where the declaration tree simply does not exist.
5733
5734 Old_Formal := First_Formal (Old_Id);
5735 New_Formal := First_Formal (New_Id);
5736 while Present (Old_Formal) and then Present (New_Formal) loop
5737 if Is_Controlling_Formal (Old_Formal)
5738 and then Is_Controlling_Formal (New_Formal)
5739 and then Skip_Controlling_Formals
5740 then
5741 -- The controlling formals will have different types when
5742 -- comparing an interface operation with its match, but both
5743 -- or neither must be access parameters.
5744
5745 if Is_Access_Type (Etype (Old_Formal))
5746 =
5747 Is_Access_Type (Etype (New_Formal))
5748 then
5749 goto Skip_Controlling_Formal;
5750 else
5751 Conformance_Error
5752 ("\access parameter does not match!", New_Formal);
5753 end if;
5754 end if;
5755
5756 -- Ada 2012: Mode conformance also requires that formal parameters
5757 -- be both aliased, or neither.
5758
5759 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5760 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5761 Conformance_Error
5762 ("\aliased parameter mismatch!", New_Formal);
5763 end if;
5764 end if;
5765
5766 if Ctype = Fully_Conformant then
5767
5768 -- Names must match. Error message is more accurate if we do
5769 -- this before checking that the types of the formals match.
5770
5771 if Chars (Old_Formal) /= Chars (New_Formal) then
5772 Conformance_Error ("\name& does not match!", New_Formal);
5773
5774 -- Set error posted flag on new formal as well to stop
5775 -- junk cascaded messages in some cases.
5776
5777 Set_Error_Posted (New_Formal);
5778 return;
5779 end if;
5780
5781 -- Null exclusion must match
5782
5783 if Null_Exclusion_Present (Parent (Old_Formal))
5784 /=
5785 Null_Exclusion_Present (Parent (New_Formal))
5786 then
5787 -- Only give error if both come from source. This should be
5788 -- investigated some time, since it should not be needed ???
5789
5790 if Comes_From_Source (Old_Formal)
5791 and then
5792 Comes_From_Source (New_Formal)
5793 then
5794 Conformance_Error
5795 ("\null exclusion for& does not match", New_Formal);
5796
5797 -- Mark error posted on the new formal to avoid duplicated
5798 -- complaint about types not matching.
5799
5800 Set_Error_Posted (New_Formal);
5801 end if;
5802 end if;
5803 end if;
5804
5805 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5806 -- case occurs whenever a subprogram is being renamed and one of its
5807 -- parameters imposes a null exclusion. For example:
5808
5809 -- type T is null record;
5810 -- type Acc_T is access T;
5811 -- subtype Acc_T_Sub is Acc_T;
5812
5813 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5814 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5815 -- renames P;
5816
5817 Old_Formal_Base := Etype (Old_Formal);
5818 New_Formal_Base := Etype (New_Formal);
5819
5820 if Get_Inst then
5821 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5822 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5823 end if;
5824
5825 Access_Types_Match := Ada_Version >= Ada_2005
5826
5827 -- Ensure that this rule is only applied when New_Id is a
5828 -- renaming of Old_Id.
5829
5830 and then Nkind (Parent (Parent (New_Id))) =
5831 N_Subprogram_Renaming_Declaration
5832 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5833 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5834 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5835
5836 -- Now handle the allowed access-type case
5837
5838 and then Is_Access_Type (Old_Formal_Base)
5839 and then Is_Access_Type (New_Formal_Base)
5840
5841 -- The type kinds must match. The only exception occurs with
5842 -- multiple generics of the form:
5843
5844 -- generic generic
5845 -- type F is private; type A is private;
5846 -- type F_Ptr is access F; type A_Ptr is access A;
5847 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5848 -- package F_Pack is ... package A_Pack is
5849 -- package F_Inst is
5850 -- new F_Pack (A, A_Ptr, A_P);
5851
5852 -- When checking for conformance between the parameters of A_P
5853 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5854 -- because the compiler has transformed A_Ptr into a subtype of
5855 -- F_Ptr. We catch this case in the code below.
5856
5857 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5858 or else
5859 (Is_Generic_Type (Old_Formal_Base)
5860 and then Is_Generic_Type (New_Formal_Base)
5861 and then Is_Internal (New_Formal_Base)
5862 and then Etype (Etype (New_Formal_Base)) =
5863 Old_Formal_Base))
5864 and then Directly_Designated_Type (Old_Formal_Base) =
5865 Directly_Designated_Type (New_Formal_Base)
5866 and then ((Is_Itype (Old_Formal_Base)
5867 and then (Can_Never_Be_Null (Old_Formal_Base)
5868 or else Is_Access_Constant
5869 (Old_Formal_Base)))
5870 or else
5871 (Is_Itype (New_Formal_Base)
5872 and then (Can_Never_Be_Null (New_Formal_Base)
5873 or else Is_Access_Constant
5874 (New_Formal_Base))));
5875
5876 -- Types must always match. In the visible part of an instance,
5877 -- usual overloading rules for dispatching operations apply, and
5878 -- we check base types (not the actual subtypes).
5879
5880 if In_Instance_Visible_Part
5881 and then Is_Dispatching_Operation (New_Id)
5882 then
5883 if not Conforming_Types
5884 (T1 => Base_Type (Etype (Old_Formal)),
5885 T2 => Base_Type (Etype (New_Formal)),
5886 Ctype => Ctype,
5887 Get_Inst => Get_Inst)
5888 and then not Access_Types_Match
5889 then
5890 Conformance_Error ("\type of & does not match!", New_Formal);
5891 return;
5892 end if;
5893
5894 elsif not Conforming_Types
5895 (T1 => Old_Formal_Base,
5896 T2 => New_Formal_Base,
5897 Ctype => Ctype,
5898 Get_Inst => Get_Inst)
5899 and then not Access_Types_Match
5900 then
5901 -- Don't give error message if old type is Any_Type. This test
5902 -- avoids some cascaded errors, e.g. in case of a bad spec.
5903
5904 if Errmsg and then Old_Formal_Base = Any_Type then
5905 Conforms := False;
5906 else
5907 if Ctype >= Subtype_Conformant
5908 and then
5909 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
5910 then
5911 Conformance_Error
5912 ("\predicate of & does not match!", New_Formal);
5913 else
5914 Conformance_Error
5915 ("\type of & does not match!", New_Formal);
5916
5917 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
5918 then
5919 Error_Msg_N ("\dimensions mismatch!", New_Formal);
5920 end if;
5921 end if;
5922 end if;
5923
5924 return;
5925 end if;
5926
5927 -- For mode conformance, mode must match
5928
5929 if Ctype >= Mode_Conformant then
5930 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
5931 if not Ekind_In (New_Id, E_Function, E_Procedure)
5932 or else not Is_Primitive_Wrapper (New_Id)
5933 then
5934 Conformance_Error ("\mode of & does not match!", New_Formal);
5935
5936 else
5937 declare
5938 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
5939 begin
5940 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
5941 then
5942 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
5943 else
5944 Conformance_Error
5945 ("\mode of & does not match!", New_Formal);
5946 end if;
5947 end;
5948 end if;
5949
5950 return;
5951
5952 -- Part of mode conformance for access types is having the same
5953 -- constant modifier.
5954
5955 elsif Access_Types_Match
5956 and then Is_Access_Constant (Old_Formal_Base) /=
5957 Is_Access_Constant (New_Formal_Base)
5958 then
5959 Conformance_Error
5960 ("\constant modifier does not match!", New_Formal);
5961 return;
5962 end if;
5963 end if;
5964
5965 if Ctype >= Subtype_Conformant then
5966
5967 -- Ada 2005 (AI-231): In case of anonymous access types check
5968 -- the null-exclusion and access-to-constant attributes must
5969 -- match. For null exclusion, we test the types rather than the
5970 -- formals themselves, since the attribute is only set reliably
5971 -- on the formals in the Ada 95 case, and we exclude the case
5972 -- where Old_Formal is marked as controlling, to avoid errors
5973 -- when matching completing bodies with dispatching declarations
5974 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
5975
5976 if Ada_Version >= Ada_2005
5977 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5978 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5979 and then
5980 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5981 Can_Never_Be_Null (Etype (New_Formal))
5982 and then
5983 not Is_Controlling_Formal (Old_Formal))
5984 or else
5985 Is_Access_Constant (Etype (Old_Formal)) /=
5986 Is_Access_Constant (Etype (New_Formal)))
5987
5988 -- Do not complain if error already posted on New_Formal. This
5989 -- avoids some redundant error messages.
5990
5991 and then not Error_Posted (New_Formal)
5992 then
5993 -- It is allowed to omit the null-exclusion in case of stream
5994 -- attribute subprograms. We recognize stream subprograms
5995 -- through their TSS-generated suffix.
5996
5997 declare
5998 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5999
6000 begin
6001 if TSS_Name /= TSS_Stream_Read
6002 and then TSS_Name /= TSS_Stream_Write
6003 and then TSS_Name /= TSS_Stream_Input
6004 and then TSS_Name /= TSS_Stream_Output
6005 then
6006 -- Here we have a definite conformance error. It is worth
6007 -- special casing the error message for the case of a
6008 -- controlling formal (which excludes null).
6009
6010 if Is_Controlling_Formal (New_Formal) then
6011 Error_Msg_Node_2 := Scope (New_Formal);
6012 Conformance_Error
6013 ("\controlling formal & of & excludes null, "
6014 & "declaration must exclude null as well",
6015 New_Formal);
6016
6017 -- Normal case (couldn't we give more detail here???)
6018
6019 else
6020 Conformance_Error
6021 ("\type of & does not match!", New_Formal);
6022 end if;
6023
6024 return;
6025 end if;
6026 end;
6027 end if;
6028 end if;
6029
6030 -- Full conformance checks
6031
6032 if Ctype = Fully_Conformant then
6033
6034 -- We have checked already that names match
6035
6036 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6037
6038 -- Check default expressions for in parameters
6039
6040 declare
6041 NewD : constant Boolean :=
6042 Present (Default_Value (New_Formal));
6043 OldD : constant Boolean :=
6044 Present (Default_Value (Old_Formal));
6045 begin
6046 if NewD or OldD then
6047
6048 -- The old default value has been analyzed because the
6049 -- current full declaration will have frozen everything
6050 -- before. The new default value has not been analyzed,
6051 -- so analyze it now before we check for conformance.
6052
6053 if NewD then
6054 Push_Scope (New_Id);
6055 Preanalyze_Spec_Expression
6056 (Default_Value (New_Formal), Etype (New_Formal));
6057 End_Scope;
6058 end if;
6059
6060 if not (NewD and OldD)
6061 or else not Fully_Conformant_Expressions
6062 (Default_Value (Old_Formal),
6063 Default_Value (New_Formal))
6064 then
6065 Conformance_Error
6066 ("\default expression for & does not match!",
6067 New_Formal);
6068 return;
6069 end if;
6070 end if;
6071 end;
6072 end if;
6073 end if;
6074
6075 -- A couple of special checks for Ada 83 mode. These checks are
6076 -- skipped if either entity is an operator in package Standard,
6077 -- or if either old or new instance is not from the source program.
6078
6079 if Ada_Version = Ada_83
6080 and then Sloc (Old_Id) > Standard_Location
6081 and then Sloc (New_Id) > Standard_Location
6082 and then Comes_From_Source (Old_Id)
6083 and then Comes_From_Source (New_Id)
6084 then
6085 declare
6086 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6087 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6088
6089 begin
6090 -- Explicit IN must be present or absent in both cases. This
6091 -- test is required only in the full conformance case.
6092
6093 if In_Present (Old_Param) /= In_Present (New_Param)
6094 and then Ctype = Fully_Conformant
6095 then
6096 Conformance_Error
6097 ("\(Ada 83) IN must appear in both declarations",
6098 New_Formal);
6099 return;
6100 end if;
6101
6102 -- Grouping (use of comma in param lists) must be the same
6103 -- This is where we catch a misconformance like:
6104
6105 -- A, B : Integer
6106 -- A : Integer; B : Integer
6107
6108 -- which are represented identically in the tree except
6109 -- for the setting of the flags More_Ids and Prev_Ids.
6110
6111 if More_Ids (Old_Param) /= More_Ids (New_Param)
6112 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6113 then
6114 Conformance_Error
6115 ("\grouping of & does not match!", New_Formal);
6116 return;
6117 end if;
6118 end;
6119 end if;
6120
6121 -- This label is required when skipping controlling formals
6122
6123 <<Skip_Controlling_Formal>>
6124
6125 Next_Formal (Old_Formal);
6126 Next_Formal (New_Formal);
6127 end loop;
6128
6129 if Present (Old_Formal) then
6130 Conformance_Error ("\too few parameters!");
6131 return;
6132
6133 elsif Present (New_Formal) then
6134 Conformance_Error ("\too many parameters!", New_Formal);
6135 return;
6136 end if;
6137 end Check_Conformance;
6138
6139 -----------------------
6140 -- Check_Conventions --
6141 -----------------------
6142
6143 procedure Check_Conventions (Typ : Entity_Id) is
6144 Ifaces_List : Elist_Id;
6145
6146 procedure Check_Convention (Op : Entity_Id);
6147 -- Verify that the convention of inherited dispatching operation Op is
6148 -- consistent among all subprograms it overrides. In order to minimize
6149 -- the search, Search_From is utilized to designate a specific point in
6150 -- the list rather than iterating over the whole list once more.
6151
6152 ----------------------
6153 -- Check_Convention --
6154 ----------------------
6155
6156 procedure Check_Convention (Op : Entity_Id) is
6157 Op_Conv : constant Convention_Id := Convention (Op);
6158 Iface_Conv : Convention_Id;
6159 Iface_Elmt : Elmt_Id;
6160 Iface_Prim_Elmt : Elmt_Id;
6161 Iface_Prim : Entity_Id;
6162
6163 begin
6164 Iface_Elmt := First_Elmt (Ifaces_List);
6165 while Present (Iface_Elmt) loop
6166 Iface_Prim_Elmt :=
6167 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6168 while Present (Iface_Prim_Elmt) loop
6169 Iface_Prim := Node (Iface_Prim_Elmt);
6170 Iface_Conv := Convention (Iface_Prim);
6171
6172 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6173 and then Iface_Conv /= Op_Conv
6174 then
6175 Error_Msg_N
6176 ("inconsistent conventions in primitive operations", Typ);
6177
6178 Error_Msg_Name_1 := Chars (Op);
6179 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6180 Error_Msg_Sloc := Sloc (Op);
6181
6182 if Comes_From_Source (Op) or else No (Alias (Op)) then
6183 if not Present (Overridden_Operation (Op)) then
6184 Error_Msg_N ("\\primitive % defined #", Typ);
6185 else
6186 Error_Msg_N
6187 ("\\overriding operation % with "
6188 & "convention % defined #", Typ);
6189 end if;
6190
6191 else pragma Assert (Present (Alias (Op)));
6192 Error_Msg_Sloc := Sloc (Alias (Op));
6193 Error_Msg_N ("\\inherited operation % with "
6194 & "convention % defined #", Typ);
6195 end if;
6196
6197 Error_Msg_Name_1 := Chars (Op);
6198 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6199 Error_Msg_Sloc := Sloc (Iface_Prim);
6200 Error_Msg_N ("\\overridden operation % with "
6201 & "convention % defined #", Typ);
6202
6203 -- Avoid cascading errors
6204
6205 return;
6206 end if;
6207
6208 Next_Elmt (Iface_Prim_Elmt);
6209 end loop;
6210
6211 Next_Elmt (Iface_Elmt);
6212 end loop;
6213 end Check_Convention;
6214
6215 -- Local variables
6216
6217 Prim_Op : Entity_Id;
6218 Prim_Op_Elmt : Elmt_Id;
6219
6220 -- Start of processing for Check_Conventions
6221
6222 begin
6223 if not Has_Interfaces (Typ) then
6224 return;
6225 end if;
6226
6227 Collect_Interfaces (Typ, Ifaces_List);
6228
6229 -- The algorithm checks every overriding dispatching operation against
6230 -- all the corresponding overridden dispatching operations, detecting
6231 -- differences in conventions.
6232
6233 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6234 while Present (Prim_Op_Elmt) loop
6235 Prim_Op := Node (Prim_Op_Elmt);
6236
6237 -- A small optimization: skip the predefined dispatching operations
6238 -- since they always have the same convention.
6239
6240 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6241 Check_Convention (Prim_Op);
6242 end if;
6243
6244 Next_Elmt (Prim_Op_Elmt);
6245 end loop;
6246 end Check_Conventions;
6247
6248 ------------------------------
6249 -- Check_Delayed_Subprogram --
6250 ------------------------------
6251
6252 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6253 procedure Possible_Freeze (T : Entity_Id);
6254 -- T is the type of either a formal parameter or of the return type. If
6255 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6256 -- itself must be delayed.
6257
6258 ---------------------
6259 -- Possible_Freeze --
6260 ---------------------
6261
6262 procedure Possible_Freeze (T : Entity_Id) is
6263 Scop : constant Entity_Id := Scope (Designator);
6264
6265 begin
6266 -- If the subprogram appears within a package instance (which may be
6267 -- the wrapper package of a subprogram instance) the freeze node for
6268 -- that package will freeze the subprogram at the proper place, so
6269 -- do not emit a freeze node for the subprogram, given that it may
6270 -- appear in the wrong scope.
6271
6272 if Ekind (Scop) = E_Package
6273 and then not Comes_From_Source (Scop)
6274 and then Is_Generic_Instance (Scop)
6275 then
6276 null;
6277
6278 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6279 Set_Has_Delayed_Freeze (Designator);
6280
6281 elsif Is_Access_Type (T)
6282 and then Has_Delayed_Freeze (Designated_Type (T))
6283 and then not Is_Frozen (Designated_Type (T))
6284 then
6285 Set_Has_Delayed_Freeze (Designator);
6286 end if;
6287 end Possible_Freeze;
6288
6289 -- Local variables
6290
6291 F : Entity_Id;
6292
6293 -- Start of processing for Check_Delayed_Subprogram
6294
6295 begin
6296 -- All subprograms, including abstract subprograms, may need a freeze
6297 -- node if some formal type or the return type needs one.
6298
6299 Possible_Freeze (Etype (Designator));
6300 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6301
6302 -- Need delayed freeze if any of the formal types themselves need a
6303 -- delayed freeze and are not yet frozen.
6304
6305 F := First_Formal (Designator);
6306 while Present (F) loop
6307 Possible_Freeze (Etype (F));
6308 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6309 Next_Formal (F);
6310 end loop;
6311
6312 -- Mark functions that return by reference. Note that it cannot be done
6313 -- for delayed_freeze subprograms because the underlying returned type
6314 -- may not be known yet (for private types).
6315
6316 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6317 declare
6318 Typ : constant Entity_Id := Etype (Designator);
6319 Utyp : constant Entity_Id := Underlying_Type (Typ);
6320
6321 begin
6322 if Is_Limited_View (Typ) then
6323 Set_Returns_By_Ref (Designator);
6324
6325 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6326 Set_Returns_By_Ref (Designator);
6327 end if;
6328 end;
6329 end if;
6330 end Check_Delayed_Subprogram;
6331
6332 ------------------------------------
6333 -- Check_Discriminant_Conformance --
6334 ------------------------------------
6335
6336 procedure Check_Discriminant_Conformance
6337 (N : Node_Id;
6338 Prev : Entity_Id;
6339 Prev_Loc : Node_Id)
6340 is
6341 Old_Discr : Entity_Id := First_Discriminant (Prev);
6342 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6343 New_Discr_Id : Entity_Id;
6344 New_Discr_Type : Entity_Id;
6345
6346 procedure Conformance_Error (Msg : String; N : Node_Id);
6347 -- Post error message for conformance error on given node. Two messages
6348 -- are output. The first points to the previous declaration with a
6349 -- general "no conformance" message. The second is the detailed reason,
6350 -- supplied as Msg. The parameter N provide information for a possible
6351 -- & insertion in the message.
6352
6353 -----------------------
6354 -- Conformance_Error --
6355 -----------------------
6356
6357 procedure Conformance_Error (Msg : String; N : Node_Id) is
6358 begin
6359 Error_Msg_Sloc := Sloc (Prev_Loc);
6360 Error_Msg_N -- CODEFIX
6361 ("not fully conformant with declaration#!", N);
6362 Error_Msg_NE (Msg, N, N);
6363 end Conformance_Error;
6364
6365 -- Start of processing for Check_Discriminant_Conformance
6366
6367 begin
6368 while Present (Old_Discr) and then Present (New_Discr) loop
6369 New_Discr_Id := Defining_Identifier (New_Discr);
6370
6371 -- The subtype mark of the discriminant on the full type has not
6372 -- been analyzed so we do it here. For an access discriminant a new
6373 -- type is created.
6374
6375 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6376 New_Discr_Type :=
6377 Access_Definition (N, Discriminant_Type (New_Discr));
6378
6379 else
6380 Find_Type (Discriminant_Type (New_Discr));
6381 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6382
6383 -- Ada 2005: if the discriminant definition carries a null
6384 -- exclusion, create an itype to check properly for consistency
6385 -- with partial declaration.
6386
6387 if Is_Access_Type (New_Discr_Type)
6388 and then Null_Exclusion_Present (New_Discr)
6389 then
6390 New_Discr_Type :=
6391 Create_Null_Excluding_Itype
6392 (T => New_Discr_Type,
6393 Related_Nod => New_Discr,
6394 Scope_Id => Current_Scope);
6395 end if;
6396 end if;
6397
6398 if not Conforming_Types
6399 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6400 then
6401 Conformance_Error ("type of & does not match!", New_Discr_Id);
6402 return;
6403 else
6404 -- Treat the new discriminant as an occurrence of the old one,
6405 -- for navigation purposes, and fill in some semantic
6406 -- information, for completeness.
6407
6408 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6409 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6410 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6411 end if;
6412
6413 -- Names must match
6414
6415 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6416 Conformance_Error ("name & does not match!", New_Discr_Id);
6417 return;
6418 end if;
6419
6420 -- Default expressions must match
6421
6422 declare
6423 NewD : constant Boolean :=
6424 Present (Expression (New_Discr));
6425 OldD : constant Boolean :=
6426 Present (Expression (Parent (Old_Discr)));
6427
6428 begin
6429 if NewD or OldD then
6430
6431 -- The old default value has been analyzed and expanded,
6432 -- because the current full declaration will have frozen
6433 -- everything before. The new default values have not been
6434 -- expanded, so expand now to check conformance.
6435
6436 if NewD then
6437 Preanalyze_Spec_Expression
6438 (Expression (New_Discr), New_Discr_Type);
6439 end if;
6440
6441 if not (NewD and OldD)
6442 or else not Fully_Conformant_Expressions
6443 (Expression (Parent (Old_Discr)),
6444 Expression (New_Discr))
6445
6446 then
6447 Conformance_Error
6448 ("default expression for & does not match!",
6449 New_Discr_Id);
6450 return;
6451 end if;
6452 end if;
6453 end;
6454
6455 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6456
6457 if Ada_Version = Ada_83 then
6458 declare
6459 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6460
6461 begin
6462 -- Grouping (use of comma in param lists) must be the same
6463 -- This is where we catch a misconformance like:
6464
6465 -- A, B : Integer
6466 -- A : Integer; B : Integer
6467
6468 -- which are represented identically in the tree except
6469 -- for the setting of the flags More_Ids and Prev_Ids.
6470
6471 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6472 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6473 then
6474 Conformance_Error
6475 ("grouping of & does not match!", New_Discr_Id);
6476 return;
6477 end if;
6478 end;
6479 end if;
6480
6481 Next_Discriminant (Old_Discr);
6482 Next (New_Discr);
6483 end loop;
6484
6485 if Present (Old_Discr) then
6486 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6487 return;
6488
6489 elsif Present (New_Discr) then
6490 Conformance_Error
6491 ("too many discriminants!", Defining_Identifier (New_Discr));
6492 return;
6493 end if;
6494 end Check_Discriminant_Conformance;
6495
6496 ----------------------------
6497 -- Check_Fully_Conformant --
6498 ----------------------------
6499
6500 procedure Check_Fully_Conformant
6501 (New_Id : Entity_Id;
6502 Old_Id : Entity_Id;
6503 Err_Loc : Node_Id := Empty)
6504 is
6505 Result : Boolean;
6506 pragma Warnings (Off, Result);
6507 begin
6508 Check_Conformance
6509 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6510 end Check_Fully_Conformant;
6511
6512 --------------------------
6513 -- Check_Limited_Return --
6514 --------------------------
6515
6516 procedure Check_Limited_Return
6517 (N : Node_Id;
6518 Expr : Node_Id;
6519 R_Type : Entity_Id)
6520 is
6521 begin
6522 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6523 -- replaced by anonymous access results. This is an incompatibility with
6524 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6525 -- controllable with special switch. ???
6526
6527 -- A limited interface that is not immutably limited is OK
6528
6529 if Is_Limited_Interface (R_Type)
6530 and then
6531 not (Is_Task_Interface (R_Type)
6532 or else Is_Protected_Interface (R_Type)
6533 or else Is_Synchronized_Interface (R_Type))
6534 then
6535 null;
6536
6537 elsif Is_Limited_Type (R_Type)
6538 and then not Is_Interface (R_Type)
6539 and then Comes_From_Source (N)
6540 and then not In_Instance_Body
6541 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6542 then
6543 -- Error in Ada 2005
6544
6545 if Ada_Version >= Ada_2005
6546 and then not Debug_Flag_Dot_L
6547 and then not GNAT_Mode
6548 then
6549 Error_Msg_N
6550 ("(Ada 2005) cannot copy object of a limited type "
6551 & "(RM-2005 6.5(5.5/2))", Expr);
6552
6553 if Is_Limited_View (R_Type) then
6554 Error_Msg_N
6555 ("\return by reference not permitted in Ada 2005", Expr);
6556 end if;
6557
6558 -- Warn in Ada 95 mode, to give folks a heads up about this
6559 -- incompatibility.
6560
6561 -- In GNAT mode, this is just a warning, to allow it to be evilly
6562 -- turned off. Otherwise it is a real error.
6563
6564 -- In a generic context, simplify the warning because it makes no
6565 -- sense to discuss pass-by-reference or copy.
6566
6567 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6568 if Inside_A_Generic then
6569 Error_Msg_N
6570 ("return of limited object not permitted in Ada 2005 "
6571 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6572
6573 elsif Is_Limited_View (R_Type) then
6574 Error_Msg_N
6575 ("return by reference not permitted in Ada 2005 "
6576 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6577 else
6578 Error_Msg_N
6579 ("cannot copy object of a limited type in Ada 2005 "
6580 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6581 end if;
6582
6583 -- Ada 95 mode, and compatibility warnings disabled
6584
6585 else
6586 pragma Assert (Ada_Version <= Ada_95);
6587 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6588 return; -- skip continuation messages below
6589 end if;
6590
6591 if not Inside_A_Generic then
6592 Error_Msg_N
6593 ("\consider switching to return of access type", Expr);
6594 Explain_Limited_Type (R_Type, Expr);
6595 end if;
6596 end if;
6597 end Check_Limited_Return;
6598
6599 ---------------------------
6600 -- Check_Mode_Conformant --
6601 ---------------------------
6602
6603 procedure Check_Mode_Conformant
6604 (New_Id : Entity_Id;
6605 Old_Id : Entity_Id;
6606 Err_Loc : Node_Id := Empty;
6607 Get_Inst : Boolean := False)
6608 is
6609 Result : Boolean;
6610 pragma Warnings (Off, Result);
6611 begin
6612 Check_Conformance
6613 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6614 end Check_Mode_Conformant;
6615
6616 --------------------------------
6617 -- Check_Overriding_Indicator --
6618 --------------------------------
6619
6620 procedure Check_Overriding_Indicator
6621 (Subp : Entity_Id;
6622 Overridden_Subp : Entity_Id;
6623 Is_Primitive : Boolean)
6624 is
6625 Decl : Node_Id;
6626 Spec : Node_Id;
6627
6628 begin
6629 -- No overriding indicator for literals
6630
6631 if Ekind (Subp) = E_Enumeration_Literal then
6632 return;
6633
6634 elsif Ekind (Subp) = E_Entry then
6635 Decl := Parent (Subp);
6636
6637 -- No point in analyzing a malformed operator
6638
6639 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6640 and then Error_Posted (Subp)
6641 then
6642 return;
6643
6644 else
6645 Decl := Unit_Declaration_Node (Subp);
6646 end if;
6647
6648 if Nkind_In (Decl, N_Subprogram_Body,
6649 N_Subprogram_Body_Stub,
6650 N_Subprogram_Declaration,
6651 N_Abstract_Subprogram_Declaration,
6652 N_Subprogram_Renaming_Declaration)
6653 then
6654 Spec := Specification (Decl);
6655
6656 elsif Nkind (Decl) = N_Entry_Declaration then
6657 Spec := Decl;
6658
6659 else
6660 return;
6661 end if;
6662
6663 -- An overriding indication is illegal on a subprogram declared
6664 -- in a protected body, where there is no operation to override.
6665
6666 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6667 and then Is_List_Member (Decl)
6668 and then Present (Parent (List_Containing (Decl)))
6669 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6670 then
6671 Error_Msg_N
6672 ("illegal overriding indication in protected body", Decl);
6673 return;
6674 end if;
6675
6676 -- The overriding operation is type conformant with the overridden one,
6677 -- but the names of the formals are not required to match. If the names
6678 -- appear permuted in the overriding operation, this is a possible
6679 -- source of confusion that is worth diagnosing. Controlling formals
6680 -- often carry names that reflect the type, and it is not worthwhile
6681 -- requiring that their names match.
6682
6683 if Present (Overridden_Subp)
6684 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6685 then
6686 declare
6687 Form1 : Entity_Id;
6688 Form2 : Entity_Id;
6689
6690 begin
6691 Form1 := First_Formal (Subp);
6692 Form2 := First_Formal (Overridden_Subp);
6693
6694 -- If the overriding operation is a synchronized operation, skip
6695 -- the first parameter of the overridden operation, which is
6696 -- implicit in the new one. If the operation is declared in the
6697 -- body it is not primitive and all formals must match.
6698
6699 if Is_Concurrent_Type (Scope (Subp))
6700 and then Is_Tagged_Type (Scope (Subp))
6701 and then not Has_Completion (Scope (Subp))
6702 then
6703 Form2 := Next_Formal (Form2);
6704 end if;
6705
6706 if Present (Form1) then
6707 Form1 := Next_Formal (Form1);
6708 Form2 := Next_Formal (Form2);
6709 end if;
6710
6711 while Present (Form1) loop
6712 if not Is_Controlling_Formal (Form1)
6713 and then Present (Next_Formal (Form2))
6714 and then Chars (Form1) = Chars (Next_Formal (Form2))
6715 then
6716 Error_Msg_Node_2 := Alias (Overridden_Subp);
6717 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6718 Error_Msg_NE
6719 ("& does not match corresponding formal of&#",
6720 Form1, Form1);
6721 exit;
6722 end if;
6723
6724 Next_Formal (Form1);
6725 Next_Formal (Form2);
6726 end loop;
6727 end;
6728 end if;
6729
6730 -- If there is an overridden subprogram, then check that there is no
6731 -- "not overriding" indicator, and mark the subprogram as overriding.
6732
6733 -- This is not done if the overridden subprogram is marked as hidden,
6734 -- which can occur for the case of inherited controlled operations
6735 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6736 -- subprogram is not itself hidden or we are within a generic instance,
6737 -- in which case the hidden flag may have been modified for the
6738 -- expansion of the instance.
6739
6740 -- (Note: This condition could probably be simplified, leaving out the
6741 -- testing for the specific controlled cases, but it seems safer and
6742 -- clearer this way, and echoes similar special-case tests of this
6743 -- kind in other places.)
6744
6745 if Present (Overridden_Subp)
6746 and then (not Is_Hidden (Overridden_Subp)
6747 or else
6748 (Nam_In (Chars (Overridden_Subp), Name_Initialize,
6749 Name_Adjust,
6750 Name_Finalize)
6751 and then Present (Alias (Overridden_Subp))
6752 and then (not Is_Hidden (Alias (Overridden_Subp))
6753 or else In_Instance)))
6754 then
6755 if Must_Not_Override (Spec) then
6756 Error_Msg_Sloc := Sloc (Overridden_Subp);
6757
6758 if Ekind (Subp) = E_Entry then
6759 Error_Msg_NE
6760 ("entry & overrides inherited operation #", Spec, Subp);
6761 else
6762 Error_Msg_NE
6763 ("subprogram & overrides inherited operation #", Spec, Subp);
6764 end if;
6765
6766 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6767 -- as an extension of Root_Controlled, and thus has a useless Adjust
6768 -- operation. This operation should not be inherited by other limited
6769 -- controlled types. An explicit Adjust for them is not overriding.
6770
6771 elsif Must_Override (Spec)
6772 and then Chars (Overridden_Subp) = Name_Adjust
6773 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6774 and then Present (Alias (Overridden_Subp))
6775 and then In_Predefined_Unit (Alias (Overridden_Subp))
6776 then
6777 Get_Name_String
6778 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
6779 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6780
6781 elsif Is_Subprogram (Subp) then
6782 if Is_Init_Proc (Subp) then
6783 null;
6784
6785 elsif No (Overridden_Operation (Subp)) then
6786
6787 -- For entities generated by Derive_Subprograms the overridden
6788 -- operation is the inherited primitive (which is available
6789 -- through the attribute alias)
6790
6791 if (Is_Dispatching_Operation (Subp)
6792 or else Is_Dispatching_Operation (Overridden_Subp))
6793 and then not Comes_From_Source (Overridden_Subp)
6794 and then Find_Dispatching_Type (Overridden_Subp) =
6795 Find_Dispatching_Type (Subp)
6796 and then Present (Alias (Overridden_Subp))
6797 and then Comes_From_Source (Alias (Overridden_Subp))
6798 then
6799 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
6800 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
6801
6802 else
6803 Set_Overridden_Operation (Subp, Overridden_Subp);
6804 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
6805 end if;
6806 end if;
6807 end if;
6808
6809 -- If primitive flag is set or this is a protected operation, then
6810 -- the operation is overriding at the point of its declaration, so
6811 -- warn if necessary. Otherwise it may have been declared before the
6812 -- operation it overrides and no check is required.
6813
6814 if Style_Check
6815 and then not Must_Override (Spec)
6816 and then (Is_Primitive
6817 or else Ekind (Scope (Subp)) = E_Protected_Type)
6818 then
6819 Style.Missing_Overriding (Decl, Subp);
6820 end if;
6821
6822 -- If Subp is an operator, it may override a predefined operation, if
6823 -- it is defined in the same scope as the type to which it applies.
6824 -- In that case Overridden_Subp is empty because of our implicit
6825 -- representation for predefined operators. We have to check whether the
6826 -- signature of Subp matches that of a predefined operator. Note that
6827 -- first argument provides the name of the operator, and the second
6828 -- argument the signature that may match that of a standard operation.
6829 -- If the indicator is overriding, then the operator must match a
6830 -- predefined signature, because we know already that there is no
6831 -- explicit overridden operation.
6832
6833 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
6834 if Must_Not_Override (Spec) then
6835
6836 -- If this is not a primitive or a protected subprogram, then
6837 -- "not overriding" is illegal.
6838
6839 if not Is_Primitive
6840 and then Ekind (Scope (Subp)) /= E_Protected_Type
6841 then
6842 Error_Msg_N ("overriding indicator only allowed "
6843 & "if subprogram is primitive", Subp);
6844
6845 elsif Can_Override_Operator (Subp) then
6846 Error_Msg_NE
6847 ("subprogram& overrides predefined operator ", Spec, Subp);
6848 end if;
6849
6850 elsif Must_Override (Spec) then
6851 if No (Overridden_Operation (Subp))
6852 and then not Can_Override_Operator (Subp)
6853 then
6854 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6855 end if;
6856
6857 elsif not Error_Posted (Subp)
6858 and then Style_Check
6859 and then Can_Override_Operator (Subp)
6860 and then not In_Predefined_Unit (Subp)
6861 then
6862 -- If style checks are enabled, indicate that the indicator is
6863 -- missing. However, at the point of declaration, the type of
6864 -- which this is a primitive operation may be private, in which
6865 -- case the indicator would be premature.
6866
6867 if Has_Private_Declaration (Etype (Subp))
6868 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
6869 then
6870 null;
6871 else
6872 Style.Missing_Overriding (Decl, Subp);
6873 end if;
6874 end if;
6875
6876 elsif Must_Override (Spec) then
6877 if Ekind (Subp) = E_Entry then
6878 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
6879 else
6880 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6881 end if;
6882
6883 -- If the operation is marked "not overriding" and it's not primitive
6884 -- then an error is issued, unless this is an operation of a task or
6885 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6886 -- has been specified have already been checked above.
6887
6888 elsif Must_Not_Override (Spec)
6889 and then not Is_Primitive
6890 and then Ekind (Subp) /= E_Entry
6891 and then Ekind (Scope (Subp)) /= E_Protected_Type
6892 then
6893 Error_Msg_N
6894 ("overriding indicator only allowed if subprogram is primitive",
6895 Subp);
6896 return;
6897 end if;
6898 end Check_Overriding_Indicator;
6899
6900 -------------------
6901 -- Check_Returns --
6902 -------------------
6903
6904 -- Note: this procedure needs to know far too much about how the expander
6905 -- messes with exceptions. The use of the flag Exception_Junk and the
6906 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6907 -- works, but is not very clean. It would be better if the expansion
6908 -- routines would leave Original_Node working nicely, and we could use
6909 -- Original_Node here to ignore all the peculiar expander messing ???
6910
6911 procedure Check_Returns
6912 (HSS : Node_Id;
6913 Mode : Character;
6914 Err : out Boolean;
6915 Proc : Entity_Id := Empty)
6916 is
6917 Handler : Node_Id;
6918
6919 procedure Check_Statement_Sequence (L : List_Id);
6920 -- Internal recursive procedure to check a list of statements for proper
6921 -- termination by a return statement (or a transfer of control or a
6922 -- compound statement that is itself internally properly terminated).
6923
6924 ------------------------------
6925 -- Check_Statement_Sequence --
6926 ------------------------------
6927
6928 procedure Check_Statement_Sequence (L : List_Id) is
6929 Last_Stm : Node_Id;
6930 Stm : Node_Id;
6931 Kind : Node_Kind;
6932
6933 function Assert_False return Boolean;
6934 -- Returns True if Last_Stm is a pragma Assert (False) that has been
6935 -- rewritten as a null statement when assertions are off. The assert
6936 -- is not active, but it is still enough to kill the warning.
6937
6938 ------------------
6939 -- Assert_False --
6940 ------------------
6941
6942 function Assert_False return Boolean is
6943 Orig : constant Node_Id := Original_Node (Last_Stm);
6944
6945 begin
6946 if Nkind (Orig) = N_Pragma
6947 and then Pragma_Name (Orig) = Name_Assert
6948 and then not Error_Posted (Orig)
6949 then
6950 declare
6951 Arg : constant Node_Id :=
6952 First (Pragma_Argument_Associations (Orig));
6953 Exp : constant Node_Id := Expression (Arg);
6954 begin
6955 return Nkind (Exp) = N_Identifier
6956 and then Chars (Exp) = Name_False;
6957 end;
6958
6959 else
6960 return False;
6961 end if;
6962 end Assert_False;
6963
6964 -- Local variables
6965
6966 Raise_Exception_Call : Boolean;
6967 -- Set True if statement sequence terminated by Raise_Exception call
6968 -- or a Reraise_Occurrence call.
6969
6970 -- Start of processing for Check_Statement_Sequence
6971
6972 begin
6973 Raise_Exception_Call := False;
6974
6975 -- Get last real statement
6976
6977 Last_Stm := Last (L);
6978
6979 -- Deal with digging out exception handler statement sequences that
6980 -- have been transformed by the local raise to goto optimization.
6981 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6982 -- optimization has occurred, we are looking at something like:
6983
6984 -- begin
6985 -- original stmts in block
6986
6987 -- exception \
6988 -- when excep1 => |
6989 -- goto L1; | omitted if No_Exception_Propagation
6990 -- when excep2 => |
6991 -- goto L2; /
6992 -- end;
6993
6994 -- goto L3; -- skip handler when exception not raised
6995
6996 -- <<L1>> -- target label for local exception
6997 -- begin
6998 -- estmts1
6999 -- end;
7000
7001 -- goto L3;
7002
7003 -- <<L2>>
7004 -- begin
7005 -- estmts2
7006 -- end;
7007
7008 -- <<L3>>
7009
7010 -- and what we have to do is to dig out the estmts1 and estmts2
7011 -- sequences (which were the original sequences of statements in
7012 -- the exception handlers) and check them.
7013
7014 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7015 Stm := Last_Stm;
7016 loop
7017 Prev (Stm);
7018 exit when No (Stm);
7019 exit when Nkind (Stm) /= N_Block_Statement;
7020 exit when not Exception_Junk (Stm);
7021 Prev (Stm);
7022 exit when No (Stm);
7023 exit when Nkind (Stm) /= N_Label;
7024 exit when not Exception_Junk (Stm);
7025 Check_Statement_Sequence
7026 (Statements (Handled_Statement_Sequence (Next (Stm))));
7027
7028 Prev (Stm);
7029 Last_Stm := Stm;
7030 exit when No (Stm);
7031 exit when Nkind (Stm) /= N_Goto_Statement;
7032 exit when not Exception_Junk (Stm);
7033 end loop;
7034 end if;
7035
7036 -- Don't count pragmas
7037
7038 while Nkind (Last_Stm) = N_Pragma
7039
7040 -- Don't count call to SS_Release (can happen after Raise_Exception)
7041
7042 or else
7043 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7044 and then
7045 Nkind (Name (Last_Stm)) = N_Identifier
7046 and then
7047 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7048
7049 -- Don't count exception junk
7050
7051 or else
7052 (Nkind_In (Last_Stm, N_Goto_Statement,
7053 N_Label,
7054 N_Object_Declaration)
7055 and then Exception_Junk (Last_Stm))
7056 or else Nkind (Last_Stm) in N_Push_xxx_Label
7057 or else Nkind (Last_Stm) in N_Pop_xxx_Label
7058
7059 -- Inserted code, such as finalization calls, is irrelevant: we only
7060 -- need to check original source.
7061
7062 or else Is_Rewrite_Insertion (Last_Stm)
7063 loop
7064 Prev (Last_Stm);
7065 end loop;
7066
7067 -- Here we have the "real" last statement
7068
7069 Kind := Nkind (Last_Stm);
7070
7071 -- Transfer of control, OK. Note that in the No_Return procedure
7072 -- case, we already diagnosed any explicit return statements, so
7073 -- we can treat them as OK in this context.
7074
7075 if Is_Transfer (Last_Stm) then
7076 return;
7077
7078 -- Check cases of explicit non-indirect procedure calls
7079
7080 elsif Kind = N_Procedure_Call_Statement
7081 and then Is_Entity_Name (Name (Last_Stm))
7082 then
7083 -- Check call to Raise_Exception procedure which is treated
7084 -- specially, as is a call to Reraise_Occurrence.
7085
7086 -- We suppress the warning in these cases since it is likely that
7087 -- the programmer really does not expect to deal with the case
7088 -- of Null_Occurrence, and thus would find a warning about a
7089 -- missing return curious, and raising Program_Error does not
7090 -- seem such a bad behavior if this does occur.
7091
7092 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7093 -- behavior will be to raise Constraint_Error (see AI-329).
7094
7095 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7096 or else
7097 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7098 then
7099 Raise_Exception_Call := True;
7100
7101 -- For Raise_Exception call, test first argument, if it is
7102 -- an attribute reference for a 'Identity call, then we know
7103 -- that the call cannot possibly return.
7104
7105 declare
7106 Arg : constant Node_Id :=
7107 Original_Node (First_Actual (Last_Stm));
7108 begin
7109 if Nkind (Arg) = N_Attribute_Reference
7110 and then Attribute_Name (Arg) = Name_Identity
7111 then
7112 return;
7113 end if;
7114 end;
7115 end if;
7116
7117 -- If statement, need to look inside if there is an else and check
7118 -- each constituent statement sequence for proper termination.
7119
7120 elsif Kind = N_If_Statement
7121 and then Present (Else_Statements (Last_Stm))
7122 then
7123 Check_Statement_Sequence (Then_Statements (Last_Stm));
7124 Check_Statement_Sequence (Else_Statements (Last_Stm));
7125
7126 if Present (Elsif_Parts (Last_Stm)) then
7127 declare
7128 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7129
7130 begin
7131 while Present (Elsif_Part) loop
7132 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7133 Next (Elsif_Part);
7134 end loop;
7135 end;
7136 end if;
7137
7138 return;
7139
7140 -- Case statement, check each case for proper termination
7141
7142 elsif Kind = N_Case_Statement then
7143 declare
7144 Case_Alt : Node_Id;
7145 begin
7146 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7147 while Present (Case_Alt) loop
7148 Check_Statement_Sequence (Statements (Case_Alt));
7149 Next_Non_Pragma (Case_Alt);
7150 end loop;
7151 end;
7152
7153 return;
7154
7155 -- Block statement, check its handled sequence of statements
7156
7157 elsif Kind = N_Block_Statement then
7158 declare
7159 Err1 : Boolean;
7160
7161 begin
7162 Check_Returns
7163 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7164
7165 if Err1 then
7166 Err := True;
7167 end if;
7168
7169 return;
7170 end;
7171
7172 -- Loop statement. If there is an iteration scheme, we can definitely
7173 -- fall out of the loop. Similarly if there is an exit statement, we
7174 -- can fall out. In either case we need a following return.
7175
7176 elsif Kind = N_Loop_Statement then
7177 if Present (Iteration_Scheme (Last_Stm))
7178 or else Has_Exit (Entity (Identifier (Last_Stm)))
7179 then
7180 null;
7181
7182 -- A loop with no exit statement or iteration scheme is either
7183 -- an infinite loop, or it has some other exit (raise/return).
7184 -- In either case, no warning is required.
7185
7186 else
7187 return;
7188 end if;
7189
7190 -- Timed entry call, check entry call and delay alternatives
7191
7192 -- Note: in expanded code, the timed entry call has been converted
7193 -- to a set of expanded statements on which the check will work
7194 -- correctly in any case.
7195
7196 elsif Kind = N_Timed_Entry_Call then
7197 declare
7198 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7199 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7200
7201 begin
7202 -- If statement sequence of entry call alternative is missing,
7203 -- then we can definitely fall through, and we post the error
7204 -- message on the entry call alternative itself.
7205
7206 if No (Statements (ECA)) then
7207 Last_Stm := ECA;
7208
7209 -- If statement sequence of delay alternative is missing, then
7210 -- we can definitely fall through, and we post the error
7211 -- message on the delay alternative itself.
7212
7213 -- Note: if both ECA and DCA are missing the return, then we
7214 -- post only one message, should be enough to fix the bugs.
7215 -- If not we will get a message next time on the DCA when the
7216 -- ECA is fixed.
7217
7218 elsif No (Statements (DCA)) then
7219 Last_Stm := DCA;
7220
7221 -- Else check both statement sequences
7222
7223 else
7224 Check_Statement_Sequence (Statements (ECA));
7225 Check_Statement_Sequence (Statements (DCA));
7226 return;
7227 end if;
7228 end;
7229
7230 -- Conditional entry call, check entry call and else part
7231
7232 -- Note: in expanded code, the conditional entry call has been
7233 -- converted to a set of expanded statements on which the check
7234 -- will work correctly in any case.
7235
7236 elsif Kind = N_Conditional_Entry_Call then
7237 declare
7238 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7239
7240 begin
7241 -- If statement sequence of entry call alternative is missing,
7242 -- then we can definitely fall through, and we post the error
7243 -- message on the entry call alternative itself.
7244
7245 if No (Statements (ECA)) then
7246 Last_Stm := ECA;
7247
7248 -- Else check statement sequence and else part
7249
7250 else
7251 Check_Statement_Sequence (Statements (ECA));
7252 Check_Statement_Sequence (Else_Statements (Last_Stm));
7253 return;
7254 end if;
7255 end;
7256 end if;
7257
7258 -- If we fall through, issue appropriate message
7259
7260 if Mode = 'F' then
7261
7262 -- Kill warning if last statement is a raise exception call,
7263 -- or a pragma Assert (False). Note that with assertions enabled,
7264 -- such a pragma has been converted into a raise exception call
7265 -- already, so the Assert_False is for the assertions off case.
7266
7267 if not Raise_Exception_Call and then not Assert_False then
7268
7269 -- In GNATprove mode, it is an error to have a missing return
7270
7271 Error_Msg_Warn := SPARK_Mode /= On;
7272
7273 -- Issue error message or warning
7274
7275 Error_Msg_N
7276 ("RETURN statement missing following this statement<<!",
7277 Last_Stm);
7278 Error_Msg_N
7279 ("\Program_Error ]<<!", Last_Stm);
7280 end if;
7281
7282 -- Note: we set Err even though we have not issued a warning
7283 -- because we still have a case of a missing return. This is
7284 -- an extremely marginal case, probably will never be noticed
7285 -- but we might as well get it right.
7286
7287 Err := True;
7288
7289 -- Otherwise we have the case of a procedure marked No_Return
7290
7291 else
7292 if not Raise_Exception_Call then
7293 if GNATprove_Mode then
7294 Error_Msg_N
7295 ("implied return after this statement would have raised "
7296 & "Program_Error", Last_Stm);
7297
7298 -- In normal compilation mode, do not warn on a generated call
7299 -- (e.g. in the body of a renaming as completion).
7300
7301 elsif Comes_From_Source (Last_Stm) then
7302 Error_Msg_N
7303 ("implied return after this statement will raise "
7304 & "Program_Error??", Last_Stm);
7305 end if;
7306
7307 Error_Msg_Warn := SPARK_Mode /= On;
7308 Error_Msg_NE
7309 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7310 end if;
7311
7312 declare
7313 RE : constant Node_Id :=
7314 Make_Raise_Program_Error (Sloc (Last_Stm),
7315 Reason => PE_Implicit_Return);
7316 begin
7317 Insert_After (Last_Stm, RE);
7318 Analyze (RE);
7319 end;
7320 end if;
7321 end Check_Statement_Sequence;
7322
7323 -- Start of processing for Check_Returns
7324
7325 begin
7326 Err := False;
7327 Check_Statement_Sequence (Statements (HSS));
7328
7329 if Present (Exception_Handlers (HSS)) then
7330 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7331 while Present (Handler) loop
7332 Check_Statement_Sequence (Statements (Handler));
7333 Next_Non_Pragma (Handler);
7334 end loop;
7335 end if;
7336 end Check_Returns;
7337
7338 ----------------------------
7339 -- Check_Subprogram_Order --
7340 ----------------------------
7341
7342 procedure Check_Subprogram_Order (N : Node_Id) is
7343
7344 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7345 -- This is used to check if S1 > S2 in the sense required by this test,
7346 -- for example nameab < namec, but name2 < name10.
7347
7348 -----------------------------
7349 -- Subprogram_Name_Greater --
7350 -----------------------------
7351
7352 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7353 L1, L2 : Positive;
7354 N1, N2 : Natural;
7355
7356 begin
7357 -- Deal with special case where names are identical except for a
7358 -- numerical suffix. These are handled specially, taking the numeric
7359 -- ordering from the suffix into account.
7360
7361 L1 := S1'Last;
7362 while S1 (L1) in '0' .. '9' loop
7363 L1 := L1 - 1;
7364 end loop;
7365
7366 L2 := S2'Last;
7367 while S2 (L2) in '0' .. '9' loop
7368 L2 := L2 - 1;
7369 end loop;
7370
7371 -- If non-numeric parts non-equal, do straight compare
7372
7373 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7374 return S1 > S2;
7375
7376 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7377 -- that a missing suffix is treated as numeric zero in this test.
7378
7379 else
7380 N1 := 0;
7381 while L1 < S1'Last loop
7382 L1 := L1 + 1;
7383 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7384 end loop;
7385
7386 N2 := 0;
7387 while L2 < S2'Last loop
7388 L2 := L2 + 1;
7389 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7390 end loop;
7391
7392 return N1 > N2;
7393 end if;
7394 end Subprogram_Name_Greater;
7395
7396 -- Start of processing for Check_Subprogram_Order
7397
7398 begin
7399 -- Check body in alpha order if this is option
7400
7401 if Style_Check
7402 and then Style_Check_Order_Subprograms
7403 and then Nkind (N) = N_Subprogram_Body
7404 and then Comes_From_Source (N)
7405 and then In_Extended_Main_Source_Unit (N)
7406 then
7407 declare
7408 LSN : String_Ptr
7409 renames Scope_Stack.Table
7410 (Scope_Stack.Last).Last_Subprogram_Name;
7411
7412 Body_Id : constant Entity_Id :=
7413 Defining_Entity (Specification (N));
7414
7415 begin
7416 Get_Decoded_Name_String (Chars (Body_Id));
7417
7418 if LSN /= null then
7419 if Subprogram_Name_Greater
7420 (LSN.all, Name_Buffer (1 .. Name_Len))
7421 then
7422 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7423 end if;
7424
7425 Free (LSN);
7426 end if;
7427
7428 LSN := new String'(Name_Buffer (1 .. Name_Len));
7429 end;
7430 end if;
7431 end Check_Subprogram_Order;
7432
7433 ------------------------------
7434 -- Check_Subtype_Conformant --
7435 ------------------------------
7436
7437 procedure Check_Subtype_Conformant
7438 (New_Id : Entity_Id;
7439 Old_Id : Entity_Id;
7440 Err_Loc : Node_Id := Empty;
7441 Skip_Controlling_Formals : Boolean := False;
7442 Get_Inst : Boolean := False)
7443 is
7444 Result : Boolean;
7445 pragma Warnings (Off, Result);
7446 begin
7447 Check_Conformance
7448 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7449 Skip_Controlling_Formals => Skip_Controlling_Formals,
7450 Get_Inst => Get_Inst);
7451 end Check_Subtype_Conformant;
7452
7453 -----------------------------------
7454 -- Check_Synchronized_Overriding --
7455 -----------------------------------
7456
7457 procedure Check_Synchronized_Overriding
7458 (Def_Id : Entity_Id;
7459 Overridden_Subp : out Entity_Id)
7460 is
7461 Ifaces_List : Elist_Id;
7462 In_Scope : Boolean;
7463 Typ : Entity_Id;
7464
7465 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7466 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7467 -- subprogram is implemented by a protected procedure or entry,
7468 -- its first parameter must be out, in out, or access-to-variable.
7469
7470 function Matches_Prefixed_View_Profile
7471 (Prim_Params : List_Id;
7472 Iface_Params : List_Id) return Boolean;
7473 -- Determine whether a subprogram's parameter profile Prim_Params
7474 -- matches that of a potentially overridden interface subprogram
7475 -- Iface_Params. Also determine if the type of first parameter of
7476 -- Iface_Params is an implemented interface.
7477
7478 ----------------------
7479 -- Is_Valid_Formal --
7480 ----------------------
7481
7482 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7483 begin
7484 return
7485 Ekind_In (F, E_In_Out_Parameter, E_Out_Parameter)
7486 or else
7487 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7488 and then not Constant_Present (Parameter_Type (Parent (F))));
7489 end Is_Valid_Formal;
7490
7491 -----------------------------------
7492 -- Matches_Prefixed_View_Profile --
7493 -----------------------------------
7494
7495 function Matches_Prefixed_View_Profile
7496 (Prim_Params : List_Id;
7497 Iface_Params : List_Id) return Boolean
7498 is
7499 function Is_Implemented
7500 (Ifaces_List : Elist_Id;
7501 Iface : Entity_Id) return Boolean;
7502 -- Determine if Iface is implemented by the current task or
7503 -- protected type.
7504
7505 --------------------
7506 -- Is_Implemented --
7507 --------------------
7508
7509 function Is_Implemented
7510 (Ifaces_List : Elist_Id;
7511 Iface : Entity_Id) return Boolean
7512 is
7513 Iface_Elmt : Elmt_Id;
7514
7515 begin
7516 Iface_Elmt := First_Elmt (Ifaces_List);
7517 while Present (Iface_Elmt) loop
7518 if Node (Iface_Elmt) = Iface then
7519 return True;
7520 end if;
7521
7522 Next_Elmt (Iface_Elmt);
7523 end loop;
7524
7525 return False;
7526 end Is_Implemented;
7527
7528 -- Local variables
7529
7530 Iface_Id : Entity_Id;
7531 Iface_Param : Node_Id;
7532 Iface_Typ : Entity_Id;
7533 Prim_Id : Entity_Id;
7534 Prim_Param : Node_Id;
7535 Prim_Typ : Entity_Id;
7536
7537 -- Start of processing for Matches_Prefixed_View_Profile
7538
7539 begin
7540 Iface_Param := First (Iface_Params);
7541 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7542
7543 if Is_Access_Type (Iface_Typ) then
7544 Iface_Typ := Designated_Type (Iface_Typ);
7545 end if;
7546
7547 Prim_Param := First (Prim_Params);
7548
7549 -- The first parameter of the potentially overridden subprogram must
7550 -- be an interface implemented by Prim.
7551
7552 if not Is_Interface (Iface_Typ)
7553 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7554 then
7555 return False;
7556 end if;
7557
7558 -- The checks on the object parameters are done, so move on to the
7559 -- rest of the parameters.
7560
7561 if not In_Scope then
7562 Next (Prim_Param);
7563 end if;
7564
7565 Next (Iface_Param);
7566 while Present (Iface_Param) and then Present (Prim_Param) loop
7567 Iface_Id := Defining_Identifier (Iface_Param);
7568 Iface_Typ := Find_Parameter_Type (Iface_Param);
7569
7570 Prim_Id := Defining_Identifier (Prim_Param);
7571 Prim_Typ := Find_Parameter_Type (Prim_Param);
7572
7573 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7574 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7575 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7576 then
7577 Iface_Typ := Designated_Type (Iface_Typ);
7578 Prim_Typ := Designated_Type (Prim_Typ);
7579 end if;
7580
7581 -- Case of multiple interface types inside a parameter profile
7582
7583 -- (Obj_Param : in out Iface; ...; Param : Iface)
7584
7585 -- If the interface type is implemented, then the matching type in
7586 -- the primitive should be the implementing record type.
7587
7588 if Ekind (Iface_Typ) = E_Record_Type
7589 and then Is_Interface (Iface_Typ)
7590 and then Is_Implemented (Ifaces_List, Iface_Typ)
7591 then
7592 if Prim_Typ /= Typ then
7593 return False;
7594 end if;
7595
7596 -- The two parameters must be both mode and subtype conformant
7597
7598 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7599 or else not
7600 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7601 then
7602 return False;
7603 end if;
7604
7605 Next (Iface_Param);
7606 Next (Prim_Param);
7607 end loop;
7608
7609 -- One of the two lists contains more parameters than the other
7610
7611 if Present (Iface_Param) or else Present (Prim_Param) then
7612 return False;
7613 end if;
7614
7615 return True;
7616 end Matches_Prefixed_View_Profile;
7617
7618 -- Start of processing for Check_Synchronized_Overriding
7619
7620 begin
7621 Overridden_Subp := Empty;
7622
7623 -- Def_Id must be an entry or a subprogram. We should skip predefined
7624 -- primitives internally generated by the front end; however at this
7625 -- stage predefined primitives are still not fully decorated. As a
7626 -- minor optimization we skip here internally generated subprograms.
7627
7628 if (Ekind (Def_Id) /= E_Entry
7629 and then Ekind (Def_Id) /= E_Function
7630 and then Ekind (Def_Id) /= E_Procedure)
7631 or else not Comes_From_Source (Def_Id)
7632 then
7633 return;
7634 end if;
7635
7636 -- Search for the concurrent declaration since it contains the list of
7637 -- all implemented interfaces. In this case, the subprogram is declared
7638 -- within the scope of a protected or a task type.
7639
7640 if Present (Scope (Def_Id))
7641 and then Is_Concurrent_Type (Scope (Def_Id))
7642 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7643 then
7644 Typ := Scope (Def_Id);
7645 In_Scope := True;
7646
7647 -- The enclosing scope is not a synchronized type and the subprogram
7648 -- has no formals.
7649
7650 elsif No (First_Formal (Def_Id)) then
7651 return;
7652
7653 -- The subprogram has formals and hence it may be a primitive of a
7654 -- concurrent type.
7655
7656 else
7657 Typ := Etype (First_Formal (Def_Id));
7658
7659 if Is_Access_Type (Typ) then
7660 Typ := Directly_Designated_Type (Typ);
7661 end if;
7662
7663 if Is_Concurrent_Type (Typ)
7664 and then not Is_Generic_Actual_Type (Typ)
7665 then
7666 In_Scope := False;
7667
7668 -- This case occurs when the concurrent type is declared within a
7669 -- generic unit. As a result the corresponding record has been built
7670 -- and used as the type of the first formal, we just have to retrieve
7671 -- the corresponding concurrent type.
7672
7673 elsif Is_Concurrent_Record_Type (Typ)
7674 and then not Is_Class_Wide_Type (Typ)
7675 and then Present (Corresponding_Concurrent_Type (Typ))
7676 then
7677 Typ := Corresponding_Concurrent_Type (Typ);
7678 In_Scope := False;
7679
7680 else
7681 return;
7682 end if;
7683 end if;
7684
7685 -- There is no overriding to check if this is an inherited operation in
7686 -- a type derivation for a generic actual.
7687
7688 Collect_Interfaces (Typ, Ifaces_List);
7689
7690 if Is_Empty_Elmt_List (Ifaces_List) then
7691 return;
7692 end if;
7693
7694 -- Determine whether entry or subprogram Def_Id overrides a primitive
7695 -- operation that belongs to one of the interfaces in Ifaces_List.
7696
7697 declare
7698 Candidate : Entity_Id := Empty;
7699 Hom : Entity_Id := Empty;
7700 Subp : Entity_Id := Empty;
7701
7702 begin
7703 -- Traverse the homonym chain, looking for a potentially overridden
7704 -- subprogram that belongs to an implemented interface.
7705
7706 Hom := Current_Entity_In_Scope (Def_Id);
7707 while Present (Hom) loop
7708 Subp := Hom;
7709
7710 if Subp = Def_Id
7711 or else not Is_Overloadable (Subp)
7712 or else not Is_Primitive (Subp)
7713 or else not Is_Dispatching_Operation (Subp)
7714 or else not Present (Find_Dispatching_Type (Subp))
7715 or else not Is_Interface (Find_Dispatching_Type (Subp))
7716 then
7717 null;
7718
7719 -- Entries and procedures can override abstract or null interface
7720 -- procedures.
7721
7722 elsif Ekind_In (Def_Id, E_Entry, E_Procedure)
7723 and then Ekind (Subp) = E_Procedure
7724 and then Matches_Prefixed_View_Profile
7725 (Parameter_Specifications (Parent (Def_Id)),
7726 Parameter_Specifications (Parent (Subp)))
7727 then
7728 Candidate := Subp;
7729
7730 -- For an overridden subprogram Subp, check whether the mode
7731 -- of its first parameter is correct depending on the kind of
7732 -- synchronized type.
7733
7734 declare
7735 Formal : constant Node_Id := First_Formal (Candidate);
7736
7737 begin
7738 -- In order for an entry or a protected procedure to
7739 -- override, the first parameter of the overridden routine
7740 -- must be of mode "out", "in out", or access-to-variable.
7741
7742 if Ekind_In (Candidate, E_Entry, E_Procedure)
7743 and then Is_Protected_Type (Typ)
7744 and then not Is_Valid_Formal (Formal)
7745 then
7746 null;
7747
7748 -- All other cases are OK since a task entry or routine does
7749 -- not have a restriction on the mode of the first parameter
7750 -- of the overridden interface routine.
7751
7752 else
7753 Overridden_Subp := Candidate;
7754 return;
7755 end if;
7756 end;
7757
7758 -- Functions can override abstract interface functions. Return
7759 -- types must be subtype conformant.
7760
7761 elsif Ekind (Def_Id) = E_Function
7762 and then Ekind (Subp) = E_Function
7763 and then Matches_Prefixed_View_Profile
7764 (Parameter_Specifications (Parent (Def_Id)),
7765 Parameter_Specifications (Parent (Subp)))
7766 and then Conforming_Types
7767 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7768 then
7769 Candidate := Subp;
7770
7771 -- If an inherited subprogram is implemented by a protected
7772 -- function, then the first parameter of the inherited
7773 -- subprogram shall be of mode in, but not an access-to-
7774 -- variable parameter (RM 9.4(11/9)).
7775
7776 if Present (First_Formal (Subp))
7777 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7778 and then
7779 (not Is_Access_Type (Etype (First_Formal (Subp)))
7780 or else
7781 Is_Access_Constant (Etype (First_Formal (Subp))))
7782 then
7783 Overridden_Subp := Subp;
7784 return;
7785 end if;
7786 end if;
7787
7788 Hom := Homonym (Hom);
7789 end loop;
7790
7791 -- After examining all candidates for overriding, we are left with
7792 -- the best match, which is a mode-incompatible interface routine.
7793
7794 if In_Scope and then Present (Candidate) then
7795 Error_Msg_PT (Def_Id, Candidate);
7796 end if;
7797
7798 Overridden_Subp := Candidate;
7799 return;
7800 end;
7801 end Check_Synchronized_Overriding;
7802
7803 ---------------------------
7804 -- Check_Type_Conformant --
7805 ---------------------------
7806
7807 procedure Check_Type_Conformant
7808 (New_Id : Entity_Id;
7809 Old_Id : Entity_Id;
7810 Err_Loc : Node_Id := Empty)
7811 is
7812 Result : Boolean;
7813 pragma Warnings (Off, Result);
7814 begin
7815 Check_Conformance
7816 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7817 end Check_Type_Conformant;
7818
7819 ---------------------------
7820 -- Can_Override_Operator --
7821 ---------------------------
7822
7823 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7824 Typ : Entity_Id;
7825
7826 begin
7827 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7828 return False;
7829
7830 else
7831 Typ := Base_Type (Etype (First_Formal (Subp)));
7832
7833 -- Check explicitly that the operation is a primitive of the type
7834
7835 return Operator_Matches_Spec (Subp, Subp)
7836 and then not Is_Generic_Type (Typ)
7837 and then Scope (Subp) = Scope (Typ)
7838 and then not Is_Class_Wide_Type (Typ);
7839 end if;
7840 end Can_Override_Operator;
7841
7842 ----------------------
7843 -- Conforming_Types --
7844 ----------------------
7845
7846 function Conforming_Types
7847 (T1 : Entity_Id;
7848 T2 : Entity_Id;
7849 Ctype : Conformance_Type;
7850 Get_Inst : Boolean := False) return Boolean
7851 is
7852 function Base_Types_Match
7853 (Typ_1 : Entity_Id;
7854 Typ_2 : Entity_Id) return Boolean;
7855 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
7856 -- in different scopes (e.g. parent and child instances), then verify
7857 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
7858 -- the same subtype chain. The whole purpose of this procedure is to
7859 -- prevent spurious ambiguities in an instantiation that may arise if
7860 -- two distinct generic types are instantiated with the same actual.
7861
7862 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
7863 -- An access parameter can designate an incomplete type. If the
7864 -- incomplete type is the limited view of a type from a limited_
7865 -- with_clause, check whether the non-limited view is available.
7866 -- If it is a (non-limited) incomplete type, get the full view.
7867
7868 function Matches_Limited_With_View
7869 (Typ_1 : Entity_Id;
7870 Typ_2 : Entity_Id) return Boolean;
7871 -- Returns True if and only if either Typ_1 denotes a limited view of
7872 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
7873 -- the limited with view of a type is used in a subprogram declaration
7874 -- and the subprogram body is in the scope of a regular with clause for
7875 -- the same unit. In such a case, the two type entities are considered
7876 -- identical for purposes of conformance checking.
7877
7878 ----------------------
7879 -- Base_Types_Match --
7880 ----------------------
7881
7882 function Base_Types_Match
7883 (Typ_1 : Entity_Id;
7884 Typ_2 : Entity_Id) return Boolean
7885 is
7886 Base_1 : constant Entity_Id := Base_Type (Typ_1);
7887 Base_2 : constant Entity_Id := Base_Type (Typ_2);
7888
7889 begin
7890 if Typ_1 = Typ_2 then
7891 return True;
7892
7893 elsif Base_1 = Base_2 then
7894
7895 -- The following is too permissive. A more precise test should
7896 -- check that the generic actual is an ancestor subtype of the
7897 -- other ???.
7898
7899 -- See code in Find_Corresponding_Spec that applies an additional
7900 -- filter to handle accidental amiguities in instances.
7901
7902 return
7903 not Is_Generic_Actual_Type (Typ_1)
7904 or else not Is_Generic_Actual_Type (Typ_2)
7905 or else Scope (Typ_1) /= Scope (Typ_2);
7906
7907 -- If Typ_2 is a generic actual type it is declared as the subtype of
7908 -- the actual. If that actual is itself a subtype we need to use its
7909 -- own base type to check for compatibility.
7910
7911 elsif Ekind (Base_2) = Ekind (Typ_2)
7912 and then Base_1 = Base_Type (Base_2)
7913 then
7914 return True;
7915
7916 elsif Ekind (Base_1) = Ekind (Typ_1)
7917 and then Base_2 = Base_Type (Base_1)
7918 then
7919 return True;
7920
7921 else
7922 return False;
7923 end if;
7924 end Base_Types_Match;
7925
7926 --------------------------
7927 -- Find_Designated_Type --
7928 --------------------------
7929
7930 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
7931 Desig : Entity_Id;
7932
7933 begin
7934 Desig := Directly_Designated_Type (Typ);
7935
7936 if Ekind (Desig) = E_Incomplete_Type then
7937
7938 -- If regular incomplete type, get full view if available
7939
7940 if Present (Full_View (Desig)) then
7941 Desig := Full_View (Desig);
7942
7943 -- If limited view of a type, get non-limited view if available,
7944 -- and check again for a regular incomplete type.
7945
7946 elsif Present (Non_Limited_View (Desig)) then
7947 Desig := Get_Full_View (Non_Limited_View (Desig));
7948 end if;
7949 end if;
7950
7951 return Desig;
7952 end Find_Designated_Type;
7953
7954 -------------------------------
7955 -- Matches_Limited_With_View --
7956 -------------------------------
7957
7958 function Matches_Limited_With_View
7959 (Typ_1 : Entity_Id;
7960 Typ_2 : Entity_Id) return Boolean
7961 is
7962 function Is_Matching_Limited_View
7963 (Typ : Entity_Id;
7964 View : Entity_Id) return Boolean;
7965 -- Determine whether non-limited view View denotes type Typ in some
7966 -- conformant fashion.
7967
7968 ------------------------------
7969 -- Is_Matching_Limited_View --
7970 ------------------------------
7971
7972 function Is_Matching_Limited_View
7973 (Typ : Entity_Id;
7974 View : Entity_Id) return Boolean
7975 is
7976 Root_Typ : Entity_Id;
7977 Root_View : Entity_Id;
7978
7979 begin
7980 -- The non-limited view directly denotes the type
7981
7982 if Typ = View then
7983 return True;
7984
7985 -- The type is a subtype of the non-limited view
7986
7987 elsif Is_Subtype_Of (Typ, View) then
7988 return True;
7989
7990 -- Both the non-limited view and the type denote class-wide types
7991
7992 elsif Is_Class_Wide_Type (Typ)
7993 and then Is_Class_Wide_Type (View)
7994 then
7995 Root_Typ := Root_Type (Typ);
7996 Root_View := Root_Type (View);
7997
7998 if Root_Typ = Root_View then
7999 return True;
8000
8001 -- An incomplete tagged type and its full view may receive two
8002 -- distinct class-wide types when the related package has not
8003 -- been analyzed yet.
8004
8005 -- package Pack is
8006 -- type T is tagged; -- CW_1
8007 -- type T is tagged null record; -- CW_2
8008 -- end Pack;
8009
8010 -- This is because the package lacks any semantic information
8011 -- that may eventually link both views of T. As a consequence,
8012 -- a client of the limited view of Pack will see CW_2 while a
8013 -- client of the non-limited view of Pack will see CW_1.
8014
8015 elsif Is_Incomplete_Type (Root_Typ)
8016 and then Present (Full_View (Root_Typ))
8017 and then Full_View (Root_Typ) = Root_View
8018 then
8019 return True;
8020
8021 elsif Is_Incomplete_Type (Root_View)
8022 and then Present (Full_View (Root_View))
8023 and then Full_View (Root_View) = Root_Typ
8024 then
8025 return True;
8026 end if;
8027 end if;
8028
8029 return False;
8030 end Is_Matching_Limited_View;
8031
8032 -- Start of processing for Matches_Limited_With_View
8033
8034 begin
8035 -- In some cases a type imported through a limited_with clause, and
8036 -- its non-limited view are both visible, for example in an anonymous
8037 -- access-to-class-wide type in a formal, or when building the body
8038 -- for a subprogram renaming after the subprogram has been frozen.
8039 -- In these cases both entities designate the same type. In addition,
8040 -- if one of them is an actual in an instance, it may be a subtype of
8041 -- the non-limited view of the other.
8042
8043 if From_Limited_With (Typ_1)
8044 and then From_Limited_With (Typ_2)
8045 and then Available_View (Typ_1) = Available_View (Typ_2)
8046 then
8047 return True;
8048
8049 elsif From_Limited_With (Typ_1) then
8050 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8051
8052 elsif From_Limited_With (Typ_2) then
8053 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8054
8055 else
8056 return False;
8057 end if;
8058 end Matches_Limited_With_View;
8059
8060 -- Local variables
8061
8062 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8063
8064 Type_1 : Entity_Id := T1;
8065 Type_2 : Entity_Id := T2;
8066
8067 -- Start of processing for Conforming_Types
8068
8069 begin
8070 -- The context is an instance association for a formal access-to-
8071 -- subprogram type; the formal parameter types require mapping because
8072 -- they may denote other formal parameters of the generic unit.
8073
8074 if Get_Inst then
8075 Type_1 := Get_Instance_Of (T1);
8076 Type_2 := Get_Instance_Of (T2);
8077 end if;
8078
8079 -- If one of the types is a view of the other introduced by a limited
8080 -- with clause, treat these as conforming for all purposes.
8081
8082 if Matches_Limited_With_View (T1, T2) then
8083 return True;
8084
8085 elsif Base_Types_Match (Type_1, Type_2) then
8086 if Ctype <= Mode_Conformant then
8087 return True;
8088
8089 else
8090 return
8091 Subtypes_Statically_Match (Type_1, Type_2)
8092 and then Dimensions_Match (Type_1, Type_2);
8093 end if;
8094
8095 elsif Is_Incomplete_Or_Private_Type (Type_1)
8096 and then Present (Full_View (Type_1))
8097 and then Base_Types_Match (Full_View (Type_1), Type_2)
8098 then
8099 return
8100 Ctype <= Mode_Conformant
8101 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8102
8103 elsif Ekind (Type_2) = E_Incomplete_Type
8104 and then Present (Full_View (Type_2))
8105 and then Base_Types_Match (Type_1, Full_View (Type_2))
8106 then
8107 return
8108 Ctype <= Mode_Conformant
8109 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8110
8111 elsif Is_Private_Type (Type_2)
8112 and then In_Instance
8113 and then Present (Full_View (Type_2))
8114 and then Base_Types_Match (Type_1, Full_View (Type_2))
8115 then
8116 return
8117 Ctype <= Mode_Conformant
8118 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8119
8120 -- Another confusion between views in a nested instance with an
8121 -- actual private type whose full view is not in scope.
8122
8123 elsif Ekind (Type_2) = E_Private_Subtype
8124 and then In_Instance
8125 and then Etype (Type_2) = Type_1
8126 then
8127 return True;
8128
8129 -- In Ada 2012, incomplete types (including limited views) can appear
8130 -- as actuals in instantiations, where they are conformant to the
8131 -- corresponding incomplete formal.
8132
8133 elsif Is_Incomplete_Type (Type_1)
8134 and then Is_Incomplete_Type (Type_2)
8135 and then In_Instance
8136 and then (Used_As_Generic_Actual (Type_1)
8137 or else Used_As_Generic_Actual (Type_2))
8138 then
8139 return True;
8140 end if;
8141
8142 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8143 -- treated recursively because they carry a signature. As far as
8144 -- conformance is concerned, convention plays no role, and either
8145 -- or both could be access to protected subprograms.
8146
8147 Are_Anonymous_Access_To_Subprogram_Types :=
8148 Ekind_In (Type_1, E_Anonymous_Access_Subprogram_Type,
8149 E_Anonymous_Access_Protected_Subprogram_Type)
8150 and then
8151 Ekind_In (Type_2, E_Anonymous_Access_Subprogram_Type,
8152 E_Anonymous_Access_Protected_Subprogram_Type);
8153
8154 -- Test anonymous access type case. For this case, static subtype
8155 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8156 -- the base types because we may have built internal subtype entities
8157 -- to handle null-excluding types (see Process_Formals).
8158
8159 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8160 and then
8161 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8162
8163 -- Ada 2005 (AI-254)
8164
8165 or else Are_Anonymous_Access_To_Subprogram_Types
8166 then
8167 declare
8168 Desig_1 : Entity_Id;
8169 Desig_2 : Entity_Id;
8170
8171 begin
8172 -- In Ada 2005, access constant indicators must match for
8173 -- subtype conformance.
8174
8175 if Ada_Version >= Ada_2005
8176 and then Ctype >= Subtype_Conformant
8177 and then
8178 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8179 then
8180 return False;
8181 end if;
8182
8183 Desig_1 := Find_Designated_Type (Type_1);
8184 Desig_2 := Find_Designated_Type (Type_2);
8185
8186 -- If the context is an instance association for a formal
8187 -- access-to-subprogram type; formal access parameter designated
8188 -- types require mapping because they may denote other formal
8189 -- parameters of the generic unit.
8190
8191 if Get_Inst then
8192 Desig_1 := Get_Instance_Of (Desig_1);
8193 Desig_2 := Get_Instance_Of (Desig_2);
8194 end if;
8195
8196 -- It is possible for a Class_Wide_Type to be introduced for an
8197 -- incomplete type, in which case there is a separate class_ wide
8198 -- type for the full view. The types conform if their Etypes
8199 -- conform, i.e. one may be the full view of the other. This can
8200 -- only happen in the context of an access parameter, other uses
8201 -- of an incomplete Class_Wide_Type are illegal.
8202
8203 if Is_Class_Wide_Type (Desig_1)
8204 and then
8205 Is_Class_Wide_Type (Desig_2)
8206 then
8207 return
8208 Conforming_Types
8209 (Etype (Base_Type (Desig_1)),
8210 Etype (Base_Type (Desig_2)), Ctype);
8211
8212 elsif Are_Anonymous_Access_To_Subprogram_Types then
8213 if Ada_Version < Ada_2005 then
8214 return
8215 Ctype = Type_Conformant
8216 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8217
8218 -- We must check the conformance of the signatures themselves
8219
8220 else
8221 declare
8222 Conformant : Boolean;
8223 begin
8224 Check_Conformance
8225 (Desig_1, Desig_2, Ctype, False, Conformant);
8226 return Conformant;
8227 end;
8228 end if;
8229
8230 -- A limited view of an actual matches the corresponding
8231 -- incomplete formal.
8232
8233 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8234 and then From_Limited_With (Desig_2)
8235 and then Used_As_Generic_Actual (Etype (Desig_2))
8236 then
8237 return True;
8238
8239 else
8240 return Base_Type (Desig_1) = Base_Type (Desig_2)
8241 and then (Ctype = Type_Conformant
8242 or else
8243 Subtypes_Statically_Match (Desig_1, Desig_2));
8244 end if;
8245 end;
8246
8247 -- Otherwise definitely no match
8248
8249 else
8250 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8251 and then Is_Access_Type (Type_2))
8252 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8253 and then Is_Access_Type (Type_1)))
8254 and then
8255 Conforming_Types
8256 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8257 then
8258 May_Hide_Profile := True;
8259 end if;
8260
8261 return False;
8262 end if;
8263 end Conforming_Types;
8264
8265 --------------------------
8266 -- Create_Extra_Formals --
8267 --------------------------
8268
8269 procedure Create_Extra_Formals (E : Entity_Id) is
8270 First_Extra : Entity_Id := Empty;
8271 Formal : Entity_Id;
8272 Last_Extra : Entity_Id := Empty;
8273
8274 function Add_Extra_Formal
8275 (Assoc_Entity : Entity_Id;
8276 Typ : Entity_Id;
8277 Scope : Entity_Id;
8278 Suffix : String) return Entity_Id;
8279 -- Add an extra formal to the current list of formals and extra formals.
8280 -- The extra formal is added to the end of the list of extra formals,
8281 -- and also returned as the result. These formals are always of mode IN.
8282 -- The new formal has the type Typ, is declared in Scope, and its name
8283 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8284 -- The following suffixes are currently used. They should not be changed
8285 -- without coordinating with CodePeer, which makes use of these to
8286 -- provide better messages.
8287
8288 -- O denotes the Constrained bit.
8289 -- L denotes the accessibility level.
8290 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8291 -- the full list in exp_ch6.BIP_Formal_Kind.
8292
8293 ----------------------
8294 -- Add_Extra_Formal --
8295 ----------------------
8296
8297 function Add_Extra_Formal
8298 (Assoc_Entity : Entity_Id;
8299 Typ : Entity_Id;
8300 Scope : Entity_Id;
8301 Suffix : String) return Entity_Id
8302 is
8303 EF : constant Entity_Id :=
8304 Make_Defining_Identifier (Sloc (Assoc_Entity),
8305 Chars => New_External_Name (Chars (Assoc_Entity),
8306 Suffix => Suffix));
8307
8308 begin
8309 -- A little optimization. Never generate an extra formal for the
8310 -- _init operand of an initialization procedure, since it could
8311 -- never be used.
8312
8313 if Chars (Formal) = Name_uInit then
8314 return Empty;
8315 end if;
8316
8317 Set_Ekind (EF, E_In_Parameter);
8318 Set_Actual_Subtype (EF, Typ);
8319 Set_Etype (EF, Typ);
8320 Set_Scope (EF, Scope);
8321 Set_Mechanism (EF, Default_Mechanism);
8322 Set_Formal_Validity (EF);
8323
8324 if No (First_Extra) then
8325 First_Extra := EF;
8326 Set_Extra_Formals (Scope, EF);
8327 end if;
8328
8329 if Present (Last_Extra) then
8330 Set_Extra_Formal (Last_Extra, EF);
8331 end if;
8332
8333 Last_Extra := EF;
8334
8335 return EF;
8336 end Add_Extra_Formal;
8337
8338 -- Local variables
8339
8340 Formal_Type : Entity_Id;
8341 P_Formal : Entity_Id := Empty;
8342
8343 -- Start of processing for Create_Extra_Formals
8344
8345 begin
8346 -- We never generate extra formals if expansion is not active because we
8347 -- don't need them unless we are generating code.
8348
8349 if not Expander_Active then
8350 return;
8351 end if;
8352
8353 -- No need to generate extra formals in interface thunks whose target
8354 -- primitive has no extra formals.
8355
8356 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8357 return;
8358 end if;
8359
8360 -- If this is a derived subprogram then the subtypes of the parent
8361 -- subprogram's formal parameters will be used to determine the need
8362 -- for extra formals.
8363
8364 if Is_Overloadable (E) and then Present (Alias (E)) then
8365 P_Formal := First_Formal (Alias (E));
8366 end if;
8367
8368 Formal := First_Formal (E);
8369 while Present (Formal) loop
8370 Last_Extra := Formal;
8371 Next_Formal (Formal);
8372 end loop;
8373
8374 -- If Extra_Formals were already created, don't do it again. This
8375 -- situation may arise for subprogram types created as part of
8376 -- dispatching calls (see Expand_Dispatching_Call).
8377
8378 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8379 return;
8380 end if;
8381
8382 -- If the subprogram is a predefined dispatching subprogram then don't
8383 -- generate any extra constrained or accessibility level formals. In
8384 -- general we suppress these for internal subprograms (by not calling
8385 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8386 -- generated stream attributes do get passed through because extra
8387 -- build-in-place formals are needed in some cases (limited 'Input).
8388
8389 if Is_Predefined_Internal_Operation (E) then
8390 goto Test_For_Func_Result_Extras;
8391 end if;
8392
8393 Formal := First_Formal (E);
8394 while Present (Formal) loop
8395
8396 -- Create extra formal for supporting the attribute 'Constrained.
8397 -- The case of a private type view without discriminants also
8398 -- requires the extra formal if the underlying type has defaulted
8399 -- discriminants.
8400
8401 if Ekind (Formal) /= E_In_Parameter then
8402 if Present (P_Formal) then
8403 Formal_Type := Etype (P_Formal);
8404 else
8405 Formal_Type := Etype (Formal);
8406 end if;
8407
8408 -- Do not produce extra formals for Unchecked_Union parameters.
8409 -- Jump directly to the end of the loop.
8410
8411 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8412 goto Skip_Extra_Formal_Generation;
8413 end if;
8414
8415 if not Has_Discriminants (Formal_Type)
8416 and then Ekind (Formal_Type) in Private_Kind
8417 and then Present (Underlying_Type (Formal_Type))
8418 then
8419 Formal_Type := Underlying_Type (Formal_Type);
8420 end if;
8421
8422 -- Suppress the extra formal if formal's subtype is constrained or
8423 -- indefinite, or we're compiling for Ada 2012 and the underlying
8424 -- type is tagged and limited. In Ada 2012, a limited tagged type
8425 -- can have defaulted discriminants, but 'Constrained is required
8426 -- to return True, so the formal is never needed (see AI05-0214).
8427 -- Note that this ensures consistency of calling sequences for
8428 -- dispatching operations when some types in a class have defaults
8429 -- on discriminants and others do not (and requiring the extra
8430 -- formal would introduce distributed overhead).
8431
8432 -- If the type does not have a completion yet, treat as prior to
8433 -- Ada 2012 for consistency.
8434
8435 if Has_Discriminants (Formal_Type)
8436 and then not Is_Constrained (Formal_Type)
8437 and then Is_Definite_Subtype (Formal_Type)
8438 and then (Ada_Version < Ada_2012
8439 or else No (Underlying_Type (Formal_Type))
8440 or else not
8441 (Is_Limited_Type (Formal_Type)
8442 and then
8443 (Is_Tagged_Type
8444 (Underlying_Type (Formal_Type)))))
8445 then
8446 Set_Extra_Constrained
8447 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8448 end if;
8449 end if;
8450
8451 -- Create extra formal for supporting accessibility checking. This
8452 -- is done for both anonymous access formals and formals of named
8453 -- access types that are marked as controlling formals. The latter
8454 -- case can occur when Expand_Dispatching_Call creates a subprogram
8455 -- type and substitutes the types of access-to-class-wide actuals
8456 -- for the anonymous access-to-specific-type of controlling formals.
8457 -- Base_Type is applied because in cases where there is a null
8458 -- exclusion the formal may have an access subtype.
8459
8460 -- This is suppressed if we specifically suppress accessibility
8461 -- checks at the package level for either the subprogram, or the
8462 -- package in which it resides. However, we do not suppress it
8463 -- simply if the scope has accessibility checks suppressed, since
8464 -- this could cause trouble when clients are compiled with a
8465 -- different suppression setting. The explicit checks at the
8466 -- package level are safe from this point of view.
8467
8468 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8469 or else (Is_Controlling_Formal (Formal)
8470 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8471 and then not
8472 (Explicit_Suppress (E, Accessibility_Check)
8473 or else
8474 Explicit_Suppress (Scope (E), Accessibility_Check))
8475 and then
8476 (No (P_Formal)
8477 or else Present (Extra_Accessibility (P_Formal)))
8478 then
8479 Set_Extra_Accessibility
8480 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8481 end if;
8482
8483 -- This label is required when skipping extra formal generation for
8484 -- Unchecked_Union parameters.
8485
8486 <<Skip_Extra_Formal_Generation>>
8487
8488 if Present (P_Formal) then
8489 Next_Formal (P_Formal);
8490 end if;
8491
8492 Next_Formal (Formal);
8493 end loop;
8494
8495 <<Test_For_Func_Result_Extras>>
8496
8497 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8498 -- function call is ... determined by the point of call ...".
8499
8500 if Needs_Result_Accessibility_Level (E) then
8501 Set_Extra_Accessibility_Of_Result
8502 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8503 end if;
8504
8505 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8506 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8507
8508 if Is_Build_In_Place_Function (E) then
8509 declare
8510 Result_Subt : constant Entity_Id := Etype (E);
8511 Formal_Typ : Entity_Id;
8512 Subp_Decl : Node_Id;
8513 Discard : Entity_Id;
8514
8515 begin
8516 -- In the case of functions with unconstrained result subtypes,
8517 -- add a 4-state formal indicating whether the return object is
8518 -- allocated by the caller (1), or should be allocated by the
8519 -- callee on the secondary stack (2), in the global heap (3), or
8520 -- in a user-defined storage pool (4). For the moment we just use
8521 -- Natural for the type of this formal. Note that this formal
8522 -- isn't usually needed in the case where the result subtype is
8523 -- constrained, but it is needed when the function has a tagged
8524 -- result, because generally such functions can be called in a
8525 -- dispatching context and such calls must be handled like calls
8526 -- to a class-wide function.
8527
8528 if Needs_BIP_Alloc_Form (E) then
8529 Discard :=
8530 Add_Extra_Formal
8531 (E, Standard_Natural,
8532 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8533
8534 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8535 -- use a user-defined pool. This formal is not added on
8536 -- ZFP as those targets do not support pools.
8537
8538 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8539 Discard :=
8540 Add_Extra_Formal
8541 (E, RTE (RE_Root_Storage_Pool_Ptr),
8542 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8543 end if;
8544 end if;
8545
8546 -- In the case of functions whose result type needs finalization,
8547 -- add an extra formal which represents the finalization master.
8548
8549 if Needs_BIP_Finalization_Master (E) then
8550 Discard :=
8551 Add_Extra_Formal
8552 (E, RTE (RE_Finalization_Master_Ptr),
8553 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8554 end if;
8555
8556 -- When the result type contains tasks, add two extra formals: the
8557 -- master of the tasks to be created, and the caller's activation
8558 -- chain.
8559
8560 if Needs_BIP_Task_Actuals (E) then
8561 Discard :=
8562 Add_Extra_Formal
8563 (E, RTE (RE_Master_Id),
8564 E, BIP_Formal_Suffix (BIP_Task_Master));
8565
8566 Set_Has_Master_Entity (E);
8567
8568 Discard :=
8569 Add_Extra_Formal
8570 (E, RTE (RE_Activation_Chain_Access),
8571 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8572 end if;
8573
8574 -- All build-in-place functions get an extra formal that will be
8575 -- passed the address of the return object within the caller.
8576
8577 Formal_Typ :=
8578 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8579
8580 -- Incomplete_View_From_Limited_With is needed here because
8581 -- gigi gets confused if the designated type is the full view
8582 -- coming from a limited-with'ed package. In the normal case,
8583 -- (no limited with) Incomplete_View_From_Limited_With
8584 -- returns Result_Subt.
8585
8586 Set_Directly_Designated_Type
8587 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
8588 Set_Etype (Formal_Typ, Formal_Typ);
8589 Set_Depends_On_Private
8590 (Formal_Typ, Has_Private_Component (Formal_Typ));
8591 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8592 Set_Is_Access_Constant (Formal_Typ, False);
8593
8594 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8595 -- the designated type comes from the limited view (for back-end
8596 -- purposes).
8597
8598 Set_From_Limited_With
8599 (Formal_Typ, From_Limited_With (Result_Subt));
8600
8601 Layout_Type (Formal_Typ);
8602
8603 -- Force the definition of the Itype in case of internal function
8604 -- calls within the same or nested scope.
8605
8606 if Is_Subprogram_Or_Generic_Subprogram (E) then
8607 Subp_Decl := Parent (E);
8608
8609 -- The insertion point for an Itype reference should be after
8610 -- the unit declaration node of the subprogram. An exception
8611 -- to this are inherited operations from a parent type in which
8612 -- case the derived type acts as their parent.
8613
8614 if Nkind_In (Subp_Decl, N_Function_Specification,
8615 N_Procedure_Specification)
8616 then
8617 Subp_Decl := Parent (Subp_Decl);
8618 end if;
8619
8620 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8621 end if;
8622
8623 Discard :=
8624 Add_Extra_Formal
8625 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8626 end;
8627 end if;
8628
8629 -- If this is an instance of a generic, we need to have extra formals
8630 -- for the Alias.
8631
8632 if Is_Generic_Instance (E) and then Present (Alias (E)) then
8633 Set_Extra_Formals (Alias (E), Extra_Formals (E));
8634 end if;
8635 end Create_Extra_Formals;
8636
8637 -----------------------------
8638 -- Enter_Overloaded_Entity --
8639 -----------------------------
8640
8641 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8642 function Matches_Predefined_Op return Boolean;
8643 -- This returns an approximation of whether S matches a predefined
8644 -- operator, based on the operator symbol, and the parameter and result
8645 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8646
8647 ---------------------------
8648 -- Matches_Predefined_Op --
8649 ---------------------------
8650
8651 function Matches_Predefined_Op return Boolean is
8652 Formal_1 : constant Entity_Id := First_Formal (S);
8653 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8654 Op : constant Name_Id := Chars (S);
8655 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8656 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8657
8658 begin
8659 -- Binary operator
8660
8661 if Present (Formal_2) then
8662 declare
8663 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8664
8665 begin
8666 -- All but "&" and "**" have same-types parameters
8667
8668 case Op is
8669 when Name_Op_Concat
8670 | Name_Op_Expon
8671 =>
8672 null;
8673
8674 when others =>
8675 if Type_1 /= Type_2 then
8676 return False;
8677 end if;
8678 end case;
8679
8680 -- Check parameter and result types
8681
8682 case Op is
8683 when Name_Op_And
8684 | Name_Op_Or
8685 | Name_Op_Xor
8686 =>
8687 return
8688 Is_Boolean_Type (Result_Type)
8689 and then Result_Type = Type_1;
8690
8691 when Name_Op_Mod
8692 | Name_Op_Rem
8693 =>
8694 return
8695 Is_Integer_Type (Result_Type)
8696 and then Result_Type = Type_1;
8697
8698 when Name_Op_Add
8699 | Name_Op_Divide
8700 | Name_Op_Multiply
8701 | Name_Op_Subtract
8702 =>
8703 return
8704 Is_Numeric_Type (Result_Type)
8705 and then Result_Type = Type_1;
8706
8707 when Name_Op_Eq
8708 | Name_Op_Ne
8709 =>
8710 return
8711 Is_Boolean_Type (Result_Type)
8712 and then not Is_Limited_Type (Type_1);
8713
8714 when Name_Op_Ge
8715 | Name_Op_Gt
8716 | Name_Op_Le
8717 | Name_Op_Lt
8718 =>
8719 return
8720 Is_Boolean_Type (Result_Type)
8721 and then (Is_Array_Type (Type_1)
8722 or else Is_Scalar_Type (Type_1));
8723
8724 when Name_Op_Concat =>
8725 return Is_Array_Type (Result_Type);
8726
8727 when Name_Op_Expon =>
8728 return
8729 (Is_Integer_Type (Result_Type)
8730 or else Is_Floating_Point_Type (Result_Type))
8731 and then Result_Type = Type_1
8732 and then Type_2 = Standard_Integer;
8733
8734 when others =>
8735 raise Program_Error;
8736 end case;
8737 end;
8738
8739 -- Unary operator
8740
8741 else
8742 case Op is
8743 when Name_Op_Abs
8744 | Name_Op_Add
8745 | Name_Op_Subtract
8746 =>
8747 return
8748 Is_Numeric_Type (Result_Type)
8749 and then Result_Type = Type_1;
8750
8751 when Name_Op_Not =>
8752 return
8753 Is_Boolean_Type (Result_Type)
8754 and then Result_Type = Type_1;
8755
8756 when others =>
8757 raise Program_Error;
8758 end case;
8759 end if;
8760 end Matches_Predefined_Op;
8761
8762 -- Local variables
8763
8764 E : Entity_Id := Current_Entity_In_Scope (S);
8765 C_E : Entity_Id := Current_Entity (S);
8766
8767 -- Start of processing for Enter_Overloaded_Entity
8768
8769 begin
8770 if Present (E) then
8771 Set_Has_Homonym (E);
8772 Set_Has_Homonym (S);
8773 end if;
8774
8775 Set_Is_Immediately_Visible (S);
8776 Set_Scope (S, Current_Scope);
8777
8778 -- Chain new entity if front of homonym in current scope, so that
8779 -- homonyms are contiguous.
8780
8781 if Present (E) and then E /= C_E then
8782 while Homonym (C_E) /= E loop
8783 C_E := Homonym (C_E);
8784 end loop;
8785
8786 Set_Homonym (C_E, S);
8787
8788 else
8789 E := C_E;
8790 Set_Current_Entity (S);
8791 end if;
8792
8793 Set_Homonym (S, E);
8794
8795 if Is_Inherited_Operation (S) then
8796 Append_Inherited_Subprogram (S);
8797 else
8798 Append_Entity (S, Current_Scope);
8799 end if;
8800
8801 Set_Public_Status (S);
8802
8803 if Debug_Flag_E then
8804 Write_Str ("New overloaded entity chain: ");
8805 Write_Name (Chars (S));
8806
8807 E := S;
8808 while Present (E) loop
8809 Write_Str (" "); Write_Int (Int (E));
8810 E := Homonym (E);
8811 end loop;
8812
8813 Write_Eol;
8814 end if;
8815
8816 -- Generate warning for hiding
8817
8818 if Warn_On_Hiding
8819 and then Comes_From_Source (S)
8820 and then In_Extended_Main_Source_Unit (S)
8821 then
8822 E := S;
8823 loop
8824 E := Homonym (E);
8825 exit when No (E);
8826
8827 -- Warn unless genuine overloading. Do not emit warning on
8828 -- hiding predefined operators in Standard (these are either an
8829 -- (artifact of our implicit declarations, or simple noise) but
8830 -- keep warning on a operator defined on a local subtype, because
8831 -- of the real danger that different operators may be applied in
8832 -- various parts of the program.
8833
8834 -- Note that if E and S have the same scope, there is never any
8835 -- hiding. Either the two conflict, and the program is illegal,
8836 -- or S is overriding an implicit inherited subprogram.
8837
8838 if Scope (E) /= Scope (S)
8839 and then (not Is_Overloadable (E)
8840 or else Subtype_Conformant (E, S))
8841 and then (Is_Immediately_Visible (E)
8842 or else Is_Potentially_Use_Visible (S))
8843 then
8844 if Scope (E) = Standard_Standard then
8845 if Nkind (S) = N_Defining_Operator_Symbol
8846 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
8847 Scope (S)
8848 and then Matches_Predefined_Op
8849 then
8850 Error_Msg_N
8851 ("declaration of & hides predefined operator?h?", S);
8852 end if;
8853
8854 -- E not immediately within Standard
8855
8856 else
8857 Error_Msg_Sloc := Sloc (E);
8858 Error_Msg_N ("declaration of & hides one #?h?", S);
8859 end if;
8860 end if;
8861 end loop;
8862 end if;
8863 end Enter_Overloaded_Entity;
8864
8865 -----------------------------
8866 -- Check_Untagged_Equality --
8867 -----------------------------
8868
8869 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8870 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8871 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8872 Obj_Decl : Node_Id;
8873
8874 begin
8875 -- This check applies only if we have a subprogram declaration with an
8876 -- untagged record type that is conformant to the predefined op.
8877
8878 if Nkind (Decl) /= N_Subprogram_Declaration
8879 or else not Is_Record_Type (Typ)
8880 or else Is_Tagged_Type (Typ)
8881 or else Etype (Next_Formal (First_Formal (Eq_Op))) /= Typ
8882 then
8883 return;
8884 end if;
8885
8886 -- In Ada 2012 case, we will output errors or warnings depending on
8887 -- the setting of debug flag -gnatd.E.
8888
8889 if Ada_Version >= Ada_2012 then
8890 Error_Msg_Warn := Debug_Flag_Dot_EE;
8891
8892 -- In earlier versions of Ada, nothing to do unless we are warning on
8893 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
8894
8895 else
8896 if not Warn_On_Ada_2012_Compatibility then
8897 return;
8898 end if;
8899 end if;
8900
8901 -- Cases where the type has already been frozen
8902
8903 if Is_Frozen (Typ) then
8904
8905 -- The check applies to a primitive operation, so check that type
8906 -- and equality operation are in the same scope.
8907
8908 if Scope (Typ) /= Current_Scope then
8909 return;
8910
8911 -- If the type is a generic actual (sub)type, the operation is not
8912 -- primitive either because the base type is declared elsewhere.
8913
8914 elsif Is_Generic_Actual_Type (Typ) then
8915 return;
8916
8917 -- Here we have a definite error of declaration after freezing
8918
8919 else
8920 if Ada_Version >= Ada_2012 then
8921 Error_Msg_NE
8922 ("equality operator must be declared before type & is "
8923 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
8924
8925 -- In Ada 2012 mode with error turned to warning, output one
8926 -- more warning to warn that the equality operation may not
8927 -- compose. This is the consequence of ignoring the error.
8928
8929 if Error_Msg_Warn then
8930 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
8931 end if;
8932
8933 else
8934 Error_Msg_NE
8935 ("equality operator must be declared before type& is "
8936 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
8937 end if;
8938
8939 -- If we are in the package body, we could just move the
8940 -- declaration to the package spec, so add a message saying that.
8941
8942 if In_Package_Body (Scope (Typ)) then
8943 if Ada_Version >= Ada_2012 then
8944 Error_Msg_N
8945 ("\move declaration to package spec<<", Eq_Op);
8946 else
8947 Error_Msg_N
8948 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
8949 end if;
8950
8951 -- Otherwise try to find the freezing point for better message.
8952
8953 else
8954 Obj_Decl := Next (Parent (Typ));
8955 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
8956 if Nkind (Obj_Decl) = N_Object_Declaration
8957 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8958 then
8959 -- Freezing point, output warnings
8960
8961 if Ada_Version >= Ada_2012 then
8962 Error_Msg_NE
8963 ("type& is frozen by declaration??", Obj_Decl, Typ);
8964 Error_Msg_N
8965 ("\an equality operator cannot be declared after "
8966 & "this point??",
8967 Obj_Decl);
8968 else
8969 Error_Msg_NE
8970 ("type& is frozen by declaration (Ada 2012)?y?",
8971 Obj_Decl, Typ);
8972 Error_Msg_N
8973 ("\an equality operator cannot be declared after "
8974 & "this point (Ada 2012)?y?",
8975 Obj_Decl);
8976 end if;
8977
8978 exit;
8979
8980 -- If we reach generated code for subprogram declaration
8981 -- or body, it is the body that froze the type and the
8982 -- declaration is legal.
8983
8984 elsif Sloc (Obj_Decl) = Sloc (Decl) then
8985 return;
8986 end if;
8987
8988 Next (Obj_Decl);
8989 end loop;
8990 end if;
8991 end if;
8992
8993 -- Here if type is not frozen yet. It is illegal to have a primitive
8994 -- equality declared in the private part if the type is visible.
8995
8996 elsif not In_Same_List (Parent (Typ), Decl)
8997 and then not Is_Limited_Type (Typ)
8998 then
8999 -- Shouldn't we give an RM reference here???
9000
9001 if Ada_Version >= Ada_2012 then
9002 Error_Msg_N
9003 ("equality operator appears too late<<", Eq_Op);
9004 else
9005 Error_Msg_N
9006 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
9007 end if;
9008
9009 -- No error detected
9010
9011 else
9012 return;
9013 end if;
9014 end Check_Untagged_Equality;
9015
9016 -----------------------------
9017 -- Find_Corresponding_Spec --
9018 -----------------------------
9019
9020 function Find_Corresponding_Spec
9021 (N : Node_Id;
9022 Post_Error : Boolean := True) return Entity_Id
9023 is
9024 Spec : constant Node_Id := Specification (N);
9025 Designator : constant Entity_Id := Defining_Entity (Spec);
9026
9027 E : Entity_Id;
9028
9029 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9030 -- Even if fully conformant, a body may depend on a generic actual when
9031 -- the spec does not, or vice versa, in which case they were distinct
9032 -- entities in the generic.
9033
9034 -------------------------------
9035 -- Different_Generic_Profile --
9036 -------------------------------
9037
9038 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9039 F1, F2 : Entity_Id;
9040
9041 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9042 -- Check that the types of corresponding formals have the same
9043 -- generic actual if any. We have to account for subtypes of a
9044 -- generic formal, declared between a spec and a body, which may
9045 -- appear distinct in an instance but matched in the generic, and
9046 -- the subtype may be used either in the spec or the body of the
9047 -- subprogram being checked.
9048
9049 -------------------------
9050 -- Same_Generic_Actual --
9051 -------------------------
9052
9053 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9054
9055 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9056 -- Predicate to check whether S1 is a subtype of S2 in the source
9057 -- of the instance.
9058
9059 -------------------------
9060 -- Is_Declared_Subtype --
9061 -------------------------
9062
9063 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9064 begin
9065 return Comes_From_Source (Parent (S1))
9066 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9067 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9068 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9069 end Is_Declared_Subtype;
9070
9071 -- Start of processing for Same_Generic_Actual
9072
9073 begin
9074 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9075 or else Is_Declared_Subtype (T1, T2)
9076 or else Is_Declared_Subtype (T2, T1);
9077 end Same_Generic_Actual;
9078
9079 -- Start of processing for Different_Generic_Profile
9080
9081 begin
9082 if not In_Instance then
9083 return False;
9084
9085 elsif Ekind (E) = E_Function
9086 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9087 then
9088 return True;
9089 end if;
9090
9091 F1 := First_Formal (Designator);
9092 F2 := First_Formal (E);
9093 while Present (F1) loop
9094 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9095 return True;
9096 end if;
9097
9098 Next_Formal (F1);
9099 Next_Formal (F2);
9100 end loop;
9101
9102 return False;
9103 end Different_Generic_Profile;
9104
9105 -- Start of processing for Find_Corresponding_Spec
9106
9107 begin
9108 E := Current_Entity (Designator);
9109 while Present (E) loop
9110
9111 -- We are looking for a matching spec. It must have the same scope,
9112 -- and the same name, and either be type conformant, or be the case
9113 -- of a library procedure spec and its body (which belong to one
9114 -- another regardless of whether they are type conformant or not).
9115
9116 if Scope (E) = Current_Scope then
9117 if Current_Scope = Standard_Standard
9118 or else (Ekind (E) = Ekind (Designator)
9119 and then Type_Conformant (E, Designator))
9120 then
9121 -- Within an instantiation, we know that spec and body are
9122 -- subtype conformant, because they were subtype conformant in
9123 -- the generic. We choose the subtype-conformant entity here as
9124 -- well, to resolve spurious ambiguities in the instance that
9125 -- were not present in the generic (i.e. when two different
9126 -- types are given the same actual). If we are looking for a
9127 -- spec to match a body, full conformance is expected.
9128
9129 if In_Instance then
9130
9131 -- Inherit the convention and "ghostness" of the matching
9132 -- spec to ensure proper full and subtype conformance.
9133
9134 Set_Convention (Designator, Convention (E));
9135
9136 -- Skip past subprogram bodies and subprogram renamings that
9137 -- may appear to have a matching spec, but that aren't fully
9138 -- conformant with it. That can occur in cases where an
9139 -- actual type causes unrelated homographs in the instance.
9140
9141 if Nkind_In (N, N_Subprogram_Body,
9142 N_Subprogram_Renaming_Declaration)
9143 and then Present (Homonym (E))
9144 and then not Fully_Conformant (Designator, E)
9145 then
9146 goto Next_Entity;
9147
9148 elsif not Subtype_Conformant (Designator, E) then
9149 goto Next_Entity;
9150
9151 elsif Different_Generic_Profile (E) then
9152 goto Next_Entity;
9153 end if;
9154 end if;
9155
9156 -- Ada 2012 (AI05-0165): For internally generated bodies of
9157 -- null procedures locate the internally generated spec. We
9158 -- enforce mode conformance since a tagged type may inherit
9159 -- from interfaces several null primitives which differ only
9160 -- in the mode of the formals.
9161
9162 if not (Comes_From_Source (E))
9163 and then Is_Null_Procedure (E)
9164 and then not Mode_Conformant (Designator, E)
9165 then
9166 null;
9167
9168 -- For null procedures coming from source that are completions,
9169 -- analysis of the generated body will establish the link.
9170
9171 elsif Comes_From_Source (E)
9172 and then Nkind (Spec) = N_Procedure_Specification
9173 and then Null_Present (Spec)
9174 then
9175 return E;
9176
9177 -- Expression functions can be completions, but cannot be
9178 -- completed by an explicit body.
9179
9180 elsif Comes_From_Source (E)
9181 and then Comes_From_Source (N)
9182 and then Nkind (N) = N_Subprogram_Body
9183 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9184 N_Expression_Function
9185 then
9186 Error_Msg_Sloc := Sloc (E);
9187 Error_Msg_N ("body conflicts with expression function#", N);
9188 return Empty;
9189
9190 elsif not Has_Completion (E) then
9191 if Nkind (N) /= N_Subprogram_Body_Stub then
9192 Set_Corresponding_Spec (N, E);
9193 end if;
9194
9195 Set_Has_Completion (E);
9196 return E;
9197
9198 elsif Nkind (Parent (N)) = N_Subunit then
9199
9200 -- If this is the proper body of a subunit, the completion
9201 -- flag is set when analyzing the stub.
9202
9203 return E;
9204
9205 -- If E is an internal function with a controlling result that
9206 -- was created for an operation inherited by a null extension,
9207 -- it may be overridden by a body without a previous spec (one
9208 -- more reason why these should be shunned). In that case we
9209 -- remove the generated body if present, because the current
9210 -- one is the explicit overriding.
9211
9212 elsif Ekind (E) = E_Function
9213 and then Ada_Version >= Ada_2005
9214 and then not Comes_From_Source (E)
9215 and then Has_Controlling_Result (E)
9216 and then Is_Null_Extension (Etype (E))
9217 and then Comes_From_Source (Spec)
9218 then
9219 Set_Has_Completion (E, False);
9220
9221 if Expander_Active
9222 and then Nkind (Parent (E)) = N_Function_Specification
9223 then
9224 Remove
9225 (Unit_Declaration_Node
9226 (Corresponding_Body (Unit_Declaration_Node (E))));
9227
9228 return E;
9229
9230 -- If expansion is disabled, or if the wrapper function has
9231 -- not been generated yet, this a late body overriding an
9232 -- inherited operation, or it is an overriding by some other
9233 -- declaration before the controlling result is frozen. In
9234 -- either case this is a declaration of a new entity.
9235
9236 else
9237 return Empty;
9238 end if;
9239
9240 -- If the body already exists, then this is an error unless
9241 -- the previous declaration is the implicit declaration of a
9242 -- derived subprogram. It is also legal for an instance to
9243 -- contain type conformant overloadable declarations (but the
9244 -- generic declaration may not), per 8.3(26/2).
9245
9246 elsif No (Alias (E))
9247 and then not Is_Intrinsic_Subprogram (E)
9248 and then not In_Instance
9249 and then Post_Error
9250 then
9251 Error_Msg_Sloc := Sloc (E);
9252
9253 if Is_Imported (E) then
9254 Error_Msg_NE
9255 ("body not allowed for imported subprogram & declared#",
9256 N, E);
9257 else
9258 Error_Msg_NE ("duplicate body for & declared#", N, E);
9259 end if;
9260 end if;
9261
9262 -- Child units cannot be overloaded, so a conformance mismatch
9263 -- between body and a previous spec is an error.
9264
9265 elsif Is_Child_Unit (E)
9266 and then
9267 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9268 and then
9269 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9270 N_Compilation_Unit
9271 and then Post_Error
9272 then
9273 Error_Msg_N
9274 ("body of child unit does not match previous declaration", N);
9275 end if;
9276 end if;
9277
9278 <<Next_Entity>>
9279 E := Homonym (E);
9280 end loop;
9281
9282 -- On exit, we know that no previous declaration of subprogram exists
9283
9284 return Empty;
9285 end Find_Corresponding_Spec;
9286
9287 ----------------------
9288 -- Fully_Conformant --
9289 ----------------------
9290
9291 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9292 Result : Boolean;
9293 begin
9294 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9295 return Result;
9296 end Fully_Conformant;
9297
9298 ----------------------------------
9299 -- Fully_Conformant_Expressions --
9300 ----------------------------------
9301
9302 function Fully_Conformant_Expressions
9303 (Given_E1 : Node_Id;
9304 Given_E2 : Node_Id;
9305 Report : Boolean := False) return Boolean
9306 is
9307 E1 : constant Node_Id := Original_Node (Given_E1);
9308 E2 : constant Node_Id := Original_Node (Given_E2);
9309 -- We always test conformance on original nodes, since it is possible
9310 -- for analysis and/or expansion to make things look as though they
9311 -- conform when they do not, e.g. by converting 1+2 into 3.
9312
9313 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9314 -- ???
9315
9316 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9317 -- Compare elements of two lists for conformance. Elements have to be
9318 -- conformant, and actuals inserted as default parameters do not match
9319 -- explicit actuals with the same value.
9320
9321 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9322 -- Compare an operator node with a function call
9323
9324 ---------
9325 -- FCE --
9326 ---------
9327
9328 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
9329 begin
9330 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
9331 end FCE;
9332
9333 ---------
9334 -- FCL --
9335 ---------
9336
9337 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
9338 N1 : Node_Id;
9339 N2 : Node_Id;
9340
9341 begin
9342 if L1 = No_List then
9343 N1 := Empty;
9344 else
9345 N1 := First (L1);
9346 end if;
9347
9348 if L2 = No_List then
9349 N2 := Empty;
9350 else
9351 N2 := First (L2);
9352 end if;
9353
9354 -- Compare two lists, skipping rewrite insertions (we want to compare
9355 -- the original trees, not the expanded versions).
9356
9357 loop
9358 if Is_Rewrite_Insertion (N1) then
9359 Next (N1);
9360 elsif Is_Rewrite_Insertion (N2) then
9361 Next (N2);
9362 elsif No (N1) then
9363 return No (N2);
9364 elsif No (N2) then
9365 return False;
9366 elsif not FCE (N1, N2) then
9367 return False;
9368 else
9369 Next (N1);
9370 Next (N2);
9371 end if;
9372 end loop;
9373 end FCL;
9374
9375 ---------
9376 -- FCO --
9377 ---------
9378
9379 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
9380 Actuals : constant List_Id := Parameter_Associations (Call_Node);
9381 Act : Node_Id;
9382
9383 begin
9384 if No (Actuals)
9385 or else Entity (Op_Node) /= Entity (Name (Call_Node))
9386 then
9387 return False;
9388
9389 else
9390 Act := First (Actuals);
9391
9392 if Nkind (Op_Node) in N_Binary_Op then
9393 if not FCE (Left_Opnd (Op_Node), Act) then
9394 return False;
9395 end if;
9396
9397 Next (Act);
9398 end if;
9399
9400 return Present (Act)
9401 and then FCE (Right_Opnd (Op_Node), Act)
9402 and then No (Next (Act));
9403 end if;
9404 end FCO;
9405
9406 -- Local variables
9407
9408 Result : Boolean;
9409
9410 -- Start of processing for Fully_Conformant_Expressions
9411
9412 begin
9413 Result := True;
9414
9415 -- Nonconformant if paren count does not match. Note: if some idiot
9416 -- complains that we don't do this right for more than 3 levels of
9417 -- parentheses, they will be treated with the respect they deserve.
9418
9419 if Paren_Count (E1) /= Paren_Count (E2) then
9420 return False;
9421
9422 -- If same entities are referenced, then they are conformant even if
9423 -- they have different forms (RM 8.3.1(19-20)).
9424
9425 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9426 if Present (Entity (E1)) then
9427 Result := Entity (E1) = Entity (E2)
9428
9429 -- One may be a discriminant that has been replaced by the
9430 -- corresponding discriminal.
9431
9432 or else
9433 (Chars (Entity (E1)) = Chars (Entity (E2))
9434 and then Ekind (Entity (E1)) = E_Discriminant
9435 and then Ekind (Entity (E2)) = E_In_Parameter)
9436
9437 -- The discriminant of a protected type is transformed into
9438 -- a local constant and then into a parameter of a protected
9439 -- operation.
9440
9441 or else
9442 (Ekind (Entity (E1)) = E_Constant
9443 and then Ekind (Entity (E2)) = E_In_Parameter
9444 and then Present (Discriminal_Link (Entity (E1)))
9445 and then Discriminal_Link (Entity (E1)) =
9446 Discriminal_Link (Entity (E2)))
9447
9448 -- AI12-050: The loop variables of quantified expressions match
9449 -- if they have the same identifier, even though they may have
9450 -- different entities.
9451
9452 or else
9453 (Chars (Entity (E1)) = Chars (Entity (E2))
9454 and then Ekind (Entity (E1)) = E_Loop_Parameter
9455 and then Ekind (Entity (E2)) = E_Loop_Parameter)
9456
9457 -- A call to an instantiation of Unchecked_Conversion is
9458 -- rewritten with the name of the generated function created for
9459 -- the instance, and this must be special-cased.
9460
9461 or else
9462 (Ekind (Entity (E1)) = E_Function
9463 and then Is_Intrinsic_Subprogram (Entity (E1))
9464 and then Is_Generic_Instance (Entity (E1))
9465 and then Entity (E2) = Alias (Entity (E1)));
9466 if Report and not Result then
9467 Error_Msg_Sloc :=
9468 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
9469 Error_Msg_NE
9470 ("Meaning of& differs because of declaration#", E1, E2);
9471 end if;
9472
9473 return Result;
9474
9475 elsif Nkind (E1) = N_Expanded_Name
9476 and then Nkind (E2) = N_Expanded_Name
9477 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9478 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9479 then
9480 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9481
9482 else
9483 -- Identifiers in component associations don't always have
9484 -- entities, but their names must conform.
9485
9486 return Nkind (E1) = N_Identifier
9487 and then Nkind (E2) = N_Identifier
9488 and then Chars (E1) = Chars (E2);
9489 end if;
9490
9491 elsif Nkind (E1) = N_Character_Literal
9492 and then Nkind (E2) = N_Expanded_Name
9493 then
9494 return Nkind (Selector_Name (E2)) = N_Character_Literal
9495 and then Chars (E1) = Chars (Selector_Name (E2));
9496
9497 elsif Nkind (E2) = N_Character_Literal
9498 and then Nkind (E1) = N_Expanded_Name
9499 then
9500 return Nkind (Selector_Name (E1)) = N_Character_Literal
9501 and then Chars (E2) = Chars (Selector_Name (E1));
9502
9503 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9504 return FCO (E1, E2);
9505
9506 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9507 return FCO (E2, E1);
9508
9509 -- Otherwise we must have the same syntactic entity
9510
9511 elsif Nkind (E1) /= Nkind (E2) then
9512 return False;
9513
9514 -- At this point, we specialize by node type
9515
9516 else
9517 case Nkind (E1) is
9518 when N_Aggregate =>
9519 return
9520 FCL (Expressions (E1), Expressions (E2))
9521 and then
9522 FCL (Component_Associations (E1),
9523 Component_Associations (E2));
9524
9525 when N_Allocator =>
9526 if Nkind (Expression (E1)) = N_Qualified_Expression
9527 or else
9528 Nkind (Expression (E2)) = N_Qualified_Expression
9529 then
9530 return FCE (Expression (E1), Expression (E2));
9531
9532 -- Check that the subtype marks and any constraints
9533 -- are conformant
9534
9535 else
9536 declare
9537 Indic1 : constant Node_Id := Expression (E1);
9538 Indic2 : constant Node_Id := Expression (E2);
9539 Elt1 : Node_Id;
9540 Elt2 : Node_Id;
9541
9542 begin
9543 if Nkind (Indic1) /= N_Subtype_Indication then
9544 return
9545 Nkind (Indic2) /= N_Subtype_Indication
9546 and then Entity (Indic1) = Entity (Indic2);
9547
9548 elsif Nkind (Indic2) /= N_Subtype_Indication then
9549 return
9550 Nkind (Indic1) /= N_Subtype_Indication
9551 and then Entity (Indic1) = Entity (Indic2);
9552
9553 else
9554 if Entity (Subtype_Mark (Indic1)) /=
9555 Entity (Subtype_Mark (Indic2))
9556 then
9557 return False;
9558 end if;
9559
9560 Elt1 := First (Constraints (Constraint (Indic1)));
9561 Elt2 := First (Constraints (Constraint (Indic2)));
9562 while Present (Elt1) and then Present (Elt2) loop
9563 if not FCE (Elt1, Elt2) then
9564 return False;
9565 end if;
9566
9567 Next (Elt1);
9568 Next (Elt2);
9569 end loop;
9570
9571 return True;
9572 end if;
9573 end;
9574 end if;
9575
9576 when N_Attribute_Reference =>
9577 return
9578 Attribute_Name (E1) = Attribute_Name (E2)
9579 and then FCL (Expressions (E1), Expressions (E2));
9580
9581 when N_Binary_Op =>
9582 return
9583 Entity (E1) = Entity (E2)
9584 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9585 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9586
9587 when N_Membership_Test
9588 | N_Short_Circuit
9589 =>
9590 return
9591 FCE (Left_Opnd (E1), Left_Opnd (E2))
9592 and then
9593 FCE (Right_Opnd (E1), Right_Opnd (E2));
9594
9595 when N_Case_Expression =>
9596 declare
9597 Alt1 : Node_Id;
9598 Alt2 : Node_Id;
9599
9600 begin
9601 if not FCE (Expression (E1), Expression (E2)) then
9602 return False;
9603
9604 else
9605 Alt1 := First (Alternatives (E1));
9606 Alt2 := First (Alternatives (E2));
9607 loop
9608 if Present (Alt1) /= Present (Alt2) then
9609 return False;
9610 elsif No (Alt1) then
9611 return True;
9612 end if;
9613
9614 if not FCE (Expression (Alt1), Expression (Alt2))
9615 or else not FCL (Discrete_Choices (Alt1),
9616 Discrete_Choices (Alt2))
9617 then
9618 return False;
9619 end if;
9620
9621 Next (Alt1);
9622 Next (Alt2);
9623 end loop;
9624 end if;
9625 end;
9626
9627 when N_Character_Literal =>
9628 return
9629 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9630
9631 when N_Component_Association =>
9632 return
9633 FCL (Choices (E1), Choices (E2))
9634 and then
9635 FCE (Expression (E1), Expression (E2));
9636
9637 when N_Explicit_Dereference =>
9638 return
9639 FCE (Prefix (E1), Prefix (E2));
9640
9641 when N_Extension_Aggregate =>
9642 return
9643 FCL (Expressions (E1), Expressions (E2))
9644 and then Null_Record_Present (E1) =
9645 Null_Record_Present (E2)
9646 and then FCL (Component_Associations (E1),
9647 Component_Associations (E2));
9648
9649 when N_Function_Call =>
9650 return
9651 FCE (Name (E1), Name (E2))
9652 and then
9653 FCL (Parameter_Associations (E1),
9654 Parameter_Associations (E2));
9655
9656 when N_If_Expression =>
9657 return
9658 FCL (Expressions (E1), Expressions (E2));
9659
9660 when N_Indexed_Component =>
9661 return
9662 FCE (Prefix (E1), Prefix (E2))
9663 and then
9664 FCL (Expressions (E1), Expressions (E2));
9665
9666 when N_Integer_Literal =>
9667 return (Intval (E1) = Intval (E2));
9668
9669 when N_Null =>
9670 return True;
9671
9672 when N_Operator_Symbol =>
9673 return
9674 Chars (E1) = Chars (E2);
9675
9676 when N_Others_Choice =>
9677 return True;
9678
9679 when N_Parameter_Association =>
9680 return
9681 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9682 and then FCE (Explicit_Actual_Parameter (E1),
9683 Explicit_Actual_Parameter (E2));
9684
9685 when N_Qualified_Expression
9686 | N_Type_Conversion
9687 | N_Unchecked_Type_Conversion
9688 =>
9689 return
9690 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9691 and then
9692 FCE (Expression (E1), Expression (E2));
9693
9694 when N_Quantified_Expression =>
9695 if not FCE (Condition (E1), Condition (E2)) then
9696 return False;
9697 end if;
9698
9699 if Present (Loop_Parameter_Specification (E1))
9700 and then Present (Loop_Parameter_Specification (E2))
9701 then
9702 declare
9703 L1 : constant Node_Id :=
9704 Loop_Parameter_Specification (E1);
9705 L2 : constant Node_Id :=
9706 Loop_Parameter_Specification (E2);
9707
9708 begin
9709 return
9710 Reverse_Present (L1) = Reverse_Present (L2)
9711 and then
9712 FCE (Defining_Identifier (L1),
9713 Defining_Identifier (L2))
9714 and then
9715 FCE (Discrete_Subtype_Definition (L1),
9716 Discrete_Subtype_Definition (L2));
9717 end;
9718
9719 elsif Present (Iterator_Specification (E1))
9720 and then Present (Iterator_Specification (E2))
9721 then
9722 declare
9723 I1 : constant Node_Id := Iterator_Specification (E1);
9724 I2 : constant Node_Id := Iterator_Specification (E2);
9725
9726 begin
9727 return
9728 FCE (Defining_Identifier (I1),
9729 Defining_Identifier (I2))
9730 and then
9731 Of_Present (I1) = Of_Present (I2)
9732 and then
9733 Reverse_Present (I1) = Reverse_Present (I2)
9734 and then FCE (Name (I1), Name (I2))
9735 and then FCE (Subtype_Indication (I1),
9736 Subtype_Indication (I2));
9737 end;
9738
9739 -- The quantified expressions used different specifications to
9740 -- walk their respective ranges.
9741
9742 else
9743 return False;
9744 end if;
9745
9746 when N_Range =>
9747 return
9748 FCE (Low_Bound (E1), Low_Bound (E2))
9749 and then
9750 FCE (High_Bound (E1), High_Bound (E2));
9751
9752 when N_Real_Literal =>
9753 return (Realval (E1) = Realval (E2));
9754
9755 when N_Selected_Component =>
9756 return
9757 FCE (Prefix (E1), Prefix (E2))
9758 and then
9759 FCE (Selector_Name (E1), Selector_Name (E2));
9760
9761 when N_Slice =>
9762 return
9763 FCE (Prefix (E1), Prefix (E2))
9764 and then
9765 FCE (Discrete_Range (E1), Discrete_Range (E2));
9766
9767 when N_String_Literal =>
9768 declare
9769 S1 : constant String_Id := Strval (E1);
9770 S2 : constant String_Id := Strval (E2);
9771 L1 : constant Nat := String_Length (S1);
9772 L2 : constant Nat := String_Length (S2);
9773
9774 begin
9775 if L1 /= L2 then
9776 return False;
9777
9778 else
9779 for J in 1 .. L1 loop
9780 if Get_String_Char (S1, J) /=
9781 Get_String_Char (S2, J)
9782 then
9783 return False;
9784 end if;
9785 end loop;
9786
9787 return True;
9788 end if;
9789 end;
9790
9791 when N_Unary_Op =>
9792 return
9793 Entity (E1) = Entity (E2)
9794 and then
9795 FCE (Right_Opnd (E1), Right_Opnd (E2));
9796
9797 -- All other node types cannot appear in this context. Strictly
9798 -- we should raise a fatal internal error. Instead we just ignore
9799 -- the nodes. This means that if anyone makes a mistake in the
9800 -- expander and mucks an expression tree irretrievably, the result
9801 -- will be a failure to detect a (probably very obscure) case
9802 -- of non-conformance, which is better than bombing on some
9803 -- case where two expressions do in fact conform.
9804
9805 when others =>
9806 return True;
9807 end case;
9808 end if;
9809 end Fully_Conformant_Expressions;
9810
9811 ----------------------------------------
9812 -- Fully_Conformant_Discrete_Subtypes --
9813 ----------------------------------------
9814
9815 function Fully_Conformant_Discrete_Subtypes
9816 (Given_S1 : Node_Id;
9817 Given_S2 : Node_Id) return Boolean
9818 is
9819 S1 : constant Node_Id := Original_Node (Given_S1);
9820 S2 : constant Node_Id := Original_Node (Given_S2);
9821
9822 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
9823 -- Special-case for a bound given by a discriminant, which in the body
9824 -- is replaced with the discriminal of the enclosing type.
9825
9826 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
9827 -- Check both bounds
9828
9829 -----------------------
9830 -- Conforming_Bounds --
9831 -----------------------
9832
9833 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
9834 begin
9835 if Is_Entity_Name (B1)
9836 and then Is_Entity_Name (B2)
9837 and then Ekind (Entity (B1)) = E_Discriminant
9838 then
9839 return Chars (B1) = Chars (B2);
9840
9841 else
9842 return Fully_Conformant_Expressions (B1, B2);
9843 end if;
9844 end Conforming_Bounds;
9845
9846 -----------------------
9847 -- Conforming_Ranges --
9848 -----------------------
9849
9850 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
9851 begin
9852 return
9853 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
9854 and then
9855 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
9856 end Conforming_Ranges;
9857
9858 -- Start of processing for Fully_Conformant_Discrete_Subtypes
9859
9860 begin
9861 if Nkind (S1) /= Nkind (S2) then
9862 return False;
9863
9864 elsif Is_Entity_Name (S1) then
9865 return Entity (S1) = Entity (S2);
9866
9867 elsif Nkind (S1) = N_Range then
9868 return Conforming_Ranges (S1, S2);
9869
9870 elsif Nkind (S1) = N_Subtype_Indication then
9871 return
9872 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
9873 and then
9874 Conforming_Ranges
9875 (Range_Expression (Constraint (S1)),
9876 Range_Expression (Constraint (S2)));
9877 else
9878 return True;
9879 end if;
9880 end Fully_Conformant_Discrete_Subtypes;
9881
9882 --------------------
9883 -- Install_Entity --
9884 --------------------
9885
9886 procedure Install_Entity (E : Entity_Id) is
9887 Prev : constant Entity_Id := Current_Entity (E);
9888 begin
9889 Set_Is_Immediately_Visible (E);
9890 Set_Current_Entity (E);
9891 Set_Homonym (E, Prev);
9892 end Install_Entity;
9893
9894 ---------------------
9895 -- Install_Formals --
9896 ---------------------
9897
9898 procedure Install_Formals (Id : Entity_Id) is
9899 F : Entity_Id;
9900 begin
9901 F := First_Formal (Id);
9902 while Present (F) loop
9903 Install_Entity (F);
9904 Next_Formal (F);
9905 end loop;
9906 end Install_Formals;
9907
9908 -----------------------------
9909 -- Is_Interface_Conformant --
9910 -----------------------------
9911
9912 function Is_Interface_Conformant
9913 (Tagged_Type : Entity_Id;
9914 Iface_Prim : Entity_Id;
9915 Prim : Entity_Id) return Boolean
9916 is
9917 -- The operation may in fact be an inherited (implicit) operation
9918 -- rather than the original interface primitive, so retrieve the
9919 -- ultimate ancestor.
9920
9921 Iface : constant Entity_Id :=
9922 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
9923 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
9924
9925 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
9926 -- Return the controlling formal of Prim
9927
9928 ------------------------
9929 -- Controlling_Formal --
9930 ------------------------
9931
9932 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
9933 E : Entity_Id;
9934
9935 begin
9936 E := First_Entity (Prim);
9937 while Present (E) loop
9938 if Is_Formal (E) and then Is_Controlling_Formal (E) then
9939 return E;
9940 end if;
9941
9942 Next_Entity (E);
9943 end loop;
9944
9945 return Empty;
9946 end Controlling_Formal;
9947
9948 -- Local variables
9949
9950 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
9951 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
9952
9953 -- Start of processing for Is_Interface_Conformant
9954
9955 begin
9956 pragma Assert (Is_Subprogram (Iface_Prim)
9957 and then Is_Subprogram (Prim)
9958 and then Is_Dispatching_Operation (Iface_Prim)
9959 and then Is_Dispatching_Operation (Prim));
9960
9961 pragma Assert (Is_Interface (Iface)
9962 or else (Present (Alias (Iface_Prim))
9963 and then
9964 Is_Interface
9965 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
9966
9967 if Prim = Iface_Prim
9968 or else not Is_Subprogram (Prim)
9969 or else Ekind (Prim) /= Ekind (Iface_Prim)
9970 or else not Is_Dispatching_Operation (Prim)
9971 or else Scope (Prim) /= Scope (Tagged_Type)
9972 or else No (Typ)
9973 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
9974 or else not Primitive_Names_Match (Iface_Prim, Prim)
9975 then
9976 return False;
9977
9978 -- The mode of the controlling formals must match
9979
9980 elsif Present (Iface_Ctrl_F)
9981 and then Present (Prim_Ctrl_F)
9982 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
9983 then
9984 return False;
9985
9986 -- Case of a procedure, or a function whose result type matches the
9987 -- result type of the interface primitive, or a function that has no
9988 -- controlling result (I or access I).
9989
9990 elsif Ekind (Iface_Prim) = E_Procedure
9991 or else Etype (Prim) = Etype (Iface_Prim)
9992 or else not Has_Controlling_Result (Prim)
9993 then
9994 return Type_Conformant
9995 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
9996
9997 -- Case of a function returning an interface, or an access to one. Check
9998 -- that the return types correspond.
9999
10000 elsif Implements_Interface (Typ, Iface) then
10001 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10002 /=
10003 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10004 then
10005 return False;
10006 else
10007 return
10008 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10009 Skip_Controlling_Formals => True);
10010 end if;
10011
10012 else
10013 return False;
10014 end if;
10015 end Is_Interface_Conformant;
10016
10017 ---------------------------------
10018 -- Is_Non_Overriding_Operation --
10019 ---------------------------------
10020
10021 function Is_Non_Overriding_Operation
10022 (Prev_E : Entity_Id;
10023 New_E : Entity_Id) return Boolean
10024 is
10025 Formal : Entity_Id;
10026 F_Typ : Entity_Id;
10027 G_Typ : Entity_Id := Empty;
10028
10029 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10030 -- If F_Type is a derived type associated with a generic actual subtype,
10031 -- then return its Generic_Parent_Type attribute, else return Empty.
10032
10033 function Types_Correspond
10034 (P_Type : Entity_Id;
10035 N_Type : Entity_Id) return Boolean;
10036 -- Returns true if and only if the types (or designated types in the
10037 -- case of anonymous access types) are the same or N_Type is derived
10038 -- directly or indirectly from P_Type.
10039
10040 -----------------------------
10041 -- Get_Generic_Parent_Type --
10042 -----------------------------
10043
10044 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10045 G_Typ : Entity_Id;
10046 Defn : Node_Id;
10047 Indic : Node_Id;
10048
10049 begin
10050 if Is_Derived_Type (F_Typ)
10051 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10052 then
10053 -- The tree must be traversed to determine the parent subtype in
10054 -- the generic unit, which unfortunately isn't always available
10055 -- via semantic attributes. ??? (Note: The use of Original_Node
10056 -- is needed for cases where a full derived type has been
10057 -- rewritten.)
10058
10059 -- If the parent type is a scalar type, the derivation creates
10060 -- an anonymous base type for it, and the source type is its
10061 -- first subtype.
10062
10063 if Is_Scalar_Type (F_Typ)
10064 and then not Comes_From_Source (F_Typ)
10065 then
10066 Defn :=
10067 Type_Definition
10068 (Original_Node (Parent (First_Subtype (F_Typ))));
10069 else
10070 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10071 end if;
10072 if Nkind (Defn) = N_Derived_Type_Definition then
10073 Indic := Subtype_Indication (Defn);
10074
10075 if Nkind (Indic) = N_Subtype_Indication then
10076 G_Typ := Entity (Subtype_Mark (Indic));
10077 else
10078 G_Typ := Entity (Indic);
10079 end if;
10080
10081 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10082 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10083 then
10084 return Generic_Parent_Type (Parent (G_Typ));
10085 end if;
10086 end if;
10087 end if;
10088
10089 return Empty;
10090 end Get_Generic_Parent_Type;
10091
10092 ----------------------
10093 -- Types_Correspond --
10094 ----------------------
10095
10096 function Types_Correspond
10097 (P_Type : Entity_Id;
10098 N_Type : Entity_Id) return Boolean
10099 is
10100 Prev_Type : Entity_Id := Base_Type (P_Type);
10101 New_Type : Entity_Id := Base_Type (N_Type);
10102
10103 begin
10104 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10105 Prev_Type := Designated_Type (Prev_Type);
10106 end if;
10107
10108 if Ekind (New_Type) = E_Anonymous_Access_Type then
10109 New_Type := Designated_Type (New_Type);
10110 end if;
10111
10112 if Prev_Type = New_Type then
10113 return True;
10114
10115 elsif not Is_Class_Wide_Type (New_Type) then
10116 while Etype (New_Type) /= New_Type loop
10117 New_Type := Etype (New_Type);
10118
10119 if New_Type = Prev_Type then
10120 return True;
10121 end if;
10122 end loop;
10123 end if;
10124 return False;
10125 end Types_Correspond;
10126
10127 -- Start of processing for Is_Non_Overriding_Operation
10128
10129 begin
10130 -- In the case where both operations are implicit derived subprograms
10131 -- then neither overrides the other. This can only occur in certain
10132 -- obscure cases (e.g., derivation from homographs created in a generic
10133 -- instantiation).
10134
10135 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10136 return True;
10137
10138 elsif Ekind (Current_Scope) = E_Package
10139 and then Is_Generic_Instance (Current_Scope)
10140 and then In_Private_Part (Current_Scope)
10141 and then Comes_From_Source (New_E)
10142 then
10143 -- We examine the formals and result type of the inherited operation,
10144 -- to determine whether their type is derived from (the instance of)
10145 -- a generic type. The first such formal or result type is the one
10146 -- tested.
10147
10148 Formal := First_Formal (Prev_E);
10149 F_Typ := Empty;
10150 while Present (Formal) loop
10151 F_Typ := Base_Type (Etype (Formal));
10152
10153 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10154 F_Typ := Designated_Type (F_Typ);
10155 end if;
10156
10157 G_Typ := Get_Generic_Parent_Type (F_Typ);
10158 exit when Present (G_Typ);
10159
10160 Next_Formal (Formal);
10161 end loop;
10162
10163 -- If the function dispatches on result check the result type
10164
10165 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10166 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10167 end if;
10168
10169 if No (G_Typ) then
10170 return False;
10171 end if;
10172
10173 -- If the generic type is a private type, then the original operation
10174 -- was not overriding in the generic, because there was no primitive
10175 -- operation to override.
10176
10177 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10178 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10179 N_Formal_Private_Type_Definition
10180 then
10181 return True;
10182
10183 -- The generic parent type is the ancestor of a formal derived
10184 -- type declaration. We need to check whether it has a primitive
10185 -- operation that should be overridden by New_E in the generic.
10186
10187 else
10188 declare
10189 P_Formal : Entity_Id;
10190 N_Formal : Entity_Id;
10191 P_Typ : Entity_Id;
10192 N_Typ : Entity_Id;
10193 P_Prim : Entity_Id;
10194 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10195
10196 begin
10197 while Present (Prim_Elt) loop
10198 P_Prim := Node (Prim_Elt);
10199
10200 if Chars (P_Prim) = Chars (New_E)
10201 and then Ekind (P_Prim) = Ekind (New_E)
10202 then
10203 P_Formal := First_Formal (P_Prim);
10204 N_Formal := First_Formal (New_E);
10205 while Present (P_Formal) and then Present (N_Formal) loop
10206 P_Typ := Etype (P_Formal);
10207 N_Typ := Etype (N_Formal);
10208
10209 if not Types_Correspond (P_Typ, N_Typ) then
10210 exit;
10211 end if;
10212
10213 Next_Entity (P_Formal);
10214 Next_Entity (N_Formal);
10215 end loop;
10216
10217 -- Found a matching primitive operation belonging to the
10218 -- formal ancestor type, so the new subprogram is
10219 -- overriding.
10220
10221 if No (P_Formal)
10222 and then No (N_Formal)
10223 and then (Ekind (New_E) /= E_Function
10224 or else
10225 Types_Correspond
10226 (Etype (P_Prim), Etype (New_E)))
10227 then
10228 return False;
10229 end if;
10230 end if;
10231
10232 Next_Elmt (Prim_Elt);
10233 end loop;
10234
10235 -- If no match found, then the new subprogram does not override
10236 -- in the generic (nor in the instance).
10237
10238 -- If the type in question is not abstract, and the subprogram
10239 -- is, this will be an error if the new operation is in the
10240 -- private part of the instance. Emit a warning now, which will
10241 -- make the subsequent error message easier to understand.
10242
10243 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
10244 and then Is_Abstract_Subprogram (Prev_E)
10245 and then In_Private_Part (Current_Scope)
10246 then
10247 Error_Msg_Node_2 := F_Typ;
10248 Error_Msg_NE
10249 ("private operation& in generic unit does not override "
10250 & "any primitive operation of& (RM 12.3 (18))??",
10251 New_E, New_E);
10252 end if;
10253
10254 return True;
10255 end;
10256 end if;
10257 else
10258 return False;
10259 end if;
10260 end Is_Non_Overriding_Operation;
10261
10262 -------------------------------------
10263 -- List_Inherited_Pre_Post_Aspects --
10264 -------------------------------------
10265
10266 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
10267 begin
10268 if Opt.List_Inherited_Aspects
10269 and then Is_Subprogram_Or_Generic_Subprogram (E)
10270 then
10271 declare
10272 Subps : constant Subprogram_List := Inherited_Subprograms (E);
10273 Items : Node_Id;
10274 Prag : Node_Id;
10275
10276 begin
10277 for Index in Subps'Range loop
10278 Items := Contract (Subps (Index));
10279
10280 if Present (Items) then
10281 Prag := Pre_Post_Conditions (Items);
10282 while Present (Prag) loop
10283 Error_Msg_Sloc := Sloc (Prag);
10284
10285 if Class_Present (Prag)
10286 and then not Split_PPC (Prag)
10287 then
10288 if Pragma_Name (Prag) = Name_Precondition then
10289 Error_Msg_N
10290 ("info: & inherits `Pre''Class` aspect from "
10291 & "#?L?", E);
10292 else
10293 Error_Msg_N
10294 ("info: & inherits `Post''Class` aspect from "
10295 & "#?L?", E);
10296 end if;
10297 end if;
10298
10299 Prag := Next_Pragma (Prag);
10300 end loop;
10301 end if;
10302 end loop;
10303 end;
10304 end if;
10305 end List_Inherited_Pre_Post_Aspects;
10306
10307 ------------------------------
10308 -- Make_Inequality_Operator --
10309 ------------------------------
10310
10311 -- S is the defining identifier of an equality operator. We build a
10312 -- subprogram declaration with the right signature. This operation is
10313 -- intrinsic, because it is always expanded as the negation of the
10314 -- call to the equality function.
10315
10316 procedure Make_Inequality_Operator (S : Entity_Id) is
10317 Loc : constant Source_Ptr := Sloc (S);
10318 Decl : Node_Id;
10319 Formals : List_Id;
10320 Op_Name : Entity_Id;
10321
10322 FF : constant Entity_Id := First_Formal (S);
10323 NF : constant Entity_Id := Next_Formal (FF);
10324
10325 begin
10326 -- Check that equality was properly defined, ignore call if not
10327
10328 if No (NF) then
10329 return;
10330 end if;
10331
10332 declare
10333 A : constant Entity_Id :=
10334 Make_Defining_Identifier (Sloc (FF),
10335 Chars => Chars (FF));
10336
10337 B : constant Entity_Id :=
10338 Make_Defining_Identifier (Sloc (NF),
10339 Chars => Chars (NF));
10340
10341 begin
10342 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
10343
10344 Formals := New_List (
10345 Make_Parameter_Specification (Loc,
10346 Defining_Identifier => A,
10347 Parameter_Type =>
10348 New_Occurrence_Of (Etype (First_Formal (S)),
10349 Sloc (Etype (First_Formal (S))))),
10350
10351 Make_Parameter_Specification (Loc,
10352 Defining_Identifier => B,
10353 Parameter_Type =>
10354 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
10355 Sloc (Etype (Next_Formal (First_Formal (S)))))));
10356
10357 Decl :=
10358 Make_Subprogram_Declaration (Loc,
10359 Specification =>
10360 Make_Function_Specification (Loc,
10361 Defining_Unit_Name => Op_Name,
10362 Parameter_Specifications => Formals,
10363 Result_Definition =>
10364 New_Occurrence_Of (Standard_Boolean, Loc)));
10365
10366 -- Insert inequality right after equality if it is explicit or after
10367 -- the derived type when implicit. These entities are created only
10368 -- for visibility purposes, and eventually replaced in the course
10369 -- of expansion, so they do not need to be attached to the tree and
10370 -- seen by the back-end. Keeping them internal also avoids spurious
10371 -- freezing problems. The declaration is inserted in the tree for
10372 -- analysis, and removed afterwards. If the equality operator comes
10373 -- from an explicit declaration, attach the inequality immediately
10374 -- after. Else the equality is inherited from a derived type
10375 -- declaration, so insert inequality after that declaration.
10376
10377 if No (Alias (S)) then
10378 Insert_After (Unit_Declaration_Node (S), Decl);
10379 elsif Is_List_Member (Parent (S)) then
10380 Insert_After (Parent (S), Decl);
10381 else
10382 Insert_After (Parent (Etype (First_Formal (S))), Decl);
10383 end if;
10384
10385 Mark_Rewrite_Insertion (Decl);
10386 Set_Is_Intrinsic_Subprogram (Op_Name);
10387 Analyze (Decl);
10388 Remove (Decl);
10389 Set_Has_Completion (Op_Name);
10390 Set_Corresponding_Equality (Op_Name, S);
10391 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
10392 end;
10393 end Make_Inequality_Operator;
10394
10395 ----------------------
10396 -- May_Need_Actuals --
10397 ----------------------
10398
10399 procedure May_Need_Actuals (Fun : Entity_Id) is
10400 F : Entity_Id;
10401 B : Boolean;
10402
10403 begin
10404 F := First_Formal (Fun);
10405 B := True;
10406 while Present (F) loop
10407 if No (Default_Value (F)) then
10408 B := False;
10409 exit;
10410 end if;
10411
10412 Next_Formal (F);
10413 end loop;
10414
10415 Set_Needs_No_Actuals (Fun, B);
10416 end May_Need_Actuals;
10417
10418 ---------------------
10419 -- Mode_Conformant --
10420 ---------------------
10421
10422 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10423 Result : Boolean;
10424 begin
10425 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
10426 return Result;
10427 end Mode_Conformant;
10428
10429 ---------------------------
10430 -- New_Overloaded_Entity --
10431 ---------------------------
10432
10433 procedure New_Overloaded_Entity
10434 (S : Entity_Id;
10435 Derived_Type : Entity_Id := Empty)
10436 is
10437 Overridden_Subp : Entity_Id := Empty;
10438 -- Set if the current scope has an operation that is type-conformant
10439 -- with S, and becomes hidden by S.
10440
10441 Is_Primitive_Subp : Boolean;
10442 -- Set to True if the new subprogram is primitive
10443
10444 E : Entity_Id;
10445 -- Entity that S overrides
10446
10447 procedure Check_For_Primitive_Subprogram
10448 (Is_Primitive : out Boolean;
10449 Is_Overriding : Boolean := False);
10450 -- If the subprogram being analyzed is a primitive operation of the type
10451 -- of a formal or result, set the Has_Primitive_Operations flag on the
10452 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10453 -- corresponding flag on the entity itself for later use.
10454
10455 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10456 -- True if a) E is a subprogram whose first formal is a concurrent type
10457 -- defined in the scope of E that has some entry or subprogram whose
10458 -- profile matches E, or b) E is an internally built dispatching
10459 -- subprogram of a protected type and there is a matching subprogram
10460 -- defined in the enclosing scope of the protected type, or c) E is
10461 -- an entry of a synchronized type and a matching procedure has been
10462 -- previously defined in the enclosing scope of the synchronized type.
10463
10464 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10465 -- Check that E is declared in the private part of the current package,
10466 -- or in the package body, where it may hide a previous declaration.
10467 -- We can't use In_Private_Part by itself because this flag is also
10468 -- set when freezing entities, so we must examine the place of the
10469 -- declaration in the tree, and recognize wrapper packages as well.
10470
10471 function Is_Overriding_Alias
10472 (Old_E : Entity_Id;
10473 New_E : Entity_Id) return Boolean;
10474 -- Check whether new subprogram and old subprogram are both inherited
10475 -- from subprograms that have distinct dispatch table entries. This can
10476 -- occur with derivations from instances with accidental homonyms. The
10477 -- function is conservative given that the converse is only true within
10478 -- instances that contain accidental overloadings.
10479
10480 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10481 -- Report conflict between entities S and E
10482
10483 ------------------------------------
10484 -- Check_For_Primitive_Subprogram --
10485 ------------------------------------
10486
10487 procedure Check_For_Primitive_Subprogram
10488 (Is_Primitive : out Boolean;
10489 Is_Overriding : Boolean := False)
10490 is
10491 Formal : Entity_Id;
10492 F_Typ : Entity_Id;
10493 B_Typ : Entity_Id;
10494
10495 function Visible_Part_Type (T : Entity_Id) return Boolean;
10496 -- Returns true if T is declared in the visible part of the current
10497 -- package scope; otherwise returns false. Assumes that T is declared
10498 -- in a package.
10499
10500 procedure Check_Private_Overriding (T : Entity_Id);
10501 -- Checks that if a primitive abstract subprogram of a visible
10502 -- abstract type is declared in a private part, then it must override
10503 -- an abstract subprogram declared in the visible part. Also checks
10504 -- that if a primitive function with a controlling result is declared
10505 -- in a private part, then it must override a function declared in
10506 -- the visible part.
10507
10508 ------------------------------
10509 -- Check_Private_Overriding --
10510 ------------------------------
10511
10512 procedure Check_Private_Overriding (T : Entity_Id) is
10513 function Overrides_Private_Part_Op return Boolean;
10514 -- This detects the special case where the overriding subprogram
10515 -- is overriding a subprogram that was declared in the same
10516 -- private part. That case is illegal by 3.9.3(10).
10517
10518 function Overrides_Visible_Function
10519 (Partial_View : Entity_Id) return Boolean;
10520 -- True if S overrides a function in the visible part. The
10521 -- overridden function could be explicitly or implicitly declared.
10522
10523 -------------------------------
10524 -- Overrides_Private_Part_Op --
10525 -------------------------------
10526
10527 function Overrides_Private_Part_Op return Boolean is
10528 Over_Decl : constant Node_Id :=
10529 Unit_Declaration_Node (Overridden_Operation (S));
10530 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
10531
10532 begin
10533 pragma Assert (Is_Overriding);
10534 pragma Assert
10535 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
10536 pragma Assert
10537 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
10538
10539 return In_Same_List (Over_Decl, Subp_Decl);
10540 end Overrides_Private_Part_Op;
10541
10542 --------------------------------
10543 -- Overrides_Visible_Function --
10544 --------------------------------
10545
10546 function Overrides_Visible_Function
10547 (Partial_View : Entity_Id) return Boolean
10548 is
10549 begin
10550 if not Is_Overriding or else not Has_Homonym (S) then
10551 return False;
10552 end if;
10553
10554 if not Present (Partial_View) then
10555 return True;
10556 end if;
10557
10558 -- Search through all the homonyms H of S in the current
10559 -- package spec, and return True if we find one that matches.
10560 -- Note that Parent (H) will be the declaration of the
10561 -- partial view of T for a match.
10562
10563 declare
10564 H : Entity_Id := S;
10565 begin
10566 loop
10567 H := Homonym (H);
10568 exit when not Present (H) or else Scope (H) /= Scope (S);
10569
10570 if Nkind_In
10571 (Parent (H),
10572 N_Private_Extension_Declaration,
10573 N_Private_Type_Declaration)
10574 and then Defining_Identifier (Parent (H)) = Partial_View
10575 then
10576 return True;
10577 end if;
10578 end loop;
10579 end;
10580
10581 return False;
10582 end Overrides_Visible_Function;
10583
10584 -- Start of processing for Check_Private_Overriding
10585
10586 begin
10587 if Is_Package_Or_Generic_Package (Current_Scope)
10588 and then In_Private_Part (Current_Scope)
10589 and then Visible_Part_Type (T)
10590 and then not In_Instance
10591 then
10592 if Is_Abstract_Type (T)
10593 and then Is_Abstract_Subprogram (S)
10594 and then (not Is_Overriding
10595 or else not Is_Abstract_Subprogram (E)
10596 or else Overrides_Private_Part_Op)
10597 then
10598 Error_Msg_N
10599 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10600 S);
10601
10602 elsif Ekind (S) = E_Function then
10603 declare
10604 Partial_View : constant Entity_Id :=
10605 Incomplete_Or_Partial_View (T);
10606
10607 begin
10608 if not Overrides_Visible_Function (Partial_View) then
10609
10610 -- Here, S is "function ... return T;" declared in
10611 -- the private part, not overriding some visible
10612 -- operation. That's illegal in the tagged case
10613 -- (but not if the private type is untagged).
10614
10615 if ((Present (Partial_View)
10616 and then Is_Tagged_Type (Partial_View))
10617 or else (not Present (Partial_View)
10618 and then Is_Tagged_Type (T)))
10619 and then T = Base_Type (Etype (S))
10620 then
10621 Error_Msg_N
10622 ("private function with tagged result must"
10623 & " override visible-part function", S);
10624 Error_Msg_N
10625 ("\move subprogram to the visible part"
10626 & " (RM 3.9.3(10))", S);
10627
10628 -- Ada 2012 (AI05-0073): Extend this check to the case
10629 -- of a function whose result subtype is defined by an
10630 -- access_definition designating specific tagged type.
10631
10632 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10633 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10634 and then
10635 not Is_Class_Wide_Type
10636 (Designated_Type (Etype (S)))
10637 and then Ada_Version >= Ada_2012
10638 then
10639 Error_Msg_N
10640 ("private function with controlling access "
10641 & "result must override visible-part function",
10642 S);
10643 Error_Msg_N
10644 ("\move subprogram to the visible part"
10645 & " (RM 3.9.3(10))", S);
10646 end if;
10647 end if;
10648 end;
10649 end if;
10650 end if;
10651 end Check_Private_Overriding;
10652
10653 -----------------------
10654 -- Visible_Part_Type --
10655 -----------------------
10656
10657 function Visible_Part_Type (T : Entity_Id) return Boolean is
10658 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10659
10660 begin
10661 -- If the entity is a private type, then it must be declared in a
10662 -- visible part.
10663
10664 if Ekind (T) in Private_Kind then
10665 return True;
10666
10667 elsif Is_Type (T) and then Has_Private_Declaration (T) then
10668 return True;
10669
10670 elsif Is_List_Member (Declaration_Node (T))
10671 and then List_Containing (Declaration_Node (T)) =
10672 Visible_Declarations (Specification (P))
10673 then
10674 return True;
10675
10676 else
10677 return False;
10678 end if;
10679 end Visible_Part_Type;
10680
10681 -- Start of processing for Check_For_Primitive_Subprogram
10682
10683 begin
10684 Is_Primitive := False;
10685
10686 if not Comes_From_Source (S) then
10687 null;
10688
10689 -- If subprogram is at library level, it is not primitive operation
10690
10691 elsif Current_Scope = Standard_Standard then
10692 null;
10693
10694 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10695 and then not In_Package_Body (Current_Scope))
10696 or else Is_Overriding
10697 then
10698 -- For function, check return type
10699
10700 if Ekind (S) = E_Function then
10701 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10702 F_Typ := Designated_Type (Etype (S));
10703 else
10704 F_Typ := Etype (S);
10705 end if;
10706
10707 B_Typ := Base_Type (F_Typ);
10708
10709 if Scope (B_Typ) = Current_Scope
10710 and then not Is_Class_Wide_Type (B_Typ)
10711 and then not Is_Generic_Type (B_Typ)
10712 then
10713 Is_Primitive := True;
10714 Set_Has_Primitive_Operations (B_Typ);
10715 Set_Is_Primitive (S);
10716 Check_Private_Overriding (B_Typ);
10717
10718 -- The Ghost policy in effect at the point of declaration
10719 -- or a tagged type and a primitive operation must match
10720 -- (SPARK RM 6.9(16)).
10721
10722 Check_Ghost_Primitive (S, B_Typ);
10723 end if;
10724 end if;
10725
10726 -- For all subprograms, check formals
10727
10728 Formal := First_Formal (S);
10729 while Present (Formal) loop
10730 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10731 F_Typ := Designated_Type (Etype (Formal));
10732 else
10733 F_Typ := Etype (Formal);
10734 end if;
10735
10736 B_Typ := Base_Type (F_Typ);
10737
10738 if Ekind (B_Typ) = E_Access_Subtype then
10739 B_Typ := Base_Type (B_Typ);
10740 end if;
10741
10742 if Scope (B_Typ) = Current_Scope
10743 and then not Is_Class_Wide_Type (B_Typ)
10744 and then not Is_Generic_Type (B_Typ)
10745 then
10746 Is_Primitive := True;
10747 Set_Is_Primitive (S);
10748 Set_Has_Primitive_Operations (B_Typ);
10749 Check_Private_Overriding (B_Typ);
10750
10751 -- The Ghost policy in effect at the point of declaration
10752 -- of a tagged type and a primitive operation must match
10753 -- (SPARK RM 6.9(16)).
10754
10755 Check_Ghost_Primitive (S, B_Typ);
10756 end if;
10757
10758 Next_Formal (Formal);
10759 end loop;
10760
10761 -- Special case: An equality function can be redefined for a type
10762 -- occurring in a declarative part, and won't otherwise be treated as
10763 -- a primitive because it doesn't occur in a package spec and doesn't
10764 -- override an inherited subprogram. It's important that we mark it
10765 -- primitive so it can be returned by Collect_Primitive_Operations
10766 -- and be used in composing the equality operation of later types
10767 -- that have a component of the type.
10768
10769 elsif Chars (S) = Name_Op_Eq
10770 and then Etype (S) = Standard_Boolean
10771 then
10772 B_Typ := Base_Type (Etype (First_Formal (S)));
10773
10774 if Scope (B_Typ) = Current_Scope
10775 and then
10776 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
10777 and then not Is_Limited_Type (B_Typ)
10778 then
10779 Is_Primitive := True;
10780 Set_Is_Primitive (S);
10781 Set_Has_Primitive_Operations (B_Typ);
10782 Check_Private_Overriding (B_Typ);
10783
10784 -- The Ghost policy in effect at the point of declaration of a
10785 -- tagged type and a primitive operation must match
10786 -- (SPARK RM 6.9(16)).
10787
10788 Check_Ghost_Primitive (S, B_Typ);
10789 end if;
10790 end if;
10791 end Check_For_Primitive_Subprogram;
10792
10793 --------------------------------------
10794 -- Has_Matching_Entry_Or_Subprogram --
10795 --------------------------------------
10796
10797 function Has_Matching_Entry_Or_Subprogram
10798 (E : Entity_Id) return Boolean
10799 is
10800 function Check_Conforming_Parameters
10801 (E1_Param : Node_Id;
10802 E2_Param : Node_Id;
10803 Ctype : Conformance_Type) return Boolean;
10804 -- Starting from the given parameters, check that all the parameters
10805 -- of two entries or subprograms are conformant. Used to skip
10806 -- the check on the controlling argument.
10807
10808 function Matching_Entry_Or_Subprogram
10809 (Conc_Typ : Entity_Id;
10810 Subp : Entity_Id) return Entity_Id;
10811 -- Return the first entry or subprogram of the given concurrent type
10812 -- whose name matches the name of Subp and has a profile conformant
10813 -- with Subp; return Empty if not found.
10814
10815 function Matching_Dispatching_Subprogram
10816 (Conc_Typ : Entity_Id;
10817 Ent : Entity_Id) return Entity_Id;
10818 -- Return the first dispatching primitive of Conc_Type defined in the
10819 -- enclosing scope of Conc_Type (i.e. before the full definition of
10820 -- this concurrent type) whose name matches the entry Ent and has a
10821 -- profile conformant with the profile of the corresponding (not yet
10822 -- built) dispatching primitive of Ent; return Empty if not found.
10823
10824 function Matching_Original_Protected_Subprogram
10825 (Prot_Typ : Entity_Id;
10826 Subp : Entity_Id) return Entity_Id;
10827 -- Return the first subprogram defined in the enclosing scope of
10828 -- Prot_Typ (before the full definition of this protected type)
10829 -- whose name matches the original name of Subp and has a profile
10830 -- conformant with the profile of Subp; return Empty if not found.
10831
10832 function Normalized_First_Parameter_Type
10833 (E : Entity_Id) return Entity_Id;
10834 -- Return the type of the first parameter unless that type
10835 -- is an anonymous access type, in which case return the
10836 -- designated type. Used to treat anonymous-access-to-synchronized
10837 -- the same as synchronized for purposes of checking for
10838 -- prefixed view profile conflicts.
10839
10840 ---------------------------------
10841 -- Check_Conforming_Parameters --
10842 ---------------------------------
10843
10844 function Check_Conforming_Parameters
10845 (E1_Param : Node_Id;
10846 E2_Param : Node_Id;
10847 Ctype : Conformance_Type) return Boolean
10848 is
10849 Param_E1 : Node_Id := E1_Param;
10850 Param_E2 : Node_Id := E2_Param;
10851
10852 begin
10853 while Present (Param_E1) and then Present (Param_E2) loop
10854 if (Ctype >= Mode_Conformant) and then
10855 Ekind (Defining_Identifier (Param_E1)) /=
10856 Ekind (Defining_Identifier (Param_E2))
10857 then
10858 return False;
10859 elsif not
10860 Conforming_Types
10861 (Find_Parameter_Type (Param_E1),
10862 Find_Parameter_Type (Param_E2),
10863 Ctype)
10864 then
10865 return False;
10866 end if;
10867
10868 Next (Param_E1);
10869 Next (Param_E2);
10870 end loop;
10871
10872 -- The candidate is not valid if one of the two lists contains
10873 -- more parameters than the other
10874
10875 return No (Param_E1) and then No (Param_E2);
10876 end Check_Conforming_Parameters;
10877
10878 ----------------------------------
10879 -- Matching_Entry_Or_Subprogram --
10880 ----------------------------------
10881
10882 function Matching_Entry_Or_Subprogram
10883 (Conc_Typ : Entity_Id;
10884 Subp : Entity_Id) return Entity_Id
10885 is
10886 E : Entity_Id;
10887
10888 begin
10889 E := First_Entity (Conc_Typ);
10890 while Present (E) loop
10891 if Chars (Subp) = Chars (E)
10892 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
10893 and then
10894 Check_Conforming_Parameters
10895 (First (Parameter_Specifications (Parent (E))),
10896 Next (First (Parameter_Specifications (Parent (Subp)))),
10897 Type_Conformant)
10898 then
10899 return E;
10900 end if;
10901
10902 Next_Entity (E);
10903 end loop;
10904
10905 return Empty;
10906 end Matching_Entry_Or_Subprogram;
10907
10908 -------------------------------------
10909 -- Matching_Dispatching_Subprogram --
10910 -------------------------------------
10911
10912 function Matching_Dispatching_Subprogram
10913 (Conc_Typ : Entity_Id;
10914 Ent : Entity_Id) return Entity_Id
10915 is
10916 E : Entity_Id;
10917
10918 begin
10919 -- Search for entities in the enclosing scope of this synchonized
10920 -- type.
10921
10922 pragma Assert (Is_Concurrent_Type (Conc_Typ));
10923 Push_Scope (Scope (Conc_Typ));
10924 E := Current_Entity_In_Scope (Ent);
10925 Pop_Scope;
10926
10927 while Present (E) loop
10928 if Scope (E) = Scope (Conc_Typ)
10929 and then Comes_From_Source (E)
10930 and then Ekind (E) = E_Procedure
10931 and then Present (First_Entity (E))
10932 and then Is_Controlling_Formal (First_Entity (E))
10933 and then Etype (First_Entity (E)) = Conc_Typ
10934 and then
10935 Check_Conforming_Parameters
10936 (First (Parameter_Specifications (Parent (Ent))),
10937 Next (First (Parameter_Specifications (Parent (E)))),
10938 Subtype_Conformant)
10939 then
10940 return E;
10941 end if;
10942
10943 E := Homonym (E);
10944 end loop;
10945
10946 return Empty;
10947 end Matching_Dispatching_Subprogram;
10948
10949 --------------------------------------------
10950 -- Matching_Original_Protected_Subprogram --
10951 --------------------------------------------
10952
10953 function Matching_Original_Protected_Subprogram
10954 (Prot_Typ : Entity_Id;
10955 Subp : Entity_Id) return Entity_Id
10956 is
10957 ICF : constant Boolean :=
10958 Is_Controlling_Formal (First_Entity (Subp));
10959 E : Entity_Id;
10960
10961 begin
10962 -- Temporarily decorate the first parameter of Subp as controlling
10963 -- formal, required to invoke Subtype_Conformant.
10964
10965 Set_Is_Controlling_Formal (First_Entity (Subp));
10966
10967 E :=
10968 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
10969
10970 while Present (E) loop
10971 if Scope (E) = Scope (Prot_Typ)
10972 and then Comes_From_Source (E)
10973 and then Ekind (Subp) = Ekind (E)
10974 and then Present (First_Entity (E))
10975 and then Is_Controlling_Formal (First_Entity (E))
10976 and then Etype (First_Entity (E)) = Prot_Typ
10977 and then Subtype_Conformant (Subp, E,
10978 Skip_Controlling_Formals => True)
10979 then
10980 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10981 return E;
10982 end if;
10983
10984 E := Homonym (E);
10985 end loop;
10986
10987 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
10988
10989 return Empty;
10990 end Matching_Original_Protected_Subprogram;
10991
10992 -------------------------------------
10993 -- Normalized_First_Parameter_Type --
10994 -------------------------------------
10995
10996 function Normalized_First_Parameter_Type
10997 (E : Entity_Id) return Entity_Id
10998 is
10999 Result : Entity_Id := Etype (First_Entity (E));
11000 begin
11001 if Ekind (Result) = E_Anonymous_Access_Type then
11002 Result := Designated_Type (Result);
11003 end if;
11004 return Result;
11005 end Normalized_First_Parameter_Type;
11006
11007 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11008
11009 begin
11010 -- Case 1: E is a subprogram whose first formal is a concurrent type
11011 -- defined in the scope of E that has an entry or subprogram whose
11012 -- profile matches E.
11013
11014 if Comes_From_Source (E)
11015 and then Is_Subprogram (E)
11016 and then Present (First_Entity (E))
11017 and then Is_Concurrent_Record_Type
11018 (Normalized_First_Parameter_Type (E))
11019 then
11020 if Scope (E) =
11021 Scope (Corresponding_Concurrent_Type
11022 (Normalized_First_Parameter_Type (E)))
11023 and then
11024 Present
11025 (Matching_Entry_Or_Subprogram
11026 (Corresponding_Concurrent_Type
11027 (Normalized_First_Parameter_Type (E)),
11028 Subp => E))
11029 then
11030 Report_Conflict (E,
11031 Matching_Entry_Or_Subprogram
11032 (Corresponding_Concurrent_Type
11033 (Normalized_First_Parameter_Type (E)),
11034 Subp => E));
11035 return True;
11036 end if;
11037
11038 -- Case 2: E is an internally built dispatching subprogram of a
11039 -- protected type and there is a subprogram defined in the enclosing
11040 -- scope of the protected type that has the original name of E and
11041 -- its profile is conformant with the profile of E. We check the
11042 -- name of the original protected subprogram associated with E since
11043 -- the expander builds dispatching primitives of protected functions
11044 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11045
11046 elsif not Comes_From_Source (E)
11047 and then Is_Subprogram (E)
11048 and then Present (First_Entity (E))
11049 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11050 and then Present (Original_Protected_Subprogram (E))
11051 and then
11052 Present
11053 (Matching_Original_Protected_Subprogram
11054 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11055 Subp => E))
11056 then
11057 Report_Conflict (E,
11058 Matching_Original_Protected_Subprogram
11059 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11060 Subp => E));
11061 return True;
11062
11063 -- Case 3: E is an entry of a synchronized type and a matching
11064 -- procedure has been previously defined in the enclosing scope
11065 -- of the synchronized type.
11066
11067 elsif Comes_From_Source (E)
11068 and then Ekind (E) = E_Entry
11069 and then
11070 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11071 then
11072 Report_Conflict (E,
11073 Matching_Dispatching_Subprogram (Current_Scope, E));
11074 return True;
11075 end if;
11076
11077 return False;
11078 end Has_Matching_Entry_Or_Subprogram;
11079
11080 ----------------------------
11081 -- Is_Private_Declaration --
11082 ----------------------------
11083
11084 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11085 Decl : constant Node_Id := Unit_Declaration_Node (E);
11086 Priv_Decls : List_Id;
11087
11088 begin
11089 if Is_Package_Or_Generic_Package (Current_Scope)
11090 and then In_Private_Part (Current_Scope)
11091 then
11092 Priv_Decls :=
11093 Private_Declarations (Package_Specification (Current_Scope));
11094
11095 return In_Package_Body (Current_Scope)
11096 or else
11097 (Is_List_Member (Decl)
11098 and then List_Containing (Decl) = Priv_Decls)
11099 or else (Nkind (Parent (Decl)) = N_Package_Specification
11100 and then not
11101 Is_Compilation_Unit
11102 (Defining_Entity (Parent (Decl)))
11103 and then List_Containing (Parent (Parent (Decl))) =
11104 Priv_Decls);
11105 else
11106 return False;
11107 end if;
11108 end Is_Private_Declaration;
11109
11110 --------------------------
11111 -- Is_Overriding_Alias --
11112 --------------------------
11113
11114 function Is_Overriding_Alias
11115 (Old_E : Entity_Id;
11116 New_E : Entity_Id) return Boolean
11117 is
11118 AO : constant Entity_Id := Alias (Old_E);
11119 AN : constant Entity_Id := Alias (New_E);
11120
11121 begin
11122 return Scope (AO) /= Scope (AN)
11123 or else No (DTC_Entity (AO))
11124 or else No (DTC_Entity (AN))
11125 or else DT_Position (AO) = DT_Position (AN);
11126 end Is_Overriding_Alias;
11127
11128 ---------------------
11129 -- Report_Conflict --
11130 ---------------------
11131
11132 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
11133 begin
11134 Error_Msg_Sloc := Sloc (E);
11135
11136 -- Generate message, with useful additional warning if in generic
11137
11138 if Is_Generic_Unit (E) then
11139 Error_Msg_N ("previous generic unit cannot be overloaded", S);
11140 Error_Msg_N ("\& conflicts with declaration#", S);
11141 else
11142 Error_Msg_N ("& conflicts with declaration#", S);
11143 end if;
11144 end Report_Conflict;
11145
11146 -- Start of processing for New_Overloaded_Entity
11147
11148 begin
11149 -- We need to look for an entity that S may override. This must be a
11150 -- homonym in the current scope, so we look for the first homonym of
11151 -- S in the current scope as the starting point for the search.
11152
11153 E := Current_Entity_In_Scope (S);
11154
11155 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
11156 -- They are directly added to the list of primitive operations of
11157 -- Derived_Type, unless this is a rederivation in the private part
11158 -- of an operation that was already derived in the visible part of
11159 -- the current package.
11160
11161 if Ada_Version >= Ada_2005
11162 and then Present (Derived_Type)
11163 and then Present (Alias (S))
11164 and then Is_Dispatching_Operation (Alias (S))
11165 and then Present (Find_Dispatching_Type (Alias (S)))
11166 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
11167 then
11168 -- For private types, when the full-view is processed we propagate to
11169 -- the full view the non-overridden entities whose attribute "alias"
11170 -- references an interface primitive. These entities were added by
11171 -- Derive_Subprograms to ensure that interface primitives are
11172 -- covered.
11173
11174 -- Inside_Freeze_Actions is non zero when S corresponds with an
11175 -- internal entity that links an interface primitive with its
11176 -- covering primitive through attribute Interface_Alias (see
11177 -- Add_Internal_Interface_Entities).
11178
11179 if Inside_Freezing_Actions = 0
11180 and then Is_Package_Or_Generic_Package (Current_Scope)
11181 and then In_Private_Part (Current_Scope)
11182 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11183 and then Nkind (Parent (S)) = N_Full_Type_Declaration
11184 and then Full_View (Defining_Identifier (Parent (E)))
11185 = Defining_Identifier (Parent (S))
11186 and then Alias (E) = Alias (S)
11187 then
11188 Check_Operation_From_Private_View (S, E);
11189 Set_Is_Dispatching_Operation (S);
11190
11191 -- Common case
11192
11193 else
11194 Enter_Overloaded_Entity (S);
11195 Check_Dispatching_Operation (S, Empty);
11196 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11197 end if;
11198
11199 return;
11200 end if;
11201
11202 -- For synchronized types check conflicts of this entity with previously
11203 -- defined entities.
11204
11205 if Ada_Version >= Ada_2005
11206 and then Has_Matching_Entry_Or_Subprogram (S)
11207 then
11208 return;
11209 end if;
11210
11211 -- If there is no homonym then this is definitely not overriding
11212
11213 if No (E) then
11214 Enter_Overloaded_Entity (S);
11215 Check_Dispatching_Operation (S, Empty);
11216 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11217
11218 -- If subprogram has an explicit declaration, check whether it has an
11219 -- overriding indicator.
11220
11221 if Comes_From_Source (S) then
11222 Check_Synchronized_Overriding (S, Overridden_Subp);
11223
11224 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
11225 -- it may have overridden some hidden inherited primitive. Update
11226 -- Overridden_Subp to avoid spurious errors when checking the
11227 -- overriding indicator.
11228
11229 if Ada_Version >= Ada_2012
11230 and then No (Overridden_Subp)
11231 and then Is_Dispatching_Operation (S)
11232 and then Present (Overridden_Operation (S))
11233 then
11234 Overridden_Subp := Overridden_Operation (S);
11235 end if;
11236
11237 Check_Overriding_Indicator
11238 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11239
11240 -- The Ghost policy in effect at the point of declaration of a
11241 -- parent subprogram and an overriding subprogram must match
11242 -- (SPARK RM 6.9(17)).
11243
11244 Check_Ghost_Overriding (S, Overridden_Subp);
11245 end if;
11246
11247 -- If there is a homonym that is not overloadable, then we have an
11248 -- error, except for the special cases checked explicitly below.
11249
11250 elsif not Is_Overloadable (E) then
11251
11252 -- Check for spurious conflict produced by a subprogram that has the
11253 -- same name as that of the enclosing generic package. The conflict
11254 -- occurs within an instance, between the subprogram and the renaming
11255 -- declaration for the package. After the subprogram, the package
11256 -- renaming declaration becomes hidden.
11257
11258 if Ekind (E) = E_Package
11259 and then Present (Renamed_Object (E))
11260 and then Renamed_Object (E) = Current_Scope
11261 and then Nkind (Parent (Renamed_Object (E))) =
11262 N_Package_Specification
11263 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
11264 then
11265 Set_Is_Hidden (E);
11266 Set_Is_Immediately_Visible (E, False);
11267 Enter_Overloaded_Entity (S);
11268 Set_Homonym (S, Homonym (E));
11269 Check_Dispatching_Operation (S, Empty);
11270 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
11271
11272 -- If the subprogram is implicit it is hidden by the previous
11273 -- declaration. However if it is dispatching, it must appear in the
11274 -- dispatch table anyway, because it can be dispatched to even if it
11275 -- cannot be called directly.
11276
11277 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
11278 Set_Scope (S, Current_Scope);
11279
11280 if Is_Dispatching_Operation (Alias (S)) then
11281 Check_Dispatching_Operation (S, Empty);
11282 end if;
11283
11284 return;
11285
11286 else
11287 Report_Conflict (S, E);
11288 return;
11289 end if;
11290
11291 -- E exists and is overloadable
11292
11293 else
11294 Check_Synchronized_Overriding (S, Overridden_Subp);
11295
11296 -- Loop through E and its homonyms to determine if any of them is
11297 -- the candidate for overriding by S.
11298
11299 while Present (E) loop
11300
11301 -- Definitely not interesting if not in the current scope
11302
11303 if Scope (E) /= Current_Scope then
11304 null;
11305
11306 -- A function can overload the name of an abstract state. The
11307 -- state can be viewed as a function with a profile that cannot
11308 -- be matched by anything.
11309
11310 elsif Ekind (S) = E_Function
11311 and then Ekind (E) = E_Abstract_State
11312 then
11313 Enter_Overloaded_Entity (S);
11314 return;
11315
11316 -- Ada 2012 (AI05-0165): For internally generated bodies of null
11317 -- procedures locate the internally generated spec. We enforce
11318 -- mode conformance since a tagged type may inherit from
11319 -- interfaces several null primitives which differ only in
11320 -- the mode of the formals.
11321
11322 elsif not Comes_From_Source (S)
11323 and then Is_Null_Procedure (S)
11324 and then not Mode_Conformant (E, S)
11325 then
11326 null;
11327
11328 -- Check if we have type conformance
11329
11330 elsif Type_Conformant (E, S) then
11331
11332 -- If the old and new entities have the same profile and one
11333 -- is not the body of the other, then this is an error, unless
11334 -- one of them is implicitly declared.
11335
11336 -- There are some cases when both can be implicit, for example
11337 -- when both a literal and a function that overrides it are
11338 -- inherited in a derivation, or when an inherited operation
11339 -- of a tagged full type overrides the inherited operation of
11340 -- a private extension. Ada 83 had a special rule for the
11341 -- literal case. In Ada 95, the later implicit operation hides
11342 -- the former, and the literal is always the former. In the
11343 -- odd case where both are derived operations declared at the
11344 -- same point, both operations should be declared, and in that
11345 -- case we bypass the following test and proceed to the next
11346 -- part. This can only occur for certain obscure cases in
11347 -- instances, when an operation on a type derived from a formal
11348 -- private type does not override a homograph inherited from
11349 -- the actual. In subsequent derivations of such a type, the
11350 -- DT positions of these operations remain distinct, if they
11351 -- have been set.
11352
11353 if Present (Alias (S))
11354 and then (No (Alias (E))
11355 or else Comes_From_Source (E)
11356 or else Is_Abstract_Subprogram (S)
11357 or else
11358 (Is_Dispatching_Operation (E)
11359 and then Is_Overriding_Alias (E, S)))
11360 and then Ekind (E) /= E_Enumeration_Literal
11361 then
11362 -- When an derived operation is overloaded it may be due to
11363 -- the fact that the full view of a private extension
11364 -- re-inherits. It has to be dealt with.
11365
11366 if Is_Package_Or_Generic_Package (Current_Scope)
11367 and then In_Private_Part (Current_Scope)
11368 then
11369 Check_Operation_From_Private_View (S, E);
11370 end if;
11371
11372 -- In any case the implicit operation remains hidden by the
11373 -- existing declaration, which is overriding. Indicate that
11374 -- E overrides the operation from which S is inherited.
11375
11376 if Present (Alias (S)) then
11377 Set_Overridden_Operation (E, Alias (S));
11378 Inherit_Subprogram_Contract (E, Alias (S));
11379
11380 else
11381 Set_Overridden_Operation (E, S);
11382 Inherit_Subprogram_Contract (E, S);
11383 end if;
11384
11385 -- When a dispatching operation overrides an inherited
11386 -- subprogram, it shall be subtype conformant with the
11387 -- inherited subprogram (RM 3.9.2 (10.2)).
11388
11389 if Comes_From_Source (E)
11390 and then Is_Dispatching_Operation (E)
11391 and then Find_Dispatching_Type (S)
11392 = Find_Dispatching_Type (E)
11393 then
11394 Check_Subtype_Conformant (E, S);
11395 end if;
11396
11397 if Comes_From_Source (E) then
11398 Check_Overriding_Indicator (E, S, Is_Primitive => False);
11399
11400 -- The Ghost policy in effect at the point of declaration
11401 -- of a parent subprogram and an overriding subprogram
11402 -- must match (SPARK RM 6.9(17)).
11403
11404 Check_Ghost_Overriding (E, S);
11405 end if;
11406
11407 return;
11408
11409 -- Within an instance, the renaming declarations for actual
11410 -- subprograms may become ambiguous, but they do not hide each
11411 -- other.
11412
11413 elsif Ekind (E) /= E_Entry
11414 and then not Comes_From_Source (E)
11415 and then not Is_Generic_Instance (E)
11416 and then (Present (Alias (E))
11417 or else Is_Intrinsic_Subprogram (E))
11418 and then (not In_Instance
11419 or else No (Parent (E))
11420 or else Nkind (Unit_Declaration_Node (E)) /=
11421 N_Subprogram_Renaming_Declaration)
11422 then
11423 -- A subprogram child unit is not allowed to override an
11424 -- inherited subprogram (10.1.1(20)).
11425
11426 if Is_Child_Unit (S) then
11427 Error_Msg_N
11428 ("child unit overrides inherited subprogram in parent",
11429 S);
11430 return;
11431 end if;
11432
11433 if Is_Non_Overriding_Operation (E, S) then
11434 Enter_Overloaded_Entity (S);
11435
11436 if No (Derived_Type)
11437 or else Is_Tagged_Type (Derived_Type)
11438 then
11439 Check_Dispatching_Operation (S, Empty);
11440 end if;
11441
11442 return;
11443 end if;
11444
11445 -- E is a derived operation or an internal operator which
11446 -- is being overridden. Remove E from further visibility.
11447 -- Furthermore, if E is a dispatching operation, it must be
11448 -- replaced in the list of primitive operations of its type
11449 -- (see Override_Dispatching_Operation).
11450
11451 Overridden_Subp := E;
11452
11453 -- It is possible for E to be in the current scope and
11454 -- yet not in the entity chain. This can only occur in a
11455 -- generic context where E is an implicit concatenation
11456 -- in the formal part, because in a generic body the
11457 -- entity chain starts with the formals.
11458
11459 -- In GNATprove mode, a wrapper for an operation with
11460 -- axiomatization may be a homonym of another declaration
11461 -- for an actual subprogram (needs refinement ???).
11462
11463 if No (Prev_Entity (E)) then
11464 if In_Instance
11465 and then GNATprove_Mode
11466 and then
11467 Nkind (Original_Node (Unit_Declaration_Node (S))) =
11468 N_Subprogram_Renaming_Declaration
11469 then
11470 return;
11471 else
11472 pragma Assert (Chars (E) = Name_Op_Concat);
11473 null;
11474 end if;
11475 end if;
11476
11477 -- E must be removed both from the entity_list of the
11478 -- current scope, and from the visibility chain.
11479
11480 if Debug_Flag_E then
11481 Write_Str ("Override implicit operation ");
11482 Write_Int (Int (E));
11483 Write_Eol;
11484 end if;
11485
11486 -- If E is a predefined concatenation, it stands for four
11487 -- different operations. As a result, a single explicit
11488 -- declaration does not hide it. In a possible ambiguous
11489 -- situation, Disambiguate chooses the user-defined op,
11490 -- so it is correct to retain the previous internal one.
11491
11492 if Chars (E) /= Name_Op_Concat
11493 or else Ekind (E) /= E_Operator
11494 then
11495 -- For nondispatching derived operations that are
11496 -- overridden by a subprogram declared in the private
11497 -- part of a package, we retain the derived subprogram
11498 -- but mark it as not immediately visible. If the
11499 -- derived operation was declared in the visible part
11500 -- then this ensures that it will still be visible
11501 -- outside the package with the proper signature
11502 -- (calls from outside must also be directed to this
11503 -- version rather than the overriding one, unlike the
11504 -- dispatching case). Calls from inside the package
11505 -- will still resolve to the overriding subprogram
11506 -- since the derived one is marked as not visible
11507 -- within the package.
11508
11509 -- If the private operation is dispatching, we achieve
11510 -- the overriding by keeping the implicit operation
11511 -- but setting its alias to be the overriding one. In
11512 -- this fashion the proper body is executed in all
11513 -- cases, but the original signature is used outside
11514 -- of the package.
11515
11516 -- If the overriding is not in the private part, we
11517 -- remove the implicit operation altogether.
11518
11519 if Is_Private_Declaration (S) then
11520 if not Is_Dispatching_Operation (E) then
11521 Set_Is_Immediately_Visible (E, False);
11522 else
11523 -- Work done in Override_Dispatching_Operation, so
11524 -- nothing else needs to be done here.
11525
11526 null;
11527 end if;
11528
11529 else
11530 Remove_Entity_And_Homonym (E);
11531 end if;
11532 end if;
11533
11534 Enter_Overloaded_Entity (S);
11535
11536 -- For entities generated by Derive_Subprograms the
11537 -- overridden operation is the inherited primitive
11538 -- (which is available through the attribute alias).
11539
11540 if not (Comes_From_Source (E))
11541 and then Is_Dispatching_Operation (E)
11542 and then Find_Dispatching_Type (E) =
11543 Find_Dispatching_Type (S)
11544 and then Present (Alias (E))
11545 and then Comes_From_Source (Alias (E))
11546 then
11547 Set_Overridden_Operation (S, Alias (E));
11548 Inherit_Subprogram_Contract (S, Alias (E));
11549
11550 -- Normal case of setting entity as overridden
11551
11552 -- Note: Static_Initialization and Overridden_Operation
11553 -- attributes use the same field in subprogram entities.
11554 -- Static_Initialization is only defined for internal
11555 -- initialization procedures, where Overridden_Operation
11556 -- is irrelevant. Therefore the setting of this attribute
11557 -- must check whether the target is an init_proc.
11558
11559 elsif not Is_Init_Proc (S) then
11560 Set_Overridden_Operation (S, E);
11561 Inherit_Subprogram_Contract (S, E);
11562 end if;
11563
11564 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11565
11566 -- The Ghost policy in effect at the point of declaration
11567 -- of a parent subprogram and an overriding subprogram
11568 -- must match (SPARK RM 6.9(17)).
11569
11570 Check_Ghost_Overriding (S, E);
11571
11572 -- If S is a user-defined subprogram or a null procedure
11573 -- expanded to override an inherited null procedure, or a
11574 -- predefined dispatching primitive then indicate that E
11575 -- overrides the operation from which S is inherited.
11576
11577 if Comes_From_Source (S)
11578 or else
11579 (Present (Parent (S))
11580 and then Nkind (Parent (S)) = N_Procedure_Specification
11581 and then Null_Present (Parent (S)))
11582 or else
11583 (Present (Alias (E))
11584 and then
11585 Is_Predefined_Dispatching_Operation (Alias (E)))
11586 then
11587 if Present (Alias (E)) then
11588 Set_Overridden_Operation (S, Alias (E));
11589 Inherit_Subprogram_Contract (S, Alias (E));
11590 end if;
11591 end if;
11592
11593 if Is_Dispatching_Operation (E) then
11594
11595 -- An overriding dispatching subprogram inherits the
11596 -- convention of the overridden subprogram (AI-117).
11597
11598 Set_Convention (S, Convention (E));
11599 Check_Dispatching_Operation (S, E);
11600
11601 else
11602 Check_Dispatching_Operation (S, Empty);
11603 end if;
11604
11605 Check_For_Primitive_Subprogram
11606 (Is_Primitive_Subp, Is_Overriding => True);
11607 goto Check_Inequality;
11608
11609 -- Apparent redeclarations in instances can occur when two
11610 -- formal types get the same actual type. The subprograms in
11611 -- in the instance are legal, even if not callable from the
11612 -- outside. Calls from within are disambiguated elsewhere.
11613 -- For dispatching operations in the visible part, the usual
11614 -- rules apply, and operations with the same profile are not
11615 -- legal (B830001).
11616
11617 elsif (In_Instance_Visible_Part
11618 and then not Is_Dispatching_Operation (E))
11619 or else In_Instance_Not_Visible
11620 then
11621 null;
11622
11623 -- Here we have a real error (identical profile)
11624
11625 else
11626 Error_Msg_Sloc := Sloc (E);
11627
11628 -- Avoid cascaded errors if the entity appears in
11629 -- subsequent calls.
11630
11631 Set_Scope (S, Current_Scope);
11632
11633 -- Generate error, with extra useful warning for the case
11634 -- of a generic instance with no completion.
11635
11636 if Is_Generic_Instance (S)
11637 and then not Has_Completion (E)
11638 then
11639 Error_Msg_N
11640 ("instantiation cannot provide body for&", S);
11641 Error_Msg_N ("\& conflicts with declaration#", S);
11642 else
11643 Error_Msg_N ("& conflicts with declaration#", S);
11644 end if;
11645
11646 return;
11647 end if;
11648
11649 else
11650 -- If one subprogram has an access parameter and the other
11651 -- a parameter of an access type, calls to either might be
11652 -- ambiguous. Verify that parameters match except for the
11653 -- access parameter.
11654
11655 if May_Hide_Profile then
11656 declare
11657 F1 : Entity_Id;
11658 F2 : Entity_Id;
11659
11660 begin
11661 F1 := First_Formal (S);
11662 F2 := First_Formal (E);
11663 while Present (F1) and then Present (F2) loop
11664 if Is_Access_Type (Etype (F1)) then
11665 if not Is_Access_Type (Etype (F2))
11666 or else not Conforming_Types
11667 (Designated_Type (Etype (F1)),
11668 Designated_Type (Etype (F2)),
11669 Type_Conformant)
11670 then
11671 May_Hide_Profile := False;
11672 end if;
11673
11674 elsif
11675 not Conforming_Types
11676 (Etype (F1), Etype (F2), Type_Conformant)
11677 then
11678 May_Hide_Profile := False;
11679 end if;
11680
11681 Next_Formal (F1);
11682 Next_Formal (F2);
11683 end loop;
11684
11685 if May_Hide_Profile
11686 and then No (F1)
11687 and then No (F2)
11688 then
11689 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11690 end if;
11691 end;
11692 end if;
11693 end if;
11694
11695 E := Homonym (E);
11696 end loop;
11697
11698 -- On exit, we know that S is a new entity
11699
11700 Enter_Overloaded_Entity (S);
11701 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11702 Check_Overriding_Indicator
11703 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11704
11705 -- The Ghost policy in effect at the point of declaration of a parent
11706 -- subprogram and an overriding subprogram must match
11707 -- (SPARK RM 6.9(17)).
11708
11709 Check_Ghost_Overriding (S, Overridden_Subp);
11710
11711 -- If S is a derived operation for an untagged type then by
11712 -- definition it's not a dispatching operation (even if the parent
11713 -- operation was dispatching), so Check_Dispatching_Operation is not
11714 -- called in that case.
11715
11716 if No (Derived_Type)
11717 or else Is_Tagged_Type (Derived_Type)
11718 then
11719 Check_Dispatching_Operation (S, Empty);
11720 end if;
11721 end if;
11722
11723 -- If this is a user-defined equality operator that is not a derived
11724 -- subprogram, create the corresponding inequality. If the operation is
11725 -- dispatching, the expansion is done elsewhere, and we do not create
11726 -- an explicit inequality operation.
11727
11728 <<Check_Inequality>>
11729 if Chars (S) = Name_Op_Eq
11730 and then Etype (S) = Standard_Boolean
11731 and then Present (Parent (S))
11732 and then not Is_Dispatching_Operation (S)
11733 then
11734 Make_Inequality_Operator (S);
11735 Check_Untagged_Equality (S);
11736 end if;
11737 end New_Overloaded_Entity;
11738
11739 ----------------------------------
11740 -- Preanalyze_Formal_Expression --
11741 ----------------------------------
11742
11743 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
11744 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
11745 begin
11746 In_Spec_Expression := True;
11747 Preanalyze_With_Freezing_And_Resolve (N, T);
11748 In_Spec_Expression := Save_In_Spec_Expression;
11749 end Preanalyze_Formal_Expression;
11750
11751 ---------------------
11752 -- Process_Formals --
11753 ---------------------
11754
11755 procedure Process_Formals
11756 (T : List_Id;
11757 Related_Nod : Node_Id)
11758 is
11759 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
11760 -- Determine whether an access type designates a type coming from a
11761 -- limited view.
11762
11763 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
11764 -- Check whether the default has a class-wide type. After analysis the
11765 -- default has the type of the formal, so we must also check explicitly
11766 -- for an access attribute.
11767
11768 ----------------------------------
11769 -- Designates_From_Limited_With --
11770 ----------------------------------
11771
11772 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
11773 Desig : Entity_Id := Typ;
11774
11775 begin
11776 if Is_Access_Type (Desig) then
11777 Desig := Directly_Designated_Type (Desig);
11778 end if;
11779
11780 if Is_Class_Wide_Type (Desig) then
11781 Desig := Root_Type (Desig);
11782 end if;
11783
11784 return
11785 Ekind (Desig) = E_Incomplete_Type
11786 and then From_Limited_With (Desig);
11787 end Designates_From_Limited_With;
11788
11789 ---------------------------
11790 -- Is_Class_Wide_Default --
11791 ---------------------------
11792
11793 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
11794 begin
11795 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
11796 or else (Nkind (D) = N_Attribute_Reference
11797 and then Attribute_Name (D) = Name_Access
11798 and then Is_Class_Wide_Type (Etype (Prefix (D))));
11799 end Is_Class_Wide_Default;
11800
11801 -- Local variables
11802
11803 Context : constant Node_Id := Parent (Parent (T));
11804 Default : Node_Id;
11805 Formal : Entity_Id;
11806 Formal_Type : Entity_Id;
11807 Param_Spec : Node_Id;
11808 Ptype : Entity_Id;
11809
11810 Num_Out_Params : Nat := 0;
11811 First_Out_Param : Entity_Id := Empty;
11812 -- Used for setting Is_Only_Out_Parameter
11813
11814 -- Start of processing for Process_Formals
11815
11816 begin
11817 -- In order to prevent premature use of the formals in the same formal
11818 -- part, the Ekind is left undefined until all default expressions are
11819 -- analyzed. The Ekind is established in a separate loop at the end.
11820
11821 Param_Spec := First (T);
11822 while Present (Param_Spec) loop
11823 Formal := Defining_Identifier (Param_Spec);
11824 Set_Never_Set_In_Source (Formal, True);
11825 Enter_Name (Formal);
11826
11827 -- Case of ordinary parameters
11828
11829 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
11830 Find_Type (Parameter_Type (Param_Spec));
11831 Ptype := Parameter_Type (Param_Spec);
11832
11833 if Ptype = Error then
11834 goto Continue;
11835 end if;
11836
11837 -- Protect against malformed parameter types
11838
11839 if Nkind (Ptype) not in N_Has_Entity then
11840 Formal_Type := Any_Type;
11841 else
11842 Formal_Type := Entity (Ptype);
11843 end if;
11844
11845 if Is_Incomplete_Type (Formal_Type)
11846 or else
11847 (Is_Class_Wide_Type (Formal_Type)
11848 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
11849 then
11850 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
11851 -- primitive operations, as long as their completion is
11852 -- in the same declarative part. If in the private part
11853 -- this means that the type cannot be a Taft-amendment type.
11854 -- Check is done on package exit. For access to subprograms,
11855 -- the use is legal for Taft-amendment types.
11856
11857 -- Ada 2012: tagged incomplete types are allowed as generic
11858 -- formal types. They do not introduce dependencies and the
11859 -- corresponding generic subprogram does not have a delayed
11860 -- freeze, because it does not need a freeze node. However,
11861 -- it is still the case that untagged incomplete types cannot
11862 -- be Taft-amendment types and must be completed in private
11863 -- part, so the subprogram must appear in the list of private
11864 -- dependents of the type.
11865
11866 if Is_Tagged_Type (Formal_Type)
11867 or else (Ada_Version >= Ada_2012
11868 and then not From_Limited_With (Formal_Type)
11869 and then not Is_Generic_Type (Formal_Type))
11870 then
11871 if Ekind (Scope (Current_Scope)) = E_Package
11872 and then not Is_Generic_Type (Formal_Type)
11873 and then not Is_Class_Wide_Type (Formal_Type)
11874 then
11875 if not Nkind_In
11876 (Parent (T), N_Access_Function_Definition,
11877 N_Access_Procedure_Definition)
11878 then
11879 Append_Elmt (Current_Scope,
11880 Private_Dependents (Base_Type (Formal_Type)));
11881
11882 -- Freezing is delayed to ensure that Register_Prim
11883 -- will get called for this operation, which is needed
11884 -- in cases where static dispatch tables aren't built.
11885 -- (Note that the same is done for controlling access
11886 -- parameter cases in function Access_Definition.)
11887
11888 if not Is_Thunk (Current_Scope) then
11889 Set_Has_Delayed_Freeze (Current_Scope);
11890 end if;
11891 end if;
11892 end if;
11893
11894 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
11895 N_Access_Procedure_Definition)
11896 then
11897 -- AI05-0151: Tagged incomplete types are allowed in all
11898 -- formal parts. Untagged incomplete types are not allowed
11899 -- in bodies. Limited views of either kind are not allowed
11900 -- if there is no place at which the non-limited view can
11901 -- become available.
11902
11903 -- Incomplete formal untagged types are not allowed in
11904 -- subprogram bodies (but are legal in their declarations).
11905 -- This excludes bodies created for null procedures, which
11906 -- are basic declarations.
11907
11908 if Is_Generic_Type (Formal_Type)
11909 and then not Is_Tagged_Type (Formal_Type)
11910 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
11911 then
11912 Error_Msg_N
11913 ("invalid use of formal incomplete type", Param_Spec);
11914
11915 elsif Ada_Version >= Ada_2012 then
11916 if Is_Tagged_Type (Formal_Type)
11917 and then (not From_Limited_With (Formal_Type)
11918 or else not In_Package_Body)
11919 then
11920 null;
11921
11922 elsif Nkind_In (Context, N_Accept_Statement,
11923 N_Accept_Alternative,
11924 N_Entry_Body)
11925 or else (Nkind (Context) = N_Subprogram_Body
11926 and then Comes_From_Source (Context))
11927 then
11928 Error_Msg_NE
11929 ("invalid use of untagged incomplete type &",
11930 Ptype, Formal_Type);
11931 end if;
11932
11933 else
11934 Error_Msg_NE
11935 ("invalid use of incomplete type&",
11936 Param_Spec, Formal_Type);
11937
11938 -- Further checks on the legality of incomplete types
11939 -- in formal parts are delayed until the freeze point
11940 -- of the enclosing subprogram or access to subprogram.
11941 end if;
11942 end if;
11943
11944 elsif Ekind (Formal_Type) = E_Void then
11945 Error_Msg_NE
11946 ("premature use of&",
11947 Parameter_Type (Param_Spec), Formal_Type);
11948 end if;
11949
11950 -- Ada 2012 (AI-142): Handle aliased parameters
11951
11952 if Ada_Version >= Ada_2012
11953 and then Aliased_Present (Param_Spec)
11954 then
11955 Set_Is_Aliased (Formal);
11956
11957 -- AI12-001: All aliased objects are considered to be specified
11958 -- as independently addressable (RM C.6(8.1/4)).
11959
11960 Set_Is_Independent (Formal);
11961 end if;
11962
11963 -- Ada 2005 (AI-231): Create and decorate an internal subtype
11964 -- declaration corresponding to the null-excluding type of the
11965 -- formal in the enclosing scope. Finally, replace the parameter
11966 -- type of the formal with the internal subtype.
11967
11968 if Ada_Version >= Ada_2005
11969 and then Null_Exclusion_Present (Param_Spec)
11970 then
11971 if not Is_Access_Type (Formal_Type) then
11972 Error_Msg_N
11973 ("`NOT NULL` allowed only for an access type", Param_Spec);
11974
11975 else
11976 if Can_Never_Be_Null (Formal_Type)
11977 and then Comes_From_Source (Related_Nod)
11978 then
11979 Error_Msg_NE
11980 ("`NOT NULL` not allowed (& already excludes null)",
11981 Param_Spec, Formal_Type);
11982 end if;
11983
11984 Formal_Type :=
11985 Create_Null_Excluding_Itype
11986 (T => Formal_Type,
11987 Related_Nod => Related_Nod,
11988 Scope_Id => Scope (Current_Scope));
11989
11990 -- If the designated type of the itype is an itype that is
11991 -- not frozen yet, we set the Has_Delayed_Freeze attribute
11992 -- on the access subtype, to prevent order-of-elaboration
11993 -- issues in the backend.
11994
11995 -- Example:
11996 -- type T is access procedure;
11997 -- procedure Op (O : not null T);
11998
11999 if Is_Itype (Directly_Designated_Type (Formal_Type))
12000 and then
12001 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12002 then
12003 Set_Has_Delayed_Freeze (Formal_Type);
12004 end if;
12005 end if;
12006 end if;
12007
12008 -- An access formal type
12009
12010 else
12011 Formal_Type :=
12012 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12013
12014 -- No need to continue if we already notified errors
12015
12016 if not Present (Formal_Type) then
12017 return;
12018 end if;
12019
12020 -- Ada 2005 (AI-254)
12021
12022 declare
12023 AD : constant Node_Id :=
12024 Access_To_Subprogram_Definition
12025 (Parameter_Type (Param_Spec));
12026 begin
12027 if Present (AD) and then Protected_Present (AD) then
12028 Formal_Type :=
12029 Replace_Anonymous_Access_To_Protected_Subprogram
12030 (Param_Spec);
12031 end if;
12032 end;
12033 end if;
12034
12035 Set_Etype (Formal, Formal_Type);
12036
12037 -- Deal with default expression if present
12038
12039 Default := Expression (Param_Spec);
12040
12041 if Present (Default) then
12042 if Out_Present (Param_Spec) then
12043 Error_Msg_N
12044 ("default initialization only allowed for IN parameters",
12045 Param_Spec);
12046 end if;
12047
12048 -- Do the special preanalysis of the expression (see section on
12049 -- "Handling of Default Expressions" in the spec of package Sem).
12050
12051 Preanalyze_Formal_Expression (Default, Formal_Type);
12052
12053 -- An access to constant cannot be the default for
12054 -- an access parameter that is an access to variable.
12055
12056 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12057 and then not Is_Access_Constant (Formal_Type)
12058 and then Is_Access_Type (Etype (Default))
12059 and then Is_Access_Constant (Etype (Default))
12060 then
12061 Error_Msg_N
12062 ("formal that is access to variable cannot be initialized "
12063 & "with an access-to-constant expression", Default);
12064 end if;
12065
12066 -- Check that the designated type of an access parameter's default
12067 -- is not a class-wide type unless the parameter's designated type
12068 -- is also class-wide.
12069
12070 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12071 and then not Designates_From_Limited_With (Formal_Type)
12072 and then Is_Class_Wide_Default (Default)
12073 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
12074 then
12075 Error_Msg_N
12076 ("access to class-wide expression not allowed here", Default);
12077 end if;
12078
12079 -- Check incorrect use of dynamically tagged expressions
12080
12081 if Is_Tagged_Type (Formal_Type) then
12082 Check_Dynamically_Tagged_Expression
12083 (Expr => Default,
12084 Typ => Formal_Type,
12085 Related_Nod => Default);
12086 end if;
12087 end if;
12088
12089 -- Ada 2005 (AI-231): Static checks
12090
12091 if Ada_Version >= Ada_2005
12092 and then Is_Access_Type (Etype (Formal))
12093 and then Can_Never_Be_Null (Etype (Formal))
12094 then
12095 Null_Exclusion_Static_Checks (Param_Spec);
12096 end if;
12097
12098 -- The following checks are relevant only when SPARK_Mode is on as
12099 -- these are not standard Ada legality rules.
12100
12101 if SPARK_Mode = On then
12102 if Ekind_In (Scope (Formal), E_Function, E_Generic_Function) then
12103
12104 -- A function cannot have a parameter of mode IN OUT or OUT
12105 -- (SPARK RM 6.1).
12106
12107 if Ekind_In (Formal, E_In_Out_Parameter, E_Out_Parameter) then
12108 Error_Msg_N
12109 ("function cannot have parameter of mode `OUT` or "
12110 & "`IN OUT`", Formal);
12111 end if;
12112
12113 -- A procedure cannot have an effectively volatile formal
12114 -- parameter of mode IN because it behaves as a constant
12115 -- (SPARK RM 7.1.3(4)).
12116
12117 elsif Ekind (Scope (Formal)) = E_Procedure
12118 and then Ekind (Formal) = E_In_Parameter
12119 and then Is_Effectively_Volatile (Formal)
12120 then
12121 Error_Msg_N
12122 ("formal parameter of mode `IN` cannot be volatile", Formal);
12123 end if;
12124 end if;
12125
12126 <<Continue>>
12127 Next (Param_Spec);
12128 end loop;
12129
12130 -- If this is the formal part of a function specification, analyze the
12131 -- subtype mark in the context where the formals are visible but not
12132 -- yet usable, and may hide outer homographs.
12133
12134 if Nkind (Related_Nod) = N_Function_Specification then
12135 Analyze_Return_Type (Related_Nod);
12136 end if;
12137
12138 -- Now set the kind (mode) of each formal
12139
12140 Param_Spec := First (T);
12141 while Present (Param_Spec) loop
12142 Formal := Defining_Identifier (Param_Spec);
12143 Set_Formal_Mode (Formal);
12144
12145 if Ekind (Formal) = E_In_Parameter then
12146 Set_Default_Value (Formal, Expression (Param_Spec));
12147
12148 if Present (Expression (Param_Spec)) then
12149 Default := Expression (Param_Spec);
12150
12151 if Is_Scalar_Type (Etype (Default)) then
12152 if Nkind (Parameter_Type (Param_Spec)) /=
12153 N_Access_Definition
12154 then
12155 Formal_Type := Entity (Parameter_Type (Param_Spec));
12156 else
12157 Formal_Type :=
12158 Access_Definition
12159 (Related_Nod, Parameter_Type (Param_Spec));
12160 end if;
12161
12162 Apply_Scalar_Range_Check (Default, Formal_Type);
12163 end if;
12164 end if;
12165
12166 elsif Ekind (Formal) = E_Out_Parameter then
12167 Num_Out_Params := Num_Out_Params + 1;
12168
12169 if Num_Out_Params = 1 then
12170 First_Out_Param := Formal;
12171 end if;
12172
12173 elsif Ekind (Formal) = E_In_Out_Parameter then
12174 Num_Out_Params := Num_Out_Params + 1;
12175 end if;
12176
12177 -- Skip remaining processing if formal type was in error
12178
12179 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
12180 goto Next_Parameter;
12181 end if;
12182
12183 -- Force call by reference if aliased
12184
12185 declare
12186 Conv : constant Convention_Id := Convention (Etype (Formal));
12187 begin
12188 if Is_Aliased (Formal) then
12189 Set_Mechanism (Formal, By_Reference);
12190
12191 -- Warn if user asked this to be passed by copy
12192
12193 if Conv = Convention_Ada_Pass_By_Copy then
12194 Error_Msg_N
12195 ("cannot pass aliased parameter & by copy??", Formal);
12196 end if;
12197
12198 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
12199
12200 elsif Conv = Convention_Ada_Pass_By_Copy then
12201 Set_Mechanism (Formal, By_Copy);
12202
12203 elsif Conv = Convention_Ada_Pass_By_Reference then
12204 Set_Mechanism (Formal, By_Reference);
12205 end if;
12206 end;
12207
12208 <<Next_Parameter>>
12209 Next (Param_Spec);
12210 end loop;
12211
12212 if Present (First_Out_Param) and then Num_Out_Params = 1 then
12213 Set_Is_Only_Out_Parameter (First_Out_Param);
12214 end if;
12215 end Process_Formals;
12216
12217 ----------------------------
12218 -- Reference_Body_Formals --
12219 ----------------------------
12220
12221 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12222 Fs : Entity_Id;
12223 Fb : Entity_Id;
12224
12225 begin
12226 if Error_Posted (Spec) then
12227 return;
12228 end if;
12229
12230 -- Iterate over both lists. They may be of different lengths if the two
12231 -- specs are not conformant.
12232
12233 Fs := First_Formal (Spec);
12234 Fb := First_Formal (Bod);
12235 while Present (Fs) and then Present (Fb) loop
12236 Generate_Reference (Fs, Fb, 'b');
12237
12238 if Style_Check then
12239 Style.Check_Identifier (Fb, Fs);
12240 end if;
12241
12242 Set_Spec_Entity (Fb, Fs);
12243 Set_Referenced (Fs, False);
12244 Next_Formal (Fs);
12245 Next_Formal (Fb);
12246 end loop;
12247 end Reference_Body_Formals;
12248
12249 -------------------------
12250 -- Set_Actual_Subtypes --
12251 -------------------------
12252
12253 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12254 Decl : Node_Id;
12255 Formal : Entity_Id;
12256 T : Entity_Id;
12257 First_Stmt : Node_Id := Empty;
12258 AS_Needed : Boolean;
12259
12260 begin
12261 -- If this is an empty initialization procedure, no need to create
12262 -- actual subtypes (small optimization).
12263
12264 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12265 return;
12266
12267 -- Within a predicate function we do not want to generate local
12268 -- subtypes that may generate nested predicate functions.
12269
12270 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
12271 return;
12272 end if;
12273
12274 -- The subtype declarations may freeze the formals. The body generated
12275 -- for an expression function is not a freeze point, so do not emit
12276 -- these declarations (small loss of efficiency in rare cases).
12277
12278 if Nkind (N) = N_Subprogram_Body
12279 and then Was_Expression_Function (N)
12280 then
12281 return;
12282 end if;
12283
12284 Formal := First_Formal (Subp);
12285 while Present (Formal) loop
12286 T := Etype (Formal);
12287
12288 -- We never need an actual subtype for a constrained formal
12289
12290 if Is_Constrained (T) then
12291 AS_Needed := False;
12292
12293 -- If we have unknown discriminants, then we do not need an actual
12294 -- subtype, or more accurately we cannot figure it out. Note that
12295 -- all class-wide types have unknown discriminants.
12296
12297 elsif Has_Unknown_Discriminants (T) then
12298 AS_Needed := False;
12299
12300 -- At this stage we have an unconstrained type that may need an
12301 -- actual subtype. For sure the actual subtype is needed if we have
12302 -- an unconstrained array type. However, in an instance, the type
12303 -- may appear as a subtype of the full view, while the actual is
12304 -- in fact private (in which case no actual subtype is needed) so
12305 -- check the kind of the base type.
12306
12307 elsif Is_Array_Type (Base_Type (T)) then
12308 AS_Needed := True;
12309
12310 -- The only other case needing an actual subtype is an unconstrained
12311 -- record type which is an IN parameter (we cannot generate actual
12312 -- subtypes for the OUT or IN OUT case, since an assignment can
12313 -- change the discriminant values. However we exclude the case of
12314 -- initialization procedures, since discriminants are handled very
12315 -- specially in this context, see the section entitled "Handling of
12316 -- Discriminants" in Einfo.
12317
12318 -- We also exclude the case of Discrim_SO_Functions (functions used
12319 -- in front-end layout mode for size/offset values), since in such
12320 -- functions only discriminants are referenced, and not only are such
12321 -- subtypes not needed, but they cannot always be generated, because
12322 -- of order of elaboration issues.
12323
12324 elsif Is_Record_Type (T)
12325 and then Ekind (Formal) = E_In_Parameter
12326 and then Chars (Formal) /= Name_uInit
12327 and then not Is_Unchecked_Union (T)
12328 and then not Is_Discrim_SO_Function (Subp)
12329 then
12330 AS_Needed := True;
12331
12332 -- All other cases do not need an actual subtype
12333
12334 else
12335 AS_Needed := False;
12336 end if;
12337
12338 -- Generate actual subtypes for unconstrained arrays and
12339 -- unconstrained discriminated records.
12340
12341 if AS_Needed then
12342 if Nkind (N) = N_Accept_Statement then
12343
12344 -- If expansion is active, the formal is replaced by a local
12345 -- variable that renames the corresponding entry of the
12346 -- parameter block, and it is this local variable that may
12347 -- require an actual subtype.
12348
12349 if Expander_Active then
12350 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12351 else
12352 Decl := Build_Actual_Subtype (T, Formal);
12353 end if;
12354
12355 if Present (Handled_Statement_Sequence (N)) then
12356 First_Stmt :=
12357 First (Statements (Handled_Statement_Sequence (N)));
12358 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12359 Mark_Rewrite_Insertion (Decl);
12360 else
12361 -- If the accept statement has no body, there will be no
12362 -- reference to the actuals, so no need to compute actual
12363 -- subtypes.
12364
12365 return;
12366 end if;
12367
12368 else
12369 Decl := Build_Actual_Subtype (T, Formal);
12370 Prepend (Decl, Declarations (N));
12371 Mark_Rewrite_Insertion (Decl);
12372 end if;
12373
12374 -- The declaration uses the bounds of an existing object, and
12375 -- therefore needs no constraint checks.
12376
12377 Analyze (Decl, Suppress => All_Checks);
12378 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
12379
12380 -- We need to freeze manually the generated type when it is
12381 -- inserted anywhere else than in a declarative part.
12382
12383 if Present (First_Stmt) then
12384 Insert_List_Before_And_Analyze (First_Stmt,
12385 Freeze_Entity (Defining_Identifier (Decl), N));
12386
12387 -- Ditto if the type has a dynamic predicate, because the
12388 -- generated function will mention the actual subtype. The
12389 -- predicate may come from an explicit aspect of be inherited.
12390
12391 elsif Has_Predicates (T) then
12392 Insert_List_After_And_Analyze (Decl,
12393 Freeze_Entity (Defining_Identifier (Decl), N));
12394 end if;
12395
12396 if Nkind (N) = N_Accept_Statement
12397 and then Expander_Active
12398 then
12399 Set_Actual_Subtype (Renamed_Object (Formal),
12400 Defining_Identifier (Decl));
12401 else
12402 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12403 end if;
12404 end if;
12405
12406 Next_Formal (Formal);
12407 end loop;
12408 end Set_Actual_Subtypes;
12409
12410 ---------------------
12411 -- Set_Formal_Mode --
12412 ---------------------
12413
12414 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12415 Spec : constant Node_Id := Parent (Formal_Id);
12416 Id : constant Entity_Id := Scope (Formal_Id);
12417
12418 begin
12419 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12420 -- since we ensure that corresponding actuals are always valid at the
12421 -- point of the call.
12422
12423 if Out_Present (Spec) then
12424 if Is_Entry (Id)
12425 or else Is_Subprogram_Or_Generic_Subprogram (Id)
12426 then
12427 Set_Has_Out_Or_In_Out_Parameter (Id, True);
12428 end if;
12429
12430 if Ekind_In (Id, E_Function, E_Generic_Function) then
12431
12432 -- [IN] OUT parameters allowed for functions in Ada 2012
12433
12434 if Ada_Version >= Ada_2012 then
12435
12436 -- Even in Ada 2012 operators can only have IN parameters
12437
12438 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12439 Error_Msg_N ("operators can only have IN parameters", Spec);
12440 end if;
12441
12442 if In_Present (Spec) then
12443 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12444 else
12445 Set_Ekind (Formal_Id, E_Out_Parameter);
12446 end if;
12447
12448 -- But not in earlier versions of Ada
12449
12450 else
12451 Error_Msg_N ("functions can only have IN parameters", Spec);
12452 Set_Ekind (Formal_Id, E_In_Parameter);
12453 end if;
12454
12455 elsif In_Present (Spec) then
12456 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12457
12458 else
12459 Set_Ekind (Formal_Id, E_Out_Parameter);
12460 Set_Never_Set_In_Source (Formal_Id, True);
12461 Set_Is_True_Constant (Formal_Id, False);
12462 Set_Current_Value (Formal_Id, Empty);
12463 end if;
12464
12465 else
12466 Set_Ekind (Formal_Id, E_In_Parameter);
12467 end if;
12468
12469 -- Set Is_Known_Non_Null for access parameters since the language
12470 -- guarantees that access parameters are always non-null. We also set
12471 -- Can_Never_Be_Null, since there is no way to change the value.
12472
12473 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12474
12475 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12476 -- null; In Ada 2005, only if then null_exclusion is explicit.
12477
12478 if Ada_Version < Ada_2005
12479 or else Can_Never_Be_Null (Etype (Formal_Id))
12480 then
12481 Set_Is_Known_Non_Null (Formal_Id);
12482 Set_Can_Never_Be_Null (Formal_Id);
12483 end if;
12484
12485 -- Ada 2005 (AI-231): Null-exclusion access subtype
12486
12487 elsif Is_Access_Type (Etype (Formal_Id))
12488 and then Can_Never_Be_Null (Etype (Formal_Id))
12489 then
12490 Set_Is_Known_Non_Null (Formal_Id);
12491
12492 -- We can also set Can_Never_Be_Null (thus preventing some junk
12493 -- access checks) for the case of an IN parameter, which cannot
12494 -- be changed, or for an IN OUT parameter, which can be changed but
12495 -- not to a null value. But for an OUT parameter, the initial value
12496 -- passed in can be null, so we can't set this flag in that case.
12497
12498 if Ekind (Formal_Id) /= E_Out_Parameter then
12499 Set_Can_Never_Be_Null (Formal_Id);
12500 end if;
12501 end if;
12502
12503 Set_Mechanism (Formal_Id, Default_Mechanism);
12504 Set_Formal_Validity (Formal_Id);
12505 end Set_Formal_Mode;
12506
12507 -------------------------
12508 -- Set_Formal_Validity --
12509 -------------------------
12510
12511 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12512 begin
12513 -- If no validity checking, then we cannot assume anything about the
12514 -- validity of parameters, since we do not know there is any checking
12515 -- of the validity on the call side.
12516
12517 if not Validity_Checks_On then
12518 return;
12519
12520 -- If validity checking for parameters is enabled, this means we are
12521 -- not supposed to make any assumptions about argument values.
12522
12523 elsif Validity_Check_Parameters then
12524 return;
12525
12526 -- If we are checking in parameters, we will assume that the caller is
12527 -- also checking parameters, so we can assume the parameter is valid.
12528
12529 elsif Ekind (Formal_Id) = E_In_Parameter
12530 and then Validity_Check_In_Params
12531 then
12532 Set_Is_Known_Valid (Formal_Id, True);
12533
12534 -- Similar treatment for IN OUT parameters
12535
12536 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12537 and then Validity_Check_In_Out_Params
12538 then
12539 Set_Is_Known_Valid (Formal_Id, True);
12540 end if;
12541 end Set_Formal_Validity;
12542
12543 ------------------------
12544 -- Subtype_Conformant --
12545 ------------------------
12546
12547 function Subtype_Conformant
12548 (New_Id : Entity_Id;
12549 Old_Id : Entity_Id;
12550 Skip_Controlling_Formals : Boolean := False) return Boolean
12551 is
12552 Result : Boolean;
12553 begin
12554 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12555 Skip_Controlling_Formals => Skip_Controlling_Formals);
12556 return Result;
12557 end Subtype_Conformant;
12558
12559 ---------------------
12560 -- Type_Conformant --
12561 ---------------------
12562
12563 function Type_Conformant
12564 (New_Id : Entity_Id;
12565 Old_Id : Entity_Id;
12566 Skip_Controlling_Formals : Boolean := False) return Boolean
12567 is
12568 Result : Boolean;
12569 begin
12570 May_Hide_Profile := False;
12571 Check_Conformance
12572 (New_Id, Old_Id, Type_Conformant, False, Result,
12573 Skip_Controlling_Formals => Skip_Controlling_Formals);
12574 return Result;
12575 end Type_Conformant;
12576
12577 -------------------------------
12578 -- Valid_Operator_Definition --
12579 -------------------------------
12580
12581 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12582 N : Integer := 0;
12583 F : Entity_Id;
12584 Id : constant Name_Id := Chars (Designator);
12585 N_OK : Boolean;
12586
12587 begin
12588 F := First_Formal (Designator);
12589 while Present (F) loop
12590 N := N + 1;
12591
12592 if Present (Default_Value (F)) then
12593 Error_Msg_N
12594 ("default values not allowed for operator parameters",
12595 Parent (F));
12596
12597 -- For function instantiations that are operators, we must check
12598 -- separately that the corresponding generic only has in-parameters.
12599 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12600 -- an error could not arise in earlier versions of the language.
12601
12602 elsif Ekind (F) /= E_In_Parameter then
12603 Error_Msg_N ("operators can only have IN parameters", F);
12604 end if;
12605
12606 Next_Formal (F);
12607 end loop;
12608
12609 -- Verify that user-defined operators have proper number of arguments
12610 -- First case of operators which can only be unary
12611
12612 if Nam_In (Id, Name_Op_Not, Name_Op_Abs) then
12613 N_OK := (N = 1);
12614
12615 -- Case of operators which can be unary or binary
12616
12617 elsif Nam_In (Id, Name_Op_Add, Name_Op_Subtract) then
12618 N_OK := (N in 1 .. 2);
12619
12620 -- All other operators can only be binary
12621
12622 else
12623 N_OK := (N = 2);
12624 end if;
12625
12626 if not N_OK then
12627 Error_Msg_N
12628 ("incorrect number of arguments for operator", Designator);
12629 end if;
12630
12631 if Id = Name_Op_Ne
12632 and then Base_Type (Etype (Designator)) = Standard_Boolean
12633 and then not Is_Intrinsic_Subprogram (Designator)
12634 then
12635 Error_Msg_N
12636 ("explicit definition of inequality not allowed", Designator);
12637 end if;
12638 end Valid_Operator_Definition;
12639
12640 end Sem_Ch6;