summaryrefslogtreecommitdiff
path: root/libxsd-frontend/xsd-frontend/generators/dependencies.cxx
blob: 55e7ae8a9a2f1cfa0cd3bf86d3cf96379cbeb012 (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
// file      : xsd-frontend/generators/dependencies.cxx
// copyright : Copyright (c) 2006-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

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

#include <xsd-frontend/generators/dependencies.hxx>

namespace XSDFrontend
{
  typedef std::vector<SemanticGraph::Path> Paths;

  namespace
  {
    // Go into included/imported (but not implied) schemas while making
    // sure we don't process the same stuff more than once.
    //
    struct Uses: Traversal::Uses
    {
      Uses (Paths& p): paths_ (p) {}

      virtual void
      traverse (Type& u)
      {
        if (u.is_a<SemanticGraph::Implies> ())
          return;

        SemanticGraph::Schema& s (u.schema ());

        if (!s.context ().count ("xsd-frontend-dependencies-seen"))
        {
          s.context ().set ("xsd-frontend-dependencies-seen", true);

          // While the edge contains the exact path that was found in the
          // schema, the schema node itself has the reative to the including
          // or importing schema path, which is what we want.
          //
          paths_.push_back (s.file ());
          Traversal::Uses::traverse (u);
        }
      }

    private:
      Paths& paths_;
    };
  }

  namespace Generators
  {
    Paths Dependencies::
    generate (SemanticGraph::Schema& s, SemanticGraph::Path const& p)
    {
      Paths r;
      r.push_back (p);

      Traversal::Schema schema;
      Uses uses (r);

      schema >> uses >> schema;

      // Some twisted schemas do recusive inclusions.
      //
      s.context ().set ("xsd-frontend-dependencies-seen", true);

      schema.dispatch (s);
      return r;
    }
  }
}