From 023b9b38cc9c6d31f1a01c29ac324c535308fa59 Mon Sep 17 00:00:00 2001
From: Liam <byteslice@airmail.cc>
Date: Wed, 9 Aug 2023 20:58:47 -0400
Subject: [PATCH] general: fix apple clang build

---
 src/common/settings_common.cpp         |   2 +
 src/common/settings_enums.h            |  16 ++--
 src/common/settings_setting.h          |  10 +-
 src/core/memory.h                      | 125 +++++++++++++------------
 src/video_core/engines/maxwell_dma.cpp |   1 +
 5 files changed, 81 insertions(+), 73 deletions(-)

diff --git a/src/common/settings_common.cpp b/src/common/settings_common.cpp
index dedf5ef909..137b65d5f7 100644
--- a/src/common/settings_common.cpp
+++ b/src/common/settings_common.cpp
@@ -1,7 +1,9 @@
 // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
 // SPDX-License-Identifier: GPL-2.0-or-later
 
+#include <functional>
 #include <string>
+#include <vector>
 #include "common/settings_common.h"
 
 namespace Settings {
diff --git a/src/common/settings_enums.h b/src/common/settings_enums.h
index a1a29ebf63..e7cb59ea5b 100644
--- a/src/common/settings_enums.h
+++ b/src/common/settings_enums.h
@@ -12,8 +12,8 @@ namespace Settings {
 
 template <typename T>
 struct EnumMetadata {
-    static constexpr std::vector<std::pair<std::string, T>> Canonicalizations();
-    static constexpr u32 Index();
+    static std::vector<std::pair<std::string, T>> Canonicalizations();
+    static u32 Index();
 };
 
 #define PAIR_45(N, X, ...) {#X, N::X} __VA_OPT__(, PAIR_46(N, __VA_ARGS__))
@@ -66,11 +66,11 @@ struct EnumMetadata {
 #define ENUM(NAME, ...)                                                                            \
     enum class NAME : u32 { __VA_ARGS__ };                                                         \
     template <>                                                                                    \
-    constexpr std::vector<std::pair<std::string, NAME>> EnumMetadata<NAME>::Canonicalizations() {  \
+    inline std::vector<std::pair<std::string, NAME>> EnumMetadata<NAME>::Canonicalizations() {     \
         return {PAIR(NAME, __VA_ARGS__)};                                                          \
     }                                                                                              \
     template <>                                                                                    \
-    constexpr u32 EnumMetadata<NAME>::Index() {                                                    \
+    inline u32 EnumMetadata<NAME>::Index() {                                                       \
         return __COUNTER__;                                                                        \
     }
 
@@ -85,7 +85,7 @@ enum class AudioEngine : u32 {
 };
 
 template <>
-constexpr std::vector<std::pair<std::string, AudioEngine>>
+inline std::vector<std::pair<std::string, AudioEngine>>
 EnumMetadata<AudioEngine>::Canonicalizations() {
     return {
         {"auto", AudioEngine::Auto},
@@ -96,7 +96,7 @@ EnumMetadata<AudioEngine>::Canonicalizations() {
 }
 
 template <>
-constexpr u32 EnumMetadata<AudioEngine>::Index() {
+inline u32 EnumMetadata<AudioEngine>::Index() {
     // This is just a sufficiently large number that is more than the number of other enums declared
     // here
     return 100;
@@ -147,7 +147,7 @@ ENUM(AntiAliasing, None, Fxaa, Smaa, MaxEnum);
 ENUM(AspectRatio, R16_9, R4_3, R21_9, R16_10, Stretch);
 
 template <typename Type>
-constexpr std::string CanonicalizeEnum(Type id) {
+inline std::string CanonicalizeEnum(Type id) {
     const auto group = EnumMetadata<Type>::Canonicalizations();
     for (auto& [name, value] : group) {
         if (value == id) {
@@ -158,7 +158,7 @@ constexpr std::string CanonicalizeEnum(Type id) {
 }
 
 template <typename Type>
-constexpr Type ToEnum(const std::string& canonicalization) {
+inline Type ToEnum(const std::string& canonicalization) {
     const auto group = EnumMetadata<Type>::Canonicalizations();
     for (auto& [name, value] : group) {
         if (name == canonicalization) {
diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h
index a8beb06e9d..e10843c73c 100644
--- a/src/common/settings_setting.h
+++ b/src/common/settings_setting.h
@@ -190,7 +190,7 @@ public:
         }
     }
 
-    [[nodiscard]] std::string constexpr Canonicalize() const override final {
+    [[nodiscard]] std::string Canonicalize() const override final {
         if constexpr (std::is_enum_v<Type>) {
             return CanonicalizeEnum(this->GetValue());
         } else {
@@ -256,11 +256,11 @@ public:
      * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
      * @param other_setting_ A second Setting to associate to this one in metadata
      */
+    template <typename T = BasicSetting>
     explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name,
                                Category category_, u32 specialization_ = Specialization::Default,
                                bool save_ = true, bool runtime_modifiable_ = false,
-                               BasicSetting* other_setting_ = nullptr)
-        requires(!ranged)
+                               typename std::enable_if<!ranged, T*>::type other_setting_ = nullptr)
         : Setting<Type, false>{
               linkage, default_val,         name,          category_, specialization_,
               save_,   runtime_modifiable_, other_setting_} {
@@ -282,12 +282,12 @@ public:
      * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded
      * @param other_setting_ A second Setting to associate to this one in metadata
      */
+    template <typename T = BasicSetting>
     explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val,
                                const Type& max_val, const std::string& name, Category category_,
                                u32 specialization_ = Specialization::Default, bool save_ = true,
                                bool runtime_modifiable_ = false,
-                               BasicSetting* other_setting_ = nullptr)
-        requires(ranged)
+                               typename std::enable_if<ranged, T*>::type other_setting_ = nullptr)
         : Setting<Type, true>{linkage,         default_val, min_val,
                               max_val,         name,        category_,
                               specialization_, save_,       runtime_modifiable_,
diff --git a/src/core/memory.h b/src/core/memory.h
index 2eb61ffd34..13047a5451 100644
--- a/src/core/memory.h
+++ b/src/core/memory.h
@@ -509,9 +509,9 @@ class GuestMemory {
 
 public:
     GuestMemory() = delete;
-    explicit GuestMemory(M& memory_, u64 addr_, std::size_t size_,
+    explicit GuestMemory(M& memory, u64 addr, std::size_t size,
                          Common::ScratchBuffer<T>* backup = nullptr)
-        : memory{memory_}, addr{addr_}, size{size_} {
+        : m_memory{memory}, m_addr{addr}, m_size{size} {
         static_assert(FLAGS & GuestMemoryFlags::Read || FLAGS & GuestMemoryFlags::Write);
         if constexpr (FLAGS & GuestMemoryFlags::Read) {
             Read(addr, size, backup);
@@ -521,89 +521,97 @@ public:
     ~GuestMemory() = default;
 
     T* data() noexcept {
-        return data_span.data();
+        return m_data_span.data();
     }
 
     const T* data() const noexcept {
-        return data_span.data();
+        return m_data_span.data();
+    }
+
+    size_t size() const noexcept {
+        return m_size;
+    }
+
+    size_t size_bytes() const noexcept {
+        return this->size() * sizeof(T);
     }
 
     [[nodiscard]] T* begin() noexcept {
-        return data();
+        return this->data();
     }
 
     [[nodiscard]] const T* begin() const noexcept {
-        return data();
+        return this->data();
     }
 
     [[nodiscard]] T* end() noexcept {
-        return data() + size;
+        return this->data() + this->size();
     }
 
     [[nodiscard]] const T* end() const noexcept {
-        return data() + size;
+        return this->data() + this->size();
     }
 
     T& operator[](size_t index) noexcept {
-        return data_span[index];
+        return m_data_span[index];
     }
 
     const T& operator[](size_t index) const noexcept {
-        return data_span[index];
+        return m_data_span[index];
     }
 
-    void SetAddressAndSize(u64 addr_, std::size_t size_) noexcept {
-        addr = addr_;
-        size = size_;
-        addr_changed = true;
+    void SetAddressAndSize(u64 addr, std::size_t size) noexcept {
+        m_addr = addr;
+        m_size = size;
+        m_addr_changed = true;
     }
 
-    std::span<T> Read(u64 addr_, std::size_t size_,
+    std::span<T> Read(u64 addr, std::size_t size,
                       Common::ScratchBuffer<T>* backup = nullptr) noexcept {
-        addr = addr_;
-        size = size_;
-        if (size == 0) {
-            is_data_copy = true;
+        m_addr = addr;
+        m_size = size;
+        if (m_size == 0) {
+            m_is_data_copy = true;
             return {};
         }
 
-        if (TrySetSpan()) {
+        if (this->TrySetSpan()) {
             if constexpr (FLAGS & GuestMemoryFlags::Safe) {
-                memory.FlushRegion(addr, size * sizeof(T));
+                m_memory.FlushRegion(m_addr, this->size_bytes());
             }
         } else {
             if (backup) {
-                backup->resize_destructive(size);
-                data_span = *backup;
+                backup->resize_destructive(this->size());
+                m_data_span = *backup;
             } else {
-                data_copy.resize(size);
-                data_span = std::span(data_copy);
+                m_data_copy.resize(this->size());
+                m_data_span = std::span(m_data_copy);
             }
-            is_data_copy = true;
-            span_valid = true;
+            m_is_data_copy = true;
+            m_span_valid = true;
             if constexpr (FLAGS & GuestMemoryFlags::Safe) {
-                memory.ReadBlock(addr, data_span.data(), size * sizeof(T));
+                m_memory.ReadBlock(m_addr, this->data(), this->size_bytes());
             } else {
-                memory.ReadBlockUnsafe(addr, data_span.data(), size * sizeof(T));
+                m_memory.ReadBlockUnsafe(m_addr, this->data(), this->size_bytes());
             }
         }
-        return data_span;
+        return m_data_span;
     }
 
     void Write(std::span<T> write_data) noexcept {
         if constexpr (FLAGS & GuestMemoryFlags::Cached) {
-            memory.WriteBlockCached(addr, write_data.data(), size * sizeof(T));
+            m_memory.WriteBlockCached(m_addr, write_data.data(), this->size_bytes());
         } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
-            memory.WriteBlock(addr, write_data.data(), size * sizeof(T));
+            m_memory.WriteBlock(m_addr, write_data.data(), this->size_bytes());
         } else {
-            memory.WriteBlockUnsafe(addr, write_data.data(), size * sizeof(T));
+            m_memory.WriteBlockUnsafe(m_addr, write_data.data(), this->size_bytes());
         }
     }
 
     bool TrySetSpan() noexcept {
-        if (u8* ptr = memory.GetSpan(addr, size * sizeof(T)); ptr) {
-            data_span = {reinterpret_cast<T*>(ptr), size};
-            span_valid = true;
+        if (u8* ptr = m_memory.GetSpan(m_addr, this->size_bytes()); ptr) {
+            m_data_span = {reinterpret_cast<T*>(ptr), this->size()};
+            m_span_valid = true;
             return true;
         }
         return false;
@@ -611,36 +619,36 @@ public:
 
 protected:
     bool IsDataCopy() const noexcept {
-        return is_data_copy;
+        return m_is_data_copy;
     }
 
     bool AddressChanged() const noexcept {
-        return addr_changed;
+        return m_addr_changed;
     }
 
-    M& memory;
-    u64 addr;
-    size_t size;
-    std::span<T> data_span{};
-    std::vector<T> data_copy;
-    bool span_valid{false};
-    bool is_data_copy{false};
-    bool addr_changed{false};
+    M& m_memory;
+    u64 m_addr{};
+    size_t m_size{};
+    std::span<T> m_data_span{};
+    std::vector<T> m_data_copy{};
+    bool m_span_valid{false};
+    bool m_is_data_copy{false};
+    bool m_addr_changed{false};
 };
 
 template <typename M, typename T, GuestMemoryFlags FLAGS>
 class GuestMemoryScoped : public GuestMemory<M, T, FLAGS> {
 public:
     GuestMemoryScoped() = delete;
-    explicit GuestMemoryScoped(M& memory_, u64 addr_, std::size_t size_,
+    explicit GuestMemoryScoped(M& memory, u64 addr, std::size_t size,
                                Common::ScratchBuffer<T>* backup = nullptr)
-        : GuestMemory<M, T, FLAGS>(memory_, addr_, size_, backup) {
+        : GuestMemory<M, T, FLAGS>(memory, addr, size, backup) {
         if constexpr (!(FLAGS & GuestMemoryFlags::Read)) {
             if (!this->TrySetSpan()) {
                 if (backup) {
-                    this->data_span = *backup;
-                    this->span_valid = true;
-                    this->is_data_copy = true;
+                    this->m_data_span = *backup;
+                    this->m_span_valid = true;
+                    this->m_is_data_copy = true;
                 }
             }
         }
@@ -648,24 +656,21 @@ public:
 
     ~GuestMemoryScoped() {
         if constexpr (FLAGS & GuestMemoryFlags::Write) {
-            if (this->size == 0) [[unlikely]] {
+            if (this->size() == 0) [[unlikely]] {
                 return;
             }
 
             if (this->AddressChanged() || this->IsDataCopy()) {
-                ASSERT(this->span_valid);
+                ASSERT(this->m_span_valid);
                 if constexpr (FLAGS & GuestMemoryFlags::Cached) {
-                    this->memory.WriteBlockCached(this->addr, this->data_span.data(),
-                                                  this->size * sizeof(T));
+                    this->m_memory.WriteBlockCached(this->m_addr, this->data(), this->size_bytes());
                 } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
-                    this->memory.WriteBlock(this->addr, this->data_span.data(),
-                                            this->size * sizeof(T));
+                    this->m_memory.WriteBlock(this->m_addr, this->data(), this->size_bytes());
                 } else {
-                    this->memory.WriteBlockUnsafe(this->addr, this->data_span.data(),
-                                                  this->size * sizeof(T));
+                    this->m_memory.WriteBlockUnsafe(this->m_addr, this->data(), this->size_bytes());
                 }
             } else if constexpr (FLAGS & GuestMemoryFlags::Safe) {
-                this->memory.InvalidateRegion(this->addr, this->size * sizeof(T));
+                this->m_memory.InvalidateRegion(this->m_addr, this->size_bytes());
             }
         }
     }
diff --git a/src/video_core/engines/maxwell_dma.cpp b/src/video_core/engines/maxwell_dma.cpp
index cd8e24b0b3..da8eab7ee0 100644
--- a/src/video_core/engines/maxwell_dma.cpp
+++ b/src/video_core/engines/maxwell_dma.cpp
@@ -5,6 +5,7 @@
 #include "common/assert.h"
 #include "common/logging/log.h"
 #include "common/microprofile.h"
+#include "common/polyfill_ranges.h"
 #include "common/settings.h"
 #include "core/core.h"
 #include "core/memory.h"