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