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