#include "abstractpass.h"
#include "attachments.h"
#include "xreatemanager.h"

using namespace std;

namespace xreate {

template<>
void defaultValue<void>(){}

void AbstractPassBase::finish(){}

AbstractPassBase::AbstractPassBase(PassManager *manager)
        : man(manager) {
}

template<>
void
AbstractPass<void>::processSymbol(const Symbol& symbol, PassContext context, const std::string& hintSymbol)
{
    if (__visitedSymbols.isCached(symbol))
        return;

    __visitedSymbols.setCachedValue(symbol);
    const Expression& declaration = CodeScope::getDeclaration(symbol);

    if (declaration.isDefined()){
        PassContext context2 = context.updateScope(symbol.scope);
        process(declaration, context2, hintSymbol);
    }
}

template<>
void
AbstractPass<void>::process(const Expression& expression, PassContext context, const std::string& varDecl){
    if (expression.__state == Expression::COMPOUND){
        for (const Expression &op: expression.getOperands()) {
            process(op, context);
        }

        for (CodeScope* scope: expression.blocks) {
            process(scope, context);
        }

        if (expression.op == Operator::CALL){
            processExpressionCall(expression, context);
        }

        return;
    }

    if (expression.__state == Expression::IDENT){
        assert(context.scope);
        processSymbol(Attachments::get<Symbol>(expression), context, expression.getValueString());
    }
}
}
