summaryrefslogtreecommitdiff
path: root/xsd/cxx/tree/order-processor.cxx
blob: 68eee8c07efa35b5500a9cff41199f8d65830a20 (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
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
// file      : xsde/cxx/tree/order-processor.cxx
// copyright : Copyright (c) 2006-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <iostream>

#include <cxx/tree/elements.hxx>
#include <cxx/tree/order-processor.hxx>

#include <xsd-frontend/semantic-graph.hxx>
#include <xsd-frontend/traversal.hxx>

using namespace std;

namespace CXX
{
  namespace Tree
  {
    namespace
    {
      struct Member: Traversal::Element, Traversal::Any
      {
        Member (size_t count): count_ (count) {}

        virtual void
        traverse (SemanticGraph::Element& e)
        {
          if (Context::skip (e))
            return;

          e.context ().set ("ordered-id", count_++);
        }

        virtual void
        traverse (SemanticGraph::Any& a)
        {
          a.context ().set ("ordered-id", count_++);
        }

        size_t count_;
      };

      //
      //
      struct Type: Traversal::Complex
      {
        Type (TypeNameSet& ordered_types, bool derived, bool mixed, bool all)
            : ordered_types_ (ordered_types),
              derived_ (derived),
              mixed_ (mixed),
              all_ (all)
        {
        }

        virtual void
        traverse (SemanticGraph::Complex& c)
        {
          SemanticGraph::Context& ctx (c.context ());

          if (!ctx.count ("ordered"))
          {
            // First process our base.
            //
            if (c.inherits_p ())
            {
              SemanticGraph::Type& b (c.inherits ().base ());

              if (!b.context ().count ("ordered"))
                dispatch (b);
            }

            // See if our base (not necessarily immediate) is ordered.
            //
            using SemanticGraph::Complex;

            Complex* b (0);

            for (Complex* p (&c); p->inherits_p ();)
            {
              if ((b = dynamic_cast<Complex*> (&p->inherits ().base ())))
              {
                if (Context::ordered_p (*b))
                  break;

                p = b;
              }
              else
                break;
            }

            bool o (all_ ||
                    (derived_ && b != 0 && Context::ordered_p (*b)) ||
                    (mixed_ && c.mixed_p ()) ||
                    ordered_types_.find (c));
            ctx.set ("ordered", o);

            // Assign ids to elements and wildcards, calculate total count.
            //
            if (o)
            {
              size_t count (
                b != 0 && Context::ordered_p (*b)
                ? b->context ().get<size_t> ("ordered-count")
                : 1);

              ctx.set ("ordered-start", count);

              Member m (count);
              Traversal::Names n (m);
              names (c, n);

              // Assign content id for mixed text.
              //
              if (Context::mixed_p (c) && count == 1)
                ctx.set ("mixed-ordered-id", m.count_++);

              ctx.set ("ordered-count", m.count_);
            }
          }
        }

      private:
        TypeNameSet& ordered_types_;
        bool derived_;
        bool mixed_;
        bool all_;
      };

      // Go into sourced/included/imported schemas while making sure
      // we don't process the same stuff more than once.
      //
      struct Uses: Traversal::Sources,
                   Traversal::Includes,
                   Traversal::Imports
      {
        Uses (char const* seen_key)
            : seen_key_ (seen_key)
        {
        }

        virtual void
        traverse (SemanticGraph::Sources& sr)
        {
          SemanticGraph::Schema& s (sr.schema ());

          if (!s.context ().count (seen_key_))
          {
            s.context ().set (seen_key_, true);
            Traversal::Sources::traverse (sr);
          }
        }

        virtual void
        traverse (SemanticGraph::Includes& i)
        {
          SemanticGraph::Schema& s (i.schema ());

          if (!s.context ().count (seen_key_))
          {
            s.context ().set (seen_key_, true);
            Traversal::Includes::traverse (i);
          }
        }

        virtual void
        traverse (SemanticGraph::Imports& i)
        {
          SemanticGraph::Schema& s (i.schema ());

          if (!s.context ().count (seen_key_))
          {
            s.context ().set (seen_key_, true);
            Traversal::Imports::traverse (i);
          }
        }

      private:
        char const* seen_key_;
      };

      char const* seen_key = "cxx-tree-order-processor-seen";

      bool
      process_impl (options const& ops,
                    SemanticGraph::Schema& tu,
                    SemanticGraph::Path const&)
      {
        // Prepare a set of ordered types.
        //
        TypeNameSet ordered_types (ops.ordered_type ().begin (),
                                   ops.ordered_type ().end ());

        // Root schema in the file-per-type mode is just a bunch
        // of includes without a namespace.
        //
        SemanticGraph::Schema::NamesIterator i (tu.names_begin ());

        // Nothing to do if this is the XML Schema namespace.
        //
        if (i == tu.names_end () ||
            i->named ().name () != L"http://www.w3.org/2001/XMLSchema")
        {
          // Note that we check first if this schema has already been
          // processed which may happen in the file-per-type compilation
          // mode.
          //
          if (!tu.context ().count (seen_key))
          {
            Traversal::Schema schema;
            Uses uses (seen_key);

            schema >> uses >> schema;

            Traversal::Names schema_names;
            Traversal::Namespace ns;
            Traversal::Names ns_names;
            Type type (ordered_types,
                       ops.ordered_type_derived (),
                       ops.ordered_type_mixed (),
                       ops.ordered_type_all ());

            schema >> schema_names >> ns >> ns_names >> type;

            // Some twisted schemas do recusive self-inclusion.
            //
            tu.context ().set (seen_key, true);

            schema.dispatch (tu);
          }
        }

        return true;
      }
    }

    bool OrderProcessor::
    process (options const& ops,
             SemanticGraph::Schema& tu,
             SemanticGraph::Path const& file)
    {
      return process_impl (ops, tu, file);
    }
  }
}