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
|
// file : xsd-frontend/transformations/enum-synthesis.cxx
// copyright : Copyright (c) 2006-2014 Code Synthesis Tools CC
// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
#include <set>
#include <xsd-frontend/semantic-graph.hxx>
#include <xsd-frontend/traversal.hxx>
#include <xsd-frontend/transformations/enum-synthesis.hxx>
namespace XSDFrontend
{
namespace
{
typedef std::set<String> Enumerators;
struct Enumerator: Traversal::Enumerator
{
Enumerator (SemanticGraph::Schema& s,
SemanticGraph::Enumeration& e,
Enumerators& enumerators)
: schema_ (s), enum_ (e), enumerators_ (enumerators)
{
}
virtual void
traverse (Type& e)
{
String const& name (e.name ());
if (enumerators_.find (name) == enumerators_.end ())
{
enumerators_.insert (name);
// Clone the enumerator and add it to enum_.
//
Type& c (schema_.new_node<Type> (e.file (), e.line (), e.column ()));
schema_.new_edge<SemanticGraph::Names> (enum_, c, name);
schema_.new_edge<SemanticGraph::Belongs> (c, enum_);
if (e.annotated_p ())
schema_.new_edge<SemanticGraph::Annotates> (e.annotation (), c);
}
}
private:
SemanticGraph::Schema& schema_;
SemanticGraph::Enumeration& enum_;
Enumerators& enumerators_;
};
//
//
struct Union: Traversal::Union
{
Union (SemanticGraph::Schema& schema)
: schema_ (schema)
{
}
virtual void
traverse (Type& u)
{
using SemanticGraph::Enumeration;
SemanticGraph::Context& uc (u.context ());
if (uc.count ("xsd-frontend-enum-synthesis-processed"))
return;
uc.set ("xsd-frontend-enum-synthesis-processed", true);
// First see if this union is suitable for synthesis.
//
SemanticGraph::Type* base (0);
for (Type::ArgumentedIterator i (u.argumented_begin ());
i != u.argumented_end (); ++i)
{
if (i->type ().is_a<SemanticGraph::Union> ())
{
// See if we can synthesize an enum for this union. This
// call can change the value i->type() returns.
//
dispatch (i->type ());
}
SemanticGraph::Type& t (i->type ());
if (!t.is_a<Enumeration> ())
return;
// Make sure all the enums have a common base.
//
if (base == 0)
base = &t;
else
{
// Traverse the inheritance hierarchy until we fine a
// common base.
//
while (base != 0)
{
SemanticGraph::Type* b (&t);
for (; b != base && b->inherits_p ();
b = &b->inherits ().base ()) ;
if (base == b)
break;
// Could not find any match on this level. Go one step
// lower and try again.
//
base = base->inherits_p () ? &base->inherits ().base () : 0;
}
if (base == 0)
return; // No common base.
}
}
if (base == 0)
return; // Empty union.
// So this union is suitable for synthesis. Base variable points
// to the "most-derived" common base type.
//
Enumeration& e (schema_.new_node<Enumeration> (
u.file (), u.line (), u.column ()));
schema_.new_edge<SemanticGraph::Restricts> (e, *base);
// Copy enumerators from the member enums.
//
{
Enumerators set;
Traversal::Enumeration en;
Traversal::Names names;
Enumerator er (schema_, e, set);
en >> names >> er;
for (Type::ArgumentedIterator i (u.argumented_begin ());
i != u.argumented_end (); ++i)
{
en.dispatch (i->type ());
}
}
// Reset edges pointing to union to point to enum.
//
if (u.annotated_p ())
{
schema_.reset_right_node (u.annotated (), e);
schema_.add_edge_right (e, u.annotated ());
}
schema_.reset_right_node (u.named (), e);
schema_.add_edge_right (e, u.named ());
for (Type::ClassifiesIterator i (u.classifies_begin ()),
end (u.classifies_end ()); i != end; ++i)
{
schema_.reset_right_node (*i, e);
schema_.add_edge_right (e, *i);
}
for (Type::BegetsIterator i (u.begets_begin ()),
end (u.begets_end ()); i != end; ++i)
{
schema_.reset_right_node (*i, e);
schema_.add_edge_right (e, *i);
}
for (Type::ArgumentsIterator i (u.arguments_begin ()),
end (u.arguments_end ()); i != end; ++i)
{
schema_.reset_left_node (*i, e);
schema_.add_edge_left (e, *i);
}
// Remove Arguments edges pointing to the union.
//
while (u.argumented_begin () != u.argumented_end ())
{
SemanticGraph::Arguments& a (*u.argumented_begin ());
schema_.delete_edge (a.type (), a.specialization (), a);
}
// Copy context and delete the union node.
//
e.context ().swap (uc);
schema_.delete_node (u);
}
private:
SemanticGraph::Schema& schema_;
};
// Go into implied/included/imported schemas while making sure
// we don't process the same stuff more than once.
//
struct Uses: Traversal::Uses
{
virtual void
traverse (Type& u)
{
SemanticGraph::Schema& s (u.schema ());
if (!s.context ().count ("xsd-frontend-enum-synthesis-seen"))
{
s.context ().set ("xsd-frontend-enum-synthesis-seen", true);
Traversal::Uses::traverse (u);
}
}
};
}
namespace Transformations
{
void EnumSynthesis::
transform (SemanticGraph::Schema& s, SemanticGraph::Path const&)
{
Traversal::Schema schema;
Uses uses;
schema >> uses >> schema;
Traversal::Names schema_names;
Traversal::Namespace ns;
Traversal::Names ns_names;
Union u (s);
schema >> schema_names >> ns >> ns_names >> u;
// Some twisted schemas do recusive inclusions.
//
s.context ().set ("xsd-frontend-enum-synthesis-seen", true);
schema.dispatch (s);
}
}
}
|