// file : cli/generator.cxx // author : Boris Kolpackov // copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC // license : MIT; see accompanying LICENSE file #include // std::toupper, std::is{alpha,upper,lower} #include #include #include #include #include #include #include "header.hxx" #include "inline.hxx" #include "source.hxx" #include "runtime-header.hxx" #include "runtime-inline.hxx" #include "runtime-source.hxx" #include "man.hxx" #include "html.hxx" #include "context.hxx" #include "generator.hxx" #include "name-processor.hxx" using namespace std; using namespace cutl; using semantics::path; namespace { static char const cxx_header[] = "// -*- C++ -*-\n" "//\n" "// This file was generated by CLI, a command line interface\n" "// compiler for C++.\n" "//\n\n"; static char const man_header[] = ".\\\"\n" ".\\\" The following documentation was generated by CLI, a command\n" ".\\\" line interface compiler for C++.\n" ".\\\"\n"; static char const html_header[] = "\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 += 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 (); } } void append (ostream& os, vector const& text, string const& file) { for (vector::const_iterator i (text.begin ()); i != text.end (); ++i) { os << *i << endl; } if (!file.empty ()) { ifstream ifs; open (ifs, file); os << ifs.rdbuf (); } } } generator:: generator () { } void generator:: generate (options const& ops, semantics::cli_unit& unit, path const& p) { try { path file (p.leaf ()); string base (file.base ().string ()); bool gen_cxx (ops.generate_cxx ()); bool gen_man (ops.generate_man ()); bool gen_html (ops.generate_html ()); if (!gen_cxx && !gen_man && !gen_html) gen_cxx = true; if (ops.stdout_ ()) { if (gen_cxx) { cerr << "error: --stdout cannot be used with C++ output" << endl; throw failed (); } if (gen_man && gen_html) { cerr << "error: --stdout cannot be used with man and html output" << endl; throw failed (); } } fs::auto_removes auto_rm; // C++ output. // if (gen_cxx) { bool inl (!ops.suppress_inline ()); string hxx_name (base + ops.hxx_suffix ()); string ixx_name (base + ops.ixx_suffix ()); string cxx_name (base + ops.cxx_suffix ()); path hxx_path (hxx_name); path ixx_path (ixx_name); path cxx_path (cxx_name); if (!ops.output_dir ().empty ()) { path dir (ops.output_dir ()); hxx_path = dir / hxx_path; ixx_path = dir / ixx_path; cxx_path = dir / cxx_path; } // Process names. // { context ctx (cerr, unit, ops); process_names (ctx); } // Check if we need to generate the runtime code. If we include // another options file, then we assume the runtime is generated // there. However, to reduce the number of standard headers we // have to include in the generated header file, we will still // need to generate some template code in the source file. // bool runtime (true); for (semantics::cli_unit::includes_iterator i (unit.includes_begin ()); runtime && i != unit.includes_end (); ++i) { if (i->is_a ()) runtime = false; } // // ofstream hxx (hxx_path.string ().c_str ()); if (!hxx.is_open ()) { cerr << "error: unable to open '" << hxx_path << "' in write mode" << endl; throw failed (); } auto_rm.add (hxx_path); // // ofstream ixx; if (inl) { ixx.open (ixx_path.string ().c_str (), ios_base::out); if (!ixx.is_open ()) { cerr << "error: unable to open '" << ixx_path << "' in write mode" << endl; throw failed (); } auto_rm.add (ixx_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. // hxx << cxx_header; if (inl) ixx << cxx_header; cxx << cxx_header; typedef compiler::ostream_filter cxx_filter; // Include settings. // bool br (ops.include_with_brackets ()); string ip (ops.include_prefix ()); string gp (ops.guard_prefix ()); if (!ip.empty () && ip[ip.size () - 1] != '/') ip.append ("/"); if (!gp.empty () && gp[gp.size () - 1] != '_') gp.append ("_"); // HXX // { context ctx (hxx, unit, ops); string guard (make_guard (gp + hxx_name, ctx)); hxx << "#ifndef " << guard << endl << "#define " << guard << endl << endl; // Copy prologue. // hxx << "// Begin prologue." << endl << "//" << endl; append (hxx, ops.hxx_prologue (), ops.hxx_prologue_file ()); hxx << "//" << endl << "// End prologue." << endl << endl; { // We don't want to indent prologues/epilogues. // cxx_filter filt (ctx.os); if (runtime) generate_runtime_header (ctx); generate_header (ctx); } if (inl) { hxx << "#include " << (br ? '<' : '"') << ip << ixx_name << (br ? '>' : '"') << endl << endl; } // Copy epilogue. // hxx << "// Begin epilogue." << endl << "//" << endl; append (hxx, ops.hxx_epilogue (), ops.hxx_epilogue_file ()); hxx << "//" << endl << "// End epilogue." << endl << endl; hxx << "#endif // " << guard << endl; } // IXX // if (inl) { context ctx (ixx, unit, ops); // Copy prologue. // ixx << "// Begin prologue." << endl << "//" << endl; append (ixx, ops.ixx_prologue (), ops.ixx_prologue_file ()); ixx << "//" << endl << "// End prologue." << endl << endl; { // We don't want to indent prologues/epilogues. // cxx_filter filt (ctx.os); if (runtime) generate_runtime_inline (ctx); generate_inline (ctx); } // Copy epilogue. // ixx << "// Begin epilogue." << endl << "//" << endl; append (ixx, ops.ixx_epilogue (), ops.ixx_epilogue_file ()); ixx << "//" << endl << "// End epilogue." << endl; } // CXX // { context ctx (cxx, unit, ops); // Copy prologue. // cxx << "// Begin prologue." << endl << "//" << endl; append (cxx, ops.cxx_prologue (), ops.cxx_prologue_file ()); cxx << "//" << endl << "// End prologue." << endl << endl; cxx << "#include " << (br ? '<' : '"') << ip << hxx_name << (br ? '>' : '"') << endl << endl; { // We don't want to indent prologues/epilogues. // cxx_filter filt (ctx.os); if (runtime && !inl) generate_runtime_inline (ctx); generate_runtime_source (ctx, runtime); if (!inl) generate_inline (ctx); generate_source (ctx); } // Copy epilogue. // cxx << "// Begin epilogue." << endl << "//" << endl; append (cxx, ops.cxx_epilogue (), ops.cxx_epilogue_file ()); cxx << "//" << endl << "// End epilogue." << endl << endl; } } // man output // if (gen_man) { ofstream man; if (!ops.stdout_ ()) { path man_path (base + ops.man_suffix ()); if (!ops.output_dir ().empty ()) man_path = path (ops.output_dir ()) / man_path; man.open (man_path.string ().c_str ()); if (!man.is_open ()) { cerr << "error: unable to open '" << man_path << "' in write mode" << endl; throw failed (); } auto_rm.add (man_path); } // The explicit cast helps VC++ 8.0 overcome its issues. // ostream& os (ops.stdout_ () ? cout : static_cast (man)); append (os, ops.man_prologue (), ops.man_prologue_file ()); os << man_header; context ctx (os, unit, ops); generate_man (ctx); append (os, ops.man_epilogue (), ops.man_epilogue_file ()); } // HTML output // if (gen_html) { ofstream html; if (!ops.stdout_ ()) { path html_path (base + ops.html_suffix ()); if (!ops.output_dir ().empty ()) html_path = path (ops.output_dir ()) / html_path; html.open (html_path.string ().c_str ()); if (!html.is_open ()) { cerr << "error: unable to open '" << html_path << "' in write mode" << endl; throw failed (); } auto_rm.add (html_path); } // The explicit cast helps VC++ 8.0 overcome its issues. // ostream& os (ops.stdout_ () ? cout : static_cast (html)); append (os, ops.html_prologue (), ops.html_prologue_file ()); os << html_header; context ctx (os, unit, ops); generate_html (ctx); append (os, ops.html_epilogue (), ops.html_epilogue_file ()); } 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 (); } }