/* Copyright 2022 - 2026 R. Thomas
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef LIEF_PDB_PUBLIC_SYMBOL_H
#define LIEF_PDB_PUBLIC_SYMBOL_H
#include <cstdint>
#include <memory>
#include <ostream>
#include <string>

#include "LIEF/compiler_attributes.hpp"
#include "LIEF/iterators.hpp"
#include "LIEF/visibility.h"


namespace LIEF::pdb {

namespace details {
class PublicSymbol;
class PublicSymbolIt;
}

/// This class provides general information (RVA, name) about a symbol
/// from the PDB's public symbol stream (or Public symbol hash stream)
class LIEF_API PublicSymbol {
  public:
  class Iterator final
    : public iterator_facade_base<Iterator, std::forward_iterator_tag,
                                  PublicSymbol, std::ptrdiff_t,
                                  const PublicSymbol*, const PublicSymbol&> {
    public:
    using implementation = details::PublicSymbolIt;
    using iterator_facade_base::operator++;

    LIEF_API Iterator();

    LIEF_API Iterator(std::unique_ptr<details::PublicSymbolIt> impl);

    LIEF_API Iterator(const Iterator&);
    LIEF_API Iterator& operator=(const Iterator&);

    LIEF_API Iterator(Iterator&&) noexcept;
    LIEF_API Iterator& operator=(Iterator&&) noexcept;

    LIEF_API ~Iterator();

    // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
    LIEF_API Iterator& operator++();

    friend LIEF_API bool operator==(const Iterator& LHS, const Iterator& RHS);

    friend bool operator!=(const Iterator& LHS, const Iterator& RHS) {
      return !(LHS == RHS);
    }

    LIEF_API const PublicSymbol& operator*() const LIEF_LIFETIMEBOUND;

    // NOLINTNEXTLINE(bugprone-derived-method-shadowing-base-method)
    LIEF_API const PublicSymbol* operator->() const LIEF_LIFETIMEBOUND;

    /// Transfer ownership of the public symbol at the current position to
    /// the caller. Returns `nullptr` if the iterator is past-the-end.
    LIEF_API std::unique_ptr<PublicSymbol> yield();

    private:
    void load() const;

    std::unique_ptr<details::PublicSymbolIt> impl_;
    mutable std::unique_ptr<PublicSymbol> cached_;
  };
  PublicSymbol(std::unique_ptr<details::PublicSymbol> impl);
  ~PublicSymbol();

  enum class FLAGS : uint32_t {
    NONE = 0,
    CODE = 1 << 0,
    FUNCTION = 1 << 1,
    MANAGED = 1 << 2,
    MSIL = 1 << 3,
  };

  /// Name of the symbol
  std::string name() const;

  /// Demangled representation of the symbol
  std::string demangled_name() const;

  /// Name of the section in which this symbol is defined (e.g. `.text`).
  ///
  /// This function returns an empty string if the section's name can't be found
  std::string section_name() const;

  /// **Relative** Virtual Address of this symbol.
  ///
  /// This function returns 0 if the RVA can't be computed.
  uint32_t RVA() const;

  std::string to_string() const;

  LIEF_API friend std::ostream& operator<<(std::ostream& os,
                                           const PublicSymbol& sym) {
    os << sym.to_string();
    return os;
  }

  private:
  std::unique_ptr<details::PublicSymbol> impl_;
};

}

#endif
