compiler: move Backend/Linemap creation out of front end.
[gcc.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 #include <gmp.h>
11 #include <mpfr.h>
12 #include <mpc.h>
13
14 #include "operator.h"
15
16 // Pointers to these types are created by the backend, passed to the
17 // frontend, and passed back to the backend. The types must be
18 // defined by the backend using these names.
19
20 // The backend representation of a type.
21 class Btype;
22
23 // The backend represention of an expression.
24 class Bexpression;
25
26 // The backend representation of a statement.
27 class Bstatement;
28
29 // The backend representation of a function definition or declaration.
30 class Bfunction;
31
32 // The backend representation of a block.
33 class Bblock;
34
35 // The backend representation of a variable.
36 class Bvariable;
37
38 // The backend representation of a label.
39 class Blabel;
40
41 // The backend interface. This is a pure abstract class that a
42 // specific backend will implement.
43
44 class Backend
45 {
46 public:
47 virtual ~Backend() { }
48
49 // Name/type/location. Used for function parameters, struct fields,
50 // interface methods.
51 struct Btyped_identifier
52 {
53 std::string name;
54 Btype* btype;
55 Location location;
56
57 Btyped_identifier()
58 : name(), btype(NULL), location(Linemap::unknown_location())
59 { }
60
61 Btyped_identifier(const std::string& a_name, Btype* a_btype,
62 Location a_location)
63 : name(a_name), btype(a_btype), location(a_location)
64 { }
65 };
66
67 // Types.
68
69 // Produce an error type. Actually the backend could probably just
70 // crash if this is called.
71 virtual Btype*
72 error_type() = 0;
73
74 // Get a void type. This is used in (at least) two ways: 1) as the
75 // return type of a function with no result parameters; 2)
76 // unsafe.Pointer is represented as *void.
77 virtual Btype*
78 void_type() = 0;
79
80 // Get the unnamed boolean type.
81 virtual Btype*
82 bool_type() = 0;
83
84 // Get an unnamed integer type with the given signedness and number
85 // of bits.
86 virtual Btype*
87 integer_type(bool is_unsigned, int bits) = 0;
88
89 // Get an unnamed floating point type with the given number of bits
90 // (32 or 64).
91 virtual Btype*
92 float_type(int bits) = 0;
93
94 // Get an unnamed complex type with the given number of bits (64 or 128).
95 virtual Btype*
96 complex_type(int bits) = 0;
97
98 // Get a pointer type.
99 virtual Btype*
100 pointer_type(Btype* to_type) = 0;
101
102 // Get a function type. The receiver, parameter, and results are
103 // generated from the types in the Function_type. The Function_type
104 // is provided so that the names are available. This should return
105 // not the type of a Go function (which is a pointer to a struct)
106 // but the type of a C function pointer (which will be used as the
107 // type of the first field of the struct). If there is more than
108 // one result, RESULT_STRUCT is a struct type to hold the results,
109 // and RESULTS may be ignored; if there are zero or one results,
110 // RESULT_STRUCT is NULL.
111 virtual Btype*
112 function_type(const Btyped_identifier& receiver,
113 const std::vector<Btyped_identifier>& parameters,
114 const std::vector<Btyped_identifier>& results,
115 Btype* result_struct,
116 Location location) = 0;
117
118 // Get a struct type.
119 virtual Btype*
120 struct_type(const std::vector<Btyped_identifier>& fields) = 0;
121
122 // Get an array type.
123 virtual Btype*
124 array_type(Btype* element_type, Bexpression* length) = 0;
125
126 // Create a placeholder pointer type. This is used for a named
127 // pointer type, since in Go a pointer type may refer to itself.
128 // NAME is the name of the type, and the location is where the named
129 // type is defined. This function is also used for unnamed function
130 // types with multiple results, in which case the type has no name
131 // and NAME will be empty. FOR_FUNCTION is true if this is for a C
132 // pointer to function type. A Go func type is represented as a
133 // pointer to a struct, and the first field of the struct is a C
134 // pointer to function. The return value will later be passed as
135 // the first parameter to set_placeholder_pointer_type or
136 // set_placeholder_function_type.
137 virtual Btype*
138 placeholder_pointer_type(const std::string& name, Location,
139 bool for_function) = 0;
140
141 // Fill in a placeholder pointer type as a pointer. This takes a
142 // type returned by placeholder_pointer_type and arranges for it to
143 // point to the type that TO_TYPE points to (that is, PLACEHOLDER
144 // becomes the same type as TO_TYPE). Returns true on success,
145 // false on failure.
146 virtual bool
147 set_placeholder_pointer_type(Btype* placeholder, Btype* to_type) = 0;
148
149 // Fill in a placeholder pointer type as a function. This takes a
150 // type returned by placeholder_pointer_type and arranges for it to
151 // become a real Go function type (which corresponds to a C/C++
152 // pointer to function type). FT will be something returned by the
153 // function_type method. Returns true on success, false on failure.
154 virtual bool
155 set_placeholder_function_type(Btype* placeholder, Btype* ft) = 0;
156
157 // Create a placeholder struct type. This is used for a named
158 // struct type, as with placeholder_pointer_type. It is also used
159 // for interface types, in which case NAME will be the empty string.
160 virtual Btype*
161 placeholder_struct_type(const std::string& name, Location) = 0;
162
163 // Fill in a placeholder struct type. This takes a type returned by
164 // placeholder_struct_type and arranges for it to become a real
165 // struct type. The parameter is as for struct_type. Returns true
166 // on success, false on failure.
167 virtual bool
168 set_placeholder_struct_type(Btype* placeholder,
169 const std::vector<Btyped_identifier>& fields)
170 = 0;
171
172 // Create a placeholder array type. This is used for a named array
173 // type, as with placeholder_pointer_type, to handle cases like
174 // type A []*A.
175 virtual Btype*
176 placeholder_array_type(const std::string& name, Location) = 0;
177
178 // Fill in a placeholder array type. This takes a type returned by
179 // placeholder_array_type and arranges for it to become a real array
180 // type. The parameters are as for array_type. Returns true on
181 // success, false on failure.
182 virtual bool
183 set_placeholder_array_type(Btype* placeholder, Btype* element_type,
184 Bexpression* length) = 0;
185
186 // Return a named version of a type. The location is the location
187 // of the type definition. This will not be called for a type
188 // created via placeholder_pointer_type, placeholder_struct_type, or
189 // placeholder_array_type.. (It may be called for a pointer,
190 // struct, or array type in a case like "type P *byte; type Q P".)
191 virtual Btype*
192 named_type(const std::string& name, Btype*, Location) = 0;
193
194 // Create a marker for a circular pointer type. Go pointer and
195 // function types can refer to themselves in ways that are not
196 // permitted in C/C++. When a circular type is found, this function
197 // is called for the circular reference. This permits the backend
198 // to decide how to handle such a type. PLACEHOLDER is the
199 // placeholder type which has already been created; if the backend
200 // is prepared to handle a circular pointer type, it may simply
201 // return PLACEHOLDER. FOR_FUNCTION is true if this is for a
202 // function type.
203 //
204 // For "type P *P" the sequence of calls will be
205 // bt1 = placeholder_pointer_type();
206 // bt2 = circular_pointer_type(bt1, false);
207 // set_placeholder_pointer_type(bt1, bt2);
208 virtual Btype*
209 circular_pointer_type(Btype* placeholder, bool for_function) = 0;
210
211 // Return whether the argument could be a special type created by
212 // circular_pointer_type. This is used to introduce explicit type
213 // conversions where needed. If circular_pointer_type returns its
214 // PLACEHOLDER parameter, this may safely always return false.
215 virtual bool
216 is_circular_pointer_type(Btype*) = 0;
217
218 // Return the size of a type.
219 virtual int64_t
220 type_size(Btype*) = 0;
221
222 // Return the alignment of a type.
223 virtual int64_t
224 type_alignment(Btype*) = 0;
225
226 // Return the alignment of a struct field of this type. This is
227 // normally the same as type_alignment, but not always.
228 virtual int64_t
229 type_field_alignment(Btype*) = 0;
230
231 // Return the offset of field INDEX in a struct type. INDEX is the
232 // entry in the FIELDS std::vector parameter of struct_type or
233 // set_placeholder_struct_type.
234 virtual int64_t
235 type_field_offset(Btype*, size_t index) = 0;
236
237 // Expressions.
238
239 // Return an expression for a zero value of the given type. This is
240 // used for cases such as local variable initialization and
241 // converting nil to other types.
242 virtual Bexpression*
243 zero_expression(Btype*) = 0;
244
245 // Create an error expression. This is used for cases which should
246 // not occur in a correct program, in order to keep the compilation
247 // going without crashing.
248 virtual Bexpression*
249 error_expression() = 0;
250
251 // Create a nil pointer expression.
252 virtual Bexpression*
253 nil_pointer_expression() = 0;
254
255 // Create a reference to a variable.
256 virtual Bexpression*
257 var_expression(Bvariable* var, Location) = 0;
258
259 // Create an expression that indirects through the pointer expression EXPR
260 // (i.e., return the expression for *EXPR). KNOWN_VALID is true if the pointer
261 // is known to point to a valid memory location. BTYPE is the expected type
262 // of the indirected EXPR.
263 virtual Bexpression*
264 indirect_expression(Btype* btype, Bexpression* expr, bool known_valid,
265 Location) = 0;
266
267 // Return an expression that declares a constant named NAME with the
268 // constant value VAL in BTYPE.
269 virtual Bexpression*
270 named_constant_expression(Btype* btype, const std::string& name,
271 Bexpression* val, Location) = 0;
272
273 // Return an expression for the multi-precision integer VAL in BTYPE.
274 virtual Bexpression*
275 integer_constant_expression(Btype* btype, mpz_t val) = 0;
276
277 // Return an expression for the floating point value VAL in BTYPE.
278 virtual Bexpression*
279 float_constant_expression(Btype* btype, mpfr_t val) = 0;
280
281 // Return an expression for the complex value VAL in BTYPE.
282 virtual Bexpression*
283 complex_constant_expression(Btype* btype, mpc_t val) = 0;
284
285 // Return an expression for the string value VAL.
286 virtual Bexpression*
287 string_constant_expression(const std::string& val) = 0;
288
289 // Return an expression for the boolean value VAL.
290 virtual Bexpression*
291 boolean_constant_expression(bool val) = 0;
292
293 // Return an expression for the real part of BCOMPLEX.
294 virtual Bexpression*
295 real_part_expression(Bexpression* bcomplex, Location) = 0;
296
297 // Return an expression for the imaginary part of BCOMPLEX.
298 virtual Bexpression*
299 imag_part_expression(Bexpression* bcomplex, Location) = 0;
300
301 // Return an expression for the complex number (BREAL, BIMAG).
302 virtual Bexpression*
303 complex_expression(Bexpression* breal, Bexpression* bimag, Location) = 0;
304
305 // Return an expression that converts EXPR to TYPE.
306 virtual Bexpression*
307 convert_expression(Btype* type, Bexpression* expr, Location) = 0;
308
309 // Create an expression for the address of a function. This is used to
310 // get the address of the code for a function.
311 virtual Bexpression*
312 function_code_expression(Bfunction*, Location) = 0;
313
314 // Create an expression that takes the address of an expression.
315 virtual Bexpression*
316 address_expression(Bexpression*, Location) = 0;
317
318 // Return an expression for the field at INDEX in BSTRUCT.
319 virtual Bexpression*
320 struct_field_expression(Bexpression* bstruct, size_t index, Location) = 0;
321
322 // Create an expression that executes BSTAT before BEXPR.
323 virtual Bexpression*
324 compound_expression(Bstatement* bstat, Bexpression* bexpr, Location) = 0;
325
326 // Return an expression that executes THEN_EXPR if CONDITION is true, or
327 // ELSE_EXPR otherwise and returns the result as type BTYPE. ELSE_EXPR
328 // may be NULL. BTYPE may be NULL.
329 virtual Bexpression*
330 conditional_expression(Btype* btype, Bexpression* condition,
331 Bexpression* then_expr, Bexpression* else_expr,
332 Location) = 0;
333
334 // Return an expression for the unary operation OP EXPR.
335 // Supported values of OP are (from operators.h):
336 // MINUS, NOT, XOR.
337 virtual Bexpression*
338 unary_expression(Operator op, Bexpression* expr, Location) = 0;
339
340 // Return an expression for the binary operation LEFT OP RIGHT.
341 // Supported values of OP are (from operators.h):
342 // EQEQ, NOTEQ, LT, LE, GT, GE, PLUS, MINUS, OR, XOR, MULT, DIV, MOD,
343 // LSHIFT, RSHIFT, AND, NOT.
344 virtual Bexpression*
345 binary_expression(Operator op, Bexpression* left, Bexpression* right,
346 Location) = 0;
347
348 // Return an expression that constructs BTYPE with VALS. BTYPE must be the
349 // backend representation a of struct. VALS must be in the same order as the
350 // corresponding fields in BTYPE.
351 virtual Bexpression*
352 constructor_expression(Btype* btype, const std::vector<Bexpression*>& vals,
353 Location) = 0;
354
355 // Return an expression that constructs an array of BTYPE with INDEXES and
356 // VALS. INDEXES and VALS must have the same amount of elements. Each index
357 // in INDEXES must be in the same order as the corresponding value in VALS.
358 virtual Bexpression*
359 array_constructor_expression(Btype* btype,
360 const std::vector<unsigned long>& indexes,
361 const std::vector<Bexpression*>& vals,
362 Location) = 0;
363
364 // Return an expression for the address of BASE[INDEX].
365 // BASE has a pointer type. This is used for slice indexing.
366 virtual Bexpression*
367 pointer_offset_expression(Bexpression* base, Bexpression* index,
368 Location) = 0;
369
370 // Return an expression for ARRAY[INDEX] as an l-value. ARRAY is a valid
371 // fixed-length array, not a slice.
372 virtual Bexpression*
373 array_index_expression(Bexpression* array, Bexpression* index, Location) = 0;
374
375 // Create an expression for a call to FN with ARGS.
376 virtual Bexpression*
377 call_expression(Bexpression* fn, const std::vector<Bexpression*>& args,
378 Bexpression* static_chain, Location) = 0;
379
380 // Return an expression that allocates SIZE bytes on the stack.
381 virtual Bexpression*
382 stack_allocation_expression(int64_t size, Location) = 0;
383
384 // Statements.
385
386 // Create an error statement. This is used for cases which should
387 // not occur in a correct program, in order to keep the compilation
388 // going without crashing.
389 virtual Bstatement*
390 error_statement() = 0;
391
392 // Create an expression statement.
393 virtual Bstatement*
394 expression_statement(Bexpression*) = 0;
395
396 // Create a variable initialization statement. This initializes a
397 // local variable at the point in the program flow where it is
398 // declared.
399 virtual Bstatement*
400 init_statement(Bvariable* var, Bexpression* init) = 0;
401
402 // Create an assignment statement.
403 virtual Bstatement*
404 assignment_statement(Bexpression* lhs, Bexpression* rhs,
405 Location) = 0;
406
407 // Create a return statement, passing the representation of the
408 // function and the list of values to return.
409 virtual Bstatement*
410 return_statement(Bfunction*, const std::vector<Bexpression*>&,
411 Location) = 0;
412
413 // Create an if statement. ELSE_BLOCK may be NULL.
414 virtual Bstatement*
415 if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
416 Location) = 0;
417
418 // Create a switch statement where the case values are constants.
419 // CASES and STATEMENTS must have the same number of entries. If
420 // VALUE matches any of the list in CASES[i], which will all be
421 // integers, then STATEMENTS[i] is executed. STATEMENTS[i] will
422 // either end with a goto statement or will fall through into
423 // STATEMENTS[i + 1]. CASES[i] is empty for the default clause,
424 // which need not be last. FUNCTION is the current function.
425 virtual Bstatement*
426 switch_statement(Bfunction* function, Bexpression* value,
427 const std::vector<std::vector<Bexpression*> >& cases,
428 const std::vector<Bstatement*>& statements,
429 Location) = 0;
430
431 // Create a single statement from two statements.
432 virtual Bstatement*
433 compound_statement(Bstatement*, Bstatement*) = 0;
434
435 // Create a single statement from a list of statements.
436 virtual Bstatement*
437 statement_list(const std::vector<Bstatement*>&) = 0;
438
439 // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if
440 // an exception occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and
441 // if not NULL, it will always be executed. This is used for handling defers
442 // in Go functions. In C++, the resulting code is of this form:
443 // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
444 virtual Bstatement*
445 exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
446 Bstatement* finally_stmt, Location) = 0;
447
448 // Blocks.
449
450 // Create a block. The frontend will call this function when it
451 // starts converting a block within a function. FUNCTION is the
452 // current function. ENCLOSING is the enclosing block; it will be
453 // NULL for the top-level block in a function. VARS is the list of
454 // local variables defined within this block; each entry will be
455 // created by the local_variable function. START_LOCATION is the
456 // location of the start of the block, more or less the location of
457 // the initial curly brace. END_LOCATION is the location of the end
458 // of the block, more or less the location of the final curly brace.
459 // The statements will be added after the block is created.
460 virtual Bblock*
461 block(Bfunction* function, Bblock* enclosing,
462 const std::vector<Bvariable*>& vars,
463 Location start_location, Location end_location) = 0;
464
465 // Add the statements to a block. The block is created first. Then
466 // the statements are created. Then the statements are added to the
467 // block. This will called exactly once per block. The vector may
468 // be empty if there are no statements.
469 virtual void
470 block_add_statements(Bblock*, const std::vector<Bstatement*>&) = 0;
471
472 // Return the block as a statement. This is used to include a block
473 // in a list of statements.
474 virtual Bstatement*
475 block_statement(Bblock*) = 0;
476
477 // Variables.
478
479 // Create an error variable. This is used for cases which should
480 // not occur in a correct program, in order to keep the compilation
481 // going without crashing.
482 virtual Bvariable*
483 error_variable() = 0;
484
485 // Create a global variable. PACKAGE_NAME is the name of the
486 // package where the variable is defined. PKGPATH is the package
487 // path for that package, from the -fgo-pkgpath or -fgo-prefix
488 // option. NAME is the name of the variable. BTYPE is the type of
489 // the variable. IS_EXTERNAL is true if the variable is defined in
490 // some other package. IS_HIDDEN is true if the variable is not
491 // exported (name begins with a lower case letter).
492 // IN_UNIQUE_SECTION is true if the variable should be put into a
493 // unique section if possible; this is intended to permit the linker
494 // to garbage collect the variable if it is not referenced.
495 // LOCATION is where the variable was defined.
496 virtual Bvariable*
497 global_variable(const std::string& package_name,
498 const std::string& pkgpath,
499 const std::string& name,
500 Btype* btype,
501 bool is_external,
502 bool is_hidden,
503 bool in_unique_section,
504 Location location) = 0;
505
506 // A global variable will 1) be initialized to zero, or 2) be
507 // initialized to a constant value, or 3) be initialized in the init
508 // function. In case 2, the frontend will call
509 // global_variable_set_init to set the initial value. If this is
510 // not called, the backend should initialize a global variable to 0.
511 // The init function may then assign a value to it.
512 virtual void
513 global_variable_set_init(Bvariable*, Bexpression*) = 0;
514
515 // Create a local variable. The frontend will create the local
516 // variables first, and then create the block which contains them.
517 // FUNCTION is the function in which the variable is defined. NAME
518 // is the name of the variable. TYPE is the type. IS_ADDRESS_TAKEN
519 // is true if the address of this variable is taken (this implies
520 // that the address does not escape the function, as otherwise the
521 // variable would be on the heap). LOCATION is where the variable
522 // is defined. For each local variable the frontend will call
523 // init_statement to set the initial value.
524 virtual Bvariable*
525 local_variable(Bfunction* function, const std::string& name, Btype* type,
526 bool is_address_taken, Location location) = 0;
527
528 // Create a function parameter. This is an incoming parameter, not
529 // a result parameter (result parameters are treated as local
530 // variables). The arguments are as for local_variable.
531 virtual Bvariable*
532 parameter_variable(Bfunction* function, const std::string& name,
533 Btype* type, bool is_address_taken,
534 Location location) = 0;
535
536 // Create a static chain parameter. This is the closure parameter.
537 virtual Bvariable*
538 static_chain_variable(Bfunction* function, const std::string& name,
539 Btype* type, Location location) = 0;
540
541 // Create a temporary variable. A temporary variable has no name,
542 // just a type. We pass in FUNCTION and BLOCK in case they are
543 // needed. If INIT is not NULL, the variable should be initialized
544 // to that value. Otherwise the initial value is irrelevant--the
545 // backend does not have to explicitly initialize it to zero.
546 // ADDRESS_IS_TAKEN is true if the programs needs to take the
547 // address of this temporary variable. LOCATION is the location of
548 // the statement or expression which requires creating the temporary
549 // variable, and may not be very useful. This function should
550 // return a variable which can be referenced later and should set
551 // *PSTATEMENT to a statement which initializes the variable.
552 virtual Bvariable*
553 temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression* init,
554 bool address_is_taken, Location location,
555 Bstatement** pstatement) = 0;
556
557 // Create an implicit variable that is compiler-defined. This is
558 // used when generating GC data and roots, when storing the values
559 // of a slice constructor, and for the zero value of types. This returns a
560 // Bvariable because it corresponds to an initialized variable in C.
561 //
562 // NAME is the name to use for the initialized variable this will create.
563 //
564 // TYPE is the type of the implicit variable.
565 //
566 // IS_HIDDEN will be true if the descriptor should only be visible
567 // within the current object.
568 //
569 // IS_CONSTANT is true if the implicit variable should be treated like it is
570 // immutable. For slice initializers, if the values must be copied to the
571 // heap, the variable IS_CONSTANT.
572 //
573 // IS_COMMON is true if the implicit variable should
574 // be treated as a common variable (multiple definitions with
575 // different sizes permitted in different object files, all merged
576 // into the largest definition at link time); this will be true for
577 // the zero value. IS_HIDDEN and IS_COMMON will never both be true.
578 //
579 // If ALIGNMENT is not zero, it is the desired alignment of the variable.
580 virtual Bvariable*
581 implicit_variable(const std::string& name, Btype* type, bool is_hidden,
582 bool is_constant, bool is_common, int64_t alignment) = 0;
583
584
585 // Set the initial value of a variable created by implicit_variable.
586 // This must be called even if there is no initializer, i.e., INIT is NULL.
587 // The NAME, TYPE, IS_HIDDEN, IS_CONSTANT, and IS_COMMON parameters are
588 // the same ones passed to implicit_variable. INIT will be a composite
589 // literal of type TYPE. It will not contain any function calls or anything
590 // else that can not be put into a read-only data section.
591 // It may contain the address of variables created by implicit_variable.
592 //
593 // If IS_COMMON is true, INIT will be NULL, and the
594 // variable should be initialized to all zeros.
595 virtual void
596 implicit_variable_set_init(Bvariable*, const std::string& name, Btype* type,
597 bool is_hidden, bool is_constant, bool is_common,
598 Bexpression* init) = 0;
599
600 // Create a reference to a named implicit variable defined in some other
601 // package. This will be a variable created by a call to implicit_variable
602 // with the same NAME and TYPE and with IS_COMMON passed as false. This
603 // corresponds to an extern global variable in C.
604 virtual Bvariable*
605 implicit_variable_reference(const std::string& name, Btype* type) = 0;
606
607 // Create a named immutable initialized data structure. This is
608 // used for type descriptors, map descriptors, and function
609 // descriptors. This returns a Bvariable because it corresponds to
610 // an initialized const variable in C.
611 //
612 // NAME is the name to use for the initialized global variable which
613 // this call will create.
614 //
615 // IS_HIDDEN will be true if the descriptor should only be visible
616 // within the current object.
617 //
618 // IS_COMMON is true if NAME may be defined by several packages, and
619 // the linker should merge all such definitions. If IS_COMMON is
620 // false, NAME should be defined in only one file. In general
621 // IS_COMMON will be true for the type descriptor of an unnamed type
622 // or a builtin type. IS_HIDDEN and IS_COMMON will never both be
623 // true.
624 //
625 // TYPE will be a struct type; the type of the returned expression
626 // must be a pointer to this struct type.
627 //
628 // We must create the named structure before we know its
629 // initializer, because the initializer may refer to its own
630 // address. After calling this the frontend will call
631 // immutable_struct_set_init.
632 virtual Bvariable*
633 immutable_struct(const std::string& name, bool is_hidden, bool is_common,
634 Btype* type, Location) = 0;
635
636 // Set the initial value of a variable created by immutable_struct.
637 // The NAME, IS_HIDDEN, IS_COMMON, TYPE, and location parameters are
638 // the same ones passed to immutable_struct. INITIALIZER will be a
639 // composite literal of type TYPE. It will not contain any function
640 // calls or anything else that can not be put into a read-only data
641 // section. It may contain the address of variables created by
642 // immutable_struct.
643 virtual void
644 immutable_struct_set_init(Bvariable*, const std::string& name,
645 bool is_hidden, bool is_common, Btype* type,
646 Location, Bexpression* initializer) = 0;
647
648 // Create a reference to a named immutable initialized data
649 // structure defined in some other package. This will be a
650 // structure created by a call to immutable_struct with the same
651 // NAME and TYPE and with IS_COMMON passed as false. This
652 // corresponds to an extern const global variable in C.
653 virtual Bvariable*
654 immutable_struct_reference(const std::string& name, Btype* type,
655 Location) = 0;
656
657 // Labels.
658
659 // Create a new label. NAME will be empty if this is a label
660 // created by the frontend for a loop construct. The location is
661 // where the label is defined.
662 virtual Blabel*
663 label(Bfunction*, const std::string& name, Location) = 0;
664
665 // Create a statement which defines a label. This statement will be
666 // put into the codestream at the point where the label should be
667 // defined.
668 virtual Bstatement*
669 label_definition_statement(Blabel*) = 0;
670
671 // Create a goto statement to a label.
672 virtual Bstatement*
673 goto_statement(Blabel*, Location) = 0;
674
675 // Create an expression for the address of a label. This is used to
676 // get the return address of a deferred function which may call
677 // recover.
678 virtual Bexpression*
679 label_address(Blabel*, Location) = 0;
680
681 // Functions.
682
683 // Create an error function. This is used for cases which should
684 // not occur in a correct program, in order to keep the compilation
685 // going without crashing.
686 virtual Bfunction*
687 error_function() = 0;
688
689 // Declare or define a function of FNTYPE.
690 // NAME is the Go name of the function. ASM_NAME, if not the empty string, is
691 // the name that should be used in the symbol table; this will be non-empty if
692 // a magic extern comment is used.
693 // IS_VISIBLE is true if this function should be visible outside of the
694 // current compilation unit. IS_DECLARATION is true if this is a function
695 // declaration rather than a definition; the function definition will be in
696 // another compilation unit.
697 // IS_INLINABLE is true if the function can be inlined.
698 // DISABLE_SPLIT_STACK is true if this function may not split the stack; this
699 // is used for the implementation of recover.
700 // IN_UNIQUE_SECTION is true if this function should be put into a unique
701 // location if possible; this is used for field tracking.
702 virtual Bfunction*
703 function(Btype* fntype, const std::string& name, const std::string& asm_name,
704 bool is_visible, bool is_declaration, bool is_inlinable,
705 bool disable_split_stack, bool in_unique_section, Location) = 0;
706
707 // Create a statement that runs all deferred calls for FUNCTION. This should
708 // be a statement that looks like this in C++:
709 // finish:
710 // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
711 virtual Bstatement*
712 function_defer_statement(Bfunction* function, Bexpression* undefer,
713 Bexpression* check_defer, Location) = 0;
714
715 // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
716 // This will only be called for a function definition. Returns true on
717 // success, false on failure.
718 virtual bool
719 function_set_parameters(Bfunction* function,
720 const std::vector<Bvariable*>& param_vars) = 0;
721
722 // Set the function body for FUNCTION using the code in CODE_STMT. Returns
723 // true on success, false on failure.
724 virtual bool
725 function_set_body(Bfunction* function, Bstatement* code_stmt) = 0;
726
727 // Look up a named built-in function in the current backend implementation.
728 // Returns NULL if no built-in function by that name exists.
729 virtual Bfunction*
730 lookup_builtin(const std::string&) = 0;
731
732 // Utility.
733
734 // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
735 // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
736 virtual void
737 write_global_definitions(const std::vector<Btype*>& type_decls,
738 const std::vector<Bexpression*>& constant_decls,
739 const std::vector<Bfunction*>& function_decls,
740 const std::vector<Bvariable*>& variable_decls) = 0;
741 };
742
743 #endif // !defined(GO_BACKEND_H)