annotate gcc/go/gofrontend/export.h @ 111:04ced10e8804

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