OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [gcc/] [ada/] [sem_ch13.adb] - Blame information for rev 750

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 706 jeremybenn
------------------------------------------------------------------------------
2
--                                                                          --
3
--                         GNAT COMPILER COMPONENTS                         --
4
--                                                                          --
5
--                             S E M _ C H 1 3                              --
6
--                                                                          --
7
--                                 B o d y                                  --
8
--                                                                          --
9
--          Copyright (C) 1992-2012, Free Software Foundation, Inc.         --
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- --
13
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
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 --
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.          --
20
--                                                                          --
21
-- GNAT was originally developed  by the GNAT team at  New York University. --
22
-- Extensive contributions were provided by Ada Core Technologies Inc.      --
23
--                                                                          --
24
------------------------------------------------------------------------------
25
 
26
with Aspects;  use Aspects;
27
with Atree;    use Atree;
28
with Checks;   use Checks;
29
with Einfo;    use Einfo;
30
with Elists;   use Elists;
31
with Errout;   use Errout;
32
with Exp_Disp; use Exp_Disp;
33
with Exp_Tss;  use Exp_Tss;
34
with Exp_Util; use Exp_Util;
35
with Lib;      use Lib;
36
with Lib.Xref; use Lib.Xref;
37
with Namet;    use Namet;
38
with Nlists;   use Nlists;
39
with Nmake;    use Nmake;
40
with Opt;      use Opt;
41
with Restrict; use Restrict;
42
with Rident;   use Rident;
43
with Rtsfind;  use Rtsfind;
44
with Sem;      use Sem;
45
with Sem_Aux;  use Sem_Aux;
46
with Sem_Ch3;  use Sem_Ch3;
47
with Sem_Ch6;  use Sem_Ch6;
48
with Sem_Ch8;  use Sem_Ch8;
49
with Sem_Dim;  use Sem_Dim;
50
with Sem_Eval; use Sem_Eval;
51
with Sem_Res;  use Sem_Res;
52
with Sem_Type; use Sem_Type;
53
with Sem_Util; use Sem_Util;
54
with Sem_Warn; use Sem_Warn;
55
with Sinput;   use Sinput;
56
with Snames;   use Snames;
57
with Stand;    use Stand;
58
with Sinfo;    use Sinfo;
59
with Stringt;  use Stringt;
60
with Targparm; use Targparm;
61
with Ttypes;   use Ttypes;
62
with Tbuild;   use Tbuild;
63
with Urealp;   use Urealp;
64
with Warnsw;   use Warnsw;
65
 
66
with GNAT.Heap_Sort_G;
67
 
68
package body Sem_Ch13 is
69
 
70
   SSU : constant Pos := System_Storage_Unit;
71
   --  Convenient short hand for commonly used constant
72
 
73
   -----------------------
74
   -- Local Subprograms --
75
   -----------------------
76
 
77
   procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint);
78
   --  This routine is called after setting one of the sizes of type entity
79
   --  Typ to Size. The purpose is to deal with the situation of a derived
80
   --  type whose inherited alignment is no longer appropriate for the new
81
   --  size value. In this case, we reset the Alignment to unknown.
82
 
83
   procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id);
84
   --  If Typ has predicates (indicated by Has_Predicates being set for Typ,
85
   --  then either there are pragma Invariant entries on the rep chain for the
86
   --  type (note that Predicate aspects are converted to pragma Predicate), or
87
   --  there are inherited aspects from a parent type, or ancestor subtypes.
88
   --  This procedure builds the spec and body for the Predicate function that
89
   --  tests these predicates. N is the freeze node for the type. The spec of
90
   --  the function is inserted before the freeze node, and the body of the
91
   --  function is inserted after the freeze node.
92
 
93
   procedure Build_Static_Predicate
94
     (Typ  : Entity_Id;
95
      Expr : Node_Id;
96
      Nam  : Name_Id);
97
   --  Given a predicated type Typ, where Typ is a discrete static subtype,
98
   --  whose predicate expression is Expr, tests if Expr is a static predicate,
99
   --  and if so, builds the predicate range list. Nam is the name of the one
100
   --  argument to the predicate function. Occurrences of the type name in the
101
   --  predicate expression have been replaced by identifier references to this
102
   --  name, which is unique, so any identifier with Chars matching Nam must be
103
   --  a reference to the type. If the predicate is non-static, this procedure
104
   --  returns doing nothing. If the predicate is static, then the predicate
105
   --  list is stored in Static_Predicate (Typ), and the Expr is rewritten as
106
   --  a canonicalized membership operation.
107
 
108
   function Get_Alignment_Value (Expr : Node_Id) return Uint;
109
   --  Given the expression for an alignment value, returns the corresponding
110
   --  Uint value. If the value is inappropriate, then error messages are
111
   --  posted as required, and a value of No_Uint is returned.
112
 
113
   function Is_Operational_Item (N : Node_Id) return Boolean;
114
   --  A specification for a stream attribute is allowed before the full type
115
   --  is declared, as explained in AI-00137 and the corrigendum. Attributes
116
   --  that do not specify a representation characteristic are operational
117
   --  attributes.
118
 
119
   procedure New_Stream_Subprogram
120
     (N    : Node_Id;
121
      Ent  : Entity_Id;
122
      Subp : Entity_Id;
123
      Nam  : TSS_Name_Type);
124
   --  Create a subprogram renaming of a given stream attribute to the
125
   --  designated subprogram and then in the tagged case, provide this as a
126
   --  primitive operation, or in the non-tagged case make an appropriate TSS
127
   --  entry. This is more properly an expansion activity than just semantics,
128
   --  but the presence of user-defined stream functions for limited types is a
129
   --  legality check, which is why this takes place here rather than in
130
   --  exp_ch13, where it was previously. Nam indicates the name of the TSS
131
   --  function to be generated.
132
   --
133
   --  To avoid elaboration anomalies with freeze nodes, for untagged types
134
   --  we generate both a subprogram declaration and a subprogram renaming
135
   --  declaration, so that the attribute specification is handled as a
136
   --  renaming_as_body. For tagged types, the specification is one of the
137
   --  primitive specs.
138
 
139
   generic
140
      with procedure Replace_Type_Reference (N : Node_Id);
141
   procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id);
142
   --  This is used to scan an expression for a predicate or invariant aspect
143
   --  replacing occurrences of the name TName (the name of the subtype to
144
   --  which the aspect applies) with appropriate references to the parameter
145
   --  of the predicate function or invariant procedure. The procedure passed
146
   --  as a generic parameter does the actual replacement of node N, which is
147
   --  either a simple direct reference to TName, or a selected component that
148
   --  represents an appropriately qualified occurrence of TName.
149
 
150
   procedure Set_Biased
151
     (E      : Entity_Id;
152
      N      : Node_Id;
153
      Msg    : String;
154
      Biased : Boolean := True);
155
   --  If Biased is True, sets Has_Biased_Representation flag for E, and
156
   --  outputs a warning message at node N if Warn_On_Biased_Representation is
157
   --  is True. This warning inserts the string Msg to describe the construct
158
   --  causing biasing.
159
 
160
   ----------------------------------------------
161
   -- Table for Validate_Unchecked_Conversions --
162
   ----------------------------------------------
163
 
164
   --  The following table collects unchecked conversions for validation.
165
   --  Entries are made by Validate_Unchecked_Conversion and then the call
166
   --  to Validate_Unchecked_Conversions does the actual error checking and
167
   --  posting of warnings. The reason for this delayed processing is to take
168
   --  advantage of back-annotations of size and alignment values performed by
169
   --  the back end.
170
 
171
   --  Note: the reason we store a Source_Ptr value instead of a Node_Id is
172
   --  that by the time Validate_Unchecked_Conversions is called, Sprint will
173
   --  already have modified all Sloc values if the -gnatD option is set.
174
 
175
   type UC_Entry is record
176
      Eloc   : Source_Ptr; -- node used for posting warnings
177
      Source : Entity_Id;  -- source type for unchecked conversion
178
      Target : Entity_Id;  -- target type for unchecked conversion
179
   end record;
180
 
181
   package Unchecked_Conversions is new Table.Table (
182
     Table_Component_Type => UC_Entry,
183
     Table_Index_Type     => Int,
184
     Table_Low_Bound      => 1,
185
     Table_Initial        => 50,
186
     Table_Increment      => 200,
187
     Table_Name           => "Unchecked_Conversions");
188
 
189
   ----------------------------------------
190
   -- Table for Validate_Address_Clauses --
191
   ----------------------------------------
192
 
193
   --  If an address clause has the form
194
 
195
   --    for X'Address use Expr
196
 
197
   --  where Expr is of the form Y'Address or recursively is a reference to a
198
   --  constant of either of these forms, and X and Y are entities of objects,
199
   --  then if Y has a smaller alignment than X, that merits a warning about
200
   --  possible bad alignment. The following table collects address clauses of
201
   --  this kind. We put these in a table so that they can be checked after the
202
   --  back end has completed annotation of the alignments of objects, since we
203
   --  can catch more cases that way.
204
 
205
   type Address_Clause_Check_Record is record
206
      N : Node_Id;
207
      --  The address clause
208
 
209
      X : Entity_Id;
210
      --  The entity of the object overlaying Y
211
 
212
      Y : Entity_Id;
213
      --  The entity of the object being overlaid
214
 
215
      Off : Boolean;
216
      --  Whether the address is offset within Y
217
   end record;
218
 
219
   package Address_Clause_Checks is new Table.Table (
220
     Table_Component_Type => Address_Clause_Check_Record,
221
     Table_Index_Type     => Int,
222
     Table_Low_Bound      => 1,
223
     Table_Initial        => 20,
224
     Table_Increment      => 200,
225
     Table_Name           => "Address_Clause_Checks");
226
 
227
   -----------------------------------------
228
   -- Adjust_Record_For_Reverse_Bit_Order --
229
   -----------------------------------------
230
 
231
   procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id) is
232
      Comp : Node_Id;
233
      CC   : Node_Id;
234
 
235
   begin
236
      --  Processing depends on version of Ada
237
 
238
      --  For Ada 95, we just renumber bits within a storage unit. We do the
239
      --  same for Ada 83 mode, since we recognize the Bit_Order attribute in
240
      --  Ada 83, and are free to add this extension.
241
 
242
      if Ada_Version < Ada_2005 then
243
         Comp := First_Component_Or_Discriminant (R);
244
         while Present (Comp) loop
245
            CC := Component_Clause (Comp);
246
 
247
            --  If component clause is present, then deal with the non-default
248
            --  bit order case for Ada 95 mode.
249
 
250
            --  We only do this processing for the base type, and in fact that
251
            --  is important, since otherwise if there are record subtypes, we
252
            --  could reverse the bits once for each subtype, which is wrong.
253
 
254
            if Present (CC)
255
              and then Ekind (R) = E_Record_Type
256
            then
257
               declare
258
                  CFB : constant Uint    := Component_Bit_Offset (Comp);
259
                  CSZ : constant Uint    := Esize (Comp);
260
                  CLC : constant Node_Id := Component_Clause (Comp);
261
                  Pos : constant Node_Id := Position (CLC);
262
                  FB  : constant Node_Id := First_Bit (CLC);
263
 
264
                  Storage_Unit_Offset : constant Uint :=
265
                                          CFB / System_Storage_Unit;
266
 
267
                  Start_Bit : constant Uint :=
268
                                CFB mod System_Storage_Unit;
269
 
270
               begin
271
                  --  Cases where field goes over storage unit boundary
272
 
273
                  if Start_Bit + CSZ > System_Storage_Unit then
274
 
275
                     --  Allow multi-byte field but generate warning
276
 
277
                     if Start_Bit mod System_Storage_Unit = 0
278
                       and then CSZ mod System_Storage_Unit = 0
279
                     then
280
                        Error_Msg_N
281
                          ("multi-byte field specified with non-standard"
282
                           & " Bit_Order?", CLC);
283
 
284
                        if Bytes_Big_Endian then
285
                           Error_Msg_N
286
                             ("bytes are not reversed "
287
                              & "(component is big-endian)?", CLC);
288
                        else
289
                           Error_Msg_N
290
                             ("bytes are not reversed "
291
                              & "(component is little-endian)?", CLC);
292
                        end if;
293
 
294
                        --  Do not allow non-contiguous field
295
 
296
                     else
297
                        Error_Msg_N
298
                          ("attempt to specify non-contiguous field "
299
                           & "not permitted", CLC);
300
                        Error_Msg_N
301
                          ("\caused by non-standard Bit_Order "
302
                           & "specified", CLC);
303
                        Error_Msg_N
304
                          ("\consider possibility of using "
305
                           & "Ada 2005 mode here", CLC);
306
                     end if;
307
 
308
                  --  Case where field fits in one storage unit
309
 
310
                  else
311
                     --  Give warning if suspicious component clause
312
 
313
                     if Intval (FB) >= System_Storage_Unit
314
                       and then Warn_On_Reverse_Bit_Order
315
                     then
316
                        Error_Msg_N
317
                          ("?Bit_Order clause does not affect " &
318
                           "byte ordering", Pos);
319
                        Error_Msg_Uint_1 :=
320
                          Intval (Pos) + Intval (FB) /
321
                          System_Storage_Unit;
322
                        Error_Msg_N
323
                          ("?position normalized to ^ before bit " &
324
                           "order interpreted", Pos);
325
                     end if;
326
 
327
                     --  Here is where we fix up the Component_Bit_Offset value
328
                     --  to account for the reverse bit order. Some examples of
329
                     --  what needs to be done are:
330
 
331
                     --    First_Bit .. Last_Bit     Component_Bit_Offset
332
                     --      old          new          old       new
333
 
334
                     --     0 .. 0       7 .. 7         0         7
335
                     --     0 .. 1       6 .. 7         0         6
336
                     --     0 .. 2       5 .. 7         0         5
337
                     --     0 .. 7       0 .. 7         0         4
338
 
339
                     --     1 .. 1       6 .. 6         1         6
340
                     --     1 .. 4       3 .. 6         1         3
341
                     --     4 .. 7       0 .. 3         4         0
342
 
343
                     --  The rule is that the first bit is is obtained by
344
                     --  subtracting the old ending bit from storage_unit - 1.
345
 
346
                     Set_Component_Bit_Offset
347
                       (Comp,
348
                        (Storage_Unit_Offset * System_Storage_Unit) +
349
                          (System_Storage_Unit - 1) -
350
                          (Start_Bit + CSZ - 1));
351
 
352
                     Set_Normalized_First_Bit
353
                       (Comp,
354
                        Component_Bit_Offset (Comp) mod
355
                          System_Storage_Unit);
356
                  end if;
357
               end;
358
            end if;
359
 
360
            Next_Component_Or_Discriminant (Comp);
361
         end loop;
362
 
363
      --  For Ada 2005, we do machine scalar processing, as fully described In
364
      --  AI-133. This involves gathering all components which start at the
365
      --  same byte offset and processing them together. Same approach is still
366
      --  valid in later versions including Ada 2012.
367
 
368
      else
369
         declare
370
            Max_Machine_Scalar_Size : constant Uint :=
371
                                        UI_From_Int
372
                                          (Standard_Long_Long_Integer_Size);
373
            --  We use this as the maximum machine scalar size
374
 
375
            Num_CC : Natural;
376
            SSU    : constant Uint := UI_From_Int (System_Storage_Unit);
377
 
378
         begin
379
            --  This first loop through components does two things. First it
380
            --  deals with the case of components with component clauses whose
381
            --  length is greater than the maximum machine scalar size (either
382
            --  accepting them or rejecting as needed). Second, it counts the
383
            --  number of components with component clauses whose length does
384
            --  not exceed this maximum for later processing.
385
 
386
            Num_CC := 0;
387
            Comp   := First_Component_Or_Discriminant (R);
388
            while Present (Comp) loop
389
               CC := Component_Clause (Comp);
390
 
391
               if Present (CC) then
392
                  declare
393
                     Fbit : constant Uint :=
394
                              Static_Integer (First_Bit (CC));
395
                     Lbit : constant Uint :=
396
                              Static_Integer (Last_Bit (CC));
397
 
398
                  begin
399
                     --  Case of component with last bit >= max machine scalar
400
 
401
                     if Lbit >= Max_Machine_Scalar_Size then
402
 
403
                        --  This is allowed only if first bit is zero, and
404
                        --  last bit + 1 is a multiple of storage unit size.
405
 
406
                        if Fbit = 0 and then (Lbit + 1) mod SSU = 0 then
407
 
408
                           --  This is the case to give a warning if enabled
409
 
410
                           if Warn_On_Reverse_Bit_Order then
411
                              Error_Msg_N
412
                                ("multi-byte field specified with "
413
                                 & "  non-standard Bit_Order?", CC);
414
 
415
                              if Bytes_Big_Endian then
416
                                 Error_Msg_N
417
                                   ("\bytes are not reversed "
418
                                    & "(component is big-endian)?", CC);
419
                              else
420
                                 Error_Msg_N
421
                                   ("\bytes are not reversed "
422
                                    & "(component is little-endian)?", CC);
423
                              end if;
424
                           end if;
425
 
426
                        --  Give error message for RM 13.4.1(10) violation
427
 
428
                        else
429
                           Error_Msg_FE
430
                             ("machine scalar rules not followed for&",
431
                              First_Bit (CC), Comp);
432
 
433
                           Error_Msg_Uint_1 := Lbit;
434
                           Error_Msg_Uint_2 := Max_Machine_Scalar_Size;
435
                           Error_Msg_F
436
                             ("\last bit (^) exceeds maximum machine "
437
                              & "scalar size (^)",
438
                              First_Bit (CC));
439
 
440
                           if (Lbit + 1) mod SSU /= 0 then
441
                              Error_Msg_Uint_1 := SSU;
442
                              Error_Msg_F
443
                                ("\and is not a multiple of Storage_Unit (^) "
444
                                 & "(RM 13.4.1(10))",
445
                                 First_Bit (CC));
446
 
447
                           else
448
                              Error_Msg_Uint_1 := Fbit;
449
                              Error_Msg_F
450
                                ("\and first bit (^) is non-zero "
451
                                 & "(RM 13.4.1(10))",
452
                                 First_Bit (CC));
453
                           end if;
454
                        end if;
455
 
456
                     --  OK case of machine scalar related component clause,
457
                     --  For now, just count them.
458
 
459
                     else
460
                        Num_CC := Num_CC + 1;
461
                     end if;
462
                  end;
463
               end if;
464
 
465
               Next_Component_Or_Discriminant (Comp);
466
            end loop;
467
 
468
            --  We need to sort the component clauses on the basis of the
469
            --  Position values in the clause, so we can group clauses with
470
            --  the same Position. together to determine the relevant machine
471
            --  scalar size.
472
 
473
            Sort_CC : declare
474
               Comps : array (0 .. Num_CC) of Entity_Id;
475
               --  Array to collect component and discriminant entities. The
476
               --  data starts at index 1, the 0'th entry is for the sort
477
               --  routine.
478
 
479
               function CP_Lt (Op1, Op2 : Natural) return Boolean;
480
               --  Compare routine for Sort
481
 
482
               procedure CP_Move (From : Natural; To : Natural);
483
               --  Move routine for Sort
484
 
485
               package Sorting is new GNAT.Heap_Sort_G (CP_Move, CP_Lt);
486
 
487
               Start : Natural;
488
               Stop  : Natural;
489
               --  Start and stop positions in the component list of the set of
490
               --  components with the same starting position (that constitute
491
               --  components in a single machine scalar).
492
 
493
               MaxL  : Uint;
494
               --  Maximum last bit value of any component in this set
495
 
496
               MSS   : Uint;
497
               --  Corresponding machine scalar size
498
 
499
               -----------
500
               -- CP_Lt --
501
               -----------
502
 
503
               function CP_Lt (Op1, Op2 : Natural) return Boolean is
504
               begin
505
                  return Position (Component_Clause (Comps (Op1))) <
506
                    Position (Component_Clause (Comps (Op2)));
507
               end CP_Lt;
508
 
509
               -------------
510
               -- CP_Move --
511
               -------------
512
 
513
               procedure CP_Move (From : Natural; To : Natural) is
514
               begin
515
                  Comps (To) := Comps (From);
516
               end CP_Move;
517
 
518
               --  Start of processing for Sort_CC
519
 
520
            begin
521
               --  Collect the machine scalar relevant component clauses
522
 
523
               Num_CC := 0;
524
               Comp   := First_Component_Or_Discriminant (R);
525
               while Present (Comp) loop
526
                  declare
527
                     CC   : constant Node_Id := Component_Clause (Comp);
528
 
529
                  begin
530
                     --  Collect only component clauses whose last bit is less
531
                     --  than machine scalar size. Any component clause whose
532
                     --  last bit exceeds this value does not take part in
533
                     --  machine scalar layout considerations. The test for
534
                     --  Error_Posted makes sure we exclude component clauses
535
                     --  for which we already posted an error.
536
 
537
                     if Present (CC)
538
                       and then not Error_Posted (Last_Bit (CC))
539
                       and then Static_Integer (Last_Bit (CC)) <
540
                                                    Max_Machine_Scalar_Size
541
                     then
542
                        Num_CC := Num_CC + 1;
543
                        Comps (Num_CC) := Comp;
544
                     end if;
545
                  end;
546
 
547
                  Next_Component_Or_Discriminant (Comp);
548
               end loop;
549
 
550
               --  Sort by ascending position number
551
 
552
               Sorting.Sort (Num_CC);
553
 
554
               --  We now have all the components whose size does not exceed
555
               --  the max machine scalar value, sorted by starting position.
556
               --  In this loop we gather groups of clauses starting at the
557
               --  same position, to process them in accordance with AI-133.
558
 
559
               Stop := 0;
560
               while Stop < Num_CC loop
561
                  Start := Stop + 1;
562
                  Stop  := Start;
563
                  MaxL  :=
564
                    Static_Integer
565
                      (Last_Bit (Component_Clause (Comps (Start))));
566
                  while Stop < Num_CC loop
567
                     if Static_Integer
568
                          (Position (Component_Clause (Comps (Stop + 1)))) =
569
                        Static_Integer
570
                          (Position (Component_Clause (Comps (Stop))))
571
                     then
572
                        Stop := Stop + 1;
573
                        MaxL :=
574
                          UI_Max
575
                            (MaxL,
576
                             Static_Integer
577
                               (Last_Bit
578
                                  (Component_Clause (Comps (Stop)))));
579
                     else
580
                        exit;
581
                     end if;
582
                  end loop;
583
 
584
                  --  Now we have a group of component clauses from Start to
585
                  --  Stop whose positions are identical, and MaxL is the
586
                  --  maximum last bit value of any of these components.
587
 
588
                  --  We need to determine the corresponding machine scalar
589
                  --  size. This loop assumes that machine scalar sizes are
590
                  --  even, and that each possible machine scalar has twice
591
                  --  as many bits as the next smaller one.
592
 
593
                  MSS := Max_Machine_Scalar_Size;
594
                  while MSS mod 2 = 0
595
                    and then (MSS / 2) >= SSU
596
                    and then (MSS / 2) > MaxL
597
                  loop
598
                     MSS := MSS / 2;
599
                  end loop;
600
 
601
                  --  Here is where we fix up the Component_Bit_Offset value
602
                  --  to account for the reverse bit order. Some examples of
603
                  --  what needs to be done for the case of a machine scalar
604
                  --  size of 8 are:
605
 
606
                  --    First_Bit .. Last_Bit     Component_Bit_Offset
607
                  --      old          new          old       new
608
 
609
                  --     0 .. 0       7 .. 7         0         7
610
                  --     0 .. 1       6 .. 7         0         6
611
                  --     0 .. 2       5 .. 7         0         5
612
                  --     0 .. 7       0 .. 7         0         4
613
 
614
                  --     1 .. 1       6 .. 6         1         6
615
                  --     1 .. 4       3 .. 6         1         3
616
                  --     4 .. 7       0 .. 3         4         0
617
 
618
                  --  The rule is that the first bit is obtained by subtracting
619
                  --  the old ending bit from machine scalar size - 1.
620
 
621
                  for C in Start .. Stop loop
622
                     declare
623
                        Comp : constant Entity_Id := Comps (C);
624
                        CC   : constant Node_Id   :=
625
                                 Component_Clause (Comp);
626
                        LB   : constant Uint :=
627
                                 Static_Integer (Last_Bit (CC));
628
                        NFB  : constant Uint := MSS - Uint_1 - LB;
629
                        NLB  : constant Uint := NFB + Esize (Comp) - 1;
630
                        Pos  : constant Uint :=
631
                                 Static_Integer (Position (CC));
632
 
633
                     begin
634
                        if Warn_On_Reverse_Bit_Order then
635
                           Error_Msg_Uint_1 := MSS;
636
                           Error_Msg_N
637
                             ("info: reverse bit order in machine " &
638
                              "scalar of length^?", First_Bit (CC));
639
                           Error_Msg_Uint_1 := NFB;
640
                           Error_Msg_Uint_2 := NLB;
641
 
642
                           if Bytes_Big_Endian then
643
                              Error_Msg_NE
644
                                ("?\info: big-endian range for "
645
                                 & "component & is ^ .. ^",
646
                                 First_Bit (CC), Comp);
647
                           else
648
                              Error_Msg_NE
649
                                ("?\info: little-endian range "
650
                                 & "for component & is ^ .. ^",
651
                                 First_Bit (CC), Comp);
652
                           end if;
653
                        end if;
654
 
655
                        Set_Component_Bit_Offset (Comp, Pos * SSU + NFB);
656
                        Set_Normalized_First_Bit (Comp, NFB mod SSU);
657
                     end;
658
                  end loop;
659
               end loop;
660
            end Sort_CC;
661
         end;
662
      end if;
663
   end Adjust_Record_For_Reverse_Bit_Order;
664
 
665
   -------------------------------------
666
   -- Alignment_Check_For_Size_Change --
667
   -------------------------------------
668
 
669
   procedure Alignment_Check_For_Size_Change (Typ : Entity_Id; Size : Uint) is
670
   begin
671
      --  If the alignment is known, and not set by a rep clause, and is
672
      --  inconsistent with the size being set, then reset it to unknown,
673
      --  we assume in this case that the size overrides the inherited
674
      --  alignment, and that the alignment must be recomputed.
675
 
676
      if Known_Alignment (Typ)
677
        and then not Has_Alignment_Clause (Typ)
678
        and then Size mod (Alignment (Typ) * SSU) /= 0
679
      then
680
         Init_Alignment (Typ);
681
      end if;
682
   end Alignment_Check_For_Size_Change;
683
 
684
   -----------------------------------
685
   -- Analyze_Aspect_Specifications --
686
   -----------------------------------
687
 
688
   procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id) is
689
      Aspect : Node_Id;
690
      Aitem  : Node_Id;
691
      Ent    : Node_Id;
692
 
693
      L : constant List_Id := Aspect_Specifications (N);
694
 
695
      Ins_Node : Node_Id := N;
696
      --  Insert pragmas (except Pre/Post/Invariant/Predicate) after this node
697
 
698
      --  The general processing involves building an attribute definition
699
      --  clause or a pragma node that corresponds to the aspect. Then one
700
      --  of two things happens:
701
 
702
      --  If we are required to delay the evaluation of this aspect to the
703
      --  freeze point, we attach the corresponding pragma/attribute definition
704
      --  clause to the aspect specification node, which is then placed in the
705
      --  Rep Item chain. In this case we mark the entity by setting the flag
706
      --  Has_Delayed_Aspects and we evaluate the rep item at the freeze point.
707
 
708
      --  If no delay is required, we just insert the pragma or attribute
709
      --  after the declaration, and it will get processed by the normal
710
      --  circuit. The From_Aspect_Specification flag is set on the pragma
711
      --  or attribute definition node in either case to activate special
712
      --  processing (e.g. not traversing the list of homonyms for inline).
713
 
714
      Delay_Required : Boolean := False;
715
      --  Set True if delay is required
716
 
717
   begin
718
      pragma Assert (Present (L));
719
 
720
      --  Loop through aspects
721
 
722
      Aspect := First (L);
723
      Aspect_Loop : while Present (Aspect) loop
724
         declare
725
            Loc  : constant Source_Ptr := Sloc (Aspect);
726
            Id   : constant Node_Id    := Identifier (Aspect);
727
            Expr : constant Node_Id    := Expression (Aspect);
728
            Nam  : constant Name_Id    := Chars (Id);
729
            A_Id : constant Aspect_Id  := Get_Aspect_Id (Nam);
730
            Anod : Node_Id;
731
 
732
            Eloc : Source_Ptr := No_Location;
733
            --  Source location of expression, modified when we split PPC's. It
734
            --  is set below when Expr is present.
735
 
736
            procedure Check_False_Aspect_For_Derived_Type;
737
            --  This procedure checks for the case of a false aspect for a
738
            --  derived type, which improperly tries to cancel an aspect
739
            --  inherited from the parent;
740
 
741
            -----------------------------------------
742
            -- Check_False_Aspect_For_Derived_Type --
743
            -----------------------------------------
744
 
745
            procedure Check_False_Aspect_For_Derived_Type is
746
            begin
747
               --  We are only checking derived types
748
 
749
               if not Is_Derived_Type (E) then
750
                  return;
751
               end if;
752
 
753
               case A_Id is
754
                  when Aspect_Atomic | Aspect_Shared =>
755
                     if not Is_Atomic (E) then
756
                        return;
757
                     end if;
758
 
759
                  when Aspect_Atomic_Components =>
760
                     if not Has_Atomic_Components (E) then
761
                        return;
762
                     end if;
763
 
764
                  when Aspect_Discard_Names =>
765
                     if not Discard_Names (E) then
766
                        return;
767
                     end if;
768
 
769
                  when Aspect_Pack =>
770
                     if not Is_Packed (E) then
771
                        return;
772
                     end if;
773
 
774
                  when Aspect_Unchecked_Union =>
775
                     if not Is_Unchecked_Union (E) then
776
                        return;
777
                     end if;
778
 
779
                  when Aspect_Volatile =>
780
                     if not Is_Volatile (E) then
781
                        return;
782
                     end if;
783
 
784
                  when Aspect_Volatile_Components =>
785
                     if not Has_Volatile_Components (E) then
786
                        return;
787
                     end if;
788
 
789
                  when others =>
790
                     return;
791
               end case;
792
 
793
               --  Fall through means we are canceling an inherited aspect
794
 
795
               Error_Msg_Name_1 := Nam;
796
               Error_Msg_NE
797
                 ("derived type& inherits aspect%, cannot cancel", Expr, E);
798
            end Check_False_Aspect_For_Derived_Type;
799
 
800
         --  Start of processing for Aspect_Loop
801
 
802
         begin
803
            --  Skip aspect if already analyzed (not clear if this is needed)
804
 
805
            if Analyzed (Aspect) then
806
               goto Continue;
807
            end if;
808
 
809
            --  Set the source location of expression, used in the case of
810
            --  a failed precondition/postcondition or invariant. Note that
811
            --  the source location of the expression is not usually the best
812
            --  choice here. For example, it gets located on the last AND
813
            --  keyword in a chain of boolean expressiond AND'ed together.
814
            --  It is best to put the message on the first character of the
815
            --  assertion, which is the effect of the First_Node call here.
816
 
817
            if Present (Expr) then
818
               Eloc := Sloc (First_Node (Expr));
819
            end if;
820
 
821
            --  Check restriction No_Implementation_Aspect_Specifications
822
 
823
            if Impl_Defined_Aspects (A_Id) then
824
               Check_Restriction
825
                 (No_Implementation_Aspect_Specifications, Aspect);
826
            end if;
827
 
828
            --  Check restriction No_Specification_Of_Aspect
829
 
830
            Check_Restriction_No_Specification_Of_Aspect (Aspect);
831
 
832
            --  Analyze this aspect
833
 
834
            Set_Analyzed (Aspect);
835
            Set_Entity (Aspect, E);
836
            Ent := New_Occurrence_Of (E, Sloc (Id));
837
 
838
            --  Check for duplicate aspect. Note that the Comes_From_Source
839
            --  test allows duplicate Pre/Post's that we generate internally
840
            --  to escape being flagged here.
841
 
842
            if No_Duplicates_Allowed (A_Id) then
843
               Anod := First (L);
844
               while Anod /= Aspect loop
845
                  if Same_Aspect
846
                      (A_Id, Get_Aspect_Id (Chars (Identifier (Anod))))
847
                    and then Comes_From_Source (Aspect)
848
                  then
849
                     Error_Msg_Name_1 := Nam;
850
                     Error_Msg_Sloc := Sloc (Anod);
851
 
852
                     --  Case of same aspect specified twice
853
 
854
                     if Class_Present (Anod) = Class_Present (Aspect) then
855
                        if not Class_Present (Anod) then
856
                           Error_Msg_NE
857
                             ("aspect% for & previously given#",
858
                              Id, E);
859
                        else
860
                           Error_Msg_NE
861
                             ("aspect `%''Class` for & previously given#",
862
                              Id, E);
863
                        end if;
864
 
865
                        --  Case of Pre and Pre'Class both specified
866
 
867
                     elsif Nam = Name_Pre then
868
                        if Class_Present (Aspect) then
869
                           Error_Msg_NE
870
                             ("aspect `Pre''Class` for & is not allowed here",
871
                              Id, E);
872
                           Error_Msg_NE
873
                             ("\since aspect `Pre` previously given#",
874
                              Id, E);
875
 
876
                        else
877
                           Error_Msg_NE
878
                             ("aspect `Pre` for & is not allowed here",
879
                              Id, E);
880
                           Error_Msg_NE
881
                             ("\since aspect `Pre''Class` previously given#",
882
                              Id, E);
883
                        end if;
884
                     end if;
885
 
886
                     --  Allowed case of X and X'Class both specified
887
                  end if;
888
 
889
                  Next (Anod);
890
               end loop;
891
            end if;
892
 
893
            --  Check some general restrictions on language defined aspects
894
 
895
            if not Impl_Defined_Aspects (A_Id) then
896
               Error_Msg_Name_1 := Nam;
897
 
898
               --  Not allowed for renaming declarations
899
 
900
               if Nkind (N) in N_Renaming_Declaration then
901
                  Error_Msg_N
902
                    ("aspect % not allowed for renaming declaration",
903
                     Aspect);
904
               end if;
905
 
906
               --  Not allowed for formal type declarations
907
 
908
               if Nkind (N) = N_Formal_Type_Declaration then
909
                  Error_Msg_N
910
                    ("aspect % not allowed for formal type declaration",
911
                     Aspect);
912
               end if;
913
            end if;
914
 
915
            --  Copy expression for later processing by the procedures
916
            --  Check_Aspect_At_[Freeze_Point | End_Of_Declarations]
917
 
918
            Set_Entity (Id, New_Copy_Tree (Expr));
919
 
920
            --  Processing based on specific aspect
921
 
922
            case A_Id is
923
 
924
               --  No_Aspect should be impossible
925
 
926
               when No_Aspect =>
927
                  raise Program_Error;
928
 
929
               --  Aspects taking an optional boolean argument. For all of
930
               --  these we just create a matching pragma and insert it, if
931
               --  the expression is missing or set to True. If the expression
932
               --  is False, we can ignore the aspect with the exception that
933
               --  in the case of a derived type, we must check for an illegal
934
               --  attempt to cancel an inherited aspect.
935
 
936
               when Boolean_Aspects =>
937
                  Set_Is_Boolean_Aspect (Aspect);
938
 
939
                  if Present (Expr)
940
                    and then Is_False (Static_Boolean (Expr))
941
                  then
942
                     Check_False_Aspect_For_Derived_Type;
943
                     goto Continue;
944
                  end if;
945
 
946
                  --  If True, build corresponding pragma node
947
 
948
                  Aitem :=
949
                    Make_Pragma (Loc,
950
                      Pragma_Argument_Associations => New_List (Ent),
951
                      Pragma_Identifier            =>
952
                        Make_Identifier (Sloc (Id), Chars (Id)));
953
 
954
                  --  Never need to delay for boolean aspects
955
 
956
                  pragma Assert (not Delay_Required);
957
 
958
               --  Library unit aspects. These are boolean aspects, but we
959
               --  have to do special things with the insertion, since the
960
               --  pragma belongs inside the declarations of a package.
961
 
962
               when Library_Unit_Aspects =>
963
                  if Present (Expr)
964
                    and then Is_False (Static_Boolean (Expr))
965
                  then
966
                     goto Continue;
967
                  end if;
968
 
969
                  --  Build corresponding pragma node
970
 
971
                  Aitem :=
972
                    Make_Pragma (Loc,
973
                      Pragma_Argument_Associations => New_List (Ent),
974
                      Pragma_Identifier            =>
975
                        Make_Identifier (Sloc (Id), Chars (Id)));
976
 
977
                  --  This requires special handling in the case of a package
978
                  --  declaration, the pragma needs to be inserted in the list
979
                  --  of declarations for the associated package. There is no
980
                  --  issue of visibility delay for these aspects.
981
 
982
                  if Nkind (N) = N_Package_Declaration then
983
                     if Nkind (Parent (N)) /= N_Compilation_Unit then
984
                        Error_Msg_N
985
                          ("incorrect context for library unit aspect&", Id);
986
                     else
987
                        Prepend
988
                          (Aitem, Visible_Declarations (Specification (N)));
989
                     end if;
990
 
991
                     goto Continue;
992
                  end if;
993
 
994
                  --  If not package declaration, no delay is required
995
 
996
                  pragma Assert (not Delay_Required);
997
 
998
               --  Aspects related to container iterators. These aspects denote
999
               --  subprograms, and thus must be delayed.
1000
 
1001
               when Aspect_Constant_Indexing    |
1002
                    Aspect_Variable_Indexing    =>
1003
 
1004
                  if not Is_Type (E) or else not Is_Tagged_Type (E) then
1005
                     Error_Msg_N ("indexing applies to a tagged type", N);
1006
                  end if;
1007
 
1008
                  Aitem :=
1009
                    Make_Attribute_Definition_Clause (Loc,
1010
                      Name       => Ent,
1011
                      Chars      => Chars (Id),
1012
                      Expression => Relocate_Node (Expr));
1013
 
1014
                  Delay_Required := True;
1015
                  Set_Is_Delayed_Aspect (Aspect);
1016
 
1017
               when Aspect_Default_Iterator     |
1018
                    Aspect_Iterator_Element     =>
1019
 
1020
                  Aitem :=
1021
                    Make_Attribute_Definition_Clause (Loc,
1022
                      Name       => Ent,
1023
                      Chars      => Chars (Id),
1024
                      Expression => Relocate_Node (Expr));
1025
 
1026
                  Delay_Required := True;
1027
                  Set_Is_Delayed_Aspect (Aspect);
1028
 
1029
               when Aspect_Implicit_Dereference =>
1030
                  if not Is_Type (E)
1031
                    or else not Has_Discriminants (E)
1032
                  then
1033
                     Error_Msg_N
1034
                       ("Aspect must apply to a type with discriminants", N);
1035
                     goto Continue;
1036
 
1037
                  else
1038
                     declare
1039
                        Disc : Entity_Id;
1040
 
1041
                     begin
1042
                        Disc := First_Discriminant (E);
1043
                        while Present (Disc) loop
1044
                           if Chars (Expr) = Chars (Disc)
1045
                             and then Ekind (Etype (Disc)) =
1046
                               E_Anonymous_Access_Type
1047
                           then
1048
                              Set_Has_Implicit_Dereference (E);
1049
                              Set_Has_Implicit_Dereference (Disc);
1050
                              goto Continue;
1051
                           end if;
1052
 
1053
                           Next_Discriminant (Disc);
1054
                        end loop;
1055
 
1056
                        --  Error if no proper access discriminant.
1057
 
1058
                        Error_Msg_NE
1059
                         ("not an access discriminant of&", Expr, E);
1060
                     end;
1061
 
1062
                     goto Continue;
1063
                  end if;
1064
 
1065
               --  Aspects corresponding to attribute definition clauses
1066
 
1067
               when Aspect_Address             |
1068
                    Aspect_Alignment           |
1069
                    Aspect_Bit_Order           |
1070
                    Aspect_Component_Size      |
1071
                    Aspect_External_Tag        |
1072
                    Aspect_Input               |
1073
                    Aspect_Machine_Radix       |
1074
                    Aspect_Object_Size         |
1075
                    Aspect_Output              |
1076
                    Aspect_Read                |
1077
                    Aspect_Size                |
1078
                    Aspect_Small               |
1079
                    Aspect_Simple_Storage_Pool |
1080
                    Aspect_Storage_Pool        |
1081
                    Aspect_Storage_Size        |
1082
                    Aspect_Stream_Size         |
1083
                    Aspect_Value_Size          |
1084
                    Aspect_Write               =>
1085
 
1086
                  --  Construct the attribute definition clause
1087
 
1088
                  Aitem :=
1089
                    Make_Attribute_Definition_Clause (Loc,
1090
                      Name       => Ent,
1091
                      Chars      => Chars (Id),
1092
                      Expression => Relocate_Node (Expr));
1093
 
1094
                  --  A delay is required except in the common case where
1095
                  --  the expression is a literal, in which case it is fine
1096
                  --  to take care of it right away.
1097
 
1098
                  if Nkind_In (Expr, N_Integer_Literal, N_String_Literal) then
1099
                     pragma Assert (not Delay_Required);
1100
                     null;
1101
                  else
1102
                     Delay_Required := True;
1103
                     Set_Is_Delayed_Aspect (Aspect);
1104
                  end if;
1105
 
1106
               --  Aspects corresponding to pragmas with two arguments, where
1107
               --  the first argument is a local name referring to the entity,
1108
               --  and the second argument is the aspect definition expression
1109
               --  which is an expression that does not get analyzed.
1110
 
1111
               when Aspect_Suppress   |
1112
                    Aspect_Unsuppress =>
1113
 
1114
                  --  Construct the pragma
1115
 
1116
                  Aitem :=
1117
                    Make_Pragma (Loc,
1118
                      Pragma_Argument_Associations => New_List (
1119
                        New_Occurrence_Of (E, Loc),
1120
                        Relocate_Node (Expr)),
1121
                      Pragma_Identifier            =>
1122
                        Make_Identifier (Sloc (Id), Chars (Id)));
1123
 
1124
                  --  We don't have to play the delay game here, since the only
1125
                  --  values are check names which don't get analyzed anyway.
1126
 
1127
                  pragma Assert (not Delay_Required);
1128
 
1129
               when Aspect_Synchronization =>
1130
 
1131
                  --  The aspect corresponds to pragma Implemented.
1132
                  --  Construct the pragma
1133
 
1134
                  Aitem :=
1135
                    Make_Pragma (Loc,
1136
                      Pragma_Argument_Associations => New_List (
1137
                        New_Occurrence_Of (E, Loc),
1138
                        Relocate_Node (Expr)),
1139
                      Pragma_Identifier            =>
1140
                        Make_Identifier (Sloc (Id), Name_Implemented));
1141
 
1142
                  pragma Assert (not Delay_Required);
1143
 
1144
               --  Aspects corresponding to pragmas with two arguments, where
1145
               --  the second argument is a local name referring to the entity,
1146
               --  and the first argument is the aspect definition expression.
1147
 
1148
               when Aspect_Warnings =>
1149
 
1150
                  --  Construct the pragma
1151
 
1152
                  Aitem :=
1153
                    Make_Pragma (Loc,
1154
                      Pragma_Argument_Associations => New_List (
1155
                        Relocate_Node (Expr),
1156
                        New_Occurrence_Of (E, Loc)),
1157
                      Pragma_Identifier            =>
1158
                        Make_Identifier (Sloc (Id), Chars (Id)),
1159
                      Class_Present                => Class_Present (Aspect));
1160
 
1161
                  --  We don't have to play the delay game here, since the only
1162
                  --  values are ON/OFF which don't get analyzed anyway.
1163
 
1164
                  pragma Assert (not Delay_Required);
1165
 
1166
               --  Default_Value and Default_Component_Value aspects. These
1167
               --  are specially handled because they have no corresponding
1168
               --  pragmas or attributes.
1169
 
1170
               when Aspect_Default_Value | Aspect_Default_Component_Value =>
1171
                  Error_Msg_Name_1 := Chars (Id);
1172
 
1173
                  if not Is_Type (E) then
1174
                     Error_Msg_N ("aspect% can only apply to a type", Id);
1175
                     goto Continue;
1176
 
1177
                  elsif not Is_First_Subtype (E) then
1178
                     Error_Msg_N ("aspect% cannot apply to subtype", Id);
1179
                     goto Continue;
1180
 
1181
                  elsif A_Id = Aspect_Default_Value
1182
                    and then not Is_Scalar_Type (E)
1183
                  then
1184
                     Error_Msg_N
1185
                       ("aspect% can only be applied to scalar type", Id);
1186
                     goto Continue;
1187
 
1188
                  elsif A_Id = Aspect_Default_Component_Value then
1189
                     if not Is_Array_Type (E) then
1190
                        Error_Msg_N
1191
                          ("aspect% can only be applied to array type", Id);
1192
                        goto Continue;
1193
                     elsif not Is_Scalar_Type (Component_Type (E)) then
1194
                        Error_Msg_N
1195
                          ("aspect% requires scalar components", Id);
1196
                        goto Continue;
1197
                     end if;
1198
                  end if;
1199
 
1200
                  Aitem := Empty;
1201
                  Delay_Required := True;
1202
                  Set_Is_Delayed_Aspect (Aspect);
1203
                  Set_Has_Default_Aspect (Base_Type (Entity (Ent)));
1204
 
1205
                  if Is_Scalar_Type (E) then
1206
                     Set_Default_Aspect_Value (Entity (Ent), Expr);
1207
                  else
1208
                     Set_Default_Aspect_Component_Value (Entity (Ent), Expr);
1209
                  end if;
1210
 
1211
               when Aspect_Attach_Handler =>
1212
                  Aitem :=
1213
                    Make_Pragma (Loc,
1214
                      Pragma_Identifier            =>
1215
                        Make_Identifier (Sloc (Id), Name_Attach_Handler),
1216
                      Pragma_Argument_Associations =>
1217
                        New_List (Ent, Relocate_Node (Expr)));
1218
 
1219
                  Set_From_Aspect_Specification (Aitem, True);
1220
                  Set_Corresponding_Aspect (Aitem, Aspect);
1221
 
1222
                  pragma Assert (not Delay_Required);
1223
 
1224
               when Aspect_Priority           |
1225
                    Aspect_Interrupt_Priority |
1226
                    Aspect_Dispatching_Domain |
1227
                    Aspect_CPU                =>
1228
                  declare
1229
                     Pname : Name_Id;
1230
 
1231
                  begin
1232
                     if A_Id = Aspect_Priority then
1233
                        Pname := Name_Priority;
1234
 
1235
                     elsif A_Id = Aspect_Interrupt_Priority then
1236
                        Pname := Name_Interrupt_Priority;
1237
 
1238
                     elsif A_Id = Aspect_CPU then
1239
                        Pname := Name_CPU;
1240
 
1241
                     else
1242
                        Pname := Name_Dispatching_Domain;
1243
                     end if;
1244
 
1245
                     Aitem :=
1246
                       Make_Pragma (Loc,
1247
                           Pragma_Identifier            =>
1248
                             Make_Identifier (Sloc (Id), Pname),
1249
                           Pragma_Argument_Associations =>
1250
                             New_List
1251
                               (Make_Pragma_Argument_Association
1252
                                  (Sloc       => Sloc (Id),
1253
                                   Expression => Relocate_Node (Expr))));
1254
 
1255
                     Set_From_Aspect_Specification (Aitem, True);
1256
                     Set_Corresponding_Aspect (Aitem, Aspect);
1257
 
1258
                     pragma Assert (not Delay_Required);
1259
                  end;
1260
 
1261
               --  Aspects Pre/Post generate Precondition/Postcondition pragmas
1262
               --  with a first argument that is the expression, and a second
1263
               --  argument that is an informative message if the test fails.
1264
               --  This is inserted right after the declaration, to get the
1265
               --  required pragma placement. The processing for the pragmas
1266
               --  takes care of the required delay.
1267
 
1268
               when Pre_Post_Aspects => declare
1269
                  Pname : Name_Id;
1270
 
1271
               begin
1272
                  if A_Id = Aspect_Pre or else A_Id = Aspect_Precondition then
1273
                     Pname := Name_Precondition;
1274
                  else
1275
                     Pname := Name_Postcondition;
1276
                  end if;
1277
 
1278
                  --  If the expressions is of the form A and then B, then
1279
                  --  we generate separate Pre/Post aspects for the separate
1280
                  --  clauses. Since we allow multiple pragmas, there is no
1281
                  --  problem in allowing multiple Pre/Post aspects internally.
1282
                  --  These should be treated in reverse order (B first and
1283
                  --  A second) since they are later inserted just after N in
1284
                  --  the order they are treated. This way, the pragma for A
1285
                  --  ends up preceding the pragma for B, which may have an
1286
                  --  importance for the error raised (either constraint error
1287
                  --  or precondition error).
1288
 
1289
                  --  We do not do this for Pre'Class, since we have to put
1290
                  --  these conditions together in a complex OR expression
1291
 
1292
                  --  We do not do this in ASIS mode, as ASIS relies on the
1293
                  --  original node representing the complete expression, when
1294
                  --  retrieving it through the source aspect table.
1295
 
1296
                  if not ASIS_Mode
1297
                    and then (Pname = Name_Postcondition
1298
                               or else not Class_Present (Aspect))
1299
                  then
1300
                     while Nkind (Expr) = N_And_Then loop
1301
                        Insert_After (Aspect,
1302
                          Make_Aspect_Specification (Sloc (Left_Opnd (Expr)),
1303
                            Identifier    => Identifier (Aspect),
1304
                            Expression    => Relocate_Node (Left_Opnd (Expr)),
1305
                            Class_Present => Class_Present (Aspect),
1306
                            Split_PPC     => True));
1307
                        Rewrite (Expr, Relocate_Node (Right_Opnd (Expr)));
1308
                        Eloc := Sloc (Expr);
1309
                     end loop;
1310
                  end if;
1311
 
1312
                  --  Build the precondition/postcondition pragma
1313
 
1314
                  Aitem :=
1315
                    Make_Pragma (Loc,
1316
                      Pragma_Identifier            =>
1317
                        Make_Identifier (Sloc (Id), Pname),
1318
                      Class_Present                => Class_Present (Aspect),
1319
                      Split_PPC                    => Split_PPC (Aspect),
1320
                      Pragma_Argument_Associations => New_List (
1321
                        Make_Pragma_Argument_Association (Eloc,
1322
                          Chars      => Name_Check,
1323
                          Expression => Relocate_Node (Expr))));
1324
 
1325
                  --  Add message unless exception messages are suppressed
1326
 
1327
                  if not Opt.Exception_Locations_Suppressed then
1328
                     Append_To (Pragma_Argument_Associations (Aitem),
1329
                       Make_Pragma_Argument_Association (Eloc,
1330
                         Chars     => Name_Message,
1331
                         Expression =>
1332
                           Make_String_Literal (Eloc,
1333
                             Strval => "failed "
1334
                                       & Get_Name_String (Pname)
1335
                                       & " from "
1336
                                       & Build_Location_String (Eloc))));
1337
                  end if;
1338
 
1339
                  Set_From_Aspect_Specification (Aitem, True);
1340
                  Set_Corresponding_Aspect (Aitem, Aspect);
1341
                  Set_Is_Delayed_Aspect (Aspect);
1342
 
1343
                  --  For Pre/Post cases, insert immediately after the entity
1344
                  --  declaration, since that is the required pragma placement.
1345
                  --  Note that for these aspects, we do not have to worry
1346
                  --  about delay issues, since the pragmas themselves deal
1347
                  --  with delay of visibility for the expression analysis.
1348
 
1349
                  --  If the entity is a library-level subprogram, the pre/
1350
                  --  postconditions must be treated as late pragmas.
1351
 
1352
                  if Nkind (Parent (N)) = N_Compilation_Unit then
1353
                     Add_Global_Declaration (Aitem);
1354
                  else
1355
                     Insert_After (N, Aitem);
1356
                  end if;
1357
 
1358
                  goto Continue;
1359
               end;
1360
 
1361
               --  Invariant aspects generate a corresponding pragma with a
1362
               --  first argument that is the entity, a second argument that is
1363
               --  the expression and a third argument that is an appropriate
1364
               --  message. This is inserted right after the declaration, to
1365
               --  get the required pragma placement. The pragma processing
1366
               --  takes care of the required delay.
1367
 
1368
               when Aspect_Invariant      |
1369
                    Aspect_Type_Invariant =>
1370
 
1371
                  --  Analysis of the pragma will verify placement legality:
1372
                  --  an invariant must apply to a private type, or appear in
1373
                  --  the private part of a spec and apply to a completion.
1374
 
1375
                  --  Construct the pragma
1376
 
1377
                  Aitem :=
1378
                    Make_Pragma (Loc,
1379
                      Pragma_Argument_Associations =>
1380
                        New_List (Ent, Relocate_Node (Expr)),
1381
                      Class_Present                => Class_Present (Aspect),
1382
                      Pragma_Identifier            =>
1383
                        Make_Identifier (Sloc (Id), Name_Invariant));
1384
 
1385
                  --  Add message unless exception messages are suppressed
1386
 
1387
                  if not Opt.Exception_Locations_Suppressed then
1388
                     Append_To (Pragma_Argument_Associations (Aitem),
1389
                       Make_Pragma_Argument_Association (Eloc,
1390
                         Chars      => Name_Message,
1391
                         Expression =>
1392
                           Make_String_Literal (Eloc,
1393
                             Strval => "failed invariant from "
1394
                                       & Build_Location_String (Eloc))));
1395
                  end if;
1396
 
1397
                  Set_From_Aspect_Specification (Aitem, True);
1398
                  Set_Corresponding_Aspect (Aitem, Aspect);
1399
                  Set_Is_Delayed_Aspect (Aspect);
1400
 
1401
                  --  For Invariant case, insert immediately after the entity
1402
                  --  declaration. We do not have to worry about delay issues
1403
                  --  since the pragma processing takes care of this.
1404
 
1405
                  Insert_After (N, Aitem);
1406
                  goto Continue;
1407
 
1408
               --  Predicate aspects generate a corresponding pragma with a
1409
               --  first argument that is the entity, and the second argument
1410
               --  is the expression.
1411
 
1412
               when Aspect_Dynamic_Predicate |
1413
                    Aspect_Predicate         |
1414
                    Aspect_Static_Predicate  =>
1415
 
1416
                  --  Construct the pragma (always a pragma Predicate, with
1417
                  --  flags recording whether it is static/dynamic).
1418
 
1419
                  Aitem :=
1420
                    Make_Pragma (Loc,
1421
                      Pragma_Argument_Associations =>
1422
                        New_List (Ent, Relocate_Node (Expr)),
1423
                      Class_Present                => Class_Present (Aspect),
1424
                      Pragma_Identifier            =>
1425
                        Make_Identifier (Sloc (Id), Name_Predicate));
1426
 
1427
                  Set_From_Aspect_Specification (Aitem, True);
1428
                  Set_Corresponding_Aspect (Aitem, Aspect);
1429
 
1430
                  --  Make sure we have a freeze node (it might otherwise be
1431
                  --  missing in cases like subtype X is Y, and we would not
1432
                  --  have a place to build the predicate function).
1433
 
1434
                  --  If the type is private, indicate that its completion
1435
                  --  has a freeze node, because that is the one that will be
1436
                  --  visible at freeze time.
1437
 
1438
                  Set_Has_Predicates (E);
1439
 
1440
                  if Is_Private_Type (E)
1441
                    and then Present (Full_View (E))
1442
                  then
1443
                     Set_Has_Predicates (Full_View (E));
1444
                     Set_Has_Delayed_Aspects (Full_View (E));
1445
                     Ensure_Freeze_Node (Full_View (E));
1446
                  end if;
1447
 
1448
                  Ensure_Freeze_Node (E);
1449
                  Set_Is_Delayed_Aspect (Aspect);
1450
                  Delay_Required := True;
1451
 
1452
               when Aspect_Test_Case => declare
1453
                  Args      : List_Id;
1454
                  Comp_Expr : Node_Id;
1455
                  Comp_Assn : Node_Id;
1456
                  New_Expr  : Node_Id;
1457
 
1458
               begin
1459
                  Args := New_List;
1460
 
1461
                  if Nkind (Parent (N)) = N_Compilation_Unit then
1462
                     Error_Msg_N
1463
                       ("incorrect placement of aspect `Test_Case`", E);
1464
                     goto Continue;
1465
                  end if;
1466
 
1467
                  if Nkind (Expr) /= N_Aggregate then
1468
                     Error_Msg_NE
1469
                       ("wrong syntax for aspect `Test_Case` for &", Id, E);
1470
                     goto Continue;
1471
                  end if;
1472
 
1473
                  --  Make pragma expressions refer to the original aspect
1474
                  --  expressions through the Original_Node link. This is used
1475
                  --  in semantic analysis for ASIS mode, so that the original
1476
                  --  expression also gets analyzed.
1477
 
1478
                  Comp_Expr := First (Expressions (Expr));
1479
                  while Present (Comp_Expr) loop
1480
                     New_Expr := Relocate_Node (Comp_Expr);
1481
                     Set_Original_Node (New_Expr, Comp_Expr);
1482
                     Append
1483
                       (Make_Pragma_Argument_Association (Sloc (Comp_Expr),
1484
                          Expression => New_Expr),
1485
                       Args);
1486
                     Next (Comp_Expr);
1487
                  end loop;
1488
 
1489
                  Comp_Assn := First (Component_Associations (Expr));
1490
                  while Present (Comp_Assn) loop
1491
                     if List_Length (Choices (Comp_Assn)) /= 1
1492
                       or else
1493
                         Nkind (First (Choices (Comp_Assn))) /= N_Identifier
1494
                     then
1495
                        Error_Msg_NE
1496
                          ("wrong syntax for aspect `Test_Case` for &", Id, E);
1497
                        goto Continue;
1498
                     end if;
1499
 
1500
                     New_Expr := Relocate_Node (Expression (Comp_Assn));
1501
                     Set_Original_Node (New_Expr, Expression (Comp_Assn));
1502
                     Append (Make_Pragma_Argument_Association (
1503
                       Sloc       => Sloc (Comp_Assn),
1504
                       Chars      => Chars (First (Choices (Comp_Assn))),
1505
                       Expression => New_Expr),
1506
                       Args);
1507
                     Next (Comp_Assn);
1508
                  end loop;
1509
 
1510
                  --  Build the test-case pragma
1511
 
1512
                  Aitem :=
1513
                    Make_Pragma (Loc,
1514
                      Pragma_Identifier            =>
1515
                        Make_Identifier (Sloc (Id), Name_Test_Case),
1516
                      Pragma_Argument_Associations =>
1517
                        Args);
1518
 
1519
                  Set_From_Aspect_Specification (Aitem, True);
1520
                  Set_Corresponding_Aspect (Aitem, Aspect);
1521
                  Set_Is_Delayed_Aspect (Aspect);
1522
 
1523
                  --  Insert immediately after the entity declaration
1524
 
1525
                  Insert_After (N, Aitem);
1526
 
1527
                  goto Continue;
1528
               end;
1529
 
1530
               when Aspect_Dimension =>
1531
                  Analyze_Aspect_Dimension (N, Id, Expr);
1532
                  goto Continue;
1533
 
1534
               when Aspect_Dimension_System =>
1535
                  Analyze_Aspect_Dimension_System (N, Id, Expr);
1536
                  goto Continue;
1537
 
1538
            end case;
1539
 
1540
            --  If a delay is required, we delay the freeze (not much point in
1541
            --  delaying the aspect if we don't delay the freeze!). The pragma
1542
            --  or attribute clause if there is one is then attached to the
1543
            --  aspect specification which is placed in the rep item list.
1544
 
1545
            if Delay_Required then
1546
               if Present (Aitem) then
1547
                  Set_From_Aspect_Specification (Aitem, True);
1548
 
1549
                  if Nkind (Aitem) = N_Pragma then
1550
                     Set_Corresponding_Aspect (Aitem, Aspect);
1551
                  end if;
1552
 
1553
                  Set_Is_Delayed_Aspect (Aitem);
1554
                  Set_Aspect_Rep_Item (Aspect, Aitem);
1555
               end if;
1556
 
1557
               Ensure_Freeze_Node (E);
1558
               Set_Has_Delayed_Aspects (E);
1559
               Record_Rep_Item (E, Aspect);
1560
 
1561
            --  If no delay required, insert the pragma/clause in the tree
1562
 
1563
            else
1564
               Set_From_Aspect_Specification (Aitem, True);
1565
 
1566
               if Nkind (Aitem) = N_Pragma then
1567
                  Set_Corresponding_Aspect (Aitem, Aspect);
1568
               end if;
1569
 
1570
               --  If this is a compilation unit, we will put the pragma in
1571
               --  the Pragmas_After list of the N_Compilation_Unit_Aux node.
1572
 
1573
               if Nkind (Parent (Ins_Node)) = N_Compilation_Unit then
1574
                  declare
1575
                     Aux : constant Node_Id :=
1576
                             Aux_Decls_Node (Parent (Ins_Node));
1577
 
1578
                  begin
1579
                     pragma Assert (Nkind (Aux) = N_Compilation_Unit_Aux);
1580
 
1581
                     if No (Pragmas_After (Aux)) then
1582
                        Set_Pragmas_After (Aux, Empty_List);
1583
                     end if;
1584
 
1585
                     --  For Pre_Post put at start of list, otherwise at end
1586
 
1587
                     if A_Id in Pre_Post_Aspects then
1588
                        Prepend (Aitem, Pragmas_After (Aux));
1589
                     else
1590
                        Append (Aitem, Pragmas_After (Aux));
1591
                     end if;
1592
                  end;
1593
 
1594
               --  Here if not compilation unit case
1595
 
1596
               else
1597
                  case A_Id is
1598
 
1599
                     --  For Pre/Post cases, insert immediately after the
1600
                     --  entity declaration, since that is the required pragma
1601
                     --  placement.
1602
 
1603
                     when Pre_Post_Aspects =>
1604
                        Insert_After (N, Aitem);
1605
 
1606
                     --  For Priority aspects, insert into the task or
1607
                     --  protected definition, which we need to create if it's
1608
                     --  not there. The same applies to CPU and
1609
                     --  Dispatching_Domain but only to tasks.
1610
 
1611
                     when Aspect_Priority           |
1612
                          Aspect_Interrupt_Priority |
1613
                          Aspect_Dispatching_Domain |
1614
                          Aspect_CPU                =>
1615
                        declare
1616
                           T : Node_Id; -- the type declaration
1617
                           L : List_Id; -- list of decls of task/protected
1618
 
1619
                        begin
1620
                           if Nkind (N) = N_Object_Declaration then
1621
                              T := Parent (Etype (Defining_Identifier (N)));
1622
                           else
1623
                              T := N;
1624
                           end if;
1625
 
1626
                           if Nkind (T) = N_Protected_Type_Declaration
1627
                             and then A_Id /= Aspect_Dispatching_Domain
1628
                             and then A_Id /= Aspect_CPU
1629
                           then
1630
                              pragma Assert
1631
                                (Present (Protected_Definition (T)));
1632
 
1633
                              L := Visible_Declarations
1634
                                     (Protected_Definition (T));
1635
 
1636
                           elsif Nkind (T) = N_Task_Type_Declaration then
1637
                              if No (Task_Definition (T)) then
1638
                                 Set_Task_Definition
1639
                                   (T,
1640
                                    Make_Task_Definition
1641
                                      (Sloc (T),
1642
                                       Visible_Declarations => New_List,
1643
                                       End_Label => Empty));
1644
                              end if;
1645
 
1646
                              L := Visible_Declarations (Task_Definition (T));
1647
 
1648
                           else
1649
                              raise Program_Error;
1650
                           end if;
1651
 
1652
                           Prepend (Aitem, To => L);
1653
 
1654
                           --  Analyze rewritten pragma. Otherwise, its
1655
                           --  analysis is done too late, after the task or
1656
                           --  protected object has been created.
1657
 
1658
                           Analyze (Aitem);
1659
                        end;
1660
 
1661
                     --  For all other cases, insert in sequence
1662
 
1663
                     when others =>
1664
                        Insert_After (Ins_Node, Aitem);
1665
                        Ins_Node := Aitem;
1666
                  end case;
1667
               end if;
1668
            end if;
1669
         end;
1670
 
1671
      <<Continue>>
1672
         Next (Aspect);
1673
      end loop Aspect_Loop;
1674
   end Analyze_Aspect_Specifications;
1675
 
1676
   -----------------------
1677
   -- Analyze_At_Clause --
1678
   -----------------------
1679
 
1680
   --  An at clause is replaced by the corresponding Address attribute
1681
   --  definition clause that is the preferred approach in Ada 95.
1682
 
1683
   procedure Analyze_At_Clause (N : Node_Id) is
1684
      CS : constant Boolean := Comes_From_Source (N);
1685
 
1686
   begin
1687
      --  This is an obsolescent feature
1688
 
1689
      Check_Restriction (No_Obsolescent_Features, N);
1690
 
1691
      if Warn_On_Obsolescent_Feature then
1692
         Error_Msg_N
1693
           ("at clause is an obsolescent feature (RM J.7(2))?", N);
1694
         Error_Msg_N
1695
           ("\use address attribute definition clause instead?", N);
1696
      end if;
1697
 
1698
      --  Rewrite as address clause
1699
 
1700
      Rewrite (N,
1701
        Make_Attribute_Definition_Clause (Sloc (N),
1702
          Name  => Identifier (N),
1703
          Chars => Name_Address,
1704
          Expression => Expression (N)));
1705
 
1706
      --  We preserve Comes_From_Source, since logically the clause still
1707
      --  comes from the source program even though it is changed in form.
1708
 
1709
      Set_Comes_From_Source (N, CS);
1710
 
1711
      --  Analyze rewritten clause
1712
 
1713
      Analyze_Attribute_Definition_Clause (N);
1714
   end Analyze_At_Clause;
1715
 
1716
   -----------------------------------------
1717
   -- Analyze_Attribute_Definition_Clause --
1718
   -----------------------------------------
1719
 
1720
   procedure Analyze_Attribute_Definition_Clause (N : Node_Id) is
1721
      Loc   : constant Source_Ptr   := Sloc (N);
1722
      Nam   : constant Node_Id      := Name (N);
1723
      Attr  : constant Name_Id      := Chars (N);
1724
      Expr  : constant Node_Id      := Expression (N);
1725
      Id    : constant Attribute_Id := Get_Attribute_Id (Attr);
1726
 
1727
      Ent : Entity_Id;
1728
      --  The entity of Nam after it is analyzed. In the case of an incomplete
1729
      --  type, this is the underlying type.
1730
 
1731
      U_Ent : Entity_Id;
1732
      --  The underlying entity to which the attribute applies. Generally this
1733
      --  is the Underlying_Type of Ent, except in the case where the clause
1734
      --  applies to full view of incomplete type or private type in which case
1735
      --  U_Ent is just a copy of Ent.
1736
 
1737
      FOnly : Boolean := False;
1738
      --  Reset to True for subtype specific attribute (Alignment, Size)
1739
      --  and for stream attributes, i.e. those cases where in the call
1740
      --  to Rep_Item_Too_Late, FOnly is set True so that only the freezing
1741
      --  rules are checked. Note that the case of stream attributes is not
1742
      --  clear from the RM, but see AI95-00137. Also, the RM seems to
1743
      --  disallow Storage_Size for derived task types, but that is also
1744
      --  clearly unintentional.
1745
 
1746
      procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type);
1747
      --  Common processing for 'Read, 'Write, 'Input and 'Output attribute
1748
      --  definition clauses.
1749
 
1750
      function Duplicate_Clause return Boolean;
1751
      --  This routine checks if the aspect for U_Ent being given by attribute
1752
      --  definition clause N is for an aspect that has already been specified,
1753
      --  and if so gives an error message. If there is a duplicate, True is
1754
      --  returned, otherwise if there is no error, False is returned.
1755
 
1756
      procedure Check_Indexing_Functions;
1757
      --  Check that the function in Constant_Indexing or Variable_Indexing
1758
      --  attribute has the proper type structure. If the name is overloaded,
1759
      --  check that all interpretations are legal.
1760
 
1761
      procedure Check_Iterator_Functions;
1762
      --  Check that there is a single function in Default_Iterator attribute
1763
      --  has the proper type structure.
1764
 
1765
      function Check_Primitive_Function (Subp : Entity_Id) return Boolean;
1766
      --  Common legality check for the previous two
1767
 
1768
      -----------------------------------
1769
      -- Analyze_Stream_TSS_Definition --
1770
      -----------------------------------
1771
 
1772
      procedure Analyze_Stream_TSS_Definition (TSS_Nam : TSS_Name_Type) is
1773
         Subp : Entity_Id := Empty;
1774
         I    : Interp_Index;
1775
         It   : Interp;
1776
         Pnam : Entity_Id;
1777
 
1778
         Is_Read : constant Boolean := (TSS_Nam = TSS_Stream_Read);
1779
         --  True for Read attribute, false for other attributes
1780
 
1781
         function Has_Good_Profile (Subp : Entity_Id) return Boolean;
1782
         --  Return true if the entity is a subprogram with an appropriate
1783
         --  profile for the attribute being defined.
1784
 
1785
         ----------------------
1786
         -- Has_Good_Profile --
1787
         ----------------------
1788
 
1789
         function Has_Good_Profile (Subp : Entity_Id) return Boolean is
1790
            F              : Entity_Id;
1791
            Is_Function    : constant Boolean := (TSS_Nam = TSS_Stream_Input);
1792
            Expected_Ekind : constant array (Boolean) of Entity_Kind :=
1793
                               (False => E_Procedure, True => E_Function);
1794
            Typ            : Entity_Id;
1795
 
1796
         begin
1797
            if Ekind (Subp) /= Expected_Ekind (Is_Function) then
1798
               return False;
1799
            end if;
1800
 
1801
            F := First_Formal (Subp);
1802
 
1803
            if No (F)
1804
              or else Ekind (Etype (F)) /= E_Anonymous_Access_Type
1805
              or else Designated_Type (Etype (F)) /=
1806
                               Class_Wide_Type (RTE (RE_Root_Stream_Type))
1807
            then
1808
               return False;
1809
            end if;
1810
 
1811
            if not Is_Function then
1812
               Next_Formal (F);
1813
 
1814
               declare
1815
                  Expected_Mode : constant array (Boolean) of Entity_Kind :=
1816
                                    (False => E_In_Parameter,
1817
                                     True  => E_Out_Parameter);
1818
               begin
1819
                  if Parameter_Mode (F) /= Expected_Mode (Is_Read) then
1820
                     return False;
1821
                  end if;
1822
               end;
1823
 
1824
               Typ := Etype (F);
1825
 
1826
            else
1827
               Typ := Etype (Subp);
1828
            end if;
1829
 
1830
            return Base_Type (Typ) = Base_Type (Ent)
1831
              and then No (Next_Formal (F));
1832
         end Has_Good_Profile;
1833
 
1834
      --  Start of processing for Analyze_Stream_TSS_Definition
1835
 
1836
      begin
1837
         FOnly := True;
1838
 
1839
         if not Is_Type (U_Ent) then
1840
            Error_Msg_N ("local name must be a subtype", Nam);
1841
            return;
1842
         end if;
1843
 
1844
         Pnam := TSS (Base_Type (U_Ent), TSS_Nam);
1845
 
1846
         --  If Pnam is present, it can be either inherited from an ancestor
1847
         --  type (in which case it is legal to redefine it for this type), or
1848
         --  be a previous definition of the attribute for the same type (in
1849
         --  which case it is illegal).
1850
 
1851
         --  In the first case, it will have been analyzed already, and we
1852
         --  can check that its profile does not match the expected profile
1853
         --  for a stream attribute of U_Ent. In the second case, either Pnam
1854
         --  has been analyzed (and has the expected profile), or it has not
1855
         --  been analyzed yet (case of a type that has not been frozen yet
1856
         --  and for which the stream attribute has been set using Set_TSS).
1857
 
1858
         if Present (Pnam)
1859
           and then (No (First_Entity (Pnam)) or else Has_Good_Profile (Pnam))
1860
         then
1861
            Error_Msg_Sloc := Sloc (Pnam);
1862
            Error_Msg_Name_1 := Attr;
1863
            Error_Msg_N ("% attribute already defined #", Nam);
1864
            return;
1865
         end if;
1866
 
1867
         Analyze (Expr);
1868
 
1869
         if Is_Entity_Name (Expr) then
1870
            if not Is_Overloaded (Expr) then
1871
               if Has_Good_Profile (Entity (Expr)) then
1872
                  Subp := Entity (Expr);
1873
               end if;
1874
 
1875
            else
1876
               Get_First_Interp (Expr, I, It);
1877
               while Present (It.Nam) loop
1878
                  if Has_Good_Profile (It.Nam) then
1879
                     Subp := It.Nam;
1880
                     exit;
1881
                  end if;
1882
 
1883
                  Get_Next_Interp (I, It);
1884
               end loop;
1885
            end if;
1886
         end if;
1887
 
1888
         if Present (Subp) then
1889
            if Is_Abstract_Subprogram (Subp) then
1890
               Error_Msg_N ("stream subprogram must not be abstract", Expr);
1891
               return;
1892
            end if;
1893
 
1894
            Set_Entity (Expr, Subp);
1895
            Set_Etype (Expr, Etype (Subp));
1896
 
1897
            New_Stream_Subprogram (N, U_Ent, Subp, TSS_Nam);
1898
 
1899
         else
1900
            Error_Msg_Name_1 := Attr;
1901
            Error_Msg_N ("incorrect expression for% attribute", Expr);
1902
         end if;
1903
      end Analyze_Stream_TSS_Definition;
1904
 
1905
      ------------------------------
1906
      -- Check_Indexing_Functions --
1907
      ------------------------------
1908
 
1909
      procedure Check_Indexing_Functions is
1910
 
1911
         procedure Check_One_Function (Subp : Entity_Id);
1912
         --  Check one possible interpretation
1913
 
1914
         ------------------------
1915
         -- Check_One_Function --
1916
         ------------------------
1917
 
1918
         procedure Check_One_Function (Subp : Entity_Id) is
1919
            Default_Element : constant Node_Id :=
1920
                                Find_Aspect
1921
                                  (Etype (First_Formal (Subp)),
1922
                                   Aspect_Iterator_Element);
1923
 
1924
         begin
1925
            if not Check_Primitive_Function (Subp) then
1926
               Error_Msg_NE
1927
                 ("aspect Indexing requires a function that applies to type&",
1928
                   Subp, Ent);
1929
            end if;
1930
 
1931
            --  An indexing function must return either the default element of
1932
            --  the container, or a reference type.
1933
 
1934
            if Present (Default_Element) then
1935
               Analyze (Default_Element);
1936
               if Is_Entity_Name (Default_Element)
1937
                 and then Covers (Entity (Default_Element), Etype (Subp))
1938
               then
1939
                  return;
1940
               end if;
1941
            end if;
1942
 
1943
            --  Otherwise the return type must be a reference type.
1944
 
1945
            if not Has_Implicit_Dereference (Etype (Subp)) then
1946
               Error_Msg_N
1947
                 ("function for indexing must return a reference type", Subp);
1948
            end if;
1949
         end Check_One_Function;
1950
 
1951
      --  Start of processing for Check_Indexing_Functions
1952
 
1953
      begin
1954
         if In_Instance then
1955
            return;
1956
         end if;
1957
 
1958
         Analyze (Expr);
1959
 
1960
         if not Is_Overloaded (Expr) then
1961
            Check_One_Function (Entity (Expr));
1962
 
1963
         else
1964
            declare
1965
               I  : Interp_Index;
1966
               It : Interp;
1967
 
1968
            begin
1969
               Get_First_Interp (Expr, I, It);
1970
               while Present (It.Nam) loop
1971
 
1972
                  --  Note that analysis will have added the interpretation
1973
                  --  that corresponds to the dereference. We only check the
1974
                  --  subprogram itself.
1975
 
1976
                  if Is_Overloadable (It.Nam) then
1977
                     Check_One_Function (It.Nam);
1978
                  end if;
1979
 
1980
                  Get_Next_Interp (I, It);
1981
               end loop;
1982
            end;
1983
         end if;
1984
      end Check_Indexing_Functions;
1985
 
1986
      ------------------------------
1987
      -- Check_Iterator_Functions --
1988
      ------------------------------
1989
 
1990
      procedure Check_Iterator_Functions is
1991
         Default : Entity_Id;
1992
 
1993
         function Valid_Default_Iterator (Subp : Entity_Id) return Boolean;
1994
         --  Check one possible interpretation for validity
1995
 
1996
         ----------------------------
1997
         -- Valid_Default_Iterator --
1998
         ----------------------------
1999
 
2000
         function Valid_Default_Iterator (Subp : Entity_Id) return Boolean is
2001
            Formal : Entity_Id;
2002
 
2003
         begin
2004
            if not Check_Primitive_Function (Subp) then
2005
               return False;
2006
            else
2007
               Formal := First_Formal (Subp);
2008
            end if;
2009
 
2010
            --  False if any subsequent formal has no default expression
2011
 
2012
            Formal := Next_Formal (Formal);
2013
            while Present (Formal) loop
2014
               if No (Expression (Parent (Formal))) then
2015
                  return False;
2016
               end if;
2017
 
2018
               Next_Formal (Formal);
2019
            end loop;
2020
 
2021
            --  True if all subsequent formals have default expressions
2022
 
2023
            return True;
2024
         end Valid_Default_Iterator;
2025
 
2026
      --  Start of processing for Check_Iterator_Functions
2027
 
2028
      begin
2029
         Analyze (Expr);
2030
 
2031
         if not Is_Entity_Name (Expr) then
2032
            Error_Msg_N ("aspect Iterator must be a function name", Expr);
2033
         end if;
2034
 
2035
         if not Is_Overloaded (Expr) then
2036
            if not Check_Primitive_Function (Entity (Expr)) then
2037
               Error_Msg_NE
2038
                 ("aspect Indexing requires a function that applies to type&",
2039
                   Entity (Expr), Ent);
2040
            end if;
2041
 
2042
            if not Valid_Default_Iterator (Entity (Expr)) then
2043
               Error_Msg_N ("improper function for default iterator", Expr);
2044
            end if;
2045
 
2046
         else
2047
            Default := Empty;
2048
            declare
2049
               I : Interp_Index;
2050
               It : Interp;
2051
 
2052
            begin
2053
               Get_First_Interp (Expr, I, It);
2054
               while Present (It.Nam) loop
2055
                  if not Check_Primitive_Function (It.Nam)
2056
                    or else not Valid_Default_Iterator (It.Nam)
2057
                  then
2058
                     Remove_Interp (I);
2059
 
2060
                  elsif Present (Default) then
2061
                     Error_Msg_N ("default iterator must be unique", Expr);
2062
 
2063
                  else
2064
                     Default := It.Nam;
2065
                  end if;
2066
 
2067
                  Get_Next_Interp (I, It);
2068
               end loop;
2069
            end;
2070
 
2071
            if Present (Default) then
2072
               Set_Entity (Expr, Default);
2073
               Set_Is_Overloaded (Expr, False);
2074
            end if;
2075
         end if;
2076
      end Check_Iterator_Functions;
2077
 
2078
      -------------------------------
2079
      -- Check_Primitive_Function  --
2080
      -------------------------------
2081
 
2082
      function Check_Primitive_Function (Subp : Entity_Id) return Boolean is
2083
         Ctrl : Entity_Id;
2084
 
2085
      begin
2086
         if Ekind (Subp) /= E_Function then
2087
            return False;
2088
         end if;
2089
 
2090
         if No (First_Formal (Subp)) then
2091
            return False;
2092
         else
2093
            Ctrl := Etype (First_Formal (Subp));
2094
         end if;
2095
 
2096
         if Ctrl = Ent
2097
           or else Ctrl = Class_Wide_Type (Ent)
2098
           or else
2099
             (Ekind (Ctrl) = E_Anonymous_Access_Type
2100
               and then
2101
                 (Designated_Type (Ctrl) = Ent
2102
                   or else Designated_Type (Ctrl) = Class_Wide_Type (Ent)))
2103
         then
2104
            null;
2105
 
2106
         else
2107
            return False;
2108
         end if;
2109
 
2110
         return True;
2111
      end Check_Primitive_Function;
2112
 
2113
      ----------------------
2114
      -- Duplicate_Clause --
2115
      ----------------------
2116
 
2117
      function Duplicate_Clause return Boolean is
2118
         A : Node_Id;
2119
 
2120
      begin
2121
         --  Nothing to do if this attribute definition clause comes from
2122
         --  an aspect specification, since we could not be duplicating an
2123
         --  explicit clause, and we dealt with the case of duplicated aspects
2124
         --  in Analyze_Aspect_Specifications.
2125
 
2126
         if From_Aspect_Specification (N) then
2127
            return False;
2128
         end if;
2129
 
2130
         --  Otherwise current clause may duplicate previous clause or a
2131
         --  previously given aspect specification for the same aspect.
2132
 
2133
         A := Get_Rep_Item_For_Entity (U_Ent, Chars (N));
2134
 
2135
         if Present (A) then
2136
            if Entity (A) = U_Ent then
2137
               Error_Msg_Name_1 := Chars (N);
2138
               Error_Msg_Sloc := Sloc (A);
2139
               Error_Msg_NE ("aspect% for & previously given#", N, U_Ent);
2140
               return True;
2141
            end if;
2142
         end if;
2143
 
2144
         return False;
2145
      end Duplicate_Clause;
2146
 
2147
   --  Start of processing for Analyze_Attribute_Definition_Clause
2148
 
2149
   begin
2150
      --  The following code is a defense against recursion. Not clear that
2151
      --  this can happen legitimately, but perhaps some error situations
2152
      --  can cause it, and we did see this recursion during testing.
2153
 
2154
      if Analyzed (N) then
2155
         return;
2156
      else
2157
         Set_Analyzed (N, True);
2158
      end if;
2159
 
2160
      --  Ignore some selected attributes in CodePeer mode since they are not
2161
      --  relevant in this context.
2162
 
2163
      if CodePeer_Mode then
2164
         case Id is
2165
 
2166
            --  Ignore Component_Size in CodePeer mode, to avoid changing the
2167
            --  internal representation of types by implicitly packing them.
2168
 
2169
            when Attribute_Component_Size =>
2170
               Rewrite (N, Make_Null_Statement (Sloc (N)));
2171
               return;
2172
 
2173
            when others =>
2174
               null;
2175
         end case;
2176
      end if;
2177
 
2178
      --  Process Ignore_Rep_Clauses option
2179
 
2180
      if Ignore_Rep_Clauses then
2181
         case Id is
2182
 
2183
            --  The following should be ignored. They do not affect legality
2184
            --  and may be target dependent. The basic idea of -gnatI is to
2185
            --  ignore any rep clauses that may be target dependent but do not
2186
            --  affect legality (except possibly to be rejected because they
2187
            --  are incompatible with the compilation target).
2188
 
2189
            when Attribute_Alignment      |
2190
                 Attribute_Bit_Order      |
2191
                 Attribute_Component_Size |
2192
                 Attribute_Machine_Radix  |
2193
                 Attribute_Object_Size    |
2194
                 Attribute_Size           |
2195
                 Attribute_Stream_Size    |
2196
                 Attribute_Value_Size     =>
2197
               Rewrite (N, Make_Null_Statement (Sloc (N)));
2198
               return;
2199
 
2200
            --  Perhaps 'Small should not be ignored by Ignore_Rep_Clauses ???
2201
 
2202
            when Attribute_Small =>
2203
               if Ignore_Rep_Clauses then
2204
                  Rewrite (N, Make_Null_Statement (Sloc (N)));
2205
                  return;
2206
               end if;
2207
 
2208
            --  The following should not be ignored, because in the first place
2209
            --  they are reasonably portable, and should not cause problems in
2210
            --  compiling code from another target, and also they do affect
2211
            --  legality, e.g. failing to provide a stream attribute for a
2212
            --  type may make a program illegal.
2213
 
2214
            when Attribute_External_Tag        |
2215
                 Attribute_Input               |
2216
                 Attribute_Output              |
2217
                 Attribute_Read                |
2218
                 Attribute_Simple_Storage_Pool |
2219
                 Attribute_Storage_Pool        |
2220
                 Attribute_Storage_Size        |
2221
                 Attribute_Write               =>
2222
               null;
2223
 
2224
            --  Other cases are errors ("attribute& cannot be set with
2225
            --  definition clause"), which will be caught below.
2226
 
2227
            when others =>
2228
               null;
2229
         end case;
2230
      end if;
2231
 
2232
      Analyze (Nam);
2233
      Ent := Entity (Nam);
2234
 
2235
      if Rep_Item_Too_Early (Ent, N) then
2236
         return;
2237
      end if;
2238
 
2239
      --  Rep clause applies to full view of incomplete type or private type if
2240
      --  we have one (if not, this is a premature use of the type). However,
2241
      --  certain semantic checks need to be done on the specified entity (i.e.
2242
      --  the private view), so we save it in Ent.
2243
 
2244
      if Is_Private_Type (Ent)
2245
        and then Is_Derived_Type (Ent)
2246
        and then not Is_Tagged_Type (Ent)
2247
        and then No (Full_View (Ent))
2248
      then
2249
         --  If this is a private type whose completion is a derivation from
2250
         --  another private type, there is no full view, and the attribute
2251
         --  belongs to the type itself, not its underlying parent.
2252
 
2253
         U_Ent := Ent;
2254
 
2255
      elsif Ekind (Ent) = E_Incomplete_Type then
2256
 
2257
         --  The attribute applies to the full view, set the entity of the
2258
         --  attribute definition accordingly.
2259
 
2260
         Ent := Underlying_Type (Ent);
2261
         U_Ent := Ent;
2262
         Set_Entity (Nam, Ent);
2263
 
2264
      else
2265
         U_Ent := Underlying_Type (Ent);
2266
      end if;
2267
 
2268
      --  Avoid cascaded error
2269
 
2270
      if Etype (Nam) = Any_Type then
2271
         return;
2272
 
2273
      --  Must be declared in current scope
2274
 
2275
      elsif Scope (Ent) /= Current_Scope then
2276
         Error_Msg_N ("entity must be declared in this scope", Nam);
2277
         return;
2278
 
2279
      --  Must not be a source renaming (we do have some cases where the
2280
      --  expander generates a renaming, and those cases are OK, in such
2281
      --  cases any attribute applies to the renamed object as well).
2282
 
2283
      elsif Is_Object (Ent)
2284
        and then Present (Renamed_Object (Ent))
2285
      then
2286
         --  Case of renamed object from source, this is an error
2287
 
2288
         if Comes_From_Source (Renamed_Object (Ent)) then
2289
            Get_Name_String (Chars (N));
2290
            Error_Msg_Strlen := Name_Len;
2291
            Error_Msg_String (1 .. Name_Len) := Name_Buffer (1 .. Name_Len);
2292
            Error_Msg_N
2293
              ("~ clause not allowed for a renaming declaration "
2294
               & "(RM 13.1(6))", Nam);
2295
            return;
2296
 
2297
         --  For the case of a compiler generated renaming, the attribute
2298
         --  definition clause applies to the renamed object created by the
2299
         --  expander. The easiest general way to handle this is to create a
2300
         --  copy of the attribute definition clause for this object.
2301
 
2302
         else
2303
            Insert_Action (N,
2304
              Make_Attribute_Definition_Clause (Loc,
2305
                Name       =>
2306
                  New_Occurrence_Of (Entity (Renamed_Object (Ent)), Loc),
2307
                Chars      => Chars (N),
2308
                Expression => Duplicate_Subexpr (Expression (N))));
2309
         end if;
2310
 
2311
      --  If no underlying entity, use entity itself, applies to some
2312
      --  previously detected error cases ???
2313
 
2314
      elsif No (U_Ent) then
2315
         U_Ent := Ent;
2316
 
2317
      --  Cannot specify for a subtype (exception Object/Value_Size)
2318
 
2319
      elsif Is_Type (U_Ent)
2320
        and then not Is_First_Subtype (U_Ent)
2321
        and then Id /= Attribute_Object_Size
2322
        and then Id /= Attribute_Value_Size
2323
        and then not From_At_Mod (N)
2324
      then
2325
         Error_Msg_N ("cannot specify attribute for subtype", Nam);
2326
         return;
2327
      end if;
2328
 
2329
      Set_Entity (N, U_Ent);
2330
 
2331
      --  Switch on particular attribute
2332
 
2333
      case Id is
2334
 
2335
         -------------
2336
         -- Address --
2337
         -------------
2338
 
2339
         --  Address attribute definition clause
2340
 
2341
         when Attribute_Address => Address : begin
2342
 
2343
            --  A little error check, catch for X'Address use X'Address;
2344
 
2345
            if Nkind (Nam) = N_Identifier
2346
              and then Nkind (Expr) = N_Attribute_Reference
2347
              and then Attribute_Name (Expr) = Name_Address
2348
              and then Nkind (Prefix (Expr)) = N_Identifier
2349
              and then Chars (Nam) = Chars (Prefix (Expr))
2350
            then
2351
               Error_Msg_NE
2352
                 ("address for & is self-referencing", Prefix (Expr), Ent);
2353
               return;
2354
            end if;
2355
 
2356
            --  Not that special case, carry on with analysis of expression
2357
 
2358
            Analyze_And_Resolve (Expr, RTE (RE_Address));
2359
 
2360
            --  Even when ignoring rep clauses we need to indicate that the
2361
            --  entity has an address clause and thus it is legal to declare
2362
            --  it imported.
2363
 
2364
            if Ignore_Rep_Clauses then
2365
               if Ekind_In (U_Ent, E_Variable, E_Constant) then
2366
                  Record_Rep_Item (U_Ent, N);
2367
               end if;
2368
 
2369
               return;
2370
            end if;
2371
 
2372
            if Duplicate_Clause then
2373
               null;
2374
 
2375
            --  Case of address clause for subprogram
2376
 
2377
            elsif Is_Subprogram (U_Ent) then
2378
               if Has_Homonym (U_Ent) then
2379
                  Error_Msg_N
2380
                    ("address clause cannot be given " &
2381
                     "for overloaded subprogram",
2382
                     Nam);
2383
                  return;
2384
               end if;
2385
 
2386
               --  For subprograms, all address clauses are permitted, and we
2387
               --  mark the subprogram as having a deferred freeze so that Gigi
2388
               --  will not elaborate it too soon.
2389
 
2390
               --  Above needs more comments, what is too soon about???
2391
 
2392
               Set_Has_Delayed_Freeze (U_Ent);
2393
 
2394
            --  Case of address clause for entry
2395
 
2396
            elsif Ekind (U_Ent) = E_Entry then
2397
               if Nkind (Parent (N)) = N_Task_Body then
2398
                  Error_Msg_N
2399
                    ("entry address must be specified in task spec", Nam);
2400
                  return;
2401
               end if;
2402
 
2403
               --  For entries, we require a constant address
2404
 
2405
               Check_Constant_Address_Clause (Expr, U_Ent);
2406
 
2407
               --  Special checks for task types
2408
 
2409
               if Is_Task_Type (Scope (U_Ent))
2410
                 and then Comes_From_Source (Scope (U_Ent))
2411
               then
2412
                  Error_Msg_N
2413
                    ("?entry address declared for entry in task type", N);
2414
                  Error_Msg_N
2415
                    ("\?only one task can be declared of this type", N);
2416
               end if;
2417
 
2418
               --  Entry address clauses are obsolescent
2419
 
2420
               Check_Restriction (No_Obsolescent_Features, N);
2421
 
2422
               if Warn_On_Obsolescent_Feature then
2423
                  Error_Msg_N
2424
                    ("attaching interrupt to task entry is an " &
2425
                     "obsolescent feature (RM J.7.1)?", N);
2426
                  Error_Msg_N
2427
                    ("\use interrupt procedure instead?", N);
2428
               end if;
2429
 
2430
            --  Case of an address clause for a controlled object which we
2431
            --  consider to be erroneous.
2432
 
2433
            elsif Is_Controlled (Etype (U_Ent))
2434
              or else Has_Controlled_Component (Etype (U_Ent))
2435
            then
2436
               Error_Msg_NE
2437
                 ("?controlled object& must not be overlaid", Nam, U_Ent);
2438
               Error_Msg_N
2439
                 ("\?Program_Error will be raised at run time", Nam);
2440
               Insert_Action (Declaration_Node (U_Ent),
2441
                 Make_Raise_Program_Error (Loc,
2442
                   Reason => PE_Overlaid_Controlled_Object));
2443
               return;
2444
 
2445
            --  Case of address clause for a (non-controlled) object
2446
 
2447
            elsif
2448
              Ekind (U_Ent) = E_Variable
2449
                or else
2450
              Ekind (U_Ent) = E_Constant
2451
            then
2452
               declare
2453
                  Expr  : constant Node_Id := Expression (N);
2454
                  O_Ent : Entity_Id;
2455
                  Off   : Boolean;
2456
 
2457
               begin
2458
                  --  Exported variables cannot have an address clause, because
2459
                  --  this cancels the effect of the pragma Export.
2460
 
2461
                  if Is_Exported (U_Ent) then
2462
                     Error_Msg_N
2463
                       ("cannot export object with address clause", Nam);
2464
                     return;
2465
                  end if;
2466
 
2467
                  Find_Overlaid_Entity (N, O_Ent, Off);
2468
 
2469
                  --  Overlaying controlled objects is erroneous
2470
 
2471
                  if Present (O_Ent)
2472
                    and then (Has_Controlled_Component (Etype (O_Ent))
2473
                                or else Is_Controlled (Etype (O_Ent)))
2474
                  then
2475
                     Error_Msg_N
2476
                       ("?cannot overlay with controlled object", Expr);
2477
                     Error_Msg_N
2478
                       ("\?Program_Error will be raised at run time", Expr);
2479
                     Insert_Action (Declaration_Node (U_Ent),
2480
                       Make_Raise_Program_Error (Loc,
2481
                         Reason => PE_Overlaid_Controlled_Object));
2482
                     return;
2483
 
2484
                  elsif Present (O_Ent)
2485
                    and then Ekind (U_Ent) = E_Constant
2486
                    and then not Is_Constant_Object (O_Ent)
2487
                  then
2488
                     Error_Msg_N ("constant overlays a variable?", Expr);
2489
 
2490
                  --  Imported variables can have an address clause, but then
2491
                  --  the import is pretty meaningless except to suppress
2492
                  --  initializations, so we do not need such variables to
2493
                  --  be statically allocated (and in fact it causes trouble
2494
                  --  if the address clause is a local value).
2495
 
2496
                  elsif Is_Imported (U_Ent) then
2497
                     Set_Is_Statically_Allocated (U_Ent, False);
2498
                  end if;
2499
 
2500
                  --  We mark a possible modification of a variable with an
2501
                  --  address clause, since it is likely aliasing is occurring.
2502
 
2503
                  Note_Possible_Modification (Nam, Sure => False);
2504
 
2505
                  --  Here we are checking for explicit overlap of one variable
2506
                  --  by another, and if we find this then mark the overlapped
2507
                  --  variable as also being volatile to prevent unwanted
2508
                  --  optimizations. This is a significant pessimization so
2509
                  --  avoid it when there is an offset, i.e. when the object
2510
                  --  is composite; they cannot be optimized easily anyway.
2511
 
2512
                  if Present (O_Ent)
2513
                    and then Is_Object (O_Ent)
2514
                    and then not Off
2515
                  then
2516
                     Set_Treat_As_Volatile (O_Ent);
2517
                  end if;
2518
 
2519
                  --  Legality checks on the address clause for initialized
2520
                  --  objects is deferred until the freeze point, because
2521
                  --  a subsequent pragma might indicate that the object is
2522
                  --  imported and thus not initialized.
2523
 
2524
                  Set_Has_Delayed_Freeze (U_Ent);
2525
 
2526
                  --  If an initialization call has been generated for this
2527
                  --  object, it needs to be deferred to after the freeze node
2528
                  --  we have just now added, otherwise GIGI will see a
2529
                  --  reference to the variable (as actual to the IP call)
2530
                  --  before its definition.
2531
 
2532
                  declare
2533
                     Init_Call : constant Node_Id := Find_Init_Call (U_Ent, N);
2534
                  begin
2535
                     if Present (Init_Call) then
2536
                        Remove (Init_Call);
2537
                        Append_Freeze_Action (U_Ent, Init_Call);
2538
                     end if;
2539
                  end;
2540
 
2541
                  if Is_Exported (U_Ent) then
2542
                     Error_Msg_N
2543
                       ("& cannot be exported if an address clause is given",
2544
                        Nam);
2545
                     Error_Msg_N
2546
                       ("\define and export a variable " &
2547
                        "that holds its address instead",
2548
                        Nam);
2549
                  end if;
2550
 
2551
                  --  Entity has delayed freeze, so we will generate an
2552
                  --  alignment check at the freeze point unless suppressed.
2553
 
2554
                  if not Range_Checks_Suppressed (U_Ent)
2555
                    and then not Alignment_Checks_Suppressed (U_Ent)
2556
                  then
2557
                     Set_Check_Address_Alignment (N);
2558
                  end if;
2559
 
2560
                  --  Kill the size check code, since we are not allocating
2561
                  --  the variable, it is somewhere else.
2562
 
2563
                  Kill_Size_Check_Code (U_Ent);
2564
 
2565
                  --  If the address clause is of the form:
2566
 
2567
                  --    for Y'Address use X'Address
2568
 
2569
                  --  or
2570
 
2571
                  --    Const : constant Address := X'Address;
2572
                  --    ...
2573
                  --    for Y'Address use Const;
2574
 
2575
                  --  then we make an entry in the table for checking the size
2576
                  --  and alignment of the overlaying variable. We defer this
2577
                  --  check till after code generation to take full advantage
2578
                  --  of the annotation done by the back end. This entry is
2579
                  --  only made if the address clause comes from source.
2580
 
2581
                  --  If the entity has a generic type, the check will be
2582
                  --  performed in the instance if the actual type justifies
2583
                  --  it, and we do not insert the clause in the table to
2584
                  --  prevent spurious warnings.
2585
 
2586
                  if Address_Clause_Overlay_Warnings
2587
                    and then Comes_From_Source (N)
2588
                    and then Present (O_Ent)
2589
                    and then Is_Object (O_Ent)
2590
                  then
2591
                     if not Is_Generic_Type (Etype (U_Ent)) then
2592
                        Address_Clause_Checks.Append ((N, U_Ent, O_Ent, Off));
2593
                     end if;
2594
 
2595
                     --  If variable overlays a constant view, and we are
2596
                     --  warning on overlays, then mark the variable as
2597
                     --  overlaying a constant (we will give warnings later
2598
                     --  if this variable is assigned).
2599
 
2600
                     if Is_Constant_Object (O_Ent)
2601
                       and then Ekind (U_Ent) = E_Variable
2602
                     then
2603
                        Set_Overlays_Constant (U_Ent);
2604
                     end if;
2605
                  end if;
2606
               end;
2607
 
2608
            --  Not a valid entity for an address clause
2609
 
2610
            else
2611
               Error_Msg_N ("address cannot be given for &", Nam);
2612
            end if;
2613
         end Address;
2614
 
2615
         ---------------
2616
         -- Alignment --
2617
         ---------------
2618
 
2619
         --  Alignment attribute definition clause
2620
 
2621
         when Attribute_Alignment => Alignment : declare
2622
            Align     : constant Uint := Get_Alignment_Value (Expr);
2623
            Max_Align : constant Uint := UI_From_Int (Maximum_Alignment);
2624
 
2625
         begin
2626
            FOnly := True;
2627
 
2628
            if not Is_Type (U_Ent)
2629
              and then Ekind (U_Ent) /= E_Variable
2630
              and then Ekind (U_Ent) /= E_Constant
2631
            then
2632
               Error_Msg_N ("alignment cannot be given for &", Nam);
2633
 
2634
            elsif Duplicate_Clause then
2635
               null;
2636
 
2637
            elsif Align /= No_Uint then
2638
               Set_Has_Alignment_Clause (U_Ent);
2639
 
2640
               --  Tagged type case, check for attempt to set alignment to a
2641
               --  value greater than Max_Align, and reset if so.
2642
 
2643
               if Is_Tagged_Type (U_Ent) and then Align > Max_Align then
2644
                  Error_Msg_N
2645
                    ("?alignment for & set to Maximum_Aligment", Nam);
2646
                     Set_Alignment (U_Ent, Max_Align);
2647
 
2648
               --  All other cases
2649
 
2650
               else
2651
                  Set_Alignment (U_Ent, Align);
2652
               end if;
2653
 
2654
               --  For an array type, U_Ent is the first subtype. In that case,
2655
               --  also set the alignment of the anonymous base type so that
2656
               --  other subtypes (such as the itypes for aggregates of the
2657
               --  type) also receive the expected alignment.
2658
 
2659
               if Is_Array_Type (U_Ent) then
2660
                  Set_Alignment (Base_Type (U_Ent), Align);
2661
               end if;
2662
            end if;
2663
         end Alignment;
2664
 
2665
         ---------------
2666
         -- Bit_Order --
2667
         ---------------
2668
 
2669
         --  Bit_Order attribute definition clause
2670
 
2671
         when Attribute_Bit_Order => Bit_Order : declare
2672
         begin
2673
            if not Is_Record_Type (U_Ent) then
2674
               Error_Msg_N
2675
                 ("Bit_Order can only be defined for record type", Nam);
2676
 
2677
            elsif Duplicate_Clause then
2678
               null;
2679
 
2680
            else
2681
               Analyze_And_Resolve (Expr, RTE (RE_Bit_Order));
2682
 
2683
               if Etype (Expr) = Any_Type then
2684
                  return;
2685
 
2686
               elsif not Is_Static_Expression (Expr) then
2687
                  Flag_Non_Static_Expr
2688
                    ("Bit_Order requires static expression!", Expr);
2689
 
2690
               else
2691
                  if (Expr_Value (Expr) = 0) /= Bytes_Big_Endian then
2692
                     Set_Reverse_Bit_Order (U_Ent, True);
2693
                  end if;
2694
               end if;
2695
            end if;
2696
         end Bit_Order;
2697
 
2698
         --------------------
2699
         -- Component_Size --
2700
         --------------------
2701
 
2702
         --  Component_Size attribute definition clause
2703
 
2704
         when Attribute_Component_Size => Component_Size_Case : declare
2705
            Csize    : constant Uint := Static_Integer (Expr);
2706
            Ctyp     : Entity_Id;
2707
            Btype    : Entity_Id;
2708
            Biased   : Boolean;
2709
            New_Ctyp : Entity_Id;
2710
            Decl     : Node_Id;
2711
 
2712
         begin
2713
            if not Is_Array_Type (U_Ent) then
2714
               Error_Msg_N ("component size requires array type", Nam);
2715
               return;
2716
            end if;
2717
 
2718
            Btype := Base_Type (U_Ent);
2719
            Ctyp := Component_Type (Btype);
2720
 
2721
            if Duplicate_Clause then
2722
               null;
2723
 
2724
            elsif Rep_Item_Too_Early (Btype, N) then
2725
               null;
2726
 
2727
            elsif Csize /= No_Uint then
2728
               Check_Size (Expr, Ctyp, Csize, Biased);
2729
 
2730
               --  For the biased case, build a declaration for a subtype that
2731
               --  will be used to represent the biased subtype that reflects
2732
               --  the biased representation of components. We need the subtype
2733
               --  to get proper conversions on referencing elements of the
2734
               --  array. Note: component size clauses are ignored in VM mode.
2735
 
2736
               if VM_Target = No_VM then
2737
                  if Biased then
2738
                     New_Ctyp :=
2739
                       Make_Defining_Identifier (Loc,
2740
                         Chars =>
2741
                           New_External_Name (Chars (U_Ent), 'C', 0, 'T'));
2742
 
2743
                     Decl :=
2744
                       Make_Subtype_Declaration (Loc,
2745
                         Defining_Identifier => New_Ctyp,
2746
                         Subtype_Indication  =>
2747
                           New_Occurrence_Of (Component_Type (Btype), Loc));
2748
 
2749
                     Set_Parent (Decl, N);
2750
                     Analyze (Decl, Suppress => All_Checks);
2751
 
2752
                     Set_Has_Delayed_Freeze        (New_Ctyp, False);
2753
                     Set_Esize                     (New_Ctyp, Csize);
2754
                     Set_RM_Size                   (New_Ctyp, Csize);
2755
                     Init_Alignment                (New_Ctyp);
2756
                     Set_Is_Itype                  (New_Ctyp, True);
2757
                     Set_Associated_Node_For_Itype (New_Ctyp, U_Ent);
2758
 
2759
                     Set_Component_Type (Btype, New_Ctyp);
2760
                     Set_Biased (New_Ctyp, N, "component size clause");
2761
                  end if;
2762
 
2763
                  Set_Component_Size (Btype, Csize);
2764
 
2765
               --  For VM case, we ignore component size clauses
2766
 
2767
               else
2768
                  --  Give a warning unless we are in GNAT mode, in which case
2769
                  --  the warning is suppressed since it is not useful.
2770
 
2771
                  if not GNAT_Mode then
2772
                     Error_Msg_N
2773
                       ("?component size ignored in this configuration", N);
2774
                  end if;
2775
               end if;
2776
 
2777
               --  Deal with warning on overridden size
2778
 
2779
               if Warn_On_Overridden_Size
2780
                 and then Has_Size_Clause (Ctyp)
2781
                 and then RM_Size (Ctyp) /= Csize
2782
               then
2783
                  Error_Msg_NE
2784
                    ("?component size overrides size clause for&",
2785
                     N, Ctyp);
2786
               end if;
2787
 
2788
               Set_Has_Component_Size_Clause (Btype, True);
2789
               Set_Has_Non_Standard_Rep (Btype, True);
2790
            end if;
2791
         end Component_Size_Case;
2792
 
2793
         -----------------------
2794
         -- Constant_Indexing --
2795
         -----------------------
2796
 
2797
         when Attribute_Constant_Indexing =>
2798
            Check_Indexing_Functions;
2799
 
2800
         ----------------------
2801
         -- Default_Iterator --
2802
         ----------------------
2803
 
2804
         when Attribute_Default_Iterator =>  Default_Iterator : declare
2805
            Func : Entity_Id;
2806
 
2807
         begin
2808
            if not Is_Tagged_Type (U_Ent) then
2809
               Error_Msg_N
2810
                 ("aspect Default_Iterator applies to  tagged type", Nam);
2811
            end if;
2812
 
2813
            Check_Iterator_Functions;
2814
 
2815
            Analyze (Expr);
2816
 
2817
            if not Is_Entity_Name (Expr)
2818
              or else Ekind (Entity (Expr)) /= E_Function
2819
            then
2820
               Error_Msg_N ("aspect Iterator must be a function", Expr);
2821
            else
2822
               Func := Entity (Expr);
2823
            end if;
2824
 
2825
            if No (First_Formal (Func))
2826
              or else Etype (First_Formal (Func)) /= U_Ent
2827
            then
2828
               Error_Msg_NE
2829
                 ("Default Iterator must be a primitive of&", Func, U_Ent);
2830
            end if;
2831
         end Default_Iterator;
2832
 
2833
         ------------------
2834
         -- External_Tag --
2835
         ------------------
2836
 
2837
         when Attribute_External_Tag => External_Tag :
2838
         begin
2839
            if not Is_Tagged_Type (U_Ent) then
2840
               Error_Msg_N ("should be a tagged type", Nam);
2841
            end if;
2842
 
2843
            if Duplicate_Clause then
2844
               null;
2845
 
2846
            else
2847
               Analyze_And_Resolve (Expr, Standard_String);
2848
 
2849
               if not Is_Static_Expression (Expr) then
2850
                  Flag_Non_Static_Expr
2851
                    ("static string required for tag name!", Nam);
2852
               end if;
2853
 
2854
               if VM_Target = No_VM then
2855
                  Set_Has_External_Tag_Rep_Clause (U_Ent);
2856
               else
2857
                  Error_Msg_Name_1 := Attr;
2858
                  Error_Msg_N
2859
                    ("% attribute unsupported in this configuration", Nam);
2860
               end if;
2861
 
2862
               if not Is_Library_Level_Entity (U_Ent) then
2863
                  Error_Msg_NE
2864
                    ("?non-unique external tag supplied for &", N, U_Ent);
2865
                  Error_Msg_N
2866
                    ("?\same external tag applies to all subprogram calls", N);
2867
                  Error_Msg_N
2868
                    ("?\corresponding internal tag cannot be obtained", N);
2869
               end if;
2870
            end if;
2871
         end External_Tag;
2872
 
2873
         --------------------------
2874
         -- Implicit_Dereference --
2875
         --------------------------
2876
 
2877
         when Attribute_Implicit_Dereference =>
2878
 
2879
            --  Legality checks already performed at the point of
2880
            --  the type declaration, aspect is not delayed.
2881
 
2882
            null;
2883
 
2884
         -----------
2885
         -- Input --
2886
         -----------
2887
 
2888
         when Attribute_Input =>
2889
            Analyze_Stream_TSS_Definition (TSS_Stream_Input);
2890
            Set_Has_Specified_Stream_Input (Ent);
2891
 
2892
         ----------------------
2893
         -- Iterator_Element --
2894
         ----------------------
2895
 
2896
         when Attribute_Iterator_Element =>
2897
            Analyze (Expr);
2898
 
2899
            if not Is_Entity_Name (Expr)
2900
              or else not Is_Type (Entity (Expr))
2901
            then
2902
               Error_Msg_N ("aspect Iterator_Element must be a type", Expr);
2903
            end if;
2904
 
2905
         -------------------
2906
         -- Machine_Radix --
2907
         -------------------
2908
 
2909
         --  Machine radix attribute definition clause
2910
 
2911
         when Attribute_Machine_Radix => Machine_Radix : declare
2912
            Radix : constant Uint := Static_Integer (Expr);
2913
 
2914
         begin
2915
            if not Is_Decimal_Fixed_Point_Type (U_Ent) then
2916
               Error_Msg_N ("decimal fixed-point type expected for &", Nam);
2917
 
2918
            elsif Duplicate_Clause then
2919
               null;
2920
 
2921
            elsif Radix /= No_Uint then
2922
               Set_Has_Machine_Radix_Clause (U_Ent);
2923
               Set_Has_Non_Standard_Rep (Base_Type (U_Ent));
2924
 
2925
               if Radix = 2 then
2926
                  null;
2927
               elsif Radix = 10 then
2928
                  Set_Machine_Radix_10 (U_Ent);
2929
               else
2930
                  Error_Msg_N ("machine radix value must be 2 or 10", Expr);
2931
               end if;
2932
            end if;
2933
         end Machine_Radix;
2934
 
2935
         -----------------
2936
         -- Object_Size --
2937
         -----------------
2938
 
2939
         --  Object_Size attribute definition clause
2940
 
2941
         when Attribute_Object_Size => Object_Size : declare
2942
            Size : constant Uint := Static_Integer (Expr);
2943
 
2944
            Biased : Boolean;
2945
            pragma Warnings (Off, Biased);
2946
 
2947
         begin
2948
            if not Is_Type (U_Ent) then
2949
               Error_Msg_N ("Object_Size cannot be given for &", Nam);
2950
 
2951
            elsif Duplicate_Clause then
2952
               null;
2953
 
2954
            else
2955
               Check_Size (Expr, U_Ent, Size, Biased);
2956
 
2957
               if Size /= 8
2958
                    and then
2959
                  Size /= 16
2960
                    and then
2961
                  Size /= 32
2962
                    and then
2963
                  UI_Mod (Size, 64) /= 0
2964
               then
2965
                  Error_Msg_N
2966
                    ("Object_Size must be 8, 16, 32, or multiple of 64",
2967
                     Expr);
2968
               end if;
2969
 
2970
               Set_Esize (U_Ent, Size);
2971
               Set_Has_Object_Size_Clause (U_Ent);
2972
               Alignment_Check_For_Size_Change (U_Ent, Size);
2973
            end if;
2974
         end Object_Size;
2975
 
2976
         ------------
2977
         -- Output --
2978
         ------------
2979
 
2980
         when Attribute_Output =>
2981
            Analyze_Stream_TSS_Definition (TSS_Stream_Output);
2982
            Set_Has_Specified_Stream_Output (Ent);
2983
 
2984
         ----------
2985
         -- Read --
2986
         ----------
2987
 
2988
         when Attribute_Read =>
2989
            Analyze_Stream_TSS_Definition (TSS_Stream_Read);
2990
            Set_Has_Specified_Stream_Read (Ent);
2991
 
2992
         ----------
2993
         -- Size --
2994
         ----------
2995
 
2996
         --  Size attribute definition clause
2997
 
2998
         when Attribute_Size => Size : declare
2999
            Size   : constant Uint := Static_Integer (Expr);
3000
            Etyp   : Entity_Id;
3001
            Biased : Boolean;
3002
 
3003
         begin
3004
            FOnly := True;
3005
 
3006
            if Duplicate_Clause then
3007
               null;
3008
 
3009
            elsif not Is_Type (U_Ent)
3010
              and then Ekind (U_Ent) /= E_Variable
3011
              and then Ekind (U_Ent) /= E_Constant
3012
            then
3013
               Error_Msg_N ("size cannot be given for &", Nam);
3014
 
3015
            elsif Is_Array_Type (U_Ent)
3016
              and then not Is_Constrained (U_Ent)
3017
            then
3018
               Error_Msg_N
3019
                 ("size cannot be given for unconstrained array", Nam);
3020
 
3021
            elsif Size /= No_Uint then
3022
               if VM_Target /= No_VM and then not GNAT_Mode then
3023
 
3024
                  --  Size clause is not handled properly on VM targets.
3025
                  --  Display a warning unless we are in GNAT mode, in which
3026
                  --  case this is useless.
3027
 
3028
                  Error_Msg_N
3029
                    ("?size clauses are ignored in this configuration", N);
3030
               end if;
3031
 
3032
               if Is_Type (U_Ent) then
3033
                  Etyp := U_Ent;
3034
               else
3035
                  Etyp := Etype (U_Ent);
3036
               end if;
3037
 
3038
               --  Check size, note that Gigi is in charge of checking that the
3039
               --  size of an array or record type is OK. Also we do not check
3040
               --  the size in the ordinary fixed-point case, since it is too
3041
               --  early to do so (there may be subsequent small clause that
3042
               --  affects the size). We can check the size if a small clause
3043
               --  has already been given.
3044
 
3045
               if not Is_Ordinary_Fixed_Point_Type (U_Ent)
3046
                 or else Has_Small_Clause (U_Ent)
3047
               then
3048
                  Check_Size (Expr, Etyp, Size, Biased);
3049
                  Set_Biased (U_Ent, N, "size clause", Biased);
3050
               end if;
3051
 
3052
               --  For types set RM_Size and Esize if possible
3053
 
3054
               if Is_Type (U_Ent) then
3055
                  Set_RM_Size (U_Ent, Size);
3056
 
3057
                  --  For elementary types, increase Object_Size to power of 2,
3058
                  --  but not less than a storage unit in any case (normally
3059
                  --  this means it will be byte addressable).
3060
 
3061
                  --  For all other types, nothing else to do, we leave Esize
3062
                  --  (object size) unset, the back end will set it from the
3063
                  --  size and alignment in an appropriate manner.
3064
 
3065
                  --  In both cases, we check whether the alignment must be
3066
                  --  reset in the wake of the size change.
3067
 
3068
                  if Is_Elementary_Type (U_Ent) then
3069
                     if Size <= System_Storage_Unit then
3070
                        Init_Esize (U_Ent, System_Storage_Unit);
3071
                     elsif Size <= 16 then
3072
                        Init_Esize (U_Ent, 16);
3073
                     elsif Size <= 32 then
3074
                        Init_Esize (U_Ent, 32);
3075
                     else
3076
                        Set_Esize  (U_Ent, (Size + 63) / 64 * 64);
3077
                     end if;
3078
 
3079
                     Alignment_Check_For_Size_Change (U_Ent, Esize (U_Ent));
3080
                  else
3081
                     Alignment_Check_For_Size_Change (U_Ent, Size);
3082
                  end if;
3083
 
3084
               --  For objects, set Esize only
3085
 
3086
               else
3087
                  if Is_Elementary_Type (Etyp) then
3088
                     if Size /= System_Storage_Unit
3089
                          and then
3090
                        Size /= System_Storage_Unit * 2
3091
                          and then
3092
                        Size /= System_Storage_Unit * 4
3093
                           and then
3094
                        Size /= System_Storage_Unit * 8
3095
                     then
3096
                        Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3097
                        Error_Msg_Uint_2 := Error_Msg_Uint_1 * 8;
3098
                        Error_Msg_N
3099
                          ("size for primitive object must be a power of 2"
3100
                            & " in the range ^-^", N);
3101
                     end if;
3102
                  end if;
3103
 
3104
                  Set_Esize (U_Ent, Size);
3105
               end if;
3106
 
3107
               Set_Has_Size_Clause (U_Ent);
3108
            end if;
3109
         end Size;
3110
 
3111
         -----------
3112
         -- Small --
3113
         -----------
3114
 
3115
         --  Small attribute definition clause
3116
 
3117
         when Attribute_Small => Small : declare
3118
            Implicit_Base : constant Entity_Id := Base_Type (U_Ent);
3119
            Small         : Ureal;
3120
 
3121
         begin
3122
            Analyze_And_Resolve (Expr, Any_Real);
3123
 
3124
            if Etype (Expr) = Any_Type then
3125
               return;
3126
 
3127
            elsif not Is_Static_Expression (Expr) then
3128
               Flag_Non_Static_Expr
3129
                 ("small requires static expression!", Expr);
3130
               return;
3131
 
3132
            else
3133
               Small := Expr_Value_R (Expr);
3134
 
3135
               if Small <= Ureal_0 then
3136
                  Error_Msg_N ("small value must be greater than zero", Expr);
3137
                  return;
3138
               end if;
3139
 
3140
            end if;
3141
 
3142
            if not Is_Ordinary_Fixed_Point_Type (U_Ent) then
3143
               Error_Msg_N
3144
                 ("small requires an ordinary fixed point type", Nam);
3145
 
3146
            elsif Has_Small_Clause (U_Ent) then
3147
               Error_Msg_N ("small already given for &", Nam);
3148
 
3149
            elsif Small > Delta_Value (U_Ent) then
3150
               Error_Msg_N
3151
                 ("small value must not be greater then delta value", Nam);
3152
 
3153
            else
3154
               Set_Small_Value (U_Ent, Small);
3155
               Set_Small_Value (Implicit_Base, Small);
3156
               Set_Has_Small_Clause (U_Ent);
3157
               Set_Has_Small_Clause (Implicit_Base);
3158
               Set_Has_Non_Standard_Rep (Implicit_Base);
3159
            end if;
3160
         end Small;
3161
 
3162
         ------------------
3163
         -- Storage_Pool --
3164
         ------------------
3165
 
3166
         --  Storage_Pool attribute definition clause
3167
 
3168
         when Attribute_Storage_Pool | Attribute_Simple_Storage_Pool => declare
3169
            Pool : Entity_Id;
3170
            T    : Entity_Id;
3171
 
3172
         begin
3173
            if Ekind (U_Ent) = E_Access_Subprogram_Type then
3174
               Error_Msg_N
3175
                 ("storage pool cannot be given for access-to-subprogram type",
3176
                  Nam);
3177
               return;
3178
 
3179
            elsif not
3180
              Ekind_In (U_Ent, E_Access_Type, E_General_Access_Type)
3181
            then
3182
               Error_Msg_N
3183
                 ("storage pool can only be given for access types", Nam);
3184
               return;
3185
 
3186
            elsif Is_Derived_Type (U_Ent) then
3187
               Error_Msg_N
3188
                 ("storage pool cannot be given for a derived access type",
3189
                  Nam);
3190
 
3191
            elsif Duplicate_Clause then
3192
               return;
3193
 
3194
            elsif Present (Associated_Storage_Pool (U_Ent)) then
3195
               Error_Msg_N ("storage pool already given for &", Nam);
3196
               return;
3197
            end if;
3198
 
3199
            if Id = Attribute_Storage_Pool then
3200
               Analyze_And_Resolve
3201
                 (Expr, Class_Wide_Type (RTE (RE_Root_Storage_Pool)));
3202
 
3203
            --  In the Simple_Storage_Pool case, we allow a variable of any
3204
            --  simple storage pool type, so we Resolve without imposing an
3205
            --  expected type.
3206
 
3207
            else
3208
               Analyze_And_Resolve (Expr);
3209
 
3210
               if not Present (Get_Rep_Pragma
3211
                                 (Etype (Expr), Name_Simple_Storage_Pool_Type))
3212
               then
3213
                  Error_Msg_N
3214
                    ("expression must be of a simple storage pool type", Expr);
3215
               end if;
3216
            end if;
3217
 
3218
            if not Denotes_Variable (Expr) then
3219
               Error_Msg_N ("storage pool must be a variable", Expr);
3220
               return;
3221
            end if;
3222
 
3223
            if Nkind (Expr) = N_Type_Conversion then
3224
               T := Etype (Expression (Expr));
3225
            else
3226
               T := Etype (Expr);
3227
            end if;
3228
 
3229
            --  The Stack_Bounded_Pool is used internally for implementing
3230
            --  access types with a Storage_Size. Since it only work properly
3231
            --  when used on one specific type, we need to check that it is not
3232
            --  hijacked improperly:
3233
 
3234
            --    type T is access Integer;
3235
            --    for T'Storage_Size use n;
3236
            --    type Q is access Float;
3237
            --    for Q'Storage_Size use T'Storage_Size; -- incorrect
3238
 
3239
            if RTE_Available (RE_Stack_Bounded_Pool)
3240
              and then Base_Type (T) = RTE (RE_Stack_Bounded_Pool)
3241
            then
3242
               Error_Msg_N ("non-shareable internal Pool", Expr);
3243
               return;
3244
            end if;
3245
 
3246
            --  If the argument is a name that is not an entity name, then
3247
            --  we construct a renaming operation to define an entity of
3248
            --  type storage pool.
3249
 
3250
            if not Is_Entity_Name (Expr)
3251
              and then Is_Object_Reference (Expr)
3252
            then
3253
               Pool := Make_Temporary (Loc, 'P', Expr);
3254
 
3255
               declare
3256
                  Rnode : constant Node_Id :=
3257
                            Make_Object_Renaming_Declaration (Loc,
3258
                              Defining_Identifier => Pool,
3259
                              Subtype_Mark        =>
3260
                                New_Occurrence_Of (Etype (Expr), Loc),
3261
                              Name                => Expr);
3262
 
3263
               begin
3264
                  Insert_Before (N, Rnode);
3265
                  Analyze (Rnode);
3266
                  Set_Associated_Storage_Pool (U_Ent, Pool);
3267
               end;
3268
 
3269
            elsif Is_Entity_Name (Expr) then
3270
               Pool := Entity (Expr);
3271
 
3272
               --  If pool is a renamed object, get original one. This can
3273
               --  happen with an explicit renaming, and within instances.
3274
 
3275
               while Present (Renamed_Object (Pool))
3276
                 and then Is_Entity_Name (Renamed_Object (Pool))
3277
               loop
3278
                  Pool := Entity (Renamed_Object (Pool));
3279
               end loop;
3280
 
3281
               if Present (Renamed_Object (Pool))
3282
                 and then Nkind (Renamed_Object (Pool)) = N_Type_Conversion
3283
                 and then Is_Entity_Name (Expression (Renamed_Object (Pool)))
3284
               then
3285
                  Pool := Entity (Expression (Renamed_Object (Pool)));
3286
               end if;
3287
 
3288
               Set_Associated_Storage_Pool (U_Ent, Pool);
3289
 
3290
            elsif Nkind (Expr) = N_Type_Conversion
3291
              and then Is_Entity_Name (Expression (Expr))
3292
              and then Nkind (Original_Node (Expr)) = N_Attribute_Reference
3293
            then
3294
               Pool := Entity (Expression (Expr));
3295
               Set_Associated_Storage_Pool (U_Ent, Pool);
3296
 
3297
            else
3298
               Error_Msg_N ("incorrect reference to a Storage Pool", Expr);
3299
               return;
3300
            end if;
3301
         end;
3302
 
3303
         ------------------
3304
         -- Storage_Size --
3305
         ------------------
3306
 
3307
         --  Storage_Size attribute definition clause
3308
 
3309
         when Attribute_Storage_Size => Storage_Size : declare
3310
            Btype : constant Entity_Id := Base_Type (U_Ent);
3311
            Sprag : Node_Id;
3312
 
3313
         begin
3314
            if Is_Task_Type (U_Ent) then
3315
               Check_Restriction (No_Obsolescent_Features, N);
3316
 
3317
               if Warn_On_Obsolescent_Feature then
3318
                  Error_Msg_N
3319
                    ("storage size clause for task is an " &
3320
                     "obsolescent feature (RM J.9)?", N);
3321
                  Error_Msg_N ("\use Storage_Size pragma instead?", N);
3322
               end if;
3323
 
3324
               FOnly := True;
3325
            end if;
3326
 
3327
            if not Is_Access_Type (U_Ent)
3328
              and then Ekind (U_Ent) /= E_Task_Type
3329
            then
3330
               Error_Msg_N ("storage size cannot be given for &", Nam);
3331
 
3332
            elsif Is_Access_Type (U_Ent) and Is_Derived_Type (U_Ent) then
3333
               Error_Msg_N
3334
                 ("storage size cannot be given for a derived access type",
3335
                  Nam);
3336
 
3337
            elsif Duplicate_Clause then
3338
               null;
3339
 
3340
            else
3341
               Analyze_And_Resolve (Expr, Any_Integer);
3342
 
3343
               if Is_Access_Type (U_Ent) then
3344
                  if Present (Associated_Storage_Pool (U_Ent)) then
3345
                     Error_Msg_N ("storage pool already given for &", Nam);
3346
                     return;
3347
                  end if;
3348
 
3349
                  if Is_OK_Static_Expression (Expr)
3350
                    and then Expr_Value (Expr) = 0
3351
                  then
3352
                     Set_No_Pool_Assigned (Btype);
3353
                  end if;
3354
 
3355
               else -- Is_Task_Type (U_Ent)
3356
                  Sprag := Get_Rep_Pragma (Btype, Name_Storage_Size);
3357
 
3358
                  if Present (Sprag) then
3359
                     Error_Msg_Sloc := Sloc (Sprag);
3360
                     Error_Msg_N
3361
                       ("Storage_Size already specified#", Nam);
3362
                     return;
3363
                  end if;
3364
               end if;
3365
 
3366
               Set_Has_Storage_Size_Clause (Btype);
3367
            end if;
3368
         end Storage_Size;
3369
 
3370
         -----------------
3371
         -- Stream_Size --
3372
         -----------------
3373
 
3374
         when Attribute_Stream_Size => Stream_Size : declare
3375
            Size : constant Uint := Static_Integer (Expr);
3376
 
3377
         begin
3378
            if Ada_Version <= Ada_95 then
3379
               Check_Restriction (No_Implementation_Attributes, N);
3380
            end if;
3381
 
3382
            if Duplicate_Clause then
3383
               null;
3384
 
3385
            elsif Is_Elementary_Type (U_Ent) then
3386
               if Size /= System_Storage_Unit
3387
                    and then
3388
                  Size /= System_Storage_Unit * 2
3389
                    and then
3390
                  Size /= System_Storage_Unit * 4
3391
                     and then
3392
                  Size /= System_Storage_Unit * 8
3393
               then
3394
                  Error_Msg_Uint_1 := UI_From_Int (System_Storage_Unit);
3395
                  Error_Msg_N
3396
                    ("stream size for elementary type must be a"
3397
                       & " power of 2 and at least ^", N);
3398
 
3399
               elsif RM_Size (U_Ent) > Size then
3400
                  Error_Msg_Uint_1 := RM_Size (U_Ent);
3401
                  Error_Msg_N
3402
                    ("stream size for elementary type must be a"
3403
                       & " power of 2 and at least ^", N);
3404
               end if;
3405
 
3406
               Set_Has_Stream_Size_Clause (U_Ent);
3407
 
3408
            else
3409
               Error_Msg_N ("Stream_Size cannot be given for &", Nam);
3410
            end if;
3411
         end Stream_Size;
3412
 
3413
         ----------------
3414
         -- Value_Size --
3415
         ----------------
3416
 
3417
         --  Value_Size attribute definition clause
3418
 
3419
         when Attribute_Value_Size => Value_Size : declare
3420
            Size   : constant Uint := Static_Integer (Expr);
3421
            Biased : Boolean;
3422
 
3423
         begin
3424
            if not Is_Type (U_Ent) then
3425
               Error_Msg_N ("Value_Size cannot be given for &", Nam);
3426
 
3427
            elsif Duplicate_Clause then
3428
               null;
3429
 
3430
            elsif Is_Array_Type (U_Ent)
3431
              and then not Is_Constrained (U_Ent)
3432
            then
3433
               Error_Msg_N
3434
                 ("Value_Size cannot be given for unconstrained array", Nam);
3435
 
3436
            else
3437
               if Is_Elementary_Type (U_Ent) then
3438
                  Check_Size (Expr, U_Ent, Size, Biased);
3439
                  Set_Biased (U_Ent, N, "value size clause", Biased);
3440
               end if;
3441
 
3442
               Set_RM_Size (U_Ent, Size);
3443
            end if;
3444
         end Value_Size;
3445
 
3446
         -----------------------
3447
         -- Variable_Indexing --
3448
         -----------------------
3449
 
3450
         when Attribute_Variable_Indexing =>
3451
            Check_Indexing_Functions;
3452
 
3453
         -----------
3454
         -- Write --
3455
         -----------
3456
 
3457
         when Attribute_Write =>
3458
            Analyze_Stream_TSS_Definition (TSS_Stream_Write);
3459
            Set_Has_Specified_Stream_Write (Ent);
3460
 
3461
         --  All other attributes cannot be set
3462
 
3463
         when others =>
3464
            Error_Msg_N
3465
              ("attribute& cannot be set with definition clause", N);
3466
      end case;
3467
 
3468
      --  The test for the type being frozen must be performed after any
3469
      --  expression the clause has been analyzed since the expression itself
3470
      --  might cause freezing that makes the clause illegal.
3471
 
3472
      if Rep_Item_Too_Late (U_Ent, N, FOnly) then
3473
         return;
3474
      end if;
3475
   end Analyze_Attribute_Definition_Clause;
3476
 
3477
   ----------------------------
3478
   -- Analyze_Code_Statement --
3479
   ----------------------------
3480
 
3481
   procedure Analyze_Code_Statement (N : Node_Id) is
3482
      HSS   : constant Node_Id   := Parent (N);
3483
      SBody : constant Node_Id   := Parent (HSS);
3484
      Subp  : constant Entity_Id := Current_Scope;
3485
      Stmt  : Node_Id;
3486
      Decl  : Node_Id;
3487
      StmtO : Node_Id;
3488
      DeclO : Node_Id;
3489
 
3490
   begin
3491
      --  Analyze and check we get right type, note that this implements the
3492
      --  requirement (RM 13.8(1)) that Machine_Code be with'ed, since that
3493
      --  is the only way that Asm_Insn could possibly be visible.
3494
 
3495
      Analyze_And_Resolve (Expression (N));
3496
 
3497
      if Etype (Expression (N)) = Any_Type then
3498
         return;
3499
      elsif Etype (Expression (N)) /= RTE (RE_Asm_Insn) then
3500
         Error_Msg_N ("incorrect type for code statement", N);
3501
         return;
3502
      end if;
3503
 
3504
      Check_Code_Statement (N);
3505
 
3506
      --  Make sure we appear in the handled statement sequence of a
3507
      --  subprogram (RM 13.8(3)).
3508
 
3509
      if Nkind (HSS) /= N_Handled_Sequence_Of_Statements
3510
        or else Nkind (SBody) /= N_Subprogram_Body
3511
      then
3512
         Error_Msg_N
3513
           ("code statement can only appear in body of subprogram", N);
3514
         return;
3515
      end if;
3516
 
3517
      --  Do remaining checks (RM 13.8(3)) if not already done
3518
 
3519
      if not Is_Machine_Code_Subprogram (Subp) then
3520
         Set_Is_Machine_Code_Subprogram (Subp);
3521
 
3522
         --  No exception handlers allowed
3523
 
3524
         if Present (Exception_Handlers (HSS)) then
3525
            Error_Msg_N
3526
              ("exception handlers not permitted in machine code subprogram",
3527
               First (Exception_Handlers (HSS)));
3528
         end if;
3529
 
3530
         --  No declarations other than use clauses and pragmas (we allow
3531
         --  certain internally generated declarations as well).
3532
 
3533
         Decl := First (Declarations (SBody));
3534
         while Present (Decl) loop
3535
            DeclO := Original_Node (Decl);
3536
            if Comes_From_Source (DeclO)
3537
              and not Nkind_In (DeclO, N_Pragma,
3538
                                       N_Use_Package_Clause,
3539
                                       N_Use_Type_Clause,
3540
                                       N_Implicit_Label_Declaration)
3541
            then
3542
               Error_Msg_N
3543
                 ("this declaration not allowed in machine code subprogram",
3544
                  DeclO);
3545
            end if;
3546
 
3547
            Next (Decl);
3548
         end loop;
3549
 
3550
         --  No statements other than code statements, pragmas, and labels.
3551
         --  Again we allow certain internally generated statements.
3552
 
3553
         --  In Ada 2012, qualified expressions are names, and the code
3554
         --  statement is initially parsed as a procedure call.
3555
 
3556
         Stmt := First (Statements (HSS));
3557
         while Present (Stmt) loop
3558
            StmtO := Original_Node (Stmt);
3559
 
3560
            --  A procedure call transformed into a code statement is OK.
3561
 
3562
            if Ada_Version >= Ada_2012
3563
              and then Nkind (StmtO) = N_Procedure_Call_Statement
3564
              and then Nkind (Name (StmtO)) = N_Qualified_Expression
3565
            then
3566
               null;
3567
 
3568
            elsif Comes_From_Source (StmtO)
3569
              and then not Nkind_In (StmtO, N_Pragma,
3570
                                            N_Label,
3571
                                            N_Code_Statement)
3572
            then
3573
               Error_Msg_N
3574
                 ("this statement is not allowed in machine code subprogram",
3575
                  StmtO);
3576
            end if;
3577
 
3578
            Next (Stmt);
3579
         end loop;
3580
      end if;
3581
   end Analyze_Code_Statement;
3582
 
3583
   -----------------------------------------------
3584
   -- Analyze_Enumeration_Representation_Clause --
3585
   -----------------------------------------------
3586
 
3587
   procedure Analyze_Enumeration_Representation_Clause (N : Node_Id) is
3588
      Ident    : constant Node_Id    := Identifier (N);
3589
      Aggr     : constant Node_Id    := Array_Aggregate (N);
3590
      Enumtype : Entity_Id;
3591
      Elit     : Entity_Id;
3592
      Expr     : Node_Id;
3593
      Assoc    : Node_Id;
3594
      Choice   : Node_Id;
3595
      Val      : Uint;
3596
 
3597
      Err : Boolean := False;
3598
      --  Set True to avoid cascade errors and crashes on incorrect source code
3599
 
3600
      Lo : constant Uint := Expr_Value (Type_Low_Bound (Universal_Integer));
3601
      Hi : constant Uint := Expr_Value (Type_High_Bound (Universal_Integer));
3602
      --  Allowed range of universal integer (= allowed range of enum lit vals)
3603
 
3604
      Min : Uint;
3605
      Max : Uint;
3606
      --  Minimum and maximum values of entries
3607
 
3608
      Max_Node : Node_Id;
3609
      --  Pointer to node for literal providing max value
3610
 
3611
   begin
3612
      if Ignore_Rep_Clauses then
3613
         return;
3614
      end if;
3615
 
3616
      --  First some basic error checks
3617
 
3618
      Find_Type (Ident);
3619
      Enumtype := Entity (Ident);
3620
 
3621
      if Enumtype = Any_Type
3622
        or else Rep_Item_Too_Early (Enumtype, N)
3623
      then
3624
         return;
3625
      else
3626
         Enumtype := Underlying_Type (Enumtype);
3627
      end if;
3628
 
3629
      if not Is_Enumeration_Type (Enumtype) then
3630
         Error_Msg_NE
3631
           ("enumeration type required, found}",
3632
            Ident, First_Subtype (Enumtype));
3633
         return;
3634
      end if;
3635
 
3636
      --  Ignore rep clause on generic actual type. This will already have
3637
      --  been flagged on the template as an error, and this is the safest
3638
      --  way to ensure we don't get a junk cascaded message in the instance.
3639
 
3640
      if Is_Generic_Actual_Type (Enumtype) then
3641
         return;
3642
 
3643
      --  Type must be in current scope
3644
 
3645
      elsif Scope (Enumtype) /= Current_Scope then
3646
         Error_Msg_N ("type must be declared in this scope", Ident);
3647
         return;
3648
 
3649
      --  Type must be a first subtype
3650
 
3651
      elsif not Is_First_Subtype (Enumtype) then
3652
         Error_Msg_N ("cannot give enumeration rep clause for subtype", N);
3653
         return;
3654
 
3655
      --  Ignore duplicate rep clause
3656
 
3657
      elsif Has_Enumeration_Rep_Clause (Enumtype) then
3658
         Error_Msg_N ("duplicate enumeration rep clause ignored", N);
3659
         return;
3660
 
3661
      --  Don't allow rep clause for standard [wide_[wide_]]character
3662
 
3663
      elsif Is_Standard_Character_Type (Enumtype) then
3664
         Error_Msg_N ("enumeration rep clause not allowed for this type", N);
3665
         return;
3666
 
3667
      --  Check that the expression is a proper aggregate (no parentheses)
3668
 
3669
      elsif Paren_Count (Aggr) /= 0 then
3670
         Error_Msg
3671
           ("extra parentheses surrounding aggregate not allowed",
3672
            First_Sloc (Aggr));
3673
         return;
3674
 
3675
      --  All tests passed, so set rep clause in place
3676
 
3677
      else
3678
         Set_Has_Enumeration_Rep_Clause (Enumtype);
3679
         Set_Has_Enumeration_Rep_Clause (Base_Type (Enumtype));
3680
      end if;
3681
 
3682
      --  Now we process the aggregate. Note that we don't use the normal
3683
      --  aggregate code for this purpose, because we don't want any of the
3684
      --  normal expansion activities, and a number of special semantic
3685
      --  rules apply (including the component type being any integer type)
3686
 
3687
      Elit := First_Literal (Enumtype);
3688
 
3689
      --  First the positional entries if any
3690
 
3691
      if Present (Expressions (Aggr)) then
3692
         Expr := First (Expressions (Aggr));
3693
         while Present (Expr) loop
3694
            if No (Elit) then
3695
               Error_Msg_N ("too many entries in aggregate", Expr);
3696
               return;
3697
            end if;
3698
 
3699
            Val := Static_Integer (Expr);
3700
 
3701
            --  Err signals that we found some incorrect entries processing
3702
            --  the list. The final checks for completeness and ordering are
3703
            --  skipped in this case.
3704
 
3705
            if Val = No_Uint then
3706
               Err := True;
3707
            elsif Val < Lo or else Hi < Val then
3708
               Error_Msg_N ("value outside permitted range", Expr);
3709
               Err := True;
3710
            end if;
3711
 
3712
            Set_Enumeration_Rep (Elit, Val);
3713
            Set_Enumeration_Rep_Expr (Elit, Expr);
3714
            Next (Expr);
3715
            Next (Elit);
3716
         end loop;
3717
      end if;
3718
 
3719
      --  Now process the named entries if present
3720
 
3721
      if Present (Component_Associations (Aggr)) then
3722
         Assoc := First (Component_Associations (Aggr));
3723
         while Present (Assoc) loop
3724
            Choice := First (Choices (Assoc));
3725
 
3726
            if Present (Next (Choice)) then
3727
               Error_Msg_N
3728
                 ("multiple choice not allowed here", Next (Choice));
3729
               Err := True;
3730
            end if;
3731
 
3732
            if Nkind (Choice) = N_Others_Choice then
3733
               Error_Msg_N ("others choice not allowed here", Choice);
3734
               Err := True;
3735
 
3736
            elsif Nkind (Choice) = N_Range then
3737
 
3738
               --  ??? should allow zero/one element range here
3739
 
3740
               Error_Msg_N ("range not allowed here", Choice);
3741
               Err := True;
3742
 
3743
            else
3744
               Analyze_And_Resolve (Choice, Enumtype);
3745
 
3746
               if Error_Posted (Choice) then
3747
                  Err := True;
3748
               end if;
3749
 
3750
               if not Err then
3751
                  if Is_Entity_Name (Choice)
3752
                    and then Is_Type (Entity (Choice))
3753
                  then
3754
                     Error_Msg_N ("subtype name not allowed here", Choice);
3755
                     Err := True;
3756
 
3757
                     --  ??? should allow static subtype with zero/one entry
3758
 
3759
                  elsif Etype (Choice) = Base_Type (Enumtype) then
3760
                     if not Is_Static_Expression (Choice) then
3761
                        Flag_Non_Static_Expr
3762
                          ("non-static expression used for choice!", Choice);
3763
                        Err := True;
3764
 
3765
                     else
3766
                        Elit := Expr_Value_E (Choice);
3767
 
3768
                        if Present (Enumeration_Rep_Expr (Elit)) then
3769
                           Error_Msg_Sloc :=
3770
                             Sloc (Enumeration_Rep_Expr (Elit));
3771
                           Error_Msg_NE
3772
                             ("representation for& previously given#",
3773
                              Choice, Elit);
3774
                           Err := True;
3775
                        end if;
3776
 
3777
                        Set_Enumeration_Rep_Expr (Elit, Expression (Assoc));
3778
 
3779
                        Expr := Expression (Assoc);
3780
                        Val := Static_Integer (Expr);
3781
 
3782
                        if Val = No_Uint then
3783
                           Err := True;
3784
 
3785
                        elsif Val < Lo or else Hi < Val then
3786
                           Error_Msg_N ("value outside permitted range", Expr);
3787
                           Err := True;
3788
                        end if;
3789
 
3790
                        Set_Enumeration_Rep (Elit, Val);
3791
                     end if;
3792
                  end if;
3793
               end if;
3794
            end if;
3795
 
3796
            Next (Assoc);
3797
         end loop;
3798
      end if;
3799
 
3800
      --  Aggregate is fully processed. Now we check that a full set of
3801
      --  representations was given, and that they are in range and in order.
3802
      --  These checks are only done if no other errors occurred.
3803
 
3804
      if not Err then
3805
         Min  := No_Uint;
3806
         Max  := No_Uint;
3807
 
3808
         Elit := First_Literal (Enumtype);
3809
         while Present (Elit) loop
3810
            if No (Enumeration_Rep_Expr (Elit)) then
3811
               Error_Msg_NE ("missing representation for&!", N, Elit);
3812
 
3813
            else
3814
               Val := Enumeration_Rep (Elit);
3815
 
3816
               if Min = No_Uint then
3817
                  Min := Val;
3818
               end if;
3819
 
3820
               if Val /= No_Uint then
3821
                  if Max /= No_Uint and then Val <= Max then
3822
                     Error_Msg_NE
3823
                       ("enumeration value for& not ordered!",
3824
                        Enumeration_Rep_Expr (Elit), Elit);
3825
                  end if;
3826
 
3827
                  Max_Node := Enumeration_Rep_Expr (Elit);
3828
                  Max := Val;
3829
               end if;
3830
 
3831
               --  If there is at least one literal whose representation is not
3832
               --  equal to the Pos value, then note that this enumeration type
3833
               --  has a non-standard representation.
3834
 
3835
               if Val /= Enumeration_Pos (Elit) then
3836
                  Set_Has_Non_Standard_Rep (Base_Type (Enumtype));
3837
               end if;
3838
            end if;
3839
 
3840
            Next (Elit);
3841
         end loop;
3842
 
3843
         --  Now set proper size information
3844
 
3845
         declare
3846
            Minsize : Uint := UI_From_Int (Minimum_Size (Enumtype));
3847
 
3848
         begin
3849
            if Has_Size_Clause (Enumtype) then
3850
 
3851
               --  All OK, if size is OK now
3852
 
3853
               if RM_Size (Enumtype) >= Minsize then
3854
                  null;
3855
 
3856
               else
3857
                  --  Try if we can get by with biasing
3858
 
3859
                  Minsize :=
3860
                    UI_From_Int (Minimum_Size (Enumtype, Biased => True));
3861
 
3862
                  --  Error message if even biasing does not work
3863
 
3864
                  if RM_Size (Enumtype) < Minsize then
3865
                     Error_Msg_Uint_1 := RM_Size (Enumtype);
3866
                     Error_Msg_Uint_2 := Max;
3867
                     Error_Msg_N
3868
                       ("previously given size (^) is too small "
3869
                        & "for this value (^)", Max_Node);
3870
 
3871
                  --  If biasing worked, indicate that we now have biased rep
3872
 
3873
                  else
3874
                     Set_Biased
3875
                       (Enumtype, Size_Clause (Enumtype), "size clause");
3876
                  end if;
3877
               end if;
3878
 
3879
            else
3880
               Set_RM_Size    (Enumtype, Minsize);
3881
               Set_Enum_Esize (Enumtype);
3882
            end if;
3883
 
3884
            Set_RM_Size   (Base_Type (Enumtype), RM_Size   (Enumtype));
3885
            Set_Esize     (Base_Type (Enumtype), Esize     (Enumtype));
3886
            Set_Alignment (Base_Type (Enumtype), Alignment (Enumtype));
3887
         end;
3888
      end if;
3889
 
3890
      --  We repeat the too late test in case it froze itself!
3891
 
3892
      if Rep_Item_Too_Late (Enumtype, N) then
3893
         null;
3894
      end if;
3895
   end Analyze_Enumeration_Representation_Clause;
3896
 
3897
   ----------------------------
3898
   -- Analyze_Free_Statement --
3899
   ----------------------------
3900
 
3901
   procedure Analyze_Free_Statement (N : Node_Id) is
3902
   begin
3903
      Analyze (Expression (N));
3904
   end Analyze_Free_Statement;
3905
 
3906
   ---------------------------
3907
   -- Analyze_Freeze_Entity --
3908
   ---------------------------
3909
 
3910
   procedure Analyze_Freeze_Entity (N : Node_Id) is
3911
      E : constant Entity_Id := Entity (N);
3912
 
3913
   begin
3914
      --  Remember that we are processing a freezing entity. Required to
3915
      --  ensure correct decoration of internal entities associated with
3916
      --  interfaces (see New_Overloaded_Entity).
3917
 
3918
      Inside_Freezing_Actions := Inside_Freezing_Actions + 1;
3919
 
3920
      --  For tagged types covering interfaces add internal entities that link
3921
      --  the primitives of the interfaces with the primitives that cover them.
3922
      --  Note: These entities were originally generated only when generating
3923
      --  code because their main purpose was to provide support to initialize
3924
      --  the secondary dispatch tables. They are now generated also when
3925
      --  compiling with no code generation to provide ASIS the relationship
3926
      --  between interface primitives and tagged type primitives. They are
3927
      --  also used to locate primitives covering interfaces when processing
3928
      --  generics (see Derive_Subprograms).
3929
 
3930
      if Ada_Version >= Ada_2005
3931
        and then Ekind (E) = E_Record_Type
3932
        and then Is_Tagged_Type (E)
3933
        and then not Is_Interface (E)
3934
        and then Has_Interfaces (E)
3935
      then
3936
         --  This would be a good common place to call the routine that checks
3937
         --  overriding of interface primitives (and thus factorize calls to
3938
         --  Check_Abstract_Overriding located at different contexts in the
3939
         --  compiler). However, this is not possible because it causes
3940
         --  spurious errors in case of late overriding.
3941
 
3942
         Add_Internal_Interface_Entities (E);
3943
      end if;
3944
 
3945
      --  Check CPP types
3946
 
3947
      if Ekind (E) = E_Record_Type
3948
        and then Is_CPP_Class (E)
3949
        and then Is_Tagged_Type (E)
3950
        and then Tagged_Type_Expansion
3951
        and then Expander_Active
3952
      then
3953
         if CPP_Num_Prims (E) = 0 then
3954
 
3955
            --  If the CPP type has user defined components then it must import
3956
            --  primitives from C++. This is required because if the C++ class
3957
            --  has no primitives then the C++ compiler does not added the _tag
3958
            --  component to the type.
3959
 
3960
            pragma Assert (Chars (First_Entity (E)) = Name_uTag);
3961
 
3962
            if First_Entity (E) /= Last_Entity (E) then
3963
               Error_Msg_N
3964
                 ("?'C'P'P type must import at least one primitive from C++",
3965
                  E);
3966
            end if;
3967
         end if;
3968
 
3969
         --  Check that all its primitives are abstract or imported from C++.
3970
         --  Check also availability of the C++ constructor.
3971
 
3972
         declare
3973
            Has_Constructors : constant Boolean := Has_CPP_Constructors (E);
3974
            Elmt             : Elmt_Id;
3975
            Error_Reported   : Boolean := False;
3976
            Prim             : Node_Id;
3977
 
3978
         begin
3979
            Elmt := First_Elmt (Primitive_Operations (E));
3980
            while Present (Elmt) loop
3981
               Prim := Node (Elmt);
3982
 
3983
               if Comes_From_Source (Prim) then
3984
                  if Is_Abstract_Subprogram (Prim) then
3985
                     null;
3986
 
3987
                  elsif not Is_Imported (Prim)
3988
                    or else Convention (Prim) /= Convention_CPP
3989
                  then
3990
                     Error_Msg_N
3991
                       ("?primitives of 'C'P'P types must be imported from C++"
3992
                        & " or abstract", Prim);
3993
 
3994
                  elsif not Has_Constructors
3995
                     and then not Error_Reported
3996
                  then
3997
                     Error_Msg_Name_1 := Chars (E);
3998
                     Error_Msg_N
3999
                       ("?'C'P'P constructor required for type %", Prim);
4000
                     Error_Reported := True;
4001
                  end if;
4002
               end if;
4003
 
4004
               Next_Elmt (Elmt);
4005
            end loop;
4006
         end;
4007
      end if;
4008
 
4009
      Inside_Freezing_Actions := Inside_Freezing_Actions - 1;
4010
 
4011
      --  If we have a type with predicates, build predicate function
4012
 
4013
      if Is_Type (E) and then Has_Predicates (E) then
4014
         Build_Predicate_Function (E, N);
4015
      end if;
4016
 
4017
      --  If type has delayed aspects, this is where we do the preanalysis at
4018
      --  the freeze point, as part of the consistent visibility check. Note
4019
      --  that this must be done after calling Build_Predicate_Function or
4020
      --  Build_Invariant_Procedure since these subprograms fix occurrences of
4021
      --  the subtype name in the saved expression so that they will not cause
4022
      --  trouble in the preanalysis.
4023
 
4024
      if Has_Delayed_Aspects (E) then
4025
         declare
4026
            Ritem : Node_Id;
4027
 
4028
         begin
4029
            --  Look for aspect specification entries for this entity
4030
 
4031
            Ritem := First_Rep_Item (E);
4032
            while Present (Ritem) loop
4033
               if Nkind (Ritem) = N_Aspect_Specification
4034
                 and then Entity (Ritem) = E
4035
                 and then Is_Delayed_Aspect (Ritem)
4036
                 and then Scope (E) = Current_Scope
4037
               then
4038
                  Check_Aspect_At_Freeze_Point (Ritem);
4039
               end if;
4040
 
4041
               Next_Rep_Item (Ritem);
4042
            end loop;
4043
         end;
4044
      end if;
4045
   end Analyze_Freeze_Entity;
4046
 
4047
   ------------------------------------------
4048
   -- Analyze_Record_Representation_Clause --
4049
   ------------------------------------------
4050
 
4051
   --  Note: we check as much as we can here, but we can't do any checks
4052
   --  based on the position values (e.g. overlap checks) until freeze time
4053
   --  because especially in Ada 2005 (machine scalar mode), the processing
4054
   --  for non-standard bit order can substantially change the positions.
4055
   --  See procedure Check_Record_Representation_Clause (called from Freeze)
4056
   --  for the remainder of this processing.
4057
 
4058
   procedure Analyze_Record_Representation_Clause (N : Node_Id) is
4059
      Ident   : constant Node_Id := Identifier (N);
4060
      Biased  : Boolean;
4061
      CC      : Node_Id;
4062
      Comp    : Entity_Id;
4063
      Fbit    : Uint;
4064
      Hbit    : Uint := Uint_0;
4065
      Lbit    : Uint;
4066
      Ocomp   : Entity_Id;
4067
      Posit   : Uint;
4068
      Rectype : Entity_Id;
4069
 
4070
      CR_Pragma : Node_Id := Empty;
4071
      --  Points to N_Pragma node if Complete_Representation pragma present
4072
 
4073
   begin
4074
      if Ignore_Rep_Clauses then
4075
         return;
4076
      end if;
4077
 
4078
      Find_Type (Ident);
4079
      Rectype := Entity (Ident);
4080
 
4081
      if Rectype = Any_Type
4082
        or else Rep_Item_Too_Early (Rectype, N)
4083
      then
4084
         return;
4085
      else
4086
         Rectype := Underlying_Type (Rectype);
4087
      end if;
4088
 
4089
      --  First some basic error checks
4090
 
4091
      if not Is_Record_Type (Rectype) then
4092
         Error_Msg_NE
4093
           ("record type required, found}", Ident, First_Subtype (Rectype));
4094
         return;
4095
 
4096
      elsif Scope (Rectype) /= Current_Scope then
4097
         Error_Msg_N ("type must be declared in this scope", N);
4098
         return;
4099
 
4100
      elsif not Is_First_Subtype (Rectype) then
4101
         Error_Msg_N ("cannot give record rep clause for subtype", N);
4102
         return;
4103
 
4104
      elsif Has_Record_Rep_Clause (Rectype) then
4105
         Error_Msg_N ("duplicate record rep clause ignored", N);
4106
         return;
4107
 
4108
      elsif Rep_Item_Too_Late (Rectype, N) then
4109
         return;
4110
      end if;
4111
 
4112
      if Present (Mod_Clause (N)) then
4113
         declare
4114
            Loc     : constant Source_Ptr := Sloc (N);
4115
            M       : constant Node_Id := Mod_Clause (N);
4116
            P       : constant List_Id := Pragmas_Before (M);
4117
            AtM_Nod : Node_Id;
4118
 
4119
            Mod_Val : Uint;
4120
            pragma Warnings (Off, Mod_Val);
4121
 
4122
         begin
4123
            Check_Restriction (No_Obsolescent_Features, Mod_Clause (N));
4124
 
4125
            if Warn_On_Obsolescent_Feature then
4126
               Error_Msg_N
4127
                 ("mod clause is an obsolescent feature (RM J.8)?", N);
4128
               Error_Msg_N
4129
                 ("\use alignment attribute definition clause instead?", N);
4130
            end if;
4131
 
4132
            if Present (P) then
4133
               Analyze_List (P);
4134
            end if;
4135
 
4136
            --  In ASIS_Mode mode, expansion is disabled, but we must convert
4137
            --  the Mod clause into an alignment clause anyway, so that the
4138
            --  back-end can compute and back-annotate properly the size and
4139
            --  alignment of types that may include this record.
4140
 
4141
            --  This seems dubious, this destroys the source tree in a manner
4142
            --  not detectable by ASIS ???
4143
 
4144
            if Operating_Mode = Check_Semantics and then ASIS_Mode then
4145
               AtM_Nod :=
4146
                 Make_Attribute_Definition_Clause (Loc,
4147
                   Name       => New_Reference_To (Base_Type (Rectype), Loc),
4148
                   Chars      => Name_Alignment,
4149
                   Expression => Relocate_Node (Expression (M)));
4150
 
4151
               Set_From_At_Mod (AtM_Nod);
4152
               Insert_After (N, AtM_Nod);
4153
               Mod_Val := Get_Alignment_Value (Expression (AtM_Nod));
4154
               Set_Mod_Clause (N, Empty);
4155
 
4156
            else
4157
               --  Get the alignment value to perform error checking
4158
 
4159
               Mod_Val := Get_Alignment_Value (Expression (M));
4160
            end if;
4161
         end;
4162
      end if;
4163
 
4164
      --  For untagged types, clear any existing component clauses for the
4165
      --  type. If the type is derived, this is what allows us to override
4166
      --  a rep clause for the parent. For type extensions, the representation
4167
      --  of the inherited components is inherited, so we want to keep previous
4168
      --  component clauses for completeness.
4169
 
4170
      if not Is_Tagged_Type (Rectype) then
4171
         Comp := First_Component_Or_Discriminant (Rectype);
4172
         while Present (Comp) loop
4173
            Set_Component_Clause (Comp, Empty);
4174
            Next_Component_Or_Discriminant (Comp);
4175
         end loop;
4176
      end if;
4177
 
4178
      --  All done if no component clauses
4179
 
4180
      CC := First (Component_Clauses (N));
4181
 
4182
      if No (CC) then
4183
         return;
4184
      end if;
4185
 
4186
      --  A representation like this applies to the base type
4187
 
4188
      Set_Has_Record_Rep_Clause (Base_Type (Rectype));
4189
      Set_Has_Non_Standard_Rep  (Base_Type (Rectype));
4190
      Set_Has_Specified_Layout  (Base_Type (Rectype));
4191
 
4192
      --  Process the component clauses
4193
 
4194
      while Present (CC) loop
4195
 
4196
         --  Pragma
4197
 
4198
         if Nkind (CC) = N_Pragma then
4199
            Analyze (CC);
4200
 
4201
            --  The only pragma of interest is Complete_Representation
4202
 
4203
            if Pragma_Name (CC) = Name_Complete_Representation then
4204
               CR_Pragma := CC;
4205
            end if;
4206
 
4207
         --  Processing for real component clause
4208
 
4209
         else
4210
            Posit := Static_Integer (Position  (CC));
4211
            Fbit  := Static_Integer (First_Bit (CC));
4212
            Lbit  := Static_Integer (Last_Bit  (CC));
4213
 
4214
            if Posit /= No_Uint
4215
              and then Fbit /= No_Uint
4216
              and then Lbit /= No_Uint
4217
            then
4218
               if Posit < 0 then
4219
                  Error_Msg_N
4220
                    ("position cannot be negative", Position (CC));
4221
 
4222
               elsif Fbit < 0 then
4223
                  Error_Msg_N
4224
                    ("first bit cannot be negative", First_Bit (CC));
4225
 
4226
               --  The Last_Bit specified in a component clause must not be
4227
               --  less than the First_Bit minus one (RM-13.5.1(10)).
4228
 
4229
               elsif Lbit < Fbit - 1 then
4230
                  Error_Msg_N
4231
                    ("last bit cannot be less than first bit minus one",
4232
                     Last_Bit (CC));
4233
 
4234
               --  Values look OK, so find the corresponding record component
4235
               --  Even though the syntax allows an attribute reference for
4236
               --  implementation-defined components, GNAT does not allow the
4237
               --  tag to get an explicit position.
4238
 
4239
               elsif Nkind (Component_Name (CC)) = N_Attribute_Reference then
4240
                  if Attribute_Name (Component_Name (CC)) = Name_Tag then
4241
                     Error_Msg_N ("position of tag cannot be specified", CC);
4242
                  else
4243
                     Error_Msg_N ("illegal component name", CC);
4244
                  end if;
4245
 
4246
               else
4247
                  Comp := First_Entity (Rectype);
4248
                  while Present (Comp) loop
4249
                     exit when Chars (Comp) = Chars (Component_Name (CC));
4250
                     Next_Entity (Comp);
4251
                  end loop;
4252
 
4253
                  if No (Comp) then
4254
 
4255
                     --  Maybe component of base type that is absent from
4256
                     --  statically constrained first subtype.
4257
 
4258
                     Comp := First_Entity (Base_Type (Rectype));
4259
                     while Present (Comp) loop
4260
                        exit when Chars (Comp) = Chars (Component_Name (CC));
4261
                        Next_Entity (Comp);
4262
                     end loop;
4263
                  end if;
4264
 
4265
                  if No (Comp) then
4266
                     Error_Msg_N
4267
                       ("component clause is for non-existent field", CC);
4268
 
4269
                  --  Ada 2012 (AI05-0026): Any name that denotes a
4270
                  --  discriminant of an object of an unchecked union type
4271
                  --  shall not occur within a record_representation_clause.
4272
 
4273
                  --  The general restriction of using record rep clauses on
4274
                  --  Unchecked_Union types has now been lifted. Since it is
4275
                  --  possible to introduce a record rep clause which mentions
4276
                  --  the discriminant of an Unchecked_Union in non-Ada 2012
4277
                  --  code, this check is applied to all versions of the
4278
                  --  language.
4279
 
4280
                  elsif Ekind (Comp) = E_Discriminant
4281
                    and then Is_Unchecked_Union (Rectype)
4282
                  then
4283
                     Error_Msg_N
4284
                       ("cannot reference discriminant of Unchecked_Union",
4285
                        Component_Name (CC));
4286
 
4287
                  elsif Present (Component_Clause (Comp)) then
4288
 
4289
                     --  Diagnose duplicate rep clause, or check consistency
4290
                     --  if this is an inherited component. In a double fault,
4291
                     --  there may be a duplicate inconsistent clause for an
4292
                     --  inherited component.
4293
 
4294
                     if Scope (Original_Record_Component (Comp)) = Rectype
4295
                       or else Parent (Component_Clause (Comp)) = N
4296
                     then
4297
                        Error_Msg_Sloc := Sloc (Component_Clause (Comp));
4298
                        Error_Msg_N ("component clause previously given#", CC);
4299
 
4300
                     else
4301
                        declare
4302
                           Rep1 : constant Node_Id := Component_Clause (Comp);
4303
                        begin
4304
                           if Intval (Position (Rep1)) /=
4305
                                                   Intval (Position (CC))
4306
                             or else Intval (First_Bit (Rep1)) /=
4307
                                                   Intval (First_Bit (CC))
4308
                             or else Intval (Last_Bit (Rep1)) /=
4309
                                                   Intval (Last_Bit (CC))
4310
                           then
4311
                              Error_Msg_N ("component clause inconsistent "
4312
                                & "with representation of ancestor", CC);
4313
                           elsif Warn_On_Redundant_Constructs then
4314
                              Error_Msg_N ("?redundant component clause "
4315
                                & "for inherited component!", CC);
4316
                           end if;
4317
                        end;
4318
                     end if;
4319
 
4320
                  --  Normal case where this is the first component clause we
4321
                  --  have seen for this entity, so set it up properly.
4322
 
4323
                  else
4324
                     --  Make reference for field in record rep clause and set
4325
                     --  appropriate entity field in the field identifier.
4326
 
4327
                     Generate_Reference
4328
                       (Comp, Component_Name (CC), Set_Ref => False);
4329
                     Set_Entity (Component_Name (CC), Comp);
4330
 
4331
                     --  Update Fbit and Lbit to the actual bit number
4332
 
4333
                     Fbit := Fbit + UI_From_Int (SSU) * Posit;
4334
                     Lbit := Lbit + UI_From_Int (SSU) * Posit;
4335
 
4336
                     if Has_Size_Clause (Rectype)
4337
                       and then RM_Size (Rectype) <= Lbit
4338
                     then
4339
                        Error_Msg_N
4340
                          ("bit number out of range of specified size",
4341
                           Last_Bit (CC));
4342
                     else
4343
                        Set_Component_Clause     (Comp, CC);
4344
                        Set_Component_Bit_Offset (Comp, Fbit);
4345
                        Set_Esize                (Comp, 1 + (Lbit - Fbit));
4346
                        Set_Normalized_First_Bit (Comp, Fbit mod SSU);
4347
                        Set_Normalized_Position  (Comp, Fbit / SSU);
4348
 
4349
                        if Warn_On_Overridden_Size
4350
                          and then Has_Size_Clause (Etype (Comp))
4351
                          and then RM_Size (Etype (Comp)) /= Esize (Comp)
4352
                        then
4353
                           Error_Msg_NE
4354
                             ("?component size overrides size clause for&",
4355
                              Component_Name (CC), Etype (Comp));
4356
                        end if;
4357
 
4358
                        --  This information is also set in the corresponding
4359
                        --  component of the base type, found by accessing the
4360
                        --  Original_Record_Component link if it is present.
4361
 
4362
                        Ocomp := Original_Record_Component (Comp);
4363
 
4364
                        if Hbit < Lbit then
4365
                           Hbit := Lbit;
4366
                        end if;
4367
 
4368
                        Check_Size
4369
                          (Component_Name (CC),
4370
                           Etype (Comp),
4371
                           Esize (Comp),
4372
                           Biased);
4373
 
4374
                        Set_Biased
4375
                          (Comp, First_Node (CC), "component clause", Biased);
4376
 
4377
                        if Present (Ocomp) then
4378
                           Set_Component_Clause     (Ocomp, CC);
4379
                           Set_Component_Bit_Offset (Ocomp, Fbit);
4380
                           Set_Normalized_First_Bit (Ocomp, Fbit mod SSU);
4381
                           Set_Normalized_Position  (Ocomp, Fbit / SSU);
4382
                           Set_Esize                (Ocomp, 1 + (Lbit - Fbit));
4383
 
4384
                           Set_Normalized_Position_Max
4385
                             (Ocomp, Normalized_Position (Ocomp));
4386
 
4387
                           --  Note: we don't use Set_Biased here, because we
4388
                           --  already gave a warning above if needed, and we
4389
                           --  would get a duplicate for the same name here.
4390
 
4391
                           Set_Has_Biased_Representation
4392
                             (Ocomp, Has_Biased_Representation (Comp));
4393
                        end if;
4394
 
4395
                        if Esize (Comp) < 0 then
4396
                           Error_Msg_N ("component size is negative", CC);
4397
                        end if;
4398
                     end if;
4399
                  end if;
4400
               end if;
4401
            end if;
4402
         end if;
4403
 
4404
         Next (CC);
4405
      end loop;
4406
 
4407
      --  Check missing components if Complete_Representation pragma appeared
4408
 
4409
      if Present (CR_Pragma) then
4410
         Comp := First_Component_Or_Discriminant (Rectype);
4411
         while Present (Comp) loop
4412
            if No (Component_Clause (Comp)) then
4413
               Error_Msg_NE
4414
                 ("missing component clause for &", CR_Pragma, Comp);
4415
            end if;
4416
 
4417
            Next_Component_Or_Discriminant (Comp);
4418
         end loop;
4419
 
4420
         --  If no Complete_Representation pragma, warn if missing components
4421
 
4422
      elsif Warn_On_Unrepped_Components then
4423
         declare
4424
            Num_Repped_Components   : Nat := 0;
4425
            Num_Unrepped_Components : Nat := 0;
4426
 
4427
         begin
4428
            --  First count number of repped and unrepped components
4429
 
4430
            Comp := First_Component_Or_Discriminant (Rectype);
4431
            while Present (Comp) loop
4432
               if Present (Component_Clause (Comp)) then
4433
                  Num_Repped_Components := Num_Repped_Components + 1;
4434
               else
4435
                  Num_Unrepped_Components := Num_Unrepped_Components + 1;
4436
               end if;
4437
 
4438
               Next_Component_Or_Discriminant (Comp);
4439
            end loop;
4440
 
4441
            --  We are only interested in the case where there is at least one
4442
            --  unrepped component, and at least half the components have rep
4443
            --  clauses. We figure that if less than half have them, then the
4444
            --  partial rep clause is really intentional. If the component
4445
            --  type has no underlying type set at this point (as for a generic
4446
            --  formal type), we don't know enough to give a warning on the
4447
            --  component.
4448
 
4449
            if Num_Unrepped_Components > 0
4450
              and then Num_Unrepped_Components < Num_Repped_Components
4451
            then
4452
               Comp := First_Component_Or_Discriminant (Rectype);
4453
               while Present (Comp) loop
4454
                  if No (Component_Clause (Comp))
4455
                    and then Comes_From_Source (Comp)
4456
                    and then Present (Underlying_Type (Etype (Comp)))
4457
                    and then (Is_Scalar_Type (Underlying_Type (Etype (Comp)))
4458
                               or else Size_Known_At_Compile_Time
4459
                                         (Underlying_Type (Etype (Comp))))
4460
                    and then not Has_Warnings_Off (Rectype)
4461
                  then
4462
                     Error_Msg_Sloc := Sloc (Comp);
4463
                     Error_Msg_NE
4464
                       ("?no component clause given for & declared #",
4465
                        N, Comp);
4466
                  end if;
4467
 
4468
                  Next_Component_Or_Discriminant (Comp);
4469
               end loop;
4470
            end if;
4471
         end;
4472
      end if;
4473
   end Analyze_Record_Representation_Clause;
4474
 
4475
   -------------------------------
4476
   -- Build_Invariant_Procedure --
4477
   -------------------------------
4478
 
4479
   --  The procedure that is constructed here has the form
4480
 
4481
   --  procedure typInvariant (Ixxx : typ) is
4482
   --  begin
4483
   --     pragma Check (Invariant, exp, "failed invariant from xxx");
4484
   --     pragma Check (Invariant, exp, "failed invariant from xxx");
4485
   --     ...
4486
   --     pragma Check (Invariant, exp, "failed inherited invariant from xxx");
4487
   --     ...
4488
   --  end typInvariant;
4489
 
4490
   procedure Build_Invariant_Procedure (Typ : Entity_Id; N : Node_Id) is
4491
      Loc   : constant Source_Ptr := Sloc (Typ);
4492
      Stmts : List_Id;
4493
      Spec  : Node_Id;
4494
      SId   : Entity_Id;
4495
      PDecl : Node_Id;
4496
      PBody : Node_Id;
4497
 
4498
      Visible_Decls : constant List_Id := Visible_Declarations (N);
4499
      Private_Decls : constant List_Id := Private_Declarations (N);
4500
 
4501
      procedure Add_Invariants (T : Entity_Id; Inherit : Boolean);
4502
      --  Appends statements to Stmts for any invariants in the rep item chain
4503
      --  of the given type. If Inherit is False, then we only process entries
4504
      --  on the chain for the type Typ. If Inherit is True, then we ignore any
4505
      --  Invariant aspects, but we process all Invariant'Class aspects, adding
4506
      --  "inherited" to the exception message and generating an informational
4507
      --  message about the inheritance of an invariant.
4508
 
4509
      Object_Name : constant Name_Id := New_Internal_Name ('I');
4510
      --  Name for argument of invariant procedure
4511
 
4512
      Object_Entity : constant Node_Id :=
4513
                        Make_Defining_Identifier (Loc, Object_Name);
4514
      --  The procedure declaration entity for the argument
4515
 
4516
      --------------------
4517
      -- Add_Invariants --
4518
      --------------------
4519
 
4520
      procedure Add_Invariants (T : Entity_Id; Inherit : Boolean) is
4521
         Ritem : Node_Id;
4522
         Arg1  : Node_Id;
4523
         Arg2  : Node_Id;
4524
         Arg3  : Node_Id;
4525
         Exp   : Node_Id;
4526
         Loc   : Source_Ptr;
4527
         Assoc : List_Id;
4528
         Str   : String_Id;
4529
 
4530
         procedure Replace_Type_Reference (N : Node_Id);
4531
         --  Replace a single occurrence N of the subtype name with a reference
4532
         --  to the formal of the predicate function. N can be an identifier
4533
         --  referencing the subtype, or a selected component, representing an
4534
         --  appropriately qualified occurrence of the subtype name.
4535
 
4536
         procedure Replace_Type_References is
4537
           new Replace_Type_References_Generic (Replace_Type_Reference);
4538
         --  Traverse an expression replacing all occurrences of the subtype
4539
         --  name with appropriate references to the object that is the formal
4540
         --  parameter of the predicate function. Note that we must ensure
4541
         --  that the type and entity information is properly set in the
4542
         --  replacement node, since we will do a Preanalyze call of this
4543
         --  expression without proper visibility of the procedure argument.
4544
 
4545
         ----------------------------
4546
         -- Replace_Type_Reference --
4547
         ----------------------------
4548
 
4549
         procedure Replace_Type_Reference (N : Node_Id) is
4550
         begin
4551
            --  Invariant'Class, replace with T'Class (obj)
4552
 
4553
            if Class_Present (Ritem) then
4554
               Rewrite (N,
4555
                 Make_Type_Conversion (Loc,
4556
                   Subtype_Mark =>
4557
                     Make_Attribute_Reference (Loc,
4558
                       Prefix         => New_Occurrence_Of (T, Loc),
4559
                       Attribute_Name => Name_Class),
4560
                   Expression   => Make_Identifier (Loc, Object_Name)));
4561
 
4562
               Set_Entity (Expression (N), Object_Entity);
4563
               Set_Etype  (Expression (N), Typ);
4564
 
4565
            --  Invariant, replace with obj
4566
 
4567
            else
4568
               Rewrite (N, Make_Identifier (Loc, Object_Name));
4569
               Set_Entity (N, Object_Entity);
4570
               Set_Etype  (N, Typ);
4571
            end if;
4572
         end Replace_Type_Reference;
4573
 
4574
      --  Start of processing for Add_Invariants
4575
 
4576
      begin
4577
         Ritem := First_Rep_Item (T);
4578
         while Present (Ritem) loop
4579
            if Nkind (Ritem) = N_Pragma
4580
              and then Pragma_Name (Ritem) = Name_Invariant
4581
            then
4582
               Arg1 := First (Pragma_Argument_Associations (Ritem));
4583
               Arg2 := Next (Arg1);
4584
               Arg3 := Next (Arg2);
4585
 
4586
               Arg1 := Get_Pragma_Arg (Arg1);
4587
               Arg2 := Get_Pragma_Arg (Arg2);
4588
 
4589
               --  For Inherit case, ignore Invariant, process only Class case
4590
 
4591
               if Inherit then
4592
                  if not Class_Present (Ritem) then
4593
                     goto Continue;
4594
                  end if;
4595
 
4596
               --  For Inherit false, process only item for right type
4597
 
4598
               else
4599
                  if Entity (Arg1) /= Typ then
4600
                     goto Continue;
4601
                  end if;
4602
               end if;
4603
 
4604
               if No (Stmts) then
4605
                  Stmts := Empty_List;
4606
               end if;
4607
 
4608
               Exp := New_Copy_Tree (Arg2);
4609
               Loc := Sloc (Exp);
4610
 
4611
               --  We need to replace any occurrences of the name of the type
4612
               --  with references to the object, converted to type'Class in
4613
               --  the case of Invariant'Class aspects.
4614
 
4615
               Replace_Type_References (Exp, Chars (T));
4616
 
4617
               --  If this invariant comes from an aspect, find the aspect
4618
               --  specification, and replace the saved expression because
4619
               --  we need the subtype references replaced for the calls to
4620
               --  Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
4621
               --  and Check_Aspect_At_End_Of_Declarations.
4622
 
4623
               if From_Aspect_Specification (Ritem) then
4624
                  declare
4625
                     Aitem : Node_Id;
4626
 
4627
                  begin
4628
                     --  Loop to find corresponding aspect, note that this
4629
                     --  must be present given the pragma is marked delayed.
4630
 
4631
                     Aitem := Next_Rep_Item (Ritem);
4632
                     while Present (Aitem) loop
4633
                        if Nkind (Aitem) = N_Aspect_Specification
4634
                          and then Aspect_Rep_Item (Aitem) = Ritem
4635
                        then
4636
                           Set_Entity
4637
                             (Identifier (Aitem), New_Copy_Tree (Exp));
4638
                           exit;
4639
                        end if;
4640
 
4641
                        Aitem := Next_Rep_Item (Aitem);
4642
                     end loop;
4643
                  end;
4644
               end if;
4645
 
4646
               --  Now we need to preanalyze the expression to properly capture
4647
               --  the visibility in the visible part. The expression will not
4648
               --  be analyzed for real until the body is analyzed, but that is
4649
               --  at the end of the private part and has the wrong visibility.
4650
 
4651
               Set_Parent (Exp, N);
4652
               Preanalyze_Spec_Expression (Exp, Standard_Boolean);
4653
 
4654
               --  Build first two arguments for Check pragma
4655
 
4656
               Assoc := New_List (
4657
                 Make_Pragma_Argument_Association (Loc,
4658
                   Expression => Make_Identifier (Loc, Name_Invariant)),
4659
                 Make_Pragma_Argument_Association (Loc, Expression => Exp));
4660
 
4661
               --  Add message if present in Invariant pragma
4662
 
4663
               if Present (Arg3) then
4664
                  Str := Strval (Get_Pragma_Arg (Arg3));
4665
 
4666
                  --  If inherited case, and message starts "failed invariant",
4667
                  --  change it to be "failed inherited invariant".
4668
 
4669
                  if Inherit then
4670
                     String_To_Name_Buffer (Str);
4671
 
4672
                     if Name_Buffer (1 .. 16) = "failed invariant" then
4673
                        Insert_Str_In_Name_Buffer ("inherited ", 8);
4674
                        Str := String_From_Name_Buffer;
4675
                     end if;
4676
                  end if;
4677
 
4678
                  Append_To (Assoc,
4679
                    Make_Pragma_Argument_Association (Loc,
4680
                      Expression => Make_String_Literal (Loc, Str)));
4681
               end if;
4682
 
4683
               --  Add Check pragma to list of statements
4684
 
4685
               Append_To (Stmts,
4686
                 Make_Pragma (Loc,
4687
                   Pragma_Identifier            =>
4688
                     Make_Identifier (Loc, Name_Check),
4689
                   Pragma_Argument_Associations => Assoc));
4690
 
4691
               --  If Inherited case and option enabled, output info msg. Note
4692
               --  that we know this is a case of Invariant'Class.
4693
 
4694
               if Inherit and Opt.List_Inherited_Aspects then
4695
                  Error_Msg_Sloc := Sloc (Ritem);
4696
                  Error_Msg_N
4697
                    ("?info: & inherits `Invariant''Class` aspect from #",
4698
                     Typ);
4699
               end if;
4700
            end if;
4701
 
4702
         <<Continue>>
4703
            Next_Rep_Item (Ritem);
4704
         end loop;
4705
      end Add_Invariants;
4706
 
4707
   --  Start of processing for Build_Invariant_Procedure
4708
 
4709
   begin
4710
      Stmts := No_List;
4711
      PDecl := Empty;
4712
      PBody := Empty;
4713
      Set_Etype (Object_Entity, Typ);
4714
 
4715
      --  Add invariants for the current type
4716
 
4717
      Add_Invariants (Typ, Inherit => False);
4718
 
4719
      --  Add invariants for parent types
4720
 
4721
      declare
4722
         Current_Typ : Entity_Id;
4723
         Parent_Typ  : Entity_Id;
4724
 
4725
      begin
4726
         Current_Typ := Typ;
4727
         loop
4728
            Parent_Typ := Etype (Current_Typ);
4729
 
4730
            if Is_Private_Type (Parent_Typ)
4731
              and then Present (Full_View (Base_Type (Parent_Typ)))
4732
            then
4733
               Parent_Typ := Full_View (Base_Type (Parent_Typ));
4734
            end if;
4735
 
4736
            exit when Parent_Typ = Current_Typ;
4737
 
4738
            Current_Typ := Parent_Typ;
4739
            Add_Invariants (Current_Typ, Inherit => True);
4740
         end loop;
4741
      end;
4742
 
4743
      --  Build the procedure if we generated at least one Check pragma
4744
 
4745
      if Stmts /= No_List then
4746
 
4747
         --  Build procedure declaration
4748
 
4749
         SId :=
4750
           Make_Defining_Identifier (Loc,
4751
             Chars => New_External_Name (Chars (Typ), "Invariant"));
4752
         Set_Has_Invariants (SId);
4753
         Set_Invariant_Procedure (Typ, SId);
4754
 
4755
         Spec :=
4756
           Make_Procedure_Specification (Loc,
4757
             Defining_Unit_Name       => SId,
4758
             Parameter_Specifications => New_List (
4759
               Make_Parameter_Specification (Loc,
4760
                 Defining_Identifier => Object_Entity,
4761
                 Parameter_Type      => New_Occurrence_Of (Typ, Loc))));
4762
 
4763
         PDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
4764
 
4765
         --  Build procedure body
4766
 
4767
         SId :=
4768
           Make_Defining_Identifier (Loc,
4769
             Chars => New_External_Name (Chars (Typ), "Invariant"));
4770
 
4771
         Spec :=
4772
           Make_Procedure_Specification (Loc,
4773
             Defining_Unit_Name       => SId,
4774
             Parameter_Specifications => New_List (
4775
               Make_Parameter_Specification (Loc,
4776
                 Defining_Identifier =>
4777
                   Make_Defining_Identifier (Loc, Object_Name),
4778
                 Parameter_Type => New_Occurrence_Of (Typ, Loc))));
4779
 
4780
         PBody :=
4781
           Make_Subprogram_Body (Loc,
4782
             Specification              => Spec,
4783
             Declarations               => Empty_List,
4784
             Handled_Statement_Sequence =>
4785
               Make_Handled_Sequence_Of_Statements (Loc,
4786
                 Statements => Stmts));
4787
 
4788
         --  Insert procedure declaration and spec at the appropriate points.
4789
         --  Skip this if there are no private declarations (that's an error
4790
         --  that will be diagnosed elsewhere, and there is no point in having
4791
         --  an invariant procedure set if the full declaration is missing).
4792
 
4793
         if Present (Private_Decls) then
4794
 
4795
            --  The spec goes at the end of visible declarations, but they have
4796
            --  already been analyzed, so we need to explicitly do the analyze.
4797
 
4798
            Append_To (Visible_Decls, PDecl);
4799
            Analyze (PDecl);
4800
 
4801
            --  The body goes at the end of the private declarations, which we
4802
            --  have not analyzed yet, so we do not need to perform an explicit
4803
            --  analyze call. We skip this if there are no private declarations
4804
            --  (this is an error that will be caught elsewhere);
4805
 
4806
            Append_To (Private_Decls, PBody);
4807
 
4808
            --  If the invariant appears on the full view of a type, the
4809
            --  analysis of the private part is complete, and we must
4810
            --  analyze the new body explicitly.
4811
 
4812
            if In_Private_Part (Current_Scope) then
4813
               Analyze (PBody);
4814
            end if;
4815
         end if;
4816
      end if;
4817
   end Build_Invariant_Procedure;
4818
 
4819
   ------------------------------
4820
   -- Build_Predicate_Function --
4821
   ------------------------------
4822
 
4823
   --  The procedure that is constructed here has the form
4824
 
4825
   --  function typPredicate (Ixxx : typ) return Boolean is
4826
   --  begin
4827
   --     return
4828
   --        exp1 and then exp2 and then ...
4829
   --        and then typ1Predicate (typ1 (Ixxx))
4830
   --        and then typ2Predicate (typ2 (Ixxx))
4831
   --        and then ...;
4832
   --  end typPredicate;
4833
 
4834
   --  Here exp1, and exp2 are expressions from Predicate pragmas. Note that
4835
   --  this is the point at which these expressions get analyzed, providing the
4836
   --  required delay, and typ1, typ2, are entities from which predicates are
4837
   --  inherited. Note that we do NOT generate Check pragmas, that's because we
4838
   --  use this function even if checks are off, e.g. for membership tests.
4839
 
4840
   procedure Build_Predicate_Function (Typ : Entity_Id; N : Node_Id) is
4841
      Loc  : constant Source_Ptr := Sloc (Typ);
4842
      Spec : Node_Id;
4843
      SId  : Entity_Id;
4844
      FDecl : Node_Id;
4845
      FBody : Node_Id;
4846
 
4847
      Expr : Node_Id;
4848
      --  This is the expression for the return statement in the function. It
4849
      --  is build by connecting the component predicates with AND THEN.
4850
 
4851
      procedure Add_Call (T : Entity_Id);
4852
      --  Includes a call to the predicate function for type T in Expr if T
4853
      --  has predicates and Predicate_Function (T) is non-empty.
4854
 
4855
      procedure Add_Predicates;
4856
      --  Appends expressions for any Predicate pragmas in the rep item chain
4857
      --  Typ to Expr. Note that we look only at items for this exact entity.
4858
      --  Inheritance of predicates for the parent type is done by calling the
4859
      --  Predicate_Function of the parent type, using Add_Call above.
4860
 
4861
      Object_Name : constant Name_Id := New_Internal_Name ('I');
4862
      --  Name for argument of Predicate procedure
4863
 
4864
      Object_Entity : constant Entity_Id :=
4865
                        Make_Defining_Identifier (Loc, Object_Name);
4866
      --  The entity for the spec entity for the argument
4867
 
4868
      Dynamic_Predicate_Present : Boolean := False;
4869
      --  Set True if a dynamic predicate is present, results in the entire
4870
      --  predicate being considered dynamic even if it looks static
4871
 
4872
      Static_Predicate_Present : Node_Id := Empty;
4873
      --  Set to N_Pragma node for a static predicate if one is encountered.
4874
 
4875
      --------------
4876
      -- Add_Call --
4877
      --------------
4878
 
4879
      procedure Add_Call (T : Entity_Id) is
4880
         Exp : Node_Id;
4881
 
4882
      begin
4883
         if Present (T) and then Present (Predicate_Function (T)) then
4884
            Set_Has_Predicates (Typ);
4885
 
4886
            --  Build the call to the predicate function of T
4887
 
4888
            Exp :=
4889
              Make_Predicate_Call
4890
                (T, Convert_To (T, Make_Identifier (Loc, Object_Name)));
4891
 
4892
            --  Add call to evolving expression, using AND THEN if needed
4893
 
4894
            if No (Expr) then
4895
               Expr := Exp;
4896
            else
4897
               Expr :=
4898
                 Make_And_Then (Loc,
4899
                   Left_Opnd  => Relocate_Node (Expr),
4900
                   Right_Opnd => Exp);
4901
            end if;
4902
 
4903
            --  Output info message on inheritance if required. Note we do not
4904
            --  give this information for generic actual types, since it is
4905
            --  unwelcome noise in that case in instantiations. We also
4906
            --  generally suppress the message in instantiations, and also
4907
            --  if it involves internal names.
4908
 
4909
            if Opt.List_Inherited_Aspects
4910
              and then not Is_Generic_Actual_Type (Typ)
4911
              and then Instantiation_Depth (Sloc (Typ)) = 0
4912
              and then not Is_Internal_Name (Chars (T))
4913
              and then not Is_Internal_Name (Chars (Typ))
4914
            then
4915
               Error_Msg_Sloc := Sloc (Predicate_Function (T));
4916
               Error_Msg_Node_2 := T;
4917
               Error_Msg_N ("?info: & inherits predicate from & #", Typ);
4918
            end if;
4919
         end if;
4920
      end Add_Call;
4921
 
4922
      --------------------
4923
      -- Add_Predicates --
4924
      --------------------
4925
 
4926
      procedure Add_Predicates is
4927
         Ritem : Node_Id;
4928
         Arg1  : Node_Id;
4929
         Arg2  : Node_Id;
4930
 
4931
         procedure Replace_Type_Reference (N : Node_Id);
4932
         --  Replace a single occurrence N of the subtype name with a reference
4933
         --  to the formal of the predicate function. N can be an identifier
4934
         --  referencing the subtype, or a selected component, representing an
4935
         --  appropriately qualified occurrence of the subtype name.
4936
 
4937
         procedure Replace_Type_References is
4938
           new Replace_Type_References_Generic (Replace_Type_Reference);
4939
         --  Traverse an expression changing every occurrence of an identifier
4940
         --  whose name matches the name of the subtype with a reference to
4941
         --  the formal parameter of the predicate function.
4942
 
4943
         ----------------------------
4944
         -- Replace_Type_Reference --
4945
         ----------------------------
4946
 
4947
         procedure Replace_Type_Reference (N : Node_Id) is
4948
         begin
4949
            Rewrite (N, Make_Identifier (Loc, Object_Name));
4950
            Set_Entity (N, Object_Entity);
4951
            Set_Etype (N, Typ);
4952
         end Replace_Type_Reference;
4953
 
4954
      --  Start of processing for Add_Predicates
4955
 
4956
      begin
4957
         Ritem := First_Rep_Item (Typ);
4958
         while Present (Ritem) loop
4959
            if Nkind (Ritem) = N_Pragma
4960
              and then Pragma_Name (Ritem) = Name_Predicate
4961
            then
4962
               if Present (Corresponding_Aspect (Ritem)) then
4963
                  case Chars (Identifier (Corresponding_Aspect (Ritem))) is
4964
                     when Name_Dynamic_Predicate =>
4965
                        Dynamic_Predicate_Present := True;
4966
                     when Name_Static_Predicate =>
4967
                        Static_Predicate_Present := Ritem;
4968
                     when others =>
4969
                        null;
4970
                  end case;
4971
               end if;
4972
 
4973
               --  Acquire arguments
4974
 
4975
               Arg1 := First (Pragma_Argument_Associations (Ritem));
4976
               Arg2 := Next (Arg1);
4977
 
4978
               Arg1 := Get_Pragma_Arg (Arg1);
4979
               Arg2 := Get_Pragma_Arg (Arg2);
4980
 
4981
               --  See if this predicate pragma is for the current type or for
4982
               --  its full view. A predicate on a private completion is placed
4983
               --  on the partial view beause this is the visible entity that
4984
               --  is frozen.
4985
 
4986
               if Entity (Arg1) = Typ
4987
                 or else Full_View (Entity (Arg1)) = Typ
4988
               then
4989
 
4990
                  --  We have a match, this entry is for our subtype
4991
 
4992
                  --  We need to replace any occurrences of the name of the
4993
                  --  type with references to the object.
4994
 
4995
                  Replace_Type_References (Arg2, Chars (Typ));
4996
 
4997
                  --  If this predicate comes from an aspect, find the aspect
4998
                  --  specification, and replace the saved expression because
4999
                  --  we need the subtype references replaced for the calls to
5000
                  --  Preanalyze_Spec_Expressin in Check_Aspect_At_Freeze_Point
5001
                  --  and Check_Aspect_At_End_Of_Declarations.
5002
 
5003
                  if From_Aspect_Specification (Ritem) then
5004
                     declare
5005
                        Aitem : Node_Id;
5006
 
5007
                     begin
5008
                        --  Loop to find corresponding aspect, note that this
5009
                        --  must be present given the pragma is marked delayed.
5010
 
5011
                        Aitem := Next_Rep_Item (Ritem);
5012
                        loop
5013
                           if Nkind (Aitem) = N_Aspect_Specification
5014
                             and then Aspect_Rep_Item (Aitem) = Ritem
5015
                           then
5016
                              Set_Entity
5017
                                (Identifier (Aitem), New_Copy_Tree (Arg2));
5018
                              exit;
5019
                           end if;
5020
 
5021
                           Aitem := Next_Rep_Item (Aitem);
5022
                        end loop;
5023
                     end;
5024
                  end if;
5025
 
5026
                  --  Now we can add the expression
5027
 
5028
                  if No (Expr) then
5029
                     Expr := Relocate_Node (Arg2);
5030
 
5031
                  --  There already was a predicate, so add to it
5032
 
5033
                  else
5034
                     Expr :=
5035
                       Make_And_Then (Loc,
5036
                         Left_Opnd  => Relocate_Node (Expr),
5037
                         Right_Opnd => Relocate_Node (Arg2));
5038
                  end if;
5039
               end if;
5040
            end if;
5041
 
5042
            Next_Rep_Item (Ritem);
5043
         end loop;
5044
      end Add_Predicates;
5045
 
5046
   --  Start of processing for Build_Predicate_Function
5047
 
5048
   begin
5049
      --  Initialize for construction of statement list
5050
 
5051
      Expr := Empty;
5052
 
5053
      --  Return if already built or if type does not have predicates
5054
 
5055
      if not Has_Predicates (Typ)
5056
        or else Present (Predicate_Function (Typ))
5057
      then
5058
         return;
5059
      end if;
5060
 
5061
      --  Add Predicates for the current type
5062
 
5063
      Add_Predicates;
5064
 
5065
      --  Add predicates for ancestor if present
5066
 
5067
      declare
5068
         Atyp : constant Entity_Id := Nearest_Ancestor (Typ);
5069
      begin
5070
         if Present (Atyp) then
5071
            Add_Call (Atyp);
5072
         end if;
5073
      end;
5074
 
5075
      --  If we have predicates, build the function
5076
 
5077
      if Present (Expr) then
5078
 
5079
         --  Build function declaration
5080
 
5081
         pragma Assert (Has_Predicates (Typ));
5082
         SId :=
5083
           Make_Defining_Identifier (Loc,
5084
             Chars => New_External_Name (Chars (Typ), "Predicate"));
5085
         Set_Has_Predicates (SId);
5086
         Set_Predicate_Function (Typ, SId);
5087
 
5088
         --  The predicate function is shared between views of a type.
5089
 
5090
         if Is_Private_Type (Typ) and then Present (Full_View (Typ)) then
5091
            Set_Predicate_Function (Full_View (Typ), SId);
5092
         end if;
5093
 
5094
         Spec :=
5095
           Make_Function_Specification (Loc,
5096
             Defining_Unit_Name       => SId,
5097
             Parameter_Specifications => New_List (
5098
               Make_Parameter_Specification (Loc,
5099
                 Defining_Identifier => Object_Entity,
5100
                 Parameter_Type      => New_Occurrence_Of (Typ, Loc))),
5101
             Result_Definition        =>
5102
               New_Occurrence_Of (Standard_Boolean, Loc));
5103
 
5104
         FDecl := Make_Subprogram_Declaration (Loc, Specification => Spec);
5105
 
5106
         --  Build function body
5107
 
5108
         SId :=
5109
           Make_Defining_Identifier (Loc,
5110
             Chars => New_External_Name (Chars (Typ), "Predicate"));
5111
 
5112
         Spec :=
5113
           Make_Function_Specification (Loc,
5114
             Defining_Unit_Name       => SId,
5115
             Parameter_Specifications => New_List (
5116
               Make_Parameter_Specification (Loc,
5117
                 Defining_Identifier =>
5118
                   Make_Defining_Identifier (Loc, Object_Name),
5119
                 Parameter_Type =>
5120
                   New_Occurrence_Of (Typ, Loc))),
5121
             Result_Definition        =>
5122
               New_Occurrence_Of (Standard_Boolean, Loc));
5123
 
5124
         FBody :=
5125
           Make_Subprogram_Body (Loc,
5126
             Specification              => Spec,
5127
             Declarations               => Empty_List,
5128
             Handled_Statement_Sequence =>
5129
               Make_Handled_Sequence_Of_Statements (Loc,
5130
                 Statements => New_List (
5131
                   Make_Simple_Return_Statement (Loc,
5132
                     Expression => Expr))));
5133
 
5134
         --  Insert declaration before freeze node and body after
5135
 
5136
         Insert_Before_And_Analyze (N, FDecl);
5137
         Insert_After_And_Analyze  (N, FBody);
5138
 
5139
         --  Deal with static predicate case
5140
 
5141
         if Ekind_In (Typ, E_Enumeration_Subtype,
5142
                           E_Modular_Integer_Subtype,
5143
                           E_Signed_Integer_Subtype)
5144
           and then Is_Static_Subtype (Typ)
5145
           and then not Dynamic_Predicate_Present
5146
         then
5147
            Build_Static_Predicate (Typ, Expr, Object_Name);
5148
 
5149
            if Present (Static_Predicate_Present)
5150
              and No (Static_Predicate (Typ))
5151
            then
5152
               Error_Msg_F
5153
                 ("expression does not have required form for "
5154
                  & "static predicate",
5155
                  Next (First (Pragma_Argument_Associations
5156
                                (Static_Predicate_Present))));
5157
            end if;
5158
         end if;
5159
      end if;
5160
   end Build_Predicate_Function;
5161
 
5162
   ----------------------------
5163
   -- Build_Static_Predicate --
5164
   ----------------------------
5165
 
5166
   procedure Build_Static_Predicate
5167
     (Typ  : Entity_Id;
5168
      Expr : Node_Id;
5169
      Nam  : Name_Id)
5170
   is
5171
      Loc : constant Source_Ptr := Sloc (Expr);
5172
 
5173
      Non_Static : exception;
5174
      --  Raised if something non-static is found
5175
 
5176
      Btyp : constant Entity_Id := Base_Type (Typ);
5177
 
5178
      BLo : constant Uint := Expr_Value (Type_Low_Bound  (Btyp));
5179
      BHi : constant Uint := Expr_Value (Type_High_Bound (Btyp));
5180
      --  Low bound and high bound value of base type of Typ
5181
 
5182
      TLo : constant Uint := Expr_Value (Type_Low_Bound  (Typ));
5183
      THi : constant Uint := Expr_Value (Type_High_Bound (Typ));
5184
      --  Low bound and high bound values of static subtype Typ
5185
 
5186
      type REnt is record
5187
         Lo, Hi : Uint;
5188
      end record;
5189
      --  One entry in a Rlist value, a single REnt (range entry) value
5190
      --  denotes one range from Lo to Hi. To represent a single value
5191
      --  range Lo = Hi = value.
5192
 
5193
      type RList is array (Nat range <>) of REnt;
5194
      --  A list of ranges. The ranges are sorted in increasing order,
5195
      --  and are disjoint (there is a gap of at least one value between
5196
      --  each range in the table). A value is in the set of ranges in
5197
      --  Rlist if it lies within one of these ranges
5198
 
5199
      False_Range : constant RList :=
5200
                      RList'(1 .. 0 => REnt'(No_Uint, No_Uint));
5201
      --  An empty set of ranges represents a range list that can never be
5202
      --  satisfied, since there are no ranges in which the value could lie,
5203
      --  so it does not lie in any of them. False_Range is a canonical value
5204
      --  for this empty set, but general processing should test for an Rlist
5205
      --  with length zero (see Is_False predicate), since other null ranges
5206
      --  may appear which must be treated as False.
5207
 
5208
      True_Range : constant RList := RList'(1 => REnt'(BLo, BHi));
5209
      --  Range representing True, value must be in the base range
5210
 
5211
      function "and" (Left, Right : RList) return RList;
5212
      --  And's together two range lists, returning a range list. This is
5213
      --  a set intersection operation.
5214
 
5215
      function "or" (Left, Right : RList) return RList;
5216
      --  Or's together two range lists, returning a range list. This is a
5217
      --  set union operation.
5218
 
5219
      function "not" (Right : RList) return RList;
5220
      --  Returns complement of a given range list, i.e. a range list
5221
      --  representing all the values in TLo .. THi that are not in the
5222
      --  input operand Right.
5223
 
5224
      function Build_Val (V : Uint) return Node_Id;
5225
      --  Return an analyzed N_Identifier node referencing this value, suitable
5226
      --  for use as an entry in the Static_Predicate list. This node is typed
5227
      --  with the base type.
5228
 
5229
      function Build_Range (Lo, Hi : Uint) return Node_Id;
5230
      --  Return an analyzed N_Range node referencing this range, suitable
5231
      --  for use as an entry in the Static_Predicate list. This node is typed
5232
      --  with the base type.
5233
 
5234
      function Get_RList (Exp : Node_Id) return RList;
5235
      --  This is a recursive routine that converts the given expression into
5236
      --  a list of ranges, suitable for use in building the static predicate.
5237
 
5238
      function Is_False (R : RList) return Boolean;
5239
      pragma Inline (Is_False);
5240
      --  Returns True if the given range list is empty, and thus represents
5241
      --  a False list of ranges that can never be satisfied.
5242
 
5243
      function Is_True (R : RList) return Boolean;
5244
      --  Returns True if R trivially represents the True predicate by having
5245
      --  a single range from BLo to BHi.
5246
 
5247
      function Is_Type_Ref (N : Node_Id) return Boolean;
5248
      pragma Inline (Is_Type_Ref);
5249
      --  Returns if True if N is a reference to the type for the predicate in
5250
      --  the expression (i.e. if it is an identifier whose Chars field matches
5251
      --  the Nam given in the call).
5252
 
5253
      function Lo_Val (N : Node_Id) return Uint;
5254
      --  Given static expression or static range from a Static_Predicate list,
5255
      --  gets expression value or low bound of range.
5256
 
5257
      function Hi_Val (N : Node_Id) return Uint;
5258
      --  Given static expression or static range from a Static_Predicate list,
5259
      --  gets expression value of high bound of range.
5260
 
5261
      function Membership_Entry (N : Node_Id) return RList;
5262
      --  Given a single membership entry (range, value, or subtype), returns
5263
      --  the corresponding range list. Raises Static_Error if not static.
5264
 
5265
      function Membership_Entries (N : Node_Id) return RList;
5266
      --  Given an element on an alternatives list of a membership operation,
5267
      --  returns the range list corresponding to this entry and all following
5268
      --  entries (i.e. returns the "or" of this list of values).
5269
 
5270
      function Stat_Pred (Typ : Entity_Id) return RList;
5271
      --  Given a type, if it has a static predicate, then return the predicate
5272
      --  as a range list, otherwise raise Non_Static.
5273
 
5274
      -----------
5275
      -- "and" --
5276
      -----------
5277
 
5278
      function "and" (Left, Right : RList) return RList is
5279
         FEnt : REnt;
5280
         --  First range of result
5281
 
5282
         SLeft : Nat := Left'First;
5283
         --  Start of rest of left entries
5284
 
5285
         SRight : Nat := Right'First;
5286
         --  Start of rest of right entries
5287
 
5288
      begin
5289
         --  If either range is True, return the other
5290
 
5291
         if Is_True (Left) then
5292
            return Right;
5293
         elsif Is_True (Right) then
5294
            return Left;
5295
         end if;
5296
 
5297
         --  If either range is False, return False
5298
 
5299
         if Is_False (Left) or else Is_False (Right) then
5300
            return False_Range;
5301
         end if;
5302
 
5303
         --  Loop to remove entries at start that are disjoint, and thus
5304
         --  just get discarded from the result entirely.
5305
 
5306
         loop
5307
            --  If no operands left in either operand, result is false
5308
 
5309
            if SLeft > Left'Last or else SRight > Right'Last then
5310
               return False_Range;
5311
 
5312
            --  Discard first left operand entry if disjoint with right
5313
 
5314
            elsif Left (SLeft).Hi < Right (SRight).Lo then
5315
               SLeft := SLeft + 1;
5316
 
5317
            --  Discard first right operand entry if disjoint with left
5318
 
5319
            elsif Right (SRight).Hi < Left (SLeft).Lo then
5320
               SRight := SRight + 1;
5321
 
5322
            --  Otherwise we have an overlapping entry
5323
 
5324
            else
5325
               exit;
5326
            end if;
5327
         end loop;
5328
 
5329
         --  Now we have two non-null operands, and first entries overlap.
5330
         --  The first entry in the result will be the overlapping part of
5331
         --  these two entries.
5332
 
5333
         FEnt := REnt'(Lo => UI_Max (Left (SLeft).Lo, Right (SRight).Lo),
5334
                       Hi => UI_Min (Left (SLeft).Hi, Right (SRight).Hi));
5335
 
5336
         --  Now we can remove the entry that ended at a lower value, since
5337
         --  its contribution is entirely contained in Fent.
5338
 
5339
         if Left (SLeft).Hi <= Right (SRight).Hi then
5340
            SLeft := SLeft + 1;
5341
         else
5342
            SRight := SRight + 1;
5343
         end if;
5344
 
5345
         --  Compute result by concatenating this first entry with the "and"
5346
         --  of the remaining parts of the left and right operands. Note that
5347
         --  if either of these is empty, "and" will yield empty, so that we
5348
         --  will end up with just Fent, which is what we want in that case.
5349
 
5350
         return
5351
           FEnt & (Left (SLeft .. Left'Last) and Right (SRight .. Right'Last));
5352
      end "and";
5353
 
5354
      -----------
5355
      -- "not" --
5356
      -----------
5357
 
5358
      function "not" (Right : RList) return RList is
5359
      begin
5360
         --  Return True if False range
5361
 
5362
         if Is_False (Right) then
5363
            return True_Range;
5364
         end if;
5365
 
5366
         --  Return False if True range
5367
 
5368
         if Is_True (Right) then
5369
            return False_Range;
5370
         end if;
5371
 
5372
         --  Here if not trivial case
5373
 
5374
         declare
5375
            Result : RList (1 .. Right'Length + 1);
5376
            --  May need one more entry for gap at beginning and end
5377
 
5378
            Count : Nat := 0;
5379
            --  Number of entries stored in Result
5380
 
5381
         begin
5382
            --  Gap at start
5383
 
5384
            if Right (Right'First).Lo > TLo then
5385
               Count := Count + 1;
5386
               Result (Count) := REnt'(TLo, Right (Right'First).Lo - 1);
5387
            end if;
5388
 
5389
            --  Gaps between ranges
5390
 
5391
            for J in Right'First .. Right'Last - 1 loop
5392
               Count := Count + 1;
5393
               Result (Count) :=
5394
                 REnt'(Right (J).Hi + 1, Right (J + 1).Lo - 1);
5395
            end loop;
5396
 
5397
            --  Gap at end
5398
 
5399
            if Right (Right'Last).Hi < THi then
5400
               Count := Count + 1;
5401
               Result (Count) := REnt'(Right (Right'Last).Hi + 1, THi);
5402
            end if;
5403
 
5404
            return Result (1 .. Count);
5405
         end;
5406
      end "not";
5407
 
5408
      ----------
5409
      -- "or" --
5410
      ----------
5411
 
5412
      function "or" (Left, Right : RList) return RList is
5413
         FEnt : REnt;
5414
         --  First range of result
5415
 
5416
         SLeft : Nat := Left'First;
5417
         --  Start of rest of left entries
5418
 
5419
         SRight : Nat := Right'First;
5420
         --  Start of rest of right entries
5421
 
5422
      begin
5423
         --  If either range is True, return True
5424
 
5425
         if Is_True (Left) or else Is_True (Right) then
5426
            return True_Range;
5427
         end if;
5428
 
5429
         --  If either range is False (empty), return the other
5430
 
5431
         if Is_False (Left) then
5432
            return Right;
5433
         elsif Is_False (Right) then
5434
            return Left;
5435
         end if;
5436
 
5437
         --  Initialize result first entry from left or right operand
5438
         --  depending on which starts with the lower range.
5439
 
5440
         if Left (SLeft).Lo < Right (SRight).Lo then
5441
            FEnt := Left (SLeft);
5442
            SLeft := SLeft + 1;
5443
         else
5444
            FEnt := Right (SRight);
5445
            SRight := SRight + 1;
5446
         end if;
5447
 
5448
         --  This loop eats ranges from left and right operands that
5449
         --  are contiguous with the first range we are gathering.
5450
 
5451
         loop
5452
            --  Eat first entry in left operand if contiguous or
5453
            --  overlapped by gathered first operand of result.
5454
 
5455
            if SLeft <= Left'Last
5456
              and then Left (SLeft).Lo <= FEnt.Hi + 1
5457
            then
5458
               FEnt.Hi := UI_Max (FEnt.Hi, Left (SLeft).Hi);
5459
               SLeft := SLeft + 1;
5460
 
5461
               --  Eat first entry in right operand if contiguous or
5462
               --  overlapped by gathered right operand of result.
5463
 
5464
            elsif SRight <= Right'Last
5465
              and then Right (SRight).Lo <= FEnt.Hi + 1
5466
            then
5467
               FEnt.Hi := UI_Max (FEnt.Hi, Right (SRight).Hi);
5468
               SRight := SRight + 1;
5469
 
5470
               --  All done if no more entries to eat!
5471
 
5472
            else
5473
               exit;
5474
            end if;
5475
         end loop;
5476
 
5477
         --  Obtain result as the first entry we just computed, concatenated
5478
         --  to the "or" of the remaining results (if one operand is empty,
5479
         --  this will just concatenate with the other
5480
 
5481
         return
5482
           FEnt & (Left (SLeft .. Left'Last) or Right (SRight .. Right'Last));
5483
      end "or";
5484
 
5485
      -----------------
5486
      -- Build_Range --
5487
      -----------------
5488
 
5489
      function Build_Range (Lo, Hi : Uint) return Node_Id is
5490
         Result : Node_Id;
5491
      begin
5492
         if Lo = Hi then
5493
            return Build_Val (Hi);
5494
         else
5495
            Result :=
5496
              Make_Range (Loc,
5497
                Low_Bound  => Build_Val (Lo),
5498
                High_Bound => Build_Val (Hi));
5499
            Set_Etype (Result, Btyp);
5500
            Set_Analyzed (Result);
5501
            return Result;
5502
         end if;
5503
      end Build_Range;
5504
 
5505
      ---------------
5506
      -- Build_Val --
5507
      ---------------
5508
 
5509
      function Build_Val (V : Uint) return Node_Id is
5510
         Result : Node_Id;
5511
 
5512
      begin
5513
         if Is_Enumeration_Type (Typ) then
5514
            Result := Get_Enum_Lit_From_Pos (Typ, V, Loc);
5515
         else
5516
            Result := Make_Integer_Literal (Loc, V);
5517
         end if;
5518
 
5519
         Set_Etype (Result, Btyp);
5520
         Set_Is_Static_Expression (Result);
5521
         Set_Analyzed (Result);
5522
         return Result;
5523
      end Build_Val;
5524
 
5525
      ---------------
5526
      -- Get_RList --
5527
      ---------------
5528
 
5529
      function Get_RList (Exp : Node_Id) return RList is
5530
         Op  : Node_Kind;
5531
         Val : Uint;
5532
 
5533
      begin
5534
         --  Static expression can only be true or false
5535
 
5536
         if Is_OK_Static_Expression (Exp) then
5537
 
5538
            --  For False
5539
 
5540
            if Expr_Value (Exp) = 0 then
5541
               return False_Range;
5542
            else
5543
               return True_Range;
5544
            end if;
5545
         end if;
5546
 
5547
         --  Otherwise test node type
5548
 
5549
         Op := Nkind (Exp);
5550
 
5551
         case Op is
5552
 
5553
            --  And
5554
 
5555
            when N_Op_And | N_And_Then =>
5556
               return Get_RList (Left_Opnd (Exp))
5557
                        and
5558
                      Get_RList (Right_Opnd (Exp));
5559
 
5560
            --  Or
5561
 
5562
            when N_Op_Or | N_Or_Else =>
5563
               return Get_RList (Left_Opnd (Exp))
5564
                        or
5565
                      Get_RList (Right_Opnd (Exp));
5566
 
5567
            --  Not
5568
 
5569
            when N_Op_Not =>
5570
               return not Get_RList (Right_Opnd (Exp));
5571
 
5572
            --  Comparisons of type with static value
5573
 
5574
            when N_Op_Compare =>
5575
               --  Type is left operand
5576
 
5577
               if Is_Type_Ref (Left_Opnd (Exp))
5578
                 and then Is_OK_Static_Expression (Right_Opnd (Exp))
5579
               then
5580
                  Val := Expr_Value (Right_Opnd (Exp));
5581
 
5582
                  --  Typ is right operand
5583
 
5584
               elsif Is_Type_Ref (Right_Opnd (Exp))
5585
                 and then Is_OK_Static_Expression (Left_Opnd (Exp))
5586
               then
5587
                  Val := Expr_Value (Left_Opnd (Exp));
5588
 
5589
                  --  Invert sense of comparison
5590
 
5591
                  case Op is
5592
                     when N_Op_Gt => Op := N_Op_Lt;
5593
                     when N_Op_Lt => Op := N_Op_Gt;
5594
                     when N_Op_Ge => Op := N_Op_Le;
5595
                     when N_Op_Le => Op := N_Op_Ge;
5596
                     when others  => null;
5597
                  end case;
5598
 
5599
                  --  Other cases are non-static
5600
 
5601
               else
5602
                  raise Non_Static;
5603
               end if;
5604
 
5605
               --  Construct range according to comparison operation
5606
 
5607
               case Op is
5608
                  when N_Op_Eq =>
5609
                     return RList'(1 => REnt'(Val, Val));
5610
 
5611
                  when N_Op_Ge =>
5612
                     return RList'(1 => REnt'(Val, BHi));
5613
 
5614
                  when N_Op_Gt =>
5615
                     return RList'(1 => REnt'(Val + 1, BHi));
5616
 
5617
                  when N_Op_Le =>
5618
                     return RList'(1 => REnt'(BLo, Val));
5619
 
5620
                  when N_Op_Lt =>
5621
                     return RList'(1 => REnt'(BLo, Val - 1));
5622
 
5623
                  when N_Op_Ne =>
5624
                     return RList'(REnt'(BLo, Val - 1),
5625
                                   REnt'(Val + 1, BHi));
5626
 
5627
                  when others  =>
5628
                     raise Program_Error;
5629
               end case;
5630
 
5631
            --  Membership (IN)
5632
 
5633
            when N_In =>
5634
               if not Is_Type_Ref (Left_Opnd (Exp)) then
5635
                  raise Non_Static;
5636
               end if;
5637
 
5638
               if Present (Right_Opnd (Exp)) then
5639
                  return Membership_Entry (Right_Opnd (Exp));
5640
               else
5641
                  return Membership_Entries (First (Alternatives (Exp)));
5642
               end if;
5643
 
5644
            --  Negative membership (NOT IN)
5645
 
5646
            when N_Not_In =>
5647
               if not Is_Type_Ref (Left_Opnd (Exp)) then
5648
                  raise Non_Static;
5649
               end if;
5650
 
5651
               if Present (Right_Opnd (Exp)) then
5652
                  return not Membership_Entry (Right_Opnd (Exp));
5653
               else
5654
                  return not Membership_Entries (First (Alternatives (Exp)));
5655
               end if;
5656
 
5657
            --  Function call, may be call to static predicate
5658
 
5659
            when N_Function_Call =>
5660
               if Is_Entity_Name (Name (Exp)) then
5661
                  declare
5662
                     Ent : constant Entity_Id := Entity (Name (Exp));
5663
                  begin
5664
                     if Has_Predicates (Ent) then
5665
                        return Stat_Pred (Etype (First_Formal (Ent)));
5666
                     end if;
5667
                  end;
5668
               end if;
5669
 
5670
               --  Other function call cases are non-static
5671
 
5672
               raise Non_Static;
5673
 
5674
            --  Qualified expression, dig out the expression
5675
 
5676
            when N_Qualified_Expression =>
5677
               return Get_RList (Expression (Exp));
5678
 
5679
            --  Xor operator
5680
 
5681
            when N_Op_Xor =>
5682
               return (Get_RList (Left_Opnd (Exp))
5683
                        and not Get_RList (Right_Opnd (Exp)))
5684
                 or   (Get_RList (Right_Opnd (Exp))
5685
                        and not Get_RList (Left_Opnd (Exp)));
5686
 
5687
            --  Any other node type is non-static
5688
 
5689
            when others =>
5690
               raise Non_Static;
5691
         end case;
5692
      end Get_RList;
5693
 
5694
      ------------
5695
      -- Hi_Val --
5696
      ------------
5697
 
5698
      function Hi_Val (N : Node_Id) return Uint is
5699
      begin
5700
         if Is_Static_Expression (N) then
5701
            return Expr_Value (N);
5702
         else
5703
            pragma Assert (Nkind (N) = N_Range);
5704
            return Expr_Value (High_Bound (N));
5705
         end if;
5706
      end Hi_Val;
5707
 
5708
      --------------
5709
      -- Is_False --
5710
      --------------
5711
 
5712
      function Is_False (R : RList) return Boolean is
5713
      begin
5714
         return R'Length = 0;
5715
      end Is_False;
5716
 
5717
      -------------
5718
      -- Is_True --
5719
      -------------
5720
 
5721
      function Is_True (R : RList) return Boolean is
5722
      begin
5723
         return R'Length = 1
5724
           and then R (R'First).Lo = BLo
5725
           and then R (R'First).Hi = BHi;
5726
      end Is_True;
5727
 
5728
      -----------------
5729
      -- Is_Type_Ref --
5730
      -----------------
5731
 
5732
      function Is_Type_Ref (N : Node_Id) return Boolean is
5733
      begin
5734
         return Nkind (N) = N_Identifier and then Chars (N) = Nam;
5735
      end Is_Type_Ref;
5736
 
5737
      ------------
5738
      -- Lo_Val --
5739
      ------------
5740
 
5741
      function Lo_Val (N : Node_Id) return Uint is
5742
      begin
5743
         if Is_Static_Expression (N) then
5744
            return Expr_Value (N);
5745
         else
5746
            pragma Assert (Nkind (N) = N_Range);
5747
            return Expr_Value (Low_Bound (N));
5748
         end if;
5749
      end Lo_Val;
5750
 
5751
      ------------------------
5752
      -- Membership_Entries --
5753
      ------------------------
5754
 
5755
      function Membership_Entries (N : Node_Id) return RList is
5756
      begin
5757
         if No (Next (N)) then
5758
            return Membership_Entry (N);
5759
         else
5760
            return Membership_Entry (N) or Membership_Entries (Next (N));
5761
         end if;
5762
      end Membership_Entries;
5763
 
5764
      ----------------------
5765
      -- Membership_Entry --
5766
      ----------------------
5767
 
5768
      function Membership_Entry (N : Node_Id) return RList is
5769
         Val : Uint;
5770
         SLo : Uint;
5771
         SHi : Uint;
5772
 
5773
      begin
5774
         --  Range case
5775
 
5776
         if Nkind (N) = N_Range then
5777
            if not Is_Static_Expression (Low_Bound (N))
5778
                 or else
5779
               not Is_Static_Expression (High_Bound (N))
5780
            then
5781
               raise Non_Static;
5782
            else
5783
               SLo := Expr_Value (Low_Bound  (N));
5784
               SHi := Expr_Value (High_Bound (N));
5785
               return RList'(1 => REnt'(SLo, SHi));
5786
            end if;
5787
 
5788
         --  Static expression case
5789
 
5790
         elsif Is_Static_Expression (N) then
5791
            Val := Expr_Value (N);
5792
            return RList'(1 => REnt'(Val, Val));
5793
 
5794
         --  Identifier (other than static expression) case
5795
 
5796
         else pragma Assert (Nkind (N) = N_Identifier);
5797
 
5798
            --  Type case
5799
 
5800
            if Is_Type (Entity (N)) then
5801
 
5802
               --  If type has predicates, process them
5803
 
5804
               if Has_Predicates (Entity (N)) then
5805
                  return Stat_Pred (Entity (N));
5806
 
5807
               --  For static subtype without predicates, get range
5808
 
5809
               elsif Is_Static_Subtype (Entity (N)) then
5810
                  SLo := Expr_Value (Type_Low_Bound  (Entity (N)));
5811
                  SHi := Expr_Value (Type_High_Bound (Entity (N)));
5812
                  return RList'(1 => REnt'(SLo, SHi));
5813
 
5814
               --  Any other type makes us non-static
5815
 
5816
               else
5817
                  raise Non_Static;
5818
               end if;
5819
 
5820
            --  Any other kind of identifier in predicate (e.g. a non-static
5821
            --  expression value) means this is not a static predicate.
5822
 
5823
            else
5824
               raise Non_Static;
5825
            end if;
5826
         end if;
5827
      end Membership_Entry;
5828
 
5829
      ---------------
5830
      -- Stat_Pred --
5831
      ---------------
5832
 
5833
      function Stat_Pred (Typ : Entity_Id) return RList is
5834
      begin
5835
         --  Not static if type does not have static predicates
5836
 
5837
         if not Has_Predicates (Typ)
5838
           or else No (Static_Predicate (Typ))
5839
         then
5840
            raise Non_Static;
5841
         end if;
5842
 
5843
         --  Otherwise we convert the predicate list to a range list
5844
 
5845
         declare
5846
            Result : RList (1 .. List_Length (Static_Predicate (Typ)));
5847
            P      : Node_Id;
5848
 
5849
         begin
5850
            P := First (Static_Predicate (Typ));
5851
            for J in Result'Range loop
5852
               Result (J) := REnt'(Lo_Val (P), Hi_Val (P));
5853
               Next (P);
5854
            end loop;
5855
 
5856
            return Result;
5857
         end;
5858
      end Stat_Pred;
5859
 
5860
   --  Start of processing for Build_Static_Predicate
5861
 
5862
   begin
5863
      --  Now analyze the expression to see if it is a static predicate
5864
 
5865
      declare
5866
         Ranges : constant RList := Get_RList (Expr);
5867
         --  Range list from expression if it is static
5868
 
5869
         Plist : List_Id;
5870
 
5871
      begin
5872
         --  Convert range list into a form for the static predicate. In the
5873
         --  Ranges array, we just have raw ranges, these must be converted
5874
         --  to properly typed and analyzed static expressions or range nodes.
5875
 
5876
         --  Note: here we limit ranges to the ranges of the subtype, so that
5877
         --  a predicate is always false for values outside the subtype. That
5878
         --  seems fine, such values are invalid anyway, and considering them
5879
         --  to fail the predicate seems allowed and friendly, and furthermore
5880
         --  simplifies processing for case statements and loops.
5881
 
5882
         Plist := New_List;
5883
 
5884
         for J in Ranges'Range loop
5885
            declare
5886
               Lo : Uint := Ranges (J).Lo;
5887
               Hi : Uint := Ranges (J).Hi;
5888
 
5889
            begin
5890
               --  Ignore completely out of range entry
5891
 
5892
               if Hi < TLo or else Lo > THi then
5893
                  null;
5894
 
5895
                  --  Otherwise process entry
5896
 
5897
               else
5898
                  --  Adjust out of range value to subtype range
5899
 
5900
                  if Lo < TLo then
5901
                     Lo := TLo;
5902
                  end if;
5903
 
5904
                  if Hi > THi then
5905
                     Hi := THi;
5906
                  end if;
5907
 
5908
                  --  Convert range into required form
5909
 
5910
                  if Lo = Hi then
5911
                     Append_To (Plist, Build_Val (Lo));
5912
                  else
5913
                     Append_To (Plist, Build_Range (Lo, Hi));
5914
                  end if;
5915
               end if;
5916
            end;
5917
         end loop;
5918
 
5919
         --  Processing was successful and all entries were static, so now we
5920
         --  can store the result as the predicate list.
5921
 
5922
         Set_Static_Predicate (Typ, Plist);
5923
 
5924
         --  The processing for static predicates put the expression into
5925
         --  canonical form as a series of ranges. It also eliminated
5926
         --  duplicates and collapsed and combined ranges. We might as well
5927
         --  replace the alternatives list of the right operand of the
5928
         --  membership test with the static predicate list, which will
5929
         --  usually be more efficient.
5930
 
5931
         declare
5932
            New_Alts : constant List_Id := New_List;
5933
            Old_Node : Node_Id;
5934
            New_Node : Node_Id;
5935
 
5936
         begin
5937
            Old_Node := First (Plist);
5938
            while Present (Old_Node) loop
5939
               New_Node := New_Copy (Old_Node);
5940
 
5941
               if Nkind (New_Node) = N_Range then
5942
                  Set_Low_Bound  (New_Node, New_Copy (Low_Bound  (Old_Node)));
5943
                  Set_High_Bound (New_Node, New_Copy (High_Bound (Old_Node)));
5944
               end if;
5945
 
5946
               Append_To (New_Alts, New_Node);
5947
               Next (Old_Node);
5948
            end loop;
5949
 
5950
            --  If empty list, replace by False
5951
 
5952
            if Is_Empty_List (New_Alts) then
5953
               Rewrite (Expr, New_Occurrence_Of (Standard_False, Loc));
5954
 
5955
            --  Else replace by set membership test
5956
 
5957
            else
5958
               Rewrite (Expr,
5959
                 Make_In (Loc,
5960
                   Left_Opnd    => Make_Identifier (Loc, Nam),
5961
                   Right_Opnd   => Empty,
5962
                   Alternatives => New_Alts));
5963
 
5964
               --  Resolve new expression in function context
5965
 
5966
               Install_Formals (Predicate_Function (Typ));
5967
               Push_Scope (Predicate_Function (Typ));
5968
               Analyze_And_Resolve (Expr, Standard_Boolean);
5969
               Pop_Scope;
5970
            end if;
5971
         end;
5972
      end;
5973
 
5974
   --  If non-static, return doing nothing
5975
 
5976
   exception
5977
      when Non_Static =>
5978
         return;
5979
   end Build_Static_Predicate;
5980
 
5981
   -----------------------------------------
5982
   -- Check_Aspect_At_End_Of_Declarations --
5983
   -----------------------------------------
5984
 
5985
   procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id) is
5986
      Ent   : constant Entity_Id := Entity     (ASN);
5987
      Ident : constant Node_Id   := Identifier (ASN);
5988
 
5989
      Freeze_Expr : constant Node_Id := Expression (ASN);
5990
      --  Expression from call to Check_Aspect_At_Freeze_Point
5991
 
5992
      End_Decl_Expr : constant Node_Id := Entity (Ident);
5993
      --  Expression to be analyzed at end of declarations
5994
 
5995
      T : constant Entity_Id := Etype (Freeze_Expr);
5996
      --  Type required for preanalyze call
5997
 
5998
      A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
5999
 
6000
      Err : Boolean;
6001
      --  Set False if error
6002
 
6003
      --  On entry to this procedure, Entity (Ident) contains a copy of the
6004
      --  original expression from the aspect, saved for this purpose, and
6005
      --  but Expression (Ident) is a preanalyzed copy of the expression,
6006
      --  preanalyzed just after the freeze point.
6007
 
6008
   begin
6009
      --  Case of stream attributes, just have to compare entities
6010
 
6011
      if A_Id = Aspect_Input  or else
6012
         A_Id = Aspect_Output or else
6013
         A_Id = Aspect_Read   or else
6014
         A_Id = Aspect_Write
6015
      then
6016
         Analyze (End_Decl_Expr);
6017
         Err := Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
6018
 
6019
      elsif A_Id = Aspect_Variable_Indexing or else
6020
            A_Id = Aspect_Constant_Indexing or else
6021
            A_Id = Aspect_Default_Iterator  or else
6022
            A_Id = Aspect_Iterator_Element
6023
      then
6024
         --  Make type unfrozen before analysis, to prevent spurious errors
6025
         --  about late attributes.
6026
 
6027
         Set_Is_Frozen (Ent, False);
6028
         Analyze (End_Decl_Expr);
6029
         Analyze (Aspect_Rep_Item (ASN));
6030
         Set_Is_Frozen (Ent, True);
6031
 
6032
         --  If the end of declarations comes before any other freeze
6033
         --  point, the Freeze_Expr is not analyzed: no check needed.
6034
 
6035
         Err :=
6036
           Analyzed (Freeze_Expr)
6037
             and then not In_Instance
6038
             and then Entity (End_Decl_Expr) /= Entity (Freeze_Expr);
6039
 
6040
      --  All other cases
6041
 
6042
      else
6043
         --  In a generic context the aspect expressions have not been
6044
         --  preanalyzed, so do it now. There are no conformance checks
6045
         --  to perform in this case.
6046
 
6047
         if No (T) then
6048
            Check_Aspect_At_Freeze_Point (ASN);
6049
            return;
6050
 
6051
         --  The default values attributes may be defined in the private part,
6052
         --  and the analysis of the expression may take place when only the
6053
         --  partial view is visible. The expression must be scalar, so use
6054
         --  the full view to resolve.
6055
 
6056
         elsif (A_Id = Aspect_Default_Value
6057
                  or else
6058
                A_Id = Aspect_Default_Component_Value)
6059
            and then Is_Private_Type (T)
6060
         then
6061
            Preanalyze_Spec_Expression (End_Decl_Expr, Full_View (T));
6062
         else
6063
            Preanalyze_Spec_Expression (End_Decl_Expr, T);
6064
         end if;
6065
 
6066
         Err := not Fully_Conformant_Expressions (End_Decl_Expr, Freeze_Expr);
6067
      end if;
6068
 
6069
      --  Output error message if error
6070
 
6071
      if Err then
6072
         Error_Msg_NE
6073
           ("visibility of aspect for& changes after freeze point",
6074
            ASN, Ent);
6075
         Error_Msg_NE
6076
           ("?info: & is frozen here, aspects evaluated at this point",
6077
            Freeze_Node (Ent), Ent);
6078
      end if;
6079
   end Check_Aspect_At_End_Of_Declarations;
6080
 
6081
   ----------------------------------
6082
   -- Check_Aspect_At_Freeze_Point --
6083
   ----------------------------------
6084
 
6085
   procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id) is
6086
      Ident : constant Node_Id := Identifier (ASN);
6087
      --  Identifier (use Entity field to save expression)
6088
 
6089
      T : Entity_Id;
6090
      --  Type required for preanalyze call
6091
 
6092
      A_Id : constant Aspect_Id := Get_Aspect_Id (Chars (Ident));
6093
 
6094
   begin
6095
      --  On entry to this procedure, Entity (Ident) contains a copy of the
6096
      --  original expression from the aspect, saved for this purpose.
6097
 
6098
      --  On exit from this procedure Entity (Ident) is unchanged, still
6099
      --  containing that copy, but Expression (Ident) is a preanalyzed copy
6100
      --  of the expression, preanalyzed just after the freeze point.
6101
 
6102
      --  Make a copy of the expression to be preanalyed
6103
 
6104
      Set_Expression (ASN, New_Copy_Tree (Entity (Ident)));
6105
 
6106
      --  Find type for preanalyze call
6107
 
6108
      case A_Id is
6109
 
6110
         --  No_Aspect should be impossible
6111
 
6112
         when No_Aspect =>
6113
            raise Program_Error;
6114
 
6115
         --  Library unit aspects should be impossible (never delayed)
6116
 
6117
         when Library_Unit_Aspects =>
6118
            raise Program_Error;
6119
 
6120
         --  Aspects taking an optional boolean argument. Should be impossible
6121
         --  since these are never delayed.
6122
 
6123
         when Boolean_Aspects =>
6124
            raise Program_Error;
6125
 
6126
         --  Test_Case aspect applies to entries and subprograms, hence should
6127
         --  never be delayed.
6128
 
6129
         when Aspect_Test_Case =>
6130
            raise Program_Error;
6131
 
6132
         when Aspect_Attach_Handler =>
6133
            T := RTE (RE_Interrupt_ID);
6134
 
6135
         --  Default_Value is resolved with the type entity in question
6136
 
6137
         when Aspect_Default_Value =>
6138
            T := Entity (ASN);
6139
 
6140
         --  Default_Component_Value is resolved with the component type
6141
 
6142
         when Aspect_Default_Component_Value =>
6143
            T := Component_Type (Entity (ASN));
6144
 
6145
         --  Aspects corresponding to attribute definition clauses
6146
 
6147
         when Aspect_Address =>
6148
            T := RTE (RE_Address);
6149
 
6150
         when Aspect_Bit_Order =>
6151
            T := RTE (RE_Bit_Order);
6152
 
6153
         when Aspect_CPU =>
6154
            T := RTE (RE_CPU_Range);
6155
 
6156
         when Aspect_Dispatching_Domain =>
6157
            T := RTE (RE_Dispatching_Domain);
6158
 
6159
         when Aspect_External_Tag =>
6160
            T := Standard_String;
6161
 
6162
         when Aspect_Priority | Aspect_Interrupt_Priority =>
6163
            T := Standard_Integer;
6164
 
6165
         when Aspect_Small =>
6166
            T := Universal_Real;
6167
 
6168
         --  For a simple storage pool, we have to retrieve the type of the
6169
         --  pool object associated with the aspect's corresponding attribute
6170
         --  definition clause.
6171
 
6172
         when Aspect_Simple_Storage_Pool =>
6173
            T := Etype (Expression (Aspect_Rep_Item (ASN)));
6174
 
6175
         when Aspect_Storage_Pool =>
6176
            T := Class_Wide_Type (RTE (RE_Root_Storage_Pool));
6177
 
6178
         when Aspect_Alignment      |
6179
              Aspect_Component_Size |
6180
              Aspect_Machine_Radix  |
6181
              Aspect_Object_Size    |
6182
              Aspect_Size           |
6183
              Aspect_Storage_Size   |
6184
              Aspect_Stream_Size    |
6185
              Aspect_Value_Size     =>
6186
            T := Any_Integer;
6187
 
6188
         --  Stream attribute. Special case, the expression is just an entity
6189
         --  that does not need any resolution, so just analyze.
6190
 
6191
         when Aspect_Input  |
6192
              Aspect_Output |
6193
              Aspect_Read   |
6194
              Aspect_Write  =>
6195
            Analyze (Expression (ASN));
6196
            return;
6197
 
6198
         --  Same for Iterator aspects, where the expression is a function
6199
         --  name. Legality rules are checked separately.
6200
 
6201
         when Aspect_Constant_Indexing    |
6202
              Aspect_Default_Iterator     |
6203
              Aspect_Iterator_Element     |
6204
              Aspect_Implicit_Dereference |
6205
              Aspect_Variable_Indexing    =>
6206
            Analyze (Expression (ASN));
6207
            return;
6208
 
6209
         --  Suppress/Unsuppress/Synchronization/Warnings should not be delayed
6210
 
6211
         when Aspect_Suppress        |
6212
              Aspect_Unsuppress      |
6213
              Aspect_Synchronization |
6214
              Aspect_Warnings        =>
6215
            raise Program_Error;
6216
 
6217
         --  Pre/Post/Invariant/Predicate take boolean expressions
6218
 
6219
         when Aspect_Dynamic_Predicate |
6220
              Aspect_Invariant         |
6221
              Aspect_Pre               |
6222
              Aspect_Precondition      |
6223
              Aspect_Post              |
6224
              Aspect_Postcondition     |
6225
              Aspect_Predicate         |
6226
              Aspect_Static_Predicate  |
6227
              Aspect_Type_Invariant    =>
6228
            T := Standard_Boolean;
6229
 
6230
         when Aspect_Dimension        |
6231
              Aspect_Dimension_System =>
6232
            raise Program_Error;
6233
 
6234
      end case;
6235
 
6236
      --  Do the preanalyze call
6237
 
6238
      Preanalyze_Spec_Expression (Expression (ASN), T);
6239
   end Check_Aspect_At_Freeze_Point;
6240
 
6241
   -----------------------------------
6242
   -- Check_Constant_Address_Clause --
6243
   -----------------------------------
6244
 
6245
   procedure Check_Constant_Address_Clause
6246
     (Expr  : Node_Id;
6247
      U_Ent : Entity_Id)
6248
   is
6249
      procedure Check_At_Constant_Address (Nod : Node_Id);
6250
      --  Checks that the given node N represents a name whose 'Address is
6251
      --  constant (in the same sense as OK_Constant_Address_Clause, i.e. the
6252
      --  address value is the same at the point of declaration of U_Ent and at
6253
      --  the time of elaboration of the address clause.
6254
 
6255
      procedure Check_Expr_Constants (Nod : Node_Id);
6256
      --  Checks that Nod meets the requirements for a constant address clause
6257
      --  in the sense of the enclosing procedure.
6258
 
6259
      procedure Check_List_Constants (Lst : List_Id);
6260
      --  Check that all elements of list Lst meet the requirements for a
6261
      --  constant address clause in the sense of the enclosing procedure.
6262
 
6263
      -------------------------------
6264
      -- Check_At_Constant_Address --
6265
      -------------------------------
6266
 
6267
      procedure Check_At_Constant_Address (Nod : Node_Id) is
6268
      begin
6269
         if Is_Entity_Name (Nod) then
6270
            if Present (Address_Clause (Entity ((Nod)))) then
6271
               Error_Msg_NE
6272
                 ("invalid address clause for initialized object &!",
6273
                           Nod, U_Ent);
6274
               Error_Msg_NE
6275
                 ("address for& cannot" &
6276
                    " depend on another address clause! (RM 13.1(22))!",
6277
                  Nod, U_Ent);
6278
 
6279
            elsif In_Same_Source_Unit (Entity (Nod), U_Ent)
6280
              and then Sloc (U_Ent) < Sloc (Entity (Nod))
6281
            then
6282
               Error_Msg_NE
6283
                 ("invalid address clause for initialized object &!",
6284
                  Nod, U_Ent);
6285
               Error_Msg_Node_2 := U_Ent;
6286
               Error_Msg_NE
6287
                 ("\& must be defined before & (RM 13.1(22))!",
6288
                  Nod, Entity (Nod));
6289
            end if;
6290
 
6291
         elsif Nkind (Nod) = N_Selected_Component then
6292
            declare
6293
               T : constant Entity_Id := Etype (Prefix (Nod));
6294
 
6295
            begin
6296
               if (Is_Record_Type (T)
6297
                    and then Has_Discriminants (T))
6298
                 or else
6299
                  (Is_Access_Type (T)
6300
                     and then Is_Record_Type (Designated_Type (T))
6301
                     and then Has_Discriminants (Designated_Type (T)))
6302
               then
6303
                  Error_Msg_NE
6304
                    ("invalid address clause for initialized object &!",
6305
                     Nod, U_Ent);
6306
                  Error_Msg_N
6307
                    ("\address cannot depend on component" &
6308
                     " of discriminated record (RM 13.1(22))!",
6309
                     Nod);
6310
               else
6311
                  Check_At_Constant_Address (Prefix (Nod));
6312
               end if;
6313
            end;
6314
 
6315
         elsif Nkind (Nod) = N_Indexed_Component then
6316
            Check_At_Constant_Address (Prefix (Nod));
6317
            Check_List_Constants (Expressions (Nod));
6318
 
6319
         else
6320
            Check_Expr_Constants (Nod);
6321
         end if;
6322
      end Check_At_Constant_Address;
6323
 
6324
      --------------------------
6325
      -- Check_Expr_Constants --
6326
      --------------------------
6327
 
6328
      procedure Check_Expr_Constants (Nod : Node_Id) is
6329
         Loc_U_Ent : constant Source_Ptr := Sloc (U_Ent);
6330
         Ent       : Entity_Id           := Empty;
6331
 
6332
      begin
6333
         if Nkind (Nod) in N_Has_Etype
6334
           and then Etype (Nod) = Any_Type
6335
         then
6336
            return;
6337
         end if;
6338
 
6339
         case Nkind (Nod) is
6340
            when N_Empty | N_Error =>
6341
               return;
6342
 
6343
            when N_Identifier | N_Expanded_Name =>
6344
               Ent := Entity (Nod);
6345
 
6346
               --  We need to look at the original node if it is different
6347
               --  from the node, since we may have rewritten things and
6348
               --  substituted an identifier representing the rewrite.
6349
 
6350
               if Original_Node (Nod) /= Nod then
6351
                  Check_Expr_Constants (Original_Node (Nod));
6352
 
6353
                  --  If the node is an object declaration without initial
6354
                  --  value, some code has been expanded, and the expression
6355
                  --  is not constant, even if the constituents might be
6356
                  --  acceptable, as in A'Address + offset.
6357
 
6358
                  if Ekind (Ent) = E_Variable
6359
                    and then
6360
                      Nkind (Declaration_Node (Ent)) = N_Object_Declaration
6361
                    and then
6362
                      No (Expression (Declaration_Node (Ent)))
6363
                  then
6364
                     Error_Msg_NE
6365
                       ("invalid address clause for initialized object &!",
6366
                        Nod, U_Ent);
6367
 
6368
                  --  If entity is constant, it may be the result of expanding
6369
                  --  a check. We must verify that its declaration appears
6370
                  --  before the object in question, else we also reject the
6371
                  --  address clause.
6372
 
6373
                  elsif Ekind (Ent) = E_Constant
6374
                    and then In_Same_Source_Unit (Ent, U_Ent)
6375
                    and then Sloc (Ent) > Loc_U_Ent
6376
                  then
6377
                     Error_Msg_NE
6378
                       ("invalid address clause for initialized object &!",
6379
                        Nod, U_Ent);
6380
                  end if;
6381
 
6382
                  return;
6383
               end if;
6384
 
6385
               --  Otherwise look at the identifier and see if it is OK
6386
 
6387
               if Ekind_In (Ent, E_Named_Integer, E_Named_Real)
6388
                 or else Is_Type (Ent)
6389
               then
6390
                  return;
6391
 
6392
               elsif
6393
                  Ekind (Ent) = E_Constant
6394
                    or else
6395
                  Ekind (Ent) = E_In_Parameter
6396
               then
6397
                  --  This is the case where we must have Ent defined before
6398
                  --  U_Ent. Clearly if they are in different units this
6399
                  --  requirement is met since the unit containing Ent is
6400
                  --  already processed.
6401
 
6402
                  if not In_Same_Source_Unit (Ent, U_Ent) then
6403
                     return;
6404
 
6405
                  --  Otherwise location of Ent must be before the location
6406
                  --  of U_Ent, that's what prior defined means.
6407
 
6408
                  elsif Sloc (Ent) < Loc_U_Ent then
6409
                     return;
6410
 
6411
                  else
6412
                     Error_Msg_NE
6413
                       ("invalid address clause for initialized object &!",
6414
                        Nod, U_Ent);
6415
                     Error_Msg_Node_2 := U_Ent;
6416
                     Error_Msg_NE
6417
                       ("\& must be defined before & (RM 13.1(22))!",
6418
                        Nod, Ent);
6419
                  end if;
6420
 
6421
               elsif Nkind (Original_Node (Nod)) = N_Function_Call then
6422
                  Check_Expr_Constants (Original_Node (Nod));
6423
 
6424
               else
6425
                  Error_Msg_NE
6426
                    ("invalid address clause for initialized object &!",
6427
                     Nod, U_Ent);
6428
 
6429
                  if Comes_From_Source (Ent) then
6430
                     Error_Msg_NE
6431
                       ("\reference to variable& not allowed"
6432
                          & " (RM 13.1(22))!", Nod, Ent);
6433
                  else
6434
                     Error_Msg_N
6435
                       ("non-static expression not allowed"
6436
                          & " (RM 13.1(22))!", Nod);
6437
                  end if;
6438
               end if;
6439
 
6440
            when N_Integer_Literal   =>
6441
 
6442
               --  If this is a rewritten unchecked conversion, in a system
6443
               --  where Address is an integer type, always use the base type
6444
               --  for a literal value. This is user-friendly and prevents
6445
               --  order-of-elaboration issues with instances of unchecked
6446
               --  conversion.
6447
 
6448
               if Nkind (Original_Node (Nod)) = N_Function_Call then
6449
                  Set_Etype (Nod, Base_Type (Etype (Nod)));
6450
               end if;
6451
 
6452
            when N_Real_Literal      |
6453
                 N_String_Literal    |
6454
                 N_Character_Literal =>
6455
               return;
6456
 
6457
            when N_Range =>
6458
               Check_Expr_Constants (Low_Bound (Nod));
6459
               Check_Expr_Constants (High_Bound (Nod));
6460
 
6461
            when N_Explicit_Dereference =>
6462
               Check_Expr_Constants (Prefix (Nod));
6463
 
6464
            when N_Indexed_Component =>
6465
               Check_Expr_Constants (Prefix (Nod));
6466
               Check_List_Constants (Expressions (Nod));
6467
 
6468
            when N_Slice =>
6469
               Check_Expr_Constants (Prefix (Nod));
6470
               Check_Expr_Constants (Discrete_Range (Nod));
6471
 
6472
            when N_Selected_Component =>
6473
               Check_Expr_Constants (Prefix (Nod));
6474
 
6475
            when N_Attribute_Reference =>
6476
               if Attribute_Name (Nod) = Name_Address
6477
                   or else
6478
                  Attribute_Name (Nod) = Name_Access
6479
                    or else
6480
                  Attribute_Name (Nod) = Name_Unchecked_Access
6481
                    or else
6482
                  Attribute_Name (Nod) = Name_Unrestricted_Access
6483
               then
6484
                  Check_At_Constant_Address (Prefix (Nod));
6485
 
6486
               else
6487
                  Check_Expr_Constants (Prefix (Nod));
6488
                  Check_List_Constants (Expressions (Nod));
6489
               end if;
6490
 
6491
            when N_Aggregate =>
6492
               Check_List_Constants (Component_Associations (Nod));
6493
               Check_List_Constants (Expressions (Nod));
6494
 
6495
            when N_Component_Association =>
6496
               Check_Expr_Constants (Expression (Nod));
6497
 
6498
            when N_Extension_Aggregate =>
6499
               Check_Expr_Constants (Ancestor_Part (Nod));
6500
               Check_List_Constants (Component_Associations (Nod));
6501
               Check_List_Constants (Expressions (Nod));
6502
 
6503
            when N_Null =>
6504
               return;
6505
 
6506
            when N_Binary_Op | N_Short_Circuit | N_Membership_Test =>
6507
               Check_Expr_Constants (Left_Opnd (Nod));
6508
               Check_Expr_Constants (Right_Opnd (Nod));
6509
 
6510
            when N_Unary_Op =>
6511
               Check_Expr_Constants (Right_Opnd (Nod));
6512
 
6513
            when N_Type_Conversion           |
6514
                 N_Qualified_Expression      |
6515
                 N_Allocator                 =>
6516
               Check_Expr_Constants (Expression (Nod));
6517
 
6518
            when N_Unchecked_Type_Conversion =>
6519
               Check_Expr_Constants (Expression (Nod));
6520
 
6521
               --  If this is a rewritten unchecked conversion, subtypes in
6522
               --  this node are those created within the instance. To avoid
6523
               --  order of elaboration issues, replace them with their base
6524
               --  types. Note that address clauses can cause order of
6525
               --  elaboration problems because they are elaborated by the
6526
               --  back-end at the point of definition, and may mention
6527
               --  entities declared in between (as long as everything is
6528
               --  static). It is user-friendly to allow unchecked conversions
6529
               --  in this context.
6530
 
6531
               if Nkind (Original_Node (Nod)) = N_Function_Call then
6532
                  Set_Etype (Expression (Nod),
6533
                    Base_Type (Etype (Expression (Nod))));
6534
                  Set_Etype (Nod, Base_Type (Etype (Nod)));
6535
               end if;
6536
 
6537
            when N_Function_Call =>
6538
               if not Is_Pure (Entity (Name (Nod))) then
6539
                  Error_Msg_NE
6540
                    ("invalid address clause for initialized object &!",
6541
                     Nod, U_Ent);
6542
 
6543
                  Error_Msg_NE
6544
                    ("\function & is not pure (RM 13.1(22))!",
6545
                     Nod, Entity (Name (Nod)));
6546
 
6547
               else
6548
                  Check_List_Constants (Parameter_Associations (Nod));
6549
               end if;
6550
 
6551
            when N_Parameter_Association =>
6552
               Check_Expr_Constants (Explicit_Actual_Parameter (Nod));
6553
 
6554
            when others =>
6555
               Error_Msg_NE
6556
                 ("invalid address clause for initialized object &!",
6557
                  Nod, U_Ent);
6558
               Error_Msg_NE
6559
                 ("\must be constant defined before& (RM 13.1(22))!",
6560
                  Nod, U_Ent);
6561
         end case;
6562
      end Check_Expr_Constants;
6563
 
6564
      --------------------------
6565
      -- Check_List_Constants --
6566
      --------------------------
6567
 
6568
      procedure Check_List_Constants (Lst : List_Id) is
6569
         Nod1 : Node_Id;
6570
 
6571
      begin
6572
         if Present (Lst) then
6573
            Nod1 := First (Lst);
6574
            while Present (Nod1) loop
6575
               Check_Expr_Constants (Nod1);
6576
               Next (Nod1);
6577
            end loop;
6578
         end if;
6579
      end Check_List_Constants;
6580
 
6581
   --  Start of processing for Check_Constant_Address_Clause
6582
 
6583
   begin
6584
      --  If rep_clauses are to be ignored, no need for legality checks. In
6585
      --  particular, no need to pester user about rep clauses that violate
6586
      --  the rule on constant addresses, given that these clauses will be
6587
      --  removed by Freeze before they reach the back end.
6588
 
6589
      if not Ignore_Rep_Clauses then
6590
         Check_Expr_Constants (Expr);
6591
      end if;
6592
   end Check_Constant_Address_Clause;
6593
 
6594
   ----------------------------------------
6595
   -- Check_Record_Representation_Clause --
6596
   ----------------------------------------
6597
 
6598
   procedure Check_Record_Representation_Clause (N : Node_Id) is
6599
      Loc     : constant Source_Ptr := Sloc (N);
6600
      Ident   : constant Node_Id    := Identifier (N);
6601
      Rectype : Entity_Id;
6602
      Fent    : Entity_Id;
6603
      CC      : Node_Id;
6604
      Fbit    : Uint;
6605
      Lbit    : Uint;
6606
      Hbit    : Uint := Uint_0;
6607
      Comp    : Entity_Id;
6608
      Pcomp   : Entity_Id;
6609
 
6610
      Max_Bit_So_Far : Uint;
6611
      --  Records the maximum bit position so far. If all field positions
6612
      --  are monotonically increasing, then we can skip the circuit for
6613
      --  checking for overlap, since no overlap is possible.
6614
 
6615
      Tagged_Parent : Entity_Id := Empty;
6616
      --  This is set in the case of a derived tagged type for which we have
6617
      --  Is_Fully_Repped_Tagged_Type True (indicating that all components are
6618
      --  positioned by record representation clauses). In this case we must
6619
      --  check for overlap between components of this tagged type, and the
6620
      --  components of its parent. Tagged_Parent will point to this parent
6621
      --  type. For all other cases Tagged_Parent is left set to Empty.
6622
 
6623
      Parent_Last_Bit : Uint;
6624
      --  Relevant only if Tagged_Parent is set, Parent_Last_Bit indicates the
6625
      --  last bit position for any field in the parent type. We only need to
6626
      --  check overlap for fields starting below this point.
6627
 
6628
      Overlap_Check_Required : Boolean;
6629
      --  Used to keep track of whether or not an overlap check is required
6630
 
6631
      Overlap_Detected : Boolean := False;
6632
      --  Set True if an overlap is detected
6633
 
6634
      Ccount : Natural := 0;
6635
      --  Number of component clauses in record rep clause
6636
 
6637
      procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id);
6638
      --  Given two entities for record components or discriminants, checks
6639
      --  if they have overlapping component clauses and issues errors if so.
6640
 
6641
      procedure Find_Component;
6642
      --  Finds component entity corresponding to current component clause (in
6643
      --  CC), and sets Comp to the entity, and Fbit/Lbit to the zero origin
6644
      --  start/stop bits for the field. If there is no matching component or
6645
      --  if the matching component does not have a component clause, then
6646
      --  that's an error and Comp is set to Empty, but no error message is
6647
      --  issued, since the message was already given. Comp is also set to
6648
      --  Empty if the current "component clause" is in fact a pragma.
6649
 
6650
      -----------------------------
6651
      -- Check_Component_Overlap --
6652
      -----------------------------
6653
 
6654
      procedure Check_Component_Overlap (C1_Ent, C2_Ent : Entity_Id) is
6655
         CC1 : constant Node_Id := Component_Clause (C1_Ent);
6656
         CC2 : constant Node_Id := Component_Clause (C2_Ent);
6657
 
6658
      begin
6659
         if Present (CC1) and then Present (CC2) then
6660
 
6661
            --  Exclude odd case where we have two tag fields in the same
6662
            --  record, both at location zero. This seems a bit strange, but
6663
            --  it seems to happen in some circumstances, perhaps on an error.
6664
 
6665
            if Chars (C1_Ent) = Name_uTag
6666
                 and then
6667
               Chars (C2_Ent) = Name_uTag
6668
            then
6669
               return;
6670
            end if;
6671
 
6672
            --  Here we check if the two fields overlap
6673
 
6674
            declare
6675
               S1 : constant Uint := Component_Bit_Offset (C1_Ent);
6676
               S2 : constant Uint := Component_Bit_Offset (C2_Ent);
6677
               E1 : constant Uint := S1 + Esize (C1_Ent);
6678
               E2 : constant Uint := S2 + Esize (C2_Ent);
6679
 
6680
            begin
6681
               if E2 <= S1 or else E1 <= S2 then
6682
                  null;
6683
               else
6684
                  Error_Msg_Node_2 := Component_Name (CC2);
6685
                  Error_Msg_Sloc := Sloc (Error_Msg_Node_2);
6686
                  Error_Msg_Node_1 := Component_Name (CC1);
6687
                  Error_Msg_N
6688
                    ("component& overlaps & #", Component_Name (CC1));
6689
                  Overlap_Detected := True;
6690
               end if;
6691
            end;
6692
         end if;
6693
      end Check_Component_Overlap;
6694
 
6695
      --------------------
6696
      -- Find_Component --
6697
      --------------------
6698
 
6699
      procedure Find_Component is
6700
 
6701
         procedure Search_Component (R : Entity_Id);
6702
         --  Search components of R for a match. If found, Comp is set.
6703
 
6704
         ----------------------
6705
         -- Search_Component --
6706
         ----------------------
6707
 
6708
         procedure Search_Component (R : Entity_Id) is
6709
         begin
6710
            Comp := First_Component_Or_Discriminant (R);
6711
            while Present (Comp) loop
6712
 
6713
               --  Ignore error of attribute name for component name (we
6714
               --  already gave an error message for this, so no need to
6715
               --  complain here)
6716
 
6717
               if Nkind (Component_Name (CC)) = N_Attribute_Reference then
6718
                  null;
6719
               else
6720
                  exit when Chars (Comp) = Chars (Component_Name (CC));
6721
               end if;
6722
 
6723
               Next_Component_Or_Discriminant (Comp);
6724
            end loop;
6725
         end Search_Component;
6726
 
6727
      --  Start of processing for Find_Component
6728
 
6729
      begin
6730
         --  Return with Comp set to Empty if we have a pragma
6731
 
6732
         if Nkind (CC) = N_Pragma then
6733
            Comp := Empty;
6734
            return;
6735
         end if;
6736
 
6737
         --  Search current record for matching component
6738
 
6739
         Search_Component (Rectype);
6740
 
6741
         --  If not found, maybe component of base type that is absent from
6742
         --  statically constrained first subtype.
6743
 
6744
         if No (Comp) then
6745
            Search_Component (Base_Type (Rectype));
6746
         end if;
6747
 
6748
         --  If no component, or the component does not reference the component
6749
         --  clause in question, then there was some previous error for which
6750
         --  we already gave a message, so just return with Comp Empty.
6751
 
6752
         if No (Comp)
6753
           or else Component_Clause (Comp) /= CC
6754
         then
6755
            Comp := Empty;
6756
 
6757
         --  Normal case where we have a component clause
6758
 
6759
         else
6760
            Fbit := Component_Bit_Offset (Comp);
6761
            Lbit := Fbit + Esize (Comp) - 1;
6762
         end if;
6763
      end Find_Component;
6764
 
6765
   --  Start of processing for Check_Record_Representation_Clause
6766
 
6767
   begin
6768
      Find_Type (Ident);
6769
      Rectype := Entity (Ident);
6770
 
6771
      if Rectype = Any_Type then
6772
         return;
6773
      else
6774
         Rectype := Underlying_Type (Rectype);
6775
      end if;
6776
 
6777
      --  See if we have a fully repped derived tagged type
6778
 
6779
      declare
6780
         PS : constant Entity_Id := Parent_Subtype (Rectype);
6781
 
6782
      begin
6783
         if Present (PS) and then Is_Fully_Repped_Tagged_Type (PS) then
6784
            Tagged_Parent := PS;
6785
 
6786
            --  Find maximum bit of any component of the parent type
6787
 
6788
            Parent_Last_Bit := UI_From_Int (System_Address_Size - 1);
6789
            Pcomp := First_Entity (Tagged_Parent);
6790
            while Present (Pcomp) loop
6791
               if Ekind_In (Pcomp, E_Discriminant, E_Component) then
6792
                  if Component_Bit_Offset (Pcomp) /= No_Uint
6793
                    and then Known_Static_Esize (Pcomp)
6794
                  then
6795
                     Parent_Last_Bit :=
6796
                       UI_Max
6797
                         (Parent_Last_Bit,
6798
                          Component_Bit_Offset (Pcomp) + Esize (Pcomp) - 1);
6799
                  end if;
6800
 
6801
                  Next_Entity (Pcomp);
6802
               end if;
6803
            end loop;
6804
         end if;
6805
      end;
6806
 
6807
      --  All done if no component clauses
6808
 
6809
      CC := First (Component_Clauses (N));
6810
 
6811
      if No (CC) then
6812
         return;
6813
      end if;
6814
 
6815
      --  If a tag is present, then create a component clause that places it
6816
      --  at the start of the record (otherwise gigi may place it after other
6817
      --  fields that have rep clauses).
6818
 
6819
      Fent := First_Entity (Rectype);
6820
 
6821
      if Nkind (Fent) = N_Defining_Identifier
6822
        and then Chars (Fent) = Name_uTag
6823
      then
6824
         Set_Component_Bit_Offset    (Fent, Uint_0);
6825
         Set_Normalized_Position     (Fent, Uint_0);
6826
         Set_Normalized_First_Bit    (Fent, Uint_0);
6827
         Set_Normalized_Position_Max (Fent, Uint_0);
6828
         Init_Esize                  (Fent, System_Address_Size);
6829
 
6830
         Set_Component_Clause (Fent,
6831
           Make_Component_Clause (Loc,
6832
             Component_Name => Make_Identifier (Loc, Name_uTag),
6833
 
6834
             Position  => Make_Integer_Literal (Loc, Uint_0),
6835
             First_Bit => Make_Integer_Literal (Loc, Uint_0),
6836
             Last_Bit  =>
6837
               Make_Integer_Literal (Loc,
6838
                 UI_From_Int (System_Address_Size))));
6839
 
6840
         Ccount := Ccount + 1;
6841
      end if;
6842
 
6843
      Max_Bit_So_Far := Uint_Minus_1;
6844
      Overlap_Check_Required := False;
6845
 
6846
      --  Process the component clauses
6847
 
6848
      while Present (CC) loop
6849
         Find_Component;
6850
 
6851
         if Present (Comp) then
6852
            Ccount := Ccount + 1;
6853
 
6854
            --  We need a full overlap check if record positions non-monotonic
6855
 
6856
            if Fbit <= Max_Bit_So_Far then
6857
               Overlap_Check_Required := True;
6858
            end if;
6859
 
6860
            Max_Bit_So_Far := Lbit;
6861
 
6862
            --  Check bit position out of range of specified size
6863
 
6864
            if Has_Size_Clause (Rectype)
6865
              and then RM_Size (Rectype) <= Lbit
6866
            then
6867
               Error_Msg_N
6868
                 ("bit number out of range of specified size",
6869
                  Last_Bit (CC));
6870
 
6871
               --  Check for overlap with tag field
6872
 
6873
            else
6874
               if Is_Tagged_Type (Rectype)
6875
                 and then Fbit < System_Address_Size
6876
               then
6877
                  Error_Msg_NE
6878
                    ("component overlaps tag field of&",
6879
                     Component_Name (CC), Rectype);
6880
                  Overlap_Detected := True;
6881
               end if;
6882
 
6883
               if Hbit < Lbit then
6884
                  Hbit := Lbit;
6885
               end if;
6886
            end if;
6887
 
6888
            --  Check parent overlap if component might overlap parent field
6889
 
6890
            if Present (Tagged_Parent)
6891
              and then Fbit <= Parent_Last_Bit
6892
            then
6893
               Pcomp := First_Component_Or_Discriminant (Tagged_Parent);
6894
               while Present (Pcomp) loop
6895
                  if not Is_Tag (Pcomp)
6896
                    and then Chars (Pcomp) /= Name_uParent
6897
                  then
6898
                     Check_Component_Overlap (Comp, Pcomp);
6899
                  end if;
6900
 
6901
                  Next_Component_Or_Discriminant (Pcomp);
6902
               end loop;
6903
            end if;
6904
         end if;
6905
 
6906
         Next (CC);
6907
      end loop;
6908
 
6909
      --  Now that we have processed all the component clauses, check for
6910
      --  overlap. We have to leave this till last, since the components can
6911
      --  appear in any arbitrary order in the representation clause.
6912
 
6913
      --  We do not need this check if all specified ranges were monotonic,
6914
      --  as recorded by Overlap_Check_Required being False at this stage.
6915
 
6916
      --  This first section checks if there are any overlapping entries at
6917
      --  all. It does this by sorting all entries and then seeing if there are
6918
      --  any overlaps. If there are none, then that is decisive, but if there
6919
      --  are overlaps, they may still be OK (they may result from fields in
6920
      --  different variants).
6921
 
6922
      if Overlap_Check_Required then
6923
         Overlap_Check1 : declare
6924
 
6925
            OC_Fbit : array (0 .. Ccount) of Uint;
6926
            --  First-bit values for component clauses, the value is the offset
6927
            --  of the first bit of the field from start of record. The zero
6928
            --  entry is for use in sorting.
6929
 
6930
            OC_Lbit : array (0 .. Ccount) of Uint;
6931
            --  Last-bit values for component clauses, the value is the offset
6932
            --  of the last bit of the field from start of record. The zero
6933
            --  entry is for use in sorting.
6934
 
6935
            OC_Count : Natural := 0;
6936
            --  Count of entries in OC_Fbit and OC_Lbit
6937
 
6938
            function OC_Lt (Op1, Op2 : Natural) return Boolean;
6939
            --  Compare routine for Sort
6940
 
6941
            procedure OC_Move (From : Natural; To : Natural);
6942
            --  Move routine for Sort
6943
 
6944
            package Sorting is new GNAT.Heap_Sort_G (OC_Move, OC_Lt);
6945
 
6946
            -----------
6947
            -- OC_Lt --
6948
            -----------
6949
 
6950
            function OC_Lt (Op1, Op2 : Natural) return Boolean is
6951
            begin
6952
               return OC_Fbit (Op1) < OC_Fbit (Op2);
6953
            end OC_Lt;
6954
 
6955
            -------------
6956
            -- OC_Move --
6957
            -------------
6958
 
6959
            procedure OC_Move (From : Natural; To : Natural) is
6960
            begin
6961
               OC_Fbit (To) := OC_Fbit (From);
6962
               OC_Lbit (To) := OC_Lbit (From);
6963
            end OC_Move;
6964
 
6965
            --  Start of processing for Overlap_Check
6966
 
6967
         begin
6968
            CC := First (Component_Clauses (N));
6969
            while Present (CC) loop
6970
 
6971
               --  Exclude component clause already marked in error
6972
 
6973
               if not Error_Posted (CC) then
6974
                  Find_Component;
6975
 
6976
                  if Present (Comp) then
6977
                     OC_Count := OC_Count + 1;
6978
                     OC_Fbit (OC_Count) := Fbit;
6979
                     OC_Lbit (OC_Count) := Lbit;
6980
                  end if;
6981
               end if;
6982
 
6983
               Next (CC);
6984
            end loop;
6985
 
6986
            Sorting.Sort (OC_Count);
6987
 
6988
            Overlap_Check_Required := False;
6989
            for J in 1 .. OC_Count - 1 loop
6990
               if OC_Lbit (J) >= OC_Fbit (J + 1) then
6991
                  Overlap_Check_Required := True;
6992
                  exit;
6993
               end if;
6994
            end loop;
6995
         end Overlap_Check1;
6996
      end if;
6997
 
6998
      --  If Overlap_Check_Required is still True, then we have to do the full
6999
      --  scale overlap check, since we have at least two fields that do
7000
      --  overlap, and we need to know if that is OK since they are in
7001
      --  different variant, or whether we have a definite problem.
7002
 
7003
      if Overlap_Check_Required then
7004
         Overlap_Check2 : declare
7005
            C1_Ent, C2_Ent : Entity_Id;
7006
            --  Entities of components being checked for overlap
7007
 
7008
            Clist : Node_Id;
7009
            --  Component_List node whose Component_Items are being checked
7010
 
7011
            Citem : Node_Id;
7012
            --  Component declaration for component being checked
7013
 
7014
         begin
7015
            C1_Ent := First_Entity (Base_Type (Rectype));
7016
 
7017
            --  Loop through all components in record. For each component check
7018
            --  for overlap with any of the preceding elements on the component
7019
            --  list containing the component and also, if the component is in
7020
            --  a variant, check against components outside the case structure.
7021
            --  This latter test is repeated recursively up the variant tree.
7022
 
7023
            Main_Component_Loop : while Present (C1_Ent) loop
7024
               if not Ekind_In (C1_Ent, E_Component, E_Discriminant) then
7025
                  goto Continue_Main_Component_Loop;
7026
               end if;
7027
 
7028
               --  Skip overlap check if entity has no declaration node. This
7029
               --  happens with discriminants in constrained derived types.
7030
               --  Possibly we are missing some checks as a result, but that
7031
               --  does not seem terribly serious.
7032
 
7033
               if No (Declaration_Node (C1_Ent)) then
7034
                  goto Continue_Main_Component_Loop;
7035
               end if;
7036
 
7037
               Clist := Parent (List_Containing (Declaration_Node (C1_Ent)));
7038
 
7039
               --  Loop through component lists that need checking. Check the
7040
               --  current component list and all lists in variants above us.
7041
 
7042
               Component_List_Loop : loop
7043
 
7044
                  --  If derived type definition, go to full declaration
7045
                  --  If at outer level, check discriminants if there are any.
7046
 
7047
                  if Nkind (Clist) = N_Derived_Type_Definition then
7048
                     Clist := Parent (Clist);
7049
                  end if;
7050
 
7051
                  --  Outer level of record definition, check discriminants
7052
 
7053
                  if Nkind_In (Clist, N_Full_Type_Declaration,
7054
                               N_Private_Type_Declaration)
7055
                  then
7056
                     if Has_Discriminants (Defining_Identifier (Clist)) then
7057
                        C2_Ent :=
7058
                          First_Discriminant (Defining_Identifier (Clist));
7059
                        while Present (C2_Ent) loop
7060
                           exit when C1_Ent = C2_Ent;
7061
                           Check_Component_Overlap (C1_Ent, C2_Ent);
7062
                           Next_Discriminant (C2_Ent);
7063
                        end loop;
7064
                     end if;
7065
 
7066
                     --  Record extension case
7067
 
7068
                  elsif Nkind (Clist) = N_Derived_Type_Definition then
7069
                     Clist := Empty;
7070
 
7071
                     --  Otherwise check one component list
7072
 
7073
                  else
7074
                     Citem := First (Component_Items (Clist));
7075
                     while Present (Citem) loop
7076
                        if Nkind (Citem) = N_Component_Declaration then
7077
                           C2_Ent := Defining_Identifier (Citem);
7078
                           exit when C1_Ent = C2_Ent;
7079
                           Check_Component_Overlap (C1_Ent, C2_Ent);
7080
                        end if;
7081
 
7082
                        Next (Citem);
7083
                     end loop;
7084
                  end if;
7085
 
7086
                  --  Check for variants above us (the parent of the Clist can
7087
                  --  be a variant, in which case its parent is a variant part,
7088
                  --  and the parent of the variant part is a component list
7089
                  --  whose components must all be checked against the current
7090
                  --  component for overlap).
7091
 
7092
                  if Nkind (Parent (Clist)) = N_Variant then
7093
                     Clist := Parent (Parent (Parent (Clist)));
7094
 
7095
                     --  Check for possible discriminant part in record, this
7096
                     --  is treated essentially as another level in the
7097
                     --  recursion. For this case the parent of the component
7098
                     --  list is the record definition, and its parent is the
7099
                     --  full type declaration containing the discriminant
7100
                     --  specifications.
7101
 
7102
                  elsif Nkind (Parent (Clist)) = N_Record_Definition then
7103
                     Clist := Parent (Parent ((Clist)));
7104
 
7105
                     --  If neither of these two cases, we are at the top of
7106
                     --  the tree.
7107
 
7108
                  else
7109
                     exit Component_List_Loop;
7110
                  end if;
7111
               end loop Component_List_Loop;
7112
 
7113
               <<Continue_Main_Component_Loop>>
7114
               Next_Entity (C1_Ent);
7115
 
7116
            end loop Main_Component_Loop;
7117
         end Overlap_Check2;
7118
      end if;
7119
 
7120
      --  The following circuit deals with warning on record holes (gaps). We
7121
      --  skip this check if overlap was detected, since it makes sense for the
7122
      --  programmer to fix this illegality before worrying about warnings.
7123
 
7124
      if not Overlap_Detected and Warn_On_Record_Holes then
7125
         Record_Hole_Check : declare
7126
            Decl : constant Node_Id := Declaration_Node (Base_Type (Rectype));
7127
            --  Full declaration of record type
7128
 
7129
            procedure Check_Component_List
7130
              (CL   : Node_Id;
7131
               Sbit : Uint;
7132
               DS   : List_Id);
7133
            --  Check component list CL for holes. The starting bit should be
7134
            --  Sbit. which is zero for the main record component list and set
7135
            --  appropriately for recursive calls for variants. DS is set to
7136
            --  a list of discriminant specifications to be included in the
7137
            --  consideration of components. It is No_List if none to consider.
7138
 
7139
            --------------------------
7140
            -- Check_Component_List --
7141
            --------------------------
7142
 
7143
            procedure Check_Component_List
7144
              (CL   : Node_Id;
7145
               Sbit : Uint;
7146
               DS   : List_Id)
7147
            is
7148
               Compl : Integer;
7149
 
7150
            begin
7151
               Compl := Integer (List_Length (Component_Items (CL)));
7152
 
7153
               if DS /= No_List then
7154
                  Compl := Compl + Integer (List_Length (DS));
7155
               end if;
7156
 
7157
               declare
7158
                  Comps : array (Natural range 0 .. Compl) of Entity_Id;
7159
                  --  Gather components (zero entry is for sort routine)
7160
 
7161
                  Ncomps : Natural := 0;
7162
                  --  Number of entries stored in Comps (starting at Comps (1))
7163
 
7164
                  Citem : Node_Id;
7165
                  --  One component item or discriminant specification
7166
 
7167
                  Nbit  : Uint;
7168
                  --  Starting bit for next component
7169
 
7170
                  CEnt  : Entity_Id;
7171
                  --  Component entity
7172
 
7173
                  Variant : Node_Id;
7174
                  --  One variant
7175
 
7176
                  function Lt (Op1, Op2 : Natural) return Boolean;
7177
                  --  Compare routine for Sort
7178
 
7179
                  procedure Move (From : Natural; To : Natural);
7180
                  --  Move routine for Sort
7181
 
7182
                  package Sorting is new GNAT.Heap_Sort_G (Move, Lt);
7183
 
7184
                  --------
7185
                  -- Lt --
7186
                  --------
7187
 
7188
                  function Lt (Op1, Op2 : Natural) return Boolean is
7189
                  begin
7190
                     return Component_Bit_Offset (Comps (Op1))
7191
                       <
7192
                       Component_Bit_Offset (Comps (Op2));
7193
                  end Lt;
7194
 
7195
                  ----------
7196
                  -- Move --
7197
                  ----------
7198
 
7199
                  procedure Move (From : Natural; To : Natural) is
7200
                  begin
7201
                     Comps (To) := Comps (From);
7202
                  end Move;
7203
 
7204
               begin
7205
                  --  Gather discriminants into Comp
7206
 
7207
                  if DS /= No_List then
7208
                     Citem := First (DS);
7209
                     while Present (Citem) loop
7210
                        if Nkind (Citem) = N_Discriminant_Specification then
7211
                           declare
7212
                              Ent : constant Entity_Id :=
7213
                                      Defining_Identifier (Citem);
7214
                           begin
7215
                              if Ekind (Ent) = E_Discriminant then
7216
                                 Ncomps := Ncomps + 1;
7217
                                 Comps (Ncomps) := Ent;
7218
                              end if;
7219
                           end;
7220
                        end if;
7221
 
7222
                        Next (Citem);
7223
                     end loop;
7224
                  end if;
7225
 
7226
                  --  Gather component entities into Comp
7227
 
7228
                  Citem := First (Component_Items (CL));
7229
                  while Present (Citem) loop
7230
                     if Nkind (Citem) = N_Component_Declaration then
7231
                        Ncomps := Ncomps + 1;
7232
                        Comps (Ncomps) := Defining_Identifier (Citem);
7233
                     end if;
7234
 
7235
                     Next (Citem);
7236
                  end loop;
7237
 
7238
                  --  Now sort the component entities based on the first bit.
7239
                  --  Note we already know there are no overlapping components.
7240
 
7241
                  Sorting.Sort (Ncomps);
7242
 
7243
                  --  Loop through entries checking for holes
7244
 
7245
                  Nbit := Sbit;
7246
                  for J in 1 .. Ncomps loop
7247
                     CEnt := Comps (J);
7248
                     Error_Msg_Uint_1 := Component_Bit_Offset (CEnt) - Nbit;
7249
 
7250
                     if Error_Msg_Uint_1 > 0 then
7251
                        Error_Msg_NE
7252
                          ("?^-bit gap before component&",
7253
                           Component_Name (Component_Clause (CEnt)), CEnt);
7254
                     end if;
7255
 
7256
                     Nbit := Component_Bit_Offset (CEnt) + Esize (CEnt);
7257
                  end loop;
7258
 
7259
                  --  Process variant parts recursively if present
7260
 
7261
                  if Present (Variant_Part (CL)) then
7262
                     Variant := First (Variants (Variant_Part (CL)));
7263
                     while Present (Variant) loop
7264
                        Check_Component_List
7265
                          (Component_List (Variant), Nbit, No_List);
7266
                        Next (Variant);
7267
                     end loop;
7268
                  end if;
7269
               end;
7270
            end Check_Component_List;
7271
 
7272
         --  Start of processing for Record_Hole_Check
7273
 
7274
         begin
7275
            declare
7276
               Sbit : Uint;
7277
 
7278
            begin
7279
               if Is_Tagged_Type (Rectype) then
7280
                  Sbit := UI_From_Int (System_Address_Size);
7281
               else
7282
                  Sbit := Uint_0;
7283
               end if;
7284
 
7285
               if Nkind (Decl) = N_Full_Type_Declaration
7286
                 and then Nkind (Type_Definition (Decl)) = N_Record_Definition
7287
               then
7288
                  Check_Component_List
7289
                    (Component_List (Type_Definition (Decl)),
7290
                     Sbit,
7291
                     Discriminant_Specifications (Decl));
7292
               end if;
7293
            end;
7294
         end Record_Hole_Check;
7295
      end if;
7296
 
7297
      --  For records that have component clauses for all components, and whose
7298
      --  size is less than or equal to 32, we need to know the size in the
7299
      --  front end to activate possible packed array processing where the
7300
      --  component type is a record.
7301
 
7302
      --  At this stage Hbit + 1 represents the first unused bit from all the
7303
      --  component clauses processed, so if the component clauses are
7304
      --  complete, then this is the length of the record.
7305
 
7306
      --  For records longer than System.Storage_Unit, and for those where not
7307
      --  all components have component clauses, the back end determines the
7308
      --  length (it may for example be appropriate to round up the size
7309
      --  to some convenient boundary, based on alignment considerations, etc).
7310
 
7311
      if Unknown_RM_Size (Rectype) and then Hbit + 1 <= 32 then
7312
 
7313
         --  Nothing to do if at least one component has no component clause
7314
 
7315
         Comp := First_Component_Or_Discriminant (Rectype);
7316
         while Present (Comp) loop
7317
            exit when No (Component_Clause (Comp));
7318
            Next_Component_Or_Discriminant (Comp);
7319
         end loop;
7320
 
7321
         --  If we fall out of loop, all components have component clauses
7322
         --  and so we can set the size to the maximum value.
7323
 
7324
         if No (Comp) then
7325
            Set_RM_Size (Rectype, Hbit + 1);
7326
         end if;
7327
      end if;
7328
   end Check_Record_Representation_Clause;
7329
 
7330
   ----------------
7331
   -- Check_Size --
7332
   ----------------
7333
 
7334
   procedure Check_Size
7335
     (N      : Node_Id;
7336
      T      : Entity_Id;
7337
      Siz    : Uint;
7338
      Biased : out Boolean)
7339
   is
7340
      UT : constant Entity_Id := Underlying_Type (T);
7341
      M  : Uint;
7342
 
7343
   begin
7344
      Biased := False;
7345
 
7346
      --  Dismiss cases for generic types or types with previous errors
7347
 
7348
      if No (UT)
7349
        or else UT = Any_Type
7350
        or else Is_Generic_Type (UT)
7351
        or else Is_Generic_Type (Root_Type (UT))
7352
      then
7353
         return;
7354
 
7355
      --  Check case of bit packed array
7356
 
7357
      elsif Is_Array_Type (UT)
7358
        and then Known_Static_Component_Size (UT)
7359
        and then Is_Bit_Packed_Array (UT)
7360
      then
7361
         declare
7362
            Asiz : Uint;
7363
            Indx : Node_Id;
7364
            Ityp : Entity_Id;
7365
 
7366
         begin
7367
            Asiz := Component_Size (UT);
7368
            Indx := First_Index (UT);
7369
            loop
7370
               Ityp := Etype (Indx);
7371
 
7372
               --  If non-static bound, then we are not in the business of
7373
               --  trying to check the length, and indeed an error will be
7374
               --  issued elsewhere, since sizes of non-static array types
7375
               --  cannot be set implicitly or explicitly.
7376
 
7377
               if not Is_Static_Subtype (Ityp) then
7378
                  return;
7379
               end if;
7380
 
7381
               --  Otherwise accumulate next dimension
7382
 
7383
               Asiz := Asiz * (Expr_Value (Type_High_Bound (Ityp)) -
7384
                               Expr_Value (Type_Low_Bound  (Ityp)) +
7385
                               Uint_1);
7386
 
7387
               Next_Index (Indx);
7388
               exit when No (Indx);
7389
            end loop;
7390
 
7391
            if Asiz <= Siz then
7392
               return;
7393
            else
7394
               Error_Msg_Uint_1 := Asiz;
7395
               Error_Msg_NE
7396
                 ("size for& too small, minimum allowed is ^", N, T);
7397
               Set_Esize   (T, Asiz);
7398
               Set_RM_Size (T, Asiz);
7399
            end if;
7400
         end;
7401
 
7402
      --  All other composite types are ignored
7403
 
7404
      elsif Is_Composite_Type (UT) then
7405
         return;
7406
 
7407
      --  For fixed-point types, don't check minimum if type is not frozen,
7408
      --  since we don't know all the characteristics of the type that can
7409
      --  affect the size (e.g. a specified small) till freeze time.
7410
 
7411
      elsif Is_Fixed_Point_Type (UT)
7412
        and then not Is_Frozen (UT)
7413
      then
7414
         null;
7415
 
7416
      --  Cases for which a minimum check is required
7417
 
7418
      else
7419
         --  Ignore if specified size is correct for the type
7420
 
7421
         if Known_Esize (UT) and then Siz = Esize (UT) then
7422
            return;
7423
         end if;
7424
 
7425
         --  Otherwise get minimum size
7426
 
7427
         M := UI_From_Int (Minimum_Size (UT));
7428
 
7429
         if Siz < M then
7430
 
7431
            --  Size is less than minimum size, but one possibility remains
7432
            --  that we can manage with the new size if we bias the type.
7433
 
7434
            M := UI_From_Int (Minimum_Size (UT, Biased => True));
7435
 
7436
            if Siz < M then
7437
               Error_Msg_Uint_1 := M;
7438
               Error_Msg_NE
7439
                 ("size for& too small, minimum allowed is ^", N, T);
7440
               Set_Esize (T, M);
7441
               Set_RM_Size (T, M);
7442
            else
7443
               Biased := True;
7444
            end if;
7445
         end if;
7446
      end if;
7447
   end Check_Size;
7448
 
7449
   -------------------------
7450
   -- Get_Alignment_Value --
7451
   -------------------------
7452
 
7453
   function Get_Alignment_Value (Expr : Node_Id) return Uint is
7454
      Align : constant Uint := Static_Integer (Expr);
7455
 
7456
   begin
7457
      if Align = No_Uint then
7458
         return No_Uint;
7459
 
7460
      elsif Align <= 0 then
7461
         Error_Msg_N ("alignment value must be positive", Expr);
7462
         return No_Uint;
7463
 
7464
      else
7465
         for J in Int range 0 .. 64 loop
7466
            declare
7467
               M : constant Uint := Uint_2 ** J;
7468
 
7469
            begin
7470
               exit when M = Align;
7471
 
7472
               if M > Align then
7473
                  Error_Msg_N
7474
                    ("alignment value must be power of 2", Expr);
7475
                  return No_Uint;
7476
               end if;
7477
            end;
7478
         end loop;
7479
 
7480
         return Align;
7481
      end if;
7482
   end Get_Alignment_Value;
7483
 
7484
   ----------------
7485
   -- Initialize --
7486
   ----------------
7487
 
7488
   procedure Initialize is
7489
   begin
7490
      Address_Clause_Checks.Init;
7491
      Independence_Checks.Init;
7492
      Unchecked_Conversions.Init;
7493
   end Initialize;
7494
 
7495
   -------------------------
7496
   -- Is_Operational_Item --
7497
   -------------------------
7498
 
7499
   function Is_Operational_Item (N : Node_Id) return Boolean is
7500
   begin
7501
      if Nkind (N) /= N_Attribute_Definition_Clause then
7502
         return False;
7503
      else
7504
         declare
7505
            Id    : constant Attribute_Id := Get_Attribute_Id (Chars (N));
7506
         begin
7507
            return   Id = Attribute_Input
7508
              or else Id = Attribute_Output
7509
              or else Id = Attribute_Read
7510
              or else Id = Attribute_Write
7511
              or else Id = Attribute_External_Tag;
7512
         end;
7513
      end if;
7514
   end Is_Operational_Item;
7515
 
7516
   ------------------
7517
   -- Minimum_Size --
7518
   ------------------
7519
 
7520
   function Minimum_Size
7521
     (T      : Entity_Id;
7522
      Biased : Boolean := False) return Nat
7523
   is
7524
      Lo     : Uint    := No_Uint;
7525
      Hi     : Uint    := No_Uint;
7526
      LoR    : Ureal   := No_Ureal;
7527
      HiR    : Ureal   := No_Ureal;
7528
      LoSet  : Boolean := False;
7529
      HiSet  : Boolean := False;
7530
      B      : Uint;
7531
      S      : Nat;
7532
      Ancest : Entity_Id;
7533
      R_Typ  : constant Entity_Id := Root_Type (T);
7534
 
7535
   begin
7536
      --  If bad type, return 0
7537
 
7538
      if T = Any_Type then
7539
         return 0;
7540
 
7541
      --  For generic types, just return zero. There cannot be any legitimate
7542
      --  need to know such a size, but this routine may be called with a
7543
      --  generic type as part of normal processing.
7544
 
7545
      elsif Is_Generic_Type (R_Typ)
7546
        or else R_Typ = Any_Type
7547
      then
7548
         return 0;
7549
 
7550
         --  Access types. Normally an access type cannot have a size smaller
7551
         --  than the size of System.Address. The exception is on VMS, where
7552
         --  we have short and long addresses, and it is possible for an access
7553
         --  type to have a short address size (and thus be less than the size
7554
         --  of System.Address itself). We simply skip the check for VMS, and
7555
         --  leave it to the back end to do the check.
7556
 
7557
      elsif Is_Access_Type (T) then
7558
         if OpenVMS_On_Target then
7559
            return 0;
7560
         else
7561
            return System_Address_Size;
7562
         end if;
7563
 
7564
      --  Floating-point types
7565
 
7566
      elsif Is_Floating_Point_Type (T) then
7567
         return UI_To_Int (Esize (R_Typ));
7568
 
7569
      --  Discrete types
7570
 
7571
      elsif Is_Discrete_Type (T) then
7572
 
7573
         --  The following loop is looking for the nearest compile time known
7574
         --  bounds following the ancestor subtype chain. The idea is to find
7575
         --  the most restrictive known bounds information.
7576
 
7577
         Ancest := T;
7578
         loop
7579
            if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
7580
               return 0;
7581
            end if;
7582
 
7583
            if not LoSet then
7584
               if Compile_Time_Known_Value (Type_Low_Bound (Ancest)) then
7585
                  Lo := Expr_Rep_Value (Type_Low_Bound (Ancest));
7586
                  LoSet := True;
7587
                  exit when HiSet;
7588
               end if;
7589
            end if;
7590
 
7591
            if not HiSet then
7592
               if Compile_Time_Known_Value (Type_High_Bound (Ancest)) then
7593
                  Hi := Expr_Rep_Value (Type_High_Bound (Ancest));
7594
                  HiSet := True;
7595
                  exit when LoSet;
7596
               end if;
7597
            end if;
7598
 
7599
            Ancest := Ancestor_Subtype (Ancest);
7600
 
7601
            if No (Ancest) then
7602
               Ancest := Base_Type (T);
7603
 
7604
               if Is_Generic_Type (Ancest) then
7605
                  return 0;
7606
               end if;
7607
            end if;
7608
         end loop;
7609
 
7610
      --  Fixed-point types. We can't simply use Expr_Value to get the
7611
      --  Corresponding_Integer_Value values of the bounds, since these do not
7612
      --  get set till the type is frozen, and this routine can be called
7613
      --  before the type is frozen. Similarly the test for bounds being static
7614
      --  needs to include the case where we have unanalyzed real literals for
7615
      --  the same reason.
7616
 
7617
      elsif Is_Fixed_Point_Type (T) then
7618
 
7619
         --  The following loop is looking for the nearest compile time known
7620
         --  bounds following the ancestor subtype chain. The idea is to find
7621
         --  the most restrictive known bounds information.
7622
 
7623
         Ancest := T;
7624
         loop
7625
            if Ancest = Any_Type or else Etype (Ancest) = Any_Type then
7626
               return 0;
7627
            end if;
7628
 
7629
            --  Note: In the following two tests for LoSet and HiSet, it may
7630
            --  seem redundant to test for N_Real_Literal here since normally
7631
            --  one would assume that the test for the value being known at
7632
            --  compile time includes this case. However, there is a glitch.
7633
            --  If the real literal comes from folding a non-static expression,
7634
            --  then we don't consider any non- static expression to be known
7635
            --  at compile time if we are in configurable run time mode (needed
7636
            --  in some cases to give a clearer definition of what is and what
7637
            --  is not accepted). So the test is indeed needed. Without it, we
7638
            --  would set neither Lo_Set nor Hi_Set and get an infinite loop.
7639
 
7640
            if not LoSet then
7641
               if Nkind (Type_Low_Bound (Ancest)) = N_Real_Literal
7642
                 or else Compile_Time_Known_Value (Type_Low_Bound (Ancest))
7643
               then
7644
                  LoR := Expr_Value_R (Type_Low_Bound (Ancest));
7645
                  LoSet := True;
7646
                  exit when HiSet;
7647
               end if;
7648
            end if;
7649
 
7650
            if not HiSet then
7651
               if Nkind (Type_High_Bound (Ancest)) = N_Real_Literal
7652
                 or else Compile_Time_Known_Value (Type_High_Bound (Ancest))
7653
               then
7654
                  HiR := Expr_Value_R (Type_High_Bound (Ancest));
7655
                  HiSet := True;
7656
                  exit when LoSet;
7657
               end if;
7658
            end if;
7659
 
7660
            Ancest := Ancestor_Subtype (Ancest);
7661
 
7662
            if No (Ancest) then
7663
               Ancest := Base_Type (T);
7664
 
7665
               if Is_Generic_Type (Ancest) then
7666
                  return 0;
7667
               end if;
7668
            end if;
7669
         end loop;
7670
 
7671
         Lo := UR_To_Uint (LoR / Small_Value (T));
7672
         Hi := UR_To_Uint (HiR / Small_Value (T));
7673
 
7674
      --  No other types allowed
7675
 
7676
      else
7677
         raise Program_Error;
7678
      end if;
7679
 
7680
      --  Fall through with Hi and Lo set. Deal with biased case
7681
 
7682
      if (Biased
7683
           and then not Is_Fixed_Point_Type (T)
7684
           and then not (Is_Enumeration_Type (T)
7685
                          and then Has_Non_Standard_Rep (T)))
7686
        or else Has_Biased_Representation (T)
7687
      then
7688
         Hi := Hi - Lo;
7689
         Lo := Uint_0;
7690
      end if;
7691
 
7692
      --  Signed case. Note that we consider types like range 1 .. -1 to be
7693
      --  signed for the purpose of computing the size, since the bounds have
7694
      --  to be accommodated in the base type.
7695
 
7696
      if Lo < 0 or else Hi < 0 then
7697
         S := 1;
7698
         B := Uint_1;
7699
 
7700
         --  S = size, B = 2 ** (size - 1) (can accommodate -B .. +(B - 1))
7701
         --  Note that we accommodate the case where the bounds cross. This
7702
         --  can happen either because of the way the bounds are declared
7703
         --  or because of the algorithm in Freeze_Fixed_Point_Type.
7704
 
7705
         while Lo < -B
7706
           or else Hi < -B
7707
           or else Lo >= B
7708
           or else Hi >= B
7709
         loop
7710
            B := Uint_2 ** S;
7711
            S := S + 1;
7712
         end loop;
7713
 
7714
      --  Unsigned case
7715
 
7716
      else
7717
         --  If both bounds are positive, make sure that both are represen-
7718
         --  table in the case where the bounds are crossed. This can happen
7719
         --  either because of the way the bounds are declared, or because of
7720
         --  the algorithm in Freeze_Fixed_Point_Type.
7721
 
7722
         if Lo > Hi then
7723
            Hi := Lo;
7724
         end if;
7725
 
7726
         --  S = size, (can accommodate 0 .. (2**size - 1))
7727
 
7728
         S := 0;
7729
         while Hi >= Uint_2 ** S loop
7730
            S := S + 1;
7731
         end loop;
7732
      end if;
7733
 
7734
      return S;
7735
   end Minimum_Size;
7736
 
7737
   ---------------------------
7738
   -- New_Stream_Subprogram --
7739
   ---------------------------
7740
 
7741
   procedure New_Stream_Subprogram
7742
     (N     : Node_Id;
7743
      Ent   : Entity_Id;
7744
      Subp  : Entity_Id;
7745
      Nam   : TSS_Name_Type)
7746
   is
7747
      Loc       : constant Source_Ptr := Sloc (N);
7748
      Sname     : constant Name_Id    := Make_TSS_Name (Base_Type (Ent), Nam);
7749
      Subp_Id   : Entity_Id;
7750
      Subp_Decl : Node_Id;
7751
      F         : Entity_Id;
7752
      Etyp      : Entity_Id;
7753
 
7754
      Defer_Declaration : constant Boolean :=
7755
                            Is_Tagged_Type (Ent) or else Is_Private_Type (Ent);
7756
      --  For a tagged type, there is a declaration for each stream attribute
7757
      --  at the freeze point, and we must generate only a completion of this
7758
      --  declaration. We do the same for private types, because the full view
7759
      --  might be tagged. Otherwise we generate a declaration at the point of
7760
      --  the attribute definition clause.
7761
 
7762
      function Build_Spec return Node_Id;
7763
      --  Used for declaration and renaming declaration, so that this is
7764
      --  treated as a renaming_as_body.
7765
 
7766
      ----------------
7767
      -- Build_Spec --
7768
      ----------------
7769
 
7770
      function Build_Spec return Node_Id is
7771
         Out_P   : constant Boolean := (Nam = TSS_Stream_Read);
7772
         Formals : List_Id;
7773
         Spec    : Node_Id;
7774
         T_Ref   : constant Node_Id := New_Reference_To (Etyp, Loc);
7775
 
7776
      begin
7777
         Subp_Id := Make_Defining_Identifier (Loc, Sname);
7778
 
7779
         --  S : access Root_Stream_Type'Class
7780
 
7781
         Formals := New_List (
7782
                      Make_Parameter_Specification (Loc,
7783
                        Defining_Identifier =>
7784
                          Make_Defining_Identifier (Loc, Name_S),
7785
                        Parameter_Type =>
7786
                          Make_Access_Definition (Loc,
7787
                            Subtype_Mark =>
7788
                              New_Reference_To (
7789
                                Designated_Type (Etype (F)), Loc))));
7790
 
7791
         if Nam = TSS_Stream_Input then
7792
            Spec := Make_Function_Specification (Loc,
7793
                      Defining_Unit_Name       => Subp_Id,
7794
                      Parameter_Specifications => Formals,
7795
                      Result_Definition        => T_Ref);
7796
         else
7797
            --  V : [out] T
7798
 
7799
            Append_To (Formals,
7800
              Make_Parameter_Specification (Loc,
7801
                Defining_Identifier => Make_Defining_Identifier (Loc, Name_V),
7802
                Out_Present         => Out_P,
7803
                Parameter_Type      => T_Ref));
7804
 
7805
            Spec :=
7806
              Make_Procedure_Specification (Loc,
7807
                Defining_Unit_Name       => Subp_Id,
7808
                Parameter_Specifications => Formals);
7809
         end if;
7810
 
7811
         return Spec;
7812
      end Build_Spec;
7813
 
7814
   --  Start of processing for New_Stream_Subprogram
7815
 
7816
   begin
7817
      F := First_Formal (Subp);
7818
 
7819
      if Ekind (Subp) = E_Procedure then
7820
         Etyp := Etype (Next_Formal (F));
7821
      else
7822
         Etyp := Etype (Subp);
7823
      end if;
7824
 
7825
      --  Prepare subprogram declaration and insert it as an action on the
7826
      --  clause node. The visibility for this entity is used to test for
7827
      --  visibility of the attribute definition clause (in the sense of
7828
      --  8.3(23) as amended by AI-195).
7829
 
7830
      if not Defer_Declaration then
7831
         Subp_Decl :=
7832
           Make_Subprogram_Declaration (Loc,
7833
             Specification => Build_Spec);
7834
 
7835
      --  For a tagged type, there is always a visible declaration for each
7836
      --  stream TSS (it is a predefined primitive operation), and the
7837
      --  completion of this declaration occurs at the freeze point, which is
7838
      --  not always visible at places where the attribute definition clause is
7839
      --  visible. So, we create a dummy entity here for the purpose of
7840
      --  tracking the visibility of the attribute definition clause itself.
7841
 
7842
      else
7843
         Subp_Id :=
7844
           Make_Defining_Identifier (Loc, New_External_Name (Sname, 'V'));
7845
         Subp_Decl :=
7846
           Make_Object_Declaration (Loc,
7847
             Defining_Identifier => Subp_Id,
7848
             Object_Definition   => New_Occurrence_Of (Standard_Boolean, Loc));
7849
      end if;
7850
 
7851
      Insert_Action (N, Subp_Decl);
7852
      Set_Entity (N, Subp_Id);
7853
 
7854
      Subp_Decl :=
7855
        Make_Subprogram_Renaming_Declaration (Loc,
7856
          Specification => Build_Spec,
7857
          Name => New_Reference_To (Subp, Loc));
7858
 
7859
      if Defer_Declaration then
7860
         Set_TSS (Base_Type (Ent), Subp_Id);
7861
      else
7862
         Insert_Action (N, Subp_Decl);
7863
         Copy_TSS (Subp_Id, Base_Type (Ent));
7864
      end if;
7865
   end New_Stream_Subprogram;
7866
 
7867
   ------------------------
7868
   -- Rep_Item_Too_Early --
7869
   ------------------------
7870
 
7871
   function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean is
7872
   begin
7873
      --  Cannot apply non-operational rep items to generic types
7874
 
7875
      if Is_Operational_Item (N) then
7876
         return False;
7877
 
7878
      elsif Is_Type (T)
7879
        and then Is_Generic_Type (Root_Type (T))
7880
      then
7881
         Error_Msg_N ("representation item not allowed for generic type", N);
7882
         return True;
7883
      end if;
7884
 
7885
      --  Otherwise check for incomplete type
7886
 
7887
      if Is_Incomplete_Or_Private_Type (T)
7888
        and then No (Underlying_Type (T))
7889
        and then
7890
          (Nkind (N) /= N_Pragma
7891
            or else Get_Pragma_Id (N) /= Pragma_Import)
7892
      then
7893
         Error_Msg_N
7894
           ("representation item must be after full type declaration", N);
7895
         return True;
7896
 
7897
      --  If the type has incomplete components, a representation clause is
7898
      --  illegal but stream attributes and Convention pragmas are correct.
7899
 
7900
      elsif Has_Private_Component (T) then
7901
         if Nkind (N) = N_Pragma then
7902
            return False;
7903
         else
7904
            Error_Msg_N
7905
              ("representation item must appear after type is fully defined",
7906
                N);
7907
            return True;
7908
         end if;
7909
      else
7910
         return False;
7911
      end if;
7912
   end Rep_Item_Too_Early;
7913
 
7914
   -----------------------
7915
   -- Rep_Item_Too_Late --
7916
   -----------------------
7917
 
7918
   function Rep_Item_Too_Late
7919
     (T     : Entity_Id;
7920
      N     : Node_Id;
7921
      FOnly : Boolean := False) return Boolean
7922
   is
7923
      S           : Entity_Id;
7924
      Parent_Type : Entity_Id;
7925
 
7926
      procedure Too_Late;
7927
      --  Output the too late message. Note that this is not considered a
7928
      --  serious error, since the effect is simply that we ignore the
7929
      --  representation clause in this case.
7930
 
7931
      --------------
7932
      -- Too_Late --
7933
      --------------
7934
 
7935
      procedure Too_Late is
7936
      begin
7937
         Error_Msg_N ("|representation item appears too late!", N);
7938
      end Too_Late;
7939
 
7940
   --  Start of processing for Rep_Item_Too_Late
7941
 
7942
   begin
7943
      --  First make sure entity is not frozen (RM 13.1(9))
7944
 
7945
      if Is_Frozen (T)
7946
 
7947
        --  Exclude imported types, which may be frozen if they appear in a
7948
        --  representation clause for a local type.
7949
 
7950
        and then not From_With_Type (T)
7951
 
7952
        --  Exclude generated entitiesa (not coming from source). The common
7953
        --  case is when we generate a renaming which prematurely freezes the
7954
        --  renamed internal entity, but we still want to be able to set copies
7955
        --  of attribute values such as Size/Alignment.
7956
 
7957
        and then Comes_From_Source (T)
7958
      then
7959
         Too_Late;
7960
         S := First_Subtype (T);
7961
 
7962
         if Present (Freeze_Node (S)) then
7963
            Error_Msg_NE
7964
              ("?no more representation items for }", Freeze_Node (S), S);
7965
         end if;
7966
 
7967
         return True;
7968
 
7969
      --  Check for case of non-tagged derived type whose parent either has
7970
      --  primitive operations, or is a by reference type (RM 13.1(10)).
7971
 
7972
      elsif Is_Type (T)
7973
        and then not FOnly
7974
        and then Is_Derived_Type (T)
7975
        and then not Is_Tagged_Type (T)
7976
      then
7977
         Parent_Type := Etype (Base_Type (T));
7978
 
7979
         if Has_Primitive_Operations (Parent_Type) then
7980
            Too_Late;
7981
            Error_Msg_NE
7982
              ("primitive operations already defined for&!", N, Parent_Type);
7983
            return True;
7984
 
7985
         elsif Is_By_Reference_Type (Parent_Type) then
7986
            Too_Late;
7987
            Error_Msg_NE
7988
              ("parent type & is a by reference type!", N, Parent_Type);
7989
            return True;
7990
         end if;
7991
      end if;
7992
 
7993
      --  No error, link item into head of chain of rep items for the entity,
7994
      --  but avoid chaining if we have an overloadable entity, and the pragma
7995
      --  is one that can apply to multiple overloaded entities.
7996
 
7997
      if Is_Overloadable (T)
7998
        and then Nkind (N) = N_Pragma
7999
      then
8000
         declare
8001
            Pname : constant Name_Id := Pragma_Name (N);
8002
         begin
8003
            if Pname = Name_Convention or else
8004
               Pname = Name_Import     or else
8005
               Pname = Name_Export     or else
8006
               Pname = Name_External   or else
8007
               Pname = Name_Interface
8008
            then
8009
               return False;
8010
            end if;
8011
         end;
8012
      end if;
8013
 
8014
      Record_Rep_Item (T, N);
8015
      return False;
8016
   end Rep_Item_Too_Late;
8017
 
8018
   -------------------------------------
8019
   -- Replace_Type_References_Generic --
8020
   -------------------------------------
8021
 
8022
   procedure Replace_Type_References_Generic (N : Node_Id; TName : Name_Id) is
8023
 
8024
      function Replace_Node (N : Node_Id) return Traverse_Result;
8025
      --  Processes a single node in the traversal procedure below, checking
8026
      --  if node N should be replaced, and if so, doing the replacement.
8027
 
8028
      procedure Replace_Type_Refs is new Traverse_Proc (Replace_Node);
8029
      --  This instantiation provides the body of Replace_Type_References
8030
 
8031
      ------------------
8032
      -- Replace_Node --
8033
      ------------------
8034
 
8035
      function Replace_Node (N : Node_Id) return Traverse_Result is
8036
         S : Entity_Id;
8037
         P : Node_Id;
8038
 
8039
      begin
8040
         --  Case of identifier
8041
 
8042
         if Nkind (N) = N_Identifier then
8043
 
8044
            --  If not the type name, all done with this node
8045
 
8046
            if Chars (N) /= TName then
8047
               return Skip;
8048
 
8049
            --  Otherwise do the replacement and we are done with this node
8050
 
8051
            else
8052
               Replace_Type_Reference (N);
8053
               return Skip;
8054
            end if;
8055
 
8056
         --  Case of selected component (which is what a qualification
8057
         --  looks like in the unanalyzed tree, which is what we have.
8058
 
8059
         elsif Nkind (N) = N_Selected_Component then
8060
 
8061
            --  If selector name is not our type, keeping going (we might
8062
            --  still have an occurrence of the type in the prefix).
8063
 
8064
            if Nkind (Selector_Name (N)) /= N_Identifier
8065
              or else Chars (Selector_Name (N)) /= TName
8066
            then
8067
               return OK;
8068
 
8069
            --  Selector name is our type, check qualification
8070
 
8071
            else
8072
               --  Loop through scopes and prefixes, doing comparison
8073
 
8074
               S := Current_Scope;
8075
               P := Prefix (N);
8076
               loop
8077
                  --  Continue if no more scopes or scope with no name
8078
 
8079
                  if No (S) or else Nkind (S) not in N_Has_Chars then
8080
                     return OK;
8081
                  end if;
8082
 
8083
                  --  Do replace if prefix is an identifier matching the
8084
                  --  scope that we are currently looking at.
8085
 
8086
                  if Nkind (P) = N_Identifier
8087
                    and then Chars (P) = Chars (S)
8088
                  then
8089
                     Replace_Type_Reference (N);
8090
                     return Skip;
8091
                  end if;
8092
 
8093
                  --  Go check scope above us if prefix is itself of the
8094
                  --  form of a selected component, whose selector matches
8095
                  --  the scope we are currently looking at.
8096
 
8097
                  if Nkind (P) = N_Selected_Component
8098
                    and then Nkind (Selector_Name (P)) = N_Identifier
8099
                    and then Chars (Selector_Name (P)) = Chars (S)
8100
                  then
8101
                     S := Scope (S);
8102
                     P := Prefix (P);
8103
 
8104
                  --  For anything else, we don't have a match, so keep on
8105
                  --  going, there are still some weird cases where we may
8106
                  --  still have a replacement within the prefix.
8107
 
8108
                  else
8109
                     return OK;
8110
                  end if;
8111
               end loop;
8112
            end if;
8113
 
8114
            --  Continue for any other node kind
8115
 
8116
         else
8117
            return OK;
8118
         end if;
8119
      end Replace_Node;
8120
 
8121
   begin
8122
      Replace_Type_Refs (N);
8123
   end Replace_Type_References_Generic;
8124
 
8125
   -------------------------
8126
   -- Same_Representation --
8127
   -------------------------
8128
 
8129
   function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean is
8130
      T1 : constant Entity_Id := Underlying_Type (Typ1);
8131
      T2 : constant Entity_Id := Underlying_Type (Typ2);
8132
 
8133
   begin
8134
      --  A quick check, if base types are the same, then we definitely have
8135
      --  the same representation, because the subtype specific representation
8136
      --  attributes (Size and Alignment) do not affect representation from
8137
      --  the point of view of this test.
8138
 
8139
      if Base_Type (T1) = Base_Type (T2) then
8140
         return True;
8141
 
8142
      elsif Is_Private_Type (Base_Type (T2))
8143
        and then Base_Type (T1) = Full_View (Base_Type (T2))
8144
      then
8145
         return True;
8146
      end if;
8147
 
8148
      --  Tagged types never have differing representations
8149
 
8150
      if Is_Tagged_Type (T1) then
8151
         return True;
8152
      end if;
8153
 
8154
      --  Representations are definitely different if conventions differ
8155
 
8156
      if Convention (T1) /= Convention (T2) then
8157
         return False;
8158
      end if;
8159
 
8160
      --  Representations are different if component alignments differ
8161
 
8162
      if (Is_Record_Type (T1) or else Is_Array_Type (T1))
8163
        and then
8164
         (Is_Record_Type (T2) or else Is_Array_Type (T2))
8165
        and then Component_Alignment (T1) /= Component_Alignment (T2)
8166
      then
8167
         return False;
8168
      end if;
8169
 
8170
      --  For arrays, the only real issue is component size. If we know the
8171
      --  component size for both arrays, and it is the same, then that's
8172
      --  good enough to know we don't have a change of representation.
8173
 
8174
      if Is_Array_Type (T1) then
8175
         if Known_Component_Size (T1)
8176
           and then Known_Component_Size (T2)
8177
           and then Component_Size (T1) = Component_Size (T2)
8178
         then
8179
            if VM_Target = No_VM then
8180
               return True;
8181
 
8182
            --  In VM targets the representation of arrays with aliased
8183
            --  components differs from arrays with non-aliased components
8184
 
8185
            else
8186
               return Has_Aliased_Components (Base_Type (T1))
8187
                        =
8188
                      Has_Aliased_Components (Base_Type (T2));
8189
            end if;
8190
         end if;
8191
      end if;
8192
 
8193
      --  Types definitely have same representation if neither has non-standard
8194
      --  representation since default representations are always consistent.
8195
      --  If only one has non-standard representation, and the other does not,
8196
      --  then we consider that they do not have the same representation. They
8197
      --  might, but there is no way of telling early enough.
8198
 
8199
      if Has_Non_Standard_Rep (T1) then
8200
         if not Has_Non_Standard_Rep (T2) then
8201
            return False;
8202
         end if;
8203
      else
8204
         return not Has_Non_Standard_Rep (T2);
8205
      end if;
8206
 
8207
      --  Here the two types both have non-standard representation, and we need
8208
      --  to determine if they have the same non-standard representation.
8209
 
8210
      --  For arrays, we simply need to test if the component sizes are the
8211
      --  same. Pragma Pack is reflected in modified component sizes, so this
8212
      --  check also deals with pragma Pack.
8213
 
8214
      if Is_Array_Type (T1) then
8215
         return Component_Size (T1) = Component_Size (T2);
8216
 
8217
      --  Tagged types always have the same representation, because it is not
8218
      --  possible to specify different representations for common fields.
8219
 
8220
      elsif Is_Tagged_Type (T1) then
8221
         return True;
8222
 
8223
      --  Case of record types
8224
 
8225
      elsif Is_Record_Type (T1) then
8226
 
8227
         --  Packed status must conform
8228
 
8229
         if Is_Packed (T1) /= Is_Packed (T2) then
8230
            return False;
8231
 
8232
         --  Otherwise we must check components. Typ2 maybe a constrained
8233
         --  subtype with fewer components, so we compare the components
8234
         --  of the base types.
8235
 
8236
         else
8237
            Record_Case : declare
8238
               CD1, CD2 : Entity_Id;
8239
 
8240
               function Same_Rep return Boolean;
8241
               --  CD1 and CD2 are either components or discriminants. This
8242
               --  function tests whether the two have the same representation
8243
 
8244
               --------------
8245
               -- Same_Rep --
8246
               --------------
8247
 
8248
               function Same_Rep return Boolean is
8249
               begin
8250
                  if No (Component_Clause (CD1)) then
8251
                     return No (Component_Clause (CD2));
8252
 
8253
                  else
8254
                     return
8255
                        Present (Component_Clause (CD2))
8256
                          and then
8257
                        Component_Bit_Offset (CD1) = Component_Bit_Offset (CD2)
8258
                          and then
8259
                        Esize (CD1) = Esize (CD2);
8260
                  end if;
8261
               end Same_Rep;
8262
 
8263
            --  Start of processing for Record_Case
8264
 
8265
            begin
8266
               if Has_Discriminants (T1) then
8267
                  CD1 := First_Discriminant (T1);
8268
                  CD2 := First_Discriminant (T2);
8269
 
8270
                  --  The number of discriminants may be different if the
8271
                  --  derived type has fewer (constrained by values). The
8272
                  --  invisible discriminants retain the representation of
8273
                  --  the original, so the discrepancy does not per se
8274
                  --  indicate a different representation.
8275
 
8276
                  while Present (CD1)
8277
                    and then Present (CD2)
8278
                  loop
8279
                     if not Same_Rep then
8280
                        return False;
8281
                     else
8282
                        Next_Discriminant (CD1);
8283
                        Next_Discriminant (CD2);
8284
                     end if;
8285
                  end loop;
8286
               end if;
8287
 
8288
               CD1 := First_Component (Underlying_Type (Base_Type (T1)));
8289
               CD2 := First_Component (Underlying_Type (Base_Type (T2)));
8290
 
8291
               while Present (CD1) loop
8292
                  if not Same_Rep then
8293
                     return False;
8294
                  else
8295
                     Next_Component (CD1);
8296
                     Next_Component (CD2);
8297
                  end if;
8298
               end loop;
8299
 
8300
               return True;
8301
            end Record_Case;
8302
         end if;
8303
 
8304
      --  For enumeration types, we must check each literal to see if the
8305
      --  representation is the same. Note that we do not permit enumeration
8306
      --  representation clauses for Character and Wide_Character, so these
8307
      --  cases were already dealt with.
8308
 
8309
      elsif Is_Enumeration_Type (T1) then
8310
         Enumeration_Case : declare
8311
            L1, L2 : Entity_Id;
8312
 
8313
         begin
8314
            L1 := First_Literal (T1);
8315
            L2 := First_Literal (T2);
8316
 
8317
            while Present (L1) loop
8318
               if Enumeration_Rep (L1) /= Enumeration_Rep (L2) then
8319
                  return False;
8320
               else
8321
                  Next_Literal (L1);
8322
                  Next_Literal (L2);
8323
               end if;
8324
            end loop;
8325
 
8326
            return True;
8327
 
8328
         end Enumeration_Case;
8329
 
8330
      --  Any other types have the same representation for these purposes
8331
 
8332
      else
8333
         return True;
8334
      end if;
8335
   end Same_Representation;
8336
 
8337
   ----------------
8338
   -- Set_Biased --
8339
   ----------------
8340
 
8341
   procedure Set_Biased
8342
     (E      : Entity_Id;
8343
      N      : Node_Id;
8344
      Msg    : String;
8345
      Biased : Boolean := True)
8346
   is
8347
   begin
8348
      if Biased then
8349
         Set_Has_Biased_Representation (E);
8350
 
8351
         if Warn_On_Biased_Representation then
8352
            Error_Msg_NE
8353
              ("?" & Msg & " forces biased representation for&", N, E);
8354
         end if;
8355
      end if;
8356
   end Set_Biased;
8357
 
8358
   --------------------
8359
   -- Set_Enum_Esize --
8360
   --------------------
8361
 
8362
   procedure Set_Enum_Esize (T : Entity_Id) is
8363
      Lo : Uint;
8364
      Hi : Uint;
8365
      Sz : Nat;
8366
 
8367
   begin
8368
      Init_Alignment (T);
8369
 
8370
      --  Find the minimum standard size (8,16,32,64) that fits
8371
 
8372
      Lo := Enumeration_Rep (Entity (Type_Low_Bound (T)));
8373
      Hi := Enumeration_Rep (Entity (Type_High_Bound (T)));
8374
 
8375
      if Lo < 0 then
8376
         if Lo >= -Uint_2**07 and then Hi < Uint_2**07 then
8377
            Sz := Standard_Character_Size;  -- May be > 8 on some targets
8378
 
8379
         elsif Lo >= -Uint_2**15 and then Hi < Uint_2**15 then
8380
            Sz := 16;
8381
 
8382
         elsif Lo >= -Uint_2**31 and then Hi < Uint_2**31 then
8383
            Sz := 32;
8384
 
8385
         else pragma Assert (Lo >= -Uint_2**63 and then Hi < Uint_2**63);
8386
            Sz := 64;
8387
         end if;
8388
 
8389
      else
8390
         if Hi < Uint_2**08 then
8391
            Sz := Standard_Character_Size;  -- May be > 8 on some targets
8392
 
8393
         elsif Hi < Uint_2**16 then
8394
            Sz := 16;
8395
 
8396
         elsif Hi < Uint_2**32 then
8397
            Sz := 32;
8398
 
8399
         else pragma Assert (Hi < Uint_2**63);
8400
            Sz := 64;
8401
         end if;
8402
      end if;
8403
 
8404
      --  That minimum is the proper size unless we have a foreign convention
8405
      --  and the size required is 32 or less, in which case we bump the size
8406
      --  up to 32. This is required for C and C++ and seems reasonable for
8407
      --  all other foreign conventions.
8408
 
8409
      if Has_Foreign_Convention (T)
8410
        and then Esize (T) < Standard_Integer_Size
8411
      then
8412
         Init_Esize (T, Standard_Integer_Size);
8413
      else
8414
         Init_Esize (T, Sz);
8415
      end if;
8416
   end Set_Enum_Esize;
8417
 
8418
   ------------------------------
8419
   -- Validate_Address_Clauses --
8420
   ------------------------------
8421
 
8422
   procedure Validate_Address_Clauses is
8423
   begin
8424
      for J in Address_Clause_Checks.First .. Address_Clause_Checks.Last loop
8425
         declare
8426
            ACCR : Address_Clause_Check_Record
8427
                     renames Address_Clause_Checks.Table (J);
8428
 
8429
            Expr : Node_Id;
8430
 
8431
            X_Alignment : Uint;
8432
            Y_Alignment : Uint;
8433
 
8434
            X_Size : Uint;
8435
            Y_Size : Uint;
8436
 
8437
         begin
8438
            --  Skip processing of this entry if warning already posted
8439
 
8440
            if not Address_Warning_Posted (ACCR.N) then
8441
 
8442
               Expr := Original_Node (Expression (ACCR.N));
8443
 
8444
               --  Get alignments
8445
 
8446
               X_Alignment := Alignment (ACCR.X);
8447
               Y_Alignment := Alignment (ACCR.Y);
8448
 
8449
               --  Similarly obtain sizes
8450
 
8451
               X_Size := Esize (ACCR.X);
8452
               Y_Size := Esize (ACCR.Y);
8453
 
8454
               --  Check for large object overlaying smaller one
8455
 
8456
               if Y_Size > Uint_0
8457
                 and then X_Size > Uint_0
8458
                 and then X_Size > Y_Size
8459
               then
8460
                  Error_Msg_NE
8461
                    ("?& overlays smaller object", ACCR.N, ACCR.X);
8462
                  Error_Msg_N
8463
                    ("\?program execution may be erroneous", ACCR.N);
8464
                  Error_Msg_Uint_1 := X_Size;
8465
                  Error_Msg_NE
8466
                    ("\?size of & is ^", ACCR.N, ACCR.X);
8467
                  Error_Msg_Uint_1 := Y_Size;
8468
                  Error_Msg_NE
8469
                    ("\?size of & is ^", ACCR.N, ACCR.Y);
8470
 
8471
               --  Check for inadequate alignment, both of the base object
8472
               --  and of the offset, if any.
8473
 
8474
               --  Note: we do not check the alignment if we gave a size
8475
               --  warning, since it would likely be redundant.
8476
 
8477
               elsif Y_Alignment /= Uint_0
8478
                 and then (Y_Alignment < X_Alignment
8479
                             or else (ACCR.Off
8480
                                        and then
8481
                                          Nkind (Expr) = N_Attribute_Reference
8482
                                        and then
8483
                                          Attribute_Name (Expr) = Name_Address
8484
                                        and then
8485
                                          Has_Compatible_Alignment
8486
                                            (ACCR.X, Prefix (Expr))
8487
                                             /= Known_Compatible))
8488
               then
8489
                  Error_Msg_NE
8490
                    ("?specified address for& may be inconsistent "
8491
                       & "with alignment",
8492
                     ACCR.N, ACCR.X);
8493
                  Error_Msg_N
8494
                    ("\?program execution may be erroneous (RM 13.3(27))",
8495
                     ACCR.N);
8496
                  Error_Msg_Uint_1 := X_Alignment;
8497
                  Error_Msg_NE
8498
                    ("\?alignment of & is ^",
8499
                     ACCR.N, ACCR.X);
8500
                  Error_Msg_Uint_1 := Y_Alignment;
8501
                  Error_Msg_NE
8502
                    ("\?alignment of & is ^",
8503
                     ACCR.N, ACCR.Y);
8504
                  if Y_Alignment >= X_Alignment then
8505
                     Error_Msg_N
8506
                      ("\?but offset is not multiple of alignment",
8507
                       ACCR.N);
8508
                  end if;
8509
               end if;
8510
            end if;
8511
         end;
8512
      end loop;
8513
   end Validate_Address_Clauses;
8514
 
8515
   ---------------------------
8516
   -- Validate_Independence --
8517
   ---------------------------
8518
 
8519
   procedure Validate_Independence is
8520
      SU   : constant Uint := UI_From_Int (System_Storage_Unit);
8521
      N    : Node_Id;
8522
      E    : Entity_Id;
8523
      IC   : Boolean;
8524
      Comp : Entity_Id;
8525
      Addr : Node_Id;
8526
      P    : Node_Id;
8527
 
8528
      procedure Check_Array_Type (Atyp : Entity_Id);
8529
      --  Checks if the array type Atyp has independent components, and
8530
      --  if not, outputs an appropriate set of error messages.
8531
 
8532
      procedure No_Independence;
8533
      --  Output message that independence cannot be guaranteed
8534
 
8535
      function OK_Component (C : Entity_Id) return Boolean;
8536
      --  Checks one component to see if it is independently accessible, and
8537
      --  if so yields True, otherwise yields False if independent access
8538
      --  cannot be guaranteed. This is a conservative routine, it only
8539
      --  returns True if it knows for sure, it returns False if it knows
8540
      --  there is a problem, or it cannot be sure there is no problem.
8541
 
8542
      procedure Reason_Bad_Component (C : Entity_Id);
8543
      --  Outputs continuation message if a reason can be determined for
8544
      --  the component C being bad.
8545
 
8546
      ----------------------
8547
      -- Check_Array_Type --
8548
      ----------------------
8549
 
8550
      procedure Check_Array_Type (Atyp : Entity_Id) is
8551
         Ctyp : constant Entity_Id := Component_Type (Atyp);
8552
 
8553
      begin
8554
         --  OK if no alignment clause, no pack, and no component size
8555
 
8556
         if not Has_Component_Size_Clause (Atyp)
8557
           and then not Has_Alignment_Clause (Atyp)
8558
           and then not Is_Packed (Atyp)
8559
         then
8560
            return;
8561
         end if;
8562
 
8563
         --  Check actual component size
8564
 
8565
         if not Known_Component_Size (Atyp)
8566
           or else not (Addressable (Component_Size (Atyp))
8567
                          and then Component_Size (Atyp) < 64)
8568
           or else Component_Size (Atyp) mod Esize (Ctyp) /= 0
8569
         then
8570
            No_Independence;
8571
 
8572
            --  Bad component size, check reason
8573
 
8574
            if Has_Component_Size_Clause (Atyp) then
8575
               P :=
8576
                 Get_Attribute_Definition_Clause
8577
                   (Atyp, Attribute_Component_Size);
8578
 
8579
               if Present (P) then
8580
                  Error_Msg_Sloc := Sloc (P);
8581
                  Error_Msg_N ("\because of Component_Size clause#", N);
8582
                  return;
8583
               end if;
8584
            end if;
8585
 
8586
            if Is_Packed (Atyp) then
8587
               P := Get_Rep_Pragma (Atyp, Name_Pack);
8588
 
8589
               if Present (P) then
8590
                  Error_Msg_Sloc := Sloc (P);
8591
                  Error_Msg_N ("\because of pragma Pack#", N);
8592
                  return;
8593
               end if;
8594
            end if;
8595
 
8596
            --  No reason found, just return
8597
 
8598
            return;
8599
         end if;
8600
 
8601
         --  Array type is OK independence-wise
8602
 
8603
         return;
8604
      end Check_Array_Type;
8605
 
8606
      ---------------------
8607
      -- No_Independence --
8608
      ---------------------
8609
 
8610
      procedure No_Independence is
8611
      begin
8612
         if Pragma_Name (N) = Name_Independent then
8613
            Error_Msg_NE
8614
              ("independence cannot be guaranteed for&", N, E);
8615
         else
8616
            Error_Msg_NE
8617
              ("independent components cannot be guaranteed for&", N, E);
8618
         end if;
8619
      end No_Independence;
8620
 
8621
      ------------------
8622
      -- OK_Component --
8623
      ------------------
8624
 
8625
      function OK_Component (C : Entity_Id) return Boolean is
8626
         Rec  : constant Entity_Id := Scope (C);
8627
         Ctyp : constant Entity_Id := Etype (C);
8628
 
8629
      begin
8630
         --  OK if no component clause, no Pack, and no alignment clause
8631
 
8632
         if No (Component_Clause (C))
8633
           and then not Is_Packed (Rec)
8634
           and then not Has_Alignment_Clause (Rec)
8635
         then
8636
            return True;
8637
         end if;
8638
 
8639
         --  Here we look at the actual component layout. A component is
8640
         --  addressable if its size is a multiple of the Esize of the
8641
         --  component type, and its starting position in the record has
8642
         --  appropriate alignment, and the record itself has appropriate
8643
         --  alignment to guarantee the component alignment.
8644
 
8645
         --  Make sure sizes are static, always assume the worst for any
8646
         --  cases where we cannot check static values.
8647
 
8648
         if not (Known_Static_Esize (C)
8649
                  and then Known_Static_Esize (Ctyp))
8650
         then
8651
            return False;
8652
         end if;
8653
 
8654
         --  Size of component must be addressable or greater than 64 bits
8655
         --  and a multiple of bytes.
8656
 
8657
         if not Addressable (Esize (C))
8658
           and then Esize (C) < Uint_64
8659
         then
8660
            return False;
8661
         end if;
8662
 
8663
         --  Check size is proper multiple
8664
 
8665
         if Esize (C) mod Esize (Ctyp) /= 0 then
8666
            return False;
8667
         end if;
8668
 
8669
         --  Check alignment of component is OK
8670
 
8671
         if not Known_Component_Bit_Offset (C)
8672
           or else Component_Bit_Offset (C) < Uint_0
8673
           or else Component_Bit_Offset (C) mod Esize (Ctyp) /= 0
8674
         then
8675
            return False;
8676
         end if;
8677
 
8678
         --  Check alignment of record type is OK
8679
 
8680
         if not Known_Alignment (Rec)
8681
           or else (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
8682
         then
8683
            return False;
8684
         end if;
8685
 
8686
         --  All tests passed, component is addressable
8687
 
8688
         return True;
8689
      end OK_Component;
8690
 
8691
      --------------------------
8692
      -- Reason_Bad_Component --
8693
      --------------------------
8694
 
8695
      procedure Reason_Bad_Component (C : Entity_Id) is
8696
         Rec  : constant Entity_Id := Scope (C);
8697
         Ctyp : constant Entity_Id := Etype (C);
8698
 
8699
      begin
8700
         --  If component clause present assume that's the problem
8701
 
8702
         if Present (Component_Clause (C)) then
8703
            Error_Msg_Sloc := Sloc (Component_Clause (C));
8704
            Error_Msg_N ("\because of Component_Clause#", N);
8705
            return;
8706
         end if;
8707
 
8708
         --  If pragma Pack clause present, assume that's the problem
8709
 
8710
         if Is_Packed (Rec) then
8711
            P := Get_Rep_Pragma (Rec, Name_Pack);
8712
 
8713
            if Present (P) then
8714
               Error_Msg_Sloc := Sloc (P);
8715
               Error_Msg_N ("\because of pragma Pack#", N);
8716
               return;
8717
            end if;
8718
         end if;
8719
 
8720
         --  See if record has bad alignment clause
8721
 
8722
         if Has_Alignment_Clause (Rec)
8723
           and then Known_Alignment (Rec)
8724
           and then (Alignment (Rec) * SU) mod Esize (Ctyp) /= 0
8725
         then
8726
            P := Get_Attribute_Definition_Clause (Rec, Attribute_Alignment);
8727
 
8728
            if Present (P) then
8729
               Error_Msg_Sloc := Sloc (P);
8730
               Error_Msg_N ("\because of Alignment clause#", N);
8731
            end if;
8732
         end if;
8733
 
8734
         --  Couldn't find a reason, so return without a message
8735
 
8736
         return;
8737
      end Reason_Bad_Component;
8738
 
8739
   --  Start of processing for Validate_Independence
8740
 
8741
   begin
8742
      for J in Independence_Checks.First .. Independence_Checks.Last loop
8743
         N  := Independence_Checks.Table (J).N;
8744
         E  := Independence_Checks.Table (J).E;
8745
         IC := Pragma_Name (N) = Name_Independent_Components;
8746
 
8747
         --  Deal with component case
8748
 
8749
         if Ekind (E) = E_Discriminant or else Ekind (E) = E_Component then
8750
            if not OK_Component (E) then
8751
               No_Independence;
8752
               Reason_Bad_Component (E);
8753
               goto Continue;
8754
            end if;
8755
         end if;
8756
 
8757
         --  Deal with record with Independent_Components
8758
 
8759
         if IC and then Is_Record_Type (E) then
8760
            Comp := First_Component_Or_Discriminant (E);
8761
            while Present (Comp) loop
8762
               if not OK_Component (Comp) then
8763
                  No_Independence;
8764
                  Reason_Bad_Component (Comp);
8765
                  goto Continue;
8766
               end if;
8767
 
8768
               Next_Component_Or_Discriminant (Comp);
8769
            end loop;
8770
         end if;
8771
 
8772
         --  Deal with address clause case
8773
 
8774
         if Is_Object (E) then
8775
            Addr := Address_Clause (E);
8776
 
8777
            if Present (Addr) then
8778
               No_Independence;
8779
               Error_Msg_Sloc := Sloc (Addr);
8780
               Error_Msg_N ("\because of Address clause#", N);
8781
               goto Continue;
8782
            end if;
8783
         end if;
8784
 
8785
         --  Deal with independent components for array type
8786
 
8787
         if IC and then Is_Array_Type (E) then
8788
            Check_Array_Type (E);
8789
         end if;
8790
 
8791
         --  Deal with independent components for array object
8792
 
8793
         if IC and then Is_Object (E) and then Is_Array_Type (Etype (E)) then
8794
            Check_Array_Type (Etype (E));
8795
         end if;
8796
 
8797
      <<Continue>> null;
8798
      end loop;
8799
   end Validate_Independence;
8800
 
8801
   -----------------------------------
8802
   -- Validate_Unchecked_Conversion --
8803
   -----------------------------------
8804
 
8805
   procedure Validate_Unchecked_Conversion
8806
     (N        : Node_Id;
8807
      Act_Unit : Entity_Id)
8808
   is
8809
      Source : Entity_Id;
8810
      Target : Entity_Id;
8811
      Vnode  : Node_Id;
8812
 
8813
   begin
8814
      --  Obtain source and target types. Note that we call Ancestor_Subtype
8815
      --  here because the processing for generic instantiation always makes
8816
      --  subtypes, and we want the original frozen actual types.
8817
 
8818
      --  If we are dealing with private types, then do the check on their
8819
      --  fully declared counterparts if the full declarations have been
8820
      --  encountered (they don't have to be visible, but they must exist!)
8821
 
8822
      Source := Ancestor_Subtype (Etype (First_Formal (Act_Unit)));
8823
 
8824
      if Is_Private_Type (Source)
8825
        and then Present (Underlying_Type (Source))
8826
      then
8827
         Source := Underlying_Type (Source);
8828
      end if;
8829
 
8830
      Target := Ancestor_Subtype (Etype (Act_Unit));
8831
 
8832
      --  If either type is generic, the instantiation happens within a generic
8833
      --  unit, and there is nothing to check. The proper check will happen
8834
      --  when the enclosing generic is instantiated.
8835
 
8836
      if Is_Generic_Type (Source) or else Is_Generic_Type (Target) then
8837
         return;
8838
      end if;
8839
 
8840
      if Is_Private_Type (Target)
8841
        and then Present (Underlying_Type (Target))
8842
      then
8843
         Target := Underlying_Type (Target);
8844
      end if;
8845
 
8846
      --  Source may be unconstrained array, but not target
8847
 
8848
      if Is_Array_Type (Target)
8849
        and then not Is_Constrained (Target)
8850
      then
8851
         Error_Msg_N
8852
           ("unchecked conversion to unconstrained array not allowed", N);
8853
         return;
8854
      end if;
8855
 
8856
      --  Warn if conversion between two different convention pointers
8857
 
8858
      if Is_Access_Type (Target)
8859
        and then Is_Access_Type (Source)
8860
        and then Convention (Target) /= Convention (Source)
8861
        and then Warn_On_Unchecked_Conversion
8862
      then
8863
         --  Give warnings for subprogram pointers only on most targets. The
8864
         --  exception is VMS, where data pointers can have different lengths
8865
         --  depending on the pointer convention.
8866
 
8867
         if Is_Access_Subprogram_Type (Target)
8868
           or else Is_Access_Subprogram_Type (Source)
8869
           or else OpenVMS_On_Target
8870
         then
8871
            Error_Msg_N
8872
              ("?conversion between pointers with different conventions!", N);
8873
         end if;
8874
      end if;
8875
 
8876
      --  Warn if one of the operands is Ada.Calendar.Time. Do not emit a
8877
      --  warning when compiling GNAT-related sources.
8878
 
8879
      if Warn_On_Unchecked_Conversion
8880
        and then not In_Predefined_Unit (N)
8881
        and then RTU_Loaded (Ada_Calendar)
8882
        and then
8883
          (Chars (Source) = Name_Time
8884
             or else
8885
           Chars (Target) = Name_Time)
8886
      then
8887
         --  If Ada.Calendar is loaded and the name of one of the operands is
8888
         --  Time, there is a good chance that this is Ada.Calendar.Time.
8889
 
8890
         declare
8891
            Calendar_Time : constant Entity_Id :=
8892
                              Full_View (RTE (RO_CA_Time));
8893
         begin
8894
            pragma Assert (Present (Calendar_Time));
8895
 
8896
            if Source = Calendar_Time
8897
              or else Target = Calendar_Time
8898
            then
8899
               Error_Msg_N
8900
                 ("?representation of 'Time values may change between " &
8901
                  "'G'N'A'T versions", N);
8902
            end if;
8903
         end;
8904
      end if;
8905
 
8906
      --  Make entry in unchecked conversion table for later processing by
8907
      --  Validate_Unchecked_Conversions, which will check sizes and alignments
8908
      --  (using values set by the back-end where possible). This is only done
8909
      --  if the appropriate warning is active.
8910
 
8911
      if Warn_On_Unchecked_Conversion then
8912
         Unchecked_Conversions.Append
8913
           (New_Val => UC_Entry'
8914
              (Eloc   => Sloc (N),
8915
               Source => Source,
8916
               Target => Target));
8917
 
8918
         --  If both sizes are known statically now, then back end annotation
8919
         --  is not required to do a proper check but if either size is not
8920
         --  known statically, then we need the annotation.
8921
 
8922
         if Known_Static_RM_Size (Source)
8923
           and then Known_Static_RM_Size (Target)
8924
         then
8925
            null;
8926
         else
8927
            Back_Annotate_Rep_Info := True;
8928
         end if;
8929
      end if;
8930
 
8931
      --  If unchecked conversion to access type, and access type is declared
8932
      --  in the same unit as the unchecked conversion, then set the flag
8933
      --  No_Strict_Aliasing (no strict aliasing is implicit here)
8934
 
8935
      if Is_Access_Type (Target) and then
8936
        In_Same_Source_Unit (Target, N)
8937
      then
8938
         Set_No_Strict_Aliasing (Implementation_Base_Type (Target));
8939
      end if;
8940
 
8941
      --  Generate N_Validate_Unchecked_Conversion node for back end in case
8942
      --  the back end needs to perform special validation checks.
8943
 
8944
      --  Shouldn't this be in Exp_Ch13, since the check only gets done if we
8945
      --  have full expansion and the back end is called ???
8946
 
8947
      Vnode :=
8948
        Make_Validate_Unchecked_Conversion (Sloc (N));
8949
      Set_Source_Type (Vnode, Source);
8950
      Set_Target_Type (Vnode, Target);
8951
 
8952
      --  If the unchecked conversion node is in a list, just insert before it.
8953
      --  If not we have some strange case, not worth bothering about.
8954
 
8955
      if Is_List_Member (N) then
8956
         Insert_After (N, Vnode);
8957
      end if;
8958
   end Validate_Unchecked_Conversion;
8959
 
8960
   ------------------------------------
8961
   -- Validate_Unchecked_Conversions --
8962
   ------------------------------------
8963
 
8964
   procedure Validate_Unchecked_Conversions is
8965
   begin
8966
      for N in Unchecked_Conversions.First .. Unchecked_Conversions.Last loop
8967
         declare
8968
            T : UC_Entry renames Unchecked_Conversions.Table (N);
8969
 
8970
            Eloc   : constant Source_Ptr := T.Eloc;
8971
            Source : constant Entity_Id  := T.Source;
8972
            Target : constant Entity_Id  := T.Target;
8973
 
8974
            Source_Siz : Uint;
8975
            Target_Siz : Uint;
8976
 
8977
         begin
8978
            --  This validation check, which warns if we have unequal sizes for
8979
            --  unchecked conversion, and thus potentially implementation
8980
            --  dependent semantics, is one of the few occasions on which we
8981
            --  use the official RM size instead of Esize. See description in
8982
            --  Einfo "Handling of Type'Size Values" for details.
8983
 
8984
            if Serious_Errors_Detected = 0
8985
              and then Known_Static_RM_Size (Source)
8986
              and then Known_Static_RM_Size (Target)
8987
 
8988
              --  Don't do the check if warnings off for either type, note the
8989
              --  deliberate use of OR here instead of OR ELSE to get the flag
8990
              --  Warnings_Off_Used set for both types if appropriate.
8991
 
8992
              and then not (Has_Warnings_Off (Source)
8993
                              or
8994
                            Has_Warnings_Off (Target))
8995
            then
8996
               Source_Siz := RM_Size (Source);
8997
               Target_Siz := RM_Size (Target);
8998
 
8999
               if Source_Siz /= Target_Siz then
9000
                  Error_Msg
9001
                    ("?types for unchecked conversion have different sizes!",
9002
                     Eloc);
9003
 
9004
                  if All_Errors_Mode then
9005
                     Error_Msg_Name_1 := Chars (Source);
9006
                     Error_Msg_Uint_1 := Source_Siz;
9007
                     Error_Msg_Name_2 := Chars (Target);
9008
                     Error_Msg_Uint_2 := Target_Siz;
9009
                     Error_Msg ("\size of % is ^, size of % is ^?", Eloc);
9010
 
9011
                     Error_Msg_Uint_1 := UI_Abs (Source_Siz - Target_Siz);
9012
 
9013
                     if Is_Discrete_Type (Source)
9014
                       and then Is_Discrete_Type (Target)
9015
                     then
9016
                        if Source_Siz > Target_Siz then
9017
                           Error_Msg
9018
                             ("\?^ high order bits of source will be ignored!",
9019
                              Eloc);
9020
 
9021
                        elsif Is_Unsigned_Type (Source) then
9022
                           Error_Msg
9023
                             ("\?source will be extended with ^ high order " &
9024
                              "zero bits?!", Eloc);
9025
 
9026
                        else
9027
                           Error_Msg
9028
                             ("\?source will be extended with ^ high order " &
9029
                              "sign bits!",
9030
                              Eloc);
9031
                        end if;
9032
 
9033
                     elsif Source_Siz < Target_Siz then
9034
                        if Is_Discrete_Type (Target) then
9035
                           if Bytes_Big_Endian then
9036
                              Error_Msg
9037
                                ("\?target value will include ^ undefined " &
9038
                                 "low order bits!",
9039
                                 Eloc);
9040
                           else
9041
                              Error_Msg
9042
                                ("\?target value will include ^ undefined " &
9043
                                 "high order bits!",
9044
                                 Eloc);
9045
                           end if;
9046
 
9047
                        else
9048
                           Error_Msg
9049
                             ("\?^ trailing bits of target value will be " &
9050
                              "undefined!", Eloc);
9051
                        end if;
9052
 
9053
                     else pragma Assert (Source_Siz > Target_Siz);
9054
                        Error_Msg
9055
                          ("\?^ trailing bits of source will be ignored!",
9056
                           Eloc);
9057
                     end if;
9058
                  end if;
9059
               end if;
9060
            end if;
9061
 
9062
            --  If both types are access types, we need to check the alignment.
9063
            --  If the alignment of both is specified, we can do it here.
9064
 
9065
            if Serious_Errors_Detected = 0
9066
              and then Ekind (Source) in Access_Kind
9067
              and then Ekind (Target) in Access_Kind
9068
              and then Target_Strict_Alignment
9069
              and then Present (Designated_Type (Source))
9070
              and then Present (Designated_Type (Target))
9071
            then
9072
               declare
9073
                  D_Source : constant Entity_Id := Designated_Type (Source);
9074
                  D_Target : constant Entity_Id := Designated_Type (Target);
9075
 
9076
               begin
9077
                  if Known_Alignment (D_Source)
9078
                    and then Known_Alignment (D_Target)
9079
                  then
9080
                     declare
9081
                        Source_Align : constant Uint := Alignment (D_Source);
9082
                        Target_Align : constant Uint := Alignment (D_Target);
9083
 
9084
                     begin
9085
                        if Source_Align < Target_Align
9086
                          and then not Is_Tagged_Type (D_Source)
9087
 
9088
                          --  Suppress warning if warnings suppressed on either
9089
                          --  type or either designated type. Note the use of
9090
                          --  OR here instead of OR ELSE. That is intentional,
9091
                          --  we would like to set flag Warnings_Off_Used in
9092
                          --  all types for which warnings are suppressed.
9093
 
9094
                          and then not (Has_Warnings_Off (D_Source)
9095
                                          or
9096
                                        Has_Warnings_Off (D_Target)
9097
                                          or
9098
                                        Has_Warnings_Off (Source)
9099
                                          or
9100
                                        Has_Warnings_Off (Target))
9101
                        then
9102
                           Error_Msg_Uint_1 := Target_Align;
9103
                           Error_Msg_Uint_2 := Source_Align;
9104
                           Error_Msg_Node_1 := D_Target;
9105
                           Error_Msg_Node_2 := D_Source;
9106
                           Error_Msg
9107
                             ("?alignment of & (^) is stricter than " &
9108
                              "alignment of & (^)!", Eloc);
9109
                           Error_Msg
9110
                             ("\?resulting access value may have invalid " &
9111
                              "alignment!", Eloc);
9112
                        end if;
9113
                     end;
9114
                  end if;
9115
               end;
9116
            end if;
9117
         end;
9118
      end loop;
9119
   end Validate_Unchecked_Conversions;
9120
 
9121
end Sem_Ch13;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.