Start automatically generating operator<<s for enums.

Change-Id: I0f53db089b9a1ba38ce82b75ab22448877be67e0
diff --git a/build/Android.common.mk b/build/Android.common.mk
index 41edefa..28ac65b 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -104,7 +104,6 @@
 LIBART_COMMON_SRC_FILES := \
 	src/atomic.cc.arm \
 	src/card_table.cc \
-	src/constants.cc \
 	src/check_jni.cc \
 	src/class_linker.cc \
 	src/class_loader.cc \
@@ -268,6 +267,12 @@
 $(error unsupported HOST_ARCH=$(HOST_ARCH))
 endif # HOST_ARCH != x86
 
+LIBART_ENUM_OPERATOR_OUT_HEADER_FILES := \
+	src/indirect_reference_table.h \
+	src/instruction_set.h \
+	src/invoke_type.h \
+	src/mutex.h
+
 LIBARTTEST_COMMON_SRC_FILES := \
 	test/StackWalk/stack_walk_jni.cc \
 	test/ReferenceMap/stack_walk_refmap_jni.cc
diff --git a/build/Android.libart.mk b/build/Android.libart.mk
index 5406c9e..f14f934 100644
--- a/build/Android.libart.mk
+++ b/build/Android.libart.mk
@@ -54,8 +54,20 @@
     LOCAL_SRC_FILES := $(LIBART_TARGET_SRC_FILES)
   else # host
     LOCAL_SRC_FILES := $(LIBART_HOST_SRC_FILES)
+    LOCAL_IS_HOST_MODULE := true
   endif
 
+  GENERATED_SRC_DIR := $$(call intermediates-dir-for,$$(LOCAL_MODULE_CLASS),$$(LOCAL_MODULE),$$(LOCAL_IS_HOST_MODULE),)
+  ENUM_OPERATOR_OUT_CC_FILES := $$(patsubst %.h,%_operator_out.cc,$$(LIBART_ENUM_OPERATOR_OUT_HEADER_FILES))
+  ENUM_OPERATOR_OUT_GEN := $$(addprefix $$(GENERATED_SRC_DIR)/,$$(ENUM_OPERATOR_OUT_CC_FILES))
+
+$$(ENUM_OPERATOR_OUT_GEN): art/tools/generate-operator-out.py
+$$(ENUM_OPERATOR_OUT_GEN): PRIVATE_CUSTOM_TOOL = art/tools/generate-operator-out.py $$< > $$@
+$$(ENUM_OPERATOR_OUT_GEN): $$(GENERATED_SRC_DIR)/%_operator_out.cc : $(LOCAL_PATH)/%.h
+	$$(transform-generated-source)
+
+  LOCAL_GENERATED_SOURCES += $$(ENUM_OPERATOR_OUT_GEN)
+
   LOCAL_CFLAGS := $(LIBART_CFLAGS)
   ifeq ($$(art_target_or_host),target)
     LOCAL_CFLAGS += $(ART_TARGET_CFLAGS)
@@ -102,7 +114,6 @@
     endif
     include $(BUILD_SHARED_LIBRARY)
   else # host
-    LOCAL_IS_HOST_MODULE := true
     ifeq ($(ART_USE_LLVM_COMPILER),true)
       include $(LLVM_GEN_INTRINSICS_MK)
       include $(LLVM_HOST_BUILD_MK)
diff --git a/src/constants.cc b/src/constants.cc
deleted file mode 100644
index 12632eb..0000000
--- a/src/constants.cc
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright (C) 2012 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-// TODO: automatically generate operator<<s for enum types.
-
-#include <iostream>
-
-#include "instruction_set.h"
-#include "invoke_type.h"
-
-namespace art {
-
-std::ostream& operator<<(std::ostream& os, const InstructionSet& rhs) {
-  switch (rhs) {
-    case kNone: os << "none"; break;
-    case kArm: os << "ARM"; break;
-    case kThumb2: os << "Thumb2"; break;
-    case kX86: os << "x86"; break;
-    case kMips: os << "MIPS"; break;
-    default: os << "InstructionSet[" << static_cast<int>(rhs) << "]"; break;
-  }
-  return os;
-}
-
-std::ostream& operator<<(std::ostream& os, const InvokeType& rhs) {
-  switch (rhs) {
-    case kStatic: os << "static"; break;
-    case kDirect: os << "direct"; break;
-    case kVirtual: os << "virtual"; break;
-    case kSuper: os << "super"; break;
-    case kInterface: os << "interface"; break;
-    default: os << "InvokeType[" << static_cast<int>(rhs) << "]"; break;
-  }
-  return os;
-}
-
-}  // namespace art
diff --git a/src/indirect_reference_table.cc b/src/indirect_reference_table.cc
index ac5e402..14d804b 100644
--- a/src/indirect_reference_table.cc
+++ b/src/indirect_reference_table.cc
@@ -329,27 +329,6 @@
   }
 }
 
