// Copyright 2024 The Dawn & Tint Authors
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
//    contributors may be used to endorse or promote products derived from
//    this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// GEN_BUILD:CONDITION(tint_build_wgsl_reader)

#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>

#include "src/tint/cmd/fuzz/wgsl/fuzz.h"
#include "src/tint/lang/hlsl/validate/validate.h"
#include "src/tint/lang/hlsl/writer/writer.h"
#include "src/tint/lang/wgsl/ast/module.h"
#include "src/tint/lang/wgsl/ast/transform/renamer.h"
#include "src/tint/utils/command/command.h"
#include "src/tint/utils/text/string.h"

namespace tint::hlsl::writer {
namespace {

bool CanRun(const Program& program) {
    if (program.AST().HasOverrides()) {
        return false;
    }

    // The PixelLocal transform assumes only a single entry point, so check for multiple entry
    // points if chromium_experimental_pixel_local is enabled.
    uint32_t num_entry_points = 0;
    for (auto* fn : program.AST().Functions()) {
        if (fn->PipelineStage() != ast::PipelineStage::kNone) {
            num_entry_points++;
        }
    }
    for (auto* enable : program.AST().Enables()) {
        if (enable->HasExtension(tint::wgsl::Extension::kChromiumExperimentalPixelLocal)) {
            if (num_entry_points > 1) {
                return false;
            }
            break;
        }
    }

    return true;
}

void ASTFuzzer(const tint::Program& program,
               const fuzz::wgsl::Context& context,
               const Options& options) {
    if (!CanRun(program)) {
        return;
    }

    // Currently disabled, as DXC can error on HLSL emitted by Tint. For example: post optimization
    // infinite loops will fail to compile, but these are beyond Tint's analysis capabilities.
    constexpr bool must_validate = false;

    const char* dxc_path = validate::kDxcDLLName;
    Result<tint::hlsl::writer::Output> res;
    if (!context.options.dxc.empty()) {
        dxc_path = context.options.dxc.c_str();
        ast::transform::DataMap inputs, outputs;
        inputs.Add<ast::transform::Renamer::Config>(ast::transform::Renamer::Target::kHlslKeywords,
                                                    /* preserve_unicode */ false);
        if (auto renamer_res = tint::ast::transform::Renamer{}.Apply(program, inputs, outputs)) {
            if (!renamer_res->IsValid()) {
                TINT_ICE() << renamer_res->Diagnostics();
            }
            res = tint::hlsl::writer::Generate(*renamer_res, options);
        }

    } else {
        res = tint::hlsl::writer::Generate(program, options);
    }

    if (res != Success) {
        return;
    }

    if (context.options.dump) {
        std::cout << "Dumping generated HLSL:\n" << res->hlsl << std::endl;
    }

    auto dxc = tint::Command::LookPath(dxc_path);
    if (dxc.Found()) {
        uint32_t hlsl_shader_model = 60;
        bool require_16bit_types = false;
        auto enable_list = program.AST().Enables();
        for (auto* enable : enable_list) {
            if (enable->HasExtension(tint::wgsl::Extension::kF16)) {
                hlsl_shader_model = 62;
                require_16bit_types = true;
                break;
            }
        }

        auto validate_res = validate::ValidateUsingDXC(dxc.Path(), res->hlsl, res->entry_points,
                                                       require_16bit_types, hlsl_shader_model);

        if (must_validate && validate_res.failed) {
            size_t line_num = 1;
            std::stringstream err;
            err << "DXC was expected to succeed, but failed:\n\n";
            for (auto line : Split(res->hlsl, "\n")) {
                err << line_num++ << ": " << line << "\n";
            }
            err << "\n\n" << validate_res.output;
            TINT_ICE() << err.str();
        }

    } else if (must_validate) {
        TINT_ICE() << "cannot validate with DXC as it was not found at: " << dxc_path;
    }
}

}  // namespace
}  // namespace tint::hlsl::writer

TINT_WGSL_PROGRAM_FUZZER(tint::hlsl::writer::ASTFuzzer);
