]> rtime.felk.cvut.cz Git - boost-statechart-viewer.git/blobdiff - src/visualizer.cpp
Add support for deferred events
[boost-statechart-viewer.git] / src / visualizer.cpp
index 4aceb86f8c10642e424962411d1fef257c38aa83..5b7827643e1cc72069e78a6c896e7e510c19cad8 100644 (file)
@@ -19,7 +19,9 @@
 ////////////////////////////////////////////////////////////////////////////////////////
 
 //standard header files
+#include <iomanip>
 #include <fstream>
+#include <map>
 
 //LLVM Header files
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
 using namespace std;
 
-class Statechart
+namespace Model
 {
-public:
+
+    inline int getIndentLevelIdx() {
+       static int i = ios_base::xalloc();
+       return i;
+    }
+
+    ostream& indent(ostream& os) { os << setw(2*os.iword(getIndentLevelIdx())) << ""; return os; }
+    ostream& indent_inc(ostream& os) { os.iword(getIndentLevelIdx())++; return os; }
+    ostream& indent_dec(ostream& os) { os.iword(getIndentLevelIdx())--; return os; }
+
+    class State;
+
+    class Context : public map<string, State*> {
+    public:
+       iterator add(State *state);
+       Context *findContext(const string &name);
+    };
+
+    class State : public Context
+    {
+       string initialInnerState;
+       list<string> defferedEvents;
+    public:
+       const string name;
+       explicit State(string name) : name(name) {}
+       void setInitialInnerState(string name) { initialInnerState = name; }
+       void addDeferredEvent(const string &name) { defferedEvents.push_back(name); }
+       friend ostream& operator<<(ostream& os, const State& s);
+    };
+
+
+    Context::iterator Context::add(State *state)
+    {
+       pair<iterator, bool> ret =  insert(value_type(state->name, state));
+       return ret.first;
+    }
+
+    Context *Context::findContext(const string &name)
+    {
+       iterator i = find(name), e;
+       if (i != end())
+           return i->second;
+       for (i = begin(), e = end(); i != e; ++i) {
+           Context *c = i->second->findContext(name);
+           if (c)
+               return c;
+       }
+       return 0;
+    }
+
+    ostream& operator<<(ostream& os, const Context& c);
+
+    ostream& operator<<(ostream& os, const State& s)
+    {
+       string label = s.name;
+       for (list<string>::const_iterator i = s.defferedEvents.begin(), e = s.defferedEvents.end(); i != e; ++i)
+           label.append("<br />").append(*i).append(" / defer");
+       os << indent << s.name << " [label=<" << label << ">]\n";
+       if (s.size()) {
+           os << indent << s.name << " -> " << s.initialInnerState << " [style = dashed]\n";
+           os << indent << "subgraph cluster_" << s.name << " {\n" << indent_inc;
+           os << indent << "label = \"" << s.name << "\"\n";
+           os << indent << s.initialInnerState << " [peripheries=2]\n";
+           os << static_cast<Context>(s);
+           os << indent_dec << indent << "}\n";
+       }
+       return os;
+    }
+
+
+    ostream& operator<<(ostream& os, const Context& c)
+    {
+       for (Context::const_iterator i = c.begin(), e = c.end(); i != e; i++) {
+           os << *i->second;
+       }
+       return os;
+    }
+
+
     class Transition
     {
     public:
-       string src, dst, event;
+       const string src, dst, event;
        Transition(string src, string dst, string event) : src(src), dst(dst), event(event) {}
     };
-    string name;
-    string name_of_start;
-    list<Transition> transitions;
-    list<string> cReactions; /** list of custom reactions. After all files are traversed this list should be empty. */
-    list<string> events;
-    list<string> states;
-
-    void write_dot_file(string fn)
-    {
-       ofstream f(fn.c_str());
-       f << "digraph " << name << " {\n";
-       f << "  " << name_of_start << " [peripheries=2]\n";
-       for (list<string>::iterator s = states.begin(), e = states.end(); s != e; ++s) {
-           f << "  " << *s << "\n";
+
+    ostream& operator<<(ostream& os, const Transition& t)
+    {
+       os << indent << t.src << " -> " << t.dst << " [label = \"" << t.event << "\"]\n";
+       return os;
+    }
+
+
+    class Machine : public Context
+    {
+    protected:
+       string initial_state;
+    public:
+       const string name;
+       explicit Machine(string name) : name(name) {}
+
+       void setInitialState(string name) { initial_state = name; }
+
+       friend ostream& operator<<(ostream& os, const Machine& m);
+    };
+
+    ostream& operator<<(ostream& os, const Machine& m)
+    {
+       os << indent << "subgraph " << m.name << " {\n" << indent_inc;
+       os << indent << m.initial_state << " [peripheries=2]\n";
+       os << static_cast<Context>(m);
+       os << indent_dec << indent << "}\n";
+       return os;
+    }
+
+
+    class Model : public map<string, Machine>
+    {
+       Context undefined;      // For forward-declared state classes
+    public:
+       list< Transition*> transitions;
+
+       iterator add(const Machine &m)
+       {
+           pair<iterator, bool> ret =  insert(value_type(m.name, m));
+           return ret.first;
        }
 
-       for (list<Transition>::iterator t = transitions.begin(), e = transitions.end(); t != e; ++t) {
-           f << t->src << " -> " << t->dst << " [label = \"" << t->event << "\"]\n";
+       void addUndefinedState(State *m)
+       {
+           undefined[m->name] = m;
        }
 
-       f << "}";
-    }
+
+       Context *findContext(const string &name)
+       {
+           Context::iterator ci = undefined.find(name);
+           if (ci != undefined.end())
+               return ci->second;
+           iterator i = find(name), e;
+           if (i != end())
+               return &i->second;
+           for (i = begin(), e = end(); i != e; ++i) {
+               Context *c = i->second.findContext(name);
+               if (c)
+                   return c;
+           }
+           return 0;
+       }
+
+       State *findState(const string &name)
+       {
+           for (iterator i = begin(), e = end(); i != e; ++i) {
+               Context *c = i->second.findContext(name);
+               if (c)
+                   return static_cast<State*>(c);
+           }
+           return 0;
+       }
+
+
+       State *removeFromUndefinedContexts(const string &name)
+       {
+           Context::iterator ci = undefined.find(name);
+           if (ci == undefined.end())
+               return 0;
+           undefined.erase(ci);
+           return ci->second;
+       }
+
+       void write_as_dot_file(string fn)
+       {
+           ofstream f(fn.c_str());
+           f << "digraph statecharts {\n" << indent_inc;
+           for (iterator i = begin(), e = end(); i != e; i++)
+               f << i->second;
+           for (list<Transition*>::iterator t = transitions.begin(), e = transitions.end(); t != e; ++t)
+               f << **t;
+           f << indent_dec << "}\n";
+       }
+    };
 };
 
 
@@ -95,20 +249,42 @@ public:
     }
 };
 
