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