/* 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:   adhocfunctiondecorator.h
 * Author: pgess <v.melnychenko@xreate.org>
 *
 * Created on July 29, 2017, 12:26 PM
 */

#ifndef ADHOCFUNCTIONDECORATOR_H
#define ADHOCFUNCTIONDECORATOR_H

#include "ast.h"
#include "llvmlayer.h"

using namespace xreate::compilation;

namespace xreate{ namespace adhoc{
template<class Parent>
class AdhocFunctionDecorator: public Parent{
public:
    AdhocFunctionDecorator(ManagedFnPtr f, CompilePass* p)
        : Parent(f, p)    {}

protected:
    llvm::Type* prepareResult(){
        PassManager* man = Parent::pass->man;
        CodeScope* entry = Parent::function->__entry;
        LLVMLayer* llvm = Parent::pass->man->llvm;
        AST* ast = Parent::pass->man->root;
        adhoc::AdhocPass* adhocpass = reinterpret_cast<AdhocPass*>(man->getPassById(PassId::AdhocPass));

        if (! Parent::function->isPrefunction){
            return Parent::prepareResult();
        }

        adhocImplementation = adhocpass->findAssotiatedScheme(entry);
        return llvm->toLLVMType(ast->expandType(adhocImplementation->getResultType()));
    }

public:
    AdhocScheme* adhocImplementation=nullptr;
};

}} //end of namespace xreate::adhoc

#endif /* ADHOCFUNCTIONDECORATOR_H */

