1------------------------------------------------------------------------------
2--                                                                          --
3--                         GNAT COMPILER COMPONENTS                         --
4--                                                                          --
5--                             S E M _ E V A L                              --
6--                                                                          --
7--                                 S p e c                                  --
8--                                                                          --
9--          Copyright (C) 1992-2003 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 2,  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 COPYING.  If not, write --
19-- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20-- MA 02111-1307, USA.                                                      --
21--                                                                          --
22-- GNAT was originally developed  by the GNAT team at  New York University. --
23-- Extensive contributions were provided by Ada Core Technologies Inc.      --
24--                                                                          --
25------------------------------------------------------------------------------
26
27--  This package contains various subprograms involved in compile time
28--  evaluation of expressions and checks for staticness of expressions
29--  and types. It also contains the circuitry for checking for violations
30--  of pure and preelaborated conditions (this naturally goes here, since
31--  these rules involve consideration of staticness).
32
33--  Note: the static evaluation for attributes is found in Sem_Attr even
34--  though logically it belongs here. We have done this so that it is easier
35--  to add new attributes to GNAT.
36
37with Types;  use Types;
38with Uintp;  use Uintp;
39with Urealp; use Urealp;
40
41package Sem_Eval is
42
43   ------------------------------------
44   -- Handling of Static Expressions --
45   ------------------------------------
46
47   --  This package contains a set of routine that process individual
48   --  subexpression nodes with the objective of folding (precomputing)
49   --  the value of static expressions that are known at compile time and
50   --  properly computing the setting of two flags that appear in every
51   --  subexpression node:
52
53   --    Is_Static_Expression
54
55   --      This flag is set on any expression that is static according
56   --      to the rules in (RM 4.9(3-32)).
57
58   --    Raises_Constraint_Error
59
60   --      This flag indicatest that it is known at compile time that the
61   --      evaluation of an expression raises constraint error. If the
62   --      expression is static, and this flag is off, then it is also known
63   --      at compile time that the expression does not raise constraint error
64   --      (i.e. the flag is accurate for static expressions, and conservative
65   --      for non-static expressions.
66
67   --  If a static expression does not raise constraint error, then the
68   --  Raises_Constraint_Error flag is off, and the expression must be
69   --  computed at compile time, which means that it has the form of either
70   --  a literal, or a constant that is itself (recursively) either a literal
71   --  or a constant.
72
73   --  The above rules must be followed exactly in order for legality
74   --  checks to be accurate. For subexpressions that are not static
75   --  according to the RM definition, they are sometimes folded anyway,
76   --  but of course in this case Is_Static_Expression is not set.
77
78   -------------------------------
79   -- Compile-Time Known Values --
80   -------------------------------
81
82   --  For most legality checking purposes the flag Is_Static_Expression
83   --  defined in Sinfo should be used. This package also provides
84   --  a routine called Is_OK_Static_Expression which in addition of
85   --  checking that an expression is static in the RM 4.9 sense, it
86   --  checks that the expression does not raise constraint error. In
87   --  fact for certain legality checks not only do we need to ascertain
88   --  that the expression is static, but we must also ensure that it
89   --  does not raise constraint error.
90   --
91   --  Neither of Is_Static_Expression and Is_OK_Static_Expression should
92   --  be used for compile time evaluation purposes. In fact certain
93   --  expression whose value is known at compile time are not static
94   --  in the RM 4.9 sense. A typical example is:
95   --
96   --     C : constant Integer := Record_Type'Size;
97   --
98   --  The expression 'C' is not static in the technical RM sense, but for
99   --  many simple record types, the size is in fact known at compile time.
100   --  When we are trying to perform compile time constant folding (for
101   --  instance for expressions such as 'C + 1', Is_Static_Expression or
102   --  Is_OK_Static_Expression are not the right functions to test to see
103   --  if folding is possible. Instead, we use Compile_Time_Know_Value.
104   --  All static expressions that do not raise constraint error (i.e.
105   --  those for which Is_OK_Static_Expression is true) are known at
106   --  compile time, but as shown by the above example, there are cases
107   --  of non-static expressions which are known at compile time.
108
109   -----------------
110   -- Subprograms --
111   -----------------
112
113   procedure Check_Non_Static_Context (N : Node_Id);
114   --  Deals with the special check required for a static expression that
115   --  appears in a non-static context, i.e. is not part of a larger static
116   --  expression (see RM 4.9(35)), i.e. the value of the expression must be
117   --  within the base range of the base type of its expected type. A check
118   --  is also made for expressions that are inside the base range, but
119   --  outside the range of the expected subtype (this is a warning message
120   --  rather than an illegality).
121   --
122   --  Note: most cases of non-static context checks are handled within
123   --  Sem_Eval itself, including all cases of expressions at the outer
124   --  level (i.e. those that are not a subexpression). Currently the only
125   --  outside customer for this procedure is Sem_Attr (because Eval_Attribute
126   --  is there). There is also one special case arising from ranges (see body
127   --  of Resolve_Range).
128
129   procedure Check_String_Literal_Length (N : Node_Id; Ttype : Entity_Id);
130   --  N is either a string literal, or a constraint error node. In the latter
131   --  case, the situation is already dealt with, and the call has no effect.
132   --  In the former case, if the target type, Ttyp is constrained, then a
133   --  check is made to see if the string literal is of appropriate length.
134
135   type Compare_Result is (LT, LE, EQ, GT, GE, NE, Unknown);
136   subtype Compare_GE is Compare_Result range EQ .. GE;
137   subtype Compare_LE is Compare_Result range LT .. EQ;
138   function Compile_Time_Compare
139     (L, R : Node_Id;
140      Rec  : Boolean := False)
141      return Compare_Result;
142   --  Given two expression nodes, finds out whether it can be determined
143   --  at compile time how the runtime values will compare. An Unknown
144   --  result means that the result of a comparison cannot be determined at
145   --  compile time, otherwise the returned result indicates the known result
146   --  of the comparison, given as tightly as possible (i.e. EQ or LT is a
147   --  preferred returned value to LE). Rec is a parameter that is set True
148   --  for a recursive call from within Compile_Time_Compare to avoid some
149   --  infinite recursion cases. It should never be set by a client.
150
151   procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id);
152   --  This procedure is called after it has been determined that Expr is
153   --  not static when it is required to be. Msg is the text of a message
154   --  that explains the error. This procedure checks if an error is already
155   --  posted on Expr, if so, it does nothing unless All_Errors_Mode is set
156   --  in which case this flag is ignored. Otherwise the given message is
157   --  posted using Error_Msg_F, and then Why_Not_Static is called on
158   --  Expr to generate additional messages. The string given as Msg
159   --  should end with ! to make it an unconditional message, to ensure
160   --  that if it is posted, the entire set of messages is all posted.
161
162   function Is_OK_Static_Expression (N : Node_Id) return Boolean;
163   --  An OK static expression is one that is static in the RM definition
164   --  sense and which does not raise constraint error. For most legality
165   --  checking purposes you should use Is_Static_Expression. For those
166   --  legality checks where the expression N should not raise constaint
167   --  error use this routine. This routine is *not* to be used in contexts
168   --  where the test is for compile time evaluation purposes. Use routine
169   --  Compile_Time_Known_Value instead (see section on "Compile-Time Known
170   --  Values" above).
171
172   function Is_Static_Range (N : Node_Id) return Boolean;
173   --  Determine if range is static, as defined in RM 4.9(26). The only
174   --  allowed argument is an N_Range node (but note that the semantic
175   --  analysis of equivalent range attribute references already turned
176   --  them into the equivalent range).
177
178   function Is_OK_Static_Range (N : Node_Id) return Boolean;
179   --  Like Is_Static_Range, but also makes sure that the bounds of the
180   --  range are compile-time evaluable (i.e. do not raise constraint error).
181   --  A result of true means that the bounds are compile time evaluable.
182   --  A result of false means they are not (either because the range is
183   --  not static, or because one or the other bound raises CE).
184
185   function Is_Static_Subtype (Typ : Entity_Id) return Boolean;
186   --  Determines whether a subtype fits the definition of an Ada static
187   --  subtype as given in (RM 4.9(26)).
188
189   function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean;
190   --  Like Is_Static_Subtype but also makes sure that the bounds of the
191   --  subtype are compile-time evaluable (i.e. do not raise constraint
192   --  error). A result of true means that the bounds are compile time
193   --  evaluable. A result of false means they are not (either because the
194   --  range is not static, or because one or the other bound raises CE).
195
196   function Subtypes_Statically_Compatible
197     (T1   : Entity_Id;
198      T2   : Entity_Id)
199      return Boolean;
200   --  Returns true if the subtypes are unconstrained or the constraint on
201   --  on T1 is statically compatible with T2 (as defined by 4.9.1(4)).
202   --  Otherwise returns false.
203
204   function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean;
205   --  Determine whether two types T1, T2, which have the same base type,
206   --  are statically matching subtypes (RM 4.9.1(1-2)).
207
208   function Compile_Time_Known_Value (Op : Node_Id) return Boolean;
209   --  Returns true if Op is an expression not raising constraint error
210   --  whose value is known at compile time. This is true if Op is a static
211   --  expression, but can also be true for expressions which are
212   --  technically non-static but which are in fact known at compile time,
213   --  such as the static lower bound of a non-static range or the value
214   --  of a constant object whose initial value is static. Note that this
215   --  routine is defended against unanalyzed expressions. Such expressions
216   --  will not cause a blowup, they may cause pessimistic (i.e. False)
217   --  results to be returned.
218
219   function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean;
220   --  Similar to Compile_Time_Known_Value, but also returns True if the
221   --  value is a compile time known aggregate, i.e. an aggregate all of
222   --  whose constituent expressions are either compile time known values
223   --  or compile time known aggregates.
224
225   function Expr_Value (N : Node_Id) return Uint;
226   --  Returns the folded value of the expression N. This function is called
227   --  in instances where it has already been determined that the expression
228   --  is static or its value is known at compile time (ie the call to
229   --  Compile_Time_Known_Value (N) returns True). This version is used for
230   --  integer values, and enumeration or character literals. In the latter
231   --  two cases, the value returned is the Pos value in the relevant
232   --  enumeration type. It can also be used for fixed-point values, in
233   --  which case it returns the corresponding integer value. It cannot be
234   --  used for floating-point values.
235
236   function Expr_Value_E (N : Node_Id) return Entity_Id;
237   --  Returns the folded value of the expression. This function is called
238   --  in instances where it has already been determined that the expression
239   --  is static or its value known at compile time. This version is used
240   --  for enumeration types and returns the corresponding enumeration
241   --  literal.
242
243   function Expr_Value_R (N : Node_Id) return Ureal;
244   --  Returns the folded value of the expression. This function is called
245   --  in instances where it has already been determined that the expression
246   --  is static or its value known at compile time. This version is used
247   --  for real values (including both the floating-point and fixed-point
248   --  cases). In the case of a fixed-point type, the real value is returned
249   --  (cf above version returning Uint).
250
251   function Expr_Value_S (N : Node_Id) return Node_Id;
252   --  Returns the folded value of the expression. This function is called
253   --  in instances where it has already been determined that the expression
254   --  is static or its value is known at compile time. This version is used
255   --  for string types and returns the corresponding N_String_Literal node.
256
257   function Expr_Rep_Value (N : Node_Id) return Uint;
258   --  This is identical to Expr_Value, except in the case of enumeration
259   --  literals of types for which an enumeration representation clause has
260   --  been given, in which case it returns the representation value rather
261   --  than the pos value. This is the value that is needed for generating
262   --  code sequences, while the Expr_Value value is appropriate for compile
263   --  time constraint errors or getting the logical value. Note that this
264   --  function does NOT concern itself with biased values, if the caller
265   --  needs a properly biased value, the subtraction of the bias must be
266   --  handled explicitly.
267
268   procedure Eval_Actual                 (N : Node_Id);
269   procedure Eval_Allocator              (N : Node_Id);
270   procedure Eval_Arithmetic_Op          (N : Node_Id);
271   procedure Eval_Character_Literal      (N : Node_Id);
272   procedure Eval_Concatenation          (N : Node_Id);
273   procedure Eval_Conditional_Expression (N : Node_Id);
274   procedure Eval_Entity_Name            (N : Node_Id);
275   procedure Eval_Indexed_Component      (N : Node_Id);
276   procedure Eval_Integer_Literal        (N : Node_Id);
277   procedure Eval_Logical_Op             (N : Node_Id);
278   procedure Eval_Membership_Op          (N : Node_Id);
279   procedure Eval_Named_Integer          (N : Node_Id);
280   procedure Eval_Named_Real             (N : Node_Id);
281   procedure Eval_Op_Expon               (N : Node_Id);
282   procedure Eval_Op_Not                 (N : Node_Id);
283   procedure Eval_Real_Literal           (N : Node_Id);
284   procedure Eval_Relational_Op          (N : Node_Id);
285   procedure Eval_Shift                  (N : Node_Id);
286   procedure Eval_Short_Circuit          (N : Node_Id);
287   procedure Eval_Slice                  (N : Node_Id);
288   procedure Eval_String_Literal         (N : Node_Id);
289   procedure Eval_Qualified_Expression   (N : Node_Id);
290   procedure Eval_Type_Conversion        (N : Node_Id);
291   procedure Eval_Unary_Op               (N : Node_Id);
292   procedure Eval_Unchecked_Conversion   (N : Node_Id);
293
294   procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean);
295   --  Rewrite N with a new N_String_Literal node as the result of the
296   --  compile time evaluation of the node N. Val is the resulting string
297   --  value from the folding operation. The Is_Static_Expression flag is
298   --  set in the result node. The result is fully analyzed and resolved.
299   --  Static indicates whether the result should be considered static or
300   --  not (True = consider static). The point here is that normally all
301   --  string literals are static, but if this was the result of some
302   --  sequence of evaluation where values were known at compile time
303   --  but not static, then the result is not static.
304
305   procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean);
306   --  Rewrite N with a (N_Integer_Literal, N_Identifier, N_Character_Literal)
307   --  node as the result of the compile time evaluation of the node N. Val
308   --  is the result in the integer case and is the position of the literal
309   --  in the literals list for the enumeration case. Is_Static_Expression
310   --  is set True in the result node. The result is fully analyzed/resolved.
311   --  Static indicates whether the result should be considered static or
312   --  not (True = consider static). The point here is that normally all
313   --  string literals are static, but if this was the result of some
314   --  sequence of evaluation where values were known at compile time
315   --  but not static, then the result is not static.
316
317   procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean);
318   --  Rewrite N with a new N_Real_Literal node as the result of the compile
319   --  time evaluation of the node N. Val is the resulting real value from
320   --  the folding operation. The Is_Static_Expression flag is set in the
321   --  result node. The result is fully analyzed and result. Static
322   --  indicates whether the result should be considered static or not
323   --  (True = consider static). The point here is that normally all
324   --  string literals are static, but if this was the result of some
325   --  sequence of evaluation where values were known at compile time
326   --  but not static, then the result is not static.
327
328   function Is_In_Range
329     (N         : Node_Id;
330      Typ       : Entity_Id;
331      Fixed_Int : Boolean := False;
332      Int_Real  : Boolean := False)
333      return      Boolean;
334   --  Returns True if it can be guaranteed at compile time that expression
335   --  N is known to be in range of the subtype Typ. If the values of N or
336   --  of either bouds of Type are unknown at compile time, False will
337   --  always be returned. A result of False does not mean that the
338   --  expression is out of range, merely that it cannot be determined at
339   --  compile time that it is in range. If Typ is a floating point type or
340   --  Int_Real is set, any integer value is treated as though it was a real
341   --  value (i.e. the underlying real value is used).  In this case we use
342   --  the corresponding real value, both for the bounds of Typ, and for the
343   --  value of the expression N. If Typ is a fixed type or a discrete type
344   --  and Int_Real is False but flag Fixed_Int is True then any fixed-point
345   --  value is treated as though it was a discrete value (i.e. the
346   --  underlying integer value is used).  In this case we use the
347   --  corresponding integer value, both for the bounds of Typ, and for the
348   --  value of the expression N. If Typ is a discret type and Fixed_Int as
349   --  well as Int_Real are false, intere values are used throughout.
350
351   function Is_Out_Of_Range
352     (N         : Node_Id;
353      Typ       : Entity_Id;
354      Fixed_Int : Boolean := False;
355      Int_Real  : Boolean := False)
356      return      Boolean;
357   --  Returns True if it can be guaranteed at compile time that expression
358   --  N is known to be out of range of the subtype Typ.  True is returned
359   --  if Typ is a scalar type, at least one of whose bounds is known at
360   --  compile time, and N is a compile time known expression which can be
361   --  determined to be outside a compile_time known bound of Typ. A result
362   --  of False does not mean that the expression is in range, merely that
363   --  it cannot be determined at compile time that it is out of range. Flags
364   --  Int_Real and Fixed_Int are used as in routine Is_In_Range above.
365
366   function In_Subrange_Of
367     (T1        : Entity_Id;
368      T2        : Entity_Id;
369      Fixed_Int : Boolean := False)
370      return      Boolean;
371   --  Returns True if it can be guaranteed at compile time that the range
372   --  of values for scalar type T1 are always in the range of scalar type
373   --  T2.  A result of False does not mean that T1 is not in T2's subrange,
374   --  only that it cannot be determined at compile time. Flag Fixed_Int is
375   --  used as in routine Is_In_Range above.
376
377   function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
378   --  Returns True if it can guarantee that Lo .. Hi is a null range.
379   --  If it cannot (because the value of Lo or Hi is not known at compile
380   --  time) then it returns False.
381
382   function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
383   --  Returns True if it can guarantee that Lo .. Hi is not a null range.
384   --  If it cannot (because the value of Lo or Hi is not known at compile
385   --  time) then it returns False.
386
387   procedure Why_Not_Static (Expr : Node_Id);
388   --  This procedure may be called after generating an error message that
389   --  complains that something is non-static. If it finds good reasons,
390   --  it generates one or more error messages pointing the appropriate
391   --  offending component of the expression. If no good reasons can be
392   --  figured out, then no messages are generated. The expectation here
393   --  is that the caller has already issued a message complaining that
394   --  the expression is non-static. Note that this message should be
395   --  placed using Error_Msg_F or Error_Msg_FE, so that it will sort
396   --  before any messages placed by this call. Note that it is fine to
397   --  call Why_Not_Static with something that is not an expression, and
398   --  usually this has no effect, but in some cases (N_Parameter_Association
399   --  or N_Range), it makes sense for the internal recursive calls.
400
401   procedure Initialize;
402   --  Initializes the internal data structures. Must be called before
403   --  each separate main program unit (e.g. in a GNSA/ASIS context).
404
405private
406   --  The Eval routines are all marked inline, since they are called once
407
408   pragma Inline (Eval_Actual);
409   pragma Inline (Eval_Allocator);
410   pragma Inline (Eval_Character_Literal);
411   pragma Inline (Eval_Conditional_Expression);
412   pragma Inline (Eval_Indexed_Component);
413   pragma Inline (Eval_Integer_Literal);
414   pragma Inline (Eval_Named_Integer);
415   pragma Inline (Eval_Named_Real);
416   pragma Inline (Eval_Real_Literal);
417   pragma Inline (Eval_Shift);
418   pragma Inline (Eval_Slice);
419   pragma Inline (Eval_String_Literal);
420   pragma Inline (Eval_Unchecked_Conversion);
421
422   pragma Inline (Is_OK_Static_Expression);
423
424end Sem_Eval;
425