Index: include/clang/AST/RecordLayout.h
===================================================================
--- include/clang/AST/RecordLayout.h	(revision 139584)
+++ include/clang/AST/RecordLayout.h	(working copy)
@@ -63,6 +63,9 @@
     /// any empty subobjects.
     CharUnits SizeOfLargestEmptySubobject;
     
+    /// VBPtrOffset - Offest of virtual base table.
+    CharUnits VBPtrOffset;
+    
     /// PrimaryBase - The primary base info for this record.
     llvm::PointerIntPair<const CXXRecordDecl *, 1, bool> PrimaryBase;
     
@@ -88,8 +91,10 @@
 
   // Constructor for C++ records.
   typedef CXXRecordLayoutInfo::BaseOffsetsMapTy BaseOffsetsMapTy;
+
   ASTRecordLayout(const ASTContext &Ctx,
-                  CharUnits size, CharUnits alignment, CharUnits datasize,
+                  CharUnits size, CharUnits alignment,
+                  CharUnits vbptroffset, CharUnits datasize,
                   const uint64_t *fieldoffsets, unsigned fieldcount,
                   CharUnits nonvirtualsize, CharUnits nonvirtualalign,
                   CharUnits SizeOfLargestEmptySubobject,
@@ -199,6 +204,11 @@
     assert(CXXInfo && "Record layout does not have C++ specific info!");
     return CXXInfo->SizeOfLargestEmptySubobject;
   }
+
+  /// getVBPtrOffset - Get the offset for the virtual base table.
+  CharUnits getVBPtrOffset() const {
+    return CXXInfo->VBPtrOffset;
+  }
 };
 
 }  // end namespace clang
Index: lib/AST/RecordLayout.cpp
===================================================================
--- lib/AST/RecordLayout.cpp	(revision 139584)
+++ lib/AST/RecordLayout.cpp	(working copy)
@@ -13,6 +13,7 @@
 
 #include "clang/AST/ASTContext.h"
 #include "clang/AST/RecordLayout.h"
+#include "clang/Basic/TargetInfo.h"
 
 using namespace clang;
 
@@ -42,7 +43,7 @@
 // Constructor for C++ records.
 ASTRecordLayout::ASTRecordLayout(const ASTContext &Ctx,
                                  CharUnits size, CharUnits alignment,
-                                 CharUnits datasize,
+                                 CharUnits vbptroffset, CharUnits datasize,
                                  const uint64_t *fieldoffsets,
                                  unsigned fieldcount,
                                  CharUnits nonvirtualsize,
@@ -67,15 +68,20 @@
   CXXInfo->SizeOfLargestEmptySubobject = SizeOfLargestEmptySubobject;
   CXXInfo->BaseOffsets = BaseOffsets;
   CXXInfo->VBaseOffsets = VBaseOffsets;
+  CXXInfo->VBPtrOffset = vbptroffset;
 
 #ifndef NDEBUG
     if (const CXXRecordDecl *PrimaryBase = getPrimaryBase()) {
-      if (isPrimaryBaseVirtual())
+      if (isPrimaryBaseVirtual()) {
+        // Microsoft ABI does not have primary virtual base.
+        if (Ctx.getTargetInfo().getCXXABI() != CXXABI_Microsoft) {
         assert(getVBaseClassOffset(PrimaryBase).isZero() &&
                "Primary virtual base must be at offset 0!");
-      else
+        }
+      } else {
         assert(getBaseClassOffsetInBits(PrimaryBase) == 0 &&
                "Primary base must be at offset 0!");
+      }
     }
 #endif        
 }
Index: lib/AST/RecordLayoutBuilder.cpp
===================================================================
--- lib/AST/RecordLayoutBuilder.cpp	(revision 139584)
+++ lib/AST/RecordLayoutBuilder.cpp	(working copy)
@@ -625,7 +625,9 @@
       PrimaryBaseIsVirtual(false), FirstNearlyEmptyVBase(0) { }
 
   void Layout(const RecordDecl *D);
+  
   void Layout(const CXXRecordDecl *D);
+  
   void Layout(const ObjCInterfaceDecl *D);
 
   void LayoutFields(const RecordDecl *D);
@@ -729,6 +731,13 @@
   static const CXXMethodDecl *ComputeKeyFunction(const CXXRecordDecl *RD);
 
   virtual ~RecordLayoutBuilder() { }
