OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
annotation.hpp
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (c) 2001-2011 Joel de Guzman
3 
4  Distributed under the Boost Software License, Version 1.0. (See accompanying
5  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 =============================================================================*/
7 #if !defined(ANNOTATION_HPP)
8 #define ANNOTATION_HPP
9 
10 #include <map>
11 #include <boost/variant/apply_visitor.hpp>
12 #include <boost/type_traits/is_base_of.hpp>
13 #include <boost/mpl/bool.hpp>
14 #include "ast.hpp"
15 
16 namespace client
17 {
19  // The annotation handler links the AST to a map of iterator positions
20  // for the purpose of subsequent semantic error handling when the
21  // program is being compiled.
23  template <typename Iterator>
24  struct annotation
25  {
26  template <typename>
27  struct result { typedef void type; };
28 
29  std::vector<Iterator>& iters;
30  annotation(std::vector<Iterator>& iters)
31  : iters(iters) {}
32 
33  struct set_id
34  {
35  typedef void result_type;
36 
37  int id;
38  set_id(int id) : id(id) {}
39 
41  {
42  x.function_name.id = id;
43  }
44 
45  void operator()(ast::identifier& x) const
46  {
47  x.id = id;
48  }
49 
50  template <typename T>
51  void operator()(T& /*x*/) const
52  {
53  // no-op
54  }
55  };
56 
57  void operator()(ast::operand& ast, Iterator pos) const
58  {
59  int id = iters.size();
60  iters.push_back(pos);
61  boost::apply_visitor(set_id(id), ast);
62  }
63 
64  void operator()(ast::identifier& ast, Iterator pos) const
65  {
66  int id = iters.size();
67  iters.push_back(pos);
68  ast.id = id;
69  }
70  };
71 }
72 
73 #endif
boost::variant< nil, bool, unsigned int, double, identifier, boost::recursive_wrapper< unary >, boost::recursive_wrapper< function_call >, boost::recursive_wrapper< expression > > operand
Definition: ast.hpp:53
void operator()(ast::operand &ast, Iterator pos) const
Definition: annotation.hpp:57
void operator()(ast::identifier &ast, Iterator pos) const
Definition: annotation.hpp:64
std::vector< Iterator > & iters
Definition: annotation.hpp:29
annotation(std::vector< Iterator > &iters)
Definition: annotation.hpp:30
void operator()(ast::identifier &x) const
Definition: annotation.hpp:45
void operator()(T &) const
Definition: annotation.hpp:51
void operator()(ast::function_call &x) const
Definition: annotation.hpp:40
identifier function_name
Definition: ast.hpp:94