Commit 9badab5d by Ian Lance Taylor

compiler: refactoring in Export class to encapsulate type refs map

    
    Convert the Export::type_refs map from a static object to a field
    contained (indirectly, via an impl class) in Export itself, for better
    encapsulation and to be able to reclaim its memory when exporting is
    done. No change in compiler functionality.
    
    Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/184170

From-SVN: r272919
parent b731572b
d3d0f3c5bbe9d272178d55bdb907b07c188800e1 1e042a49d6f2e95d371301aa7b911522dc5877f4
The first line of this file holds the git revision number of the last The first line of this file holds the git revision number of the last
merge done from the gofrontend repository. merge done from the gofrontend repository.
...@@ -41,14 +41,6 @@ const char Export::v2_magic[Export::magic_len] = ...@@ -41,14 +41,6 @@ const char Export::v2_magic[Export::magic_len] =
const int Export::checksum_len; const int Export::checksum_len;
// Constructor.
Export::Export(Stream* stream)
: stream_(stream), type_index_(1), packages_()
{
go_assert(Export::checksum_len == Go_sha1_helper::checksum_len);
}
// Type hash table operations, treating aliases as distinct. // Type hash table operations, treating aliases as distinct.
class Type_hash_alias_identical class Type_hash_alias_identical
...@@ -80,14 +72,31 @@ class Type_alias_identical ...@@ -80,14 +72,31 @@ class Type_alias_identical
} }
}; };
// Mapping from Type objects to a constant index. This would be nicer // Mapping from Type objects to a constant index.
// as a field in Export, but then export.h would have to #include
// types.h.
typedef Unordered_map_hash(const Type*, int, Type_hash_alias_identical, typedef Unordered_map_hash(const Type*, int, Type_hash_alias_identical,
Type_alias_identical) Type_refs; Type_alias_identical) Type_refs;
// Implementation object for class Export. Hidden implementation avoids
// having to #include types.h in export.h, or use a static map.
struct Export_impl {
Type_refs type_refs;
};
static Type_refs type_refs; // Constructor.
Export::Export(Stream* stream)
: stream_(stream), type_index_(1), packages_(), impl_(new Export_impl)
{
go_assert(Export::checksum_len == Go_sha1_helper::checksum_len);
}
// Destructor.
Export::~Export()
{
delete this->impl_;
}
// A traversal class to collect functions and global variables // A traversal class to collect functions and global variables
// referenced by inlined functions. // referenced by inlined functions.
...@@ -635,7 +644,7 @@ Export::set_type_index(Type* type) ...@@ -635,7 +644,7 @@ Export::set_type_index(Type* type)
type = type->forwarded(); type = type->forwarded();
std::pair<Type_refs::iterator, bool> ins = std::pair<Type_refs::iterator, bool> ins =
type_refs.insert(std::make_pair(type, 0)); this->impl_->type_refs.insert(std::make_pair(type, 0));
if (!ins.second) if (!ins.second)
{ {
// We've already seen this type. // We've already seen this type.
...@@ -1011,8 +1020,8 @@ Export::write_types(int unexported_type_index) ...@@ -1011,8 +1020,8 @@ Export::write_types(int unexported_type_index)
{ {
// Map from type index to type. // Map from type index to type.
std::vector<const Type*> types(static_cast<size_t>(this->type_index_)); std::vector<const Type*> types(static_cast<size_t>(this->type_index_));
for (Type_refs::const_iterator p = type_refs.begin(); for (Type_refs::const_iterator p = this->impl_->type_refs.begin();
p != type_refs.end(); p != this->impl_->type_refs.end();
++p) ++p)
{ {
if (p->second >= 0) if (p->second >= 0)
...@@ -1152,8 +1161,8 @@ int ...@@ -1152,8 +1161,8 @@ int
Export::type_index(const Type* type) Export::type_index(const Type* type)
{ {
type = type->forwarded(); type = type->forwarded();
Type_refs::const_iterator p = type_refs.find(type); Type_refs::const_iterator p = this->impl_->type_refs.find(type);
go_assert(p != type_refs.end()); go_assert(p != this->impl_->type_refs.end());
int index = p->second; int index = p->second;
go_assert(index != 0); go_assert(index != 0);
return index; return index;
...@@ -1231,7 +1240,7 @@ Export::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code) ...@@ -1231,7 +1240,7 @@ Export::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
Named_object* named_object = gogo->lookup_global(name); Named_object* named_object = gogo->lookup_global(name);
go_assert(named_object != NULL && named_object->is_type()); go_assert(named_object != NULL && named_object->is_type());
std::pair<Type_refs::iterator, bool> ins = std::pair<Type_refs::iterator, bool> ins =
type_refs.insert(std::make_pair(named_object->type_value(), code)); this->impl_->type_refs.insert(std::make_pair(named_object->type_value(), code));
go_assert(ins.second); go_assert(ins.second);
// We also insert the underlying type. We can see the underlying // We also insert the underlying type. We can see the underlying
...@@ -1239,7 +1248,7 @@ Export::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code) ...@@ -1239,7 +1248,7 @@ Export::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
// fails--we expect duplications here, and it doesn't matter when // fails--we expect duplications here, and it doesn't matter when
// they occur. // they occur.
Type* real_type = named_object->type_value()->real_type(); Type* real_type = named_object->type_value()->real_type();
type_refs.insert(std::make_pair(real_type, code)); this->impl_->type_refs.insert(std::make_pair(real_type, code));
} }
// Class Export::Stream. // Class Export::Stream.
......
...@@ -22,6 +22,7 @@ class Import_init_set; ...@@ -22,6 +22,7 @@ class Import_init_set;
class Backend; class Backend;
class Temporary_statement; class Temporary_statement;
class Unnamed_label; class Unnamed_label;
struct Export_impl;
// Codes used for the builtin types. These are all negative to make // Codes used for the builtin types. These are all negative to make
// them easily distinct from the codes assigned by Export::write_type. // them easily distinct from the codes assigned by Export::write_type.
...@@ -121,6 +122,7 @@ class Export : public String_dump ...@@ -121,6 +122,7 @@ class Export : public String_dump
}; };
Export(Stream*); Export(Stream*);
~Export();
// Size of export data magic string (which includes version number). // Size of export data magic string (which includes version number).
static const int magic_len = 4; static const int magic_len = 4;
...@@ -262,6 +264,8 @@ class Export : public String_dump ...@@ -262,6 +264,8 @@ class Export : public String_dump
int type_index_; int type_index_;
// Packages we have written out. // Packages we have written out.
Unordered_map(const Package*, int) packages_; Unordered_map(const Package*, int) packages_;
// Hidden implementation-specific state.
Export_impl* impl_;
}; };
// An export streamer that puts the export stream in a named section. // An export streamer that puts the export stream in a named section.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment