aboutsummaryrefslogtreecommitdiff
path: root/odb/generator.cxx
blob: 9e1da47be815de0d92b78cc5b5821567b9193882 (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
// file      : cli/generator.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2010 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#include <cctype>  // std::toupper, std::is{alpha,upper,lower}
#include <string>
#include <fstream>
#include <iostream>

#include <cutl/fs/auto-remove.hxx>

#include <cutl/compiler/code-stream.hxx>
#include <cutl/compiler/cxx-indenter.hxx>

#include <odb/context.hxx>
#include <odb/generator.hxx>

#include <odb/source.hxx>

using namespace std;
using namespace cutl;

using semantics::path;

namespace
{
  static char const cxx_header[] =
    "// This code was generated by CodeSynthesis ODB object persistence\n"
    "// compiler for C++.\n"
    "//\n\n";

  /*
  string
  make_guard (string const& file, context& ctx)
  {
    string g (file);

    // Split words, e.g., "FooBar" to "Foo_Bar" and convert everything
    // to upper case.
    //
    string r;
    for (string::size_type i (0), n (g.size ()); i < n - 1; ++i)
    {
      char c1 (g[i]);
      char c2 (g[i + 1]);

      r += toupper (c1);

      if (isalpha (c1) && isalpha (c2) && islower (c1) && isupper (c2))
        r += "_";
    }
    r += std::toupper (g[g.size () - 1]);

    return ctx.escape (r);
  }
  */

  void
  open (ifstream& ifs, string const& path)
  {
    ifs.open (path.c_str (), ios_base::in | ios_base::binary);

    if (!ifs.is_open ())
    {
      cerr << path << ": error: unable to open in read mode" << endl;
      throw generator::failed ();
    }
  }
}

generator::
generator ()
{
}

void generator::
generate (options const& ops, semantics::unit& unit, path const& p)
{
  try
  {
    path file (p.leaf ());
    string base (file.base ().string ());

    fs::auto_removes auto_rm;

    // C++ output.
    //
    string cxx_name (base + ops.odb_file_suffix () + ops.cxx_suffix ());
    path cxx_path (cxx_name);

    if (!ops.output_dir ().empty ())
    {
      path dir (ops.output_dir ());
      cxx_path = dir / cxx_path;
    }

    //
    //
    ofstream cxx (cxx_path.string ().c_str ());

    if (!cxx.is_open ())
    {
      cerr << "error: unable to open '" << cxx_path << "' in write mode"
           << endl;
      throw failed ();
    }

    auto_rm.add (cxx_path);

    // Print headers.
    //
    cxx << cxx_header;

    typedef compiler::ostream_filter<compiler::cxx_indenter, char> cxx_filter;

    // Include settings.
    //
    bool br (ops.include_with_brackets ());
    string ip (ops.include_prefix ());

    if (!ip.empty () && ip[ip.size () - 1] != '/')
      ip.append ("/");

    // CXX
    //
    {
      cxx_filter filt (cxx);
      context ctx (cxx, unit, ops);

      cxx << "#include " << (br ? '<' : '"') << ip << file <<
        (br ? '>' : '"') << endl
          << endl;

      generate_source (ctx);
    }

    auto_rm.cancel ();
  }
  catch (const generation_failed&)
  {
    // Code generation failed. Diagnostics has already been issued.
    //
    throw failed ();
  }
  catch (semantics::invalid_path const& e)
  {
    cerr << "error: '" << e.path () << "' is not a valid filesystem path"
         << endl;
    throw failed ();
  }
  catch (fs::error const&)
  {
    // Auto-removal of generated files failed. Ignore it.
    //
    throw failed ();
  }
}