OSDN Git Service

Fix bug with taking address of a variable when address does not escape.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / backend.h
1 // backend.h -- Go frontend interface to backend  -*- C++ -*-
2
3 // Copyright 2011 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #ifndef GO_BACKEND_H
8 #define GO_BACKEND_H
9
10 // Pointers to these types are created by the backend, passed to the
11 // frontend, and passed back to the backend.  The types must be
12 // defined by the backend using these names.
13
14 // The backend representation of a type.
15 class Btype;
16
17 // The backend represention of an expression.
18 class Bexpression;
19
20 // The backend representation of a statement.
21 class Bstatement;
22
23 // The backend representation of a function definition.
24 class Bfunction;
25
26 // The backend representation of a block.
27 class Bblock;
28
29 // The backend representation of a variable.
30 class Bvariable;
31
32 // The backend representation of a label.
33 class Blabel;
34
35 // The backend interface.  This is a pure abstract class that a
36 // specific backend will implement.
37
38 class Backend
39 {
40  public:
41   virtual ~Backend() { }
42
43   // Name/type/location.  Used for function parameters, struct fields,
44   // interface methods.
45   struct Btyped_identifier
46   {
47     std::string name;
48     Btype* btype;
49     source_location location;
50
51     Btyped_identifier()
52       : name(), btype(NULL), location(UNKNOWN_LOCATION)
53     { }
54
55     Btyped_identifier(const std::string& a_name, Btype* a_btype,
56                      source_location a_location)
57       : name(a_name), btype(a_btype), location(a_location)
58     { }
59   };
60
61   // Types.
62
63   // Produce an error type.  Actually the backend could probably just
64   // crash if this is called.
65   virtual Btype*
66   error_type() = 0;
67
68   // Get a void type.  This is used in (at least) two ways: 1) as the
69   // return type of a function with no result parameters; 2)
70   // unsafe.Pointer is represented as *void.
71   virtual Btype*
72   void_type() = 0;
73
74   // Get the unnamed boolean type.
75   virtual Btype*
76   bool_type() = 0;
77
78   // Get an unnamed integer type with the given signedness and number
79   // of bits.
80   virtual Btype*
81   integer_type(bool is_unsigned, int bits) = 0;
82
83   // Get an unnamed floating point type with the given number of bits
84   // (32 or 64).
85   virtual Btype*
86   float_type(int bits) = 0;
87
88   // Get an unnamed complex type with the given number of bits (64 or 128).
89   virtual Btype*
90   complex_type(int bits) = 0;
91
92   // Get a pointer type.
93   virtual Btype*
94   pointer_type(Btype* to_type) = 0;
95
96   // Get a function type.  The receiver, parameter, and results are
97   // generated from the types in the Function_type.  The Function_type
98   // is provided so that the names are available.
99   virtual Btype*
100   function_type(const Btyped_identifier& receiver,
101                 const std::vector<Btyped_identifier>& parameters,
102                 const std::vector<Btyped_identifier>& results,
103                 source_location location) = 0;
104
105   // Get a struct type.
106   virtual Btype*
107   struct_type(const std::vector<Btyped_identifier>& fields) = 0;
108
109   // Get an array type.
110   virtual Btype*
111   array_type(Btype* element_type, Bexpression* length) = 0;
112
113   // Create a placeholder pointer type.  This is used for a named
114   // pointer type, since in Go a pointer type may refer to itself.
115   // NAME is the name of the type, and the location is where the named
116   // type is defined.  FOR_FUNCTION is true if this is for a Go
117   // function type, which corresponds to a C/C++ pointer to function
118   // type.  The return value will later be passed as the first
119   // parameter to set_placeholder_pointer_type or
120   // set_placeholder_function_type.
121   virtual Btype*
122   placeholder_pointer_type(const std::string& name, source_location,
123                            bool for_function) = 0;
124
125   // Fill in a placeholder pointer type as a pointer.  This takes a
126   // type returned by placeholder_pointer_type and arranges for it to
127   // point to to_type.  Returns true on success, false on failure.
128   virtual bool
129   set_placeholder_pointer_type(Btype* placeholder, Btype* to_type) = 0;
130
131   // Fill in a placeholder pointer type as a function.  This takes a
132   // type returned by placeholder_pointer_type and arranges for it to
133   // become a real Go function type (which corresponds to a C/C++
134   // pointer to function type).  FT will be something returned by the
135   // function_type method.  Returns true on success, false on failure.
136   virtual bool
137   set_placeholder_function_type(Btype* placeholder, Btype* ft) = 0;
138
139   // Create a placeholder struct type.  This is used for a named
140   // struct type, as with placeholder_pointer_type.
141   virtual Btype*
142   placeholder_struct_type(const std::string& name, source_location) = 0;
143
144   // Fill in a placeholder struct type.  This takes a type returned by
145   // placeholder_struct_type and arranges for it to become a real
146   // struct type.  The parameter is as for struct_type.  Returns true
147   // on success, false on failure.
148   virtual bool
149   set_placeholder_struct_type(Btype* placeholder,
150                               const std::vector<Btyped_identifier>& fields)
151                         = 0;
152
153   // Create a placeholder array type.  This is used for a named array
154   // type, as with placeholder_pointer_type, to handle cases like
155   // type A []*A.
156   virtual Btype*
157   placeholder_array_type(const std::string& name, source_location) = 0;
158
159   // Fill in a placeholder array type.  This takes a type returned by
160   // placeholder_array_type and arranges for it to become a real array
161   // type.  The parameters are as for array_type.  Returns true on
162   // success, false on failure.
163   virtual bool
164   set_placeholder_array_type(Btype* placeholder, Btype* element_type,
165                              Bexpression* length) = 0;
166
167   // Return a named version of a type.  The location is the location
168   // of the type definition.  This will not be called for a type
169   // created via placeholder_pointer_type, placeholder_struct_type, or
170   // placeholder_array_type..  (It may be called for a pointer,
171   // struct, or array type in a case like "type P *byte; type Q P".)
172   virtual Btype*
173   named_type(const std::string& name, Btype*, source_location) = 0;
174
175   // Create a marker for a circular pointer type.  Go pointer and
176   // function types can refer to themselves in ways that are not
177   // permitted in C/C++.  When a circular type is found, this function
178   // is called for the circular reference.  This permits the backend
179   // to decide how to handle such a type.  PLACEHOLDER is the
180   // placeholder type which has already been created; if the backend
181   // is prepared to handle a circular pointer type, it may simply
182   // return PLACEHOLDER.  FOR_FUNCTION is true if this is for a
183   // function type.
184   //
185   // For "type P *P" the sequence of calls will be
186   //   bt1 = placeholder_pointer_type();
187   //   bt2 = circular_pointer_type(bt1, false);
188   //   set_placeholder_pointer_type(bt1, bt2);
189   virtual Btype*
190   circular_pointer_type(Btype* placeholder, bool for_function) = 0;
191
192   // Return whether the argument could be a special type created by
193   // circular_pointer_type.  This is used to introduce explicit type
194   // conversions where needed.  If circular_pointer_type returns its
195   // PLACEHOLDER parameter, this may safely always return false.
196   virtual bool
197   is_circular_pointer_type(Btype*) = 0;
198
199   // Statements.
200
201   // Create an error statement.  This is used for cases which should
202   // not occur in a correct program, in order to keep the compilation
203   // going without crashing.
204   virtual Bstatement*
205   error_statement() = 0;
206
207   // Create an expression statement.
208   virtual Bstatement*
209   expression_statement(Bexpression*) = 0;
210
211   // Create a variable initialization statement.  This initializes a
212   // local variable at the point in the program flow where it is
213   // declared.
214   virtual Bstatement*
215   init_statement(Bvariable* var, Bexpression* init) = 0;
216
217   // Create an assignment statement.
218   virtual Bstatement*
219   assignment_statement(Bexpression* lhs, Bexpression* rhs,
220                        source_location) = 0;
221
222   // Create a return statement, passing the representation of the
223   // function and the list of values to return.
224   virtual Bstatement*
225   return_statement(Bfunction*, const std::vector<Bexpression*>&,
226                    source_location) = 0;
227
228   // Create an if statement.  ELSE_BLOCK may be NULL.
229   virtual Bstatement*
230   if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
231                source_location) = 0;
232
233   // Create a switch statement where the case values are constants.
234   // CASES and STATEMENTS must have the same number of entries.  If
235   // VALUE matches any of the list in CASES[i], which will all be
236   // integers, then STATEMENTS[i] is executed.  STATEMENTS[i] will
237   // either end with a goto statement or will fall through into
238   // STATEMENTS[i + 1].  CASES[i] is empty for the default clause,
239   // which need not be last.
240   virtual Bstatement*
241   switch_statement(Bexpression* value,
242                    const std::vector<std::vector<Bexpression*> >& cases,
243                    const std::vector<Bstatement*>& statements,
244                    source_location) = 0;
245
246   // Create a single statement from two statements.
247   virtual Bstatement*
248   compound_statement(Bstatement*, Bstatement*) = 0;
249
250   // Create a single statement from a list of statements.
251   virtual Bstatement*
252   statement_list(const std::vector<Bstatement*>&) = 0;
253
254   // Blocks.
255
256   // Create a block.  The frontend will call this function when it
257   // starts converting a block within a function.  FUNCTION is the
258   // current function.  ENCLOSING is the enclosing block; it will be
259   // NULL for the top-level block in a function.  VARS is the list of
260   // local variables defined within this block; each entry will be
261   // created by the local_variable function.  START_LOCATION is the
262   // location of the start of the block, more or less the location of
263   // the initial curly brace.  END_LOCATION is the location of the end
264   // of the block, more or less the location of the final curly brace.
265   // The statements will be added after the block is created.
266   virtual Bblock*
267   block(Bfunction* function, Bblock* enclosing,
268         const std::vector<Bvariable*>& vars,
269         source_location start_location, source_location end_location) = 0;
270
271   // Add the statements to a block.  The block is created first.  Then
272   // the statements are created.  Then the statements are added to the
273   // block.  This will called exactly once per block.  The vector may
274   // be empty if there are no statements.
275   virtual void
276   block_add_statements(Bblock*, const std::vector<Bstatement*>&) = 0;
277
278   // Return the block as a statement.  This is used to include a block
279   // in a list of statements.
280   virtual Bstatement*
281   block_statement(Bblock*) = 0;
282
283   // Variables.
284
285   // Create an error variable.  This is used for cases which should
286   // not occur in a correct program, in order to keep the compilation
287   // going without crashing.
288   virtual Bvariable*
289   error_variable() = 0;
290
291   // Create a global variable.  PACKAGE_NAME is the name of the
292   // package where the variable is defined.  UNIQUE_PREFIX is the
293   // prefix for that package, from the -fgo-prefix option.  NAME is
294   // the name of the variable.  BTYPE is the type of the variable.
295   // IS_EXTERNAL is true if the variable is defined in some other
296   // package.  IS_HIDDEN is true if the variable is not exported (name
297   // begins with a lower case letter).  LOCATION is where the variable
298   // was defined.
299   virtual Bvariable*
300   global_variable(const std::string& package_name,
301                   const std::string& unique_prefix,
302                   const std::string& name,
303                   Btype* btype,
304                   bool is_external,
305                   bool is_hidden,
306                   source_location location) = 0;
307
308   // A global variable will 1) be initialized to zero, or 2) be
309   // initialized to a constant value, or 3) be initialized in the init
310   // function.  In case 2, the frontend will call
311   // global_variable_set_init to set the initial value.  If this is
312   // not called, the backend should initialize a global variable to 0.
313   // The init function may then assign a value to it.
314   virtual void
315   global_variable_set_init(Bvariable*, Bexpression*) = 0;
316
317   // Create a local variable.  The frontend will create the local
318   // variables first, and then create the block which contains them.
319   // FUNCTION is the function in which the variable is defined.  NAME
320   // is the name of the variable.  TYPE is the type.  IS_ADDRESS_TAKEN
321   // is true if the address of this variable is taken (this implies
322   // that the address does not escape the function, as otherwise the
323   // variable would be on the heap).  LOCATION is where the variable
324   // is defined.  For each local variable the frontend will call
325   // init_statement to set the initial value.
326   virtual Bvariable*
327   local_variable(Bfunction* function, const std::string& name, Btype* type,
328                  bool is_address_taken, source_location location) = 0;
329
330   // Create a function parameter.  This is an incoming parameter, not
331   // a result parameter (result parameters are treated as local
332   // variables).  The arguments are as for local_variable.
333   virtual Bvariable*
334   parameter_variable(Bfunction* function, const std::string& name,
335                      Btype* type, bool is_address_taken,
336                      source_location location) = 0;
337
338   // Create a temporary variable.  A temporary variable has no name,
339   // just a type.  We pass in FUNCTION and BLOCK in case they are
340   // needed.  If INIT is not NULL, the variable should be initialized
341   // to that value.  Otherwise the initial value is irrelevant--the
342   // backend does not have to explicitly initialize it to zero.
343   // ADDRESS_IS_TAKEN is true if the programs needs to take the
344   // address of this temporary variable.  LOCATION is the location of
345   // the statement or expression which requires creating the temporary
346   // variable, and may not be very useful.  This function should
347   // return a variable which can be referenced later and should set
348   // *PSTATEMENT to a statement which initializes the variable.
349   virtual Bvariable*
350   temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression* init,
351                      bool address_is_taken, source_location location,
352                      Bstatement** pstatement) = 0;
353
354   // Labels.
355   
356   // Create a new label.  NAME will be empty if this is a label
357   // created by the frontend for a loop construct.  The location is
358   // where the the label is defined.
359   virtual Blabel*
360   label(Bfunction*, const std::string& name, source_location) = 0;
361
362   // Create a statement which defines a label.  This statement will be
363   // put into the codestream at the point where the label should be
364   // defined.
365   virtual Bstatement*
366   label_definition_statement(Blabel*) = 0;
367
368   // Create a goto statement to a label.
369   virtual Bstatement*
370   goto_statement(Blabel*, source_location) = 0;
371
372   // Create an expression for the address of a label.  This is used to
373   // get the return address of a deferred function which may call
374   // recover.
375   virtual Bexpression*
376   label_address(Blabel*, source_location) = 0;
377 };
378
379 // The backend interface has to define this function.
380
381 extern Backend* go_get_backend();
382
383 // FIXME: Temporary helper functions while converting to new backend
384 // interface.
385
386 extern Btype* tree_to_type(tree);
387 extern Bexpression* tree_to_expr(tree);
388 extern Bstatement* tree_to_stat(tree);
389 extern Bfunction* tree_to_function(tree);
390 extern Bblock* tree_to_block(tree);
391 extern tree type_to_tree(Btype*);
392 extern tree expr_to_tree(Bexpression*);
393 extern tree stat_to_tree(Bstatement*);
394 extern tree block_to_tree(Bblock*);
395 extern tree var_to_tree(Bvariable*);
396
397 #endif // !defined(GO_BACKEND_H)