Просмотр исходного кода

Move to by-value friend comparison operator definitions. (#222)

This is often a (much) better option for this code as we have many types
that are just an integer, pointer, or enum of data that would be much
more efficient as a by-value parameter. It is also a cleaner design in
most cases.

One place can't migrate in this way: iterators that use the LLVM CRTP
library for filling out iterator methods. The CRTP dispatch expects
operators to be actual members, and so those are left as-is in this
patch.
Chandler Carruth 5 лет назад
Родитель
Сommit
8f9609c53b
3 измененных файлов с 40 добавлено и 32 удалено
  1. 4 4
      lexer/token_kind.h
  2. 32 24
      lexer/tokenized_buffer.h
  3. 4 4
      parser/parse_node_kind.h

+ 4 - 4
lexer/token_kind.h

@@ -33,11 +33,11 @@ class TokenKind {
   // constructed using the above factory functions for each unique kind.
   TokenKind() = delete;
 
-  auto operator==(const TokenKind& rhs) const -> bool {
-    return kind_value == rhs.kind_value;
+  friend auto operator==(TokenKind lhs, TokenKind rhs) -> bool {
+    return lhs.kind_value == rhs.kind_value;
   }
-  auto operator!=(const TokenKind& rhs) const -> bool {
-    return kind_value != rhs.kind_value;
+  friend auto operator!=(TokenKind lhs, TokenKind rhs) -> bool {
+    return lhs.kind_value != rhs.kind_value;
   }
 
   // Get a friendly name for the token for logging or debugging.

+ 32 - 24
lexer/tokenized_buffer.h

@@ -47,19 +47,23 @@ class TokenizedBuffer {
    public:
     Token() = default;
 
-    auto operator==(const Token& rhs) const -> bool {
-      return index == rhs.index;
+    friend auto operator==(Token lhs, Token rhs) -> bool {
+      return lhs.index == rhs.index;
     }
-    auto operator!=(const Token& rhs) const -> bool {
-      return index != rhs.index;
+    friend auto operator!=(Token lhs, Token rhs) -> bool {
+      return lhs.index != rhs.index;
     }
-    auto operator<(const Token& rhs) const -> bool { return index < rhs.index; }
-    auto operator<=(const Token& rhs) const -> bool {
-      return index <= rhs.index;
+    friend auto operator<(Token lhs, Token rhs) -> bool {
+      return lhs.index < rhs.index;
     }
-    auto operator>(const Token& rhs) const -> bool { return index > rhs.index; }
-    auto operator>=(const Token& rhs) const -> bool {
-      return index >= rhs.index;
+    friend auto operator<=(Token lhs, Token rhs) -> bool {
+      return lhs.index <= rhs.index;
+    }
+    friend auto operator>(Token lhs, Token rhs) -> bool {
+      return lhs.index > rhs.index;
+    }
+    friend auto operator>=(Token lhs, Token rhs) -> bool {
+      return lhs.index >= rhs.index;
     }
 
    private:
@@ -85,19 +89,23 @@ class TokenizedBuffer {
    public:
     Line() = default;
 
-    auto operator==(const Line& rhs) const -> bool {
-      return index == rhs.index;
+    friend auto operator==(Line lhs, Line rhs) -> bool {
+      return lhs.index == rhs.index;
+    }
+    friend auto operator!=(Line lhs, Line rhs) -> bool {
+      return lhs.index != rhs.index;
+    }
+    friend auto operator<(Line lhs, Line rhs) -> bool {
+      return lhs.index < rhs.index;
     }
-    auto operator!=(const Line& rhs) const -> bool {
-      return index != rhs.index;
+    friend auto operator<=(Line lhs, Line rhs) -> bool {
+      return lhs.index <= rhs.index;
     }
-    auto operator<(const Line& rhs) const -> bool { return index < rhs.index; }
-    auto operator<=(const Line& rhs) const -> bool {
-      return index <= rhs.index;
+    friend auto operator>(Line lhs, Line rhs) -> bool {
+      return lhs.index > rhs.index;
     }
-    auto operator>(const Line& rhs) const -> bool { return index > rhs.index; }
-    auto operator>=(const Line& rhs) const -> bool {
-      return index >= rhs.index;
+    friend auto operator>=(Line lhs, Line rhs) -> bool {
+      return lhs.index >= rhs.index;
     }
 
    private:
@@ -125,11 +133,11 @@ class TokenizedBuffer {
 
     // Most normal APIs are provided by the `TokenizedBuffer`, we just support
     // basic comparison operations.
-    auto operator==(const Identifier& rhs) const -> bool {
-      return index == rhs.index;
+    friend auto operator==(Identifier lhs, Identifier rhs) -> bool {
+      return lhs.index == rhs.index;
     }
-    auto operator!=(const Identifier& rhs) const -> bool {
-      return index != rhs.index;
+    friend auto operator!=(Identifier lhs, Identifier rhs) -> bool {
+      return lhs.index != rhs.index;
     }
 
    private:

+ 4 - 4
parser/parse_node_kind.h

@@ -44,11 +44,11 @@ class ParseNodeKind {
   // constructed using the above factory functions for each unique kind.
   ParseNodeKind() = delete;
 
-  auto operator==(const ParseNodeKind& rhs) const -> bool {
-    return kind == rhs.kind;
+  friend auto operator==(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
+    return lhs.kind == rhs.kind;
   }
-  auto operator!=(const ParseNodeKind& rhs) const -> bool {
-    return kind != rhs.kind;
+  friend auto operator!=(ParseNodeKind lhs, ParseNodeKind rhs) -> bool {
+    return lhs.kind != rhs.kind;
   }
 
   // Gets a friendly name for the token for logging or debugging.