Change dex cache to be java object instead of array, add pointer to dex file in dex cache.

Generic clean up to facilitate having GDB macros for Pretty* helper functions.

Improved cleanliness of DexCache since having it as an object array was not the best solution.

Fixed a bug in InOrderWalk caused by ResolveType sometimes allocating classes.

Rename C++ Method to AbstractMethod and add two new classes Constructor, Method which both inherit from AbstractMethod.

Rename done to have the C++ code be closer to the java code.

Change-Id: I4995b4c5e47a3822192b08afa24a639d3b1f4da9
diff --git a/src/debugger.cc b/src/debugger.cc
index 87ad446..8b87945 100644
--- a/src/debugger.cc
+++ b/src/debugger.cc
@@ -95,7 +95,7 @@
 };
 
 struct AllocRecordStackTraceElement {
-  Method* method;
+  AbstractMethod* method;
   uint32_t dex_pc;
 
   int32_t LineNumber() const SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
@@ -119,9 +119,9 @@
 };
 
 struct Breakpoint {
-  Method* method;
+  AbstractMethod* method;
   uint32_t dex_pc;
-  Breakpoint(Method* method, uint32_t dex_pc) : method(method), dex_pc(dex_pc) {}
+  Breakpoint(AbstractMethod* method, uint32_t dex_pc) : method(method), dex_pc(dex_pc) {}
 };
 
 static std::ostream& operator<<(std::ostream& os, const Breakpoint& rhs)
@@ -138,7 +138,7 @@
   JDWP::JdwpStepSize step_size;
   JDWP::JdwpStepDepth step_depth;
 
-  const Method* method;
+  const AbstractMethod* method;
   int32_t line_number; // Or -1 for native methods.
   std::set<uint32_t> dex_pcs;
   int stack_depth;
@@ -181,7 +181,7 @@
 static std::vector<Breakpoint> gBreakpoints GUARDED_BY(gBreakpointsLock);
 static SingleStepControl gSingleStepControl GUARDED_BY(gBreakpointsLock);
 
-static bool IsBreakpoint(Method* m, uint32_t dex_pc)
+static bool IsBreakpoint(AbstractMethod* m, uint32_t dex_pc)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   MutexLock mu(gBreakpointsLock);
   for (size_t i = 0; i < gBreakpoints.size(); ++i) {
@@ -928,7 +928,7 @@
 #endif
 }
 
-static JDWP::MethodId ToMethodId(const Method* m)
+static JDWP::MethodId ToMethodId(const AbstractMethod* m)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
 #ifdef MOVING_GARBAGE_COLLECTOR
   UNIMPLEMENTED(FATAL);
@@ -946,16 +946,16 @@
 #endif
 }
 
-static Method* FromMethodId(JDWP::MethodId mid)
+static AbstractMethod* FromMethodId(JDWP::MethodId mid)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
 #ifdef MOVING_GARBAGE_COLLECTOR
   UNIMPLEMENTED(FATAL);
 #else
-  return reinterpret_cast<Method*>(static_cast<uintptr_t>(mid));
+  return reinterpret_cast<AbstractMethod*>(static_cast<uintptr_t>(mid));
 #endif
 }
 
-static void SetLocation(JDWP::JdwpLocation& location, Method* m, uint32_t dex_pc)
+static void SetLocation(JDWP::JdwpLocation& location, AbstractMethod* m, uint32_t dex_pc)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   if (m == NULL) {
     memset(&location, 0, sizeof(location));
@@ -970,7 +970,7 @@
 
 std::string Dbg::GetMethodName(JDWP::RefTypeId, JDWP::MethodId methodId)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
-  Method* m = FromMethodId(methodId);
+  AbstractMethod* m = FromMethodId(methodId);
   return MethodHelper(m).GetName();
 }
 
@@ -1012,7 +1012,7 @@
   return newSlot;
 }
 