+class FindTransitVisitor : public RecursiveASTVisitor<FindTransitVisitor>
+{
+    Model::Model &model;
+    const CXXRecordDecl *SrcState;
+    const Type *EventType;
+public:
+    explicit FindTransitVisitor(Model::Model &model, const CXXRecordDecl *SrcState, const Type *EventType)
+       : model(model), SrcState(SrcState), EventType(EventType) {}
+
+    bool VisitMemberExpr(MemberExpr *E) {
+       if (E->getMemberNameInfo().getAsString() != "transit")
+           return true;
+       if (E->hasExplicitTemplateArgs()) {
+           const Type *DstStateType = E->getExplicitTemplateArgs()[0].getArgument().getAsType().getTypePtr();
+           CXXRecordDecl *DstState = DstStateType->getAsCXXRecordDecl();
+           CXXRecordDecl *Event = EventType->getAsCXXRecordDecl();
+           Model::Transition *T = new Model::Transition(SrcState->getName(), DstState->getName(), Event->getName());
+           model.transitions.push_back(T);
+       }
+       return true;
+    }
+};
 
 class Visitor : public RecursiveASTVisitor<Visitor>
 {
-    ASTContext *Context;
-    Statechart &sc;
+    ASTContext *ASTCtx;
+    Model::Model &model;
     DiagnosticsEngine &Diags;
     unsigned diag_unhandled_reaction_type, diag_unhandled_reaction_decl,
-       diag_found_state, diag_found_statemachine;
+       diag_found_state, diag_found_statemachine, diag_no_history;
 
 public:
     bool shouldVisitTemplateInstantiations() const { return true; }
 
-    explicit Visitor(ASTContext *Context, Statechart &sc, DiagnosticsEngine &Diags)
-       : Context(Context), sc(sc), Diags(Diags)
+    explicit Visitor(ASTContext *Context, Model::Model &model, DiagnosticsEngine &Diags)
+       : ASTCtx(Context), model(model), Diags(Diags)
     {
        diag_found_statemachine =
            Diags.getCustomDiagID(DiagnosticsEngine::Note, "Found statemachine '%0'");
@@ -118,12 +294,32 @@ public:
            Diags.getCustomDiagID(DiagnosticsEngine::Error, "Unhandled reaction type '%0'");
        diag_unhandled_reaction_decl =
            Diags.getCustomDiagID(DiagnosticsEngine::Error, "Unhandled reaction decl '%0'");
+       diag_unhandled_reaction_decl =
+           Diags.getCustomDiagID(DiagnosticsEngine::Error, "History is not yet supported");
     }
 
     DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) { return Diags.Report(Loc, DiagID); }
 
