/* 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/.
 *
 * operators.cpp
 *
 * Author: pgess <v.melnychenko@xreate.org>
 * Created on April 8, 2017, 1:35 PM
 */

#include "pointers.h"
#include "llvmlayer.h"

#ifdef XREATE_ENABLE_EXTERN
  #include "ExternLayer.h"
#endif

#include <vector>

using namespace llvm;
using namespace std;

namespace xreate {

namespace pointers {

llvm::Value*
PointerArithmetic::add(llvm::Value *left, llvm::Value *right, compilation::Context context, const std::string& hintVarDecl){
    LLVMLayer* llvm = context.pass->man->llvm;

    if (left->getType()->isPointerTy() && right->getType()->isIntegerTy()){

        std::vector<llvm::Value*> indexes{right};
        //{llvm::ConstantInt::get(llvm::Type::getInt32Ty(llvm::getGlobalContext()), 0)};
        //indexes.push_back(right);

        return llvm->irBuilder.CreateGEP(left, llvm::ArrayRef<llvm::Value*>(indexes), hintVarDecl);
    }

    return nullptr;
}

}//end of pointers namespace
} //end of xreate namespace
