// Copyright 2021 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.

#ifndef SRC_DAWN_NATIVE_COMPILATIONMESSAGES_H_
#define SRC_DAWN_NATIVE_COMPILATIONMESSAGES_H_

#include <memory>
#include <string>
#include <vector>

#include "src/dawn/common/MutexProtected.h"
#include "src/dawn/native/Error.h"
#include "src/dawn/native/Serializable.h"
#include "src/dawn/native/dawn_platform.h"
#include "src/utils/non_copyable.h"

namespace tint::diag {
class Diagnostic;
class List;
}  // namespace tint::diag

namespace dawn::native {

uint64_t CountUTF16CodeUnitsFromUTF8String(const std::string_view& utf8String);

// CompilationMessageContent is serializable and holds the content of each compilation message.
#define COMPILATION_MESSAGE_CONTENT_MEMBER(X) \
    X(std::string, message)                   \
    X(wgpu::CompilationMessageType, type)     \
    X(uint64_t, lineNum)                      \
    X(uint64_t, linePosInBytes)               \
    X(uint64_t, offsetInBytes)                \
    X(uint64_t, lengthInBytes)                \
    X(uint64_t, linePosInUTF16)               \
    X(uint64_t, offsetInUTF16)                \
    X(uint64_t, lengthInUTF16)
DAWN_SERIALIZABLE(struct, CompilationMessageContent, COMPILATION_MESSAGE_CONTENT_MEMBER){};
#undef COMPILATION_MESSAGE_CONTENT_MEMBER

// ParsedCompilationMessages holds the compilation messages generated by Tint or loaded from cache,
// and is used to construct the OwnedCompilationMessages needed in ShaderModuleBase.
#define PARSED_COMPILATION_MESSAGES_MEMBER(X)           \
    X(std::vector<CompilationMessageContent>, messages) \
    X(std::vector<std::string>, formattedTintMessages)
DAWN_SERIALIZABLE(struct, ParsedCompilationMessages, PARSED_COMPILATION_MESSAGES_MEMBER) {
    // Adds a message on line 0 (before the first line).
    void AddUnanchoredMessage(std::string_view message, wgpu::CompilationMessageType type =
                                                            wgpu::CompilationMessageType::Info);
    // For testing only. Uses the linePos/offset/length for both utf8 and utf16
    // (which is incorrect for non-ASCII strings).
    void AddMessageForTesting(
        std::string_view message,
        wgpu::CompilationMessageType type = wgpu::CompilationMessageType::Info,
        uint64_t lineNum = 0, uint64_t linePos = 0, uint64_t offset = 0, uint64_t length = 0);
    void AddMessages(const tint::diag::List& diagnostics);

    const CompilationInfo* GetCompilationInfo();
    const std::vector<std::string>& GetFormattedTintMessages() const;

  private:
    void AddMessage(const tint::diag::Diagnostic& diagnostic);
    void AddMessage(CompilationMessageContent && message);
    void AddFormattedTintMessages(const tint::diag::List& diagnostics);
};
#undef PARSED_COMPILATION_MESSAGES_MEMBER

// Members of OwnedCompilationMessages hold the raw pointers to each other, so
// OwnedCompilationMessages is non-movable. OwnedCompilationMessages is immutable after
// construction, so it is thread-safe.
class OwnedCompilationMessages : public NonMovable {
  public:
    // Create and store the required CompilationInfo and pointed CompilationMessage and
    // DawnCompilationMessageUtf16 objects in constructor, and OwnedCompilationMessages will be
    // read-only afterward.
    explicit OwnedCompilationMessages(ParsedCompilationMessages&& parsedCompilationMessagges);

    const CompilationInfo* GetCompilationInfo() const;
    const std::vector<std::string>& GetFormattedTintMessages() const;
    bool HasWarningsOrErrors() const;

  private:
    const ParsedCompilationMessages mMessageContents;
    bool mHasWarningsOrErrors = false;
    CompilationInfo mCompilationInfo;
    std::vector<DawnCompilationMessageUtf16> mUtf16Messages;
    std::vector<CompilationMessage> mMessagesList;
};

}  // namespace dawn::native

#endif  // SRC_DAWN_NATIVE_COMPILATIONMESSAGES_H_