-static uint16_t DemangleSlot(uint16_t slot, Method* m)
+static uint16_t DemangleSlot(uint16_t slot, AbstractMethod* m)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   if (slot == kEclipseWorkaroundSlot) {
     return 0;
@@ -1065,7 +1065,7 @@
   expandBufAdd4BE(pReply, direct_method_count + virtual_method_count);
 
   for (size_t i = 0; i < direct_method_count + virtual_method_count; ++i) {
-    Method* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
+    AbstractMethod* m = (i < direct_method_count) ? c->GetDirectMethod(i) : c->GetVirtualMethod(i - direct_method_count);
     MethodHelper mh(m);
     expandBufAddMethodId(pReply, ToMethodId(m));
     expandBufAddUtf8String(pReply, mh.GetName());
@@ -1109,7 +1109,7 @@
       return true;
     }
   };
-  Method* m = FromMethodId(methodId);
+  AbstractMethod* m = FromMethodId(methodId);
   MethodHelper mh(m);
   uint64_t start, end;
   if (m->IsNative()) {
@@ -1163,7 +1163,7 @@
       ++pContext->variable_count;
     }
   };
-  Method* m = FromMethodId(methodId);
+  AbstractMethod* m = FromMethodId(methodId);
   MethodHelper mh(m);
   const DexFile::CodeItem* code_item = mh.GetCodeItem();
 
@@ -1676,7 +1676,7 @@
     if (frame_id != GetFrameId()) {
       return true;  // continue
     }
-    Method* m = GetMethod();
+    AbstractMethod* m = GetMethod();
     if (m->IsNative() || m->IsStatic()) {
       this_object = NULL;
     } else {
@@ -1690,7 +1690,7 @@
   JDWP::FrameId frame_id;
 };
 
-static Object* GetThis(Thread* self, Method* m, size_t frame_id)
+static Object* GetThis(Thread* self, AbstractMethod* m, size_t frame_id)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   // TODO: should we return the 'this' we passed through to non-static native methods?
   if (m->IsNative() || m->IsStatic()) {
@@ -1742,7 +1742,7 @@
         return true;  // Not our frame, carry on.
       }
       // TODO: check that the tag is compatible with the actual type of the slot!
-      Method* m = GetMethod();
+      AbstractMethod* m = GetMethod();
       uint16_t reg = DemangleSlot(slot_, m);
 
       switch (tag_) {
@@ -1861,7 +1861,7 @@
         return true;  // Not our frame, carry on.
       }
       // TODO: check that the tag is compatible with the actual type of the slot!
-      Method* m = GetMethod();
+      AbstractMethod* m = GetMethod();
       uint16_t reg = DemangleSlot(slot_, m);
 
       switch (tag_) {
@@ -1920,7 +1920,7 @@
   visitor.WalkStack();
 }
 
