[Ada] Further Ada 2020 work on accessibility checking
[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 Modify_Tree_For_C
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 -- we are generating C code, create subprogram declaration
4077 -- to simplify subsequent C generation.
4078
4079 elsif No (Spec_Id)
4080 and then Modify_Tree_For_C
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 we are generating C and this is a function returning a constrained
4175 -- array type for which we must create a procedure with an extra out
4176 -- parameter, build and analyze the body now. The procedure declaration
4177 -- has already been created. We reuse the source body of the function,
4178 -- because in an instance it may contain global references that cannot
4179 -- be reanalyzed. The source function itself is not used any further,
4180 -- so we mark it as having a completion. If the subprogram is a stub the
4181 -- transformation is done later, when the proper body is analyzed.
4182
4183 if Expander_Active
4184 and then Modify_Tree_For_C
4185 and then Present (Spec_Id)
4186 and then Ekind (Spec_Id) = E_Function
4187 and then Nkind (N) /= N_Subprogram_Body_Stub
4188 and then Rewritten_For_C (Spec_Id)
4189 then
4190 Set_Has_Completion (Spec_Id);
4191
4192 Rewrite (N, Build_Procedure_Body_Form (Spec_Id, N));
4193 Analyze (N);
4194
4195 -- The entity for the created procedure must remain invisible, so it
4196 -- does not participate in resolution of subsequent references to the
4197 -- function.
4198
4199 Set_Is_Immediately_Visible (Corresponding_Spec (N), False);
4200 goto Leave;
4201 end if;
4202
4203 -- If a separate spec is present, then deal with freezing issues
4204
4205 if Present (Spec_Id) then
4206 Spec_Decl := Unit_Declaration_Node (Spec_Id);
4207 Verify_Overriding_Indicator;
4208
4209 -- In general, the spec will be frozen when we start analyzing the
4210 -- body. However, for internally generated operations, such as
4211 -- wrapper functions for inherited operations with controlling
4212 -- results, the spec may not have been frozen by the time we expand
4213 -- the freeze actions that include the bodies. In particular, extra
4214 -- formals for accessibility or for return-in-place may need to be
4215 -- generated. Freeze nodes, if any, are inserted before the current
4216 -- body. These freeze actions are also needed in Compile_Only mode to
4217 -- enable the proper back-end type annotations.
4218 -- They are necessary in any case to ensure proper elaboration order
4219 -- in gigi.
4220
4221 if Nkind (N) = N_Subprogram_Body
4222 and then Was_Expression_Function (N)
4223 and then not Has_Completion (Spec_Id)
4224 and then Serious_Errors_Detected = 0
4225 and then (Expander_Active
4226 or else Operating_Mode = Check_Semantics
4227 or else Is_Ignored_Ghost_Entity (Spec_Id))
4228 then
4229 -- The body generated for an expression function that is not a
4230 -- completion is a freeze point neither for the profile nor for
4231 -- anything else. That's why, in order to prevent any freezing
4232 -- during analysis, we need to mask types declared outside the
4233 -- expression (and in an outer scope) that are not yet frozen.
4234 -- This also needs to be done in the case of an ignored Ghost
4235 -- expression function, where the expander isn't active.
4236
4237 Set_Is_Frozen (Spec_Id);
4238 Mask_Types := Mask_Unfrozen_Types (Spec_Id);
4239
4240 elsif not Is_Frozen (Spec_Id)
4241 and then Serious_Errors_Detected = 0
4242 then
4243 Set_Has_Delayed_Freeze (Spec_Id);
4244 Freeze_Before (N, Spec_Id);
4245 end if;
4246 end if;
4247
4248 -- If the subprogram has a class-wide clone, build its body as a copy
4249 -- of the original body, and rewrite body of original subprogram as a
4250 -- wrapper that calls the clone. If N is a stub, this construction will
4251 -- take place when the proper body is analyzed. No action needed if this
4252 -- subprogram has been eliminated.
4253
4254 if Present (Spec_Id)
4255 and then Present (Class_Wide_Clone (Spec_Id))
4256 and then (Comes_From_Source (N) or else Was_Expression_Function (N))
4257 and then Nkind (N) /= N_Subprogram_Body_Stub
4258 and then not (Expander_Active and then Is_Eliminated (Spec_Id))
4259 then
4260 Build_Class_Wide_Clone_Body (Spec_Id, N);
4261
4262 -- This is the new body for the existing primitive operation
4263
4264 Rewrite (N, Build_Class_Wide_Clone_Call
4265 (Sloc (N), New_List, Spec_Id, Parent (Spec_Id)));
4266 Set_Has_Completion (Spec_Id, False);
4267 Analyze (N);
4268 return;
4269 end if;
4270
4271 -- Place subprogram on scope stack, and make formals visible. If there
4272 -- is a spec, the visible entity remains that of the spec.
4273
4274 if Present (Spec_Id) then
4275 Generate_Reference (Spec_Id, Body_Id, 'b', Set_Ref => False);
4276
4277 if Is_Child_Unit (Spec_Id) then
4278 Generate_Reference (Spec_Id, Scope (Spec_Id), 'k', False);
4279 end if;
4280
4281 if Style_Check then
4282 Style.Check_Identifier (Body_Id, Spec_Id);
4283 end if;
4284
4285 Set_Is_Compilation_Unit (Body_Id, Is_Compilation_Unit (Spec_Id));
4286 Set_Is_Child_Unit (Body_Id, Is_Child_Unit (Spec_Id));
4287
4288 if Is_Abstract_Subprogram (Spec_Id) then
4289 Error_Msg_N ("an abstract subprogram cannot have a body", N);
4290 goto Leave;
4291
4292 else
4293 Set_Convention (Body_Id, Convention (Spec_Id));
4294 Set_Has_Completion (Spec_Id);
4295
4296 if Is_Protected_Type (Scope (Spec_Id)) then
4297 Prot_Typ := Scope (Spec_Id);
4298 end if;
4299
4300 -- If this is a body generated for a renaming, do not check for
4301 -- full conformance. The check is redundant, because the spec of
4302 -- the body is a copy of the spec in the renaming declaration,
4303 -- and the test can lead to spurious errors on nested defaults.
4304
4305 if Present (Spec_Decl)
4306 and then not Comes_From_Source (N)
4307 and then
4308 (Nkind (Original_Node (Spec_Decl)) =
4309 N_Subprogram_Renaming_Declaration
4310 or else (Present (Corresponding_Body (Spec_Decl))
4311 and then
4312 Nkind (Unit_Declaration_Node
4313 (Corresponding_Body (Spec_Decl))) =
4314 N_Subprogram_Renaming_Declaration))
4315 then
4316 Conformant := True;
4317
4318 -- Conversely, the spec may have been generated for specless body
4319 -- with an inline pragma. The entity comes from source, which is
4320 -- both semantically correct and necessary for proper inlining.
4321 -- The subprogram declaration itself is not in the source.
4322
4323 elsif Comes_From_Source (N)
4324 and then Present (Spec_Decl)
4325 and then not Comes_From_Source (Spec_Decl)
4326 and then Has_Pragma_Inline (Spec_Id)
4327 then
4328 Conformant := True;
4329
4330 else
4331 Check_Conformance
4332 (Body_Id, Spec_Id,
4333 Fully_Conformant, True, Conformant, Body_Id);
4334 end if;
4335
4336 -- If the body is not fully conformant, we have to decide if we
4337 -- should analyze it or not. If it has a really messed up profile
4338 -- then we probably should not analyze it, since we will get too
4339 -- many bogus messages.
4340
4341 -- Our decision is to go ahead in the non-fully conformant case
4342 -- only if it is at least mode conformant with the spec. Note
4343 -- that the call to Check_Fully_Conformant has issued the proper
4344 -- error messages to complain about the lack of conformance.
4345
4346 if not Conformant
4347 and then not Mode_Conformant (Body_Id, Spec_Id)
4348 then
4349 goto Leave;
4350 end if;
4351 end if;
4352
4353 -- In the case we are dealing with an expression function we check
4354 -- the formals attached to the spec instead of the body - so we don't
4355 -- reference body formals.
4356
4357 if Spec_Id /= Body_Id
4358 and then not Is_Expression_Function (Spec_Id)
4359 then
4360 Reference_Body_Formals (Spec_Id, Body_Id);
4361 end if;
4362
4363 Set_Ekind (Body_Id, E_Subprogram_Body);
4364
4365 if Nkind (N) = N_Subprogram_Body_Stub then
4366 Set_Corresponding_Spec_Of_Stub (N, Spec_Id);
4367
4368 -- Regular body
4369
4370 else
4371 Set_Corresponding_Spec (N, Spec_Id);
4372
4373 -- Ada 2005 (AI-345): If the operation is a primitive operation
4374 -- of a concurrent type, the type of the first parameter has been
4375 -- replaced with the corresponding record, which is the proper
4376 -- run-time structure to use. However, within the body there may
4377 -- be uses of the formals that depend on primitive operations
4378 -- of the type (in particular calls in prefixed form) for which
4379 -- we need the original concurrent type. The operation may have
4380 -- several controlling formals, so the replacement must be done
4381 -- for all of them.
4382
4383 if Comes_From_Source (Spec_Id)
4384 and then Present (First_Entity (Spec_Id))
4385 and then Ekind (Etype (First_Entity (Spec_Id))) = E_Record_Type
4386 and then Is_Tagged_Type (Etype (First_Entity (Spec_Id)))
4387 and then Present (Interfaces (Etype (First_Entity (Spec_Id))))
4388 and then Present (Corresponding_Concurrent_Type
4389 (Etype (First_Entity (Spec_Id))))
4390 then
4391 declare
4392 Typ : constant Entity_Id := Etype (First_Entity (Spec_Id));
4393 Form : Entity_Id;
4394
4395 begin
4396 Form := First_Formal (Spec_Id);
4397 while Present (Form) loop
4398 if Etype (Form) = Typ then
4399 Set_Etype (Form, Corresponding_Concurrent_Type (Typ));
4400 end if;
4401
4402 Next_Formal (Form);
4403 end loop;
4404 end;
4405 end if;
4406
4407 -- Make the formals visible, and place subprogram on scope stack.
4408 -- This is also the point at which we set Last_Real_Spec_Entity
4409 -- to mark the entities which will not be moved to the body.
4410
4411 Install_Formals (Spec_Id);
4412 Last_Real_Spec_Entity := Last_Entity (Spec_Id);
4413
4414 -- Within an instance, add local renaming declarations so that
4415 -- gdb can retrieve the values of actuals more easily. This is
4416 -- only relevant if generating code.
4417
4418 if Is_Generic_Instance (Spec_Id)
4419 and then Is_Wrapper_Package (Current_Scope)
4420 and then Expander_Active
4421 then
4422 Build_Subprogram_Instance_Renamings (N, Current_Scope);
4423 end if;
4424
4425 Push_Scope (Spec_Id);
4426
4427 -- Make sure that the subprogram is immediately visible. For
4428 -- child units that have no separate spec this is indispensable.
4429 -- Otherwise it is safe albeit redundant.
4430
4431 Set_Is_Immediately_Visible (Spec_Id);
4432 end if;
4433
4434 Set_Corresponding_Body (Unit_Declaration_Node (Spec_Id), Body_Id);
4435 Set_Is_Obsolescent (Body_Id, Is_Obsolescent (Spec_Id));
4436 Set_Scope (Body_Id, Scope (Spec_Id));
4437
4438 -- Case of subprogram body with no previous spec
4439
4440 else
4441 -- Check for style warning required
4442
4443 if Style_Check
4444
4445 -- Only apply check for source level subprograms for which checks
4446 -- have not been suppressed.
4447
4448 and then Comes_From_Source (Body_Id)
4449 and then not Suppress_Style_Checks (Body_Id)
4450
4451 -- No warnings within an instance
4452
4453 and then not In_Instance
4454
4455 -- No warnings for expression functions
4456
4457 and then Nkind (Original_Node (N)) /= N_Expression_Function
4458 then
4459 Style.Body_With_No_Spec (N);
4460 end if;
4461
4462 New_Overloaded_Entity (Body_Id);
4463
4464 if Nkind (N) /= N_Subprogram_Body_Stub then
4465 Set_Acts_As_Spec (N);
4466 Generate_Definition (Body_Id);
4467 Generate_Reference
4468 (Body_Id, Body_Id, 'b', Set_Ref => False, Force => True);
4469
4470 -- If the body is an entry wrapper created for an entry with
4471 -- preconditions, it must be compiled in the context of the
4472 -- enclosing synchronized object, because it may mention other
4473 -- operations of the type.
4474
4475 if Is_Entry_Wrapper (Body_Id) then
4476 declare
4477 Prot : constant Entity_Id := Etype (First_Entity (Body_Id));
4478 begin
4479 Push_Scope (Prot);
4480 Install_Declarations (Prot);
4481 end;
4482 end if;
4483
4484 Install_Formals (Body_Id);
4485
4486 Push_Scope (Body_Id);
4487 end if;
4488
4489 -- For stubs and bodies with no previous spec, generate references to
4490 -- formals.
4491
4492 Generate_Reference_To_Formals (Body_Id);
4493 end if;
4494
4495 -- Entry barrier functions are generated outside the protected type and
4496 -- should not carry the SPARK_Mode of the enclosing context.
4497
4498 if Nkind (N) = N_Subprogram_Body
4499 and then Is_Entry_Barrier_Function (N)
4500 then
4501 null;
4502
4503 -- The body is generated as part of expression function expansion. When
4504 -- the expression function appears in the visible declarations of a
4505 -- package, the body is added to the private declarations. Since both
4506 -- declarative lists may be subject to a different SPARK_Mode, inherit
4507 -- the mode of the spec.
4508
4509 -- package P with SPARK_Mode is
4510 -- function Expr_Func ... is (...); -- original
4511 -- [function Expr_Func ...;] -- generated spec
4512 -- -- mode is ON
4513 -- private
4514 -- pragma SPARK_Mode (Off);
4515 -- [function Expr_Func ... is return ...;] -- generated body
4516 -- end P; -- mode is ON
4517
4518 elsif not Comes_From_Source (N)
4519 and then Present (Spec_Id)
4520 and then Is_Expression_Function (Spec_Id)
4521 then
4522 Set_SPARK_Pragma (Body_Id, SPARK_Pragma (Spec_Id));
4523 Set_SPARK_Pragma_Inherited
4524 (Body_Id, SPARK_Pragma_Inherited (Spec_Id));
4525
4526 -- Set the SPARK_Mode from the current context (may be overwritten later
4527 -- with explicit pragma). Exclude the case where the SPARK_Mode appears
4528 -- initially on a stand-alone subprogram body, but is then relocated to
4529 -- a generated corresponding spec. In this scenario the mode is shared
4530 -- between the spec and body.
4531
4532 elsif No (SPARK_Pragma (Body_Id)) then
4533 Set_SPARK_Pragma (Body_Id, SPARK_Mode_Pragma);
4534 Set_SPARK_Pragma_Inherited (Body_Id);
4535 end if;
4536
4537 -- A subprogram body may be instantiated or inlined at a later pass.
4538 -- Restore the state of Ignore_SPARK_Mode_Pragmas_In_Instance when it
4539 -- applied to the initial declaration of the body.
4540
4541 if Present (Spec_Id) then
4542 if Ignore_SPARK_Mode_Pragmas (Spec_Id) then
4543 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4544 end if;
4545
4546 else
4547 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in
4548 -- case the body is instantiated or inlined later and out of context.
4549 -- The body uses this attribute to restore the value of the global
4550 -- flag.
4551
4552 if Ignore_SPARK_Mode_Pragmas_In_Instance then
4553 Set_Ignore_SPARK_Mode_Pragmas (Body_Id);
4554
4555 elsif Ignore_SPARK_Mode_Pragmas (Body_Id) then
4556 Ignore_SPARK_Mode_Pragmas_In_Instance := True;
4557 end if;
4558 end if;
4559
4560 -- Preserve relevant elaboration-related attributes of the context which
4561 -- are no longer available or very expensive to recompute once analysis,
4562 -- resolution, and expansion are over.
4563
4564 if No (Spec_Id) then
4565 Mark_Elaboration_Attributes
4566 (N_Id => Body_Id,
4567 Checks => True,
4568 Warnings => True);
4569 end if;
4570
4571 -- If this is the proper body of a stub, we must verify that the stub
4572 -- conforms to the body, and to the previous spec if one was present.
4573 -- We know already that the body conforms to that spec. This test is
4574 -- only required for subprograms that come from source.
4575
4576 if Nkind (Parent (N)) = N_Subunit
4577 and then Comes_From_Source (N)
4578 and then not Error_Posted (Body_Id)
4579 and then Nkind (Corresponding_Stub (Parent (N))) =
4580 N_Subprogram_Body_Stub
4581 then
4582 declare
4583 Old_Id : constant Entity_Id :=
4584 Defining_Entity
4585 (Specification (Corresponding_Stub (Parent (N))));
4586
4587 Conformant : Boolean := False;
4588
4589 begin
4590 if No (Spec_Id) then
4591 Check_Fully_Conformant (Body_Id, Old_Id);
4592
4593 else
4594 Check_Conformance
4595 (Body_Id, Old_Id, Fully_Conformant, False, Conformant);
4596
4597 if not Conformant then
4598
4599 -- The stub was taken to be a new declaration. Indicate that
4600 -- it lacks a body.
4601
4602 Set_Has_Completion (Old_Id, False);
4603 end if;
4604 end if;
4605 end;
4606 end if;
4607
4608 Set_Has_Completion (Body_Id);
4609 Check_Eliminated (Body_Id);
4610
4611 -- Analyze any aspect specifications that appear on the subprogram body
4612 -- stub. Stop the analysis now as the stub does not have a declarative
4613 -- or a statement part, and it cannot be inlined.
4614
4615 if Nkind (N) = N_Subprogram_Body_Stub then
4616 if Has_Aspects (N) then
4617 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4618 end if;
4619
4620 goto Leave;
4621 end if;
4622
4623 -- Handle inlining
4624
4625 if Expander_Active
4626 and then Serious_Errors_Detected = 0
4627 and then Present (Spec_Id)
4628 and then Has_Pragma_Inline (Spec_Id)
4629 then
4630 -- Legacy implementation (relying on front-end inlining)
4631
4632 if not Back_End_Inlining then
4633 if Has_Pragma_Inline_Always (Spec_Id)
4634 or else (Front_End_Inlining
4635 and then not Opt.Disable_FE_Inline)
4636 then
4637 Build_Body_To_Inline (N, Spec_Id);
4638 end if;
4639
4640 -- New implementation (relying on back-end inlining)
4641
4642 else
4643 if Has_Pragma_Inline_Always (Spec_Id)
4644 or else Optimization_Level > 0
4645 then
4646 -- Handle function returning an unconstrained type
4647
4648 if Comes_From_Source (Body_Id)
4649 and then Ekind (Spec_Id) = E_Function
4650 and then Returns_Unconstrained_Type (Spec_Id)
4651
4652 -- If function builds in place, i.e. returns a limited type,
4653 -- inlining cannot be done.
4654
4655 and then not Is_Limited_Type (Etype (Spec_Id))
4656 then
4657 Check_And_Split_Unconstrained_Function (N, Spec_Id, Body_Id);
4658
4659 else
4660 declare
4661 Subp_Body : constant Node_Id :=
4662 Unit_Declaration_Node (Body_Id);
4663 Subp_Decl : constant List_Id := Declarations (Subp_Body);
4664
4665 begin
4666 -- Do not pass inlining to the backend if the subprogram
4667 -- has declarations or statements which cannot be inlined
4668 -- by the backend. This check is done here to emit an
4669 -- error instead of the generic warning message reported
4670 -- by the GCC backend (ie. "function might not be
4671 -- inlinable").
4672
4673 if Present (Subp_Decl)
4674 and then Has_Excluded_Declaration (Spec_Id, Subp_Decl)
4675 then
4676 null;
4677
4678 elsif Has_Excluded_Statement
4679 (Spec_Id,
4680 Statements
4681 (Handled_Statement_Sequence (Subp_Body)))
4682 then
4683 null;
4684
4685 -- If the backend inlining is available then at this
4686 -- stage we only have to mark the subprogram as inlined.
4687 -- The expander will take care of registering it in the
4688 -- table of subprograms inlined by the backend a part of
4689 -- processing calls to it (cf. Expand_Call)
4690
4691 else
4692 Set_Is_Inlined (Spec_Id);
4693 end if;
4694 end;
4695 end if;
4696 end if;
4697 end if;
4698
4699 -- In GNATprove mode, inline only when there is a separate subprogram
4700 -- declaration for now, as inlining of subprogram bodies acting as
4701 -- declarations, or subprogram stubs, are not supported by front-end
4702 -- inlining. This inlining should occur after analysis of the body, so
4703 -- that it is known whether the value of SPARK_Mode, which can be
4704 -- defined by a pragma inside the body, is applicable to the body.
4705 -- Inlining can be disabled with switch -gnatdm
4706
4707 elsif GNATprove_Mode
4708 and then Full_Analysis
4709 and then not Inside_A_Generic
4710 and then Present (Spec_Id)
4711 and then
4712 Nkind (Unit_Declaration_Node (Spec_Id)) = N_Subprogram_Declaration
4713 and then Body_Has_SPARK_Mode_On
4714 and then Can_Be_Inlined_In_GNATprove_Mode (Spec_Id, Body_Id)
4715 and then not Body_Has_Contract
4716 and then not Debug_Flag_M
4717 then
4718 Build_Body_To_Inline (N, Spec_Id);
4719 end if;
4720
4721 -- When generating code, inherited pre/postconditions are handled when
4722 -- expanding the corresponding contract.
4723
4724 -- Ada 2005 (AI-262): In library subprogram bodies, after the analysis
4725 -- of the specification we have to install the private withed units.
4726 -- This holds for child units as well.
4727
4728 if Is_Compilation_Unit (Body_Id)
4729 or else Nkind (Parent (N)) = N_Compilation_Unit
4730 then
4731 Install_Private_With_Clauses (Body_Id);
4732 end if;
4733
4734 Check_Anonymous_Return;
4735
4736 -- Set the Protected_Formal field of each extra formal of the protected
4737 -- subprogram to reference the corresponding extra formal of the
4738 -- subprogram that implements it. For regular formals this occurs when
4739 -- the protected subprogram's declaration is expanded, but the extra
4740 -- formals don't get created until the subprogram is frozen. We need to
4741 -- do this before analyzing the protected subprogram's body so that any
4742 -- references to the original subprogram's extra formals will be changed
4743 -- refer to the implementing subprogram's formals (see Expand_Formal).
4744
4745 if Present (Spec_Id)
4746 and then Is_Protected_Type (Scope (Spec_Id))
4747 and then Present (Protected_Body_Subprogram (Spec_Id))
4748 then
4749 declare
4750 Impl_Subp : constant Entity_Id :=
4751 Protected_Body_Subprogram (Spec_Id);
4752 Prot_Ext_Formal : Entity_Id := Extra_Formals (Spec_Id);
4753 Impl_Ext_Formal : Entity_Id := Extra_Formals (Impl_Subp);
4754
4755 begin
4756 while Present (Prot_Ext_Formal) loop
4757 pragma Assert (Present (Impl_Ext_Formal));
4758 Set_Protected_Formal (Prot_Ext_Formal, Impl_Ext_Formal);
4759 Next_Formal_With_Extras (Prot_Ext_Formal);
4760 Next_Formal_With_Extras (Impl_Ext_Formal);
4761 end loop;
4762 end;
4763 end if;
4764
4765 -- Generate minimum accessibility local objects to correspond with
4766 -- any extra formal added for anonymous access types. This new local
4767 -- object can then be used instead of the formal in case it is used
4768 -- in an actual to a call to a nested subprogram.
4769
4770 -- This method is used to supplement our "small integer model" for
4771 -- accessibility-check generation (for more information see
4772 -- Accessibility_Level).
4773
4774 -- Because we allow accessibility values greater than our expected value
4775 -- passing along the same extra accessibility formal as an actual
4776 -- to a nested subprogram becomes a problem because high values mean
4777 -- different things to the callee even though they are the same to the
4778 -- caller. So, as described in the first section, we create a local
4779 -- object representing the minimum of the accessibility level value that
4780 -- is passed in and the accessibility level of the callee's parameter
4781 -- and locals and use it in the case of a call to a nested subprogram.
4782 -- This generated object is refered to as a "minimum accessiblity
4783 -- level."
4784
4785 if Present (Spec_Id) or else Present (Body_Id) then
4786 Body_Nod := Unit_Declaration_Node (Body_Id);
4787
4788 declare
4789 Form : Entity_Id;
4790 begin
4791 -- Grab the appropriate formal depending on whether there exists
4792 -- an actual spec for the subprogram or whether we are dealing
4793 -- with a protected subprogram.
4794
4795 if Present (Spec_Id) then
4796 if Present (Protected_Body_Subprogram (Spec_Id)) then
4797 Form := First_Formal (Protected_Body_Subprogram (Spec_Id));
4798 else
4799 Form := First_Formal (Spec_Id);
4800 end if;
4801 else
4802 Form := First_Formal (Body_Id);
4803 end if;
4804
4805 -- Loop through formals if the subprogram is capable of accepting
4806 -- a generated local object. If it is not then it is also not
4807 -- capable of having local subprograms meaning it would not need
4808 -- a minimum accessibility level object anyway.
4809
4810 if Present (Body_Nod)
4811 and then Has_Declarations (Body_Nod)
4812 and then Nkind (Body_Nod) /= N_Package_Specification
4813 then
4814 while Present (Form) loop
4815
4816 if Present (Extra_Accessibility (Form))
4817 and then No (Minimum_Accessibility (Form))
4818 then
4819 -- Generate the minimum accessibility level object
4820
4821 -- A60b : constant natural := natural'min(1, paramL);
4822
4823 Generate_Minimum_Accessibility
4824 (Extra_Accessibility (Form), Form);
4825 end if;
4826
4827 Next_Formal (Form);
4828 end loop;
4829
4830 -- Generate the minimum accessibility level object for the
4831 -- function's Extra_Accessibility_Of_Result.
4832
4833 -- A31b : constant natural := natural'min (2, funcL);
4834
4835 if Ekind (Body_Id) = E_Function
4836 and then Present (Extra_Accessibility_Of_Result (Body_Id))
4837 then
4838 Generate_Minimum_Accessibility
4839 (Extra_Accessibility_Of_Result (Body_Id));
4840
4841 -- Replace the Extra_Accessibility_Of_Result with the new
4842 -- minimum accessibility object.
4843
4844 Set_Extra_Accessibility_Of_Result
4845 (Body_Id, Minimum_Accessibility
4846 (Extra_Accessibility_Of_Result (Body_Id)));
4847 end if;
4848 end if;
4849 end;
4850 end if;
4851
4852 -- Now we can go on to analyze the body
4853
4854 HSS := Handled_Statement_Sequence (N);
4855 Set_Actual_Subtypes (N, Current_Scope);
4856
4857 -- Add a declaration for the Protection object, renaming declarations
4858 -- for discriminals and privals and finally a declaration for the entry
4859 -- family index (if applicable). This form of early expansion is done
4860 -- when the Expander is active because Install_Private_Data_Declarations
4861 -- references entities which were created during regular expansion. The
4862 -- subprogram entity must come from source, and not be an internally
4863 -- generated subprogram.
4864
4865 if Expander_Active
4866 and then Present (Prot_Typ)
4867 and then Present (Spec_Id)
4868 and then Comes_From_Source (Spec_Id)
4869 and then not Is_Eliminated (Spec_Id)
4870 then
4871 Install_Private_Data_Declarations
4872 (Sloc (N), Spec_Id, Prot_Typ, N, Declarations (N));
4873 end if;
4874
4875 -- Ada 2012 (AI05-0151): Incomplete types coming from a limited context
4876 -- may now appear in parameter and result profiles. Since the analysis
4877 -- of a subprogram body may use the parameter and result profile of the
4878 -- spec, swap any limited views with their non-limited counterpart.
4879
4880 if Ada_Version >= Ada_2012 and then Present (Spec_Id) then
4881 Exch_Views := Exchange_Limited_Views (Spec_Id);
4882 end if;
4883
4884 -- If the return type is an anonymous access type whose designated type
4885 -- is the limited view of a class-wide type and the non-limited view is
4886 -- available, update the return type accordingly.
4887
4888 if Ada_Version >= Ada_2005 and then Present (Spec_Id) then
4889 declare
4890 Etyp : Entity_Id;
4891 Rtyp : Entity_Id;
4892
4893 begin
4894 Rtyp := Etype (Spec_Id);
4895
4896 if Ekind (Rtyp) = E_Anonymous_Access_Type then
4897 Etyp := Directly_Designated_Type (Rtyp);
4898
4899 if Is_Class_Wide_Type (Etyp)
4900 and then From_Limited_With (Etyp)
4901 then
4902 Desig_View := Etyp;
4903 Set_Directly_Designated_Type (Rtyp, Available_View (Etyp));
4904 end if;
4905 end if;
4906 end;
4907 end if;
4908
4909 -- Analyze any aspect specifications that appear on the subprogram body
4910
4911 if Has_Aspects (N) then
4912 Analyze_Aspects_On_Subprogram_Body_Or_Stub (N);
4913 end if;
4914
4915 Analyze_Declarations (Declarations (N));
4916
4917 -- Verify that the SPARK_Mode of the body agrees with that of its spec
4918
4919 if Present (Spec_Id) and then Present (SPARK_Pragma (Body_Id)) then
4920 if Present (SPARK_Pragma (Spec_Id)) then
4921 if Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Spec_Id)) = Off
4922 and then
4923 Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = On
4924 then
4925 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4926 Error_Msg_N ("incorrect application of SPARK_Mode#", N);
4927 Error_Msg_Sloc := Sloc (SPARK_Pragma (Spec_Id));
4928 Error_Msg_NE
4929 ("\value Off was set for SPARK_Mode on & #", N, Spec_Id);
4930 end if;
4931
4932 elsif Nkind (Parent (Parent (Spec_Id))) = N_Subprogram_Body_Stub then
4933 null;
4934
4935 -- SPARK_Mode Off could complete no SPARK_Mode in a generic, either
4936 -- as specified in source code, or because SPARK_Mode On is ignored
4937 -- in an instance where the context is SPARK_Mode Off/Auto.
4938
4939 elsif Get_SPARK_Mode_From_Annotation (SPARK_Pragma (Body_Id)) = Off
4940 and then (Is_Generic_Unit (Spec_Id) or else In_Instance)
4941 then
4942 null;
4943
4944 else
4945 Error_Msg_Sloc := Sloc (SPARK_Pragma (Body_Id));
4946 Error_Msg_N ("incorrect application of SPARK_Mode #", N);
4947 Error_Msg_Sloc := Sloc (Spec_Id);
4948 Error_Msg_NE
4949 ("\no value was set for SPARK_Mode on & #", N, Spec_Id);
4950 end if;
4951 end if;
4952
4953 -- A subprogram body freezes its own contract. Analyze the contract
4954 -- after the declarations of the body have been processed as pragmas
4955 -- are now chained on the contract of the subprogram body.
4956
4957 Analyze_Entry_Or_Subprogram_Body_Contract (Body_Id);
4958
4959 -- Check completion, and analyze the statements
4960
4961 Check_Completion;
4962 Inspect_Deferred_Constant_Completion (Declarations (N));
4963 Analyze (HSS);
4964
4965 -- Add the generated minimum accessibility objects to the subprogram
4966 -- body's list of declarations after analysis of the statements and
4967 -- contracts.
4968
4969 while Is_Non_Empty_List (Minimum_Acc_Objs) loop
4970 if Present (Declarations (Body_Nod)) then
4971 Prepend (Remove_Head (Minimum_Acc_Objs), Declarations (Body_Nod));
4972 else
4973 Set_Declarations
4974 (Body_Nod, New_List (Remove_Head (Minimum_Acc_Objs)));
4975 end if;
4976 end loop;
4977
4978 -- Deal with end of scope processing for the body
4979
4980 Process_End_Label (HSS, 't', Current_Scope);
4981 Update_Use_Clause_Chain;
4982 End_Scope;
4983
4984 -- If we are compiling an entry wrapper, remove the enclosing
4985 -- synchronized object from the stack.
4986
4987 if Is_Entry_Wrapper (Body_Id) then
4988 End_Scope;
4989 end if;
4990
4991 Check_Subprogram_Order (N);
4992 Set_Analyzed (Body_Id);
4993
4994 -- If we have a separate spec, then the analysis of the declarations
4995 -- caused the entities in the body to be chained to the spec id, but
4996 -- we want them chained to the body id. Only the formal parameters
4997 -- end up chained to the spec id in this case.
4998
4999 if Present (Spec_Id) then
5000
5001 -- We must conform to the categorization of our spec
5002
5003 Validate_Categorization_Dependency (N, Spec_Id);
5004
5005 -- And if this is a child unit, the parent units must conform
5006
5007 if Is_Child_Unit (Spec_Id) then
5008 Validate_Categorization_Dependency
5009 (Unit_Declaration_Node (Spec_Id), Spec_Id);
5010 end if;
5011
5012 -- Here is where we move entities from the spec to the body
5013
5014 -- Case where there are entities that stay with the spec
5015
5016 if Present (Last_Real_Spec_Entity) then
5017
5018 -- No body entities (happens when the only real spec entities come
5019 -- from precondition and postcondition pragmas).
5020
5021 if No (Last_Entity (Body_Id)) then
5022 Set_First_Entity (Body_Id, Next_Entity (Last_Real_Spec_Entity));
5023
5024 -- Body entities present (formals), so chain stuff past them
5025
5026 else
5027 Link_Entities
5028 (Last_Entity (Body_Id), Next_Entity (Last_Real_Spec_Entity));
5029 end if;
5030
5031 Set_Next_Entity (Last_Real_Spec_Entity, Empty);
5032 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5033 Set_Last_Entity (Spec_Id, Last_Real_Spec_Entity);
5034
5035 -- Case where there are no spec entities, in this case there can be
5036 -- no body entities either, so just move everything.
5037
5038 -- If the body is generated for an expression function, it may have
5039 -- been preanalyzed already, if 'access was applied to it.
5040
5041 else
5042 if Nkind (Original_Node (Unit_Declaration_Node (Spec_Id))) /=
5043 N_Expression_Function
5044 then
5045 pragma Assert (No (Last_Entity (Body_Id)));
5046 null;
5047 end if;
5048
5049 Set_First_Entity (Body_Id, First_Entity (Spec_Id));
5050 Set_Last_Entity (Body_Id, Last_Entity (Spec_Id));
5051 Set_First_Entity (Spec_Id, Empty);
5052 Set_Last_Entity (Spec_Id, Empty);
5053 end if;
5054
5055 -- Otherwise the body does not complete a previous declaration. Check
5056 -- the categorization of the body against the units it withs.
5057
5058 else
5059 Validate_Categorization_Dependency (N, Body_Id);
5060 end if;
5061
5062 Check_Missing_Return;
5063
5064 -- Now we are going to check for variables that are never modified in
5065 -- the body of the procedure. But first we deal with a special case
5066 -- where we want to modify this check. If the body of the subprogram
5067 -- starts with a raise statement or its equivalent, or if the body
5068 -- consists entirely of a null statement, then it is pretty obvious that
5069 -- it is OK to not reference the parameters. For example, this might be
5070 -- the following common idiom for a stubbed function: statement of the
5071 -- procedure raises an exception. In particular this deals with the
5072 -- common idiom of a stubbed function, which appears something like:
5073
5074 -- function F (A : Integer) return Some_Type;
5075 -- X : Some_Type;
5076 -- begin
5077 -- raise Program_Error;
5078 -- return X;
5079 -- end F;
5080
5081 -- Here the purpose of X is simply to satisfy the annoying requirement
5082 -- in Ada that there be at least one return, and we certainly do not
5083 -- want to go posting warnings on X that it is not initialized. On
5084 -- the other hand, if X is entirely unreferenced that should still
5085 -- get a warning.
5086
5087 -- What we do is to detect these cases, and if we find them, flag the
5088 -- subprogram as being Is_Trivial_Subprogram and then use that flag to
5089 -- suppress unwanted warnings. For the case of the function stub above
5090 -- we have a special test to set X as apparently assigned to suppress
5091 -- the warning.
5092
5093 declare
5094 Stm : Node_Id;
5095
5096 begin
5097 -- Skip call markers installed by the ABE mechanism, labels, and
5098 -- Push_xxx_Error_Label to find the first real statement.
5099
5100 Stm := First (Statements (HSS));
5101 while Nkind (Stm) in N_Call_Marker | N_Label | N_Push_xxx_Label loop
5102 Next (Stm);
5103 end loop;
5104
5105 -- Do the test on the original statement before expansion
5106
5107 declare
5108 Ostm : constant Node_Id := Original_Node (Stm);
5109
5110 begin
5111 -- If explicit raise statement, turn on flag
5112
5113 if Nkind (Ostm) = N_Raise_Statement then
5114 Set_Trivial_Subprogram (Stm);
5115
5116 -- If null statement, and no following statements, turn on flag
5117
5118 elsif Nkind (Stm) = N_Null_Statement
5119 and then Comes_From_Source (Stm)
5120 and then No (Next (Stm))
5121 then
5122 Set_Trivial_Subprogram (Stm);
5123
5124 -- Check for explicit call cases which likely raise an exception
5125
5126 elsif Nkind (Ostm) = N_Procedure_Call_Statement then
5127 if Is_Entity_Name (Name (Ostm)) then
5128 declare
5129 Ent : constant Entity_Id := Entity (Name (Ostm));
5130
5131 begin
5132 -- If the procedure is marked No_Return, then likely it
5133 -- raises an exception, but in any case it is not coming
5134 -- back here, so turn on the flag.
5135
5136 if Present (Ent)
5137 and then Ekind (Ent) = E_Procedure
5138 and then No_Return (Ent)
5139 then
5140 Set_Trivial_Subprogram (Stm);
5141 end if;
5142 end;
5143 end if;
5144 end if;
5145 end;
5146 end;
5147
5148 -- Check for variables that are never modified
5149
5150 declare
5151 E1 : Entity_Id;
5152 E2 : Entity_Id;
5153
5154 begin
5155 -- If there is a separate spec, then transfer Never_Set_In_Source
5156 -- flags from out parameters to the corresponding entities in the
5157 -- body. The reason we do that is we want to post error flags on
5158 -- the body entities, not the spec entities.
5159
5160 if Present (Spec_Id) then
5161 E1 := First_Entity (Spec_Id);
5162 while Present (E1) loop
5163 if Ekind (E1) = E_Out_Parameter then
5164 E2 := First_Entity (Body_Id);
5165 while Present (E2) loop
5166 exit when Chars (E1) = Chars (E2);
5167 Next_Entity (E2);
5168 end loop;
5169
5170 if Present (E2) then
5171 Set_Never_Set_In_Source (E2, Never_Set_In_Source (E1));
5172 end if;
5173 end if;
5174
5175 Next_Entity (E1);
5176 end loop;
5177 end if;
5178
5179 -- Check references of the subprogram spec when we are dealing with
5180 -- an expression function due to it having a generated body.
5181 -- Otherwise, we simply check the formals of the subprogram body.
5182
5183 if Present (Spec_Id)
5184 and then Is_Expression_Function (Spec_Id)
5185 then
5186 Check_References (Spec_Id);
5187 else
5188 Check_References (Body_Id);
5189 end if;
5190 end;
5191
5192 -- Check for nested subprogram, and mark outer level subprogram if so
5193
5194 declare
5195 Ent : Entity_Id;
5196
5197 begin
5198 if Present (Spec_Id) then
5199 Ent := Spec_Id;
5200 else
5201 Ent := Body_Id;
5202 end if;
5203
5204 loop
5205 Ent := Enclosing_Subprogram (Ent);
5206 exit when No (Ent) or else Is_Subprogram (Ent);
5207 end loop;
5208
5209 if Present (Ent) then
5210 Set_Has_Nested_Subprogram (Ent);
5211 end if;
5212 end;
5213
5214 -- Restore the limited views in the spec, if any, to let the back end
5215 -- process it without running into circularities.
5216
5217 if Exch_Views /= No_Elist then
5218 Restore_Limited_Views (Exch_Views);
5219 end if;
5220
5221 if Mask_Types /= No_Elist then
5222 Unmask_Unfrozen_Types (Mask_Types);
5223 end if;
5224
5225 if Present (Desig_View) then
5226 Set_Directly_Designated_Type (Etype (Spec_Id), Desig_View);
5227 end if;
5228
5229 <<Leave>>
5230 if Present (Ignored_Ghost_Region) then
5231 Expander_Active := Saved_EA;
5232 end if;
5233
5234 Ignore_SPARK_Mode_Pragmas_In_Instance := Saved_ISMP;
5235 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5236 end Analyze_Subprogram_Body_Helper;
5237
5238 ------------------------------------
5239 -- Analyze_Subprogram_Declaration --
5240 ------------------------------------
5241
5242 procedure Analyze_Subprogram_Declaration (N : Node_Id) is
5243 Scop : constant Entity_Id := Current_Scope;
5244 Designator : Entity_Id;
5245
5246 Is_Completion : Boolean;
5247 -- Indicates whether a null procedure declaration is a completion
5248
5249 begin
5250 -- Null procedures are not allowed in SPARK
5251
5252 if Nkind (Specification (N)) = N_Procedure_Specification
5253 and then Null_Present (Specification (N))
5254 then
5255 -- Null procedures are allowed in protected types, following the
5256 -- recent AI12-0147.
5257
5258 if Is_Protected_Type (Current_Scope)
5259 and then Ada_Version < Ada_2012
5260 then
5261 Error_Msg_N ("protected operation cannot be a null procedure", N);
5262 end if;
5263
5264 Analyze_Null_Procedure (N, Is_Completion);
5265
5266 -- The null procedure acts as a body, nothing further is needed
5267
5268 if Is_Completion then
5269 return;
5270 end if;
5271 end if;
5272
5273 Designator := Analyze_Subprogram_Specification (Specification (N));
5274
5275 -- A reference may already have been generated for the unit name, in
5276 -- which case the following call is redundant. However it is needed for
5277 -- declarations that are the rewriting of an expression function.
5278
5279 Generate_Definition (Designator);
5280
5281 -- Set the SPARK mode from the current context (may be overwritten later
5282 -- with explicit pragma). This is not done for entry barrier functions
5283 -- because they are generated outside the protected type and should not
5284 -- carry the mode of the enclosing context.
5285
5286 if Nkind (N) = N_Subprogram_Declaration
5287 and then Is_Entry_Barrier_Function (N)
5288 then
5289 null;
5290
5291 else
5292 Set_SPARK_Pragma (Designator, SPARK_Mode_Pragma);
5293 Set_SPARK_Pragma_Inherited (Designator);
5294 end if;
5295
5296 -- Save the state of flag Ignore_SPARK_Mode_Pragmas_In_Instance in case
5297 -- the body of this subprogram is instantiated or inlined later and out
5298 -- of context. The body uses this attribute to restore the value of the
5299 -- global flag.
5300
5301 if Ignore_SPARK_Mode_Pragmas_In_Instance then
5302 Set_Ignore_SPARK_Mode_Pragmas (Designator);
5303 end if;
5304
5305 -- Preserve relevant elaboration-related attributes of the context which
5306 -- are no longer available or very expensive to recompute once analysis,
5307 -- resolution, and expansion are over.
5308
5309 Mark_Elaboration_Attributes
5310 (N_Id => Designator,
5311 Checks => True,
5312 Warnings => True);
5313
5314 if Debug_Flag_C then
5315 Write_Str ("==> subprogram spec ");
5316 Write_Name (Chars (Designator));
5317 Write_Str (" from ");
5318 Write_Location (Sloc (N));
5319 Write_Eol;
5320 Indent;
5321 end if;
5322
5323 Validate_RCI_Subprogram_Declaration (N);
5324 New_Overloaded_Entity (Designator);
5325 Check_Delayed_Subprogram (Designator);
5326
5327 -- If the type of the first formal of the current subprogram is a non-
5328 -- generic tagged private type, mark the subprogram as being a private
5329 -- primitive. Ditto if this is a function with controlling result, and
5330 -- the return type is currently private. In both cases, the type of the
5331 -- controlling argument or result must be in the current scope for the
5332 -- operation to be primitive.
5333
5334 if Has_Controlling_Result (Designator)
5335 and then Is_Private_Type (Etype (Designator))
5336 and then Scope (Etype (Designator)) = Current_Scope
5337 and then not Is_Generic_Actual_Type (Etype (Designator))
5338 then
5339 Set_Is_Private_Primitive (Designator);
5340
5341 elsif Present (First_Formal (Designator)) then
5342 declare
5343 Formal_Typ : constant Entity_Id :=
5344 Etype (First_Formal (Designator));
5345 begin
5346 Set_Is_Private_Primitive (Designator,
5347 Is_Tagged_Type (Formal_Typ)
5348 and then Scope (Formal_Typ) = Current_Scope
5349 and then Is_Private_Type (Formal_Typ)
5350 and then not Is_Generic_Actual_Type (Formal_Typ));
5351 end;
5352 end if;
5353
5354 -- Ada 2005 (AI-251): Abstract interface primitives must be abstract
5355 -- or null.
5356
5357 if Ada_Version >= Ada_2005
5358 and then Comes_From_Source (N)
5359 and then Is_Dispatching_Operation (Designator)
5360 then
5361 declare
5362 E : Entity_Id;
5363 Etyp : Entity_Id;
5364
5365 begin
5366 if Has_Controlling_Result (Designator) then
5367 Etyp := Etype (Designator);
5368
5369 else
5370 E := First_Entity (Designator);
5371 while Present (E)
5372 and then Is_Formal (E)
5373 and then not Is_Controlling_Formal (E)
5374 loop
5375 Next_Entity (E);
5376 end loop;
5377
5378 Etyp := Etype (E);
5379 end if;
5380
5381 if Is_Access_Type (Etyp) then
5382 Etyp := Directly_Designated_Type (Etyp);
5383 end if;
5384
5385 if Is_Interface (Etyp)
5386 and then not Is_Abstract_Subprogram (Designator)
5387 and then not (Ekind (Designator) = E_Procedure
5388 and then Null_Present (Specification (N)))
5389 then
5390 Error_Msg_Name_1 := Chars (Defining_Entity (N));
5391
5392 -- Specialize error message based on procedures vs. functions,
5393 -- since functions can't be null subprograms.
5394
5395 if Ekind (Designator) = E_Procedure then
5396 Error_Msg_N
5397 ("interface procedure % must be abstract or null", N);
5398 else
5399 Error_Msg_N
5400 ("interface function % must be abstract", N);
5401 end if;
5402 end if;
5403 end;
5404 end if;
5405
5406 -- What is the following code for, it used to be
5407
5408 -- ??? Set_Suppress_Elaboration_Checks
5409 -- ??? (Designator, Elaboration_Checks_Suppressed (Designator));
5410
5411 -- The following seems equivalent, but a bit dubious
5412
5413 if Elaboration_Checks_Suppressed (Designator) then
5414 Set_Kill_Elaboration_Checks (Designator);
5415 end if;
5416
5417 -- For a compilation unit, set body required. This flag will only be
5418 -- reset if a valid Import or Interface pragma is processed later on.
5419
5420 if Nkind (Parent (N)) = N_Compilation_Unit then
5421 Set_Body_Required (Parent (N), True);
5422
5423 if Ada_Version >= Ada_2005
5424 and then Nkind (Specification (N)) = N_Procedure_Specification
5425 and then Null_Present (Specification (N))
5426 then
5427 Error_Msg_N
5428 ("null procedure cannot be declared at library level", N);
5429 end if;
5430 end if;
5431
5432 Generate_Reference_To_Formals (Designator);
5433 Check_Eliminated (Designator);
5434
5435 if Debug_Flag_C then
5436 Outdent;
5437 Write_Str ("<== subprogram spec ");
5438 Write_Name (Chars (Designator));
5439 Write_Str (" from ");
5440 Write_Location (Sloc (N));
5441 Write_Eol;
5442 end if;
5443
5444 -- Indicate that this is a protected operation, because it may be used
5445 -- in subsequent declarations within the protected type.
5446
5447 if Is_Protected_Type (Current_Scope) then
5448 Set_Convention (Designator, Convention_Protected);
5449 end if;
5450
5451 List_Inherited_Pre_Post_Aspects (Designator);
5452
5453 -- Process the aspects before establishing the proper categorization in
5454 -- case the subprogram is a compilation unit and one of its aspects is
5455 -- converted into a categorization pragma.
5456
5457 if Has_Aspects (N) then
5458 Analyze_Aspect_Specifications (N, Designator);
5459 end if;
5460
5461 if Scop /= Standard_Standard and then not Is_Child_Unit (Designator) then
5462 Set_Categorization_From_Scope (Designator, Scop);
5463
5464 -- Otherwise the unit is a compilation unit and/or a child unit. Set the
5465 -- proper categorization of the unit based on its pragmas.
5466
5467 else
5468 Push_Scope (Designator);
5469 Set_Categorization_From_Pragmas (N);
5470 Validate_Categorization_Dependency (N, Designator);
5471 Pop_Scope;
5472 end if;
5473 end Analyze_Subprogram_Declaration;
5474
5475 --------------------------------------
5476 -- Analyze_Subprogram_Specification --
5477 --------------------------------------
5478
5479 -- Reminder: N here really is a subprogram specification (not a subprogram
5480 -- declaration). This procedure is called to analyze the specification in
5481 -- both subprogram bodies and subprogram declarations (specs).
5482
5483 function Analyze_Subprogram_Specification (N : Node_Id) return Entity_Id is
5484 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean;
5485 -- Determine whether entity E denotes the spec or body of an invariant
5486 -- procedure.
5487
5488 ------------------------------------
5489 -- Is_Invariant_Procedure_Or_Body --
5490 ------------------------------------
5491
5492 function Is_Invariant_Procedure_Or_Body (E : Entity_Id) return Boolean is
5493 Decl : constant Node_Id := Unit_Declaration_Node (E);
5494 Spec : Entity_Id;
5495
5496 begin
5497 if Nkind (Decl) = N_Subprogram_Body then
5498 Spec := Corresponding_Spec (Decl);
5499 else
5500 Spec := E;
5501 end if;
5502
5503 return
5504 Present (Spec)
5505 and then Ekind (Spec) = E_Procedure
5506 and then (Is_Partial_Invariant_Procedure (Spec)
5507 or else Is_Invariant_Procedure (Spec));
5508 end Is_Invariant_Procedure_Or_Body;
5509
5510 -- Local variables
5511
5512 Designator : constant Entity_Id := Defining_Entity (N);
5513 Formals : constant List_Id := Parameter_Specifications (N);
5514
5515 -- Start of processing for Analyze_Subprogram_Specification
5516
5517 begin
5518 -- Proceed with analysis. Do not emit a cross-reference entry if the
5519 -- specification comes from an expression function, because it may be
5520 -- the completion of a previous declaration. If it is not, the cross-
5521 -- reference entry will be emitted for the new subprogram declaration.
5522
5523 if Nkind (Parent (N)) /= N_Expression_Function then
5524 Generate_Definition (Designator);
5525 end if;
5526
5527 if Nkind (N) = N_Function_Specification then
5528 Set_Ekind (Designator, E_Function);
5529 Set_Mechanism (Designator, Default_Mechanism);
5530 else
5531 Set_Ekind (Designator, E_Procedure);
5532 Set_Etype (Designator, Standard_Void_Type);
5533 end if;
5534
5535 -- Flag Is_Inlined_Always is True by default, and reversed to False for
5536 -- those subprograms which could be inlined in GNATprove mode (because
5537 -- Body_To_Inline is non-Empty) but should not be inlined.
5538
5539 if GNATprove_Mode then
5540 Set_Is_Inlined_Always (Designator);
5541 end if;
5542
5543 -- Introduce new scope for analysis of the formals and the return type
5544
5545 Set_Scope (Designator, Current_Scope);
5546
5547 if Present (Formals) then
5548 Push_Scope (Designator);
5549 Process_Formals (Formals, N);
5550
5551 -- Check dimensions in N for formals with default expression
5552
5553 Analyze_Dimension_Formals (N, Formals);
5554
5555 -- Ada 2005 (AI-345): If this is an overriding operation of an
5556 -- inherited interface operation, and the controlling type is
5557 -- a synchronized type, replace the type with its corresponding
5558 -- record, to match the proper signature of an overriding operation.
5559 -- Same processing for an access parameter whose designated type is
5560 -- derived from a synchronized interface.
5561
5562 -- This modification is not done for invariant procedures because
5563 -- the corresponding record may not necessarely be visible when the
5564 -- concurrent type acts as the full view of a private type.
5565
5566 -- package Pack is
5567 -- type Prot is private with Type_Invariant => ...;
5568 -- procedure ConcInvariant (Obj : Prot);
5569 -- private
5570 -- protected type Prot is ...;
5571 -- type Concurrent_Record_Prot is record ...;
5572 -- procedure ConcInvariant (Obj : Prot) is
5573 -- ...
5574 -- end ConcInvariant;
5575 -- end Pack;
5576
5577 -- In the example above, both the spec and body of the invariant
5578 -- procedure must utilize the private type as the controlling type.
5579
5580 if Ada_Version >= Ada_2005
5581 and then not Is_Invariant_Procedure_Or_Body (Designator)
5582 then
5583 declare
5584 Formal : Entity_Id;
5585 Formal_Typ : Entity_Id;
5586 Rec_Typ : Entity_Id;
5587 Desig_Typ : Entity_Id;
5588
5589 begin
5590 Formal := First_Formal (Designator);
5591 while Present (Formal) loop
5592 Formal_Typ := Etype (Formal);
5593
5594 if Is_Concurrent_Type (Formal_Typ)
5595 and then Present (Corresponding_Record_Type (Formal_Typ))
5596 then
5597 Rec_Typ := Corresponding_Record_Type (Formal_Typ);
5598
5599 if Present (Interfaces (Rec_Typ)) then
5600 Set_Etype (Formal, Rec_Typ);
5601 end if;
5602
5603 elsif Ekind (Formal_Typ) = E_Anonymous_Access_Type then
5604 Desig_Typ := Designated_Type (Formal_Typ);
5605
5606 if Is_Concurrent_Type (Desig_Typ)
5607 and then Present (Corresponding_Record_Type (Desig_Typ))
5608 then
5609 Rec_Typ := Corresponding_Record_Type (Desig_Typ);
5610
5611 if Present (Interfaces (Rec_Typ)) then
5612 Set_Directly_Designated_Type (Formal_Typ, Rec_Typ);
5613 end if;
5614 end if;
5615 end if;
5616
5617 Next_Formal (Formal);
5618 end loop;
5619 end;
5620 end if;
5621
5622 End_Scope;
5623
5624 -- The subprogram scope is pushed and popped around the processing of
5625 -- the return type for consistency with call above to Process_Formals
5626 -- (which itself can call Analyze_Return_Type), and to ensure that any
5627 -- itype created for the return type will be associated with the proper
5628 -- scope.
5629
5630 elsif Nkind (N) = N_Function_Specification then
5631 Push_Scope (Designator);
5632 Analyze_Return_Type (N);
5633 End_Scope;
5634 end if;
5635
5636 -- Function case
5637
5638 if Nkind (N) = N_Function_Specification then
5639
5640 -- Deal with operator symbol case
5641
5642 if Nkind (Designator) = N_Defining_Operator_Symbol then
5643 Valid_Operator_Definition (Designator);
5644 end if;
5645
5646 May_Need_Actuals (Designator);
5647
5648 -- Ada 2005 (AI-251): If the return type is abstract, verify that
5649 -- the subprogram is abstract also. This does not apply to renaming
5650 -- declarations, where abstractness is inherited, and to subprogram
5651 -- bodies generated for stream operations, which become renamings as
5652 -- bodies.
5653
5654 -- In case of primitives associated with abstract interface types
5655 -- the check is applied later (see Analyze_Subprogram_Declaration).
5656
5657 if Nkind (Original_Node (Parent (N))) not in
5658 N_Abstract_Subprogram_Declaration |
5659 N_Formal_Abstract_Subprogram_Declaration |
5660 N_Subprogram_Renaming_Declaration
5661 then
5662 if Is_Abstract_Type (Etype (Designator)) then
5663 Error_Msg_N
5664 ("function that returns abstract type must be abstract", N);
5665
5666 -- Ada 2012 (AI-0073): Extend this test to subprograms with an
5667 -- access result whose designated type is abstract.
5668
5669 elsif Ada_Version >= Ada_2012
5670 and then Nkind (Result_Definition (N)) = N_Access_Definition
5671 and then
5672 not Is_Class_Wide_Type (Designated_Type (Etype (Designator)))
5673 and then Is_Abstract_Type (Designated_Type (Etype (Designator)))
5674 then
5675 Error_Msg_N
5676 ("function whose access result designates abstract type "
5677 & "must be abstract", N);
5678 end if;
5679 end if;
5680 end if;
5681
5682 return Designator;
5683 end Analyze_Subprogram_Specification;
5684
5685 -----------------------
5686 -- Check_Conformance --
5687 -----------------------
5688
5689 procedure Check_Conformance
5690 (New_Id : Entity_Id;
5691 Old_Id : Entity_Id;
5692 Ctype : Conformance_Type;
5693 Errmsg : Boolean;
5694 Conforms : out Boolean;
5695 Err_Loc : Node_Id := Empty;
5696 Get_Inst : Boolean := False;
5697 Skip_Controlling_Formals : Boolean := False)
5698 is
5699 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id);
5700 -- Sets Conforms to False. If Errmsg is False, then that's all it does.
5701 -- If Errmsg is True, then processing continues to post an error message
5702 -- for conformance error on given node. Two messages are output. The
5703 -- first message points to the previous declaration with a general "no
5704 -- conformance" message. The second is the detailed reason, supplied as
5705 -- Msg. The parameter N provide information for a possible & insertion
5706 -- in the message, and also provides the location for posting the
5707 -- message in the absence of a specified Err_Loc location.
5708
5709 function Conventions_Match (Id1, Id2 : Entity_Id) return Boolean;
5710 -- True if the conventions of entities Id1 and Id2 match.
5711
5712 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean;
5713 -- True if the null exclusions of two formals of anonymous access type
5714 -- match.
5715
5716 -----------------------
5717 -- Conformance_Error --
5718 -----------------------
5719
5720 procedure Conformance_Error (Msg : String; N : Node_Id := New_Id) is
5721 Enode : Node_Id;
5722
5723 begin
5724 Conforms := False;
5725
5726 if Errmsg then
5727 if No (Err_Loc) then
5728 Enode := N;
5729 else
5730 Enode := Err_Loc;
5731 end if;
5732
5733 Error_Msg_Sloc := Sloc (Old_Id);
5734
5735 case Ctype is
5736 when Type_Conformant =>
5737 Error_Msg_N -- CODEFIX
5738 ("not type conformant with declaration#!", Enode);
5739
5740 when Mode_Conformant =>
5741 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5742 Error_Msg_N
5743 ("not mode conformant with operation inherited#!",
5744 Enode);
5745 else
5746 Error_Msg_N
5747 ("not mode conformant with declaration#!", Enode);
5748 end if;
5749
5750 when Subtype_Conformant =>
5751 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5752 Error_Msg_N
5753 ("not subtype conformant with operation inherited#!",
5754 Enode);
5755 else
5756 Error_Msg_N
5757 ("not subtype conformant with declaration#!", Enode);
5758 end if;
5759
5760 when Fully_Conformant =>
5761 if Nkind (Parent (Old_Id)) = N_Full_Type_Declaration then
5762 Error_Msg_N -- CODEFIX
5763 ("not fully conformant with operation inherited#!",
5764 Enode);
5765 else
5766 Error_Msg_N -- CODEFIX
5767 ("not fully conformant with declaration#!", Enode);
5768 end if;
5769 end case;
5770
5771 Error_Msg_NE (Msg, Enode, N);
5772 end if;
5773 end Conformance_Error;
5774
5775 -----------------------
5776 -- Conventions_Match --
5777 -----------------------
5778
5779 function Conventions_Match
5780 (Id1 : Entity_Id;
5781 Id2 : Entity_Id) return Boolean
5782 is
5783 begin
5784 -- Ignore the conventions of anonymous access-to-subprogram types
5785 -- and subprogram types because these are internally generated and
5786 -- the only way these may receive a convention is if they inherit
5787 -- the convention of a related subprogram.
5788
5789 if Ekind (Id1) in E_Anonymous_Access_Subprogram_Type
5790 | E_Subprogram_Type
5791 or else
5792 Ekind (Id2) in E_Anonymous_Access_Subprogram_Type
5793 | E_Subprogram_Type
5794 then
5795 return True;
5796
5797 -- Otherwise compare the conventions directly
5798
5799 else
5800 return Convention (Id1) = Convention (Id2);
5801 end if;
5802 end Conventions_Match;
5803
5804 ---------------------------
5805 -- Null_Exclusions_Match --
5806 ---------------------------
5807
5808 function Null_Exclusions_Match (F1, F2 : Entity_Id) return Boolean is
5809 begin
5810 if not Is_Anonymous_Access_Type (Etype (F1))
5811 or else not Is_Anonymous_Access_Type (Etype (F2))
5812 then
5813 return True;
5814 end if;
5815
5816 -- AI12-0289-1: Case of controlling access parameter; False if the
5817 -- partial view is untagged, the full view is tagged, and no explicit
5818 -- "not null". Note that at this point, we're processing the package
5819 -- body, so private/full types have been swapped. The Sloc test below
5820 -- is to detect the (legal) case where F1 comes after the full type
5821 -- declaration. This part is disabled pre-2005, because "not null" is
5822 -- not allowed on those language versions.
5823
5824 if Ada_Version >= Ada_2005
5825 and then Is_Controlling_Formal (F1)
5826 and then not Null_Exclusion_Present (Parent (F1))
5827 and then not Null_Exclusion_Present (Parent (F2))
5828 then
5829 declare
5830 D : constant Entity_Id := Directly_Designated_Type (Etype (F1));
5831 Partial_View_Of_Desig : constant Entity_Id :=
5832 Incomplete_Or_Partial_View (D);
5833 begin
5834 return No (Partial_View_Of_Desig)
5835 or else Is_Tagged_Type (Partial_View_Of_Desig)
5836 or else Sloc (D) < Sloc (F1);
5837 end;
5838
5839 -- Not a controlling parameter, or one or both views have an explicit
5840 -- "not null".
5841
5842 else
5843 return Null_Exclusion_Present (Parent (F1)) =
5844 Null_Exclusion_Present (Parent (F2));
5845 end if;
5846 end Null_Exclusions_Match;
5847
5848 -- Local Variables
5849
5850 Old_Type : constant Entity_Id := Etype (Old_Id);
5851 New_Type : constant Entity_Id := Etype (New_Id);
5852 Old_Formal : Entity_Id;
5853 New_Formal : Entity_Id;
5854 Old_Formal_Base : Entity_Id;
5855 New_Formal_Base : Entity_Id;
5856
5857 -- Start of processing for Check_Conformance
5858
5859 begin
5860 Conforms := True;
5861
5862 -- We need a special case for operators, since they don't appear
5863 -- explicitly.
5864
5865 if Ctype = Type_Conformant then
5866 if Ekind (New_Id) = E_Operator
5867 and then Operator_Matches_Spec (New_Id, Old_Id)
5868 then
5869 return;
5870 end if;
5871 end if;
5872
5873 -- If both are functions/operators, check return types conform
5874
5875 if Old_Type /= Standard_Void_Type
5876 and then
5877 New_Type /= Standard_Void_Type
5878 then
5879 -- If we are checking interface conformance we omit controlling
5880 -- arguments and result, because we are only checking the conformance
5881 -- of the remaining parameters.
5882
5883 if Has_Controlling_Result (Old_Id)
5884 and then Has_Controlling_Result (New_Id)
5885 and then Skip_Controlling_Formals
5886 then
5887 null;
5888
5889 elsif not Conforming_Types (Old_Type, New_Type, Ctype, Get_Inst) then
5890 if Ctype >= Subtype_Conformant
5891 and then not Predicates_Match (Old_Type, New_Type)
5892 then
5893 Conformance_Error
5894 ("\predicate of return type does not match!", New_Id);
5895 else
5896 Conformance_Error
5897 ("\return type does not match!", New_Id);
5898 end if;
5899
5900 return;
5901 end if;
5902
5903 -- Ada 2005 (AI-231): In case of anonymous access types check the
5904 -- null-exclusion and access-to-constant attributes match.
5905
5906 if Ada_Version >= Ada_2005
5907 and then Ekind (Etype (Old_Type)) = E_Anonymous_Access_Type
5908 and then
5909 (Can_Never_Be_Null (Old_Type) /= Can_Never_Be_Null (New_Type)
5910 or else Is_Access_Constant (Etype (Old_Type)) /=
5911 Is_Access_Constant (Etype (New_Type)))
5912 then
5913 Conformance_Error ("\return type does not match!", New_Id);
5914 return;
5915 end if;
5916
5917 -- If either is a function/operator and the other isn't, error
5918
5919 elsif Old_Type /= Standard_Void_Type
5920 or else New_Type /= Standard_Void_Type
5921 then
5922 Conformance_Error ("\functions can only match functions!", New_Id);
5923 return;
5924 end if;
5925
5926 -- In subtype conformant case, conventions must match (RM 6.3.1(16)).
5927 -- If this is a renaming as body, refine error message to indicate that
5928 -- the conflict is with the original declaration. If the entity is not
5929 -- frozen, the conventions don't have to match, the one of the renamed
5930 -- entity is inherited.
5931
5932 if Ctype >= Subtype_Conformant then
5933 if not Conventions_Match (Old_Id, New_Id) then
5934 if not Is_Frozen (New_Id) then
5935 null;
5936
5937 elsif Present (Err_Loc)
5938 and then Nkind (Err_Loc) = N_Subprogram_Renaming_Declaration
5939 and then Present (Corresponding_Spec (Err_Loc))
5940 then
5941 Error_Msg_Name_1 := Chars (New_Id);
5942 Error_Msg_Name_2 :=
5943 Name_Ada + Convention_Id'Pos (Convention (New_Id));
5944 Conformance_Error ("\prior declaration for% has convention %!");
5945 return;
5946
5947 else
5948 Conformance_Error ("\calling conventions do not match!");
5949 return;
5950 end if;
5951 else
5952 Check_Formal_Subprogram_Conformance
5953 (New_Id, Old_Id, Err_Loc, Errmsg, Conforms);
5954
5955 if not Conforms then
5956 return;
5957 end if;
5958 end if;
5959 end if;
5960
5961 -- Deal with parameters
5962
5963 -- Note: we use the entity information, rather than going directly
5964 -- to the specification in the tree. This is not only simpler, but
5965 -- absolutely necessary for some cases of conformance tests between
5966 -- operators, where the declaration tree simply does not exist.
5967
5968 Old_Formal := First_Formal (Old_Id);
5969 New_Formal := First_Formal (New_Id);
5970 while Present (Old_Formal) and then Present (New_Formal) loop
5971 if Is_Controlling_Formal (Old_Formal)
5972 and then Is_Controlling_Formal (New_Formal)
5973 and then Skip_Controlling_Formals
5974 then
5975 -- The controlling formals will have different types when
5976 -- comparing an interface operation with its match, but both
5977 -- or neither must be access parameters.
5978
5979 if Is_Access_Type (Etype (Old_Formal))
5980 =
5981 Is_Access_Type (Etype (New_Formal))
5982 then
5983 goto Skip_Controlling_Formal;
5984 else
5985 Conformance_Error
5986 ("\access parameter does not match!", New_Formal);
5987 end if;
5988 end if;
5989
5990 -- Ada 2012: Mode conformance also requires that formal parameters
5991 -- be both aliased, or neither.
5992
5993 if Ctype >= Mode_Conformant and then Ada_Version >= Ada_2012 then
5994 if Is_Aliased (Old_Formal) /= Is_Aliased (New_Formal) then
5995 Conformance_Error
5996 ("\aliased parameter mismatch!", New_Formal);
5997 end if;
5998 end if;
5999
6000 if Ctype = Fully_Conformant then
6001
6002 -- Names must match. Error message is more accurate if we do
6003 -- this before checking that the types of the formals match.
6004
6005 if Chars (Old_Formal) /= Chars (New_Formal) then
6006 Conformance_Error ("\name& does not match!", New_Formal);
6007
6008 -- Set error posted flag on new formal as well to stop
6009 -- junk cascaded messages in some cases.
6010
6011 Set_Error_Posted (New_Formal);
6012 return;
6013 end if;
6014
6015 -- Null exclusion must match
6016
6017 if not Null_Exclusions_Match (Old_Formal, New_Formal) then
6018 Conformance_Error
6019 ("\null exclusion for& does not match", New_Formal);
6020
6021 -- Mark error posted on the new formal to avoid duplicated
6022 -- complaint about types not matching.
6023
6024 Set_Error_Posted (New_Formal);
6025 end if;
6026 end if;
6027
6028 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
6029 -- case occurs whenever a subprogram is being renamed and one of its
6030 -- parameters imposes a null exclusion. For example:
6031
6032 -- type T is null record;
6033 -- type Acc_T is access T;
6034 -- subtype Acc_T_Sub is Acc_T;
6035
6036 -- procedure P (Obj : not null Acc_T_Sub); -- itype
6037 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
6038 -- renames P;
6039
6040 Old_Formal_Base := Etype (Old_Formal);
6041 New_Formal_Base := Etype (New_Formal);
6042
6043 if Get_Inst then
6044 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
6045 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
6046 end if;
6047
6048 -- Types must always match. In the visible part of an instance,
6049 -- usual overloading rules for dispatching operations apply, and
6050 -- we check base types (not the actual subtypes).
6051
6052 if In_Instance_Visible_Part
6053 and then Is_Dispatching_Operation (New_Id)
6054 then
6055 if not Conforming_Types
6056 (T1 => Base_Type (Etype (Old_Formal)),
6057 T2 => Base_Type (Etype (New_Formal)),
6058 Ctype => Ctype,
6059 Get_Inst => Get_Inst)
6060 then
6061 Conformance_Error ("\type of & does not match!", New_Formal);
6062 return;
6063 end if;
6064
6065 elsif not Conforming_Types
6066 (T1 => Old_Formal_Base,
6067 T2 => New_Formal_Base,
6068 Ctype => Ctype,
6069 Get_Inst => Get_Inst)
6070 then
6071 -- Don't give error message if old type is Any_Type. This test
6072 -- avoids some cascaded errors, e.g. in case of a bad spec.
6073
6074 if Errmsg and then Old_Formal_Base = Any_Type then
6075 Conforms := False;
6076 else
6077 if Ctype >= Subtype_Conformant
6078 and then
6079 not Predicates_Match (Old_Formal_Base, New_Formal_Base)
6080 then
6081 Conformance_Error
6082 ("\predicate of & does not match!", New_Formal);
6083 else
6084 Conformance_Error
6085 ("\type of & does not match!", New_Formal);
6086
6087 if not Dimensions_Match (Old_Formal_Base, New_Formal_Base)
6088 then
6089 Error_Msg_N ("\dimensions mismatch!", New_Formal);
6090 end if;
6091 end if;
6092 end if;
6093
6094 return;
6095 end if;
6096
6097 -- For mode conformance, mode must match
6098
6099 if Ctype >= Mode_Conformant then
6100 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
6101 if Ekind (New_Id) not in E_Function | E_Procedure
6102 or else not Is_Primitive_Wrapper (New_Id)
6103 then
6104 Conformance_Error ("\mode of & does not match!", New_Formal);
6105
6106 else
6107 declare
6108 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
6109 begin
6110 if Is_Protected_Type (Corresponding_Concurrent_Type (T))
6111 then
6112 Conforms := False;
6113
6114 if Errmsg then
6115 Error_Msg_PT (New_Id, Ultimate_Alias (Old_Id));
6116 end if;
6117 else
6118 Conformance_Error
6119 ("\mode of & does not match!", New_Formal);
6120 end if;
6121 end;
6122 end if;
6123
6124 return;
6125
6126 elsif Is_Access_Type (Old_Formal_Base)
6127 and then Is_Access_Type (New_Formal_Base)
6128 and then Is_Access_Constant (Old_Formal_Base) /=
6129 Is_Access_Constant (New_Formal_Base)
6130 then
6131 Conformance_Error
6132 ("\constant modifier does not match!", New_Formal);
6133 return;
6134 end if;
6135 end if;
6136
6137 if Ctype >= Subtype_Conformant then
6138
6139 -- Ada 2005 (AI-231): In case of anonymous access types check
6140 -- the null-exclusion and access-to-constant attributes must
6141 -- match. For null exclusion, we test the types rather than the
6142 -- formals themselves, since the attribute is only set reliably
6143 -- on the formals in the Ada 95 case, and we exclude the case
6144 -- where Old_Formal is marked as controlling, to avoid errors
6145 -- when matching completing bodies with dispatching declarations
6146 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
6147
6148 if Ada_Version >= Ada_2005
6149 and then Is_Anonymous_Access_Type (Etype (Old_Formal))
6150 and then Is_Anonymous_Access_Type (Etype (New_Formal))
6151 and then
6152 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
6153 Can_Never_Be_Null (Etype (New_Formal))
6154 and then
6155 not Is_Controlling_Formal (Old_Formal))
6156 or else
6157 Is_Access_Constant (Etype (Old_Formal)) /=
6158 Is_Access_Constant (Etype (New_Formal)))
6159
6160 -- Do not complain if error already posted on New_Formal. This
6161 -- avoids some redundant error messages.
6162
6163 and then not Error_Posted (New_Formal)
6164 then
6165 -- It is allowed to omit the null-exclusion in case of stream
6166 -- attribute subprograms. We recognize stream subprograms
6167 -- through their TSS-generated suffix.
6168
6169 declare
6170 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
6171
6172 begin
6173 if TSS_Name /= TSS_Stream_Read
6174 and then TSS_Name /= TSS_Stream_Write
6175 and then TSS_Name /= TSS_Stream_Input
6176 and then TSS_Name /= TSS_Stream_Output
6177 then
6178 -- Here we have a definite conformance error. It is worth
6179 -- special casing the error message for the case of a
6180 -- controlling formal (which excludes null).
6181
6182 if Is_Controlling_Formal (New_Formal) then
6183 Error_Msg_Node_2 := Scope (New_Formal);
6184 Conformance_Error
6185 ("\controlling formal & of & excludes null, "
6186 & "declaration must exclude null as well",
6187 New_Formal);
6188
6189 -- Normal case (couldn't we give more detail here???)
6190
6191 else
6192 Conformance_Error
6193 ("\type of & does not match!", New_Formal);
6194 end if;
6195
6196 return;
6197 end if;
6198 end;
6199 end if;
6200 end if;
6201
6202 -- Full conformance checks
6203
6204 if Ctype = Fully_Conformant then
6205
6206 -- We have checked already that names match
6207
6208 if Parameter_Mode (Old_Formal) = E_In_Parameter then
6209
6210 -- Check default expressions for in parameters
6211
6212 declare
6213 NewD : constant Boolean :=
6214 Present (Default_Value (New_Formal));
6215 OldD : constant Boolean :=
6216 Present (Default_Value (Old_Formal));
6217 begin
6218 if NewD or OldD then
6219
6220 -- The old default value has been analyzed because the
6221 -- current full declaration will have frozen everything
6222 -- before. The new default value has not been analyzed,
6223 -- so analyze it now before we check for conformance.
6224
6225 if NewD then
6226 Push_Scope (New_Id);
6227 Preanalyze_Spec_Expression
6228 (Default_Value (New_Formal), Etype (New_Formal));
6229 End_Scope;
6230 end if;
6231
6232 if not (NewD and OldD)
6233 or else not Fully_Conformant_Expressions
6234 (Default_Value (Old_Formal),
6235 Default_Value (New_Formal))
6236 then
6237 Conformance_Error
6238 ("\default expression for & does not match!",
6239 New_Formal);
6240 return;
6241 end if;
6242 end if;
6243 end;
6244 end if;
6245 end if;
6246
6247 -- A couple of special checks for Ada 83 mode. These checks are
6248 -- skipped if either entity is an operator in package Standard,
6249 -- or if either old or new instance is not from the source program.
6250
6251 if Ada_Version = Ada_83
6252 and then Sloc (Old_Id) > Standard_Location
6253 and then Sloc (New_Id) > Standard_Location
6254 and then Comes_From_Source (Old_Id)
6255 and then Comes_From_Source (New_Id)
6256 then
6257 declare
6258 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
6259 New_Param : constant Node_Id := Declaration_Node (New_Formal);
6260
6261 begin
6262 -- Explicit IN must be present or absent in both cases. This
6263 -- test is required only in the full conformance case.
6264
6265 if In_Present (Old_Param) /= In_Present (New_Param)
6266 and then Ctype = Fully_Conformant
6267 then
6268 Conformance_Error
6269 ("\(Ada 83) IN must appear in both declarations",
6270 New_Formal);
6271 return;
6272 end if;
6273
6274 -- Grouping (use of comma in param lists) must be the same
6275 -- This is where we catch a misconformance like:
6276
6277 -- A, B : Integer
6278 -- A : Integer; B : Integer
6279
6280 -- which are represented identically in the tree except
6281 -- for the setting of the flags More_Ids and Prev_Ids.
6282
6283 if More_Ids (Old_Param) /= More_Ids (New_Param)
6284 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
6285 then
6286 Conformance_Error
6287 ("\grouping of & does not match!", New_Formal);
6288 return;
6289 end if;
6290 end;
6291 end if;
6292
6293 -- This label is required when skipping controlling formals
6294
6295 <<Skip_Controlling_Formal>>
6296
6297 Next_Formal (Old_Formal);
6298 Next_Formal (New_Formal);
6299 end loop;
6300
6301 if Present (Old_Formal) then
6302 Conformance_Error ("\too few parameters!");
6303 return;
6304
6305 elsif Present (New_Formal) then
6306 Conformance_Error ("\too many parameters!", New_Formal);
6307 return;
6308 end if;
6309 end Check_Conformance;
6310
6311 -----------------------
6312 -- Check_Conventions --
6313 -----------------------
6314
6315 procedure Check_Conventions (Typ : Entity_Id) is
6316 Ifaces_List : Elist_Id;
6317
6318 procedure Check_Convention (Op : Entity_Id);
6319 -- Verify that the convention of inherited dispatching operation Op is
6320 -- consistent among all subprograms it overrides. In order to minimize
6321 -- the search, Search_From is utilized to designate a specific point in
6322 -- the list rather than iterating over the whole list once more.
6323
6324 ----------------------
6325 -- Check_Convention --
6326 ----------------------
6327
6328 procedure Check_Convention (Op : Entity_Id) is
6329 Op_Conv : constant Convention_Id := Convention (Op);
6330 Iface_Conv : Convention_Id;
6331 Iface_Elmt : Elmt_Id;
6332 Iface_Prim_Elmt : Elmt_Id;
6333 Iface_Prim : Entity_Id;
6334
6335 begin
6336 Iface_Elmt := First_Elmt (Ifaces_List);
6337 while Present (Iface_Elmt) loop
6338 Iface_Prim_Elmt :=
6339 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
6340 while Present (Iface_Prim_Elmt) loop
6341 Iface_Prim := Node (Iface_Prim_Elmt);
6342 Iface_Conv := Convention (Iface_Prim);
6343
6344 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
6345 and then Iface_Conv /= Op_Conv
6346 then
6347 Error_Msg_N
6348 ("inconsistent conventions in primitive operations", Typ);
6349
6350 Error_Msg_Name_1 := Chars (Op);
6351 Error_Msg_Name_2 := Get_Convention_Name (Op_Conv);
6352 Error_Msg_Sloc := Sloc (Op);
6353
6354 if Comes_From_Source (Op) or else No (Alias (Op)) then
6355 if not Present (Overridden_Operation (Op)) then
6356 Error_Msg_N ("\\primitive % defined #", Typ);
6357 else
6358 Error_Msg_N
6359 ("\\overriding operation % with "
6360 & "convention % defined #", Typ);
6361 end if;
6362
6363 else pragma Assert (Present (Alias (Op)));
6364 Error_Msg_Sloc := Sloc (Alias (Op));
6365 Error_Msg_N ("\\inherited operation % with "
6366 & "convention % defined #", Typ);
6367 end if;
6368
6369 Error_Msg_Name_1 := Chars (Op);
6370 Error_Msg_Name_2 := Get_Convention_Name (Iface_Conv);
6371 Error_Msg_Sloc := Sloc (Iface_Prim);
6372 Error_Msg_N ("\\overridden operation % with "
6373 & "convention % defined #", Typ);
6374
6375 -- Avoid cascading errors
6376
6377 return;
6378 end if;
6379
6380 Next_Elmt (Iface_Prim_Elmt);
6381 end loop;
6382
6383 Next_Elmt (Iface_Elmt);
6384 end loop;
6385 end Check_Convention;
6386
6387 -- Local variables
6388
6389 Prim_Op : Entity_Id;
6390 Prim_Op_Elmt : Elmt_Id;
6391
6392 -- Start of processing for Check_Conventions
6393
6394 begin
6395 if not Has_Interfaces (Typ) then
6396 return;
6397 end if;
6398
6399 Collect_Interfaces (Typ, Ifaces_List);
6400
6401 -- The algorithm checks every overriding dispatching operation against
6402 -- all the corresponding overridden dispatching operations, detecting
6403 -- differences in conventions.
6404
6405 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
6406 while Present (Prim_Op_Elmt) loop
6407 Prim_Op := Node (Prim_Op_Elmt);
6408
6409 -- A small optimization: skip the predefined dispatching operations
6410 -- since they always have the same convention.
6411
6412 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
6413 Check_Convention (Prim_Op);
6414 end if;
6415
6416 Next_Elmt (Prim_Op_Elmt);
6417 end loop;
6418 end Check_Conventions;
6419
6420 ------------------------------
6421 -- Check_Delayed_Subprogram --
6422 ------------------------------
6423
6424 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
6425 procedure Possible_Freeze (T : Entity_Id);
6426 -- T is the type of either a formal parameter or of the return type. If
6427 -- T is not yet frozen and needs a delayed freeze, then the subprogram
6428 -- itself must be delayed.
6429
6430 ---------------------
6431 -- Possible_Freeze --
6432 ---------------------
6433
6434 procedure Possible_Freeze (T : Entity_Id) is
6435 Scop : constant Entity_Id := Scope (Designator);
6436
6437 begin
6438 -- If the subprogram appears within a package instance (which may be
6439 -- the wrapper package of a subprogram instance) the freeze node for
6440 -- that package will freeze the subprogram at the proper place, so
6441 -- do not emit a freeze node for the subprogram, given that it may
6442 -- appear in the wrong scope.
6443
6444 if Ekind (Scop) = E_Package
6445 and then not Comes_From_Source (Scop)
6446 and then Is_Generic_Instance (Scop)
6447 then
6448 null;
6449
6450 elsif Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
6451 Set_Has_Delayed_Freeze (Designator);
6452
6453 elsif Is_Access_Type (T)
6454 and then Has_Delayed_Freeze (Designated_Type (T))
6455 and then not Is_Frozen (Designated_Type (T))
6456 then
6457 Set_Has_Delayed_Freeze (Designator);
6458 end if;
6459 end Possible_Freeze;
6460
6461 -- Local variables
6462
6463 F : Entity_Id;
6464
6465 -- Start of processing for Check_Delayed_Subprogram
6466
6467 begin
6468 -- All subprograms, including abstract subprograms, may need a freeze
6469 -- node if some formal type or the return type needs one.
6470
6471 Possible_Freeze (Etype (Designator));
6472 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
6473
6474 -- Need delayed freeze if any of the formal types themselves need a
6475 -- delayed freeze and are not yet frozen.
6476
6477 F := First_Formal (Designator);
6478 while Present (F) loop
6479 Possible_Freeze (Etype (F));
6480 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6481 Next_Formal (F);
6482 end loop;
6483
6484 -- Mark functions that return by reference. Note that it cannot be done
6485 -- for delayed_freeze subprograms because the underlying returned type
6486 -- may not be known yet (for private types).
6487
6488 if not Has_Delayed_Freeze (Designator) and then Expander_Active then
6489 declare
6490 Typ : constant Entity_Id := Etype (Designator);
6491 Utyp : constant Entity_Id := Underlying_Type (Typ);
6492
6493 begin
6494 if Is_Limited_View (Typ) then
6495 Set_Returns_By_Ref (Designator);
6496
6497 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
6498 Set_Returns_By_Ref (Designator);
6499 end if;
6500 end;
6501 end if;
6502 end Check_Delayed_Subprogram;
6503
6504 ------------------------------------
6505 -- Check_Discriminant_Conformance --
6506 ------------------------------------
6507
6508 procedure Check_Discriminant_Conformance
6509 (N : Node_Id;
6510 Prev : Entity_Id;
6511 Prev_Loc : Node_Id)
6512 is
6513 Old_Discr : Entity_Id := First_Discriminant (Prev);
6514 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6515 New_Discr_Id : Entity_Id;
6516 New_Discr_Type : Entity_Id;
6517
6518 procedure Conformance_Error (Msg : String; N : Node_Id);
6519 -- Post error message for conformance error on given node. Two messages
6520 -- are output. The first points to the previous declaration with a
6521 -- general "no conformance" message. The second is the detailed reason,
6522 -- supplied as Msg. The parameter N provide information for a possible
6523 -- & insertion in the message.
6524
6525 -----------------------
6526 -- Conformance_Error --
6527 -----------------------
6528
6529 procedure Conformance_Error (Msg : String; N : Node_Id) is
6530 begin
6531 Error_Msg_Sloc := Sloc (Prev_Loc);
6532 Error_Msg_N -- CODEFIX
6533 ("not fully conformant with declaration#!", N);
6534 Error_Msg_NE (Msg, N, N);
6535 end Conformance_Error;
6536
6537 -- Start of processing for Check_Discriminant_Conformance
6538
6539 begin
6540 while Present (Old_Discr) and then Present (New_Discr) loop
6541 New_Discr_Id := Defining_Identifier (New_Discr);
6542
6543 -- The subtype mark of the discriminant on the full type has not
6544 -- been analyzed so we do it here. For an access discriminant a new
6545 -- type is created.
6546
6547 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6548 New_Discr_Type :=
6549 Access_Definition (N, Discriminant_Type (New_Discr));
6550
6551 else
6552 Find_Type (Discriminant_Type (New_Discr));
6553 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
6554
6555 -- Ada 2005: if the discriminant definition carries a null
6556 -- exclusion, create an itype to check properly for consistency
6557 -- with partial declaration.
6558
6559 if Is_Access_Type (New_Discr_Type)
6560 and then Null_Exclusion_Present (New_Discr)
6561 then
6562 New_Discr_Type :=
6563 Create_Null_Excluding_Itype
6564 (T => New_Discr_Type,
6565 Related_Nod => New_Discr,
6566 Scope_Id => Current_Scope);
6567 end if;
6568 end if;
6569
6570 if not Conforming_Types
6571 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6572 then
6573 Conformance_Error ("type of & does not match!", New_Discr_Id);
6574 return;
6575 else
6576 -- Treat the new discriminant as an occurrence of the old one,
6577 -- for navigation purposes, and fill in some semantic
6578 -- information, for completeness.
6579
6580 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6581 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6582 Set_Scope (New_Discr_Id, Scope (Old_Discr));
6583 end if;
6584
6585 -- Names must match
6586
6587 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6588 Conformance_Error ("name & does not match!", New_Discr_Id);
6589 return;
6590 end if;
6591
6592 -- Default expressions must match
6593
6594 declare
6595 NewD : constant Boolean :=
6596 Present (Expression (New_Discr));
6597 OldD : constant Boolean :=
6598 Present (Expression (Parent (Old_Discr)));
6599
6600 begin
6601 if NewD or OldD then
6602
6603 -- The old default value has been analyzed and expanded,
6604 -- because the current full declaration will have frozen
6605 -- everything before. The new default values have not been
6606 -- expanded, so expand now to check conformance.
6607
6608 if NewD then
6609 Preanalyze_Spec_Expression
6610 (Expression (New_Discr), New_Discr_Type);
6611 end if;
6612
6613 if not (NewD and OldD)
6614 or else not Fully_Conformant_Expressions
6615 (Expression (Parent (Old_Discr)),
6616 Expression (New_Discr))
6617
6618 then
6619 Conformance_Error
6620 ("default expression for & does not match!",
6621 New_Discr_Id);
6622 return;
6623 end if;
6624 end if;
6625 end;
6626
6627 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6628
6629 if Ada_Version = Ada_83 then
6630 declare
6631 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6632
6633 begin
6634 -- Grouping (use of comma in param lists) must be the same
6635 -- This is where we catch a misconformance like:
6636
6637 -- A, B : Integer
6638 -- A : Integer; B : Integer
6639
6640 -- which are represented identically in the tree except
6641 -- for the setting of the flags More_Ids and Prev_Ids.
6642
6643 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6644 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6645 then
6646 Conformance_Error
6647 ("grouping of & does not match!", New_Discr_Id);
6648 return;
6649 end if;
6650 end;
6651 end if;
6652
6653 Next_Discriminant (Old_Discr);
6654 Next (New_Discr);
6655 end loop;
6656
6657 if Present (Old_Discr) then
6658 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6659 return;
6660
6661 elsif Present (New_Discr) then
6662 Conformance_Error
6663 ("too many discriminants!", Defining_Identifier (New_Discr));
6664 return;
6665 end if;
6666 end Check_Discriminant_Conformance;
6667
6668 -----------------------------------------
6669 -- Check_Formal_Subprogram_Conformance --
6670 -----------------------------------------
6671
6672 procedure Check_Formal_Subprogram_Conformance
6673 (New_Id : Entity_Id;
6674 Old_Id : Entity_Id;
6675 Err_Loc : Node_Id;
6676 Errmsg : Boolean;
6677 Conforms : out Boolean)
6678 is
6679 N : Node_Id;
6680 begin
6681 Conforms := True;
6682
6683 if Is_Formal_Subprogram (Old_Id)
6684 or else Is_Formal_Subprogram (New_Id)
6685 or else (Is_Subprogram (New_Id)
6686 and then Present (Alias (New_Id))
6687 and then Is_Formal_Subprogram (Alias (New_Id)))
6688 then
6689 if Present (Err_Loc) then
6690 N := Err_Loc;
6691 else
6692 N := New_Id;
6693 end if;
6694
6695 Conforms := False;
6696
6697 if Errmsg then
6698 Error_Msg_Sloc := Sloc (Old_Id);
6699 Error_Msg_N ("not subtype conformant with declaration#!", N);
6700 Error_Msg_NE
6701 ("\formal subprograms are not subtype conformant "
6702 & "(RM 6.3.1 (17/3))", N, New_Id);
6703 end if;
6704 end if;
6705 end Check_Formal_Subprogram_Conformance;
6706
6707 procedure Check_Formal_Subprogram_Conformance
6708 (New_Id : Entity_Id;
6709 Old_Id : Entity_Id;
6710 Err_Loc : Node_Id := Empty)
6711 is
6712 Ignore : Boolean;
6713 begin
6714 Check_Formal_Subprogram_Conformance
6715 (New_Id, Old_Id, Err_Loc, True, Ignore);
6716 end Check_Formal_Subprogram_Conformance;
6717
6718 ----------------------------
6719 -- Check_Fully_Conformant --
6720 ----------------------------
6721
6722 procedure Check_Fully_Conformant
6723 (New_Id : Entity_Id;
6724 Old_Id : Entity_Id;
6725 Err_Loc : Node_Id := Empty)
6726 is
6727 Result : Boolean;
6728 pragma Warnings (Off, Result);
6729 begin
6730 Check_Conformance
6731 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6732 end Check_Fully_Conformant;
6733
6734 --------------------------
6735 -- Check_Limited_Return --
6736 --------------------------
6737
6738 procedure Check_Limited_Return
6739 (N : Node_Id;
6740 Expr : Node_Id;
6741 R_Type : Entity_Id)
6742 is
6743 begin
6744 -- Ada 2005 (AI-318-02): Return-by-reference types have been removed and
6745 -- replaced by anonymous access results. This is an incompatibility with
6746 -- Ada 95. Not clear whether this should be enforced yet or perhaps
6747 -- controllable with special switch. ???
6748
6749 -- A limited interface that is not immutably limited is OK
6750
6751 if Is_Limited_Interface (R_Type)
6752 and then
6753 not (Is_Task_Interface (R_Type)
6754 or else Is_Protected_Interface (R_Type)
6755 or else Is_Synchronized_Interface (R_Type))
6756 then
6757 null;
6758
6759 elsif Is_Limited_Type (R_Type)
6760 and then not Is_Interface (R_Type)
6761 and then Comes_From_Source (N)
6762 and then not In_Instance_Body
6763 and then not OK_For_Limited_Init_In_05 (R_Type, Expr)
6764 then
6765 -- Error in Ada 2005
6766
6767 if Ada_Version >= Ada_2005
6768 and then not Debug_Flag_Dot_L
6769 and then not GNAT_Mode
6770 then
6771 Error_Msg_N
6772 ("(Ada 2005) cannot copy object of a limited type "
6773 & "(RM-2005 6.5(5.5/2))", Expr);
6774
6775 if Is_Limited_View (R_Type) then
6776 Error_Msg_N
6777 ("\return by reference not permitted in Ada 2005", Expr);
6778 end if;
6779
6780 -- Warn in Ada 95 mode, to give folks a heads up about this
6781 -- incompatibility.
6782
6783 -- In GNAT mode, this is just a warning, to allow it to be evilly
6784 -- turned off. Otherwise it is a real error.
6785
6786 -- In a generic context, simplify the warning because it makes no
6787 -- sense to discuss pass-by-reference or copy.
6788
6789 elsif Warn_On_Ada_2005_Compatibility or GNAT_Mode then
6790 if Inside_A_Generic then
6791 Error_Msg_N
6792 ("return of limited object not permitted in Ada 2005 "
6793 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6794
6795 elsif Is_Limited_View (R_Type) then
6796 Error_Msg_N
6797 ("return by reference not permitted in Ada 2005 "
6798 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6799 else
6800 Error_Msg_N
6801 ("cannot copy object of a limited type in Ada 2005 "
6802 & "(RM-2005 6.5(5.5/2))?y?", Expr);
6803 end if;
6804
6805 -- Ada 95 mode, and compatibility warnings disabled
6806
6807 else
6808 pragma Assert (Ada_Version <= Ada_95);
6809 pragma Assert (not (Warn_On_Ada_2005_Compatibility or GNAT_Mode));
6810 return; -- skip continuation messages below
6811 end if;
6812
6813 if not Inside_A_Generic then
6814 Error_Msg_N
6815 ("\consider switching to return of access type", Expr);
6816 Explain_Limited_Type (R_Type, Expr);
6817 end if;
6818 end if;
6819 end Check_Limited_Return;
6820
6821 ---------------------------
6822 -- Check_Mode_Conformant --
6823 ---------------------------
6824
6825 procedure Check_Mode_Conformant
6826 (New_Id : Entity_Id;
6827 Old_Id : Entity_Id;
6828 Err_Loc : Node_Id := Empty;
6829 Get_Inst : Boolean := False)
6830 is
6831 Result : Boolean;
6832 pragma Warnings (Off, Result);
6833 begin
6834 Check_Conformance
6835 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6836 end Check_Mode_Conformant;
6837
6838 --------------------------------
6839 -- Check_Overriding_Indicator --
6840 --------------------------------
6841
6842 procedure Check_Overriding_Indicator
6843 (Subp : Entity_Id;
6844 Overridden_Subp : Entity_Id;
6845 Is_Primitive : Boolean)
6846 is
6847 Decl : Node_Id;
6848 Spec : Node_Id;
6849
6850 begin
6851 -- No overriding indicator for literals
6852
6853 if Ekind (Subp) = E_Enumeration_Literal then
6854 return;
6855
6856 elsif Ekind (Subp) = E_Entry then
6857 Decl := Parent (Subp);
6858
6859 -- No point in analyzing a malformed operator
6860
6861 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6862 and then Error_Posted (Subp)
6863 then
6864 return;
6865
6866 else
6867 Decl := Unit_Declaration_Node (Subp);
6868 end if;
6869
6870 if Nkind (Decl) in N_Subprogram_Body
6871 | N_Subprogram_Body_Stub
6872 | N_Subprogram_Declaration
6873 | N_Abstract_Subprogram_Declaration
6874 | N_Subprogram_Renaming_Declaration
6875 then
6876 Spec := Specification (Decl);
6877
6878 elsif Nkind (Decl) = N_Entry_Declaration then
6879 Spec := Decl;
6880
6881 else
6882 return;
6883 end if;
6884
6885 -- An overriding indication is illegal on a subprogram declared
6886 -- in a protected body, where there is no operation to override.
6887
6888 if (Must_Override (Spec) or else Must_Not_Override (Spec))
6889 and then Is_List_Member (Decl)
6890 and then Present (Parent (List_Containing (Decl)))
6891 and then Nkind (Parent (List_Containing (Decl))) = N_Protected_Body
6892 then
6893 Error_Msg_N
6894 ("illegal overriding indication in protected body", Decl);
6895 return;
6896 end if;
6897
6898 -- The overriding operation is type conformant with the overridden one,
6899 -- but the names of the formals are not required to match. If the names
6900 -- appear permuted in the overriding operation, this is a possible
6901 -- source of confusion that is worth diagnosing. Controlling formals
6902 -- often carry names that reflect the type, and it is not worthwhile
6903 -- requiring that their names match.
6904
6905 if Present (Overridden_Subp)
6906 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6907 then
6908 declare
6909 Form1 : Entity_Id;
6910 Form2 : Entity_Id;
6911
6912 begin
6913 Form1 := First_Formal (Subp);
6914 Form2 := First_Formal (Overridden_Subp);
6915
6916 -- If the overriding operation is a synchronized operation, skip
6917 -- the first parameter of the overridden operation, which is
6918 -- implicit in the new one. If the operation is declared in the
6919 -- body it is not primitive and all formals must match.
6920
6921 if Is_Concurrent_Type (Scope (Subp))
6922 and then Is_Tagged_Type (Scope (Subp))
6923 and then not Has_Completion (Scope (Subp))
6924 then
6925 Form2 := Next_Formal (Form2);
6926 end if;
6927
6928 if Present (Form1) then
6929 Form1 := Next_Formal (Form1);
6930 Form2 := Next_Formal (Form2);
6931 end if;
6932
6933 while Present (Form1) loop
6934 if not Is_Controlling_Formal (Form1)
6935 and then Present (Next_Formal (Form2))
6936 and then Chars (Form1) = Chars (Next_Formal (Form2))
6937 then
6938 Error_Msg_Node_2 := Alias (Overridden_Subp);
6939 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6940 Error_Msg_NE
6941 ("& does not match corresponding formal of&#",
6942 Form1, Form1);
6943 exit;
6944 end if;
6945
6946 Next_Formal (Form1);
6947 Next_Formal (Form2);
6948 end loop;
6949 end;
6950 end if;
6951
6952 -- If there is an overridden subprogram, then check that there is no
6953 -- "not overriding" indicator, and mark the subprogram as overriding.
6954
6955 -- This is not done if the overridden subprogram is marked as hidden,
6956 -- which can occur for the case of inherited controlled operations
6957 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6958 -- subprogram is not itself hidden or we are within a generic instance,
6959 -- in which case the hidden flag may have been modified for the
6960 -- expansion of the instance.
6961
6962 -- (Note: This condition could probably be simplified, leaving out the
6963 -- testing for the specific controlled cases, but it seems safer and
6964 -- clearer this way, and echoes similar special-case tests of this
6965 -- kind in other places.)
6966
6967 if Present (Overridden_Subp)
6968 and then (not Is_Hidden (Overridden_Subp)
6969 or else
6970 (Chars (Overridden_Subp) in Name_Initialize
6971 | Name_Adjust
6972 | Name_Finalize
6973 and then Present (Alias (Overridden_Subp))
6974 and then (not Is_Hidden (Alias (Overridden_Subp))
6975 or else In_Instance)))
6976 then
6977 if Must_Not_Override (Spec) then
6978 Error_Msg_Sloc := Sloc (Overridden_Subp);
6979
6980 if Ekind (Subp) = E_Entry then
6981 Error_Msg_NE
6982 ("entry & overrides inherited operation #", Spec, Subp);
6983 else
6984 Error_Msg_NE
6985 ("subprogram & overrides inherited operation #", Spec, Subp);
6986 end if;
6987
6988 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
6989 -- as an extension of Root_Controlled, and thus has a useless Adjust
6990 -- operation. This operation should not be inherited by other limited
6991 -- controlled types. An explicit Adjust for them is not overriding.
6992
6993 elsif Must_Override (Spec)
6994 and then Chars (Overridden_Subp) = Name_Adjust
6995 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6996 and then Present (Alias (Overridden_Subp))
6997 and then In_Predefined_Unit (Alias (Overridden_Subp))
6998 then
6999 Get_Name_String
7000 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))));
7001 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7002
7003 elsif Is_Subprogram (Subp) then
7004 if Is_Init_Proc (Subp) then
7005 null;
7006
7007 elsif No (Overridden_Operation (Subp)) then
7008
7009 -- For entities generated by Derive_Subprograms the overridden
7010 -- operation is the inherited primitive (which is available
7011 -- through the attribute alias)
7012
7013 if (Is_Dispatching_Operation (Subp)
7014 or else Is_Dispatching_Operation (Overridden_Subp))
7015 and then not Comes_From_Source (Overridden_Subp)
7016 and then Find_Dispatching_Type (Overridden_Subp) =
7017 Find_Dispatching_Type (Subp)
7018 and then Present (Alias (Overridden_Subp))
7019 and then Comes_From_Source (Alias (Overridden_Subp))
7020 then
7021 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
7022 Inherit_Subprogram_Contract (Subp, Alias (Overridden_Subp));
7023
7024 else
7025 Set_Overridden_Operation (Subp, Overridden_Subp);
7026 Inherit_Subprogram_Contract (Subp, Overridden_Subp);
7027 end if;
7028 end if;
7029 end if;
7030
7031 -- If primitive flag is set or this is a protected operation, then
7032 -- the operation is overriding at the point of its declaration, so
7033 -- warn if necessary. Otherwise it may have been declared before the
7034 -- operation it overrides and no check is required.
7035
7036 if Style_Check
7037 and then not Must_Override (Spec)
7038 and then (Is_Primitive
7039 or else Ekind (Scope (Subp)) = E_Protected_Type)
7040 then
7041 Style.Missing_Overriding (Decl, Subp);
7042 end if;
7043
7044 -- If Subp is an operator, it may override a predefined operation, if
7045 -- it is defined in the same scope as the type to which it applies.
7046 -- In that case Overridden_Subp is empty because of our implicit
7047 -- representation for predefined operators. We have to check whether the
7048 -- signature of Subp matches that of a predefined operator. Note that
7049 -- first argument provides the name of the operator, and the second
7050 -- argument the signature that may match that of a standard operation.
7051 -- If the indicator is overriding, then the operator must match a
7052 -- predefined signature, because we know already that there is no
7053 -- explicit overridden operation.
7054
7055 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
7056 if Must_Not_Override (Spec) then
7057
7058 -- If this is not a primitive or a protected subprogram, then
7059 -- "not overriding" is illegal.
7060
7061 if not Is_Primitive
7062 and then Ekind (Scope (Subp)) /= E_Protected_Type
7063 then
7064 Error_Msg_N ("overriding indicator only allowed "
7065 & "if subprogram is primitive", Subp);
7066
7067 elsif Can_Override_Operator (Subp) then
7068 Error_Msg_NE
7069 ("subprogram& overrides predefined operator ", Spec, Subp);
7070 end if;
7071
7072 elsif Must_Override (Spec) then
7073 if No (Overridden_Operation (Subp))
7074 and then not Can_Override_Operator (Subp)
7075 then
7076 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7077 end if;
7078
7079 elsif not Error_Posted (Subp)
7080 and then Style_Check
7081 and then Can_Override_Operator (Subp)
7082 and then not In_Predefined_Unit (Subp)
7083 then
7084 -- If style checks are enabled, indicate that the indicator is
7085 -- missing. However, at the point of declaration, the type of
7086 -- which this is a primitive operation may be private, in which
7087 -- case the indicator would be premature.
7088
7089 if Has_Private_Declaration (Etype (Subp))
7090 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
7091 then
7092 null;
7093 else
7094 Style.Missing_Overriding (Decl, Subp);
7095 end if;
7096 end if;
7097
7098 elsif Must_Override (Spec) then
7099 if Ekind (Subp) = E_Entry then
7100 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
7101 else
7102 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
7103 end if;
7104
7105 -- If the operation is marked "not overriding" and it's not primitive
7106 -- then an error is issued, unless this is an operation of a task or
7107 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
7108 -- has been specified have already been checked above.
7109
7110 elsif Must_Not_Override (Spec)
7111 and then not Is_Primitive
7112 and then Ekind (Subp) /= E_Entry
7113 and then Ekind (Scope (Subp)) /= E_Protected_Type
7114 then
7115 Error_Msg_N
7116 ("overriding indicator only allowed if subprogram is primitive",
7117 Subp);
7118 return;
7119 end if;
7120 end Check_Overriding_Indicator;
7121
7122 -------------------
7123 -- Check_Returns --
7124 -------------------
7125
7126 -- Note: this procedure needs to know far too much about how the expander
7127 -- messes with exceptions. The use of the flag Exception_Junk and the
7128 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
7129 -- works, but is not very clean. It would be better if the expansion
7130 -- routines would leave Original_Node working nicely, and we could use
7131 -- Original_Node here to ignore all the peculiar expander messing ???
7132
7133 procedure Check_Returns
7134 (HSS : Node_Id;
7135 Mode : Character;
7136 Err : out Boolean;
7137 Proc : Entity_Id := Empty)
7138 is
7139 Handler : Node_Id;
7140
7141 procedure Check_Statement_Sequence (L : List_Id);
7142 -- Internal recursive procedure to check a list of statements for proper
7143 -- termination by a return statement (or a transfer of control or a
7144 -- compound statement that is itself internally properly terminated).
7145
7146 ------------------------------
7147 -- Check_Statement_Sequence --
7148 ------------------------------
7149
7150 procedure Check_Statement_Sequence (L : List_Id) is
7151 Last_Stm : Node_Id;
7152 Stm : Node_Id;
7153 Kind : Node_Kind;
7154
7155 function Assert_False return Boolean;
7156 -- Returns True if Last_Stm is a pragma Assert (False) that has been
7157 -- rewritten as a null statement when assertions are off. The assert
7158 -- is not active, but it is still enough to kill the warning.
7159
7160 ------------------
7161 -- Assert_False --
7162 ------------------
7163
7164 function Assert_False return Boolean is
7165 Orig : constant Node_Id := Original_Node (Last_Stm);
7166
7167 begin
7168 if Nkind (Orig) = N_Pragma
7169 and then Pragma_Name (Orig) = Name_Assert
7170 and then not Error_Posted (Orig)
7171 then
7172 declare
7173 Arg : constant Node_Id :=
7174 First (Pragma_Argument_Associations (Orig));
7175 Exp : constant Node_Id := Expression (Arg);
7176 begin
7177 return Nkind (Exp) = N_Identifier
7178 and then Chars (Exp) = Name_False;
7179 end;
7180
7181 else
7182 return False;
7183 end if;
7184 end Assert_False;
7185
7186 -- Local variables
7187
7188 Raise_Exception_Call : Boolean;
7189 -- Set True if statement sequence terminated by Raise_Exception call
7190 -- or a Reraise_Occurrence call.
7191
7192 -- Start of processing for Check_Statement_Sequence
7193
7194 begin
7195 Raise_Exception_Call := False;
7196
7197 -- Get last real statement
7198
7199 Last_Stm := Last (L);
7200
7201 -- Deal with digging out exception handler statement sequences that
7202 -- have been transformed by the local raise to goto optimization.
7203 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
7204 -- optimization has occurred, we are looking at something like:
7205
7206 -- begin
7207 -- original stmts in block
7208
7209 -- exception \
7210 -- when excep1 => |
7211 -- goto L1; | omitted if No_Exception_Propagation
7212 -- when excep2 => |
7213 -- goto L2; /
7214 -- end;
7215
7216 -- goto L3; -- skip handler when exception not raised
7217
7218 -- <<L1>> -- target label for local exception
7219 -- begin
7220 -- estmts1
7221 -- end;
7222
7223 -- goto L3;
7224
7225 -- <<L2>>
7226 -- begin
7227 -- estmts2
7228 -- end;
7229
7230 -- <<L3>>
7231
7232 -- and what we have to do is to dig out the estmts1 and estmts2
7233 -- sequences (which were the original sequences of statements in
7234 -- the exception handlers) and check them.
7235
7236 if Nkind (Last_Stm) = N_Label and then Exception_Junk (Last_Stm) then
7237 Stm := Last_Stm;
7238 loop
7239 Prev (Stm);
7240 exit when No (Stm);
7241 exit when Nkind (Stm) /= N_Block_Statement;
7242 exit when not Exception_Junk (Stm);
7243 Prev (Stm);
7244 exit when No (Stm);
7245 exit when Nkind (Stm) /= N_Label;
7246 exit when not Exception_Junk (Stm);
7247 Check_Statement_Sequence
7248 (Statements (Handled_Statement_Sequence (Next (Stm))));
7249
7250 Prev (Stm);
7251 Last_Stm := Stm;
7252 exit when No (Stm);
7253 exit when Nkind (Stm) /= N_Goto_Statement;
7254 exit when not Exception_Junk (Stm);
7255 end loop;
7256 end if;
7257
7258 -- Don't count pragmas
7259
7260 while Nkind (Last_Stm) = N_Pragma
7261
7262 -- Don't count call to SS_Release (can happen after Raise_Exception)
7263
7264 or else
7265 (Nkind (Last_Stm) = N_Procedure_Call_Statement
7266 and then
7267 Nkind (Name (Last_Stm)) = N_Identifier
7268 and then
7269 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
7270
7271 -- Don't count exception junk
7272
7273 or else
7274 (Nkind (Last_Stm) in
7275 N_Goto_Statement | N_Label | N_Object_Declaration
7276 and then Exception_Junk (Last_Stm))
7277 or else Nkind (Last_Stm) in N_Push_xxx_Label | N_Pop_xxx_Label
7278
7279 -- Inserted code, such as finalization calls, is irrelevant: we only
7280 -- need to check original source.
7281
7282 or else Is_Rewrite_Insertion (Last_Stm)
7283 loop
7284 Prev (Last_Stm);
7285 end loop;
7286
7287 -- Here we have the "real" last statement
7288
7289 Kind := Nkind (Last_Stm);
7290
7291 -- Transfer of control, OK. Note that in the No_Return procedure
7292 -- case, we already diagnosed any explicit return statements, so
7293 -- we can treat them as OK in this context.
7294
7295 if Is_Transfer (Last_Stm) then
7296 return;
7297
7298 -- Check cases of explicit non-indirect procedure calls
7299
7300 elsif Kind = N_Procedure_Call_Statement
7301 and then Is_Entity_Name (Name (Last_Stm))
7302 then
7303 -- Check call to Raise_Exception procedure which is treated
7304 -- specially, as is a call to Reraise_Occurrence.
7305
7306 -- We suppress the warning in these cases since it is likely that
7307 -- the programmer really does not expect to deal with the case
7308 -- of Null_Occurrence, and thus would find a warning about a
7309 -- missing return curious, and raising Program_Error does not
7310 -- seem such a bad behavior if this does occur.
7311
7312 -- Note that in the Ada 2005 case for Raise_Exception, the actual
7313 -- behavior will be to raise Constraint_Error (see AI-329).
7314
7315 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
7316 or else
7317 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
7318 then
7319 Raise_Exception_Call := True;
7320
7321 -- For Raise_Exception call, test first argument, if it is
7322 -- an attribute reference for a 'Identity call, then we know
7323 -- that the call cannot possibly return.
7324
7325 declare
7326 Arg : constant Node_Id :=
7327 Original_Node (First_Actual (Last_Stm));
7328 begin
7329 if Nkind (Arg) = N_Attribute_Reference
7330 and then Attribute_Name (Arg) = Name_Identity
7331 then
7332 return;
7333 end if;
7334 end;
7335 end if;
7336
7337 -- If statement, need to look inside if there is an else and check
7338 -- each constituent statement sequence for proper termination.
7339
7340 elsif Kind = N_If_Statement
7341 and then Present (Else_Statements (Last_Stm))
7342 then
7343 Check_Statement_Sequence (Then_Statements (Last_Stm));
7344 Check_Statement_Sequence (Else_Statements (Last_Stm));
7345
7346 if Present (Elsif_Parts (Last_Stm)) then
7347 declare
7348 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
7349
7350 begin
7351 while Present (Elsif_Part) loop
7352 Check_Statement_Sequence (Then_Statements (Elsif_Part));
7353 Next (Elsif_Part);
7354 end loop;
7355 end;
7356 end if;
7357
7358 return;
7359
7360 -- Case statement, check each case for proper termination
7361
7362 elsif Kind = N_Case_Statement then
7363 declare
7364 Case_Alt : Node_Id;
7365 begin
7366 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
7367 while Present (Case_Alt) loop
7368 Check_Statement_Sequence (Statements (Case_Alt));
7369 Next_Non_Pragma (Case_Alt);
7370 end loop;
7371 end;
7372
7373 return;
7374
7375 -- Block statement, check its handled sequence of statements
7376
7377 elsif Kind = N_Block_Statement then
7378 declare
7379 Err1 : Boolean;
7380
7381 begin
7382 Check_Returns
7383 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
7384
7385 if Err1 then
7386 Err := True;
7387 end if;
7388
7389 return;
7390 end;
7391
7392 -- Loop statement. If there is an iteration scheme, we can definitely
7393 -- fall out of the loop. Similarly if there is an exit statement, we
7394 -- can fall out. In either case we need a following return.
7395
7396 elsif Kind = N_Loop_Statement then
7397 if Present (Iteration_Scheme (Last_Stm))
7398 or else Has_Exit (Entity (Identifier (Last_Stm)))
7399 then
7400 null;
7401
7402 -- A loop with no exit statement or iteration scheme is either
7403 -- an infinite loop, or it has some other exit (raise/return).
7404 -- In either case, no warning is required.
7405
7406 else
7407 return;
7408 end if;
7409
7410 -- Timed entry call, check entry call and delay alternatives
7411
7412 -- Note: in expanded code, the timed entry call has been converted
7413 -- to a set of expanded statements on which the check will work
7414 -- correctly in any case.
7415
7416 elsif Kind = N_Timed_Entry_Call then
7417 declare
7418 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7419 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
7420
7421 begin
7422 -- If statement sequence of entry call alternative is missing,
7423 -- then we can definitely fall through, and we post the error
7424 -- message on the entry call alternative itself.
7425
7426 if No (Statements (ECA)) then
7427 Last_Stm := ECA;
7428
7429 -- If statement sequence of delay alternative is missing, then
7430 -- we can definitely fall through, and we post the error
7431 -- message on the delay alternative itself.
7432
7433 -- Note: if both ECA and DCA are missing the return, then we
7434 -- post only one message, should be enough to fix the bugs.
7435 -- If not we will get a message next time on the DCA when the
7436 -- ECA is fixed.
7437
7438 elsif No (Statements (DCA)) then
7439 Last_Stm := DCA;
7440
7441 -- Else check both statement sequences
7442
7443 else
7444 Check_Statement_Sequence (Statements (ECA));
7445 Check_Statement_Sequence (Statements (DCA));
7446 return;
7447 end if;
7448 end;
7449
7450 -- Conditional entry call, check entry call and else part
7451
7452 -- Note: in expanded code, the conditional entry call has been
7453 -- converted to a set of expanded statements on which the check
7454 -- will work correctly in any case.
7455
7456 elsif Kind = N_Conditional_Entry_Call then
7457 declare
7458 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
7459
7460 begin
7461 -- If statement sequence of entry call alternative is missing,
7462 -- then we can definitely fall through, and we post the error
7463 -- message on the entry call alternative itself.
7464
7465 if No (Statements (ECA)) then
7466 Last_Stm := ECA;
7467
7468 -- Else check statement sequence and else part
7469
7470 else
7471 Check_Statement_Sequence (Statements (ECA));
7472 Check_Statement_Sequence (Else_Statements (Last_Stm));
7473 return;
7474 end if;
7475 end;
7476 end if;
7477
7478 -- If we fall through, issue appropriate message
7479
7480 if Mode = 'F' then
7481
7482 -- Kill warning if last statement is a raise exception call,
7483 -- or a pragma Assert (False). Note that with assertions enabled,
7484 -- such a pragma has been converted into a raise exception call
7485 -- already, so the Assert_False is for the assertions off case.
7486
7487 if not Raise_Exception_Call and then not Assert_False then
7488
7489 -- In GNATprove mode, it is an error to have a missing return
7490
7491 Error_Msg_Warn := SPARK_Mode /= On;
7492
7493 -- Issue error message or warning
7494
7495 Error_Msg_N
7496 ("RETURN statement missing following this statement<<!",
7497 Last_Stm);
7498 Error_Msg_N
7499 ("\Program_Error ]<<!", Last_Stm);
7500 end if;
7501
7502 -- Note: we set Err even though we have not issued a warning
7503 -- because we still have a case of a missing return. This is
7504 -- an extremely marginal case, probably will never be noticed
7505 -- but we might as well get it right.
7506
7507 Err := True;
7508
7509 -- Otherwise we have the case of a procedure marked No_Return
7510
7511 else
7512 if not Raise_Exception_Call then
7513 if GNATprove_Mode then
7514 Error_Msg_N
7515 ("implied return after this statement would have raised "
7516 & "Program_Error", Last_Stm);
7517
7518 -- In normal compilation mode, do not warn on a generated call
7519 -- (e.g. in the body of a renaming as completion).
7520
7521 elsif Comes_From_Source (Last_Stm) then
7522 Error_Msg_N
7523 ("implied return after this statement will raise "
7524 & "Program_Error??", Last_Stm);
7525 end if;
7526
7527 Error_Msg_Warn := SPARK_Mode /= On;
7528 Error_Msg_NE
7529 ("\procedure & is marked as No_Return<<!", Last_Stm, Proc);
7530 end if;
7531
7532 declare
7533 RE : constant Node_Id :=
7534 Make_Raise_Program_Error (Sloc (Last_Stm),
7535 Reason => PE_Implicit_Return);
7536 begin
7537 Insert_After (Last_Stm, RE);
7538 Analyze (RE);
7539 end;
7540 end if;
7541 end Check_Statement_Sequence;
7542
7543 -- Start of processing for Check_Returns
7544
7545 begin
7546 Err := False;
7547 Check_Statement_Sequence (Statements (HSS));
7548
7549 if Present (Exception_Handlers (HSS)) then
7550 Handler := First_Non_Pragma (Exception_Handlers (HSS));
7551 while Present (Handler) loop
7552 Check_Statement_Sequence (Statements (Handler));
7553 Next_Non_Pragma (Handler);
7554 end loop;
7555 end if;
7556 end Check_Returns;
7557
7558 ----------------------------
7559 -- Check_Subprogram_Order --
7560 ----------------------------
7561
7562 procedure Check_Subprogram_Order (N : Node_Id) is
7563
7564 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
7565 -- This is used to check if S1 > S2 in the sense required by this test,
7566 -- for example nameab < namec, but name2 < name10.
7567
7568 -----------------------------
7569 -- Subprogram_Name_Greater --
7570 -----------------------------
7571
7572 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7573 L1, L2 : Positive;
7574 N1, N2 : Natural;
7575
7576 begin
7577 -- Deal with special case where names are identical except for a
7578 -- numerical suffix. These are handled specially, taking the numeric
7579 -- ordering from the suffix into account.
7580
7581 L1 := S1'Last;
7582 while S1 (L1) in '0' .. '9' loop
7583 L1 := L1 - 1;
7584 end loop;
7585
7586 L2 := S2'Last;
7587 while S2 (L2) in '0' .. '9' loop
7588 L2 := L2 - 1;
7589 end loop;
7590
7591 -- If non-numeric parts non-equal, do straight compare
7592
7593 if S1 (S1'First .. L1) /= S2 (S2'First .. L2) then
7594 return S1 > S2;
7595
7596 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7597 -- that a missing suffix is treated as numeric zero in this test.
7598
7599 else
7600 N1 := 0;
7601 while L1 < S1'Last loop
7602 L1 := L1 + 1;
7603 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7604 end loop;
7605
7606 N2 := 0;
7607 while L2 < S2'Last loop
7608 L2 := L2 + 1;
7609 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7610 end loop;
7611
7612 return N1 > N2;
7613 end if;
7614 end Subprogram_Name_Greater;
7615
7616 -- Start of processing for Check_Subprogram_Order
7617
7618 begin
7619 -- Check body in alpha order if this is option
7620
7621 if Style_Check
7622 and then Style_Check_Order_Subprograms
7623 and then Nkind (N) = N_Subprogram_Body
7624 and then Comes_From_Source (N)
7625 and then In_Extended_Main_Source_Unit (N)
7626 then
7627 declare
7628 LSN : String_Ptr
7629 renames Scope_Stack.Table
7630 (Scope_Stack.Last).Last_Subprogram_Name;
7631
7632 Body_Id : constant Entity_Id :=
7633 Defining_Entity (Specification (N));
7634
7635 begin
7636 Get_Decoded_Name_String (Chars (Body_Id));
7637
7638 if LSN /= null then
7639 if Subprogram_Name_Greater
7640 (LSN.all, Name_Buffer (1 .. Name_Len))
7641 then
7642 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7643 end if;
7644
7645 Free (LSN);
7646 end if;
7647
7648 LSN := new String'(Name_Buffer (1 .. Name_Len));
7649 end;
7650 end if;
7651 end Check_Subprogram_Order;
7652
7653 ------------------------------
7654 -- Check_Subtype_Conformant --
7655 ------------------------------
7656
7657 procedure Check_Subtype_Conformant
7658 (New_Id : Entity_Id;
7659 Old_Id : Entity_Id;
7660 Err_Loc : Node_Id := Empty;
7661 Skip_Controlling_Formals : Boolean := False;
7662 Get_Inst : Boolean := False)
7663 is
7664 Result : Boolean;
7665 pragma Warnings (Off, Result);
7666 begin
7667 Check_Conformance
7668 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
7669 Skip_Controlling_Formals => Skip_Controlling_Formals,
7670 Get_Inst => Get_Inst);
7671 end Check_Subtype_Conformant;
7672
7673 -----------------------------------
7674 -- Check_Synchronized_Overriding --
7675 -----------------------------------
7676
7677 procedure Check_Synchronized_Overriding
7678 (Def_Id : Entity_Id;
7679 Overridden_Subp : out Entity_Id)
7680 is
7681 Ifaces_List : Elist_Id;
7682 In_Scope : Boolean;
7683 Typ : Entity_Id;
7684
7685 function Is_Valid_Formal (F : Entity_Id) return Boolean;
7686 -- Predicate for legality rule in 9.4 (11.9/2): If an inherited
7687 -- subprogram is implemented by a protected procedure or entry,
7688 -- its first parameter must be out, in out, or access-to-variable.
7689
7690 function Matches_Prefixed_View_Profile
7691 (Prim_Params : List_Id;
7692 Iface_Params : List_Id) return Boolean;
7693 -- Determine whether a subprogram's parameter profile Prim_Params
7694 -- matches that of a potentially overridden interface subprogram
7695 -- Iface_Params. Also determine if the type of first parameter of
7696 -- Iface_Params is an implemented interface.
7697
7698 ----------------------
7699 -- Is_Valid_Formal --
7700 ----------------------
7701
7702 function Is_Valid_Formal (F : Entity_Id) return Boolean is
7703 begin
7704 return
7705 Ekind (F) in E_In_Out_Parameter | E_Out_Parameter
7706 or else
7707 (Nkind (Parameter_Type (Parent (F))) = N_Access_Definition
7708 and then not Constant_Present (Parameter_Type (Parent (F))));
7709 end Is_Valid_Formal;
7710
7711 -----------------------------------
7712 -- Matches_Prefixed_View_Profile --
7713 -----------------------------------
7714
7715 function Matches_Prefixed_View_Profile
7716 (Prim_Params : List_Id;
7717 Iface_Params : List_Id) return Boolean
7718 is
7719 function Is_Implemented
7720 (Ifaces_List : Elist_Id;
7721 Iface : Entity_Id) return Boolean;
7722 -- Determine if Iface is implemented by the current task or
7723 -- protected type.
7724
7725 --------------------
7726 -- Is_Implemented --
7727 --------------------
7728
7729 function Is_Implemented
7730 (Ifaces_List : Elist_Id;
7731 Iface : Entity_Id) return Boolean
7732 is
7733 Iface_Elmt : Elmt_Id;
7734
7735 begin
7736 Iface_Elmt := First_Elmt (Ifaces_List);
7737 while Present (Iface_Elmt) loop
7738 if Node (Iface_Elmt) = Iface then
7739 return True;
7740 end if;
7741
7742 Next_Elmt (Iface_Elmt);
7743 end loop;
7744
7745 return False;
7746 end Is_Implemented;
7747
7748 -- Local variables
7749
7750 Iface_Id : Entity_Id;
7751 Iface_Param : Node_Id;
7752 Iface_Typ : Entity_Id;
7753 Prim_Id : Entity_Id;
7754 Prim_Param : Node_Id;
7755 Prim_Typ : Entity_Id;
7756
7757 -- Start of processing for Matches_Prefixed_View_Profile
7758
7759 begin
7760 Iface_Param := First (Iface_Params);
7761 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
7762
7763 if Is_Access_Type (Iface_Typ) then
7764 Iface_Typ := Designated_Type (Iface_Typ);
7765 end if;
7766
7767 Prim_Param := First (Prim_Params);
7768
7769 -- The first parameter of the potentially overridden subprogram must
7770 -- be an interface implemented by Prim.
7771
7772 if not Is_Interface (Iface_Typ)
7773 or else not Is_Implemented (Ifaces_List, Iface_Typ)
7774 then
7775 return False;
7776 end if;
7777
7778 -- The checks on the object parameters are done, so move on to the
7779 -- rest of the parameters.
7780
7781 if not In_Scope then
7782 Next (Prim_Param);
7783 end if;
7784
7785 Next (Iface_Param);
7786 while Present (Iface_Param) and then Present (Prim_Param) loop
7787 Iface_Id := Defining_Identifier (Iface_Param);
7788 Iface_Typ := Find_Parameter_Type (Iface_Param);
7789
7790 Prim_Id := Defining_Identifier (Prim_Param);
7791 Prim_Typ := Find_Parameter_Type (Prim_Param);
7792
7793 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
7794 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
7795 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
7796 then
7797 Iface_Typ := Designated_Type (Iface_Typ);
7798 Prim_Typ := Designated_Type (Prim_Typ);
7799 end if;
7800
7801 -- Case of multiple interface types inside a parameter profile
7802
7803 -- (Obj_Param : in out Iface; ...; Param : Iface)
7804
7805 -- If the interface type is implemented, then the matching type in
7806 -- the primitive should be the implementing record type.
7807
7808 if Ekind (Iface_Typ) = E_Record_Type
7809 and then Is_Interface (Iface_Typ)
7810 and then Is_Implemented (Ifaces_List, Iface_Typ)
7811 then
7812 if Prim_Typ /= Typ then
7813 return False;
7814 end if;
7815
7816 -- The two parameters must be both mode and subtype conformant
7817
7818 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
7819 or else not
7820 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
7821 then
7822 return False;
7823 end if;
7824
7825 Next (Iface_Param);
7826 Next (Prim_Param);
7827 end loop;
7828
7829 -- One of the two lists contains more parameters than the other
7830
7831 if Present (Iface_Param) or else Present (Prim_Param) then
7832 return False;
7833 end if;
7834
7835 return True;
7836 end Matches_Prefixed_View_Profile;
7837
7838 -- Start of processing for Check_Synchronized_Overriding
7839
7840 begin
7841 Overridden_Subp := Empty;
7842
7843 -- Def_Id must be an entry or a subprogram. We should skip predefined
7844 -- primitives internally generated by the front end; however at this
7845 -- stage predefined primitives are still not fully decorated. As a
7846 -- minor optimization we skip here internally generated subprograms.
7847
7848 if (Ekind (Def_Id) /= E_Entry
7849 and then Ekind (Def_Id) /= E_Function
7850 and then Ekind (Def_Id) /= E_Procedure)
7851 or else not Comes_From_Source (Def_Id)
7852 then
7853 return;
7854 end if;
7855
7856 -- Search for the concurrent declaration since it contains the list of
7857 -- all implemented interfaces. In this case, the subprogram is declared
7858 -- within the scope of a protected or a task type.
7859
7860 if Present (Scope (Def_Id))
7861 and then Is_Concurrent_Type (Scope (Def_Id))
7862 and then not Is_Generic_Actual_Type (Scope (Def_Id))
7863 then
7864 Typ := Scope (Def_Id);
7865 In_Scope := True;
7866
7867 -- The enclosing scope is not a synchronized type and the subprogram
7868 -- has no formals.
7869
7870 elsif No (First_Formal (Def_Id)) then
7871 return;
7872
7873 -- The subprogram has formals and hence it may be a primitive of a
7874 -- concurrent type.
7875
7876 else
7877 Typ := Etype (First_Formal (Def_Id));
7878
7879 if Is_Access_Type (Typ) then
7880 Typ := Directly_Designated_Type (Typ);
7881 end if;
7882
7883 if Is_Concurrent_Type (Typ)
7884 and then not Is_Generic_Actual_Type (Typ)
7885 then
7886 In_Scope := False;
7887
7888 -- This case occurs when the concurrent type is declared within a
7889 -- generic unit. As a result the corresponding record has been built
7890 -- and used as the type of the first formal, we just have to retrieve
7891 -- the corresponding concurrent type.
7892
7893 elsif Is_Concurrent_Record_Type (Typ)
7894 and then not Is_Class_Wide_Type (Typ)
7895 and then Present (Corresponding_Concurrent_Type (Typ))
7896 then
7897 Typ := Corresponding_Concurrent_Type (Typ);
7898 In_Scope := False;
7899
7900 else
7901 return;
7902 end if;
7903 end if;
7904
7905 -- There is no overriding to check if this is an inherited operation in
7906 -- a type derivation for a generic actual.
7907
7908 Collect_Interfaces (Typ, Ifaces_List);
7909
7910 if Is_Empty_Elmt_List (Ifaces_List) then
7911 return;
7912 end if;
7913
7914 -- Determine whether entry or subprogram Def_Id overrides a primitive
7915 -- operation that belongs to one of the interfaces in Ifaces_List.
7916
7917 declare
7918 Candidate : Entity_Id := Empty;
7919 Hom : Entity_Id := Empty;
7920 Subp : Entity_Id := Empty;
7921
7922 begin
7923 -- Traverse the homonym chain, looking for a potentially overridden
7924 -- subprogram that belongs to an implemented interface.
7925
7926 Hom := Current_Entity_In_Scope (Def_Id);
7927 while Present (Hom) loop
7928 Subp := Hom;
7929
7930 if Subp = Def_Id
7931 or else not Is_Overloadable (Subp)
7932 or else not Is_Primitive (Subp)
7933 or else not Is_Dispatching_Operation (Subp)
7934 or else not Present (Find_Dispatching_Type (Subp))
7935 or else not Is_Interface (Find_Dispatching_Type (Subp))
7936 then
7937 null;
7938
7939 -- Entries and procedures can override abstract or null interface
7940 -- procedures.
7941
7942 elsif Ekind (Def_Id) in E_Entry | E_Procedure
7943 and then Ekind (Subp) = E_Procedure
7944 and then Matches_Prefixed_View_Profile
7945 (Parameter_Specifications (Parent (Def_Id)),
7946 Parameter_Specifications (Parent (Subp)))
7947 then
7948 Candidate := Subp;
7949
7950 -- For an overridden subprogram Subp, check whether the mode
7951 -- of its first parameter is correct depending on the kind of
7952 -- synchronized type.
7953
7954 declare
7955 Formal : constant Node_Id := First_Formal (Candidate);
7956
7957 begin
7958 -- In order for an entry or a protected procedure to
7959 -- override, the first parameter of the overridden routine
7960 -- must be of mode "out", "in out", or access-to-variable.
7961
7962 if Ekind (Candidate) in E_Entry | E_Procedure
7963 and then Is_Protected_Type (Typ)
7964 and then not Is_Valid_Formal (Formal)
7965 then
7966 null;
7967
7968 -- All other cases are OK since a task entry or routine does
7969 -- not have a restriction on the mode of the first parameter
7970 -- of the overridden interface routine.
7971
7972 else
7973 Overridden_Subp := Candidate;
7974 return;
7975 end if;
7976 end;
7977
7978 -- Functions can override abstract interface functions. Return
7979 -- types must be subtype conformant.
7980
7981 elsif Ekind (Def_Id) = E_Function
7982 and then Ekind (Subp) = E_Function
7983 and then Matches_Prefixed_View_Profile
7984 (Parameter_Specifications (Parent (Def_Id)),
7985 Parameter_Specifications (Parent (Subp)))
7986 and then Conforming_Types
7987 (Etype (Def_Id), Etype (Subp), Subtype_Conformant)
7988 then
7989 Candidate := Subp;
7990
7991 -- If an inherited subprogram is implemented by a protected
7992 -- function, then the first parameter of the inherited
7993 -- subprogram shall be of mode in, but not an access-to-
7994 -- variable parameter (RM 9.4(11/9)).
7995
7996 if Present (First_Formal (Subp))
7997 and then Ekind (First_Formal (Subp)) = E_In_Parameter
7998 and then
7999 (not Is_Access_Type (Etype (First_Formal (Subp)))
8000 or else
8001 Is_Access_Constant (Etype (First_Formal (Subp))))
8002 then
8003 Overridden_Subp := Subp;
8004 return;
8005 end if;
8006 end if;
8007
8008 Hom := Homonym (Hom);
8009 end loop;
8010
8011 -- After examining all candidates for overriding, we are left with
8012 -- the best match, which is a mode-incompatible interface routine.
8013
8014 if In_Scope and then Present (Candidate) then
8015 Error_Msg_PT (Def_Id, Candidate);
8016 end if;
8017
8018 Overridden_Subp := Candidate;
8019 return;
8020 end;
8021 end Check_Synchronized_Overriding;
8022
8023 ---------------------------
8024 -- Check_Type_Conformant --
8025 ---------------------------
8026
8027 procedure Check_Type_Conformant
8028 (New_Id : Entity_Id;
8029 Old_Id : Entity_Id;
8030 Err_Loc : Node_Id := Empty)
8031 is
8032 Result : Boolean;
8033 pragma Warnings (Off, Result);
8034 begin
8035 Check_Conformance
8036 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
8037 end Check_Type_Conformant;
8038
8039 ---------------------------
8040 -- Can_Override_Operator --
8041 ---------------------------
8042
8043 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
8044 Typ : Entity_Id;
8045
8046 begin
8047 if Nkind (Subp) /= N_Defining_Operator_Symbol then
8048 return False;
8049
8050 else
8051 Typ := Base_Type (Etype (First_Formal (Subp)));
8052
8053 -- Check explicitly that the operation is a primitive of the type
8054
8055 return Operator_Matches_Spec (Subp, Subp)
8056 and then not Is_Generic_Type (Typ)
8057 and then Scope (Subp) = Scope (Typ)
8058 and then not Is_Class_Wide_Type (Typ);
8059 end if;
8060 end Can_Override_Operator;
8061
8062 ----------------------
8063 -- Conforming_Types --
8064 ----------------------
8065
8066 function Conforming_Types
8067 (T1 : Entity_Id;
8068 T2 : Entity_Id;
8069 Ctype : Conformance_Type;
8070 Get_Inst : Boolean := False) return Boolean
8071 is
8072 function Base_Types_Match
8073 (Typ_1 : Entity_Id;
8074 Typ_2 : Entity_Id) return Boolean;
8075 -- If neither Typ_1 nor Typ_2 are generic actual types, or if they are
8076 -- in different scopes (e.g. parent and child instances), then verify
8077 -- that the base types are equal. Otherwise Typ_1 and Typ_2 must be on
8078 -- the same subtype chain. The whole purpose of this procedure is to
8079 -- prevent spurious ambiguities in an instantiation that may arise if
8080 -- two distinct generic types are instantiated with the same actual.
8081
8082 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id;
8083 -- An access parameter can designate an incomplete type. If the
8084 -- incomplete type is the limited view of a type from a limited_
8085 -- with_clause, check whether the non-limited view is available.
8086 -- If it is a (non-limited) incomplete type, get the full view.
8087
8088 function Matches_Limited_With_View
8089 (Typ_1 : Entity_Id;
8090 Typ_2 : Entity_Id) return Boolean;
8091 -- Returns True if and only if either Typ_1 denotes a limited view of
8092 -- Typ_2 or Typ_2 denotes a limited view of Typ_1. This can arise when
8093 -- the limited with view of a type is used in a subprogram declaration
8094 -- and the subprogram body is in the scope of a regular with clause for
8095 -- the same unit. In such a case, the two type entities are considered
8096 -- identical for purposes of conformance checking.
8097
8098 ----------------------
8099 -- Base_Types_Match --
8100 ----------------------
8101
8102 function Base_Types_Match
8103 (Typ_1 : Entity_Id;
8104 Typ_2 : Entity_Id) return Boolean
8105 is
8106 Base_1 : constant Entity_Id := Base_Type (Typ_1);
8107 Base_2 : constant Entity_Id := Base_Type (Typ_2);
8108
8109 begin
8110 if Typ_1 = Typ_2 then
8111 return True;
8112
8113 elsif Base_1 = Base_2 then
8114
8115 -- The following is too permissive. A more precise test should
8116 -- check that the generic actual is an ancestor subtype of the
8117 -- other ???.
8118
8119 -- See code in Find_Corresponding_Spec that applies an additional
8120 -- filter to handle accidental amiguities in instances.
8121
8122 return
8123 not Is_Generic_Actual_Type (Typ_1)
8124 or else not Is_Generic_Actual_Type (Typ_2)
8125 or else Scope (Typ_1) /= Scope (Typ_2);
8126
8127 -- If Typ_2 is a generic actual type it is declared as the subtype of
8128 -- the actual. If that actual is itself a subtype we need to use its
8129 -- own base type to check for compatibility.
8130
8131 elsif Ekind (Base_2) = Ekind (Typ_2)
8132 and then Base_1 = Base_Type (Base_2)
8133 then
8134 return True;
8135
8136 elsif Ekind (Base_1) = Ekind (Typ_1)
8137 and then Base_2 = Base_Type (Base_1)
8138 then
8139 return True;
8140
8141 else
8142 return False;
8143 end if;
8144 end Base_Types_Match;
8145
8146 --------------------------
8147 -- Find_Designated_Type --
8148 --------------------------
8149
8150 function Find_Designated_Type (Typ : Entity_Id) return Entity_Id is
8151 Desig : Entity_Id;
8152
8153 begin
8154 Desig := Directly_Designated_Type (Typ);
8155
8156 if Ekind (Desig) = E_Incomplete_Type then
8157
8158 -- If regular incomplete type, get full view if available
8159
8160 if Present (Full_View (Desig)) then
8161 Desig := Full_View (Desig);
8162
8163 -- If limited view of a type, get non-limited view if available,
8164 -- and check again for a regular incomplete type.
8165
8166 elsif Present (Non_Limited_View (Desig)) then
8167 Desig := Get_Full_View (Non_Limited_View (Desig));
8168 end if;
8169 end if;
8170
8171 return Desig;
8172 end Find_Designated_Type;
8173
8174 -------------------------------
8175 -- Matches_Limited_With_View --
8176 -------------------------------
8177
8178 function Matches_Limited_With_View
8179 (Typ_1 : Entity_Id;
8180 Typ_2 : Entity_Id) return Boolean
8181 is
8182 function Is_Matching_Limited_View
8183 (Typ : Entity_Id;
8184 View : Entity_Id) return Boolean;
8185 -- Determine whether non-limited view View denotes type Typ in some
8186 -- conformant fashion.
8187
8188 ------------------------------
8189 -- Is_Matching_Limited_View --
8190 ------------------------------
8191
8192 function Is_Matching_Limited_View
8193 (Typ : Entity_Id;
8194 View : Entity_Id) return Boolean
8195 is
8196 Root_Typ : Entity_Id;
8197 Root_View : Entity_Id;
8198
8199 begin
8200 -- The non-limited view directly denotes the type
8201
8202 if Typ = View then
8203 return True;
8204
8205 -- The type is a subtype of the non-limited view
8206
8207 elsif Is_Subtype_Of (Typ, View) then
8208 return True;
8209
8210 -- Both the non-limited view and the type denote class-wide types
8211
8212 elsif Is_Class_Wide_Type (Typ)
8213 and then Is_Class_Wide_Type (View)
8214 then
8215 Root_Typ := Root_Type (Typ);
8216 Root_View := Root_Type (View);
8217
8218 if Root_Typ = Root_View then
8219 return True;
8220
8221 -- An incomplete tagged type and its full view may receive two
8222 -- distinct class-wide types when the related package has not
8223 -- been analyzed yet.
8224
8225 -- package Pack is
8226 -- type T is tagged; -- CW_1
8227 -- type T is tagged null record; -- CW_2
8228 -- end Pack;
8229
8230 -- This is because the package lacks any semantic information
8231 -- that may eventually link both views of T. As a consequence,
8232 -- a client of the limited view of Pack will see CW_2 while a
8233 -- client of the non-limited view of Pack will see CW_1.
8234
8235 elsif Is_Incomplete_Type (Root_Typ)
8236 and then Present (Full_View (Root_Typ))
8237 and then Full_View (Root_Typ) = Root_View
8238 then
8239 return True;
8240
8241 elsif Is_Incomplete_Type (Root_View)
8242 and then Present (Full_View (Root_View))
8243 and then Full_View (Root_View) = Root_Typ
8244 then
8245 return True;
8246 end if;
8247 end if;
8248
8249 return False;
8250 end Is_Matching_Limited_View;
8251
8252 -- Start of processing for Matches_Limited_With_View
8253
8254 begin
8255 -- In some cases a type imported through a limited_with clause, and
8256 -- its non-limited view are both visible, for example in an anonymous
8257 -- access-to-class-wide type in a formal, or when building the body
8258 -- for a subprogram renaming after the subprogram has been frozen.
8259 -- In these cases both entities designate the same type. In addition,
8260 -- if one of them is an actual in an instance, it may be a subtype of
8261 -- the non-limited view of the other.
8262
8263 if From_Limited_With (Typ_1)
8264 and then From_Limited_With (Typ_2)
8265 and then Available_View (Typ_1) = Available_View (Typ_2)
8266 then
8267 return True;
8268
8269 elsif From_Limited_With (Typ_1) then
8270 return Is_Matching_Limited_View (Typ_2, Available_View (Typ_1));
8271
8272 elsif From_Limited_With (Typ_2) then
8273 return Is_Matching_Limited_View (Typ_1, Available_View (Typ_2));
8274
8275 else
8276 return False;
8277 end if;
8278 end Matches_Limited_With_View;
8279
8280 -- Local variables
8281
8282 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
8283
8284 Type_1 : Entity_Id := T1;
8285 Type_2 : Entity_Id := T2;
8286
8287 -- Start of processing for Conforming_Types
8288
8289 begin
8290 -- The context is an instance association for a formal access-to-
8291 -- subprogram type; the formal parameter types require mapping because
8292 -- they may denote other formal parameters of the generic unit.
8293
8294 if Get_Inst then
8295 Type_1 := Get_Instance_Of (T1);
8296 Type_2 := Get_Instance_Of (T2);
8297 end if;
8298
8299 -- If one of the types is a view of the other introduced by a limited
8300 -- with clause, treat these as conforming for all purposes.
8301
8302 if Matches_Limited_With_View (T1, T2) then
8303 return True;
8304
8305 elsif Base_Types_Match (Type_1, Type_2) then
8306 if Ctype <= Mode_Conformant then
8307 return True;
8308
8309 else
8310 return
8311 Subtypes_Statically_Match (Type_1, Type_2)
8312 and then Dimensions_Match (Type_1, Type_2);
8313 end if;
8314
8315 elsif Is_Incomplete_Or_Private_Type (Type_1)
8316 and then Present (Full_View (Type_1))
8317 and then Base_Types_Match (Full_View (Type_1), Type_2)
8318 then
8319 return
8320 Ctype <= Mode_Conformant
8321 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
8322
8323 elsif Ekind (Type_2) = E_Incomplete_Type
8324 and then Present (Full_View (Type_2))
8325 and then Base_Types_Match (Type_1, Full_View (Type_2))
8326 then
8327 return
8328 Ctype <= Mode_Conformant
8329 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8330
8331 elsif Is_Private_Type (Type_2)
8332 and then In_Instance
8333 and then Present (Full_View (Type_2))
8334 and then Base_Types_Match (Type_1, Full_View (Type_2))
8335 then
8336 return
8337 Ctype <= Mode_Conformant
8338 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
8339
8340 -- Another confusion between views in a nested instance with an
8341 -- actual private type whose full view is not in scope.
8342
8343 elsif Ekind (Type_2) = E_Private_Subtype
8344 and then In_Instance
8345 and then Etype (Type_2) = Type_1
8346 then
8347 return True;
8348
8349 -- In Ada 2012, incomplete types (including limited views) can appear
8350 -- as actuals in instantiations, where they are conformant to the
8351 -- corresponding incomplete formal.
8352
8353 elsif Is_Incomplete_Type (Type_1)
8354 and then Is_Incomplete_Type (Type_2)
8355 and then In_Instance
8356 and then (Used_As_Generic_Actual (Type_1)
8357 or else Used_As_Generic_Actual (Type_2))
8358 then
8359 return True;
8360 end if;
8361
8362 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
8363 -- treated recursively because they carry a signature. As far as
8364 -- conformance is concerned, convention plays no role, and either
8365 -- or both could be access to protected subprograms.
8366
8367 Are_Anonymous_Access_To_Subprogram_Types :=
8368 Ekind (Type_1) in E_Anonymous_Access_Subprogram_Type
8369 | E_Anonymous_Access_Protected_Subprogram_Type
8370 and then
8371 Ekind (Type_2) in E_Anonymous_Access_Subprogram_Type
8372 | E_Anonymous_Access_Protected_Subprogram_Type;
8373
8374 -- Test anonymous access type case. For this case, static subtype
8375 -- matching is required for mode conformance (RM 6.3.1(15)). We check
8376 -- the base types because we may have built internal subtype entities
8377 -- to handle null-excluding types (see Process_Formals).
8378
8379 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
8380 and then
8381 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
8382
8383 -- Ada 2005 (AI-254)
8384
8385 or else Are_Anonymous_Access_To_Subprogram_Types
8386 then
8387 declare
8388 Desig_1 : Entity_Id;
8389 Desig_2 : Entity_Id;
8390
8391 begin
8392 -- In Ada 2005, access constant indicators must match for
8393 -- subtype conformance.
8394
8395 if Ada_Version >= Ada_2005
8396 and then Ctype >= Subtype_Conformant
8397 and then
8398 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
8399 then
8400 return False;
8401 end if;
8402
8403 Desig_1 := Find_Designated_Type (Type_1);
8404 Desig_2 := Find_Designated_Type (Type_2);
8405
8406 -- If the context is an instance association for a formal
8407 -- access-to-subprogram type; formal access parameter designated
8408 -- types require mapping because they may denote other formal
8409 -- parameters of the generic unit.
8410
8411 if Get_Inst then
8412 Desig_1 := Get_Instance_Of (Desig_1);
8413 Desig_2 := Get_Instance_Of (Desig_2);
8414 end if;
8415
8416 -- It is possible for a Class_Wide_Type to be introduced for an
8417 -- incomplete type, in which case there is a separate class_ wide
8418 -- type for the full view. The types conform if their Etypes
8419 -- conform, i.e. one may be the full view of the other. This can
8420 -- only happen in the context of an access parameter, other uses
8421 -- of an incomplete Class_Wide_Type are illegal.
8422
8423 if Is_Class_Wide_Type (Desig_1)
8424 and then
8425 Is_Class_Wide_Type (Desig_2)
8426 then
8427 return
8428 Conforming_Types
8429 (Etype (Base_Type (Desig_1)),
8430 Etype (Base_Type (Desig_2)), Ctype);
8431
8432 elsif Are_Anonymous_Access_To_Subprogram_Types then
8433 if Ada_Version < Ada_2005 then
8434 return
8435 Ctype = Type_Conformant
8436 or else Subtypes_Statically_Match (Desig_1, Desig_2);
8437
8438 -- We must check the conformance of the signatures themselves
8439
8440 else
8441 declare
8442 Conformant : Boolean;
8443 begin
8444 Check_Conformance
8445 (Desig_1, Desig_2, Ctype, False, Conformant);
8446 return Conformant;
8447 end;
8448 end if;
8449
8450 -- A limited view of an actual matches the corresponding
8451 -- incomplete formal.
8452
8453 elsif Ekind (Desig_2) = E_Incomplete_Subtype
8454 and then From_Limited_With (Desig_2)
8455 and then Used_As_Generic_Actual (Etype (Desig_2))
8456 then
8457 return True;
8458
8459 else
8460 return Base_Type (Desig_1) = Base_Type (Desig_2)
8461 and then (Ctype = Type_Conformant
8462 or else
8463 Subtypes_Statically_Match (Desig_1, Desig_2));
8464 end if;
8465 end;
8466
8467 -- Otherwise definitely no match
8468
8469 else
8470 if ((Ekind (Type_1) = E_Anonymous_Access_Type
8471 and then Is_Access_Type (Type_2))
8472 or else (Ekind (Type_2) = E_Anonymous_Access_Type
8473 and then Is_Access_Type (Type_1)))
8474 and then
8475 Conforming_Types
8476 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
8477 then
8478 May_Hide_Profile := True;
8479 end if;
8480
8481 return False;
8482 end if;
8483 end Conforming_Types;
8484
8485 --------------------------
8486 -- Create_Extra_Formals --
8487 --------------------------
8488
8489 procedure Create_Extra_Formals (E : Entity_Id) is
8490 First_Extra : Entity_Id := Empty;
8491 Formal : Entity_Id;
8492 Last_Extra : Entity_Id := Empty;
8493
8494 function Add_Extra_Formal
8495 (Assoc_Entity : Entity_Id;
8496 Typ : Entity_Id;
8497 Scope : Entity_Id;
8498 Suffix : String) return Entity_Id;
8499 -- Add an extra formal to the current list of formals and extra formals.
8500 -- The extra formal is added to the end of the list of extra formals,
8501 -- and also returned as the result. These formals are always of mode IN.
8502 -- The new formal has the type Typ, is declared in Scope, and its name
8503 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
8504 -- The following suffixes are currently used. They should not be changed
8505 -- without coordinating with CodePeer, which makes use of these to
8506 -- provide better messages.
8507
8508 -- O denotes the Constrained bit.
8509 -- L denotes the accessibility level.
8510 -- BIP_xxx denotes an extra formal for a build-in-place function. See
8511 -- the full list in exp_ch6.BIP_Formal_Kind.
8512
8513 ----------------------
8514 -- Add_Extra_Formal --
8515 ----------------------
8516
8517 function Add_Extra_Formal
8518 (Assoc_Entity : Entity_Id;
8519 Typ : Entity_Id;
8520 Scope : Entity_Id;
8521 Suffix : String) return Entity_Id
8522 is
8523 EF : constant Entity_Id :=
8524 Make_Defining_Identifier (Sloc (Assoc_Entity),
8525 Chars => New_External_Name (Chars (Assoc_Entity),
8526 Suffix => Suffix));
8527
8528 begin
8529 -- A little optimization. Never generate an extra formal for the
8530 -- _init operand of an initialization procedure, since it could
8531 -- never be used.
8532
8533 if Chars (Formal) = Name_uInit then
8534 return Empty;
8535 end if;
8536
8537 Set_Ekind (EF, E_In_Parameter);
8538 Set_Actual_Subtype (EF, Typ);
8539 Set_Etype (EF, Typ);
8540 Set_Scope (EF, Scope);
8541 Set_Mechanism (EF, Default_Mechanism);
8542 Set_Formal_Validity (EF);
8543
8544 if No (First_Extra) then
8545 First_Extra := EF;
8546 Set_Extra_Formals (Scope, EF);
8547 end if;
8548
8549 if Present (Last_Extra) then
8550 Set_Extra_Formal (Last_Extra, EF);
8551 end if;
8552
8553 Last_Extra := EF;
8554
8555 return EF;
8556 end Add_Extra_Formal;
8557
8558 -- Local variables
8559
8560 Formal_Type : Entity_Id;
8561 P_Formal : Entity_Id := Empty;
8562
8563 -- Start of processing for Create_Extra_Formals
8564
8565 begin
8566 -- We never generate extra formals if expansion is not active because we
8567 -- don't need them unless we are generating code.
8568
8569 if not Expander_Active then
8570 return;
8571 end if;
8572
8573 -- No need to generate extra formals in interface thunks whose target
8574 -- primitive has no extra formals.
8575
8576 if Is_Thunk (E) and then No (Extra_Formals (Thunk_Entity (E))) then
8577 return;
8578 end if;
8579
8580 -- If this is a derived subprogram then the subtypes of the parent
8581 -- subprogram's formal parameters will be used to determine the need
8582 -- for extra formals.
8583
8584 if Is_Overloadable (E) and then Present (Alias (E)) then
8585 P_Formal := First_Formal (Alias (E));
8586 end if;
8587
8588 Formal := First_Formal (E);
8589 while Present (Formal) loop
8590 Last_Extra := Formal;
8591 Next_Formal (Formal);
8592 end loop;
8593
8594 -- If Extra_Formals were already created, don't do it again. This
8595 -- situation may arise for subprogram types created as part of
8596 -- dispatching calls (see Expand_Dispatching_Call).
8597
8598 if Present (Last_Extra) and then Present (Extra_Formal (Last_Extra)) then
8599 return;
8600 end if;
8601
8602 -- If the subprogram is a predefined dispatching subprogram then don't
8603 -- generate any extra constrained or accessibility level formals. In
8604 -- general we suppress these for internal subprograms (by not calling
8605 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
8606 -- generated stream attributes do get passed through because extra
8607 -- build-in-place formals are needed in some cases (limited 'Input).
8608
8609 if Is_Predefined_Internal_Operation (E) then
8610 goto Test_For_Func_Result_Extras;
8611 end if;
8612
8613 Formal := First_Formal (E);
8614 while Present (Formal) loop
8615
8616 -- Create extra formal for supporting the attribute 'Constrained.
8617 -- The case of a private type view without discriminants also
8618 -- requires the extra formal if the underlying type has defaulted
8619 -- discriminants.
8620
8621 if Ekind (Formal) /= E_In_Parameter then
8622 if Present (P_Formal) then
8623 Formal_Type := Etype (P_Formal);
8624 else
8625 Formal_Type := Etype (Formal);
8626 end if;
8627
8628 -- Do not produce extra formals for Unchecked_Union parameters.
8629 -- Jump directly to the end of the loop.
8630
8631 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
8632 goto Skip_Extra_Formal_Generation;
8633 end if;
8634
8635 if not Has_Discriminants (Formal_Type)
8636 and then Ekind (Formal_Type) in Private_Kind
8637 and then Present (Underlying_Type (Formal_Type))
8638 then
8639 Formal_Type := Underlying_Type (Formal_Type);
8640 end if;
8641
8642 -- Suppress the extra formal if formal's subtype is constrained or
8643 -- indefinite, or we're compiling for Ada 2012 and the underlying
8644 -- type is tagged and limited. In Ada 2012, a limited tagged type
8645 -- can have defaulted discriminants, but 'Constrained is required
8646 -- to return True, so the formal is never needed (see AI05-0214).
8647 -- Note that this ensures consistency of calling sequences for
8648 -- dispatching operations when some types in a class have defaults
8649 -- on discriminants and others do not (and requiring the extra
8650 -- formal would introduce distributed overhead).
8651
8652 -- If the type does not have a completion yet, treat as prior to
8653 -- Ada 2012 for consistency.
8654
8655 if Has_Discriminants (Formal_Type)
8656 and then not Is_Constrained (Formal_Type)
8657 and then Is_Definite_Subtype (Formal_Type)
8658 and then (Ada_Version < Ada_2012
8659 or else No (Underlying_Type (Formal_Type))
8660 or else not
8661 (Is_Limited_Type (Formal_Type)
8662 and then
8663 (Is_Tagged_Type
8664 (Underlying_Type (Formal_Type)))))
8665 then
8666 Set_Extra_Constrained
8667 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
8668 end if;
8669 end if;
8670
8671 -- Create extra formal for supporting accessibility checking. This
8672 -- is done for both anonymous access formals and formals of named
8673 -- access types that are marked as controlling formals. The latter
8674 -- case can occur when Expand_Dispatching_Call creates a subprogram
8675 -- type and substitutes the types of access-to-class-wide actuals
8676 -- for the anonymous access-to-specific-type of controlling formals.
8677 -- Base_Type is applied because in cases where there is a null
8678 -- exclusion the formal may have an access subtype.
8679
8680 -- This is suppressed if we specifically suppress accessibility
8681 -- checks at the package level for either the subprogram, or the
8682 -- package in which it resides. However, we do not suppress it
8683 -- simply if the scope has accessibility checks suppressed, since
8684 -- this could cause trouble when clients are compiled with a
8685 -- different suppression setting. The explicit checks at the
8686 -- package level are safe from this point of view.
8687
8688 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
8689 or else (Is_Controlling_Formal (Formal)
8690 and then Is_Access_Type (Base_Type (Etype (Formal)))))
8691 and then not
8692 (Explicit_Suppress (E, Accessibility_Check)
8693 or else
8694 Explicit_Suppress (Scope (E), Accessibility_Check))
8695 and then
8696 (No (P_Formal)
8697 or else Present (Extra_Accessibility (P_Formal)))
8698 then
8699 Set_Extra_Accessibility
8700 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
8701 end if;
8702
8703 -- This label is required when skipping extra formal generation for
8704 -- Unchecked_Union parameters.
8705
8706 <<Skip_Extra_Formal_Generation>>
8707
8708 if Present (P_Formal) then
8709 Next_Formal (P_Formal);
8710 end if;
8711
8712 Next_Formal (Formal);
8713 end loop;
8714
8715 <<Test_For_Func_Result_Extras>>
8716
8717 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
8718 -- function call is ... determined by the point of call ...".
8719
8720 if Needs_Result_Accessibility_Level (E) then
8721 Set_Extra_Accessibility_Of_Result
8722 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
8723 end if;
8724
8725 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
8726 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
8727
8728 if Is_Build_In_Place_Function (E) then
8729 declare
8730 Result_Subt : constant Entity_Id := Etype (E);
8731 Formal_Typ : Entity_Id;
8732 Subp_Decl : Node_Id;
8733 Discard : Entity_Id;
8734
8735 begin
8736 -- In the case of functions with unconstrained result subtypes,
8737 -- add a 4-state formal indicating whether the return object is
8738 -- allocated by the caller (1), or should be allocated by the
8739 -- callee on the secondary stack (2), in the global heap (3), or
8740 -- in a user-defined storage pool (4). For the moment we just use
8741 -- Natural for the type of this formal. Note that this formal
8742 -- isn't usually needed in the case where the result subtype is
8743 -- constrained, but it is needed when the function has a tagged
8744 -- result, because generally such functions can be called in a
8745 -- dispatching context and such calls must be handled like calls
8746 -- to a class-wide function.
8747
8748 if Needs_BIP_Alloc_Form (E) then
8749 Discard :=
8750 Add_Extra_Formal
8751 (E, Standard_Natural,
8752 E, BIP_Formal_Suffix (BIP_Alloc_Form));
8753
8754 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
8755 -- use a user-defined pool. This formal is not added on
8756 -- ZFP as those targets do not support pools.
8757
8758 if RTE_Available (RE_Root_Storage_Pool_Ptr) then
8759 Discard :=
8760 Add_Extra_Formal
8761 (E, RTE (RE_Root_Storage_Pool_Ptr),
8762 E, BIP_Formal_Suffix (BIP_Storage_Pool));
8763 end if;
8764 end if;
8765
8766 -- In the case of functions whose result type needs finalization,
8767 -- add an extra formal which represents the finalization master.
8768
8769 if Needs_BIP_Finalization_Master (E) then
8770 Discard :=
8771 Add_Extra_Formal
8772 (E, RTE (RE_Finalization_Master_Ptr),
8773 E, BIP_Formal_Suffix (BIP_Finalization_Master));
8774 end if;
8775
8776 -- When the result type contains tasks, add two extra formals: the
8777 -- master of the tasks to be created, and the caller's activation
8778 -- chain.
8779
8780 if Needs_BIP_Task_Actuals (E) then
8781 Discard :=
8782 Add_Extra_Formal
8783 (E, RTE (RE_Master_Id),
8784 E, BIP_Formal_Suffix (BIP_Task_Master));
8785
8786 Set_Has_Master_Entity (E);
8787
8788 Discard :=
8789 Add_Extra_Formal
8790 (E, RTE (RE_Activation_Chain_Access),
8791 E, BIP_Formal_Suffix (BIP_Activation_Chain));
8792 end if;
8793
8794 -- All build-in-place functions get an extra formal that will be
8795 -- passed the address of the return object within the caller.
8796
8797 Formal_Typ :=
8798 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
8799
8800 -- Incomplete_View_From_Limited_With is needed here because
8801 -- gigi gets confused if the designated type is the full view
8802 -- coming from a limited-with'ed package. In the normal case,
8803 -- (no limited with) Incomplete_View_From_Limited_With
8804 -- returns Result_Subt.
8805
8806 Set_Directly_Designated_Type
8807 (Formal_Typ, Incomplete_View_From_Limited_With (Result_Subt));
8808 Set_Etype (Formal_Typ, Formal_Typ);
8809 Set_Depends_On_Private
8810 (Formal_Typ, Has_Private_Component (Formal_Typ));
8811 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
8812 Set_Is_Access_Constant (Formal_Typ, False);
8813
8814 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
8815 -- the designated type comes from the limited view (for back-end
8816 -- purposes).
8817
8818 Set_From_Limited_With
8819 (Formal_Typ, From_Limited_With (Result_Subt));
8820
8821 Layout_Type (Formal_Typ);
8822
8823 -- Force the definition of the Itype in case of internal function
8824 -- calls within the same or nested scope.
8825
8826 if Is_Subprogram_Or_Generic_Subprogram (E) then
8827 Subp_Decl := Parent (E);
8828
8829 -- The insertion point for an Itype reference should be after
8830 -- the unit declaration node of the subprogram. An exception
8831 -- to this are inherited operations from a parent type in which
8832 -- case the derived type acts as their parent.
8833
8834 if Nkind (Subp_Decl) in N_Function_Specification
8835 | N_Procedure_Specification
8836 then
8837 Subp_Decl := Parent (Subp_Decl);
8838 end if;
8839
8840 Build_Itype_Reference (Formal_Typ, Subp_Decl);
8841 end if;
8842
8843 Discard :=
8844 Add_Extra_Formal
8845 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
8846 end;
8847 end if;
8848
8849 -- If this is an instance of a generic, we need to have extra formals
8850 -- for the Alias.
8851
8852 if Is_Generic_Instance (E) and then Present (Alias (E)) then
8853 Set_Extra_Formals (Alias (E), Extra_Formals (E));
8854 end if;
8855 end Create_Extra_Formals;
8856
8857 -----------------------------
8858 -- Enter_Overloaded_Entity --
8859 -----------------------------
8860
8861 procedure Enter_Overloaded_Entity (S : Entity_Id) is
8862 function Matches_Predefined_Op return Boolean;
8863 -- This returns an approximation of whether S matches a predefined
8864 -- operator, based on the operator symbol, and the parameter and result
8865 -- types. The rules are scattered throughout chapter 4 of the Ada RM.
8866
8867 ---------------------------
8868 -- Matches_Predefined_Op --
8869 ---------------------------
8870
8871 function Matches_Predefined_Op return Boolean is
8872 Formal_1 : constant Entity_Id := First_Formal (S);
8873 Formal_2 : constant Entity_Id := Next_Formal (Formal_1);
8874 Op : constant Name_Id := Chars (S);
8875 Result_Type : constant Entity_Id := Base_Type (Etype (S));
8876 Type_1 : constant Entity_Id := Base_Type (Etype (Formal_1));
8877
8878 begin
8879 -- Binary operator
8880
8881 if Present (Formal_2) then
8882 declare
8883 Type_2 : constant Entity_Id := Base_Type (Etype (Formal_2));
8884
8885 begin
8886 -- All but "&" and "**" have same-types parameters
8887
8888 case Op is
8889 when Name_Op_Concat
8890 | Name_Op_Expon
8891 =>
8892 null;
8893
8894 when others =>
8895 if Type_1 /= Type_2 then
8896 return False;
8897 end if;
8898 end case;
8899
8900 -- Check parameter and result types
8901
8902 case Op is
8903 when Name_Op_And
8904 | Name_Op_Or
8905 | Name_Op_Xor
8906 =>
8907 return
8908 Is_Boolean_Type (Result_Type)
8909 and then Result_Type = Type_1;
8910
8911 when Name_Op_Mod
8912 | Name_Op_Rem
8913 =>
8914 return
8915 Is_Integer_Type (Result_Type)
8916 and then Result_Type = Type_1;
8917
8918 when Name_Op_Add
8919 | Name_Op_Divide
8920 | Name_Op_Multiply
8921 | Name_Op_Subtract
8922 =>
8923 return
8924 Is_Numeric_Type (Result_Type)
8925 and then Result_Type = Type_1;
8926
8927 when Name_Op_Eq
8928 | Name_Op_Ne
8929 =>
8930 return
8931 Is_Boolean_Type (Result_Type)
8932 and then not Is_Limited_Type (Type_1);
8933
8934 when Name_Op_Ge
8935 | Name_Op_Gt
8936 | Name_Op_Le
8937 | Name_Op_Lt
8938 =>
8939 return
8940 Is_Boolean_Type (Result_Type)
8941 and then (Is_Array_Type (Type_1)
8942 or else Is_Scalar_Type (Type_1));
8943
8944 when Name_Op_Concat =>
8945 return Is_Array_Type (Result_Type);
8946
8947 when Name_Op_Expon =>
8948 return
8949 (Is_Integer_Type (Result_Type)
8950 or else Is_Floating_Point_Type (Result_Type))
8951 and then Result_Type = Type_1
8952 and then Type_2 = Standard_Integer;
8953
8954 when others =>
8955 raise Program_Error;
8956 end case;
8957 end;
8958
8959 -- Unary operator
8960
8961 else
8962 case Op is
8963 when Name_Op_Abs
8964 | Name_Op_Add
8965 | Name_Op_Subtract
8966 =>
8967 return
8968 Is_Numeric_Type (Result_Type)
8969 and then Result_Type = Type_1;
8970
8971 when Name_Op_Not =>
8972 return
8973 Is_Boolean_Type (Result_Type)
8974 and then Result_Type = Type_1;
8975
8976 when others =>
8977 raise Program_Error;
8978 end case;
8979 end if;
8980 end Matches_Predefined_Op;
8981
8982 -- Local variables
8983
8984 E : Entity_Id := Current_Entity_In_Scope (S);
8985 C_E : Entity_Id := Current_Entity (S);
8986
8987 -- Start of processing for Enter_Overloaded_Entity
8988
8989 begin
8990 if Present (E) then
8991 Set_Has_Homonym (E);
8992 Set_Has_Homonym (S);
8993 end if;
8994
8995 Set_Is_Immediately_Visible (S);
8996 Set_Scope (S, Current_Scope);
8997
8998 -- Chain new entity if front of homonym in current scope, so that
8999 -- homonyms are contiguous.
9000
9001 if Present (E) and then E /= C_E then
9002 while Homonym (C_E) /= E loop
9003 C_E := Homonym (C_E);
9004 end loop;
9005
9006 Set_Homonym (C_E, S);
9007
9008 else
9009 E := C_E;
9010 Set_Current_Entity (S);
9011 end if;
9012
9013 Set_Homonym (S, E);
9014
9015 if Is_Inherited_Operation (S) then
9016 Append_Inherited_Subprogram (S);
9017 else
9018 Append_Entity (S, Current_Scope);
9019 end if;
9020
9021 Set_Public_Status (S);
9022
9023 if Debug_Flag_E then
9024 Write_Str ("New overloaded entity chain: ");
9025 Write_Name (Chars (S));
9026
9027 E := S;
9028 while Present (E) loop
9029 Write_Str (" "); Write_Int (Int (E));
9030 E := Homonym (E);
9031 end loop;
9032
9033 Write_Eol;
9034 end if;
9035
9036 -- Generate warning for hiding
9037
9038 if Warn_On_Hiding
9039 and then Comes_From_Source (S)
9040 and then In_Extended_Main_Source_Unit (S)
9041 then
9042 E := S;
9043 loop
9044 E := Homonym (E);
9045 exit when No (E);
9046
9047 -- Warn unless genuine overloading. Do not emit warning on
9048 -- hiding predefined operators in Standard (these are either an
9049 -- artifact of our implicit declarations, or simple noise) but
9050 -- keep warning on a operator defined on a local subtype, because
9051 -- of the real danger that different operators may be applied in
9052 -- various parts of the program.
9053
9054 -- Note that if E and S have the same scope, there is never any
9055 -- hiding. Either the two conflict, and the program is illegal,
9056 -- or S is overriding an implicit inherited subprogram.
9057
9058 if Scope (E) /= Scope (S)
9059 and then (not Is_Overloadable (E)
9060 or else Subtype_Conformant (E, S))
9061 and then (Is_Immediately_Visible (E)
9062 or else Is_Potentially_Use_Visible (S))
9063 then
9064 if Scope (E) = Standard_Standard then
9065 if Nkind (S) = N_Defining_Operator_Symbol
9066 and then Scope (Base_Type (Etype (First_Formal (S)))) /=
9067 Scope (S)
9068 and then Matches_Predefined_Op
9069 then
9070 Error_Msg_N
9071 ("declaration of & hides predefined operator?h?", S);
9072 end if;
9073
9074 -- E not immediately within Standard
9075
9076 else
9077 Error_Msg_Sloc := Sloc (E);
9078 Error_Msg_N ("declaration of & hides one #?h?", S);
9079 end if;
9080 end if;
9081 end loop;
9082 end if;
9083 end Enter_Overloaded_Entity;
9084
9085 -----------------------------
9086 -- Check_Untagged_Equality --
9087 -----------------------------
9088
9089 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
9090 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
9091 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
9092 Obj_Decl : Node_Id;
9093
9094 begin
9095 -- This check applies only if we have a subprogram declaration with an
9096 -- untagged record type that is conformant to the predefined op.
9097
9098 if Nkind (Decl) /= N_Subprogram_Declaration
9099 or else not Is_Record_Type (Typ)
9100 or else Is_Tagged_Type (Typ)
9101 or else Etype (Next_Formal (First_Formal (Eq_Op))) /= Typ
9102 then
9103 return;
9104 end if;
9105
9106 -- In Ada 2012 case, we will output errors or warnings depending on
9107 -- the setting of debug flag -gnatd.E.
9108
9109 if Ada_Version >= Ada_2012 then
9110 Error_Msg_Warn := Debug_Flag_Dot_EE;
9111
9112 -- In earlier versions of Ada, nothing to do unless we are warning on
9113 -- Ada 2012 incompatibilities (Warn_On_Ada_2012_Incompatibility set).
9114
9115 else
9116 if not Warn_On_Ada_2012_Compatibility then
9117 return;
9118 end if;
9119 end if;
9120
9121 -- Cases where the type has already been frozen
9122
9123 if Is_Frozen (Typ) then
9124
9125 -- The check applies to a primitive operation, so check that type
9126 -- and equality operation are in the same scope.
9127
9128 if Scope (Typ) /= Current_Scope then
9129 return;
9130
9131 -- If the type is a generic actual (sub)type, the operation is not
9132 -- primitive either because the base type is declared elsewhere.
9133
9134 elsif Is_Generic_Actual_Type (Typ) then
9135 return;
9136
9137 -- Here we have a definite error of declaration after freezing
9138
9139 else
9140 if Ada_Version >= Ada_2012 then
9141 Error_Msg_NE
9142 ("equality operator must be declared before type & is "
9143 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)<<", Eq_Op, Typ);
9144
9145 -- In Ada 2012 mode with error turned to warning, output one
9146 -- more warning to warn that the equality operation may not
9147 -- compose. This is the consequence of ignoring the error.
9148
9149 if Error_Msg_Warn then
9150 Error_Msg_N ("\equality operation may not compose??", Eq_Op);
9151 end if;
9152
9153 else
9154 Error_Msg_NE
9155 ("equality operator must be declared before type& is "
9156 & "frozen (RM 4.5.2 (9.8)) (Ada 2012)?y?", Eq_Op, Typ);
9157 end if;
9158
9159 -- If we are in the package body, we could just move the
9160 -- declaration to the package spec, so add a message saying that.
9161
9162 if In_Package_Body (Scope (Typ)) then
9163 if Ada_Version >= Ada_2012 then
9164 Error_Msg_N
9165 ("\move declaration to package spec<<", Eq_Op);
9166 else
9167 Error_Msg_N
9168 ("\move declaration to package spec (Ada 2012)?y?", Eq_Op);
9169 end if;
9170
9171 -- Otherwise try to find the freezing point for better message.
9172
9173 else
9174 Obj_Decl := Next (Parent (Typ));
9175 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9176 if Nkind (Obj_Decl) = N_Object_Declaration
9177 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9178 then
9179 -- Freezing point, output warnings
9180
9181 if Ada_Version >= Ada_2012 then
9182 Error_Msg_NE
9183 ("type& is frozen by declaration??", Obj_Decl, Typ);
9184 Error_Msg_N
9185 ("\an equality operator cannot be declared after "
9186 & "this point??",
9187 Obj_Decl);
9188 else
9189 Error_Msg_NE
9190 ("type& is frozen by declaration (Ada 2012)?y?",
9191 Obj_Decl, Typ);
9192 Error_Msg_N
9193 ("\an equality operator cannot be declared after "
9194 & "this point (Ada 2012)?y?",
9195 Obj_Decl);
9196 end if;
9197
9198 exit;
9199
9200 -- If we reach generated code for subprogram declaration
9201 -- or body, it is the body that froze the type and the
9202 -- declaration is legal.
9203
9204 elsif Sloc (Obj_Decl) = Sloc (Decl) then
9205 return;
9206 end if;
9207
9208 Next (Obj_Decl);
9209 end loop;
9210 end if;
9211 end if;
9212
9213 -- Here if type is not frozen yet. It is illegal to have a primitive
9214 -- equality declared in the private part if the type is visible.
9215
9216 elsif not In_Same_List (Parent (Typ), Decl)
9217 and then not Is_Limited_Type (Typ)
9218 then
9219 -- Shouldn't we give an RM reference here???
9220
9221 if Ada_Version >= Ada_2012 then
9222 Error_Msg_N
9223 ("equality operator appears too late<<", Eq_Op);
9224 else
9225 Error_Msg_N
9226 ("equality operator appears too late (Ada 2012)?y?", Eq_Op);
9227 end if;
9228
9229 -- Finally check for AI12-0352: declaration of a user-defined primitive
9230 -- equality operation for a record type T is illegal if it occurs after
9231 -- a type has been derived from T.
9232
9233 else
9234 Obj_Decl := Next (Parent (Typ));
9235
9236 while Present (Obj_Decl) and then Obj_Decl /= Decl loop
9237 if Nkind (Obj_Decl) = N_Full_Type_Declaration
9238 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
9239 then
9240 Error_Msg_N
9241 ("equality operator cannot appear after derivation", Eq_Op);
9242 Error_Msg_NE
9243 ("an equality operator for& cannot be declared after "
9244 & "this point??",
9245 Obj_Decl, Typ);
9246 end if;
9247
9248 Next (Obj_Decl);
9249 end loop;
9250 end if;
9251 end Check_Untagged_Equality;
9252
9253 -----------------------------
9254 -- Find_Corresponding_Spec --
9255 -----------------------------
9256
9257 function Find_Corresponding_Spec
9258 (N : Node_Id;
9259 Post_Error : Boolean := True) return Entity_Id
9260 is
9261 Spec : constant Node_Id := Specification (N);
9262 Designator : constant Entity_Id := Defining_Entity (Spec);
9263
9264 E : Entity_Id;
9265
9266 function Different_Generic_Profile (E : Entity_Id) return Boolean;
9267 -- Even if fully conformant, a body may depend on a generic actual when
9268 -- the spec does not, or vice versa, in which case they were distinct
9269 -- entities in the generic.
9270
9271 -------------------------------
9272 -- Different_Generic_Profile --
9273 -------------------------------
9274
9275 function Different_Generic_Profile (E : Entity_Id) return Boolean is
9276 F1, F2 : Entity_Id;
9277
9278 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean;
9279 -- Check that the types of corresponding formals have the same
9280 -- generic actual if any. We have to account for subtypes of a
9281 -- generic formal, declared between a spec and a body, which may
9282 -- appear distinct in an instance but matched in the generic, and
9283 -- the subtype may be used either in the spec or the body of the
9284 -- subprogram being checked.
9285
9286 -------------------------
9287 -- Same_Generic_Actual --
9288 -------------------------
9289
9290 function Same_Generic_Actual (T1, T2 : Entity_Id) return Boolean is
9291
9292 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean;
9293 -- Predicate to check whether S1 is a subtype of S2 in the source
9294 -- of the instance.
9295
9296 -------------------------
9297 -- Is_Declared_Subtype --
9298 -------------------------
9299
9300 function Is_Declared_Subtype (S1, S2 : Entity_Id) return Boolean is
9301 begin
9302 return Comes_From_Source (Parent (S1))
9303 and then Nkind (Parent (S1)) = N_Subtype_Declaration
9304 and then Is_Entity_Name (Subtype_Indication (Parent (S1)))
9305 and then Entity (Subtype_Indication (Parent (S1))) = S2;
9306 end Is_Declared_Subtype;
9307
9308 -- Start of processing for Same_Generic_Actual
9309
9310 begin
9311 return Is_Generic_Actual_Type (T1) = Is_Generic_Actual_Type (T2)
9312 or else Is_Declared_Subtype (T1, T2)
9313 or else Is_Declared_Subtype (T2, T1);
9314 end Same_Generic_Actual;
9315
9316 -- Start of processing for Different_Generic_Profile
9317
9318 begin
9319 if not In_Instance then
9320 return False;
9321
9322 elsif Ekind (E) = E_Function
9323 and then not Same_Generic_Actual (Etype (E), Etype (Designator))
9324 then
9325 return True;
9326 end if;
9327
9328 F1 := First_Formal (Designator);
9329 F2 := First_Formal (E);
9330 while Present (F1) loop
9331 if not Same_Generic_Actual (Etype (F1), Etype (F2)) then
9332 return True;
9333 end if;
9334
9335 Next_Formal (F1);
9336 Next_Formal (F2);
9337 end loop;
9338
9339 return False;
9340 end Different_Generic_Profile;
9341
9342 -- Start of processing for Find_Corresponding_Spec
9343
9344 begin
9345 E := Current_Entity (Designator);
9346 while Present (E) loop
9347
9348 -- We are looking for a matching spec. It must have the same scope,
9349 -- and the same name, and either be type conformant, or be the case
9350 -- of a library procedure spec and its body (which belong to one
9351 -- another regardless of whether they are type conformant or not).
9352
9353 if Scope (E) = Current_Scope then
9354 if Current_Scope = Standard_Standard
9355 or else (Ekind (E) = Ekind (Designator)
9356 and then Type_Conformant (E, Designator))
9357 then
9358 -- Within an instantiation, we know that spec and body are
9359 -- subtype conformant, because they were subtype conformant in
9360 -- the generic. We choose the subtype-conformant entity here as
9361 -- well, to resolve spurious ambiguities in the instance that
9362 -- were not present in the generic (i.e. when two different
9363 -- types are given the same actual). If we are looking for a
9364 -- spec to match a body, full conformance is expected.
9365
9366 if In_Instance then
9367
9368 -- Inherit the convention and "ghostness" of the matching
9369 -- spec to ensure proper full and subtype conformance.
9370
9371 Set_Convention (Designator, Convention (E));
9372
9373 -- Skip past subprogram bodies and subprogram renamings that
9374 -- may appear to have a matching spec, but that aren't fully
9375 -- conformant with it. That can occur in cases where an
9376 -- actual type causes unrelated homographs in the instance.
9377
9378 if Nkind (N) in N_Subprogram_Body
9379 | N_Subprogram_Renaming_Declaration
9380 and then Present (Homonym (E))
9381 and then not Fully_Conformant (Designator, E)
9382 then
9383 goto Next_Entity;
9384
9385 elsif not Subtype_Conformant (Designator, E) then
9386 goto Next_Entity;
9387
9388 elsif Different_Generic_Profile (E) then
9389 goto Next_Entity;
9390 end if;
9391 end if;
9392
9393 -- Ada 2012 (AI05-0165): For internally generated bodies of
9394 -- null procedures locate the internally generated spec. We
9395 -- enforce mode conformance since a tagged type may inherit
9396 -- from interfaces several null primitives which differ only
9397 -- in the mode of the formals.
9398
9399 if not (Comes_From_Source (E))
9400 and then Is_Null_Procedure (E)
9401 and then not Mode_Conformant (Designator, E)
9402 then
9403 null;
9404
9405 -- For null procedures coming from source that are completions,
9406 -- analysis of the generated body will establish the link.
9407
9408 elsif Comes_From_Source (E)
9409 and then Nkind (Spec) = N_Procedure_Specification
9410 and then Null_Present (Spec)
9411 then
9412 return E;
9413
9414 -- Expression functions can be completions, but cannot be
9415 -- completed by an explicit body.
9416
9417 elsif Comes_From_Source (E)
9418 and then Comes_From_Source (N)
9419 and then Nkind (N) = N_Subprogram_Body
9420 and then Nkind (Original_Node (Unit_Declaration_Node (E))) =
9421 N_Expression_Function
9422 then
9423 Error_Msg_Sloc := Sloc (E);
9424 Error_Msg_N ("body conflicts with expression function#", N);
9425 return Empty;
9426
9427 elsif not Has_Completion (E) then
9428 if Nkind (N) /= N_Subprogram_Body_Stub then
9429 Set_Corresponding_Spec (N, E);
9430 end if;
9431
9432 Set_Has_Completion (E);
9433 return E;
9434
9435 elsif Nkind (Parent (N)) = N_Subunit then
9436
9437 -- If this is the proper body of a subunit, the completion
9438 -- flag is set when analyzing the stub.
9439
9440 return E;
9441
9442 -- If E is an internal function with a controlling result that
9443 -- was created for an operation inherited by a null extension,
9444 -- it may be overridden by a body without a previous spec (one
9445 -- more reason why these should be shunned). In that case we
9446 -- remove the generated body if present, because the current
9447 -- one is the explicit overriding.
9448
9449 elsif Ekind (E) = E_Function
9450 and then Ada_Version >= Ada_2005
9451 and then not Comes_From_Source (E)
9452 and then Has_Controlling_Result (E)
9453 and then Is_Null_Extension (Etype (E))
9454 and then Comes_From_Source (Spec)
9455 then
9456 Set_Has_Completion (E, False);
9457
9458 if Expander_Active
9459 and then Nkind (Parent (E)) = N_Function_Specification
9460 then
9461 Remove
9462 (Unit_Declaration_Node
9463 (Corresponding_Body (Unit_Declaration_Node (E))));
9464
9465 return E;
9466
9467 -- If expansion is disabled, or if the wrapper function has
9468 -- not been generated yet, this a late body overriding an
9469 -- inherited operation, or it is an overriding by some other
9470 -- declaration before the controlling result is frozen. In
9471 -- either case this is a declaration of a new entity.
9472
9473 else
9474 return Empty;
9475 end if;
9476
9477 -- If the body already exists, then this is an error unless
9478 -- the previous declaration is the implicit declaration of a
9479 -- derived subprogram. It is also legal for an instance to
9480 -- contain type conformant overloadable declarations (but the
9481 -- generic declaration may not), per 8.3(26/2).
9482
9483 elsif No (Alias (E))
9484 and then not Is_Intrinsic_Subprogram (E)
9485 and then not In_Instance
9486 and then Post_Error
9487 then
9488 Error_Msg_Sloc := Sloc (E);
9489
9490 if Is_Imported (E) then
9491 Error_Msg_NE
9492 ("body not allowed for imported subprogram & declared#",
9493 N, E);
9494 else
9495 Error_Msg_NE ("duplicate body for & declared#", N, E);
9496 end if;
9497 end if;
9498
9499 -- Child units cannot be overloaded, so a conformance mismatch
9500 -- between body and a previous spec is an error.
9501
9502 elsif Is_Child_Unit (E)
9503 and then
9504 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
9505 and then
9506 Nkind (Parent (Unit_Declaration_Node (Designator))) =
9507 N_Compilation_Unit
9508 and then Post_Error
9509 then
9510 Error_Msg_N
9511 ("body of child unit does not match previous declaration", N);
9512 end if;
9513 end if;
9514
9515 <<Next_Entity>>
9516 E := Homonym (E);
9517 end loop;
9518
9519 -- On exit, we know that no previous declaration of subprogram exists
9520
9521 return Empty;
9522 end Find_Corresponding_Spec;
9523
9524 ----------------------
9525 -- Fully_Conformant --
9526 ----------------------
9527
9528 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9529 Result : Boolean;
9530 begin
9531 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
9532 return Result;
9533 end Fully_Conformant;
9534
9535 ----------------------------------
9536 -- Fully_Conformant_Expressions --
9537 ----------------------------------
9538
9539 function Fully_Conformant_Expressions
9540 (Given_E1 : Node_Id;
9541 Given_E2 : Node_Id;
9542 Report : Boolean := False) return Boolean
9543 is
9544 E1 : constant Node_Id := Original_Node (Given_E1);
9545 E2 : constant Node_Id := Original_Node (Given_E2);
9546 -- We always test conformance on original nodes, since it is possible
9547 -- for analysis and/or expansion to make things look as though they
9548 -- conform when they do not, e.g. by converting 1+2 into 3.
9549
9550 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean;
9551 -- ???
9552
9553 function FCL (L1 : List_Id; L2 : List_Id) return Boolean;
9554 -- Compare elements of two lists for conformance. Elements have to be
9555 -- conformant, and actuals inserted as default parameters do not match
9556 -- explicit actuals with the same value.
9557
9558 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean;
9559 -- Compare an operator node with a function call
9560
9561 ---------
9562 -- FCE --
9563 ---------
9564
9565 function FCE (Given_E1 : Node_Id; Given_E2 : Node_Id) return Boolean is
9566 begin
9567 return Fully_Conformant_Expressions (Given_E1, Given_E2, Report);
9568 end FCE;
9569
9570 ---------
9571 -- FCL --
9572 ---------
9573
9574 function FCL (L1 : List_Id; L2 : List_Id) return Boolean is
9575 N1 : Node_Id;
9576 N2 : Node_Id;
9577
9578 begin
9579 if L1 = No_List then
9580 N1 := Empty;
9581 else
9582 N1 := First (L1);
9583 end if;
9584
9585 if L2 = No_List then
9586 N2 := Empty;
9587 else
9588 N2 := First (L2);
9589 end if;
9590
9591 -- Compare two lists, skipping rewrite insertions (we want to compare
9592 -- the original trees, not the expanded versions).
9593
9594 loop
9595 if Is_Rewrite_Insertion (N1) then
9596 Next (N1);
9597 elsif Is_Rewrite_Insertion (N2) then
9598 Next (N2);
9599 elsif No (N1) then
9600 return No (N2);
9601 elsif No (N2) then
9602 return False;
9603 elsif not FCE (N1, N2) then
9604 return False;
9605 else
9606 Next (N1);
9607 Next (N2);
9608 end if;
9609 end loop;
9610 end FCL;
9611
9612 ---------
9613 -- FCO --
9614 ---------
9615
9616 function FCO (Op_Node : Node_Id; Call_Node : Node_Id) return Boolean is
9617 Actuals : constant List_Id := Parameter_Associations (Call_Node);
9618 Act : Node_Id;
9619
9620 begin
9621 if No (Actuals)
9622 or else Entity (Op_Node) /= Entity (Name (Call_Node))
9623 then
9624 return False;
9625
9626 else
9627 Act := First (Actuals);
9628
9629 if Nkind (Op_Node) in N_Binary_Op then
9630 if not FCE (Left_Opnd (Op_Node), Act) then
9631 return False;
9632 end if;
9633
9634 Next (Act);
9635 end if;
9636
9637 return Present (Act)
9638 and then FCE (Right_Opnd (Op_Node), Act)
9639 and then No (Next (Act));
9640 end if;
9641 end FCO;
9642
9643 function User_Defined_Numeric_Literal_Mismatch return Boolean;
9644 -- Usually literals with the same value like 12345 and 12_345
9645 -- or 123.0 and 123.00 conform, but not if they are
9646 -- user-defined literals.
9647
9648 -------------------------------------------
9649 -- User_Defined_Numeric_Literal_Mismatch --
9650 -------------------------------------------
9651
9652 function User_Defined_Numeric_Literal_Mismatch return Boolean is
9653 E1_Is_User_Defined : constant Boolean :=
9654 Nkind (Given_E1) not in N_Integer_Literal | N_Real_Literal;
9655 E2_Is_User_Defined : constant Boolean :=
9656 Nkind (Given_E2) not in N_Integer_Literal | N_Real_Literal;
9657
9658 begin
9659 pragma Assert (E1_Is_User_Defined = E2_Is_User_Defined);
9660
9661 return E1_Is_User_Defined and then
9662 not String_Equal (String_From_Numeric_Literal (E1),
9663 String_From_Numeric_Literal (E2));
9664 end User_Defined_Numeric_Literal_Mismatch;
9665
9666 -- Local variables
9667
9668 Result : Boolean;
9669
9670 -- Start of processing for Fully_Conformant_Expressions
9671
9672 begin
9673 Result := True;
9674
9675 -- Nonconformant if paren count does not match. Note: if some idiot
9676 -- complains that we don't do this right for more than 3 levels of
9677 -- parentheses, they will be treated with the respect they deserve.
9678
9679 if Paren_Count (E1) /= Paren_Count (E2) then
9680 return False;
9681
9682 -- If same entities are referenced, then they are conformant even if
9683 -- they have different forms (RM 8.3.1(19-20)).
9684
9685 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
9686 if Present (Entity (E1)) then
9687 Result := Entity (E1) = Entity (E2)
9688
9689 -- One may be a discriminant that has been replaced by the
9690 -- corresponding discriminal.
9691
9692 or else
9693 (Chars (Entity (E1)) = Chars (Entity (E2))
9694 and then Ekind (Entity (E1)) = E_Discriminant
9695 and then Ekind (Entity (E2)) = E_In_Parameter)
9696
9697 -- The discriminant of a protected type is transformed into
9698 -- a local constant and then into a parameter of a protected
9699 -- operation.
9700
9701 or else
9702 (Ekind (Entity (E1)) = E_Constant
9703 and then Ekind (Entity (E2)) = E_In_Parameter
9704 and then Present (Discriminal_Link (Entity (E1)))
9705 and then Discriminal_Link (Entity (E1)) =
9706 Discriminal_Link (Entity (E2)))
9707
9708 -- AI12-050: The loop variables of quantified expressions match
9709 -- if they have the same identifier, even though they may have
9710 -- different entities.
9711
9712 or else
9713 (Chars (Entity (E1)) = Chars (Entity (E2))
9714 and then Ekind (Entity (E1)) = E_Loop_Parameter
9715 and then Ekind (Entity (E2)) = E_Loop_Parameter)
9716
9717 -- A call to an instantiation of Unchecked_Conversion is
9718 -- rewritten with the name of the generated function created for
9719 -- the instance, and this must be special-cased.
9720
9721 or else
9722 (Ekind (Entity (E1)) = E_Function
9723 and then Is_Intrinsic_Subprogram (Entity (E1))
9724 and then Is_Generic_Instance (Entity (E1))
9725 and then Entity (E2) = Alias (Entity (E1)));
9726 if Report and not Result then
9727 Error_Msg_Sloc :=
9728 Text_Ptr'Max (Sloc (Entity (E1)), Sloc (Entity (E2)));
9729 Error_Msg_NE
9730 ("Meaning of& differs because of declaration#", E1, E2);
9731 end if;
9732
9733 return Result;
9734
9735 elsif Nkind (E1) = N_Expanded_Name
9736 and then Nkind (E2) = N_Expanded_Name
9737 and then Nkind (Selector_Name (E1)) = N_Character_Literal
9738 and then Nkind (Selector_Name (E2)) = N_Character_Literal
9739 then
9740 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
9741
9742 else
9743 -- Identifiers in component associations don't always have
9744 -- entities, but their names must conform.
9745
9746 return Nkind (E1) = N_Identifier
9747 and then Nkind (E2) = N_Identifier
9748 and then Chars (E1) = Chars (E2);
9749 end if;
9750
9751 elsif Nkind (E1) = N_Character_Literal
9752 and then Nkind (E2) = N_Expanded_Name
9753 then
9754 return Nkind (Selector_Name (E2)) = N_Character_Literal
9755 and then Chars (E1) = Chars (Selector_Name (E2));
9756
9757 elsif Nkind (E2) = N_Character_Literal
9758 and then Nkind (E1) = N_Expanded_Name
9759 then
9760 return Nkind (Selector_Name (E1)) = N_Character_Literal
9761 and then Chars (E2) = Chars (Selector_Name (E1));
9762
9763 elsif Nkind (E1) in N_Op and then Nkind (E2) = N_Function_Call then
9764 return FCO (E1, E2);
9765
9766 elsif Nkind (E2) in N_Op and then Nkind (E1) = N_Function_Call then
9767 return FCO (E2, E1);
9768
9769 -- Otherwise we must have the same syntactic entity
9770
9771 elsif Nkind (E1) /= Nkind (E2) then
9772 return False;
9773
9774 -- At this point, we specialize by node type
9775
9776 else
9777 case Nkind (E1) is
9778 when N_Aggregate =>
9779 return
9780 FCL (Expressions (E1), Expressions (E2))
9781 and then
9782 FCL (Component_Associations (E1),
9783 Component_Associations (E2));
9784
9785 when N_Allocator =>
9786 if Nkind (Expression (E1)) = N_Qualified_Expression
9787 or else
9788 Nkind (Expression (E2)) = N_Qualified_Expression
9789 then
9790 return FCE (Expression (E1), Expression (E2));
9791
9792 -- Check that the subtype marks and any constraints
9793 -- are conformant
9794
9795 else
9796 declare
9797 Indic1 : constant Node_Id := Expression (E1);
9798 Indic2 : constant Node_Id := Expression (E2);
9799 Elt1 : Node_Id;
9800 Elt2 : Node_Id;
9801
9802 begin
9803 if Nkind (Indic1) /= N_Subtype_Indication then
9804 return
9805 Nkind (Indic2) /= N_Subtype_Indication
9806 and then Entity (Indic1) = Entity (Indic2);
9807
9808 elsif Nkind (Indic2) /= N_Subtype_Indication then
9809 return
9810 Nkind (Indic1) /= N_Subtype_Indication
9811 and then Entity (Indic1) = Entity (Indic2);
9812
9813 else
9814 if Entity (Subtype_Mark (Indic1)) /=
9815 Entity (Subtype_Mark (Indic2))
9816 then
9817 return False;
9818 end if;
9819
9820 Elt1 := First (Constraints (Constraint (Indic1)));
9821 Elt2 := First (Constraints (Constraint (Indic2)));
9822 while Present (Elt1) and then Present (Elt2) loop
9823 if not FCE (Elt1, Elt2) then
9824 return False;
9825 end if;
9826
9827 Next (Elt1);
9828 Next (Elt2);
9829 end loop;
9830
9831 return True;
9832 end if;
9833 end;
9834 end if;
9835
9836 when N_Attribute_Reference =>
9837 return
9838 Attribute_Name (E1) = Attribute_Name (E2)
9839 and then FCL (Expressions (E1), Expressions (E2));
9840
9841 when N_Binary_Op =>
9842 return
9843 Entity (E1) = Entity (E2)
9844 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
9845 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
9846
9847 when N_Membership_Test
9848 | N_Short_Circuit
9849 =>
9850 return
9851 FCE (Left_Opnd (E1), Left_Opnd (E2))
9852 and then
9853 FCE (Right_Opnd (E1), Right_Opnd (E2));
9854
9855 when N_Case_Expression =>
9856 declare
9857 Alt1 : Node_Id;
9858 Alt2 : Node_Id;
9859
9860 begin
9861 if not FCE (Expression (E1), Expression (E2)) then
9862 return False;
9863
9864 else
9865 Alt1 := First (Alternatives (E1));
9866 Alt2 := First (Alternatives (E2));
9867 loop
9868 if Present (Alt1) /= Present (Alt2) then
9869 return False;
9870 elsif No (Alt1) then
9871 return True;
9872 end if;
9873
9874 if not FCE (Expression (Alt1), Expression (Alt2))
9875 or else not FCL (Discrete_Choices (Alt1),
9876 Discrete_Choices (Alt2))
9877 then
9878 return False;
9879 end if;
9880
9881 Next (Alt1);
9882 Next (Alt2);
9883 end loop;
9884 end if;
9885 end;
9886
9887 when N_Character_Literal =>
9888 return
9889 Char_Literal_Value (E1) = Char_Literal_Value (E2);
9890
9891 when N_Component_Association =>
9892 return
9893 FCL (Choices (E1), Choices (E2))
9894 and then
9895 FCE (Expression (E1), Expression (E2));
9896
9897 when N_Explicit_Dereference =>
9898 return
9899 FCE (Prefix (E1), Prefix (E2));
9900
9901 when N_Extension_Aggregate =>
9902 return
9903 FCL (Expressions (E1), Expressions (E2))
9904 and then Null_Record_Present (E1) =
9905 Null_Record_Present (E2)
9906 and then FCL (Component_Associations (E1),
9907 Component_Associations (E2));
9908
9909 when N_Function_Call =>
9910 return
9911 FCE (Name (E1), Name (E2))
9912 and then
9913 FCL (Parameter_Associations (E1),
9914 Parameter_Associations (E2));
9915
9916 when N_If_Expression =>
9917 return
9918 FCL (Expressions (E1), Expressions (E2));
9919
9920 when N_Indexed_Component =>
9921 return
9922 FCE (Prefix (E1), Prefix (E2))
9923 and then
9924 FCL (Expressions (E1), Expressions (E2));
9925
9926 when N_Integer_Literal =>
9927 return (Intval (E1) = Intval (E2))
9928 and then not User_Defined_Numeric_Literal_Mismatch;
9929
9930 when N_Null =>
9931 return True;
9932
9933 when N_Operator_Symbol =>
9934 return
9935 Chars (E1) = Chars (E2);
9936
9937 when N_Others_Choice =>
9938 return True;
9939
9940 when N_Parameter_Association =>
9941 return
9942 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
9943 and then FCE (Explicit_Actual_Parameter (E1),
9944 Explicit_Actual_Parameter (E2));
9945
9946 when N_Qualified_Expression
9947 | N_Type_Conversion
9948 | N_Unchecked_Type_Conversion
9949 =>
9950 return
9951 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
9952 and then
9953 FCE (Expression (E1), Expression (E2));
9954
9955 when N_Quantified_Expression =>
9956 if not FCE (Condition (E1), Condition (E2)) then
9957 return False;
9958 end if;
9959
9960 if Present (Loop_Parameter_Specification (E1))
9961 and then Present (Loop_Parameter_Specification (E2))
9962 then
9963 declare
9964 L1 : constant Node_Id :=
9965 Loop_Parameter_Specification (E1);
9966 L2 : constant Node_Id :=
9967 Loop_Parameter_Specification (E2);
9968
9969 begin
9970 return
9971 Reverse_Present (L1) = Reverse_Present (L2)
9972 and then
9973 FCE (Defining_Identifier (L1),
9974 Defining_Identifier (L2))
9975 and then
9976 FCE (Discrete_Subtype_Definition (L1),
9977 Discrete_Subtype_Definition (L2));
9978 end;
9979
9980 elsif Present (Iterator_Specification (E1))
9981 and then Present (Iterator_Specification (E2))
9982 then
9983 declare
9984 I1 : constant Node_Id := Iterator_Specification (E1);
9985 I2 : constant Node_Id := Iterator_Specification (E2);
9986
9987 begin
9988 return
9989 FCE (Defining_Identifier (I1),
9990 Defining_Identifier (I2))
9991 and then
9992 Of_Present (I1) = Of_Present (I2)
9993 and then
9994 Reverse_Present (I1) = Reverse_Present (I2)
9995 and then FCE (Name (I1), Name (I2))
9996 and then FCE (Subtype_Indication (I1),
9997 Subtype_Indication (I2));
9998 end;
9999
10000 -- The quantified expressions used different specifications to
10001 -- walk their respective ranges.
10002
10003 else
10004 return False;
10005 end if;
10006
10007 when N_Range =>
10008 return
10009 FCE (Low_Bound (E1), Low_Bound (E2))
10010 and then
10011 FCE (High_Bound (E1), High_Bound (E2));
10012
10013 when N_Real_Literal =>
10014 return (Realval (E1) = Realval (E2))
10015 and then not User_Defined_Numeric_Literal_Mismatch;
10016
10017 when N_Selected_Component =>
10018 return
10019 FCE (Prefix (E1), Prefix (E2))
10020 and then
10021 FCE (Selector_Name (E1), Selector_Name (E2));
10022
10023 when N_Slice =>
10024 return
10025 FCE (Prefix (E1), Prefix (E2))
10026 and then
10027 FCE (Discrete_Range (E1), Discrete_Range (E2));
10028
10029 when N_String_Literal =>
10030 declare
10031 S1 : constant String_Id := Strval (E1);
10032 S2 : constant String_Id := Strval (E2);
10033 L1 : constant Nat := String_Length (S1);
10034 L2 : constant Nat := String_Length (S2);
10035
10036 begin
10037 if L1 /= L2 then
10038 return False;
10039
10040 else
10041 for J in 1 .. L1 loop
10042 if Get_String_Char (S1, J) /=
10043 Get_String_Char (S2, J)
10044 then
10045 return False;
10046 end if;
10047 end loop;
10048
10049 return True;
10050 end if;
10051 end;
10052
10053 when N_Unary_Op =>
10054 return
10055 Entity (E1) = Entity (E2)
10056 and then
10057 FCE (Right_Opnd (E1), Right_Opnd (E2));
10058
10059 -- All other node types cannot appear in this context. Strictly
10060 -- we should raise a fatal internal error. Instead we just ignore
10061 -- the nodes. This means that if anyone makes a mistake in the
10062 -- expander and mucks an expression tree irretrievably, the result
10063 -- will be a failure to detect a (probably very obscure) case
10064 -- of non-conformance, which is better than bombing on some
10065 -- case where two expressions do in fact conform.
10066
10067 when others =>
10068 return True;
10069 end case;
10070 end if;
10071 end Fully_Conformant_Expressions;
10072
10073 ----------------------------------------
10074 -- Fully_Conformant_Discrete_Subtypes --
10075 ----------------------------------------
10076
10077 function Fully_Conformant_Discrete_Subtypes
10078 (Given_S1 : Node_Id;
10079 Given_S2 : Node_Id) return Boolean
10080 is
10081 S1 : constant Node_Id := Original_Node (Given_S1);
10082 S2 : constant Node_Id := Original_Node (Given_S2);
10083
10084 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
10085 -- Special-case for a bound given by a discriminant, which in the body
10086 -- is replaced with the discriminal of the enclosing type.
10087
10088 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
10089 -- Check both bounds
10090
10091 -----------------------
10092 -- Conforming_Bounds --
10093 -----------------------
10094
10095 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
10096 begin
10097 if Is_Entity_Name (B1)
10098 and then Is_Entity_Name (B2)
10099 and then Ekind (Entity (B1)) = E_Discriminant
10100 then
10101 return Chars (B1) = Chars (B2);
10102
10103 else
10104 return Fully_Conformant_Expressions (B1, B2);
10105 end if;
10106 end Conforming_Bounds;
10107
10108 -----------------------
10109 -- Conforming_Ranges --
10110 -----------------------
10111
10112 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
10113 begin
10114 return
10115 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
10116 and then
10117 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
10118 end Conforming_Ranges;
10119
10120 -- Start of processing for Fully_Conformant_Discrete_Subtypes
10121
10122 begin
10123 if Nkind (S1) /= Nkind (S2) then
10124 return False;
10125
10126 elsif Is_Entity_Name (S1) then
10127 return Entity (S1) = Entity (S2);
10128
10129 elsif Nkind (S1) = N_Range then
10130 return Conforming_Ranges (S1, S2);
10131
10132 elsif Nkind (S1) = N_Subtype_Indication then
10133 return
10134 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
10135 and then
10136 Conforming_Ranges
10137 (Range_Expression (Constraint (S1)),
10138 Range_Expression (Constraint (S2)));
10139 else
10140 return True;
10141 end if;
10142 end Fully_Conformant_Discrete_Subtypes;
10143
10144 --------------------
10145 -- Install_Entity --
10146 --------------------
10147
10148 procedure Install_Entity (E : Entity_Id) is
10149 Prev : constant Entity_Id := Current_Entity (E);
10150 begin
10151 Set_Is_Immediately_Visible (E);
10152 Set_Current_Entity (E);
10153 Set_Homonym (E, Prev);
10154 end Install_Entity;
10155
10156 ---------------------
10157 -- Install_Formals --
10158 ---------------------
10159
10160 procedure Install_Formals (Id : Entity_Id) is
10161 F : Entity_Id;
10162 begin
10163 F := First_Formal (Id);
10164 while Present (F) loop
10165 Install_Entity (F);
10166 Next_Formal (F);
10167 end loop;
10168 end Install_Formals;
10169
10170 -----------------------------
10171 -- Is_Interface_Conformant --
10172 -----------------------------
10173
10174 function Is_Interface_Conformant
10175 (Tagged_Type : Entity_Id;
10176 Iface_Prim : Entity_Id;
10177 Prim : Entity_Id) return Boolean
10178 is
10179 -- The operation may in fact be an inherited (implicit) operation
10180 -- rather than the original interface primitive, so retrieve the
10181 -- ultimate ancestor.
10182
10183 Iface : constant Entity_Id :=
10184 Find_Dispatching_Type (Ultimate_Alias (Iface_Prim));
10185 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
10186
10187 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
10188 -- Return the controlling formal of Prim
10189
10190 ------------------------
10191 -- Controlling_Formal --
10192 ------------------------
10193
10194 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
10195 E : Entity_Id;
10196
10197 begin
10198 E := First_Entity (Prim);
10199 while Present (E) loop
10200 if Is_Formal (E) and then Is_Controlling_Formal (E) then
10201 return E;
10202 end if;
10203
10204 Next_Entity (E);
10205 end loop;
10206
10207 return Empty;
10208 end Controlling_Formal;
10209
10210 -- Local variables
10211
10212 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
10213 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
10214
10215 -- Start of processing for Is_Interface_Conformant
10216
10217 begin
10218 pragma Assert (Is_Subprogram (Iface_Prim)
10219 and then Is_Subprogram (Prim)
10220 and then Is_Dispatching_Operation (Iface_Prim)
10221 and then Is_Dispatching_Operation (Prim));
10222
10223 pragma Assert (Is_Interface (Iface)
10224 or else (Present (Alias (Iface_Prim))
10225 and then
10226 Is_Interface
10227 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
10228
10229 if Prim = Iface_Prim
10230 or else not Is_Subprogram (Prim)
10231 or else Ekind (Prim) /= Ekind (Iface_Prim)
10232 or else not Is_Dispatching_Operation (Prim)
10233 or else Scope (Prim) /= Scope (Tagged_Type)
10234 or else No (Typ)
10235 or else Base_Type (Typ) /= Base_Type (Tagged_Type)
10236 or else not Primitive_Names_Match (Iface_Prim, Prim)
10237 then
10238 return False;
10239
10240 -- The mode of the controlling formals must match
10241
10242 elsif Present (Iface_Ctrl_F)
10243 and then Present (Prim_Ctrl_F)
10244 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
10245 then
10246 return False;
10247
10248 -- Case of a procedure, or a function whose result type matches the
10249 -- result type of the interface primitive, or a function that has no
10250 -- controlling result (I or access I).
10251
10252 elsif Ekind (Iface_Prim) = E_Procedure
10253 or else Etype (Prim) = Etype (Iface_Prim)
10254 or else not Has_Controlling_Result (Prim)
10255 then
10256 return Type_Conformant
10257 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
10258
10259 -- Case of a function returning an interface, or an access to one. Check
10260 -- that the return types correspond.
10261
10262 elsif Implements_Interface (Typ, Iface) then
10263 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
10264 /=
10265 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
10266 then
10267 return False;
10268 else
10269 return
10270 Type_Conformant (Prim, Ultimate_Alias (Iface_Prim),
10271 Skip_Controlling_Formals => True);
10272 end if;
10273
10274 else
10275 return False;
10276 end if;
10277 end Is_Interface_Conformant;
10278
10279 ---------------------------------
10280 -- Is_Non_Overriding_Operation --
10281 ---------------------------------
10282
10283 function Is_Non_Overriding_Operation
10284 (Prev_E : Entity_Id;
10285 New_E : Entity_Id) return Boolean
10286 is
10287 Formal : Entity_Id;
10288 F_Typ : Entity_Id;
10289 G_Typ : Entity_Id := Empty;
10290
10291 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
10292 -- If F_Type is a derived type associated with a generic actual subtype,
10293 -- then return its Generic_Parent_Type attribute, else return Empty.
10294
10295 function Types_Correspond
10296 (P_Type : Entity_Id;
10297 N_Type : Entity_Id) return Boolean;
10298 -- Returns true if and only if the types (or designated types in the
10299 -- case of anonymous access types) are the same or N_Type is derived
10300 -- directly or indirectly from P_Type.
10301
10302 -----------------------------
10303 -- Get_Generic_Parent_Type --
10304 -----------------------------
10305
10306 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
10307 G_Typ : Entity_Id;
10308 Defn : Node_Id;
10309 Indic : Node_Id;
10310
10311 begin
10312 if Is_Derived_Type (F_Typ)
10313 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
10314 then
10315 -- The tree must be traversed to determine the parent subtype in
10316 -- the generic unit, which unfortunately isn't always available
10317 -- via semantic attributes. ??? (Note: The use of Original_Node
10318 -- is needed for cases where a full derived type has been
10319 -- rewritten.)
10320
10321 -- If the parent type is a scalar type, the derivation creates
10322 -- an anonymous base type for it, and the source type is its
10323 -- first subtype.
10324
10325 if Is_Scalar_Type (F_Typ)
10326 and then not Comes_From_Source (F_Typ)
10327 then
10328 Defn :=
10329 Type_Definition
10330 (Original_Node (Parent (First_Subtype (F_Typ))));
10331 else
10332 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
10333 end if;
10334 if Nkind (Defn) = N_Derived_Type_Definition then
10335 Indic := Subtype_Indication (Defn);
10336
10337 if Nkind (Indic) = N_Subtype_Indication then
10338 G_Typ := Entity (Subtype_Mark (Indic));
10339 else
10340 G_Typ := Entity (Indic);
10341 end if;
10342
10343 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
10344 and then Present (Generic_Parent_Type (Parent (G_Typ)))
10345 then
10346 return Generic_Parent_Type (Parent (G_Typ));
10347 end if;
10348 end if;
10349 end if;
10350
10351 return Empty;
10352 end Get_Generic_Parent_Type;
10353
10354 ----------------------
10355 -- Types_Correspond --
10356 ----------------------
10357
10358 function Types_Correspond
10359 (P_Type : Entity_Id;
10360 N_Type : Entity_Id) return Boolean
10361 is
10362 Prev_Type : Entity_Id := Base_Type (P_Type);
10363 New_Type : Entity_Id := Base_Type (N_Type);
10364
10365 begin
10366 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
10367 Prev_Type := Designated_Type (Prev_Type);
10368 end if;
10369
10370 if Ekind (New_Type) = E_Anonymous_Access_Type then
10371 New_Type := Designated_Type (New_Type);
10372 end if;
10373
10374 if Prev_Type = New_Type then
10375 return True;
10376
10377 elsif not Is_Class_Wide_Type (New_Type) then
10378 while Etype (New_Type) /= New_Type loop
10379 New_Type := Etype (New_Type);
10380
10381 if New_Type = Prev_Type then
10382 return True;
10383 end if;
10384 end loop;
10385 end if;
10386 return False;
10387 end Types_Correspond;
10388
10389 -- Start of processing for Is_Non_Overriding_Operation
10390
10391 begin
10392 -- In the case where both operations are implicit derived subprograms
10393 -- then neither overrides the other. This can only occur in certain
10394 -- obscure cases (e.g., derivation from homographs created in a generic
10395 -- instantiation).
10396
10397 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
10398 return True;
10399
10400 elsif Ekind (Current_Scope) = E_Package
10401 and then Is_Generic_Instance (Current_Scope)
10402 and then In_Private_Part (Current_Scope)
10403 and then Comes_From_Source (New_E)
10404 then
10405 -- We examine the formals and result type of the inherited operation,
10406 -- to determine whether their type is derived from (the instance of)
10407 -- a generic type. The first such formal or result type is the one
10408 -- tested.
10409
10410 Formal := First_Formal (Prev_E);
10411 F_Typ := Empty;
10412 while Present (Formal) loop
10413 F_Typ := Base_Type (Etype (Formal));
10414
10415 if Ekind (F_Typ) = E_Anonymous_Access_Type then
10416 F_Typ := Designated_Type (F_Typ);
10417 end if;
10418
10419 G_Typ := Get_Generic_Parent_Type (F_Typ);
10420 exit when Present (G_Typ);
10421
10422 Next_Formal (Formal);
10423 end loop;
10424
10425 -- If the function dispatches on result check the result type
10426
10427 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
10428 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
10429 end if;
10430
10431 if No (G_Typ) then
10432 return False;
10433 end if;
10434
10435 -- If the generic type is a private type, then the original operation
10436 -- was not overriding in the generic, because there was no primitive
10437 -- operation to override.
10438
10439 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
10440 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
10441 N_Formal_Private_Type_Definition
10442 then
10443 return True;
10444
10445 -- The generic parent type is the ancestor of a formal derived
10446 -- type declaration. We need to check whether it has a primitive
10447 -- operation that should be overridden by New_E in the generic.
10448
10449 else
10450 declare
10451 P_Formal : Entity_Id;
10452 N_Formal : Entity_Id;
10453 P_Typ : Entity_Id;
10454 N_Typ : Entity_Id;
10455 P_Prim : Entity_Id;
10456 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
10457
10458 begin
10459 while Present (Prim_Elt) loop
10460 P_Prim := Node (Prim_Elt);
10461
10462 if Chars (P_Prim) = Chars (New_E)
10463 and then Ekind (P_Prim) = Ekind (New_E)
10464 then
10465 P_Formal := First_Formal (P_Prim);
10466 N_Formal := First_Formal (New_E);
10467 while Present (P_Formal) and then Present (N_Formal) loop
10468 P_Typ := Etype (P_Formal);
10469 N_Typ := Etype (N_Formal);
10470
10471 if not Types_Correspond (P_Typ, N_Typ) then
10472 exit;
10473 end if;
10474
10475 Next_Entity (P_Formal);
10476 Next_Entity (N_Formal);
10477 end loop;
10478
10479 -- Found a matching primitive operation belonging to the
10480 -- formal ancestor type, so the new subprogram is
10481 -- overriding.
10482
10483 if No (P_Formal)
10484 and then No (N_Formal)
10485 and then (Ekind (New_E) /= E_Function
10486 or else
10487 Types_Correspond
10488 (Etype (P_Prim), Etype (New_E)))
10489 then
10490 return False;
10491 end if;
10492 end if;
10493
10494 Next_Elmt (Prim_Elt);
10495 end loop;
10496
10497 -- If no match found, then the new subprogram does not override
10498 -- in the generic (nor in the instance).
10499
10500 -- If the type in question is not abstract, and the subprogram
10501 -- is, this will be an error if the new operation is in the
10502 -- private part of the instance. Emit a warning now, which will
10503 -- make the subsequent error message easier to understand.
10504
10505 if Present (F_Typ) and then not Is_Abstract_Type (F_Typ)
10506 and then Is_Abstract_Subprogram (Prev_E)
10507 and then In_Private_Part (Current_Scope)
10508 then
10509 Error_Msg_Node_2 := F_Typ;
10510 Error_Msg_NE
10511 ("private operation& in generic unit does not override "
10512 & "any primitive operation of& (RM 12.3 (18))??",
10513 New_E, New_E);
10514 end if;
10515
10516 return True;
10517 end;
10518 end if;
10519 else
10520 return False;
10521 end if;
10522 end Is_Non_Overriding_Operation;
10523
10524 -------------------------------------
10525 -- List_Inherited_Pre_Post_Aspects --
10526 -------------------------------------
10527
10528 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
10529 begin
10530 if Opt.List_Inherited_Aspects
10531 and then Is_Subprogram_Or_Generic_Subprogram (E)
10532 then
10533 declare
10534 Subps : constant Subprogram_List := Inherited_Subprograms (E);
10535 Items : Node_Id;
10536 Prag : Node_Id;
10537
10538 begin
10539 for Index in Subps'Range loop
10540 Items := Contract (Subps (Index));
10541
10542 if Present (Items) then
10543 Prag := Pre_Post_Conditions (Items);
10544 while Present (Prag) loop
10545 Error_Msg_Sloc := Sloc (Prag);
10546
10547 if Class_Present (Prag)
10548 and then not Split_PPC (Prag)
10549 then
10550 if Pragma_Name (Prag) = Name_Precondition then
10551 Error_Msg_N
10552 ("info: & inherits `Pre''Class` aspect from "
10553 & "#?L?", E);
10554 else
10555 Error_Msg_N
10556 ("info: & inherits `Post''Class` aspect from "
10557 & "#?L?", E);
10558 end if;
10559 end if;
10560
10561 Prag := Next_Pragma (Prag);
10562 end loop;
10563 end if;
10564 end loop;
10565 end;
10566 end if;
10567 end List_Inherited_Pre_Post_Aspects;
10568
10569 ------------------------------
10570 -- Make_Inequality_Operator --
10571 ------------------------------
10572
10573 -- S is the defining identifier of an equality operator. We build a
10574 -- subprogram declaration with the right signature. This operation is
10575 -- intrinsic, because it is always expanded as the negation of the
10576 -- call to the equality function.
10577
10578 procedure Make_Inequality_Operator (S : Entity_Id) is
10579 Loc : constant Source_Ptr := Sloc (S);
10580 Decl : Node_Id;
10581 Formals : List_Id;
10582 Op_Name : Entity_Id;
10583
10584 FF : constant Entity_Id := First_Formal (S);
10585 NF : constant Entity_Id := Next_Formal (FF);
10586
10587 begin
10588 -- Check that equality was properly defined, ignore call if not
10589
10590 if No (NF) then
10591 return;
10592 end if;
10593
10594 declare
10595 A : constant Entity_Id :=
10596 Make_Defining_Identifier (Sloc (FF),
10597 Chars => Chars (FF));
10598
10599 B : constant Entity_Id :=
10600 Make_Defining_Identifier (Sloc (NF),
10601 Chars => Chars (NF));
10602
10603 begin
10604 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
10605
10606 Formals := New_List (
10607 Make_Parameter_Specification (Loc,
10608 Defining_Identifier => A,
10609 Parameter_Type =>
10610 New_Occurrence_Of (Etype (First_Formal (S)),
10611 Sloc (Etype (First_Formal (S))))),
10612
10613 Make_Parameter_Specification (Loc,
10614 Defining_Identifier => B,
10615 Parameter_Type =>
10616 New_Occurrence_Of (Etype (Next_Formal (First_Formal (S))),
10617 Sloc (Etype (Next_Formal (First_Formal (S)))))));
10618
10619 Decl :=
10620 Make_Subprogram_Declaration (Loc,
10621 Specification =>
10622 Make_Function_Specification (Loc,
10623 Defining_Unit_Name => Op_Name,
10624 Parameter_Specifications => Formals,
10625 Result_Definition =>
10626 New_Occurrence_Of (Standard_Boolean, Loc)));
10627
10628 -- Insert inequality right after equality if it is explicit or after
10629 -- the derived type when implicit. These entities are created only
10630 -- for visibility purposes, and eventually replaced in the course
10631 -- of expansion, so they do not need to be attached to the tree and
10632 -- seen by the back-end. Keeping them internal also avoids spurious
10633 -- freezing problems. The declaration is inserted in the tree for
10634 -- analysis, and removed afterwards. If the equality operator comes
10635 -- from an explicit declaration, attach the inequality immediately
10636 -- after. Else the equality is inherited from a derived type
10637 -- declaration, so insert inequality after that declaration.
10638
10639 if No (Alias (S)) then
10640 Insert_After (Unit_Declaration_Node (S), Decl);
10641 elsif Is_List_Member (Parent (S)) then
10642 Insert_After (Parent (S), Decl);
10643 else
10644 Insert_After (Parent (Etype (First_Formal (S))), Decl);
10645 end if;
10646
10647 Mark_Rewrite_Insertion (Decl);
10648 Set_Is_Intrinsic_Subprogram (Op_Name);
10649 Analyze (Decl);
10650 Remove (Decl);
10651 Set_Has_Completion (Op_Name);
10652 Set_Corresponding_Equality (Op_Name, S);
10653 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
10654 end;
10655 end Make_Inequality_Operator;
10656
10657 ----------------------
10658 -- May_Need_Actuals --
10659 ----------------------
10660
10661 procedure May_Need_Actuals (Fun : Entity_Id) is
10662 F : Entity_Id;
10663 B : Boolean;
10664
10665 begin
10666 F := First_Formal (Fun);
10667 B := True;
10668 while Present (F) loop
10669 if No (Default_Value (F)) then
10670 B := False;
10671 exit;
10672 end if;
10673
10674 Next_Formal (F);
10675 end loop;
10676
10677 Set_Needs_No_Actuals (Fun, B);
10678 end May_Need_Actuals;
10679
10680 ---------------------
10681 -- Mode_Conformant --
10682 ---------------------
10683
10684 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
10685 Result : Boolean;
10686 begin
10687 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
10688 return Result;
10689 end Mode_Conformant;
10690
10691 ---------------------------
10692 -- New_Overloaded_Entity --
10693 ---------------------------
10694
10695 procedure New_Overloaded_Entity
10696 (S : Entity_Id;
10697 Derived_Type : Entity_Id := Empty)
10698 is
10699 Overridden_Subp : Entity_Id := Empty;
10700 -- Set if the current scope has an operation that is type-conformant
10701 -- with S, and becomes hidden by S.
10702
10703 Is_Primitive_Subp : Boolean;
10704 -- Set to True if the new subprogram is primitive
10705
10706 E : Entity_Id;
10707 -- Entity that S overrides
10708
10709 procedure Check_For_Primitive_Subprogram
10710 (Is_Primitive : out Boolean;
10711 Is_Overriding : Boolean := False);
10712 -- If the subprogram being analyzed is a primitive operation of the type
10713 -- of a formal or result, set the Has_Primitive_Operations flag on the
10714 -- type, and set Is_Primitive to True (otherwise set to False). Set the
10715 -- corresponding flag on the entity itself for later use.
10716
10717 function Has_Matching_Entry_Or_Subprogram (E : Entity_Id) return Boolean;
10718 -- True if a) E is a subprogram whose first formal is a concurrent type
10719 -- defined in the scope of E that has some entry or subprogram whose
10720 -- profile matches E, or b) E is an internally built dispatching
10721 -- subprogram of a protected type and there is a matching subprogram
10722 -- defined in the enclosing scope of the protected type, or c) E is
10723 -- an entry of a synchronized type and a matching procedure has been
10724 -- previously defined in the enclosing scope of the synchronized type.
10725
10726 function Is_Private_Declaration (E : Entity_Id) return Boolean;
10727 -- Check that E is declared in the private part of the current package,
10728 -- or in the package body, where it may hide a previous declaration.
10729 -- We can't use In_Private_Part by itself because this flag is also
10730 -- set when freezing entities, so we must examine the place of the
10731 -- declaration in the tree, and recognize wrapper packages as well.
10732
10733 function Is_Overriding_Alias
10734 (Old_E : Entity_Id;
10735 New_E : Entity_Id) return Boolean;
10736 -- Check whether new subprogram and old subprogram are both inherited
10737 -- from subprograms that have distinct dispatch table entries. This can
10738 -- occur with derivations from instances with accidental homonyms. The
10739 -- function is conservative given that the converse is only true within
10740 -- instances that contain accidental overloadings.
10741
10742 procedure Report_Conflict (S : Entity_Id; E : Entity_Id);
10743 -- Report conflict between entities S and E
10744
10745 ------------------------------------
10746 -- Check_For_Primitive_Subprogram --
10747 ------------------------------------
10748
10749 procedure Check_For_Primitive_Subprogram
10750 (Is_Primitive : out Boolean;
10751 Is_Overriding : Boolean := False)
10752 is
10753 Formal : Entity_Id;
10754 F_Typ : Entity_Id;
10755 B_Typ : Entity_Id;
10756
10757 function Visible_Part_Type (T : Entity_Id) return Boolean;
10758 -- Returns true if T is declared in the visible part of the current
10759 -- package scope; otherwise returns false. Assumes that T is declared
10760 -- in a package.
10761
10762 procedure Check_Private_Overriding (T : Entity_Id);
10763 -- Checks that if a primitive abstract subprogram of a visible
10764 -- abstract type is declared in a private part, then it must override
10765 -- an abstract subprogram declared in the visible part. Also checks
10766 -- that if a primitive function with a controlling result is declared
10767 -- in a private part, then it must override a function declared in
10768 -- the visible part.
10769
10770 ------------------------------
10771 -- Check_Private_Overriding --
10772 ------------------------------
10773
10774 procedure Check_Private_Overriding (T : Entity_Id) is
10775 function Overrides_Private_Part_Op return Boolean;
10776 -- This detects the special case where the overriding subprogram
10777 -- is overriding a subprogram that was declared in the same
10778 -- private part. That case is illegal by 3.9.3(10).
10779
10780 function Overrides_Visible_Function
10781 (Partial_View : Entity_Id) return Boolean;
10782 -- True if S overrides a function in the visible part. The
10783 -- overridden function could be explicitly or implicitly declared.
10784
10785 -------------------------------
10786 -- Overrides_Private_Part_Op --
10787 -------------------------------
10788
10789 function Overrides_Private_Part_Op return Boolean is
10790 Over_Decl : constant Node_Id :=
10791 Unit_Declaration_Node (Overridden_Operation (S));
10792 Subp_Decl : constant Node_Id := Unit_Declaration_Node (S);
10793
10794 begin
10795 pragma Assert (Is_Overriding);
10796 pragma Assert
10797 (Nkind (Over_Decl) = N_Abstract_Subprogram_Declaration);
10798 pragma Assert
10799 (Nkind (Subp_Decl) = N_Abstract_Subprogram_Declaration);
10800
10801 return In_Same_List (Over_Decl, Subp_Decl);
10802 end Overrides_Private_Part_Op;
10803
10804 --------------------------------
10805 -- Overrides_Visible_Function --
10806 --------------------------------
10807
10808 function Overrides_Visible_Function
10809 (Partial_View : Entity_Id) return Boolean
10810 is
10811 begin
10812 if not Is_Overriding or else not Has_Homonym (S) then
10813 return False;
10814 end if;
10815
10816 if not Present (Partial_View) then
10817 return True;
10818 end if;
10819
10820 -- Search through all the homonyms H of S in the current
10821 -- package spec, and return True if we find one that matches.
10822 -- Note that Parent (H) will be the declaration of the
10823 -- partial view of T for a match.
10824
10825 declare
10826 H : Entity_Id := S;
10827 begin
10828 loop
10829 H := Homonym (H);
10830 exit when not Present (H) or else Scope (H) /= Scope (S);
10831
10832 if Nkind (Parent (H)) in
10833 N_Private_Extension_Declaration |
10834 N_Private_Type_Declaration
10835 and then Defining_Identifier (Parent (H)) = Partial_View
10836 then
10837 return True;
10838 end if;
10839 end loop;
10840 end;
10841
10842 return False;
10843 end Overrides_Visible_Function;
10844
10845 -- Start of processing for Check_Private_Overriding
10846
10847 begin
10848 if Is_Package_Or_Generic_Package (Current_Scope)
10849 and then In_Private_Part (Current_Scope)
10850 and then Visible_Part_Type (T)
10851 and then not In_Instance
10852 then
10853 if Is_Abstract_Type (T)
10854 and then Is_Abstract_Subprogram (S)
10855 and then (not Is_Overriding
10856 or else not Is_Abstract_Subprogram (E)
10857 or else Overrides_Private_Part_Op)
10858 then
10859 Error_Msg_N
10860 ("abstract subprograms must be visible (RM 3.9.3(10))!",
10861 S);
10862
10863 elsif Ekind (S) = E_Function then
10864 declare
10865 Partial_View : constant Entity_Id :=
10866 Incomplete_Or_Partial_View (T);
10867
10868 begin
10869 if not Overrides_Visible_Function (Partial_View) then
10870
10871 -- Here, S is "function ... return T;" declared in
10872 -- the private part, not overriding some visible
10873 -- operation. That's illegal in the tagged case
10874 -- (but not if the private type is untagged).
10875
10876 if ((Present (Partial_View)
10877 and then Is_Tagged_Type (Partial_View))
10878 or else (not Present (Partial_View)
10879 and then Is_Tagged_Type (T)))
10880 and then T = Base_Type (Etype (S))
10881 then
10882 Error_Msg_N
10883 ("private function with tagged result must"
10884 & " override visible-part function", S);
10885 Error_Msg_N
10886 ("\move subprogram to the visible part"
10887 & " (RM 3.9.3(10))", S);
10888
10889 -- Ada 2012 (AI05-0073): Extend this check to the case
10890 -- of a function whose result subtype is defined by an
10891 -- access_definition designating specific tagged type.
10892
10893 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
10894 and then Is_Tagged_Type (Designated_Type (Etype (S)))
10895 and then
10896 not Is_Class_Wide_Type
10897 (Designated_Type (Etype (S)))
10898 and then Ada_Version >= Ada_2012
10899 then
10900 Error_Msg_N
10901 ("private function with controlling access "
10902 & "result must override visible-part function",
10903 S);
10904 Error_Msg_N
10905 ("\move subprogram to the visible part"
10906 & " (RM 3.9.3(10))", S);
10907 end if;
10908 end if;
10909 end;
10910 end if;
10911 end if;
10912 end Check_Private_Overriding;
10913
10914 -----------------------
10915 -- Visible_Part_Type --
10916 -----------------------
10917
10918 function Visible_Part_Type (T : Entity_Id) return Boolean is
10919 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
10920
10921 begin
10922 -- If the entity is a private type, then it must be declared in a
10923 -- visible part.
10924
10925 if Ekind (T) in Private_Kind then
10926 return True;
10927
10928 elsif Is_Type (T) and then Has_Private_Declaration (T) then
10929 return True;
10930
10931 elsif Is_List_Member (Declaration_Node (T))
10932 and then List_Containing (Declaration_Node (T)) =
10933 Visible_Declarations (Specification (P))
10934 then
10935 return True;
10936
10937 else
10938 return False;
10939 end if;
10940 end Visible_Part_Type;
10941
10942 -- Start of processing for Check_For_Primitive_Subprogram
10943
10944 begin
10945 Is_Primitive := False;
10946
10947 if not Comes_From_Source (S) then
10948 null;
10949
10950 -- If subprogram is at library level, it is not primitive operation
10951
10952 elsif Current_Scope = Standard_Standard then
10953 null;
10954
10955 elsif (Is_Package_Or_Generic_Package (Current_Scope)
10956 and then not In_Package_Body (Current_Scope))
10957 or else Is_Overriding
10958 then
10959 -- For function, check return type
10960
10961 if Ekind (S) = E_Function then
10962 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
10963 F_Typ := Designated_Type (Etype (S));
10964 else
10965 F_Typ := Etype (S);
10966 end if;
10967
10968 B_Typ := Base_Type (F_Typ);
10969
10970 if Scope (B_Typ) = Current_Scope
10971 and then not Is_Class_Wide_Type (B_Typ)
10972 and then not Is_Generic_Type (B_Typ)
10973 then
10974 Is_Primitive := True;
10975 Set_Has_Primitive_Operations (B_Typ);
10976 Set_Is_Primitive (S);
10977 Check_Private_Overriding (B_Typ);
10978
10979 -- The Ghost policy in effect at the point of declaration
10980 -- or a tagged type and a primitive operation must match
10981 -- (SPARK RM 6.9(16)).
10982
10983 Check_Ghost_Primitive (S, B_Typ);
10984 end if;
10985 end if;
10986
10987 -- For all subprograms, check formals
10988
10989 Formal := First_Formal (S);
10990 while Present (Formal) loop
10991 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
10992 F_Typ := Designated_Type (Etype (Formal));
10993 else
10994 F_Typ := Etype (Formal);
10995 end if;
10996
10997 B_Typ := Base_Type (F_Typ);
10998
10999 if Ekind (B_Typ) = E_Access_Subtype then
11000 B_Typ := Base_Type (B_Typ);
11001 end if;
11002
11003 if Scope (B_Typ) = Current_Scope
11004 and then not Is_Class_Wide_Type (B_Typ)
11005 and then not Is_Generic_Type (B_Typ)
11006 then
11007 Is_Primitive := True;
11008 Set_Is_Primitive (S);
11009 Set_Has_Primitive_Operations (B_Typ);
11010 Check_Private_Overriding (B_Typ);
11011
11012 -- The Ghost policy in effect at the point of declaration
11013 -- of a tagged type and a primitive operation must match
11014 -- (SPARK RM 6.9(16)).
11015
11016 Check_Ghost_Primitive (S, B_Typ);
11017 end if;
11018
11019 Next_Formal (Formal);
11020 end loop;
11021
11022 -- Special case: An equality function can be redefined for a type
11023 -- occurring in a declarative part, and won't otherwise be treated as
11024 -- a primitive because it doesn't occur in a package spec and doesn't
11025 -- override an inherited subprogram. It's important that we mark it
11026 -- primitive so it can be returned by Collect_Primitive_Operations
11027 -- and be used in composing the equality operation of later types
11028 -- that have a component of the type.
11029
11030 elsif Chars (S) = Name_Op_Eq
11031 and then Etype (S) = Standard_Boolean
11032 then
11033 B_Typ := Base_Type (Etype (First_Formal (S)));
11034
11035 if Scope (B_Typ) = Current_Scope
11036 and then
11037 Base_Type (Etype (Next_Formal (First_Formal (S)))) = B_Typ
11038 and then not Is_Limited_Type (B_Typ)
11039 then
11040 Is_Primitive := True;
11041 Set_Is_Primitive (S);
11042 Set_Has_Primitive_Operations (B_Typ);
11043 Check_Private_Overriding (B_Typ);
11044
11045 -- The Ghost policy in effect at the point of declaration of a
11046 -- tagged type and a primitive operation must match
11047 -- (SPARK RM 6.9(16)).
11048
11049 Check_Ghost_Primitive (S, B_Typ);
11050 end if;
11051 end if;
11052 end Check_For_Primitive_Subprogram;
11053
11054 --------------------------------------
11055 -- Has_Matching_Entry_Or_Subprogram --
11056 --------------------------------------
11057
11058 function Has_Matching_Entry_Or_Subprogram
11059 (E : Entity_Id) return Boolean
11060 is
11061 function Check_Conforming_Parameters
11062 (E1_Param : Node_Id;
11063 E2_Param : Node_Id;
11064 Ctype : Conformance_Type) return Boolean;
11065 -- Starting from the given parameters, check that all the parameters
11066 -- of two entries or subprograms are conformant. Used to skip
11067 -- the check on the controlling argument.
11068
11069 function Matching_Entry_Or_Subprogram
11070 (Conc_Typ : Entity_Id;
11071 Subp : Entity_Id) return Entity_Id;
11072 -- Return the first entry or subprogram of the given concurrent type
11073 -- whose name matches the name of Subp and has a profile conformant
11074 -- with Subp; return Empty if not found.
11075
11076 function Matching_Dispatching_Subprogram
11077 (Conc_Typ : Entity_Id;
11078 Ent : Entity_Id) return Entity_Id;
11079 -- Return the first dispatching primitive of Conc_Type defined in the
11080 -- enclosing scope of Conc_Type (i.e. before the full definition of
11081 -- this concurrent type) whose name matches the entry Ent and has a
11082 -- profile conformant with the profile of the corresponding (not yet
11083 -- built) dispatching primitive of Ent; return Empty if not found.
11084
11085 function Matching_Original_Protected_Subprogram
11086 (Prot_Typ : Entity_Id;
11087 Subp : Entity_Id) return Entity_Id;
11088 -- Return the first subprogram defined in the enclosing scope of
11089 -- Prot_Typ (before the full definition of this protected type)
11090 -- whose name matches the original name of Subp and has a profile
11091 -- conformant with the profile of Subp; return Empty if not found.
11092
11093 function Normalized_First_Parameter_Type
11094 (E : Entity_Id) return Entity_Id;
11095 -- Return the type of the first parameter unless that type
11096 -- is an anonymous access type, in which case return the
11097 -- designated type. Used to treat anonymous-access-to-synchronized
11098 -- the same as synchronized for purposes of checking for
11099 -- prefixed view profile conflicts.
11100
11101 ---------------------------------
11102 -- Check_Conforming_Parameters --
11103 ---------------------------------
11104
11105 function Check_Conforming_Parameters
11106 (E1_Param : Node_Id;
11107 E2_Param : Node_Id;
11108 Ctype : Conformance_Type) return Boolean
11109 is
11110 Param_E1 : Node_Id := E1_Param;
11111 Param_E2 : Node_Id := E2_Param;
11112
11113 begin
11114 while Present (Param_E1) and then Present (Param_E2) loop
11115 if (Ctype >= Mode_Conformant) and then
11116 Ekind (Defining_Identifier (Param_E1)) /=
11117 Ekind (Defining_Identifier (Param_E2))
11118 then
11119 return False;
11120 elsif not
11121 Conforming_Types
11122 (Find_Parameter_Type (Param_E1),
11123 Find_Parameter_Type (Param_E2),
11124 Ctype)
11125 then
11126 return False;
11127 end if;
11128
11129 Next (Param_E1);
11130 Next (Param_E2);
11131 end loop;
11132
11133 -- The candidate is not valid if one of the two lists contains
11134 -- more parameters than the other
11135
11136 return No (Param_E1) and then No (Param_E2);
11137 end Check_Conforming_Parameters;
11138
11139 ----------------------------------
11140 -- Matching_Entry_Or_Subprogram --
11141 ----------------------------------
11142
11143 function Matching_Entry_Or_Subprogram
11144 (Conc_Typ : Entity_Id;
11145 Subp : Entity_Id) return Entity_Id
11146 is
11147 E : Entity_Id;
11148
11149 begin
11150 E := First_Entity (Conc_Typ);
11151 while Present (E) loop
11152 if Chars (Subp) = Chars (E)
11153 and then (Ekind (E) = E_Entry or else Is_Subprogram (E))
11154 and then
11155 Check_Conforming_Parameters
11156 (First (Parameter_Specifications (Parent (E))),
11157 Next (First (Parameter_Specifications (Parent (Subp)))),
11158 Type_Conformant)
11159 then
11160 return E;
11161 end if;
11162
11163 Next_Entity (E);
11164 end loop;
11165
11166 return Empty;
11167 end Matching_Entry_Or_Subprogram;
11168
11169 -------------------------------------
11170 -- Matching_Dispatching_Subprogram --
11171 -------------------------------------
11172
11173 function Matching_Dispatching_Subprogram
11174 (Conc_Typ : Entity_Id;
11175 Ent : Entity_Id) return Entity_Id
11176 is
11177 E : Entity_Id;
11178
11179 begin
11180 -- Search for entities in the enclosing scope of this synchonized
11181 -- type.
11182
11183 pragma Assert (Is_Concurrent_Type (Conc_Typ));
11184 Push_Scope (Scope (Conc_Typ));
11185 E := Current_Entity_In_Scope (Ent);
11186 Pop_Scope;
11187
11188 while Present (E) loop
11189 if Scope (E) = Scope (Conc_Typ)
11190 and then Comes_From_Source (E)
11191 and then Ekind (E) = E_Procedure
11192 and then Present (First_Entity (E))
11193 and then Is_Controlling_Formal (First_Entity (E))
11194 and then Etype (First_Entity (E)) = Conc_Typ
11195 and then
11196 Check_Conforming_Parameters
11197 (First (Parameter_Specifications (Parent (Ent))),
11198 Next (First (Parameter_Specifications (Parent (E)))),
11199 Subtype_Conformant)
11200 then
11201 return E;
11202 end if;
11203
11204 E := Homonym (E);
11205 end loop;
11206
11207 return Empty;
11208 end Matching_Dispatching_Subprogram;
11209
11210 --------------------------------------------
11211 -- Matching_Original_Protected_Subprogram --
11212 --------------------------------------------
11213
11214 function Matching_Original_Protected_Subprogram
11215 (Prot_Typ : Entity_Id;
11216 Subp : Entity_Id) return Entity_Id
11217 is
11218 ICF : constant Boolean :=
11219 Is_Controlling_Formal (First_Entity (Subp));
11220 E : Entity_Id;
11221
11222 begin
11223 -- Temporarily decorate the first parameter of Subp as controlling
11224 -- formal, required to invoke Subtype_Conformant.
11225
11226 Set_Is_Controlling_Formal (First_Entity (Subp));
11227
11228 E :=
11229 Current_Entity_In_Scope (Original_Protected_Subprogram (Subp));
11230
11231 while Present (E) loop
11232 if Scope (E) = Scope (Prot_Typ)
11233 and then Comes_From_Source (E)
11234 and then Ekind (Subp) = Ekind (E)
11235 and then Present (First_Entity (E))
11236 and then Is_Controlling_Formal (First_Entity (E))
11237 and then Etype (First_Entity (E)) = Prot_Typ
11238 and then Subtype_Conformant (Subp, E,
11239 Skip_Controlling_Formals => True)
11240 then
11241 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11242 return E;
11243 end if;
11244
11245 E := Homonym (E);
11246 end loop;
11247
11248 Set_Is_Controlling_Formal (First_Entity (Subp), ICF);
11249
11250 return Empty;
11251 end Matching_Original_Protected_Subprogram;
11252
11253 -------------------------------------
11254 -- Normalized_First_Parameter_Type --
11255 -------------------------------------
11256
11257 function Normalized_First_Parameter_Type
11258 (E : Entity_Id) return Entity_Id
11259 is
11260 Result : Entity_Id := Etype (First_Entity (E));
11261 begin
11262 if Ekind (Result) = E_Anonymous_Access_Type then
11263 Result := Designated_Type (Result);
11264 end if;
11265 return Result;
11266 end Normalized_First_Parameter_Type;
11267
11268 -- Start of processing for Has_Matching_Entry_Or_Subprogram
11269
11270 begin
11271 -- Case 1: E is a subprogram whose first formal is a concurrent type
11272 -- defined in the scope of E that has an entry or subprogram whose
11273 -- profile matches E.
11274
11275 if Comes_From_Source (E)
11276 and then Is_Subprogram (E)
11277 and then Present (First_Entity (E))
11278 and then Is_Concurrent_Record_Type
11279 (Normalized_First_Parameter_Type (E))
11280 then
11281 if Scope (E) =
11282 Scope (Corresponding_Concurrent_Type
11283 (Normalized_First_Parameter_Type (E)))
11284 and then
11285 Present
11286 (Matching_Entry_Or_Subprogram
11287 (Corresponding_Concurrent_Type
11288 (Normalized_First_Parameter_Type (E)),
11289 Subp => E))
11290 then
11291 Report_Conflict (E,
11292 Matching_Entry_Or_Subprogram
11293 (Corresponding_Concurrent_Type
11294 (Normalized_First_Parameter_Type (E)),
11295 Subp => E));
11296 return True;
11297 end if;
11298
11299 -- Case 2: E is an internally built dispatching subprogram of a
11300 -- protected type and there is a subprogram defined in the enclosing
11301 -- scope of the protected type that has the original name of E and
11302 -- its profile is conformant with the profile of E. We check the
11303 -- name of the original protected subprogram associated with E since
11304 -- the expander builds dispatching primitives of protected functions
11305 -- and procedures with other names (see Exp_Ch9.Build_Selected_Name).
11306
11307 elsif not Comes_From_Source (E)
11308 and then Is_Subprogram (E)
11309 and then Present (First_Entity (E))
11310 and then Is_Concurrent_Record_Type (Etype (First_Entity (E)))
11311 and then Present (Original_Protected_Subprogram (E))
11312 and then
11313 Present
11314 (Matching_Original_Protected_Subprogram
11315 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11316 Subp => E))
11317 then
11318 Report_Conflict (E,
11319 Matching_Original_Protected_Subprogram
11320 (Corresponding_Concurrent_Type (Etype (First_Entity (E))),
11321 Subp => E));
11322 return True;
11323
11324 -- Case 3: E is an entry of a synchronized type and a matching
11325 -- procedure has been previously defined in the enclosing scope
11326 -- of the synchronized type.
11327
11328 elsif Comes_From_Source (E)
11329 and then Ekind (E) = E_Entry
11330 and then
11331 Present (Matching_Dispatching_Subprogram (Current_Scope, E))
11332 then
11333 Report_Conflict (E,
11334 Matching_Dispatching_Subprogram (Current_Scope, E));
11335 return True;
11336 end if;
11337
11338 return False;
11339 end Has_Matching_Entry_Or_Subprogram;
11340
11341 ----------------------------
11342 -- Is_Private_Declaration --
11343 ----------------------------
11344
11345 function Is_Private_Declaration (E : Entity_Id) return Boolean is
11346 Decl : constant Node_Id := Unit_Declaration_Node (E);
11347 Priv_Decls : List_Id;
11348
11349 begin
11350 if Is_Package_Or_Generic_Package (Current_Scope)
11351 and then In_Private_Part (Current_Scope)
11352 then
11353 Priv_Decls :=
11354 Private_Declarations (Package_Specification (Current_Scope));
11355
11356 return In_Package_Body (Current_Scope)
11357 or else
11358 (Is_List_Member (Decl)
11359 and then List_Containing (Decl) = Priv_Decls)
11360 or else (Nkind (Parent (Decl)) = N_Package_Specification
11361 and then not
11362 Is_Compilation_Unit
11363 (Defining_Entity (Parent (Decl)))
11364 and then List_Containing (Parent (Parent (Decl))) =
11365 Priv_Decls);
11366 else
11367 return False;
11368 end if;
11369 end Is_Private_Declaration;
11370
11371 --------------------------
11372 -- Is_Overriding_Alias --
11373 --------------------------
11374
11375 function Is_Overriding_Alias
11376 (Old_E : Entity_Id;
11377 New_E : Entity_Id) return Boolean
11378 is
11379 AO : constant Entity_Id := Alias (Old_E);
11380 AN : constant Entity_Id := Alias (New_E);
11381
11382 begin
11383 return Scope (AO) /= Scope (AN)
11384 or else No (DTC_Entity (AO))
11385 or else No (DTC_Entity (AN))
11386 or else DT_Position (AO) = DT_Position (AN);
11387 end Is_Overriding_Alias;
11388
11389 ---------------------
11390 -- Report_Conflict --
11391 ---------------------
11392
11393 procedure Report_Conflict (S : Entity_Id; E : Entity_Id) is
11394 begin
11395 Error_Msg_Sloc := Sloc (E);
11396
11397 -- Generate message, with useful additional warning if in generic
11398
11399 if Is_Generic_Unit (E) then
11400 Error_Msg_N ("previous generic unit cannot be overloaded", S);
11401 Error_Msg_N ("\& conflicts with declaration#", S);
11402 else
11403 Error_Msg_N ("& conflicts with declaration#", S);
11404 end if;
11405 end Report_Conflict;
11406
11407 -- Start of processing for New_Overloaded_Entity
11408
11409 begin
11410 -- We need to look for an entity that S may override. This must be a
11411 -- homonym in the current scope, so we look for the first homonym of
11412 -- S in the current scope as the starting point for the search.
11413
11414 E := Current_Entity_In_Scope (S);
11415
11416 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
11417 -- They are directly added to the list of primitive operations of
11418 -- Derived_Type, unless this is a rederivation in the private part
11419 -- of an operation that was already derived in the visible part of
11420 -- the current package.
11421
11422 if Ada_Version >= Ada_2005
11423 and then Present (Derived_Type)
11424 and then Present (Alias (S))
11425 and then Is_Dispatching_Operation (Alias (S))
11426 and then Present (Find_Dispatching_Type (Alias (S)))
11427 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
11428 then
11429 -- For private types, when the full-view is processed we propagate to
11430 -- the full view the non-overridden entities whose attribute "alias"
11431 -- references an interface primitive. These entities were added by
11432 -- Derive_Subprograms to ensure that interface primitives are
11433 -- covered.
11434
11435 -- Inside_Freeze_Actions is non zero when S corresponds with an
11436 -- internal entity that links an interface primitive with its
11437 -- covering primitive through attribute Interface_Alias (see
11438 -- Add_Internal_Interface_Entities).
11439
11440 if Inside_Freezing_Actions = 0
11441 and then Is_Package_Or_Generic_Package (Current_Scope)
11442 and then In_Private_Part (Current_Scope)
11443 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
11444 and then Nkind (Parent (S)) = N_Full_Type_Declaration
11445 and then Full_View (Defining_Identifier (Parent (E)))
11446 = Defining_Identifier (Parent (S))
11447 and then Alias (E) = Alias (S)
11448 then
11449 Check_Operation_From_Private_View (S, E);
11450 Set_Is_Dispatching_Operation (S);
11451
11452 -- Common case
11453
11454 else
11455 Enter_Overloaded_Entity (S);
11456 Check_Dispatching_Operation (S, Empty);
11457 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11458 end if;
11459
11460 return;
11461 end if;
11462
11463 -- For synchronized types check conflicts of this entity with previously
11464 -- defined entities.
11465
11466 if Ada_Version >= Ada_2005
11467 and then Has_Matching_Entry_Or_Subprogram (S)
11468 then
11469 return;
11470 end if;
11471
11472 -- If there is no homonym then this is definitely not overriding
11473
11474 if No (E) then
11475 Enter_Overloaded_Entity (S);
11476 Check_Dispatching_Operation (S, Empty);
11477 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11478
11479 -- If subprogram has an explicit declaration, check whether it has an
11480 -- overriding indicator.
11481
11482 if Comes_From_Source (S) then
11483 Check_Synchronized_Overriding (S, Overridden_Subp);
11484
11485 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
11486 -- it may have overridden some hidden inherited primitive. Update
11487 -- Overridden_Subp to avoid spurious errors when checking the
11488 -- overriding indicator.
11489
11490 if Ada_Version >= Ada_2012
11491 and then No (Overridden_Subp)
11492 and then Is_Dispatching_Operation (S)
11493 and then Present (Overridden_Operation (S))
11494 then
11495 Overridden_Subp := Overridden_Operation (S);
11496 end if;
11497
11498 Check_Overriding_Indicator
11499 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11500
11501 -- The Ghost policy in effect at the point of declaration of a
11502 -- parent subprogram and an overriding subprogram must match
11503 -- (SPARK RM 6.9(17)).
11504
11505 Check_Ghost_Overriding (S, Overridden_Subp);
11506 end if;
11507
11508 -- If there is a homonym that is not overloadable, then we have an
11509 -- error, except for the special cases checked explicitly below.
11510
11511 elsif not Is_Overloadable (E) then
11512
11513 -- Check for spurious conflict produced by a subprogram that has the
11514 -- same name as that of the enclosing generic package. The conflict
11515 -- occurs within an instance, between the subprogram and the renaming
11516 -- declaration for the package. After the subprogram, the package
11517 -- renaming declaration becomes hidden.
11518
11519 if Ekind (E) = E_Package
11520 and then Present (Renamed_Object (E))
11521 and then Renamed_Object (E) = Current_Scope
11522 and then Nkind (Parent (Renamed_Object (E))) =
11523 N_Package_Specification
11524 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
11525 then
11526 Set_Is_Hidden (E);
11527 Set_Is_Immediately_Visible (E, False);
11528 Enter_Overloaded_Entity (S);
11529 Set_Homonym (S, Homonym (E));
11530 Check_Dispatching_Operation (S, Empty);
11531 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
11532
11533 -- If the subprogram is implicit it is hidden by the previous
11534 -- declaration. However if it is dispatching, it must appear in the
11535 -- dispatch table anyway, because it can be dispatched to even if it
11536 -- cannot be called directly.
11537
11538 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
11539 Set_Scope (S, Current_Scope);
11540
11541 if Is_Dispatching_Operation (Alias (S)) then
11542 Check_Dispatching_Operation (S, Empty);
11543 end if;
11544
11545 return;
11546
11547 else
11548 Report_Conflict (S, E);
11549 return;
11550 end if;
11551
11552 -- E exists and is overloadable
11553
11554 else
11555 Check_Synchronized_Overriding (S, Overridden_Subp);
11556
11557 -- Loop through E and its homonyms to determine if any of them is
11558 -- the candidate for overriding by S.
11559
11560 while Present (E) loop
11561
11562 -- Definitely not interesting if not in the current scope
11563
11564 if Scope (E) /= Current_Scope then
11565 null;
11566
11567 -- A function can overload the name of an abstract state. The
11568 -- state can be viewed as a function with a profile that cannot
11569 -- be matched by anything.
11570
11571 elsif Ekind (S) = E_Function
11572 and then Ekind (E) = E_Abstract_State
11573 then
11574 Enter_Overloaded_Entity (S);
11575 return;
11576
11577 -- Ada 2012 (AI05-0165): For internally generated bodies of null
11578 -- procedures locate the internally generated spec. We enforce
11579 -- mode conformance since a tagged type may inherit from
11580 -- interfaces several null primitives which differ only in
11581 -- the mode of the formals.
11582
11583 elsif not Comes_From_Source (S)
11584 and then Is_Null_Procedure (S)
11585 and then not Mode_Conformant (E, S)
11586 then
11587 null;
11588
11589 -- Check if we have type conformance
11590
11591 elsif Type_Conformant (E, S) then
11592
11593 -- If the old and new entities have the same profile and one
11594 -- is not the body of the other, then this is an error, unless
11595 -- one of them is implicitly declared.
11596
11597 -- There are some cases when both can be implicit, for example
11598 -- when both a literal and a function that overrides it are
11599 -- inherited in a derivation, or when an inherited operation
11600 -- of a tagged full type overrides the inherited operation of
11601 -- a private extension. Ada 83 had a special rule for the
11602 -- literal case. In Ada 95, the later implicit operation hides
11603 -- the former, and the literal is always the former. In the
11604 -- odd case where both are derived operations declared at the
11605 -- same point, both operations should be declared, and in that
11606 -- case we bypass the following test and proceed to the next
11607 -- part. This can only occur for certain obscure cases in
11608 -- instances, when an operation on a type derived from a formal
11609 -- private type does not override a homograph inherited from
11610 -- the actual. In subsequent derivations of such a type, the
11611 -- DT positions of these operations remain distinct, if they
11612 -- have been set.
11613
11614 if Present (Alias (S))
11615 and then (No (Alias (E))
11616 or else Comes_From_Source (E)
11617 or else Is_Abstract_Subprogram (S)
11618 or else
11619 (Is_Dispatching_Operation (E)
11620 and then Is_Overriding_Alias (E, S)))
11621 and then Ekind (E) /= E_Enumeration_Literal
11622 then
11623 -- When an derived operation is overloaded it may be due to
11624 -- the fact that the full view of a private extension
11625 -- re-inherits. It has to be dealt with.
11626
11627 if Is_Package_Or_Generic_Package (Current_Scope)
11628 and then In_Private_Part (Current_Scope)
11629 then
11630 Check_Operation_From_Private_View (S, E);
11631 end if;
11632
11633 -- In any case the implicit operation remains hidden by the
11634 -- existing declaration, which is overriding. Indicate that
11635 -- E overrides the operation from which S is inherited.
11636
11637 if Present (Alias (S)) then
11638 Set_Overridden_Operation (E, Alias (S));
11639 Inherit_Subprogram_Contract (E, Alias (S));
11640
11641 else
11642 Set_Overridden_Operation (E, S);
11643 Inherit_Subprogram_Contract (E, S);
11644 end if;
11645
11646 -- When a dispatching operation overrides an inherited
11647 -- subprogram, it shall be subtype conformant with the
11648 -- inherited subprogram (RM 3.9.2 (10.2)).
11649
11650 if Comes_From_Source (E)
11651 and then Is_Dispatching_Operation (E)
11652 and then Find_Dispatching_Type (S)
11653 = Find_Dispatching_Type (E)
11654 then
11655 Check_Subtype_Conformant (E, S);
11656 end if;
11657
11658 if Comes_From_Source (E) then
11659 Check_Overriding_Indicator (E, S, Is_Primitive => False);
11660
11661 -- The Ghost policy in effect at the point of declaration
11662 -- of a parent subprogram and an overriding subprogram
11663 -- must match (SPARK RM 6.9(17)).
11664
11665 Check_Ghost_Overriding (E, S);
11666 end if;
11667
11668 return;
11669
11670 -- Within an instance, the renaming declarations for actual
11671 -- subprograms may become ambiguous, but they do not hide each
11672 -- other.
11673
11674 elsif Ekind (E) /= E_Entry
11675 and then not Comes_From_Source (E)
11676 and then not Is_Generic_Instance (E)
11677 and then (Present (Alias (E))
11678 or else Is_Intrinsic_Subprogram (E))
11679 and then (not In_Instance
11680 or else No (Parent (E))
11681 or else Nkind (Unit_Declaration_Node (E)) /=
11682 N_Subprogram_Renaming_Declaration)
11683 then
11684 -- A subprogram child unit is not allowed to override an
11685 -- inherited subprogram (10.1.1(20)).
11686
11687 if Is_Child_Unit (S) then
11688 Error_Msg_N
11689 ("child unit overrides inherited subprogram in parent",
11690 S);
11691 return;
11692 end if;
11693
11694 if Is_Non_Overriding_Operation (E, S) then
11695 Enter_Overloaded_Entity (S);
11696
11697 if No (Derived_Type)
11698 or else Is_Tagged_Type (Derived_Type)
11699 then
11700 Check_Dispatching_Operation (S, Empty);
11701 end if;
11702
11703 return;
11704 end if;
11705
11706 -- E is a derived operation or an internal operator which
11707 -- is being overridden. Remove E from further visibility.
11708 -- Furthermore, if E is a dispatching operation, it must be
11709 -- replaced in the list of primitive operations of its type
11710 -- (see Override_Dispatching_Operation).
11711
11712 Overridden_Subp := E;
11713
11714 -- It is possible for E to be in the current scope and
11715 -- yet not in the entity chain. This can only occur in a
11716 -- generic context where E is an implicit concatenation
11717 -- in the formal part, because in a generic body the
11718 -- entity chain starts with the formals.
11719
11720 -- In GNATprove mode, a wrapper for an operation with
11721 -- axiomatization may be a homonym of another declaration
11722 -- for an actual subprogram (needs refinement ???).
11723
11724 if No (Prev_Entity (E)) then
11725 if In_Instance
11726 and then GNATprove_Mode
11727 and then
11728 Nkind (Original_Node (Unit_Declaration_Node (S))) =
11729 N_Subprogram_Renaming_Declaration
11730 then
11731 return;
11732 else
11733 pragma Assert (Chars (E) = Name_Op_Concat);
11734 null;
11735 end if;
11736 end if;
11737
11738 -- E must be removed both from the entity_list of the
11739 -- current scope, and from the visibility chain.
11740
11741 if Debug_Flag_E then
11742 Write_Str ("Override implicit operation ");
11743 Write_Int (Int (E));
11744 Write_Eol;
11745 end if;
11746
11747 -- If E is a predefined concatenation, it stands for four
11748 -- different operations. As a result, a single explicit
11749 -- declaration does not hide it. In a possible ambiguous
11750 -- situation, Disambiguate chooses the user-defined op,
11751 -- so it is correct to retain the previous internal one.
11752
11753 if Chars (E) /= Name_Op_Concat
11754 or else Ekind (E) /= E_Operator
11755 then
11756 -- For nondispatching derived operations that are
11757 -- overridden by a subprogram declared in the private
11758 -- part of a package, we retain the derived subprogram
11759 -- but mark it as not immediately visible. If the
11760 -- derived operation was declared in the visible part
11761 -- then this ensures that it will still be visible
11762 -- outside the package with the proper signature
11763 -- (calls from outside must also be directed to this
11764 -- version rather than the overriding one, unlike the
11765 -- dispatching case). Calls from inside the package
11766 -- will still resolve to the overriding subprogram
11767 -- since the derived one is marked as not visible
11768 -- within the package.
11769
11770 -- If the private operation is dispatching, we achieve
11771 -- the overriding by keeping the implicit operation
11772 -- but setting its alias to be the overriding one. In
11773 -- this fashion the proper body is executed in all
11774 -- cases, but the original signature is used outside
11775 -- of the package.
11776
11777 -- If the overriding is not in the private part, we
11778 -- remove the implicit operation altogether.
11779
11780 if Is_Private_Declaration (S) then
11781 if not Is_Dispatching_Operation (E) then
11782 Set_Is_Immediately_Visible (E, False);
11783 else
11784 -- Work done in Override_Dispatching_Operation, so
11785 -- nothing else needs to be done here.
11786
11787 null;
11788 end if;
11789
11790 else
11791 Remove_Entity_And_Homonym (E);
11792 end if;
11793 end if;
11794
11795 Enter_Overloaded_Entity (S);
11796
11797 -- For entities generated by Derive_Subprograms the
11798 -- overridden operation is the inherited primitive
11799 -- (which is available through the attribute alias).
11800
11801 if not (Comes_From_Source (E))
11802 and then Is_Dispatching_Operation (E)
11803 and then Find_Dispatching_Type (E) =
11804 Find_Dispatching_Type (S)
11805 and then Present (Alias (E))
11806 and then Comes_From_Source (Alias (E))
11807 then
11808 Set_Overridden_Operation (S, Alias (E));
11809 Inherit_Subprogram_Contract (S, Alias (E));
11810
11811 -- Normal case of setting entity as overridden
11812
11813 -- Note: Static_Initialization and Overridden_Operation
11814 -- attributes use the same field in subprogram entities.
11815 -- Static_Initialization is only defined for internal
11816 -- initialization procedures, where Overridden_Operation
11817 -- is irrelevant. Therefore the setting of this attribute
11818 -- must check whether the target is an init_proc.
11819
11820 elsif not Is_Init_Proc (S) then
11821 Set_Overridden_Operation (S, E);
11822 Inherit_Subprogram_Contract (S, E);
11823 end if;
11824
11825 Check_Overriding_Indicator (S, E, Is_Primitive => True);
11826
11827 -- The Ghost policy in effect at the point of declaration
11828 -- of a parent subprogram and an overriding subprogram
11829 -- must match (SPARK RM 6.9(17)).
11830
11831 Check_Ghost_Overriding (S, E);
11832
11833 -- If S is a user-defined subprogram or a null procedure
11834 -- expanded to override an inherited null procedure, or a
11835 -- predefined dispatching primitive then indicate that E
11836 -- overrides the operation from which S is inherited.
11837
11838 if Comes_From_Source (S)
11839 or else
11840 (Present (Parent (S))
11841 and then Nkind (Parent (S)) = N_Procedure_Specification
11842 and then Null_Present (Parent (S)))
11843 or else
11844 (Present (Alias (E))
11845 and then
11846 Is_Predefined_Dispatching_Operation (Alias (E)))
11847 then
11848 if Present (Alias (E)) then
11849 Set_Overridden_Operation (S, Alias (E));
11850 Inherit_Subprogram_Contract (S, Alias (E));
11851 end if;
11852 end if;
11853
11854 if Is_Dispatching_Operation (E) then
11855
11856 -- An overriding dispatching subprogram inherits the
11857 -- convention of the overridden subprogram (AI-117).
11858
11859 Set_Convention (S, Convention (E));
11860 Check_Dispatching_Operation (S, E);
11861
11862 else
11863 Check_Dispatching_Operation (S, Empty);
11864 end if;
11865
11866 Check_For_Primitive_Subprogram
11867 (Is_Primitive_Subp, Is_Overriding => True);
11868 goto Check_Inequality;
11869
11870 -- Apparent redeclarations in instances can occur when two
11871 -- formal types get the same actual type. The subprograms in
11872 -- in the instance are legal, even if not callable from the
11873 -- outside. Calls from within are disambiguated elsewhere.
11874 -- For dispatching operations in the visible part, the usual
11875 -- rules apply, and operations with the same profile are not
11876 -- legal (B830001).
11877
11878 elsif (In_Instance_Visible_Part
11879 and then not Is_Dispatching_Operation (E))
11880 or else In_Instance_Not_Visible
11881 then
11882 null;
11883
11884 -- Here we have a real error (identical profile)
11885
11886 else
11887 Error_Msg_Sloc := Sloc (E);
11888
11889 -- Avoid cascaded errors if the entity appears in
11890 -- subsequent calls.
11891
11892 Set_Scope (S, Current_Scope);
11893
11894 -- Generate error, with extra useful warning for the case
11895 -- of a generic instance with no completion.
11896
11897 if Is_Generic_Instance (S)
11898 and then not Has_Completion (E)
11899 then
11900 Error_Msg_N
11901 ("instantiation cannot provide body for&", S);
11902 Error_Msg_N ("\& conflicts with declaration#", S);
11903 else
11904 Error_Msg_N ("& conflicts with declaration#", S);
11905 end if;
11906
11907 return;
11908 end if;
11909
11910 else
11911 -- If one subprogram has an access parameter and the other
11912 -- a parameter of an access type, calls to either might be
11913 -- ambiguous. Verify that parameters match except for the
11914 -- access parameter.
11915
11916 if May_Hide_Profile then
11917 declare
11918 F1 : Entity_Id;
11919 F2 : Entity_Id;
11920
11921 begin
11922 F1 := First_Formal (S);
11923 F2 := First_Formal (E);
11924 while Present (F1) and then Present (F2) loop
11925 if Is_Access_Type (Etype (F1)) then
11926 if not Is_Access_Type (Etype (F2))
11927 or else not Conforming_Types
11928 (Designated_Type (Etype (F1)),
11929 Designated_Type (Etype (F2)),
11930 Type_Conformant)
11931 then
11932 May_Hide_Profile := False;
11933 end if;
11934
11935 elsif
11936 not Conforming_Types
11937 (Etype (F1), Etype (F2), Type_Conformant)
11938 then
11939 May_Hide_Profile := False;
11940 end if;
11941
11942 Next_Formal (F1);
11943 Next_Formal (F2);
11944 end loop;
11945
11946 if May_Hide_Profile
11947 and then No (F1)
11948 and then No (F2)
11949 then
11950 Error_Msg_NE ("calls to& may be ambiguous??", S, S);
11951 end if;
11952 end;
11953 end if;
11954 end if;
11955
11956 E := Homonym (E);
11957 end loop;
11958
11959 -- On exit, we know that S is a new entity
11960
11961 Enter_Overloaded_Entity (S);
11962 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
11963 Check_Overriding_Indicator
11964 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
11965
11966 -- The Ghost policy in effect at the point of declaration of a parent
11967 -- subprogram and an overriding subprogram must match
11968 -- (SPARK RM 6.9(17)).
11969
11970 Check_Ghost_Overriding (S, Overridden_Subp);
11971
11972 -- If S is a derived operation for an untagged type then by
11973 -- definition it's not a dispatching operation (even if the parent
11974 -- operation was dispatching), so Check_Dispatching_Operation is not
11975 -- called in that case.
11976
11977 if No (Derived_Type)
11978 or else Is_Tagged_Type (Derived_Type)
11979 then
11980 Check_Dispatching_Operation (S, Empty);
11981 end if;
11982 end if;
11983
11984 -- If this is a user-defined equality operator that is not a derived
11985 -- subprogram, create the corresponding inequality. If the operation is
11986 -- dispatching, the expansion is done elsewhere, and we do not create
11987 -- an explicit inequality operation.
11988
11989 <<Check_Inequality>>
11990 if Chars (S) = Name_Op_Eq
11991 and then Etype (S) = Standard_Boolean
11992 and then Present (Parent (S))
11993 and then not Is_Dispatching_Operation (S)
11994 then
11995 Make_Inequality_Operator (S);
11996 Check_Untagged_Equality (S);
11997 end if;
11998 end New_Overloaded_Entity;
11999
12000 ----------------------------------
12001 -- Preanalyze_Formal_Expression --
12002 ----------------------------------
12003
12004 procedure Preanalyze_Formal_Expression (N : Node_Id; T : Entity_Id) is
12005 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
12006 begin
12007 In_Spec_Expression := True;
12008 Preanalyze_With_Freezing_And_Resolve (N, T);
12009 In_Spec_Expression := Save_In_Spec_Expression;
12010 end Preanalyze_Formal_Expression;
12011
12012 ---------------------
12013 -- Process_Formals --
12014 ---------------------
12015
12016 procedure Process_Formals
12017 (T : List_Id;
12018 Related_Nod : Node_Id)
12019 is
12020 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean;
12021 -- Determine whether an access type designates a type coming from a
12022 -- limited view.
12023
12024 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
12025 -- Check whether the default has a class-wide type. After analysis the
12026 -- default has the type of the formal, so we must also check explicitly
12027 -- for an access attribute.
12028
12029 ----------------------------------
12030 -- Designates_From_Limited_With --
12031 ----------------------------------
12032
12033 function Designates_From_Limited_With (Typ : Entity_Id) return Boolean is
12034 Desig : Entity_Id := Typ;
12035
12036 begin
12037 if Is_Access_Type (Desig) then
12038 Desig := Directly_Designated_Type (Desig);
12039 end if;
12040
12041 if Is_Class_Wide_Type (Desig) then
12042 Desig := Root_Type (Desig);
12043 end if;
12044
12045 return
12046 Ekind (Desig) = E_Incomplete_Type
12047 and then From_Limited_With (Desig);
12048 end Designates_From_Limited_With;
12049
12050 ---------------------------
12051 -- Is_Class_Wide_Default --
12052 ---------------------------
12053
12054 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
12055 begin
12056 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
12057 or else (Nkind (D) = N_Attribute_Reference
12058 and then Attribute_Name (D) = Name_Access
12059 and then Is_Class_Wide_Type (Etype (Prefix (D))));
12060 end Is_Class_Wide_Default;
12061
12062 -- Local variables
12063
12064 Context : constant Node_Id := Parent (Parent (T));
12065 Default : Node_Id;
12066 Formal : Entity_Id;
12067 Formal_Type : Entity_Id;
12068 Param_Spec : Node_Id;
12069 Ptype : Entity_Id;
12070
12071 Num_Out_Params : Nat := 0;
12072 First_Out_Param : Entity_Id := Empty;
12073 -- Used for setting Is_Only_Out_Parameter
12074
12075 -- Start of processing for Process_Formals
12076
12077 begin
12078 -- In order to prevent premature use of the formals in the same formal
12079 -- part, the Ekind is left undefined until all default expressions are
12080 -- analyzed. The Ekind is established in a separate loop at the end.
12081
12082 Param_Spec := First (T);
12083 while Present (Param_Spec) loop
12084 Formal := Defining_Identifier (Param_Spec);
12085 Set_Never_Set_In_Source (Formal, True);
12086 Enter_Name (Formal);
12087
12088 -- Case of ordinary parameters
12089
12090 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
12091 Find_Type (Parameter_Type (Param_Spec));
12092 Ptype := Parameter_Type (Param_Spec);
12093
12094 if Ptype = Error then
12095 goto Continue;
12096 end if;
12097
12098 -- Protect against malformed parameter types
12099
12100 if Nkind (Ptype) not in N_Has_Entity then
12101 Formal_Type := Any_Type;
12102 else
12103 Formal_Type := Entity (Ptype);
12104 end if;
12105
12106 if Is_Incomplete_Type (Formal_Type)
12107 or else
12108 (Is_Class_Wide_Type (Formal_Type)
12109 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
12110 then
12111 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
12112 -- primitive operations, as long as their completion is
12113 -- in the same declarative part. If in the private part
12114 -- this means that the type cannot be a Taft-amendment type.
12115 -- Check is done on package exit. For access to subprograms,
12116 -- the use is legal for Taft-amendment types.
12117
12118 -- Ada 2012: tagged incomplete types are allowed as generic
12119 -- formal types. They do not introduce dependencies and the
12120 -- corresponding generic subprogram does not have a delayed
12121 -- freeze, because it does not need a freeze node. However,
12122 -- it is still the case that untagged incomplete types cannot
12123 -- be Taft-amendment types and must be completed in private
12124 -- part, so the subprogram must appear in the list of private
12125 -- dependents of the type.
12126
12127 if Is_Tagged_Type (Formal_Type)
12128 or else (Ada_Version >= Ada_2012
12129 and then not From_Limited_With (Formal_Type)
12130 and then not Is_Generic_Type (Formal_Type))
12131 then
12132 if Ekind (Scope (Current_Scope)) = E_Package
12133 and then not Is_Generic_Type (Formal_Type)
12134 and then not Is_Class_Wide_Type (Formal_Type)
12135 then
12136 if Nkind (Parent (T)) not in
12137 N_Access_Function_Definition |
12138 N_Access_Procedure_Definition
12139 then
12140 Append_Elmt (Current_Scope,
12141 Private_Dependents (Base_Type (Formal_Type)));
12142
12143 -- Freezing is delayed to ensure that Register_Prim
12144 -- will get called for this operation, which is needed
12145 -- in cases where static dispatch tables aren't built.
12146 -- (Note that the same is done for controlling access
12147 -- parameter cases in function Access_Definition.)
12148
12149 if not Is_Thunk (Current_Scope) then
12150 Set_Has_Delayed_Freeze (Current_Scope);
12151 end if;
12152 end if;
12153 end if;
12154
12155 elsif Nkind (Parent (T)) not in N_Access_Function_Definition
12156 | N_Access_Procedure_Definition
12157 then
12158 -- AI05-0151: Tagged incomplete types are allowed in all
12159 -- formal parts. Untagged incomplete types are not allowed
12160 -- in bodies. Limited views of either kind are not allowed
12161 -- if there is no place at which the non-limited view can
12162 -- become available.
12163
12164 -- Incomplete formal untagged types are not allowed in
12165 -- subprogram bodies (but are legal in their declarations).
12166 -- This excludes bodies created for null procedures, which
12167 -- are basic declarations.
12168
12169 if Is_Generic_Type (Formal_Type)
12170 and then not Is_Tagged_Type (Formal_Type)
12171 and then Nkind (Parent (Related_Nod)) = N_Subprogram_Body
12172 then
12173 Error_Msg_N
12174 ("invalid use of formal incomplete type", Param_Spec);
12175
12176 elsif Ada_Version >= Ada_2012 then
12177 if Is_Tagged_Type (Formal_Type)
12178 and then (not From_Limited_With (Formal_Type)
12179 or else not In_Package_Body)
12180 then
12181 null;
12182
12183 elsif Nkind (Context) in N_Accept_Statement
12184 | N_Accept_Alternative
12185 | N_Entry_Body
12186 or else (Nkind (Context) = N_Subprogram_Body
12187 and then Comes_From_Source (Context))
12188 then
12189 Error_Msg_NE
12190 ("invalid use of untagged incomplete type &",
12191 Ptype, Formal_Type);
12192 end if;
12193
12194 else
12195 Error_Msg_NE
12196 ("invalid use of incomplete type&",
12197 Param_Spec, Formal_Type);
12198
12199 -- Further checks on the legality of incomplete types
12200 -- in formal parts are delayed until the freeze point
12201 -- of the enclosing subprogram or access to subprogram.
12202 end if;
12203 end if;
12204
12205 elsif Ekind (Formal_Type) = E_Void then
12206 Error_Msg_NE
12207 ("premature use of&",
12208 Parameter_Type (Param_Spec), Formal_Type);
12209 end if;
12210
12211 -- Ada 2012 (AI-142): Handle aliased parameters
12212
12213 if Ada_Version >= Ada_2012
12214 and then Aliased_Present (Param_Spec)
12215 then
12216 Set_Is_Aliased (Formal);
12217
12218 -- AI12-001: All aliased objects are considered to be specified
12219 -- as independently addressable (RM C.6(8.1/4)).
12220
12221 Set_Is_Independent (Formal);
12222 end if;
12223
12224 -- Ada 2005 (AI-231): Create and decorate an internal subtype
12225 -- declaration corresponding to the null-excluding type of the
12226 -- formal in the enclosing scope. Finally, replace the parameter
12227 -- type of the formal with the internal subtype.
12228
12229 if Ada_Version >= Ada_2005
12230 and then Null_Exclusion_Present (Param_Spec)
12231 then
12232 if not Is_Access_Type (Formal_Type) then
12233 Error_Msg_N
12234 ("`NOT NULL` allowed only for an access type", Param_Spec);
12235
12236 else
12237 if Can_Never_Be_Null (Formal_Type)
12238 and then Comes_From_Source (Related_Nod)
12239 then
12240 Error_Msg_NE
12241 ("`NOT NULL` not allowed (& already excludes null)",
12242 Param_Spec, Formal_Type);
12243 end if;
12244
12245 Formal_Type :=
12246 Create_Null_Excluding_Itype
12247 (T => Formal_Type,
12248 Related_Nod => Related_Nod,
12249 Scope_Id => Scope (Current_Scope));
12250
12251 -- If the designated type of the itype is an itype that is
12252 -- not frozen yet, we set the Has_Delayed_Freeze attribute
12253 -- on the access subtype, to prevent order-of-elaboration
12254 -- issues in the backend.
12255
12256 -- Example:
12257 -- type T is access procedure;
12258 -- procedure Op (O : not null T);
12259
12260 if Is_Itype (Directly_Designated_Type (Formal_Type))
12261 and then
12262 not Is_Frozen (Directly_Designated_Type (Formal_Type))
12263 then
12264 Set_Has_Delayed_Freeze (Formal_Type);
12265 end if;
12266 end if;
12267 end if;
12268
12269 -- An access formal type
12270
12271 else
12272 Formal_Type :=
12273 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
12274
12275 -- No need to continue if we already notified errors
12276
12277 if not Present (Formal_Type) then
12278 return;
12279 end if;
12280
12281 -- Ada 2005 (AI-254)
12282
12283 declare
12284 AD : constant Node_Id :=
12285 Access_To_Subprogram_Definition
12286 (Parameter_Type (Param_Spec));
12287 begin
12288 if Present (AD) and then Protected_Present (AD) then
12289 Formal_Type :=
12290 Replace_Anonymous_Access_To_Protected_Subprogram
12291 (Param_Spec);
12292 end if;
12293 end;
12294 end if;
12295
12296 Set_Etype (Formal, Formal_Type);
12297
12298 -- Deal with default expression if present
12299
12300 Default := Expression (Param_Spec);
12301
12302 if Present (Default) then
12303 if Out_Present (Param_Spec) then
12304 Error_Msg_N
12305 ("default initialization only allowed for IN parameters",
12306 Param_Spec);
12307 end if;
12308
12309 -- Do the special preanalysis of the expression (see section on
12310 -- "Handling of Default Expressions" in the spec of package Sem).
12311
12312 Preanalyze_Formal_Expression (Default, Formal_Type);
12313
12314 -- An access to constant cannot be the default for
12315 -- an access parameter that is an access to variable.
12316
12317 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12318 and then not Is_Access_Constant (Formal_Type)
12319 and then Is_Access_Type (Etype (Default))
12320 and then Is_Access_Constant (Etype (Default))
12321 then
12322 Error_Msg_N
12323 ("formal that is access to variable cannot be initialized "
12324 & "with an access-to-constant expression", Default);
12325 end if;
12326
12327 -- Check that the designated type of an access parameter's default
12328 -- is not a class-wide type unless the parameter's designated type
12329 -- is also class-wide.
12330
12331 if Ekind (Formal_Type) = E_Anonymous_Access_Type
12332 and then not Designates_From_Limited_With (Formal_Type)
12333 and then Is_Class_Wide_Default (Default)
12334 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
12335 then
12336 Error_Msg_N
12337 ("access to class-wide expression not allowed here", Default);
12338 end if;
12339
12340 -- Check incorrect use of dynamically tagged expressions
12341
12342 if Is_Tagged_Type (Formal_Type) then
12343 Check_Dynamically_Tagged_Expression
12344 (Expr => Default,
12345 Typ => Formal_Type,
12346 Related_Nod => Default);
12347 end if;
12348 end if;
12349
12350 -- Ada 2005 (AI-231): Static checks
12351
12352 if Ada_Version >= Ada_2005
12353 and then Is_Access_Type (Etype (Formal))
12354 and then Can_Never_Be_Null (Etype (Formal))
12355 then
12356 Null_Exclusion_Static_Checks (Param_Spec);
12357 end if;
12358
12359 -- The following checks are relevant only when SPARK_Mode is on as
12360 -- these are not standard Ada legality rules.
12361
12362 if SPARK_Mode = On then
12363 if Ekind (Scope (Formal)) in E_Function | E_Generic_Function then
12364
12365 -- A function cannot have a parameter of mode IN OUT or OUT
12366 -- (SPARK RM 6.1).
12367
12368 if Ekind (Formal) in E_In_Out_Parameter | E_Out_Parameter then
12369 Error_Msg_N
12370 ("function cannot have parameter of mode `OUT` or "
12371 & "`IN OUT`", Formal);
12372 end if;
12373
12374 -- A procedure cannot have an effectively volatile formal
12375 -- parameter of mode IN because it behaves as a constant
12376 -- (SPARK RM 7.1.3(4)).
12377
12378 elsif Ekind (Scope (Formal)) = E_Procedure
12379 and then Ekind (Formal) = E_In_Parameter
12380 and then Is_Effectively_Volatile (Formal)
12381 then
12382 Error_Msg_N
12383 ("formal parameter of mode `IN` cannot be volatile", Formal);
12384 end if;
12385 end if;
12386
12387 -- Deal with aspects on formal parameters. Only Unreferenced is
12388 -- supported for the time being.
12389
12390 if Has_Aspects (Param_Spec) then
12391 declare
12392 Aspect : Node_Id := First (Aspect_Specifications (Param_Spec));
12393 begin
12394 while Present (Aspect) loop
12395 if Chars (Identifier (Aspect)) = Name_Unreferenced then
12396 Set_Has_Pragma_Unreferenced (Formal);
12397 else
12398 Error_Msg_NE
12399 ("unsupported aspect& on parameter",
12400 Aspect, Identifier (Aspect));
12401 end if;
12402
12403 Next (Aspect);
12404 end loop;
12405 end;
12406 end if;
12407
12408 <<Continue>>
12409 Next (Param_Spec);
12410 end loop;
12411
12412 -- If this is the formal part of a function specification, analyze the
12413 -- subtype mark in the context where the formals are visible but not
12414 -- yet usable, and may hide outer homographs.
12415
12416 if Nkind (Related_Nod) = N_Function_Specification then
12417 Analyze_Return_Type (Related_Nod);
12418 end if;
12419
12420 -- Now set the kind (mode) of each formal
12421
12422 Param_Spec := First (T);
12423 while Present (Param_Spec) loop
12424 Formal := Defining_Identifier (Param_Spec);
12425 Set_Formal_Mode (Formal);
12426
12427 if Ekind (Formal) = E_In_Parameter then
12428 Set_Default_Value (Formal, Expression (Param_Spec));
12429
12430 if Present (Expression (Param_Spec)) then
12431 Default := Expression (Param_Spec);
12432
12433 if Is_Scalar_Type (Etype (Default)) then
12434 if Nkind (Parameter_Type (Param_Spec)) /=
12435 N_Access_Definition
12436 then
12437 Formal_Type := Entity (Parameter_Type (Param_Spec));
12438 else
12439 Formal_Type :=
12440 Access_Definition
12441 (Related_Nod, Parameter_Type (Param_Spec));
12442 end if;
12443
12444 Apply_Scalar_Range_Check (Default, Formal_Type);
12445 end if;
12446 end if;
12447
12448 elsif Ekind (Formal) = E_Out_Parameter then
12449 Num_Out_Params := Num_Out_Params + 1;
12450
12451 if Num_Out_Params = 1 then
12452 First_Out_Param := Formal;
12453 end if;
12454
12455 elsif Ekind (Formal) = E_In_Out_Parameter then
12456 Num_Out_Params := Num_Out_Params + 1;
12457 end if;
12458
12459 -- Skip remaining processing if formal type was in error
12460
12461 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
12462 goto Next_Parameter;
12463 end if;
12464
12465 -- Force call by reference if aliased
12466
12467 declare
12468 Conv : constant Convention_Id := Convention (Etype (Formal));
12469 begin
12470 if Is_Aliased (Formal) then
12471 Set_Mechanism (Formal, By_Reference);
12472
12473 -- Warn if user asked this to be passed by copy
12474
12475 if Conv = Convention_Ada_Pass_By_Copy then
12476 Error_Msg_N
12477 ("cannot pass aliased parameter & by copy??", Formal);
12478 end if;
12479
12480 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
12481
12482 elsif Conv = Convention_Ada_Pass_By_Copy then
12483 Set_Mechanism (Formal, By_Copy);
12484
12485 elsif Conv = Convention_Ada_Pass_By_Reference then
12486 Set_Mechanism (Formal, By_Reference);
12487 end if;
12488 end;
12489
12490 <<Next_Parameter>>
12491 Next (Param_Spec);
12492 end loop;
12493
12494 if Present (First_Out_Param) and then Num_Out_Params = 1 then
12495 Set_Is_Only_Out_Parameter (First_Out_Param);
12496 end if;
12497 end Process_Formals;
12498
12499 ----------------------------
12500 -- Reference_Body_Formals --
12501 ----------------------------
12502
12503 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
12504 Fs : Entity_Id;
12505 Fb : Entity_Id;
12506
12507 begin
12508 if Error_Posted (Spec) then
12509 return;
12510 end if;
12511
12512 -- Iterate over both lists. They may be of different lengths if the two
12513 -- specs are not conformant.
12514
12515 Fs := First_Formal (Spec);
12516 Fb := First_Formal (Bod);
12517 while Present (Fs) and then Present (Fb) loop
12518 Generate_Reference (Fs, Fb, 'b');
12519
12520 if Style_Check then
12521 Style.Check_Identifier (Fb, Fs);
12522 end if;
12523
12524 Set_Spec_Entity (Fb, Fs);
12525 Set_Referenced (Fs, False);
12526 Next_Formal (Fs);
12527 Next_Formal (Fb);
12528 end loop;
12529 end Reference_Body_Formals;
12530
12531 -------------------------
12532 -- Set_Actual_Subtypes --
12533 -------------------------
12534
12535 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
12536 Decl : Node_Id;
12537 Formal : Entity_Id;
12538 T : Entity_Id;
12539 First_Stmt : Node_Id := Empty;
12540 AS_Needed : Boolean;
12541
12542 begin
12543 -- If this is an empty initialization procedure, no need to create
12544 -- actual subtypes (small optimization).
12545
12546 if Ekind (Subp) = E_Procedure and then Is_Null_Init_Proc (Subp) then
12547 return;
12548
12549 -- Within a predicate function we do not want to generate local
12550 -- subtypes that may generate nested predicate functions.
12551
12552 elsif Is_Subprogram (Subp) and then Is_Predicate_Function (Subp) then
12553 return;
12554 end if;
12555
12556 -- The subtype declarations may freeze the formals. The body generated
12557 -- for an expression function is not a freeze point, so do not emit
12558 -- these declarations (small loss of efficiency in rare cases).
12559
12560 if Nkind (N) = N_Subprogram_Body
12561 and then Was_Expression_Function (N)
12562 then
12563 return;
12564 end if;
12565
12566 Formal := First_Formal (Subp);
12567 while Present (Formal) loop
12568 T := Etype (Formal);
12569
12570 -- We never need an actual subtype for a constrained formal
12571
12572 if Is_Constrained (T) then
12573 AS_Needed := False;
12574
12575 -- If we have unknown discriminants, then we do not need an actual
12576 -- subtype, or more accurately we cannot figure it out. Note that
12577 -- all class-wide types have unknown discriminants.
12578
12579 elsif Has_Unknown_Discriminants (T) then
12580 AS_Needed := False;
12581
12582 -- At this stage we have an unconstrained type that may need an
12583 -- actual subtype. For sure the actual subtype is needed if we have
12584 -- an unconstrained array type. However, in an instance, the type
12585 -- may appear as a subtype of the full view, while the actual is
12586 -- in fact private (in which case no actual subtype is needed) so
12587 -- check the kind of the base type.
12588
12589 elsif Is_Array_Type (Base_Type (T)) then
12590 AS_Needed := True;
12591
12592 -- The only other case needing an actual subtype is an unconstrained
12593 -- record type which is an IN parameter (we cannot generate actual
12594 -- subtypes for the OUT or IN OUT case, since an assignment can
12595 -- change the discriminant values. However we exclude the case of
12596 -- initialization procedures, since discriminants are handled very
12597 -- specially in this context, see the section entitled "Handling of
12598 -- Discriminants" in Einfo.
12599
12600 -- We also exclude the case of Discrim_SO_Functions (functions used
12601 -- in front-end layout mode for size/offset values), since in such
12602 -- functions only discriminants are referenced, and not only are such
12603 -- subtypes not needed, but they cannot always be generated, because
12604 -- of order of elaboration issues.
12605
12606 elsif Is_Record_Type (T)
12607 and then Ekind (Formal) = E_In_Parameter
12608 and then Chars (Formal) /= Name_uInit
12609 and then not Is_Unchecked_Union (T)
12610 and then not Is_Discrim_SO_Function (Subp)
12611 then
12612 AS_Needed := True;
12613
12614 -- All other cases do not need an actual subtype
12615
12616 else
12617 AS_Needed := False;
12618 end if;
12619
12620 -- Generate actual subtypes for unconstrained arrays and
12621 -- unconstrained discriminated records.
12622
12623 if AS_Needed then
12624 if Nkind (N) = N_Accept_Statement then
12625
12626 -- If expansion is active, the formal is replaced by a local
12627 -- variable that renames the corresponding entry of the
12628 -- parameter block, and it is this local variable that may
12629 -- require an actual subtype.
12630
12631 if Expander_Active then
12632 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
12633 else
12634 Decl := Build_Actual_Subtype (T, Formal);
12635 end if;
12636
12637 if Present (Handled_Statement_Sequence (N)) then
12638 First_Stmt :=
12639 First (Statements (Handled_Statement_Sequence (N)));
12640 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
12641 Mark_Rewrite_Insertion (Decl);
12642 else
12643 -- If the accept statement has no body, there will be no
12644 -- reference to the actuals, so no need to compute actual
12645 -- subtypes.
12646
12647 return;
12648 end if;
12649
12650 else
12651 Decl := Build_Actual_Subtype (T, Formal);
12652 Prepend (Decl, Declarations (N));
12653 Mark_Rewrite_Insertion (Decl);
12654 end if;
12655
12656 -- The declaration uses the bounds of an existing object, and
12657 -- therefore needs no constraint checks.
12658
12659 Analyze (Decl, Suppress => All_Checks);
12660 Set_Is_Actual_Subtype (Defining_Identifier (Decl));
12661
12662 -- We need to freeze manually the generated type when it is
12663 -- inserted anywhere else than in a declarative part.
12664
12665 if Present (First_Stmt) then
12666 Insert_List_Before_And_Analyze (First_Stmt,
12667 Freeze_Entity (Defining_Identifier (Decl), N));
12668
12669 -- Ditto if the type has a dynamic predicate, because the
12670 -- generated function will mention the actual subtype. The
12671 -- predicate may come from an explicit aspect of be inherited.
12672
12673 elsif Has_Predicates (T) then
12674 Insert_List_After_And_Analyze (Decl,
12675 Freeze_Entity (Defining_Identifier (Decl), N));
12676 end if;
12677
12678 if Nkind (N) = N_Accept_Statement
12679 and then Expander_Active
12680 then
12681 Set_Actual_Subtype (Renamed_Object (Formal),
12682 Defining_Identifier (Decl));
12683 else
12684 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
12685 end if;
12686 end if;
12687
12688 Next_Formal (Formal);
12689 end loop;
12690 end Set_Actual_Subtypes;
12691
12692 ---------------------
12693 -- Set_Formal_Mode --
12694 ---------------------
12695
12696 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
12697 Spec : constant Node_Id := Parent (Formal_Id);
12698 Id : constant Entity_Id := Scope (Formal_Id);
12699
12700 begin
12701 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
12702 -- since we ensure that corresponding actuals are always valid at the
12703 -- point of the call.
12704
12705 if Out_Present (Spec) then
12706 if Is_Entry (Id)
12707 or else Is_Subprogram_Or_Generic_Subprogram (Id)
12708 then
12709 Set_Has_Out_Or_In_Out_Parameter (Id, True);
12710 end if;
12711
12712 if Ekind (Id) in E_Function | E_Generic_Function then
12713
12714 -- [IN] OUT parameters allowed for functions in Ada 2012
12715
12716 if Ada_Version >= Ada_2012 then
12717
12718 -- Even in Ada 2012 operators can only have IN parameters
12719
12720 if Is_Operator_Symbol_Name (Chars (Scope (Formal_Id))) then
12721 Error_Msg_N ("operators can only have IN parameters", Spec);
12722 end if;
12723
12724 if In_Present (Spec) then
12725 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12726 else
12727 Set_Ekind (Formal_Id, E_Out_Parameter);
12728 end if;
12729
12730 -- But not in earlier versions of Ada
12731
12732 else
12733 Error_Msg_N ("functions can only have IN parameters", Spec);
12734 Set_Ekind (Formal_Id, E_In_Parameter);
12735 end if;
12736
12737 elsif In_Present (Spec) then
12738 Set_Ekind (Formal_Id, E_In_Out_Parameter);
12739
12740 else
12741 Set_Ekind (Formal_Id, E_Out_Parameter);
12742 Set_Never_Set_In_Source (Formal_Id, True);
12743 Set_Is_True_Constant (Formal_Id, False);
12744 Set_Current_Value (Formal_Id, Empty);
12745 end if;
12746
12747 else
12748 Set_Ekind (Formal_Id, E_In_Parameter);
12749 end if;
12750
12751 -- Set Is_Known_Non_Null for access parameters since the language
12752 -- guarantees that access parameters are always non-null. We also set
12753 -- Can_Never_Be_Null, since there is no way to change the value.
12754
12755 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
12756
12757 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
12758 -- null; In Ada 2005, only if then null_exclusion is explicit.
12759
12760 if Ada_Version < Ada_2005
12761 or else Can_Never_Be_Null (Etype (Formal_Id))
12762 then
12763 Set_Is_Known_Non_Null (Formal_Id);
12764 Set_Can_Never_Be_Null (Formal_Id);
12765 end if;
12766
12767 -- Ada 2005 (AI-231): Null-exclusion access subtype
12768
12769 elsif Is_Access_Type (Etype (Formal_Id))
12770 and then Can_Never_Be_Null (Etype (Formal_Id))
12771 then
12772 Set_Is_Known_Non_Null (Formal_Id);
12773
12774 -- We can also set Can_Never_Be_Null (thus preventing some junk
12775 -- access checks) for the case of an IN parameter, which cannot
12776 -- be changed, or for an IN OUT parameter, which can be changed but
12777 -- not to a null value. But for an OUT parameter, the initial value
12778 -- passed in can be null, so we can't set this flag in that case.
12779
12780 if Ekind (Formal_Id) /= E_Out_Parameter then
12781 Set_Can_Never_Be_Null (Formal_Id);
12782 end if;
12783 end if;
12784
12785 Set_Mechanism (Formal_Id, Default_Mechanism);
12786 Set_Formal_Validity (Formal_Id);
12787 end Set_Formal_Mode;
12788
12789 -------------------------
12790 -- Set_Formal_Validity --
12791 -------------------------
12792
12793 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12794 begin
12795 -- If no validity checking, then we cannot assume anything about the
12796 -- validity of parameters, since we do not know there is any checking
12797 -- of the validity on the call side.
12798
12799 if not Validity_Checks_On then
12800 return;
12801
12802 -- If validity checking for parameters is enabled, this means we are
12803 -- not supposed to make any assumptions about argument values.
12804
12805 elsif Validity_Check_Parameters then
12806 return;
12807
12808 -- If we are checking in parameters, we will assume that the caller is
12809 -- also checking parameters, so we can assume the parameter is valid.
12810
12811 elsif Ekind (Formal_Id) = E_In_Parameter
12812 and then Validity_Check_In_Params
12813 then
12814 Set_Is_Known_Valid (Formal_Id, True);
12815
12816 -- Similar treatment for IN OUT parameters
12817
12818 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12819 and then Validity_Check_In_Out_Params
12820 then
12821 Set_Is_Known_Valid (Formal_Id, True);
12822 end if;
12823 end Set_Formal_Validity;
12824
12825 ------------------------
12826 -- Subtype_Conformant --
12827 ------------------------
12828
12829 function Subtype_Conformant
12830 (New_Id : Entity_Id;
12831 Old_Id : Entity_Id;
12832 Skip_Controlling_Formals : Boolean := False) return Boolean
12833 is
12834 Result : Boolean;
12835 begin
12836 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12837 Skip_Controlling_Formals => Skip_Controlling_Formals);
12838 return Result;
12839 end Subtype_Conformant;
12840
12841 ---------------------
12842 -- Type_Conformant --
12843 ---------------------
12844
12845 function Type_Conformant
12846 (New_Id : Entity_Id;
12847 Old_Id : Entity_Id;
12848 Skip_Controlling_Formals : Boolean := False) return Boolean
12849 is
12850 Result : Boolean;
12851 begin
12852 May_Hide_Profile := False;
12853 Check_Conformance
12854 (New_Id, Old_Id, Type_Conformant, False, Result,
12855 Skip_Controlling_Formals => Skip_Controlling_Formals);
12856 return Result;
12857 end Type_Conformant;
12858
12859 -------------------------------
12860 -- Valid_Operator_Definition --
12861 -------------------------------
12862
12863 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12864 N : Integer := 0;
12865 F : Entity_Id;
12866 Id : constant Name_Id := Chars (Designator);
12867 N_OK : Boolean;
12868
12869 begin
12870 F := First_Formal (Designator);
12871 while Present (F) loop
12872 N := N + 1;
12873
12874 if Present (Default_Value (F)) then
12875 Error_Msg_N
12876 ("default values not allowed for operator parameters",
12877 Parent (F));
12878
12879 -- For function instantiations that are operators, we must check
12880 -- separately that the corresponding generic only has in-parameters.
12881 -- For subprogram declarations this is done in Set_Formal_Mode. Such
12882 -- an error could not arise in earlier versions of the language.
12883
12884 elsif Ekind (F) /= E_In_Parameter then
12885 Error_Msg_N ("operators can only have IN parameters", F);
12886 end if;
12887
12888 Next_Formal (F);
12889 end loop;
12890
12891 -- Verify that user-defined operators have proper number of arguments
12892 -- First case of operators which can only be unary
12893
12894 if Id in Name_Op_Not | Name_Op_Abs then
12895 N_OK := (N = 1);
12896
12897 -- Case of operators which can be unary or binary
12898
12899 elsif Id in Name_Op_Add | Name_Op_Subtract then
12900 N_OK := (N in 1 .. 2);
12901
12902 -- All other operators can only be binary
12903
12904 else
12905 N_OK := (N = 2);
12906 end if;
12907
12908 if not N_OK then
12909 Error_Msg_N
12910 ("incorrect number of arguments for operator", Designator);
12911 end if;
12912
12913 if Id = Name_Op_Ne
12914 and then Base_Type (Etype (Designator)) = Standard_Boolean
12915 and then not Is_Intrinsic_Subprogram (Designator)
12916 then
12917 Error_Msg_N
12918 ("explicit definition of inequality not allowed", Designator);
12919 end if;
12920 end Valid_Operator_Definition;
12921
12922 end Sem_Ch6;