-
Notifications
You must be signed in to change notification settings - Fork 17
/
SymbolEntry.cc
353 lines (299 loc) · 6.5 KB
/
SymbolEntry.cc
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*----------------------------------------------------------------------\
| |
| __ __ ____ _____ ____ |
| \ \ / /_ _/ ___|_ _|___ \ |
| \ V / _` \___ \ | | __) | |
| | | (_| |___) || | / __/ |
| |_|\__,_|____/ |_| |_____| |
| |
| core system |
| (C) SuSE GmbH |
\-----------------------------------------------------------------------/
File: SymbolEntry.cc
symbol entry class
Author: Klaus Kaempf <kkaempf@suse.de>
Maintainer: Klaus Kaempf <kkaempf@suse.de>
/-*/
// -*- c++ -*-
#include <stdio.h>
#include <string>
using std::string;
#ifndef DO_DEBUG
#define DO_DEBUG 0
#endif
#include "ycp/y2log.h"
#include "y2/SymbolEntry.h"
#include "ycp/SymbolTable.h"
#include "ycp/YCPVoid.h"
#include "ycp/YCPCode.h"
IMPL_BASE_POINTER(SymbolEntry);
UstringHash* SymbolEntry::_nameHash = NULL;
Ustring SymbolEntry::emptyUstring = Ustring ( *( SymbolEntry::_nameHash ? SymbolEntry::_nameHash : (SymbolEntry::_nameHash = new UstringHash)), "");
#ifdef D_MEMUSAGE
void __UUsage ()
{
// fprintf (stderr, "%d Ustrings using %ld bytes\n", SymbolEntry::_nameHash.size(), SymbolEntry::_nameHash.sum());
}
#endif
/**
* constructor
*/
SymbolEntry::SymbolEntry (const Y2Namespace* name_space, unsigned int position, const char *name, category_t cat, constTypePtr type)
: m_global ((cat == c_global)||(cat == c_filename))
, m_namespace (name_space)
, m_position (position)
, m_name ( Ustring ( *( _nameHash ? _nameHash : (_nameHash = new UstringHash)), name) )
, m_category ((cat == c_filename) ? cat : (m_global ? c_unspec : cat))
, m_type (type)
, m_value (YCPNull())
, m_recurse_stack (NULL)
{
}
SymbolEntry::~SymbolEntry ()
{
}
const Y2Namespace *
SymbolEntry::nameSpace () const
{
return m_namespace;
}
void
SymbolEntry::setNamespace (const Y2Namespace *name_space)
{
m_namespace = name_space;
}
bool
SymbolEntry::isGlobal () const
{
return m_global;
}
unsigned int
SymbolEntry::position () const
{
return m_position;
}
void
SymbolEntry::setPosition (unsigned int position)
{
m_position = position;
return;
}
YCPValue
SymbolEntry::setValue (YCPValue value)
{
y2debug ("SymbolEntry::setValue (%s@%p = '%s')", m_name.asString().c_str(), this, value.isNull() ? "nil" : value->toString().c_str());
if (!value.isNull()
&& (m_category == c_reference))
{
y2debug ("C_REFERENCE");
if (value->isReference())
{
return m_value = value;
}
if (m_value.isNull()
|| !m_value->isReference())
{
y2error ("Setting uninitialized reference");
return YCPNull ();
}
return m_value->asReference()->entry()->setValue (value);
}
// use YCPVoid for nil to avoid problems with function references
if (value.isNull ())
{
value = YCPVoid ();
}
return m_value = value;
}
YCPValue
SymbolEntry::value () const
{
if ((m_category == c_reference)
&& !m_value.isNull()
&& m_value->isReference())
{
y2debug ("DE-REFERENCE");
return m_value->asReference()->entry()->value();
}
return m_value;
}
void
SymbolEntry::push ()
{
if (! m_recurse_stack)
{
m_recurse_stack = new valuestack_t;
}
m_recurse_stack->push (m_value);
}
void
SymbolEntry::pop ()
{
if (! m_recurse_stack)
return;
m_value = m_recurse_stack->top ();
m_recurse_stack->pop ();
}
const char *
SymbolEntry::name () const
{
return m_name.asString().c_str();
}
SymbolEntry::category_t
SymbolEntry::category () const
{
return m_category;
}
void
SymbolEntry::setCategory (SymbolEntry::category_t cat)
{
m_category = cat;
return;
}
constTypePtr
SymbolEntry::type () const
{
return m_type;
}
void
SymbolEntry::setType (constTypePtr type)
{
m_type = type;
return;
}
void
SymbolEntry::setGlobal (bool global)
{
m_global = global;
}
string
SymbolEntry::catString () const
{
switch (m_category)
{
case c_unspec:
return "unspecified";
break;
case c_module:
return "module";
break;
case c_variable:
return "variable";
break;
case c_reference:
return "reference";
break;
case c_function:
return "function";
break;
case c_builtin:
return "builtin";
break;
case c_typedef:
return "typedef";
break;
case c_const:
return "const";
break;
case c_namespace:
return "namespace";
break;
case c_self:
return "self";
break;
case c_filename:
return "filename";
break;
case c_predefined:
return "predefined";
break;
default:
break;
}
return "?cat?";
}
string
SymbolEntry::toString (bool with_type) const
{
string s = (with_type && m_global) ? "global " : "";
#if DO_DEBUG
y2debug ("SymbolEntry::toString %p: name '%s'", this, m_name.asString().c_str());
y2debug ("SymbolEntry::toString %p: with_type %d, cat %s, name '%s'", this, with_type, catString().c_str(), m_name.asString().c_str());
#endif
switch (m_category)
{
case c_unspec:
{
return s + catString() + " '" + m_type->toString() + " " + m_name.asString() + "'";
}
break;
case c_module:
{
return s + catString() + " \"" + m_name.asString() + "\"";
}
break;
case c_variable:
case c_reference:
case c_function:
{
#if DO_DEBUG
y2debug ("m_namespace %p[%s], m_global %d, with_type %d", m_namespace, m_namespace ? m_namespace->name().c_str() : "", m_global, with_type);
#endif
if (with_type)
{
constFunctionTypePtr ftype = m_type;
s += (((m_category == c_variable)||(m_category ==c_reference)) ? m_type->toString() : ftype->returnType()->toString()) + " ";
if (m_global
&& m_namespace != 0
&& !m_namespace->name().empty())
{
s += (m_namespace->name() + "::");
}
s += m_name.asString();
if (m_category == c_function)
{
constFunctionTypePtr type = (constFunctionTypePtr)m_type;
// are there any parameters
if (type->parameters())
{
s += " " + type->parameters ()->toString ();
}
else
{
s += " ()";
}
}
return s;
}
else
{
string s = string ((m_global && (m_namespace!=0)) ? (m_namespace->name() + "::") : "") + m_name.asString();
return s;
}
}
case c_typedef:
{
return s + catString() + " " + m_type->toString() + " " + m_name.asString() + ";";
}
break;
case c_const:
{
return s + m_type->toString() + " " + m_name.asString();
}
break;
case c_namespace:
case c_self:
case c_filename:
case c_predefined:
{
return s + catString() + " '" + m_name.asString() + "'";
}
break;
default:
{
y2error ("category %d ?", m_category);
}
break;
}
return "?SymbolEntry?";
}