/*
 * File:   CFAGraph.cpp
 * Author: pgess
 *
 * Created on June 27, 2016, 2:09 PM
 */

#include "analysis/cfagraph.h"
#include "analysis/aux.h"

using namespace xreate::analysis;
using namespace std;

void
CFAGraph::print(std::ostringstream& output) const {
            const std::string& atomBinding = Config::get("clasp.bindings.function");
            const std::string& atomBindingScope = Config::get("clasp.bindings.scope");

            //show function tags
    int counterTags = 0;
    std::ostringstream bufFunctionNames;
    boost::format formatFunction("function(%1%).");
    boost::format formatBind(atomBinding + "(%1%, %2%).");
    for (auto function: this->__nodesFunction.left) {
        const  auto tags = this->__functionTags.equal_range(function.first);
        if (tags.first == tags.second) {
            //no tags
            bufFunctionNames << "; " << function.second ;
            continue;
        }

        output << formatFunction % (function.second) << std::endl;
        for (const auto& tag_: boost::make_iterator_range(tags)){
            const Expression& tag = tag_.second;

            list<string> tagRaw = xreate::analysis::compile(tag);
                        assert(tagRaw.size() == 1);

            output << formatBind
                    % (function.second)
                    % (tagRaw.front())
                << endl;
            ++counterTags;
        }
    }

    if (bufFunctionNames.tellp()){
            output << formatFunction % (bufFunctionNames.str().substr(2)) << std::endl;
    }

    if (counterTags == 0) {
        output << "%no functtion tags at all" << endl;
    }

            //declare scopes
    boost::format formatScope("scope(0..%1%).");
    output << formatScope % (__clasp->getScopesCount() - 1) << std::endl;

    //show context rules:
    for (auto rule: this->__contextRules) {
            output << ContextRule(rule.second).compile(rule.first) << std::endl;
    };

            //show scope tags:
    counterTags = 0;
    boost::format formatScopeBind(atomBindingScope + "(%1%, %2%, strong).");
    for (auto entry: this->__scopeTags) {
        ScopePacked scopeId = entry.first;
        const Expression& tag = entry.second;
        list<string> tagRaw = xreate::analysis::compile(tag);
        assert(tagRaw.size() == 1);

        output << formatScopeBind % scopeId %(tagRaw.front()) << endl;
        ++counterTags;
    }

    if (counterTags == 0) {
            output << "%scope tags: no tags at all" << endl;
    }

    output << endl << "%\t\tStatic analysis: CFA" << endl;

            //parent connections
            //TOTEST CFG parent function
    boost::format formatFunctionParent("cfa_parent(%1%, function(%2%)).");
    for (const auto &relation: this->__parentFunctionRelations) {
        const string& function = this->__nodesFunction.left.at(relation.right);

        output << formatFunctionParent % relation.left % function << endl;
    }

            //TOTEST CFG parent scope
    boost::format formatScopeParent("cfa_parent(%1%, scope(%2%)).");
    for (const auto &relation: this->__parentScopeRelations) {
        output << formatScopeParent % relation.first % relation.second << endl;
    }

            //call connections
    boost::format formatCall("cfa_call(%1%, %2%).");

    for (const auto &relation: this->__callRelations) {
        const ScopePacked scopeFrom = relation.left;
        const string& functionTo = this->__nodesFunction.left.at(relation.right);

        output << formatCall % (scopeFrom) % (functionTo) << endl;
    }

            //function specializations descrtiption
            //SECTIONTAG late-context cfa_function_specializations
    boost::format formatSpecializations("cfa_function_specializations(%1%, %2%).");
    const list<ManagedFnPtr>& functions = __clasp->ast->getAllFunctions();
    for (auto f: functions){
        if (f->guardContext.isValid()){
            list<string> guardRaw = xreate::analysis::compile(f->guardContext);
            assert(guardRaw.size() == 1);
            output << formatSpecializations % (f->getName()) % (guardRaw.front()) << endl;
        }
    }
}

void
CFAGraph::addFunctionAnnotations(const std::string& function, const std::map<std::string, Expression>& tags) {
    unsigned int fid = registerNodeFunction(function);

    for (auto& tag: tags){
            __functionTags.emplace(fid, tag.second);
    }
}

void
CFAGraph::addScopeAnnotations(const ScopePacked& scope, const std::vector<Expression>& tags){
    for (Expression tag: tags){
            __scopeTags.emplace(scope, tag);
    }
}

void
CFAGraph::addContextRules(const ScopePacked& scope, const std::vector<Expression>& rules){
    for (Expression rule: rules){
            __contextRules.emplace(scope, rule);
    }
}

void
CFAGraph::addCallConnection(const ScopePacked& scopeFrom, const std::string& functionTo) {
    unsigned int idFuncTo = registerNodeFunction(functionTo);

    __callRelations.insert(CALL_RELATIONS::value_type(scopeFrom, idFuncTo));
}

void
CFAGraph::addParentConnection(const ScopePacked& scope, const std::string& functionParent){
    __parentFunctionRelations.insert(PARENT_FUNCTION_RELATIONS::value_type(scope, registerNodeFunction(functionParent)));
}

void
CFAGraph::addParentConnection(const ScopePacked& scope, const ScopePacked& scopeParent){
    __parentScopeRelations.emplace(scope, scopeParent);
}

unsigned int
CFAGraph::registerNodeFunction(const std::string& fname){
    auto pos = __nodesFunction.left.insert(make_pair(__nodesFunction.size(), fname));

    return pos.first->first;
}