+
+  virtual bool HasVirtualBasePointer() const { 
+    return false; 
+  }
+  virtual CharUnits GetVBPtrOffset() const { 
+    return CharUnits::fromQuantity(-1); 
+  }
 };
 } // end anonymous namespace
 
@@ -1843,15 +1852,72 @@
 namespace {
   // This class implements layout specific to the Microsoft ABI.
   class MSRecordLayoutBuilder : public RecordLayoutBuilder {
+    bool hasVirtualBasePointer;
+    CharUnits VBPtrOffset;
+
+    bool HasNewVirtualFunction(const CXXRecordDecl *RD) const;
+
+    /// Add vbptr or vfptr to layout.
+    void AddVPointer();
+
+    void LayoutVirtualBases(const CXXRecordDecl *RD,
+                            const CXXRecordDecl *MostDerivedClass);
+
+    void Layout(const CXXRecordDecl *RD);
   public:
     MSRecordLayoutBuilder(const ASTContext& Ctx,
                           EmptySubobjectMap *EmptySubobjects) :
-      RecordLayoutBuilder(Ctx, EmptySubobjects) {}
+      RecordLayoutBuilder(Ctx, EmptySubobjects), 
+      hasVirtualBasePointer(false),
+      VBPtrOffset(CharUnits::fromQuantity(-1)){ }
 
+    virtual ~MSRecordLayoutBuilder(){ }
+
     virtual CharUnits GetVirtualPointersSize(const CXXRecordDecl *RD) const;
+
+    virtual bool HasVirtualBasePointer() const { 
+      return hasVirtualBasePointer; 
+    }
+    virtual CharUnits GetVBPtrOffset() const { 
+      return VBPtrOffset; 
+    }
   };
 }
 
+void
+MSRecordLayoutBuilder::AddVPointer() {
+  setSize(getSize() + CharUnits::fromQuantity(4));
+  setDataSize(getSize());
+
+  CharUnits UnpackedBaseAlign = 
+    Context.toCharUnitsFromBits(Context.Target.getPointerAlign(0));
+  CharUnits BaseAlign = (Packed) ? CharUnits::One() : UnpackedBaseAlign;
+
+  // The maximum field alignment overrides base align.
+  if (!MaxFieldAlignment.isZero()) {
+    BaseAlign = std::min(BaseAlign, MaxFieldAlignment);
+    UnpackedBaseAlign = std::min(UnpackedBaseAlign, MaxFieldAlignment);
+  }
+
+  // Update the alignment.
+  UpdateAlignment(BaseAlign, UnpackedBaseAlign);
+}
+
+/// If class doesn`t have new vfunctions then he wouldn`t have vftable.
+bool 
+MSRecordLayoutBuilder::HasNewVirtualFunction(const CXXRecordDecl *RD) const {
+  for (CXXRecordDecl::method_iterator method = RD->method_begin();
+       method != RD->method_end();
+       ++method) {
+    if (method->isVirtual() &&
+      !method->size_overridden_methods()) {
+      return true;
+    }
+  }
+
+  return false;
+}
+
 CharUnits
 MSRecordLayoutBuilder::GetVirtualPointersSize(const CXXRecordDecl *RD) const {
   // We should reserve space for two pointers if the class has both
@@ -1863,6 +1929,98 @@
   return PointerWidth;
 }
 
