File: Synopsis/PTree/TypeVisitor.hh
  1//
  2// Copyright (C) 2004 Stefan Seefeld
  3// All rights reserved.
  4// Licensed to the public under the terms of the GNU LGPL (>= 2),
  5// see the file COPYING for details.
  6//
  7#ifndef Synopsis_PTree_TypeVisitor_hh_
  8#define Synopsis_PTree_TypeVisitor_hh_
  9
 10#include <Synopsis/PTree.hh>
 11#include <Synopsis/Token.hh>
 12#include <cassert>
 13
 14namespace Synopsis
 15{
 16namespace PTree
 17{
 18
 19class TypeVisitor : public Visitor
 20{
 21public:
 22  TypeVisitor() : my_type(Token::BadToken) {}
 23
 24  Token::Type type_of(Node *node) { node->accept(this); return my_type;}
 25
 26  virtual void visit(Literal *) { my_type = Token::Constant;}
 27  virtual void visit(Identifier *) { my_type = Token::Identifier;}
 28  virtual void visit(Keyword *kwd) { my_type = kwd->token();}
 29  virtual void visit(Typedef *) {my_type = Token::ntTypedef;}
 30  virtual void visit(TemplateDecl *) { my_type = Token::ntTemplateDecl;}
 31  virtual void visit(TemplateInstantiation *) { my_type = Token::ntTemplateInstantiation;}
 32  virtual void visit(ExternTemplate *) { my_type = Token::ntExternTemplate;}
 33  virtual void visit(MetaclassDecl *) { my_type = Token::ntMetaclassDecl;}
 34  virtual void visit(ParameterDeclaration *) { my_type = Token::ntParameterDecl;}
 35  virtual void visit(LinkageSpec *) { my_type = Token::ntLinkageSpec;}
 36  virtual void visit(NamespaceSpec *) { my_type = Token::ntNamespaceSpec;}
 37  virtual void visit(NamespaceAlias *) { my_type = Token::ntNamespaceAlias;}
 38  virtual void visit(UsingDirective *) { my_type = Token::ntUsing;}
 39  virtual void visit(Declaration *) { my_type = Token::ntDeclaration;}
 40  virtual void visit(UsingDeclaration *) { my_type = Token::ntUsing;}
 41  virtual void visit(Declarator *) { my_type = Token::ntDeclarator;}
 42  virtual void visit(Name *) { my_type = Token::ntName;}
 43  virtual void visit(FstyleCastExpr *) { my_type = Token::ntFstyleCast;}
 44  virtual void visit(ClassSpec *) { my_type = Token::ntClassSpec;}
 45  virtual void visit(EnumSpec *) { my_type = Token::ntEnumSpec;}
 46  virtual void visit(TypeParameter *) { my_type = 0;} // FIXME !!!
 47  virtual void visit(AccessSpec *) { my_type = Token::ntAccessSpec;}
 48  virtual void visit(AccessDecl *) { my_type = Token::ntAccessDecl;}
 49  virtual void visit(UserAccessSpec *) { my_type = Token::ntUserAccessSpec;}
 50  virtual void visit(IfStatement *) { my_type = Token::ntIfStatement;}
 51  virtual void visit(SwitchStatement *) { my_type = Token::ntSwitchStatement;}
 52  virtual void visit(WhileStatement *) { my_type = Token::ntWhileStatement;}
 53  virtual void visit(DoStatement *) { my_type = Token::ntDoStatement;}
 54  virtual void visit(ForStatement *) { my_type = Token::ntForStatement;}
 55  virtual void visit(TryStatement *) { my_type = Token::ntTryStatement;}
 56  virtual void visit(BreakStatement *) { my_type = Token::ntBreakStatement;}
 57  virtual void visit(ContinueStatement *) { my_type = Token::ntContinueStatement;}
 58  virtual void visit(ReturnStatement *) { my_type = Token::ntReturnStatement;}
 59  virtual void visit(GotoStatement *) { my_type = Token::ntGotoStatement;}
 60  virtual void visit(CaseStatement *) { my_type = Token::ntCaseStatement;}
 61  virtual void visit(DefaultStatement *) { my_type = Token::ntDefaultStatement;}
 62  virtual void visit(LabelStatement *) { my_type = Token::ntLabelStatement;}
 63  virtual void visit(ExprStatement *) { my_type = Token::ntExprStatement;}
 64  virtual void visit(Expression *) { my_type = Token::ntCommaExpr;}
 65  virtual void visit(AssignExpr *) { my_type = Token::ntAssignExpr;}
 66  virtual void visit(CondExpr *) { my_type = Token::ntCondExpr;}
 67  virtual void visit(InfixExpr *) { my_type = Token::ntInfixExpr;}
 68  virtual void visit(PmExpr *) { my_type = Token::ntPmExpr;}
 69  virtual void visit(CastExpr *) { my_type = Token::ntCastExpr;}
 70  virtual void visit(UnaryExpr *) { my_type = Token::ntUnaryExpr;}
 71  virtual void visit(ThrowExpr *) { my_type = Token::ntThrowExpr;}
 72  virtual void visit(SizeofExpr *) { my_type = Token::ntSizeofExpr;}
 73  virtual void visit(TypeidExpr *) { my_type = Token::ntTypeidExpr;}
 74  virtual void visit(TypeofExpr *) { my_type = Token::ntTypeofExpr;}
 75  virtual void visit(NewExpr *) { my_type = Token::ntNewExpr;}
 76  virtual void visit(DeleteExpr *) { my_type = Token::ntDeleteExpr;}
 77  virtual void visit(ArrayExpr *) { my_type = Token::ntArrayExpr;}
 78  virtual void visit(FuncallExpr *) { my_type = Token::ntFuncallExpr;}
 79  virtual void visit(PostfixExpr *) { my_type = Token::ntPostfixExpr;}
 80  virtual void visit(DotMemberExpr *) { my_type = Token::ntDotMemberExpr;}
 81  virtual void visit(ArrowMemberExpr *) { my_type = Token::ntArrowMemberExpr;}
 82  virtual void visit(ParenExpr *) { my_type = Token::ntParenExpr;}
 83private:
 84  Token::Type my_type;
 85};
 86
 87inline Token::Type type_of(const Node *node)
 88{
 89  assert(node);
 90  TypeVisitor v;
 91  return v.type_of(const_cast<Node *>(node));
 92}
 93
 94inline bool is_a(const Node *node, Token::Type t)
 95{
 96  if (!node) return false;
 97  TypeVisitor v;
 98  Token::Type type = v.type_of(const_cast<Node *>(node));
 99  return type == t;
100}
101
102inline bool is_a(const Node *node, Token::Type t1, Token::Type t2)
103{
104  if (!node) return false;
105  TypeVisitor v;
106  Token::Type type = v.type_of(const_cast<Node *>(node));
107  return type == t1 || type == t2;
108}
109
110inline bool is_a(const Node *node, Token::Type t1, Token::Type t2, Token::Type t3)
111{
112  if (!node) return false;
113  TypeVisitor v;
114  Token::Type type = v.type_of(const_cast<Node *>(node));
115  return type == t1 || type == t2 || type == t3;
116}
117
118}
119}
120
121#endif