aboutsummaryrefslogtreecommitdiff
path: root/xsd/cxx/tree/counter.cxx
blob: 23fc221bc785ea95637f8d69bf2813118c165ea0 (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
// file      : xsd/cxx/tree/counter.cxx
// copyright : Copyright (c) 2006-2014 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <cxx/tree/counter.hxx>

#include <iostream>

namespace CXX
{
  namespace Tree
  {
    namespace
    {
      struct Member: Traversal::Member
      {
        Member (size_t& complexity)
            : complexity_ (complexity)
        {
        }

        virtual void
        traverse (Type&)
        {
          complexity_++;
        }

        size_t& complexity_;
      };

      struct Any: Traversal::Any, Traversal::AnyAttribute
      {
        Any (size_t& complexity)
            : complexity_ (complexity)
        {
        }

        virtual void
        traverse (SemanticGraph::Any&)
        {
          complexity_++;
        }

        virtual void
        traverse (SemanticGraph::AnyAttribute&)
        {
          complexity_++;
        }

        size_t& complexity_;
      };

      struct TypeBase: Traversal::List,
                       Traversal::Union,
                       Traversal::Enumeration,
                       Traversal::Complex,
                       Context
      {
        TypeBase (Context& c, size_t& complexity)
            : Context (c), complexity_ (complexity)
        {
        }

        virtual void
        traverse (SemanticGraph::List&)
        {
          complexity_++;
        }

        virtual void
        traverse (SemanticGraph::Union&)
        {
          complexity_++;
        }

        virtual void
        traverse (SemanticGraph::Enumeration& e)
        {
          bool string_based (false);
          {
            IsStringBasedType t (string_based);
            t.dispatch (e);
          }

          complexity_ += (string_based ? 1 : 2);
        }

        virtual void
        traverse (SemanticGraph::Complex& c)
        {
          complexity_++; // One for the type itself.

          // Plus some for each member.
          //
          Any any (complexity_);
          Member member (complexity_);
          Traversal::Names names;

          names >> member;

          if (options.generate_wildcard ())
            names >> any;

          Complex::names (c, names);
        }

      private:
        size_t& complexity_;
      };


      //
      //
      struct GlobalType: Traversal::Type, Context

      {
        GlobalType (Context& c, Counts& counts)
            : Context (c), counts_ (counts)
        {
        }

        virtual void
        traverse (SemanticGraph::Type& t)
        {
          counts_.global_types++;

          size_t complexity (0);
          TypeBase type (*this, complexity);
          type.dispatch (t);

          counts_.complexity_total += complexity;
          counts_.complexity.push_back (complexity);
        }

      private:
        Counts& counts_;
      };

      //
      //
      struct GlobalElement: Traversal::Element,
                            GlobalElementBase,
                            Context
      {
        GlobalElement (Context& c, Counts& counts)
            : GlobalElementBase (c),
              Context (c),
              counts_ (counts),
              last_ (0)
        {
        }

        ~GlobalElement ()
        {
          if (last_ != 0)
          {
            last_->context ().set ("last", true);
            count_last ();
          }
        }

        virtual void
        traverse (Type& e)
        {
          // Check if the previous element we saw needs to be generated.
          //
          if (last_ != 0)
            count_last ();

          last_ = &e;

          if (counts_.global_elements == 0)
            e.context ().set ("first", true);

          counts_.global_elements++;
        }

      private:
        void
        count_last ()
        {
          if (generate_p (*last_))
          {
            counts_.generated_global_elements++;

            size_t complexity (0);

            if (doc_root_p (*last_))
            {
              if (options.generate_element_type ())
              {
                complexity += 1; // For c-tors and d-tor.

                if (!options.suppress_parsing ())
                  complexity += 1;

                if (options.generate_serialization ())
                  complexity += 1;
              }
              else
              {
                if (!options.suppress_parsing ())
                  complexity += 6; // 13 parsing functions.

                if (options.generate_serialization ())
                  complexity += 4; // 8 serialization functions.
              }
            }

            if (complexity == 0)
            {
              // This element must be a substitution group members. For
              // such elements we are only generating an entry in a map.
              // We will assign it a complexity of 1 so that we don't
              // end up with the total complexity that is less than the
              // number of elements and types.
              //
              complexity = 1;
            }

            counts_.complexity_total += complexity;
            counts_.complexity.push_back (complexity);
          }
        }

      private:
        Counts& counts_;
        SemanticGraph::Element* last_;
      };
    }

    Counts Counter::
    count (options const& ops,
           SemanticGraph::Schema& tu,
           SemanticGraph::Path const& path)
    {
      Counts counts;
      Context ctx (std::wcerr, tu, path, ops, counts, false, 0, 0, 0, 0);

      Traversal::Schema schema;
      Sources sources;

      schema >> sources >> schema;

      Traversal::Names schema_names;
      Traversal::Namespace ns;
      Traversal::Names ns_names;
      GlobalType global_type (ctx, counts);
      GlobalElement global_element (ctx, counts);

      schema >> schema_names >> ns >> ns_names;

      ns_names >> global_element;
      ns_names >> global_type;

      schema.dispatch (tu);

      return counts;
    }
  }
}