home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gnat-2.06-src.tgz / tar.out / fsf / gnat / ada / sem_eval.ads < prev    next >
Text File  |  1996-09-28  |  13KB  |  246 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT COMPILER COMPONENTS                         --
  4. --                                                                          --
  5. --                             S E M _ E V A L                              --
  6. --                                                                          --
  7. --                                 S p e c                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.34 $                             --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. --
  22. --                                                                          --
  23. ------------------------------------------------------------------------------
  24.  
  25. --  This package contains various subprograms involved in compile time
  26. --  evaluation of expressions and checks for staticness of expressions
  27. --  and types. It also contains the circuitry for checking for violations
  28. --  of pure and preelaborated conditions (this naturally goes here, since
  29. --  these rules involve consideration of staticness).
  30.  
  31. --  Note: the static evaluation for attributes is found in Sem_Attr even
  32. --  though logically it belongs here. We have done this so that it is easier
  33. --  to add new attributes to GNAT.
  34.  
  35. with Types;  use Types;
  36. with Uintp;  use Uintp;
  37. with Urealp; use Urealp;
  38.  
  39. package Sem_Eval is
  40.  
  41.    ------------------------------------
  42.    -- Handling of Static Expressions --
  43.    ------------------------------------
  44.  
  45.    --  This package contains a set of routine that process individual
  46.    --  subexpression nodes with the objective of folding (precomputing)
  47.    --  the value of static expressions that are known at compile time and
  48.    --  properly computing the setting of two flags that appear in every
  49.    --  subexpression node:
  50.  
  51.    --    Is_Static_Expression
  52.  
  53.    --      This flag is set on any expression that is static according
  54.    --      to the rules in (RM 4.9(3-14)).
  55.  
  56.    --    Raises_Constraint_Error
  57.  
  58.    --      This flag indicatest that it is known at compile time that the
  59.    --      evaluation of an expression raises constraint error. If the
  60.    --      expression is static, and this flag is off, then it is also known
  61.    --      at compile time that the expression does not raise constraint error
  62.    --      (i.e. the flag is accurate for static expressions, and conservative
  63.    --      for non-static expressions.
  64.  
  65.    --  If a static expression does not raise constraint error, then the
  66.    --  Raises_Constraint_Error flag is off, and the expression must be
  67.    --  computed at compile time, which means that it has the form of either
  68.    --  a literal, or a constant that is itself (recursively) either a literal
  69.    --  or a constant.
  70.  
  71.    --  The above rules must be followed exactly in order for legality
  72.    --  checks to be accurate. For subexpressions that are not static
  73.    --  according to the RM definition, they are sometimes folded anyway,
  74.    --  but of course in this case Is_Static_Expression is not set.
  75.  
  76.    -----------------
  77.    -- Subprograms --
  78.    -----------------
  79.  
  80.    procedure Check_Non_Static_Context (N : Node_Id);
  81.    --  Deals with the special check required for a static expression that
  82.    --  appears in a non-static context, i.e. is not part of a larger static
  83.    --  expression (see RM 4.9(35)), i.e. the value of the expression must be
  84.    --  within the base range of the base type of its expected type. A check
  85.    --  is also made for expressions that are inside the base range, but
  86.    --  outside the range of the expected subtype (this is a warning message
  87.    --  rather than an illegality).
  88.    --
  89.    --  Note: most cases of non-static context checks are handled within
  90.    --  Sem_Eval itself, including all cases of expressions at the outer
  91.    --  level (i.e. those that are not a subexpression). Currently the
  92.    --  only outside customers for this procedure are Sem_Attr (because
  93.    --  Eval_Attribute is there), and Sem_Aggr (because aggregate processing
  94.    --  intertwines analysis and expansion in a way that makes it hard to
  95.    --  separate out a clean Eval_Aggregate routine).
  96.  
  97.    function Expr_Value (N : Node_Id) return Uint;
  98.    --  Returns the folded value of the expression. This function is called
  99.    --  in instances where it has already been determined that the expression
  100.    --  is static. This version is used for integer values, and enumeration or
  101.    --  character literals. In the latter two cases, the value returned is the
  102.    --  Pos value in the relevant enumeration type. It can also be used for
  103.    --  fixed-point values, in which case it returns the corresponding integer
  104.    --  value. It cannot be used for floating-point values.
  105.  
  106.    function Expr_Value_E (N : Node_Id) return Entity_Id;
  107.    --  Returns the folded value of the expression. This function is called
  108.    --  in instances where it has already been determined that the expression
  109.    --  is static. This version is used for enumeration types and returns the
  110.    --  corresponding enumeration literal.
  111.  
  112.    function Expr_Value_R (N : Node_Id) return Ureal;
  113.    --  Returns the folded value of the expression. This function is called
  114.    --  in instances where it has already been determined that the expression
  115.    --  is static. This version is used for real values (including both the
  116.    --  floating-point and fixed-point cases). In the case of a fixed-point
  117.    --  type, the real value is returned (cf above version returning Uint).
  118.  
  119.    function Expr_Value_S (N : Node_Id) return String_Id;
  120.    --  Returns the folded value of the expression. This function is called
  121.    --  in instances where it has already been determined that the expression
  122.    --  is static. This version is used for string types and returns the
  123.    --  corresponding string id value.
  124.  
  125.    procedure Eval_Actual                 (N : Node_Id);
  126.    procedure Eval_Aggregate              (N : Node_Id);
  127.    procedure Eval_Allocator              (N : Node_Id);
  128.    procedure Eval_Arithmetic_Op          (N : Node_Id);
  129.    procedure Eval_Character_Literal      (N : Node_Id);
  130.    procedure Eval_Concatenation          (N : Node_Id);
  131.    procedure Eval_Conditional_Expression (N : Node_Id);
  132.    procedure Eval_Entity_Name            (N : Node_Id);
  133.    procedure Eval_Indexed_Component      (N : Node_Id);
  134.    procedure Eval_Integer_Literal        (N : Node_Id);
  135.    procedure Eval_Logical_Op             (N : Node_Id);
  136.    procedure Eval_Membership_Op          (N : Node_Id);
  137.    procedure Eval_Named_Integer          (N : Node_Id);
  138.    procedure Eval_Named_Real             (N : Node_Id);
  139.    procedure Eval_Op_Expon               (N : Node_Id);
  140.    procedure Eval_Op_Not                 (N : Node_Id);
  141.    procedure Eval_Real_Literal           (N : Node_Id);
  142.    procedure Eval_Relational_Op          (N : Node_Id);
  143.    procedure Eval_Shift                  (N : Node_Id);
  144.    procedure Eval_Short_Circuit          (N : Node_Id);
  145.    procedure Eval_Slice                  (N : Node_Id);
  146.    procedure Eval_String_Literal         (N : Node_Id);
  147.    procedure Eval_Qualified_Expression   (N : Node_Id);
  148.    procedure Eval_Type_Conversion        (N : Node_Id);
  149.    procedure Eval_Unary_Op               (N : Node_Id);
  150.    procedure Eval_Unchecked_Conversion   (N : Node_Id);
  151.  
  152.    procedure Fold_Str (N : Node_Id; Val : String_Id);
  153.    --  Rewrite N with a new N_String_Literal node as the result of the
  154.    --  compile time evaluation of the node N. Val is the resulting string
  155.    --  value from the folding operation. The Is_Static_Expression flag is
  156.    --  set in the result node.
  157.  
  158.    procedure Fold_Uint (N : Node_Id; Val : Uint);
  159.    --  Rewrite N with a (N_Integer_Literal, N_Identifier, N_Character_Literal)
  160.    --  node as the result of the compile time evaluation of the node N. Val
  161.    --  is the result in the integer case and is the position of the literal
  162.    --  in the literals list for the enumeration case. Is_Static_Expression
  163.    --  is set True in the result node.
  164.  
  165.    procedure Fold_Ureal (N : Node_Id; Val : Ureal);
  166.    --  Rewrite N with a new N_Real_Literal node as the result of the compile
  167.    --  time evaluation of the node N. Val is the resulting real value from
  168.    --  the folding operation. The Is_Static_Expression flag is set in the
  169.    --  result node.
  170.  
  171.    function Is_OK_Static_Expression (N : Node_Id) return Boolean;
  172.    --  An OK static expression is one that is static in the RM definition
  173.    --  sense and which does not raise constraint error. This is used in
  174.    --  contexts where the test is for compile time evaluation purposes.
  175.    --  For legality checking purposes, always use Is_Static_Expression.
  176.  
  177.    function Is_In_Range (N : Node_Id; Typ : Entity_Id) return Boolean;
  178.    --  See if it can be determined at compile time that the expression
  179.    --  represented by node N is known to be in range of the subtype
  180.    --  Typ. True is returned if Typ is a scalar type, Both of
  181.    --  whose bounds is static, and N is a static expression which can
  182.    --  be determined to be inside the static bounds.  A result of False
  183.    --  does not mean that the expression is out of range, merely that it
  184.    --  cannot be determined at compile time that it is in range.
  185.  
  186.    function Is_Out_Of_Range (N : Node_Id; Typ : Entity_Id) return Boolean;
  187.    --  See if it can be determined at compile time that the expression
  188.    --  represented by node N is known to be out of range of the subtype
  189.    --  Typ. True is returned if Typ is a scalar type, at least one of
  190.    --  whose bounds is static, and N is a static expression which can
  191.    --  be determined to be outside a static bound. Note that Typ does
  192.    --  not have to be a static subtype, since there are cases where N
  193.    --  is clearly outside the range of one bound, even though the other
  194.    --  bound is non-static. A result of False does not mean that the
  195.    --  expression is in range, merely that it cannot be determined at
  196.    --  compile time that it is out of range.
  197.  
  198.    function Is_Static_Range (N : Node_Id) return Boolean;
  199.    --  Determine if range is static, as defined in RM 4.9(26). The only
  200.    --  allowed argument is an N_Range node (but note that the semantic
  201.    --  analysis of equivalent range attribute references already turned
  202.    --  them into the equivalent range).
  203.  
  204.    function Is_OK_Static_Range (N : Node_Id) return Boolean;
  205.    --  Like Is_Static_Range, but also makes sure that the bounds of the
  206.    --  range are compile-time evaluable (i.e. do not raise constraint error).
  207.    --  A result of true means that the bounds are compile time evaluable.
  208.    --  A result of false means they are not (either because the range is
  209.    --  not static, or because one or the other bound raises CE).
  210.  
  211.    function Is_Static_Subtype (Typ : Entity_Id) return Boolean;
  212.    --  Determines whether a subtype fits the definition of an Ada static
  213.    --  subtype as given in (RM 4.9(26)).
  214.  
  215.    function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean;
  216.    --  Like Is_Static_Subtype but also makes sure that the bounds of the
  217.    --  subtype are compile-time evaluable (i.e. do not raise constraint
  218.    --  error). A result of true means that the bounds are compile time
  219.    --  evaluable. A result of false means they are not (either because the
  220.    --  range is not static, or because one or the other bound raises CE).
  221.  
  222.    function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean;
  223.    --  Determine whether two types T1, T2, which have the same base type,
  224.    --  are statically matching subtypes (RM 4.9.1(1-2)).
  225.  
  226. private
  227.    --  The Eval routines are all marked inline, since they are called once
  228.  
  229.    pragma Inline (Eval_Actual);
  230.    pragma Inline (Eval_Allocator);
  231.    pragma Inline (Eval_Character_Literal);
  232.    pragma Inline (Eval_Conditional_Expression);
  233.    pragma Inline (Eval_Indexed_Component);
  234.    pragma Inline (Eval_Integer_Literal);
  235.    pragma Inline (Eval_Named_Integer);
  236.    pragma Inline (Eval_Named_Real);
  237.    pragma Inline (Eval_Real_Literal);
  238.    pragma Inline (Eval_Shift);
  239.    pragma Inline (Eval_Slice);
  240.    pragma Inline (Eval_String_Literal);
  241.    pragma Inline (Eval_Unchecked_Conversion);
  242.  
  243.    pragma Inline (Is_OK_Static_Expression);
  244.  
  245. end Sem_Eval;
  246.