aboutsummaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.compile/cp-simple-template.cc
blob: ec46694757bb69474f5ed2f800c1384da73d6e41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Copyright 2016 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

// NOTE: We cannot currently use namespaces until namespace-qualified
// symbol lookups are fixed in gdb

template <typename T, int V>
T mytemplate (int a)
{
  return static_cast<T> (a) + V;
}

template <typename T, int V>
T mytemplate (void)
{
  return -V;
}

template <int V = 100>
int mytemplate (void)
{
  return V;
}

struct A
{
  A (int val) : value (val) { }
  operator int () const { return value; }

  template <typename T = A>
  T tempmethod (void)
  {
    return value;
  }

  template <typename T = A, int V = -1>
  static T stempmethod (void)
  {
    return V;
  }

  template <typename T = A, int V = -2>
  static T stempmethod (T arg)
  {
    return arg + V;
  }

  int value;
};

template<>
int
A::tempmethod (void)
{
  return -value;
}

template <typename T>
T deduct (T a)
{
  return a;
}

extern char const g_str[] = "hello";

template <typename T>
int mod_test (T a) { return 1; }

template <typename T>
int mod_test (T* const a) { return 2; }

template <typename T>
int mod_test (T const* const a) { return 3; }

#if 0
/* This chaining of defaults has no good representation in the debug info.
   For each instance where T2 defaulted to T1, we will have as many
   default values in the debug info for T2, one for each such instance.  */
template <typename T1 = int, typename T2 = T1, typename T3 = T2,
	  int V1 = 10, int V2 = 20, const char* V3 = g_str>
T1 defaultvals (void)
{
  return static_cast<T1> (V1);
}
#else
template <typename T = A, int V = 10, const char* S = g_str>
T defaultvals (void)
{
  return static_cast<T> (V);
}
#endif

// A handful of operator templates
struct O
{
  O (int v) : v_ (v) { }

  template <typename T>
  operator T (void) { return -v_; }

  template <typename T>
  O operator+ (T val)
  {
    return v_ + val;
  }

  int v_;
};

template <typename T>
const T** ret_test (void) { return 0; }

template <typename T>
T const* const* ret2_test (void) { return 0; }

// Some simple class templates
template <typename T1 = O, typename T2 = int, int V = 3>
class classt
{
public:
  classt (T1 v) : val1_ (v), val2_ (107) { }
  T1 get1 (void) const { return val1_; }
  T2 get2 (void) const { return val2_; }
  int get3 (void) const { return V; }

private:
  T1 val1_;
  T2 val2_;
};

int
main (void)
{
  A a (20);
  O o (30);
  int var = 0xdeadbeef;
  int i = 1;
  const int j = 1;
  int* pi = &i;
  int const* const cpci = &j;
  int *const cpi = &i;

  int o_val = o + 30;
  int mod_value = mod_test (i) + mod_test (cpci) + mod_test (cpi);
  const char** cp = ret_test<char> ();
  char const* const* ccp = ret2_test<char> ();

  classt<> cddd (o);
  classt<int> cdd (100);
  classt<int, char> cd (101);
  classt<int, char, 12> c (102);
  int cvals = cddd.get1 () + cddd.get2 () + cddd.get3 ();
  cvals += cdd.get1 () + cdd.get2 () + cdd.get3 ();
  cvals += cd.get1 () + cd.get2 () + cd.get3 ();
  cvals += c.get1 () + c.get2 () + c.get3 ();

  return mytemplate<int, 1> (0)
    + mytemplate<int, 1> ()
    + mytemplate<0> ()
    + mytemplate ()
    + a.tempmethod ()
    + a.tempmethod<int> ()
    + A::stempmethod ()
    + A::stempmethod (0)
    + defaultvals ()
    + defaultvals<int, 20> ()
    + deduct (0); // break here
}