Use Maybe<Id<T>> for {Func,Type,Var}DefDefinitionData

sizeof(db->funcs[0].def) 224  (-24)
sizeof(db->types[0].def) 248  (-24)
sizeof(db->vars[0].def) 160   (-24)
This commit is contained in:
Fangrui Song 2018-02-01 22:31:43 -08:00
parent 392cd79d04
commit 3c6f3f661f
5 changed files with 13 additions and 23 deletions

View File

@ -40,12 +40,14 @@ template <typename T>
struct Id { struct Id {
size_t id; size_t id;
Id() : id(0) {} // Needed for containers. Do not use directly. Id() : id(-1) {} // Needed for containers and Maybe<Id>. Do not use directly.
explicit Id(size_t id) : id(id) {} explicit Id(size_t id) : id(id) {}
// Needed for google::dense_hash_map. // Needed for google::dense_hash_map.
explicit operator size_t() const { return id; } explicit operator size_t() const { return id; }
bool HasValue() const { return id != size_t(-1); }
bool operator==(const Id<T>& other) const { return id == other.id; } bool operator==(const Id<T>& other) const { return id == other.id; }
bool operator<(const Id<T>& other) const { return id < other.id; } bool operator<(const Id<T>& other) const { return id < other.id; }
@ -171,7 +173,7 @@ struct TypeDefDefinitionData {
// If set, then this is the same underlying type as the given value (ie, this // If set, then this is the same underlying type as the given value (ie, this
// type comes from a using or typedef statement). // type comes from a using or typedef statement).
optional<TypeId> alias_of; Maybe<TypeId> alias_of;
// Immediate parent types. // Immediate parent types.
std::vector<TypeId> parents; std::vector<TypeId> parents;
@ -269,7 +271,7 @@ struct FuncDefDefinitionData {
Maybe<Range> definition_extent; Maybe<Range> definition_extent;
// Type which declares this one (ie, it is a method) // Type which declares this one (ie, it is a method)
optional<TypeId> declaring_type; Maybe<TypeId> declaring_type;
// Method this method overrides. // Method this method overrides.
std::vector<FuncId> base; std::vector<FuncId> base;
@ -395,7 +397,7 @@ struct VarDefDefinitionData {
Maybe<Range> definition_extent; Maybe<Range> definition_extent;
// Type of the variable. // Type of the variable.
optional<TypeId> variable_type; Maybe<TypeId> variable_type;
// Function/type which declares this one. // Function/type which declares this one.
size_t parent_id = size_t(-1); size_t parent_id = size_t(-1);

View File

@ -25,7 +25,9 @@ public:
const T& operator*() const { return storage; } const T& operator*() const { return storage; }
T& operator*() { return storage; } T& operator*() { return storage; }
bool has_value() const; bool has_value() const {
return storage.HasValue();
}
explicit operator bool() const { return has_value(); } explicit operator bool() const { return has_value(); }
operator optional<T>() const { operator optional<T>() const {
if (has_value()) if (has_value())

View File

@ -146,16 +146,6 @@ bool Range::operator<(const Range& that) const {
return end < that.end; return end < that.end;
} }
template <>
bool Maybe<Position>::has_value() const {
return storage.line >= 0;
}
template <>
bool Maybe<Range>::has_value() const {
return storage.start.line >= 0;
}
// Position // Position
void Reflect(Reader& visitor, Position& value) { void Reflect(Reader& visitor, Position& value) {
if (visitor.Format() == SerializeFormat::Json) { if (visitor.Format() == SerializeFormat::Json) {

View File

@ -15,6 +15,7 @@ struct Position {
Position(int16_t line, int16_t column); Position(int16_t line, int16_t column);
explicit Position(const char* encoded); explicit Position(const char* encoded);
bool HasValue() const { return line >= 0; }
std::string ToString(); std::string ToString();
std::string ToPrettyString(const std::string& filename); std::string ToPrettyString(const std::string& filename);
@ -38,6 +39,7 @@ struct Range {
Range(Position start, Position end); Range(Position start, Position end);
explicit Range(const char* encoded); explicit Range(const char* encoded);
bool HasValue() const { return start.HasValue(); }
bool Contains(int line, int column) const; bool Contains(int line, int column) const;
Range RemovePrefix(Position position) const; Range RemovePrefix(Position position) const;
@ -49,11 +51,6 @@ struct Range {
}; };
MAKE_HASHABLE(Range, t.start, t.end); MAKE_HASHABLE(Range, t.start, t.end);
template <>
bool Maybe<Position>::has_value() const;
template <>
bool Maybe<Range>::has_value() const;
// Reflection // Reflection
void Reflect(Reader& visitor, Position& value); void Reflect(Reader& visitor, Position& value);
void Reflect(Writer& visitor, Position& value); void Reflect(Writer& visitor, Position& value);

View File

@ -33,6 +33,8 @@ struct QueryLocation {
return result; return result;
} }
bool HasValue() const { return range.HasValue(); }
bool operator==(const QueryLocation& other) const { bool operator==(const QueryLocation& other) const {
return path == other.path && range == other.range; return path == other.path && range == other.range;
} }
@ -57,9 +59,6 @@ struct hash<::SymbolKind> {
}; };
} // namespace std } // namespace std
template <>
bool Maybe<QueryLocation>::has_value() const;
struct SymbolIdx { struct SymbolIdx {
SymbolKind kind; SymbolKind kind;
size_t idx; size_t idx;