File : sem_ch13.ads


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