summaryrefslogtreecommitdiff
path: root/xsd/xsd/cxx/elements.cxx
diff options
context:
space:
mode:
Diffstat (limited to 'xsd/xsd/cxx/elements.cxx')
-rw-r--r--xsd/xsd/cxx/elements.cxx237
1 files changed, 119 insertions, 118 deletions
diff --git a/xsd/xsd/cxx/elements.cxx b/xsd/xsd/cxx/elements.cxx
index d2600dc..dfa2f38 100644
--- a/xsd/xsd/cxx/elements.cxx
+++ b/xsd/xsd/cxx/elements.cxx
@@ -1,16 +1,14 @@
// file : xsd/cxx/elements.cxx
-// author : Boris Kolpackov <boris@codesynthesis.com>
-// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC
+// copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
#include <cxx/elements.hxx>
-#include <backend-elements/regex.hxx>
-
#include <cctype> // std::toupper
#include <memory>
#include <sstream>
#include <fstream>
+#include <cassert>
#include <iostream>
using std::wcerr;
@@ -28,7 +26,7 @@ namespace CXX
namespace
{
- WideChar const* keywords[] = {
+ wchar_t const* keywords[] = {
L"NULL",
L"and",
L"asm",
@@ -114,55 +112,47 @@ namespace CXX
Context (std::wostream& o,
SemanticGraph::Schema& root,
SemanticGraph::Path const& path,
- StringLiteralMap const* string_literal_map_,
- NarrowString const& char_type__,
- NarrowString const& char_encoding__,
- Boolean include_with_brackets__,
- NarrowString const& include_prefix__,
- NarrowString const& esymbol,
- Containers::Vector<NarrowString> const& nsm,
- Containers::Vector<NarrowString> const& nsr,
- Boolean trace_namespace_regex_,
- Containers::Vector<NarrowString> const& ir,
- Boolean trace_include_regex_,
- Boolean inline_,
- Containers::Vector<NarrowString> const& reserved_name)
+ options_type const& ops,
+ StringLiteralMap const* string_literal_map_)
: os (o),
schema_root (root),
schema_path (schema_path_),
+ options (ops),
+ std (ops.std ()),
char_type (char_type_),
char_encoding (char_encoding_),
L (L_),
string_type (string_type_),
+ auto_ptr (auto_ptr_),
string_literal_map (string_literal_map_),
- include_with_brackets (include_with_brackets_),
- include_prefix (include_prefix_),
type_exp (type_exp_),
inst_exp (inst_exp_),
inl (inl_),
ns_mapping_cache (ns_mapping_cache_),
schema_path_ (path),
xs_ns_ (0),
- char_type_ (char_type__),
- char_encoding_ (char_encoding__),
+ char_type_ (ops.char_type ()),
+ char_encoding_ (ops.char_encoding ()),
L_ (char_type == L"wchar_t" ? L"L" : L""),
- include_with_brackets_ (include_with_brackets__),
- include_prefix_ (include_prefix__),
- type_exp_ (esymbol ? esymbol + " " : esymbol),
- inst_exp_ (esymbol ? esymbol + "\n" : esymbol),
- inl_ (inline_ ? L"inline\n" : L""),
+ inl_ (ops.generate_inline () ? L"inline\n" : L""),
cxx_id_expr_ (L"^(::)?([a-zA-Z_]\\w*)(::[a-zA-Z_]\\w*)*$"),
cxx_id_expr (cxx_id_expr_),
- trace_namespace_regex (trace_namespace_regex_),
urn_mapping_ (L"#^urn.*:([a-zA-Z_].*)$#$1#"),
urn_mapping (urn_mapping_),
nsr_mapping (nsr_mapping_),
nsm_mapping (nsm_mapping_),
include_mapping (include_mapping_),
- trace_include_regex (trace_include_regex_),
reserved_name_map (reserved_name_map_),
keyword_set (keyword_set_)
{
+ // Export symbol.
+ //
+ {
+ String es (ops.export_symbol ());
+ type_exp_ = es ? es + L" " : es;
+ inst_exp_ = es ? es + L"\n" : es;
+ }
+
// Resolve and cache XML Schema namespace.
//
{
@@ -196,6 +186,12 @@ namespace CXX
else
string_type_ = L"::std::basic_string< " + char_type + L" >";
+ // Automatic pointer type.
+ //
+ auto_ptr_ = std >= cxx_version::cxx11
+ ? "::std::unique_ptr"
+ : "::std::auto_ptr";
+
// Default encoding.
//
if (!char_encoding)
@@ -209,28 +205,28 @@ namespace CXX
// Default mapping.
//
nsr_mapping_.push_back (
- Regex (L"#^.* (.*?/)??"L"(([a-zA-Z_]\\w*)(/[a-zA-Z_]\\w*)*)/?$#$2#"));
+ Regex (L"#^.* (.*?/)??" L"(([a-zA-Z_]\\w*)(/[a-zA-Z_]\\w*)*)/?$#$2#"));
nsr_mapping_.push_back (
Regex (L"#^.* http://www\\.w3\\.org/2001/XMLSchema$#xml_schema#"));
// Custom regex mapping.
//
- for (Containers::Vector<NarrowString>::ConstIterator
- i (nsr.begin ()), e (nsr.end ()); i != e; ++i)
+ for (NarrowStrings::const_iterator i (ops.namespace_regex ().begin ()),
+ e (ops.namespace_regex ().end ()); i != e; ++i)
{
- nsr_mapping_.push_back (Regex (*i));
+ nsr_mapping_.push_back (Regex (String (*i)));
}
// Custom direct mapping.
//
- for (Containers::Vector<NarrowString>::ConstIterator
- i (nsm.begin ()), e (nsm.end ()); i != e; ++i)
+ for (NarrowStrings::const_iterator i (ops.namespace_map ().begin ()),
+ e (ops.namespace_map ().end ()); i != e; ++i)
{
String s (*i);
// Split the string in two parts at the last '='.
//
- Size pos (s.rfind ('='));
+ size_t pos (s.rfind ('='));
if (pos == String::npos)
throw InvalidNamespaceMapping (s, "delimiter ('=') not found");
@@ -248,22 +244,22 @@ namespace CXX
// Include path regex
//
- for (Containers::Vector<NarrowString>::ConstIterator
- i (ir.begin ()), e (ir.end ()); i != e; ++i)
+ for (NarrowStrings::const_iterator i (ops.include_regex ().begin ()),
+ e (ops.include_regex ().end ()); i != e; ++i)
{
- include_mapping_.push_back (Regex (*i));
+ include_mapping_.push_back (Regex (String (*i)));
}
// Reserved names.
//
- for (Containers::Vector<NarrowString>::ConstIterator
- i (reserved_name.begin ()), e (reserved_name.end ()); i != e; ++i)
+ for (NarrowStrings::const_iterator i (ops.reserved_name ().begin ()),
+ e (ops.reserved_name ().end ()); i != e; ++i)
{
String s (*i);
// Split the string in two parts at '='.
//
- Size pos (s.find ('='));
+ size_t pos (s.find ('='));
if (pos == String::npos)
reserved_name_map_[s] = L"";
@@ -273,7 +269,7 @@ namespace CXX
// Populate the keyword set.
//
- for (Size i (0); i < sizeof (keywords) / sizeof (char*); ++i)
+ for (size_t i (0); i < sizeof (keywords) / sizeof (char*); ++i)
keyword_set_.insert (keywords[i]);
}
@@ -287,7 +283,7 @@ namespace CXX
using SemanticGraph::Sources;
String tmp;
- MapMapping::ConstIterator i (nsm_mapping.find (ns.name ()));
+ MapMapping::const_iterator i (nsm_mapping.find (ns.name ()));
if (i != nsm_mapping.end ())
{
@@ -317,20 +313,18 @@ namespace CXX
if (!path.empty ())
{
+ path.normalize ();
+
// Try to use the portable representation of the path. If that
// fails, fall back to the native representation.
//
try
{
- pair = path.string ();
+ pair = path.posix_string ();
}
catch (SemanticGraph::InvalidPath const&)
{
-#if !defined(BOOST_FILESYSTEM_VERSION) || BOOST_FILESYSTEM_VERSION == 2
- pair = path.native_file_string ();
-#else
pair = path.string ();
-#endif
}
}
@@ -338,7 +332,7 @@ namespace CXX
// Check cache first
//
- MappingCache::ConstIterator i (ns_mapping_cache.find (pair));
+ MappingCache::const_iterator i (ns_mapping_cache.find (pair));
if (i != ns_mapping_cache.end ())
{
@@ -346,32 +340,34 @@ namespace CXX
}
else
{
- if (trace_namespace_regex)
+ bool trace (options.namespace_regex_trace ());
+
+ if (trace)
wcerr << "namespace: '" << pair << "'" << endl;
- Boolean found (false);
+ bool found (false);
Regex colon (L"#/#::#");
- for (RegexMapping::ConstReverseIterator e (nsr_mapping.rbegin ());
+ for (RegexMapping::const_reverse_iterator e (nsr_mapping.rbegin ());
e != nsr_mapping.rend (); ++e)
{
- if (trace_namespace_regex)
- wcerr << "try: '" << e->pattern () << "' : ";
+ if (trace)
+ wcerr << "try: '" << e->regex () << "' : ";
if (e->match (pair))
{
- tmp = e->merge (pair);
- tmp = colon.merge (tmp); // replace `/' with `::'
+ tmp = e->replace (pair);
+ tmp = colon.replace (tmp); // replace `/' with `::'
// Check the result.
//
found = cxx_id_expr.match (tmp);
- if (trace_namespace_regex)
+ if (trace)
wcerr << "'" << tmp << "' : ";
}
- if (trace_namespace_regex)
+ if (trace)
wcerr << (found ? '+' : '-') << endl;
if (found)
@@ -392,7 +388,7 @@ namespace CXX
}
else
{
- tmp = colon.merge (n); // replace `/' with `::'
+ tmp = colon.replace (n); // replace `/' with `::'
if (!cxx_id_expr.match (tmp))
{
@@ -401,8 +397,8 @@ namespace CXX
if (urn_mapping.match (n))
{
Regex filter (L"#[.:-]#_#");
- tmp = urn_mapping.merge (n);
- tmp = filter.merge (tmp);
+ tmp = urn_mapping.replace (n);
+ tmp = filter.replace (tmp);
if (!cxx_id_expr.match (tmp))
throw NoNamespaceMapping (
@@ -496,7 +492,7 @@ namespace CXX
}
String Context::
- fq_name (SemanticGraph::Nameable& n, Char const* name_key)
+ fq_name (SemanticGraph::Nameable& n, char const* name_key)
{
using namespace SemanticGraph;
@@ -548,17 +544,17 @@ namespace CXX
escape (String const& name)
{
String r;
- Size n (name.size ());
+ size_t n (name.size ());
// In most common cases we will have that many chars.
//
r.reserve (n);
- for (Size i (0); i < n; ++i)
+ for (size_t i (0); i < n; ++i)
{
- Boolean first (i == 0);
+ bool first (i == 0);
- UnsignedLong u (unicode_char (name, i)); // May advance i.
+ unsigned int u (unicode_char (name, i)); // May advance i.
if (first)
{
@@ -574,7 +570,7 @@ namespace CXX
u == '_'))
r.push_back ('_');
else
- r.push_back (static_cast<WideChar> (u));
+ r.push_back (static_cast<wchar_t> (u));
}
if (r.empty ())
@@ -582,7 +578,7 @@ namespace CXX
// Custom reserved words.
//
- ReservedNameMap::ConstIterator i (reserved_name_map.find (r));
+ ReservedNameMap::const_iterator i (reserved_name_map.find (r));
if (i != reserved_name_map.end ())
{
@@ -618,14 +614,14 @@ namespace CXX
//
String
- charlit (UnsignedLong u)
+ charlit (unsigned int u)
{
String r ("\\x");
- Boolean lead (true);
+ bool lead (true);
- for (Long i (7); i >= 0; --i)
+ for (int i (7); i >= 0; --i)
{
- UnsignedLong x ((u >> (i * 4)) & 0x0F);
+ unsigned int x ((u >> (i * 4)) & 0x0F);
if (lead)
{
@@ -645,7 +641,7 @@ namespace CXX
strlit_ascii (String const& str)
{
String r;
- Size n (str.size ());
+ size_t n (str.size ());
// In most common cases we will have that many chars.
//
@@ -653,11 +649,11 @@ namespace CXX
r += '"';
- Boolean escape (false);
+ bool escape (false);
- for (Size i (0); i < n; ++i)
+ for (size_t i (0); i < n; ++i)
{
- UnsignedLong u (Context::unicode_char (str, i)); // May advance i.
+ unsigned int u (Context::unicode_char (str, i)); // May advance i.
// [128 - ] - unrepresentable
// 127 - \x7F
@@ -738,7 +734,7 @@ namespace CXX
}
default:
{
- r += static_cast<WideChar> (u);
+ r += static_cast<wchar_t> (u);
break;
}
}
@@ -756,7 +752,7 @@ namespace CXX
return r;
}
- const UnsignedLong utf8_first_char_mask[5] =
+ const unsigned int utf8_first_char_mask[5] =
{
0x00, 0x00, 0xC0, 0xE0, 0xF0
};
@@ -765,7 +761,7 @@ namespace CXX
strlit_utf8 (String const& str)
{
String r;
- Size n (str.size ());
+ size_t n (str.size ());
// In most common cases we will have that many chars.
//
@@ -773,11 +769,11 @@ namespace CXX
r += '"';
- Boolean escape (false);
+ bool escape (false);
- for (Size i (0); i < n; ++i)
+ for (size_t i (0); i < n; ++i)
{
- UnsignedLong u (Context::unicode_char (str, i)); // May advance i.
+ unsigned int u (Context::unicode_char (str, i)); // May advance i.
// [128 - ] - UTF-8
// 127 - \x7F
@@ -858,15 +854,15 @@ namespace CXX
}
default:
{
- r += static_cast<WideChar> (u);
+ r += static_cast<wchar_t> (u);
break;
}
}
}
else
{
- UnsignedLong count;
- UnsignedLong tmp[4];
+ unsigned int count (0);
+ unsigned int tmp[4];
if (u < 0x800)
count = 2;
@@ -895,10 +891,13 @@ namespace CXX
case 1:
{
tmp[0] = u | utf8_first_char_mask[count];
+ break;
}
+ default:
+ assert (false);
}
- for (UnsignedLong j (0); j < count; ++j)
+ for (unsigned int j (0); j < count; ++j)
r += charlit (tmp[j]);
escape = true;
@@ -914,7 +913,7 @@ namespace CXX
strlit_iso8859_1 (String const& str)
{
String r;
- Size n (str.size ());
+ size_t n (str.size ());
// In most common cases we will have that many chars.
//
@@ -922,11 +921,11 @@ namespace CXX
r += '"';
- Boolean escape (false);
+ bool escape (false);
- for (Size i (0); i < n; ++i)
+ for (size_t i (0); i < n; ++i)
{
- UnsignedLong u (Context::unicode_char (str, i)); // May advance i.
+ unsigned int u (Context::unicode_char (str, i)); // May advance i.
// [256 - ] - unrepresentable
// [127 - 255] - \xXX
@@ -1007,7 +1006,7 @@ namespace CXX
}
default:
{
- r += static_cast<WideChar> (u);
+ r += static_cast<wchar_t> (u);
break;
}
}
@@ -1034,7 +1033,7 @@ namespace CXX
strlit_utf32 (String const& str)
{
String r;
- Size n (str.size ());
+ size_t n (str.size ());
// In most common cases we will have that many chars.
//
@@ -1042,11 +1041,11 @@ namespace CXX
r += L"L\"";
- Boolean escape (false);
+ bool escape (false);
- for (Size i (0); i < n; ++i)
+ for (size_t i (0); i < n; ++i)
{
- UnsignedLong u (Context::unicode_char (str, i)); // May advance i.
+ unsigned int u (Context::unicode_char (str, i)); // May advance i.
// [128 - ] - \xUUUUUUUU
// 127 - \x7F
@@ -1105,9 +1104,10 @@ namespace CXX
{
if (escape)
{
- // Close and open the string so there are no clashes.
+ // Close and open the string so there are no clashes. C++11
+ // requires a space between " and L.
//
- r += L"\"L\"";
+ r += L"\" L\"";
escape = false;
}
@@ -1125,7 +1125,7 @@ namespace CXX
}
default:
{
- r += static_cast<WideChar> (u);
+ r += static_cast<wchar_t> (u);
break;
}
}
@@ -1148,7 +1148,7 @@ namespace CXX
// First see if we have a custom mapping.
//
assert (string_literal_map != 0);
- StringLiteralMap::ConstIterator i (string_literal_map->find (str));
+ StringLiteralMap::const_iterator i (string_literal_map->find (str));
if (i != string_literal_map->end ())
return i->second;
@@ -1175,16 +1175,16 @@ namespace CXX
{
String r;
- WideChar const* s (str.c_str ());
- Size size (str.size ());
+ wchar_t const* s (str.c_str ());
+ size_t size (str.size ());
// In most common cases we will have that many chars.
//
r.reserve (size);
- for (WideChar const* p (s); p < s + size; ++p)
+ for (wchar_t const* p (s); p < s + size; ++p)
{
- UnsignedLong u (unicode_char (p)); // May advance p.
+ unsigned int u (unicode_char (p)); // May advance p.
// We are going to treat \v, \f and \n as unrepresentable
// here even though they can be present in C++ source code.
@@ -1192,7 +1192,7 @@ namespace CXX
if (u > 127 || (u < 32 && u != '\t'))
r += L'?';
else
- r += static_cast<WideChar> (u);
+ r += static_cast<wchar_t> (u);
}
return r;
@@ -1201,30 +1201,31 @@ namespace CXX
String Context::
process_include_path (String const& name) const
{
- String path (include_prefix + name);
+ String path (String (options.include_prefix ()) + name);
+ bool trace (options.include_regex_trace ());
- if (trace_include_regex)
+ if (trace)
wcerr << "include: '" << path << "'" << endl;
String r;
- Boolean found (false);
+ bool found (false);
- for (RegexMapping::ConstReverseIterator e (include_mapping.rbegin ());
+ for (RegexMapping::const_reverse_iterator e (include_mapping.rbegin ());
e != include_mapping.rend (); ++e)
{
- if (trace_include_regex)
- wcerr << "try: '" << e->pattern () << "' : ";
+ if (trace)
+ wcerr << "try: '" << e->regex () << "' : ";
if (e->match (path))
{
- r = e->merge (path);
+ r = e->replace (path);
found = true;
- if (trace_include_regex)
+ if (trace)
wcerr << "'" << r << "' : ";
}
- if (trace_include_regex)
+ if (trace)
wcerr << (found ? '+' : '-') << endl;
if (found)
@@ -1236,8 +1237,8 @@ namespace CXX
if (!r.empty () && r[0] != L'"' && r[0] != L'<')
{
- WideChar op (include_with_brackets ? L'<' : L'"');
- WideChar cl (include_with_brackets ? L'>' : L'"');
+ wchar_t op (options.include_with_brackets () ? L'<' : L'"');
+ wchar_t cl (options.include_with_brackets () ? L'>' : L'"');
r = op + r + cl;
}
@@ -1247,7 +1248,7 @@ namespace CXX
// Namespace
//
- Void Namespace::
+ void Namespace::
pre (Type& n)
{
String ns (ctx_.ns_name (n));
@@ -1281,7 +1282,7 @@ namespace CXX
} while (true);
}
- Void Namespace::
+ void Namespace::
post (Type& n)
{
String ns (ctx_.ns_name (n));