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