aboutsummaryrefslogtreecommitdiff
path: root/xsd/cxx/parser/attribute-validation-source.cxx
blob: 0503d782bb7047a2237af23ead40cc3af9ecc57d (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
// file      : xsd/cxx/parser/attribute-validation-source.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <cxx/parser/attribute-validation-source.hxx>

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

namespace CXX
{
  namespace Parser
  {
    namespace
    {
      struct Test: Traversal::Attribute,
                   Traversal::AnyAttribute,
                   Context
      {
        Test (Context& c)
            : Context (c)
        {
        }

        virtual Void
        traverse (SemanticGraph::Attribute& a)
        {
          String const& name (a.name ());

          if (a.qualified () && a.namespace_ ().name ())
          {
            String const& ns (a.namespace_ ().name ());

            os << "n == " << L << strlit (name) << " &&" << endl
               << "ns == " << L << strlit (ns);
          }
          else
            os << "n == " << L << strlit (name) << " && ns.empty ()";
        }

        virtual Void
        traverse (SemanticGraph::AnyAttribute& a)
        {
          String const& ns (a.definition_namespace ().name ());

          for (SemanticGraph::AnyAttribute::NamespaceIterator
                 i (a.namespace_begin ()), e (a.namespace_end ()); i != e;)
          {
            if (*i == L"##any")
            {
              os << "!n.empty ()";
            }
            else if (*i == L"##other")
            {
              if (ns)
              {
                // Note that here I assume that ##other does not include
                // unqualified names in a schema with target namespace.
                // This is not what the spec says but that seems to be
                // the consensus.
                //
                os << "(!ns.empty () && ns != " << L << strlit (ns) << ")";
              }
              else
                os << "!ns.empty ()";
            }
            else if (*i == L"##local")
            {
              os << "(ns.empty () && !n.empty ())";
            }
            else if (*i == L"##targetNamespace")
            {
              os << "ns == " << L << strlit (ns);
            }
            else
            {
              os << "ns == " << L << strlit (*i);
            }

            if (++i != e)
              os << " ||" << endl;
          }
        }
      };

      //
      //
      struct PhaseOne: Traversal::Attribute, Context
      {
        PhaseOne (Context& c)
            : Context (c), test_ (c)
        {
        }

        virtual Void
        traverse (Type& a)
        {
          String const& name (ename (a));
          String const& inst (emember (a));

          SemanticGraph::Type& type (a.type ());
          String const& post (post_name (type));
          String const& ret (ret_type (type));

          os << "if (";

          test_.traverse (a);

          os << ")"
             << "{"
             << "if (this->" << inst << ")"
             << "{"
             << "this->" << inst << "->pre ();"
             << "this->" << inst << "->_pre_impl ();"
             << "this->" << inst << "->_characters (s);"
             << "this->" << inst << "->_post_impl ();";

          if (ret == L"void")
            os << "this->" << inst << "->" << post << " ();"
               << "this->" << name << " ();";
          else
            os << arg_type (type) << " tmp (this->" << inst << "->" <<
              post << " ());"
               << "this->" << name << " (tmp);"
               << endl;

          os << "}";

          if (!a.optional ())
            os << "static_cast< v_state_attr_* > (" <<
              "this->v_state_attr_stack_.top ())->" << name << " = true;";

          os << "return true;"
             << "}";
        }

      private:
        Test test_;
      };


      //
      //
      struct PhaseTwo: Traversal::AnyAttribute, Context
      {
        PhaseTwo (Context& c)
            : Context (c), test_ (c)
        {
        }

        virtual Void
        traverse (Type& a)
        {
          os << "if (";

          test_.traverse (a);

          os << ")" << endl
             << "{"
             << "this->_any_attribute (ns, n, s);"
             << "return true;"
             << "}";
        }

      private:
        Test test_;
      };


      //
      //
      struct AttributeStateInit: Traversal::Attribute, Context
      {
        AttributeStateInit (Context& c)
            : Context (c)
        {
        }

        virtual Void
        traverse (Type& a)
        {
          if (!a.optional ())
            os << "as." << ename (a) << " = false;";
        }
      };


      //
      //
      struct AttributeStateCheck: Traversal::Attribute, Context
      {
        AttributeStateCheck (Context& c)
            : Context (c)
        {
        }

        virtual Void
        traverse (Type& a)
        {
          if (!a.optional ())
          {
            String ns (a.qualified () ? a.namespace_ ().name () : String ());

            os << "if (!as." << ename (a) << ")" << endl
               << "this->_expected_attribute (" << endl
               << L << strlit (ns) << ", " << L << strlit (a.name ()) << ");";
          }
        }
      };

      //
      //
      struct Complex: Traversal::Complex, Context
      {
        Complex (Context& c)
            : Context (c),
              phase_one_ (c),
              phase_two_ (c),
              attribute_state_init_ (c),
              attribute_state_check_ (c)
        {
          names_phase_one_ >> phase_one_;
          names_phase_two_ >> phase_two_;

          names_attribute_state_init_ >> attribute_state_init_;
          names_attribute_state_check_ >> attribute_state_check_;
        }

        virtual Void
        traverse (Type& c)
        {
          Boolean has_att (has<Traversal::Attribute> (c));
          Boolean has_any (has<Traversal::AnyAttribute> (c));

          if (!has_att && !has_any)
            return;

          Boolean has_req_att (false);
          if (has_att)
          {
            RequiredAttributeTest test (has_req_att);
            Traversal::Names names_test (test);
            names (c, names_test);
          }

          String const& name (ename (c));

          os <<"// Attribute validation and dispatch functions for " <<
            name << "." << endl
             <<"//" << endl;

          if (has_att)
          {
            // _attribute_impl_phase_one
            //
            os << "bool " << name << "::" << endl
               << "_attribute_impl_phase_one (const " << string_type <<
              "& ns," << endl
               << "const " << string_type << "& n," << endl
               << "const " << string_type << "& s)" << endl
               << "{";

            names (c, names_phase_one_);

            // Nothing matched - call our base (extension) or return false
            // if there is no base (or restriction (even from anyType)).
            //
            os << "return ";

            if (c.inherits_p () &&
                !c.inherits ().is_a<SemanticGraph::Restricts> ())
            {
              os << "this->" << fq_name (c.inherits ().base ()) <<
                "::_attribute_impl_phase_one (ns, n, s);";
            }
            else
              os << "false;";

            os << "}";
          }


          if (has_any)
          {
            // _attribute_impl_phase_two
            //
            os << "bool " << name << "::" << endl
               << "_attribute_impl_phase_two (const " << string_type <<
              "& ns," << endl
               << "const " << string_type << "& n," << endl
               << "const " << string_type << "& s)"
               << "{";

            names (c, names_phase_two_);

            // Nothing matched - call our base (extension) or return false
            // if there is no base (or restriction (even from anyType)).
            //
            os << "return ";

            if (c.inherits_p () &&
                !c.inherits ().is_a<SemanticGraph::Restricts> ())
            {
              os << "this->" << fq_name (c.inherits ().base ()) <<
                "::_attribute_impl_phase_two (ns, n, s);";
            }
            else
              os << "false;";

            os << "}";
          }

          if (has_req_att)
          {
            // _pre_a_validate
            //
            os << "void " << name << "::" << endl
               << "_pre_a_validate ()"
               << "{"
               << "this->v_state_attr_stack_.push ();"
               << "v_state_attr_& as = *static_cast< v_state_attr_* > (" <<
              "this->v_state_attr_stack_.top ());"
               << endl;

            names (c, names_attribute_state_init_);

            // Call our base (extension) last.
            //
            if (c.inherits_p () &&
                !c.inherits ().is_a<SemanticGraph::Restricts> ())
            {
              os << "this->" << fq_name (c.inherits ().base ()) <<
                "::_pre_a_validate ();";
            }

            os << "}";


            // _post_a_validate
            //
            os << "void " << name << "::" << endl
               << "_post_a_validate ()"
               << "{";

            // Call our base (extension) first.
            //
            if (c.inherits_p () &&
                !c.inherits ().is_a<SemanticGraph::Restricts> ())
            {
              os << "this->" << fq_name (c.inherits ().base ()) <<
                "::_post_a_validate ();"
                 << endl;
            }

            os << "v_state_attr_& as = *static_cast< v_state_attr_* > (" <<
              "this->v_state_attr_stack_.top ());"
               << endl;

            names (c, names_attribute_state_check_);

            os << endl
               << "this->v_state_attr_stack_.pop ();"
               << "}";
          }
        }

      private:
        PhaseOne phase_one_;
        Traversal::Names names_phase_one_;

        PhaseTwo phase_two_;
        Traversal::Names names_phase_two_;

        AttributeStateInit attribute_state_init_;
        Traversal::Names names_attribute_state_init_;

        AttributeStateCheck attribute_state_check_;
        Traversal::Names names_attribute_state_check_;
      };
    }

    Void
    generate_attribute_validation_source (Context& ctx)
    {
      Traversal::Schema schema;

      Traversal::Sources sources;
      Traversal::Names schema_names;

      Namespace ns (ctx);
      Traversal::Names names;

      schema >> sources >> schema;
      schema >> schema_names >> ns >> names;

      Complex complex (ctx);

      names >> complex;

      schema.dispatch (ctx.schema_root);
    }
  }
}