111
|
1 // export.cc -- Export declarations in Go frontend.
|
|
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 #include "go-system.h"
|
|
8
|
|
9 #include "go-sha1.h"
|
|
10 #include "go-c.h"
|
|
11
|
|
12 #include "gogo.h"
|
|
13 #include "types.h"
|
|
14 #include "statements.h"
|
|
15 #include "export.h"
|
|
16
|
|
17 #include "go-linemap.h"
|
|
18 #include "backend.h"
|
|
19
|
|
20 // This file handles exporting global declarations.
|
|
21
|
|
22 // Class Export.
|
|
23
|
|
24 const int Export::magic_len;
|
|
25
|
|
26 // Current version magic string.
|
|
27 const char Export::cur_magic[Export::magic_len] =
|
|
28 {
|
131
|
29 'v', '3', ';', '\n'
|
111
|
30 };
|
|
31
|
131
|
32 // Magic strings for previous versions (still supported).
|
111
|
33 const char Export::v1_magic[Export::magic_len] =
|
|
34 {
|
|
35 'v', '1', ';', '\n'
|
|
36 };
|
131
|
37 const char Export::v2_magic[Export::magic_len] =
|
|
38 {
|
|
39 'v', '2', ';', '\n'
|
|
40 };
|
111
|
41
|
|
42 const int Export::checksum_len;
|
|
43
|
|
44 // Constructor.
|
|
45
|
|
46 Export::Export(Stream* stream)
|
131
|
47 : stream_(stream), type_index_(1), packages_()
|
111
|
48 {
|
|
49 go_assert(Export::checksum_len == Go_sha1_helper::checksum_len);
|
|
50 }
|
|
51
|
131
|
52 // Type hash table operations, treating aliases as distinct.
|
|
53
|
|
54 class Type_hash_alias_identical
|
|
55 {
|
|
56 public:
|
|
57 unsigned int
|
|
58 operator()(const Type* type) const
|
|
59 {
|
|
60 return type->hash_for_method(NULL,
|
|
61 (Type::COMPARE_ERRORS
|
|
62 | Type::COMPARE_TAGS
|
|
63 | Type::COMPARE_ALIASES));
|
|
64 }
|
|
65 };
|
|
66
|
|
67 class Type_alias_identical
|
|
68 {
|
|
69 public:
|
|
70 bool
|
|
71 operator()(const Type* t1, const Type* t2) const
|
|
72 {
|
|
73 return Type::are_identical(t1, t2,
|
|
74 (Type::COMPARE_ERRORS
|
|
75 | Type::COMPARE_TAGS
|
|
76 | Type::COMPARE_ALIASES),
|
|
77 NULL);
|
|
78 }
|
|
79 };
|
|
80
|
|
81 // Mapping from Type objects to a constant index. This would be nicer
|
|
82 // as a field in Export, but then export.h would have to #include
|
|
83 // types.h.
|
|
84
|
|
85 typedef Unordered_map_hash(const Type*, int, Type_hash_alias_identical,
|
|
86 Type_alias_identical) Type_refs;
|
|
87
|
|
88 static Type_refs type_refs;
|
|
89
|
111
|
90 // A functor to sort Named_object pointers by name.
|
|
91
|
|
92 struct Sort_bindings
|
|
93 {
|
|
94 bool
|
|
95 operator()(const Named_object* n1, const Named_object* n2) const
|
|
96 { return n1->name() < n2->name(); }
|
|
97 };
|
|
98
|
|
99 // Return true if we should export NO.
|
|
100
|
|
101 static bool
|
|
102 should_export(Named_object* no)
|
|
103 {
|
|
104 // We only export objects which are locally defined.
|
|
105 if (no->package() != NULL)
|
|
106 return false;
|
|
107
|
|
108 // We don't export packages.
|
|
109 if (no->is_package())
|
|
110 return false;
|
|
111
|
|
112 // We don't export hidden names.
|
|
113 if (Gogo::is_hidden_name(no->name()))
|
|
114 return false;
|
|
115
|
131
|
116 // We don't export various special functions.
|
|
117 if (Gogo::is_special_name(no->name()))
|
111
|
118 return false;
|
|
119
|
|
120 // Methods are exported with the type, not here.
|
|
121 if (no->is_function()
|
|
122 && no->func_value()->type()->is_method())
|
|
123 return false;
|
|
124 if (no->is_function_declaration()
|
|
125 && no->func_declaration_value()->type()->is_method())
|
|
126 return false;
|
|
127
|
|
128 // Don't export dummy global variables created for initializers when
|
|
129 // used with sinks.
|
|
130 if (no->is_variable() && no->name()[0] == '_' && no->name()[1] == '.')
|
|
131 return false;
|
|
132
|
|
133 return true;
|
|
134 }
|
|
135
|
|
136 // Export those identifiers marked for exporting.
|
|
137
|
|
138 void
|
|
139 Export::export_globals(const std::string& package_name,
|
|
140 const std::string& prefix,
|
|
141 const std::string& pkgpath,
|
|
142 const std::map<std::string, Package*>& packages,
|
|
143 const std::map<std::string, Package*>& imports,
|
|
144 const std::string& import_init_fn,
|
|
145 const Import_init_set& imported_init_fns,
|
|
146 const Bindings* bindings)
|
|
147 {
|
|
148 // If there have been any errors so far, don't try to export
|
|
149 // anything. That way the export code doesn't have to worry about
|
|
150 // mismatched types or other confusions.
|
|
151 if (saw_errors())
|
|
152 return;
|
|
153
|
|
154 // Export the symbols in sorted order. That will reduce cases where
|
|
155 // irrelevant changes to the source code affect the exported
|
|
156 // interface.
|
|
157 std::vector<Named_object*> exports;
|
|
158 exports.reserve(bindings->size_definitions());
|
|
159
|
|
160 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
|
|
161 p != bindings->end_definitions();
|
|
162 ++p)
|
|
163 if (should_export(*p))
|
|
164 exports.push_back(*p);
|
|
165
|
|
166 for (Bindings::const_declarations_iterator p =
|
|
167 bindings->begin_declarations();
|
|
168 p != bindings->end_declarations();
|
|
169 ++p)
|
|
170 {
|
|
171 // We export a function declaration as it may be implemented in
|
|
172 // supporting C code. We do not export type declarations.
|
|
173 if (p->second->is_function_declaration()
|
|
174 && should_export(p->second))
|
|
175 exports.push_back(p->second);
|
|
176 }
|
|
177
|
|
178 std::sort(exports.begin(), exports.end(), Sort_bindings());
|
|
179
|
131
|
180 // Assign indexes to all exported types and types referenced by
|
|
181 // exported types, and collect all packages mentioned.
|
|
182 Unordered_set(const Package*) type_imports;
|
|
183 int unexported_type_index = this->prepare_types(&exports, &type_imports);
|
|
184
|
111
|
185 // Although the export data is readable, at least this version is,
|
|
186 // it is conceptually a binary format. Start with a four byte
|
|
187 // version number.
|
|
188 this->write_bytes(Export::cur_magic, Export::magic_len);
|
|
189
|
|
190 // The package name.
|
|
191 this->write_c_string("package ");
|
|
192 this->write_string(package_name);
|
131
|
193 this->write_c_string("\n");
|
111
|
194
|
|
195 // The prefix or package path, used for all global symbols.
|
|
196 if (prefix.empty())
|
|
197 {
|
|
198 go_assert(!pkgpath.empty());
|
|
199 this->write_c_string("pkgpath ");
|
|
200 this->write_string(pkgpath);
|
|
201 }
|
|
202 else
|
|
203 {
|
|
204 this->write_c_string("prefix ");
|
|
205 this->write_string(prefix);
|
|
206 }
|
131
|
207 this->write_c_string("\n");
|
111
|
208
|
|
209 this->write_packages(packages);
|
|
210
|
131
|
211 this->write_imports(imports, type_imports);
|
111
|
212
|
|
213 this->write_imported_init_fns(package_name, import_init_fn,
|
|
214 imported_init_fns);
|
|
215
|
|
216 // FIXME: It might be clever to add something about the processor
|
|
217 // and ABI being used, although ideally any problems in that area
|
|
218 // would be caught by the linker.
|
|
219
|
131
|
220 // Write out all the types, both exported and not.
|
|
221 this->write_types(unexported_type_index);
|
|
222
|
|
223 // Write out the non-type export data.
|
111
|
224 for (std::vector<Named_object*>::const_iterator p = exports.begin();
|
|
225 p != exports.end();
|
|
226 ++p)
|
131
|
227 {
|
|
228 if (!(*p)->is_type())
|
|
229 (*p)->export_named_object(this);
|
|
230 }
|
111
|
231
|
|
232 std::string checksum = this->stream_->checksum();
|
|
233 std::string s = "checksum ";
|
|
234 for (std::string::const_iterator p = checksum.begin();
|
|
235 p != checksum.end();
|
|
236 ++p)
|
|
237 {
|
|
238 unsigned char c = *p;
|
|
239 unsigned int dig = c >> 4;
|
|
240 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
|
|
241 dig = c & 0xf;
|
|
242 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
|
|
243 }
|
131
|
244 s += "\n";
|
111
|
245 this->stream_->write_checksum(s);
|
|
246 }
|
|
247
|
131
|
248 // Traversal class to find referenced types.
|
|
249
|
|
250 class Find_types_to_prepare : public Traverse
|
|
251 {
|
|
252 public:
|
|
253 Find_types_to_prepare(Export* exp,
|
|
254 Unordered_set(const Package*)* imports)
|
|
255 : Traverse(traverse_types),
|
|
256 exp_(exp), imports_(imports)
|
|
257 { }
|
|
258
|
|
259 int
|
|
260 type(Type* type);
|
|
261
|
|
262 // Traverse the components of a function type.
|
|
263 void
|
|
264 traverse_function(Function_type*);
|
|
265
|
|
266 // Traverse the methods of a named type, and register its package.
|
|
267 void
|
|
268 traverse_named_type(Named_type*);
|
|
269
|
|
270 private:
|
|
271 // Exporters.
|
|
272 Export* exp_;
|
|
273 // List of packages we are building.
|
|
274 Unordered_set(const Package*)* imports_;
|
|
275 };
|
|
276
|
|
277 // Set type index of referenced type, record package imports, and make
|
|
278 // sure we traverse methods of named types.
|
|
279
|
|
280 int
|
|
281 Find_types_to_prepare::type(Type* type)
|
|
282 {
|
|
283 // Skip forwarders; don't try to give them a type index.
|
|
284 if (type->forward_declaration_type() != NULL)
|
|
285 return TRAVERSE_CONTINUE;
|
|
286
|
|
287 // Skip the void type, which we'll see when exporting
|
|
288 // unsafe.Pointer. The void type is not itself exported, because
|
|
289 // Pointer_type::do_export checks for it.
|
|
290 if (type->is_void_type())
|
|
291 return TRAVERSE_SKIP_COMPONENTS;
|
|
292
|
|
293 if (!this->exp_->set_type_index(type))
|
|
294 {
|
|
295 // We've already seen this type.
|
|
296 return TRAVERSE_SKIP_COMPONENTS;
|
|
297 }
|
|
298
|
|
299 // At this stage of compilation traversing interface types traverses
|
|
300 // the final list of methods, but we export the locally defined
|
|
301 // methods. If there is an embedded interface type we need to make
|
|
302 // sure to export that. Check classification, rather than calling
|
|
303 // the interface_type method, because we want to handle named types
|
|
304 // below.
|
|
305 if (type->classification() == Type::TYPE_INTERFACE)
|
|
306 {
|
|
307 Interface_type* it = type->interface_type();
|
|
308 const Typed_identifier_list* methods = it->local_methods();
|
|
309 if (methods != NULL)
|
|
310 {
|
|
311 for (Typed_identifier_list::const_iterator p = methods->begin();
|
|
312 p != methods->end();
|
|
313 ++p)
|
|
314 {
|
|
315 if (p->name().empty())
|
|
316 Type::traverse(p->type(), this);
|
|
317 else
|
|
318 this->traverse_function(p->type()->function_type());
|
|
319 }
|
|
320 }
|
|
321 return TRAVERSE_SKIP_COMPONENTS;
|
|
322 }
|
|
323
|
|
324 Named_type* nt = type->named_type();
|
|
325 if (nt != NULL)
|
|
326 this->traverse_named_type(nt);
|
|
327
|
|
328 return TRAVERSE_CONTINUE;
|
|
329 }
|
|
330
|
|
331 // Traverse the types in a function type. We don't need the function
|
|
332 // type itself, just the receiver, parameter, and result types.
|
|
333
|
|
334 void
|
|
335 Find_types_to_prepare::traverse_function(Function_type* type)
|
|
336 {
|
|
337 go_assert(type != NULL);
|
|
338 if (this->remember_type(type))
|
|
339 return;
|
|
340 const Typed_identifier* receiver = type->receiver();
|
|
341 if (receiver != NULL)
|
|
342 Type::traverse(receiver->type(), this);
|
|
343 const Typed_identifier_list* parameters = type->parameters();
|
|
344 if (parameters != NULL)
|
|
345 parameters->traverse(this);
|
|
346 const Typed_identifier_list* results = type->results();
|
|
347 if (results != NULL)
|
|
348 results->traverse(this);
|
|
349 }
|
|
350
|
|
351 // Traverse the methods of a named type, and record its package.
|
|
352
|
|
353 void
|
|
354 Find_types_to_prepare::traverse_named_type(Named_type* nt)
|
|
355 {
|
|
356 const Package* package = nt->named_object()->package();
|
|
357 if (package != NULL)
|
|
358 this->imports_->insert(package);
|
|
359
|
|
360 // We have to traverse the methods of named types, because we are
|
|
361 // going to export them. This is not done by ordinary type
|
|
362 // traversal.
|
|
363 const Bindings* methods = nt->local_methods();
|
|
364 if (methods != NULL)
|
|
365 {
|
|
366 for (Bindings::const_definitions_iterator pm =
|
|
367 methods->begin_definitions();
|
|
368 pm != methods->end_definitions();
|
|
369 ++pm)
|
|
370 this->traverse_function((*pm)->func_value()->type());
|
|
371
|
|
372 for (Bindings::const_declarations_iterator pm =
|
|
373 methods->begin_declarations();
|
|
374 pm != methods->end_declarations();
|
|
375 ++pm)
|
|
376 {
|
|
377 Named_object* mno = pm->second;
|
|
378 if (mno->is_function_declaration())
|
|
379 this->traverse_function(mno->func_declaration_value()->type());
|
|
380 }
|
|
381 }
|
|
382 }
|
|
383
|
|
384 // Prepare to export types by assigning a type index to every exported
|
|
385 // type and every type referenced by an exported type. Also collect
|
|
386 // all the packages we see in types, so that if we refer to any types
|
|
387 // from indirectly imported packages we can tell the importer about
|
|
388 // the package. This returns the number of exported types.
|
|
389
|
|
390 int
|
|
391 Export::prepare_types(const std::vector<Named_object*>* exports,
|
|
392 Unordered_set(const Package*)* imports)
|
|
393 {
|
|
394 // Assign indexes to all the exported types.
|
|
395 for (std::vector<Named_object*>::const_iterator p = exports->begin();
|
|
396 p != exports->end();
|
|
397 ++p)
|
|
398 {
|
|
399 if (!(*p)->is_type())
|
|
400 continue;
|
|
401 this->set_type_index((*p)->type_value());
|
|
402 }
|
|
403
|
|
404 int ret = this->type_index_;
|
|
405
|
|
406 // Use a single instance of the traversal class because traversal
|
|
407 // classes keep track of which types they've already seen. That
|
|
408 // lets us avoid type reference loops.
|
|
409 Find_types_to_prepare find(this, imports);
|
|
410
|
|
411 // Traverse all the exported objects and assign indexes to all types.
|
|
412 for (std::vector<Named_object*>::const_iterator p = exports->begin();
|
|
413 p != exports->end();
|
|
414 ++p)
|
|
415 {
|
|
416 Named_object* no = *p;
|
|
417 switch (no->classification())
|
|
418 {
|
|
419 case Named_object::NAMED_OBJECT_CONST:
|
|
420 {
|
|
421 Type* t = no->const_value()->type();
|
|
422 if (t != NULL && !t->is_abstract())
|
|
423 Type::traverse(t, &find);
|
|
424 }
|
|
425 break;
|
|
426
|
|
427 case Named_object::NAMED_OBJECT_TYPE:
|
|
428 Type::traverse(no->type_value()->real_type(), &find);
|
|
429 find.traverse_named_type(no->type_value());
|
|
430 break;
|
|
431
|
|
432 case Named_object::NAMED_OBJECT_VAR:
|
|
433 Type::traverse(no->var_value()->type(), &find);
|
|
434 break;
|
|
435
|
|
436 case Named_object::NAMED_OBJECT_FUNC:
|
|
437 find.traverse_function(no->func_value()->type());
|
|
438 break;
|
|
439
|
|
440 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
|
|
441 find.traverse_function(no->func_declaration_value()->type());
|
|
442 break;
|
|
443
|
|
444 default:
|
|
445 // We shouldn't see anything else. If we do we'll give an
|
|
446 // error later when we try to actually export it.
|
|
447 break;
|
|
448 }
|
|
449 }
|
|
450
|
|
451 return ret;
|
|
452 }
|
|
453
|
|
454 // Give a type an index if it doesn't already have one. Return true
|
|
455 // if we set the type index, false if it was already known.
|
|
456
|
|
457 bool
|
|
458 Export::set_type_index(Type* type)
|
|
459 {
|
|
460 type = type->forwarded();
|
|
461
|
|
462 std::pair<Type_refs::iterator, bool> ins =
|
|
463 type_refs.insert(std::make_pair(type, 0));
|
|
464 if (!ins.second)
|
|
465 {
|
|
466 // We've already seen this type.
|
|
467 return false;
|
|
468 }
|
|
469
|
|
470 int index = this->type_index_;
|
|
471 ++this->type_index_;
|
|
472 ins.first->second = index;
|
|
473
|
|
474 return true;
|
|
475 }
|
|
476
|
111
|
477 // Sort packages.
|
|
478
|
|
479 static bool
|
|
480 packages_compare(const Package* a, const Package* b)
|
|
481 {
|
|
482 return a->package_name() < b->package_name();
|
|
483 }
|
|
484
|
|
485 // Write out all the known packages whose pkgpath symbol is not a
|
|
486 // simple transformation of the pkgpath, so that the importing code
|
|
487 // can reliably know it.
|
|
488
|
|
489 void
|
|
490 Export::write_packages(const std::map<std::string, Package*>& packages)
|
|
491 {
|
|
492 // Sort for consistent output.
|
|
493 std::vector<Package*> out;
|
|
494 for (std::map<std::string, Package*>::const_iterator p = packages.begin();
|
|
495 p != packages.end();
|
|
496 ++p)
|
|
497 {
|
|
498 if (p->second->pkgpath_symbol()
|
|
499 != Gogo::pkgpath_for_symbol(p->second->pkgpath()))
|
|
500 out.push_back(p->second);
|
|
501 }
|
|
502
|
|
503 std::sort(out.begin(), out.end(), packages_compare);
|
|
504
|
|
505 for (std::vector<Package*>::const_iterator p = out.begin();
|
|
506 p != out.end();
|
|
507 ++p)
|
|
508 {
|
|
509 this->write_c_string("package ");
|
|
510 this->write_string((*p)->package_name());
|
|
511 this->write_c_string(" ");
|
|
512 this->write_string((*p)->pkgpath());
|
|
513 this->write_c_string(" ");
|
|
514 this->write_string((*p)->pkgpath_symbol());
|
131
|
515 this->write_c_string("\n");
|
111
|
516 }
|
|
517 }
|
|
518
|
|
519 // Sort imported packages.
|
|
520
|
|
521 static bool
|
|
522 import_compare(const std::pair<std::string, Package*>& a,
|
|
523 const std::pair<std::string, Package*>& b)
|
|
524 {
|
|
525 return a.first < b.first;
|
|
526 }
|
|
527
|
|
528 // Write out the imported packages.
|
|
529
|
|
530 void
|
131
|
531 Export::write_imports(const std::map<std::string, Package*>& imports,
|
|
532 const Unordered_set(const Package*)& type_imports)
|
111
|
533 {
|
|
534 // Sort the imports for more consistent output.
|
131
|
535 Unordered_set(const Package*) seen;
|
111
|
536 std::vector<std::pair<std::string, Package*> > sorted_imports;
|
|
537 for (std::map<std::string, Package*>::const_iterator p = imports.begin();
|
|
538 p != imports.end();
|
|
539 ++p)
|
131
|
540 {
|
|
541 sorted_imports.push_back(std::make_pair(p->first, p->second));
|
|
542 seen.insert(p->second);
|
|
543 }
|
111
|
544
|
|
545 std::sort(sorted_imports.begin(), sorted_imports.end(), import_compare);
|
|
546
|
|
547 for (std::vector<std::pair<std::string, Package*> >::const_iterator p =
|
|
548 sorted_imports.begin();
|
|
549 p != sorted_imports.end();
|
|
550 ++p)
|
|
551 {
|
|
552 this->write_c_string("import ");
|
|
553 this->write_string(p->second->package_name());
|
|
554 this->write_c_string(" ");
|
|
555 this->write_string(p->second->pkgpath());
|
|
556 this->write_c_string(" \"");
|
|
557 this->write_string(p->first);
|
131
|
558 this->write_c_string("\"\n");
|
111
|
559
|
|
560 this->packages_.insert(p->second);
|
|
561 }
|
131
|
562
|
|
563 // Write out a separate list of indirectly imported packages.
|
|
564 std::vector<const Package*> indirect_imports;
|
|
565 for (Unordered_set(const Package*)::const_iterator p =
|
|
566 type_imports.begin();
|
|
567 p != type_imports.end();
|
|
568 ++p)
|
|
569 {
|
|
570 if (seen.find(*p) == seen.end())
|
|
571 indirect_imports.push_back(*p);
|
|
572 }
|
|
573
|
|
574 std::sort(indirect_imports.begin(), indirect_imports.end(),
|
|
575 packages_compare);
|
|
576
|
|
577 for (std::vector<const Package*>::const_iterator p =
|
|
578 indirect_imports.begin();
|
|
579 p != indirect_imports.end();
|
|
580 ++p)
|
|
581 {
|
|
582 this->write_c_string("indirectimport ");
|
|
583 this->write_string((*p)->package_name());
|
|
584 this->write_c_string(" ");
|
|
585 this->write_string((*p)->pkgpath());
|
|
586 this->write_c_string("\n");
|
|
587 }
|
111
|
588 }
|
|
589
|
|
590 void
|
|
591 Export::add_init_graph_edge(Init_graph* init_graph, unsigned src, unsigned sink)
|
|
592 {
|
|
593 Init_graph::iterator it = init_graph->find(src);
|
|
594 if (it != init_graph->end())
|
|
595 it->second.insert(sink);
|
|
596 else
|
|
597 {
|
|
598 std::set<unsigned> succs;
|
|
599 succs.insert(sink);
|
|
600 (*init_graph)[src] = succs;
|
|
601 }
|
|
602 }
|
|
603
|
|
604 // Constructs the imported portion of the init graph, e.g. those
|
|
605 // edges that we read from imported packages.
|
|
606
|
|
607 void
|
|
608 Export::populate_init_graph(Init_graph* init_graph,
|
|
609 const Import_init_set& imported_init_fns,
|
|
610 const std::map<std::string, unsigned>& init_idx)
|
|
611 {
|
|
612 for (Import_init_set::const_iterator p = imported_init_fns.begin();
|
|
613 p != imported_init_fns.end();
|
|
614 ++p)
|
|
615 {
|
|
616 const Import_init* ii = *p;
|
|
617 std::map<std::string, unsigned>::const_iterator srcit =
|
|
618 init_idx.find(ii->init_name());
|
|
619 go_assert(srcit != init_idx.end());
|
|
620 unsigned src = srcit->second;
|
|
621 for (std::set<std::string>::const_iterator pci = ii->precursors().begin();
|
|
622 pci != ii->precursors().end();
|
|
623 ++pci)
|
|
624 {
|
|
625 std::map<std::string, unsigned>::const_iterator it =
|
|
626 init_idx.find(*pci);
|
|
627 go_assert(it != init_idx.end());
|
|
628 unsigned sink = it->second;
|
|
629 add_init_graph_edge(init_graph, src, sink);
|
|
630 }
|
|
631 }
|
|
632 }
|
|
633
|
|
634 // Write out the initialization functions which need to run for this
|
|
635 // package.
|
|
636
|
|
637 void
|
|
638 Export::write_imported_init_fns(const std::string& package_name,
|
|
639 const std::string& import_init_fn,
|
|
640 const Import_init_set& imported_init_fns)
|
|
641 {
|
|
642 if (import_init_fn.empty() && imported_init_fns.empty()) return;
|
|
643
|
|
644 // Maps a given init function to the its index in the exported "init" clause.
|
|
645 std::map<std::string, unsigned> init_idx;
|
|
646
|
|
647 this->write_c_string("init");
|
|
648
|
|
649 if (!import_init_fn.empty())
|
|
650 {
|
|
651 this->write_c_string(" ");
|
|
652 this->write_string(package_name);
|
|
653 this->write_c_string(" ");
|
|
654 this->write_string(import_init_fn);
|
|
655 init_idx[import_init_fn] = 0;
|
|
656 }
|
|
657
|
|
658 if (imported_init_fns.empty())
|
|
659 {
|
131
|
660 this->write_c_string("\n");
|
111
|
661 return;
|
|
662 }
|
|
663
|
|
664 typedef std::map<int, std::vector<std::string> > level_map;
|
|
665 Init_graph init_graph;
|
|
666 level_map inits_at_level;
|
|
667
|
|
668 // Walk through the set of import inits (already sorted by
|
|
669 // init fcn name) and write them out to the exports.
|
|
670 for (Import_init_set::const_iterator p = imported_init_fns.begin();
|
|
671 p != imported_init_fns.end();
|
|
672 ++p)
|
|
673 {
|
|
674 const Import_init* ii = *p;
|
|
675
|
|
676 if (ii->init_name() == import_init_fn)
|
|
677 continue;
|
|
678
|
|
679 this->write_c_string(" ");
|
|
680 this->write_string(ii->package_name());
|
|
681 this->write_c_string(" ");
|
|
682 this->write_string(ii->init_name());
|
|
683
|
|
684 // Populate init_idx.
|
|
685 go_assert(init_idx.find(ii->init_name()) == init_idx.end());
|
|
686 unsigned idx = init_idx.size();
|
|
687 init_idx[ii->init_name()] = idx;
|
|
688
|
|
689 // If the init function has a non-negative priority value, this
|
|
690 // is an indication that it was referred to in an older version
|
|
691 // export data section (e.g. we read a legacy object
|
|
692 // file). Record such init fcns so that we can fix up the graph
|
|
693 // for them (handled later in this function).
|
|
694 if (ii->priority() > 0)
|
|
695 {
|
|
696 level_map::iterator it = inits_at_level.find(ii->priority());
|
|
697 if (it == inits_at_level.end())
|
|
698 {
|
|
699 std::vector<std::string> l;
|
|
700 l.push_back(ii->init_name());
|
|
701 inits_at_level[ii->priority()] = l;
|
|
702 }
|
|
703 else
|
|
704 it->second.push_back(ii->init_name());
|
|
705 }
|
|
706 }
|
131
|
707 this->write_c_string("\n");
|
111
|
708
|
|
709 // Create the init graph. Start by populating the graph with
|
|
710 // all the edges we inherited from imported packages.
|
|
711 populate_init_graph(&init_graph, imported_init_fns, init_idx);
|
|
712
|
|
713 // Now add edges from the local init function to each of the
|
|
714 // imported fcns.
|
|
715 if (!import_init_fn.empty())
|
|
716 {
|
|
717 unsigned src = 0;
|
|
718 go_assert(init_idx[import_init_fn] == 0);
|
|
719 for (Import_init_set::const_iterator p = imported_init_fns.begin();
|
|
720 p != imported_init_fns.end();
|
|
721 ++p)
|
|
722 {
|
|
723 const Import_init* ii = *p;
|
|
724 unsigned sink = init_idx[ii->init_name()];
|
|
725 add_init_graph_edge(&init_graph, src, sink);
|
|
726 }
|
|
727 }
|
|
728
|
|
729 // In the scenario where one or more of the packages we imported
|
|
730 // was written with the legacy export data format, add dummy edges
|
|
731 // to capture the priority relationships. Here is a package import
|
|
732 // graph as an example:
|
|
733 //
|
|
734 // *A
|
|
735 // /|
|
|
736 // / |
|
|
737 // B *C
|
|
738 // /|
|
|
739 // / |
|
|
740 // *D *E
|
|
741 // | /|
|
|
742 // |/ |
|
|
743 // *F *G
|
|
744 //
|
|
745 // Let's suppose that the object for package "C" is from an old
|
|
746 // gccgo, e.g. it has the old export data format. All other
|
|
747 // packages are compiled with the new compiler and have the new
|
|
748 // format. Packages with *'s have init functions. The scenario is
|
|
749 // that we're compiling a package "A"; during this process we'll
|
|
750 // read the export data for "C". It should look something like
|
|
751 //
|
|
752 // init F F..import 1 G G..import 1 D D..import 2 E E..import 2;
|
|
753 //
|
|
754 // To capture this information and convey it to the consumers of
|
|
755 // "A", the code below adds edges to the graph from each priority K
|
|
756 // function to every priority K-1 function for appropriate values
|
|
757 // of K. This will potentially add more edges than we need (for
|
|
758 // example, an edge from D to G), but given that we don't expect
|
|
759 // to see large numbers of old objects, this will hopefully be OK.
|
|
760
|
|
761 if (inits_at_level.size() > 0)
|
|
762 {
|
|
763 for (level_map::reverse_iterator it = inits_at_level.rbegin();
|
|
764 it != inits_at_level.rend(); ++it)
|
|
765 {
|
|
766 int level = it->first;
|
|
767 if (level < 2) break;
|
|
768 const std::vector<std::string>& fcns_at_level = it->second;
|
|
769 for (std::vector<std::string>::const_iterator sit =
|
|
770 fcns_at_level.begin();
|
|
771 sit != fcns_at_level.end(); ++sit)
|
|
772 {
|
|
773 unsigned src = init_idx[*sit];
|
|
774 level_map::iterator it2 = inits_at_level.find(level - 1);
|
|
775 if (it2 != inits_at_level.end())
|
|
776 {
|
|
777 const std::vector<std::string> fcns_at_lm1 = it2->second;
|
|
778 for (std::vector<std::string>::const_iterator mit =
|
|
779 fcns_at_lm1.begin();
|
|
780 mit != fcns_at_lm1.end(); ++mit)
|
|
781 {
|
|
782 unsigned sink = init_idx[*mit];
|
|
783 add_init_graph_edge(&init_graph, src, sink);
|
|
784 }
|
|
785 }
|
|
786 }
|
|
787 }
|
|
788 }
|
|
789
|
|
790 // Write out the resulting graph.
|
|
791 this->write_c_string("init_graph");
|
|
792 for (Init_graph::const_iterator ki = init_graph.begin();
|
|
793 ki != init_graph.end(); ++ki)
|
|
794 {
|
|
795 unsigned src = ki->first;
|
|
796 const std::set<unsigned>& successors = ki->second;
|
|
797 for (std::set<unsigned>::const_iterator vi = successors.begin();
|
|
798 vi != successors.end(); ++vi)
|
|
799 {
|
|
800 this->write_c_string(" ");
|
|
801 this->write_unsigned(src);
|
|
802 unsigned sink = (*vi);
|
|
803 this->write_c_string(" ");
|
|
804 this->write_unsigned(sink);
|
|
805 }
|
|
806 }
|
131
|
807 this->write_c_string("\n");
|
|
808 }
|
|
809
|
|
810 // Write the types to the export stream.
|
|
811
|
|
812 void
|
|
813 Export::write_types(int unexported_type_index)
|
|
814 {
|
|
815 // Map from type index to type.
|
|
816 std::vector<const Type*> types(static_cast<size_t>(this->type_index_));
|
|
817 for (Type_refs::const_iterator p = type_refs.begin();
|
|
818 p != type_refs.end();
|
|
819 ++p)
|
|
820 {
|
|
821 if (p->second >= 0)
|
|
822 types.at(p->second) = p->first;
|
|
823 }
|
|
824
|
|
825 // Write the type information to a buffer.
|
|
826 Stream_to_string type_data;
|
|
827 Export::Stream* orig_stream = this->stream_;
|
|
828 this->stream_ = &type_data;
|
|
829
|
|
830 std::vector<size_t> type_sizes(static_cast<size_t>(this->type_index_));
|
|
831 type_sizes[0] = 0;
|
|
832
|
|
833 // Start at 1 because type index 0 is not used.
|
|
834 size_t start_size = 0;
|
|
835 for (int i = 1; i < this->type_index_; ++i)
|
|
836 {
|
|
837 this->write_type_definition(types[i], i);
|
|
838
|
|
839 size_t cur_size = type_data.string().size();
|
|
840 type_sizes[i] = cur_size - start_size;
|
|
841 start_size = cur_size;
|
|
842 }
|
|
843
|
|
844 // Back to original stream.
|
|
845 this->stream_ = orig_stream;
|
|
846
|
|
847 // The line "types MAXP1 EXPORTEDP1 SIZES..." appears before the
|
|
848 // types. MAXP1 is one more than the maximum type index used; that
|
|
849 // is, it is the size of the array we need to allocate to hold all
|
|
850 // the values. Indexes 1 up to but not including EXPORTEDP1 are the
|
|
851 // exported types. The other types are not exported. SIZES... is a
|
|
852 // list of MAXP1-1 entries listing the size of the type definition
|
|
853 // for each type, starting at index 1.
|
|
854 char buf[100];
|
|
855 snprintf(buf, sizeof buf, "types %d %d", this->type_index_,
|
|
856 unexported_type_index);
|
|
857 this->write_c_string(buf);
|
|
858
|
|
859 // Start at 1 because type index 0 is not used.
|
|
860 for (int i = 1; i < this->type_index_; ++i)
|
|
861 {
|
|
862 snprintf(buf, sizeof buf, " %lu",
|
|
863 static_cast<unsigned long>(type_sizes[i]));
|
|
864 this->write_c_string(buf);
|
|
865 }
|
|
866 this->write_c_string("\n");
|
|
867 this->write_string(type_data.string());
|
|
868 }
|
|
869
|
|
870 // Write a single type to the export stream.
|
|
871
|
|
872 void
|
|
873 Export::write_type_definition(const Type* type, int index)
|
|
874 {
|
|
875 this->write_c_string("type ");
|
|
876
|
|
877 char buf[30];
|
|
878 snprintf(buf, sizeof buf, "%d ", index);
|
|
879 this->write_c_string(buf);
|
|
880
|
|
881 const Named_type* nt = type->named_type();
|
|
882 if (nt != NULL)
|
|
883 {
|
|
884 const Named_object* no = nt->named_object();
|
|
885 const Package* package = no->package();
|
|
886
|
|
887 this->write_c_string("\"");
|
|
888 if (package != NULL && !Gogo::is_hidden_name(no->name()))
|
|
889 {
|
|
890 this->write_string(package->pkgpath());
|
|
891 this->write_c_string(".");
|
|
892 }
|
|
893 this->write_string(nt->named_object()->name());
|
|
894 this->write_c_string("\" ");
|
|
895
|
|
896 if (nt->is_alias())
|
|
897 this->write_c_string("= ");
|
|
898 }
|
|
899
|
|
900 type->export_type(this);
|
|
901
|
|
902 // Type::export_type will print a newline for a named type, but not
|
|
903 // otherwise.
|
|
904 if (nt == NULL)
|
|
905 this->write_c_string("\n");
|
111
|
906 }
|
|
907
|
|
908 // Write a name to the export stream.
|
|
909
|
|
910 void
|
|
911 Export::write_name(const std::string& name)
|
|
912 {
|
|
913 if (name.empty())
|
|
914 this->write_c_string("?");
|
|
915 else
|
|
916 this->write_string(Gogo::message_name(name));
|
|
917 }
|
|
918
|
|
919 // Write an integer value to the export stream.
|
|
920
|
|
921 void
|
|
922 Export::write_int(int value)
|
|
923 {
|
|
924 char buf[100];
|
|
925 snprintf(buf, sizeof buf, "%d", value);
|
|
926 this->write_c_string(buf);
|
|
927 }
|
|
928
|
|
929 // Write an integer value to the export stream.
|
|
930
|
|
931 void
|
|
932 Export::write_unsigned(unsigned value)
|
|
933 {
|
|
934 char buf[100];
|
|
935 snprintf(buf, sizeof buf, "%u", value);
|
|
936 this->write_c_string(buf);
|
|
937 }
|
|
938
|
131
|
939 // Export a type.
|
111
|
940
|
|
941 void
|
|
942 Export::write_type(const Type* type)
|
|
943 {
|
|
944 type = type->forwarded();
|
131
|
945 Type_refs::const_iterator p = type_refs.find(type);
|
|
946 go_assert(p != type_refs.end());
|
|
947 int index = p->second;
|
|
948 go_assert(index != 0);
|
111
|
949 char buf[30];
|
131
|
950 snprintf(buf, sizeof buf, "<type %d>", index);
|
111
|
951 this->write_c_string(buf);
|
|
952 }
|
|
953
|
|
954 // Export escape note.
|
|
955
|
|
956 void
|
|
957 Export::write_escape(std::string* note)
|
|
958 {
|
|
959 if (note != NULL && *note != "esc:0x0")
|
|
960 {
|
|
961 this->write_c_string(" ");
|
|
962 char buf[50];
|
|
963 go_assert(note->find("esc:") != std::string::npos);
|
|
964 snprintf(buf, sizeof buf, "<%s>", note->c_str());
|
|
965 this->write_c_string(buf);
|
|
966 }
|
|
967 }
|
|
968
|
|
969 // Add the builtin types to the export table.
|
|
970
|
|
971 void
|
|
972 Export::register_builtin_types(Gogo* gogo)
|
|
973 {
|
|
974 this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
|
|
975 this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
|
|
976 this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
|
|
977 this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
|
|
978 this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
|
|
979 this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
|
|
980 this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
|
|
981 this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
|
|
982 this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
|
|
983 this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
|
|
984 this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
|
|
985 this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
|
|
986 this->register_builtin_type(gogo, "int", BUILTIN_INT);
|
|
987 this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
|
|
988 this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
|
|
989 this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
|
|
990 this->register_builtin_type(gogo, "string", BUILTIN_STRING);
|
|
991 this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
|
|
992 this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
|
|
993 this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
|
|
994 }
|
|
995
|
|
996 // Register one builtin type in the export table.
|
|
997
|
|
998 void
|
|
999 Export::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
|
|
1000 {
|
|
1001 Named_object* named_object = gogo->lookup_global(name);
|
|
1002 go_assert(named_object != NULL && named_object->is_type());
|
|
1003 std::pair<Type_refs::iterator, bool> ins =
|
131
|
1004 type_refs.insert(std::make_pair(named_object->type_value(), code));
|
111
|
1005 go_assert(ins.second);
|
|
1006
|
|
1007 // We also insert the underlying type. We can see the underlying
|
131
|
1008 // type at least for string and bool. It's OK if this insert
|
|
1009 // fails--we expect duplications here, and it doesn't matter when
|
|
1010 // they occur.
|
|
1011 Type* real_type = named_object->type_value()->real_type();
|
|
1012 type_refs.insert(std::make_pair(real_type, code));
|
111
|
1013 }
|
|
1014
|
|
1015 // Class Export::Stream.
|
|
1016
|
|
1017 Export::Stream::Stream()
|
|
1018 {
|
|
1019 this->sha1_helper_ = go_create_sha1_helper();
|
|
1020 go_assert(this->sha1_helper_ != NULL);
|
|
1021 }
|
|
1022
|
|
1023 Export::Stream::~Stream()
|
|
1024 {
|
|
1025 }
|
|
1026
|
|
1027 // Write bytes to the stream. This keeps a checksum of bytes as they
|
|
1028 // go by.
|
|
1029
|
|
1030 void
|
|
1031 Export::Stream::write_and_sum_bytes(const char* bytes, size_t length)
|
|
1032 {
|
|
1033 this->sha1_helper_->process_bytes(bytes, length);
|
|
1034 this->do_write(bytes, length);
|
|
1035 }
|
|
1036
|
|
1037 // Get the checksum.
|
|
1038
|
|
1039 std::string
|
|
1040 Export::Stream::checksum()
|
|
1041 {
|
|
1042 std::string rval = this->sha1_helper_->finish();
|
|
1043 delete this->sha1_helper_;
|
|
1044 return rval;
|
|
1045 }
|
|
1046
|
|
1047 // Write the checksum string to the export data.
|
|
1048
|
|
1049 void
|
|
1050 Export::Stream::write_checksum(const std::string& s)
|
|
1051 {
|
|
1052 this->do_write(s.data(), s.length());
|
|
1053 }
|
|
1054
|
|
1055 // Class Stream_to_section.
|
|
1056
|
|
1057 Stream_to_section::Stream_to_section(Backend* backend)
|
|
1058 : backend_(backend)
|
|
1059 {
|
|
1060 }
|
|
1061
|
|
1062 // Write data to a section.
|
|
1063
|
|
1064 void
|
|
1065 Stream_to_section::do_write(const char* bytes, size_t length)
|
|
1066 {
|
|
1067 this->backend_->write_export_data (bytes, length);
|
|
1068 }
|