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"
18 class Import_init_set
;
21 // Codes used for the builtin types. These are all negative to make
22 // them easily distinct from the codes assigned by Export::write_type.
23 // Note that these codes may not be changed! Changing them would
24 // break existing export data.
37 BUILTIN_FLOAT64
= -10,
40 BUILTIN_UINTPTR
= -13,
43 BUILTIN_COMPLEX64
= -17,
44 BUILTIN_COMPLEX128
= -18,
49 SMALLEST_BUILTIN_CODE
= -21
52 // Export data version number. New export data is written with the
53 // "current" version, but there is support for reading files with
54 // older version export data (at least for now).
56 enum Export_data_version
{
57 EXPORT_FORMAT_UNKNOWN
= 0,
60 EXPORT_FORMAT_CURRENT
= EXPORT_FORMAT_V2
63 // This class manages exporting Go declarations. It handles the main
64 // loop of exporting. A pointer to this class is also passed to the
65 // various specific export implementations.
67 class Export
: public String_dump
70 // The Stream class is an interface used to output the exported
71 // information. The caller should instantiate a child of this
79 // Write a string. Implements the String_dump interface.
81 write_string(const std::string
& s
)
82 { this->write_and_sum_bytes(s
.data(), s
.length()); }
84 // Write a nul terminated string. Implements the String_dump interface.
86 write_c_string(const char* s
)
87 { this->write_and_sum_bytes(s
, strlen(s
)); }
91 write_bytes(const char* bytes
, size_t length
)
92 { this->write_and_sum_bytes(bytes
, length
); }
94 // Return the raw bytes of the checksum data.
98 // Write a checksum string to the stream. This will be called at
99 // the end of the other output.
101 write_checksum(const std::string
&);
104 // This function is called with data to export. This data must be
105 // made available as a contiguous stream for the importer.
107 do_write(const char* bytes
, size_t length
) = 0;
111 write_and_sum_bytes(const char*, size_t);
113 // The checksum helper.
114 Go_sha1_helper
* sha1_helper_
;
119 // Size of export data magic string (which includes version number).
120 static const int magic_len
= 4;
122 // Magic strings (current version and older v1 version).
123 static const char cur_magic
[magic_len
];
124 static const char v1_magic
[magic_len
];
126 // The length of the checksum string.
127 static const int checksum_len
= 20;
129 // Register the builtin types.
131 register_builtin_types(Gogo
*);
133 // Export the identifiers in BINDINGS which are marked for export.
134 // The exporting is done via a series of calls to THIS->STREAM_. If
135 // is nothing to export, this->stream_->write will not be called.
136 // PREFIX is the package prefix. PKGPATH is the package path.
137 // Only one of PREFIX and PKGPATH will be non-empty.
138 // PACKAGES is all the packages we have seen.
139 // IMPORTS is the explicitly imported packages.
140 // IMPORT_INIT_FN is the name of the import initialization function
141 // for this package; it will be empty if none is needed.
142 // IMPORTED_INIT_FNS is the list of initialization functions for
143 // imported packages.
145 export_globals(const std::string
& package_name
,
146 const std::string
& prefix
,
147 const std::string
& pkgpath
,
148 const std::map
<std::string
, Package
*>& packages
,
149 const std::map
<std::string
, Package
*>& imports
,
150 const std::string
& import_init_fn
,
151 const Import_init_set
& imported_init_fns
,
152 const Bindings
* bindings
);
154 // Write a string to the export stream.
156 write_string(const std::string
& s
)
157 { this->stream_
->write_string(s
); }
159 // Write a nul terminated string to the export stream.
161 write_c_string(const char* s
)
162 { this->stream_
->write_c_string(s
); }
164 // Write some bytes to the export stream.
166 write_bytes(const char* bytes
, size_t length
)
167 { this->stream_
->write_bytes(bytes
, length
); }
169 // Write a name to the export stream. If NAME is empty, write "?".
171 write_name(const std::string
& name
);
173 // Write out a type. This handles references back to previous
176 write_type(const Type
*);
178 // Write the escape note to the export stream. If NOTE is NULL, write
181 write_escape(std::string
* note
);
183 // Write an integer value.
187 // Write an unsigned value.
189 write_unsigned(unsigned);
192 Export(const Export
&);
193 Export
& operator=(const Export
&);
195 // Write out all known packages.
197 write_packages(const std::map
<std::string
, Package
*>& packages
);
199 typedef std::map
<unsigned, std::set
<unsigned> > Init_graph
;
202 add_init_graph_edge(Init_graph
* init_graph
, unsigned src
, unsigned sink
);
205 populate_init_graph(Init_graph
* init_graph
,
206 const Import_init_set
& imported_init_fns
,
207 const std::map
<std::string
, unsigned>& init_idx
);
209 // Write out the imported packages.
211 write_imports(const std::map
<std::string
, Package
*>& imports
);
213 // Write out the imported initialization functions and init graph.
215 write_imported_init_fns(const std::string
& package_name
,
216 const std::string
&, const Import_init_set
&);
218 // Register one builtin type.
220 register_builtin_type(Gogo
*, const char* name
, Builtin_code
);
222 // Mapping from Type objects to a constant index.
223 typedef Unordered_map(const Type
*, int) Type_refs
;
225 // The stream to which we are writing data.
228 Type_refs type_refs_
;
229 // Index number of next type.
231 // Packages we have written out.
232 Unordered_set(const Package
*) packages_
;
235 // An export streamer which puts the export stream in a named section.
237 class Stream_to_section
: public Export::Stream
240 Stream_to_section(Backend
*);
244 do_write(const char*, size_t);
250 #endif // !defined(GO_EXPORT_H)