-std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs) {
-  switch (rhs) {
-  case kSirtOrInvalid:
-    os << "stack indirect reference table or invalid reference";
-    break;
-  case kLocal:
-    os << "local reference";
-    break;
-  case kGlobal:
-    os << "global reference";
-    break;
-  case kWeakGlobal:
-    os << "weak global reference";
-    break;
-  default:
-    os << "IndirectRefKind[" << static_cast<int>(rhs) << "]";
-    break;
-  }
-  return os;
-}
-
 void IndirectReferenceTable::Dump() const {
   LOG(WARNING) << kind_ << " table dump:";
   std::vector<const Object*> entries(table_, table_ + Capacity());
diff --git a/src/indirect_reference_table.h b/src/indirect_reference_table.h
index b1f6d8c..b0a0d64 100644
--- a/src/indirect_reference_table.h
+++ b/src/indirect_reference_table.h
@@ -106,12 +106,12 @@
  * For convenience these match up with enum jobjectRefType from jni.h.
  */
 enum IndirectRefKind {
-    kSirtOrInvalid = 0,
-    kLocal         = 1,
-    kGlobal        = 2,
-    kWeakGlobal    = 3
+  kSirtOrInvalid = 0, // <<stack indirect reference table or invalid reference>>
+  kLocal         = 1, // <<local reference>>
+  kGlobal        = 2, // <<global reference>>
+  kWeakGlobal    = 3  // <<weak global reference>>
 };
-std::ostream& operator<<(std::ostream& os, IndirectRefKind rhs);
+std::ostream& operator<<(std::ostream& os, const IndirectRefKind& rhs);
 
 /*
  * Determine what kind of indirect reference this is.
diff --git a/src/invoke_type.h b/src/invoke_type.h
index f37f1b4..d724fdb 100644
--- a/src/invoke_type.h
+++ b/src/invoke_type.h
@@ -22,7 +22,11 @@
 namespace art {
 
 enum InvokeType {
-  kStatic, kDirect, kVirtual, kSuper, kInterface,
+  kStatic,    // <<static>>
+  kDirect,    // <<direct>>
+  kVirtual,   // <<virtual>>
+  kSuper,     // <<super>>
+  kInterface, // <<interface>>
   kMaxInvokeType = kInterface
 };
 
diff --git a/src/jdwp/jdwp.h b/src/jdwp/jdwp.h
index d297f6f..7d8bf22 100644
--- a/src/jdwp/jdwp.h
+++ b/src/jdwp/jdwp.h
@@ -87,8 +87,8 @@
  */
 enum JdwpTransportType {
   kJdwpTransportUnknown = 0,
-  kJdwpTransportSocket,       /* transport=dt_socket */
-  kJdwpTransportAndroidAdb,   /* transport=dt_android_adb */
+  kJdwpTransportSocket,       // transport=dt_socket
+  kJdwpTransportAndroidAdb,   // transport=dt_android_adb
 };
 std::ostream& operator<<(std::ostream& os, const JdwpTransportType& rhs);
 
diff --git a/src/mutex.cc b/src/mutex.cc
index 1e30543..b0b82f3 100644
--- a/src/mutex.cc
+++ b/src/mutex.cc
@@ -194,14 +194,4 @@
   }
 }
 
-std::ostream& operator<<(std::ostream& os, const MutexRank& rhs) {
-  switch (rhs) {
-    case kHeapLock: os << "HeapLock"; break;
-    case kThreadListLock: os << "ThreadListLock"; break;
-    case kThreadSuspendCountLock: os << "ThreadSuspendCountLock"; break;
-    default: os << "MutexRank[" << static_cast<int>(rhs) << "]"; break;
-  }
-  return os;
-}
-
 }  // namespace
