OSDN Git Service

01f3cfa4c2bec586b81a3f58dec9484aabc2c9b7
[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 class Function_type;
11 class Struct_type;
12 class Interface_type;
13
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.
17
18 // The backend representation of a type.
19 class Btype;
20
21 // The backend represention of an expression.
22 class Bexpression;
23
24 // The backend representation of a statement.
25 class Bstatement;
26
27 // The backend representation of a function definition.
28 class Bfunction;
29
30 // The backend representation of a label.
31 class Blabel;
32
33 // A list of backend types.
34 typedef std::vector<Btype*> Btypes;
35
36 // The backend interface.  This is a pure abstract class that a
37 // specific backend will implement.
38
39 class Backend
40 {
41  public:
42   virtual ~Backend() { }
43
44   // Types.
45
46   // Produce an error type.  Actually the backend could probably just
47   // crash if this is called.
48   virtual Btype*
49   error_type() = 0;
50
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.
54   virtual Btype*
55   void_type() = 0;
56
57   // Get the unnamed boolean type.
58   virtual Btype*
59   bool_type() = 0;
60
61   // Get an unnamed integer type with the given signedness and number
62   // of bits.
63   virtual Btype*
64   integer_type(bool is_unsigned, int bits) = 0;
65
66   // Get an unnamed floating point type with the given number of bits.
67   virtual Btype*
68   float_type(int bits) = 0;
69
70   // Get the unnamed string type.
71   virtual Btype*
72   string_type() = 0;
73
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.
77   virtual Btype*
78   function_type(const Function_type*, Btype* receiver,
79                 const Btypes* parameters,
80                 const Btypes* results) = 0;
81
82   // Get a struct type.  The Struct_type is provided to get the field
83   // names.
84   virtual Btype*
85   struct_type(const Struct_type*, const Btypes* field_types) = 0;
86
87   // Get an array type.
88   virtual Btype*
89   array_type(const Btype* element_type, const Bexpression* length) = 0;
90
91   // Get a slice type.
92   virtual Btype*
93   slice_type(const Btype* element_type) = 0;
94
95   // Get a map type.
96   virtual Btype*
97   map_type(const Btype* key_type, const Btype* value_type, source_location) = 0;
98
99   // Get a channel type.
100   virtual Btype*
101   channel_type(const Btype* element_type) = 0;
102
103   // Get an interface type.  The Interface_type is provided to get the
104   // method names.
105   virtual Btype*
106   interface_type(const Interface_type*, const Btypes* method_types) = 0;
107
108   // Statements.
109
110   // Create an expression statement.
111   virtual Bstatement*
112   expression_statement(Bexpression*) = 0;
113
114   // Create an assignment statement.
115   virtual Bstatement*
116   assignment_statement(Bexpression* lhs, Bexpression* rhs,
117                        source_location) = 0;
118
119   // Create a return statement, passing the representation of the
120   // function and the list of values to return.
121   virtual Bstatement*
122   return_statement(Bfunction*, const std::vector<Bexpression*>&,
123                    source_location) = 0;
124
125   // Create an if statement.  ELSE_BLOCK may be NULL.
126   virtual Bstatement*
127   if_statement(Bexpression* condition, Bstatement* then_block,
128                Bstatement* else_block, source_location) = 0;
129
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.
137   virtual Bstatement*
138   switch_statement(Bexpression* value,
139                    const std::vector<std::vector<Bexpression*> >& cases,
140                    const std::vector<Bstatement*>& statements,
141                    source_location) = 0;
142
143   // Create a single statement from a list of statements.
144   virtual Bstatement*
145   statement_list(const std::vector<Bstatement*>&) = 0;
146
147   // Labels.
148   
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.
152   virtual Blabel*
153   label(Bfunction*, const std::string& name, source_location) = 0;
154
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
157   // defined.
158   virtual Bstatement*
159   label_definition_statement(Blabel*) = 0;
160
161   // Create a goto statement to a label.
162   virtual Bstatement*
163   goto_statement(Blabel*, source_location) = 0;
164
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
167   // recover.
168   virtual Bexpression*
169   label_address(Blabel*, source_location) = 0;
170 };
171
172 // The backend interface has to define this function.
173
174 extern Backend* go_get_backend();
175
176 // FIXME: Temporary helper functions while converting to new backend
177 // interface.
178
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*);
184
185 #endif // !defined(GO_BACKEND_H)