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