-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotation.h
91 lines (75 loc) · 2.28 KB
/
annotation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*=============================================================================
Copyright (c) 2001-2011 Joel de Guzman
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#ifndef ANNOTATION_H
#define ANNOTATION_H
#include <boost/variant/apply_visitor.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/mpl/bool.hpp>
#include <ast.h>
#include <QDebug>
typedef std::string::const_iterator Iterator;
struct annotation
{
template <typename, typename>
struct result { typedef void type; };
std::vector<Iterator>& iters;
annotation(std::vector<Iterator>& iters)
: iters(iters) {}
struct set_id
{
typedef void result_type;
int id;
set_id(int id) : id(id) {}
template <typename T>
void operator()(T& x) const
{
this->dispatch(x, boost::is_base_of<ast::tagged, T>());
}
// This will catch all nodes except those inheriting from ast::tagged
template <typename T>
void dispatch(T& /*x*/, boost::mpl::false_) const
{
// (no-op) no need for tags
}
// This will catch all nodes inheriting from ast::tagged
template <typename T>
void dispatch(T& x, boost::mpl::true_) const
{
x.id = id;
}
};
void operator()(ast::tagged& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
void operator()(ast::operand& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
boost::apply_visitor(set_id(id), ast);
}
void operator()(ast::declaration& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
void operator()(ast::return_statement& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
void operator()(ast::identifier& ast, Iterator pos) const
{
int id = iters.size();
iters.push_back(pos);
ast.id = id;
}
};
#endif // ANNOTATION_H