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