+/// I don`t think that this approach is a good.
+static bool AllBasesVirtual(const CXXRecordDecl* RD)
+{
+  for (auto I = RD->bases_begin(), E = RD->bases_end(); I != E; ++I) {
+    if (!I->isVirtual()) {
+      return false;
+    }
+  }
+  return true;
+}
+
+void MSRecordLayoutBuilder::LayoutVirtualBases(const CXXRecordDecl *RD,
+                                         const CXXRecordDecl *MostDerivedClass)
+{
+  for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
+       E = RD->vbases_end(); I != E; ++I) {
+
+    const CXXRecordDecl* BaseDecl = I->getType()->getAsCXXRecordDecl();
+    const BaseSubobjectInfo* BaseInfo = VirtualBaseInfo.lookup(BaseDecl);
+    assert(BaseInfo && "Did not find virtual base info!");
+    LayoutVirtualBase(BaseInfo);
+  }
+}
+
+void MSRecordLayoutBuilder::Layout(const CXXRecordDecl *RD) {
+  InitializeLayout(RD);
+
+  if (AllBasesVirtual(RD) &&
+      RD->getNumBases()) {
+    // need vfptr
+    if (HasNewVirtualFunction(RD)) {
+      AddVPointer();
+    }
+
+    VBPtrOffset = getSize();
+    AddVPointer();
+    hasVirtualBasePointer = true;
+
+    ComputeBaseSubobjectInfo(RD);
+  } else {
+    LayoutNonVirtualBases(RD);
+  }
+
+  // vbptr
+  if (RD->getNumVBases() &&
+      !hasVirtualBasePointer) {
+    // Add vbptr.
+    VBPtrOffset = getSize();
+    AddVPointer();
+    hasVirtualBasePointer = true;
+  }
+
+  LayoutFields(RD);
+
+  NonVirtualSize = Context.toCharUnitsFromBits(
+                                      llvm::RoundUpToAlignment(getSizeInBits(),
+                                      Context.getTargetInfo().getCharAlign()));
+  NonVirtualAlignment = Alignment;
+
+  // Lay out the virtual bases and add the primary virtual base offsets.
+  LayoutVirtualBases(RD, RD);
+
+  VisitedVirtualBases.clear();
+
+  // Finally, round the size of the total struct up to the alignment of the
+  // struct itself.
+  FinishLayout(RD);
+
+#ifndef NDEBUG
+  // Check that we have base offsets for all bases.
+  for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
+    E = RD->bases_end(); I != E; ++I) {
+      if (I->isVirtual())
+        continue;
+
+      const CXXRecordDecl *BaseDecl =
+        cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
+
+      assert(Bases.count(BaseDecl) && "Did not find base offset!");
+  }
+
+  // And all virtual bases.
+  for (CXXRecordDecl::base_class_const_iterator I = RD->vbases_begin(),
+    E = RD->vbases_end(); I != E; ++I) {
+      const CXXRecordDecl *BaseDecl =
+        cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
+
+      assert(VBases.count(BaseDecl) && "Did not find base offset!");
+  }
+#endif
+}
+
 /// getASTRecordLayout - Get or compute information about the layout of the
 /// specified record (struct/union/class), which indicates its size and field
 /// position information.
@@ -1911,6 +2069,7 @@
     NewEntry =
       new (*this) ASTRecordLayout(*this, Builder->getSize(), 
                                   Builder->Alignment,
+                                  Builder->GetVBPtrOffset(),
                                   DataSize, 
                                   Builder->FieldOffsets.data(),
                                   Builder->FieldOffsets.size(),
@@ -2020,13 +2179,26 @@
   IndentLevel++;
 
   const CXXRecordDecl *PrimaryBase = Layout.getPrimaryBase();
+  bool HasVbptr = Layout.getVBPtrOffset() != CharUnits::fromQuantity(-1);
 
   // Vtable pointer.
   if (RD->isDynamicClass() && !PrimaryBase) {
     PrintOffset(OS, Offset, IndentLevel);
     OS << '(' << RD << " vtable pointer)\n";
   }
+  
+  if (HasVbptr && !PrimaryBase) {
+    PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
+    OS << '(' << RD << " vbtable pointer)\n";
+
+    // one vbtable per class
+    HasVbptr = false;
+  }
+  
   // Dump (non-virtual) bases
+  CharUnits BaseOffset = CharUnits::Zero();
+  const CXXRecordDecl *Base = 0;
+
   for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
          E = RD->bases_end(); I != E; ++I) {
     assert(!I->getType()->isDependentType() &&
@@ -2034,16 +2206,24 @@
     if (I->isVirtual())
       continue;
 
-    const CXXRecordDecl *Base =
+    BaseOffset = CharUnits::Zero();
+
+    Base =
       cast<CXXRecordDecl>(I->getType()->getAs<RecordType>()->getDecl());
 
-    CharUnits BaseOffset = Offset + Layout.getBaseClassOffset(Base);
+    BaseOffset = Offset + Layout.getBaseClassOffset(Base);
 
     DumpCXXRecordLayout(OS, Base, C, BaseOffset, IndentLevel,
                         Base == PrimaryBase ? "(primary base)" : "(base)",
                         /*IncludeVirtualBases=*/false);
   }
 
+  // vbptr
+  if (HasVbptr) {
+    PrintOffset(OS, Offset + Layout.getVBPtrOffset(), IndentLevel);
+    OS << '(' << RD << " vbtable pointer)\n";
+  }
+
   // Dump fields.
   uint64_t FieldNo = 0;
   for (CXXRecordDecl::field_iterator I = RD->field_begin(),
