aboutsummaryrefslogtreecommitdiff
path: root/xsd/processing/inheritance/processor.cxx
blob: 59ff63b184f4a8f6e6dc4f2327d1ab04e0f21296 (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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// file      : processing/inheritance/processor.cxx
// copyright : Copyright (c) 2006-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <set>
#include <iostream>

#include <processing/inheritance/processor.hxx>

#include <elements.hxx>

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

using namespace std;

namespace Processing
{
  namespace SemanticGraph = XSDFrontend::SemanticGraph;
  namespace Traversal = XSDFrontend::Traversal;

  namespace Inheritance
  {
    namespace
    {
      struct Dep
      {
        Dep (SemanticGraph::Type& t,
             SemanticGraph::Member* m = 0,
             String const& xpath = L"")
            : type (t), member (m), member_xpath (xpath)
        {
        }

        SemanticGraph::Type& type;
        SemanticGraph::Member* member; // Member if type is anonymous.
        String member_xpath;
      };

      inline bool
      operator< (Dep const& a, Dep const& b)
      {
        return &a.type < &b.type;
      }

      typedef set<Dep> DepSet;
      typedef set<SemanticGraph::Type*> TypeSet;

      String
      xpath (SemanticGraph::Nameable& n)
      {
        if (dynamic_cast<SemanticGraph::Namespace*> (&n) != 0)
          return L"<namespace-level>"; // There is a bug if you see this.

        if (n.named_p ())
        {
          SemanticGraph::Scope& scope (n.scope ());

          if (dynamic_cast<SemanticGraph::Namespace*> (&scope) != 0)
            return n.name ();

          return xpath (scope) + L"/" + n.name ();
        }
        else
        {
          return L"(anonymous type for " +
            n.context ().get<String> ("instance-name") + L")";
        }
      }


      // Calculate the list of dependencies for this complex
      // type.
      //
      struct ComplexType: Traversal::Complex,
                          Traversal::Member
      {
        ComplexType (DepSet& dep_set)
            : dep_set_ (dep_set), last_ (0)
        {
          *this >> names_ >> *this;
        }

        virtual void
        traverse (SemanticGraph::Complex& c)
        {
          using SemanticGraph::Complex;

          if (c.inherits_p ())
            dep_set_.insert (Dep (c.inherits ().base (), last_, last_xpath_));

          types_seen_.insert (&c);

          // Go after anonymous types.
          //
          names (c);
        }

        virtual void
        traverse (SemanticGraph::Member& m)
        {
          SemanticGraph::Type& t (m.type ());

          if (!t.named_p () && types_seen_.find (&t) == types_seen_.end ())
          {
            SemanticGraph::Context& ctx (t.context ());

            last_xpath_ = xpath (m);

            String prev_xpath;

            if (ctx.count ("instance-name"))
              prev_xpath = ctx.get<String> ("instance-name");

            ctx.set ("instance-name", last_xpath_);

            last_ = &m;
            dispatch (t);

            if (prev_xpath)
              ctx.set ("instance-name", prev_xpath);
            else
              ctx.remove ("instance-name");
          }
        }

      private:
        DepSet& dep_set_;
        TypeSet types_seen_;

        SemanticGraph::Member* last_;
        String last_xpath_;

        Traversal::Names names_;
      };


      //
      //
      template <typename N, typename A>
      struct NodeArgs
      {
        NodeArgs (N& node, A arg)
            : node_ (node), arg_ (arg)
        {
        }

        operator N& () const
        {
          return node_;
        }

        template <typename E>
        void
        add_edge_left (E& e)
        {
          node_.add_edge_left (e, arg_);
        }

        template <typename E>
        void
        add_edge_right (E& e)
        {
          node_.add_edge_right (e, arg_);
        }

      private:
        N& node_;
        A arg_;
      };


      //
      //
      struct Global: Traversal::Type,
                     Traversal::Complex,
                     Traversal::Element
      {
        Global (SemanticGraph::Schema& root,
                SemanticGraph::Schema& schema,
                bool& failed)
            : root_ (root), schema_ (schema), failed_ (failed)
        {
        }

        virtual void
        traverse (SemanticGraph::Type& t)
        {
          if (t.named_p ())
            types_seen_.insert (&t);
        }

        virtual void
        traverse (SemanticGraph::Complex& c)
        {
          check_dep (c, c);
          types_seen_.insert (&c);
        };

        virtual void
        traverse (SemanticGraph::Element& e)
        {
          SemanticGraph::Type& t (e.type ());

          if (!t.named_p ())
          {
            t.context ().set ("instance-name", xpath (e));
            check_dep (e, t);
            t.context ().remove ("instance-name");
          }
        };

      private:
        void
        check_dep (SemanticGraph::Nameable& global,
                   SemanticGraph::Type& type)
        {
          using SemanticGraph::Type;
          using SemanticGraph::Scope;
          using SemanticGraph::Names;
          using SemanticGraph::Schema;

          DepSet prereqs;

          // Calculate our prerequisistes.
          //
          {
            ComplexType complex (prereqs);
            complex.dispatch (type);
          }

          for (DepSet::const_iterator i (prereqs.begin ());
               i != prereqs.end (); ++i)
          {
            Dep const& dep (*i);
            Type& t (dep.type);

            // Ignore IDREF templates.
            //
            if (!t.named_p () &&
                (t.is_a<SemanticGraph::Fundamental::IdRef> () ||
                 t.is_a<SemanticGraph::Fundamental::IdRefs> ()))
              continue;

            // We won't be able to generate compilable code in case of a
            // dependency on ourselves (e.g., a member element with
            // anonymous type that inherits from us).
            //
            if (&t == &type)
            {
              assert (dep.member != 0);

              SemanticGraph::Member& m (*dep.member);

              wcerr << t.file () << ":" << t.line () << ":" << t.column ()
                    << " error: nested anonymous type for '"
                    << dep.member_xpath << "' cyclicly inherits from '"
                    << t.name () << "'" << endl;

              wcerr << t.file () << ":" << t.line () << ":" << t.column ()
                    << " error: unable to generate valid code for such "
                    << "cyclic inheritance" << endl;

              wcerr << m.file () << ":" << m.line () << ":" << m.column ()
                    << " info: '" << m.name () << "' element is declared here"
                    << endl;

              wcerr << t.file () << ":" << t.line () << ":" << t.column ()
                    << ": info: consider explicitly naming this type "
                    << "or remove the --preserve-anonymous option"
                    << endl;

              failed_ = true;
              continue;
            }

            if (types_seen_.find (&t) == types_seen_.end ())
            {
              Scope& scope (t.scope ());
              Schema& schema (dynamic_cast<Schema&> (scope.scope ()));

              // Don't worry about types that are in included/imported
              // schemas.
              //
              if (&schema != &schema_ && !sources_p (schema_, schema))
                continue;

              if (t.context ().count ("seen"))
              {
                wcerr << t.file () << ":" << t.line () << ":" << t.column ()
                      << " error: nested anonymous type in '" << t.name ()
                      << "' or '" << type.name () << "' inherits from one of "
                      << "these types and makes them mutually dependant"
                      << endl;

                wcerr << t.file () << ":" << t.line () << ":" << t.column ()
                      << " error: unable to generate valid code for such "
                      << "cyclic dependency" << endl;

                wcerr << type.file () << ":" << type.line () << ":"
                      << type.column () << " info: '" << type.name ()
                      << "' type is defined here"
                      << endl;

                wcerr << t.file () << ":" << t.line () << ":" << t.column ()
                      << ": info: consider explicitly naming the anonymous "
                      << "type or remove the --preserve-anonymous option"
                      << endl;

                failed_ = true;
                continue;
              }

              //wcerr << "type '" << t.name () << "' needs to be moved " <<
              //  "before " << (global.is_a<Type> () ? "type" : "element") <<
              //  " '" << global.name () << "'" << endl;


              // Delete current Names edge.
              //
              String name (t.name ());
              {
                Names& n (t.named ());
                root_.delete_edge (scope, t, n);
              }

              // Insert a new Names edge before global.
              //
              {
                // t.scope () and global.scope () can be different in
                // case of the chameleon inclusion.
                //
                Scope& scope (global.scope ());

                // Convert to the insert-after call.
                //
                Scope::NamesIterator i (scope.find (global.named ()));

                if (i == scope.names_begin ())
                  i = scope.names_end ();
                else
                  --i;

                NodeArgs<Scope, Scope::NamesIterator> na (scope, i);
                root_.new_edge<Names> (na, t, name);
              }

              // Recursively process the moved type.
              //
              global.context ().set ("seen", true);
              dispatch (t);
              global.context ().remove ("seen");
            }
          }
        }

      private:
        // Return true if root sources s.
        //
        bool
        sources_p (SemanticGraph::Schema& root, SemanticGraph::Schema& s)
        {
          using SemanticGraph::Schema;
          using SemanticGraph::Sources;

          for (Schema::UsesIterator i (root.uses_begin ());
               i != root.uses_end (); ++i)
          {
            if (i->is_a<Sources> ())
            {
              if (&i->schema () == &s || sources_p (i->schema (), s))
                return true;
            }
          }

          return false;
        }

      private:
        SemanticGraph::Schema& root_;
        SemanticGraph::Schema& schema_;
        TypeSet types_seen_;
        bool& failed_;
      };


      // Sources traverser that goes into each schema only once.
      //
      struct Sources: Traversal::Sources
      {
        virtual void
        traverse (SemanticGraph::Sources& s)
        {
          if (schemas_.insert (&s.schema ()).second)
            Traversal::Sources::traverse (s);
        }

      private:
        set<SemanticGraph::Schema*> schemas_;
      };

      // Go into included/imported schemas while making sure we don't
      // process the same stuff more than once.
      //
      struct Uses: Traversal::Includes, Traversal::Imports
      {
        Uses (SemanticGraph::Schema& root, bool& failed)
            : root_ (root), failed_ (failed)
        {
        }

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

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

      private:
        void
        traverse (SemanticGraph::Schema& s)
        {
          if (!s.context ().count ("processing-inheritance-seen"))
          {
            Traversal::Schema schema;
            Sources sources;

            schema >> sources >> schema;
            schema >> *this;

            Traversal::Names schema_names;
            Traversal::Namespace ns;
            Traversal::Names ns_names;

            schema >> schema_names >> ns >> ns_names;

            Global global (root_, s, failed_);

            ns_names >> global;

            s.context ().set ("processing-inheritance-seen", true);
            schema.dispatch (s);
          }
        }

      private:
        SemanticGraph::Schema& root_;
        bool& failed_;
      };
    }

    void Processor::
    process (SemanticGraph::Schema& tu, SemanticGraph::Path const&)
    {
      bool failed (false);

      // We need to process include/imported schemas since other
      // parts of the process, for example, name processors can
      // rely on the order of types in the schema.
      //
      Traversal::Schema schema;
      Sources sources;
      Uses uses (tu, failed);

      schema >> sources >> schema;
      schema >> uses;

      Traversal::Names schema_names;
      Traversal::Namespace ns;
      Traversal::Names ns_names;

      schema >> schema_names >> ns >> ns_names;

      Global global (tu, tu, failed);

      ns_names >> global;

      // Some twisted schemas do recusive self-inclusion.
      //
      tu.context ().set ("processing-inheritance-seen", true);

      schema.dispatch (tu);

      if (failed)
        throw Failed ();
    }
  }
}