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