-void Dbg::PostLocationEvent(const Method* m, int dex_pc, Object* this_object, int event_flags) {
+void Dbg::PostLocationEvent(const AbstractMethod* m, int dex_pc, Object* this_object, int event_flags) {
   Class* c = m->GetDeclaringClass();
 
   JDWP::JdwpLocation location;
@@ -1942,8 +1942,8 @@
 }
 
 void Dbg::PostException(Thread* thread,
-                        JDWP::FrameId throw_frame_id, Method* throw_method, uint32_t throw_dex_pc,
-                        Method* catch_method, uint32_t catch_dex_pc, Throwable* exception) {
+                        JDWP::FrameId throw_frame_id, AbstractMethod* throw_method, uint32_t throw_dex_pc,
+                        AbstractMethod* catch_method, uint32_t catch_dex_pc, Throwable* exception) {
   if (!IsDebuggerActive()) {
     return;
   }
@@ -1993,7 +1993,7 @@
   }
 
   size_t frame_id;
-  Method* m = self->GetCurrentMethod(NULL, &frame_id);
+  AbstractMethod* m = self->GetCurrentMethod(NULL, &frame_id);
   //LOG(INFO) << "UpdateDebugger " << PrettyMethod(m) << "@" << dex_pc << " frame " << frame_id;
 
   if (dex_pc == -1) {
@@ -2097,14 +2097,14 @@
 
 void Dbg::WatchLocation(const JDWP::JdwpLocation* location) {
   MutexLock mu(gBreakpointsLock);
-  Method* m = FromMethodId(location->method_id);
+  AbstractMethod* m = FromMethodId(location->method_id);
   gBreakpoints.push_back(Breakpoint(m, location->dex_pc));
   VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": " << gBreakpoints[gBreakpoints.size() - 1];
 }
 
 void Dbg::UnwatchLocation(const JDWP::JdwpLocation* location) {
   MutexLock mu(gBreakpointsLock);
-  Method* m = FromMethodId(location->method_id);
+  AbstractMethod* m = FromMethodId(location->method_id);
   for (size_t i = 0; i < gBreakpoints.size(); ++i) {
     if (gBreakpoints[i].method == m && gBreakpoints[i].dex_pc == location->dex_pc) {
       VLOG(jdwp) << "Removed breakpoint #" << i << ": " << gBreakpoints[i];
@@ -2150,7 +2150,7 @@
     // annotalysis.
     bool VisitFrame() NO_THREAD_SAFETY_ANALYSIS {
       gBreakpointsLock.AssertHeld();
-      const Method* m = GetMethod();
+      const AbstractMethod* m = GetMethod();
       if (!m->IsRuntimeMethod()) {
         ++gSingleStepControl.stack_depth;
         if (gSingleStepControl.method == NULL) {
@@ -2215,7 +2215,7 @@
     uint32_t last_pc;
   };
   gSingleStepControl.dex_pcs.clear();
-  const Method* m = gSingleStepControl.method;
+  const AbstractMethod* m = gSingleStepControl.method;
   if (m->IsNative()) {
     gSingleStepControl.line_number = -1;
   } else {
@@ -2352,7 +2352,7 @@
       return status;
     }
 
-    Method* m = FromMethodId(methodId);
+    AbstractMethod* m = FromMethodId(methodId);
     if (m->IsStatic() != (receiver == NULL)) {
       return JDWP::ERR_INVALID_METHODID;
     }
@@ -2461,9 +2461,9 @@
   soa.Self()->ClearException();
 
   // Translate the method through the vtable, unless the debugger wants to suppress it.
-  Method* m = pReq->method_;
+  AbstractMethod* m = pReq->method_;
   if ((pReq->options_ & JDWP::INVOKE_NONVIRTUAL) == 0 && pReq->receiver_ != NULL) {
-    Method* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_);
+    AbstractMethod* actual_method = pReq->class_->FindVirtualMethodForVirtualOrInterface(pReq->method_);
     if (actual_method != m) {
       VLOG(jdwp) << "ExecuteMethod translated " << PrettyMethod(m) << " to " << PrettyMethod(actual_method);
       m = actual_method;
@@ -3128,7 +3128,7 @@
     if (depth >= kMaxAllocRecordStackDepth) {
       return false;
     }
-    Method* m = GetMethod();
+    AbstractMethod* m = GetMethod();
     if (!m->IsRuntimeMethod()) {
       record->stack[depth].method = m;
       record->stack[depth].dex_pc = GetDexPc();
@@ -3211,7 +3211,7 @@
               << PrettyClass(record->type);
 
     for (size_t stack_frame = 0; stack_frame < kMaxAllocRecordStackDepth; ++stack_frame) {
-      const Method* m = record->stack[stack_frame].method;
+      const AbstractMethod* m = record->stack[stack_frame].method;
       if (m == NULL) {
         break;
       }
@@ -3330,7 +3330,7 @@
 
     MethodHelper mh;
     for (size_t i = 0; i < kMaxAllocRecordStackDepth; i++) {
-      Method* m = record->stack[i].method;
+      AbstractMethod* m = record->stack[i].method;
       if (m != NULL) {
         mh.ChangeMethod(m);
         class_names.Add(mh.GetDeclaringClassDescriptor());