aboutsummaryrefslogtreecommitdiff
path: root/xsd/cxx/parser/elements.cxx
blob: 399553a8057de9b22b739ff5e6e9920df6a44360 (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
// file      : xsd/cxx/parser/elements.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/elements.hxx>

namespace CXX
{
  namespace Parser
  {
    // Keep this symbol first to help HP-UX linker (long symbols?).
    //
    Content::Value Context::
    content (SemanticGraph::Complex& c)
    {
      using namespace SemanticGraph;

      if (c.mixed ())
        return Content::mixed;

      if (c.inherits_p ())
      {
        Type& base (c.inherits ().base ());

        if (Complex* cb = dynamic_cast<Complex*> (&base))
          return content (*cb);

        if (base.is_a<AnyType> ())
          return Content::complex;

        // Everyhting else (built-in type and AnySimpleType) is simple
        // content.
        //
        return Content::simple;
      }
      else
        return Content::complex;
    }

    Context::
    Context (std::wostream& o,
             SemanticGraph::Schema& root,
             CLI::Options const& ops,
             StringLiteralMap const* map,
             Regex const* he,
             Regex const* ie,
             Regex const* hie)
        : CXX::Context (o,
                        root,
                        map,
                        ops.value<CLI::char_type> (),
                        ops.value<CLI::char_encoding> (),
                        ops.value<CLI::include_with_brackets> (),
                        ops.value<CLI::include_prefix> (),
                        ops.value<CLI::export_symbol> (),
                        ops.value<CLI::namespace_map> (),
                        ops.value<CLI::namespace_regex> (),
                        ops.value<CLI::namespace_regex_trace> (),
                        ops.value<CLI::include_regex> (),
                        ops.value<CLI::include_regex_trace> (),
                        ops.value<CLI::generate_inline> (),
                        ops.value<CLI::reserved_name> ()),
          options (ops),
          xml_parser (xml_parser_),
          simple_base (simple_base_),
          complex_base (complex_base_),
          list_base (list_base_),
          cout_inst (cout_inst_),
          cerr_inst (cerr_inst_),
          parser_map (parser_map_),
          std_string_type (std_string_type_),
          validation (validation_),
          polymorphic (polymorphic_),
          hxx_expr (he),
          ixx_expr (ie),
          hxx_impl_expr (hie),
          xml_parser_ (ops.value<CLI::xml_parser> ()),
          validation_ ((ops.value<CLI::xml_parser> () == "expat" ||
                        ops.value<CLI::generate_validation> ()) &&
                       !ops.value<CLI::suppress_validation> ()),
          polymorphic_ (ops.value<CLI::generate_polymorphic> ())
    {
      if (char_type == L"char")
        std_string_type = L"::std::string";
      else if (char_type == L"wchar_t")
        std_string_type = L"::std::wstring";
      else
        std_string_type = L"::std::basic_string< " + char_type + L" >";

      String xs_ns (xs_ns_name ());

      string_type = xs_ns + L"::ro_string";
      simple_base = xs_ns + L"::simple_content";
      complex_base = xs_ns + L"::complex_content";
      list_base = xs_ns + L"::list_base";

      cout_inst = (char_type == L"char" ? L"std::cout" : L"std::wcout");
      cerr_inst = (char_type == L"char" ? L"std::cerr" : L"std::wcerr");

      if (polymorphic)
        parser_map_ = xs_ns + L"::parser_map";
    }

    Context::
    Context (Context& c)
        : CXX::Context (c),
          options (c.options),
          xml_parser (c.xml_parser),
          simple_base (c.simple_base),
          complex_base (c.complex_base),
          list_base (c.list_base),
          cout_inst (c.cout_inst),
          cerr_inst (c.cerr_inst),
          parser_map (c.parser_map),
          std_string_type (c.std_string_type),
          validation (c.validation),
          polymorphic (c.polymorphic),
          hxx_expr (c.hxx_expr),
          ixx_expr (c.ixx_expr),
          hxx_impl_expr (c.hxx_impl_expr)
    {
    }

    Context::
    Context (Context& c, std::wostream& o)
        : CXX::Context (c, o),
          options (c.options),
          xml_parser (c.xml_parser),
          simple_base (c.simple_base),
          complex_base (c.complex_base),
          list_base (c.list_base),
          cout_inst (c.cout_inst),
          cerr_inst (c.cerr_inst),
          parser_map (c.parser_map),
          std_string_type (c.std_string_type),
          validation (c.validation),
          polymorphic (c.polymorphic),
          hxx_expr (c.hxx_expr),
          ixx_expr (c.ixx_expr),
          hxx_impl_expr (c.hxx_impl_expr)
    {
    }

    Boolean Context::
    anonymous (SemanticGraph::Type& t)
    {
      return t.context ().count ("anonymous");
    }

    String const& Context::
    ret_type (SemanticGraph::Type& t)
    {
      return t.context ().get<String> ("ret-type");
    }

    String const& Context::
    arg_type (SemanticGraph::Type& t)
    {
      return t.context ().get<String> ("arg-type");
    }

    String const& Context::
    post_name (SemanticGraph::Type& t)
    {
      return t.context ().get<String> ("post");
    }

    String const& Context::
    eparser (SemanticGraph::Member& m)
    {
      return m.context ().get<String> ("parser");
    }

    String const& Context::
    emember (SemanticGraph::Member& m)
    {
      return m.context ().get<String> ("member");
    }

    String const& Context::
    emember_cache (SemanticGraph::Member& m)
    {
      return m.context ().get<String> ("member-cache");
    }

    String const& Context::
    emember_map (SemanticGraph::Member& m)
    {
      return m.context ().get<String> ("member-map");
    }

    String const& Context::
    eimpl (SemanticGraph::Type& t)
    {
      return t.context ().get<String> ("impl");
    }

    // Includes
    //
    Void TypeForward::
    traverse (SemanticGraph::Type& t)
    {
      os << "class " << t.context ().get<String> (name_key_) << ";";
    }

    Void Includes::
    traverse_ (SemanticGraph::Uses& u)
    {
      // Support for weak (forward) inclusion used in the file-per-type
      // compilation model.
      //
      Boolean weak (u.context ().count ("weak"));

      if (weak && (type_ == header || type_ == impl_header))
      {
        // Generate forward declarations. We don't really need them
        // in the impl files.
        //
        if (type_ == header)
          schema_.dispatch (u.schema ());

        return;
      }

      if (type_ == source && !weak)
        return;

      SemanticGraph::Path path (u.path ());

      // 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.string ();
      }
      catch (SemanticGraph::InvalidPath const&)
      {
        path_str = path.native_file_string ();
      }

      String inc_path;

      switch (type_)
      {
      case header:
      case source:
        {
          inc_path = ctx_.hxx_expr->merge (path_str);
          break;
        }
      case impl_header:
        {
          inc_path = ctx_.hxx_impl_expr->merge (path_str);
          break;
        }
      }

      ctx_.os << "#include " << ctx_.process_include_path (inc_path) << endl
              << endl;
    }
  }
}