/* 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/.
 * 
 * adhocpass.h
 *
 *  Created on: Nov 28, 2015
 *      Author: pgess <v.melnychenko@xreate.org>
 */

//SECTIONTAG adhoc pass
#ifndef SRC_INSTRUCTIONS_ADHOC_H_
#define SRC_INSTRUCTIONS_ADHOC_H_

#include "abstractpass.h"

#ifndef FRIENDS_ADHOC
    #define FRIENDS_ADHOC
#endif

namespace xreate { namespace context {
    class ContextQuery;
}}

namespace xreate{ namespace adhoc{
class AdhocScheme {
public:
    AdhocScheme(const Expression& scheme);
    CodeScope* getCommandImplementation(const Expression& comm);
    const TypeAnnotation& getResultType();
    const std::string& getName();

private:
    TypeAnnotation __resultType;
    std::string __name;
    std::map<std::string, CodeScope*> __commands;
};

class AdhocExpression: public Expression{
public:
    AdhocExpression();
    AdhocExpression(const Expression& base);
    
    void setCommand(const Expression& comm);
    Expression getCommand() const;
};

class AdhocPass: public AbstractPass<void> {
    FRIENDS_ADHOC
public:
    AdhocPass(PassManager* manager): AbstractPass(manager) {}
    void run() override;
    AdhocScheme* findAssotiatedScheme(CodeScope* entry);
    
private:
    std::map<std::string, AdhocScheme*> __schemes;
    context::ContextQuery* queryContext;
};

}} //end of namespace xreate::adhoc

#endif /* SRC_INSTRUCTIONS_ADHOC_H_ */
