import source from 1.3.40
[external/swig.git] / Examples / test-suite / preproc.i
1 %module preproc
2
3 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) one; /* Ruby, wrong constant name */
4 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) two; /* Ruby, wrong constant name */
5 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) three; /* Ruby, wrong constant name */
6 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) __GMP_HAVE_CONST; /* Ruby, wrong constant name */
7 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) __GMP_HAVE_PROTOTYPES; /* Ruby, wrong constant name */
8 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) __GMP_HAVE_TOKEN_PASTE; /* Ruby, wrong constant name */
9
10 /* check __cplusplus case */
11 %header
12 %{
13 #ifdef __cplusplus
14 extern "C"
15 {
16 #endif /* __cplusplus */
17   /* C code */
18 #ifdef __cplusplus
19 }
20 #endif /* __cplusplus */
21
22 %}
23
24
25 /* This interface file tests whether SWIG's extended C
26    preprocessor is working right. 
27
28    In this example, SWIG 1.3.6 chokes on "//" in a #define with a
29    syntax error.
30 */
31
32 #define SLASHSLASH "//"
33
34 /* This SWIG -*- c -*- interface is to test for some strange
35    preprocessor bug.
36
37    I get syntax errors unless I remove the apostrophe in the comment
38    or the sharp-sign substitution.  (The apostrophe seems to disable
39    sharp-sign substitution.)
40 */
41
42
43 %define TYPEMAP_LIST_VECTOR_INPUT_OUTPUT(SCM_TYPE)
44
45      /* Don't check for NULL pointers (override checks). */
46
47      %typemap(argout, doc="($arg <vector of <" #SCM_TYPE ">>)") 
48           int *VECTORLENOUTPUT
49      {
50      }
51
52 %enddef
53
54 TYPEMAP_LIST_VECTOR_INPUT_OUTPUT(boolean)
55
56 // preproc_3
57
58 #define Sum( A, B, \
59              C)    \
60         A + B + C 
61
62
63 // preproc_4
64 %{
65   int hello0()
66   {
67     return 0;
68   }
69
70   int hello1()
71   {
72     return 1;
73   }
74
75   int hello2()
76   {
77     return 2;
78   }  
79   int f(int min) { return min; }
80 %}
81
82 #define ARITH_RTYPE(A1, A2) A2
83
84 #define HELLO_TYPE(A, B) ARITH_RTYPE(A, ARITH_RTYPE(A,B))
85
86 //
87 // These two work fine
88 //
89 int hello0();
90 ARITH_RTYPE(double,int) hello1();
91
92
93 //
94 // This doesn't work with 1.3.17+ ( but it was ok in 1.3.16 )
95 // it gets expanded as (using -E)
96 // 
97 //   ARITH_RTYPE(double,int) hello2();
98 //
99 HELLO_TYPE(double,int) hello2();
100
101 #define min(x,y) ((x) < (y)) ? (x) : (y) 
102 int f(int min);
103
104 // preproc_5
105
106 %warnfilter(SWIGWARN_PARSE_REDEFINED) A5;       // Ruby, wrong constant name
107 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) a5;       // Ruby, wrong constant name
108 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) b5;       // Ruby, wrong constant name
109 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) c5;       // Ruby, wrong constant name
110 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) d5;       // Ruby, wrong constant name
111
112 // Various preprocessor bits of nastiness.
113
114
115 /* Test argument name substitution */
116 #define foo(x,xx) #x #xx
117 #define bar(x,xx) x + xx
118
119 %constant char *a5 = foo(hello,world);
120 %constant int   b5 = bar(3,4);
121
122 // Wrap your brain around this one ;-)
123
124 %{
125 #define cat(x,y) x ## y
126 %}
127
128 #define cat(x,y) x ## y
129
130 /* This should expand to cat(1,2);  
131    See K&R, p. 231 */
132
133 %constant int c5 = cat(cat(1,2),;)
134
135 #define xcat(x,y) cat(x,y)
136
137 /* This expands to 123.  See K&R, p. 231 */
138 %constant int d5 = xcat(xcat(1,2),3);
139
140
141 #define C1\
142 "hello"
143
144 #define C2
145 #define C3 C2
146
147 #define ALONG_\
148 NAME 42
149
150 #define C4"Hello"
151
152 // preproc_6
153
154 %warnfilter(SWIGWARN_PARSE_REDEFINED) A6; /* Ruby, wrong constant name */
155 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) a6; /* Ruby, wrong constant name */
156 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) b6; /* Ruby, wrong constant name */
157 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) c6; /* Ruby, wrong constant name */
158 %warnfilter(SWIGWARN_RUBY_WRONG_NAME) d6; /* Ruby, wrong constant name */
159
160 #define add(a, b) (a + b)
161 #define times(a, b) (a * b)
162 #define op(x) x(1, 5)
163  
164 /* expand to (1 + 5) */
165 %constant int a6 = op(add);
166 /* expand to (1 * 5) */
167 %constant int b6 = op(times);
168 /* expand to ((1 + 5) * 5) */
169 %constant int c6 = times(add(1, 5), 5);
170 /* expand to ((1 + 5) * 5) */
171 %constant int d6 = times(op(add), 5);                 
172
173 /* This interface file tests whether SWIG's extended C
174    preprocessor is working right. 
175
176    In this example, SWIG 1.3a5 reports missing macro arguments, which
177    is bogus.
178 */
179
180 %define MACRO1(C_TYPE, GETLENGTH)
181      /* nothing */
182 %enddef
183
184 %define MACRO2(XYZZY)
185   MACRO1(XYZZY, 1)
186 %enddef
187
188 MACRO2(int)
189
190 // cpp_macro_noarg.  Tests to make sure macros with no arguments work right.
191 #define MACROWITHARG(x) something(x) 
192
193 typedef int MACROWITHARG; 
194
195 /* 
196 This testcase tests for embedded defines and embedded %constants
197 */
198
199 %inline %{
200
201 typedef struct EmbeddedDefines {
202   int dummy;
203 #define  EMBEDDED_DEFINE 44
204 #ifdef SWIG
205 %constant EMBEDDED_SWIG_CONSTANT = 55;
206 #endif
207 } EmbeddedDefines;
208
209 %}
210
211 /* 
212 This testcase tests operators for defines
213 */
214
215 #define A1   1 + 2
216 #define A2   3 - 4
217 #define A3   5 * 6
218 #define A4   7 / 8
219 #define A5   9 >> 10
220 #define A6   11 << 12
221 #define A7   13 & 14
222 #define A8   15 | 16
223 #define A9   17 ^ 18
224 #define A10  19 && 20
225 #define A11  21 || 21
226 #define A12  ~22
227 #define A13  !23
228
229
230
231 #ifdef __cplusplus
232                    
233 #define %mangle_macro(...) #@__VA_ARGS__
234 #define %mangle_macro_str(...) ##@__VA_ARGS__
235
236 %define my_func(...)
237 inline const char* mangle_macro ## #@__VA_ARGS__ () {
238   return %mangle_macro_str(__VA_ARGS__);
239 }
240 %enddef
241
242 %inline {
243   my_func(class Int) ;
244   my_func(std::pair<double, std::complex< double > >*) ;
245 }
246
247 #endif
248
249
250 #if defined (__cplusplus) \
251 || defined (_AIX) \
252 || defined (__DECC) \
253 || (defined (__mips) && defined (_SYSTYPE_SVR4)) \
254 || defined (_MSC_VER) \
255 || defined (_WIN32)
256 #define __GMP_HAVE_CONST 1
257 #define __GMP_HAVE_PROTOTYPES 1
258 #define __GMP_HAVE_TOKEN_PASTE 1
259 #else
260 #define __GMP_HAVE_CONST 0
261 #define __GMP_HAVE_PROTOTYPES 0
262 #define __GMP_HAVE_TOKEN_PASTE 0
263 #endif
264
265
266 /* empty TWO() macro is broken */
267 #define ONE 1
268 #define TWO() 2
269 #define THREE(FOO) 3
270
271 #define one ONE
272 #define two TWO()
273 #define three THREE(42)
274
275
276 #if defined(one)
277 /* hello */
278 #else
279 /* chiao */
280 #endif;
281
282 #ifdef SWIGCHICKEN
283 /* define is a scheme keyword (and thus an invalid variable name), so SWIG warns about it */
284 %warnfilter(SWIGWARN_PARSE_KEYWORD) define; 
285 #endif
286
287 #ifdef SWIGRUBY
288 %rename(ddefined) defined;
289 #endif
290 #ifdef SWIGPHP
291 %rename(endif_) endif;
292 #endif
293 %inline %{
294 const int endif = 1;
295 const int define = 1;
296 const int defined = 1; 
297 int test(int defined)
298 {
299   return defined;
300 }
301  
302 %}
303
304 #pragma SWIG nowarn=SWIGWARN_PP_CPP_WARNING
305 #warning "Some warning"
306
307 /* check that #error can be turned into a warning, but suppress the warning message for the test-suite! */
308 #pragma SWIG nowarn=SWIGWARN_PP_CPP_ERROR
309 #pragma SWIG cpperraswarn=1
310 #error "Some error"
311
312
313 #define MASK(shift, size) (((1 << (size)) - 1) <<(shift))
314 #define SOME_MASK_DEF (80*MASK(8, 10))
315
316 /* some constants */
317 #define BOLTZMANN    (1.380658e-23)
318 #define AVOGADRO     (6.0221367e23)
319 #define RGAS         (BOLTZMANN*AVOGADRO)
320 #define RGASX        (BOLTZMANN*AVOGADRO*BOLTZMANN)
321
322 %{
323 #define TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(TYPE) \
324 struct TypeNameTraits { \
325   int val; \
326 } \
327
328 %}
329
330
331 #define TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(TYPE) \
332 struct TypeNameTraits { \
333   int val; \
334 } \
335
336 %inline %{
337 TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(int);
338 %}
339
340 %inline %{
341 int method(struct TypeNameTraits tnt) {
342   return tnt.val;
343 }
344 %}