aboutsummaryrefslogtreecommitdiff
path: root/xsde/cxx/hybrid/aggregate-include.hxx
blob: bd77e9a8b64d944d4af06a27f35b85a64dba2e1f (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
// file      : xsde/cxx/hybrid/aggregate-include.hxx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#ifndef CXX_HYBRID_AGGREGATE_INCLUDE_HXX
#define CXX_HYBRID_AGGREGATE_INCLUDE_HXX

#include <set>

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

#include <cxx/hybrid/elements.hxx>

namespace CXX
{
  namespace Hybrid
  {
    // Parser/serializer implementation includes for additional
    // schemas (polymorphic code).
    //

    // For base types we only want member's types, but not the
    // base itself.
    //
    struct BaseInclude: Traversal::Complex, Context
    {
      BaseInclude (Context& c)
          : Context (c)
      {
      }

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

        if (!restriction_p (c))
        {
          names (c);
          contains_compositor (c);
        }
      }
    };

    struct TypeInclude: Traversal::Type,
                        Traversal::Complex,
                        Context
    {
      TypeInclude (Context& c)
          : Context (c), base_ (c)
      {
        *this >> inherits_ >> base_ >> inherits_;

        *this >> contains_compositor_;
        base_ >> contains_compositor_;

        *this >> names_;
        base_ >> names_;

        contains_compositor_ >> compositor_;
        compositor_ >> contains_particle_;
        contains_particle_ >> compositor_;
        contains_particle_ >> particle_;

        names_ >> attribute_;

        particle_ >> belongs_;
        attribute_ >> belongs_;
        belongs_ >> *this;
      }

      virtual void
      traverse (SemanticGraph::Type& t)
      {
        if (types_.find (&t) != types_.end ())
          return;

        types_.insert (&t);

        if (polymorphic (t))
          collect (t);
      }

      virtual void
      traverse (SemanticGraph::Complex& c)
      {
        if (types_.find (&c) != types_.end ())
          return;

        types_.insert (&c);

        if (polymorphic (c))
          collect (c);

        inherits (c);

        if (!restriction_p (c))
        {
          names (c);
          contains_compositor (c);
        }
      }

    private:
      virtual void
      collect (SemanticGraph::Type& t)
      {
        using SemanticGraph::Type;

        for (Type::BegetsIterator i (t.begets_begin ());
             i != t.begets_end ();
             ++i)
        {
          Type& d (i->derived ());
          emit (d);
          dispatch (d);
          collect (d);
        }
      }

      virtual void
      emit (SemanticGraph::Type& t)
      {
        using SemanticGraph::Schema;

        Schema* s (&dynamic_cast<Schema&> (t.scope ().scope ()));

        // If this is not a top-level schema, get one that
        // includes/import/sources this schema. Top-level schema
        // is either not used by any other schema or is imported
        // into a special schema that doesn't have a namespace.
        //
        for (;;)
        {
          if (!s->used_p ())
            break;

          SemanticGraph::Uses& u (*s->used_begin ());
          Schema& us (u.user ());

          if (us.names_begin () == us.names_end ())
            break;

          s = &us;
        }


        if (s != &schema_root && schemas_.find (s) == schemas_.end ())
        {
          schemas_.insert (s);

          SemanticGraph::Path path (s->used_begin ()->path ());
          path.normalize ();

          // Try to use the portable representation of the path. If that
          // fails, fall back to the native representation.
          //
          NarrowString path_str;
          try
          {
            path_str = path.posix_string ();
          }
          catch (SemanticGraph::InvalidPath const&)
          {
            path_str = path.string ();
          }

          String inc_path (hxx_expr->replace (path_str));
          os << "#include " << process_include_path (inc_path) << endl
             << endl;
        }
      }

    private:
      std::set<SemanticGraph::Type*> types_;
      std::set<SemanticGraph::Schema*> schemas_;

      BaseInclude base_;
      Traversal::Inherits inherits_;

      Traversal::Compositor compositor_;
      Traversal::Element particle_;
      Traversal::ContainsCompositor contains_compositor_;
      Traversal::ContainsParticle contains_particle_;

      Traversal::Names names_;
      Traversal::Attribute attribute_;

      Traversal::Belongs belongs_;
    };

    struct AggregateInclude: Traversal::Type,
                             Traversal::Element,
                             Context
    {
      AggregateInclude (Context& c, char const* key)
          : Context (c), key_ (key), type_include_ (c)
      {
      }

      virtual void
      traverse (SemanticGraph::Type& t)
      {
        if (t.context ().count (key_))
          type_include_.dispatch (t);
      }

      virtual void
      traverse (SemanticGraph::Element& e)
      {
        if (e.context ().count (key_))
          type_include_.dispatch (e.type ());
      }

    private:
      char const* key_;
      TypeInclude type_include_;
    };
  }
}

#endif // CXX_HYBRID_AGGREGATE_INCLUDE_HXX