annotate gcc/ada/expander.ads @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
5 -- E X P A N D E R --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- S p e c --
kono
parents:
diff changeset
8 -- --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
111
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
kono
parents:
diff changeset
17 -- for more details. You should have received a copy of the GNU General --
kono
parents:
diff changeset
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
kono
parents:
diff changeset
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
kono
parents:
diff changeset
20 -- --
kono
parents:
diff changeset
21 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
23 -- --
kono
parents:
diff changeset
24 ------------------------------------------------------------------------------
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 -- This procedure performs any required expansion for the specified node.
kono
parents:
diff changeset
27 -- The argument is the node that is a candidate for possible expansion.
kono
parents:
diff changeset
28 -- If no expansion is required, then Expand returns without doing anything.
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 -- If the node does need expansion, then the subtree is replaced by the
kono
parents:
diff changeset
31 -- tree corresponding to the required rewriting. This tree is a syntactic
kono
parents:
diff changeset
32 -- tree, except that all Entity fields must be correctly set on all
kono
parents:
diff changeset
33 -- direct names, since the expander presumably knows what it wants, and in
kono
parents:
diff changeset
34 -- any case it doesn't work to have the semantic analyzer perform visibility
kono
parents:
diff changeset
35 -- analysis on these trees (they may have references to non-visible runtime
kono
parents:
diff changeset
36 -- routines etc.) There are a few exceptions to this rule in special cases,
kono
parents:
diff changeset
37 -- but they must be documented clearly.
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 -- Expand is called in two different situations:
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 -- Nodes that are not subexpressions (Nkind not in N_Subexpr)
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 -- In this case, Expand is called from the body of Sem, immediately
kono
parents:
diff changeset
44 -- after completing semantic analysis by calling the corresponding
kono
parents:
diff changeset
45 -- Analyze_N_xxx procedure. If expansion occurs, the given node must
kono
parents:
diff changeset
46 -- be replaced with another node that is also not a subexpression.
kono
parents:
diff changeset
47 -- This seems naturally to be the case, since it is hard to imagine any
kono
parents:
diff changeset
48 -- situation in which it would make sense to replace a non-expression
kono
parents:
diff changeset
49 -- subtree with an expression. Once the substitution is completed, the
kono
parents:
diff changeset
50 -- Expand routine must call Analyze on the resulting node to do any
kono
parents:
diff changeset
51 -- required semantic analysis. Note that references to children copied
kono
parents:
diff changeset
52 -- from the old tree won't be reanalyzed, since their Analyzed flag
kono
parents:
diff changeset
53 -- is set.
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 -- Nodes that are subexpressions (Nkind in N_Subexpr)
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 -- In this case, Expand is called from Sem_Res.Resolve after completing
kono
parents:
diff changeset
58 -- the resolution of the subexpression (this means that the expander sees
kono
parents:
diff changeset
59 -- the fully typed subtree). If expansion occurs, the given node must be
kono
parents:
diff changeset
60 -- replaced by a node that is also a subexpression. Again it is hard
kono
parents:
diff changeset
61 -- to see how this restriction could possibly be violated. Once the
kono
parents:
diff changeset
62 -- substitution is completed, the Expand routine must first call Analyze
kono
parents:
diff changeset
63 -- on the resulting node to do any required semantic analysis, and then
kono
parents:
diff changeset
64 -- call Resolve on the node to set the type (typically the type will be
kono
parents:
diff changeset
65 -- the same as the original type of the input node, but this is not
kono
parents:
diff changeset
66 -- always the case).
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 -- In both these cases, Replace or Rewrite must be used to achieve the
kono
parents:
diff changeset
69 -- expansion of the node, since the Expander routine is only passed the
kono
parents:
diff changeset
70 -- Node_Id of the node to be expanded, and the resulting expanded Node_Id
kono
parents:
diff changeset
71 -- must be the same (the parameter to Expand is mode in, not mode in-out).
kono
parents:
diff changeset
72
kono
parents:
diff changeset
73 -- For nodes other than subexpressions, it is not necessary to preserve the
kono
parents:
diff changeset
74 -- original tree in the Expand routines, unlike the case for modifications
kono
parents:
diff changeset
75 -- to the tree made in the semantic analyzer. This is because anyone who is
kono
parents:
diff changeset
76 -- interested in working with the original tree (like ASIS) is required to
kono
parents:
diff changeset
77 -- compile in semantics checks only mode. Thus Replace may be freely used
kono
parents:
diff changeset
78 -- in such instances.
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 -- For subexpressions, preservation of the original tree is required because
kono
parents:
diff changeset
81 -- of the need for conformance checking of default expressions, which occurs
kono
parents:
diff changeset
82 -- on expanded trees. This means that Replace should not ever be used on
kono
parents:
diff changeset
83 -- on subexpression nodes. Instead use Rewrite.
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 -- Note: the front end avoids calls to any of the expand routines if code
kono
parents:
diff changeset
86 -- is not being generated. This is done for three reasons:
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 -- 1. Make sure tree does not get mucked up by the expander if no
kono
parents:
diff changeset
89 -- code is being generated, and is thus usable by ASIS etc.
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 -- 2. Save time, since expansion is not needed if a compilation is
kono
parents:
diff changeset
92 -- being done only to check the semantics, or if code generation
kono
parents:
diff changeset
93 -- has been canceled due to previously detected errors.
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 -- 3. Allow the expand routines to assume that the tree is error free.
kono
parents:
diff changeset
96 -- This results from the fact that code generation mode is always
kono
parents:
diff changeset
97 -- cancelled when any error occurs.
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 -- If we ever decide to implement a feature allowing object modules to be
kono
parents:
diff changeset
100 -- generated even if errors have been detected, then point 3 will no longer
kono
parents:
diff changeset
101 -- hold, and the expand routines will have to be modified to operate properly
kono
parents:
diff changeset
102 -- in the presence of errors (for many reasons this is not currently true).
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 -- Note: a consequence of this approach is that error messages must never
kono
parents:
diff changeset
105 -- be generated in the expander, since this would mean that such error
kono
parents:
diff changeset
106 -- messages are not generated when the expander is not being called.
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 -- Expansion is the last stage of analyzing a node, so Expand sets the
kono
parents:
diff changeset
109 -- Analyzed flag of the node being analyzed as its last action. This is
kono
parents:
diff changeset
110 -- done even if expansion is off (in this case, the only effect of the
kono
parents:
diff changeset
111 -- call to Expand is to set the Analyzed flag to True).
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 with Types; use Types;
kono
parents:
diff changeset
114
kono
parents:
diff changeset
115 package Expander is
kono
parents:
diff changeset
116
kono
parents:
diff changeset
117 -- The flag Opt.Expander_Active controls whether expansion is active
kono
parents:
diff changeset
118 -- (True) or deactivated (False). When expansion is deactivated all
kono
parents:
diff changeset
119 -- calls to expander routines have no effect. To temporarily disable
kono
parents:
diff changeset
120 -- expansion, always call the routines defined below, do NOT change
kono
parents:
diff changeset
121 -- Expander_Active directly.
kono
parents:
diff changeset
122 --
kono
parents:
diff changeset
123 -- You should not use this flag to test if you are currently processing
kono
parents:
diff changeset
124 -- a generic spec or body. Use the flag Inside_A_Generic instead (see
kono
parents:
diff changeset
125 -- the spec of package Sem).
kono
parents:
diff changeset
126 --
kono
parents:
diff changeset
127 -- There is no good reason for permanently changing the value of this flag
kono
parents:
diff changeset
128 -- except after detecting a syntactic or semantic error. In this event
kono
parents:
diff changeset
129 -- this flag is set to False to disable all subsequent expansion activity.
kono
parents:
diff changeset
130 --
kono
parents:
diff changeset
131 -- In general this flag should be used as a read only value. The only
kono
parents:
diff changeset
132 -- exceptions where it makes sense to temporarily change its value are:
kono
parents:
diff changeset
133 --
kono
parents:
diff changeset
134 -- (a) when starting/completing the processing of a generic definition
kono
parents:
diff changeset
135 -- or declaration (see routines Start_Generic_Processing and
kono
parents:
diff changeset
136 -- End_Generic_Processing in Sem_Ch12)
kono
parents:
diff changeset
137 --
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
138 -- (b) when starting/completing the preanalysis of an expression
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
139 -- (see the spec of package Sem for more info on preanalysis.)
111
kono
parents:
diff changeset
140 --
kono
parents:
diff changeset
141 -- Note that when processing a spec expression (In_Spec_Expression
kono
parents:
diff changeset
142 -- is True) or performing semantic analysis of a generic spec or body
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
143 -- (Inside_A_Generic) or when performing preanalysis (Full_Analysis is
111
kono
parents:
diff changeset
144 -- False) the Expander_Active flag is False.
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 procedure Expand (N : Node_Id);
kono
parents:
diff changeset
147 -- Expand node N, as described above
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 procedure Expander_Mode_Save_And_Set (Status : Boolean);
kono
parents:
diff changeset
150 -- Saves the current setting of the Expander_Active flag on an internal
kono
parents:
diff changeset
151 -- stack and then sets the flag to the given value.
kono
parents:
diff changeset
152 --
kono
parents:
diff changeset
153 -- Note: this routine has no effect in ASIS and GNATprove modes. In ASIS
kono
parents:
diff changeset
154 -- mode, all expansion activity is always off, since we want the original
kono
parents:
diff changeset
155 -- semantic tree for ASIS purposes without any expansion. In GNATprove
kono
parents:
diff changeset
156 -- mode, a very light expansion is performed on specific nodes. Both are
kono
parents:
diff changeset
157 -- achieved by setting Expander_Active False in ASIS and GNATprove modes.
kono
parents:
diff changeset
158 -- In situations such as the call to Instantiate_Bodies in Frontend,
kono
parents:
diff changeset
159 -- Expander_Mode_Save_And_Set may be called to temporarily turn the
kono
parents:
diff changeset
160 -- expander on, but this will have no effect in ASIS and GNATprove modes.
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 procedure Expander_Mode_Restore;
kono
parents:
diff changeset
163 -- Restores the setting of the Expander_Active flag using the top entry
kono
parents:
diff changeset
164 -- pushed onto the stack by Expander_Mode_Save_And_Reset, popping the
kono
parents:
diff changeset
165 -- stack, except that if any errors have been detected, then the state of
kono
parents:
diff changeset
166 -- the flag is left set to False. Disabled for ASIS and GNATprove modes
kono
parents:
diff changeset
167 -- (see above).
kono
parents:
diff changeset
168
kono
parents:
diff changeset
169 end Expander;