/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * File:   DominatorsAnalysisProvider.h
 * Author: pgess  <v.melnychenko@xreate.org>
 *
 * Created on May 13, 2016, 11:39 AM
 */

#ifndef DominatorsAnalysisProvider_H
#define DominatorsAnalysisProvider_H

#include "transcendlayer.h"
#include <boost/smart_ptr/scoped_ptr.hpp>

namespace xreate{namespace dominators{

class DominatorTree;
class PostDominatorTree;
class ScopeNode;

struct CFAGraphAdapter {
    std::list<ScopeNode*> nodes;
    ScopeNode* nodeRoot;

    ScopeNode* registerScope(ScopePacked scope);
    static CFAGraphAdapter* build(const cfa::CFAGraph* graph);
    static CFAGraphAdapter* build(const std::multimap<ScopePacked, ScopePacked>& dataScopesOrder);
    CFAGraphAdapter() { }
};

/** \brief Dominators Analysis report */
class DominatorsAnalysisProvider: public IAnalysisReport {
public:
    typedef std::pair<ScopePacked, ScopePacked> DominatedRange;
    typedef std::map<ScopePacked, DominatedRange> Dominators;

    DominatorsAnalysisProvider();
    virtual ~DominatorsAnalysisProvider();

    void run(CFAGraphAdapter* program);
    void print(std::ostringstream& output) const override;

    const Dominators& getForwardDominators() const;
    const Dominators& getPostDominators() const;

private:
    boost::shared_ptr<DominatorTree> treeForwardDominators;
    boost::shared_ptr<PostDominatorTree> treePostDominators;
};

}} //end of namespace xreate::dominators

#endif /* DominatorsAnalysisProvider_H */

