From 3c6f3f661ffdff91ac161d934128926873462325 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 1 Feb 2018 22:31:43 -0800 Subject: [PATCH] Use Maybe> 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) --- src/indexer.h | 10 ++++++---- src/maybe.h | 4 +++- src/position.cc | 10 ---------- src/position.h | 7 ++----- src/query.h | 5 ++--- 5 files changed, 13 insertions(+), 23 deletions(-) diff --git a/src/indexer.h b/src/indexer.h index f5b148a9..4db78fbd 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -40,12 +40,14 @@ template struct Id { size_t id; - Id() : id(0) {} // Needed for containers. Do not use directly. + Id() : id(-1) {} // Needed for containers and Maybe. Do not use directly. explicit Id(size_t id) : id(id) {} // Needed for google::dense_hash_map. explicit operator size_t() const { return id; } + bool HasValue() const { return id != size_t(-1); } + bool operator==(const Id& other) const { return id == other.id; } bool operator<(const Id& 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 // type comes from a using or typedef statement). - optional alias_of; + Maybe alias_of; // Immediate parent types. std::vector parents; @@ -269,7 +271,7 @@ struct FuncDefDefinitionData { Maybe definition_extent; // Type which declares this one (ie, it is a method) - optional declaring_type; + Maybe declaring_type; // Method this method overrides. std::vector base; @@ -395,7 +397,7 @@ struct VarDefDefinitionData { Maybe definition_extent; // Type of the variable. - optional variable_type; + Maybe variable_type; // Function/type which declares this one. size_t parent_id = size_t(-1); diff --git a/src/maybe.h b/src/maybe.h index 19628151..0b1d2712 100644 --- a/src/maybe.h +++ b/src/maybe.h @@ -25,7 +25,9 @@ public: const T& operator*() const { return storage; } T& operator*() { return storage; } - bool has_value() const; + bool has_value() const { + return storage.HasValue(); + } explicit operator bool() const { return has_value(); } operator optional() const { if (has_value()) diff --git a/src/position.cc b/src/position.cc index d750793f..24db6909 100644 --- a/src/position.cc +++ b/src/position.cc @@ -146,16 +146,6 @@ bool Range::operator<(const Range& that) const { return end < that.end; } -template <> -bool Maybe::has_value() const { - return storage.line >= 0; -} - -template <> -bool Maybe::has_value() const { - return storage.start.line >= 0; -} - // Position void Reflect(Reader& visitor, Position& value) { if (visitor.Format() == SerializeFormat::Json) { diff --git a/src/position.h b/src/position.h index 123320c2..b01f696e 100644 --- a/src/position.h +++ b/src/position.h @@ -15,6 +15,7 @@ struct Position { Position(int16_t line, int16_t column); explicit Position(const char* encoded); + bool HasValue() const { return line >= 0; } std::string ToString(); std::string ToPrettyString(const std::string& filename); @@ -38,6 +39,7 @@ struct Range { Range(Position start, Position end); explicit Range(const char* encoded); + bool HasValue() const { return start.HasValue(); } bool Contains(int line, int column) const; Range RemovePrefix(Position position) const; @@ -49,11 +51,6 @@ struct Range { }; MAKE_HASHABLE(Range, t.start, t.end); -template <> -bool Maybe::has_value() const; -template <> -bool Maybe::has_value() const; - // Reflection void Reflect(Reader& visitor, Position& value); void Reflect(Writer& visitor, Position& value); diff --git a/src/query.h b/src/query.h index 6d05db7c..1e9efca8 100644 --- a/src/query.h +++ b/src/query.h @@ -33,6 +33,8 @@ struct QueryLocation { return result; } + bool HasValue() const { return range.HasValue(); } + bool operator==(const QueryLocation& other) const { return path == other.path && range == other.range; } @@ -57,9 +59,6 @@ struct hash<::SymbolKind> { }; } // namespace std -template <> -bool Maybe::has_value() const; - struct SymbolIdx { SymbolKind kind; size_t idx;