1 // export.cc -- Export declarations in Go frontend.
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.
15 #include "statements.h"
18 // This file handles exporting global declarations.
22 // Version 1 magic number.
24 const int Export::v1_magic_len;
26 const char Export::v1_magic[Export::v1_magic_len] =
31 const int Export::v1_checksum_len;
35 Export::Export(Stream* stream)
36 : stream_(stream), type_refs_(), type_index_(1)
40 // A functor to sort Named_object pointers by name.
45 operator()(const Named_object* n1, const Named_object* n2) const
46 { return n1->name() < n2->name(); }
49 // Return true if we should export NO.
52 should_export(Named_object* no)
54 // We only export objects which are locally defined.
55 if (no->package() != NULL)
58 // We don't export packages.
62 // We don't export hidden names.
63 if (Gogo::is_hidden_name(no->name()))
66 // We don't export nested functions.
67 if (no->is_function() && no->func_value()->enclosing() != NULL)
70 // We don't export thunks.
71 if (no->is_function() && Gogo::is_thunk(no))
74 // Methods are exported with the type, not here.
76 && no->func_value()->type()->is_method())
78 if (no->is_function_declaration()
79 && no->func_declaration_value()->type()->is_method())
82 // Don't export dummy global variables created for initializers when
84 if (no->is_variable() && no->name()[0] == '_' && no->name()[1] == '.')
90 // Export those identifiers marked for exporting.
93 Export::export_globals(const std::string& package_name,
94 const std::string& unique_prefix,
96 const std::string& import_init_fn,
97 const std::set<Import_init>& imported_init_fns,
98 const Bindings* bindings)
100 // If there have been any errors so far, don't try to export
101 // anything. That way the export code doesn't have to worry about
102 // mismatched types or other confusions.
106 // Export the symbols in sorted order. That will reduce cases where
107 // irrelevant changes to the source code affect the exported
109 std::vector<Named_object*> exports;
110 exports.reserve(bindings->size_definitions());
112 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
113 p != bindings->end_definitions();
115 if (should_export(*p))
116 exports.push_back(*p);
118 for (Bindings::const_declarations_iterator p =
119 bindings->begin_declarations();
120 p != bindings->end_declarations();
123 // We export a function declaration as it may be implemented in
124 // supporting C code. We do not export type declarations.
125 if (p->second->is_function_declaration()
126 && should_export(p->second))
127 exports.push_back(p->second);
130 std::sort(exports.begin(), exports.end(), Sort_bindings());
132 // Although the export data is readable, at least this version is,
133 // it is conceptually a binary format. Start with a four byte
135 this->write_bytes(Export::v1_magic, Export::v1_magic_len);
138 this->write_c_string("package ");
139 this->write_string(package_name);
140 this->write_c_string(";\n");
142 // The unique prefix. This prefix is used for all global symbols.
143 this->write_c_string("prefix ");
144 this->write_string(unique_prefix);
145 this->write_c_string(";\n");
147 // The package priority.
149 snprintf(buf, sizeof buf, "priority %d;\n", package_priority);
150 this->write_c_string(buf);
152 this->write_imported_init_fns(package_name, package_priority, import_init_fn,
155 // FIXME: It might be clever to add something about the processor
156 // and ABI being used, although ideally any problems in that area
157 // would be caught by the linker.
159 for (std::vector<Named_object*>::const_iterator p = exports.begin();
162 (*p)->export_named_object(this);
164 std::string checksum = this->stream_->checksum();
165 std::string s = "checksum ";
166 for (std::string::const_iterator p = checksum.begin();
170 unsigned char c = *p;
171 unsigned int dig = c >> 4;
172 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
174 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
177 this->stream_->write_checksum(s);
180 // Write out the import control variables for this package.
183 Export::write_imported_init_fns(
184 const std::string& package_name,
186 const std::string& import_init_fn,
187 const std::set<Import_init>& imported_init_fns)
189 if (import_init_fn.empty() && imported_init_fns.empty())
192 this->write_c_string("import");
194 if (!import_init_fn.empty())
196 this->write_c_string(" ");
197 this->write_string(package_name);
198 this->write_c_string(" ");
199 this->write_string(import_init_fn);
201 snprintf(buf, sizeof buf, " %d", priority);
202 this->write_c_string(buf);
205 if (!imported_init_fns.empty())
207 // Sort the list of functions for more consistent output.
208 std::vector<Import_init> v;
209 for (std::set<Import_init>::const_iterator p = imported_init_fns.begin();
210 p != imported_init_fns.end();
213 std::sort(v.begin(), v.end());
215 for (std::vector<Import_init>::const_iterator p = v.begin();
219 this->write_c_string(" ");
220 this->write_string(p->package_name());
221 this->write_c_string(" ");
222 this->write_string(p->init_name());
224 snprintf(buf, sizeof buf, " %d", p->priority());
225 this->write_c_string(buf);
229 this->write_c_string(";\n");
232 // Write a name to the export stream.
235 Export::write_name(const std::string& name)
238 this->write_c_string("?");
240 this->write_string(Gogo::message_name(name));
243 // Export a type. We have to ensure that on import we create a single
244 // Named_type node for each named type. We do this by keeping a hash
245 // table mapping named types to reference numbers. The first time we
246 // see a named type we assign it a reference number by making an entry
247 // in the hash table. If we see it again, we just refer to the
250 // Named types are, of course, associated with packages. Note that we
251 // may see a named type when importing one package, and then later see
252 // the same named type when importing a different package. The home
253 // package may or may not be imported during this compilation. The
254 // reference number scheme has to get this all right. Basic approach
255 // taken from "On the Linearization of Graphs and Writing Symbol
256 // Files" by Robert Griesemer.
259 Export::write_type(const Type* type)
261 // We don't want to assign a reference number to a forward
262 // declaration to a type which was defined later.
263 type = type->forwarded();
265 Type_refs::const_iterator p = this->type_refs_.find(type);
266 if (p != this->type_refs_.end())
268 // This type was already in the table.
269 int index = p->second;
270 go_assert(index != 0);
272 snprintf(buf, sizeof buf, "<type %d>", index);
273 this->write_c_string(buf);
277 const Named_type* named_type = type->named_type();
278 const Forward_declaration_type* forward = type->forward_declaration_type();
280 int index = this->type_index_;
284 snprintf(buf, sizeof buf, "<type %d ", index);
285 this->write_c_string(buf);
287 if (named_type != NULL || forward != NULL)
289 const Named_object* named_object;
290 if (named_type != NULL)
292 // The builtin types should have been predefined.
293 go_assert(!Linemap::is_predeclared_location(named_type->location())
294 || (named_type->named_object()->package()->name()
296 named_object = named_type->named_object();
299 named_object = forward->named_object();
301 const Package* package = named_object->package();
303 std::string s = "\"";
304 if (package != NULL && !Gogo::is_hidden_name(named_object->name()))
306 s += package->unique_prefix();
308 s += package->name();
311 s += named_object->name();
313 this->write_string(s);
315 // We must add a named type to the table now, since the
316 // definition of the type may refer to the named type via a
318 this->type_refs_[type] = index;
321 type->export_type(this);
323 this->write_c_string(">");
325 if (named_type == NULL)
326 this->type_refs_[type] = index;
329 // Add the builtin types to the export table.
332 Export::register_builtin_types(Gogo* gogo)
334 this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
335 this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
336 this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
337 this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
338 this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
339 this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
340 this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
341 this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
342 this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
343 this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
344 this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
345 this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
346 this->register_builtin_type(gogo, "int", BUILTIN_INT);
347 this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
348 this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
349 this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
350 this->register_builtin_type(gogo, "string", BUILTIN_STRING);
351 this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
352 this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
353 this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
356 // Register one builtin type in the export table.
359 Export::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
361 Named_object* named_object = gogo->lookup_global(name);
362 go_assert(named_object != NULL && named_object->is_type());
363 std::pair<Type_refs::iterator, bool> ins =
364 this->type_refs_.insert(std::make_pair(named_object->type_value(), code));
365 go_assert(ins.second);
367 // We also insert the underlying type. We can see the underlying
368 // type at least for string and bool. We skip the type aliases byte
370 if (code != BUILTIN_BYTE && code != BUILTIN_RUNE)
372 Type* real_type = named_object->type_value()->real_type();
373 ins = this->type_refs_.insert(std::make_pair(real_type, code));
374 go_assert(ins.second);
378 // Class Export::Stream.
380 Export::Stream::Stream()
382 this->checksum_ = new sha1_ctx;
383 memset(this->checksum_, 0, sizeof(sha1_ctx));
384 sha1_init_ctx(this->checksum_);
387 Export::Stream::~Stream()
391 // Write bytes to the stream. This keeps a checksum of bytes as they
395 Export::Stream::write_and_sum_bytes(const char* bytes, size_t length)
397 sha1_process_bytes(bytes, length, this->checksum_);
398 this->do_write(bytes, length);
404 Export::Stream::checksum()
406 // Use a union to provide the required alignment.
409 char checksum[Export::v1_checksum_len];
412 sha1_finish_ctx(this->checksum_, u.checksum);
413 return std::string(u.checksum, Export::v1_checksum_len);
416 // Write the checksum string to the export data.
419 Export::Stream::write_checksum(const std::string& s)
421 this->do_write(s.data(), s.length());
424 // Class Stream_to_section.
426 Stream_to_section::Stream_to_section()
430 // Write data to a section.
433 Stream_to_section::do_write(const char* bytes, size_t length)
435 go_write_export_data (bytes, length);