llvm/mlir/lib/Target/Cpp/TranslateToCpp.cpp

//===- TranslateToCpp.cpp - Translating to C++ calls ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/Support/IndentedOstream.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Target/Cpp/CppEmitter.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ScopedHashTable.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FormatVariadic.h"
#include <stack>
#include <utility>

#define DEBUG_TYPE

usingnamespacemlir;
usingnamespacemlir::emitc;
formatv;

/// Convenience functions to produce interleaved output with functions returning
/// a LogicalResult. This is different than those in STLExtras as functions used
/// on each element doesn't return a string.
template <typename ForwardIterator, typename UnaryFunctor,
          typename NullaryFunctor>
inline LogicalResult
interleaveWithError(ForwardIterator begin, ForwardIterator end,
                    UnaryFunctor eachFn, NullaryFunctor betweenFn) {}

template <typename Container, typename UnaryFunctor, typename NullaryFunctor>
inline LogicalResult interleaveWithError(const Container &c,
                                         UnaryFunctor eachFn,
                                         NullaryFunctor betweenFn) {}

template <typename Container, typename UnaryFunctor>
inline LogicalResult interleaveCommaWithError(const Container &c,
                                              raw_ostream &os,
                                              UnaryFunctor eachFn) {}

/// Return the precedence of a operator as an integer, higher values
/// imply higher precedence.
static FailureOr<int> getOperatorPrecedence(Operation *operation) {}

namespace {
/// Emitter that uses dialect specific emitters to emit C++ code.
struct CppEmitter {};
} // namespace

/// Determine whether expression \p op should be emitted in a deferred way.
static bool hasDeferredEmission(Operation *op) {}

/// Determine whether expression \p expressionOp should be emitted inline, i.e.
/// as part of its user. This function recommends inlining of any expressions
/// that can be inlined unless it is used by another expression, under the
/// assumption that  any expression fusion/re-materialization was taken care of
/// by transformations run by the backend.
static bool shouldBeInlined(ExpressionOp expressionOp) {}

static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
                                     Attribute value) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::ConstantOp constantOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::VariableOp variableOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::GlobalOp globalOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::AssignOp assignOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::LoadOp loadOp) {}

static LogicalResult printBinaryOperation(CppEmitter &emitter,
                                          Operation *operation,
                                          StringRef binaryOperator) {}

static LogicalResult printUnaryOperation(CppEmitter &emitter,
                                         Operation *operation,
                                         StringRef unaryOperator) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::AddOp addOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::DivOp divOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::MulOp mulOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::RemOp remOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::SubOp subOp) {}

static LogicalResult emitSwitchCase(CppEmitter &emitter,
                                    raw_indented_ostream &os, Region &region) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::SwitchOp switchOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::CmpOp cmpOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::ConditionalOp conditionalOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::VerbatimOp verbatimOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    cf::BranchOp branchOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    cf::CondBranchOp condBranchOp) {}

static LogicalResult printCallOperation(CppEmitter &emitter, Operation *callOp,
                                        StringRef callee) {}

static LogicalResult printOperation(CppEmitter &emitter, func::CallOp callOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::CallOp callOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::CallOpaqueOp callOpaqueOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::ApplyOp applyOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::BitwiseAndOp bitwiseAndOp) {}

static LogicalResult
printOperation(CppEmitter &emitter,
               emitc::BitwiseLeftShiftOp bitwiseLeftShiftOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::BitwiseNotOp bitwiseNotOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::BitwiseOrOp bitwiseOrOp) {}

static LogicalResult
printOperation(CppEmitter &emitter,
               emitc::BitwiseRightShiftOp bitwiseRightShiftOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::BitwiseXorOp bitwiseXorOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::UnaryPlusOp unaryPlusOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::UnaryMinusOp unaryMinusOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::CastOp castOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::ExpressionOp expressionOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::IncludeOp includeOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::LogicalAndOp logicalAndOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::LogicalNotOp logicalNotOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::LogicalOrOp logicalOrOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::ForOp forOp) {}

static LogicalResult printOperation(CppEmitter &emitter, emitc::IfOp ifOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    func::ReturnOp returnOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::ReturnOp returnOp) {}

static LogicalResult printOperation(CppEmitter &emitter, ModuleOp moduleOp) {}

static LogicalResult printFunctionArgs(CppEmitter &emitter,
                                       Operation *functionOp,
                                       ArrayRef<Type> arguments) {}

static LogicalResult printFunctionArgs(CppEmitter &emitter,
                                       Operation *functionOp,
                                       Region::BlockArgListType arguments) {}

static LogicalResult printFunctionBody(CppEmitter &emitter,
                                       Operation *functionOp,
                                       Region::BlockListType &blocks) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    func::FuncOp functionOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    emitc::FuncOp functionOp) {}

static LogicalResult printOperation(CppEmitter &emitter,
                                    DeclareFuncOp declareFuncOp) {}

CppEmitter::CppEmitter(raw_ostream &os, bool declareVariablesAtTop)
    :{}

std::string CppEmitter::getSubscriptName(emitc::SubscriptOp op) {}

std::string CppEmitter::createMemberAccess(emitc::MemberOp op) {}

std::string CppEmitter::createMemberAccess(emitc::MemberOfPtrOp op) {}

void CppEmitter::cacheDeferredOpResult(Value value, StringRef str) {}

/// Return the existing or a new name for a Value.
StringRef CppEmitter::getOrCreateName(Value val) {}

/// Return the existing or a new label for a Block.
StringRef CppEmitter::getOrCreateName(Block &block) {}

bool CppEmitter::shouldMapToUnsigned(IntegerType::SignednessSemantics val) {}

bool CppEmitter::hasValueInScope(Value val) {}

bool CppEmitter::hasBlockLabel(Block &block) {}

LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {}

LogicalResult CppEmitter::emitExpression(ExpressionOp expressionOp) {}

LogicalResult CppEmitter::emitOperand(Value value) {}

LogicalResult CppEmitter::emitOperands(Operation &op) {}

LogicalResult
CppEmitter::emitOperandsAndAttributes(Operation &op,
                                      ArrayRef<StringRef> exclude) {}

LogicalResult CppEmitter::emitVariableAssignment(OpResult result) {}

LogicalResult CppEmitter::emitVariableDeclaration(OpResult result,
                                                  bool trailingSemicolon) {}

LogicalResult CppEmitter::emitGlobalVariable(GlobalOp op) {}

LogicalResult CppEmitter::emitAssignPrefix(Operation &op) {}

LogicalResult CppEmitter::emitLabel(Block &block) {}

LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {}

LogicalResult CppEmitter::emitVariableDeclaration(Location loc, Type type,
                                                  StringRef name) {}

LogicalResult CppEmitter::emitType(Location loc, Type type) {}

LogicalResult CppEmitter::emitTypes(Location loc, ArrayRef<Type> types) {}

LogicalResult CppEmitter::emitTupleType(Location loc, ArrayRef<Type> types) {}

LogicalResult emitc::translateToCpp(Operation *op, raw_ostream &os,
                                    bool declareVariablesAtTop) {}