From 23b903971634376adc6622c1b5ba8c16c4d35a84 Mon Sep 17 00:00:00 2001 From: Rangi Date: Wed, 8 Oct 2025 14:52:34 -0400 Subject: [PATCH] Give clearer names to `template` parameters --- .clang-format | 1 + include/backtrace.hpp | 6 +-- include/diagnostics.hpp | 48 ++++++++++++------------ include/helpers.hpp | 12 +++--- include/itertools.hpp | 80 ++++++++++++++++++++++------------------ include/link/section.hpp | 12 +++--- include/util.hpp | 5 ++- src/asm/charmap.cpp | 4 +- src/asm/lexer.cpp | 8 ++-- src/asm/parser.y | 6 ++- src/gfx/pal_packing.cpp | 24 ++++++------ src/gfx/pal_spec.cpp | 6 +-- src/link/output.cpp | 8 ++-- 13 files changed, 118 insertions(+), 102 deletions(-) diff --git a/.clang-format b/.clang-format index 30cf4102..db481b97 100644 --- a/.clang-format +++ b/.clang-format @@ -71,6 +71,7 @@ Language: Cpp MaxEmptyLinesToKeep: 1 NamespaceIndentation: None PPIndentWidth: -1 +PenaltyBreakScopeResolution: 1000 PointerAlignment: Right QualifierAlignment: Right ReflowComments: true diff --git a/include/backtrace.hpp b/include/backtrace.hpp index e0d8947c..ba945a3f 100644 --- a/include/backtrace.hpp +++ b/include/backtrace.hpp @@ -25,15 +25,15 @@ extern Tracing tracing; bool trace_ParseTraceDepth(char const *arg); -template -void trace_PrintBacktrace(std::vector const &stack, M getName, N getLineNo) { +template +void trace_PrintBacktrace(std::vector const &stack, NameFnT getName, LineNoFnT getLineNo) { size_t n = stack.size(); if (n == 0) { return; // LCOV_EXCL_LINE } auto printLocation = [&](size_t i) { - T const &item = stack[n - i - 1]; + NodeT const &item = stack[n - i - 1]; style_Reset(stderr); if (!tracing.collapse) { fputs(" ", stderr); // Just three spaces; the fourth will be printed next diff --git a/include/diagnostics.hpp b/include/diagnostics.hpp index 796e58b9..b1d67af8 100644 --- a/include/diagnostics.hpp +++ b/include/diagnostics.hpp @@ -30,35 +30,35 @@ struct WarningState { std::pair> getInitialWarningState(std::string &flag); -template +template struct WarningFlag { char const *name; - L level; + LevelEnumT level; }; enum WarningBehavior { DISABLED, ENABLED, ERROR }; -template +template struct ParamWarning { - W firstID; - W lastID; + WarningEnumT firstID; + WarningEnumT lastID; uint8_t defaultLevel; }; -template +template struct DiagnosticsState { - WarningState flagStates[W::NB_WARNINGS]; - WarningState metaStates[W::NB_WARNINGS]; + WarningState flagStates[WarningEnumT::NB_WARNINGS]; + WarningState metaStates[WarningEnumT::NB_WARNINGS]; bool warningsEnabled = true; bool warningsAreErrors = false; }; -template +template struct Diagnostics { - std::vector> metaWarnings; - std::vector> warningFlags; - std::vector> paramWarnings; - DiagnosticsState state; + std::vector> metaWarnings; + std::vector> warningFlags; + std::vector> paramWarnings; + DiagnosticsState state; uint64_t nbErrors; void incrementErrors() { @@ -67,12 +67,12 @@ struct Diagnostics { } } - WarningBehavior getWarningBehavior(W id) const; + WarningBehavior getWarningBehavior(WarningEnumT id) const; void processWarningFlag(char const *flag); }; -template -WarningBehavior Diagnostics::getWarningBehavior(W id) const { +template +WarningBehavior Diagnostics::getWarningBehavior(WarningEnumT id) const { // Check if warnings are globally disabled if (!state.warningsEnabled) { return WarningBehavior::DISABLED; @@ -112,7 +112,7 @@ WarningBehavior Diagnostics::getWarningBehavior(W id) const { } // If no meta flag is specified, check the default state of this warning flag - if (warningFlags[id].level == L::LEVEL_DEFAULT) { // enabled by default + if (warningFlags[id].level == LevelEnumT::LEVEL_DEFAULT) { // enabled by default return enabledBehavior; } @@ -120,8 +120,8 @@ WarningBehavior Diagnostics::getWarningBehavior(W id) const { return WarningBehavior::DISABLED; } -template -void Diagnostics::processWarningFlag(char const *flag) { +template +void Diagnostics::processWarningFlag(char const *flag) { std::string rootFlag = flag; // Check for `-Werror` or `-Wno-error` to return early @@ -140,8 +140,8 @@ void Diagnostics::processWarningFlag(char const *flag) { // Try to match the flag against a parametric warning // If there was an equals sign, it will have set `param`; if not, `param` will be 0, // which applies to all levels - for (ParamWarning const ¶mWarning : paramWarnings) { - W baseID = paramWarning.firstID; + for (ParamWarning const ¶mWarning : paramWarnings) { + WarningEnumT baseID = paramWarning.firstID; uint8_t maxParam = paramWarning.lastID - baseID + 1; assume(paramWarning.defaultLevel <= maxParam); @@ -183,13 +183,13 @@ void Diagnostics::processWarningFlag(char const *flag) { } // Try to match against a "meta" warning - for (WarningFlag const &metaWarning : metaWarnings) { + for (WarningFlag const &metaWarning : metaWarnings) { if (rootFlag != metaWarning.name) { continue; } // Set each of the warning flags that meets this level - for (W id : EnumSeq(W::NB_WARNINGS)) { + for (WarningEnumT id : EnumSeq(WarningEnumT::NB_WARNINGS)) { if (metaWarning.level >= warningFlags[id].level) { state.metaStates[id].update(flagState); } @@ -198,7 +198,7 @@ void Diagnostics::processWarningFlag(char const *flag) { } // Try to match against a "normal" flag - for (W id : EnumSeq(W::NB_PLAIN_WARNINGS)) { + for (WarningEnumT id : EnumSeq(WarningEnumT::NB_PLAIN_WARNINGS)) { if (rootFlag == warningFlags[id].name) { state.flagStates[id].update(flagState); return; diff --git a/include/helpers.hpp b/include/helpers.hpp index 9718737b..e8ce37bd 100644 --- a/include/helpers.hpp +++ b/include/helpers.hpp @@ -100,16 +100,16 @@ static inline int clz(unsigned int x) { #define RRANGE(s) std::rbegin(s), std::rend(s) // MSVC does not inline `strlen()` or `.length()` of a constant string -template -static constexpr int literal_strlen(char const (&)[N]) { - return N - 1; +template +static constexpr int literal_strlen(char const (&)[SizeOfString]) { + return SizeOfString - 1; // Don't count the ending '\0' } // For ad-hoc RAII in place of a `defer` statement or cross-platform `__attribute__((cleanup))` -template +template struct Defer { - T deferred; - Defer(T func) : deferred(func) {} + DeferredFnT deferred; + Defer(DeferredFnT func) : deferred(func) {} ~Defer() { deferred(); } }; diff --git a/include/itertools.hpp b/include/itertools.hpp index a361b2e0..fb3ef59c 100644 --- a/include/itertools.hpp +++ b/include/itertools.hpp @@ -12,29 +12,31 @@ #include #include -template +// A wrapper around iterables to reverse their iteration order; used in `for`-each loops. +template struct ReversedIterable { - T &_iterable; + IterableT &_iterable; }; -template -auto begin(ReversedIterable r) { +template +auto begin(ReversedIterable r) { return std::rbegin(r._iterable); } -template -auto end(ReversedIterable r) { +template +auto end(ReversedIterable r) { return std::rend(r._iterable); } -template -ReversedIterable reversed(T &&_iterable) { +template +ReversedIterable reversed(IterableT &&_iterable) { return {_iterable}; } -template +// A map from `std::string` keys to `ItemT` items, iterable in the order the items were inserted. +template class InsertionOrderedMap { - std::deque list; + std::deque list; std::unordered_map map; // Indexes into `list` public: @@ -44,25 +46,25 @@ public: bool contains(std::string const &name) const { return map.find(name) != map.end(); } - T &operator[](size_t i) { return list[i]; } + ItemT &operator[](size_t i) { return list[i]; } typename decltype(list)::iterator begin() { return list.begin(); } typename decltype(list)::iterator end() { return list.end(); } typename decltype(list)::const_iterator begin() const { return list.begin(); } typename decltype(list)::const_iterator end() const { return list.end(); } - T &add(std::string const &name) { + ItemT &add(std::string const &name) { map[name] = list.size(); return list.emplace_back(); } - T &add(std::string const &name, T &&value) { + ItemT &add(std::string const &name, ItemT &&value) { map[name] = list.size(); list.emplace_back(std::move(value)); return list.back(); } - T &addAnonymous() { + ItemT &addAnonymous() { // Add the new item to the list, but do not update the map return list.emplace_back(); } @@ -75,43 +77,45 @@ public: } }; -template +// An iterable of `enum` values in the half-open range [start, stop). +template class EnumSeq { - T _start; - T _stop; + EnumT _start; + EnumT _stop; class Iterator { - T _value; + EnumT _value; public: - explicit Iterator(T value) : _value(value) {} + explicit Iterator(EnumT value) : _value(value) {} Iterator &operator++() { - _value = static_cast(_value + 1); + _value = static_cast(_value + 1); return *this; } - T operator*() const { return _value; } + EnumT operator*() const { return _value; } bool operator==(Iterator const &rhs) const { return _value == rhs._value; } }; public: - explicit EnumSeq(T stop) : _start(static_cast(0)), _stop(stop) {} - explicit EnumSeq(T start, T stop) : _start(start), _stop(stop) {} + explicit EnumSeq(EnumT stop) : _start(static_cast(0)), _stop(stop) {} + explicit EnumSeq(EnumT start, EnumT stop) : _start(start), _stop(stop) {} Iterator begin() { return Iterator(_start); } Iterator end() { return Iterator(_stop); } }; +// Only needed inside `ZipContainer` below. // This is not a fully generic implementation; its current use cases only require for-loop behavior. // We also assume that all iterators have the same length. -template +template class ZipIterator { - std::tuple _iters; + std::tuple _iters; public: - explicit ZipIterator(std::tuple &&iters) : _iters(iters) {} + explicit ZipIterator(std::tuple &&iters) : _iters(iters) {} ZipIterator &operator++() { std::apply([](auto &&...it) { (++it, ...); }, _iters); @@ -129,12 +133,14 @@ public: } }; -template +// Only needed inside `zip` below. +template class ZipContainer { - std::tuple _containers; + std::tuple _containers; public: - explicit ZipContainer(Ts &&...containers) : _containers(std::forward(containers)...) {} + explicit ZipContainer(IterableTs &&...containers) + : _containers(std::forward(containers)...) {} auto begin() { return ZipIterator(std::apply( @@ -157,15 +163,19 @@ public: } }; +// Only needed inside `zip` below. // Take ownership of objects and rvalue refs passed to us, but not lvalue refs -template -using Holder = std:: - conditional_t, T, std::remove_cv_t>>; +template +using ZipHolder = std::conditional_t< + std::is_lvalue_reference_v, + IterableT, + std::remove_cv_t>>; +// Iterates over N containers at once, yielding tuples of N items at a time. // Does the same number of iterations as the first container's iterator! -template -static constexpr auto zip(Ts &&...cs) { - return ZipContainer...>(std::forward(cs)...); +template +static constexpr auto zip(IterableTs &&...containers) { + return ZipContainer...>(std::forward(containers)...); } #endif // RGBDS_ITERTOOLS_HPP diff --git a/include/link/section.hpp b/include/link/section.hpp index fab4a8dd..0fb3ba36 100644 --- a/include/link/section.hpp +++ b/include/link/section.hpp @@ -52,28 +52,28 @@ struct Section { private: // Template class for both const and non-const iterators over the "pieces" of this section - template + template class PiecesIterable { - T *_firstPiece; + SectionT *_firstPiece; class Iterator { - T *_piece; + SectionT *_piece; public: - explicit Iterator(T *piece) : _piece(piece) {} + explicit Iterator(SectionT *piece) : _piece(piece) {} Iterator &operator++() { _piece = _piece->nextPiece.get(); return *this; } - T &operator*() const { return *_piece; } + SectionT &operator*() const { return *_piece; } bool operator==(Iterator const &rhs) const { return _piece == rhs._piece; } }; public: - explicit PiecesIterable(T *firstPiece) : _firstPiece(firstPiece) {} + explicit PiecesIterable(SectionT *firstPiece) : _firstPiece(firstPiece) {} Iterator begin() { return Iterator(_firstPiece); } Iterator end() { return Iterator(nullptr); } diff --git a/include/util.hpp b/include/util.hpp index 5abd7d57..90c1235b 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -64,7 +64,8 @@ struct Uppercase { } }; -template -using UpperMap = std::unordered_map; +// An unordered map from case-insensitive `std::string` keys to `ItemT` items +template +using UpperMap = std::unordered_map; #endif // RGBDS_UTIL_HPP diff --git a/src/asm/charmap.cpp b/src/asm/charmap.cpp index cd999cc1..473433f5 100644 --- a/src/asm/charmap.cpp +++ b/src/asm/charmap.cpp @@ -38,8 +38,8 @@ struct Charmap { }; // Traverse the trie depth-first to derive the character mappings in definition order -template -bool forEachChar(Charmap const &charmap, F callback) { +template +bool forEachChar(Charmap const &charmap, CallbackFnT callback) { // clang-format off: nested initializers for (std::stack> prefixes({{0, ""}}); !prefixes.empty();) { // clang-format on diff --git a/src/asm/lexer.cpp b/src/asm/lexer.cpp index 55adfa9a..66e3a396 100644 --- a/src/asm/lexer.cpp +++ b/src/asm/lexer.cpp @@ -816,8 +816,8 @@ static int nextChar() { return peek(); } -template -static int skipChars(P predicate) { +template +static int skipChars(PredicateFnT predicate) { int c = peek(); while (predicate(c)) { c = nextChar(); @@ -2281,8 +2281,8 @@ yy::parser::symbol_type yylex() { } } -template -static Capture makeCapture(char const *name, F callback) { +template +static Capture makeCapture(char const *name, CallbackFnT callback) { // Due to parser internals, it reads the EOL after the expression before calling this. // Thus, we don't need to keep one in the buffer afterwards. // The following assumption checks that. diff --git a/src/asm/parser.y b/src/asm/parser.y index 1e3f509c..cdc5ee98 100644 --- a/src/asm/parser.y +++ b/src/asm/parser.y @@ -57,8 +57,10 @@ yy::parser::symbol_type yylex(); // Provided by lexer.cpp - template - static auto handleSymbolByType(std::string const &symName, N numCallback, S strCallback) { + template + static auto handleSymbolByType( + std::string const &symName, NumCallbackFnT numCallback, StrCallbackFnT strCallback + ) { if (Symbol *sym = sym_FindScopedSymbol(symName); sym && sym->type == SYM_EQUS) { return strCallback(*sym->getEqus()); } else { diff --git a/src/gfx/pal_packing.cpp b/src/gfx/pal_packing.cpp index b9749400..e20cdd53 100644 --- a/src/gfx/pal_packing.cpp +++ b/src/gfx/pal_packing.cpp @@ -70,7 +70,7 @@ public: private: // Template class for both const and non-const iterators over the non-empty `_assigned` slots - template typename Constness> + template typename Constness> class AssignedSetsIter { public: friend class AssignedSets; @@ -84,7 +84,7 @@ private: private: Constness *_array = nullptr; - I _iter{}; + IteratorT _iter{}; AssignedSetsIter(decltype(_array) array, decltype(_iter) &&iter) : _array(array), _iter(iter) {} @@ -164,11 +164,11 @@ public: size_t nbColorSets() const { return std::distance(RANGE(*this)); } private: - template + template static void addUniqueColors( std::unordered_set &colors, - I iter, - I const &end, + IteratorT iter, + IteratorT const &end, std::vector const &colorSets ) { for (; iter != end; ++iter) { @@ -240,18 +240,20 @@ public: } // Computes the "relative size" of a set of color sets on this palette - template - size_t combinedVolume(I &&begin, I const &end, std::vector const &colorSets) const { + template + size_t combinedVolume( + IteratorT &&begin, IteratorT const &end, std::vector const &colorSets + ) const { std::unordered_set &colors = uniqueColors(); - addUniqueColors(colors, std::forward(begin), end, colorSets); + addUniqueColors(colors, std::forward(begin), end, colorSets); return colors.size(); } // Computes the "relative size" of a set of colors on this palette - template - size_t combinedVolume(I &&begin, I &&end) const { + template + size_t combinedVolume(IteratorT &&begin, IteratorT &&end) const { std::unordered_set &colors = uniqueColors(); - colors.insert(std::forward(begin), std::forward(end)); + colors.insert(std::forward(begin), std::forward(end)); return colors.size(); } }; diff --git a/src/gfx/pal_spec.cpp b/src/gfx/pal_spec.cpp index 21b1246c..15d8c908 100644 --- a/src/gfx/pal_spec.cpp +++ b/src/gfx/pal_spec.cpp @@ -210,15 +210,15 @@ static void warnExtraColors( } // Parses the initial part of a string_view, advancing the "read index" as it does -template // Should be uint*_t -static std::optional parseDec(std::string const &str, size_t &n) { +template // Should be uint*_t +static std::optional parseDec(std::string const &str, size_t &n) { uintmax_t value = 0; auto result = std::from_chars(str.data() + n, str.data() + str.length(), value); if (static_cast(result.ec)) { return std::nullopt; } n = result.ptr - str.data(); - return std::optional{value}; + return std::optional{value}; } static std::optional parseColor(std::string const &str, size_t &n, uint16_t i) { diff --git a/src/link/output.cpp b/src/link/output.cpp index 7c7dfbf6..9511372f 100644 --- a/src/link/output.cpp +++ b/src/link/output.cpp @@ -314,8 +314,8 @@ static bool compareSymbols(SortedSymbol const &sym1, SortedSymbol const &sym2) { < std::tie(sym2.addr, sym2_local, sym2.parentAddr, sym2_name); } -template -static void forEachSortedSection(SortedSections const &bankSections, F callback) { +template +static void forEachSortedSection(SortedSections const &bankSections, CallbackFnT callback) { for (Section const *sect : bankSections.zeroLenSections) { for (Section const &piece : sect->pieces()) { callback(piece); @@ -415,8 +415,8 @@ static void writeSectionName(std::string const &name, FILE *file) { } } -template -uint16_t forEachSection(SortedSections const §List, F callback) { +template +uint16_t forEachSection(SortedSections const §List, CallbackFnT callback) { uint16_t used = 0; auto section = sectList.sections.begin(); auto zeroLenSection = sectList.zeroLenSections.begin();