xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/decl-microsoft-call-conv.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple i686-pc-win32 -fms-extensions -verify %s
2*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple i686-pc-mingw32 -verify %s
3*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 -triple i686-pc-mingw32 -fms-extensions -verify %s
4f4a2713aSLionel Sambuc 
5f4a2713aSLionel Sambuc typedef void void_fun_t();
6f4a2713aSLionel Sambuc typedef void __cdecl cdecl_fun_t();
7f4a2713aSLionel Sambuc 
8f4a2713aSLionel Sambuc // Pointers to free functions
9f4a2713aSLionel Sambuc void            free_func_default(); // expected-note 2 {{previous declaration is here}}
10f4a2713aSLionel Sambuc void __cdecl    free_func_cdecl(); // expected-note 2 {{previous declaration is here}}
11f4a2713aSLionel Sambuc void __stdcall  free_func_stdcall(); // expected-note 2 {{previous declaration is here}}
12f4a2713aSLionel Sambuc void __fastcall free_func_fastcall(); // expected-note 2 {{previous declaration is here}}
13*0a6a1f1dSLionel Sambuc void __vectorcall free_func_vectorcall(); // expected-note 2 {{previous declaration is here}}
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc void __cdecl    free_func_default();
16f4a2713aSLionel Sambuc void __stdcall  free_func_default(); // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
17f4a2713aSLionel Sambuc void __fastcall free_func_default(); // expected-error {{function declared 'fastcall' here was previously declared without calling convention}}
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc void            free_func_cdecl();
20f4a2713aSLionel Sambuc void __stdcall  free_func_cdecl(); // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}}
21f4a2713aSLionel Sambuc void __fastcall free_func_cdecl(); // expected-error {{function declared 'fastcall' here was previously declared 'cdecl'}}
22f4a2713aSLionel Sambuc 
23f4a2713aSLionel Sambuc void            free_func_stdcall();
24f4a2713aSLionel Sambuc void __cdecl    free_func_stdcall(); // expected-error {{function declared 'cdecl' here was previously declared 'stdcall'}}
25f4a2713aSLionel Sambuc void __fastcall free_func_stdcall(); // expected-error {{function declared 'fastcall' here was previously declared 'stdcall'}}
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc void __cdecl    free_func_fastcall(); // expected-error {{function declared 'cdecl' here was previously declared 'fastcall'}}
28f4a2713aSLionel Sambuc void __stdcall  free_func_fastcall(); // expected-error {{function declared 'stdcall' here was previously declared 'fastcall'}}
29f4a2713aSLionel Sambuc void            free_func_fastcall();
30f4a2713aSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc void __cdecl    free_func_vectorcall(); // expected-error {{function declared 'cdecl' here was previously declared 'vectorcall'}}
32*0a6a1f1dSLionel Sambuc void __stdcall  free_func_vectorcall(); // expected-error {{function declared 'stdcall' here was previously declared 'vectorcall'}}
33*0a6a1f1dSLionel Sambuc void            free_func_vectorcall();
34*0a6a1f1dSLionel Sambuc 
35f4a2713aSLionel Sambuc // Overloaded functions may have different calling conventions
36f4a2713aSLionel Sambuc void __fastcall free_func_default(int);
37f4a2713aSLionel Sambuc void __cdecl    free_func_default(int *);
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc void __thiscall free_func_cdecl(char *);
40f4a2713aSLionel Sambuc void __cdecl    free_func_cdecl(double);
41f4a2713aSLionel Sambuc 
42f4a2713aSLionel Sambuc typedef void void_fun_t();
43f4a2713aSLionel Sambuc typedef void __cdecl cdecl_fun_t();
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc // Pointers to member functions
46f4a2713aSLionel Sambuc struct S {
47f4a2713aSLionel Sambuc   void            member_default1(); // expected-note {{previous declaration is here}}
48f4a2713aSLionel Sambuc   void            member_default2();
49f4a2713aSLionel Sambuc   void __cdecl    member_cdecl1();
50f4a2713aSLionel Sambuc   void __cdecl    member_cdecl2(); // expected-note {{previous declaration is here}}
51f4a2713aSLionel Sambuc   void __thiscall member_thiscall1();
52f4a2713aSLionel Sambuc   void __thiscall member_thiscall2(); // expected-note {{previous declaration is here}}
53*0a6a1f1dSLionel Sambuc   void __vectorcall member_vectorcall1();
54*0a6a1f1dSLionel Sambuc   void __vectorcall member_vectorcall2(); // expected-note {{previous declaration is here}}
55f4a2713aSLionel Sambuc 
56f4a2713aSLionel Sambuc   // Typedefs carrying the __cdecl convention are adjusted to __thiscall.
57f4a2713aSLionel Sambuc   void_fun_t           member_typedef_default; // expected-note {{previous declaration is here}}
58f4a2713aSLionel Sambuc   cdecl_fun_t          member_typedef_cdecl1;  // expected-note {{previous declaration is here}}
59f4a2713aSLionel Sambuc   cdecl_fun_t __cdecl  member_typedef_cdecl2;
60f4a2713aSLionel Sambuc   void_fun_t __stdcall member_typedef_stdcall;
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   // Static member functions can't be __thiscall
63f4a2713aSLionel Sambuc   static void            static_member_default1();
64f4a2713aSLionel Sambuc   static void            static_member_default2();
65f4a2713aSLionel Sambuc   static void            static_member_default3(); // expected-note {{previous declaration is here}}
66f4a2713aSLionel Sambuc   static void __cdecl    static_member_cdecl1();
67f4a2713aSLionel Sambuc   static void __cdecl    static_member_cdecl2(); // expected-note {{previous declaration is here}}
68f4a2713aSLionel Sambuc   static void __stdcall  static_member_stdcall1();
69f4a2713aSLionel Sambuc   static void __stdcall  static_member_stdcall2();
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc   // Variadic functions can't be other than default or __cdecl
72f4a2713aSLionel Sambuc   void            member_variadic_default(int x, ...);
73f4a2713aSLionel Sambuc   void __cdecl    member_variadic_cdecl(int x, ...);
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc   static void            static_member_variadic_default(int x, ...);
76f4a2713aSLionel Sambuc   static void __cdecl    static_member_variadic_cdecl(int x, ...);
77f4a2713aSLionel Sambuc };
78f4a2713aSLionel Sambuc 
member_default1()79f4a2713aSLionel Sambuc void __cdecl    S::member_default1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
member_default2()80f4a2713aSLionel Sambuc void __thiscall S::member_default2() {}
81f4a2713aSLionel Sambuc 
member_typedef_default()82f4a2713aSLionel Sambuc void __cdecl   S::member_typedef_default() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
member_typedef_cdecl1()83f4a2713aSLionel Sambuc void __cdecl   S::member_typedef_cdecl1() {} // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
member_typedef_cdecl2()84f4a2713aSLionel Sambuc void __cdecl   S::member_typedef_cdecl2() {}
member_typedef_stdcall()85f4a2713aSLionel Sambuc void __stdcall S::member_typedef_stdcall() {}
86f4a2713aSLionel Sambuc 
member_cdecl1()87f4a2713aSLionel Sambuc void            S::member_cdecl1() {}
member_cdecl2()88f4a2713aSLionel Sambuc void __thiscall S::member_cdecl2() {} // expected-error {{function declared 'thiscall' here was previously declared 'cdecl'}}
89f4a2713aSLionel Sambuc 
member_thiscall1()90f4a2713aSLionel Sambuc void            S::member_thiscall1() {}
member_thiscall2()91f4a2713aSLionel Sambuc void __cdecl    S::member_thiscall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'thiscall'}}
92f4a2713aSLionel Sambuc 
member_vectorcall1()93*0a6a1f1dSLionel Sambuc void            S::member_vectorcall1() {}
member_vectorcall2()94*0a6a1f1dSLionel Sambuc void __cdecl    S::member_vectorcall2() {} // expected-error {{function declared 'cdecl' here was previously declared 'vectorcall'}}
95*0a6a1f1dSLionel Sambuc 
static_member_default1()96f4a2713aSLionel Sambuc void            S::static_member_default1() {}
static_member_default2()97f4a2713aSLionel Sambuc void __cdecl    S::static_member_default2() {}
static_member_default3()98f4a2713aSLionel Sambuc void __stdcall  S::static_member_default3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
99f4a2713aSLionel Sambuc 
static_member_cdecl1()100f4a2713aSLionel Sambuc void            S::static_member_cdecl1() {}
static_member_cdecl2()101f4a2713aSLionel Sambuc void __stdcall  S::static_member_cdecl2() {} // expected-error {{function declared 'stdcall' here was previously declared 'cdecl'}}
102f4a2713aSLionel Sambuc 
member_variadic_default(int x,...)103f4a2713aSLionel Sambuc void __cdecl    S::member_variadic_default(int x, ...) { (void)x; }
member_variadic_cdecl(int x,...)104f4a2713aSLionel Sambuc void            S::member_variadic_cdecl(int x, ...) { (void)x; }
105f4a2713aSLionel Sambuc 
static_member_variadic_default(int x,...)106f4a2713aSLionel Sambuc void __cdecl    S::static_member_variadic_default(int x, ...) { (void)x; }
static_member_variadic_cdecl(int x,...)107f4a2713aSLionel Sambuc void            S::static_member_variadic_cdecl(int x, ...) { (void)x; }
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc // Declare a template using a calling convention.
mystrlen(const CharT * str)110f4a2713aSLionel Sambuc template <class CharT> inline int __cdecl mystrlen(const CharT *str) {
111f4a2713aSLionel Sambuc   int i;
112f4a2713aSLionel Sambuc   for (i = 0; str[i]; i++) { }
113f4a2713aSLionel Sambuc   return i;
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc extern int sse_strlen(const char *str);
mystrlen(const char * str)116f4a2713aSLionel Sambuc template <> inline int __cdecl mystrlen(const char *str) {
117f4a2713aSLionel Sambuc   return sse_strlen(str);
118f4a2713aSLionel Sambuc }
use_tmpl(const char * str,const int * ints)119f4a2713aSLionel Sambuc void use_tmpl(const char *str, const int *ints) {
120f4a2713aSLionel Sambuc   mystrlen(str);
121f4a2713aSLionel Sambuc   mystrlen(ints);
122f4a2713aSLionel Sambuc }
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc struct MixedCCStaticOverload {
125f4a2713aSLionel Sambuc   static void overloaded(int a);
126f4a2713aSLionel Sambuc   static void __stdcall overloaded(short a);
127f4a2713aSLionel Sambuc };
128f4a2713aSLionel Sambuc 
overloaded(int a)129f4a2713aSLionel Sambuc void MixedCCStaticOverload::overloaded(int a) {}
overloaded(short a)130f4a2713aSLionel Sambuc void MixedCCStaticOverload::overloaded(short a) {}
131f4a2713aSLionel Sambuc 
132f4a2713aSLionel Sambuc // Friend function decls are cdecl by default, not thiscall.  Friend method
133f4a2713aSLionel Sambuc // decls should always be redeclarations, because the class cannot be
134f4a2713aSLionel Sambuc // incomplete.
135f4a2713aSLionel Sambuc struct FriendClass {
friend_methodFriendClass136f4a2713aSLionel Sambuc   void friend_method() {}
137f4a2713aSLionel Sambuc };
friend_stdcall1()138f4a2713aSLionel Sambuc void __stdcall friend_stdcall1() {}
139f4a2713aSLionel Sambuc class MakeFriendDecls {
140f4a2713aSLionel Sambuc   int x;
141f4a2713aSLionel Sambuc   friend void FriendClass::friend_method();
142f4a2713aSLionel Sambuc   friend void              friend_default();
143f4a2713aSLionel Sambuc   friend void              friend_stdcall1();
144f4a2713aSLionel Sambuc   friend void __stdcall    friend_stdcall2();
145f4a2713aSLionel Sambuc   friend void              friend_stdcall3(); // expected-note {{previous declaration is here}}
146f4a2713aSLionel Sambuc };
friend_default()147f4a2713aSLionel Sambuc void           friend_default() {}
friend_stdcall3()148f4a2713aSLionel Sambuc void __stdcall friend_stdcall3() {} // expected-error {{function declared 'stdcall' here was previously declared without calling convention}}
friend_stdcall2()149f4a2713aSLionel Sambuc void __stdcall friend_stdcall2() {}
150f4a2713aSLionel Sambuc 
151f4a2713aSLionel Sambuc // Test functions with multiple attributes.
152f4a2713aSLionel Sambuc void __attribute__((noreturn)) __stdcall __attribute__((regparm(1))) multi_attribute(int x);
multi_attribute(int x)153f4a2713aSLionel Sambuc void multi_attribute(int x) { __builtin_unreachable(); }
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc 
156*0a6a1f1dSLionel Sambuc // expected-error@+3 {{vectorcall and cdecl attributes are not compatible}}
157f4a2713aSLionel Sambuc // expected-error@+2 {{stdcall and cdecl attributes are not compatible}}
158f4a2713aSLionel Sambuc // expected-error@+1 {{fastcall and cdecl attributes are not compatible}}
159*0a6a1f1dSLionel Sambuc void __cdecl __cdecl __stdcall __cdecl __fastcall __vectorcall multi_cc(int x);
160f4a2713aSLionel Sambuc 
StdcallTemplate(T)161f4a2713aSLionel Sambuc template <typename T> void __stdcall StdcallTemplate(T) {}
StdcallTemplate(int)162f4a2713aSLionel Sambuc template <> void StdcallTemplate<int>(int) {}
StdcallTemplate(short)163f4a2713aSLionel Sambuc template <> void __stdcall StdcallTemplate<short>(short) {}
164f4a2713aSLionel Sambuc 
165f4a2713aSLionel Sambuc // FIXME: Note the template, not the implicit instantiation.
166f4a2713aSLionel Sambuc // expected-error@+2 {{function declared 'cdecl' here was previously declared 'stdcall}}
167f4a2713aSLionel Sambuc // expected-note@+1 {{previous declaration is here}}
StdcallTemplate(long)168f4a2713aSLionel Sambuc template <> void __cdecl StdcallTemplate<long>(long) {}
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc struct ExactlyInt {
cast_to_intExactlyInt171f4a2713aSLionel Sambuc   template <typename T> static int cast_to_int(T) {
172f4a2713aSLionel Sambuc     return T::this_is_not_an_int();
173f4a2713aSLionel Sambuc   }
174f4a2713aSLionel Sambuc };
cast_to_int(int x)175f4a2713aSLionel Sambuc template <> inline int ExactlyInt::cast_to_int<int>(int x) { return x; }
176f4a2713aSLionel Sambuc 
177f4a2713aSLionel Sambuc namespace test2 {
178f4a2713aSLionel Sambuc   class foo {
179f4a2713aSLionel Sambuc     template <typename T> void bar(T v);
180f4a2713aSLionel Sambuc   };
181f4a2713aSLionel Sambuc   extern template void foo::bar(const void *);
182f4a2713aSLionel Sambuc }
183f4a2713aSLionel Sambuc 
184f4a2713aSLionel Sambuc namespace test3 {
185f4a2713aSLionel Sambuc   struct foo {
186f4a2713aSLionel Sambuc     typedef void bar();
187f4a2713aSLionel Sambuc   };
188f4a2713aSLionel Sambuc   bool zed(foo::bar *);
bah()189f4a2713aSLionel Sambuc   void bah() {}
baz()190f4a2713aSLionel Sambuc   void baz() { zed(bah); }
191f4a2713aSLionel Sambuc }
192f4a2713aSLionel Sambuc 
193f4a2713aSLionel Sambuc namespace test4 {
194f4a2713aSLionel Sambuc   class foo {
195f4a2713aSLionel Sambuc     template <typename T> static void bar(T v);
196f4a2713aSLionel Sambuc   };
197f4a2713aSLionel Sambuc   extern template void foo::bar(const void *);
198f4a2713aSLionel Sambuc }
199*0a6a1f1dSLionel Sambuc 
200*0a6a1f1dSLionel Sambuc namespace test5 {
201*0a6a1f1dSLionel Sambuc   template <class T>
202*0a6a1f1dSLionel Sambuc   class valarray {
203*0a6a1f1dSLionel Sambuc     void bar();
204*0a6a1f1dSLionel Sambuc   };
205*0a6a1f1dSLionel Sambuc   extern template void valarray<int>::bar();
206*0a6a1f1dSLionel Sambuc }
207*0a6a1f1dSLionel Sambuc 
208*0a6a1f1dSLionel Sambuc namespace test6 {
209*0a6a1f1dSLionel Sambuc   struct foo {
210*0a6a1f1dSLionel Sambuc     int bar();
211*0a6a1f1dSLionel Sambuc   };
212*0a6a1f1dSLionel Sambuc   typedef int bar_t();
zed(bar_t foo::*)213*0a6a1f1dSLionel Sambuc   void zed(bar_t foo::*) {
214*0a6a1f1dSLionel Sambuc   }
baz()215*0a6a1f1dSLionel Sambuc   void baz() {
216*0a6a1f1dSLionel Sambuc     zed(&foo::bar);
217*0a6a1f1dSLionel Sambuc   }
218*0a6a1f1dSLionel Sambuc }
219*0a6a1f1dSLionel Sambuc 
220*0a6a1f1dSLionel Sambuc namespace test7 {
221*0a6a1f1dSLionel Sambuc   template <typename T>
222*0a6a1f1dSLionel Sambuc   struct S {
ftest7::S223*0a6a1f1dSLionel Sambuc     void f(T t) {
224*0a6a1f1dSLionel Sambuc       t = 42;
225*0a6a1f1dSLionel Sambuc     }
226*0a6a1f1dSLionel Sambuc   };
227*0a6a1f1dSLionel Sambuc   template<> void S<void*>::f(void*);
g(S<void * > s,void * p)228*0a6a1f1dSLionel Sambuc   void g(S<void*> s, void* p) {
229*0a6a1f1dSLionel Sambuc     s.f(p);
230*0a6a1f1dSLionel Sambuc   }
231*0a6a1f1dSLionel Sambuc }
232*0a6a1f1dSLionel Sambuc 
233*0a6a1f1dSLionel Sambuc namespace test8 {
234*0a6a1f1dSLionel Sambuc   template <typename T>
235*0a6a1f1dSLionel Sambuc   struct S {
ftest8::S236*0a6a1f1dSLionel Sambuc     void f(T t) { // expected-note {{previous declaration is here}}
237*0a6a1f1dSLionel Sambuc       t = 42; // expected-error {{assigning to 'void *' from incompatible type 'int'}}
238*0a6a1f1dSLionel Sambuc     }
239*0a6a1f1dSLionel Sambuc   };
240*0a6a1f1dSLionel Sambuc   template<> void __cdecl S<void*>::f(void*); // expected-error {{function declared 'cdecl' here was previously declared without calling convention}}
g(S<void * > s,void * p)241*0a6a1f1dSLionel Sambuc   void g(S<void*> s, void* p) {
242*0a6a1f1dSLionel Sambuc     s.f(p); // expected-note {{in instantiation of member function 'test8::S<void *>::f' requested here}}
243*0a6a1f1dSLionel Sambuc   }
244*0a6a1f1dSLionel Sambuc }
245