1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2005, Free Software Foundation, Inc. --
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, 51 Franklin Street, Fifth Floor, --
20 -- Boston, MA 02110-1301, USA. --
22 -- GNAT was originally developed by the GNAT team at New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc. --
25 ------------------------------------------------------------------------------
27 -- This procedure performs any required expansion for the specified node.
28 -- The argument is the node that is a candidate for possible expansion.
29 -- If no expansion is required, then Expand returns without doing anything.
31 -- If the node does need expansion, then the subtree is replaced by the
32 -- tree corresponding to the required rewriting. This tree is a syntactic
33 -- tree, except that all Entity fields must be correctly set on all
34 -- direct names, since the expander presumably knows what it wants, and in
35 -- any case it doesn't work to have the semantic analyzer perform visibility
36 -- analysis on these trees (they may have references to non-visible runtime
37 -- routines etc.) There are a few exceptions to this rule in special cases,
38 -- but they must be documented clearly.
40 -- Expand is called in two different situations:
42 -- Nodes that are not subexpressions (Nkind not in N_Subexpr)
44 -- In this case, Expand is called from the body of Sem, immediately
45 -- after completing semantic analysis by calling the corresponding
46 -- Analyze_N_xxx procedure. If expansion occurs, the given node must
47 -- be replaced with another node that is also not a subexpression.
48 -- This seems naturally to be the case, since it is hard to imagine any
49 -- situation in which it would make sense to replace a non-expression
50 -- subtree with an expression. Once the substitution is completed, the
51 -- Expand routine must call Analyze on the resulting node to do any
52 -- required semantic analysis. Note that references to children copied
53 -- from the old tree won't be reanalyzed, since their Analyzed flag
56 -- Nodes that are subexpressions (Nkind in N_Subexpr)
58 -- In this case, Expand is called from Sem_Res.Resolve after completing
59 -- the resolution of the subexpression (this means that the expander sees
60 -- the fully typed subtree). If expansion occurs, the given node must be
61 -- replaced by a node that is also a subexpression. Again it is hard
62 -- to see how this restriction could possibly be violated. Once the
63 -- substitution is completed, the Expand routine must first call Analyze
64 -- on the resulting node to do any required semantic analysis, and then
65 -- call Resolve on the node to set the type (typically the type will be
66 -- the same as the original type of the input node, but this is not
69 -- In both these cases, Replace or Rewrite must be used to achieve the
70 -- of the node, since the Expander routine is only passed the Node_Id
71 -- of the node to be expanded, and the resulting expanded Node_Id must
72 -- be the same (the parameter to Expand is mode in, not mode in-out).
74 -- For nodes other than subexpressions, it is not necessary to preserve the
75 -- original tree in the Expand routines, unlike the case for modifications
76 -- to the tree made in the semantic analyzer. This is because anyone who is
77 -- interested in working with the original tree (like ASIS) is required to
78 -- compile in semantics checks only mode. Thus Replace may be freely used
81 -- For subexpressions, preservation of the original tree is required because
82 -- of the need for conformance checking of default expressions, which occurs
83 -- on expanded trees. This means that Replace should not ever be used on
84 -- on subexpression nodes. Instead use Rewrite.
86 -- Note: the front end avoids calls to any of the expand routines if code
87 -- is not being generated. This is done for three reasons:
89 -- 1. Make sure tree does not get mucked up by the expander if no
90 -- code is being generated, and is thus usable by ASIS etc.
92 -- 2. Save time, since expansion is not needed if a compilation is
93 -- being done only to check the semantics, or if code generation
94 -- has been canceled due to previously detected errors.
96 -- 3. Allow the expand routines to assume that the tree is error free.
97 -- This results from the fact that code generation mode is always
98 -- cancelled when any error occurs.
100 -- If we ever decide to implement a feature allowing object modules to be
101 -- generated even if errors have been detected, then point 3 will no longer
102 -- hold, and the expand routines will have to be modified to operate properly
103 -- in the presence of errors (for many reasons this is not currently true).
105 -- Note: a consequence of this approach is that error messages must never
106 -- be generated in the expander, since this would mean that such error
107 -- messages are not generated when the expander is not being called.
109 -- Expansion is the last stage of analyzing a node, so Expand sets the
110 -- Analyzed flag of the node being analyzed as its last action. This is
111 -- done even if expansion is off (in this case, the only effect of the
112 -- call to Expand is to set the Analyzed flag to True).
114 with Types
; use Types
;
118 -- The flag Opt.Expander_Active controls whether expansion is active
119 -- (True) or deactivated (False). When expansion is deactivated all
120 -- calls to expander routines have no effect. To temporarily disable
121 -- expansion, always call the routines defined below, do NOT change
122 -- Expander_Active directly.
124 -- You should not use this flag to test if you are currently processing
125 -- a generic spec or body. Use the flag Inside_A_Generic instead (see
126 -- the spec of package Sem).
128 -- There is no good reason for permanently changing the value of this flag
129 -- except after detecting a syntactic or semantic error. In this event
130 -- this flag is set to False to disable all subsequent expansion activity.
132 -- In general this flag should be used as a read only value. The only
133 -- exceptions where it makes sense to temporarily change its value are:
135 -- (a) when starting/completing the processing of a generic definition
136 -- or declaration (see routines Start_Generic_Processing and
137 -- End_Generic_Processing in Sem_Ch12)
139 -- (b) when starting/completing the pre-analysis of an expression
140 -- (see the spec of package Sem for more info on pre-analysis.)
142 -- Note that when processing a default expression (In_Default_Expression
143 -- is True) or performing semantic analysis of a generic spec or body
144 -- (Inside_A_Generic) or when performing pre-analysis (Full_Analysis is
145 -- False) the Expander_Active flag is False.
147 procedure Expand
(N
: Node_Id
);
148 -- Expand node N, as described above
150 procedure Expander_Mode_Save_And_Set
(Status
: Boolean);
151 -- Saves the current setting of the Expander_Active flag on an internal
152 -- stack and then sets the flag to the given value.
154 -- Note: this routine has no effect in ASIS_Mode. In ASIS_Mode, all
155 -- expansion activity is always off, since we want the original semantic
156 -- tree for ASIS purposes without any expansion. This is achieved by
157 -- setting Expander_Active False in ASIS_Mode. In situations such as
158 -- the call to Instantiate_Bodies in Frontend, Expander_Mode_Save_And_Set
159 -- may be called to temporarily turn the expander on, but this will have
160 -- no effect in ASIS mode.
162 procedure Expander_Mode_Restore
;
163 -- Restores the setting of the Expander_Active flag using the top entry
164 -- pushed onto the stack by Expander_Mode_Save_And_Reset, popping the
165 -- stack, except that if any errors have been detected, then the state
166 -- of the flag is left set to False. Disabled for ASIS_Mode (see above).