1 // export.h -- Export declarations in Go frontend. -*- C++ -*-
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.
10 #include "string-dump.h"
15 class Export_function_body
;
21 class Import_init_set
;
23 class Temporary_statement
;
27 // Codes used for the builtin types. These are all negative to make
28 // them easily distinct from the codes assigned by Export::write_type.
29 // Note that these codes may not be changed! Changing them would
30 // break existing export data.
43 BUILTIN_FLOAT64
= -10,
46 BUILTIN_UINTPTR
= -13,
49 BUILTIN_COMPLEX64
= -17,
50 BUILTIN_COMPLEX128
= -18,
56 SMALLEST_BUILTIN_CODE
= -22
59 // Export data version number. New export data is written with the
60 // "current" version, but there is support for reading files with
61 // older version export data (at least for now).
63 enum Export_data_version
{
64 EXPORT_FORMAT_UNKNOWN
= 0,
68 EXPORT_FORMAT_CURRENT
= EXPORT_FORMAT_V3
71 // This class manages exporting Go declarations. It handles the main
72 // loop of exporting. A pointer to this class is also passed to the
73 // various specific export implementations.
75 class Export
: public String_dump
78 // The Stream class is an interface used to output the exported
79 // information. The caller should instantiate a child of this
87 // Write a string. Implements the String_dump interface.
89 write_string(const std::string
& s
)
90 { this->write_and_sum_bytes(s
.data(), s
.length()); }
92 // Write a nul terminated string. Implements the String_dump interface.
94 write_c_string(const char* s
)
95 { this->write_and_sum_bytes(s
, strlen(s
)); }
99 write_bytes(const char* bytes
, size_t length
)
100 { this->write_and_sum_bytes(bytes
, length
); }
102 // Return the raw bytes of the checksum data.
106 // Write a checksum string to the stream. This will be called at
107 // the end of the other output.
109 write_checksum(const std::string
&);
112 // This function is called with data to export. This data must be
113 // made available as a contiguous stream for the importer.
115 do_write(const char* bytes
, size_t length
) = 0;
119 write_and_sum_bytes(const char*, size_t);
121 // The checksum helper.
122 Go_sha1_helper
* sha1_helper_
;
128 // Size of export data magic string (which includes version number).
129 static const int magic_len
= 4;
131 // Magic strings (current version and older versions).
132 static const char cur_magic
[magic_len
];
133 static const char v1_magic
[magic_len
];
134 static const char v2_magic
[magic_len
];
136 // The length of the checksum string.
137 static const int checksum_len
= 20;
139 // Register the builtin types.
141 register_builtin_types(Gogo
*);
143 // Export the identifiers in BINDINGS which are marked for export.
144 // The exporting is done via a series of calls to THIS->STREAM_. If
145 // is nothing to export, this->stream_->write will not be called.
146 // PREFIX is the package prefix. PKGPATH is the package path.
147 // Only one of PREFIX and PKGPATH will be non-empty.
148 // PACKAGES is all the packages we have seen.
149 // IMPORTS is the explicitly imported packages.
150 // IMPORT_INIT_FN is the name of the import initialization function
151 // for this package; it will be empty if none is needed.
152 // IMPORTED_INIT_FNS is the list of initialization functions for
153 // imported packages.
155 export_globals(const std::string
& package_name
,
156 const std::string
& prefix
,
157 const std::string
& pkgpath
,
158 const std::map
<std::string
, Package
*>& packages
,
159 const std::map
<std::string
, Package
*>& imports
,
160 const std::string
& import_init_fn
,
161 const Import_init_set
& imported_init_fns
,
162 const Bindings
* bindings
,
163 Unordered_set(Named_object
*)* marked_inline_functions
);
165 // Record a type that is mentioned in export data. Return value is
166 // TRUE for newly visited types, FALSE for types that have been seen
171 // Assign type indices to types mentioned in export data.
173 assign_type_indices(const std::vector
<Named_object
*>& sorted_exports
);
175 // Write a string to the export stream.
177 write_string(const std::string
& s
)
178 { this->stream_
->write_string(s
); }
180 // Write a nul terminated string to the export stream.
182 write_c_string(const char* s
)
183 { this->stream_
->write_c_string(s
); }
185 // Write some bytes to the export stream.
187 write_bytes(const char* bytes
, size_t length
)
188 { this->stream_
->write_bytes(bytes
, length
); }
190 // Write a name to the export stream. If NAME is empty, write "?".
192 write_name(const std::string
& name
);
194 // Write out a type. This handles references back to previous
197 write_type(const Type
*);
199 // Write a type to an exported function body.
201 write_type_to(const Type
*, Export_function_body
*);
203 // Write the escape note to the export stream. If NOTE is NULL, write
206 write_escape(std::string
* note
);
208 // Write an integer value.
212 // Write an unsigned value.
214 write_unsigned(unsigned);
216 // Return the index of a package.
218 package_index(const Package
* p
) const;
220 // Return the index of the "unsafe" package, which must be one of
221 // the exported packages.
223 unsafe_package_index() const;
226 Export(const Export
&);
227 Export
& operator=(const Export
&);
229 // Write out all known packages.
231 write_packages(const std::map
<std::string
, Package
*>& packages
);
233 typedef std::map
<unsigned, std::set
<unsigned> > Init_graph
;
236 add_init_graph_edge(Init_graph
* init_graph
, unsigned src
, unsigned sink
);
239 populate_init_graph(Init_graph
* init_graph
,
240 const Import_init_set
& imported_init_fns
,
241 const std::map
<std::string
, unsigned>& init_idx
);
243 // Write out the imported packages.
245 write_imports(const std::map
<std::string
, Package
*>& imports
,
246 const Unordered_set(const Package
*)& type_imports
);
248 // Write out the imported initialization functions and init graph.
250 write_imported_init_fns(const std::string
& package_name
,
251 const std::string
&, const Import_init_set
&);
253 // Write out all types.
255 write_types(int unexported_type_index
);
257 // Write out one type definition.
259 write_type_definition(const Type
* type
, int index
);
261 // Register one builtin type.
263 register_builtin_type(Gogo
*, const char* name
, Builtin_code
);
265 // Return the index of a type in the export data.
267 type_index(const Type
*);
269 // Set the index of a type.
271 set_type_index(const Type
*);
273 // The stream to which we are writing data.
275 // Index number of next type.
277 // Packages we have written out.
278 Unordered_map(const Package
*, int) packages_
;
279 // Hidden implementation-specific state.
283 // An export streamer that puts the export stream in a named section.
285 class Stream_to_section
: public Export::Stream
288 Stream_to_section(Backend
*);
292 do_write(const char*, size_t);
298 // An export streamer that puts the export stream in a string.
300 class Stream_to_string
: public Export::Stream
309 { return this->string_
; }
313 do_write(const char* s
, size_t len
)
314 { this->string_
.append(s
, len
); }
320 // Class to manage exporting a function body. This is passed around
321 // to Statements and Expressions. It builds up the export data for
324 class Export_function_body
: public String_dump
327 Export_function_body(Export
* exp
, int indent
)
328 : exp_(exp
), body_(), type_context_(NULL
), next_temporary_index_(0),
329 temporary_indexes_(), next_label_index_(0), label_indexes_(),
333 // Write a character to the body.
336 { this->body_
.append(1, c
); }
338 // Write a NUL terminated string to the body.
340 write_c_string(const char* str
)
341 { this->body_
.append(str
); }
343 // Write a string to the body.
345 write_string(const std::string
& str
)
346 { this->body_
.append(str
); }
348 // Write a type reference to the body.
350 write_type(const Type
* type
)
351 { this->exp_
->write_type_to(type
, this); }
353 // Return the current type context.
356 { return this->type_context_
; }
358 // Set the current type context.
360 set_type_context(Type
* type
)
361 { this->type_context_
= type
; }
363 // Append as many spaces as the current indentation level.
367 for (int i
= this->indent_
; i
> 0; i
--)
368 this->write_char(' ');
371 // Increment the indentation level.
376 // Decrement the indentation level.
381 // Return the index of a package.
383 package_index(const Package
* p
) const
384 { return this->exp_
->package_index(p
); }
386 // Return the index of the "unsafe" package.
388 unsafe_package_index() const
389 { return this->exp_
->unsafe_package_index(); }
391 // Record a temporary statement and return its index.
393 record_temporary(const Temporary_statement
*);
395 // Return the index of a temporary statement.
397 temporary_index(const Temporary_statement
*);
399 // Return the index of an unnamed label. If it doesn't already have
400 // an index, give it one.
402 unnamed_label_index(const Unnamed_label
*);
404 // Return a reference to the completed body.
407 { return this->body_
; }
410 // The overall export data.
412 // The body we are building.
414 // Current type context. Used to avoid duplicate type conversions.
416 // Index to give to next temporary statement.
417 unsigned int next_temporary_index_
;
418 // Map temporary statements to indexes.
419 Unordered_map(const Temporary_statement
*, unsigned int) temporary_indexes_
;
420 // Index to give to the next unnamed label.
421 unsigned int next_label_index_
;
422 // Map unnamed labels to indexes.
423 Unordered_map(const Unnamed_label
*, unsigned int) label_indexes_
;
424 // Current indentation level: the number of spaces before each statement.
428 #endif // !defined(GO_EXPORT_H)