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