Index: test/SemaCXX/calling-conv-compat.cpp
===================================================================
--- test/SemaCXX/calling-conv-compat.cpp	(revision 0)
+++ test/SemaCXX/calling-conv-compat.cpp	(working copy)
@@ -0,0 +1,299 @@
+// RUN: %clang_cc1 -fsyntax-only -fms-extensions -cxx-abi microsoft -verify -triple i686-pc-win32 %s
+
+// Pointers to free functions
+void            free_func_default();
+void __cdecl    free_func_cdecl();
+void __stdcall  free_func_stdcall();
+void __fastcall free_func_fastcall();
+
+typedef void (           *fptr_default)();
+typedef void (__cdecl    *fptr_cdecl)();
+typedef void (__stdcall  *fptr_stdcall)();
+typedef void (__fastcall *fptr_fastcall)();
+
+// expected-note@+4 {{candidate function not viable: no known conversion from 'void () __attribute__((stdcall))' to 'fptr_default' (aka 'void (*)()') for 1st argument}}
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void () __attribute__((fastcall))' to 'fptr_default' (aka 'void (*)()') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (*)() __attribute__((stdcall))' to 'fptr_default' (aka 'void (*)()') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (*)() __attribute__((fastcall))' to 'fptr_default' (aka 'void (*)()') for 1st argument}}
+void cb_fptr_default(fptr_default ptr);
+// expected-note@+4 {{candidate function not viable: no known conversion from 'void () __attribute__((stdcall))' to 'fptr_cdecl' (aka 'void (*)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void () __attribute__((fastcall))' to 'fptr_cdecl' (aka 'void (*)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (*)() __attribute__((stdcall))' to 'fptr_cdecl' (aka 'void (*)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (*)() __attribute__((fastcall))' to 'fptr_cdecl' (aka 'void (*)() __attribute__((cdecl))') for 1st argument}}
+void cb_fptr_cdecl(fptr_cdecl ptr);
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void ()' to 'fptr_stdcall' (aka 'void (*)() __attribute__((stdcall))') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void () __attribute__((cdecl))' to 'fptr_stdcall' (aka 'void (*)() __attribute__((stdcall))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void () __attribute__((fastcall))' to 'fptr_stdcall' (aka 'void (*)() __attribute__((stdcall))') for 1st argument}}
+void cb_fptr_stdcall(fptr_stdcall ptr);
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void ()' to 'fptr_fastcall' (aka 'void (*)() __attribute__((fastcall))') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void () __attribute__((cdecl))' to 'fptr_fastcall' (aka 'void (*)() __attribute__((fastcall))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void () __attribute__((stdcall))' to 'fptr_fastcall' (aka 'void (*)() __attribute__((fastcall))') for 1st argument}}
+void cb_fptr_fastcall(fptr_fastcall ptr);
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void () __attribute__((stdcall))' to 'const fptr_default' (aka 'void (*const)()') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void () __attribute__((fastcall))' to 'const fptr_default' (aka 'void (*const)()') for 1st argument}}
+void cb_fptr_const_default(const fptr_default ptr);
+
+void call_free_func() {
+  cb_fptr_default(free_func_default);
+  cb_fptr_default(free_func_cdecl);
+  cb_fptr_default(free_func_stdcall); // expected-error {{no matching function for call to 'cb_fptr_default'}}
+  cb_fptr_default(free_func_fastcall); // expected-error {{no matching function for call to 'cb_fptr_default'}}
+  cb_fptr_default(&free_func_default);
+  cb_fptr_default(&free_func_cdecl);
+  cb_fptr_default(&free_func_stdcall); // expected-error {{no matching function for call to 'cb_fptr_default'}}
+  cb_fptr_default(&free_func_fastcall); // expected-error {{no matching function for call to 'cb_fptr_default'}}
+
+  cb_fptr_cdecl(free_func_default);
+  cb_fptr_cdecl(free_func_cdecl);
+  cb_fptr_cdecl(free_func_stdcall); // expected-error {{no matching function for call to 'cb_fptr_cdecl'}}
+  cb_fptr_cdecl(free_func_fastcall); // expected-error {{no matching function for call to 'cb_fptr_cdecl'}}
+  cb_fptr_cdecl(&free_func_default);
+  cb_fptr_cdecl(&free_func_cdecl);
+  cb_fptr_cdecl(&free_func_stdcall); // expected-error {{no matching function for call to 'cb_fptr_cdecl'}}
+  cb_fptr_cdecl(&free_func_fastcall); // expected-error {{no matching function for call to 'cb_fptr_cdecl'}}
+
+  cb_fptr_stdcall(free_func_default); // expected-error {{no matching function for call to 'cb_fptr_stdcall'}}
+  cb_fptr_stdcall(free_func_cdecl); // expected-error {{no matching function for call to 'cb_fptr_stdcall'}}
+  cb_fptr_stdcall(free_func_stdcall);
+  cb_fptr_stdcall(free_func_fastcall); // expected-error {{no matching function for call to 'cb_fptr_stdcall'}}
+
+  cb_fptr_fastcall(free_func_default); // expected-error {{no matching function for call to 'cb_fptr_fastcall'}}
+  cb_fptr_fastcall(free_func_cdecl); // expected-error {{no matching function for call to 'cb_fptr_fastcall'}}
+  cb_fptr_fastcall(free_func_stdcall); // expected-error {{no matching function for call to 'cb_fptr_fastcall'}}
+  cb_fptr_fastcall(free_func_fastcall);
+
+  cb_fptr_const_default(free_func_default);
+  cb_fptr_const_default(free_func_cdecl);
+  cb_fptr_const_default(free_func_stdcall); // expected-error {{no matching function for call to 'cb_fptr_const_default'}}
+  cb_fptr_const_default(free_func_fastcall); // expected-error {{no matching function for call to 'cb_fptr_const_default'}}
+
+}
+
+// Pointers to variadic functions
+// variadic function can't declared stdcall or fastcall
+void         free_func_variadic_default(int, ...);
+void __cdecl free_func_variadic_cdecl(int, ...);
+
+typedef void (        *fptr_variadic_default)(int, ...);
+typedef void (__cdecl *fptr_variadic_cdecl)(int, ...);
+
+void cb_fptr_variadic_default(fptr_variadic_default ptr);
+void cb_fptr_variadic_cdecl(fptr_variadic_cdecl ptr);
+
+void call_free_variadic_func() {
+  cb_fptr_variadic_default(free_func_variadic_default);
+  cb_fptr_variadic_default(free_func_variadic_cdecl);
+  cb_fptr_variadic_default(&free_func_variadic_default);
+  cb_fptr_variadic_default(&free_func_variadic_cdecl);
+
+  cb_fptr_variadic_cdecl(free_func_variadic_default);
+  cb_fptr_variadic_cdecl(free_func_variadic_cdecl);
+  cb_fptr_variadic_cdecl(&free_func_variadic_default);
+  cb_fptr_variadic_cdecl(&free_func_variadic_cdecl);
+}
+
+// References to functions
+typedef void (           &fref_default)();
+typedef void (__cdecl    &fref_cdecl)();
+typedef void (__stdcall  &fref_stdcall)();
+typedef void (__fastcall &fref_fastcall)();
+
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void () __attribute__((stdcall))' to 'fref_default' (aka 'void (&)()') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void () __attribute__((fastcall))' to 'fref_default' (aka 'void (&)()') for 1st argument}}
+void cb_fref_default(fref_default ptr);
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void () __attribute__((stdcall))' to 'fref_cdecl' (aka 'void (&)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void () __attribute__((fastcall))' to 'fref_cdecl' (aka 'void (&)() __attribute__((cdecl))') for 1st argument}}
+void cb_fref_cdecl(fref_cdecl ptr);
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void ()' to 'fref_stdcall' (aka 'void (&)() __attribute__((stdcall))') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void () __attribute__((cdecl))' to 'fref_stdcall' (aka 'void (&)() __attribute__((stdcall))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void () __attribute__((fastcall))' to 'fref_stdcall' (aka 'void (&)() __attribute__((stdcall))') for 1st argument}}
+void cb_fref_stdcall(fref_stdcall ptr);
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void ()' to 'fref_fastcall' (aka 'void (&)() __attribute__((fastcall))') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void () __attribute__((cdecl))' to 'fref_fastcall' (aka 'void (&)() __attribute__((fastcall))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void () __attribute__((stdcall))' to 'fref_fastcall' (aka 'void (&)() __attribute__((fastcall))') for 1st argument}}
+void cb_fref_fastcall(fref_fastcall ptr);
+
+void call_free_func_ref() {
+  cb_fref_default(free_func_default);
+  cb_fref_default(free_func_cdecl);
+  cb_fref_default(free_func_stdcall); // expected-error {{no matching function for call to 'cb_fref_default'}}
+  cb_fref_default(free_func_fastcall); // expected-error {{no matching function for call to 'cb_fref_default'}}
+
+  cb_fref_cdecl(free_func_default);
+  cb_fref_cdecl(free_func_cdecl);
+  cb_fref_cdecl(free_func_stdcall); // expected-error {{no matching function for call to 'cb_fref_cdecl'}}
+  cb_fref_cdecl(free_func_fastcall); // expected-error {{no matching function for call to 'cb_fref_cdecl'}}
+
+  cb_fref_stdcall(free_func_default); // expected-error {{no matching function for call to 'cb_fref_stdcall'}}
+  cb_fref_stdcall(free_func_cdecl); // expected-error {{no matching function for call to 'cb_fref_stdcall'}}
+  cb_fref_stdcall(free_func_stdcall);
+  cb_fref_stdcall(free_func_fastcall); // expected-error {{no matching function for call to 'cb_fref_stdcall'}}
+
+  cb_fref_fastcall(free_func_default); // expected-error {{no matching function for call to 'cb_fref_fastcall'}}
+  cb_fref_fastcall(free_func_cdecl); // expected-error {{no matching function for call to 'cb_fref_fastcall'}}
+  cb_fref_fastcall(free_func_stdcall); // expected-error {{no matching function for call to 'cb_fref_fastcall'}}
+  cb_fref_fastcall(free_func_fastcall);
+}
+
+// References to variadic functions
+// variadic function can't declared stdcall or fastcall
+typedef void (        &fref_variadic_default)(int, ...);
+typedef void (__cdecl &fref_variadic_cdecl)(int, ...);
+
+void cb_fref_variadic_default(fptr_variadic_default ptr);
+void cb_fref_variadic_cdecl(fptr_variadic_cdecl ptr);
+
+void call_free_variadic_func_ref() {
+  cb_fref_variadic_default(free_func_variadic_default);
+  cb_fref_variadic_default(free_func_variadic_cdecl);
+
+  cb_fref_variadic_cdecl(free_func_variadic_default);
+  cb_fref_variadic_cdecl(free_func_variadic_cdecl);
+}
+
+// Pointers to members
+namespace NonVariadic {
+
+struct A {
+  void            member_default();
+  void __cdecl    member_cdecl();
+  void __thiscall member_thiscall();
+};
+
+struct B : public A {
+};
+
+struct C {
+  void            member_default();
+  void __cdecl    member_cdecl();
+  void __thiscall member_thiscall();
+};
+
+typedef void (           A::*memb_a_default)();
+typedef void (__cdecl    A::*memb_a_cdecl)();
+typedef void (__thiscall A::*memb_a_thiscall)();
+typedef void (           B::*memb_b_default)();
+typedef void (__cdecl    B::*memb_b_cdecl)();
+typedef void (__thiscall B::*memb_b_thiscall)();
+typedef void (           C::*memb_c_default)();
+typedef void (__cdecl    C::*memb_c_cdecl)();
+typedef void (__thiscall C::*memb_c_thiscall)();
+
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((cdecl))' to 'memb_a_default' (aka 'void (NonVariadic::A::*)()') for 1st argument}}
+void cb_memb_a_default(memb_a_default ptr);
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)()' to 'memb_a_cdecl' (aka 'void (NonVariadic::A::*)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((thiscall))' to 'memb_a_cdecl' (aka 'void (NonVariadic::A::*)() __attribute__((cdecl))') for 1st argument}}
+void cb_memb_a_cdecl(memb_a_cdecl ptr);
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((cdecl))' to 'memb_a_thiscall' (aka 'void (NonVariadic::A::*)() __attribute__((thiscall))') for 1st argument}}
+void cb_memb_a_thiscall(memb_a_thiscall ptr);
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((cdecl))' to 'memb_b_default' (aka 'void (NonVariadic::B::*)()') for 1st argument}}
+void cb_memb_b_default(memb_b_default ptr);
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((thiscall))' to 'memb_b_cdecl' (aka 'void (NonVariadic::B::*)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)()' to 'memb_b_cdecl' (aka 'void (NonVariadic::B::*)() __attribute__((cdecl))') for 1st argument}}
+void cb_memb_b_cdecl(memb_b_cdecl ptr);
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((cdecl))' to 'memb_b_thiscall' (aka 'void (NonVariadic::B::*)() __attribute__((thiscall))') for 1st argument}}
+void cb_memb_b_thiscall(memb_b_thiscall ptr);
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)()' to 'memb_c_default' (aka 'void (NonVariadic::C::*)()') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((cdecl))' to 'memb_c_default' (aka 'void (NonVariadic::C::*)()') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((thiscall))' to 'memb_c_default' (aka 'void (NonVariadic::C::*)()') for 1st argument}}
+void cb_memb_c_default(memb_c_default ptr);
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)()' to 'memb_c_cdecl' (aka 'void (NonVariadic::C::*)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((cdecl))' to 'memb_c_cdecl' (aka 'void (NonVariadic::C::*)() __attribute__((cdecl))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((thiscall))' to 'memb_c_cdecl' (aka 'void (NonVariadic::C::*)() __attribute__((cdecl))') for 1st argument}}
+void cb_memb_c_cdecl(memb_c_cdecl ptr);
+// expected-note@+3 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)()' to 'memb_c_thiscall' (aka 'void (NonVariadic::C::*)() __attribute__((thiscall))') for 1st argument}}
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((cdecl))' to 'memb_c_thiscall' (aka 'void (NonVariadic::C::*)() __attribute__((thiscall))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (NonVariadic::A::*)() __attribute__((thiscall))' to 'memb_c_thiscall' (aka 'void (NonVariadic::C::*)() __attribute__((thiscall))') for 1st argument}}
+void cb_memb_c_thiscall(memb_c_thiscall ptr);
+
+void call_member() {
+  cb_memb_a_default(&A::member_default);
+  cb_memb_a_default(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_a_default'}}
+  cb_memb_a_default(&A::member_thiscall);
+
+  cb_memb_a_cdecl(&A::member_default); // expected-error {{no matching function for call to 'cb_memb_a_cdecl'}}
+  cb_memb_a_cdecl(&A::member_cdecl);
+  cb_memb_a_cdecl(&A::member_thiscall); // expected-error {{no matching function for call to 'cb_memb_a_cdecl'}}
+
+  cb_memb_a_thiscall(&A::member_default);
+  cb_memb_a_thiscall(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_a_thiscall'}}
+  cb_memb_a_thiscall(&A::member_thiscall);
+}
+
+void call_member_inheritance() {
+  cb_memb_b_default(&A::member_default);
+  cb_memb_b_default(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_b_default'}}
+  cb_memb_b_default(&A::member_thiscall);
+  cb_memb_c_default(&A::member_default); // expected-error {{no matching function for call to 'cb_memb_c_default'}}
+  cb_memb_c_default(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_c_default'}}
+  cb_memb_c_default(&A::member_thiscall); // expected-error {{no matching function for call to 'cb_memb_c_default'}}
+
+  cb_memb_b_cdecl(&A::member_default); // expected-error {{no matching function for call to 'cb_memb_b_cdecl'}}
+  cb_memb_b_cdecl(&A::member_cdecl);
+  cb_memb_b_cdecl(&A::member_thiscall); // expected-error {{no matching function for call to 'cb_memb_b_cdecl'}}
+  cb_memb_c_cdecl(&A::member_default); // expected-error {{no matching function for call to 'cb_memb_c_cdecl'}}
+  cb_memb_c_cdecl(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_c_cdecl'}}
+  cb_memb_c_cdecl(&A::member_thiscall); // expected-error {{no matching function for call to 'cb_memb_c_cdecl'}}
+
+  cb_memb_b_thiscall(&A::member_default);
+  cb_memb_b_thiscall(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_b_thiscall'}}
+  cb_memb_b_thiscall(&A::member_thiscall);
+  cb_memb_c_thiscall(&A::member_default); // expected-error {{no matching function for call to 'cb_memb_c_thiscall'}}
+  cb_memb_c_thiscall(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_c_thiscall'}}
+  cb_memb_c_thiscall(&A::member_thiscall); // expected-error {{no matching function for call to 'cb_memb_c_thiscall'}}
+}
+} // end namespace NonVariadic
+
+namespace Variadic {
+struct A {
+  void            member_default(int, ...);
+  void __cdecl    member_cdecl(int, ...);
+  void __thiscall member_thiscall(int, ...);
+};
+
+struct B : public A {
+};
+
+struct C {
+  void            member_default(int, ...);
+  void __cdecl    member_cdecl(int, ...);
+};
+
+typedef void (           A::*memb_a_default)(int, ...);
+typedef void (__cdecl    A::*memb_a_cdecl)(int, ...);
+typedef void (           B::*memb_b_default)(int, ...);
+typedef void (__cdecl    B::*memb_b_cdecl)(int, ...);
+typedef void (           C::*memb_c_default)(int, ...);
+typedef void (__cdecl    C::*memb_c_cdecl)(int, ...);
+
+void cb_memb_a_default(memb_a_default ptr);
+void cb_memb_a_cdecl(memb_a_cdecl ptr);
+void cb_memb_b_default(memb_b_default ptr);
+void cb_memb_b_cdecl(memb_b_cdecl ptr);
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (Variadic::A::*)(int, ...)' to 'memb_c_default' (aka 'void (Variadic::C::*)(int, ...)') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (Variadic::A::*)(int, ...) __attribute__((cdecl))' to 'memb_c_default' (aka 'void (Variadic::C::*)(int, ...)') for 1st argument}}
+void cb_memb_c_default(memb_c_default ptr);
+// expected-note@+2 {{candidate function not viable: no known conversion from 'void (Variadic::A::*)(int, ...)' to 'memb_c_cdecl' (aka 'void (Variadic::C::*)(int, ...) __attribute__((cdecl))') for 1st argument}}
+// expected-note@+1 {{candidate function not viable: no known conversion from 'void (Variadic::A::*)(int, ...) __attribute__((cdecl))' to 'memb_c_cdecl' (aka 'void (Variadic::C::*)(int, ...) __attribute__((cdecl))') for 1st argument}}
+void cb_memb_c_cdecl(memb_c_cdecl ptr);
+
+void call_member() {
+  cb_memb_a_default(&A::member_default);
+  cb_memb_a_default(&A::member_cdecl);
+
+  cb_memb_a_cdecl(&A::member_default);
+  cb_memb_a_cdecl(&A::member_cdecl);
+}
+
+void call_member_inheritance() {
+  cb_memb_b_default(&A::member_default);
+  cb_memb_b_default(&A::member_cdecl);
+  cb_memb_c_default(&A::member_default); // expected-error {{no matching function for call to 'cb_memb_c_default'}}
+  cb_memb_c_default(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_c_default'}}
+
+  cb_memb_b_cdecl(&A::member_default);
+  cb_memb_b_cdecl(&A::member_cdecl);
+  cb_memb_c_cdecl(&A::member_default); // expected-error {{no matching function for call to 'cb_memb_c_cdecl'}}
+  cb_memb_c_cdecl(&A::member_cdecl); // expected-error {{no matching function for call to 'cb_memb_c_cdecl'}}
+}
+} // end namespace Variadic
Index: lib/Sema/SemaOverload.cpp
===================================================================
--- lib/Sema/SemaOverload.cpp	(revision 176385)
+++ lib/Sema/SemaOverload.cpp	(working copy)
@@ -21,6 +21,7 @@
 #include "clang/AST/TypeOrdering.h"
 #include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/PartialDiagnostic.h"
+#include "clang/Basic/TargetInfo.h"
 #include "clang/Lex/Preprocessor.h"
 #include "clang/Sema/Initialization.h"
 #include "clang/Sema/Lookup.h"
@@ -1955,6 +1956,202 @@
   return Context.getPointerType(QualifiedCanonToPointee);
 }
 
+/// \brief Adjust the function type so that is has the same type except for
+/// calling convention specification that is inhereted from another funciton
+/// type.
+///
+/// \param Func The FunctionType to adjust.
+///
+/// \param CCSource The FunctionType from which the calling convention
+/// specification is copied.
+///
+/// \param Quals Qualifiers for the new type.
+///
+/// \param Context The ASTContext that manages these types.
+///
+/// \returns Qualified function type with calling convention matching CCSource.
+static inline QualType 
+GetQualFunctionTypeWithSameCC(const FunctionType *Func,
+                              const FunctionType *CCSource,
+                              Qualifiers Quals,
+                              ASTContext &Context) {
+  assert(Func && "First argument must be valid function type");
+  assert(CCSource && "Second argument must be valid function type");
+
+  CallingConv CC = CCSource->getExtInfo().getCC();
+  FunctionType::ExtInfo AdjustedExtInfo = 
+    Func->getExtInfo().withCallingConv(CC);
+  const FunctionType *AdjustedType = 
+    Context.adjustFunctionType(Func, AdjustedExtInfo);
+  return Context.getQualifiedType(AdjustedType, Quals);
+}
+
+/// \brief Check whether two function types have compatible calling
+/// conventions. For example, functions can use default calling convention
+/// which matches another explicitly stated CC.
+///
+/// \param F1 The type of the first function.
+///
+/// \param F2 The type of the second function.
+///
+/// \param Context Current context used to determine ABI.
+///
+/// \returns \c true if functions have compatible calling conventions, \c false
+/// otherwise.
+static inline bool
+FunctionsHaveCompatibleCallingConventions(const FunctionType *F1,
+                                          const FunctionType *F2,
+                                          ASTContext &Context) {
+  assert(F1 && "First argument must be valid function type");
+  assert(F2 && "Second argument must be valid function type");
+
+  // Equal function types have matching calling conventions. 
+  if (F1 == F2)
+    return true;
+
+  TargetCXXABI::Kind ABIKind = Context.getTargetInfo().getCXXABI().getKind();
+  switch(ABIKind) {
+  // Microsoft ABI assumes CC_C to be the default calling convention for free
+  // functions and static member functions.
+  case TargetCXXABI::Microsoft: {
+    CallingConv CC1 = F1->getExtInfo().getCC(),
+                CC2 = F2->getExtInfo().getCC();
+    return (CC1 == CC_C && CC2 == CC_Default) ||
+           (CC1 == CC_Default && CC2 == CC_C);
+  }
+
+  // Other ABIs does not feature compatible calling conventons.
+  default:
+    return false;
+  }
+  llvm_unreachable("Calling convention compatibility is defined in each ABI");
+}
+
+/// \brief Check whether two CXX member functions have compatible calling
+/// conventions.
+///
+/// \param F1 The type of the first function.
+///
+/// \param F2 The type of the second function.
+///
+/// \param Context Current context used to determine ABI.
+///
+/// \returns \c true if member functions have compatible calling conventions,
+/// \c false otherwise.
+static inline bool
+MethodsHaveCompatibleCallingConventions(const FunctionProtoType *F1,
+                                        const FunctionProtoType *F2,
+                                        ASTContext &Context) {
+  assert(F1 && "First argument must be valid function prototype");
+  assert(F2 && "Second argument must be valid function prototype");
+
+  // Equal function types have matching calling conventions.
+  if (F1 == F2)
+    return true;
+
+  TargetCXXABI::Kind ABIKind = Context.getTargetInfo().getCXXABI().getKind();
+  switch(ABIKind) {
+  // Microsoft ABI assumes CC_X86ThisCall to be the default calling convention
+  // for non-variadic member functions and CC_C -- for the variadic ones.
+  case TargetCXXABI::Microsoft: {
+    CallingConv CC1 = F1->getExtInfo().getCC(),
+                CC2 = F2->getExtInfo().getCC();
+    const bool Variadic = F1->isVariadic() || F2->isVariadic();
+    CallingConv ImplicitCC = Variadic ? CC_C : CC_X86ThisCall;
+    return (CC1 == CC_Default && CC2 == ImplicitCC) ||
+           (CC1 == ImplicitCC && CC2 == CC_Default);
+  }
+
+  // Other ABIs does not feature compatible calling conventons.
+  default:
+    return false;
+  }
+  llvm_unreachable("Calling convention compatibility is defined in each ABI");
+}
+
+/// \brief Create a function pointer type having the calling convention,
+/// compatible with another function type, if CCs are compatible.
+///
+/// \param FromType Qualified pointer to function type that should be adjusted.
+///
+/// \param FromPointeeType TODO:
+///
+/// \param ToPointeeType Qualified function type to use as CC source.
+///
+/// \param Context The context to use for creating types.
+///
+/// \param [out] CompatibleCC Whether functions differ only by CCs and these
+/// CCs are compatible.
+///
+/// \returns Adjusted FromType having the same CC as ToPointeeType if function
+/// types match except for CC, original FromType otherwise.
+static QualType
+BuildCompatibleCCFunctionPointerType(QualType FromType,
+                                     QualType FromPointeeType,
+                                     QualType ToPointeeType,
+                                     ASTContext &Context,
+                                     bool &CompatibleCC) {
+  CompatibleCC = false;
+
+  const FunctionProtoType *FromProtoType = 
+    FromPointeeType->getAs<FunctionProtoType>();
+  const FunctionProtoType *ToProtoType = 
+    ToPointeeType->getAs<FunctionProtoType>();
+
+  if (!FromProtoType || !ToProtoType)
+    return FromType;
+
+  CompatibleCC = FunctionsHaveCompatibleCallingConventions(FromProtoType,
+    ToProtoType, Context);
+  if (!CompatibleCC)
+    return FromType;
+
+  QualType ConvertedQualType = GetQualFunctionTypeWithSameCC(FromProtoType, 
+    ToProtoType, FromPointeeType.getQualifiers(), Context);
+  return Context.getPointerType(ConvertedQualType);
+}
+
+/// \brief Create a pointer to member function type having the calling
+/// convention, compatible with another member function type, if CCs are
+/// compatible.
+///
+/// \param FromTypePtr Pointer to member function type to adjust.
+///
+/// \param ToTypePtr Pointer to member function type to match against and to
+/// use as the CC source.
+///
+/// \param Context The context used for creating types.
+///
+/// \param [out] CompatibleCC Whether member functions differ only by CCs and
+/// these CCs are compatible.
+///
+/// \returns Adjusted qualified member function type having the same CC as
+/// ToTypePtr if function types match and CCs are compatible, the type
+/// pointed to by FromTypePtr otherwise.
+static QualType
+BuildCompatibleCCMemberFunctionPointerType(const MemberPointerType *FromTypePtr,
+                                           const MemberPointerType *ToTypePtr,
+                                           ASTContext &Context,
+                                           bool &CompatibleCC) {
+  CompatibleCC = false;
+  
+  QualType FromPointeeType = FromTypePtr->getPointeeType();
+  const FunctionProtoType *FromProtoType = 
+    FromPointeeType->getAs<FunctionProtoType>();
+  const FunctionProtoType *ToProtoType = 
+    ToTypePtr->getPointeeType()->getAs<FunctionProtoType>();
+
+  if (!FromProtoType || !ToProtoType)
+    return FromPointeeType;
+  CompatibleCC = MethodsHaveCompatibleCallingConventions(FromProtoType,
+      ToProtoType, Context);
+  if(!CompatibleCC)
+    return FromPointeeType;
+
+  return GetQualFunctionTypeWithSameCC(FromProtoType, ToProtoType,
+    FromPointeeType.getQualifiers(), Context);
+}
+
 static bool isNullPointerConstantForConversion(Expr *Expr,
                                                bool InOverloadResolution,
                                                ASTContext &Context) {
@@ -2076,6 +2273,18 @@
     return true;
   }
 
+  // With Microsoft ABI, CC_C is the implicit default calling convention.
+  if (Context.getTargetInfo().getCXXABI().isMicrosoft() && 
+      FromPointeeType->isFunctionType() && ToPointeeType->isFunctionType()) {
+    bool CompatibleCC = false;
+    ConvertedType = BuildCompatibleCCFunctionPointerType(FromType,
+                                                         FromPointeeType,
+                                                         ToPointeeType,
+                                                         Context,
+                                                         CompatibleCC);
+    return CompatibleCC;
+  }
+
   // When we're overloading in C, we allow a special kind of pointer
   // conversion for compatible-but-not-identical pointee types.
   if (!getLangOpts().CPlusPlus &&
@@ -2729,19 +2938,44 @@
   if (!FromTypePtr)
     return false;
 
-  // A pointer to member of B can be converted to a pointer to member of D,
-  // where D is derived from B (C++ 4.11p2).
   QualType FromClass(FromTypePtr->getClass(), 0);
   QualType ToClass(ToTypePtr->getClass(), 0);
 
+  // With Microsoft ABI CC_X86ThisCall is the default calling convention for
+  // non-variadic member functions as CC_C is default for the variadic ones.
+  // Do not return immediately since a further downcast (C++ 4.11p2) is
+  // allowed.
+  bool ConvertedCompatibleCC = false;
+  QualType AdjustedFromPointeeType = FromTypePtr->getPointeeType();
+  if (Context.getTargetInfo().getCXXABI().isMicrosoft() &&
+      ToTypePtr->isMemberFunctionPointer() && 
+      FromTypePtr->isMemberFunctionPointer()) {
+    AdjustedFromPointeeType = 
+      BuildCompatibleCCMemberFunctionPointerType(FromTypePtr,
+                                                 ToTypePtr,
+                                                 Context,
+                                                 ConvertedCompatibleCC);
+  }
+
+  // A pointer to member of B can be converted to a pointer to member of D,
+  // where D is derived from B (C++ 4.11p2).
   if (!Context.hasSameUnqualifiedType(FromClass, ToClass) &&
       !RequireCompleteType(From->getLocStart(), ToClass, 0) &&
       IsDerivedFrom(ToClass, FromClass)) {
-    ConvertedType = Context.getMemberPointerType(FromTypePtr->getPointeeType(),
+    ConvertedType = Context.getMemberPointerType(AdjustedFromPointeeType,
                                                  ToClass.getTypePtr());
     return true;
   }
 
+  // Report correct conversion if adjusted the calling convention but did not
+  // make downcast.
+  if (ConvertedCompatibleCC) {
+    ConvertedType = Context.getMemberPointerType(AdjustedFromPointeeType,
+                                                 FromClass.getTypePtr());
+    return true;
+  }
+
+
   return false;
 }
 
@@ -2777,6 +3011,21 @@
   assert(FromClass->isRecordType() && "Pointer into non-class.");
   assert(ToClass->isRecordType() && "Pointer into non-class.");
 
+  // Assume implicit calling convention conversion is okay.
+  QualType ToPointeeType = ToPtrType->getPointeeType().getCanonicalType();
+  QualType FromPointeeType = FromPtrType->getPointeeType().getCanonicalType();
+  if (FromClass == ToClass) {
+    const FunctionProtoType *FromProto = 
+        FromPointeeType->getAs<FunctionProtoType>();
+    const FunctionProtoType *ToProto = 
+        ToPointeeType->getAs<FunctionProtoType>();
+    if (FromProto && ToProto && 
+        MethodsHaveCompatibleCallingConventions(FromProto, ToProto, Context)) {
+      Kind = CK_NoOp;
+      return false;
+    }
+  }
+
   CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true,
                      /*DetectVirtual=*/true);
   bool DerivationOkay = IsDerivedFrom(ToClass, FromClass, Paths);
@@ -3959,8 +4208,22 @@
   DerivedToBase = false;
   ObjCConversion = false;
   ObjCLifetimeConversion = false;
+  const FunctionProtoType *FunctionT1 = UnqualT1->getAs<FunctionProtoType>();
+  const FunctionProtoType *FunctionT2 = UnqualT2->getAs<FunctionProtoType>();
   if (UnqualT1 == UnqualT2) {
     // Nothing to do.
+  } else if (Context.getTargetInfo().getCXXABI().isMicrosoft() && 
+             FunctionT1 && FunctionT2) {
+    // Microsoft ABI assumes CC_C is the default calling convention if no CC is
+    // explicitly specified.
+    bool CompatibleCC = FunctionsHaveCompatibleCallingConventions(FunctionT1,
+      FunctionT2, Context);
+    QualType AdjustedT1 = GetQualFunctionTypeWithSameCC(FunctionT1, 
+      FunctionT2, T2.getQualifiers(), Context);
+    if (AdjustedT1 != T2 || !CompatibleCC)
+      return Ref_Incompatible;
+      
+    // Implicit CC conversion is allowed. Need to check qualifiers.
   } else if (!RequireCompleteType(Loc, OrigT2, 0) &&
            IsDerivedFrom(UnqualT2, UnqualT1))
     DerivedToBase = true;