diff --git a/tools/generate-operator-out.py b/tools/generate-operator-out.py
new file mode 100755
index 0000000..b1d102e
--- /dev/null
+++ b/tools/generate-operator-out.py
@@ -0,0 +1,150 @@
+#!/usr/bin/python2.4
+#
+# Copyright 2012 Google Inc. All Rights Reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#    * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#    * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#    * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""Generates default implementations of operator<< for enum types."""
+
+import codecs
+import os
+import re
+import string
+import sys
+
+
+_ENUM_START_RE = re.compile(r'\benum\b\s+(\S+)\s+\{')
+_ENUM_VALUE_RE = re.compile(r'([A-Za-z0-9_]+)(.*)')
+_ENUM_END_RE = re.compile(r'^\s*\};$')
+_ENUMS = {}
+
+def Confused(filename, line_number, line):
+  sys.stderr.write('%s:%d: confused by:\n%s\n' % (filename, line_number, line))
+  sys.exit(1)
+
+
+def ProcessFile(filename):
+  lines = codecs.open(filename, 'r', 'utf8', 'replace').read().split('\n')
+  in_enum = False
+  line_number = 0
+  for raw_line in lines:
+    line_number += 1
+
+    # TODO: take namespaces and enclosing classes/structs into account.
+
+    # Is this the start of a new enum?
+    if not in_enum:
+      m = _ENUM_START_RE.search(raw_line)
+      if m:
+        # Yes, so add an empty entry to _ENUMS for this enum.
+        enum_name = m.group(1)
+        _ENUMS[enum_name] = []
+        in_enum = True
+      continue
+
+    # Is this the end of the current enum?
+    m = _ENUM_END_RE.search(raw_line)
+    if m:
+      if not in_enum:
+        Confused(filename, line_number, raw_line)
+      in_enum = False
+      continue
+
+    # Is this another enum value?
+    m = _ENUM_VALUE_RE.search(raw_line.strip())
+    if not m:
+      Confused(filename, line_number, raw_line)
+
+    enum_value = m.group(1)
+
+    # By default, we turn "kSomeValue" into "SomeValue".
+    enum_text = enum_value
+    if enum_text.startswith('k'):
+      enum_text = enum_text[1:]
+
+    # Lose literal values because we don't care; turn "= 123, // blah" into ", // blah".
+    rest = m.group(2).strip()
+    m_literal = re.compile(r'= (0x[0-9a-f]+|-?[0-9]+)').search(rest)
+    if m_literal:
+      rest = rest[(len(m_literal.group(0))):]
+
+    # With "kSomeValue = kOtherValue," we take the original and skip later synonyms.
+    # TODO: check that the rhs is actually an existing value.
+    if rest.startswith('= k'):
+      continue
+
+    # Remove any trailing comma and whitespace
+    if rest.startswith(','):
+      rest = rest[1:]
+    rest = rest.strip()
+
+    # Anything left should be a comment.
+    if len(rest) and not rest.startswith('// '):
+      print rest
+      Confused(filename, line_number, raw_line)
+
+    m_comment = re.compile(r'<<(.*?)>>').search(rest)
+    if m_comment:
+      enum_text = m_comment.group(1)
+
+    _ENUMS[enum_name].append((enum_value, enum_text))
+
+def main():
+  header_files = []
+  for header_file in sys.argv[1:]:
+    header_files.append(header_file)
+    ProcessFile(header_file)
+
+  print '#include <iostream>'
+  print
+
+  for header_file in header_files:
+    print '#include "%s"' % header_file
+
+  print
+  print 'namespace art {'
+  print
+
+  for enum_name in _ENUMS:
+    print '// This was automatically generated by %s --- do not edit!' % sys.argv[0]
+    print 'std::ostream& operator<<(std::ostream& os, const %s& rhs) {' % enum_name
+    print '  switch (rhs) {'
+    for (enum_value, enum_text) in _ENUMS[enum_name]:
+      print '    case %s: os << "%s"; break;' % (enum_value, enum_text)
+    print '    default: os << "%s[" << static_cast<int>(rhs) << "]"; break;' % enum_name
+    print '  }'
+    print '  return os;'
+    print '}'
+    print
+
+  print '} // namespace art'
+
+  sys.exit(0)
+
+
+if __name__ == '__main__':
+  main()