OSDN Git Service

2005-06-15 Andrew Pinski <pinskia@physics.uc.edu>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_eval.ads
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-2005 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
37 with Types;  use Types;
38 with Uintp;  use Uintp;
39 with Urealp; use Urealp;
40
41 package 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_Known_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) return Compare_Result;
141    --  Given two expression nodes, finds out whether it can be determined
142    --  at compile time how the runtime values will compare. An Unknown
143    --  result means that the result of a comparison cannot be determined at
144    --  compile time, otherwise the returned result indicates the known result
145    --  of the comparison, given as tightly as possible (i.e. EQ or LT is a
146    --  preferred returned value to LE). Rec is a parameter that is set True
147    --  for a recursive call from within Compile_Time_Compare to avoid some
148    --  infinite recursion cases. It should never be set by a client.
149
150    procedure Flag_Non_Static_Expr (Msg : String; Expr : Node_Id);
151    --  This procedure is called after it has been determined that Expr is
152    --  not static when it is required to be. Msg is the text of a message
153    --  that explains the error. This procedure checks if an error is already
154    --  posted on Expr, if so, it does nothing unless All_Errors_Mode is set
155    --  in which case this flag is ignored. Otherwise the given message is
156    --  posted using Error_Msg_F, and then Why_Not_Static is called on
157    --  Expr to generate additional messages. The string given as Msg
158    --  should end with ! to make it an unconditional message, to ensure
159    --  that if it is posted, the entire set of messages is all posted.
160
161    function Is_OK_Static_Expression (N : Node_Id) return Boolean;
162    --  An OK static expression is one that is static in the RM definition
163    --  sense and which does not raise constraint error. For most legality
164    --  checking purposes you should use Is_Static_Expression. For those
165    --  legality checks where the expression N should not raise constaint
166    --  error use this routine. This routine is *not* to be used in contexts
167    --  where the test is for compile time evaluation purposes. Use routine
168    --  Compile_Time_Known_Value instead (see section on "Compile-Time Known
169    --  Values" above).
170
171    function Is_Static_Range (N : Node_Id) return Boolean;
172    --  Determine if range is static, as defined in RM 4.9(26). The only
173    --  allowed argument is an N_Range node (but note that the semantic
174    --  analysis of equivalent range attribute references already turned
175    --  them into the equivalent range).
176
177    function Is_OK_Static_Range (N : Node_Id) return Boolean;
178    --  Like Is_Static_Range, but also makes sure that the bounds of the
179    --  range are compile-time evaluable (i.e. do not raise constraint error).
180    --  A result of true means that the bounds are compile time evaluable.
181    --  A result of false means they are not (either because the range is
182    --  not static, or because one or the other bound raises CE).
183
184    function Is_Static_Subtype (Typ : Entity_Id) return Boolean;
185    --  Determines whether a subtype fits the definition of an Ada static
186    --  subtype as given in (RM 4.9(26)).
187
188    function Is_OK_Static_Subtype (Typ : Entity_Id) return Boolean;
189    --  Like Is_Static_Subtype but also makes sure that the bounds of the
190    --  subtype are compile-time evaluable (i.e. do not raise constraint
191    --  error). A result of true means that the bounds are compile time
192    --  evaluable. A result of false means they are not (either because the
193    --  range is not static, or because one or the other bound raises CE).
194
195    function Subtypes_Statically_Compatible
196      (T1 : Entity_Id;
197       T2 : Entity_Id) return Boolean;
198    --  Returns true if the subtypes are unconstrained or the constraint on
199    --  on T1 is statically compatible with T2 (as defined by 4.9.1(4)).
200    --  Otherwise returns false.
201
202    function Subtypes_Statically_Match (T1, T2 : Entity_Id) return Boolean;
203    --  Determine whether two types T1, T2, which have the same base type,
204    --  are statically matching subtypes (RM 4.9.1(1-2)).
205
206    function Compile_Time_Known_Value (Op : Node_Id) return Boolean;
207    --  Returns true if Op is an expression not raising constraint error
208    --  whose value is known at compile time. This is true if Op is a static
209    --  expression, but can also be true for expressions which are
210    --  technically non-static but which are in fact known at compile time,
211    --  such as the static lower bound of a non-static range or the value
212    --  of a constant object whose initial value is static. Note that this
213    --  routine is defended against unanalyzed expressions. Such expressions
214    --  will not cause a blowup, they may cause pessimistic (i.e. False)
215    --  results to be returned.
216
217    function Compile_Time_Known_Value_Or_Aggr (Op : Node_Id) return Boolean;
218    --  Similar to Compile_Time_Known_Value, but also returns True if the
219    --  value is a compile time known aggregate, i.e. an aggregate all of
220    --  whose constituent expressions are either compile time known values
221    --  or compile time known aggregates.
222
223    function Compile_Time_Known_Bounds (T : Entity_Id) return Boolean;
224    --  If T is an array whose index bounds are all known at compile time,
225    --  then True is returned, if T is not an array, or one or more of its
226    --  index bounds is not known at compile time, then False is returned.
227
228    function Expr_Value (N : Node_Id) return Uint;
229    --  Returns the folded value of the expression N. This function is called
230    --  in instances where it has already been determined that the expression
231    --  is static or its value is known at compile time (ie the call to
232    --  Compile_Time_Known_Value (N) returns True). This version is used for
233    --  integer values, and enumeration or character literals. In the latter
234    --  two cases, the value returned is the Pos value in the relevant
235    --  enumeration type. It can also be used for fixed-point values, in
236    --  which case it returns the corresponding integer value. It cannot be
237    --  used for floating-point values.
238
239    function Expr_Value_E (N : Node_Id) return Entity_Id;
240    --  Returns the folded value of the expression. This function is called
241    --  in instances where it has already been determined that the expression
242    --  is static or its value known at compile time. This version is used
243    --  for enumeration types and returns the corresponding enumeration
244    --  literal.
245
246    function Expr_Value_R (N : Node_Id) return Ureal;
247    --  Returns the folded value of the expression. This function is called
248    --  in instances where it has already been determined that the expression
249    --  is static or its value known at compile time. This version is used
250    --  for real values (including both the floating-point and fixed-point
251    --  cases). In the case of a fixed-point type, the real value is returned
252    --  (cf above version returning Uint).
253
254    function Expr_Value_S (N : Node_Id) return Node_Id;
255    --  Returns the folded value of the expression. This function is called
256    --  in instances where it has already been determined that the expression
257    --  is static or its value is known at compile time. This version is used
258    --  for string types and returns the corresponding N_String_Literal node.
259
260    function Expr_Rep_Value (N : Node_Id) return Uint;
261    --  This is identical to Expr_Value, except in the case of enumeration
262    --  literals of types for which an enumeration representation clause has
263    --  been given, in which case it returns the representation value rather
264    --  than the pos value. This is the value that is needed for generating
265    --  code sequences, while the Expr_Value value is appropriate for compile
266    --  time constraint errors or getting the logical value. Note that this
267    --  function does NOT concern itself with biased values, if the caller
268    --  needs a properly biased value, the subtraction of the bias must be
269    --  handled explicitly.
270
271    procedure Eval_Actual                 (N : Node_Id);
272    procedure Eval_Allocator              (N : Node_Id);
273    procedure Eval_Arithmetic_Op          (N : Node_Id);
274    procedure Eval_Call                   (N : Node_Id);
275    procedure Eval_Character_Literal      (N : Node_Id);
276    procedure Eval_Concatenation          (N : Node_Id);
277    procedure Eval_Conditional_Expression (N : Node_Id);
278    procedure Eval_Entity_Name            (N : Node_Id);
279    procedure Eval_Indexed_Component      (N : Node_Id);
280    procedure Eval_Integer_Literal        (N : Node_Id);
281    procedure Eval_Logical_Op             (N : Node_Id);
282    procedure Eval_Membership_Op          (N : Node_Id);
283    procedure Eval_Named_Integer          (N : Node_Id);
284    procedure Eval_Named_Real             (N : Node_Id);
285    procedure Eval_Op_Expon               (N : Node_Id);
286    procedure Eval_Op_Not                 (N : Node_Id);
287    procedure Eval_Real_Literal           (N : Node_Id);
288    procedure Eval_Relational_Op          (N : Node_Id);
289    procedure Eval_Shift                  (N : Node_Id);
290    procedure Eval_Short_Circuit          (N : Node_Id);
291    procedure Eval_Slice                  (N : Node_Id);
292    procedure Eval_String_Literal         (N : Node_Id);
293    procedure Eval_Qualified_Expression   (N : Node_Id);
294    procedure Eval_Type_Conversion        (N : Node_Id);
295    procedure Eval_Unary_Op               (N : Node_Id);
296    procedure Eval_Unchecked_Conversion   (N : Node_Id);
297
298    procedure Fold_Str (N : Node_Id; Val : String_Id; Static : Boolean);
299    --  Rewrite N with a new N_String_Literal node as the result of the
300    --  compile time evaluation of the node N. Val is the resulting string
301    --  value from the folding operation. The Is_Static_Expression flag is
302    --  set in the result node. The result is fully analyzed and resolved.
303    --  Static indicates whether the result should be considered static or
304    --  not (True = consider static). The point here is that normally all
305    --  string literals are static, but if this was the result of some
306    --  sequence of evaluation where values were known at compile time
307    --  but not static, then the result is not static.
308
309    procedure Fold_Uint (N : Node_Id; Val : Uint; Static : Boolean);
310    --  Rewrite N with a (N_Integer_Literal, N_Identifier, N_Character_Literal)
311    --  node as the result of the compile time evaluation of the node N. Val
312    --  is the result in the integer case and is the position of the literal
313    --  in the literals list for the enumeration case. Is_Static_Expression
314    --  is set True in the result node. The result is fully analyzed/resolved.
315    --  Static indicates whether the result should be considered static or
316    --  not (True = consider static). The point here is that normally all
317    --  string literals are static, but if this was the result of some
318    --  sequence of evaluation where values were known at compile time
319    --  but not static, then the result is not static.
320
321    procedure Fold_Ureal (N : Node_Id; Val : Ureal; Static : Boolean);
322    --  Rewrite N with a new N_Real_Literal node as the result of the compile
323    --  time evaluation of the node N. Val is the resulting real value from
324    --  the folding operation. The Is_Static_Expression flag is set in the
325    --  result node. The result is fully analyzed and result. Static
326    --  indicates whether the result should be considered static or not
327    --  (True = consider static). The point here is that normally all
328    --  string literals are static, but if this was the result of some
329    --  sequence of evaluation where values were known at compile time
330    --  but not static, then the result is not static.
331
332    function Is_In_Range
333      (N         : Node_Id;
334       Typ       : Entity_Id;
335       Fixed_Int : Boolean := False;
336       Int_Real  : Boolean := False) return Boolean;
337    --  Returns True if it can be guaranteed at compile time that expression
338    --  N is known to be in range of the subtype Typ. If the values of N or
339    --  of either bouds of Type are unknown at compile time, False will
340    --  always be returned. A result of False does not mean that the
341    --  expression is out of range, merely that it cannot be determined at
342    --  compile time that it is in range. If Typ is a floating point type or
343    --  Int_Real is set, any integer value is treated as though it was a real
344    --  value (i.e. the underlying real value is used).  In this case we use
345    --  the corresponding real value, both for the bounds of Typ, and for the
346    --  value of the expression N. If Typ is a fixed type or a discrete type
347    --  and Int_Real is False but flag Fixed_Int is True then any fixed-point
348    --  value is treated as though it was a discrete value (i.e. the
349    --  underlying integer value is used).  In this case we use the
350    --  corresponding integer value, both for the bounds of Typ, and for the
351    --  value of the expression N. If Typ is a discret type and Fixed_Int as
352    --  well as Int_Real are false, intere values are used throughout.
353
354    function Is_Out_Of_Range
355      (N         : Node_Id;
356       Typ       : Entity_Id;
357       Fixed_Int : Boolean := False;
358       Int_Real  : Boolean := False) return Boolean;
359    --  Returns True if it can be guaranteed at compile time that expression
360    --  N is known to be out of range of the subtype Typ.  True is returned
361    --  if Typ is a scalar type, at least one of whose bounds is known at
362    --  compile time, and N is a compile time known expression which can be
363    --  determined to be outside a compile_time known bound of Typ. A result
364    --  of False does not mean that the expression is in range, merely that
365    --  it cannot be determined at compile time that it is out of range. Flags
366    --  Int_Real and Fixed_Int are used as in routine Is_In_Range above.
367
368    function In_Subrange_Of
369      (T1        : Entity_Id;
370       T2        : Entity_Id;
371       Fixed_Int : Boolean := False) return Boolean;
372    --  Returns True if it can be guaranteed at compile time that the range
373    --  of values for scalar type T1 are always in the range of scalar type
374    --  T2.  A result of False does not mean that T1 is not in T2's subrange,
375    --  only that it cannot be determined at compile time. Flag Fixed_Int is
376    --  used as in routine Is_In_Range above.
377
378    function Is_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
379    --  Returns True if it can guarantee that Lo .. Hi is a null range.
380    --  If it cannot (because the value of Lo or Hi is not known at compile
381    --  time) then it returns False.
382
383    function Not_Null_Range (Lo : Node_Id; Hi : Node_Id) return Boolean;
384    --  Returns True if it can guarantee that Lo .. Hi is not a null range.
385    --  If it cannot (because the value of Lo or Hi is not known at compile
386    --  time) then it returns False.
387
388    procedure Why_Not_Static (Expr : Node_Id);
389    --  This procedure may be called after generating an error message that
390    --  complains that something is non-static. If it finds good reasons,
391    --  it generates one or more error messages pointing the appropriate
392    --  offending component of the expression. If no good reasons can be
393    --  figured out, then no messages are generated. The expectation here
394    --  is that the caller has already issued a message complaining that
395    --  the expression is non-static. Note that this message should be
396    --  placed using Error_Msg_F or Error_Msg_FE, so that it will sort
397    --  before any messages placed by this call. Note that it is fine to
398    --  call Why_Not_Static with something that is not an expression, and
399    --  usually this has no effect, but in some cases (N_Parameter_Association
400    --  or N_Range), it makes sense for the internal recursive calls.
401
402    procedure Initialize;
403    --  Initializes the internal data structures. Must be called before
404    --  each separate main program unit (e.g. in a GNSA/ASIS context).
405
406 private
407    --  The Eval routines are all marked inline, since they are called once
408
409    pragma Inline (Eval_Actual);
410    pragma Inline (Eval_Allocator);
411    pragma Inline (Eval_Character_Literal);
412    pragma Inline (Eval_Conditional_Expression);
413    pragma Inline (Eval_Indexed_Component);
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
424 end Sem_Eval;