changeset 23372:8a23ed65ef21

modernize token sources * token.h, token.cc: Use m_ prefix for member variables. Prefer member initialization over assignment in constructor body. Use named union and struct with constructors. Move class definition inside namespace. Declare member functions const where possible.
author John W. Eaton <jwe@octave.org>
date Fri, 07 Apr 2017 18:26:18 -0400
parents a5280a05b188
children 67a638ac7f24
files libinterp/parse-tree/token.cc libinterp/parse-tree/token.h
diffstat 2 files changed, 192 insertions(+), 182 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/token.cc	Fri Apr 07 15:31:37 2017 -0700
+++ b/libinterp/parse-tree/token.cc	Fri Apr 07 18:26:18 2017 -0400
@@ -32,164 +32,124 @@
 #include "token.h"
 #include "utils.h"
 
-token::token (int tv, int l, int c)
-{
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = generic_token;
-}
-
-token::token (int tv, bool is_kw, int l, int c)
-{
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = is_kw ? keyword_token : generic_token;
-}
-
-token::token (int tv, const char *s, int l, int c)
-{
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = string_token;
-  str = new std::string (s);
-}
-
-token::token (int tv, const std::string& s, int l, int c)
-{
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = string_token;
-  str = new std::string (s);
-}
-
-token::token (int tv, double d, const std::string& s, int l, int c)
-{
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = double_token;
-  num = d;
-  orig_text = s;
-}
-
-token::token (int tv, end_tok_type t, int l, int c)
-{
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = ettype_token;
-  et = t;
-}
-
-token::token (int tv, symbol_table::symbol_record *s, int l, int c)
-{
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = sym_rec_token;
-  sr = s;
-}
-
-token::token (int tv, const std::string& mth, const std::string& cls,
-              int l, int c)
+namespace octave
 {
-  maybe_cmd = false;
-  tspc = false;
-  line_num = l;
-  column_num = c;
-  tok_val = tv;
-  type_tag = scls_name_token;
-  sc.method_nm = new std::string (mth);
-  sc.class_nm = new std::string (cls);
-}
+  token::token (int tv, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (generic_token), m_tok_info (),
+      m_orig_text ()
+  { }
+
+  token::token (int tv, bool is_kw, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (is_kw ? keyword_token : generic_token),
+      m_tok_info (), m_orig_text ()
+  { }
 
-token::~token (void)
-{
-  if (type_tag == string_token)
-    delete str;
+  token::token (int tv, const char *s, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (string_token), m_tok_info (s),
+      m_orig_text ()
+  { }
+
+  token::token (int tv, const std::string& s, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (string_token), m_tok_info (s),
+      m_orig_text ()
+  { }
 
-  if (type_tag == scls_name_token)
-    {
-      delete sc.method_nm;
-      delete sc.class_nm;
-    }
-}
+  token::token (int tv, double d, const std::string& s, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (double_token), m_tok_info (d),
+      m_orig_text (s)
+  { }
+
+  token::token (int tv, end_tok_type t, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (ettype_token), m_tok_info (t),
+      m_orig_text ()
+  { }
 
-std::string
-token::text (void) const
-{
-  assert (type_tag == string_token);
-  return *str;
-}
+  token::token (int tv, symbol_table::symbol_record *sr, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (sym_rec_token), m_tok_info (sr),
+      m_orig_text ()
+  { }
 
-std::string
-token::symbol_name (void) const
-{
-  assert (type_tag == sym_rec_token);
-  return sr->name ();
-}
+  token::token (int tv, const std::string& method_nm,
+                const std::string& class_nm, int l, int c)
+    : m_maybe_cmd (false), m_tspc (false), m_line_num (l), m_column_num (c),
+      m_tok_val (tv), m_type_tag (scls_name_token),
+      m_tok_info (method_nm, class_nm), m_orig_text ()
+  { }
+
+  token::~token (void)
+  {
+    if (m_type_tag == string_token)
+      delete m_tok_info.m_str;
+
+    if (m_type_tag == scls_name_token)
+      delete m_tok_info.m_superclass_info;
+  }
 
-double
-token::number (void) const
-{
-  assert (type_tag == double_token);
-  return num;
-}
+  std::string
+  token::text (void) const
+  {
+    assert (m_type_tag == string_token);
+    return *m_tok_info.m_str;
+  }
 
-token::token_type
-token::ttype (void) const
-{
-  return type_tag;
-}
+  std::string
+  token::symbol_name (void) const
+  {
+    assert (m_type_tag == sym_rec_token);
+    return m_tok_info.m_sr->name ();
+  }
 
-token::end_tok_type
-token::ettype (void) const
-{
-  assert (type_tag == ettype_token);
-  return et;
-}
+  double
+  token::number (void) const
+  {
+    assert (m_type_tag == double_token);
+    return m_tok_info.m_num;
+  }
+
+  token::token_type
+  token::ttype (void) const
+  {
+    return m_type_tag;
+  }
 
-symbol_table::symbol_record *
-token::sym_rec (void)
-{
-  assert (type_tag == sym_rec_token);
-  return sr;
-}
+  token::end_tok_type
+  token::ettype (void) const
+  {
+    assert (m_type_tag == ettype_token);
+    return m_tok_info.m_et;
+  }
+
+  symbol_table::symbol_record *
+  token::sym_rec (void) const
+  {
+    assert (m_type_tag == sym_rec_token);
+    return m_tok_info.m_sr;
+  }
 
-std::string
-token::superclass_method_name (void)
-{
-  assert (type_tag == scls_name_token);
-  return *sc.method_nm;
+  std::string
+  token::superclass_method_name (void) const
+  {
+    assert (m_type_tag == scls_name_token);
+    return m_tok_info.m_superclass_info->m_method_nm;
+  }
+
+  std::string
+  token::superclass_class_name (void) const
+  {
+    assert (m_type_tag == scls_name_token);
+    return m_tok_info.m_superclass_info->m_class_nm;
+  }
+
+  std::string
+  token::text_rep (void) const
+  {
+    return m_orig_text;
+  }
 }
-
-std::string
-token::superclass_class_name (void)
-{
-  assert (type_tag == scls_name_token);
-  return *sc.class_nm;
-}
-
-std::string
-token::text_rep (void)
-{
-  return orig_text;
-}
--- a/libinterp/parse-tree/token.h	Fri Apr 07 15:31:37 2017 -0700
+++ b/libinterp/parse-tree/token.h	Fri Apr 07 18:26:18 2017 -0400
@@ -78,32 +78,32 @@
 
     // No copying!
 
-    token (const token& tok) = delete;
+    token (const token&) = delete;
 
-    token& operator = (const token& tok) = delete;
+    token& operator = (const token&) = delete;
 
     ~token (void);
 
-    void mark_may_be_command (void) { maybe_cmd = true; }
-    bool may_be_command (void) const { return maybe_cmd; }
+    void mark_may_be_command (void) { m_maybe_cmd = true; }
+    bool may_be_command (void) const { return m_maybe_cmd; }
 
-    void mark_trailing_space (void) { tspc = true; }
-    bool space_follows_token (void) const { return tspc; }
+    void mark_trailing_space (void) { m_tspc = true; }
+    bool space_follows_token (void) const { return m_tspc; }
 
-    int token_value (void) const { return tok_val; }
-    bool token_value_is (int tv) const { return tv == tok_val; }
+    int token_value (void) const { return m_tok_val; }
+    bool token_value_is (int tv) const { return tv == m_tok_val; }
 
-    int line (void) const { return line_num; }
-    int column (void) const { return column_num; }
+    int line (void) const { return m_line_num; }
+    int column (void) const { return m_column_num; }
 
     bool is_keyword (void) const
     {
-      return type_tag == keyword_token || type_tag == ettype_token;
+      return m_type_tag == keyword_token || m_type_tag == ettype_token;
     }
 
     bool is_symbol (void) const
     {
-      return type_tag == sym_rec_token;
+      return m_type_tag == sym_rec_token;
     }
 
     std::string text (void) const;
@@ -111,34 +111,84 @@
     double number (void) const;
     token_type ttype (void) const;
     end_tok_type ettype (void) const;
-    symbol_table::symbol_record *sym_rec (void);
+    symbol_table::symbol_record *sym_rec (void) const;
 
-    std::string superclass_method_name (void);
-    std::string superclass_class_name (void);
+    std::string superclass_method_name (void) const;
+    std::string superclass_class_name (void) const;
 
-    std::string text_rep (void);
+    std::string text_rep (void) const;
 
   private:
 
-    bool maybe_cmd;
-    bool tspc;
-    int line_num;
-    int column_num;
-    int tok_val;
-    token_type type_tag;
-    union
+    bool m_maybe_cmd;
+
+    bool m_tspc;
+
+    int m_line_num;
+
+    int m_column_num;
+
+    int m_tok_val;
+
+    token_type m_type_tag;
+
+    union tok_info
     {
-      std::string *str;
-      double num;
-      end_tok_type et;
-      symbol_table::symbol_record *sr;
-      struct
+      tok_info (void) { }
+
+      tok_info (const char *s) : m_str (new std::string (s)) { }
+
+      tok_info (const std::string& str) : m_str (new std::string (str)) { }
+
+      tok_info (double num) : m_num (num) { }
+
+      tok_info (end_tok_type et) : m_et (et) { }
+
+      tok_info (symbol_table::symbol_record *sr) : m_sr (sr) { }
+
+      tok_info (const std::string& method_nm, const std::string& class_nm)
+        : m_superclass_info (new superclass_info (method_nm, class_nm))
+      { }
+
+      tok_info (const tok_info&) = delete;
+
+      tok_info& operator = (const tok_info&) = delete;
+
+      ~tok_info (void) { }
+
+      std::string *m_str;
+
+      double m_num;
+
+      end_tok_type m_et;
+
+      symbol_table::symbol_record *m_sr;
+
+      struct superclass_info
       {
-        std::string *method_nm;
-        std::string *class_nm;
-      } sc;
+        superclass_info (void) = delete;
+
+        superclass_info (const std::string& method_nm,
+                         const std::string& class_nm)
+          : m_method_nm (method_nm), m_class_nm (class_nm)
+        { }
+
+        superclass_info (const superclass_info&) = delete;
+
+        superclass_info& operator = (const superclass_info&) = delete;
+
+        ~superclass_info (void) = default;
+
+        std::string m_method_nm;
+        std::string m_class_nm;
+      };
+
+      superclass_info *m_superclass_info;
     };
-    std::string orig_text;
+
+    tok_info m_tok_info;
+
+    std::string m_orig_text;
   };
 }