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.
14 // Pointers to these types are created by the backend, passed to the
15 // frontend, and passed back to the backend. The types must be
16 // defined by the backend using these names.
18 // The backend representation of a type.
21 // The backend represention of an expression.
24 // The backend representation of a statement.
27 // The backend representation of a function definition.
30 // The backend representation of a label.
33 // A list of backend types.
34 typedef std::vector<Btype*> Btypes;
36 // The backend interface. This is a pure abstract class that a
37 // specific backend will implement.
42 virtual ~Backend() { }
46 // Produce an error type. Actually the backend could probably just
47 // crash if this is called.
51 // Get a void type. This is used in (at least) two ways: 1) as the
52 // return type of a function with no result parameters; 2)
53 // unsafe.Pointer is represented as *void.
57 // Get the unnamed boolean type.
61 // Get an unnamed integer type with the given signedness and number
64 integer_type(bool is_unsigned, int bits) = 0;
66 // Get an unnamed floating point type with the given number of bits.
68 float_type(int bits) = 0;
70 // Get the unnamed string type.
74 // Get a function type. The receiver, parameter, and results are
75 // generated from the types in the Function_type. The Function_type
76 // is provided so that the names are available.
78 function_type(const Function_type*, Btype* receiver,
79 const Btypes* parameters,
80 const Btypes* results) = 0;
82 // Get a struct type. The Struct_type is provided to get the field
85 struct_type(const Struct_type*, const Btypes* field_types) = 0;
89 array_type(const Btype* element_type, const Bexpression* length) = 0;
93 slice_type(const Btype* element_type) = 0;
97 map_type(const Btype* key_type, const Btype* value_type, source_location) = 0;
99 // Get a channel type.
101 channel_type(const Btype* element_type) = 0;
103 // Get an interface type. The Interface_type is provided to get the
106 interface_type(const Interface_type*, const Btypes* method_types) = 0;
110 // Create an expression statement.
112 expression_statement(Bexpression*) = 0;
114 // Create an assignment statement.
116 assignment_statement(Bexpression* lhs, Bexpression* rhs,
117 source_location) = 0;
119 // Create a return statement, passing the representation of the
120 // function and the list of values to return.
122 return_statement(Bfunction*, const std::vector<Bexpression*>&,
123 source_location) = 0;
125 // Create an if statement. ELSE_BLOCK may be NULL.
127 if_statement(Bexpression* condition, Bstatement* then_block,
128 Bstatement* else_block, source_location) = 0;
130 // Create a switch statement where the case values are constants.
131 // CASES and STATEMENTS must have the same number of entries. If
132 // VALUE matches any of the list in CASES[i], which will all be
133 // integers, then STATEMENTS[i] is executed. STATEMENTS[i] will
134 // either end with a goto statement or will fall through into
135 // STATEMENTS[i + 1]. CASES[i] is empty for the default clause,
136 // which need not be last.
138 switch_statement(Bexpression* value,
139 const std::vector<std::vector<Bexpression*> >& cases,
140 const std::vector<Bstatement*>& statements,
141 source_location) = 0;
143 // Create a single statement from a list of statements.
145 statement_list(const std::vector<Bstatement*>&) = 0;
149 // Create a new label. NAME will be empty if this is a label
150 // created by the frontend for a loop construct. The location is
151 // where the the label is defined.
153 label(Bfunction*, const std::string& name, source_location) = 0;
155 // Create a statement which defines a label. This statement will be
156 // put into the codestream at the point where the label should be
159 label_definition_statement(Blabel*) = 0;
161 // Create a goto statement to a label.
163 goto_statement(Blabel*, source_location) = 0;
165 // Create an expression for the address of a label. This is used to
166 // get the return address of a deferred function which may call
169 label_address(Blabel*, source_location) = 0;
172 // The backend interface has to define this function.
174 extern Backend* go_get_backend();
176 // FIXME: Temporary helper functions while converting to new backend
179 extern Bexpression* tree_to_expr(tree);
180 extern Bstatement* tree_to_stat(tree);
181 extern Bfunction* tree_to_function(tree);
182 extern tree expr_to_tree(Bexpression*);
183 extern tree stat_to_tree(Bstatement*);
185 #endif // !defined(GO_BACKEND_H)