/*
 * 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:   latereasoning.h
 * Author: pgess <v.melnychenko@xreate.org>
 *
 * Created on May 26, 2018, 3:44 PM
 */

#ifndef CMPLLATEREASONING_H
#define CMPLLATEREASONING_H

#include "ast.h"
#include "aux/latereasoning.h"
#include "pass/compilepass.h"
#include "llvmlayer.h"
#include "transcendlayer.h"

namespace xreate{
namespace interpretation{
class InterpretationScope;
}
}

namespace llvm{
class Value;
}

namespace xreate{
namespace latereasoning{

typedef std::function<llvm::Value*() > CompilerHandler;
typedef std::function<llvm::Value*(const Gringo::Symbol&) > Handler;

class LateReasoningCompiler{
public:
    LateReasoningCompiler(interpretation::InterpretationScope* scope,
                          compilation::Context ctx) 
        : __context(ctx), __scopeI12n(scope) {}
        
    llvm::Value* processSwitchLateStatement(const Expression& expr,
                                            const std::string& identHint);
    llvm::Value* compileAutoExpand(const LateAnnotation& annotation,
                           llvm::Type* resultT,
                           const std::string& hint,
                           Handler handler);

private:
    compilation::Context __context;
    interpretation::InterpretationScope* __scopeI12n;

    llvm::Value* compileExpand(const Symbol& keyS,
                               llvm::Value* keyRaw,
                               const ExpandedType& domainT,
                               llvm::Type* resultT,
                               const std::string& hint,
                               CompilerHandler compilerBody);

    std::list<Expression> findKeys(const std::list<SymbolPacked>& keys);
};

}
}

#endif /* CMPLLATEREASONING_H */

