IDL compiler front-end library
IDL_FrontEnd.h
Go to the documentation of this file.
1 /*
3 * Nirvana IDL front-end library.
4 *
5 * This is a part of the Nirvana project.
6 *
7 * Author: Igor Popov
8 *
9 * Copyright (c) 2021 Igor Popov.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
23 *
24 * Send comments and/or bug reports to:
25 * popov.nirvana@gmail.com
26 */
27 #ifndef IDLFE_IDL_FRONTEND_H_
28 #define IDLFE_IDL_FRONTEND_H_
29 #pragma once
30 
31 #include <assert.h>
32 #include <string>
33 #include <vector>
34 #include <iostream>
35 #include <filesystem>
36 
37 namespace AST {
38 class Root;
39 class Builder;
40 class Interface;
41 }
42 
43 namespace FE {
44 class Driver;
45 }
46 
47 namespace simplecpp {
48 struct DUI;
49 }
50 
63 {
64 public:
70  int main (int argc, const char* const argv []) noexcept
71  {
72  return run (filename (argv [0]), argc - 1, argv + 1);
73  }
74 
82  int run (const char* command, int argc, const char* const argv []) noexcept;
83 
88  static const char* filename (const char* path) noexcept;
89 
91  unsigned flags () const noexcept
92  {
93  return flags_;
94  }
95 
97  static const unsigned FLAG_DEPRECATE_ANONYMOUS_TYPES = 1;
98 
100  static const unsigned FLAG_DEPRECATE_PSEUDO_INTERFACES = 2;
101 
103  static const unsigned FLAG_ENABLE_CONST_OBJREF = 4;
104 
106  std::ostream& err_out () const noexcept
107  {
108  return err_out_;
109  }
110 
111 protected:
112  friend class FE::Driver;
113  friend class AST::Builder;
114 
116  class CmdLine
117  {
118  public:
120  const char* arg () const noexcept
121  {
122  assert (!end ());
123  return *arg_;
124  }
125 
127  bool end () const noexcept
128  {
129  return arg_ >= end_;
130  }
131 
135  bool next () noexcept;
136 
145  const char* parameter (const char* switch_end);
146 
151  CmdLine (int argc, const char* const argv []) :
152  arg_ (argv),
153  end_ (argv + argc)
154  {}
155 
156  private:
157  const char* const* arg_;
158  const char* const* end_;
159  };
160 
166  IDL_FrontEnd (unsigned flags = 0, std::ostream& err_out = std::cerr) :
167  command_ (nullptr),
168  flags_ (flags),
169  err_out_ (err_out),
170  preprocess_to_stdout_ (false)
171  {}
172 
179  virtual void parse_arguments (CmdLine& args);
180 
199  virtual bool parse_command_line (CmdLine& args);
200 
206  virtual void print_usage_info (const char* exe_name);
207 
209  std::vector <std::string>& defines () noexcept
210  {
211  return defines_;
212  }
213 
215  std::vector <std::string>& undefines () noexcept
216  {
217  return undefines_;
218  }
219 
221  std::vector <std::string>& include_paths () noexcept
222  {
223  return include_paths_;
224  }
225 
227  std::vector <std::string>& includes () noexcept
228  {
229  return includes_;
230  }
231 
233  std::vector <std::string>& files () noexcept
234  {
235  return files_;
236  }
237 
245  virtual void generate_code (const AST::Root& tree) = 0;
246 
252  virtual void file_begin (const std::filesystem::path& file, AST::Builder& builder)
253  {}
254 
263  virtual void interface_end (const AST::Interface& itf, AST::Builder& builder)
264  {}
265 
266 private:
267  bool compile (const simplecpp::DUI& prep_params, const std::string& file);
268 
269 private:
270  const char* command_;
271  unsigned flags_;
272  std::vector <std::string> defines_;
273  std::vector <std::string> undefines_;
274  std::vector <std::string> include_paths_;
275  std::vector <std::string> includes_;
276  std::vector <std::string> files_;
277  std::ostream& err_out_;
278  bool preprocess_to_stdout_;
279 };
280 
284 
285 #endif
The AST builder.
Definition: Builder.h:60
Interface definition.
Definition: Interface.h:95
Abstract Syntax Tree root.
Definition: Root.h:62
Command line argument iterator.
Definition: IDL_FrontEnd.h:117
const char * parameter(const char *switch_end)
Get the parameter followed to the switch.
bool end() const noexcept
Definition: IDL_FrontEnd.h:127
const char * arg() const noexcept
Definition: IDL_FrontEnd.h:120
bool next() noexcept
Advances to the next argument.
IDL front-end.
Definition: IDL_FrontEnd.h:63
IDL_FrontEnd(unsigned flags=0, std::ostream &err_out=std::cerr)
Constructor.
Definition: IDL_FrontEnd.h:166
virtual void generate_code(const AST::Root &tree)=0
Generate user code from AST.
int main(int argc, const char *const argv[]) noexcept
Call this method from the main() function.
Definition: IDL_FrontEnd.h:70
virtual void print_usage_info(const char *exe_name)
Prints usage information to std::cout.
static const unsigned FLAG_ENABLE_CONST_OBJREF
Allow Nirvana const interface references.
Definition: IDL_FrontEnd.h:103
virtual bool parse_command_line(CmdLine &args)
Parse command line parameter.
std::vector< std::string > & includes() noexcept
Definition: IDL_FrontEnd.h:227
std::vector< std::string > & defines() noexcept
Definition: IDL_FrontEnd.h:209
static const unsigned FLAG_DEPRECATE_ANONYMOUS_TYPES
Disallows anonymous IDL types as required by the C++11 Language Mapping Specification.
Definition: IDL_FrontEnd.h:97
std::vector< std::string > & include_paths() noexcept
Definition: IDL_FrontEnd.h:221
virtual void parse_arguments(CmdLine &args)
Parse command line parameters.
static const char * filename(const char *path) noexcept
Get file name from path.
virtual void file_begin(const std::filesystem::path &file, AST::Builder &builder)
Begin of the file parsing.
Definition: IDL_FrontEnd.h:252
int run(const char *command, int argc, const char *const argv[]) noexcept
The same as main (), but aruments must not include the first main() argument (executable file name).
std::vector< std::string > & undefines() noexcept
Definition: IDL_FrontEnd.h:215
unsigned flags() const noexcept
Definition: IDL_FrontEnd.h:91
std::vector< std::string > & files() noexcept
Definition: IDL_FrontEnd.h:233
virtual void interface_end(const AST::Interface &itf, AST::Builder &builder)
End of the interface parsing.
Definition: IDL_FrontEnd.h:263
std::ostream & err_out() const noexcept
Definition: IDL_FrontEnd.h:106
static const unsigned FLAG_DEPRECATE_PSEUDO_INTERFACES
Disallows pseudo interfaces.
Definition: IDL_FrontEnd.h:100
Abstract Syntax Tree namespace.
Definition: Array.h:34