merge from gcc
[external/binutils.git] / gdb / testsuite / gdb.cp / templates.cc
1 /* This test code is from Wendell Baker (wbaker@comet.berkeley.edu) */
2
3 #include <stddef.h>
4
5 int a_i;
6 char a_c;
7 double a_d;
8
9 typedef void *Pix;
10
11 int
12 f(int i)
13 { return 0; }
14
15 int
16 f(int i, char c)
17 { return 0; }
18
19 int
20 f(int i, char c, double d)
21 { return 0; }
22
23 int
24 f(int i, char c, double d, char *cs)
25 { return 0; }
26
27 int
28 f(int i, char c, double d, char *cs, void (*fig)(int, char))
29 { return 0; }
30
31 int
32 f(int i, char c, double d, char *cs, void (*fig)(char, int))
33 { return 0; }
34
35 class R {
36 public:
37     int i;
38 };
39 class S {
40 public:
41     int i;
42 };
43 class T {
44 public:
45     int i;
46 };
47
48 char g(char, const char, volatile char)
49 { return 'c'; }
50 char g(R, char&, const char&, volatile char&)
51 { return 'c'; }
52 char g(char*, const char*, volatile char*)
53 { return 'c'; }
54 char g(S, char*&, const char*&, volatile char*&)
55 { return 'c'; }
56
57 signed char g(T,signed char, const signed char, volatile signed char)
58 { return 'c'; }
59 signed char g(T, R, signed char&, const signed char&, volatile signed char&)
60 { return 'c'; }
61 signed char g(T, signed char*, const signed char*, volatile signed char*)
62 { return 'c'; }
63 signed char g(T, S, signed char*&, const signed char*&, volatile signed char*&)
64 { return 'c'; }
65
66 unsigned char g(unsigned char, const unsigned char, volatile unsigned char)
67 { return 'c'; }
68 unsigned char g(R, unsigned char&, const unsigned char&, volatile unsigned char&)
69 { return 'c'; }
70 unsigned char g(unsigned char*, const unsigned char*, volatile unsigned char*)
71 { return 'c'; }
72 unsigned char g(S, unsigned char*&, const unsigned char*&, volatile unsigned char*&)
73 { return 'c'; }
74
75 short g(short, const short, volatile short)
76 { return 0; }
77 short g(R, short&, const short&, volatile short&)
78 { return 0; }
79 short g(short*, const short*, volatile short*)
80 { return 0; }
81 short g(S, short*&, const short*&, volatile short*&)
82 { return 0; }
83
84 signed short g(T, signed short, const signed short, volatile signed short)
85 { return 0; }
86 signed short g(T, R, signed short&, const signed short&, volatile signed short&)
87 { return 0; }
88 signed short g(T, signed short*, const signed short*, volatile signed short*)
89 { return 0; }
90 signed short g(T, S, double, signed short*&, const signed short*&, volatile signed short*&)
91 { return 0; }
92
93 unsigned short g(unsigned short, const unsigned short, volatile unsigned short)
94 { return 0; }
95 unsigned short g(R, unsigned short&, const unsigned short&, volatile unsigned short&)
96 { return 0; }
97 unsigned short g(unsigned short*, const unsigned short*, volatile unsigned short*)
98 { return 0; }
99 unsigned short g(S, unsigned short*&, const unsigned short*&, volatile unsigned short*&)
100 { return 0; }
101
102 int g(int, const int, volatile int)
103 { return 0; }
104 int g(R, int&, const int&, volatile int&)
105 { return 0; }
106 int g(int*, const int*, volatile int*)
107 { return 0; }
108 int g(S, int*&, const int*&, volatile int*&)
109 { return 0; }
110
111 signed int g(T, signed int, const signed int, volatile signed int)
112 { return 0; }
113 signed int g(T, R, signed int&, const signed int&, volatile signed int&)
114 { return 0; }
115 signed int g(T, signed int*, const signed int*, volatile signed int*)
116 { return 0; }
117 signed int g(T, S, signed int*&, const signed int*&, volatile signed int*&)
118 { return 0; }
119
120 unsigned int g(unsigned int, const unsigned int, volatile unsigned int)
121 { return 0; }
122 unsigned int g(R, unsigned int&, const unsigned int&, volatile unsigned int&)
123 { return 0; }
124 unsigned int g(unsigned int*, const unsigned int*, volatile unsigned int*)
125 { return 0; }
126 unsigned int g(S, unsigned int*&, const unsigned int*&, volatile unsigned int*&)
127 { return 0; }
128
129 long g(long, const long, volatile long)
130 { return 0; }
131 long g(R, long&, const long&, volatile long&)
132 { return 0; }
133 long g(long*, const long*, volatile long*)
134 { return 0; }
135 long g(S, long*&, const long*&, volatile long*&)
136 { return 0; }
137
138 signed long g(T, signed long, const signed long, volatile signed long)
139 { return 0; }
140 signed long g(T, R, signed long&, const signed long&, volatile signed long&)
141 { return 0; }
142 signed long g(T, signed long*, const signed long*, volatile signed long*)
143 { return 0; }
144 signed long g(T, S, signed long*&, const signed long*&, volatile signed long*&)
145 { return 0; }
146
147 unsigned long g(unsigned long, const unsigned long, volatile unsigned long)
148 { return 0; }
149 unsigned long g(S, unsigned long&, const unsigned long&, volatile unsigned long&)
150 { return 0; }
151 unsigned long g(unsigned long*, const unsigned long*, volatile unsigned long*)
152 { return 0; }
153 unsigned long g(S, unsigned long*&, const unsigned long*&, volatile unsigned long*&)
154 { return 0; }
155
156 #ifdef __GNUC__
157 long long g(long long, const long long, volatile long long)
158 { return 0; }
159 long long g(S, long long&, const long long&, volatile long long&)
160 { return 0; }
161 long long g(long long*, const long long*, volatile long long*)
162 { return 0; }
163 long long g(R, long long*&, const long long*&, volatile long long*&)
164 { return 0; }
165
166 signed long long g(T, signed long long, const signed long long, volatile signed long long)
167 { return 0; }
168 signed long long g(T, R, signed long long&, const signed long long&, volatile signed long long&)
169 { return 0; }
170 signed long long g(T, signed long long*, const signed long long*, volatile signed long long*)
171 { return 0; }
172 signed long long g(T, S, signed long long*&, const signed long long*&, volatile signed long long*&)
173 { return 0; }
174
175 unsigned long long g(unsigned long long, const unsigned long long, volatile unsigned long long)
176 { return 0; }
177 unsigned long long g(R, unsigned long long*, const unsigned long long*, volatile unsigned long long*)
178 { return 0; }
179 unsigned long long g(unsigned long long&, const unsigned long long&, volatile unsigned long long&)
180 { return 0; }
181 unsigned long long g(S, unsigned long long*&, const unsigned long long*&, volatile unsigned long long*&)
182 { return 0; }
183 #endif
184
185 float g(float, const float, volatile float)
186 { return 0; }
187 float g(char, float&, const float&, volatile float&)
188 { return 0; }
189 float g(float*, const float*, volatile float*)
190 { return 0; }
191 float g(char, float*&, const float*&, volatile float*&)
192 { return 0; }
193
194 double g(double, const double, volatile double)
195 { return 0; }
196 double g(char, double&, const double&, volatile double&)
197 { return 0; }
198 double g(double*, const double*, volatile double*)
199 { return 0; }
200 double g(char, double*&, const double*&, volatile double*&)
201 { return 0; }
202
203 #ifdef __GNUC__
204 long double g(long double, const long double, volatile long double)
205 { return 0; }
206 long double g(char, long double&, const long double&, volatile long double&)
207 { return 0; }
208 long double g(long double*, const long double*, volatile long double*)
209 { return 0; }
210 long double g(char, long double*&, const long double*&, volatile long double*&)
211 { return 0; }
212 #endif
213
214 class c {
215 public:
216     c(int) {};
217     int i;
218 };
219
220 class c g(c, const c, volatile c)
221 { return 0; }
222 c g(char, c&, const c&, volatile c&)
223 { return 0; }
224 c g(c*, const c*, volatile c*)
225 { return 0; }
226 c g(char, c*&, const c*&, volatile c*&)
227 { return 0; }
228
229 /*
230 void h(char = 'a')
231 { }
232 void h(char, signed char = 'a')
233 { }
234 void h(unsigned char = 'a')
235 { }
236 */
237 /*
238 void h(char = (char)'a')
239 { }
240 void h(char, signed char = (signed char)'a')
241 { }
242 void h(unsigned char = (unsigned char)'a')
243 { }
244
245
246 void h(short = (short)43)
247 { }
248 void h(char, signed short = (signed short)43)
249 { }
250 void h(unsigned short = (unsigned short)43)
251 { }
252
253 void h(int = (int)43)
254 { }
255 void h(char, signed int = (signed int)43)
256 { }
257 void h(unsigned int = (unsigned int)43)
258 { }
259
260
261 void h(long = (long)43)
262 { }
263 void h(char, signed long = (signed long)43)
264 { }
265 void h(unsigned long = (unsigned long)43)
266 { }
267
268 #ifdef __GNUC__
269 void h(long long = 43)
270 { }
271 void h(char, signed long long = 43)
272 { }
273 void h(unsigned long long = 43)
274 { }
275 #endif
276
277 void h(float = 4.3e-10)
278 { }
279 void h(double = 4.3)
280 { }
281 #ifdef __GNUC__
282 void h(long double = 4.33e33)
283 { }
284 #endif
285 */
286
287 /* An unneeded printf() definition - actually, just a stub - used to occupy
288    this space.  It has been removed and replaced with this comment which
289    exists to occupy some lines so that templates.exp won't need adjustment.  */
290
291 class T1 {
292 public:
293     static void* operator new(size_t) throw ();
294     static void operator delete(void *pointer);
295
296     void operator=(const T1&);
297     T1& operator=(int);
298
299     int operator==(int) const;
300     int operator==(const T1&) const;
301     int operator!=(int) const;
302     int operator!=(const T1&) const;
303
304     int operator<=(int) const;
305     int operator<=(const T1&) const;
306     int operator<(int) const;
307     int operator<(const T1&) const;
308     int operator>=(int) const;
309     int operator>=(const T1&) const;
310     int operator>(int) const;
311     int operator>(const T1&) const;
312
313     void operator+(int) const;
314     T1& operator+(const T1&) const;
315     void operator+=(int) const;
316     T1& operator+=(const T1&) const;
317
318     T1& operator++() const;
319
320     void operator-(int) const;
321     T1& operator-(const T1&) const;
322     void operator-=(int) const;
323     T1& operator-=(const T1&) const;
324
325     T1& operator--() const;
326
327     void operator*(int) const;
328     T1& operator*(const T1&) const;
329     void operator*=(int) const;
330     T1& operator*=(const T1&) const;
331
332     void operator/(int) const;
333     T1& operator/(const T1&) const;
334     void operator/=(int) const;
335     T1& operator/=(const T1&) const;
336
337     void operator%(int) const;
338     T1& operator%(const T1&) const;
339     void operator%=(int) const;
340     T1& operator%=(const T1&) const;
341
342     void operator&&(int) const;
343     T1& operator&&(const T1&) const;
344
345     void operator||(int) const;
346     T1& operator||(const T1&) const;
347
348     void operator&(int) const;
349     T1& operator&(const T1&) const;
350     void operator&=(int) const;
351     T1& operator&=(const T1&) const;
352
353     void operator|(int) const;
354     T1& operator|(const T1&) const;
355     void operator|=(int) const;
356     T1& operator|=(const T1&) const;
357
358     void operator^(int) const;
359     T1& operator^(const T1&) const;
360     void operator^=(int) const;
361     T1& operator^=(const T1&) const;
362
363     T1& operator!() const;
364     T1& operator~() const;
365 };
366
367 void* 
368 T1::operator new(size_t) throw ()
369 { return 0; }
370
371 void
372 T1::operator delete(void *pointer)
373 { }
374
375 class T2 {
376 public:
377     T2(int i): integer(i)
378         { }
379     int integer;
380 };
381
382 int operator==(const T2&, const T2&)
383 { return 0; }
384 int operator==(const T2&, char)
385 { return 0; }
386 int operator!=(const T2&, const T2&)
387 { return 0; }
388 int operator!=(const T2&, char)
389 { return 0; }
390
391 int operator<=(const T2&, const T2&)
392 { return 0; }
393 int operator<=(const T2&, char)
394 { return 0; }
395 int operator<(const T2&, const T2&)
396 { return 0; }
397 int operator<(const T2&, char)
398 { return 0; }
399 int operator>=(const T2&, const T2&)
400 { return 0; }
401 int operator>=(const T2&, char)
402 { return 0; }
403 int operator>(const T2&, const T2&)
404 { return 0; }
405 int operator>(const T2&, char)
406 { return 0; }
407
408 T2 operator+(const T2 t, int i)
409 { return t.integer + i; }
410 T2 operator+(const T2 a, const T2& b)
411 { return a.integer + b.integer; }
412 T2& operator+=(T2& t, int i)
413 { t.integer += i; return t; }
414 T2& operator+=(T2& a, const T2& b)
415 { a.integer += b.integer; return a; }
416
417 T2 operator-(const T2 t, int i)
418 { return t.integer - i; }
419 T2 operator-(const T2 a, const T2& b)
420 { return a.integer - b.integer; }
421 T2& operator-=(T2& t, int i)
422 { t.integer -= i; return t; }
423 T2& operator-=(T2& a, const T2& b)
424 { a.integer -= b.integer; return a; }
425
426 T2 operator*(const T2 t, int i)
427 { return t.integer * i; }
428 T2 operator*(const T2 a, const T2& b)
429 { return a.integer * b.integer; }
430 T2& operator*=(T2& t, int i)
431 { t.integer *= i; return t; }
432 T2& operator*=(T2& a, const T2& b)
433 { a.integer *= b.integer; return a; }
434
435 T2 operator/(const T2 t, int i)
436 { return t.integer / i; }
437 T2 operator/(const T2 a, const T2& b)
438 { return a.integer / b.integer; }
439 T2& operator/=(T2& t, int i)
440 { t.integer /= i; return t; }
441 T2& operator/=(T2& a, const T2& b)
442 { a.integer /= b.integer; return a; }
443
444 T2 operator%(const T2 t, int i)
445 { return t.integer % i; }
446 T2 operator%(const T2 a, const T2& b)
447 { return a.integer % b.integer; }
448 T2& operator%=(T2& t, int i)
449 { t.integer %= i; return t; }
450 T2& operator%=(T2& a, const T2& b)
451 { a.integer %= b.integer; return a; }
452
453 template<class T>
454 class T5 {
455 public:
456     T5(int);
457     T5(const T5<T>&);
458     ~T5();
459     static void* operator new(size_t) throw ();
460     static void operator delete(void *pointer);
461     int value();
462     
463     static T X;
464     T x;
465     int val;
466 };
467
468 template<class T>
469 T5<T>::T5(int v)
470 { val = v; }
471
472 template<class T>
473 T5<T>::T5(const T5<T>&)
474 {}
475
476 template<class T>
477 T5<T>::~T5()
478 {}
479
480 template<class T>
481 void*
482 T5<T>::operator new(size_t) throw ()
483 { return 0; }
484
485 template<class T>
486 void
487 T5<T>::operator delete(void *pointer)
488 { }
489
490 template<class T>
491 int
492 T5<T>::value()
493 { return val; }
494
495 template <class T>
496 T GetMax (T a, T b) {
497   T result;
498   result = (a>b)? a : b;
499   // set breakpoint on a line with no real code
500   return (result);
501 }
502
503 #if ! defined(__GNUC__) || defined(GCC_BUG)
504 template<class T>
505 T T5<T>::X;
506 #endif
507
508
509
510
511 T5<char> t5c(1);
512 T5<int> t5i(2);
513 T5<int (*)(char, void *)> t5fi1(3);
514 T5<int (*)(int, double **, void *)> t5fi2(4);
515
516  
517
518
519
520
521 class x {
522 public:
523     int (*manage[5])(double,
524                      void *(*malloc)(unsigned size),
525                      void (*free)(void *pointer));
526     int (*device[5])(int open(const char *, unsigned mode, unsigned perms, int extra), 
527                      int *(*read)(int fd, void *place, unsigned size),
528                      int *(*write)(int fd, void *place, unsigned size),
529                      void (*close)(int fd));
530 };
531 T5<x> t5x(5);
532
533 #if !defined(__GNUC__) || (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 6)
534 template class T5<char>;
535 template class T5<int>;
536 template class T5<int (*)(char, void *)>;
537 template class T5<int (*)(int, double **, void *)>;
538 template class T5<x>;
539 #endif
540
541 class T7 {
542 public:
543     static int get();
544     static void put(int);
545 };
546
547 int
548 T7::get()
549 { return 1; }
550
551 void
552 T7::put(int i)
553 {
554     // nothing
555 }
556
557 // More template kinds.  GDB 4.16 didn't handle these, but
558 // Wildebeest does.  Note: Assuming HP aCC is used to compile
559 // this file; with g++ or HP cfront or other compilers the
560 // demangling may not get done correctly.
561
562 // Ordinary template, to be instantiated with different types
563 template<class T>
564 class Foo {
565 public:
566   int x;
567   T t;
568   T foo (int, T);
569 };
570
571
572 template<class T> T Foo<T>::foo (int i, T tt)
573 {
574   return tt;
575 }
576
577 // Template with int parameter
578
579 template<class T, int sz>
580 class Bar {
581 public:
582   int x;
583   T t;
584   T bar (int, T);
585 };
586
587
588 template<class T, int sz> T Bar<T, sz>::bar (int i, T tt)
589 {
590   if (i < sz)
591     return tt;
592   else
593     return 0;
594 }
595
596 // function template with int parameter
597 template<class T> int dummy (T tt, int i)
598 {
599   return tt;
600 }
601
602 // Template with partial specializations
603 template<class T1, class T2>
604 class Spec {
605 public:
606   int x;
607   T1 spec (T2);
608 };
609
610 template<class T1, class T2>
611 T1 Spec<T1, T2>::spec (T2 t2)
612 {
613   return 0;
614 }
615
616 template<class T>
617 class Spec<T, T*> {
618 public:
619   int x;
620   T spec (T*);
621 };
622
623 template<class T>
624 T Spec<T, T*>::spec (T * tp)
625 {
626   return *tp;
627 }
628
629 // Template with char parameter
630 template<class T, char sz>
631 class Baz {
632 public:
633   ~Baz () { };
634   int x;
635   T t;
636   T baz (int, T);
637 };
638
639 template<class T, char sz> T Baz<T, sz>::baz (int i, T tt)
640 {
641   if (i < sz)
642     return tt;
643   else
644     return 0;
645 }
646
647 typedef Baz<int, 1> intBazOne;
648
649 // Template with char * parameter
650 template<class T, char * sz>
651 class Qux {
652 public:
653   int x;
654   T t;
655   T qux (int, T);
656 };
657
658 template<class T, char * sz> T Qux<T, sz>::qux (int i, T tt)
659 {
660   if (sz[0] == 'q')
661     return tt;
662   else
663     return 0;
664 }
665
666 // Template with a function pointer parameter
667 template<class T, int (*f)(int) >
668 class Qux1 {
669 public:
670   int x;
671   T t;
672   T qux (int, T);
673 };
674
675 template<class T, int (*f)(int)> T Qux1<T, f>::qux (int i, T tt)
676 {
677   if (f != 0)
678     return tt;
679   else
680     return 0;
681 }
682
683 // Some functions to provide as arguments to template
684 int gf1 (int a) {
685   return a * 2 + 13;
686 }
687 int gf2 (int a) {
688   return a * 2 + 26;
689 }
690
691 char string[3];
692
693
694 // Template for nested instantiations
695
696 template<class T>
697 class Garply {
698 public:
699   int x;
700   T t;
701   T garply (int, T);
702 };
703
704 template<class T> T Garply<T>::garply (int i, T tt)
705 {
706   if (i > x)
707     return tt;
708   else
709     {
710       x += i;
711       return tt;
712     }
713 }
714
715
716 int main()
717 {
718     int i;
719     long l, m, n;
720 #ifdef usestubs
721     set_debug_traps();
722     breakpoint();
723 #endif
724     i = i + 1;
725
726     // New tests added here
727
728   Foo<int> fint={0,0};
729   Foo<char> fchar={0,0};
730   Foo<volatile char *> fvpchar = {0, 0};
731
732   Bar<int, 33> bint;
733   Bar<int, (4 > 3)> bint2;
734
735   Baz<int, 's'> bazint;
736   Baz<char, 'a'> bazint2;
737
738   Qux<char, string> quxint2;
739   Qux<int, string> quxint;
740
741   Qux1<int, gf1> qux11;
742
743   int x = fint.foo(33, 47);
744   char c = fchar.foo(33, 'x');
745   volatile char * cp = fvpchar.foo(33, 0);
746   
747   int y = dummy<int> (400, 600);
748
749   int z = bint.bar(55, 66);
750   z += bint2.bar(55, 66);
751
752   c = bazint2.baz(4, 'y');
753   c = quxint2.qux(4, 'z');
754   
755   y = bazint.baz(4,3);
756   y = quxint.qux(4, 22);
757   y += qux11.qux(4, 22);
758
759   y *= gf1(y) - gf2(y);
760   
761   Spec<int, char> sic;
762   Spec<int, int *> siip;
763
764   sic.spec ('c');
765   siip.spec (&x);
766
767   Garply<int> f;
768   Garply<char> fc;
769   f.x = 13;
770
771   Garply<Garply<char> > nf;
772   nf.x = 31;
773   
774   x = f.garply (3, 4);
775   
776   fc = nf.garply (3, fc);
777
778   y = x + fc.x;
779   
780   i=GetMax<int>(x,y);
781   n=GetMax<long>(l,m);
782
783   intBazOne ibo;
784   z = ibo.baz (2, 21);
785
786   t5i.value();
787
788   return 0;
789 }
790
791
792
793
794
795
796
797
798
799
800
801
802