]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ada/sem_ch6.adb
[multiple changes]
[thirdparty/gcc.git] / gcc / ada / sem_ch6.adb
CommitLineData
996ae0b0
RK
1------------------------------------------------------------------------------
2-- --
3-- GNAT COMPILER COMPONENTS --
4-- --
5-- S E M _ C H 6 --
6-- --
7-- B o d y --
996ae0b0 8-- --
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
2512 -- nested bodies. This is now unecessary.
07fc65c4 2513
84f4072a
JM
2514 -- Look ahead to recognize a pragma inline that appears after the body
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
fbf5a39b
AC
5506 if Ctype = Fully_Conformant then
5507
5508 -- Names must match. Error message is more accurate if we do
5509 -- this before checking that the types of the formals match.
5510
5511 if Chars (Old_Formal) /= Chars (New_Formal) then
5d37ba92 5512 Conformance_Error ("\name & does not match!", New_Formal);
fbf5a39b
AC
5513
5514 -- Set error posted flag on new formal as well to stop
5515 -- junk cascaded messages in some cases.
5516
5517 Set_Error_Posted (New_Formal);
5518 return;
5519 end if;
40b93859
RD
5520
5521 -- Null exclusion must match
5522
5523 if Null_Exclusion_Present (Parent (Old_Formal))
5524 /=
5525 Null_Exclusion_Present (Parent (New_Formal))
5526 then
5527 -- Only give error if both come from source. This should be
5528 -- investigated some time, since it should not be needed ???
5529
5530 if Comes_From_Source (Old_Formal)
5531 and then
5532 Comes_From_Source (New_Formal)
5533 then
5534 Conformance_Error
5535 ("\null exclusion for & does not match", New_Formal);
5536
5537 -- Mark error posted on the new formal to avoid duplicated
5538 -- complaint about types not matching.
5539
5540 Set_Error_Posted (New_Formal);
5541 end if;
5542 end if;
fbf5a39b 5543 end if;
996ae0b0 5544
ec4867fa
ES
5545 -- Ada 2005 (AI-423): Possible access [sub]type and itype match. This
5546 -- case occurs whenever a subprogram is being renamed and one of its
5547 -- parameters imposes a null exclusion. For example:
5548
5549 -- type T is null record;
5550 -- type Acc_T is access T;
5551 -- subtype Acc_T_Sub is Acc_T;
5552
5553 -- procedure P (Obj : not null Acc_T_Sub); -- itype
5554 -- procedure Ren_P (Obj : Acc_T_Sub) -- subtype
5555 -- renames P;
5556
5557 Old_Formal_Base := Etype (Old_Formal);
5558 New_Formal_Base := Etype (New_Formal);
5559
5560 if Get_Inst then
5561 Old_Formal_Base := Get_Instance_Of (Old_Formal_Base);
5562 New_Formal_Base := Get_Instance_Of (New_Formal_Base);
5563 end if;
5564
0791fbe9 5565 Access_Types_Match := Ada_Version >= Ada_2005
ec4867fa
ES
5566
5567 -- Ensure that this rule is only applied when New_Id is a
5d37ba92 5568 -- renaming of Old_Id.
ec4867fa 5569
5d37ba92
ES
5570 and then Nkind (Parent (Parent (New_Id))) =
5571 N_Subprogram_Renaming_Declaration
ec4867fa
ES
5572 and then Nkind (Name (Parent (Parent (New_Id)))) in N_Has_Entity
5573 and then Present (Entity (Name (Parent (Parent (New_Id)))))
5574 and then Entity (Name (Parent (Parent (New_Id)))) = Old_Id
5575
5576 -- Now handle the allowed access-type case
5577
5578 and then Is_Access_Type (Old_Formal_Base)
5579 and then Is_Access_Type (New_Formal_Base)
5d37ba92
ES
5580
5581 -- The type kinds must match. The only exception occurs with
5582 -- multiple generics of the form:
5583
5584 -- generic generic
5585 -- type F is private; type A is private;
5586 -- type F_Ptr is access F; type A_Ptr is access A;
5587 -- with proc F_P (X : F_Ptr); with proc A_P (X : A_Ptr);
5588 -- package F_Pack is ... package A_Pack is
5589 -- package F_Inst is
5590 -- new F_Pack (A, A_Ptr, A_P);
5591
5592 -- When checking for conformance between the parameters of A_P
5593 -- and F_P, the type kinds of F_Ptr and A_Ptr will not match
5594 -- because the compiler has transformed A_Ptr into a subtype of
5595 -- F_Ptr. We catch this case in the code below.
5596
5597 and then (Ekind (Old_Formal_Base) = Ekind (New_Formal_Base)
5598 or else
5599 (Is_Generic_Type (Old_Formal_Base)
5600 and then Is_Generic_Type (New_Formal_Base)
5601 and then Is_Internal (New_Formal_Base)
5602 and then Etype (Etype (New_Formal_Base)) =
5603 Old_Formal_Base))
ec4867fa
ES
5604 and then Directly_Designated_Type (Old_Formal_Base) =
5605 Directly_Designated_Type (New_Formal_Base)
5606 and then ((Is_Itype (Old_Formal_Base)
5607 and then Can_Never_Be_Null (Old_Formal_Base))
5608 or else
5609 (Is_Itype (New_Formal_Base)
5610 and then Can_Never_Be_Null (New_Formal_Base)));
5611
996ae0b0
RK
5612 -- Types must always match. In the visible part of an instance,
5613 -- usual overloading rules for dispatching operations apply, and
5614 -- we check base types (not the actual subtypes).
5615
5616 if In_Instance_Visible_Part
5617 and then Is_Dispatching_Operation (New_Id)
5618 then
5619 if not Conforming_Types
ec4867fa
ES
5620 (T1 => Base_Type (Etype (Old_Formal)),
5621 T2 => Base_Type (Etype (New_Formal)),
5622 Ctype => Ctype,
5623 Get_Inst => Get_Inst)
5624 and then not Access_Types_Match
996ae0b0 5625 then
5d37ba92 5626 Conformance_Error ("\type of & does not match!", New_Formal);
996ae0b0
RK
5627 return;
5628 end if;
5629
5630 elsif not Conforming_Types
5d37ba92
ES
5631 (T1 => Old_Formal_Base,
5632 T2 => New_Formal_Base,
ec4867fa
ES
5633 Ctype => Ctype,
5634 Get_Inst => Get_Inst)
5635 and then not Access_Types_Match
996ae0b0 5636 then
c27f2f15
RD
5637 -- Don't give error message if old type is Any_Type. This test
5638 -- avoids some cascaded errors, e.g. in case of a bad spec.
5639
5640 if Errmsg and then Old_Formal_Base = Any_Type then
5641 Conforms := False;
5642 else
5643 Conformance_Error ("\type of & does not match!", New_Formal);
5644 end if;
5645
996ae0b0
RK
5646 return;
5647 end if;
5648
5649 -- For mode conformance, mode must match
5650
5d37ba92
ES
5651 if Ctype >= Mode_Conformant then
5652 if Parameter_Mode (Old_Formal) /= Parameter_Mode (New_Formal) then
dd54644b
JM
5653 if not Ekind_In (New_Id, E_Function, E_Procedure)
5654 or else not Is_Primitive_Wrapper (New_Id)
5655 then
5656 Conformance_Error ("\mode of & does not match!", New_Formal);
c199ccf7 5657
dd54644b
JM
5658 else
5659 declare
c199ccf7 5660 T : constant Entity_Id := Find_Dispatching_Type (New_Id);
dd54644b
JM
5661 begin
5662 if Is_Protected_Type
5663 (Corresponding_Concurrent_Type (T))
5664 then
5665 Error_Msg_PT (T, New_Id);
5666 else
5667 Conformance_Error
5668 ("\mode of & does not match!", New_Formal);
5669 end if;
5670 end;
5671 end if;
5672
5d37ba92
ES
5673 return;
5674
5675 -- Part of mode conformance for access types is having the same
5676 -- constant modifier.
5677
5678 elsif Access_Types_Match
5679 and then Is_Access_Constant (Old_Formal_Base) /=
5680 Is_Access_Constant (New_Formal_Base)
5681 then
5682 Conformance_Error
5683 ("\constant modifier does not match!", New_Formal);
5684 return;
5685 end if;
996ae0b0
RK
5686 end if;
5687
0a36105d 5688 if Ctype >= Subtype_Conformant then
996ae0b0 5689
0a36105d
JM
5690 -- Ada 2005 (AI-231): In case of anonymous access types check
5691 -- the null-exclusion and access-to-constant attributes must
c7b9d548
AC
5692 -- match. For null exclusion, we test the types rather than the
5693 -- formals themselves, since the attribute is only set reliably
5694 -- on the formals in the Ada 95 case, and we exclude the case
5695 -- where Old_Formal is marked as controlling, to avoid errors
5696 -- when matching completing bodies with dispatching declarations
5697 -- (access formals in the bodies aren't marked Can_Never_Be_Null).
996ae0b0 5698
0791fbe9 5699 if Ada_Version >= Ada_2005
0a36105d
JM
5700 and then Ekind (Etype (Old_Formal)) = E_Anonymous_Access_Type
5701 and then Ekind (Etype (New_Formal)) = E_Anonymous_Access_Type
5702 and then
c7b9d548
AC
5703 ((Can_Never_Be_Null (Etype (Old_Formal)) /=
5704 Can_Never_Be_Null (Etype (New_Formal))
5705 and then
5706 not Is_Controlling_Formal (Old_Formal))
0a36105d
JM
5707 or else
5708 Is_Access_Constant (Etype (Old_Formal)) /=
5709 Is_Access_Constant (Etype (New_Formal)))
40b93859
RD
5710
5711 -- Do not complain if error already posted on New_Formal. This
5712 -- avoids some redundant error messages.
5713
5714 and then not Error_Posted (New_Formal)
0a36105d
JM
5715 then
5716 -- It is allowed to omit the null-exclusion in case of stream
5717 -- attribute subprograms. We recognize stream subprograms
5718 -- through their TSS-generated suffix.
996ae0b0 5719
0a36105d
JM
5720 declare
5721 TSS_Name : constant TSS_Name_Type := Get_TSS_Name (New_Id);
5722 begin
5723 if TSS_Name /= TSS_Stream_Read
5724 and then TSS_Name /= TSS_Stream_Write
5725 and then TSS_Name /= TSS_Stream_Input
5726 and then TSS_Name /= TSS_Stream_Output
5727 then
5728 Conformance_Error
5d37ba92 5729 ("\type of & does not match!", New_Formal);
0a36105d
JM
5730 return;
5731 end if;
5732 end;
5733 end if;
5734 end if;
41251c60 5735
0a36105d 5736 -- Full conformance checks
41251c60 5737
0a36105d 5738 if Ctype = Fully_Conformant then
e660dbf7 5739
0a36105d 5740 -- We have checked already that names match
e660dbf7 5741
0a36105d 5742 if Parameter_Mode (Old_Formal) = E_In_Parameter then
41251c60
JM
5743
5744 -- Check default expressions for in parameters
5745
996ae0b0
RK
5746 declare
5747 NewD : constant Boolean :=
5748 Present (Default_Value (New_Formal));
5749 OldD : constant Boolean :=
5750 Present (Default_Value (Old_Formal));
5751 begin
5752 if NewD or OldD then
5753
82c80734
RD
5754 -- The old default value has been analyzed because the
5755 -- current full declaration will have frozen everything
0a36105d
JM
5756 -- before. The new default value has not been analyzed,
5757 -- so analyze it now before we check for conformance.
996ae0b0
RK
5758
5759 if NewD then
0a36105d 5760 Push_Scope (New_Id);
21d27997 5761 Preanalyze_Spec_Expression
fbf5a39b 5762 (Default_Value (New_Formal), Etype (New_Formal));
996ae0b0
RK
5763 End_Scope;
5764 end if;
5765
5766 if not (NewD and OldD)
5767 or else not Fully_Conformant_Expressions
5768 (Default_Value (Old_Formal),
5769 Default_Value (New_Formal))
5770 then
5771 Conformance_Error
5d37ba92 5772 ("\default expression for & does not match!",
996ae0b0
RK
5773 New_Formal);
5774 return;
5775 end if;
5776 end if;
5777 end;
5778 end if;
5779 end if;
5780
5781 -- A couple of special checks for Ada 83 mode. These checks are
0a36105d 5782 -- skipped if either entity is an operator in package Standard,
996ae0b0
RK
5783 -- or if either old or new instance is not from the source program.
5784
0ab80019 5785 if Ada_Version = Ada_83
996ae0b0
RK
5786 and then Sloc (Old_Id) > Standard_Location
5787 and then Sloc (New_Id) > Standard_Location
5788 and then Comes_From_Source (Old_Id)
5789 and then Comes_From_Source (New_Id)
5790 then
5791 declare
5792 Old_Param : constant Node_Id := Declaration_Node (Old_Formal);
5793 New_Param : constant Node_Id := Declaration_Node (New_Formal);
5794
5795 begin
5796 -- Explicit IN must be present or absent in both cases. This
5797 -- test is required only in the full conformance case.
5798
5799 if In_Present (Old_Param) /= In_Present (New_Param)
5800 and then Ctype = Fully_Conformant
5801 then
5802 Conformance_Error
5d37ba92 5803 ("\(Ada 83) IN must appear in both declarations",
996ae0b0
RK
5804 New_Formal);
5805 return;
5806 end if;
5807
5808 -- Grouping (use of comma in param lists) must be the same
5809 -- This is where we catch a misconformance like:
5810
0a36105d 5811 -- A, B : Integer
996ae0b0
RK
5812 -- A : Integer; B : Integer
5813
5814 -- which are represented identically in the tree except
5815 -- for the setting of the flags More_Ids and Prev_Ids.
5816
5817 if More_Ids (Old_Param) /= More_Ids (New_Param)
5818 or else Prev_Ids (Old_Param) /= Prev_Ids (New_Param)
5819 then
5820 Conformance_Error
5d37ba92 5821 ("\grouping of & does not match!", New_Formal);
996ae0b0
RK
5822 return;
5823 end if;
5824 end;
5825 end if;
5826
41251c60
JM
5827 -- This label is required when skipping controlling formals
5828
5829 <<Skip_Controlling_Formal>>
5830
996ae0b0
RK
5831 Next_Formal (Old_Formal);
5832 Next_Formal (New_Formal);
5833 end loop;
5834
5835 if Present (Old_Formal) then
5d37ba92 5836 Conformance_Error ("\too few parameters!");
996ae0b0
RK
5837 return;
5838
5839 elsif Present (New_Formal) then
5d37ba92 5840 Conformance_Error ("\too many parameters!", New_Formal);
996ae0b0
RK
5841 return;
5842 end if;
996ae0b0
RK
5843 end Check_Conformance;
5844
ec4867fa
ES
5845 -----------------------
5846 -- Check_Conventions --
5847 -----------------------
5848
5849 procedure Check_Conventions (Typ : Entity_Id) is
ce2b6ba5 5850 Ifaces_List : Elist_Id;
0a36105d 5851
ce2b6ba5 5852 procedure Check_Convention (Op : Entity_Id);
0a36105d
JM
5853 -- Verify that the convention of inherited dispatching operation Op is
5854 -- consistent among all subprograms it overrides. In order to minimize
5855 -- the search, Search_From is utilized to designate a specific point in
5856 -- the list rather than iterating over the whole list once more.
ec4867fa
ES
5857
5858 ----------------------
5859 -- Check_Convention --
5860 ----------------------
5861
ce2b6ba5
JM
5862 procedure Check_Convention (Op : Entity_Id) is
5863 Iface_Elmt : Elmt_Id;
5864 Iface_Prim_Elmt : Elmt_Id;
5865 Iface_Prim : Entity_Id;
ec4867fa 5866
ce2b6ba5
JM
5867 begin
5868 Iface_Elmt := First_Elmt (Ifaces_List);
5869 while Present (Iface_Elmt) loop
5870 Iface_Prim_Elmt :=
5871 First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
5872 while Present (Iface_Prim_Elmt) loop
5873 Iface_Prim := Node (Iface_Prim_Elmt);
5874
5875 if Is_Interface_Conformant (Typ, Iface_Prim, Op)
5876 and then Convention (Iface_Prim) /= Convention (Op)
5877 then
ed2233dc 5878 Error_Msg_N
ce2b6ba5 5879 ("inconsistent conventions in primitive operations", Typ);
ec4867fa 5880
ce2b6ba5
JM
5881 Error_Msg_Name_1 := Chars (Op);
5882 Error_Msg_Name_2 := Get_Convention_Name (Convention (Op));
5883 Error_Msg_Sloc := Sloc (Op);
ec4867fa 5884
7a963087 5885 if Comes_From_Source (Op) or else No (Alias (Op)) then
038140ed 5886 if not Present (Overridden_Operation (Op)) then
ed2233dc 5887 Error_Msg_N ("\\primitive % defined #", Typ);
ce2b6ba5 5888 else
ed2233dc 5889 Error_Msg_N
19d846a0
RD
5890 ("\\overriding operation % with " &
5891 "convention % defined #", Typ);
ce2b6ba5 5892 end if;
ec4867fa 5893
ce2b6ba5
JM
5894 else pragma Assert (Present (Alias (Op)));
5895 Error_Msg_Sloc := Sloc (Alias (Op));
ed2233dc 5896 Error_Msg_N
19d846a0
RD
5897 ("\\inherited operation % with " &
5898 "convention % defined #", Typ);
ce2b6ba5 5899 end if;
ec4867fa 5900
ce2b6ba5
JM
5901 Error_Msg_Name_1 := Chars (Op);
5902 Error_Msg_Name_2 :=
5903 Get_Convention_Name (Convention (Iface_Prim));
5904 Error_Msg_Sloc := Sloc (Iface_Prim);
ed2233dc 5905 Error_Msg_N
19d846a0
RD
5906 ("\\overridden operation % with " &
5907 "convention % defined #", Typ);
ec4867fa 5908
ce2b6ba5 5909 -- Avoid cascading errors
ec4867fa 5910
ce2b6ba5
JM
5911 return;
5912 end if;
ec4867fa 5913
ce2b6ba5
JM
5914 Next_Elmt (Iface_Prim_Elmt);
5915 end loop;
ec4867fa 5916
ce2b6ba5 5917 Next_Elmt (Iface_Elmt);
ec4867fa
ES
5918 end loop;
5919 end Check_Convention;
5920
5921 -- Local variables
5922
5923 Prim_Op : Entity_Id;
5924 Prim_Op_Elmt : Elmt_Id;
5925
5926 -- Start of processing for Check_Conventions
5927
5928 begin
ce2b6ba5
JM
5929 if not Has_Interfaces (Typ) then
5930 return;
5931 end if;
5932
5933 Collect_Interfaces (Typ, Ifaces_List);
5934
0a36105d
JM
5935 -- The algorithm checks every overriding dispatching operation against
5936 -- all the corresponding overridden dispatching operations, detecting
f3d57416 5937 -- differences in conventions.
ec4867fa
ES
5938
5939 Prim_Op_Elmt := First_Elmt (Primitive_Operations (Typ));
5940 while Present (Prim_Op_Elmt) loop
5941 Prim_Op := Node (Prim_Op_Elmt);
5942
0a36105d 5943 -- A small optimization: skip the predefined dispatching operations
ce2b6ba5 5944 -- since they always have the same convention.
ec4867fa 5945
ce2b6ba5
JM
5946 if not Is_Predefined_Dispatching_Operation (Prim_Op) then
5947 Check_Convention (Prim_Op);
ec4867fa
ES
5948 end if;
5949
5950 Next_Elmt (Prim_Op_Elmt);
5951 end loop;
5952 end Check_Conventions;
5953
996ae0b0
RK
5954 ------------------------------
5955 -- Check_Delayed_Subprogram --
5956 ------------------------------
5957
5958 procedure Check_Delayed_Subprogram (Designator : Entity_Id) is
5959 F : Entity_Id;
5960
5961 procedure Possible_Freeze (T : Entity_Id);
5962 -- T is the type of either a formal parameter or of the return type.
5963 -- If T is not yet frozen and needs a delayed freeze, then the
4a13695c
AC
5964 -- subprogram itself must be delayed. If T is the limited view of an
5965 -- incomplete type the subprogram must be frozen as well, because
5966 -- T may depend on local types that have not been frozen yet.
996ae0b0 5967
82c80734
RD
5968 ---------------------
5969 -- Possible_Freeze --
5970 ---------------------
5971
996ae0b0
RK
5972 procedure Possible_Freeze (T : Entity_Id) is
5973 begin
4a13695c 5974 if Has_Delayed_Freeze (T) and then not Is_Frozen (T) then
996ae0b0
RK
5975 Set_Has_Delayed_Freeze (Designator);
5976
5977 elsif Is_Access_Type (T)
5978 and then Has_Delayed_Freeze (Designated_Type (T))
5979 and then not Is_Frozen (Designated_Type (T))
5980 then
5981 Set_Has_Delayed_Freeze (Designator);
e358346d 5982
4a13695c 5983 elsif Ekind (T) = E_Incomplete_Type and then From_With_Type (T) then
e358346d 5984 Set_Has_Delayed_Freeze (Designator);
406935b6 5985
9aff36e9
RD
5986 -- AI05-0151: In Ada 2012, Incomplete types can appear in the profile
5987 -- of a subprogram or entry declaration.
406935b6
AC
5988
5989 elsif Ekind (T) = E_Incomplete_Type
5990 and then Ada_Version >= Ada_2012
5991 then
5992 Set_Has_Delayed_Freeze (Designator);
996ae0b0 5993 end if;
4a13695c 5994
996ae0b0
RK
5995 end Possible_Freeze;
5996
5997 -- Start of processing for Check_Delayed_Subprogram
5998
5999 begin
76e3504f
AC
6000 -- All subprograms, including abstract subprograms, may need a freeze
6001 -- node if some formal type or the return type needs one.
996ae0b0 6002
76e3504f
AC
6003 Possible_Freeze (Etype (Designator));
6004 Possible_Freeze (Base_Type (Etype (Designator))); -- needed ???
996ae0b0 6005
76e3504f
AC
6006 -- Need delayed freeze if any of the formal types themselves need
6007 -- a delayed freeze and are not yet frozen.
996ae0b0 6008
76e3504f
AC
6009 F := First_Formal (Designator);
6010 while Present (F) loop
6011 Possible_Freeze (Etype (F));
6012 Possible_Freeze (Base_Type (Etype (F))); -- needed ???
6013 Next_Formal (F);
6014 end loop;
996ae0b0
RK
6015
6016 -- Mark functions that return by reference. Note that it cannot be
6017 -- done for delayed_freeze subprograms because the underlying
6018 -- returned type may not be known yet (for private types)
6019
6020 if not Has_Delayed_Freeze (Designator)
6021 and then Expander_Active
6022 then
6023 declare
6024 Typ : constant Entity_Id := Etype (Designator);
6025 Utyp : constant Entity_Id := Underlying_Type (Typ);
9694c039 6026
996ae0b0 6027 begin
40f07b4b 6028 if Is_Immutably_Limited_Type (Typ) then
996ae0b0 6029 Set_Returns_By_Ref (Designator);
9694c039 6030
048e5cef 6031 elsif Present (Utyp) and then CW_Or_Has_Controlled_Part (Utyp) then
996ae0b0
RK
6032 Set_Returns_By_Ref (Designator);
6033 end if;
6034 end;
6035 end if;
6036 end Check_Delayed_Subprogram;
6037
6038 ------------------------------------
6039 -- Check_Discriminant_Conformance --
6040 ------------------------------------
6041
6042 procedure Check_Discriminant_Conformance
6043 (N : Node_Id;
6044 Prev : Entity_Id;
6045 Prev_Loc : Node_Id)
6046 is
6047 Old_Discr : Entity_Id := First_Discriminant (Prev);
6048 New_Discr : Node_Id := First (Discriminant_Specifications (N));
6049 New_Discr_Id : Entity_Id;
6050 New_Discr_Type : Entity_Id;
6051
6052 procedure Conformance_Error (Msg : String; N : Node_Id);
82c80734
RD
6053 -- Post error message for conformance error on given node. Two messages
6054 -- are output. The first points to the previous declaration with a
6055 -- general "no conformance" message. The second is the detailed reason,
6056 -- supplied as Msg. The parameter N provide information for a possible
6057 -- & insertion in the message.
996ae0b0
RK
6058
6059 -----------------------
6060 -- Conformance_Error --
6061 -----------------------
6062
6063 procedure Conformance_Error (Msg : String; N : Node_Id) is
6064 begin
6065 Error_Msg_Sloc := Sloc (Prev_Loc);
483c78cb
RD
6066 Error_Msg_N -- CODEFIX
6067 ("not fully conformant with declaration#!", N);
996ae0b0
RK
6068 Error_Msg_NE (Msg, N, N);
6069 end Conformance_Error;
6070
6071 -- Start of processing for Check_Discriminant_Conformance
6072
6073 begin
6074 while Present (Old_Discr) and then Present (New_Discr) loop
6075
6076 New_Discr_Id := Defining_Identifier (New_Discr);
6077
82c80734
RD
6078 -- The subtype mark of the discriminant on the full type has not
6079 -- been analyzed so we do it here. For an access discriminant a new
6080 -- type is created.
996ae0b0
RK
6081
6082 if Nkind (Discriminant_Type (New_Discr)) = N_Access_Definition then
6083 New_Discr_Type :=
6084 Access_Definition (N, Discriminant_Type (New_Discr));
6085
6086 else
6087 Analyze (Discriminant_Type (New_Discr));
6088 New_Discr_Type := Etype (Discriminant_Type (New_Discr));
e50e1c5e
AC
6089
6090 -- Ada 2005: if the discriminant definition carries a null
6091 -- exclusion, create an itype to check properly for consistency
6092 -- with partial declaration.
6093
6094 if Is_Access_Type (New_Discr_Type)
6095 and then Null_Exclusion_Present (New_Discr)
6096 then
6097 New_Discr_Type :=
6098 Create_Null_Excluding_Itype
6099 (T => New_Discr_Type,
6100 Related_Nod => New_Discr,
6101 Scope_Id => Current_Scope);
6102 end if;
996ae0b0
RK
6103 end if;
6104
6105 if not Conforming_Types
6106 (Etype (Old_Discr), New_Discr_Type, Fully_Conformant)
6107 then
6108 Conformance_Error ("type of & does not match!", New_Discr_Id);
6109 return;
fbf5a39b 6110 else
82c80734
RD
6111 -- Treat the new discriminant as an occurrence of the old one,
6112 -- for navigation purposes, and fill in some semantic
fbf5a39b
AC
6113 -- information, for completeness.
6114
6115 Generate_Reference (Old_Discr, New_Discr_Id, 'r');
6116 Set_Etype (New_Discr_Id, Etype (Old_Discr));
6117 Set_Scope (New_Discr_Id, Scope (Old_Discr));
996ae0b0
RK
6118 end if;
6119
6120 -- Names must match
6121
6122 if Chars (Old_Discr) /= Chars (Defining_Identifier (New_Discr)) then
6123 Conformance_Error ("name & does not match!", New_Discr_Id);
6124 return;
6125 end if;
6126
6127 -- Default expressions must match
6128
6129 declare
6130 NewD : constant Boolean :=
6131 Present (Expression (New_Discr));
6132 OldD : constant Boolean :=
6133 Present (Expression (Parent (Old_Discr)));
6134
6135 begin
6136 if NewD or OldD then
6137
6138 -- The old default value has been analyzed and expanded,
6139 -- because the current full declaration will have frozen
82c80734
RD
6140 -- everything before. The new default values have not been
6141 -- expanded, so expand now to check conformance.
996ae0b0
RK
6142
6143 if NewD then
21d27997 6144 Preanalyze_Spec_Expression
996ae0b0
RK
6145 (Expression (New_Discr), New_Discr_Type);
6146 end if;
6147
6148 if not (NewD and OldD)
6149 or else not Fully_Conformant_Expressions
6150 (Expression (Parent (Old_Discr)),
6151 Expression (New_Discr))
6152
6153 then
6154 Conformance_Error
6155 ("default expression for & does not match!",
6156 New_Discr_Id);
6157 return;
6158 end if;
6159 end if;
6160 end;
6161
6162 -- In Ada 83 case, grouping must match: (A,B : X) /= (A : X; B : X)
6163
0ab80019 6164 if Ada_Version = Ada_83 then
996ae0b0
RK
6165 declare
6166 Old_Disc : constant Node_Id := Declaration_Node (Old_Discr);
6167
6168 begin
6169 -- Grouping (use of comma in param lists) must be the same
6170 -- This is where we catch a misconformance like:
6171
60370fb1 6172 -- A, B : Integer
996ae0b0
RK
6173 -- A : Integer; B : Integer
6174
6175 -- which are represented identically in the tree except
6176 -- for the setting of the flags More_Ids and Prev_Ids.
6177
6178 if More_Ids (Old_Disc) /= More_Ids (New_Discr)
6179 or else Prev_Ids (Old_Disc) /= Prev_Ids (New_Discr)
6180 then
6181 Conformance_Error
6182 ("grouping of & does not match!", New_Discr_Id);
6183 return;
6184 end if;
6185 end;
6186 end if;
6187
6188 Next_Discriminant (Old_Discr);
6189 Next (New_Discr);
6190 end loop;
6191
6192 if Present (Old_Discr) then
6193 Conformance_Error ("too few discriminants!", Defining_Identifier (N));
6194 return;
6195
6196 elsif Present (New_Discr) then
6197 Conformance_Error
6198 ("too many discriminants!", Defining_Identifier (New_Discr));
6199 return;
6200 end if;
6201 end Check_Discriminant_Conformance;
6202
6203 ----------------------------
6204 -- Check_Fully_Conformant --
6205 ----------------------------
6206
6207 procedure Check_Fully_Conformant
6208 (New_Id : Entity_Id;
6209 Old_Id : Entity_Id;
6210 Err_Loc : Node_Id := Empty)
6211 is
6212 Result : Boolean;
81db9d77 6213 pragma Warnings (Off, Result);
996ae0b0
RK
6214 begin
6215 Check_Conformance
6216 (New_Id, Old_Id, Fully_Conformant, True, Result, Err_Loc);
6217 end Check_Fully_Conformant;
6218
6219 ---------------------------
6220 -- Check_Mode_Conformant --
6221 ---------------------------
6222
6223 procedure Check_Mode_Conformant
6224 (New_Id : Entity_Id;
6225 Old_Id : Entity_Id;
6226 Err_Loc : Node_Id := Empty;
6227 Get_Inst : Boolean := False)
6228 is
6229 Result : Boolean;
81db9d77 6230 pragma Warnings (Off, Result);
996ae0b0
RK
6231 begin
6232 Check_Conformance
6233 (New_Id, Old_Id, Mode_Conformant, True, Result, Err_Loc, Get_Inst);
6234 end Check_Mode_Conformant;
6235
fbf5a39b 6236 --------------------------------
758c442c 6237 -- Check_Overriding_Indicator --
fbf5a39b
AC
6238 --------------------------------
6239
758c442c 6240 procedure Check_Overriding_Indicator
ec4867fa 6241 (Subp : Entity_Id;
5d37ba92
ES
6242 Overridden_Subp : Entity_Id;
6243 Is_Primitive : Boolean)
fbf5a39b 6244 is
758c442c
GD
6245 Decl : Node_Id;
6246 Spec : Node_Id;
fbf5a39b
AC
6247
6248 begin
ec4867fa 6249 -- No overriding indicator for literals
fbf5a39b 6250
ec4867fa 6251 if Ekind (Subp) = E_Enumeration_Literal then
758c442c 6252 return;
fbf5a39b 6253
ec4867fa
ES
6254 elsif Ekind (Subp) = E_Entry then
6255 Decl := Parent (Subp);
6256
53b10ce9
AC
6257 -- No point in analyzing a malformed operator
6258
6259 elsif Nkind (Subp) = N_Defining_Operator_Symbol
6260 and then Error_Posted (Subp)
6261 then
6262 return;
6263
758c442c
GD
6264 else
6265 Decl := Unit_Declaration_Node (Subp);
6266 end if;
fbf5a39b 6267
800621e0
RD
6268 if Nkind_In (Decl, N_Subprogram_Body,
6269 N_Subprogram_Body_Stub,
6270 N_Subprogram_Declaration,
6271 N_Abstract_Subprogram_Declaration,
6272 N_Subprogram_Renaming_Declaration)
758c442c
GD
6273 then
6274 Spec := Specification (Decl);
ec4867fa
ES
6275
6276 elsif Nkind (Decl) = N_Entry_Declaration then
6277 Spec := Decl;
6278
758c442c
GD
6279 else
6280 return;
6281 end if;
fbf5a39b 6282
e7d72fb9
AC
6283 -- The overriding operation is type conformant with the overridden one,
6284 -- but the names of the formals are not required to match. If the names
6823270c 6285 -- appear permuted in the overriding operation, this is a possible
e7d72fb9
AC
6286 -- source of confusion that is worth diagnosing. Controlling formals
6287 -- often carry names that reflect the type, and it is not worthwhile
6288 -- requiring that their names match.
6289
c9e7bd8e 6290 if Present (Overridden_Subp)
e7d72fb9
AC
6291 and then Nkind (Subp) /= N_Defining_Operator_Symbol
6292 then
6293 declare
6294 Form1 : Entity_Id;
6295 Form2 : Entity_Id;
6296
6297 begin
6298 Form1 := First_Formal (Subp);
6299 Form2 := First_Formal (Overridden_Subp);
6300
c9e7bd8e
AC
6301 -- If the overriding operation is a synchronized operation, skip
6302 -- the first parameter of the overridden operation, which is
6823270c
AC
6303 -- implicit in the new one. If the operation is declared in the
6304 -- body it is not primitive and all formals must match.
c9e7bd8e 6305
6823270c
AC
6306 if Is_Concurrent_Type (Scope (Subp))
6307 and then Is_Tagged_Type (Scope (Subp))
6308 and then not Has_Completion (Scope (Subp))
6309 then
c9e7bd8e
AC
6310 Form2 := Next_Formal (Form2);
6311 end if;
6312
e7d72fb9
AC
6313 if Present (Form1) then
6314 Form1 := Next_Formal (Form1);
6315 Form2 := Next_Formal (Form2);
6316 end if;
6317
6318 while Present (Form1) loop
6319 if not Is_Controlling_Formal (Form1)
6320 and then Present (Next_Formal (Form2))
6321 and then Chars (Form1) = Chars (Next_Formal (Form2))
6322 then
6323 Error_Msg_Node_2 := Alias (Overridden_Subp);
6324 Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
ed2233dc 6325 Error_Msg_NE
19d846a0 6326 ("& does not match corresponding formal of&#",
e7d72fb9
AC
6327 Form1, Form1);
6328 exit;
6329 end if;
6330
6331 Next_Formal (Form1);
6332 Next_Formal (Form2);
6333 end loop;
6334 end;
6335 end if;
6336
676e8420
AC
6337 -- If there is an overridden subprogram, then check that there is no
6338 -- "not overriding" indicator, and mark the subprogram as overriding.
51bf9bdf
AC
6339 -- This is not done if the overridden subprogram is marked as hidden,
6340 -- which can occur for the case of inherited controlled operations
6341 -- (see Derive_Subprogram), unless the inherited subprogram's parent
6342 -- subprogram is not itself hidden. (Note: This condition could probably
6343 -- be simplified, leaving out the testing for the specific controlled
6344 -- cases, but it seems safer and clearer this way, and echoes similar
6345 -- special-case tests of this kind in other places.)
6346
fd0d899b 6347 if Present (Overridden_Subp)
51bf9bdf
AC
6348 and then (not Is_Hidden (Overridden_Subp)
6349 or else
6350 ((Chars (Overridden_Subp) = Name_Initialize
f0709ca6
AC
6351 or else
6352 Chars (Overridden_Subp) = Name_Adjust
6353 or else
6354 Chars (Overridden_Subp) = Name_Finalize)
6355 and then Present (Alias (Overridden_Subp))
6356 and then not Is_Hidden (Alias (Overridden_Subp))))
fd0d899b 6357 then
ec4867fa
ES
6358 if Must_Not_Override (Spec) then
6359 Error_Msg_Sloc := Sloc (Overridden_Subp);
fbf5a39b 6360
ec4867fa 6361 if Ekind (Subp) = E_Entry then
ed2233dc 6362 Error_Msg_NE
5d37ba92 6363 ("entry & overrides inherited operation #", Spec, Subp);
ec4867fa 6364 else
ed2233dc 6365 Error_Msg_NE
5d37ba92 6366 ("subprogram & overrides inherited operation #", Spec, Subp);
ec4867fa 6367 end if;
21d27997 6368
bd603506 6369 -- Special-case to fix a GNAT oddity: Limited_Controlled is declared
24a120ac
AC
6370 -- as an extension of Root_Controlled, and thus has a useless Adjust
6371 -- operation. This operation should not be inherited by other limited
6372 -- controlled types. An explicit Adjust for them is not overriding.
6373
6374 elsif Must_Override (Spec)
6375 and then Chars (Overridden_Subp) = Name_Adjust
6376 and then Is_Limited_Type (Etype (First_Formal (Subp)))
6377 and then Present (Alias (Overridden_Subp))
bd603506
RD
6378 and then
6379 Is_Predefined_File_Name
6380 (Unit_File_Name (Get_Source_Unit (Alias (Overridden_Subp))))
24a120ac
AC
6381 then
6382 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6383
21d27997 6384 elsif Is_Subprogram (Subp) then
2fe829ae
ES
6385 if Is_Init_Proc (Subp) then
6386 null;
6387
6388 elsif No (Overridden_Operation (Subp)) then
1c1289e7
AC
6389
6390 -- For entities generated by Derive_Subprograms the overridden
6391 -- operation is the inherited primitive (which is available
6392 -- through the attribute alias)
6393
6394 if (Is_Dispatching_Operation (Subp)
f9673bb0 6395 or else Is_Dispatching_Operation (Overridden_Subp))
1c1289e7 6396 and then not Comes_From_Source (Overridden_Subp)
f9673bb0
AC
6397 and then Find_Dispatching_Type (Overridden_Subp) =
6398 Find_Dispatching_Type (Subp)
1c1289e7
AC
6399 and then Present (Alias (Overridden_Subp))
6400 and then Comes_From_Source (Alias (Overridden_Subp))
6401 then
6402 Set_Overridden_Operation (Subp, Alias (Overridden_Subp));
2fe829ae 6403
1c1289e7
AC
6404 else
6405 Set_Overridden_Operation (Subp, Overridden_Subp);
6406 end if;
6407 end if;
ec4867fa 6408 end if;
f937473f 6409
618fb570
AC
6410 -- If primitive flag is set or this is a protected operation, then
6411 -- the operation is overriding at the point of its declaration, so
6412 -- warn if necessary. Otherwise it may have been declared before the
6413 -- operation it overrides and no check is required.
3c25856a
AC
6414
6415 if Style_Check
618fb570
AC
6416 and then not Must_Override (Spec)
6417 and then (Is_Primitive
6418 or else Ekind (Scope (Subp)) = E_Protected_Type)
3c25856a 6419 then
235f4375
AC
6420 Style.Missing_Overriding (Decl, Subp);
6421 end if;
6422
53b10ce9
AC
6423 -- If Subp is an operator, it may override a predefined operation, if
6424 -- it is defined in the same scope as the type to which it applies.
676e8420 6425 -- In that case Overridden_Subp is empty because of our implicit
5d37ba92
ES
6426 -- representation for predefined operators. We have to check whether the
6427 -- signature of Subp matches that of a predefined operator. Note that
6428 -- first argument provides the name of the operator, and the second
6429 -- argument the signature that may match that of a standard operation.
21d27997
RD
6430 -- If the indicator is overriding, then the operator must match a
6431 -- predefined signature, because we know already that there is no
6432 -- explicit overridden operation.
f937473f 6433
21d27997 6434 elsif Nkind (Subp) = N_Defining_Operator_Symbol then
806f6d37 6435 if Must_Not_Override (Spec) then
f937473f 6436
806f6d37
AC
6437 -- If this is not a primitive or a protected subprogram, then
6438 -- "not overriding" is illegal.
618fb570 6439
806f6d37
AC
6440 if not Is_Primitive
6441 and then Ekind (Scope (Subp)) /= E_Protected_Type
6442 then
6443 Error_Msg_N
6444 ("overriding indicator only allowed "
6445 & "if subprogram is primitive", Subp);
618fb570 6446
806f6d37
AC
6447 elsif Can_Override_Operator (Subp) then
6448 Error_Msg_NE
6449 ("subprogram& overrides predefined operator ", Spec, Subp);
6450 end if;
f937473f 6451
806f6d37
AC
6452 elsif Must_Override (Spec) then
6453 if No (Overridden_Operation (Subp))
6454 and then not Can_Override_Operator (Subp)
6455 then
6456 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
6457 end if;
5d37ba92 6458
806f6d37
AC
6459 elsif not Error_Posted (Subp)
6460 and then Style_Check
6461 and then Can_Override_Operator (Subp)
6462 and then
6463 not Is_Predefined_File_Name
6464 (Unit_File_Name (Get_Source_Unit (Subp)))
6465 then
6466 -- If style checks are enabled, indicate that the indicator is
6467 -- missing. However, at the point of declaration, the type of
6468 -- which this is a primitive operation may be private, in which
6469 -- case the indicator would be premature.
235f4375 6470
806f6d37
AC
6471 if Has_Private_Declaration (Etype (Subp))
6472 or else Has_Private_Declaration (Etype (First_Formal (Subp)))
53b10ce9 6473 then
806f6d37
AC
6474 null;
6475 else
6476 Style.Missing_Overriding (Decl, Subp);
5d5832bc 6477 end if;
806f6d37 6478 end if;
21d27997
RD
6479
6480 elsif Must_Override (Spec) then
6481 if Ekind (Subp) = E_Entry then
ed2233dc 6482 Error_Msg_NE ("entry & is not overriding", Spec, Subp);
5d37ba92 6483 else
ed2233dc 6484 Error_Msg_NE ("subprogram & is not overriding", Spec, Subp);
758c442c 6485 end if;
5d37ba92
ES
6486
6487 -- If the operation is marked "not overriding" and it's not primitive
6488 -- then an error is issued, unless this is an operation of a task or
6489 -- protected type (RM05-8.3.1(3/2-4/2)). Error cases where "overriding"
6490 -- has been specified have already been checked above.
6491
6492 elsif Must_Not_Override (Spec)
6493 and then not Is_Primitive
6494 and then Ekind (Subp) /= E_Entry
6495 and then Ekind (Scope (Subp)) /= E_Protected_Type
6496 then
ed2233dc 6497 Error_Msg_N
5d37ba92
ES
6498 ("overriding indicator only allowed if subprogram is primitive",
6499 Subp);
5d37ba92 6500 return;
fbf5a39b 6501 end if;
758c442c 6502 end Check_Overriding_Indicator;
fbf5a39b 6503
996ae0b0
RK
6504 -------------------
6505 -- Check_Returns --
6506 -------------------
6507
0a36105d
JM
6508 -- Note: this procedure needs to know far too much about how the expander
6509 -- messes with exceptions. The use of the flag Exception_Junk and the
6510 -- incorporation of knowledge of Exp_Ch11.Expand_Local_Exception_Handlers
6511 -- works, but is not very clean. It would be better if the expansion
6512 -- routines would leave Original_Node working nicely, and we could use
6513 -- Original_Node here to ignore all the peculiar expander messing ???
6514
996ae0b0
RK
6515 procedure Check_Returns
6516 (HSS : Node_Id;
6517 Mode : Character;
c8ef728f
ES
6518 Err : out Boolean;
6519 Proc : Entity_Id := Empty)
996ae0b0
RK
6520 is
6521 Handler : Node_Id;
6522
6523 procedure Check_Statement_Sequence (L : List_Id);
6524 -- Internal recursive procedure to check a list of statements for proper
6525 -- termination by a return statement (or a transfer of control or a
6526 -- compound statement that is itself internally properly terminated).
6527
6528 ------------------------------
6529 -- Check_Statement_Sequence --
6530 ------------------------------
6531
6532 procedure Check_Statement_Sequence (L : List_Id) is
6533 Last_Stm : Node_Id;
0a36105d 6534 Stm : Node_Id;
996ae0b0
RK
6535 Kind : Node_Kind;
6536
6537 Raise_Exception_Call : Boolean;
6538 -- Set True if statement sequence terminated by Raise_Exception call
6539 -- or a Reraise_Occurrence call.
6540
6541 begin
6542 Raise_Exception_Call := False;
6543
6544 -- Get last real statement
6545
6546 Last_Stm := Last (L);
6547
0a36105d
JM
6548 -- Deal with digging out exception handler statement sequences that
6549 -- have been transformed by the local raise to goto optimization.
6550 -- See Exp_Ch11.Expand_Local_Exception_Handlers for details. If this
6551 -- optimization has occurred, we are looking at something like:
6552
6553 -- begin
6554 -- original stmts in block
6555
6556 -- exception \
6557 -- when excep1 => |
6558 -- goto L1; | omitted if No_Exception_Propagation
6559 -- when excep2 => |
6560 -- goto L2; /
6561 -- end;
6562
6563 -- goto L3; -- skip handler when exception not raised
6564
6565 -- <<L1>> -- target label for local exception
6566 -- begin
6567 -- estmts1
6568 -- end;
6569
6570 -- goto L3;
6571
6572 -- <<L2>>
6573 -- begin
6574 -- estmts2
6575 -- end;
6576
6577 -- <<L3>>
6578
6579 -- and what we have to do is to dig out the estmts1 and estmts2
6580 -- sequences (which were the original sequences of statements in
6581 -- the exception handlers) and check them.
6582
6583 if Nkind (Last_Stm) = N_Label
6584 and then Exception_Junk (Last_Stm)
6585 then
6586 Stm := Last_Stm;
6587 loop
6588 Prev (Stm);
6589 exit when No (Stm);
6590 exit when Nkind (Stm) /= N_Block_Statement;
6591 exit when not Exception_Junk (Stm);
6592 Prev (Stm);
6593 exit when No (Stm);
6594 exit when Nkind (Stm) /= N_Label;
6595 exit when not Exception_Junk (Stm);
6596 Check_Statement_Sequence
6597 (Statements (Handled_Statement_Sequence (Next (Stm))));
6598
6599 Prev (Stm);
6600 Last_Stm := Stm;
6601 exit when No (Stm);
6602 exit when Nkind (Stm) /= N_Goto_Statement;
6603 exit when not Exception_Junk (Stm);
6604 end loop;
6605 end if;
6606
996ae0b0
RK
6607 -- Don't count pragmas
6608
6609 while Nkind (Last_Stm) = N_Pragma
6610
6611 -- Don't count call to SS_Release (can happen after Raise_Exception)
6612
6613 or else
6614 (Nkind (Last_Stm) = N_Procedure_Call_Statement
6615 and then
6616 Nkind (Name (Last_Stm)) = N_Identifier
6617 and then
6618 Is_RTE (Entity (Name (Last_Stm)), RE_SS_Release))
6619
6620 -- Don't count exception junk
6621
6622 or else
800621e0
RD
6623 (Nkind_In (Last_Stm, N_Goto_Statement,
6624 N_Label,
6625 N_Object_Declaration)
0a36105d
JM
6626 and then Exception_Junk (Last_Stm))
6627 or else Nkind (Last_Stm) in N_Push_xxx_Label
6628 or else Nkind (Last_Stm) in N_Pop_xxx_Label
996ae0b0
RK
6629 loop
6630 Prev (Last_Stm);
6631 end loop;
6632
6633 -- Here we have the "real" last statement
6634
6635 Kind := Nkind (Last_Stm);
6636
6637 -- Transfer of control, OK. Note that in the No_Return procedure
6638 -- case, we already diagnosed any explicit return statements, so
6639 -- we can treat them as OK in this context.
6640
6641 if Is_Transfer (Last_Stm) then
6642 return;
6643
6644 -- Check cases of explicit non-indirect procedure calls
6645
6646 elsif Kind = N_Procedure_Call_Statement
6647 and then Is_Entity_Name (Name (Last_Stm))
6648 then
6649 -- Check call to Raise_Exception procedure which is treated
6650 -- specially, as is a call to Reraise_Occurrence.
6651
6652 -- We suppress the warning in these cases since it is likely that
6653 -- the programmer really does not expect to deal with the case
6654 -- of Null_Occurrence, and thus would find a warning about a
6655 -- missing return curious, and raising Program_Error does not
6656 -- seem such a bad behavior if this does occur.
6657
c8ef728f
ES
6658 -- Note that in the Ada 2005 case for Raise_Exception, the actual
6659 -- behavior will be to raise Constraint_Error (see AI-329).
6660
996ae0b0
RK
6661 if Is_RTE (Entity (Name (Last_Stm)), RE_Raise_Exception)
6662 or else
6663 Is_RTE (Entity (Name (Last_Stm)), RE_Reraise_Occurrence)
6664 then
6665 Raise_Exception_Call := True;
6666
6667 -- For Raise_Exception call, test first argument, if it is
6668 -- an attribute reference for a 'Identity call, then we know
6669 -- that the call cannot possibly return.
6670
6671 declare
6672 Arg : constant Node_Id :=
6673 Original_Node (First_Actual (Last_Stm));
996ae0b0
RK
6674 begin
6675 if Nkind (Arg) = N_Attribute_Reference
6676 and then Attribute_Name (Arg) = Name_Identity
6677 then
6678 return;
6679 end if;
6680 end;
6681 end if;
6682
6683 -- If statement, need to look inside if there is an else and check
6684 -- each constituent statement sequence for proper termination.
6685
6686 elsif Kind = N_If_Statement
6687 and then Present (Else_Statements (Last_Stm))
6688 then
6689 Check_Statement_Sequence (Then_Statements (Last_Stm));
6690 Check_Statement_Sequence (Else_Statements (Last_Stm));
6691
6692 if Present (Elsif_Parts (Last_Stm)) then
6693 declare
6694 Elsif_Part : Node_Id := First (Elsif_Parts (Last_Stm));
6695
6696 begin
6697 while Present (Elsif_Part) loop
6698 Check_Statement_Sequence (Then_Statements (Elsif_Part));
6699 Next (Elsif_Part);
6700 end loop;
6701 end;
6702 end if;
6703
6704 return;
6705
6706 -- Case statement, check each case for proper termination
6707
6708 elsif Kind = N_Case_Statement then
6709 declare
6710 Case_Alt : Node_Id;
996ae0b0
RK
6711 begin
6712 Case_Alt := First_Non_Pragma (Alternatives (Last_Stm));
6713 while Present (Case_Alt) loop
6714 Check_Statement_Sequence (Statements (Case_Alt));
6715 Next_Non_Pragma (Case_Alt);
6716 end loop;
6717 end;
6718
6719 return;
6720
6721 -- Block statement, check its handled sequence of statements
6722
6723 elsif Kind = N_Block_Statement then
6724 declare
6725 Err1 : Boolean;
6726
6727 begin
6728 Check_Returns
6729 (Handled_Statement_Sequence (Last_Stm), Mode, Err1);
6730
6731 if Err1 then
6732 Err := True;
6733 end if;
6734
6735 return;
6736 end;
6737
6738 -- Loop statement. If there is an iteration scheme, we can definitely
6739 -- fall out of the loop. Similarly if there is an exit statement, we
6740 -- can fall out. In either case we need a following return.
6741
6742 elsif Kind = N_Loop_Statement then
6743 if Present (Iteration_Scheme (Last_Stm))
6744 or else Has_Exit (Entity (Identifier (Last_Stm)))
6745 then
6746 null;
6747
f3d57416
RW
6748 -- A loop with no exit statement or iteration scheme is either
6749 -- an infinite loop, or it has some other exit (raise/return).
996ae0b0
RK
6750 -- In either case, no warning is required.
6751
6752 else
6753 return;
6754 end if;
6755
6756 -- Timed entry call, check entry call and delay alternatives
6757
6758 -- Note: in expanded code, the timed entry call has been converted
6759 -- to a set of expanded statements on which the check will work
6760 -- correctly in any case.
6761
6762 elsif Kind = N_Timed_Entry_Call then
6763 declare
6764 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6765 DCA : constant Node_Id := Delay_Alternative (Last_Stm);
6766
6767 begin
6768 -- If statement sequence of entry call alternative is missing,
6769 -- then we can definitely fall through, and we post the error
6770 -- message on the entry call alternative itself.
6771
6772 if No (Statements (ECA)) then
6773 Last_Stm := ECA;
6774
6775 -- If statement sequence of delay alternative is missing, then
6776 -- we can definitely fall through, and we post the error
6777 -- message on the delay alternative itself.
6778
6779 -- Note: if both ECA and DCA are missing the return, then we
6780 -- post only one message, should be enough to fix the bugs.
6781 -- If not we will get a message next time on the DCA when the
6782 -- ECA is fixed!
6783
6784 elsif No (Statements (DCA)) then
6785 Last_Stm := DCA;
6786
6787 -- Else check both statement sequences
6788
6789 else
6790 Check_Statement_Sequence (Statements (ECA));
6791 Check_Statement_Sequence (Statements (DCA));
6792 return;
6793 end if;
6794 end;
6795
6796 -- Conditional entry call, check entry call and else part
6797
6798 -- Note: in expanded code, the conditional entry call has been
6799 -- converted to a set of expanded statements on which the check
6800 -- will work correctly in any case.
6801
6802 elsif Kind = N_Conditional_Entry_Call then
6803 declare
6804 ECA : constant Node_Id := Entry_Call_Alternative (Last_Stm);
6805
6806 begin
6807 -- If statement sequence of entry call alternative is missing,
6808 -- then we can definitely fall through, and we post the error
6809 -- message on the entry call alternative itself.
6810
6811 if No (Statements (ECA)) then
6812 Last_Stm := ECA;
6813
6814 -- Else check statement sequence and else part
6815
6816 else
6817 Check_Statement_Sequence (Statements (ECA));
6818 Check_Statement_Sequence (Else_Statements (Last_Stm));
6819 return;
6820 end if;
6821 end;
6822 end if;
6823
6824 -- If we fall through, issue appropriate message
6825
6826 if Mode = 'F' then
996ae0b0
RK
6827 if not Raise_Exception_Call then
6828 Error_Msg_N
5d37ba92 6829 ("?RETURN statement missing following this statement!",
996ae0b0
RK
6830 Last_Stm);
6831 Error_Msg_N
5d37ba92 6832 ("\?Program_Error may be raised at run time!",
996ae0b0
RK
6833 Last_Stm);
6834 end if;
6835
6836 -- Note: we set Err even though we have not issued a warning
6837 -- because we still have a case of a missing return. This is
6838 -- an extremely marginal case, probably will never be noticed
6839 -- but we might as well get it right.
6840
6841 Err := True;
6842
c8ef728f
ES
6843 -- Otherwise we have the case of a procedure marked No_Return
6844
996ae0b0 6845 else
800621e0
RD
6846 if not Raise_Exception_Call then
6847 Error_Msg_N
6848 ("?implied return after this statement " &
6849 "will raise Program_Error",
6850 Last_Stm);
6851 Error_Msg_NE
6852 ("\?procedure & is marked as No_Return!",
6853 Last_Stm, Proc);
6854 end if;
c8ef728f
ES
6855
6856 declare
6857 RE : constant Node_Id :=
6858 Make_Raise_Program_Error (Sloc (Last_Stm),
6859 Reason => PE_Implicit_Return);
6860 begin
6861 Insert_After (Last_Stm, RE);
6862 Analyze (RE);
6863 end;
996ae0b0
RK
6864 end if;
6865 end Check_Statement_Sequence;
6866
6867 -- Start of processing for Check_Returns
6868
6869 begin
6870 Err := False;
6871 Check_Statement_Sequence (Statements (HSS));
6872
6873 if Present (Exception_Handlers (HSS)) then
6874 Handler := First_Non_Pragma (Exception_Handlers (HSS));
6875 while Present (Handler) loop
6876 Check_Statement_Sequence (Statements (Handler));
6877 Next_Non_Pragma (Handler);
6878 end loop;
6879 end if;
6880 end Check_Returns;
6881
67c86178
AC
6882 -------------------------------
6883 -- Check_Subprogram_Contract --
6884 -------------------------------
6885
6886 procedure Check_Subprogram_Contract (Spec_Id : Entity_Id) is
6887
029b67ba
YM
6888 -- Code is currently commented out as, in some cases, it causes crashes
6889 -- because Direct_Primitive_Operations is not available for a private
6890 -- type. This may cause more warnings to be issued than necessary. See
6891 -- below for the intended use of this variable. ???
6892
67c86178
AC
6893-- Inherited : constant Subprogram_List :=
6894-- Inherited_Subprograms (Spec_Id);
dc36a7e3 6895-- -- List of subprograms inherited by this subprogram
67c86178 6896
119e3be6
AC
6897 -- We ignore postconditions "True" or "False" and contract-cases which
6898 -- have similar Ensures components, which we call "trivial", when
6899 -- issuing warnings, since these postconditions and contract-cases
6900 -- purposedly ignore the post-state.
6901
dc36a7e3 6902 Last_Postcondition : Node_Id := Empty;
119e3be6
AC
6903 -- Last non-trivial postcondition on the subprogram, or else Empty if
6904 -- either no non-trivial postcondition or only inherited postconditions.
67c86178 6905
22f46473 6906 Last_Contract_Case : Node_Id := Empty;
119e3be6 6907 -- Last non-trivial contract-case on the subprogram, or else Empty
22f46473 6908
67c86178 6909 Attribute_Result_Mentioned : Boolean := False;
119e3be6
AC
6910 -- Whether attribute 'Result is mentioned in a non-trivial postcondition
6911 -- or contract-case.
67c86178 6912
1f163ef7 6913 No_Warning_On_Some_Postcondition : Boolean := False;
119e3be6
AC
6914 -- Whether there exists a non-trivial postcondition or contract-case
6915 -- without a corresponding warning.
1f163ef7 6916
dc36a7e3 6917 Post_State_Mentioned : Boolean := False;
119e3be6
AC
6918 -- Whether some expression mentioned in a postcondition or contract-case
6919 -- can have a different value in the post-state than in the pre-state.
67c86178
AC
6920
6921 function Check_Attr_Result (N : Node_Id) return Traverse_Result;
dc36a7e3
RD
6922 -- Check if N is a reference to the attribute 'Result, and if so set
6923 -- Attribute_Result_Mentioned and return Abandon. Otherwise return OK.
67c86178
AC
6924
6925 function Check_Post_State (N : Node_Id) return Traverse_Result;
6926 -- Check whether the value of evaluating N can be different in the
6927 -- post-state, compared to the same evaluation in the pre-state, and
6928 -- if so set Post_State_Mentioned and return Abandon. Return Skip on
6929 -- reference to attribute 'Old, in order to ignore its prefix, which
6930 -- is precisely evaluated in the pre-state. Otherwise return OK.
6931
119e3be6 6932 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean;
2a1f6a1f
AC
6933 -- Return True if node N is trivially "True" or "False", and it comes
6934 -- from source. In particular, nodes that are statically known "True" or
6935 -- "False" by the compiler but not written as such in source code are
6936 -- not considered as trivial.
119e3be6 6937
22f46473
AC
6938 procedure Process_Contract_Cases (Spec : Node_Id);
6939 -- This processes the Spec_CTC_List from Spec, processing any contract
6940 -- case from the list. The caller has checked that Spec_CTC_List is
6941 -- non-Empty.
6942
dc36a7e3 6943 procedure Process_Post_Conditions (Spec : Node_Id; Class : Boolean);
67c86178 6944 -- This processes the Spec_PPC_List from Spec, processing any
22f46473 6945 -- postcondition from the list. If Class is True, then only
67c86178
AC
6946 -- postconditions marked with Class_Present are considered. The
6947 -- caller has checked that Spec_PPC_List is non-Empty.
6948
6949 function Find_Attribute_Result is new Traverse_Func (Check_Attr_Result);
6950
6951 function Find_Post_State is new Traverse_Func (Check_Post_State);
6952
6953 -----------------------
6954 -- Check_Attr_Result --
6955 -----------------------
6956
6957 function Check_Attr_Result (N : Node_Id) return Traverse_Result is
6958 begin
6959 if Nkind (N) = N_Attribute_Reference
dc36a7e3 6960 and then Get_Attribute_Id (Attribute_Name (N)) = Attribute_Result
67c86178
AC
6961 then
6962 Attribute_Result_Mentioned := True;
6963 return Abandon;
6964 else
6965 return OK;
6966 end if;
6967 end Check_Attr_Result;
6968
6969 ----------------------
6970 -- Check_Post_State --
6971 ----------------------
6972
6973 function Check_Post_State (N : Node_Id) return Traverse_Result is
6974 Found : Boolean := False;
6975
6976 begin
6977 case Nkind (N) is
6978 when N_Function_Call |
6979 N_Explicit_Dereference =>
6980 Found := True;
6981
6982 when N_Identifier |
6983 N_Expanded_Name =>
dc36a7e3 6984
67c86178
AC
6985 declare
6986 E : constant Entity_Id := Entity (N);
bd38b431 6987
67c86178 6988 begin
bd38b431
AC
6989 -- ???Quantified expressions get analyzed later, so E can
6990 -- be empty at this point. In this case, we suppress the
5b5588dd
AC
6991 -- warning, just in case E is assignable. It seems better to
6992 -- have false negatives than false positives. At some point,
6993 -- we should make the warning more accurate, either by
bd38b431
AC
6994 -- analyzing quantified expressions earlier, or moving
6995 -- this processing later.
5b5588dd 6996
bd38b431
AC
6997 if No (E)
6998 or else
6999 (Is_Entity_Name (N)
7000 and then Ekind (E) in Assignable_Kind)
67c86178
AC
7001 then
7002 Found := True;
7003 end if;
7004 end;
7005
7006 when N_Attribute_Reference =>
7007 case Get_Attribute_Id (Attribute_Name (N)) is
7008 when Attribute_Old =>
7009 return Skip;
7010 when Attribute_Result =>
7011 Found := True;
7012 when others =>
7013 null;
7014 end case;
7015
7016 when others =>
7017 null;
7018 end case;
7019
7020 if Found then
7021 Post_State_Mentioned := True;
7022 return Abandon;
7023 else
7024 return OK;
7025 end if;
7026 end Check_Post_State;
7027
119e3be6
AC
7028 --------------------------------
7029 -- Is_Trivial_Post_Or_Ensures --
7030 --------------------------------
7031
7032 function Is_Trivial_Post_Or_Ensures (N : Node_Id) return Boolean is
7033 begin
7034 return Is_Entity_Name (N)
7035 and then (Entity (N) = Standard_True
7036 or else
2a1f6a1f
AC
7037 Entity (N) = Standard_False)
7038 and then Comes_From_Source (N);
119e3be6
AC
7039 end Is_Trivial_Post_Or_Ensures;
7040
22f46473
AC
7041 ----------------------------
7042 -- Process_Contract_Cases --
7043 ----------------------------
7044
7045 procedure Process_Contract_Cases (Spec : Node_Id) is
119e3be6
AC
7046 Prag : Node_Id;
7047 Arg : Node_Id;
7048
22f46473
AC
7049 Ignored : Traverse_Final_Result;
7050 pragma Unreferenced (Ignored);
7051
7052 begin
7053 Prag := Spec_CTC_List (Contract (Spec));
22f46473
AC
7054 loop
7055 -- Retrieve the Ensures component of the contract-case, if any
7056
ce6002ec 7057 Arg := Get_Ensures_From_CTC_Pragma (Prag);
22f46473 7058
119e3be6
AC
7059 -- Ignore trivial contract-case when Ensures component is "True"
7060 -- or "False".
22f46473 7061
119e3be6
AC
7062 if Pragma_Name (Prag) = Name_Contract_Case
7063 and then not Is_Trivial_Post_Or_Ensures (Expression (Arg))
7064 then
22f46473
AC
7065 -- Since contract-cases are listed in reverse order, the first
7066 -- contract-case in the list is the last in the source.
7067
7068 if No (Last_Contract_Case) then
7069 Last_Contract_Case := Prag;
7070 end if;
7071
7072 -- For functions, look for presence of 'Result in Ensures
7073
7074 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7075 Ignored := Find_Attribute_Result (Arg);
7076 end if;
7077
7078 -- For each individual contract-case, look for presence
7079 -- of an expression that could be evaluated differently
7080 -- in post-state.
7081
7082 Post_State_Mentioned := False;
7083 Ignored := Find_Post_State (Arg);
7084
1f163ef7
AC
7085 if Post_State_Mentioned then
7086 No_Warning_On_Some_Postcondition := True;
7087 else
119e3be6
AC
7088 Error_Msg_N
7089 ("?`Ensures` component refers only to pre-state", Prag);
22f46473
AC
7090 end if;
7091 end if;
7092
7093 Prag := Next_Pragma (Prag);
7094 exit when No (Prag);
7095 end loop;
7096 end Process_Contract_Cases;
7097
67c86178
AC
7098 -----------------------------
7099 -- Process_Post_Conditions --
7100 -----------------------------
7101
7102 procedure Process_Post_Conditions
7103 (Spec : Node_Id;
7104 Class : Boolean)
7105 is
7106 Prag : Node_Id;
7107 Arg : Node_Id;
7108 Ignored : Traverse_Final_Result;
7109 pragma Unreferenced (Ignored);
7110
7111 begin
7112 Prag := Spec_PPC_List (Contract (Spec));
67c86178
AC
7113 loop
7114 Arg := First (Pragma_Argument_Associations (Prag));
7115
119e3be6 7116 -- Ignore trivial postcondition of "True" or "False"
67c86178 7117
119e3be6
AC
7118 if Pragma_Name (Prag) = Name_Postcondition
7119 and then not Is_Trivial_Post_Or_Ensures (Expression (Arg))
7120 then
22f46473 7121 -- Since pre- and post-conditions are listed in reverse order,
b285815e 7122 -- the first postcondition in the list is last in the source.
67c86178 7123
b285815e 7124 if not Class and then No (Last_Postcondition) then
22f46473
AC
7125 Last_Postcondition := Prag;
7126 end if;
67c86178 7127
22f46473 7128 -- For functions, look for presence of 'Result in postcondition
67c86178 7129
22f46473
AC
7130 if Ekind_In (Spec_Id, E_Function, E_Generic_Function) then
7131 Ignored := Find_Attribute_Result (Arg);
7132 end if;
67c86178 7133
22f46473
AC
7134 -- For each individual non-inherited postcondition, look
7135 -- for presence of an expression that could be evaluated
7136 -- differently in post-state.
67c86178 7137
22f46473
AC
7138 if not Class then
7139 Post_State_Mentioned := False;
7140 Ignored := Find_Post_State (Arg);
7141
1f163ef7
AC
7142 if Post_State_Mentioned then
7143 No_Warning_On_Some_Postcondition := True;
7144 else
b285815e
RD
7145 Error_Msg_N
7146 ("?postcondition refers only to pre-state", Prag);
22f46473 7147 end if;
67c86178
AC
7148 end if;
7149 end if;
7150
7151 Prag := Next_Pragma (Prag);
7152 exit when No (Prag);
7153 end loop;
7154 end Process_Post_Conditions;
7155
7156 -- Start of processing for Check_Subprogram_Contract
7157
7158 begin
7159 if not Warn_On_Suspicious_Contract then
7160 return;
7161 end if;
7162
22f46473
AC
7163 -- Process spec postconditions
7164
67c86178
AC
7165 if Present (Spec_PPC_List (Contract (Spec_Id))) then
7166 Process_Post_Conditions (Spec_Id, Class => False);
7167 end if;
7168
7169 -- Process inherited postconditions
7170
7171 -- Code is currently commented out as, in some cases, it causes crashes
7172 -- because Direct_Primitive_Operations is not available for a private
dc36a7e3 7173 -- type. This may cause more warnings to be issued than necessary. ???
67c86178
AC
7174
7175-- for J in Inherited'Range loop
7176-- if Present (Spec_PPC_List (Contract (Inherited (J)))) then
7177-- Process_Post_Conditions (Inherited (J), Class => True);
7178-- end if;
7179-- end loop;
7180
22f46473
AC
7181 -- Process contract cases
7182
7183 if Present (Spec_CTC_List (Contract (Spec_Id))) then
7184 Process_Contract_Cases (Spec_Id);
7185 end if;
7186
67c86178 7187 -- Issue warning for functions whose postcondition does not mention
1f163ef7
AC
7188 -- 'Result after all postconditions have been processed, and provided
7189 -- all postconditions do not already get a warning that they only refer
7190 -- to pre-state.
67c86178
AC
7191
7192 if Ekind_In (Spec_Id, E_Function, E_Generic_Function)
22f46473 7193 and then (Present (Last_Postcondition)
b285815e 7194 or else Present (Last_Contract_Case))
67c86178 7195 and then not Attribute_Result_Mentioned
1f163ef7 7196 and then No_Warning_On_Some_Postcondition
67c86178 7197 then
22f46473
AC
7198 if Present (Last_Postcondition) then
7199 if Present (Last_Contract_Case) then
7200 Error_Msg_N ("?neither function postcondition nor " &
7201 "contract cases do mention result",
7202 Last_Postcondition);
7203
7204 else
7205 Error_Msg_N ("?function postcondition does not mention result",
7206 Last_Postcondition);
7207 end if;
7208 else
7209 Error_Msg_N ("?contract cases do not mention result",
7210 Last_Contract_Case);
7211 end if;
67c86178
AC
7212 end if;
7213 end Check_Subprogram_Contract;
7214
996ae0b0
RK
7215 ----------------------------
7216 -- Check_Subprogram_Order --
7217 ----------------------------
7218
7219 procedure Check_Subprogram_Order (N : Node_Id) is
7220
7221 function Subprogram_Name_Greater (S1, S2 : String) return Boolean;
dc36a7e3
RD
7222 -- This is used to check if S1 > S2 in the sense required by this test,
7223 -- for example nameab < namec, but name2 < name10.
996ae0b0 7224
82c80734
RD
7225 -----------------------------
7226 -- Subprogram_Name_Greater --
7227 -----------------------------
7228
996ae0b0
RK
7229 function Subprogram_Name_Greater (S1, S2 : String) return Boolean is
7230 L1, L2 : Positive;
7231 N1, N2 : Natural;
7232
7233 begin
7234 -- Remove trailing numeric parts
7235
7236 L1 := S1'Last;
7237 while S1 (L1) in '0' .. '9' loop
7238 L1 := L1 - 1;
7239 end loop;
7240
7241 L2 := S2'Last;
7242 while S2 (L2) in '0' .. '9' loop
7243 L2 := L2 - 1;
7244 end loop;
7245
7246 -- If non-numeric parts non-equal, that's decisive
7247
7248 if S1 (S1'First .. L1) < S2 (S2'First .. L2) then
7249 return False;
7250
7251 elsif S1 (S1'First .. L1) > S2 (S2'First .. L2) then
7252 return True;
7253
7254 -- If non-numeric parts equal, compare suffixed numeric parts. Note
7255 -- that a missing suffix is treated as numeric zero in this test.
7256
7257 else
7258 N1 := 0;
7259 while L1 < S1'Last loop
7260 L1 := L1 + 1;
7261 N1 := N1 * 10 + Character'Pos (S1 (L1)) - Character'Pos ('0');
7262 end loop;
7263
7264 N2 := 0;
7265 while L2 < S2'Last loop
7266 L2 := L2 + 1;
7267 N2 := N2 * 10 + Character'Pos (S2 (L2)) - Character'Pos ('0');
7268 end loop;
7269
7270 return N1 > N2;
7271 end if;
7272 end Subprogram_Name_Greater;
7273
7274 -- Start of processing for Check_Subprogram_Order
7275
7276 begin
7277 -- Check body in alpha order if this is option
7278
fbf5a39b 7279 if Style_Check
bc202b70 7280 and then Style_Check_Order_Subprograms
996ae0b0
RK
7281 and then Nkind (N) = N_Subprogram_Body
7282 and then Comes_From_Source (N)
7283 and then In_Extended_Main_Source_Unit (N)
7284 then
7285 declare
7286 LSN : String_Ptr
7287 renames Scope_Stack.Table
7288 (Scope_Stack.Last).Last_Subprogram_Name;
7289
7290 Body_Id : constant Entity_Id :=
7291 Defining_Entity (Specification (N));
7292
7293 begin
7294 Get_Decoded_Name_String (Chars (Body_Id));
7295
7296 if LSN /= null then
7297 if Subprogram_Name_Greater
7298 (LSN.all, Name_Buffer (1 .. Name_Len))
7299 then
7300 Style.Subprogram_Not_In_Alpha_Order (Body_Id);
7301 end if;
7302
7303 Free (LSN);
7304 end if;
7305
7306 LSN := new String'(Name_Buffer (1 .. Name_Len));
7307 end;
7308 end if;
7309 end Check_Subprogram_Order;
7310
7311 ------------------------------
7312 -- Check_Subtype_Conformant --
7313 ------------------------------
7314
7315 procedure Check_Subtype_Conformant
ce2b6ba5
JM
7316 (New_Id : Entity_Id;
7317 Old_Id : Entity_Id;
7318 Err_Loc : Node_Id := Empty;
f307415a
AC
7319 Skip_Controlling_Formals : Boolean := False;
7320 Get_Inst : Boolean := False)
996ae0b0
RK
7321 is
7322 Result : Boolean;
81db9d77 7323 pragma Warnings (Off, Result);
996ae0b0
RK
7324 begin
7325 Check_Conformance
ce2b6ba5 7326 (New_Id, Old_Id, Subtype_Conformant, True, Result, Err_Loc,
f307415a
AC
7327 Skip_Controlling_Formals => Skip_Controlling_Formals,
7328 Get_Inst => Get_Inst);
996ae0b0
RK
7329 end Check_Subtype_Conformant;
7330
7331 ---------------------------
7332 -- Check_Type_Conformant --
7333 ---------------------------
7334
7335 procedure Check_Type_Conformant
7336 (New_Id : Entity_Id;
7337 Old_Id : Entity_Id;
7338 Err_Loc : Node_Id := Empty)
7339 is
7340 Result : Boolean;
81db9d77 7341 pragma Warnings (Off, Result);
996ae0b0
RK
7342 begin
7343 Check_Conformance
7344 (New_Id, Old_Id, Type_Conformant, True, Result, Err_Loc);
7345 end Check_Type_Conformant;
7346
806f6d37
AC
7347 ---------------------------
7348 -- Can_Override_Operator --
7349 ---------------------------
7350
7351 function Can_Override_Operator (Subp : Entity_Id) return Boolean is
7352 Typ : Entity_Id;
f146302c 7353
806f6d37
AC
7354 begin
7355 if Nkind (Subp) /= N_Defining_Operator_Symbol then
7356 return False;
7357
7358 else
7359 Typ := Base_Type (Etype (First_Formal (Subp)));
7360
f146302c
AC
7361 -- Check explicitly that the operation is a primitive of the type
7362
806f6d37 7363 return Operator_Matches_Spec (Subp, Subp)
f146302c 7364 and then not Is_Generic_Type (Typ)
806f6d37
AC
7365 and then Scope (Subp) = Scope (Typ)
7366 and then not Is_Class_Wide_Type (Typ);
7367 end if;
7368 end Can_Override_Operator;
7369
996ae0b0
RK
7370 ----------------------
7371 -- Conforming_Types --
7372 ----------------------
7373
7374 function Conforming_Types
7375 (T1 : Entity_Id;
7376 T2 : Entity_Id;
7377 Ctype : Conformance_Type;
d05ef0ab 7378 Get_Inst : Boolean := False) return Boolean
996ae0b0
RK
7379 is
7380 Type_1 : Entity_Id := T1;
7381 Type_2 : Entity_Id := T2;
af4b9434 7382 Are_Anonymous_Access_To_Subprogram_Types : Boolean := False;
996ae0b0
RK
7383
7384 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean;
0a36105d
JM
7385 -- If neither T1 nor T2 are generic actual types, or if they are in
7386 -- different scopes (e.g. parent and child instances), then verify that
7387 -- the base types are equal. Otherwise T1 and T2 must be on the same
7388 -- subtype chain. The whole purpose of this procedure is to prevent
7389 -- spurious ambiguities in an instantiation that may arise if two
7390 -- distinct generic types are instantiated with the same actual.
7391
5d37ba92
ES
7392 function Find_Designated_Type (T : Entity_Id) return Entity_Id;
7393 -- An access parameter can designate an incomplete type. If the
7394 -- incomplete type is the limited view of a type from a limited_
7395 -- with_clause, check whether the non-limited view is available. If
7396 -- it is a (non-limited) incomplete type, get the full view.
7397
0a36105d
JM
7398 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean;
7399 -- Returns True if and only if either T1 denotes a limited view of T2
7400 -- or T2 denotes a limited view of T1. This can arise when the limited
7401 -- with view of a type is used in a subprogram declaration and the
7402 -- subprogram body is in the scope of a regular with clause for the
7403 -- same unit. In such a case, the two type entities can be considered
7404 -- identical for purposes of conformance checking.
996ae0b0
RK
7405
7406 ----------------------
7407 -- Base_Types_Match --
7408 ----------------------
7409
7410 function Base_Types_Match (T1, T2 : Entity_Id) return Boolean is
7411 begin
7412 if T1 = T2 then
7413 return True;
7414
7415 elsif Base_Type (T1) = Base_Type (T2) then
7416
0a36105d 7417 -- The following is too permissive. A more precise test should
996ae0b0
RK
7418 -- check that the generic actual is an ancestor subtype of the
7419 -- other ???.
7420
7421 return not Is_Generic_Actual_Type (T1)
07fc65c4
GB
7422 or else not Is_Generic_Actual_Type (T2)
7423 or else Scope (T1) /= Scope (T2);
996ae0b0 7424
0a36105d
JM
7425 else
7426 return False;
7427 end if;
7428 end Base_Types_Match;
aa720a54 7429
5d37ba92
ES
7430 --------------------------
7431 -- Find_Designated_Type --
7432 --------------------------
7433
7434 function Find_Designated_Type (T : Entity_Id) return Entity_Id is
7435 Desig : Entity_Id;
7436
7437 begin
7438 Desig := Directly_Designated_Type (T);
7439
7440 if Ekind (Desig) = E_Incomplete_Type then
7441
7442 -- If regular incomplete type, get full view if available
7443
7444 if Present (Full_View (Desig)) then
7445 Desig := Full_View (Desig);
7446
7447 -- If limited view of a type, get non-limited view if available,
7448 -- and check again for a regular incomplete type.
7449
7450 elsif Present (Non_Limited_View (Desig)) then
7451 Desig := Get_Full_View (Non_Limited_View (Desig));
7452 end if;
7453 end if;
7454
7455 return Desig;
7456 end Find_Designated_Type;
7457
0a36105d
JM
7458 -------------------------------
7459 -- Matches_Limited_With_View --
7460 -------------------------------
7461
7462 function Matches_Limited_With_View (T1, T2 : Entity_Id) return Boolean is
7463 begin
7464 -- In some cases a type imported through a limited_with clause, and
7465 -- its nonlimited view are both visible, for example in an anonymous
7466 -- access-to-class-wide type in a formal. Both entities designate the
7467 -- same type.
7468
7469 if From_With_Type (T1)
7470 and then T2 = Available_View (T1)
aa720a54
AC
7471 then
7472 return True;
7473
41251c60 7474 elsif From_With_Type (T2)
0a36105d 7475 and then T1 = Available_View (T2)
41251c60
JM
7476 then
7477 return True;
3e24afaa
AC
7478
7479 elsif From_With_Type (T1)
7480 and then From_With_Type (T2)
7481 and then Available_View (T1) = Available_View (T2)
7482 then
7483 return True;
41251c60 7484
996ae0b0
RK
7485 else
7486 return False;
7487 end if;
0a36105d 7488 end Matches_Limited_With_View;
996ae0b0 7489
ec4867fa 7490 -- Start of processing for Conforming_Types
758c442c 7491
996ae0b0
RK
7492 begin
7493 -- The context is an instance association for a formal
82c80734
RD
7494 -- access-to-subprogram type; the formal parameter types require
7495 -- mapping because they may denote other formal parameters of the
7496 -- generic unit.
996ae0b0
RK
7497
7498 if Get_Inst then
7499 Type_1 := Get_Instance_Of (T1);
7500 Type_2 := Get_Instance_Of (T2);
7501 end if;
7502
0a36105d
JM
7503 -- If one of the types is a view of the other introduced by a limited
7504 -- with clause, treat these as conforming for all purposes.
996ae0b0 7505
0a36105d
JM
7506 if Matches_Limited_With_View (T1, T2) then
7507 return True;
7508
7509 elsif Base_Types_Match (Type_1, Type_2) then
996ae0b0
RK
7510 return Ctype <= Mode_Conformant
7511 or else Subtypes_Statically_Match (Type_1, Type_2);
7512
7513 elsif Is_Incomplete_Or_Private_Type (Type_1)
7514 and then Present (Full_View (Type_1))
7515 and then Base_Types_Match (Full_View (Type_1), Type_2)
7516 then
7517 return Ctype <= Mode_Conformant
7518 or else Subtypes_Statically_Match (Full_View (Type_1), Type_2);
7519
7520 elsif Ekind (Type_2) = E_Incomplete_Type
7521 and then Present (Full_View (Type_2))
7522 and then Base_Types_Match (Type_1, Full_View (Type_2))
7523 then
7524 return Ctype <= Mode_Conformant
7525 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
fbf5a39b
AC
7526
7527 elsif Is_Private_Type (Type_2)
7528 and then In_Instance
7529 and then Present (Full_View (Type_2))
7530 and then Base_Types_Match (Type_1, Full_View (Type_2))
7531 then
7532 return Ctype <= Mode_Conformant
7533 or else Subtypes_Statically_Match (Type_1, Full_View (Type_2));
996ae0b0
RK
7534 end if;
7535
0a36105d 7536 -- Ada 2005 (AI-254): Anonymous access-to-subprogram types must be
758c442c 7537 -- treated recursively because they carry a signature.
af4b9434
AC
7538
7539 Are_Anonymous_Access_To_Subprogram_Types :=
f937473f
RD
7540 Ekind (Type_1) = Ekind (Type_2)
7541 and then
800621e0 7542 (Ekind (Type_1) = E_Anonymous_Access_Subprogram_Type
f937473f
RD
7543 or else
7544 Ekind (Type_1) = E_Anonymous_Access_Protected_Subprogram_Type);
af4b9434 7545
996ae0b0 7546 -- Test anonymous access type case. For this case, static subtype
5d37ba92
ES
7547 -- matching is required for mode conformance (RM 6.3.1(15)). We check
7548 -- the base types because we may have built internal subtype entities
7549 -- to handle null-excluding types (see Process_Formals).
996ae0b0 7550
5d37ba92
ES
7551 if (Ekind (Base_Type (Type_1)) = E_Anonymous_Access_Type
7552 and then
7553 Ekind (Base_Type (Type_2)) = E_Anonymous_Access_Type)
0ab80019 7554 or else Are_Anonymous_Access_To_Subprogram_Types -- Ada 2005 (AI-254)
996ae0b0
RK
7555 then
7556 declare
7557 Desig_1 : Entity_Id;
7558 Desig_2 : Entity_Id;
7559
7560 begin
885c4871 7561 -- In Ada 2005, access constant indicators must match for
5d37ba92 7562 -- subtype conformance.
9dcb52e1 7563
0791fbe9 7564 if Ada_Version >= Ada_2005
5d37ba92
ES
7565 and then Ctype >= Subtype_Conformant
7566 and then
7567 Is_Access_Constant (Type_1) /= Is_Access_Constant (Type_2)
7568 then
7569 return False;
996ae0b0
RK
7570 end if;
7571
5d37ba92 7572 Desig_1 := Find_Designated_Type (Type_1);
5d37ba92 7573 Desig_2 := Find_Designated_Type (Type_2);
996ae0b0 7574
5d37ba92 7575 -- If the context is an instance association for a formal
82c80734
RD
7576 -- access-to-subprogram type; formal access parameter designated
7577 -- types require mapping because they may denote other formal
7578 -- parameters of the generic unit.
996ae0b0
RK
7579
7580 if Get_Inst then
7581 Desig_1 := Get_Instance_Of (Desig_1);
7582 Desig_2 := Get_Instance_Of (Desig_2);
7583 end if;
7584
82c80734
RD
7585 -- It is possible for a Class_Wide_Type to be introduced for an
7586 -- incomplete type, in which case there is a separate class_ wide
7587 -- type for the full view. The types conform if their Etypes
7588 -- conform, i.e. one may be the full view of the other. This can
7589 -- only happen in the context of an access parameter, other uses
7590 -- of an incomplete Class_Wide_Type are illegal.
996ae0b0 7591
fbf5a39b 7592 if Is_Class_Wide_Type (Desig_1)
4adf3c50
AC
7593 and then
7594 Is_Class_Wide_Type (Desig_2)
996ae0b0
RK
7595 then
7596 return
fbf5a39b
AC
7597 Conforming_Types
7598 (Etype (Base_Type (Desig_1)),
7599 Etype (Base_Type (Desig_2)), Ctype);
af4b9434
AC
7600
7601 elsif Are_Anonymous_Access_To_Subprogram_Types then
0791fbe9 7602 if Ada_Version < Ada_2005 then
758c442c
GD
7603 return Ctype = Type_Conformant
7604 or else
af4b9434
AC
7605 Subtypes_Statically_Match (Desig_1, Desig_2);
7606
758c442c
GD
7607 -- We must check the conformance of the signatures themselves
7608
7609 else
7610 declare
7611 Conformant : Boolean;
7612 begin
7613 Check_Conformance
7614 (Desig_1, Desig_2, Ctype, False, Conformant);
7615 return Conformant;
7616 end;
7617 end if;
7618
996ae0b0
RK
7619 else
7620 return Base_Type (Desig_1) = Base_Type (Desig_2)
7621 and then (Ctype = Type_Conformant
af4b9434
AC
7622 or else
7623 Subtypes_Statically_Match (Desig_1, Desig_2));
996ae0b0
RK
7624 end if;
7625 end;
7626
7627 -- Otherwise definitely no match
7628
7629 else
c8ef728f
ES
7630 if ((Ekind (Type_1) = E_Anonymous_Access_Type
7631 and then Is_Access_Type (Type_2))
7632 or else (Ekind (Type_2) = E_Anonymous_Access_Type
7633 and then Is_Access_Type (Type_1)))
7634 and then
7635 Conforming_Types
7636 (Designated_Type (Type_1), Designated_Type (Type_2), Ctype)
7637 then
7638 May_Hide_Profile := True;
7639 end if;
7640
996ae0b0
RK
7641 return False;
7642 end if;
996ae0b0
RK
7643 end Conforming_Types;
7644
7645 --------------------------
7646 -- Create_Extra_Formals --
7647 --------------------------
7648
7649 procedure Create_Extra_Formals (E : Entity_Id) is
7650 Formal : Entity_Id;
ec4867fa 7651 First_Extra : Entity_Id := Empty;
996ae0b0
RK
7652 Last_Extra : Entity_Id;
7653 Formal_Type : Entity_Id;
7654 P_Formal : Entity_Id := Empty;
7655
ec4867fa
ES
7656 function Add_Extra_Formal
7657 (Assoc_Entity : Entity_Id;
7658 Typ : Entity_Id;
7659 Scope : Entity_Id;
7660 Suffix : String) return Entity_Id;
7661 -- Add an extra formal to the current list of formals and extra formals.
7662 -- The extra formal is added to the end of the list of extra formals,
7663 -- and also returned as the result. These formals are always of mode IN.
7664 -- The new formal has the type Typ, is declared in Scope, and its name
7665 -- is given by a concatenation of the name of Assoc_Entity and Suffix.
cd5a9750
AC
7666 -- The following suffixes are currently used. They should not be changed
7667 -- without coordinating with CodePeer, which makes use of these to
7668 -- provide better messages.
7669
d92eccc3
AC
7670 -- O denotes the Constrained bit.
7671 -- L denotes the accessibility level.
cd5a9750
AC
7672 -- BIP_xxx denotes an extra formal for a build-in-place function. See
7673 -- the full list in exp_ch6.BIP_Formal_Kind.
996ae0b0 7674
fbf5a39b
AC
7675 ----------------------
7676 -- Add_Extra_Formal --
7677 ----------------------
7678
ec4867fa
ES
7679 function Add_Extra_Formal
7680 (Assoc_Entity : Entity_Id;
7681 Typ : Entity_Id;
7682 Scope : Entity_Id;
7683 Suffix : String) return Entity_Id
7684 is
996ae0b0 7685 EF : constant Entity_Id :=
ec4867fa
ES
7686 Make_Defining_Identifier (Sloc (Assoc_Entity),
7687 Chars => New_External_Name (Chars (Assoc_Entity),
f937473f 7688 Suffix => Suffix));
996ae0b0
RK
7689
7690 begin
82c80734
RD
7691 -- A little optimization. Never generate an extra formal for the
7692 -- _init operand of an initialization procedure, since it could
7693 -- never be used.
996ae0b0
RK
7694
7695 if Chars (Formal) = Name_uInit then
7696 return Empty;
7697 end if;
7698
7699 Set_Ekind (EF, E_In_Parameter);
7700 Set_Actual_Subtype (EF, Typ);
7701 Set_Etype (EF, Typ);
ec4867fa 7702 Set_Scope (EF, Scope);
996ae0b0
RK
7703 Set_Mechanism (EF, Default_Mechanism);
7704 Set_Formal_Validity (EF);
7705
ec4867fa
ES
7706 if No (First_Extra) then
7707 First_Extra := EF;
7708 Set_Extra_Formals (Scope, First_Extra);
7709 end if;
7710
7711 if Present (Last_Extra) then
7712 Set_Extra_Formal (Last_Extra, EF);
7713 end if;
7714
996ae0b0 7715 Last_Extra := EF;
ec4867fa 7716
996ae0b0
RK
7717 return EF;
7718 end Add_Extra_Formal;
7719
7720 -- Start of processing for Create_Extra_Formals
7721
7722 begin
f937473f
RD
7723 -- We never generate extra formals if expansion is not active
7724 -- because we don't need them unless we are generating code.
7725
7726 if not Expander_Active then
7727 return;
7728 end if;
7729
82c80734 7730 -- If this is a derived subprogram then the subtypes of the parent
16b05213 7731 -- subprogram's formal parameters will be used to determine the need
82c80734 7732 -- for extra formals.
996ae0b0
RK
7733
7734 if Is_Overloadable (E) and then Present (Alias (E)) then
7735 P_Formal := First_Formal (Alias (E));
7736 end if;
7737
7738 Last_Extra := Empty;
7739 Formal := First_Formal (E);
7740 while Present (Formal) loop
7741 Last_Extra := Formal;
7742 Next_Formal (Formal);
7743 end loop;
7744
f937473f 7745 -- If Extra_formals were already created, don't do it again. This
82c80734
RD
7746 -- situation may arise for subprogram types created as part of
7747 -- dispatching calls (see Expand_Dispatching_Call)
996ae0b0
RK
7748
7749 if Present (Last_Extra) and then
7750 Present (Extra_Formal (Last_Extra))
7751 then
7752 return;
7753 end if;
7754
19590d70
GD
7755 -- If the subprogram is a predefined dispatching subprogram then don't
7756 -- generate any extra constrained or accessibility level formals. In
7757 -- general we suppress these for internal subprograms (by not calling
7758 -- Freeze_Subprogram and Create_Extra_Formals at all), but internally
7759 -- generated stream attributes do get passed through because extra
7760 -- build-in-place formals are needed in some cases (limited 'Input).
7761
bac7206d 7762 if Is_Predefined_Internal_Operation (E) then
63585f75 7763 goto Test_For_Func_Result_Extras;
19590d70
GD
7764 end if;
7765
996ae0b0 7766 Formal := First_Formal (E);
996ae0b0
RK
7767 while Present (Formal) loop
7768
7769 -- Create extra formal for supporting the attribute 'Constrained.
7770 -- The case of a private type view without discriminants also
7771 -- requires the extra formal if the underlying type has defaulted
7772 -- discriminants.
7773
7774 if Ekind (Formal) /= E_In_Parameter then
7775 if Present (P_Formal) then
7776 Formal_Type := Etype (P_Formal);
7777 else
7778 Formal_Type := Etype (Formal);
7779 end if;
7780
5d09245e
AC
7781 -- Do not produce extra formals for Unchecked_Union parameters.
7782 -- Jump directly to the end of the loop.
7783
7784 if Is_Unchecked_Union (Base_Type (Formal_Type)) then
7785 goto Skip_Extra_Formal_Generation;
7786 end if;
7787
996ae0b0
RK
7788 if not Has_Discriminants (Formal_Type)
7789 and then Ekind (Formal_Type) in Private_Kind
7790 and then Present (Underlying_Type (Formal_Type))
7791 then
7792 Formal_Type := Underlying_Type (Formal_Type);
7793 end if;
7794
5e5db3b4
GD
7795 -- Suppress the extra formal if formal's subtype is constrained or
7796 -- indefinite, or we're compiling for Ada 2012 and the underlying
7797 -- type is tagged and limited. In Ada 2012, a limited tagged type
7798 -- can have defaulted discriminants, but 'Constrained is required
7799 -- to return True, so the formal is never needed (see AI05-0214).
7800 -- Note that this ensures consistency of calling sequences for
7801 -- dispatching operations when some types in a class have defaults
7802 -- on discriminants and others do not (and requiring the extra
7803 -- formal would introduce distributed overhead).
7804
996ae0b0 7805 if Has_Discriminants (Formal_Type)
f937473f
RD
7806 and then not Is_Constrained (Formal_Type)
7807 and then not Is_Indefinite_Subtype (Formal_Type)
5e5db3b4
GD
7808 and then (Ada_Version < Ada_2012
7809 or else
7810 not (Is_Tagged_Type (Underlying_Type (Formal_Type))
7811 and then Is_Limited_Type (Formal_Type)))
996ae0b0
RK
7812 then
7813 Set_Extra_Constrained
d92eccc3 7814 (Formal, Add_Extra_Formal (Formal, Standard_Boolean, E, "O"));
996ae0b0
RK
7815 end if;
7816 end if;
7817
0a36105d
JM
7818 -- Create extra formal for supporting accessibility checking. This
7819 -- is done for both anonymous access formals and formals of named
7820 -- access types that are marked as controlling formals. The latter
7821 -- case can occur when Expand_Dispatching_Call creates a subprogram
7822 -- type and substitutes the types of access-to-class-wide actuals
7823 -- for the anonymous access-to-specific-type of controlling formals.
5d37ba92
ES
7824 -- Base_Type is applied because in cases where there is a null
7825 -- exclusion the formal may have an access subtype.
996ae0b0
RK
7826
7827 -- This is suppressed if we specifically suppress accessibility
f937473f 7828 -- checks at the package level for either the subprogram, or the
fbf5a39b
AC
7829 -- package in which it resides. However, we do not suppress it
7830 -- simply if the scope has accessibility checks suppressed, since
7831 -- this could cause trouble when clients are compiled with a
7832 -- different suppression setting. The explicit checks at the
7833 -- package level are safe from this point of view.
996ae0b0 7834
5d37ba92 7835 if (Ekind (Base_Type (Etype (Formal))) = E_Anonymous_Access_Type
0a36105d 7836 or else (Is_Controlling_Formal (Formal)
5d37ba92 7837 and then Is_Access_Type (Base_Type (Etype (Formal)))))
996ae0b0 7838 and then not
fbf5a39b 7839 (Explicit_Suppress (E, Accessibility_Check)
996ae0b0 7840 or else
fbf5a39b 7841 Explicit_Suppress (Scope (E), Accessibility_Check))
996ae0b0 7842 and then
c8ef728f 7843 (No (P_Formal)
996ae0b0
RK
7844 or else Present (Extra_Accessibility (P_Formal)))
7845 then
811c6a85 7846 Set_Extra_Accessibility
d92eccc3 7847 (Formal, Add_Extra_Formal (Formal, Standard_Natural, E, "L"));
996ae0b0
RK
7848 end if;
7849
5d09245e
AC
7850 -- This label is required when skipping extra formal generation for
7851 -- Unchecked_Union parameters.
7852
7853 <<Skip_Extra_Formal_Generation>>
7854
f937473f
RD
7855 if Present (P_Formal) then
7856 Next_Formal (P_Formal);
7857 end if;
7858
996ae0b0
RK
7859 Next_Formal (Formal);
7860 end loop;
ec4867fa 7861
63585f75
SB
7862 <<Test_For_Func_Result_Extras>>
7863
7864 -- Ada 2012 (AI05-234): "the accessibility level of the result of a
7865 -- function call is ... determined by the point of call ...".
7866
7867 if Needs_Result_Accessibility_Level (E) then
7868 Set_Extra_Accessibility_Of_Result
7869 (E, Add_Extra_Formal (E, Standard_Natural, E, "L"));
7870 end if;
19590d70 7871
ec4867fa 7872 -- Ada 2005 (AI-318-02): In the case of build-in-place functions, add
f937473f
RD
7873 -- appropriate extra formals. See type Exp_Ch6.BIP_Formal_Kind.
7874
0791fbe9 7875 if Ada_Version >= Ada_2005 and then Is_Build_In_Place_Function (E) then
ec4867fa 7876 declare
f937473f 7877 Result_Subt : constant Entity_Id := Etype (E);
1a36a0cd 7878 Full_Subt : constant Entity_Id := Available_View (Result_Subt);
2fcc44fa 7879 Formal_Typ : Entity_Id;
f937473f 7880
2fcc44fa 7881 Discard : Entity_Id;
f937473f 7882 pragma Warnings (Off, Discard);
ec4867fa
ES
7883
7884 begin
f937473f 7885 -- In the case of functions with unconstrained result subtypes,
9a1bc6d5
AC
7886 -- add a 4-state formal indicating whether the return object is
7887 -- allocated by the caller (1), or should be allocated by the
7888 -- callee on the secondary stack (2), in the global heap (3), or
7889 -- in a user-defined storage pool (4). For the moment we just use
7890 -- Natural for the type of this formal. Note that this formal
7891 -- isn't usually needed in the case where the result subtype is
7892 -- constrained, but it is needed when the function has a tagged
7893 -- result, because generally such functions can be called in a
7894 -- dispatching context and such calls must be handled like calls
7895 -- to a class-wide function.
0a36105d 7896
1bb6e262 7897 if Needs_BIP_Alloc_Form (E) then
f937473f
RD
7898 Discard :=
7899 Add_Extra_Formal
7900 (E, Standard_Natural,
7901 E, BIP_Formal_Suffix (BIP_Alloc_Form));
200b7162 7902
8417f4b2 7903 -- Add BIP_Storage_Pool, in case BIP_Alloc_Form indicates to
3e452820
AC
7904 -- use a user-defined pool. This formal is not added on
7905 -- .NET/JVM/ZFP as those targets do not support pools.
200b7162 7906
ea10ca9c
AC
7907 if VM_Target = No_VM
7908 and then RTE_Available (RE_Root_Storage_Pool_Ptr)
3e452820 7909 then
8417f4b2
AC
7910 Discard :=
7911 Add_Extra_Formal
7912 (E, RTE (RE_Root_Storage_Pool_Ptr),
7913 E, BIP_Formal_Suffix (BIP_Storage_Pool));
7914 end if;
f937473f 7915 end if;
ec4867fa 7916
df3e68b1 7917 -- In the case of functions whose result type needs finalization,
ca5af305 7918 -- add an extra formal which represents the finalization master.
df3e68b1 7919
ca5af305 7920 if Needs_BIP_Finalization_Master (E) then
f937473f
RD
7921 Discard :=
7922 Add_Extra_Formal
ca5af305
AC
7923 (E, RTE (RE_Finalization_Master_Ptr),
7924 E, BIP_Formal_Suffix (BIP_Finalization_Master));
f937473f
RD
7925 end if;
7926
94bbf008
AC
7927 -- When the result type contains tasks, add two extra formals: the
7928 -- master of the tasks to be created, and the caller's activation
7929 -- chain.
f937473f 7930
1a36a0cd 7931 if Has_Task (Full_Subt) then
f937473f
RD
7932 Discard :=
7933 Add_Extra_Formal
7934 (E, RTE (RE_Master_Id),
af89615f 7935 E, BIP_Formal_Suffix (BIP_Task_Master));
f937473f
RD
7936 Discard :=
7937 Add_Extra_Formal
7938 (E, RTE (RE_Activation_Chain_Access),
7939 E, BIP_Formal_Suffix (BIP_Activation_Chain));
7940 end if;
ec4867fa 7941
f937473f
RD
7942 -- All build-in-place functions get an extra formal that will be
7943 -- passed the address of the return object within the caller.
ec4867fa 7944
1a36a0cd
AC
7945 Formal_Typ :=
7946 Create_Itype (E_Anonymous_Access_Type, E, Scope_Id => Scope (E));
ec4867fa 7947
1a36a0cd
AC
7948 Set_Directly_Designated_Type (Formal_Typ, Result_Subt);
7949 Set_Etype (Formal_Typ, Formal_Typ);
7950 Set_Depends_On_Private
7951 (Formal_Typ, Has_Private_Component (Formal_Typ));
7952 Set_Is_Public (Formal_Typ, Is_Public (Scope (Formal_Typ)));
7953 Set_Is_Access_Constant (Formal_Typ, False);
ec4867fa 7954
1a36a0cd
AC
7955 -- Ada 2005 (AI-50217): Propagate the attribute that indicates
7956 -- the designated type comes from the limited view (for back-end
7957 -- purposes).
ec4867fa 7958
1a36a0cd 7959 Set_From_With_Type (Formal_Typ, From_With_Type (Result_Subt));
f937473f 7960
1a36a0cd
AC
7961 Layout_Type (Formal_Typ);
7962
7963 Discard :=
7964 Add_Extra_Formal
7965 (E, Formal_Typ, E, BIP_Formal_Suffix (BIP_Object_Access));
ec4867fa
ES
7966 end;
7967 end if;
996ae0b0
RK
7968 end Create_Extra_Formals;
7969
7970 -----------------------------
7971 -- Enter_Overloaded_Entity --
7972 -----------------------------
7973
7974 procedure Enter_Overloaded_Entity (S : Entity_Id) is
7975 E : Entity_Id := Current_Entity_In_Scope (S);
7976 C_E : Entity_Id := Current_Entity (S);
7977
7978 begin
7979 if Present (E) then
7980 Set_Has_Homonym (E);
7981 Set_Has_Homonym (S);
7982 end if;
7983
7984 Set_Is_Immediately_Visible (S);
7985 Set_Scope (S, Current_Scope);
7986
7987 -- Chain new entity if front of homonym in current scope, so that
7988 -- homonyms are contiguous.
7989
7990 if Present (E)
7991 and then E /= C_E
7992 then
7993 while Homonym (C_E) /= E loop
7994 C_E := Homonym (C_E);
7995 end loop;
7996
7997 Set_Homonym (C_E, S);
7998
7999 else
8000 E := C_E;
8001 Set_Current_Entity (S);
8002 end if;
8003
8004 Set_Homonym (S, E);
8005
8006 Append_Entity (S, Current_Scope);
8007 Set_Public_Status (S);
8008
8009 if Debug_Flag_E then
8010 Write_Str ("New overloaded entity chain: ");
8011 Write_Name (Chars (S));
996ae0b0 8012
82c80734 8013 E := S;
996ae0b0
RK
8014 while Present (E) loop
8015 Write_Str (" "); Write_Int (Int (E));
8016 E := Homonym (E);
8017 end loop;
8018
8019 Write_Eol;
8020 end if;
8021
8022 -- Generate warning for hiding
8023
8024 if Warn_On_Hiding
8025 and then Comes_From_Source (S)
8026 and then In_Extended_Main_Source_Unit (S)
8027 then
8028 E := S;
8029 loop
8030 E := Homonym (E);
8031 exit when No (E);
8032
7fc53871
AC
8033 -- Warn unless genuine overloading. Do not emit warning on
8034 -- hiding predefined operators in Standard (these are either an
8035 -- (artifact of our implicit declarations, or simple noise) but
8036 -- keep warning on a operator defined on a local subtype, because
8037 -- of the real danger that different operators may be applied in
8038 -- various parts of the program.
996ae0b0 8039
1f250383
AC
8040 -- Note that if E and S have the same scope, there is never any
8041 -- hiding. Either the two conflict, and the program is illegal,
8042 -- or S is overriding an implicit inherited subprogram.
8043
8044 if Scope (E) /= Scope (S)
8045 and then (not Is_Overloadable (E)
8d606a78 8046 or else Subtype_Conformant (E, S))
f937473f
RD
8047 and then (Is_Immediately_Visible (E)
8048 or else
8049 Is_Potentially_Use_Visible (S))
996ae0b0 8050 then
7fc53871
AC
8051 if Scope (E) /= Standard_Standard then
8052 Error_Msg_Sloc := Sloc (E);
8053 Error_Msg_N ("declaration of & hides one#?", S);
8054
8055 elsif Nkind (S) = N_Defining_Operator_Symbol
8056 and then
1f250383 8057 Scope (Base_Type (Etype (First_Formal (S)))) /= Scope (S)
7fc53871
AC
8058 then
8059 Error_Msg_N
8060 ("declaration of & hides predefined operator?", S);
8061 end if;
996ae0b0
RK
8062 end if;
8063 end loop;
8064 end if;
8065 end Enter_Overloaded_Entity;
8066
e5a58fac
AC
8067 -----------------------------
8068 -- Check_Untagged_Equality --
8069 -----------------------------
8070
8071 procedure Check_Untagged_Equality (Eq_Op : Entity_Id) is
8072 Typ : constant Entity_Id := Etype (First_Formal (Eq_Op));
8073 Decl : constant Node_Id := Unit_Declaration_Node (Eq_Op);
8074 Obj_Decl : Node_Id;
8075
8076 begin
8077 if Nkind (Decl) = N_Subprogram_Declaration
8078 and then Is_Record_Type (Typ)
8079 and then not Is_Tagged_Type (Typ)
8080 then
21a5b575
AC
8081 -- If the type is not declared in a package, or if we are in the
8082 -- body of the package or in some other scope, the new operation is
8083 -- not primitive, and therefore legal, though suspicious. If the
8084 -- type is a generic actual (sub)type, the operation is not primitive
8085 -- either because the base type is declared elsewhere.
8086
e5a58fac 8087 if Is_Frozen (Typ) then
21a5b575
AC
8088 if Ekind (Scope (Typ)) /= E_Package
8089 or else Scope (Typ) /= Current_Scope
8090 then
8091 null;
e5a58fac 8092
21a5b575
AC
8093 elsif Is_Generic_Actual_Type (Typ) then
8094 null;
e5a58fac 8095
21a5b575 8096 elsif In_Package_Body (Scope (Typ)) then
ae6ede77
AC
8097 Error_Msg_NE
8098 ("equality operator must be declared "
8099 & "before type& is frozen", Eq_Op, Typ);
8100 Error_Msg_N
8101 ("\move declaration to package spec", Eq_Op);
21a5b575
AC
8102
8103 else
8104 Error_Msg_NE
8105 ("equality operator must be declared "
8106 & "before type& is frozen", Eq_Op, Typ);
8107
8108 Obj_Decl := Next (Parent (Typ));
8109 while Present (Obj_Decl)
8110 and then Obj_Decl /= Decl
8111 loop
8112 if Nkind (Obj_Decl) = N_Object_Declaration
8113 and then Etype (Defining_Identifier (Obj_Decl)) = Typ
8114 then
8115 Error_Msg_NE ("type& is frozen by declaration?",
8116 Obj_Decl, Typ);
8117 Error_Msg_N
8118 ("\an equality operator cannot be declared after this "
a4640a39 8119 & "point (RM 4.5.2 (9.8)) (Ada 2012))?", Obj_Decl);
21a5b575
AC
8120 exit;
8121 end if;
8122
8123 Next (Obj_Decl);
8124 end loop;
8125 end if;
e5a58fac
AC
8126
8127 elsif not In_Same_List (Parent (Typ), Decl)
8128 and then not Is_Limited_Type (Typ)
8129 then
21a5b575
AC
8130
8131 -- This makes it illegal to have a primitive equality declared in
8132 -- the private part if the type is visible.
8133
e5a58fac
AC
8134 Error_Msg_N ("equality operator appears too late", Eq_Op);
8135 end if;
8136 end if;
8137 end Check_Untagged_Equality;
8138
996ae0b0
RK
8139 -----------------------------
8140 -- Find_Corresponding_Spec --
8141 -----------------------------
8142
d44202ba
HK
8143 function Find_Corresponding_Spec
8144 (N : Node_Id;
8145 Post_Error : Boolean := True) return Entity_Id
8146 is
996ae0b0
RK
8147 Spec : constant Node_Id := Specification (N);
8148 Designator : constant Entity_Id := Defining_Entity (Spec);
8149
8150 E : Entity_Id;
8151
8152 begin
8153 E := Current_Entity (Designator);
996ae0b0
RK
8154 while Present (E) loop
8155
8156 -- We are looking for a matching spec. It must have the same scope,
8157 -- and the same name, and either be type conformant, or be the case
8158 -- of a library procedure spec and its body (which belong to one
8159 -- another regardless of whether they are type conformant or not).
8160
8161 if Scope (E) = Current_Scope then
fbf5a39b
AC
8162 if Current_Scope = Standard_Standard
8163 or else (Ekind (E) = Ekind (Designator)
8164 and then Type_Conformant (E, Designator))
996ae0b0
RK
8165 then
8166 -- Within an instantiation, we know that spec and body are
8167 -- subtype conformant, because they were subtype conformant
8168 -- in the generic. We choose the subtype-conformant entity
8169 -- here as well, to resolve spurious ambiguities in the
8170 -- instance that were not present in the generic (i.e. when
8171 -- two different types are given the same actual). If we are
8172 -- looking for a spec to match a body, full conformance is
8173 -- expected.
8174
8175 if In_Instance then
8176 Set_Convention (Designator, Convention (E));
8177
0187b60e
AC
8178 -- Skip past subprogram bodies and subprogram renamings that
8179 -- may appear to have a matching spec, but that aren't fully
8180 -- conformant with it. That can occur in cases where an
8181 -- actual type causes unrelated homographs in the instance.
8182
8183 if Nkind_In (N, N_Subprogram_Body,
8184 N_Subprogram_Renaming_Declaration)
996ae0b0 8185 and then Present (Homonym (E))
c7b9d548 8186 and then not Fully_Conformant (Designator, E)
996ae0b0
RK
8187 then
8188 goto Next_Entity;
8189
c7b9d548 8190 elsif not Subtype_Conformant (Designator, E) then
996ae0b0
RK
8191 goto Next_Entity;
8192 end if;
8193 end if;
8194
25ebc085
AC
8195 -- Ada 2012 (AI05-0165): For internally generated bodies of
8196 -- null procedures locate the internally generated spec. We
8197 -- enforce mode conformance since a tagged type may inherit
8198 -- from interfaces several null primitives which differ only
8199 -- in the mode of the formals.
8200
8201 if not (Comes_From_Source (E))
8202 and then Is_Null_Procedure (E)
8203 and then not Mode_Conformant (Designator, E)
8204 then
8205 null;
8206
8207 elsif not Has_Completion (E) then
996ae0b0
RK
8208 if Nkind (N) /= N_Subprogram_Body_Stub then
8209 Set_Corresponding_Spec (N, E);
8210 end if;
8211
8212 Set_Has_Completion (E);
8213 return E;
8214
8215 elsif Nkind (Parent (N)) = N_Subunit then
8216
8217 -- If this is the proper body of a subunit, the completion
8218 -- flag is set when analyzing the stub.
8219
8220 return E;
8221
81db9d77
ES
8222 -- If E is an internal function with a controlling result
8223 -- that was created for an operation inherited by a null
8224 -- extension, it may be overridden by a body without a previous
8225 -- spec (one more reason why these should be shunned). In that
1366997b
AC
8226 -- case remove the generated body if present, because the
8227 -- current one is the explicit overriding.
81db9d77
ES
8228
8229 elsif Ekind (E) = E_Function
0791fbe9 8230 and then Ada_Version >= Ada_2005
81db9d77
ES
8231 and then not Comes_From_Source (E)
8232 and then Has_Controlling_Result (E)
8233 and then Is_Null_Extension (Etype (E))
8234 and then Comes_From_Source (Spec)
8235 then
8236 Set_Has_Completion (E, False);
8237
1366997b
AC
8238 if Expander_Active
8239 and then Nkind (Parent (E)) = N_Function_Specification
8240 then
81db9d77
ES
8241 Remove
8242 (Unit_Declaration_Node
1366997b
AC
8243 (Corresponding_Body (Unit_Declaration_Node (E))));
8244
81db9d77
ES
8245 return E;
8246
1366997b
AC
8247 -- If expansion is disabled, or if the wrapper function has
8248 -- not been generated yet, this a late body overriding an
8249 -- inherited operation, or it is an overriding by some other
8250 -- declaration before the controlling result is frozen. In
8251 -- either case this is a declaration of a new entity.
81db9d77
ES
8252
8253 else
8254 return Empty;
8255 end if;
8256
d44202ba
HK
8257 -- If the body already exists, then this is an error unless
8258 -- the previous declaration is the implicit declaration of a
756ef2a0
AC
8259 -- derived subprogram. It is also legal for an instance to
8260 -- contain type conformant overloadable declarations (but the
8261 -- generic declaration may not), per 8.3(26/2).
996ae0b0
RK
8262
8263 elsif No (Alias (E))
8264 and then not Is_Intrinsic_Subprogram (E)
8265 and then not In_Instance
d44202ba 8266 and then Post_Error
996ae0b0
RK
8267 then
8268 Error_Msg_Sloc := Sloc (E);
8dbd1460 8269
07fc65c4
GB
8270 if Is_Imported (E) then
8271 Error_Msg_NE
8272 ("body not allowed for imported subprogram & declared#",
8273 N, E);
8274 else
8275 Error_Msg_NE ("duplicate body for & declared#", N, E);
8276 end if;
996ae0b0
RK
8277 end if;
8278
d44202ba
HK
8279 -- Child units cannot be overloaded, so a conformance mismatch
8280 -- between body and a previous spec is an error.
8281
996ae0b0
RK
8282 elsif Is_Child_Unit (E)
8283 and then
8284 Nkind (Unit_Declaration_Node (Designator)) = N_Subprogram_Body
8285 and then
5d37ba92 8286 Nkind (Parent (Unit_Declaration_Node (Designator))) =
d44202ba
HK
8287 N_Compilation_Unit
8288 and then Post_Error
996ae0b0 8289 then
996ae0b0
RK
8290 Error_Msg_N
8291 ("body of child unit does not match previous declaration", N);
8292 end if;
8293 end if;
8294
8295 <<Next_Entity>>
8296 E := Homonym (E);
8297 end loop;
8298
8299 -- On exit, we know that no previous declaration of subprogram exists
8300
8301 return Empty;
8302 end Find_Corresponding_Spec;
8303
8304 ----------------------
8305 -- Fully_Conformant --
8306 ----------------------
8307
8308 function Fully_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
8309 Result : Boolean;
996ae0b0
RK
8310 begin
8311 Check_Conformance (New_Id, Old_Id, Fully_Conformant, False, Result);
8312 return Result;
8313 end Fully_Conformant;
8314
8315 ----------------------------------
8316 -- Fully_Conformant_Expressions --
8317 ----------------------------------
8318
8319 function Fully_Conformant_Expressions
8320 (Given_E1 : Node_Id;
d05ef0ab 8321 Given_E2 : Node_Id) return Boolean
996ae0b0
RK
8322 is
8323 E1 : constant Node_Id := Original_Node (Given_E1);
8324 E2 : constant Node_Id := Original_Node (Given_E2);
8325 -- We always test conformance on original nodes, since it is possible
8326 -- for analysis and/or expansion to make things look as though they
8327 -- conform when they do not, e.g. by converting 1+2 into 3.
8328
8329 function FCE (Given_E1, Given_E2 : Node_Id) return Boolean
8330 renames Fully_Conformant_Expressions;
8331
8332 function FCL (L1, L2 : List_Id) return Boolean;
8333 -- Compare elements of two lists for conformance. Elements have to
8334 -- be conformant, and actuals inserted as default parameters do not
8335 -- match explicit actuals with the same value.
8336
8337 function FCO (Op_Node, Call_Node : Node_Id) return Boolean;
e895b435 8338 -- Compare an operator node with a function call
996ae0b0
RK
8339
8340 ---------
8341 -- FCL --
8342 ---------
8343
8344 function FCL (L1, L2 : List_Id) return Boolean is
8345 N1, N2 : Node_Id;
8346
8347 begin
8348 if L1 = No_List then
8349 N1 := Empty;
8350 else
8351 N1 := First (L1);
8352 end if;
8353
8354 if L2 = No_List then
8355 N2 := Empty;
8356 else
8357 N2 := First (L2);
8358 end if;
8359
8360 -- Compare two lists, skipping rewrite insertions (we want to
8361 -- compare the original trees, not the expanded versions!)
8362
8363 loop
8364 if Is_Rewrite_Insertion (N1) then
8365 Next (N1);
8366 elsif Is_Rewrite_Insertion (N2) then
8367 Next (N2);
8368 elsif No (N1) then
8369 return No (N2);
8370 elsif No (N2) then
8371 return False;
8372 elsif not FCE (N1, N2) then
8373 return False;
8374 else
8375 Next (N1);
8376 Next (N2);
8377 end if;
8378 end loop;
8379 end FCL;
8380
8381 ---------
8382 -- FCO --
8383 ---------
8384
8385 function FCO (Op_Node, Call_Node : Node_Id) return Boolean is
8386 Actuals : constant List_Id := Parameter_Associations (Call_Node);
8387 Act : Node_Id;
8388
8389 begin
8390 if No (Actuals)
8391 or else Entity (Op_Node) /= Entity (Name (Call_Node))
8392 then
8393 return False;
8394
8395 else
8396 Act := First (Actuals);
8397
8398 if Nkind (Op_Node) in N_Binary_Op then
996ae0b0
RK
8399 if not FCE (Left_Opnd (Op_Node), Act) then
8400 return False;
8401 end if;
8402
8403 Next (Act);
8404 end if;
8405
8406 return Present (Act)
8407 and then FCE (Right_Opnd (Op_Node), Act)
8408 and then No (Next (Act));
8409 end if;
8410 end FCO;
8411
8412 -- Start of processing for Fully_Conformant_Expressions
8413
8414 begin
8415 -- Non-conformant if paren count does not match. Note: if some idiot
8416 -- complains that we don't do this right for more than 3 levels of
0a36105d 8417 -- parentheses, they will be treated with the respect they deserve!
996ae0b0
RK
8418
8419 if Paren_Count (E1) /= Paren_Count (E2) then
8420 return False;
8421
82c80734
RD
8422 -- If same entities are referenced, then they are conformant even if
8423 -- they have different forms (RM 8.3.1(19-20)).
996ae0b0
RK
8424
8425 elsif Is_Entity_Name (E1) and then Is_Entity_Name (E2) then
8426 if Present (Entity (E1)) then
8427 return Entity (E1) = Entity (E2)
8428 or else (Chars (Entity (E1)) = Chars (Entity (E2))
8429 and then Ekind (Entity (E1)) = E_Discriminant
8430 and then Ekind (Entity (E2)) = E_In_Parameter);
8431
8432 elsif Nkind (E1) = N_Expanded_Name
8433 and then Nkind (E2) = N_Expanded_Name
8434 and then Nkind (Selector_Name (E1)) = N_Character_Literal
8435 and then Nkind (Selector_Name (E2)) = N_Character_Literal
8436 then
8437 return Chars (Selector_Name (E1)) = Chars (Selector_Name (E2));
8438
8439 else
8440 -- Identifiers in component associations don't always have
8441 -- entities, but their names must conform.
8442
8443 return Nkind (E1) = N_Identifier
8444 and then Nkind (E2) = N_Identifier
8445 and then Chars (E1) = Chars (E2);
8446 end if;
8447
8448 elsif Nkind (E1) = N_Character_Literal
8449 and then Nkind (E2) = N_Expanded_Name
8450 then
8451 return Nkind (Selector_Name (E2)) = N_Character_Literal
8452 and then Chars (E1) = Chars (Selector_Name (E2));
8453
8454 elsif Nkind (E2) = N_Character_Literal
8455 and then Nkind (E1) = N_Expanded_Name
8456 then
8457 return Nkind (Selector_Name (E1)) = N_Character_Literal
8458 and then Chars (E2) = Chars (Selector_Name (E1));
8459
8460 elsif Nkind (E1) in N_Op
8461 and then Nkind (E2) = N_Function_Call
8462 then
8463 return FCO (E1, E2);
8464
8465 elsif Nkind (E2) in N_Op
8466 and then Nkind (E1) = N_Function_Call
8467 then
8468 return FCO (E2, E1);
8469
8470 -- Otherwise we must have the same syntactic entity
8471
8472 elsif Nkind (E1) /= Nkind (E2) then
8473 return False;
8474
8475 -- At this point, we specialize by node type
8476
8477 else
8478 case Nkind (E1) is
8479
8480 when N_Aggregate =>
8481 return
8482 FCL (Expressions (E1), Expressions (E2))
19d846a0
RD
8483 and then
8484 FCL (Component_Associations (E1),
8485 Component_Associations (E2));
996ae0b0
RK
8486
8487 when N_Allocator =>
8488 if Nkind (Expression (E1)) = N_Qualified_Expression
8489 or else
8490 Nkind (Expression (E2)) = N_Qualified_Expression
8491 then
8492 return FCE (Expression (E1), Expression (E2));
8493
8494 -- Check that the subtype marks and any constraints
8495 -- are conformant
8496
8497 else
8498 declare
8499 Indic1 : constant Node_Id := Expression (E1);
8500 Indic2 : constant Node_Id := Expression (E2);
8501 Elt1 : Node_Id;
8502 Elt2 : Node_Id;
8503
8504 begin
8505 if Nkind (Indic1) /= N_Subtype_Indication then
8506 return
8507 Nkind (Indic2) /= N_Subtype_Indication
8508 and then Entity (Indic1) = Entity (Indic2);
8509
8510 elsif Nkind (Indic2) /= N_Subtype_Indication then
8511 return
8512 Nkind (Indic1) /= N_Subtype_Indication
8513 and then Entity (Indic1) = Entity (Indic2);
8514
8515 else
8516 if Entity (Subtype_Mark (Indic1)) /=
8517 Entity (Subtype_Mark (Indic2))
8518 then
8519 return False;
8520 end if;
8521
8522 Elt1 := First (Constraints (Constraint (Indic1)));
8523 Elt2 := First (Constraints (Constraint (Indic2)));
996ae0b0
RK
8524 while Present (Elt1) and then Present (Elt2) loop
8525 if not FCE (Elt1, Elt2) then
8526 return False;
8527 end if;
8528
8529 Next (Elt1);
8530 Next (Elt2);
8531 end loop;
8532
8533 return True;
8534 end if;
8535 end;
8536 end if;
8537
8538 when N_Attribute_Reference =>
8539 return
8540 Attribute_Name (E1) = Attribute_Name (E2)
8541 and then FCL (Expressions (E1), Expressions (E2));
8542
8543 when N_Binary_Op =>
8544 return
8545 Entity (E1) = Entity (E2)
8546 and then FCE (Left_Opnd (E1), Left_Opnd (E2))
8547 and then FCE (Right_Opnd (E1), Right_Opnd (E2));
8548
514d0fc5 8549 when N_Short_Circuit | N_Membership_Test =>
996ae0b0
RK
8550 return
8551 FCE (Left_Opnd (E1), Left_Opnd (E2))
8552 and then
8553 FCE (Right_Opnd (E1), Right_Opnd (E2));
8554
19d846a0
RD
8555 when N_Case_Expression =>
8556 declare
8557 Alt1 : Node_Id;
8558 Alt2 : Node_Id;
8559
8560 begin
8561 if not FCE (Expression (E1), Expression (E2)) then
8562 return False;
8563
8564 else
8565 Alt1 := First (Alternatives (E1));
8566 Alt2 := First (Alternatives (E2));
8567 loop
8568 if Present (Alt1) /= Present (Alt2) then
8569 return False;
8570 elsif No (Alt1) then
8571 return True;
8572 end if;
8573
8574 if not FCE (Expression (Alt1), Expression (Alt2))
8575 or else not FCL (Discrete_Choices (Alt1),
8576 Discrete_Choices (Alt2))
8577 then
8578 return False;
8579 end if;
8580
8581 Next (Alt1);
8582 Next (Alt2);
8583 end loop;
8584 end if;
8585 end;
8586
996ae0b0
RK
8587 when N_Character_Literal =>
8588 return
8589 Char_Literal_Value (E1) = Char_Literal_Value (E2);
8590
8591 when N_Component_Association =>
8592 return
8593 FCL (Choices (E1), Choices (E2))
19d846a0
RD
8594 and then
8595 FCE (Expression (E1), Expression (E2));
996ae0b0
RK
8596
8597 when N_Conditional_Expression =>
8598 return
8599 FCL (Expressions (E1), Expressions (E2));
8600
8601 when N_Explicit_Dereference =>
8602 return
8603 FCE (Prefix (E1), Prefix (E2));
8604
8605 when N_Extension_Aggregate =>
8606 return
8607 FCL (Expressions (E1), Expressions (E2))
8608 and then Null_Record_Present (E1) =
8609 Null_Record_Present (E2)
8610 and then FCL (Component_Associations (E1),
8611 Component_Associations (E2));
8612
8613 when N_Function_Call =>
8614 return
8615 FCE (Name (E1), Name (E2))
19d846a0
RD
8616 and then
8617 FCL (Parameter_Associations (E1),
8618 Parameter_Associations (E2));
996ae0b0
RK
8619
8620 when N_Indexed_Component =>
8621 return
8622 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
8623 and then
8624 FCL (Expressions (E1), Expressions (E2));
996ae0b0
RK
8625
8626 when N_Integer_Literal =>
8627 return (Intval (E1) = Intval (E2));
8628
8629 when N_Null =>
8630 return True;
8631
8632 when N_Operator_Symbol =>
8633 return
8634 Chars (E1) = Chars (E2);
8635
8636 when N_Others_Choice =>
8637 return True;
8638
8639 when N_Parameter_Association =>
8640 return
996ae0b0
RK
8641 Chars (Selector_Name (E1)) = Chars (Selector_Name (E2))
8642 and then FCE (Explicit_Actual_Parameter (E1),
8643 Explicit_Actual_Parameter (E2));
8644
8645 when N_Qualified_Expression =>
8646 return
8647 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
8648 and then
8649 FCE (Expression (E1), Expression (E2));
996ae0b0 8650
2010d078
AC
8651 when N_Quantified_Expression =>
8652 if not FCE (Condition (E1), Condition (E2)) then
8653 return False;
8654 end if;
8655
8656 if Present (Loop_Parameter_Specification (E1))
8657 and then Present (Loop_Parameter_Specification (E2))
8658 then
8659 declare
8660 L1 : constant Node_Id :=
8661 Loop_Parameter_Specification (E1);
8662 L2 : constant Node_Id :=
8663 Loop_Parameter_Specification (E2);
8664
8665 begin
8666 return
8667 Reverse_Present (L1) = Reverse_Present (L2)
8668 and then
8669 FCE (Defining_Identifier (L1),
8670 Defining_Identifier (L2))
8671 and then
8672 FCE (Discrete_Subtype_Definition (L1),
8673 Discrete_Subtype_Definition (L2));
8674 end;
8675
804670f1
AC
8676 elsif Present (Iterator_Specification (E1))
8677 and then Present (Iterator_Specification (E2))
8678 then
2010d078
AC
8679 declare
8680 I1 : constant Node_Id := Iterator_Specification (E1);
8681 I2 : constant Node_Id := Iterator_Specification (E2);
8682
8683 begin
8684 return
8685 FCE (Defining_Identifier (I1),
8686 Defining_Identifier (I2))
8687 and then
8688 Of_Present (I1) = Of_Present (I2)
8689 and then
8690 Reverse_Present (I1) = Reverse_Present (I2)
8691 and then FCE (Name (I1), Name (I2))
8692 and then FCE (Subtype_Indication (I1),
8693 Subtype_Indication (I2));
8694 end;
804670f1
AC
8695
8696 -- The quantified expressions used different specifications to
8697 -- walk their respective ranges.
8698
8699 else
8700 return False;
2010d078
AC
8701 end if;
8702
996ae0b0
RK
8703 when N_Range =>
8704 return
8705 FCE (Low_Bound (E1), Low_Bound (E2))
19d846a0
RD
8706 and then
8707 FCE (High_Bound (E1), High_Bound (E2));
996ae0b0
RK
8708
8709 when N_Real_Literal =>
8710 return (Realval (E1) = Realval (E2));
8711
8712 when N_Selected_Component =>
8713 return
8714 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
8715 and then
8716 FCE (Selector_Name (E1), Selector_Name (E2));
996ae0b0
RK
8717
8718 when N_Slice =>
8719 return
8720 FCE (Prefix (E1), Prefix (E2))
19d846a0
RD
8721 and then
8722 FCE (Discrete_Range (E1), Discrete_Range (E2));
996ae0b0
RK
8723
8724 when N_String_Literal =>
8725 declare
8726 S1 : constant String_Id := Strval (E1);
8727 S2 : constant String_Id := Strval (E2);
8728 L1 : constant Nat := String_Length (S1);
8729 L2 : constant Nat := String_Length (S2);
8730
8731 begin
8732 if L1 /= L2 then
8733 return False;
8734
8735 else
8736 for J in 1 .. L1 loop
8737 if Get_String_Char (S1, J) /=
8738 Get_String_Char (S2, J)
8739 then
8740 return False;
8741 end if;
8742 end loop;
8743
8744 return True;
8745 end if;
8746 end;
8747
8748 when N_Type_Conversion =>
8749 return
8750 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
8751 and then
8752 FCE (Expression (E1), Expression (E2));
996ae0b0
RK
8753
8754 when N_Unary_Op =>
8755 return
8756 Entity (E1) = Entity (E2)
19d846a0
RD
8757 and then
8758 FCE (Right_Opnd (E1), Right_Opnd (E2));
996ae0b0
RK
8759
8760 when N_Unchecked_Type_Conversion =>
8761 return
8762 FCE (Subtype_Mark (E1), Subtype_Mark (E2))
19d846a0
RD
8763 and then
8764 FCE (Expression (E1), Expression (E2));
996ae0b0
RK
8765
8766 -- All other node types cannot appear in this context. Strictly
8767 -- we should raise a fatal internal error. Instead we just ignore
8768 -- the nodes. This means that if anyone makes a mistake in the
8769 -- expander and mucks an expression tree irretrievably, the
8770 -- result will be a failure to detect a (probably very obscure)
8771 -- case of non-conformance, which is better than bombing on some
8772 -- case where two expressions do in fact conform.
8773
8774 when others =>
8775 return True;
8776
8777 end case;
8778 end if;
8779 end Fully_Conformant_Expressions;
8780
fbf5a39b
AC
8781 ----------------------------------------
8782 -- Fully_Conformant_Discrete_Subtypes --
8783 ----------------------------------------
8784
8785 function Fully_Conformant_Discrete_Subtypes
8786 (Given_S1 : Node_Id;
d05ef0ab 8787 Given_S2 : Node_Id) return Boolean
fbf5a39b
AC
8788 is
8789 S1 : constant Node_Id := Original_Node (Given_S1);
8790 S2 : constant Node_Id := Original_Node (Given_S2);
8791
8792 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean;
82c80734
RD
8793 -- Special-case for a bound given by a discriminant, which in the body
8794 -- is replaced with the discriminal of the enclosing type.
fbf5a39b
AC
8795
8796 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean;
e895b435 8797 -- Check both bounds
fbf5a39b 8798
5d37ba92
ES
8799 -----------------------
8800 -- Conforming_Bounds --
8801 -----------------------
8802
fbf5a39b
AC
8803 function Conforming_Bounds (B1, B2 : Node_Id) return Boolean is
8804 begin
8805 if Is_Entity_Name (B1)
8806 and then Is_Entity_Name (B2)
8807 and then Ekind (Entity (B1)) = E_Discriminant
8808 then
8809 return Chars (B1) = Chars (B2);
8810
8811 else
8812 return Fully_Conformant_Expressions (B1, B2);
8813 end if;
8814 end Conforming_Bounds;
8815
5d37ba92
ES
8816 -----------------------
8817 -- Conforming_Ranges --
8818 -----------------------
8819
fbf5a39b
AC
8820 function Conforming_Ranges (R1, R2 : Node_Id) return Boolean is
8821 begin
8822 return
8823 Conforming_Bounds (Low_Bound (R1), Low_Bound (R2))
8824 and then
8825 Conforming_Bounds (High_Bound (R1), High_Bound (R2));
8826 end Conforming_Ranges;
8827
8828 -- Start of processing for Fully_Conformant_Discrete_Subtypes
8829
8830 begin
8831 if Nkind (S1) /= Nkind (S2) then
8832 return False;
8833
8834 elsif Is_Entity_Name (S1) then
8835 return Entity (S1) = Entity (S2);
8836
8837 elsif Nkind (S1) = N_Range then
8838 return Conforming_Ranges (S1, S2);
8839
8840 elsif Nkind (S1) = N_Subtype_Indication then
8841 return
8842 Entity (Subtype_Mark (S1)) = Entity (Subtype_Mark (S2))
8843 and then
8844 Conforming_Ranges
8845 (Range_Expression (Constraint (S1)),
8846 Range_Expression (Constraint (S2)));
8847 else
8848 return True;
8849 end if;
8850 end Fully_Conformant_Discrete_Subtypes;
8851
996ae0b0
RK
8852 --------------------
8853 -- Install_Entity --
8854 --------------------
8855
8856 procedure Install_Entity (E : Entity_Id) is
8857 Prev : constant Entity_Id := Current_Entity (E);
996ae0b0
RK
8858 begin
8859 Set_Is_Immediately_Visible (E);
8860 Set_Current_Entity (E);
8861 Set_Homonym (E, Prev);
8862 end Install_Entity;
8863
8864 ---------------------
8865 -- Install_Formals --
8866 ---------------------
8867
8868 procedure Install_Formals (Id : Entity_Id) is
8869 F : Entity_Id;
996ae0b0
RK
8870 begin
8871 F := First_Formal (Id);
996ae0b0
RK
8872 while Present (F) loop
8873 Install_Entity (F);
8874 Next_Formal (F);
8875 end loop;
8876 end Install_Formals;
8877
ce2b6ba5
JM
8878 -----------------------------
8879 -- Is_Interface_Conformant --
8880 -----------------------------
8881
8882 function Is_Interface_Conformant
8883 (Tagged_Type : Entity_Id;
8884 Iface_Prim : Entity_Id;
8885 Prim : Entity_Id) return Boolean
8886 is
fceeaab6
ES
8887 Iface : constant Entity_Id := Find_Dispatching_Type (Iface_Prim);
8888 Typ : constant Entity_Id := Find_Dispatching_Type (Prim);
8889
25ebc085
AC
8890 function Controlling_Formal (Prim : Entity_Id) return Entity_Id;
8891 -- Return the controlling formal of Prim
8892
59e6b23c
AC
8893 ------------------------
8894 -- Controlling_Formal --
8895 ------------------------
8896
25ebc085
AC
8897 function Controlling_Formal (Prim : Entity_Id) return Entity_Id is
8898 E : Entity_Id := First_Entity (Prim);
59e6b23c 8899
25ebc085
AC
8900 begin
8901 while Present (E) loop
8902 if Is_Formal (E) and then Is_Controlling_Formal (E) then
8903 return E;
8904 end if;
8905
8906 Next_Entity (E);
8907 end loop;
8908
8909 return Empty;
8910 end Controlling_Formal;
8911
8912 -- Local variables
8913
8914 Iface_Ctrl_F : constant Entity_Id := Controlling_Formal (Iface_Prim);
8915 Prim_Ctrl_F : constant Entity_Id := Controlling_Formal (Prim);
8916
8917 -- Start of processing for Is_Interface_Conformant
8918
ce2b6ba5
JM
8919 begin
8920 pragma Assert (Is_Subprogram (Iface_Prim)
8921 and then Is_Subprogram (Prim)
8922 and then Is_Dispatching_Operation (Iface_Prim)
8923 and then Is_Dispatching_Operation (Prim));
8924
fceeaab6 8925 pragma Assert (Is_Interface (Iface)
ce2b6ba5
JM
8926 or else (Present (Alias (Iface_Prim))
8927 and then
8928 Is_Interface
8929 (Find_Dispatching_Type (Ultimate_Alias (Iface_Prim)))));
8930
8931 if Prim = Iface_Prim
8932 or else not Is_Subprogram (Prim)
8933 or else Ekind (Prim) /= Ekind (Iface_Prim)
8934 or else not Is_Dispatching_Operation (Prim)
8935 or else Scope (Prim) /= Scope (Tagged_Type)
fceeaab6
ES
8936 or else No (Typ)
8937 or else Base_Type (Typ) /= Tagged_Type
ce2b6ba5
JM
8938 or else not Primitive_Names_Match (Iface_Prim, Prim)
8939 then
8940 return False;
8941
25ebc085
AC
8942 -- The mode of the controlling formals must match
8943
8944 elsif Present (Iface_Ctrl_F)
8945 and then Present (Prim_Ctrl_F)
8946 and then Ekind (Iface_Ctrl_F) /= Ekind (Prim_Ctrl_F)
8947 then
8948 return False;
8949
8950 -- Case of a procedure, or a function whose result type matches the
8951 -- result type of the interface primitive, or a function that has no
8952 -- controlling result (I or access I).
ce2b6ba5
JM
8953
8954 elsif Ekind (Iface_Prim) = E_Procedure
8955 or else Etype (Prim) = Etype (Iface_Prim)
fceeaab6 8956 or else not Has_Controlling_Result (Prim)
ce2b6ba5 8957 then
b4d7b435
AC
8958 return Type_Conformant
8959 (Iface_Prim, Prim, Skip_Controlling_Formals => True);
ce2b6ba5 8960
fceeaab6
ES
8961 -- Case of a function returning an interface, or an access to one.
8962 -- Check that the return types correspond.
ce2b6ba5 8963
fceeaab6
ES
8964 elsif Implements_Interface (Typ, Iface) then
8965 if (Ekind (Etype (Prim)) = E_Anonymous_Access_Type)
9a3c9940
RD
8966 /=
8967 (Ekind (Etype (Iface_Prim)) = E_Anonymous_Access_Type)
fceeaab6
ES
8968 then
8969 return False;
fceeaab6
ES
8970 else
8971 return
ce2b6ba5
JM
8972 Type_Conformant (Prim, Iface_Prim,
8973 Skip_Controlling_Formals => True);
fceeaab6 8974 end if;
ce2b6ba5 8975
fceeaab6
ES
8976 else
8977 return False;
ce2b6ba5 8978 end if;
ce2b6ba5
JM
8979 end Is_Interface_Conformant;
8980
996ae0b0
RK
8981 ---------------------------------
8982 -- Is_Non_Overriding_Operation --
8983 ---------------------------------
8984
8985 function Is_Non_Overriding_Operation
8986 (Prev_E : Entity_Id;
d05ef0ab 8987 New_E : Entity_Id) return Boolean
996ae0b0
RK
8988 is
8989 Formal : Entity_Id;
8990 F_Typ : Entity_Id;
8991 G_Typ : Entity_Id := Empty;
8992
8993 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id;
5d37ba92
ES
8994 -- If F_Type is a derived type associated with a generic actual subtype,
8995 -- then return its Generic_Parent_Type attribute, else return Empty.
996ae0b0
RK
8996
8997 function Types_Correspond
8998 (P_Type : Entity_Id;
d05ef0ab 8999 N_Type : Entity_Id) return Boolean;
82c80734
RD
9000 -- Returns true if and only if the types (or designated types in the
9001 -- case of anonymous access types) are the same or N_Type is derived
9002 -- directly or indirectly from P_Type.
996ae0b0
RK
9003
9004 -----------------------------
9005 -- Get_Generic_Parent_Type --
9006 -----------------------------
9007
9008 function Get_Generic_Parent_Type (F_Typ : Entity_Id) return Entity_Id is
9009 G_Typ : Entity_Id;
702d2020 9010 Defn : Node_Id;
996ae0b0
RK
9011 Indic : Node_Id;
9012
9013 begin
9014 if Is_Derived_Type (F_Typ)
9015 and then Nkind (Parent (F_Typ)) = N_Full_Type_Declaration
9016 then
82c80734
RD
9017 -- The tree must be traversed to determine the parent subtype in
9018 -- the generic unit, which unfortunately isn't always available
9019 -- via semantic attributes. ??? (Note: The use of Original_Node
9020 -- is needed for cases where a full derived type has been
9021 -- rewritten.)
996ae0b0 9022
702d2020
AC
9023 Defn := Type_Definition (Original_Node (Parent (F_Typ)));
9024 if Nkind (Defn) = N_Derived_Type_Definition then
9025 Indic := Subtype_Indication (Defn);
996ae0b0 9026
702d2020
AC
9027 if Nkind (Indic) = N_Subtype_Indication then
9028 G_Typ := Entity (Subtype_Mark (Indic));
9029 else
9030 G_Typ := Entity (Indic);
9031 end if;
996ae0b0 9032
702d2020
AC
9033 if Nkind (Parent (G_Typ)) = N_Subtype_Declaration
9034 and then Present (Generic_Parent_Type (Parent (G_Typ)))
9035 then
9036 return Generic_Parent_Type (Parent (G_Typ));
9037 end if;
996ae0b0
RK
9038 end if;
9039 end if;
9040
9041 return Empty;
9042 end Get_Generic_Parent_Type;
9043
9044 ----------------------
9045 -- Types_Correspond --
9046 ----------------------
9047
9048 function Types_Correspond
9049 (P_Type : Entity_Id;
d05ef0ab 9050 N_Type : Entity_Id) return Boolean
996ae0b0
RK
9051 is
9052 Prev_Type : Entity_Id := Base_Type (P_Type);
9053 New_Type : Entity_Id := Base_Type (N_Type);
9054
9055 begin
9056 if Ekind (Prev_Type) = E_Anonymous_Access_Type then
9057 Prev_Type := Designated_Type (Prev_Type);
9058 end if;
9059
9060 if Ekind (New_Type) = E_Anonymous_Access_Type then
9061 New_Type := Designated_Type (New_Type);
9062 end if;
9063
9064 if Prev_Type = New_Type then
9065 return True;
9066
9067 elsif not Is_Class_Wide_Type (New_Type) then
9068 while Etype (New_Type) /= New_Type loop
9069 New_Type := Etype (New_Type);
9070 if New_Type = Prev_Type then
9071 return True;
9072 end if;
9073 end loop;
9074 end if;
9075 return False;
9076 end Types_Correspond;
9077
9078 -- Start of processing for Is_Non_Overriding_Operation
9079
9080 begin
82c80734
RD
9081 -- In the case where both operations are implicit derived subprograms
9082 -- then neither overrides the other. This can only occur in certain
9083 -- obscure cases (e.g., derivation from homographs created in a generic
9084 -- instantiation).
996ae0b0
RK
9085
9086 if Present (Alias (Prev_E)) and then Present (Alias (New_E)) then
9087 return True;
9088
9089 elsif Ekind (Current_Scope) = E_Package
9090 and then Is_Generic_Instance (Current_Scope)
9091 and then In_Private_Part (Current_Scope)
9092 and then Comes_From_Source (New_E)
9093 then
702d2020
AC
9094 -- We examine the formals and result type of the inherited operation,
9095 -- to determine whether their type is derived from (the instance of)
9096 -- a generic type. The first such formal or result type is the one
9097 -- tested.
996ae0b0
RK
9098
9099 Formal := First_Formal (Prev_E);
996ae0b0
RK
9100 while Present (Formal) loop
9101 F_Typ := Base_Type (Etype (Formal));
9102
9103 if Ekind (F_Typ) = E_Anonymous_Access_Type then
9104 F_Typ := Designated_Type (F_Typ);
9105 end if;
9106
9107 G_Typ := Get_Generic_Parent_Type (F_Typ);
702d2020 9108 exit when Present (G_Typ);
996ae0b0
RK
9109
9110 Next_Formal (Formal);
9111 end loop;
9112
c8ef728f 9113 if No (G_Typ) and then Ekind (Prev_E) = E_Function then
996ae0b0
RK
9114 G_Typ := Get_Generic_Parent_Type (Base_Type (Etype (Prev_E)));
9115 end if;
9116
9117 if No (G_Typ) then
9118 return False;
9119 end if;
9120
8dbd1460
AC
9121 -- If the generic type is a private type, then the original operation
9122 -- was not overriding in the generic, because there was no primitive
9123 -- operation to override.
996ae0b0
RK
9124
9125 if Nkind (Parent (G_Typ)) = N_Formal_Type_Declaration
9126 and then Nkind (Formal_Type_Definition (Parent (G_Typ))) =
8dbd1460 9127 N_Formal_Private_Type_Definition
996ae0b0
RK
9128 then
9129 return True;
9130
9131 -- The generic parent type is the ancestor of a formal derived
9132 -- type declaration. We need to check whether it has a primitive
9133 -- operation that should be overridden by New_E in the generic.
9134
9135 else
9136 declare
9137 P_Formal : Entity_Id;
9138 N_Formal : Entity_Id;
9139 P_Typ : Entity_Id;
9140 N_Typ : Entity_Id;
9141 P_Prim : Entity_Id;
9142 Prim_Elt : Elmt_Id := First_Elmt (Primitive_Operations (G_Typ));
9143
9144 begin
9145 while Present (Prim_Elt) loop
9146 P_Prim := Node (Prim_Elt);
fbf5a39b 9147
996ae0b0
RK
9148 if Chars (P_Prim) = Chars (New_E)
9149 and then Ekind (P_Prim) = Ekind (New_E)
9150 then
9151 P_Formal := First_Formal (P_Prim);
9152 N_Formal := First_Formal (New_E);
9153 while Present (P_Formal) and then Present (N_Formal) loop
9154 P_Typ := Etype (P_Formal);
9155 N_Typ := Etype (N_Formal);
9156
9157 if not Types_Correspond (P_Typ, N_Typ) then
9158 exit;
9159 end if;
9160
9161 Next_Entity (P_Formal);
9162 Next_Entity (N_Formal);
9163 end loop;
9164
82c80734
RD
9165 -- Found a matching primitive operation belonging to the
9166 -- formal ancestor type, so the new subprogram is
9167 -- overriding.
996ae0b0 9168
c8ef728f
ES
9169 if No (P_Formal)
9170 and then No (N_Formal)
996ae0b0
RK
9171 and then (Ekind (New_E) /= E_Function
9172 or else
9173 Types_Correspond
9174 (Etype (P_Prim), Etype (New_E)))
9175 then
9176 return False;
9177 end if;
9178 end if;
9179
9180 Next_Elmt (Prim_Elt);
9181 end loop;
9182
82c80734
RD
9183 -- If no match found, then the new subprogram does not
9184 -- override in the generic (nor in the instance).
996ae0b0 9185
260359e3
AC
9186 -- If the type in question is not abstract, and the subprogram
9187 -- is, this will be an error if the new operation is in the
9188 -- private part of the instance. Emit a warning now, which will
9189 -- make the subsequent error message easier to understand.
9190
9191 if not Is_Abstract_Type (F_Typ)
9192 and then Is_Abstract_Subprogram (Prev_E)
9193 and then In_Private_Part (Current_Scope)
9194 then
9195 Error_Msg_Node_2 := F_Typ;
9196 Error_Msg_NE
9197 ("private operation& in generic unit does not override " &
9198 "any primitive operation of& (RM 12.3 (18))?",
9199 New_E, New_E);
9200 end if;
9201
996ae0b0
RK
9202 return True;
9203 end;
9204 end if;
9205 else
9206 return False;
9207 end if;
9208 end Is_Non_Overriding_Operation;
9209
beacce02
AC
9210 -------------------------------------
9211 -- List_Inherited_Pre_Post_Aspects --
9212 -------------------------------------
9213
9214 procedure List_Inherited_Pre_Post_Aspects (E : Entity_Id) is
9215 begin
e606088a 9216 if Opt.List_Inherited_Aspects
beacce02
AC
9217 and then (Is_Subprogram (E) or else Is_Generic_Subprogram (E))
9218 then
9219 declare
9220 Inherited : constant Subprogram_List :=
9221 Inherited_Subprograms (E);
9222 P : Node_Id;
9223
9224 begin
9225 for J in Inherited'Range loop
dac3bede
YM
9226 P := Spec_PPC_List (Contract (Inherited (J)));
9227
beacce02
AC
9228 while Present (P) loop
9229 Error_Msg_Sloc := Sloc (P);
9230
9231 if Class_Present (P) and then not Split_PPC (P) then
9232 if Pragma_Name (P) = Name_Precondition then
9233 Error_Msg_N
9234 ("?info: & inherits `Pre''Class` aspect from #", E);
9235 else
9236 Error_Msg_N
9237 ("?info: & inherits `Post''Class` aspect from #", E);
9238 end if;
9239 end if;
9240
9241 P := Next_Pragma (P);
9242 end loop;
9243 end loop;
9244 end;
9245 end if;
9246 end List_Inherited_Pre_Post_Aspects;
9247
996ae0b0
RK
9248 ------------------------------
9249 -- Make_Inequality_Operator --
9250 ------------------------------
9251
9252 -- S is the defining identifier of an equality operator. We build a
9253 -- subprogram declaration with the right signature. This operation is
9254 -- intrinsic, because it is always expanded as the negation of the
9255 -- call to the equality function.
9256
9257 procedure Make_Inequality_Operator (S : Entity_Id) is
9258 Loc : constant Source_Ptr := Sloc (S);
9259 Decl : Node_Id;
9260 Formals : List_Id;
9261 Op_Name : Entity_Id;
9262
c8ef728f
ES
9263 FF : constant Entity_Id := First_Formal (S);
9264 NF : constant Entity_Id := Next_Formal (FF);
996ae0b0
RK
9265
9266 begin
c8ef728f 9267 -- Check that equality was properly defined, ignore call if not
996ae0b0 9268
c8ef728f 9269 if No (NF) then
996ae0b0
RK
9270 return;
9271 end if;
9272
c8ef728f
ES
9273 declare
9274 A : constant Entity_Id :=
9275 Make_Defining_Identifier (Sloc (FF),
9276 Chars => Chars (FF));
9277
5d37ba92
ES
9278 B : constant Entity_Id :=
9279 Make_Defining_Identifier (Sloc (NF),
9280 Chars => Chars (NF));
c8ef728f
ES
9281
9282 begin
9283 Op_Name := Make_Defining_Operator_Symbol (Loc, Name_Op_Ne);
9284
9285 Formals := New_List (
9286 Make_Parameter_Specification (Loc,
9287 Defining_Identifier => A,
9288 Parameter_Type =>
9289 New_Reference_To (Etype (First_Formal (S)),
9290 Sloc (Etype (First_Formal (S))))),
9291
9292 Make_Parameter_Specification (Loc,
9293 Defining_Identifier => B,
9294 Parameter_Type =>
9295 New_Reference_To (Etype (Next_Formal (First_Formal (S))),
9296 Sloc (Etype (Next_Formal (First_Formal (S)))))));
9297
9298 Decl :=
9299 Make_Subprogram_Declaration (Loc,
9300 Specification =>
9301 Make_Function_Specification (Loc,
9302 Defining_Unit_Name => Op_Name,
9303 Parameter_Specifications => Formals,
9304 Result_Definition =>
9305 New_Reference_To (Standard_Boolean, Loc)));
9306
9307 -- Insert inequality right after equality if it is explicit or after
9308 -- the derived type when implicit. These entities are created only
9309 -- for visibility purposes, and eventually replaced in the course of
9310 -- expansion, so they do not need to be attached to the tree and seen
9311 -- by the back-end. Keeping them internal also avoids spurious
9312 -- freezing problems. The declaration is inserted in the tree for
9313 -- analysis, and removed afterwards. If the equality operator comes
9314 -- from an explicit declaration, attach the inequality immediately
9315 -- after. Else the equality is inherited from a derived type
9316 -- declaration, so insert inequality after that declaration.
9317
9318 if No (Alias (S)) then
9319 Insert_After (Unit_Declaration_Node (S), Decl);
9320 elsif Is_List_Member (Parent (S)) then
9321 Insert_After (Parent (S), Decl);
9322 else
9323 Insert_After (Parent (Etype (First_Formal (S))), Decl);
9324 end if;
996ae0b0 9325
c8ef728f
ES
9326 Mark_Rewrite_Insertion (Decl);
9327 Set_Is_Intrinsic_Subprogram (Op_Name);
9328 Analyze (Decl);
9329 Remove (Decl);
9330 Set_Has_Completion (Op_Name);
9331 Set_Corresponding_Equality (Op_Name, S);
f937473f 9332 Set_Is_Abstract_Subprogram (Op_Name, Is_Abstract_Subprogram (S));
c8ef728f 9333 end;
996ae0b0
RK
9334 end Make_Inequality_Operator;
9335
9336 ----------------------
9337 -- May_Need_Actuals --
9338 ----------------------
9339
9340 procedure May_Need_Actuals (Fun : Entity_Id) is
9341 F : Entity_Id;
9342 B : Boolean;
9343
9344 begin
9345 F := First_Formal (Fun);
9346 B := True;
996ae0b0
RK
9347 while Present (F) loop
9348 if No (Default_Value (F)) then
9349 B := False;
9350 exit;
9351 end if;
9352
9353 Next_Formal (F);
9354 end loop;
9355
9356 Set_Needs_No_Actuals (Fun, B);
9357 end May_Need_Actuals;
9358
9359 ---------------------
9360 -- Mode_Conformant --
9361 ---------------------
9362
9363 function Mode_Conformant (New_Id, Old_Id : Entity_Id) return Boolean is
9364 Result : Boolean;
996ae0b0
RK
9365 begin
9366 Check_Conformance (New_Id, Old_Id, Mode_Conformant, False, Result);
9367 return Result;
9368 end Mode_Conformant;
9369
9370 ---------------------------
9371 -- New_Overloaded_Entity --
9372 ---------------------------
9373
9374 procedure New_Overloaded_Entity
9375 (S : Entity_Id;
9376 Derived_Type : Entity_Id := Empty)
9377 is
ec4867fa 9378 Overridden_Subp : Entity_Id := Empty;
758c442c
GD
9379 -- Set if the current scope has an operation that is type-conformant
9380 -- with S, and becomes hidden by S.
9381
5d37ba92
ES
9382 Is_Primitive_Subp : Boolean;
9383 -- Set to True if the new subprogram is primitive
9384
fbf5a39b
AC
9385 E : Entity_Id;
9386 -- Entity that S overrides
9387
996ae0b0 9388 Prev_Vis : Entity_Id := Empty;
ec4867fa
ES
9389 -- Predecessor of E in Homonym chain
9390
5d37ba92
ES
9391 procedure Check_For_Primitive_Subprogram
9392 (Is_Primitive : out Boolean;
9393 Is_Overriding : Boolean := False);
9394 -- If the subprogram being analyzed is a primitive operation of the type
9395 -- of a formal or result, set the Has_Primitive_Operations flag on the
9396 -- type, and set Is_Primitive to True (otherwise set to False). Set the
9397 -- corresponding flag on the entity itself for later use.
9398
ec4867fa
ES
9399 procedure Check_Synchronized_Overriding
9400 (Def_Id : Entity_Id;
ec4867fa
ES
9401 Overridden_Subp : out Entity_Id);
9402 -- First determine if Def_Id is an entry or a subprogram either defined
9403 -- in the scope of a task or protected type, or is a primitive of such
9404 -- a type. Check whether Def_Id overrides a subprogram of an interface
9405 -- implemented by the synchronized type, return the overridden entity
9406 -- or Empty.
758c442c 9407
996ae0b0
RK
9408 function Is_Private_Declaration (E : Entity_Id) return Boolean;
9409 -- Check that E is declared in the private part of the current package,
9410 -- or in the package body, where it may hide a previous declaration.
fbf5a39b 9411 -- We can't use In_Private_Part by itself because this flag is also
996ae0b0
RK
9412 -- set when freezing entities, so we must examine the place of the
9413 -- declaration in the tree, and recognize wrapper packages as well.
9414
2ddc2000
AC
9415 function Is_Overriding_Alias
9416 (Old_E : Entity_Id;
9417 New_E : Entity_Id) return Boolean;
9418 -- Check whether new subprogram and old subprogram are both inherited
9419 -- from subprograms that have distinct dispatch table entries. This can
9420 -- occur with derivations from instances with accidental homonyms.
9421 -- The function is conservative given that the converse is only true
9422 -- within instances that contain accidental overloadings.
9423
5d37ba92
ES
9424 ------------------------------------
9425 -- Check_For_Primitive_Subprogram --
9426 ------------------------------------
996ae0b0 9427
5d37ba92
ES
9428 procedure Check_For_Primitive_Subprogram
9429 (Is_Primitive : out Boolean;
9430 Is_Overriding : Boolean := False)
ec4867fa 9431 is
996ae0b0
RK
9432 Formal : Entity_Id;
9433 F_Typ : Entity_Id;
07fc65c4 9434 B_Typ : Entity_Id;
996ae0b0
RK
9435
9436 function Visible_Part_Type (T : Entity_Id) return Boolean;
8dbd1460
AC
9437 -- Returns true if T is declared in the visible part of the current
9438 -- package scope; otherwise returns false. Assumes that T is declared
9439 -- in a package.
996ae0b0
RK
9440
9441 procedure Check_Private_Overriding (T : Entity_Id);
9442 -- Checks that if a primitive abstract subprogram of a visible
8dbd1460
AC
9443 -- abstract type is declared in a private part, then it must override
9444 -- an abstract subprogram declared in the visible part. Also checks
9445 -- that if a primitive function with a controlling result is declared
9446 -- in a private part, then it must override a function declared in
9447 -- the visible part.
996ae0b0
RK
9448
9449 ------------------------------
9450 -- Check_Private_Overriding --
9451 ------------------------------
9452
9453 procedure Check_Private_Overriding (T : Entity_Id) is
9454 begin
51c16e29 9455 if Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0
RK
9456 and then In_Private_Part (Current_Scope)
9457 and then Visible_Part_Type (T)
9458 and then not In_Instance
9459 then
f937473f
RD
9460 if Is_Abstract_Type (T)
9461 and then Is_Abstract_Subprogram (S)
9462 and then (not Is_Overriding
8dbd1460 9463 or else not Is_Abstract_Subprogram (E))
996ae0b0 9464 then
ed2233dc 9465 Error_Msg_N
19d846a0
RD
9466 ("abstract subprograms must be visible "
9467 & "(RM 3.9.3(10))!", S);
758c442c 9468
996ae0b0 9469 elsif Ekind (S) = E_Function
82c80734 9470 and then not Is_Overriding
996ae0b0 9471 then
2e79de51
AC
9472 if Is_Tagged_Type (T)
9473 and then T = Base_Type (Etype (S))
9474 then
9475 Error_Msg_N
9476 ("private function with tagged result must"
9477 & " override visible-part function", S);
9478 Error_Msg_N
9479 ("\move subprogram to the visible part"
9480 & " (RM 3.9.3(10))", S);
9481
9482 -- AI05-0073: extend this test to the case of a function
9483 -- with a controlling access result.
9484
9485 elsif Ekind (Etype (S)) = E_Anonymous_Access_Type
9486 and then Is_Tagged_Type (Designated_Type (Etype (S)))
9487 and then
9488 not Is_Class_Wide_Type (Designated_Type (Etype (S)))
dbe945f1 9489 and then Ada_Version >= Ada_2012
2e79de51
AC
9490 then
9491 Error_Msg_N
9492 ("private function with controlling access result "
9493 & "must override visible-part function", S);
9494 Error_Msg_N
9495 ("\move subprogram to the visible part"
9496 & " (RM 3.9.3(10))", S);
9497 end if;
996ae0b0
RK
9498 end if;
9499 end if;
9500 end Check_Private_Overriding;
9501
9502 -----------------------
9503 -- Visible_Part_Type --
9504 -----------------------
9505
9506 function Visible_Part_Type (T : Entity_Id) return Boolean is
07fc65c4
GB
9507 P : constant Node_Id := Unit_Declaration_Node (Scope (T));
9508 N : Node_Id;
996ae0b0
RK
9509
9510 begin
8dbd1460
AC
9511 -- If the entity is a private type, then it must be declared in a
9512 -- visible part.
996ae0b0
RK
9513
9514 if Ekind (T) in Private_Kind then
9515 return True;
9516 end if;
9517
9518 -- Otherwise, we traverse the visible part looking for its
9519 -- corresponding declaration. We cannot use the declaration
9520 -- node directly because in the private part the entity of a
9521 -- private type is the one in the full view, which does not
9522 -- indicate that it is the completion of something visible.
9523
07fc65c4 9524 N := First (Visible_Declarations (Specification (P)));
996ae0b0
RK
9525 while Present (N) loop
9526 if Nkind (N) = N_Full_Type_Declaration
9527 and then Present (Defining_Identifier (N))
9528 and then T = Defining_Identifier (N)
9529 then
9530 return True;
9531
800621e0
RD
9532 elsif Nkind_In (N, N_Private_Type_Declaration,
9533 N_Private_Extension_Declaration)
996ae0b0
RK
9534 and then Present (Defining_Identifier (N))
9535 and then T = Full_View (Defining_Identifier (N))
9536 then
9537 return True;
9538 end if;
9539
9540 Next (N);
9541 end loop;
9542
9543 return False;
9544 end Visible_Part_Type;
9545
5d37ba92 9546 -- Start of processing for Check_For_Primitive_Subprogram
996ae0b0
RK
9547
9548 begin
5d37ba92
ES
9549 Is_Primitive := False;
9550
996ae0b0
RK
9551 if not Comes_From_Source (S) then
9552 null;
9553
5d37ba92 9554 -- If subprogram is at library level, it is not primitive operation
15ce9ca2
AC
9555
9556 elsif Current_Scope = Standard_Standard then
9557 null;
9558
b9b2405f 9559 elsif (Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0 9560 and then not In_Package_Body (Current_Scope))
82c80734 9561 or else Is_Overriding
996ae0b0 9562 then
07fc65c4 9563 -- For function, check return type
996ae0b0 9564
07fc65c4 9565 if Ekind (S) = E_Function then
5d37ba92
ES
9566 if Ekind (Etype (S)) = E_Anonymous_Access_Type then
9567 F_Typ := Designated_Type (Etype (S));
9568 else
9569 F_Typ := Etype (S);
9570 end if;
9571
9572 B_Typ := Base_Type (F_Typ);
07fc65c4 9573
5d37ba92
ES
9574 if Scope (B_Typ) = Current_Scope
9575 and then not Is_Class_Wide_Type (B_Typ)
9576 and then not Is_Generic_Type (B_Typ)
9577 then
9578 Is_Primitive := True;
07fc65c4 9579 Set_Has_Primitive_Operations (B_Typ);
5d37ba92 9580 Set_Is_Primitive (S);
07fc65c4
GB
9581 Check_Private_Overriding (B_Typ);
9582 end if;
996ae0b0
RK
9583 end if;
9584
07fc65c4 9585 -- For all subprograms, check formals
996ae0b0 9586
07fc65c4 9587 Formal := First_Formal (S);
996ae0b0
RK
9588 while Present (Formal) loop
9589 if Ekind (Etype (Formal)) = E_Anonymous_Access_Type then
9590 F_Typ := Designated_Type (Etype (Formal));
9591 else
9592 F_Typ := Etype (Formal);
9593 end if;
9594
07fc65c4
GB
9595 B_Typ := Base_Type (F_Typ);
9596
ec4867fa
ES
9597 if Ekind (B_Typ) = E_Access_Subtype then
9598 B_Typ := Base_Type (B_Typ);
9599 end if;
9600
5d37ba92
ES
9601 if Scope (B_Typ) = Current_Scope
9602 and then not Is_Class_Wide_Type (B_Typ)
9603 and then not Is_Generic_Type (B_Typ)
9604 then
9605 Is_Primitive := True;
9606 Set_Is_Primitive (S);
07fc65c4
GB
9607 Set_Has_Primitive_Operations (B_Typ);
9608 Check_Private_Overriding (B_Typ);
996ae0b0
RK
9609 end if;
9610
9611 Next_Formal (Formal);
9612 end loop;
996ae0b0 9613 end if;
5d37ba92
ES
9614 end Check_For_Primitive_Subprogram;
9615
9616 -----------------------------------
9617 -- Check_Synchronized_Overriding --
9618 -----------------------------------
9619
9620 procedure Check_Synchronized_Overriding
9621 (Def_Id : Entity_Id;
5d37ba92
ES
9622 Overridden_Subp : out Entity_Id)
9623 is
5d37ba92
ES
9624 Ifaces_List : Elist_Id;
9625 In_Scope : Boolean;
9626 Typ : Entity_Id;
9627
8aa15e3b
JM
9628 function Matches_Prefixed_View_Profile
9629 (Prim_Params : List_Id;
9630 Iface_Params : List_Id) return Boolean;
9631 -- Determine whether a subprogram's parameter profile Prim_Params
9632 -- matches that of a potentially overridden interface subprogram
9633 -- Iface_Params. Also determine if the type of first parameter of
9634 -- Iface_Params is an implemented interface.
9635
8aa15e3b
JM
9636 -----------------------------------
9637 -- Matches_Prefixed_View_Profile --
9638 -----------------------------------
9639
9640 function Matches_Prefixed_View_Profile
9641 (Prim_Params : List_Id;
9642 Iface_Params : List_Id) return Boolean
9643 is
9644 Iface_Id : Entity_Id;
9645 Iface_Param : Node_Id;
9646 Iface_Typ : Entity_Id;
9647 Prim_Id : Entity_Id;
9648 Prim_Param : Node_Id;
9649 Prim_Typ : Entity_Id;
9650
9651 function Is_Implemented
9652 (Ifaces_List : Elist_Id;
9653 Iface : Entity_Id) return Boolean;
9654 -- Determine if Iface is implemented by the current task or
9655 -- protected type.
9656
9657 --------------------
9658 -- Is_Implemented --
9659 --------------------
9660
9661 function Is_Implemented
9662 (Ifaces_List : Elist_Id;
9663 Iface : Entity_Id) return Boolean
9664 is
9665 Iface_Elmt : Elmt_Id;
9666
9667 begin
9668 Iface_Elmt := First_Elmt (Ifaces_List);
9669 while Present (Iface_Elmt) loop
9670 if Node (Iface_Elmt) = Iface then
9671 return True;
9672 end if;
9673
9674 Next_Elmt (Iface_Elmt);
9675 end loop;
9676
9677 return False;
9678 end Is_Implemented;
9679
9680 -- Start of processing for Matches_Prefixed_View_Profile
9681
9682 begin
9683 Iface_Param := First (Iface_Params);
9684 Iface_Typ := Etype (Defining_Identifier (Iface_Param));
9685
9686 if Is_Access_Type (Iface_Typ) then
9687 Iface_Typ := Designated_Type (Iface_Typ);
9688 end if;
9689
9690 Prim_Param := First (Prim_Params);
9691
9692 -- The first parameter of the potentially overridden subprogram
9693 -- must be an interface implemented by Prim.
9694
9695 if not Is_Interface (Iface_Typ)
9696 or else not Is_Implemented (Ifaces_List, Iface_Typ)
9697 then
9698 return False;
9699 end if;
9700
9701 -- The checks on the object parameters are done, move onto the
9702 -- rest of the parameters.
9703
9704 if not In_Scope then
9705 Prim_Param := Next (Prim_Param);
9706 end if;
9707
9708 Iface_Param := Next (Iface_Param);
9709 while Present (Iface_Param) and then Present (Prim_Param) loop
9710 Iface_Id := Defining_Identifier (Iface_Param);
9711 Iface_Typ := Find_Parameter_Type (Iface_Param);
9712
8aa15e3b
JM
9713 Prim_Id := Defining_Identifier (Prim_Param);
9714 Prim_Typ := Find_Parameter_Type (Prim_Param);
9715
15e4986c
JM
9716 if Ekind (Iface_Typ) = E_Anonymous_Access_Type
9717 and then Ekind (Prim_Typ) = E_Anonymous_Access_Type
9718 and then Is_Concurrent_Type (Designated_Type (Prim_Typ))
9719 then
9720 Iface_Typ := Designated_Type (Iface_Typ);
9721 Prim_Typ := Designated_Type (Prim_Typ);
8aa15e3b
JM
9722 end if;
9723
9724 -- Case of multiple interface types inside a parameter profile
9725
9726 -- (Obj_Param : in out Iface; ...; Param : Iface)
9727
9728 -- If the interface type is implemented, then the matching type
9729 -- in the primitive should be the implementing record type.
9730
9731 if Ekind (Iface_Typ) = E_Record_Type
9732 and then Is_Interface (Iface_Typ)
9733 and then Is_Implemented (Ifaces_List, Iface_Typ)
9734 then
9735 if Prim_Typ /= Typ then
9736 return False;
9737 end if;
9738
9739 -- The two parameters must be both mode and subtype conformant
9740
9741 elsif Ekind (Iface_Id) /= Ekind (Prim_Id)
9742 or else not
9743 Conforming_Types (Iface_Typ, Prim_Typ, Subtype_Conformant)
9744 then
9745 return False;
9746 end if;
9747
9748 Next (Iface_Param);
9749 Next (Prim_Param);
9750 end loop;
9751
9752 -- One of the two lists contains more parameters than the other
9753
9754 if Present (Iface_Param) or else Present (Prim_Param) then
9755 return False;
9756 end if;
9757
9758 return True;
9759 end Matches_Prefixed_View_Profile;
9760
9761 -- Start of processing for Check_Synchronized_Overriding
9762
5d37ba92
ES
9763 begin
9764 Overridden_Subp := Empty;
9765
8aa15e3b
JM
9766 -- Def_Id must be an entry or a subprogram. We should skip predefined
9767 -- primitives internally generated by the frontend; however at this
9768 -- stage predefined primitives are still not fully decorated. As a
9769 -- minor optimization we skip here internally generated subprograms.
5d37ba92 9770
8aa15e3b
JM
9771 if (Ekind (Def_Id) /= E_Entry
9772 and then Ekind (Def_Id) /= E_Function
9773 and then Ekind (Def_Id) /= E_Procedure)
9774 or else not Comes_From_Source (Def_Id)
5d37ba92
ES
9775 then
9776 return;
9777 end if;
9778
9779 -- Search for the concurrent declaration since it contains the list
9780 -- of all implemented interfaces. In this case, the subprogram is
9781 -- declared within the scope of a protected or a task type.
9782
9783 if Present (Scope (Def_Id))
9784 and then Is_Concurrent_Type (Scope (Def_Id))
9785 and then not Is_Generic_Actual_Type (Scope (Def_Id))
9786 then
9787 Typ := Scope (Def_Id);
9788 In_Scope := True;
9789
8aa15e3b 9790 -- The enclosing scope is not a synchronized type and the subprogram
4adf3c50 9791 -- has no formals.
8aa15e3b
JM
9792
9793 elsif No (First_Formal (Def_Id)) then
9794 return;
5d37ba92 9795
8aa15e3b 9796 -- The subprogram has formals and hence it may be a primitive of a
4adf3c50 9797 -- concurrent type.
5d37ba92 9798
8aa15e3b
JM
9799 else
9800 Typ := Etype (First_Formal (Def_Id));
9801
9802 if Is_Access_Type (Typ) then
9803 Typ := Directly_Designated_Type (Typ);
8c3dd7a8
JM
9804 end if;
9805
8aa15e3b
JM
9806 if Is_Concurrent_Type (Typ)
9807 and then not Is_Generic_Actual_Type (Typ)
5d37ba92 9808 then
5d37ba92
ES
9809 In_Scope := False;
9810
9811 -- This case occurs when the concurrent type is declared within
9812 -- a generic unit. As a result the corresponding record has been
9813 -- built and used as the type of the first formal, we just have
9814 -- to retrieve the corresponding concurrent type.
9815
8aa15e3b 9816 elsif Is_Concurrent_Record_Type (Typ)
dd54644b 9817 and then not Is_Class_Wide_Type (Typ)
8aa15e3b 9818 and then Present (Corresponding_Concurrent_Type (Typ))
5d37ba92 9819 then
8aa15e3b 9820 Typ := Corresponding_Concurrent_Type (Typ);
5d37ba92
ES
9821 In_Scope := False;
9822
9823 else
9824 return;
9825 end if;
8aa15e3b
JM
9826 end if;
9827
9828 -- There is no overriding to check if is an inherited operation in a
9829 -- type derivation on for a generic actual.
9830
9831 Collect_Interfaces (Typ, Ifaces_List);
9832
9833 if Is_Empty_Elmt_List (Ifaces_List) then
5d37ba92
ES
9834 return;
9835 end if;
9836
8aa15e3b
JM
9837 -- Determine whether entry or subprogram Def_Id overrides a primitive
9838 -- operation that belongs to one of the interfaces in Ifaces_List.
5d37ba92 9839
8aa15e3b
JM
9840 declare
9841 Candidate : Entity_Id := Empty;
9842 Hom : Entity_Id := Empty;
9843 Iface_Typ : Entity_Id;
9844 Subp : Entity_Id := Empty;
9845
9846 begin
4adf3c50 9847 -- Traverse the homonym chain, looking for a potentially
8aa15e3b
JM
9848 -- overridden subprogram that belongs to an implemented
9849 -- interface.
9850
9851 Hom := Current_Entity_In_Scope (Def_Id);
9852 while Present (Hom) loop
9853 Subp := Hom;
9854
15e4986c
JM
9855 if Subp = Def_Id
9856 or else not Is_Overloadable (Subp)
9857 or else not Is_Primitive (Subp)
9858 or else not Is_Dispatching_Operation (Subp)
79afa047 9859 or else not Present (Find_Dispatching_Type (Subp))
15e4986c 9860 or else not Is_Interface (Find_Dispatching_Type (Subp))
8aa15e3b 9861 then
15e4986c 9862 null;
8aa15e3b 9863
15e4986c 9864 -- Entries and procedures can override abstract or null
4adf3c50 9865 -- interface procedures.
8aa15e3b 9866
15e4986c
JM
9867 elsif (Ekind (Def_Id) = E_Procedure
9868 or else Ekind (Def_Id) = E_Entry)
8aa15e3b 9869 and then Ekind (Subp) = E_Procedure
8aa15e3b
JM
9870 and then Matches_Prefixed_View_Profile
9871 (Parameter_Specifications (Parent (Def_Id)),
9872 Parameter_Specifications (Parent (Subp)))
9873 then
9874 Candidate := Subp;
9875
15e4986c
JM
9876 -- For an overridden subprogram Subp, check whether the mode
9877 -- of its first parameter is correct depending on the kind
9878 -- of synchronized type.
8aa15e3b 9879
15e4986c
JM
9880 declare
9881 Formal : constant Node_Id := First_Formal (Candidate);
9882
9883 begin
9884 -- In order for an entry or a protected procedure to
9885 -- override, the first parameter of the overridden
9886 -- routine must be of mode "out", "in out" or
9887 -- access-to-variable.
9888
9889 if (Ekind (Candidate) = E_Entry
9890 or else Ekind (Candidate) = E_Procedure)
9891 and then Is_Protected_Type (Typ)
9892 and then Ekind (Formal) /= E_In_Out_Parameter
9893 and then Ekind (Formal) /= E_Out_Parameter
9894 and then Nkind (Parameter_Type (Parent (Formal)))
9895 /= N_Access_Definition
9896 then
9897 null;
9898
9899 -- All other cases are OK since a task entry or routine
9900 -- does not have a restriction on the mode of the first
9901 -- parameter of the overridden interface routine.
9902
9903 else
9904 Overridden_Subp := Candidate;
9905 return;
9906 end if;
9907 end;
8aa15e3b
JM
9908
9909 -- Functions can override abstract interface functions
9910
9911 elsif Ekind (Def_Id) = E_Function
9912 and then Ekind (Subp) = E_Function
8aa15e3b
JM
9913 and then Matches_Prefixed_View_Profile
9914 (Parameter_Specifications (Parent (Def_Id)),
9915 Parameter_Specifications (Parent (Subp)))
9916 and then Etype (Result_Definition (Parent (Def_Id))) =
9917 Etype (Result_Definition (Parent (Subp)))
9918 then
9919 Overridden_Subp := Subp;
9920 return;
9921 end if;
9922
9923 Hom := Homonym (Hom);
9924 end loop;
9925
4adf3c50
AC
9926 -- After examining all candidates for overriding, we are left with
9927 -- the best match which is a mode incompatible interface routine.
9928 -- Do not emit an error if the Expander is active since this error
9929 -- will be detected later on after all concurrent types are
9930 -- expanded and all wrappers are built. This check is meant for
9931 -- spec-only compilations.
8aa15e3b 9932
4adf3c50 9933 if Present (Candidate) and then not Expander_Active then
8aa15e3b
JM
9934 Iface_Typ :=
9935 Find_Parameter_Type (Parent (First_Formal (Candidate)));
9936
4adf3c50
AC
9937 -- Def_Id is primitive of a protected type, declared inside the
9938 -- type, and the candidate is primitive of a limited or
9939 -- synchronized interface.
8aa15e3b
JM
9940
9941 if In_Scope
9942 and then Is_Protected_Type (Typ)
9943 and then
9944 (Is_Limited_Interface (Iface_Typ)
c199ccf7
AC
9945 or else Is_Protected_Interface (Iface_Typ)
9946 or else Is_Synchronized_Interface (Iface_Typ)
9947 or else Is_Task_Interface (Iface_Typ))
8aa15e3b 9948 then
dd54644b 9949 Error_Msg_PT (Parent (Typ), Candidate);
8aa15e3b 9950 end if;
5d37ba92 9951 end if;
8aa15e3b
JM
9952
9953 Overridden_Subp := Candidate;
9954 return;
9955 end;
5d37ba92
ES
9956 end Check_Synchronized_Overriding;
9957
9958 ----------------------------
9959 -- Is_Private_Declaration --
9960 ----------------------------
9961
9962 function Is_Private_Declaration (E : Entity_Id) return Boolean is
9963 Priv_Decls : List_Id;
9964 Decl : constant Node_Id := Unit_Declaration_Node (E);
9965
9966 begin
9967 if Is_Package_Or_Generic_Package (Current_Scope)
9968 and then In_Private_Part (Current_Scope)
9969 then
9970 Priv_Decls :=
a4901c08
AC
9971 Private_Declarations
9972 (Specification (Unit_Declaration_Node (Current_Scope)));
5d37ba92
ES
9973
9974 return In_Package_Body (Current_Scope)
9975 or else
9976 (Is_List_Member (Decl)
a4901c08 9977 and then List_Containing (Decl) = Priv_Decls)
5d37ba92 9978 or else (Nkind (Parent (Decl)) = N_Package_Specification
a4901c08
AC
9979 and then not
9980 Is_Compilation_Unit
9981 (Defining_Entity (Parent (Decl)))
9982 and then List_Containing (Parent (Parent (Decl))) =
9983 Priv_Decls);
5d37ba92
ES
9984 else
9985 return False;
9986 end if;
9987 end Is_Private_Declaration;
996ae0b0 9988
2ddc2000
AC
9989 --------------------------
9990 -- Is_Overriding_Alias --
9991 --------------------------
9992
9993 function Is_Overriding_Alias
9994 (Old_E : Entity_Id;
9995 New_E : Entity_Id) return Boolean
9996 is
9997 AO : constant Entity_Id := Alias (Old_E);
9998 AN : constant Entity_Id := Alias (New_E);
9999
10000 begin
10001 return Scope (AO) /= Scope (AN)
10002 or else No (DTC_Entity (AO))
10003 or else No (DTC_Entity (AN))
10004 or else DT_Position (AO) = DT_Position (AN);
10005 end Is_Overriding_Alias;
10006
996ae0b0
RK
10007 -- Start of processing for New_Overloaded_Entity
10008
10009 begin
fbf5a39b
AC
10010 -- We need to look for an entity that S may override. This must be a
10011 -- homonym in the current scope, so we look for the first homonym of
10012 -- S in the current scope as the starting point for the search.
10013
10014 E := Current_Entity_In_Scope (S);
10015
947430d5
AC
10016 -- Ada 2005 (AI-251): Derivation of abstract interface primitives.
10017 -- They are directly added to the list of primitive operations of
10018 -- Derived_Type, unless this is a rederivation in the private part
10019 -- of an operation that was already derived in the visible part of
10020 -- the current package.
10021
0791fbe9 10022 if Ada_Version >= Ada_2005
947430d5
AC
10023 and then Present (Derived_Type)
10024 and then Present (Alias (S))
10025 and then Is_Dispatching_Operation (Alias (S))
10026 and then Present (Find_Dispatching_Type (Alias (S)))
10027 and then Is_Interface (Find_Dispatching_Type (Alias (S)))
10028 then
10029 -- For private types, when the full-view is processed we propagate to
10030 -- the full view the non-overridden entities whose attribute "alias"
10031 -- references an interface primitive. These entities were added by
10032 -- Derive_Subprograms to ensure that interface primitives are
10033 -- covered.
10034
10035 -- Inside_Freeze_Actions is non zero when S corresponds with an
10036 -- internal entity that links an interface primitive with its
10037 -- covering primitive through attribute Interface_Alias (see
4adf3c50 10038 -- Add_Internal_Interface_Entities).
947430d5
AC
10039
10040 if Inside_Freezing_Actions = 0
10041 and then Is_Package_Or_Generic_Package (Current_Scope)
10042 and then In_Private_Part (Current_Scope)
10043 and then Nkind (Parent (E)) = N_Private_Extension_Declaration
10044 and then Nkind (Parent (S)) = N_Full_Type_Declaration
10045 and then Full_View (Defining_Identifier (Parent (E)))
10046 = Defining_Identifier (Parent (S))
10047 and then Alias (E) = Alias (S)
10048 then
10049 Check_Operation_From_Private_View (S, E);
10050 Set_Is_Dispatching_Operation (S);
10051
10052 -- Common case
10053
10054 else
10055 Enter_Overloaded_Entity (S);
10056 Check_Dispatching_Operation (S, Empty);
10057 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10058 end if;
10059
10060 return;
10061 end if;
10062
fbf5a39b
AC
10063 -- If there is no homonym then this is definitely not overriding
10064
996ae0b0
RK
10065 if No (E) then
10066 Enter_Overloaded_Entity (S);
10067 Check_Dispatching_Operation (S, Empty);
5d37ba92 10068 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
996ae0b0 10069
ec4867fa
ES
10070 -- If subprogram has an explicit declaration, check whether it
10071 -- has an overriding indicator.
758c442c 10072
ec4867fa 10073 if Comes_From_Source (S) then
8aa15e3b 10074 Check_Synchronized_Overriding (S, Overridden_Subp);
ea034236
AC
10075
10076 -- (Ada 2012: AI05-0125-1): If S is a dispatching operation then
10077 -- it may have overridden some hidden inherited primitive. Update
308e6f3a 10078 -- Overridden_Subp to avoid spurious errors when checking the
ea034236
AC
10079 -- overriding indicator.
10080
10081 if Ada_Version >= Ada_2012
10082 and then No (Overridden_Subp)
10083 and then Is_Dispatching_Operation (S)
038140ed 10084 and then Present (Overridden_Operation (S))
ea034236
AC
10085 then
10086 Overridden_Subp := Overridden_Operation (S);
10087 end if;
10088
5d37ba92
ES
10089 Check_Overriding_Indicator
10090 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
758c442c
GD
10091 end if;
10092
fbf5a39b
AC
10093 -- If there is a homonym that is not overloadable, then we have an
10094 -- error, except for the special cases checked explicitly below.
10095
996ae0b0
RK
10096 elsif not Is_Overloadable (E) then
10097
10098 -- Check for spurious conflict produced by a subprogram that has the
10099 -- same name as that of the enclosing generic package. The conflict
10100 -- occurs within an instance, between the subprogram and the renaming
10101 -- declaration for the package. After the subprogram, the package
10102 -- renaming declaration becomes hidden.
10103
10104 if Ekind (E) = E_Package
10105 and then Present (Renamed_Object (E))
10106 and then Renamed_Object (E) = Current_Scope
10107 and then Nkind (Parent (Renamed_Object (E))) =
10108 N_Package_Specification
10109 and then Present (Generic_Parent (Parent (Renamed_Object (E))))
10110 then
10111 Set_Is_Hidden (E);
10112 Set_Is_Immediately_Visible (E, False);
10113 Enter_Overloaded_Entity (S);
10114 Set_Homonym (S, Homonym (E));
10115 Check_Dispatching_Operation (S, Empty);
5d37ba92 10116 Check_Overriding_Indicator (S, Empty, Is_Primitive => False);
996ae0b0
RK
10117
10118 -- If the subprogram is implicit it is hidden by the previous
82c80734
RD
10119 -- declaration. However if it is dispatching, it must appear in the
10120 -- dispatch table anyway, because it can be dispatched to even if it
10121 -- cannot be called directly.
996ae0b0 10122
4adf3c50 10123 elsif Present (Alias (S)) and then not Comes_From_Source (S) then
996ae0b0
RK
10124 Set_Scope (S, Current_Scope);
10125
10126 if Is_Dispatching_Operation (Alias (S)) then
10127 Check_Dispatching_Operation (S, Empty);
10128 end if;
10129
10130 return;
10131
10132 else
10133 Error_Msg_Sloc := Sloc (E);
996ae0b0 10134
f3d57416 10135 -- Generate message, with useful additional warning if in generic
996ae0b0
RK
10136
10137 if Is_Generic_Unit (E) then
5d37ba92
ES
10138 Error_Msg_N ("previous generic unit cannot be overloaded", S);
10139 Error_Msg_N ("\& conflicts with declaration#", S);
10140 else
10141 Error_Msg_N ("& conflicts with declaration#", S);
996ae0b0
RK
10142 end if;
10143
10144 return;
10145 end if;
10146
fbf5a39b
AC
10147 -- E exists and is overloadable
10148
996ae0b0 10149 else
8aa15e3b 10150 Check_Synchronized_Overriding (S, Overridden_Subp);
758c442c 10151
82c80734
RD
10152 -- Loop through E and its homonyms to determine if any of them is
10153 -- the candidate for overriding by S.
996ae0b0
RK
10154
10155 while Present (E) loop
fbf5a39b
AC
10156
10157 -- Definitely not interesting if not in the current scope
10158
996ae0b0
RK
10159 if Scope (E) /= Current_Scope then
10160 null;
10161
25ebc085
AC
10162 -- Ada 2012 (AI05-0165): For internally generated bodies of
10163 -- null procedures locate the internally generated spec. We
10164 -- enforce mode conformance since a tagged type may inherit
10165 -- from interfaces several null primitives which differ only
10166 -- in the mode of the formals.
10167
10168 elsif not Comes_From_Source (S)
10169 and then Is_Null_Procedure (S)
10170 and then not Mode_Conformant (E, S)
10171 then
10172 null;
10173
fbf5a39b
AC
10174 -- Check if we have type conformance
10175
ec4867fa 10176 elsif Type_Conformant (E, S) then
c8ef728f 10177
82c80734
RD
10178 -- If the old and new entities have the same profile and one
10179 -- is not the body of the other, then this is an error, unless
10180 -- one of them is implicitly declared.
996ae0b0
RK
10181
10182 -- There are some cases when both can be implicit, for example
10183 -- when both a literal and a function that overrides it are
f3d57416 10184 -- inherited in a derivation, or when an inherited operation
ec4867fa 10185 -- of a tagged full type overrides the inherited operation of
f3d57416 10186 -- a private extension. Ada 83 had a special rule for the
885c4871 10187 -- literal case. In Ada 95, the later implicit operation hides
82c80734
RD
10188 -- the former, and the literal is always the former. In the
10189 -- odd case where both are derived operations declared at the
10190 -- same point, both operations should be declared, and in that
10191 -- case we bypass the following test and proceed to the next
df46b832
AC
10192 -- part. This can only occur for certain obscure cases in
10193 -- instances, when an operation on a type derived from a formal
10194 -- private type does not override a homograph inherited from
10195 -- the actual. In subsequent derivations of such a type, the
10196 -- DT positions of these operations remain distinct, if they
10197 -- have been set.
996ae0b0
RK
10198
10199 if Present (Alias (S))
10200 and then (No (Alias (E))
10201 or else Comes_From_Source (E)
2ddc2000 10202 or else Is_Abstract_Subprogram (S)
df46b832
AC
10203 or else
10204 (Is_Dispatching_Operation (E)
2ddc2000 10205 and then Is_Overriding_Alias (E, S)))
df46b832 10206 and then Ekind (E) /= E_Enumeration_Literal
996ae0b0 10207 then
82c80734
RD
10208 -- When an derived operation is overloaded it may be due to
10209 -- the fact that the full view of a private extension
996ae0b0
RK
10210 -- re-inherits. It has to be dealt with.
10211
e660dbf7 10212 if Is_Package_Or_Generic_Package (Current_Scope)
996ae0b0
RK
10213 and then In_Private_Part (Current_Scope)
10214 then
10215 Check_Operation_From_Private_View (S, E);
10216 end if;
10217
038140ed
AC
10218 -- In any case the implicit operation remains hidden by the
10219 -- existing declaration, which is overriding. Indicate that
10220 -- E overrides the operation from which S is inherited.
996ae0b0 10221
038140ed
AC
10222 if Present (Alias (S)) then
10223 Set_Overridden_Operation (E, Alias (S));
10224 else
10225 Set_Overridden_Operation (E, S);
10226 end if;
758c442c
GD
10227
10228 if Comes_From_Source (E) then
5d37ba92 10229 Check_Overriding_Indicator (E, S, Is_Primitive => False);
758c442c
GD
10230 end if;
10231
996ae0b0
RK
10232 return;
10233
26a43556
AC
10234 -- Within an instance, the renaming declarations for actual
10235 -- subprograms may become ambiguous, but they do not hide each
10236 -- other.
996ae0b0
RK
10237
10238 elsif Ekind (E) /= E_Entry
10239 and then not Comes_From_Source (E)
10240 and then not Is_Generic_Instance (E)
10241 and then (Present (Alias (E))
10242 or else Is_Intrinsic_Subprogram (E))
10243 and then (not In_Instance
10244 or else No (Parent (E))
10245 or else Nkind (Unit_Declaration_Node (E)) /=
8dbd1460 10246 N_Subprogram_Renaming_Declaration)
996ae0b0 10247 then
26a43556
AC
10248 -- A subprogram child unit is not allowed to override an
10249 -- inherited subprogram (10.1.1(20)).
996ae0b0
RK
10250
10251 if Is_Child_Unit (S) then
10252 Error_Msg_N
10253 ("child unit overrides inherited subprogram in parent",
10254 S);
10255 return;
10256 end if;
10257
10258 if Is_Non_Overriding_Operation (E, S) then
10259 Enter_Overloaded_Entity (S);
8dbd1460 10260
c8ef728f 10261 if No (Derived_Type)
996ae0b0
RK
10262 or else Is_Tagged_Type (Derived_Type)
10263 then
10264 Check_Dispatching_Operation (S, Empty);
10265 end if;
10266
10267 return;
10268 end if;
10269
10270 -- E is a derived operation or an internal operator which
10271 -- is being overridden. Remove E from further visibility.
10272 -- Furthermore, if E is a dispatching operation, it must be
10273 -- replaced in the list of primitive operations of its type
10274 -- (see Override_Dispatching_Operation).
10275
ec4867fa 10276 Overridden_Subp := E;
758c442c 10277
996ae0b0
RK
10278 declare
10279 Prev : Entity_Id;
10280
10281 begin
10282 Prev := First_Entity (Current_Scope);
996ae0b0
RK
10283 while Present (Prev)
10284 and then Next_Entity (Prev) /= E
10285 loop
10286 Next_Entity (Prev);
10287 end loop;
10288
10289 -- It is possible for E to be in the current scope and
10290 -- yet not in the entity chain. This can only occur in a
10291 -- generic context where E is an implicit concatenation
10292 -- in the formal part, because in a generic body the
10293 -- entity chain starts with the formals.
10294
10295 pragma Assert
10296 (Present (Prev) or else Chars (E) = Name_Op_Concat);
10297
10298 -- E must be removed both from the entity_list of the
10299 -- current scope, and from the visibility chain
10300
10301 if Debug_Flag_E then
10302 Write_Str ("Override implicit operation ");
10303 Write_Int (Int (E));
10304 Write_Eol;
10305 end if;
10306
10307 -- If E is a predefined concatenation, it stands for four
10308 -- different operations. As a result, a single explicit
10309 -- declaration does not hide it. In a possible ambiguous
10310 -- situation, Disambiguate chooses the user-defined op,
10311 -- so it is correct to retain the previous internal one.
10312
10313 if Chars (E) /= Name_Op_Concat
10314 or else Ekind (E) /= E_Operator
10315 then
10316 -- For nondispatching derived operations that are
10317 -- overridden by a subprogram declared in the private
8dbd1460
AC
10318 -- part of a package, we retain the derived subprogram
10319 -- but mark it as not immediately visible. If the
10320 -- derived operation was declared in the visible part
10321 -- then this ensures that it will still be visible
10322 -- outside the package with the proper signature
10323 -- (calls from outside must also be directed to this
10324 -- version rather than the overriding one, unlike the
10325 -- dispatching case). Calls from inside the package
10326 -- will still resolve to the overriding subprogram
10327 -- since the derived one is marked as not visible
10328 -- within the package.
996ae0b0
RK
10329
10330 -- If the private operation is dispatching, we achieve
10331 -- the overriding by keeping the implicit operation
9865d858 10332 -- but setting its alias to be the overriding one. In
996ae0b0
RK
10333 -- this fashion the proper body is executed in all
10334 -- cases, but the original signature is used outside
10335 -- of the package.
10336
10337 -- If the overriding is not in the private part, we
10338 -- remove the implicit operation altogether.
10339
10340 if Is_Private_Declaration (S) then
996ae0b0
RK
10341 if not Is_Dispatching_Operation (E) then
10342 Set_Is_Immediately_Visible (E, False);
10343 else
e895b435 10344 -- Work done in Override_Dispatching_Operation,
a46cde68 10345 -- so nothing else needs to be done here.
996ae0b0
RK
10346
10347 null;
10348 end if;
996ae0b0 10349
fbf5a39b
AC
10350 else
10351 -- Find predecessor of E in Homonym chain
996ae0b0
RK
10352
10353 if E = Current_Entity (E) then
10354 Prev_Vis := Empty;
10355 else
10356 Prev_Vis := Current_Entity (E);
10357 while Homonym (Prev_Vis) /= E loop
10358 Prev_Vis := Homonym (Prev_Vis);
10359 end loop;
10360 end if;
10361
10362 if Prev_Vis /= Empty then
10363
10364 -- Skip E in the visibility chain
10365
10366 Set_Homonym (Prev_Vis, Homonym (E));
10367
10368 else
10369 Set_Name_Entity_Id (Chars (E), Homonym (E));
10370 end if;
10371
10372 Set_Next_Entity (Prev, Next_Entity (E));
10373
10374 if No (Next_Entity (Prev)) then
10375 Set_Last_Entity (Current_Scope, Prev);
10376 end if;
996ae0b0
RK
10377 end if;
10378 end if;
10379
10380 Enter_Overloaded_Entity (S);
1c1289e7
AC
10381
10382 -- For entities generated by Derive_Subprograms the
10383 -- overridden operation is the inherited primitive
10384 -- (which is available through the attribute alias).
10385
10386 if not (Comes_From_Source (E))
10387 and then Is_Dispatching_Operation (E)
f9673bb0
AC
10388 and then Find_Dispatching_Type (E) =
10389 Find_Dispatching_Type (S)
1c1289e7
AC
10390 and then Present (Alias (E))
10391 and then Comes_From_Source (Alias (E))
10392 then
10393 Set_Overridden_Operation (S, Alias (E));
2fe829ae 10394
6320f5e1
AC
10395 -- Normal case of setting entity as overridden
10396
10397 -- Note: Static_Initialization and Overridden_Operation
10398 -- attributes use the same field in subprogram entities.
10399 -- Static_Initialization is only defined for internal
10400 -- initialization procedures, where Overridden_Operation
10401 -- is irrelevant. Therefore the setting of this attribute
10402 -- must check whether the target is an init_proc.
10403
2fe829ae 10404 elsif not Is_Init_Proc (S) then
1c1289e7
AC
10405 Set_Overridden_Operation (S, E);
10406 end if;
10407
5d37ba92 10408 Check_Overriding_Indicator (S, E, Is_Primitive => True);
996ae0b0 10409
fc53fe76 10410 -- If S is a user-defined subprogram or a null procedure
38ef8ebe
AC
10411 -- expanded to override an inherited null procedure, or a
10412 -- predefined dispatching primitive then indicate that E
038140ed 10413 -- overrides the operation from which S is inherited.
fc53fe76
AC
10414
10415 if Comes_From_Source (S)
10416 or else
10417 (Present (Parent (S))
10418 and then
10419 Nkind (Parent (S)) = N_Procedure_Specification
10420 and then
10421 Null_Present (Parent (S)))
38ef8ebe
AC
10422 or else
10423 (Present (Alias (E))
f16e8df9
RD
10424 and then
10425 Is_Predefined_Dispatching_Operation (Alias (E)))
fc53fe76 10426 then
c8ef728f 10427 if Present (Alias (E)) then
41251c60 10428 Set_Overridden_Operation (S, Alias (E));
41251c60
JM
10429 end if;
10430 end if;
10431
996ae0b0 10432 if Is_Dispatching_Operation (E) then
fbf5a39b 10433
82c80734 10434 -- An overriding dispatching subprogram inherits the
f9673bb0 10435 -- convention of the overridden subprogram (AI-117).
996ae0b0
RK
10436
10437 Set_Convention (S, Convention (E));
41251c60
JM
10438 Check_Dispatching_Operation (S, E);
10439
996ae0b0
RK
10440 else
10441 Check_Dispatching_Operation (S, Empty);
10442 end if;
10443
5d37ba92
ES
10444 Check_For_Primitive_Subprogram
10445 (Is_Primitive_Subp, Is_Overriding => True);
996ae0b0
RK
10446 goto Check_Inequality;
10447 end;
10448
10449 -- Apparent redeclarations in instances can occur when two
10450 -- formal types get the same actual type. The subprograms in
10451 -- in the instance are legal, even if not callable from the
10452 -- outside. Calls from within are disambiguated elsewhere.
10453 -- For dispatching operations in the visible part, the usual
10454 -- rules apply, and operations with the same profile are not
10455 -- legal (B830001).
10456
10457 elsif (In_Instance_Visible_Part
10458 and then not Is_Dispatching_Operation (E))
10459 or else In_Instance_Not_Visible
10460 then
10461 null;
10462
10463 -- Here we have a real error (identical profile)
10464
10465 else
10466 Error_Msg_Sloc := Sloc (E);
10467
10468 -- Avoid cascaded errors if the entity appears in
10469 -- subsequent calls.
10470
10471 Set_Scope (S, Current_Scope);
10472
5d37ba92
ES
10473 -- Generate error, with extra useful warning for the case
10474 -- of a generic instance with no completion.
996ae0b0
RK
10475
10476 if Is_Generic_Instance (S)
10477 and then not Has_Completion (E)
10478 then
10479 Error_Msg_N
5d37ba92
ES
10480 ("instantiation cannot provide body for&", S);
10481 Error_Msg_N ("\& conflicts with declaration#", S);
10482 else
10483 Error_Msg_N ("& conflicts with declaration#", S);
996ae0b0
RK
10484 end if;
10485
10486 return;
10487 end if;
10488
10489 else
c8ef728f
ES
10490 -- If one subprogram has an access parameter and the other
10491 -- a parameter of an access type, calls to either might be
10492 -- ambiguous. Verify that parameters match except for the
10493 -- access parameter.
10494
10495 if May_Hide_Profile then
10496 declare
ec4867fa
ES
10497 F1 : Entity_Id;
10498 F2 : Entity_Id;
8dbd1460 10499
c8ef728f
ES
10500 begin
10501 F1 := First_Formal (S);
10502 F2 := First_Formal (E);
10503 while Present (F1) and then Present (F2) loop
10504 if Is_Access_Type (Etype (F1)) then
10505 if not Is_Access_Type (Etype (F2))
10506 or else not Conforming_Types
10507 (Designated_Type (Etype (F1)),
10508 Designated_Type (Etype (F2)),
10509 Type_Conformant)
10510 then
10511 May_Hide_Profile := False;
10512 end if;
10513
10514 elsif
10515 not Conforming_Types
10516 (Etype (F1), Etype (F2), Type_Conformant)
10517 then
10518 May_Hide_Profile := False;
10519 end if;
10520
10521 Next_Formal (F1);
10522 Next_Formal (F2);
10523 end loop;
10524
10525 if May_Hide_Profile
10526 and then No (F1)
10527 and then No (F2)
10528 then
10529 Error_Msg_NE ("calls to& may be ambiguous?", S, S);
10530 end if;
10531 end;
10532 end if;
996ae0b0
RK
10533 end if;
10534
996ae0b0
RK
10535 E := Homonym (E);
10536 end loop;
10537
10538 -- On exit, we know that S is a new entity
10539
10540 Enter_Overloaded_Entity (S);
5d37ba92
ES
10541 Check_For_Primitive_Subprogram (Is_Primitive_Subp);
10542 Check_Overriding_Indicator
10543 (S, Overridden_Subp, Is_Primitive => Is_Primitive_Subp);
996ae0b0 10544
c4d67e2d 10545 -- Overloading is not allowed in SPARK, except for operators
8ed68165 10546
c4d67e2d
AC
10547 if Nkind (S) /= N_Defining_Operator_Symbol then
10548 Error_Msg_Sloc := Sloc (Homonym (S));
10549 Check_SPARK_Restriction
10550 ("overloading not allowed with entity#", S);
10551 end if;
8ed68165 10552
82c80734
RD
10553 -- If S is a derived operation for an untagged type then by
10554 -- definition it's not a dispatching operation (even if the parent
e917aec2
RD
10555 -- operation was dispatching), so Check_Dispatching_Operation is not
10556 -- called in that case.
996ae0b0 10557
c8ef728f 10558 if No (Derived_Type)
996ae0b0
RK
10559 or else Is_Tagged_Type (Derived_Type)
10560 then
10561 Check_Dispatching_Operation (S, Empty);
10562 end if;
10563 end if;
10564
82c80734
RD
10565 -- If this is a user-defined equality operator that is not a derived
10566 -- subprogram, create the corresponding inequality. If the operation is
10567 -- dispatching, the expansion is done elsewhere, and we do not create
10568 -- an explicit inequality operation.
996ae0b0
RK
10569
10570 <<Check_Inequality>>
10571 if Chars (S) = Name_Op_Eq
10572 and then Etype (S) = Standard_Boolean
10573 and then Present (Parent (S))
10574 and then not Is_Dispatching_Operation (S)
10575 then
10576 Make_Inequality_Operator (S);
d151d6a3 10577
dbe945f1 10578 if Ada_Version >= Ada_2012 then
e5a58fac
AC
10579 Check_Untagged_Equality (S);
10580 end if;
996ae0b0 10581 end if;
996ae0b0
RK
10582 end New_Overloaded_Entity;
10583
10584 ---------------------
10585 -- Process_Formals --
10586 ---------------------
10587
10588 procedure Process_Formals
07fc65c4 10589 (T : List_Id;
996ae0b0
RK
10590 Related_Nod : Node_Id)
10591 is
10592 Param_Spec : Node_Id;
10593 Formal : Entity_Id;
10594 Formal_Type : Entity_Id;
10595 Default : Node_Id;
10596 Ptype : Entity_Id;
10597
800621e0
RD
10598 Num_Out_Params : Nat := 0;
10599 First_Out_Param : Entity_Id := Empty;
21d27997 10600 -- Used for setting Is_Only_Out_Parameter
800621e0 10601
950d217a
AC
10602 function Designates_From_With_Type (Typ : Entity_Id) return Boolean;
10603 -- Determine whether an access type designates a type coming from a
10604 -- limited view.
10605
07fc65c4 10606 function Is_Class_Wide_Default (D : Node_Id) return Boolean;
82c80734
RD
10607 -- Check whether the default has a class-wide type. After analysis the
10608 -- default has the type of the formal, so we must also check explicitly
10609 -- for an access attribute.
07fc65c4 10610
950d217a
AC
10611 -------------------------------
10612 -- Designates_From_With_Type --
10613 -------------------------------
10614
10615 function Designates_From_With_Type (Typ : Entity_Id) return Boolean is
10616 Desig : Entity_Id := Typ;
10617
10618 begin
10619 if Is_Access_Type (Desig) then
10620 Desig := Directly_Designated_Type (Desig);
10621 end if;
10622
10623 if Is_Class_Wide_Type (Desig) then
10624 Desig := Root_Type (Desig);
10625 end if;
10626
10627 return
10628 Ekind (Desig) = E_Incomplete_Type
10629 and then From_With_Type (Desig);
10630 end Designates_From_With_Type;
10631
07fc65c4
GB
10632 ---------------------------
10633 -- Is_Class_Wide_Default --
10634 ---------------------------
10635
10636 function Is_Class_Wide_Default (D : Node_Id) return Boolean is
10637 begin
10638 return Is_Class_Wide_Type (Designated_Type (Etype (D)))
10639 or else (Nkind (D) = N_Attribute_Reference
0f853035
YM
10640 and then Attribute_Name (D) = Name_Access
10641 and then Is_Class_Wide_Type (Etype (Prefix (D))));
07fc65c4
GB
10642 end Is_Class_Wide_Default;
10643
10644 -- Start of processing for Process_Formals
10645
996ae0b0
RK
10646 begin
10647 -- In order to prevent premature use of the formals in the same formal
10648 -- part, the Ekind is left undefined until all default expressions are
10649 -- analyzed. The Ekind is established in a separate loop at the end.
10650
10651 Param_Spec := First (T);
996ae0b0 10652 while Present (Param_Spec) loop
996ae0b0 10653 Formal := Defining_Identifier (Param_Spec);
5d37ba92 10654 Set_Never_Set_In_Source (Formal, True);
996ae0b0
RK
10655 Enter_Name (Formal);
10656
10657 -- Case of ordinary parameters
10658
10659 if Nkind (Parameter_Type (Param_Spec)) /= N_Access_Definition then
10660 Find_Type (Parameter_Type (Param_Spec));
10661 Ptype := Parameter_Type (Param_Spec);
10662
10663 if Ptype = Error then
10664 goto Continue;
10665 end if;
10666
10667 Formal_Type := Entity (Ptype);
10668
ec4867fa
ES
10669 if Is_Incomplete_Type (Formal_Type)
10670 or else
10671 (Is_Class_Wide_Type (Formal_Type)
10672 and then Is_Incomplete_Type (Root_Type (Formal_Type)))
996ae0b0 10673 then
93bcda23
AC
10674 -- Ada 2005 (AI-326): Tagged incomplete types allowed in
10675 -- primitive operations, as long as their completion is
10676 -- in the same declarative part. If in the private part
10677 -- this means that the type cannot be a Taft-amendment type.
cec29135
ES
10678 -- Check is done on package exit. For access to subprograms,
10679 -- the use is legal for Taft-amendment types.
fbf5a39b 10680
6eddd7b4
AC
10681 -- Ada 2012: tagged incomplete types are allowed as generic
10682 -- formal types. They do not introduce dependencies and the
10683 -- corresponding generic subprogram does not have a delayed
10684 -- freeze, because it does not need a freeze node.
10685
d8db0bca 10686 if Is_Tagged_Type (Formal_Type) then
93bcda23 10687 if Ekind (Scope (Current_Scope)) = E_Package
93bcda23 10688 and then not From_With_Type (Formal_Type)
6eddd7b4 10689 and then not Is_Generic_Type (Formal_Type)
93bcda23
AC
10690 and then not Is_Class_Wide_Type (Formal_Type)
10691 then
cec29135
ES
10692 if not Nkind_In
10693 (Parent (T), N_Access_Function_Definition,
10694 N_Access_Procedure_Definition)
10695 then
10696 Append_Elmt
10697 (Current_Scope,
10698 Private_Dependents (Base_Type (Formal_Type)));
4637729f
AC
10699
10700 -- Freezing is delayed to ensure that Register_Prim
10701 -- will get called for this operation, which is needed
10702 -- in cases where static dispatch tables aren't built.
10703 -- (Note that the same is done for controlling access
10704 -- parameter cases in function Access_Definition.)
10705
10706 Set_Has_Delayed_Freeze (Current_Scope);
cec29135 10707 end if;
93bcda23 10708 end if;
fbf5a39b 10709
0a36105d
JM
10710 -- Special handling of Value_Type for CIL case
10711
10712 elsif Is_Value_Type (Formal_Type) then
10713 null;
10714
800621e0
RD
10715 elsif not Nkind_In (Parent (T), N_Access_Function_Definition,
10716 N_Access_Procedure_Definition)
996ae0b0 10717 then
dd386db0
AC
10718 -- AI05-0151: Tagged incomplete types are allowed in all
10719 -- formal parts. Untagged incomplete types are not allowed
10720 -- in bodies.
10721
10722 if Ada_Version >= Ada_2012 then
10723 if Is_Tagged_Type (Formal_Type) then
10724 null;
10725
0f1a6a0b
AC
10726 elsif Nkind_In (Parent (Parent (T)), N_Accept_Statement,
10727 N_Entry_Body,
10728 N_Subprogram_Body)
dd386db0
AC
10729 then
10730 Error_Msg_NE
10731 ("invalid use of untagged incomplete type&",
0f1a6a0b 10732 Ptype, Formal_Type);
dd386db0
AC
10733 end if;
10734
10735 else
10736 Error_Msg_NE
10737 ("invalid use of incomplete type&",
0f1a6a0b 10738 Param_Spec, Formal_Type);
dd386db0
AC
10739
10740 -- Further checks on the legality of incomplete types
10741 -- in formal parts are delayed until the freeze point
10742 -- of the enclosing subprogram or access to subprogram.
10743 end if;
996ae0b0
RK
10744 end if;
10745
10746 elsif Ekind (Formal_Type) = E_Void then
0f1a6a0b
AC
10747 Error_Msg_NE
10748 ("premature use of&",
10749 Parameter_Type (Param_Spec), Formal_Type);
996ae0b0
RK
10750 end if;
10751
fecbd779
AC
10752 -- Ada 2012 (AI-142): Handle aliased parameters
10753
10754 if Ada_Version >= Ada_2012
10755 and then Aliased_Present (Param_Spec)
10756 then
10757 Set_Is_Aliased (Formal);
10758 end if;
10759
0ab80019 10760 -- Ada 2005 (AI-231): Create and decorate an internal subtype
7324bf49 10761 -- declaration corresponding to the null-excluding type of the
d8db0bca
JM
10762 -- formal in the enclosing scope. Finally, replace the parameter
10763 -- type of the formal with the internal subtype.
7324bf49 10764
0791fbe9 10765 if Ada_Version >= Ada_2005
41251c60 10766 and then Null_Exclusion_Present (Param_Spec)
7324bf49 10767 then
ec4867fa 10768 if not Is_Access_Type (Formal_Type) then
ed2233dc 10769 Error_Msg_N
0a36105d
JM
10770 ("`NOT NULL` allowed only for an access type", Param_Spec);
10771
ec4867fa
ES
10772 else
10773 if Can_Never_Be_Null (Formal_Type)
10774 and then Comes_From_Source (Related_Nod)
10775 then
ed2233dc 10776 Error_Msg_NE
0a36105d 10777 ("`NOT NULL` not allowed (& already excludes null)",
0f1a6a0b 10778 Param_Spec, Formal_Type);
ec4867fa 10779 end if;
41251c60 10780
ec4867fa
ES
10781 Formal_Type :=
10782 Create_Null_Excluding_Itype
10783 (T => Formal_Type,
10784 Related_Nod => Related_Nod,
10785 Scope_Id => Scope (Current_Scope));
0a36105d 10786
fcf848c4
AC
10787 -- If the designated type of the itype is an itype that is
10788 -- not frozen yet, we set the Has_Delayed_Freeze attribute
10789 -- on the access subtype, to prevent order-of-elaboration
10790 -- issues in the backend.
0a36105d
JM
10791
10792 -- Example:
10793 -- type T is access procedure;
10794 -- procedure Op (O : not null T);
10795
fcf848c4
AC
10796 if Is_Itype (Directly_Designated_Type (Formal_Type))
10797 and then
10798 not Is_Frozen (Directly_Designated_Type (Formal_Type))
10799 then
0a36105d
JM
10800 Set_Has_Delayed_Freeze (Formal_Type);
10801 end if;
ec4867fa 10802 end if;
7324bf49
AC
10803 end if;
10804
996ae0b0
RK
10805 -- An access formal type
10806
10807 else
10808 Formal_Type :=
10809 Access_Definition (Related_Nod, Parameter_Type (Param_Spec));
7324bf49 10810
f937473f
RD
10811 -- No need to continue if we already notified errors
10812
10813 if not Present (Formal_Type) then
10814 return;
10815 end if;
10816
0ab80019 10817 -- Ada 2005 (AI-254)
7324bf49 10818
af4b9434
AC
10819 declare
10820 AD : constant Node_Id :=
10821 Access_To_Subprogram_Definition
10822 (Parameter_Type (Param_Spec));
10823 begin
10824 if Present (AD) and then Protected_Present (AD) then
10825 Formal_Type :=
10826 Replace_Anonymous_Access_To_Protected_Subprogram
f937473f 10827 (Param_Spec);
af4b9434
AC
10828 end if;
10829 end;
996ae0b0
RK
10830 end if;
10831
10832 Set_Etype (Formal, Formal_Type);
0f853035 10833
fecbd779
AC
10834 -- Deal with default expression if present
10835
fbf5a39b 10836 Default := Expression (Param_Spec);
996ae0b0
RK
10837
10838 if Present (Default) then
2ba431e5 10839 Check_SPARK_Restriction
fe5d3068 10840 ("default expression is not allowed", Default);
38171f43 10841
996ae0b0 10842 if Out_Present (Param_Spec) then
ed2233dc 10843 Error_Msg_N
996ae0b0
RK
10844 ("default initialization only allowed for IN parameters",
10845 Param_Spec);
10846 end if;
10847
10848 -- Do the special preanalysis of the expression (see section on
10849 -- "Handling of Default Expressions" in the spec of package Sem).
10850
21d27997 10851 Preanalyze_Spec_Expression (Default, Formal_Type);
996ae0b0 10852
f29b857f
ES
10853 -- An access to constant cannot be the default for
10854 -- an access parameter that is an access to variable.
2eb160f2
ST
10855
10856 if Ekind (Formal_Type) = E_Anonymous_Access_Type
10857 and then not Is_Access_Constant (Formal_Type)
10858 and then Is_Access_Type (Etype (Default))
10859 and then Is_Access_Constant (Etype (Default))
10860 then
f29b857f
ES
10861 Error_Msg_N
10862 ("formal that is access to variable cannot be initialized " &
10863 "with an access-to-constant expression", Default);
2eb160f2
ST
10864 end if;
10865
d8db0bca
JM
10866 -- Check that the designated type of an access parameter's default
10867 -- is not a class-wide type unless the parameter's designated type
10868 -- is also class-wide.
996ae0b0
RK
10869
10870 if Ekind (Formal_Type) = E_Anonymous_Access_Type
950d217a 10871 and then not Designates_From_With_Type (Formal_Type)
07fc65c4 10872 and then Is_Class_Wide_Default (Default)
996ae0b0
RK
10873 and then not Is_Class_Wide_Type (Designated_Type (Formal_Type))
10874 then
07fc65c4
GB
10875 Error_Msg_N
10876 ("access to class-wide expression not allowed here", Default);
996ae0b0 10877 end if;
4755cce9
JM
10878
10879 -- Check incorrect use of dynamically tagged expressions
10880
10881 if Is_Tagged_Type (Formal_Type) then
10882 Check_Dynamically_Tagged_Expression
10883 (Expr => Default,
10884 Typ => Formal_Type,
10885 Related_Nod => Default);
10886 end if;
996ae0b0
RK
10887 end if;
10888
41251c60
JM
10889 -- Ada 2005 (AI-231): Static checks
10890
0791fbe9 10891 if Ada_Version >= Ada_2005
41251c60
JM
10892 and then Is_Access_Type (Etype (Formal))
10893 and then Can_Never_Be_Null (Etype (Formal))
10894 then
10895 Null_Exclusion_Static_Checks (Param_Spec);
10896 end if;
10897
996ae0b0
RK
10898 <<Continue>>
10899 Next (Param_Spec);
10900 end loop;
10901
82c80734
RD
10902 -- If this is the formal part of a function specification, analyze the
10903 -- subtype mark in the context where the formals are visible but not
10904 -- yet usable, and may hide outer homographs.
10905
10906 if Nkind (Related_Nod) = N_Function_Specification then
10907 Analyze_Return_Type (Related_Nod);
10908 end if;
10909
996ae0b0
RK
10910 -- Now set the kind (mode) of each formal
10911
10912 Param_Spec := First (T);
996ae0b0
RK
10913 while Present (Param_Spec) loop
10914 Formal := Defining_Identifier (Param_Spec);
10915 Set_Formal_Mode (Formal);
10916
10917 if Ekind (Formal) = E_In_Parameter then
10918 Set_Default_Value (Formal, Expression (Param_Spec));
10919
10920 if Present (Expression (Param_Spec)) then
10921 Default := Expression (Param_Spec);
10922
10923 if Is_Scalar_Type (Etype (Default)) then
5ebfaacf
AC
10924 if Nkind (Parameter_Type (Param_Spec)) /=
10925 N_Access_Definition
996ae0b0
RK
10926 then
10927 Formal_Type := Entity (Parameter_Type (Param_Spec));
996ae0b0 10928 else
5ebfaacf
AC
10929 Formal_Type :=
10930 Access_Definition
10931 (Related_Nod, Parameter_Type (Param_Spec));
996ae0b0
RK
10932 end if;
10933
10934 Apply_Scalar_Range_Check (Default, Formal_Type);
10935 end if;
2820d220 10936 end if;
800621e0
RD
10937
10938 elsif Ekind (Formal) = E_Out_Parameter then
10939 Num_Out_Params := Num_Out_Params + 1;
10940
10941 if Num_Out_Params = 1 then
10942 First_Out_Param := Formal;
10943 end if;
10944
10945 elsif Ekind (Formal) = E_In_Out_Parameter then
10946 Num_Out_Params := Num_Out_Params + 1;
996ae0b0
RK
10947 end if;
10948
4172a8e3
AC
10949 -- Skip remaining processing if formal type was in error
10950
10951 if Etype (Formal) = Any_Type or else Error_Posted (Formal) then
10952 goto Next_Parameter;
10953 end if;
10954
fecbd779
AC
10955 -- Force call by reference if aliased
10956
10957 if Is_Aliased (Formal) then
10958 Set_Mechanism (Formal, By_Reference);
5ebfaacf
AC
10959
10960 -- Warn if user asked this to be passed by copy
10961
10962 if Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10963 Error_Msg_N
10964 ("?cannot pass aliased parameter & by copy", Formal);
10965 end if;
10966
10967 -- Force mechanism if type has Convention Ada_Pass_By_Ref/Copy
10968
10969 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Copy then
10970 Set_Mechanism (Formal, By_Copy);
10971
10972 elsif Convention (Formal_Type) = Convention_Ada_Pass_By_Reference then
10973 Set_Mechanism (Formal, By_Reference);
fecbd779
AC
10974 end if;
10975
4172a8e3 10976 <<Next_Parameter>>
996ae0b0
RK
10977 Next (Param_Spec);
10978 end loop;
800621e0
RD
10979
10980 if Present (First_Out_Param) and then Num_Out_Params = 1 then
10981 Set_Is_Only_Out_Parameter (First_Out_Param);
10982 end if;
996ae0b0
RK
10983 end Process_Formals;
10984
21d27997
RD
10985 ------------------
10986 -- Process_PPCs --
10987 ------------------
10988
10989 procedure Process_PPCs
10990 (N : Node_Id;
10991 Spec_Id : Entity_Id;
10992 Body_Id : Entity_Id)
10993 is
10994 Loc : constant Source_Ptr := Sloc (N);
10995 Prag : Node_Id;
21d27997
RD
10996 Parms : List_Id;
10997
e606088a
AC
10998 Designator : Entity_Id;
10999 -- Subprogram designator, set from Spec_Id if present, else Body_Id
11000
beacce02
AC
11001 Precond : Node_Id := Empty;
11002 -- Set non-Empty if we prepend precondition to the declarations. This
11003 -- is used to hook up inherited preconditions (adding the condition
11004 -- expression with OR ELSE, and adding the message).
11005
11006 Inherited_Precond : Node_Id;
11007 -- Precondition inherited from parent subprogram
11008
11009 Inherited : constant Subprogram_List :=
e606088a
AC
11010 Inherited_Subprograms (Spec_Id);
11011 -- List of subprograms inherited by this subprogram
beacce02
AC
11012
11013 Plist : List_Id := No_List;
11014 -- List of generated postconditions
11015
90e85233
YM
11016 function Grab_CC return Node_Id;
11017 -- Prag contains an analyzed contract case pragma. This function copies
11018 -- relevant components of the pragma, creates the corresponding Check
11019 -- pragma and returns the Check pragma as the result.
11020
f0709ca6
AC
11021 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id;
11022 -- Prag contains an analyzed precondition or postcondition pragma. This
11023 -- function copies the pragma, changes it to the corresponding Check
11024 -- pragma and returns the Check pragma as the result. If Pspec is non-
11025 -- empty, this is the case of inheriting a PPC, where we must change
11026 -- references to parameters of the inherited subprogram to point to the
11027 -- corresponding parameters of the current subprogram.
21d27997 11028
b4ca2d2c
AC
11029 function Invariants_Or_Predicates_Present return Boolean;
11030 -- Determines if any invariants or predicates are present for any OUT
11031 -- or IN OUT parameters of the subprogram, or (for a function) if the
11032 -- return value has an invariant.
e606088a 11033
a4901c08
AC
11034 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean;
11035 -- T is the entity for a private type for which invariants are defined.
11036 -- This function returns True if the procedure corresponding to the
11037 -- value of Designator is a public procedure from the point of view of
11038 -- this type (i.e. its spec is in the visible part of the package that
11039 -- contains the declaration of the private type). A True value means
11040 -- that an invariant check is required (for an IN OUT parameter, or
11041 -- the returned value of a function.
11042
e228f7ee
AC
11043 function Last_Implicit_Declaration return Node_Id;
11044 -- Return the last internally-generated declaration of N
11045
90e85233
YM
11046 -------------
11047 -- Grab_CC --
11048 -------------
11049
11050 function Grab_CC return Node_Id is
b285815e 11051 Loc : constant Source_Ptr := Sloc (Prag);
90e85233
YM
11052 CP : Node_Id;
11053 Req : Node_Id;
11054 Ens : Node_Id;
11055 Post : Node_Id;
90e85233 11056
b285815e
RD
11057 -- As with postcondition, the string is "failed xx from yy" where
11058 -- xx is in all lower case. The reason for this different wording
11059 -- compared to other Check cases is that the failure is not at the
11060 -- point of occurrence of the pragma, unlike the other Check cases.
90e85233
YM
11061
11062 Msg : constant String :=
11063 "failed contract case from " & Build_Location_String (Loc);
11064
11065 begin
11066 -- Copy the Requires and Ensures expressions
11067
b285815e 11068 Req := New_Copy_Tree
ce6002ec 11069 (Expression (Get_Requires_From_CTC_Pragma (Prag)),
b285815e 11070 New_Scope => Current_Scope);
90e85233 11071
b285815e 11072 Ens := New_Copy_Tree
ce6002ec 11073 (Expression (Get_Ensures_From_CTC_Pragma (Prag)),
b285815e 11074 New_Scope => Current_Scope);
90e85233
YM
11075
11076 -- Build the postcondition (not Requires'Old or else Ensures)
11077
b285815e
RD
11078 Post :=
11079 Make_Or_Else (Loc,
11080 Left_Opnd =>
11081 Make_Op_Not (Loc,
11082 Make_Attribute_Reference (Loc,
11083 Prefix => Req,
11084 Attribute_Name => Name_Old)),
11085 Right_Opnd => Ens);
90e85233
YM
11086
11087 -- For a contract case pragma within a generic, generate a
11088 -- postcondition pragma for later expansion. This is also used
11089 -- when an error was detected, thus setting Expander_Active to False.
11090
11091 if not Expander_Active then
b285815e
RD
11092 CP :=
11093 Make_Pragma (Loc,
11094 Chars => Name_Postcondition,
11095 Pragma_Argument_Associations => New_List (
11096 Make_Pragma_Argument_Association (Loc,
11097 Chars => Name_Check,
11098 Expression => Post),
11099
11100 Make_Pragma_Argument_Association (Loc,
11101 Chars => Name_Message,
11102 Expression => Make_String_Literal (Loc, Msg))));
90e85233
YM
11103
11104 -- Otherwise, create the Check pragma
11105
11106 else
b285815e
RD
11107 CP :=
11108 Make_Pragma (Loc,
11109 Chars => Name_Check,
11110 Pragma_Argument_Associations => New_List (
11111 Make_Pragma_Argument_Association (Loc,
11112 Chars => Name_Name,
11113 Expression => Make_Identifier (Loc, Name_Postcondition)),
90e85233 11114
b285815e
RD
11115 Make_Pragma_Argument_Association (Loc,
11116 Chars => Name_Check,
11117 Expression => Post),
90e85233 11118
b285815e
RD
11119 Make_Pragma_Argument_Association (Loc,
11120 Chars => Name_Message,
11121 Expression => Make_String_Literal (Loc, Msg))));
90e85233
YM
11122 end if;
11123
11124 -- Return the Postcondition or Check pragma
11125
11126 return CP;
11127 end Grab_CC;
11128
21d27997
RD
11129 --------------
11130 -- Grab_PPC --
11131 --------------
11132
f0709ca6
AC
11133 function Grab_PPC (Pspec : Entity_Id := Empty) return Node_Id is
11134 Nam : constant Name_Id := Pragma_Name (Prag);
11135 Map : Elist_Id;
11136 CP : Node_Id;
21d27997
RD
11137
11138 begin
f0709ca6
AC
11139 -- Prepare map if this is the case where we have to map entities of
11140 -- arguments in the overridden subprogram to corresponding entities
11141 -- of the current subprogram.
11142
11143 if No (Pspec) then
11144 Map := No_Elist;
11145
11146 else
11147 declare
11148 PF : Entity_Id;
11149 CF : Entity_Id;
11150
11151 begin
11152 Map := New_Elmt_List;
11153 PF := First_Formal (Pspec);
e606088a 11154 CF := First_Formal (Designator);
f0709ca6
AC
11155 while Present (PF) loop
11156 Append_Elmt (PF, Map);
11157 Append_Elmt (CF, Map);
11158 Next_Formal (PF);
11159 Next_Formal (CF);
11160 end loop;
11161 end;
11162 end if;
11163
308e6f3a 11164 -- Now we can copy the tree, doing any required substitutions
f0709ca6
AC
11165
11166 CP := New_Copy_Tree (Prag, Map => Map, New_Scope => Current_Scope);
11167
21d27997
RD
11168 -- Set Analyzed to false, since we want to reanalyze the check
11169 -- procedure. Note that it is only at the outer level that we
11170 -- do this fiddling, for the spec cases, the already preanalyzed
11171 -- parameters are not affected.
766d7add 11172
1fb00064
AC
11173 Set_Analyzed (CP, False);
11174
11175 -- We also make sure Comes_From_Source is False for the copy
11176
11177 Set_Comes_From_Source (CP, False);
11178
0dabde3a 11179 -- For a postcondition pragma within a generic, preserve the pragma
90e85233
YM
11180 -- for later expansion. This is also used when an error was detected,
11181 -- thus setting Expander_Active to False.
21d27997 11182
0dabde3a
ES
11183 if Nam = Name_Postcondition
11184 and then not Expander_Active
11185 then
11186 return CP;
11187 end if;
11188
1fb00064 11189 -- Change copy of pragma into corresponding pragma Check
21d27997
RD
11190
11191 Prepend_To (Pragma_Argument_Associations (CP),
11192 Make_Pragma_Argument_Association (Sloc (Prag),
7675ad4f
AC
11193 Expression => Make_Identifier (Loc, Nam)));
11194 Set_Pragma_Identifier (CP, Make_Identifier (Sloc (Prag), Name_Check));
21d27997 11195
beacce02
AC
11196 -- If this is inherited case and the current message starts with
11197 -- "failed p", we change it to "failed inherited p...".
f0709ca6
AC
11198
11199 if Present (Pspec) then
beacce02
AC
11200 declare
11201 Msg : constant Node_Id :=
11202 Last (Pragma_Argument_Associations (CP));
11203
11204 begin
11205 if Chars (Msg) = Name_Message then
11206 String_To_Name_Buffer (Strval (Expression (Msg)));
11207
11208 if Name_Buffer (1 .. 8) = "failed p" then
11209 Insert_Str_In_Name_Buffer ("inherited ", 8);
11210 Set_Strval
11211 (Expression (Last (Pragma_Argument_Associations (CP))),
11212 String_From_Name_Buffer);
11213 end if;
11214 end if;
11215 end;
f0709ca6
AC
11216 end if;
11217
11218 -- Return the check pragma
11219
21d27997
RD
11220 return CP;
11221 end Grab_PPC;
11222
b4ca2d2c
AC
11223 --------------------------------------
11224 -- Invariants_Or_Predicates_Present --
11225 --------------------------------------
e606088a 11226
b4ca2d2c
AC
11227 function Invariants_Or_Predicates_Present return Boolean is
11228 Formal : Entity_Id;
e606088a
AC
11229
11230 begin
11231 -- Check function return result
11232
11233 if Ekind (Designator) /= E_Procedure
11234 and then Has_Invariants (Etype (Designator))
11235 then
11236 return True;
11237 end if;
11238
11239 -- Check parameters
11240
11241 Formal := First_Formal (Designator);
11242 while Present (Formal) loop
11243 if Ekind (Formal) /= E_In_Parameter
b4ca2d2c
AC
11244 and then
11245 (Has_Invariants (Etype (Formal))
11246 or else Present (Predicate_Function (Etype (Formal))))
e606088a
AC
11247 then
11248 return True;
11249 end if;
11250
11251 Next_Formal (Formal);
11252 end loop;
11253
11254 return False;
b4ca2d2c 11255 end Invariants_Or_Predicates_Present;
e606088a 11256
a4901c08
AC
11257 ------------------------------
11258 -- Is_Public_Subprogram_For --
11259 ------------------------------
11260
11261 -- The type T is a private type, its declaration is therefore in
11262 -- the list of public declarations of some package. The test for a
11263 -- public subprogram is that its declaration is in this same list
11264 -- of declarations for the same package (note that all the public
11265 -- declarations are in one list, and all the private declarations
11266 -- in another, so this deals with the public/private distinction).
11267
11268 function Is_Public_Subprogram_For (T : Entity_Id) return Boolean is
11269 DD : constant Node_Id := Unit_Declaration_Node (Designator);
11270 -- The subprogram declaration for the subprogram in question
11271
11272 TL : constant List_Id :=
11273 Visible_Declarations
11274 (Specification (Unit_Declaration_Node (Scope (T))));
11275 -- The list of declarations containing the private declaration of
11276 -- the type. We know it is a private type, so we know its scope is
11277 -- the package in question, and we know it must be in the visible
11278 -- declarations of this package.
11279
11280 begin
11281 -- If the subprogram declaration is not a list member, it must be
11282 -- an Init_Proc, in which case we want to consider it to be a
11283 -- public subprogram, since we do get initializations to deal with.
11284
11285 if not Is_List_Member (DD) then
11286 return True;
11287
11288 -- Otherwise we test whether the subprogram is declared in the
11289 -- visible declarations of the package containing the type.
11290
11291 else
11292 return TL = List_Containing (DD);
11293 end if;
11294 end Is_Public_Subprogram_For;
11295
e228f7ee
AC
11296 -------------------------------
11297 -- Last_Implicit_Declaration --
11298 -------------------------------
11299
11300 function Last_Implicit_Declaration return Node_Id is
11301 Loc : constant Source_Ptr := Sloc (N);
11302 Decls : List_Id := Declarations (N);
11303 Decl : Node_Id;
11304 Succ : Node_Id;
11305
11306 begin
11307 if No (Decls) then
11308 Decls := New_List (Make_Null_Statement (Loc));
11309 Set_Declarations (N, Decls);
11310
11311 elsif Is_Empty_List (Declarations (N)) then
11312 Append_To (Decls, Make_Null_Statement (Loc));
11313 end if;
11314
11315 -- Implicit and source declarations may be interspersed. Search for
11316 -- the last implicit declaration which is either succeeded by a
11317 -- source construct or is the last node in the declarative list.
11318
11319 Decl := First (Declarations (N));
11320 while Present (Decl) loop
11321 Succ := Next (Decl);
11322
11323 -- The current declaration is the last one, do not return Empty
11324
11325 if No (Succ) then
11326 exit;
11327
11328 -- The successor is a source construct
11329
11330 elsif Comes_From_Source (Succ) then
11331 exit;
11332 end if;
11333
11334 Next (Decl);
11335 end loop;
11336
11337 return Decl;
11338 end Last_Implicit_Declaration;
11339
21d27997
RD
11340 -- Start of processing for Process_PPCs
11341
11342 begin
e606088a
AC
11343 -- Capture designator from spec if present, else from body
11344
11345 if Present (Spec_Id) then
11346 Designator := Spec_Id;
11347 else
11348 Designator := Body_Id;
11349 end if;
11350
62db841a 11351 -- Internally generated subprograms, such as type-specific functions,
844ec038 11352 -- don't get assertion checks.
62db841a
AC
11353
11354 if Get_TSS_Name (Designator) /= TSS_Null then
11355 return;
11356 end if;
11357
21d27997
RD
11358 -- Grab preconditions from spec
11359
11360 if Present (Spec_Id) then
11361
11362 -- Loop through PPC pragmas from spec. Note that preconditions from
11363 -- the body will be analyzed and converted when we scan the body
11364 -- declarations below.
11365
dac3bede 11366 Prag := Spec_PPC_List (Contract (Spec_Id));
21d27997 11367 while Present (Prag) loop
1fb00064
AC
11368 if Pragma_Name (Prag) = Name_Precondition then
11369
beacce02
AC
11370 -- For Pre (or Precondition pragma), we simply prepend the
11371 -- pragma to the list of declarations right away so that it
11372 -- will be executed at the start of the procedure. Note that
11373 -- this processing reverses the order of the list, which is
11374 -- what we want since new entries were chained to the head of
2d395256
AC
11375 -- the list. There can be more than one precondition when we
11376 -- use pragma Precondition.
beacce02
AC
11377
11378 if not Class_Present (Prag) then
11379 Prepend (Grab_PPC, Declarations (N));
11380
11381 -- For Pre'Class there can only be one pragma, and we save
11382 -- it in Precond for now. We will add inherited Pre'Class
11383 -- stuff before inserting this pragma in the declarations.
11384 else
11385 Precond := Grab_PPC;
11386 end if;
21d27997
RD
11387 end if;
11388
11389 Prag := Next_Pragma (Prag);
11390 end loop;
beacce02
AC
11391
11392 -- Now deal with inherited preconditions
11393
11394 for J in Inherited'Range loop
dac3bede 11395 Prag := Spec_PPC_List (Contract (Inherited (J)));
beacce02
AC
11396
11397 while Present (Prag) loop
11398 if Pragma_Name (Prag) = Name_Precondition
11399 and then Class_Present (Prag)
11400 then
3c971dcc 11401 Inherited_Precond := Grab_PPC (Inherited (J));
beacce02
AC
11402
11403 -- No precondition so far, so establish this as the first
11404
11405 if No (Precond) then
11406 Precond := Inherited_Precond;
11407
11408 -- Here we already have a precondition, add inherited one
11409
11410 else
11411 -- Add new precondition to old one using OR ELSE
11412
11413 declare
11414 New_Expr : constant Node_Id :=
11415 Get_Pragma_Arg
11416 (Next
11417 (First
11418 (Pragma_Argument_Associations
11419 (Inherited_Precond))));
11420 Old_Expr : constant Node_Id :=
11421 Get_Pragma_Arg
11422 (Next
11423 (First
11424 (Pragma_Argument_Associations
11425 (Precond))));
11426
11427 begin
11428 if Paren_Count (Old_Expr) = 0 then
11429 Set_Paren_Count (Old_Expr, 1);
11430 end if;
11431
11432 if Paren_Count (New_Expr) = 0 then
11433 Set_Paren_Count (New_Expr, 1);
11434 end if;
11435
11436 Rewrite (Old_Expr,
11437 Make_Or_Else (Sloc (Old_Expr),
11438 Left_Opnd => Relocate_Node (Old_Expr),
11439 Right_Opnd => New_Expr));
11440 end;
11441
11442 -- Add new message in the form:
11443
11444 -- failed precondition from bla
11445 -- also failed inherited precondition from bla
11446 -- ...
11447
3c971dcc
AC
11448 -- Skip this if exception locations are suppressed
11449
11450 if not Exception_Locations_Suppressed then
11451 declare
11452 New_Msg : constant Node_Id :=
11453 Get_Pragma_Arg
11454 (Last
11455 (Pragma_Argument_Associations
11456 (Inherited_Precond)));
11457 Old_Msg : constant Node_Id :=
11458 Get_Pragma_Arg
11459 (Last
11460 (Pragma_Argument_Associations
11461 (Precond)));
11462 begin
11463 Start_String (Strval (Old_Msg));
11464 Store_String_Chars (ASCII.LF & " also ");
11465 Store_String_Chars (Strval (New_Msg));
11466 Set_Strval (Old_Msg, End_String);
11467 end;
11468 end if;
beacce02
AC
11469 end if;
11470 end if;
11471
11472 Prag := Next_Pragma (Prag);
11473 end loop;
11474 end loop;
11475
11476 -- If we have built a precondition for Pre'Class (including any
11477 -- Pre'Class aspects inherited from parent subprograms), then we
11478 -- insert this composite precondition at this stage.
11479
11480 if Present (Precond) then
11481 Prepend (Precond, Declarations (N));
11482 end if;
21d27997
RD
11483 end if;
11484
11485 -- Build postconditions procedure if needed and prepend the following
11486 -- declaration to the start of the declarations for the subprogram.
11487
11488 -- procedure _postconditions [(_Result : resulttype)] is
11489 -- begin
11490 -- pragma Check (Postcondition, condition [,message]);
11491 -- pragma Check (Postcondition, condition [,message]);
11492 -- ...
e606088a
AC
11493 -- Invariant_Procedure (_Result) ...
11494 -- Invariant_Procedure (Arg1)
11495 -- ...
21d27997
RD
11496 -- end;
11497
11498 -- First we deal with the postconditions in the body
11499
11500 if Is_Non_Empty_List (Declarations (N)) then
11501
11502 -- Loop through declarations
11503
11504 Prag := First (Declarations (N));
11505 while Present (Prag) loop
11506 if Nkind (Prag) = N_Pragma then
11507
11508 -- If pragma, capture if enabled postcondition, else ignore
11509
11510 if Pragma_Name (Prag) = Name_Postcondition
11511 and then Check_Enabled (Name_Postcondition)
11512 then
11513 if Plist = No_List then
11514 Plist := Empty_List;
11515 end if;
11516
11517 Analyze (Prag);
0dabde3a 11518
f0709ca6
AC
11519 -- If expansion is disabled, as in a generic unit, save
11520 -- pragma for later expansion.
0dabde3a
ES
11521
11522 if not Expander_Active then
f0709ca6 11523 Prepend (Grab_PPC, Declarations (N));
0dabde3a 11524 else
f0709ca6 11525 Append (Grab_PPC, Plist);
0dabde3a 11526 end if;
21d27997
RD
11527 end if;
11528
11529 Next (Prag);
11530
043ce308 11531 -- Not a pragma, if comes from source, then end scan
21d27997
RD
11532
11533 elsif Comes_From_Source (Prag) then
11534 exit;
11535
043ce308 11536 -- Skip stuff not coming from source
21d27997
RD
11537
11538 else
11539 Next (Prag);
11540 end if;
11541 end loop;
11542 end if;
11543
11544 -- Now deal with any postconditions from the spec
11545
11546 if Present (Spec_Id) then
e606088a 11547 Spec_Postconditions : declare
90e85233
YM
11548 procedure Process_Contract_Cases (Spec : Node_Id);
11549 -- This processes the Spec_CTC_List from Spec, processing any
11550 -- contract-case from the list. The caller has checked that
11551 -- Spec_CTC_List is non-Empty.
11552
f0709ca6
AC
11553 procedure Process_Post_Conditions
11554 (Spec : Node_Id;
11555 Class : Boolean);
11556 -- This processes the Spec_PPC_List from Spec, processing any
11557 -- postconditions from the list. If Class is True, then only
11558 -- postconditions marked with Class_Present are considered.
11559 -- The caller has checked that Spec_PPC_List is non-Empty.
11560
90e85233
YM
11561 ----------------------------
11562 -- Process_Contract_Cases --
11563 ----------------------------
11564
11565 procedure Process_Contract_Cases (Spec : Node_Id) is
11566 begin
11567 -- Loop through Contract_Case pragmas from spec
11568
11569 Prag := Spec_CTC_List (Contract (Spec));
11570 loop
11571 if Pragma_Name (Prag) = Name_Contract_Case then
11572 if Plist = No_List then
11573 Plist := Empty_List;
11574 end if;
11575
11576 if not Expander_Active then
11577 Prepend (Grab_CC, Declarations (N));
11578 else
11579 Append (Grab_CC, Plist);
11580 end if;
11581 end if;
11582
11583 Prag := Next_Pragma (Prag);
11584 exit when No (Prag);
11585 end loop;
90e85233
YM
11586 end Process_Contract_Cases;
11587
f0709ca6
AC
11588 -----------------------------
11589 -- Process_Post_Conditions --
11590 -----------------------------
11591
11592 procedure Process_Post_Conditions
11593 (Spec : Node_Id;
11594 Class : Boolean)
11595 is
11596 Pspec : Node_Id;
21d27997 11597
f0709ca6
AC
11598 begin
11599 if Class then
11600 Pspec := Spec;
0dabde3a 11601 else
f0709ca6 11602 Pspec := Empty;
0dabde3a 11603 end if;
f0709ca6
AC
11604
11605 -- Loop through PPC pragmas from spec
11606
dac3bede 11607 Prag := Spec_PPC_List (Contract (Spec));
f0709ca6
AC
11608 loop
11609 if Pragma_Name (Prag) = Name_Postcondition
f0709ca6
AC
11610 and then (not Class or else Class_Present (Prag))
11611 then
11612 if Plist = No_List then
11613 Plist := Empty_List;
11614 end if;
11615
11616 if not Expander_Active then
11617 Prepend
11618 (Grab_PPC (Pspec), Declarations (N));
11619 else
11620 Append (Grab_PPC (Pspec), Plist);
11621 end if;
11622 end if;
11623
11624 Prag := Next_Pragma (Prag);
11625 exit when No (Prag);
11626 end loop;
11627 end Process_Post_Conditions;
11628
e606088a
AC
11629 -- Start of processing for Spec_Postconditions
11630
f0709ca6 11631 begin
90e85233
YM
11632 -- Process postconditions expressed as contract-cases
11633
11634 if Present (Spec_CTC_List (Contract (Spec_Id))) then
11635 Process_Contract_Cases (Spec_Id);
11636 end if;
11637
11638 -- Process spec postconditions
11639
dac3bede 11640 if Present (Spec_PPC_List (Contract (Spec_Id))) then
f0709ca6 11641 Process_Post_Conditions (Spec_Id, Class => False);
21d27997
RD
11642 end if;
11643
beacce02 11644 -- Process inherited postconditions
f0709ca6 11645
beacce02 11646 for J in Inherited'Range loop
dac3bede 11647 if Present (Spec_PPC_List (Contract (Inherited (J)))) then
beacce02 11648 Process_Post_Conditions (Inherited (J), Class => True);
f0709ca6
AC
11649 end if;
11650 end loop;
e606088a 11651 end Spec_Postconditions;
21d27997
RD
11652 end if;
11653
e606088a
AC
11654 -- If we had any postconditions and expansion is enabled, or if the
11655 -- procedure has invariants, then build the _Postconditions procedure.
21d27997 11656
b4ca2d2c 11657 if (Present (Plist) or else Invariants_Or_Predicates_Present)
0dabde3a
ES
11658 and then Expander_Active
11659 then
e606088a
AC
11660 if No (Plist) then
11661 Plist := Empty_List;
11662 end if;
11663
11664 -- Special processing for function case
11665
11666 if Ekind (Designator) /= E_Procedure then
11667 declare
11668 Rent : constant Entity_Id :=
fecbd779 11669 Make_Defining_Identifier (Loc, Name_uResult);
e606088a
AC
11670 Ftyp : constant Entity_Id := Etype (Designator);
11671
11672 begin
11673 Set_Etype (Rent, Ftyp);
11674
11675 -- Add argument for return
11676
11677 Parms :=
11678 New_List (
11679 Make_Parameter_Specification (Loc,
11680 Parameter_Type => New_Occurrence_Of (Ftyp, Loc),
11681 Defining_Identifier => Rent));
11682
a4901c08
AC
11683 -- Add invariant call if returning type with invariants and
11684 -- this is a public function, i.e. a function declared in the
11685 -- visible part of the package defining the private type.
e606088a 11686
fd0ff1cf
RD
11687 if Has_Invariants (Etype (Rent))
11688 and then Present (Invariant_Procedure (Etype (Rent)))
a4901c08 11689 and then Is_Public_Subprogram_For (Etype (Rent))
fd0ff1cf 11690 then
e606088a
AC
11691 Append_To (Plist,
11692 Make_Invariant_Call (New_Occurrence_Of (Rent, Loc)));
11693 end if;
11694 end;
11695
11696 -- Procedure rather than a function
21d27997 11697
21d27997
RD
11698 else
11699 Parms := No_List;
11700 end if;
11701
b4ca2d2c
AC
11702 -- Add invariant calls and predicate calls for parameters. Note that
11703 -- this is done for functions as well, since in Ada 2012 they can
11704 -- have IN OUT args.
e606088a
AC
11705
11706 declare
11707 Formal : Entity_Id;
b4ca2d2c 11708 Ftype : Entity_Id;
e606088a
AC
11709
11710 begin
11711 Formal := First_Formal (Designator);
11712 while Present (Formal) loop
b4ca2d2c
AC
11713 if Ekind (Formal) /= E_In_Parameter then
11714 Ftype := Etype (Formal);
11715
11716 if Has_Invariants (Ftype)
11717 and then Present (Invariant_Procedure (Ftype))
a4901c08 11718 and then Is_Public_Subprogram_For (Ftype)
b4ca2d2c
AC
11719 then
11720 Append_To (Plist,
11721 Make_Invariant_Call
11722 (New_Occurrence_Of (Formal, Loc)));
11723 end if;
11724
11725 if Present (Predicate_Function (Ftype)) then
11726 Append_To (Plist,
11727 Make_Predicate_Check
11728 (Ftype, New_Occurrence_Of (Formal, Loc)));
11729 end if;
e606088a
AC
11730 end if;
11731
11732 Next_Formal (Formal);
11733 end loop;
11734 end;
11735
11736 -- Build and insert postcondition procedure
11737
043ce308
AC
11738 declare
11739 Post_Proc : constant Entity_Id :=
e606088a
AC
11740 Make_Defining_Identifier (Loc,
11741 Chars => Name_uPostconditions);
043ce308 11742 -- The entity for the _Postconditions procedure
f0709ca6 11743
043ce308 11744 begin
e228f7ee 11745 Insert_After (Last_Implicit_Declaration,
043ce308
AC
11746 Make_Subprogram_Body (Loc,
11747 Specification =>
11748 Make_Procedure_Specification (Loc,
11749 Defining_Unit_Name => Post_Proc,
11750 Parameter_Specifications => Parms),
11751
11752 Declarations => Empty_List,
11753
11754 Handled_Statement_Sequence =>
11755 Make_Handled_Sequence_Of_Statements (Loc,
11756 Statements => Plist)));
21d27997 11757
5ffe0bab 11758 Set_Ekind (Post_Proc, E_Procedure);
5ffe0bab 11759
3bb3f6d6
AC
11760 -- If this is a procedure, set the Postcondition_Proc attribute on
11761 -- the proper defining entity for the subprogram.
21d27997 11762
e606088a
AC
11763 if Ekind (Designator) = E_Procedure then
11764 Set_Postcondition_Proc (Designator, Post_Proc);
043ce308
AC
11765 end if;
11766 end;
21d27997 11767
e606088a 11768 Set_Has_Postconditions (Designator);
21d27997
RD
11769 end if;
11770 end Process_PPCs;
11771
fbf5a39b
AC
11772 ----------------------------
11773 -- Reference_Body_Formals --
11774 ----------------------------
11775
11776 procedure Reference_Body_Formals (Spec : Entity_Id; Bod : Entity_Id) is
11777 Fs : Entity_Id;
11778 Fb : Entity_Id;
11779
11780 begin
11781 if Error_Posted (Spec) then
11782 return;
11783 end if;
11784
0a36105d
JM
11785 -- Iterate over both lists. They may be of different lengths if the two
11786 -- specs are not conformant.
11787
fbf5a39b
AC
11788 Fs := First_Formal (Spec);
11789 Fb := First_Formal (Bod);
0a36105d 11790 while Present (Fs) and then Present (Fb) loop
fbf5a39b
AC
11791 Generate_Reference (Fs, Fb, 'b');
11792
11793 if Style_Check then
11794 Style.Check_Identifier (Fb, Fs);
11795 end if;
11796
11797 Set_Spec_Entity (Fb, Fs);
11798 Set_Referenced (Fs, False);
11799 Next_Formal (Fs);
11800 Next_Formal (Fb);
11801 end loop;
11802 end Reference_Body_Formals;
11803
996ae0b0
RK
11804 -------------------------
11805 -- Set_Actual_Subtypes --
11806 -------------------------
11807
11808 procedure Set_Actual_Subtypes (N : Node_Id; Subp : Entity_Id) is
2820d220
AC
11809 Decl : Node_Id;
11810 Formal : Entity_Id;
11811 T : Entity_Id;
11812 First_Stmt : Node_Id := Empty;
11813 AS_Needed : Boolean;
996ae0b0
RK
11814
11815 begin
f3d57416 11816 -- If this is an empty initialization procedure, no need to create
fbf5a39b
AC
11817 -- actual subtypes (small optimization).
11818
11819 if Ekind (Subp) = E_Procedure
11820 and then Is_Null_Init_Proc (Subp)
11821 then
11822 return;
11823 end if;
11824
996ae0b0
RK
11825 Formal := First_Formal (Subp);
11826 while Present (Formal) loop
11827 T := Etype (Formal);
11828
e895b435 11829 -- We never need an actual subtype for a constrained formal
996ae0b0
RK
11830
11831 if Is_Constrained (T) then
11832 AS_Needed := False;
11833
82c80734
RD
11834 -- If we have unknown discriminants, then we do not need an actual
11835 -- subtype, or more accurately we cannot figure it out! Note that
11836 -- all class-wide types have unknown discriminants.
996ae0b0
RK
11837
11838 elsif Has_Unknown_Discriminants (T) then
11839 AS_Needed := False;
11840
82c80734
RD
11841 -- At this stage we have an unconstrained type that may need an
11842 -- actual subtype. For sure the actual subtype is needed if we have
11843 -- an unconstrained array type.
996ae0b0
RK
11844
11845 elsif Is_Array_Type (T) then
11846 AS_Needed := True;
11847
d8db0bca
JM
11848 -- The only other case needing an actual subtype is an unconstrained
11849 -- record type which is an IN parameter (we cannot generate actual
11850 -- subtypes for the OUT or IN OUT case, since an assignment can
11851 -- change the discriminant values. However we exclude the case of
11852 -- initialization procedures, since discriminants are handled very
11853 -- specially in this context, see the section entitled "Handling of
11854 -- Discriminants" in Einfo.
11855
11856 -- We also exclude the case of Discrim_SO_Functions (functions used
11857 -- in front end layout mode for size/offset values), since in such
11858 -- functions only discriminants are referenced, and not only are such
11859 -- subtypes not needed, but they cannot always be generated, because
11860 -- of order of elaboration issues.
996ae0b0
RK
11861
11862 elsif Is_Record_Type (T)
11863 and then Ekind (Formal) = E_In_Parameter
11864 and then Chars (Formal) /= Name_uInit
5d09245e 11865 and then not Is_Unchecked_Union (T)
996ae0b0
RK
11866 and then not Is_Discrim_SO_Function (Subp)
11867 then
11868 AS_Needed := True;
11869
11870 -- All other cases do not need an actual subtype
11871
11872 else
11873 AS_Needed := False;
11874 end if;
11875
11876 -- Generate actual subtypes for unconstrained arrays and
11877 -- unconstrained discriminated records.
11878
11879 if AS_Needed then
7324bf49 11880 if Nkind (N) = N_Accept_Statement then
fbf5a39b 11881
57a8057a 11882 -- If expansion is active, the formal is replaced by a local
fbf5a39b
AC
11883 -- variable that renames the corresponding entry of the
11884 -- parameter block, and it is this local variable that may
da94696d 11885 -- require an actual subtype.
fbf5a39b 11886
da94696d 11887 if Full_Expander_Active then
fbf5a39b
AC
11888 Decl := Build_Actual_Subtype (T, Renamed_Object (Formal));
11889 else
11890 Decl := Build_Actual_Subtype (T, Formal);
11891 end if;
11892
996ae0b0
RK
11893 if Present (Handled_Statement_Sequence (N)) then
11894 First_Stmt :=
11895 First (Statements (Handled_Statement_Sequence (N)));
11896 Prepend (Decl, Statements (Handled_Statement_Sequence (N)));
11897 Mark_Rewrite_Insertion (Decl);
11898 else
82c80734
RD
11899 -- If the accept statement has no body, there will be no
11900 -- reference to the actuals, so no need to compute actual
11901 -- subtypes.
996ae0b0
RK
11902
11903 return;
11904 end if;
11905
11906 else
fbf5a39b 11907 Decl := Build_Actual_Subtype (T, Formal);
996ae0b0
RK
11908 Prepend (Decl, Declarations (N));
11909 Mark_Rewrite_Insertion (Decl);
11910 end if;
11911
82c80734
RD
11912 -- The declaration uses the bounds of an existing object, and
11913 -- therefore needs no constraint checks.
2820d220 11914
7324bf49 11915 Analyze (Decl, Suppress => All_Checks);
2820d220 11916
996ae0b0
RK
11917 -- We need to freeze manually the generated type when it is
11918 -- inserted anywhere else than in a declarative part.
11919
11920 if Present (First_Stmt) then
11921 Insert_List_Before_And_Analyze (First_Stmt,
c159409f 11922 Freeze_Entity (Defining_Identifier (Decl), N));
996ae0b0
RK
11923 end if;
11924
fbf5a39b 11925 if Nkind (N) = N_Accept_Statement
da94696d 11926 and then Full_Expander_Active
fbf5a39b
AC
11927 then
11928 Set_Actual_Subtype (Renamed_Object (Formal),
11929 Defining_Identifier (Decl));
11930 else
11931 Set_Actual_Subtype (Formal, Defining_Identifier (Decl));
11932 end if;
996ae0b0
RK
11933 end if;
11934
11935 Next_Formal (Formal);
11936 end loop;
11937 end Set_Actual_Subtypes;
11938
11939 ---------------------
11940 -- Set_Formal_Mode --
11941 ---------------------
11942
11943 procedure Set_Formal_Mode (Formal_Id : Entity_Id) is
11944 Spec : constant Node_Id := Parent (Formal_Id);
11945
11946 begin
11947 -- Note: we set Is_Known_Valid for IN parameters and IN OUT parameters
11948 -- since we ensure that corresponding actuals are always valid at the
11949 -- point of the call.
11950
11951 if Out_Present (Spec) then
996ae0b0
RK
11952 if Ekind (Scope (Formal_Id)) = E_Function
11953 or else Ekind (Scope (Formal_Id)) = E_Generic_Function
11954 then
b4ca2d2c 11955 -- [IN] OUT parameters allowed for functions in Ada 2012
c56a9ba4
AC
11956
11957 if Ada_Version >= Ada_2012 then
11958 if In_Present (Spec) then
11959 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11960 else
11961 Set_Ekind (Formal_Id, E_Out_Parameter);
11962 end if;
11963
b4ca2d2c
AC
11964 -- But not in earlier versions of Ada
11965
c56a9ba4
AC
11966 else
11967 Error_Msg_N ("functions can only have IN parameters", Spec);
11968 Set_Ekind (Formal_Id, E_In_Parameter);
11969 end if;
996ae0b0
RK
11970
11971 elsif In_Present (Spec) then
11972 Set_Ekind (Formal_Id, E_In_Out_Parameter);
11973
11974 else
fbf5a39b
AC
11975 Set_Ekind (Formal_Id, E_Out_Parameter);
11976 Set_Never_Set_In_Source (Formal_Id, True);
11977 Set_Is_True_Constant (Formal_Id, False);
11978 Set_Current_Value (Formal_Id, Empty);
996ae0b0
RK
11979 end if;
11980
11981 else
11982 Set_Ekind (Formal_Id, E_In_Parameter);
11983 end if;
11984
fbf5a39b 11985 -- Set Is_Known_Non_Null for access parameters since the language
82c80734
RD
11986 -- guarantees that access parameters are always non-null. We also set
11987 -- Can_Never_Be_Null, since there is no way to change the value.
fbf5a39b
AC
11988
11989 if Nkind (Parameter_Type (Spec)) = N_Access_Definition then
2820d220 11990
885c4871 11991 -- Ada 2005 (AI-231): In Ada 95, access parameters are always non-
2813bb6b 11992 -- null; In Ada 2005, only if then null_exclusion is explicit.
2820d220 11993
0791fbe9 11994 if Ada_Version < Ada_2005
2813bb6b 11995 or else Can_Never_Be_Null (Etype (Formal_Id))
2820d220
AC
11996 then
11997 Set_Is_Known_Non_Null (Formal_Id);
11998 Set_Can_Never_Be_Null (Formal_Id);
11999 end if;
2813bb6b 12000
41251c60
JM
12001 -- Ada 2005 (AI-231): Null-exclusion access subtype
12002
2813bb6b
ES
12003 elsif Is_Access_Type (Etype (Formal_Id))
12004 and then Can_Never_Be_Null (Etype (Formal_Id))
12005 then
2813bb6b 12006 Set_Is_Known_Non_Null (Formal_Id);
a1d72281
EB
12007
12008 -- We can also set Can_Never_Be_Null (thus preventing some junk
12009 -- access checks) for the case of an IN parameter, which cannot
12010 -- be changed, or for an IN OUT parameter, which can be changed but
12011 -- not to a null value. But for an OUT parameter, the initial value
12012 -- passed in can be null, so we can't set this flag in that case.
12013
12014 if Ekind (Formal_Id) /= E_Out_Parameter then
12015 Set_Can_Never_Be_Null (Formal_Id);
12016 end if;
fbf5a39b
AC
12017 end if;
12018
996ae0b0
RK
12019 Set_Mechanism (Formal_Id, Default_Mechanism);
12020 Set_Formal_Validity (Formal_Id);
12021 end Set_Formal_Mode;
12022
12023 -------------------------
12024 -- Set_Formal_Validity --
12025 -------------------------
12026
12027 procedure Set_Formal_Validity (Formal_Id : Entity_Id) is
12028 begin
82c80734
RD
12029 -- If no validity checking, then we cannot assume anything about the
12030 -- validity of parameters, since we do not know there is any checking
12031 -- of the validity on the call side.
996ae0b0
RK
12032
12033 if not Validity_Checks_On then
12034 return;
12035
fbf5a39b
AC
12036 -- If validity checking for parameters is enabled, this means we are
12037 -- not supposed to make any assumptions about argument values.
12038
12039 elsif Validity_Check_Parameters then
12040 return;
12041
12042 -- If we are checking in parameters, we will assume that the caller is
12043 -- also checking parameters, so we can assume the parameter is valid.
12044
996ae0b0
RK
12045 elsif Ekind (Formal_Id) = E_In_Parameter
12046 and then Validity_Check_In_Params
12047 then
12048 Set_Is_Known_Valid (Formal_Id, True);
12049
fbf5a39b
AC
12050 -- Similar treatment for IN OUT parameters
12051
996ae0b0
RK
12052 elsif Ekind (Formal_Id) = E_In_Out_Parameter
12053 and then Validity_Check_In_Out_Params
12054 then
12055 Set_Is_Known_Valid (Formal_Id, True);
12056 end if;
12057 end Set_Formal_Validity;
12058
12059 ------------------------
12060 -- Subtype_Conformant --
12061 ------------------------
12062
ce2b6ba5
JM
12063 function Subtype_Conformant
12064 (New_Id : Entity_Id;
12065 Old_Id : Entity_Id;
12066 Skip_Controlling_Formals : Boolean := False) return Boolean
12067 is
996ae0b0 12068 Result : Boolean;
996ae0b0 12069 begin
ce2b6ba5
JM
12070 Check_Conformance (New_Id, Old_Id, Subtype_Conformant, False, Result,
12071 Skip_Controlling_Formals => Skip_Controlling_Formals);
996ae0b0
RK
12072 return Result;
12073 end Subtype_Conformant;
12074
12075 ---------------------
12076 -- Type_Conformant --
12077 ---------------------
12078
41251c60
JM
12079 function Type_Conformant
12080 (New_Id : Entity_Id;
12081 Old_Id : Entity_Id;
12082 Skip_Controlling_Formals : Boolean := False) return Boolean
12083 is
996ae0b0 12084 Result : Boolean;
996ae0b0 12085 begin
c8ef728f
ES
12086 May_Hide_Profile := False;
12087
41251c60
JM
12088 Check_Conformance
12089 (New_Id, Old_Id, Type_Conformant, False, Result,
12090 Skip_Controlling_Formals => Skip_Controlling_Formals);
996ae0b0
RK
12091 return Result;
12092 end Type_Conformant;
12093
12094 -------------------------------
12095 -- Valid_Operator_Definition --
12096 -------------------------------
12097
12098 procedure Valid_Operator_Definition (Designator : Entity_Id) is
12099 N : Integer := 0;
12100 F : Entity_Id;
12101 Id : constant Name_Id := Chars (Designator);
12102 N_OK : Boolean;
12103
12104 begin
12105 F := First_Formal (Designator);
996ae0b0
RK
12106 while Present (F) loop
12107 N := N + 1;
12108
12109 if Present (Default_Value (F)) then
ed2233dc 12110 Error_Msg_N
996ae0b0
RK
12111 ("default values not allowed for operator parameters",
12112 Parent (F));
12113 end if;
12114
12115 Next_Formal (F);
12116 end loop;
12117
12118 -- Verify that user-defined operators have proper number of arguments
12119 -- First case of operators which can only be unary
12120
12121 if Id = Name_Op_Not
12122 or else Id = Name_Op_Abs
12123 then
12124 N_OK := (N = 1);
12125
12126 -- Case of operators which can be unary or binary
12127
12128 elsif Id = Name_Op_Add
12129 or Id = Name_Op_Subtract
12130 then
12131 N_OK := (N in 1 .. 2);
12132
12133 -- All other operators can only be binary
12134
12135 else
12136 N_OK := (N = 2);
12137 end if;
12138
12139 if not N_OK then
12140 Error_Msg_N
12141 ("incorrect number of arguments for operator", Designator);
12142 end if;
12143
12144 if Id = Name_Op_Ne
12145 and then Base_Type (Etype (Designator)) = Standard_Boolean
12146 and then not Is_Intrinsic_Subprogram (Designator)
12147 then
12148 Error_Msg_N
12149 ("explicit definition of inequality not allowed", Designator);
12150 end if;
12151 end Valid_Operator_Definition;
12152
12153end Sem_Ch6;