summaryrefslogtreecommitdiff
path: root/CIAO/CIDLC/ExecImplGenerator.cpp
blob: d616124c22b143e65253b6715795bb4eb4f24285 (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
// file      : CIDLC/ExecImplGenerator.cpp
// author    : Jeff Parsons <j.parsons@vanderbilt.edu>
// cvs-id    : $Id$

#include "ExecImplGenerator.hpp"
#include "ExecImplHeaderGenerator.hpp"
#include "ExecImplSourceGenerator.hpp"
#include "CxxNamePrinter.hpp"
#include "Upcase.hpp"

#include "CCF/CodeGenerationKit/Regex.hpp"
#include "CCF/CodeGenerationKit/IndentationCxx.hpp"
#include "CCF/CodeGenerationKit/IndentationImplanter.hpp"

using namespace CCF;
using namespace CIDL;
using namespace SemanticGraph;
using namespace Traversal;

using std::string;
using std::ostream;

ExecImplGenerator::ExecImplGenerator (CommandLine const& cl)
    : cl_ (cl),
      file_name_ (""),
      export_macro_ ("")
{
}

void ExecImplGenerator::options (CL::Description& d)
{
  d.add_option (CL::OptionDescription (
                  "exec-hdr-file-suffix",
                  "suffix",
                  "Use provided suffix instead of default \'_exec.h\' "
                  "when constructing name of executor implementation file.",
                  CL::OptionType::value));

  d.add_option (CL::OptionDescription (
                  "exec-hdr-file-regex",
                  "regex",
                  "Use provided regular expression when constructing "
                  "name of executor implementation file.",
                  CL::OptionType::value));

  d.add_option (CL::OptionDescription (
                  "exec-src-file-suffix",
                  "suffix",
                  "Use provided suffix instead of default \'_exec.cpp\' "
                  "when constructing name of executor implementation file.",
                  CL::OptionType::value));

  d.add_option (CL::OptionDescription (
                  "exec-src-file-regex",
                  "regex",
                  "Use provided regular expression when constructing "
                  "name of executor implementation file.",
                  CL::OptionType::value));

  d.add_option (CL::OptionDescription (
                  "exec-export-macro",
                  "macro",
                  "Replace default executor DLL export macro "
                  "with provided macro.",
                  CL::OptionType::value));

  d.add_option (CL::OptionDescription (
                  "exec-export-include",
                  "file",
                  "Replace default executor export include file "
                  "with provided file.",
                  CL::OptionType::value));
}


void ExecImplGenerator::generate (SemanticGraph::TranslationUnit& u,
                                  fs::path const& file)
{
  // Generate files
  compute_export_macro (file);

  {
    fs::ofstream hdr_ofs;
    ostream& hdr_os = configure_stream ("exec-hdr-file-suffix",
                                        "_exec.h",
                                        "exec-hdr-file-regex",
                                        hdr_ofs);

    Indentation::Implanter<Indentation::Cxx> header_guard (hdr_os);

    // Set C++ name printer for os.
    //
    CxxNamePrinter name_printer;
    hdr_os.pword (name_printer_index) = &name_printer;


    ExecImplHeaderEmitter hdr_emitter (hdr_os,
                                       cl_,
                                      export_macro_,
                                      file);
    hdr_emitter.generate (u);
  }

  {
    fs::ofstream src_ofs;
    ostream& src_os = configure_stream ("exec-src-file-suffix",
                                        "_exec.cpp",
                                        "exec-src-file-regex",
                                        src_ofs);

    Indentation::Implanter<Indentation::Cxx> header_guard (src_os);

    // Set C++ name printer for os.
    //
    CxxNamePrinter name_printer;
    src_os.pword (name_printer_index) = &name_printer;

    ExecImplSourceEmitter src_emitter (src_os,
                                       cl_,
                                       export_macro_,
                                       file);
    src_emitter.generate (u);
  }
}

void
ExecImplGenerator::compute_export_macro (const fs::path& file_path)
{
  if (!file_path.empty ())
  {
    file_name_ = file_path.leaf ();
  }

  export_macro_ = cl_.get_value ("exec-export-macro", "");

  if (export_macro_.empty () && !file_name_.empty ())
  {
    // Modify a copy of the filename string.
    export_macro_ = file_name_;
    str_upcase (export_macro_);

    // Replace the suffix.
    export_macro_ =
      regex::perl_s (export_macro_,
                     "/^(.+?)(\\.(IDL|CIDL|CDL))?$/$1_EXEC_Export/");

    // Replace any remaining '.' in the string with '_'.
    export_macro_ = regex::perl_s (export_macro_,
                                   "/\\./_/");
  }
}

ostream&
ExecImplGenerator::configure_stream (string const& suffix_option,
                                     string const& default_suffix,
                                     string const& regex_option,
                                     fs::ofstream& ofs)
{
  if (! file_name_.empty ())
  {
    string file_suffix = cl_.get_value (suffix_option,
                                        default_suffix);
    string file_expr =
      cl_.get_value (regex_option,
                     "/^(.+?)(\\.(idl|cidl|cdl))?$/$1" + file_suffix + "/");

    string file_name = regex::perl_s (file_name_, file_expr);

    fs::path file_path (file_name);

    ofs.open (file_path, std::ios_base::out);

    if (!ofs.is_open ())
    {
      cerr << file_name
          << ": error: unable to open file in write mode"
          << endl;
    }
  }

  return ofs.is_open ()
    ? static_cast<ostream&> (ofs)
    : static_cast<ostream&> (std::cout);
}