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