annotate gcc/go/gofrontend/export.h @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 // export.h -- Export declarations in Go frontend. -*- C++ -*-
kono
parents:
diff changeset
2
kono
parents:
diff changeset
3 // Copyright 2009 The Go Authors. All rights reserved.
kono
parents:
diff changeset
4 // Use of this source code is governed by a BSD-style
kono
parents:
diff changeset
5 // license that can be found in the LICENSE file.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 #ifndef GO_EXPORT_H
kono
parents:
diff changeset
8 #define GO_EXPORT_H
kono
parents:
diff changeset
9
kono
parents:
diff changeset
10 #include "string-dump.h"
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 class Go_sha1_helper;
kono
parents:
diff changeset
13 class Gogo;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
14 class Named_object;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
15 class Export_function_body;
111
kono
parents:
diff changeset
16 class Import_init;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
17 class Named_object;
111
kono
parents:
diff changeset
18 class Bindings;
kono
parents:
diff changeset
19 class Type;
kono
parents:
diff changeset
20 class Package;
kono
parents:
diff changeset
21 class Import_init_set;
kono
parents:
diff changeset
22 class Backend;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
23 class Temporary_statement;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
24 class Unnamed_label;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
25 struct Export_impl;
111
kono
parents:
diff changeset
26
kono
parents:
diff changeset
27 // Codes used for the builtin types. These are all negative to make
kono
parents:
diff changeset
28 // them easily distinct from the codes assigned by Export::write_type.
kono
parents:
diff changeset
29 // Note that these codes may not be changed! Changing them would
kono
parents:
diff changeset
30 // break existing export data.
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 enum Builtin_code
kono
parents:
diff changeset
33 {
kono
parents:
diff changeset
34 BUILTIN_INT8 = -1,
kono
parents:
diff changeset
35 BUILTIN_INT16 = -2,
kono
parents:
diff changeset
36 BUILTIN_INT32 = -3,
kono
parents:
diff changeset
37 BUILTIN_INT64 = -4,
kono
parents:
diff changeset
38 BUILTIN_UINT8 = -5,
kono
parents:
diff changeset
39 BUILTIN_UINT16 = -6,
kono
parents:
diff changeset
40 BUILTIN_UINT32 = -7,
kono
parents:
diff changeset
41 BUILTIN_UINT64 = -8,
kono
parents:
diff changeset
42 BUILTIN_FLOAT32 = -9,
kono
parents:
diff changeset
43 BUILTIN_FLOAT64 = -10,
kono
parents:
diff changeset
44 BUILTIN_INT = -11,
kono
parents:
diff changeset
45 BUILTIN_UINT = -12,
kono
parents:
diff changeset
46 BUILTIN_UINTPTR = -13,
kono
parents:
diff changeset
47 BUILTIN_BOOL = -15,
kono
parents:
diff changeset
48 BUILTIN_STRING = -16,
kono
parents:
diff changeset
49 BUILTIN_COMPLEX64 = -17,
kono
parents:
diff changeset
50 BUILTIN_COMPLEX128 = -18,
kono
parents:
diff changeset
51 BUILTIN_ERROR = -19,
kono
parents:
diff changeset
52 BUILTIN_BYTE = -20,
kono
parents:
diff changeset
53 BUILTIN_RUNE = -21,
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 SMALLEST_BUILTIN_CODE = -21
kono
parents:
diff changeset
56 };
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 // Export data version number. New export data is written with the
kono
parents:
diff changeset
59 // "current" version, but there is support for reading files with
kono
parents:
diff changeset
60 // older version export data (at least for now).
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 enum Export_data_version {
kono
parents:
diff changeset
63 EXPORT_FORMAT_UNKNOWN = 0,
kono
parents:
diff changeset
64 EXPORT_FORMAT_V1 = 1,
kono
parents:
diff changeset
65 EXPORT_FORMAT_V2 = 2,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
66 EXPORT_FORMAT_V3 = 3,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
67 EXPORT_FORMAT_CURRENT = EXPORT_FORMAT_V3
111
kono
parents:
diff changeset
68 };
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 // This class manages exporting Go declarations. It handles the main
kono
parents:
diff changeset
71 // loop of exporting. A pointer to this class is also passed to the
kono
parents:
diff changeset
72 // various specific export implementations.
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 class Export : public String_dump
kono
parents:
diff changeset
75 {
kono
parents:
diff changeset
76 public:
kono
parents:
diff changeset
77 // The Stream class is an interface used to output the exported
kono
parents:
diff changeset
78 // information. The caller should instantiate a child of this
kono
parents:
diff changeset
79 // class.
kono
parents:
diff changeset
80 class Stream
kono
parents:
diff changeset
81 {
kono
parents:
diff changeset
82 public:
kono
parents:
diff changeset
83 Stream();
kono
parents:
diff changeset
84 virtual ~Stream();
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 // Write a string. Implements the String_dump interface.
kono
parents:
diff changeset
87 void
kono
parents:
diff changeset
88 write_string(const std::string& s)
kono
parents:
diff changeset
89 { this->write_and_sum_bytes(s.data(), s.length()); }
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 // Write a nul terminated string. Implements the String_dump interface.
kono
parents:
diff changeset
92 void
kono
parents:
diff changeset
93 write_c_string(const char* s)
kono
parents:
diff changeset
94 { this->write_and_sum_bytes(s, strlen(s)); }
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 // Write some bytes.
kono
parents:
diff changeset
97 void
kono
parents:
diff changeset
98 write_bytes(const char* bytes, size_t length)
kono
parents:
diff changeset
99 { this->write_and_sum_bytes(bytes, length); }
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 // Return the raw bytes of the checksum data.
kono
parents:
diff changeset
102 std::string
kono
parents:
diff changeset
103 checksum();
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 // Write a checksum string to the stream. This will be called at
kono
parents:
diff changeset
106 // the end of the other output.
kono
parents:
diff changeset
107 void
kono
parents:
diff changeset
108 write_checksum(const std::string&);
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 protected:
kono
parents:
diff changeset
111 // This function is called with data to export. This data must be
kono
parents:
diff changeset
112 // made available as a contiguous stream for the importer.
kono
parents:
diff changeset
113 virtual void
kono
parents:
diff changeset
114 do_write(const char* bytes, size_t length) = 0;
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 private:
kono
parents:
diff changeset
117 void
kono
parents:
diff changeset
118 write_and_sum_bytes(const char*, size_t);
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 // The checksum helper.
kono
parents:
diff changeset
121 Go_sha1_helper* sha1_helper_;
kono
parents:
diff changeset
122 };
kono
parents:
diff changeset
123
kono
parents:
diff changeset
124 Export(Stream*);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
125 ~Export();
111
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 // Size of export data magic string (which includes version number).
kono
parents:
diff changeset
128 static const int magic_len = 4;
kono
parents:
diff changeset
129
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
130 // Magic strings (current version and older versions).
111
kono
parents:
diff changeset
131 static const char cur_magic[magic_len];
kono
parents:
diff changeset
132 static const char v1_magic[magic_len];
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
133 static const char v2_magic[magic_len];
111
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 // The length of the checksum string.
kono
parents:
diff changeset
136 static const int checksum_len = 20;
kono
parents:
diff changeset
137
kono
parents:
diff changeset
138 // Register the builtin types.
kono
parents:
diff changeset
139 void
kono
parents:
diff changeset
140 register_builtin_types(Gogo*);
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 // Export the identifiers in BINDINGS which are marked for export.
kono
parents:
diff changeset
143 // The exporting is done via a series of calls to THIS->STREAM_. If
kono
parents:
diff changeset
144 // is nothing to export, this->stream_->write will not be called.
kono
parents:
diff changeset
145 // PREFIX is the package prefix. PKGPATH is the package path.
kono
parents:
diff changeset
146 // Only one of PREFIX and PKGPATH will be non-empty.
kono
parents:
diff changeset
147 // PACKAGES is all the packages we have seen.
kono
parents:
diff changeset
148 // IMPORTS is the explicitly imported packages.
kono
parents:
diff changeset
149 // IMPORT_INIT_FN is the name of the import initialization function
kono
parents:
diff changeset
150 // for this package; it will be empty if none is needed.
kono
parents:
diff changeset
151 // IMPORTED_INIT_FNS is the list of initialization functions for
kono
parents:
diff changeset
152 // imported packages.
kono
parents:
diff changeset
153 void
kono
parents:
diff changeset
154 export_globals(const std::string& package_name,
kono
parents:
diff changeset
155 const std::string& prefix,
kono
parents:
diff changeset
156 const std::string& pkgpath,
kono
parents:
diff changeset
157 const std::map<std::string, Package*>& packages,
kono
parents:
diff changeset
158 const std::map<std::string, Package*>& imports,
kono
parents:
diff changeset
159 const std::string& import_init_fn,
kono
parents:
diff changeset
160 const Import_init_set& imported_init_fns,
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
161 const Bindings* bindings,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
162 Unordered_set(Named_object*)* marked_inline_functions);
111
kono
parents:
diff changeset
163
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
164 // Record a type that is mentioned in export data. Return value is
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
165 // TRUE for newly visited types, FALSE for types that have been seen
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
166 // previously.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
167 bool
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
168 record_type(Type*);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
169
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
170 // Assign type indices to types mentioned in export data.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
171 int
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
172 assign_type_indices(const std::vector<Named_object*>& sorted_exports);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
173
111
kono
parents:
diff changeset
174 // Write a string to the export stream.
kono
parents:
diff changeset
175 void
kono
parents:
diff changeset
176 write_string(const std::string& s)
kono
parents:
diff changeset
177 { this->stream_->write_string(s); }
kono
parents:
diff changeset
178
kono
parents:
diff changeset
179 // Write a nul terminated string to the export stream.
kono
parents:
diff changeset
180 void
kono
parents:
diff changeset
181 write_c_string(const char* s)
kono
parents:
diff changeset
182 { this->stream_->write_c_string(s); }
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 // Write some bytes to the export stream.
kono
parents:
diff changeset
185 void
kono
parents:
diff changeset
186 write_bytes(const char* bytes, size_t length)
kono
parents:
diff changeset
187 { this->stream_->write_bytes(bytes, length); }
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 // Write a name to the export stream. If NAME is empty, write "?".
kono
parents:
diff changeset
190 void
kono
parents:
diff changeset
191 write_name(const std::string& name);
kono
parents:
diff changeset
192
kono
parents:
diff changeset
193 // Write out a type. This handles references back to previous
kono
parents:
diff changeset
194 // definitions.
kono
parents:
diff changeset
195 void
kono
parents:
diff changeset
196 write_type(const Type*);
kono
parents:
diff changeset
197
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
198 // Write a type to an exported function body.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
199 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
200 write_type_to(const Type*, Export_function_body*);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
201
111
kono
parents:
diff changeset
202 // Write the escape note to the export stream. If NOTE is NULL, write
kono
parents:
diff changeset
203 // nothing.
kono
parents:
diff changeset
204 void
kono
parents:
diff changeset
205 write_escape(std::string* note);
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 // Write an integer value.
kono
parents:
diff changeset
208 void
kono
parents:
diff changeset
209 write_int(int);
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 // Write an unsigned value.
kono
parents:
diff changeset
212 void
kono
parents:
diff changeset
213 write_unsigned(unsigned);
kono
parents:
diff changeset
214
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
215 // Return the index of a package.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
216 int
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
217 package_index(const Package* p) const;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
218
111
kono
parents:
diff changeset
219 private:
kono
parents:
diff changeset
220 Export(const Export&);
kono
parents:
diff changeset
221 Export& operator=(const Export&);
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 // Write out all known packages.
kono
parents:
diff changeset
224 void
kono
parents:
diff changeset
225 write_packages(const std::map<std::string, Package*>& packages);
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 typedef std::map<unsigned, std::set<unsigned> > Init_graph;
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 static void
kono
parents:
diff changeset
230 add_init_graph_edge(Init_graph* init_graph, unsigned src, unsigned sink);
kono
parents:
diff changeset
231
kono
parents:
diff changeset
232 static void
kono
parents:
diff changeset
233 populate_init_graph(Init_graph* init_graph,
kono
parents:
diff changeset
234 const Import_init_set& imported_init_fns,
kono
parents:
diff changeset
235 const std::map<std::string, unsigned>& init_idx);
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 // Write out the imported packages.
kono
parents:
diff changeset
238 void
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
239 write_imports(const std::map<std::string, Package*>& imports,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
240 const Unordered_set(const Package*)& type_imports);
111
kono
parents:
diff changeset
241
kono
parents:
diff changeset
242 // Write out the imported initialization functions and init graph.
kono
parents:
diff changeset
243 void
kono
parents:
diff changeset
244 write_imported_init_fns(const std::string& package_name,
kono
parents:
diff changeset
245 const std::string&, const Import_init_set&);
kono
parents:
diff changeset
246
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
247 // Write out all types.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
248 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
249 write_types(int unexported_type_index);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
250
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
251 // Write out one type definition.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
252 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
253 write_type_definition(const Type* type, int index);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
254
111
kono
parents:
diff changeset
255 // Register one builtin type.
kono
parents:
diff changeset
256 void
kono
parents:
diff changeset
257 register_builtin_type(Gogo*, const char* name, Builtin_code);
kono
parents:
diff changeset
258
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
259 // Return the index of a type in the export data.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
260 int
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
261 type_index(const Type*);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
262
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
263 // Set the index of a type.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
264 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
265 set_type_index(const Type*);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
266
111
kono
parents:
diff changeset
267 // The stream to which we are writing data.
kono
parents:
diff changeset
268 Stream* stream_;
kono
parents:
diff changeset
269 // Index number of next type.
kono
parents:
diff changeset
270 int type_index_;
kono
parents:
diff changeset
271 // Packages we have written out.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
272 Unordered_map(const Package*, int) packages_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
273 // Hidden implementation-specific state.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
274 Export_impl* impl_;
111
kono
parents:
diff changeset
275 };
kono
parents:
diff changeset
276
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
277 // An export streamer that puts the export stream in a named section.
111
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 class Stream_to_section : public Export::Stream
kono
parents:
diff changeset
280 {
kono
parents:
diff changeset
281 public:
kono
parents:
diff changeset
282 Stream_to_section(Backend*);
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 protected:
kono
parents:
diff changeset
285 void
kono
parents:
diff changeset
286 do_write(const char*, size_t);
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 private:
kono
parents:
diff changeset
289 Backend* backend_;
kono
parents:
diff changeset
290 };
kono
parents:
diff changeset
291
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
292 // An export streamer that puts the export stream in a string.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
293
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
294 class Stream_to_string : public Export::Stream
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
295 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
296 public:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
297 Stream_to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
298 : string_()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
299 {}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
300
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
301 const std::string&
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
302 string() const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
303 { return this->string_; }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
304
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
305 protected:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
306 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
307 do_write(const char* s, size_t len)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
308 { this->string_.append(s, len); }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
309
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
310 private:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
311 std::string string_;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
312 };
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
313
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
314 // Class to manage exporting a function body. This is passed around
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
315 // to Statements and Expressions. It builds up the export data for
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
316 // the function.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
317
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
318 class Export_function_body : public String_dump
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
319 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
320 public:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
321 Export_function_body(Export* exp, int indent)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
322 : exp_(exp), body_(), type_context_(NULL), next_temporary_index_(0),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
323 temporary_indexes_(), next_label_index_(0), label_indexes_(),
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
324 indent_(indent)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
325 { }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
326
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
327 // Write a character to the body.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
328 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
329 write_char(char c)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
330 { this->body_.append(1, c); }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
331
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
332 // Write a NUL terminated string to the body.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
333 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
334 write_c_string(const char* str)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
335 { this->body_.append(str); }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
336
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
337 // Write a string to the body.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
338 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
339 write_string(const std::string& str)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
340 { this->body_.append(str); }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
341
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
342 // Write a type reference to the body.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
343 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
344 write_type(const Type* type)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
345 { this->exp_->write_type_to(type, this); }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
346
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
347 // Return the current type context.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
348 Type*
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
349 type_context() const
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
350 { return this->type_context_; }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
351
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
352 // Set the current type context.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
353 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
354 set_type_context(Type* type)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
355 { this->type_context_ = type; }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
356
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
357 // Append as many spaces as the current indentation level.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
358 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
359 indent()
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
360 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
361 for (int i = this->indent_; i > 0; i--)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
362 this->write_char(' ');
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
363 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
364
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
365 // Increment the indentation level.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
366 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
367 increment_indent()
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
368 { ++this->indent_; }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
369
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
370 // Decrement the indentation level.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
371 void
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
372 decrement_indent()
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
373 { --this->indent_; }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
374
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
375 // Return the index of a package.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
376 int
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
377 package_index(const Package* p) const
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
378 { return this->exp_->package_index(p); }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
379
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
380 // Record a temporary statement and return its index.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
381 unsigned int
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
382 record_temporary(const Temporary_statement*);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
383
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
384 // Return the index of a temporary statement.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
385 unsigned int
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
386 temporary_index(const Temporary_statement*);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
387
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
388 // Return the index of an unnamed label. If it doesn't already have
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
389 // an index, give it one.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
390 unsigned int
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
391 unnamed_label_index(const Unnamed_label*);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
392
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
393 // Return a reference to the completed body.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
394 const std::string&
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
395 body() const
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
396 { return this->body_; }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
397
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
398 private:
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
399 // The overall export data.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
400 Export* exp_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
401 // The body we are building.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
402 std::string body_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
403 // Current type context. Used to avoid duplicate type conversions.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
404 Type* type_context_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
405 // Index to give to next temporary statement.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
406 unsigned int next_temporary_index_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
407 // Map temporary statements to indexes.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
408 Unordered_map(const Temporary_statement*, unsigned int) temporary_indexes_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
409 // Index to give to the next unnamed label.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
410 unsigned int next_label_index_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
411 // Map unnamed labels to indexes.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
412 Unordered_map(const Unnamed_label*, unsigned int) label_indexes_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
413 // Current indentation level: the number of spaces before each statement.
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
414 int indent_;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
415 };
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
416
111
kono
parents:
diff changeset
417 #endif // !defined(GO_EXPORT_H)