annotate gcc/go/gofrontend/export.h @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
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;
111
kono
parents:
diff changeset
15 class Import_init;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
16 class Named_object;
111
kono
parents:
diff changeset
17 class Bindings;
kono
parents:
diff changeset
18 class Type;
kono
parents:
diff changeset
19 class Package;
kono
parents:
diff changeset
20 class Import_init_set;
kono
parents:
diff changeset
21 class Backend;
kono
parents:
diff changeset
22
kono
parents:
diff changeset
23 // Codes used for the builtin types. These are all negative to make
kono
parents:
diff changeset
24 // them easily distinct from the codes assigned by Export::write_type.
kono
parents:
diff changeset
25 // Note that these codes may not be changed! Changing them would
kono
parents:
diff changeset
26 // break existing export data.
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 enum Builtin_code
kono
parents:
diff changeset
29 {
kono
parents:
diff changeset
30 BUILTIN_INT8 = -1,
kono
parents:
diff changeset
31 BUILTIN_INT16 = -2,
kono
parents:
diff changeset
32 BUILTIN_INT32 = -3,
kono
parents:
diff changeset
33 BUILTIN_INT64 = -4,
kono
parents:
diff changeset
34 BUILTIN_UINT8 = -5,
kono
parents:
diff changeset
35 BUILTIN_UINT16 = -6,
kono
parents:
diff changeset
36 BUILTIN_UINT32 = -7,
kono
parents:
diff changeset
37 BUILTIN_UINT64 = -8,
kono
parents:
diff changeset
38 BUILTIN_FLOAT32 = -9,
kono
parents:
diff changeset
39 BUILTIN_FLOAT64 = -10,
kono
parents:
diff changeset
40 BUILTIN_INT = -11,
kono
parents:
diff changeset
41 BUILTIN_UINT = -12,
kono
parents:
diff changeset
42 BUILTIN_UINTPTR = -13,
kono
parents:
diff changeset
43 BUILTIN_BOOL = -15,
kono
parents:
diff changeset
44 BUILTIN_STRING = -16,
kono
parents:
diff changeset
45 BUILTIN_COMPLEX64 = -17,
kono
parents:
diff changeset
46 BUILTIN_COMPLEX128 = -18,
kono
parents:
diff changeset
47 BUILTIN_ERROR = -19,
kono
parents:
diff changeset
48 BUILTIN_BYTE = -20,
kono
parents:
diff changeset
49 BUILTIN_RUNE = -21,
kono
parents:
diff changeset
50
kono
parents:
diff changeset
51 SMALLEST_BUILTIN_CODE = -21
kono
parents:
diff changeset
52 };
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 // Export data version number. New export data is written with the
kono
parents:
diff changeset
55 // "current" version, but there is support for reading files with
kono
parents:
diff changeset
56 // older version export data (at least for now).
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 enum Export_data_version {
kono
parents:
diff changeset
59 EXPORT_FORMAT_UNKNOWN = 0,
kono
parents:
diff changeset
60 EXPORT_FORMAT_V1 = 1,
kono
parents:
diff changeset
61 EXPORT_FORMAT_V2 = 2,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
62 EXPORT_FORMAT_V3 = 3,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
63 EXPORT_FORMAT_CURRENT = EXPORT_FORMAT_V3
111
kono
parents:
diff changeset
64 };
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 // This class manages exporting Go declarations. It handles the main
kono
parents:
diff changeset
67 // loop of exporting. A pointer to this class is also passed to the
kono
parents:
diff changeset
68 // various specific export implementations.
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 class Export : public String_dump
kono
parents:
diff changeset
71 {
kono
parents:
diff changeset
72 public:
kono
parents:
diff changeset
73 // The Stream class is an interface used to output the exported
kono
parents:
diff changeset
74 // information. The caller should instantiate a child of this
kono
parents:
diff changeset
75 // class.
kono
parents:
diff changeset
76 class Stream
kono
parents:
diff changeset
77 {
kono
parents:
diff changeset
78 public:
kono
parents:
diff changeset
79 Stream();
kono
parents:
diff changeset
80 virtual ~Stream();
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 // Write a string. Implements the String_dump interface.
kono
parents:
diff changeset
83 void
kono
parents:
diff changeset
84 write_string(const std::string& s)
kono
parents:
diff changeset
85 { this->write_and_sum_bytes(s.data(), s.length()); }
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 // Write a nul terminated string. Implements the String_dump interface.
kono
parents:
diff changeset
88 void
kono
parents:
diff changeset
89 write_c_string(const char* s)
kono
parents:
diff changeset
90 { this->write_and_sum_bytes(s, strlen(s)); }
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 // Write some bytes.
kono
parents:
diff changeset
93 void
kono
parents:
diff changeset
94 write_bytes(const char* bytes, size_t length)
kono
parents:
diff changeset
95 { this->write_and_sum_bytes(bytes, length); }
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 // Return the raw bytes of the checksum data.
kono
parents:
diff changeset
98 std::string
kono
parents:
diff changeset
99 checksum();
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 // Write a checksum string to the stream. This will be called at
kono
parents:
diff changeset
102 // the end of the other output.
kono
parents:
diff changeset
103 void
kono
parents:
diff changeset
104 write_checksum(const std::string&);
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 protected:
kono
parents:
diff changeset
107 // This function is called with data to export. This data must be
kono
parents:
diff changeset
108 // made available as a contiguous stream for the importer.
kono
parents:
diff changeset
109 virtual void
kono
parents:
diff changeset
110 do_write(const char* bytes, size_t length) = 0;
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 private:
kono
parents:
diff changeset
113 void
kono
parents:
diff changeset
114 write_and_sum_bytes(const char*, size_t);
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 // The checksum helper.
kono
parents:
diff changeset
117 Go_sha1_helper* sha1_helper_;
kono
parents:
diff changeset
118 };
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 Export(Stream*);
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 // Size of export data magic string (which includes version number).
kono
parents:
diff changeset
123 static const int magic_len = 4;
kono
parents:
diff changeset
124
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
125 // Magic strings (current version and older versions).
111
kono
parents:
diff changeset
126 static const char cur_magic[magic_len];
kono
parents:
diff changeset
127 static const char v1_magic[magic_len];
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
128 static const char v2_magic[magic_len];
111
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 // The length of the checksum string.
kono
parents:
diff changeset
131 static const int checksum_len = 20;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 // Register the builtin types.
kono
parents:
diff changeset
134 void
kono
parents:
diff changeset
135 register_builtin_types(Gogo*);
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 // Export the identifiers in BINDINGS which are marked for export.
kono
parents:
diff changeset
138 // The exporting is done via a series of calls to THIS->STREAM_. If
kono
parents:
diff changeset
139 // is nothing to export, this->stream_->write will not be called.
kono
parents:
diff changeset
140 // PREFIX is the package prefix. PKGPATH is the package path.
kono
parents:
diff changeset
141 // Only one of PREFIX and PKGPATH will be non-empty.
kono
parents:
diff changeset
142 // PACKAGES is all the packages we have seen.
kono
parents:
diff changeset
143 // IMPORTS is the explicitly imported packages.
kono
parents:
diff changeset
144 // IMPORT_INIT_FN is the name of the import initialization function
kono
parents:
diff changeset
145 // for this package; it will be empty if none is needed.
kono
parents:
diff changeset
146 // IMPORTED_INIT_FNS is the list of initialization functions for
kono
parents:
diff changeset
147 // imported packages.
kono
parents:
diff changeset
148 void
kono
parents:
diff changeset
149 export_globals(const std::string& package_name,
kono
parents:
diff changeset
150 const std::string& prefix,
kono
parents:
diff changeset
151 const std::string& pkgpath,
kono
parents:
diff changeset
152 const std::map<std::string, Package*>& packages,
kono
parents:
diff changeset
153 const std::map<std::string, Package*>& imports,
kono
parents:
diff changeset
154 const std::string& import_init_fn,
kono
parents:
diff changeset
155 const Import_init_set& imported_init_fns,
kono
parents:
diff changeset
156 const Bindings* bindings);
kono
parents:
diff changeset
157
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
158 // Set the index of a type.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
159 bool
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
160 set_type_index(Type*);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
161
111
kono
parents:
diff changeset
162 // Write a string to the export stream.
kono
parents:
diff changeset
163 void
kono
parents:
diff changeset
164 write_string(const std::string& s)
kono
parents:
diff changeset
165 { this->stream_->write_string(s); }
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 // Write a nul terminated string to the export stream.
kono
parents:
diff changeset
168 void
kono
parents:
diff changeset
169 write_c_string(const char* s)
kono
parents:
diff changeset
170 { this->stream_->write_c_string(s); }
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 // Write some bytes to the export stream.
kono
parents:
diff changeset
173 void
kono
parents:
diff changeset
174 write_bytes(const char* bytes, size_t length)
kono
parents:
diff changeset
175 { this->stream_->write_bytes(bytes, length); }
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 // Write a name to the export stream. If NAME is empty, write "?".
kono
parents:
diff changeset
178 void
kono
parents:
diff changeset
179 write_name(const std::string& name);
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 // Write out a type. This handles references back to previous
kono
parents:
diff changeset
182 // definitions.
kono
parents:
diff changeset
183 void
kono
parents:
diff changeset
184 write_type(const Type*);
kono
parents:
diff changeset
185
kono
parents:
diff changeset
186 // Write the escape note to the export stream. If NOTE is NULL, write
kono
parents:
diff changeset
187 // nothing.
kono
parents:
diff changeset
188 void
kono
parents:
diff changeset
189 write_escape(std::string* note);
kono
parents:
diff changeset
190
kono
parents:
diff changeset
191 // Write an integer value.
kono
parents:
diff changeset
192 void
kono
parents:
diff changeset
193 write_int(int);
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 // Write an unsigned value.
kono
parents:
diff changeset
196 void
kono
parents:
diff changeset
197 write_unsigned(unsigned);
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 private:
kono
parents:
diff changeset
200 Export(const Export&);
kono
parents:
diff changeset
201 Export& operator=(const Export&);
kono
parents:
diff changeset
202
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
203 // Prepare types for exporting.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
204 int
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
205 prepare_types(const std::vector<Named_object*>* exports,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
206 Unordered_set(const Package*)* imports);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
207
111
kono
parents:
diff changeset
208 // Write out all known packages.
kono
parents:
diff changeset
209 void
kono
parents:
diff changeset
210 write_packages(const std::map<std::string, Package*>& packages);
kono
parents:
diff changeset
211
kono
parents:
diff changeset
212 typedef std::map<unsigned, std::set<unsigned> > Init_graph;
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 static void
kono
parents:
diff changeset
215 add_init_graph_edge(Init_graph* init_graph, unsigned src, unsigned sink);
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 static void
kono
parents:
diff changeset
218 populate_init_graph(Init_graph* init_graph,
kono
parents:
diff changeset
219 const Import_init_set& imported_init_fns,
kono
parents:
diff changeset
220 const std::map<std::string, unsigned>& init_idx);
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 // Write out the imported packages.
kono
parents:
diff changeset
223 void
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
224 write_imports(const std::map<std::string, Package*>& imports,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
225 const Unordered_set(const Package*)& type_imports);
111
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 // Write out the imported initialization functions and init graph.
kono
parents:
diff changeset
228 void
kono
parents:
diff changeset
229 write_imported_init_fns(const std::string& package_name,
kono
parents:
diff changeset
230 const std::string&, const Import_init_set&);
kono
parents:
diff changeset
231
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
232 // Write out all types.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
233 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
234 write_types(int unexported_type_index);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
235
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
236 // Write out one type definition.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
237 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
238 write_type_definition(const Type* type, int index);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
239
111
kono
parents:
diff changeset
240 // Register one builtin type.
kono
parents:
diff changeset
241 void
kono
parents:
diff changeset
242 register_builtin_type(Gogo*, const char* name, Builtin_code);
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 // The stream to which we are writing data.
kono
parents:
diff changeset
245 Stream* stream_;
kono
parents:
diff changeset
246 // Index number of next type.
kono
parents:
diff changeset
247 int type_index_;
kono
parents:
diff changeset
248 // Packages we have written out.
kono
parents:
diff changeset
249 Unordered_set(const Package*) packages_;
kono
parents:
diff changeset
250 };
kono
parents:
diff changeset
251
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
252 // An export streamer that puts the export stream in a named section.
111
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 class Stream_to_section : public Export::Stream
kono
parents:
diff changeset
255 {
kono
parents:
diff changeset
256 public:
kono
parents:
diff changeset
257 Stream_to_section(Backend*);
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 protected:
kono
parents:
diff changeset
260 void
kono
parents:
diff changeset
261 do_write(const char*, size_t);
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 private:
kono
parents:
diff changeset
264 Backend* backend_;
kono
parents:
diff changeset
265 };
kono
parents:
diff changeset
266
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
267 // An export streamer that puts the export stream in a string.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
268
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
269 class Stream_to_string : public Export::Stream
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
270 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
271 public:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
272 Stream_to_string()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
273 : string_()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
274 {}
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
275
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
276 const std::string&
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
277 string() const
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
278 { return this->string_; }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
279
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
280 protected:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
281 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
282 do_write(const char* s, size_t len)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
283 { this->string_.append(s, len); }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
284
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
285 private:
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
286 std::string string_;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
287 };
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
288
111
kono
parents:
diff changeset
289 #endif // !defined(GO_EXPORT_H)