+    void HandleCustomReaction(const CXXRecordDecl *SrcState, const Type *EventType)
+    {
+       IdentifierInfo& II = ASTCtx->Idents.get("react");
+       // TODO: Lookup for react even in base classes - probably by using Sema::LookupQualifiedName()
+       for (DeclContext::lookup_const_result ReactRes = SrcState->lookup(DeclarationName(&II));
+            ReactRes.first != ReactRes.second; ++ReactRes.first) {
+           if (CXXMethodDecl *React = dyn_cast<CXXMethodDecl>(*ReactRes.first))
+               if (const ParmVarDecl *p = React->getParamDecl(0)) {
+                   const Type *ParmType = p->getType().getTypePtr();
+                   if (ParmType->isLValueReferenceType())
+                       ParmType = dyn_cast<LValueReferenceType>(ParmType)->getPointeeType().getTypePtr();
+                   if (ParmType == EventType)
+                       FindTransitVisitor(model, SrcState, EventType).TraverseStmt(React->getBody());
+               }
+       }
+    }
+
     void HandleReaction(const Type *T, const SourceLocation Loc, CXXRecordDecl *SrcState)
     {
+       // TODO: Improve Loc tracking
        if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(T))
            HandleReaction(ET->getNamedType().getTypePtr(), Loc, SrcState);
        else if (const TemplateSpecializationType *TST = dyn_cast<TemplateSpecializationType>(T)) {
@@ -134,13 +330,23 @@ public:
                CXXRecordDecl *Event = EventType->getAsCXXRecordDecl();
                CXXRecordDecl *DstState = DstStateType->getAsCXXRecordDecl();
 
-               sc.transitions.push_back(Statechart::Transition(SrcState->getName(), DstState->getName(),
-                                                               Event->getName()));
+               Model::Transition *T = new Model::Transition(SrcState->getName(), DstState->getName(), Event->getName());
+               model.transitions.push_back(T);
+           } else if (name == "boost::statechart::custom_reaction") {
+               const Type *EventType = TST->getArg(0).getAsType().getTypePtr();
+               HandleCustomReaction(SrcState, EventType);
+           } else if (name == "boost::statechart::deferral") {
+               const Type *EventType = TST->getArg(0).getAsType().getTypePtr();
+               CXXRecordDecl *Event = EventType->getAsCXXRecordDecl();
+
+               Model::State *s = model.findState(SrcState->getName());
+               assert(s);
+               s->addDeferredEvent(Event->getName());
            } else if (name == "boost::mpl::list") {
                for (TemplateSpecializationType::iterator Arg = TST->begin(), End = TST->end(); Arg != End; ++Arg)
                    HandleReaction(Arg->getAsType().getTypePtr(), Loc, SrcState);
-           }
-           //->getDecl()->getQualifiedNameAsString();
+           } else
+               Diag(Loc, diag_unhandled_reaction_type) << name;
        } else
            Diag(Loc, diag_unhandled_reaction_type) << T->getTypeClassName();
     }
@@ -154,22 +360,55 @@ public:
            Diag(Decl->getLocation(), diag_unhandled_reaction_decl) << Decl->getDeclKindName();
     }
 
