summaryrefslogtreecommitdiff
path: root/modules/CIAO/CCF/Example/IDL2/CxxMapping
diff options
context:
space:
mode:
Diffstat (limited to 'modules/CIAO/CCF/Example/IDL2/CxxMapping')
-rw-r--r--modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.cpp32
-rw-r--r--modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.hpp34
-rw-r--r--modules/CIAO/CCF/Example/IDL2/CxxMapping/GeneratorImpl.hpp533
-rw-r--r--modules/CIAO/CCF/Example/IDL2/CxxMapping/driver.cpp96
-rw-r--r--modules/CIAO/CCF/Example/IDL2/CxxMapping/test.idl12
5 files changed, 707 insertions, 0 deletions
diff --git a/modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.cpp b/modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.cpp
new file mode 100644
index 00000000000..6645b7f0b67
--- /dev/null
+++ b/modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.cpp
@@ -0,0 +1,32 @@
+// file : Example/IDL2/CxxMapping/Generator.cpp
+// author : Boris Kolpackov <boris@dre.vanderbilt.edu>
+// cvs-id : $Id$
+
+#include "Generator.hpp"
+#include "GeneratorImpl.hpp"
+
+namespace IDL2
+{
+ Generator::
+ ~Generator ()
+ {
+ }
+
+ Generator::
+ Generator ()
+ : pimpl_ (new GeneratorImpl), impl_ (*pimpl_)
+ {
+ }
+
+ Generator::
+ Generator (GeneratorImpl& gi)
+ : pimpl_ (), impl_ (gi)
+ {
+ }
+
+ void Generator::
+ generate (CCF::IDL2::SemanticGraph::TranslationUnit& tu)
+ {
+ impl_.generate (tu);
+ }
+}
diff --git a/modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.hpp b/modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.hpp
new file mode 100644
index 00000000000..4e231267dae
--- /dev/null
+++ b/modules/CIAO/CCF/Example/IDL2/CxxMapping/Generator.hpp
@@ -0,0 +1,34 @@
+// file : Example/IDL2/CxxMapping/Generator.hpp
+// author : Boris Kolpackov <boris@dre.vanderbilt.edu>
+// cvs-id : $Id$
+
+#ifndef EXAMPLE_IDL2_CXX_MAPPING_GENERATOR_HPP
+#define EXAMPLE_IDL2_CXX_MAPPING_GENERATOR_HPP
+
+#include <memory>
+
+#include "CCF/IDL2/SemanticGraph.hpp"
+
+namespace IDL2
+{
+ class GeneratorImpl;
+
+ class Generator
+ {
+ public:
+ ~Generator ();
+ Generator ();
+
+ void
+ generate (CCF::IDL2::SemanticGraph::TranslationUnit& tu);
+
+ protected:
+ Generator (GeneratorImpl&);
+
+ protected:
+ std::auto_ptr<GeneratorImpl> pimpl_;
+ GeneratorImpl& impl_;
+ };
+}
+
+#endif // EXAMPLE_IDL2_CXX_MAPPING_GENERATOR_HPP
diff --git a/modules/CIAO/CCF/Example/IDL2/CxxMapping/GeneratorImpl.hpp b/modules/CIAO/CCF/Example/IDL2/CxxMapping/GeneratorImpl.hpp
new file mode 100644
index 00000000000..f5e6edc3e77
--- /dev/null
+++ b/modules/CIAO/CCF/Example/IDL2/CxxMapping/GeneratorImpl.hpp
@@ -0,0 +1,533 @@
+// file : Example/IDL2/CxxMapping/GeneratorImpl.hpp
+// author : Boris Kolpackov <boris@dre.vanderbilt.edu>
+// cvs-id : $Id$
+
+#ifndef EXAMPLE_IDL2_CXX_MAPPING_GENERATOR_IMPL_HPP
+#define EXAMPLE_IDL2_CXX_MAPPING_GENERATOR_IMPL_HPP
+
+#include <iostream>
+
+#include "CCF/CodeGenerationKit/IndentationCxx.hpp"
+#include "CCF/CodeGenerationKit/IndentationImplanter.hpp"
+
+#include "CCF/IDL2/SemanticGraph.hpp"
+#include "CCF/IDL2/Traversal.hpp"
+
+namespace IDL2
+{
+ using namespace CCF::IDL2;
+
+ using std::cout;
+ using std::endl;
+
+ struct Inhibitor
+ {
+ virtual void
+ operator() () const = 0;
+ };
+
+ template <typename O, typename S, typename T>
+ class InhibitorTemplate : public Inhibitor
+ {
+ public:
+ typedef void (O::*F) (S&, T&);
+
+ InhibitorTemplate (O& o, F f, S& s, T& t)
+ : o_ (o), f_ (f), s_ (s), t_ (t)
+ {
+ }
+
+ virtual void
+ operator() () const
+ {
+ (o_.*f_)(s_, t_);
+ }
+
+ private:
+ O& o_;
+ F f_;
+ S& s_;
+ T& t_;
+ };
+
+ template <typename O,
+ typename S,
+ typename T = Traversal::EdgeDispatcherBase>
+ class Factory
+ {
+ public:
+ typedef void (O::*F) (S&, T&);
+
+ Factory (O& o, F f, T& t)
+ : o_ (o), f_ (f), t_ (t)
+ {
+ }
+
+ InhibitorTemplate<O, S, T>
+ operator() (S& s) const
+ {
+ return InhibitorTemplate<O, S, T> (o_, f_, s, t_);
+ }
+
+ private:
+ O& o_;
+ F f_;
+ T& t_;
+ };
+
+ std::ostream&
+ operator<< (std::ostream& os, Inhibitor const& p)
+ {
+ p ();
+ return os;
+ }
+
+ class GeneratorImpl
+ {
+ protected:
+ // Layer 1
+ //
+
+ // Layer 2
+ //
+
+ //--
+
+ // Layer 3
+ //
+
+ //--
+
+ struct InterfaceHdr : Traversal::UnconstrainedInterface
+ {
+ virtual void
+ pre (Type&)
+ {
+ cout << "class ";
+ }
+
+ virtual void
+ name (Type& i)
+ {
+ cout << i.name ();
+ }
+
+ virtual void
+ inherits_pre (Type&)
+ {
+ cout << " : ";
+ }
+
+ virtual void
+ inherits_none (Type&)
+ {
+ cout << " : public virtual CORBA::Impl::Stub";
+ }
+
+ virtual void
+ names_pre (Type&)
+ {
+ cout << "{"
+ << "public:" << endl;
+ }
+
+ virtual void
+ names_post (Type&)
+ {
+ cout << "}";
+ }
+
+ virtual void
+ post (Type&)
+ {
+ cout << ";";
+ }
+
+ virtual void
+ comma (Type&)
+ {
+ cout << ", ";
+ }
+ };
+
+
+ struct Module : Traversal::Module
+ {
+ virtual void
+ pre (Type& m)
+ {
+ cout << "namespace " << m.name ();
+ }
+
+ virtual void
+ names_pre (Type& m)
+ {
+ cout << "{";
+ }
+
+ virtual void
+ names_post (Type& m)
+ {
+ cout << "}";
+ }
+ };
+
+ struct TypeName : Traversal::Nameable,
+ Traversal::FundamentalType
+ {
+ virtual void
+ traverse (SemanticGraph::Nameable& n)
+ {
+ cout << n.scoped_name ();
+ }
+
+ virtual void
+ traverse (SemanticGraph::FundamentalType& ft)
+ {
+ cout << ft.name ();
+ }
+ };
+
+ // Layer 4
+ //
+
+ //--
+
+
+ struct OperationHdr : Traversal::Operation
+ {
+ virtual void
+ name (Type& o)
+ {
+ cout << " " << o.name ();
+ }
+
+ virtual void
+ receives_pre (Type&)
+ {
+ cout << " (";
+ }
+
+ virtual void
+ receives_post (Type&)
+ {
+ cout << ")";
+ }
+
+ virtual void
+ raises_pre (Type&)
+ {
+ cout << " throw (";
+ }
+
+ virtual void
+ raises_post (Type&)
+ {
+ cout << ")";
+ }
+
+ virtual void
+ post (Type&)
+ {
+ cout << ";";
+ }
+
+ virtual void
+ comma (Type&)
+ {
+ cout << ", ";
+ }
+ };
+
+ struct OperationImpl : Traversal::Operation
+ {
+ OperationImpl ()
+ : marshal (*this, &OperationImpl::receives, marshal_),
+ unmarshal (*this, &OperationImpl::receives, unmarshal_),
+ ret_type (*this, &OperationImpl::returns, ret_type_),
+ cast_type (*this, &OperationImpl::returns, cast_type_)
+ {
+ }
+
+ virtual void
+ name (Type& o)
+ {
+ cout << " "
+ << o.scoped_name ().scope_name ().simple_name ()
+ << "::" << o.name ();
+ }
+
+ virtual void
+ receives_pre (Type&)
+ {
+ cout << " (";
+ }
+
+ virtual void
+ receives_post (Type&)
+ {
+ cout << ")";
+ }
+
+ virtual void
+ raises_pre (Type&)
+ {
+ cout << " throw (";
+ }
+
+ virtual void
+ raises_post (Type&)
+ {
+ cout << ")";
+ }
+
+ virtual void
+ post (Type& o)
+ {
+ cout << "{"
+ << "OCDRStream os;"
+ << "static_cast<void> (os" << marshal (o) << ");"
+ << "ICDRStream is (Stub::make_call (\""
+ << o.name () << "\", os));"
+ << ret_type (o) << " _retval;"
+ << "is >> _retval" << unmarshal (o) << ";"
+ << "return " << "static_cast<" << cast_type (o) << "> (_retval);"
+ << "}";
+ }
+
+ virtual void
+ comma (Type&)
+ {
+ cout << ", ";
+ }
+
+ struct MarshalParameter : Traversal::Receives,
+ Traversal::InParameter,
+ Traversal::InOutParameter
+ {
+ MarshalParameter ()
+ {
+ node_traverser (*this);
+ }
+
+ virtual void
+ traverse (SemanticGraph::InParameter& p)
+ {
+ cout << " << " << p.name ();
+ }
+
+ virtual void
+ traverse (SemanticGraph::InOutParameter& p)
+ {
+ cout << " << " << p.name ();
+ }
+ } marshal_;
+
+ Factory<OperationImpl, Type> marshal;
+
+ struct UnmarshalParameter : Traversal::Receives,
+ Traversal::InOutParameter,
+ Traversal::OutParameter
+ {
+ UnmarshalParameter ()
+ {
+ node_traverser (*this);
+ }
+
+ virtual void
+ traverse (SemanticGraph::InOutParameter& p)
+ {
+ cout << " >> " << p.name ();
+ }
+
+ virtual void
+ traverse (SemanticGraph::OutParameter& p)
+ {
+ cout << " >> " << p.name ();
+ }
+ } unmarshal_;
+
+ Factory<OperationImpl, Type> unmarshal;
+
+
+ struct ReturnType : Traversal::Belongs, Traversal::Void, TypeName
+ {
+ ReturnType ()
+ {
+ node_traverser (*this);
+ }
+
+ virtual void
+ traverse (SemanticGraph::Void&)
+ {
+ // Void is a type for which do-nothing operator>> (ICDRStream&)
+ // is provided. Its main purpose is to make C++ type handling
+ // more regular and thus allow more streamlined code generation.
+ //
+ cout << "Void";
+ }
+ } ret_type_;
+
+ Factory<OperationImpl, Type> ret_type;
+
+ struct CastType : Traversal::Belongs, TypeName
+ {
+ CastType ()
+ {
+ node_traverser (*this);
+ }
+ } cast_type_;
+
+ Factory<OperationImpl, Type> cast_type;
+ };
+
+ // Layer 5
+ //
+
+ //--
+
+ struct Parameter : Traversal::Parameter
+ {
+ virtual void
+ name (Type& p)
+ {
+ cout << " " << p.name ();
+ }
+ };
+
+ public:
+
+ GeneratorImpl ()
+ {
+ // Layer 1
+ //
+ unit.edge_traverser (contains_principal);
+
+ //--
+
+ contains_principal.node_traverser (region);
+
+ // Layer 2
+ //
+ region.edge_traverser (contains_root);
+
+ //--
+
+ contains_root.node_traverser (root);
+
+ // Layer 3
+ //
+ root.edge_traverser (defines);
+
+ //--
+ defines.node_traverser (interface_hdr);
+ defines.node_traverser (interface_impl);
+ defines.node_traverser (module);
+
+ // Layer 4
+ //
+ module.edge_traverser (defines);
+
+ interface_hdr.edge_traverser (inherits);
+ interface_hdr.edge_traverser (interface_hdr_defines);
+
+ interface_impl.edge_traverser (interface_impl_defines);
+
+ //--
+ inherits.node_traverser (type_name);
+
+ interface_hdr_defines.node_traverser (operation_hdr);
+ interface_impl_defines.node_traverser (operation_impl);
+
+ // Layer 5
+ //
+
+ operation_hdr.edge_traverser (receives);
+ operation_hdr.edge_traverser (returns);
+
+ operation_impl.edge_traverser (receives);
+ operation_impl.edge_traverser (returns);
+
+ //--
+
+ receives.node_traverser (parameter);
+ returns.node_traverser (type_name);
+
+ // Layer 6
+ //
+ parameter.edge_traverser (belongs);
+
+ //--
+
+ belongs.node_traverser (type_name);
+
+ }
+
+ protected:
+ Traversal::TranslationUnit unit;
+
+ // Layer 1
+ //
+ Traversal::ContainsPrincipal contains_principal;
+
+ //--
+
+ Traversal::TranslationRegion region;
+
+ // Layer 2
+ //
+ Traversal::ContainsRoot contains_root;
+
+ //--
+
+ Traversal::Root root;
+
+ // Layer 3
+ //
+ Traversal::Defines defines;
+
+ //--
+ InterfaceHdr interface_hdr;
+ Traversal::UnconstrainedInterface interface_impl;
+
+ Module module;
+
+ TypeName type_name;
+
+ // Layer 4
+ //
+ Traversal::Inherits inherits;
+ Traversal::Defines interface_hdr_defines;
+ Traversal::Defines interface_impl_defines;
+
+ //--
+ OperationHdr operation_hdr;
+ OperationImpl operation_impl;
+
+ // Layer 5
+ //
+ Traversal::Receives receives;
+ Traversal::Returns returns;
+
+ //--
+
+ Parameter parameter;
+
+ // Layer 6
+ //
+ Traversal::Belongs belongs;
+
+ public:
+ void
+ generate (CCF::IDL2::SemanticGraph::TranslationUnit& tu)
+ {
+ // Plug automatic IDL indenter.
+ //
+ Indentation::Implanter<Indentation::Cxx> guard (cout);
+
+ unit.traverse (tu);
+ }
+ };
+}
+
+#endif // EXAMPLE_IDL2_CXX_MAPPING_GENERATOR_IMPL_HPP
diff --git a/modules/CIAO/CCF/Example/IDL2/CxxMapping/driver.cpp b/modules/CIAO/CCF/Example/IDL2/CxxMapping/driver.cpp
new file mode 100644
index 00000000000..11c6e895611
--- /dev/null
+++ b/modules/CIAO/CCF/Example/IDL2/CxxMapping/driver.cpp
@@ -0,0 +1,96 @@
+// file : Example/IDL2/CxxMapping/driver.cpp
+// author : Boris Kolpackov <boris@dre.vanderbilt.edu>
+// cvs-id : $Id$
+
+#include <iostream>
+
+#include "CCF/CompilerElements/Context.hpp"
+#include "CCF/CompilerElements/FileSystem.hpp"
+#include "CCF/CompilerElements/Diagnostic.hpp"
+#include "CCF/CompilerElements/TokenStream.hpp"
+#include "CCF/CompilerElements/Preprocessor.hpp"
+
+#include "CCF/IDL2/LexicalAnalyzer.hpp"
+#include "CCF/IDL2/Parser.hpp"
+#include "CCF/IDL2/SemanticGraph.hpp"
+#include "CCF/IDL2/SemanticAction/Impl/Factory.hpp"
+
+#include "Generator.hpp"
+
+using std::cerr;
+using std::cout;
+using std::endl;
+
+using namespace CCF::CompilerElements;
+using namespace CCF::IDL2;
+using namespace CCF::IDL2::SemanticGraph;
+
+//@@ code repetition in driver.
+//
+
+int
+main ()
+{
+ try
+ {
+ Diagnostic::Stream dout;
+
+ fs::path file_path ("stdout");
+
+ InputStreamAdapter isa (std::cin);
+ CPP::Preprocessor pp (isa);
+
+ LexicalAnalyzer lexer (pp);
+
+ TokenList token_stream;
+
+ //@@ bad token comparison
+ for (TokenPtr token = lexer.next ();; token = lexer.next ())
+ {
+ token_stream.push_back (token);
+ if (ReferenceCounting::strict_cast<EndOfStream> (token) != 0) break;
+ }
+
+ if (token_stream.size () < 2)
+ {
+ cerr << "no tokens produced so nothing to parse" << endl;
+ return 0;
+ }
+
+ TranslationUnit tu;
+
+ // Compilation context.
+ //
+ CCF::CompilerElements::Context context;
+ context.set ("file-path", file_path);
+ context.set ("trace-semantic-action", false);
+
+
+ SemanticAction::Impl::Factory actions (context, dout, tu);
+
+ Parser parser (context, dout, lexer, actions);
+
+ Parsing::parse (token_stream.begin (),
+ token_stream.end (),
+ parser.start ());
+
+ if (dout.error_count () != 0) return -1;
+
+ IDL2::Generator g;
+
+ g.generate (tu);
+ }
+ catch (std::bad_cast const&)
+ {
+ cerr << "bad cast exception" << endl;
+ }
+ catch (InvalidName const&)
+ {
+ cerr << "invalid name exception" << endl;
+ }
+ catch (...)
+ {
+ cerr << "caught unknown exception" << endl;
+ return -1;
+ }
+}
diff --git a/modules/CIAO/CCF/Example/IDL2/CxxMapping/test.idl b/modules/CIAO/CCF/Example/IDL2/CxxMapping/test.idl
new file mode 100644
index 00000000000..82e44440c58
--- /dev/null
+++ b/modules/CIAO/CCF/Example/IDL2/CxxMapping/test.idl
@@ -0,0 +1,12 @@
+// file : Example/IDL2/CxxMapping/test.idl
+// author : Boris Kolpackov <boris@dre.vanderbilt.edu>
+// cvs-id : $Id$
+
+module M
+{
+ interface I
+ {
+ long foo (in string s, inout boolean b, out long l);
+ void bar ();
+ };
+};