1 // backend.h -- Go frontend interface to backend -*- C++ -*-
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.
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.
14 // The backend representation of a type.
17 // The backend represention of an expression.
20 // The backend representation of a statement.
23 // The backend representation of a function definition.
26 // The backend representation of a block.
29 // The backend representation of a variable.
32 // The backend representation of a label.
35 // The backend interface. This is a pure abstract class that a
36 // specific backend will implement.
41 virtual ~Backend() { }
43 // Name/type/location. Used for function parameters, struct fields,
45 struct Btyped_identifier
49 source_location location;
52 : name(), btype(NULL), location(UNKNOWN_LOCATION)
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)
63 // Produce an error type. Actually the backend could probably just
64 // crash if this is called.
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.
74 // Get the unnamed boolean type.
78 // Get an unnamed integer type with the given signedness and number
81 integer_type(bool is_unsigned, int bits) = 0;
83 // Get an unnamed floating point type with the given number of bits
86 float_type(int bits) = 0;
88 // Get an unnamed complex type with the given number of bits (64 or 128).
90 complex_type(int bits) = 0;
92 // Get a pointer type.
94 pointer_type(Btype* to_type) = 0;
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.
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;
105 // Get a struct type.
107 struct_type(const std::vector<Btyped_identifier>& fields) = 0;
109 // Get an array type.
111 array_type(Btype* element_type, Bexpression* length) = 0;
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. This function is also used for unnamed function
117 // types with multiple results, in which case the type has no name
118 // and NAME will be empty. FOR_FUNCTION is true if this is for a Go
119 // function type, which corresponds to a C/C++ pointer to function
120 // type. The return value will later be passed as the first
121 // parameter to set_placeholder_pointer_type or
122 // set_placeholder_function_type.
124 placeholder_pointer_type(const std::string& name, source_location,
125 bool for_function) = 0;
127 // Fill in a placeholder pointer type as a pointer. This takes a
128 // type returned by placeholder_pointer_type and arranges for it to
129 // point to to_type. Returns true on success, false on failure.
131 set_placeholder_pointer_type(Btype* placeholder, Btype* to_type) = 0;
133 // Fill in a placeholder pointer type as a function. This takes a
134 // type returned by placeholder_pointer_type and arranges for it to
135 // become a real Go function type (which corresponds to a C/C++
136 // pointer to function type). FT will be something returned by the
137 // function_type method. Returns true on success, false on failure.
139 set_placeholder_function_type(Btype* placeholder, Btype* ft) = 0;
141 // Create a placeholder struct type. This is used for a named
142 // struct type, as with placeholder_pointer_type.
144 placeholder_struct_type(const std::string& name, source_location) = 0;
146 // Fill in a placeholder struct type. This takes a type returned by
147 // placeholder_struct_type and arranges for it to become a real
148 // struct type. The parameter is as for struct_type. Returns true
149 // on success, false on failure.
151 set_placeholder_struct_type(Btype* placeholder,
152 const std::vector<Btyped_identifier>& fields)
155 // Create a placeholder array type. This is used for a named array
156 // type, as with placeholder_pointer_type, to handle cases like
159 placeholder_array_type(const std::string& name, source_location) = 0;
161 // Fill in a placeholder array type. This takes a type returned by
162 // placeholder_array_type and arranges for it to become a real array
163 // type. The parameters are as for array_type. Returns true on
164 // success, false on failure.
166 set_placeholder_array_type(Btype* placeholder, Btype* element_type,
167 Bexpression* length) = 0;
169 // Return a named version of a type. The location is the location
170 // of the type definition. This will not be called for a type
171 // created via placeholder_pointer_type, placeholder_struct_type, or
172 // placeholder_array_type.. (It may be called for a pointer,
173 // struct, or array type in a case like "type P *byte; type Q P".)
175 named_type(const std::string& name, Btype*, source_location) = 0;
177 // Create a marker for a circular pointer type. Go pointer and
178 // function types can refer to themselves in ways that are not
179 // permitted in C/C++. When a circular type is found, this function
180 // is called for the circular reference. This permits the backend
181 // to decide how to handle such a type. PLACEHOLDER is the
182 // placeholder type which has already been created; if the backend
183 // is prepared to handle a circular pointer type, it may simply
184 // return PLACEHOLDER. FOR_FUNCTION is true if this is for a
187 // For "type P *P" the sequence of calls will be
188 // bt1 = placeholder_pointer_type();
189 // bt2 = circular_pointer_type(bt1, false);
190 // set_placeholder_pointer_type(bt1, bt2);
192 circular_pointer_type(Btype* placeholder, bool for_function) = 0;
194 // Return whether the argument could be a special type created by
195 // circular_pointer_type. This is used to introduce explicit type
196 // conversions where needed. If circular_pointer_type returns its
197 // PLACEHOLDER parameter, this may safely always return false.
199 is_circular_pointer_type(Btype*) = 0;
203 // Create an error statement. This is used for cases which should
204 // not occur in a correct program, in order to keep the compilation
205 // going without crashing.
207 error_statement() = 0;
209 // Create an expression statement.
211 expression_statement(Bexpression*) = 0;
213 // Create a variable initialization statement. This initializes a
214 // local variable at the point in the program flow where it is
217 init_statement(Bvariable* var, Bexpression* init) = 0;
219 // Create an assignment statement.
221 assignment_statement(Bexpression* lhs, Bexpression* rhs,
222 source_location) = 0;
224 // Create a return statement, passing the representation of the
225 // function and the list of values to return.
227 return_statement(Bfunction*, const std::vector<Bexpression*>&,
228 source_location) = 0;
230 // Create an if statement. ELSE_BLOCK may be NULL.
232 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
233 source_location) = 0;
235 // Create a switch statement where the case values are constants.
236 // CASES and STATEMENTS must have the same number of entries. If
237 // VALUE matches any of the list in CASES[i], which will all be
238 // integers, then STATEMENTS[i] is executed. STATEMENTS[i] will
239 // either end with a goto statement or will fall through into
240 // STATEMENTS[i + 1]. CASES[i] is empty for the default clause,
241 // which need not be last.
243 switch_statement(Bexpression* value,
244 const std::vector<std::vector<Bexpression*> >& cases,
245 const std::vector<Bstatement*>& statements,
246 source_location) = 0;
248 // Create a single statement from two statements.
250 compound_statement(Bstatement*, Bstatement*) = 0;
252 // Create a single statement from a list of statements.
254 statement_list(const std::vector<Bstatement*>&) = 0;
258 // Create a block. The frontend will call this function when it
259 // starts converting a block within a function. FUNCTION is the
260 // current function. ENCLOSING is the enclosing block; it will be
261 // NULL for the top-level block in a function. VARS is the list of
262 // local variables defined within this block; each entry will be
263 // created by the local_variable function. START_LOCATION is the
264 // location of the start of the block, more or less the location of
265 // the initial curly brace. END_LOCATION is the location of the end
266 // of the block, more or less the location of the final curly brace.
267 // The statements will be added after the block is created.
269 block(Bfunction* function, Bblock* enclosing,
270 const std::vector<Bvariable*>& vars,
271 source_location start_location, source_location end_location) = 0;
273 // Add the statements to a block. The block is created first. Then
274 // the statements are created. Then the statements are added to the
275 // block. This will called exactly once per block. The vector may
276 // be empty if there are no statements.
278 block_add_statements(Bblock*, const std::vector<Bstatement*>&) = 0;
280 // Return the block as a statement. This is used to include a block
281 // in a list of statements.
283 block_statement(Bblock*) = 0;
287 // Create an error variable. This is used for cases which should
288 // not occur in a correct program, in order to keep the compilation
289 // going without crashing.
291 error_variable() = 0;
293 // Create a global variable. PACKAGE_NAME is the name of the
294 // package where the variable is defined. UNIQUE_PREFIX is the
295 // prefix for that package, from the -fgo-prefix option. NAME is
296 // the name of the variable. BTYPE is the type of the variable.
297 // IS_EXTERNAL is true if the variable is defined in some other
298 // package. IS_HIDDEN is true if the variable is not exported (name
299 // begins with a lower case letter). LOCATION is where the variable
302 global_variable(const std::string& package_name,
303 const std::string& unique_prefix,
304 const std::string& name,
308 source_location location) = 0;
310 // A global variable will 1) be initialized to zero, or 2) be
311 // initialized to a constant value, or 3) be initialized in the init
312 // function. In case 2, the frontend will call
313 // global_variable_set_init to set the initial value. If this is
314 // not called, the backend should initialize a global variable to 0.
315 // The init function may then assign a value to it.
317 global_variable_set_init(Bvariable*, Bexpression*) = 0;
319 // Create a local variable. The frontend will create the local
320 // variables first, and then create the block which contains them.
321 // FUNCTION is the function in which the variable is defined. NAME
322 // is the name of the variable. TYPE is the type. IS_ADDRESS_TAKEN
323 // is true if the address of this variable is taken (this implies
324 // that the address does not escape the function, as otherwise the
325 // variable would be on the heap). LOCATION is where the variable
326 // is defined. For each local variable the frontend will call
327 // init_statement to set the initial value.
329 local_variable(Bfunction* function, const std::string& name, Btype* type,
330 bool is_address_taken, source_location location) = 0;
332 // Create a function parameter. This is an incoming parameter, not
333 // a result parameter (result parameters are treated as local
334 // variables). The arguments are as for local_variable.
336 parameter_variable(Bfunction* function, const std::string& name,
337 Btype* type, bool is_address_taken,
338 source_location location) = 0;
340 // Create a temporary variable. A temporary variable has no name,
341 // just a type. We pass in FUNCTION and BLOCK in case they are
342 // needed. If INIT is not NULL, the variable should be initialized
343 // to that value. Otherwise the initial value is irrelevant--the
344 // backend does not have to explicitly initialize it to zero.
345 // ADDRESS_IS_TAKEN is true if the programs needs to take the
346 // address of this temporary variable. LOCATION is the location of
347 // the statement or expression which requires creating the temporary
348 // variable, and may not be very useful. This function should
349 // return a variable which can be referenced later and should set
350 // *PSTATEMENT to a statement which initializes the variable.
352 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression* init,
353 bool address_is_taken, source_location location,
354 Bstatement** pstatement) = 0;
358 // Create a new label. NAME will be empty if this is a label
359 // created by the frontend for a loop construct. The location is
360 // where the the label is defined.
362 label(Bfunction*, const std::string& name, source_location) = 0;
364 // Create a statement which defines a label. This statement will be
365 // put into the codestream at the point where the label should be
368 label_definition_statement(Blabel*) = 0;
370 // Create a goto statement to a label.
372 goto_statement(Blabel*, source_location) = 0;
374 // Create an expression for the address of a label. This is used to
375 // get the return address of a deferred function which may call
378 label_address(Blabel*, source_location) = 0;
381 // The backend interface has to define this function.
383 extern Backend* go_get_backend();
385 // FIXME: Temporary helper functions while converting to new backend
388 extern Btype* tree_to_type(tree);
389 extern Bexpression* tree_to_expr(tree);
390 extern Bstatement* tree_to_stat(tree);
391 extern Bfunction* tree_to_function(tree);
392 extern Bblock* tree_to_block(tree);
393 extern tree type_to_tree(Btype*);
394 extern tree expr_to_tree(Bexpression*);
395 extern tree stat_to_tree(Bstatement*);
396 extern tree block_to_tree(Bblock*);
397 extern tree var_to_tree(Bvariable*);
399 #endif // !defined(GO_BACKEND_H)