3 #include <boost/foreach.hpp>
5 #include <core/assert.hh>
6 #include "generator.hh"
8 namespace ag
= ast::generator
;
11 generateBrowseVisitorHeader(const char * working_directory
)
13 assert(working_directory
);
16 std::string
path(working_directory
);
18 path
.append("/browse-visitor.hh");
19 out
.open(path
.c_str(), std::fstream::out
| std::fstream::trunc
);
20 assert(out
.is_open() && out
.good());
24 ag::appendGeneratedWarning(out
);
25 out
<< "#ifndef AST_BROWSE_VISITOR_HH" NL
26 << "# define AST_BROWSE_VISITOR_HH" NL
28 << "#include <ast/visitor.hh>" NL
33 #define BROWSE_VISITOR_HH(Prefix, Const) \
34 out << " /** @defgroup Visitors */" NL \
36 << " * @brief A visitor which browse the AST from the top to the bottom." NL \
37 << " * @ingroup Visitors" NL \
39 << " class "Prefix"BrowseVisitor : public "Prefix"Visitor" NL \
42 << " virtual ~"Prefix"BrowseVisitor();" NL \
45 for (node = ag::nodes; !node->name.empty() > 0; node++) \
46 out << " virtual void visit("Const << node->name << " & node);" \
51 BROWSE_VISITOR_HH("", "");
52 BROWSE_VISITOR_HH("Const", "const ");
54 out
<< "} // namespace ast" NL
55 << "#endif // !AST_BROWSE_VISITOR_HH" NL
;
60 generateBrowseVisitorSource(const char * working_directory
)
62 assert(working_directory
);
65 std::string
path(working_directory
);
67 path
.append("/browse-visitor.cc");
68 out
.open(path
.c_str(), std::fstream::out
| std::fstream::trunc
);
69 assert(out
.is_open() && out
.good());
73 ag::appendGeneratedWarning(out
);
74 out
<< "#include <boost/foreach.hpp>" NL
75 << "#include <core/assert.hh>" NL
76 << "#include <ast/browse-visitor.hh>" NL
77 << "#include <ast/ast.hh>" NL NL
78 << "#define NL << std::endl" NL NL
82 #define BROWSE_VISITOR_CC(Name, Const) \
83 out << " "Name"::~"Name"() {}" NL NL; \
85 for (node = ag::nodes; !node->name.empty() > 0; node++) \
87 out << " void "Name"::visit("Const << node->name \
88 << " & node __attribute__((unused)))" NL \
91 BOOST_FOREACH(const ag::Attribute * attr, allAttributes(node)) \
93 if (ag::isNode(attr->type)) \
94 out << " node." << attr->name << "->accept(*this);" NL; \
95 else if (ag::isVectorOfNode(attr->type)) \
96 out << " if (node." << attr->name << ")" NL \
97 << " BOOST_FOREACH(Node * n, (*node." << attr->name \
100 << " n->accept(*this);" NL \
107 BROWSE_VISITOR_CC("BrowseVisitor", "");
108 BROWSE_VISITOR_CC("ConstBrowseVisitor", "const ");
110 out
<< "} // namespace ast" NL
;
114 int main(int argc
, char **argv
)
117 generateBrowseVisitorHeader(argv
[1]);
118 generateBrowseVisitorSource(argv
[1]);