+    CXXRecordDecl *getTemplateArgDecl(const Type *T, unsigned ArgNum)
+    {
+       if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(T))
+           return getTemplateArgDecl(ET->getNamedType().getTypePtr(), ArgNum);
+       else if (const TemplateSpecializationType *TST = dyn_cast<TemplateSpecializationType>(T)) {
+           if (TST->getNumArgs() >= ArgNum+1)
+               return TST->getArg(ArgNum).getAsType()->getAsCXXRecordDecl();
+       }
+       return 0;
+    }
+
 
     bool VisitCXXRecordDecl(CXXRecordDecl *Declaration)
     {
        if (!Declaration->isCompleteDefinition())
            return true;
+       if (Declaration->getQualifiedNameAsString() == "boost::statechart::state")
+           return true; // This is an "abstract class" not a real state
 
        MyCXXRecordDecl *RecordDecl = static_cast<MyCXXRecordDecl*>(Declaration);
        const CXXBaseSpecifier *Base;
 
-       if (RecordDecl->isDerivedFrom("boost::statechart::simple_state"))
+       if (RecordDecl->isDerivedFrom("boost::statechart::simple_state", &Base))
        {
-           string state(RecordDecl->getName()); //getQualifiedNameAsString());
-           Diag(RecordDecl->getLocStart(), diag_found_state) << state;
-           sc.states.push_back(state);
+           string name(RecordDecl->getName()); //getQualifiedNameAsString());
+           Diag(RecordDecl->getLocStart(), diag_found_state) << name;
+
+           Model::State *state;
+           // Either we saw a reference to forward declared state
+           // before, or we create a new state.
+           if (!(state = model.removeFromUndefinedContexts(name)))
+               state = new Model::State(name);
+
+           CXXRecordDecl *Context = getTemplateArgDecl(Base->getType().getTypePtr(), 1);
+           Model::Context *c = model.findContext(Context->getName());
+           if (!c) {
+               Model::State *s = new Model::State(Context->getName());
+               model.addUndefinedState(s);
+               c = s;
+           }
+           c->add(state);
+
+           if (CXXRecordDecl *InnerInitialState = getTemplateArgDecl(Base->getType().getTypePtr(), 2))
+               state->setInitialInnerState(InnerInitialState->getName());
 
-           IdentifierInfo& II = Context->Idents.get("reactions");
+//         if (CXXRecordDecl *History = getTemplateArgDecl(Base->getType().getTypePtr(), 3))
+//             Diag(History->getLocStart(), diag_no_history);
+
+           IdentifierInfo& II = ASTCtx->Idents.get("reactions");
            // TODO: Lookup for reactions even in base classes - probably by using Sema::LookupQualifiedName()
            for (DeclContext::lookup_result Reactions = RecordDecl->lookup(DeclarationName(&II));
                 Reactions.first != Reactions.second; ++Reactions.first)
@@ -177,18 +416,16 @@ public:
        }
        else if (RecordDecl->isDerivedFrom("boost::statechart::state_machine", &Base))
        {
-           sc.name = RecordDecl->getQualifiedNameAsString();
-           Diag(RecordDecl->getLocStart(), diag_found_statemachine) << sc.name;
+           Model::Machine m(RecordDecl->getName());
+           Diag(RecordDecl->getLocStart(), diag_found_statemachine) << m.name;
 
-           if (const ElaboratedType *ET = dyn_cast<ElaboratedType>(Base->getType())) {
-               if (const TemplateSpecializationType *TST = dyn_cast<TemplateSpecializationType>(ET->getNamedType())) {
-                   sc.name_of_start = TST->getArg(1).getAsType()->getAsCXXRecordDecl()->getName();
-               }
-           }
+           if (CXXRecordDecl *InitialState = getTemplateArgDecl(Base->getType().getTypePtr(), 1))
+               m.setInitialState(InitialState->getName());
+           model.add(m);
        }
        else if (RecordDecl->isDerivedFrom("boost::statechart::event"))
        {
-           sc.events.push_back(RecordDecl->getNameAsString());
+           //sc.events.push_back(RecordDecl->getNameAsString());
        }
        return true;
     }
@@ -197,17 +434,17 @@ public:
 
 class VisualizeStatechartConsumer : public clang::ASTConsumer
 {
-    Statechart statechart;
+    Model::Model model;
     Visitor visitor;
     string destFileName;
 public:
     explicit VisualizeStatechartConsumer(ASTContext *Context, std::string destFileName,
                                         DiagnosticsEngine &D)
-       : visitor(Context, statechart, D), destFileName(destFileName) {}
+       : visitor(Context, model, D), destFileName(destFileName) {}
 
     virtual void HandleTranslationUnit(clang::ASTContext &Context) {
        visitor.TraverseDecl(Context.getTranslationUnitDecl());
-       statechart.write_dot_file(destFileName);
+       model.write_as_dot_file(destFileName);
     }
 };