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
|
// file : cli/name-processor.cxx
// author : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license : MIT; see accompanying LICENSE file
#include <set>
#include <sstream>
#include "context.hxx"
#include "name-processor.hxx"
using namespace std;
namespace
{
typedef set<string> name_set;
typedef ::context context_base;
struct context: context_base
{
context (context_base& c): context_base (c) {}
context (context& c): context_base (c) {}
public:
string
find_name (string const& n, string const& suffix, name_set& set)
{
string name (escape (n + suffix));
for (size_t i (1); set.find (name) != set.end (); ++i)
{
ostringstream os;
os << i;
name = escape (n + os.str () + suffix);
}
set.insert (name);
return name;
}
string
find_name (string const& n, name_set& set)
{
return find_name (n, "", set);
}
};
struct primary_option: traversal::option, context
{
primary_option (context& c, name_set& set)
: context (c), set_ (set)
{
}
virtual void
traverse (type& o)
{
string n (o.name ()), name;
// Get rid of leading special characters, e.f., -, --, /, etc.
//
for (size_t i (0); i < n.size (); ++i)
{
if (isalpha (n[i]) || n[i] == '_')
{
name.assign (n.c_str (), i, n.size () - i);
break;
}
}
o.context ().set ("name", find_name (name, set_));
}
private:
name_set& set_;
};
struct intermediate_option: traversal::option, context
{
intermediate_option (context& c, name_set& set)
: context (c), set_ (set)
{
}
virtual void
traverse (type& o)
{
if (specifier && o.type ().name () != "bool")
{
semantics::context& oc (o.context ());
string const& base (oc.get<string> ("name"));
oc.set ("specifier", find_name (base + "_specified", set_));
}
}
private:
name_set& set_;
};
struct secondary_option: traversal::option, context
{
secondary_option (context& c, name_set& set)
: context (c), set_ (set)
{
}
virtual void
traverse (type& o)
{
semantics::context& oc (o.context ());
string const& base (oc.get<string> ("name"));
oc.set ("member", find_name (base + "_", set_));
if (specifier && o.type ().name () != "bool")
{
string const& base (oc.get<string> ("specifier"));
oc.set ("specifier-member", find_name (base + "_", set_));
}
}
private:
name_set& set_;
};
struct class_: traversal::class_, context
{
class_ (context& c) : context (c) {}
virtual void
traverse (type& c)
{
semantics::context& cc (c.context ());
cc.set ("member-name-set", name_set ());
name_set& member_set (cc.get<name_set> ("member-name-set"));
member_set.insert (escape (c.name ()));
// First assign primary names.
//
{
primary_option option (*this, member_set);
traversal::names names (option);
class_::names (c, names);
}
// Then assign intermediate names.
//
{
intermediate_option option (*this, member_set);
traversal::names names (option);
class_::names (c, names);
}
// Finally assign secondary names.
//
{
secondary_option option (*this, member_set);
traversal::names names (option);
class_::names (c, names);
}
}
};
void
process_names_ (context_base& c)
{
context ctx (c);
traversal::cli_unit unit;
traversal::names unit_names;
traversal::namespace_ ns;
class_ cl (ctx);
unit >> unit_names >> ns;
unit_names >> cl;
traversal::names ns_names;
ns >> ns_names >> ns;
ns_names >> cl;
unit.dispatch (ctx.unit);
}
}
void
process_names (context_base& c)
{
process_names_ (c);
}
|