1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             S E M _ C H 1 3                              --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2019, 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
26with Table;
27with Types; use Types;
28with Uintp; use Uintp;
29
30package Sem_Ch13 is
31   procedure Analyze_At_Clause                          (N : Node_Id);
32   procedure Analyze_Attribute_Definition_Clause        (N : Node_Id);
33   procedure Analyze_Enumeration_Representation_Clause  (N : Node_Id);
34   procedure Analyze_Free_Statement                     (N : Node_Id);
35   procedure Analyze_Freeze_Entity                      (N : Node_Id);
36   procedure Analyze_Freeze_Generic_Entity              (N : Node_Id);
37   procedure Analyze_Record_Representation_Clause       (N : Node_Id);
38   procedure Analyze_Code_Statement                     (N : Node_Id);
39
40   procedure Analyze_Aspect_Specifications (N : Node_Id; E : Entity_Id);
41   --  This procedure is called to analyze aspect specifications for node N. E
42   --  is the corresponding entity declared by the declaration node N. Callers
43   --  should check that Has_Aspects (N) is True before calling this routine.
44
45   procedure Analyze_Aspects_On_Subprogram_Body_Or_Stub (N : Node_Id);
46   --  Analyze the aspect specifications of [generic] subprogram body or stub
47   --  N. Callers should check that Has_Aspects (N) is True before calling the
48   --  routine. This routine diagnoses misplaced aspects that should appear on
49   --  the initial declaration of N and offers suggestions for replacements.
50
51   procedure Adjust_Record_For_Reverse_Bit_Order (R : Entity_Id);
52   --  Called from Freeze where R is a record entity for which reverse bit
53   --  order is specified and there is at least one component clause. Note:
54   --  component positions are normally adjusted as per AI95-0133, unless
55   --  -gnatd.p is used to restore original Ada 95 mode.
56
57   procedure Check_Record_Representation_Clause (N : Node_Id);
58   --  This procedure completes the analysis of a record representation clause
59   --  N. It is called at freeze time after adjustment of component clause bit
60   --  positions for possible non-standard bit order. In the case of Ada 2005
61   --  (machine scalar) mode, this adjustment can make substantial changes, so
62   --  some checks, in particular for component overlaps cannot be done at the
63   --  time the record representation clause is first seen, but must be delayed
64   --  till freeze time, and in particular is called after calling the above
65   --  procedure for adjusting record bit positions for reverse bit order.
66
67   procedure Initialize;
68   --  Initialize internal tables for new compilation
69
70   procedure Kill_Rep_Clause (N : Node_Id);
71   --  This procedure is called for a rep clause N when we are in -gnatI mode
72   --  (Ignore_Rep_Clauses). It replaces the node N with a null statement. This
73   --  is only called if Ignore_Rep_Clauses is True.
74
75   procedure Set_Enum_Esize (T : Entity_Id);
76   --  This routine sets the Esize field for an enumeration type T, based
77   --  on the current representation information available for T. Note that
78   --  the setting of the RM_Size field is not affected. This routine also
79   --  initializes the alignment field to zero.
80
81   function Minimum_Size
82     (T      : Entity_Id;
83      Biased : Boolean := False) return Nat;
84   --  Given an elementary type, determines the minimum number of bits required
85   --  to represent all values of the type. This function may not be called
86   --  with any other types. If the flag Biased is set True, then the minimum
87   --  size calculation that biased representation is used in the case of a
88   --  discrete type, e.g. the range 7..8 gives a minimum size of 4 with
89   --  Biased set to False, and 1 with Biased set to True. Note that the
90   --  biased parameter only has an effect if the type is not biased, it
91   --  causes Minimum_Size to indicate the minimum size of an object with
92   --  the given type, of the size the type would have if it were biased. If
93   --  the type is already biased, then Minimum_Size returns the biased size,
94   --  regardless of the setting of Biased. Also, fixed-point types are never
95   --  biased in the current implementation. If the size is not known at
96   --  compile time, this function returns 0.
97
98   procedure Check_Constant_Address_Clause (Expr : Node_Id; U_Ent : Entity_Id);
99   --  Expr is an expression for an address clause. This procedure checks
100   --  that the expression is constant, in the limited sense that it is safe
101   --  to evaluate it at the point the object U_Ent is declared, rather than
102   --  at the point of the address clause. The condition for this to be true
103   --  is that the expression has no variables, no constants declared after
104   --  U_Ent, and no calls to non-pure functions. If this condition is not
105   --  met, then an appropriate error message is posted. This check is applied
106   --  at the point an object with an address clause is frozen, as well as for
107   --  address clauses for tasks and entries.
108
109   procedure Check_Size
110     (N      : Node_Id;
111      T      : Entity_Id;
112      Siz    : Uint;
113      Biased : out Boolean);
114   --  Called when size Siz is specified for subtype T. This subprogram checks
115   --  that the size is appropriate, posting errors on node N as required.
116   --  This check is effective for elementary types and bit-packed arrays.
117   --  For other non-elementary types, a check is only made if an explicit
118   --  size has been given for the type (and the specified size must match).
119   --  The parameter Biased is set False if the size specified did not require
120   --  the use of biased representation, and True if biased representation
121   --  was required to meet the size requirement. Note that Biased is only
122   --  set if the type is not currently biased, but biasing it is the only
123   --  way to meet the requirement. If the type is currently biased, then
124   --  this biased size is used in the initial check, and Biased is False.
125   --  If the size is too small, and an error message is given, then both
126   --  Esize and RM_Size are reset to the allowed minimum value in T.
127
128   function Rep_Item_Too_Early (T : Entity_Id; N : Node_Id) return Boolean;
129   --  Called at start of processing a representation clause/pragma. Used to
130   --  check that the representation item is not being applied to an incomplete
131   --  type or to a generic formal type or a type derived from a generic formal
132   --  type. Returns False if no such error occurs. If this error does occur,
133   --  appropriate error messages are posted on node N, and True is returned.
134
135   generic
136      with procedure Replace_Type_Reference (N : Node_Id);
137   procedure Replace_Type_References_Generic (N : Node_Id; T : Entity_Id);
138   --  This is used to scan an expression for a predicate or invariant aspect
139   --  replacing occurrences of the name of the subtype to which the aspect
140   --  applies with appropriate references to the parameter of the predicate
141   --  function or invariant procedure. The procedure passed as a generic
142   --  parameter does the actual replacement of node N, which is either a
143   --  simple direct reference to T, or a selected component that represents
144   --  an appropriately qualified occurrence of T.
145
146   function Rep_Item_Too_Late
147     (T     : Entity_Id;
148      N     : Node_Id;
149      FOnly : Boolean := False) return Boolean;
150   --  Called at the start of processing a representation clause or a
151   --  representation pragma. Used to check that a representation item for
152   --  entity T does not appear too late (according to the rules in RM 13.1(9)
153   --  and RM 13.1(10)). N is the associated node, which in the pragma case
154   --  is the pragma or representation clause itself, used for placing error
155   --  messages if the item is too late.
156   --
157   --  Fonly is a flag that causes only the freezing rule (para 9) to be
158   --  applied, and the tests of para 10 are skipped. This is appropriate for
159   --  both subtype related attributes (Alignment and Size) and for stream
160   --  attributes, which, although certainly not subtype related attributes,
161   --  clearly should not be subject to the para 10 restrictions (see
162   --  AI95-00137). Similarly, we also skip the para 10 restrictions for
163   --  the Storage_Size case where they also clearly do not apply, and for
164   --  Stream_Convert which is in the same category as the stream attributes.
165   --
166   --  If the rep item is too late, an appropriate message is output and True
167   --  is returned, which is a signal that the caller should abandon processing
168   --  for the item. If the item is not too late, then False is returned, and
169   --  the caller can continue processing the item.
170   --
171   --  If no error is detected, this call also as a side effect links the
172   --  representation item onto the head of the representation item chain
173   --  (referenced by the First_Rep_Item field of the entity).
174   --
175   --  Note: Rep_Item_Too_Late must be called with the underlying type in the
176   --  case of a private or incomplete type. The protocol is to first check for
177   --  Rep_Item_Too_Early using the initial entity, then take the underlying
178   --  type, then call Rep_Item_Too_Late on the result.
179   --
180   --  Note: Calls to Rep_Item_Too_Late are ignored for the case of attribute
181   --  definition clauses which have From_Aspect_Specification set. This is
182   --  because such clauses are linked on to the Rep_Item chain in procedure
183   --  Sem_Ch13.Analyze_Aspect_Specifications. See that procedure for details.
184
185   function Same_Representation (Typ1, Typ2 : Entity_Id) return Boolean;
186   --  Given two types, where the two types are related by possible derivation,
187   --  determines if the two types have the same representation, or different
188   --  representations, requiring the special processing for representation
189   --  change. A False result is possible only for array, enumeration or
190   --  record types.
191
192   procedure Validate_Compile_Time_Warning_Error (N : Node_Id);
193   --  N is a pragma Compile_Time_Error or Compile_Warning_Error whose boolean
194   --  expression is not known at compile time. This procedure makes an entry
195   --  in a table. The actual checking is performed by Validate_Compile_Time_
196   --  Warning_Errors, which is invoked after calling the back end.
197
198   procedure Validate_Compile_Time_Warning_Errors;
199   --  This routine is called after calling the back end to validate pragmas
200   --  Compile_Time_Error and Compile_Time_Warning for size and alignment
201   --  appropriateness. The reason it is called that late is to take advantage
202   --  of any back-annotation of size and alignment performed by the back end.
203
204   procedure Validate_Unchecked_Conversion
205     (N        : Node_Id;
206      Act_Unit : Entity_Id);
207   --  Validate a call to unchecked conversion. N is the node for the actual
208   --  instantiation, which is used only for error messages. Act_Unit is the
209   --  entity for the instantiation, from which the actual types etc. for this
210   --  instantiation can be determined. This procedure makes an entry in a
211   --  table and/or generates an N_Validate_Unchecked_Conversion node. The
212   --  actual checking is done in Validate_Unchecked_Conversions or in the
213   --  back end as required.
214
215   procedure Validate_Unchecked_Conversions;
216   --  This routine is called after calling the back end to validate unchecked
217   --  conversions for size and alignment appropriateness. The reason it is
218   --  called that late is to take advantage of any back-annotation of size
219   --  and alignment performed by the back end.
220
221   procedure Validate_Address_Clauses;
222   --  This is called after the back end has been called (and thus after the
223   --  alignments of objects have been back annotated). It goes through the
224   --  table of saved address clauses checking for suspicious alignments and
225   --  if necessary issuing warnings.
226
227   procedure Validate_Independence;
228   --  This is called after the back end has been called (and thus after the
229   --  layout of components has been back annotated). It goes through the
230   --  table of saved pragma Independent[_Component] entries, checking that
231   --  independence can be achieved, and if necessary issuing error messages.
232
233   -------------------------------------
234   -- Table for Validate_Independence --
235   -------------------------------------
236
237   --  If a legal pragma Independent or Independent_Components is given for
238   --  an entity, then an entry is made in this table, to be checked by a
239   --  call to Validate_Independence after back annotation of layout is done.
240
241   type Independence_Check_Record is record
242      N : Node_Id;
243      --  The pragma Independent or Independent_Components
244
245      E : Entity_Id;
246      --  The entity to which it applies
247   end record;
248
249   package Independence_Checks is new Table.Table (
250     Table_Component_Type => Independence_Check_Record,
251     Table_Index_Type     => Int,
252     Table_Low_Bound      => 1,
253     Table_Initial        => 20,
254     Table_Increment      => 200,
255     Table_Name           => "Independence_Checks");
256
257   -----------------------------------
258   -- Handling of Aspect Visibility --
259   -----------------------------------
260
261   --  The visibility of aspects is tricky. First, the visibility is delayed
262   --  to the freeze point. This is not too complicated, what we do is simply
263   --  to leave the aspect "laying in wait" for the freeze point, and at that
264   --  point materialize and analyze the corresponding attribute definition
265   --  clause or pragma. There is some special processing for preconditions
266   --  and postonditions, where the pragmas themselves deal with the required
267   --  delay, but basically the approach is the same, delay analysis of the
268   --  expression to the freeze point.
269
270   --  Much harder is the requirement for diagnosing cases in which an early
271   --  freeze causes a change in visibility. Consider:
272
273   --    package AspectVis is
274   --       R_Size : constant Integer := 32;
275   --
276   --       package Inner is
277   --          type R is new Integer with
278   --            Size => R_Size;
279   --          F : R; -- freezes
280   --          R_Size : constant Integer := 64;
281   --          S : constant Integer := R'Size; -- 32 not 64
282   --       end Inner;
283   --    end AspectVis;
284
285   --  Here the 32 not 64 shows what would be expected if this program were
286   --  legal, since the evaluation of R_Size has to be done at the freeze
287   --  point and gets the outer definition not the inner one.
288
289   --  But the language rule requires this program to be diagnosed as illegal
290   --  because the visibility changes between the freeze point and the end of
291   --  the declarative region.
292
293   --  To meet this requirement, we first note that the Expression field of the
294   --  N_Aspect_Specification node holds the raw unanalyzed expression, which
295   --  will get used in processing the aspect. At the time of analyzing the
296   --  N_Aspect_Specification node, we create a complete copy of the expression
297   --  and store it in the entity field of the Identifier (an odd usage, but
298   --  the identifier is not used except to identify the aspect, so its Entity
299   --  field is otherwise unused, and we are short of room in the node).
300
301   --  This copy stays unanalyzed up to the freeze point, where we analyze the
302   --  resulting pragma or attribute definition clause, except that in the
303   --  case of invariants and predicates, we mark occurrences of the subtype
304   --  name as having the entity of the subprogram parameter, so that they
305   --  will not cause trouble in the following steps.
306
307   --  Then at the freeze point, we create another copy of this unanalyzed
308   --  expression. By this time we no longer need the Expression field for
309   --  other purposes, so we can store it there. Now we have two copies of
310   --  the original unanalyzed expression. One of them gets preanalyzed at
311   --  the freeze point to capture the visibility at the freeze point.
312
313   --  Now when we hit the freeze all at the end of the declarative part, if
314   --  we come across a frozen entity with delayed aspects, we still have one
315   --  copy of the unanalyzed expression available in the node, and we again
316   --  do a preanalysis using that copy and the visibility at the end of the
317   --  declarative part. Now we have two preanalyzed expression (preanalysis
318   --  is good enough, since we are only interested in referenced entities).
319   --  One captures the visibility at the freeze point, the other captures the
320   --  visibility at the end of the declarative part. We see if the entities
321   --  in these two expressions are the same, by seeing if the two expressions
322   --  are fully conformant, and if not, issue appropriate error messages.
323
324   --  Quite an awkward approach, but this is an awkard requirement
325
326   procedure Analyze_Aspects_At_Freeze_Point (E : Entity_Id);
327   --  Analyze all the delayed aspects for entity E at freezing point. This
328   --  includes dealing with inheriting delayed aspects from the parent type
329   --  in the case where a derived type is frozen.
330
331   procedure Check_Aspect_At_Freeze_Point (ASN : Node_Id);
332   --  Performs the processing described above at the freeze point, ASN is the
333   --  N_Aspect_Specification node for the aspect.
334
335   procedure Check_Aspect_At_End_Of_Declarations (ASN : Node_Id);
336   --  Performs the processing described above at the freeze all point, and
337   --  issues appropriate error messages if the visibility has indeed changed.
338   --  Again, ASN is the N_Aspect_Specification node for the aspect.
339
340   procedure Inherit_Aspects_At_Freeze_Point (Typ : Entity_Id);
341   --  Given an entity Typ that denotes a derived type or a subtype, this
342   --  routine performs the inheritance of aspects at the freeze point.
343
344   procedure Resolve_Aspect_Expressions (E : Entity_Id);
345   --  Name resolution of an aspect expression happens at the end of the
346   --  current declarative part or at the freeze point for the entity,
347   --  whichever comes first. For declarations in the visible part of a
348   --  package, name resolution takes place before analysis of the private
349   --  part even though the freeze point of the entity may appear later.
350
351   procedure Validate_Iterable_Aspect (Typ : Entity_Id; ASN : Node_Id);
352   --  For SPARK 2014 formal containers. The expression has the form of an
353   --  aggregate, and each entry must denote a function with the proper syntax
354   --  for First, Next, and Has_Element. Optionally an Element primitive may
355   --  also be defined.
356
357   procedure Install_Discriminants (E : Entity_Id);
358   --  Make visible the discriminants of type entity E
359
360   procedure Uninstall_Discriminants (E : Entity_Id);
361   --  Remove visibility to the discriminants of type entity E
362
363end Sem_Ch13;
364