From 4974b4763bd60eb875f93a71dbe2fe82ecfed9fc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 5 Sep 2009 16:29:23 +0200 Subject: Add semantic graph and traversal mechanism The parser now builds the semantic graph. --- cli/traversal/class.cxx | 27 ++++++++ cli/traversal/class.hxx | 27 ++++++++ cli/traversal/elements.cxx | 15 +++++ cli/traversal/elements.hxx | 143 +++++++++++++++++++++++++++++++++++++++++++ cli/traversal/expression.hxx | 17 +++++ cli/traversal/namespace.cxx | 27 ++++++++ cli/traversal/namespace.hxx | 27 ++++++++ cli/traversal/option.cxx | 60 ++++++++++++++++++ cli/traversal/option.hxx | 75 +++++++++++++++++++++++ cli/traversal/unit.cxx | 47 ++++++++++++++ cli/traversal/unit.hxx | 59 ++++++++++++++++++ 11 files changed, 524 insertions(+) create mode 100644 cli/traversal/class.cxx create mode 100644 cli/traversal/class.hxx create mode 100644 cli/traversal/elements.cxx create mode 100644 cli/traversal/elements.hxx create mode 100644 cli/traversal/expression.hxx create mode 100644 cli/traversal/namespace.cxx create mode 100644 cli/traversal/namespace.hxx create mode 100644 cli/traversal/option.cxx create mode 100644 cli/traversal/option.hxx create mode 100644 cli/traversal/unit.cxx create mode 100644 cli/traversal/unit.hxx (limited to 'cli/traversal') diff --git a/cli/traversal/class.cxx b/cli/traversal/class.cxx new file mode 100644 index 0000000..54af3e1 --- /dev/null +++ b/cli/traversal/class.cxx @@ -0,0 +1,27 @@ +// file : cli/traversal/class.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include + +namespace traversal +{ + void class_:: + traverse (type& c) + { + pre (c); + names (c); + post (c); + } + + void class_:: + pre (type&) + { + } + + void class_:: + post (type&) + { + } +} diff --git a/cli/traversal/class.hxx b/cli/traversal/class.hxx new file mode 100644 index 0000000..d4610d9 --- /dev/null +++ b/cli/traversal/class.hxx @@ -0,0 +1,27 @@ +// file : cli/traversal/class.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CLI_TRAVERSAL_CLASS_HXX +#define CLI_TRAVERSAL_CLASS_HXX + +#include +#include + +namespace traversal +{ + struct class_: scope_template + { + virtual void + traverse (type&); + + virtual void + pre (type&); + + virtual void + post (type&); + }; +} + +#endif // CLI_TRAVERSAL_CLASS_HXX diff --git a/cli/traversal/elements.cxx b/cli/traversal/elements.cxx new file mode 100644 index 0000000..acabc3c --- /dev/null +++ b/cli/traversal/elements.cxx @@ -0,0 +1,15 @@ +// file : cli/traversal/elements.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include + +namespace traversal +{ + void names:: + traverse (type& e) + { + dispatch (e.named ()); + } +} diff --git a/cli/traversal/elements.hxx b/cli/traversal/elements.hxx new file mode 100644 index 0000000..ac57af4 --- /dev/null +++ b/cli/traversal/elements.hxx @@ -0,0 +1,143 @@ +// file : cli/traversal/elements.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CLI_TRAVERSAL_ELEMENTS_HXX +#define CLI_TRAVERSAL_ELEMENTS_HXX + +#include + +#include + +namespace traversal +{ + using namespace cutl; + + // + // + typedef compiler::dispatcher node_dispatcher; + typedef compiler::dispatcher edge_dispatcher; + + // + // + struct node_base: node_dispatcher, edge_dispatcher + { + void + edge_traverser (edge_dispatcher& d) + { + edge_dispatcher::traverser (d); + } + + edge_dispatcher& + edge_traverser () + { + return *this; + } + + using node_dispatcher::dispatch; + using edge_dispatcher::dispatch; + + using edge_dispatcher::iterate_and_dispatch; + }; + + struct edge_base: edge_dispatcher, node_dispatcher + { + void + node_traverser (node_dispatcher& d) + { + node_dispatcher::traverser (d); + } + + node_dispatcher& + node_traverser () + { + return *this; + } + + using edge_dispatcher::dispatch; + using node_dispatcher::dispatch; + + using node_dispatcher::iterate_and_dispatch; + }; + + inline edge_base& + operator>> (node_base& n, edge_base& e) + { + n.edge_traverser (e); + return e; + } + + inline node_base& + operator>> (edge_base& e, node_base& n) + { + e.node_traverser (n); + return n; + } + + // + // + template + struct node: compiler::traverser_impl, + virtual node_base + { + }; + + template + struct edge: compiler::traverser_impl, + virtual edge_base + { + }; + + // Edges + // + + struct names: edge + { + names () + { + } + + names (node_dispatcher& n) + { + node_traverser (n); + } + + virtual void + traverse (type&); + }; + + // Nodes + // + + struct nameable: node {}; + + template + struct scope_template: node + { + public: + virtual void + traverse (T& s) + { + names (s); + } + + virtual void + names (T& s) + { + names (s, *this); + } + + virtual void + names (T& s, edge_dispatcher& d) + { + iterate_and_dispatch (s.names_begin (), s.names_end (), d); + } + }; + + typedef scope_template scope; + + struct type: node {}; +} + +#endif // CLI_TRAVERSAL_ELEMENTS_HXX diff --git a/cli/traversal/expression.hxx b/cli/traversal/expression.hxx new file mode 100644 index 0000000..71d9f6e --- /dev/null +++ b/cli/traversal/expression.hxx @@ -0,0 +1,17 @@ +// file : cli/traversal/expression.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CLI_TRAVERSAL_EXPRESSION_HXX +#define CLI_TRAVERSAL_EXPRESSION_HXX + +#include +#include + +namespace traversal +{ + struct expression: node {}; +} + +#endif // CLI_TRAVERSAL_EXPRESSION_HXX diff --git a/cli/traversal/namespace.cxx b/cli/traversal/namespace.cxx new file mode 100644 index 0000000..47547db --- /dev/null +++ b/cli/traversal/namespace.cxx @@ -0,0 +1,27 @@ +// file : cli/traversal/namespace.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include + +namespace traversal +{ + void namespace_:: + traverse (type& n) + { + pre (n); + names (n); + post (n); + } + + void namespace_:: + pre (type&) + { + } + + void namespace_:: + post (type&) + { + } +} diff --git a/cli/traversal/namespace.hxx b/cli/traversal/namespace.hxx new file mode 100644 index 0000000..5903582 --- /dev/null +++ b/cli/traversal/namespace.hxx @@ -0,0 +1,27 @@ +// file : cli/traversal/namespace.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CLI_TRAVERSAL_NAMESPACE_HXX +#define CLI_TRAVERSAL_NAMESPACE_HXX + +#include +#include + +namespace traversal +{ + struct namespace_: scope_template + { + virtual void + traverse (type&); + + virtual void + pre (type&); + + virtual void + post (type&); + }; +} + +#endif // CLI_TRAVERSAL_NAMESPACE_HXX diff --git a/cli/traversal/option.cxx b/cli/traversal/option.cxx new file mode 100644 index 0000000..64e728c --- /dev/null +++ b/cli/traversal/option.cxx @@ -0,0 +1,60 @@ +// file : cli/traversal/option.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include +#include + +namespace traversal +{ + // belongs + // + void belongs:: + traverse (type& e) + { + dispatch (e.type ()); + } + + // initialized + // + void initialized:: + traverse (type& e) + { + dispatch (e.expression ()); + } + + // option + // + void option:: + traverse (type& o) + { + pre (o); + belongs (o); + if (o.initialized_p ()) + initialized (o); + post (o); + } + + void option:: + pre (type&) + { + } + + void option:: + belongs (type& o) + { + belongs (o, edge_traverser ()); + } + + void option:: + initialized (type& o) + { + initialized (o, edge_traverser ()); + } + + void option:: + post (type&) + { + } +} diff --git a/cli/traversal/option.hxx b/cli/traversal/option.hxx new file mode 100644 index 0000000..4ff2a58 --- /dev/null +++ b/cli/traversal/option.hxx @@ -0,0 +1,75 @@ +// file : cli/traversal/option.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CLI_TRAVERSAL_OPTION_HXX +#define CLI_TRAVERSAL_OPTION_HXX + +#include +#include + +namespace traversal +{ + struct belongs: edge + { + belongs () + { + } + + belongs (node_dispatcher& n) + { + node_traverser (n); + } + + virtual void + traverse (type&); + }; + + struct initialized: edge + { + initialized () + { + } + + initialized (node_dispatcher& n) + { + node_traverser (n); + } + + virtual void + traverse (type&); + }; + + struct option: node + { + virtual void + traverse (type&); + + virtual void + pre (type&); + + virtual void + belongs (type&); + + virtual void + initialized (type&); + + virtual void + post (type&); + + void + belongs (type& o, edge_dispatcher& d) + { + d.dispatch (o.belongs ()); + } + + void + initialized (type& o, edge_dispatcher& d) + { + d.dispatch (o.initialized ()); + } + }; +} + +#endif // CLI_TRAVERSAL_OPTION_HXX diff --git a/cli/traversal/unit.cxx b/cli/traversal/unit.cxx new file mode 100644 index 0000000..32b2e0d --- /dev/null +++ b/cli/traversal/unit.cxx @@ -0,0 +1,47 @@ +// file : cli/traversal/unit.cxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include + +namespace traversal +{ + // cxx_includes + // + void cxx_includes:: + traverse (type& e) + { + dispatch (e.includee ()); + } + + // cli_includes + // + void cli_includes:: + traverse (type& e) + { + dispatch (e.includee ()); + } + + // cli_unit + // + void cli_unit:: + traverse (type& u) + { + pre (u); + iterate_and_dispatch ( + u.includes_begin (), u.includes_end (), edge_traverser ()); + names (u); + post (u); + } + + void cli_unit:: + pre (type&) + { + } + + void cli_unit:: + post (type&) + { + } +} diff --git a/cli/traversal/unit.hxx b/cli/traversal/unit.hxx new file mode 100644 index 0000000..25ec556 --- /dev/null +++ b/cli/traversal/unit.hxx @@ -0,0 +1,59 @@ +// file : cli/traversal/unit.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2009 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CLI_TRAVERSAL_UNIT_HXX +#define CLI_TRAVERSAL_UNIT_HXX + +#include +#include + +namespace traversal +{ + struct cxx_includes: edge + { + cxx_includes () + { + } + + cxx_includes (node_dispatcher& n) + { + node_traverser (n); + } + + virtual void + traverse (type&); + }; + + struct cli_includes: edge + { + cli_includes () + { + } + + cli_includes (node_dispatcher& n) + { + node_traverser (n); + } + + virtual void + traverse (type&); + }; + + struct cxx_unit: node {}; + + struct cli_unit: scope_template + { + virtual void + traverse (type&); + + virtual void + pre (type&); + + virtual void + post (type&); + }; +} + +#endif // CLI_TRAVERSAL_UNIT_HXX -- cgit v1.1