]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/go/gofrontend/export.h
ee61d2752de480397d5cc12eddcefcea5cdc1216
[thirdparty/gcc.git] / gcc / go / gofrontend / export.h
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 struct sha1_ctx;
13 class Gogo;
14 class Import_init;
15 class Bindings;
16 class Type;
17 class Package;
18 class Import_init_set;
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,
40 BUILTIN_BOOL = -15,
41 BUILTIN_STRING = -16,
42 BUILTIN_COMPLEX64 = -17,
43 BUILTIN_COMPLEX128 = -18,
44 BUILTIN_ERROR = -19,
45 BUILTIN_BYTE = -20,
46 BUILTIN_RUNE = -21,
47
48 SMALLEST_BUILTIN_CODE = -21
49 };
50
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
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
66 class Export : public String_dump
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
78 // Write a string. Implements the String_dump interface.
79 void
80 write_string(const std::string& s)
81 { this->write_and_sum_bytes(s.data(), s.length()); }
82
83 // Write a nul terminated string. Implements the String_dump interface.
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
112 // The checksum.
113 sha1_ctx* checksum_;
114 };
115
116 Export(Stream*);
117
118 // Size of export data magic string (which includes version number).
119 static const int magic_len = 4;
120
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;
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.
135 // PREFIX is the package prefix. PKGPATH is the package path.
136 // Only one of PREFIX and PKGPATH will be non-empty.
137 // PACKAGES is all the packages we have seen.
138 // IMPORTS is the explicitly imported packages.
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,
145 const std::string& prefix,
146 const std::string& pkgpath,
147 const std::map<std::string, Package*>& packages,
148 const std::map<std::string, Package*>& imports,
149 const std::string& import_init_fn,
150 const Import_init_set& imported_init_fns,
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
168 // Write a name to the export stream. If NAME is empty, write "?".
169 void
170 write_name(const std::string& name);
171
172 // Write out a type. This handles references back to previous
173 // definitions.
174 void
175 write_type(const Type*);
176
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
182 // Write an integer value.
183 void
184 write_int(int);
185
186 // Write an unsigned value.
187 void
188 write_unsigned(unsigned);
189
190 private:
191 Export(const Export&);
192 Export& operator=(const Export&);
193
194 // Write out all known packages.
195 void
196 write_packages(const std::map<std::string, Package*>& packages);
197
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
208 // Write out the imported packages.
209 void
210 write_imports(const std::map<std::string, Package*>& imports);
211
212 // Write out the imported initialization functions and init graph.
213 void
214 write_imported_init_fns(const std::string& package_name,
215 const std::string&, const Import_init_set&);
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_;
230 // Packages we have written out.
231 Unordered_set(const Package*) packages_;
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);
244 };
245
246 #endif // !defined(GO_EXPORT_H)