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