From 2b747df80a4580df265b11ee52e5777c824c71e2 Mon Sep 17 00:00:00 2001 From: Peter Van der Beken Date: Sat, 2 Mar 2024 07:50:19 +0000 Subject: [PATCH] Bug 1640839 - Add some helpers for enums to calculate the size and use it in EnumeratedArray. r=glandium,jgilbert,media-playback-reviewers,padenot Differential Revision: https://phabricator.services.mozilla.com/D201334 --- dom/animation/EffectCompositor.h | 2 +- dom/base/FlushType.h | 2 +- dom/canvas/CanvasRenderingContext2D.h | 8 +- dom/canvas/WebGLContext.h | 2 +- dom/media/ipc/RemoteDecoderManagerChild.cpp | 6 +- dom/media/mediasession/MediaSession.h | 2 +- dom/quota/QuotaManager.h | 3 +- dom/system/IOUtils.h | 4 +- dom/system/PathUtils.h | 3 +- dom/xslt/xslt/txEXSLTFunctions.cpp | 2 +- gfx/layers/ImageContainer.h | 2 +- gfx/layers/ipc/VideoBridgeParent.cpp | 2 +- gfx/thebes/gfxPlatformFontList.h | 5 +- ipc/glue/UtilityAudioDecoderChild.cpp | 2 +- ipc/glue/UtilityProcessManager.h | 3 +- js/public/RootingAPI.h | 6 +- js/src/gc/AllocKind.h | 5 +- js/src/gc/Nursery.h | 4 +- js/src/gc/Statistics.cpp | 4 +- js/src/gc/Statistics.h | 2 +- js/src/jit/CodeGenerator.cpp | 4 +- js/src/jit/ExecutableAllocator.h | 3 +- js/src/jit/IonOptimizationLevels.h | 2 +- js/src/jit/JitRuntime.h | 11 +-- js/src/jit/JitZone.h | 3 +- js/src/jit/WarpCacheIRTranspiler.cpp | 3 +- js/src/util/StructuredSpewer.h | 4 +- js/src/vm/GlobalObject.h | 10 +-- js/src/vm/HelperThreadState.h | 3 +- js/src/vm/Runtime.h | 2 +- js/src/wasm/WasmBuiltinModule.h | 2 +- js/src/wasm/WasmBuiltins.cpp | 2 +- js/src/wasm/WasmCode.h | 4 +- js/src/wasm/WasmCodegenTypes.h | 2 +- js/src/wasm/WasmGenerator.cpp | 2 +- layout/base/LayoutTelemetryTools.h | 4 +- layout/base/nsPresContext.h | 3 +- layout/generic/nsGridContainerFrame.h | 6 +- layout/generic/nsTextPaintStyle.cpp | 2 +- layout/generic/nsTextPaintStyle.h | 2 +- layout/style/ServoStyleSet.h | 2 +- mfbt/ArrayUtils.h | 6 +- mfbt/EnumTypeTraits.h | 82 +++++++++++++------ mfbt/EnumeratedArray.h | 27 +++--- mfbt/tests/TestEnumeratedArray.cpp | 12 ++- .../formautofill/FormAutofillNative.cpp | 18 ++-- toolkit/crashreporter/nsExceptionHandler.h | 3 +- .../exthandler/unix/nsOSHelperAppService.cpp | 2 +- widget/nsPrinterBase.cpp | 2 +- widget/nsPrinterBase.h | 2 +- widget/nsXPLookAndFeel.cpp | 2 +- widget/windows/nsWindow.h | 2 +- xpcom/base/EnumeratedArrayCycleCollection.h | 12 +-- 53 files changed, 189 insertions(+), 126 deletions(-) diff --git a/dom/animation/EffectCompositor.h b/dom/animation/EffectCompositor.h index 3b98f8be0ec1..78ec63f5952e 100644 --- a/dom/animation/EffectCompositor.h +++ b/dom/animation/EffectCompositor.h @@ -245,7 +245,7 @@ class EffectCompositor { // indicate that the style rule on the element is out of date but without // posting a restyle to update it. EnumeratedArray, - CascadeLevel(kCascadeLevelCount)> + kCascadeLevelCount> mElementsToRestyle; bool mIsInPreTraverse = false; diff --git a/dom/base/FlushType.h b/dom/base/FlushType.h index 236c3b3914e2..c90cbbd16fa3 100644 --- a/dom/base/FlushType.h +++ b/dom/base/FlushType.h @@ -40,7 +40,7 @@ enum class FlushType : uint8_t { // Flush type strings that will be displayed in the profiler // clang-format off -const EnumeratedArray +const EnumeratedArray kFlushTypeNames = { "", "Event", diff --git a/dom/canvas/CanvasRenderingContext2D.h b/dom/canvas/CanvasRenderingContext2D.h index 8f00501d8ffb..065cd4fa66b7 100644 --- a/dom/canvas/CanvasRenderingContext2D.h +++ b/dom/canvas/CanvasRenderingContext2D.h @@ -1008,9 +1008,11 @@ class CanvasRenderingContext2D : public nsICanvasRenderingContextInternal, RefPtr fontLanguage; nsFont fontFont; - EnumeratedArray, Style::MAX> gradientStyles; - EnumeratedArray, Style::MAX> patternStyles; - EnumeratedArray colorStyles; + EnumeratedArray, size_t(Style::MAX)> + gradientStyles; + EnumeratedArray, size_t(Style::MAX)> + patternStyles; + EnumeratedArray colorStyles; nsCString font; CanvasTextAlign textAlign = CanvasTextAlign::Start; diff --git a/dom/canvas/WebGLContext.h b/dom/canvas/WebGLContext.h index 02e229bded9b..a4757232490e 100644 --- a/dom/canvas/WebGLContext.h +++ b/dom/canvas/WebGLContext.h @@ -969,7 +969,7 @@ class WebGLContext : public VRefCounted, public SupportsWeakPtr { // WebGL extensions (implemented in WebGLContextExtensions.cpp) EnumeratedArray, - WebGLExtensionID::Max> + size_t(WebGLExtensionID::Max)> mExtensions; public: diff --git a/dom/media/ipc/RemoteDecoderManagerChild.cpp b/dom/media/ipc/RemoteDecoderManagerChild.cpp index ee660f6e9b38..23ee7c0fb7a6 100644 --- a/dom/media/ipc/RemoteDecoderManagerChild.cpp +++ b/dom/media/ipc/RemoteDecoderManagerChild.cpp @@ -51,7 +51,7 @@ using namespace gfx; // should be launched serially. Protects sLaunchPromise StaticMutex sLaunchMutex; static EnumeratedArray, - RemoteDecodeIn::SENTINEL> + size_t(RemoteDecodeIn::SENTINEL)> sLaunchPromises MOZ_GUARDED_BY(sLaunchMutex); // Only modified on the main-thread, read on any thread. While it could be read @@ -62,7 +62,7 @@ static StaticDataMutex> // Only accessed from sRemoteDecoderManagerChildThread static EnumeratedArray, - RemoteDecodeIn::SENTINEL> + size_t(RemoteDecodeIn::SENTINEL)> sRemoteDecoderManagerChildForProcesses; static StaticAutoPtr>> sRecreateTasks; @@ -71,7 +71,7 @@ static StaticAutoPtr>> sRecreateTasks; // processes. StaticMutex sProcessSupportedMutex; static EnumeratedArray, - RemoteDecodeIn::SENTINEL> + size_t(RemoteDecodeIn::SENTINEL)> sProcessSupported MOZ_GUARDED_BY(sProcessSupportedMutex); class ShutdownObserver final : public nsIObserver { diff --git a/dom/media/mediasession/MediaSession.h b/dom/media/mediasession/MediaSession.h index 19c1ec232354..c6fed8f55f46 100644 --- a/dom/media/mediasession/MediaSession.h +++ b/dom/media/mediasession/MediaSession.h @@ -121,7 +121,7 @@ class MediaSession final : public nsIDocumentActivity, public nsWrapperCache { RefPtr mMediaMetadata; EnumeratedArray, - MediaSessionAction::EndGuard_> + size_t(MediaSessionAction::EndGuard_)> mActionHandlers; // This is used as is a hint for the user agent to determine whether the diff --git a/dom/quota/QuotaManager.h b/dom/quota/QuotaManager.h index f5cb70fff89e..354977166a07 100644 --- a/dom/quota/QuotaManager.h +++ b/dom/quota/QuotaManager.h @@ -727,7 +727,8 @@ class QuotaManager final : public BackgroundThreadObject { nsCOMPtr mStorageConnection; - EnumeratedArray mShutdownSteps; + EnumeratedArray + mShutdownSteps; LazyInitializedOnce mShutdownStartedAt; // Accesses to mQuotaManagerShutdownSteps must be protected by mQuotaMutex. diff --git a/dom/system/IOUtils.h b/dom/system/IOUtils.h index fccf2da04310..6acfcbfb2435 100644 --- a/dom/system/IOUtils.h +++ b/dom/system/IOUtils.h @@ -69,8 +69,8 @@ class IOUtils final { }; template - using PhaseArray = - EnumeratedArray; + using PhaseArray = EnumeratedArray; static already_AddRefed Read(GlobalObject& aGlobal, const nsAString& aPath, diff --git a/dom/system/PathUtils.h b/dom/system/PathUtils.h index fba2cdd77953..1870d92f2352 100644 --- a/dom/system/PathUtils.h +++ b/dom/system/PathUtils.h @@ -241,7 +241,8 @@ class PathUtils::DirectoryCache final { void ResolveWithDirectory(Promise* aPromise, const Directory aRequestedDir); template - using DirectoryArray = EnumeratedArray; + using DirectoryArray = + EnumeratedArray; DirectoryArray mDirectories; DirectoryArray> mPromises; diff --git a/dom/xslt/xslt/txEXSLTFunctions.cpp b/dom/xslt/xslt/txEXSLTFunctions.cpp index bff111771a0b..42ad30e74bfc 100644 --- a/dom/xslt/xslt/txEXSLTFunctions.cpp +++ b/dom/xslt/xslt/txEXSLTFunctions.cpp @@ -190,7 +190,7 @@ struct txEXSLTFunctionDescriptor { }; static EnumeratedArray + size_t(txEXSLTType::_LIMIT)> descriptTable; class txEXSLTFunctionCall : public FunctionCall { diff --git a/gfx/layers/ImageContainer.h b/gfx/layers/ImageContainer.h index 6ad5d1b8f1a9..f86266c5575b 100644 --- a/gfx/layers/ImageContainer.h +++ b/gfx/layers/ImageContainer.h @@ -192,7 +192,7 @@ class Image { mozilla::EnumeratedArray, - mozilla::layers::LayersBackend::LAYERS_LAST> + size_t(mozilla::layers::LayersBackend::LAYERS_LAST)> mBackendData; void* mImplData; diff --git a/gfx/layers/ipc/VideoBridgeParent.cpp b/gfx/layers/ipc/VideoBridgeParent.cpp index 7a6f2095cc70..50f0549e505e 100644 --- a/gfx/layers/ipc/VideoBridgeParent.cpp +++ b/gfx/layers/ipc/VideoBridgeParent.cpp @@ -18,7 +18,7 @@ using namespace mozilla::ipc; using namespace mozilla::gfx; using VideoBridgeTable = EnumeratedArray; + size_t(VideoBridgeSource::_Count)>; static StaticDataMutex sVideoBridgeFromProcess( "VideoBridges"); diff --git a/gfx/thebes/gfxPlatformFontList.h b/gfx/thebes/gfxPlatformFontList.h index 2bdafd3ca751..b19b5ee1fc95 100644 --- a/gfx/thebes/gfxPlatformFontList.h +++ b/gfx/thebes/gfxPlatformFontList.h @@ -1011,12 +1011,13 @@ class gfxPlatformFontList : public gfxFontInfoLoader { // When system-wide font lookup fails for a character, cache it to skip future // searches. This is an array of bitsets, one for each FontVisibility level. mozilla::EnumeratedArray + size_t(FontVisibility::Count)> mCodepointsWithNoFonts MOZ_GUARDED_BY(mLock); // the family to use for U+FFFD fallback, to avoid expensive search every time // on pages with lots of problems - mozilla::EnumeratedArray + mozilla::EnumeratedArray mReplacementCharFallbackFamily MOZ_GUARDED_BY(mLock); // Sorted array of lowercased family names; use ContainsSorted to test diff --git a/ipc/glue/UtilityAudioDecoderChild.cpp b/ipc/glue/UtilityAudioDecoderChild.cpp index 1a9bd7e50684..34fc63ea82f0 100644 --- a/ipc/glue/UtilityAudioDecoderChild.cpp +++ b/ipc/glue/UtilityAudioDecoderChild.cpp @@ -43,7 +43,7 @@ NS_IMETHODIMP UtilityAudioDecoderChildShutdownObserver::Observe( NS_IMPL_ISUPPORTS(UtilityAudioDecoderChildShutdownObserver, nsIObserver); static EnumeratedArray, - SandboxingKind::COUNT> + size_t(SandboxingKind::COUNT)> sAudioDecoderChilds; UtilityAudioDecoderChild::UtilityAudioDecoderChild(SandboxingKind aKind) diff --git a/ipc/glue/UtilityProcessManager.h b/ipc/glue/UtilityProcessManager.h index 1faf555fc3a3..64b0f6504fbf 100644 --- a/ipc/glue/UtilityProcessManager.h +++ b/ipc/glue/UtilityProcessManager.h @@ -228,7 +228,8 @@ class UtilityProcessManager final : public UtilityProcessHost::Listener { ~ProcessFields() = default; }; - EnumeratedArray, SandboxingKind::COUNT> + EnumeratedArray, + size_t(SandboxingKind::COUNT)> mProcesses; RefPtr GetProcess(SandboxingKind); diff --git a/js/public/RootingAPI.h b/js/public/RootingAPI.h index a3aa7a099219..e35a2c5bc8f8 100644 --- a/js/public/RootingAPI.h +++ b/js/public/RootingAPI.h @@ -981,12 +981,12 @@ enum class AutoGCRooterKind : uint8_t { Limit }; -using RootedListHeads = - mozilla::EnumeratedArray; +using RootedListHeads = mozilla::EnumeratedArray; using AutoRooterListHeads = mozilla::EnumeratedArray; + size_t(AutoGCRooterKind::Limit)>; // Superclass of JSContext which can be used for rooting data in use by the // current thread but that does not provide all the functions of a JSContext. diff --git a/js/src/gc/AllocKind.h b/js/src/gc/AllocKind.h index d47b65368c68..f73352e557e3 100644 --- a/js/src/gc/AllocKind.h +++ b/js/src/gc/AllocKind.h @@ -197,13 +197,14 @@ constexpr auto SomeAllocKinds(AllocKind first = AllocKind::FIRST, // with each index corresponding to a particular alloc kind. template using AllAllocKindArray = - mozilla::EnumeratedArray; + mozilla::EnumeratedArray; // ObjectAllocKindArray gives an enumerated array of ValueTypes, // with each index corresponding to a particular object alloc kind. template using ObjectAllocKindArray = - mozilla::EnumeratedArray; + mozilla::EnumeratedArray; /* * Map from C++ type to alloc kind for non-object types. JSObject does not have diff --git a/js/src/gc/Nursery.h b/js/src/gc/Nursery.h index 9d9798d9736e..0d7b607ff885 100644 --- a/js/src/gc/Nursery.h +++ b/js/src/gc/Nursery.h @@ -382,10 +382,10 @@ class Nursery { }; using ProfileTimes = mozilla::EnumeratedArray; + size_t(ProfileKey::KeyCount)>; using ProfileDurations = mozilla::EnumeratedArray; + size_t(ProfileKey::KeyCount)>; // Calculate the promotion rate of the most recent minor GC. // The valid_for_tenuring parameter is used to return whether this diff --git a/js/src/gc/Statistics.cpp b/js/src/gc/Statistics.cpp index ee6e9c26582b..1cdd1b239197 100644 --- a/js/src/gc/Statistics.cpp +++ b/js/src/gc/Statistics.cpp @@ -154,11 +154,11 @@ struct PhaseInfo { }; // A table of PhaseInfo indexed by Phase. -using PhaseTable = EnumeratedArray; +using PhaseTable = EnumeratedArray; // A table of PhaseKindInfo indexed by PhaseKind. using PhaseKindTable = - EnumeratedArray; + EnumeratedArray; #include "gc/StatsPhasesGenerated.inc" diff --git a/js/src/gc/Statistics.h b/js/src/gc/Statistics.h index d896bdccf078..bc6d7bf5ddcc 100644 --- a/js/src/gc/Statistics.h +++ b/js/src/gc/Statistics.h @@ -138,7 +138,7 @@ struct Statistics { template using EnumeratedArray = - mozilla::EnumeratedArray; + mozilla::EnumeratedArray; using TimeDuration = mozilla::TimeDuration; using TimeStamp = mozilla::TimeStamp; diff --git a/js/src/jit/CodeGenerator.cpp b/js/src/jit/CodeGenerator.cpp index e3b6a91b1341..f492835c83fb 100644 --- a/js/src/jit/CodeGenerator.cpp +++ b/js/src/jit/CodeGenerator.cpp @@ -2167,8 +2167,8 @@ class CreateDependentString { NotInlineString, Count }; - mozilla::EnumeratedArray fallbacks_, - joins_; + mozilla::EnumeratedArray + fallbacks_, joins_; public: CreateDependentString(CharEncoding encoding, Register string, Register temp1, diff --git a/js/src/jit/ExecutableAllocator.h b/js/src/jit/ExecutableAllocator.h index ea638d858ae7..02c8727e85b4 100644 --- a/js/src/jit/ExecutableAllocator.h +++ b/js/src/jit/ExecutableAllocator.h @@ -72,7 +72,8 @@ class ExecutablePool { bool m_mark : 1; // Number of bytes currently allocated for each CodeKind. - mozilla::EnumeratedArray m_codeBytes; + mozilla::EnumeratedArray + m_codeBytes; public: void release(bool willDestroy = false); diff --git a/js/src/jit/IonOptimizationLevels.h b/js/src/jit/IonOptimizationLevels.h index 25612869b8ac..92e458613154 100644 --- a/js/src/jit/IonOptimizationLevels.h +++ b/js/src/jit/IonOptimizationLevels.h @@ -182,7 +182,7 @@ class OptimizationInfo { class OptimizationLevelInfo { private: mozilla::EnumeratedArray + size_t(OptimizationLevel::Count)> infos_; public: diff --git a/js/src/jit/JitRuntime.h b/js/src/jit/JitRuntime.h index 0b33f1240d91..7d038ed0e2e8 100644 --- a/js/src/jit/JitRuntime.h +++ b/js/src/jit/JitRuntime.h @@ -74,15 +74,16 @@ enum class BailoutReturnKind { // is stored in JitRuntime and generated when creating the JitRuntime. class BaselineICFallbackCode { JitCode* code_ = nullptr; - using OffsetArray = mozilla::EnumeratedArray; + using OffsetArray = + mozilla::EnumeratedArray; OffsetArray offsets_ = {}; // Keep track of offset into various baseline stubs' code at return // point from called script. using BailoutReturnArray = mozilla::EnumeratedArray; + size_t(BailoutReturnKind::Count)>; BailoutReturnArray bailoutReturnOffsets_ = {}; public: @@ -175,12 +176,12 @@ class JitRuntime { // Thunk to do a generic call from Ion. mozilla::EnumeratedArray, - IonGenericCallKind::Count> + size_t(IonGenericCallKind::Count)> ionGenericCallStubOffset_; // Thunk used by the debugger for breakpoint and step mode. mozilla::EnumeratedArray, - DebugTrapHandlerKind::Count> + size_t(DebugTrapHandlerKind::Count)> debugTrapHandlers_; // BaselineInterpreter state. diff --git a/js/src/jit/JitZone.h b/js/src/jit/JitZone.h index 30981c8df4a8..d4f2350b8da8 100644 --- a/js/src/jit/JitZone.h +++ b/js/src/jit/JitZone.h @@ -141,7 +141,8 @@ class JitZone { Count }; - mozilla::EnumeratedArray, StubIndex::Count> + mozilla::EnumeratedArray, + size_t(StubIndex::Count)> stubs_; mozilla::Maybe currentCompilationId_; diff --git a/js/src/jit/WarpCacheIRTranspiler.cpp b/js/src/jit/WarpCacheIRTranspiler.cpp index 62c30e2f049b..4fcfdf15ef0c 100644 --- a/js/src/jit/WarpCacheIRTranspiler.cpp +++ b/js/src/jit/WarpCacheIRTranspiler.cpp @@ -49,7 +49,8 @@ class MOZ_RAII WarpCacheIRTranspiler : public WarpBuilderShared { // Array mapping call arguments to OperandId. using ArgumentKindArray = - mozilla::EnumeratedArray; + mozilla::EnumeratedArray; ArgumentKindArray argumentOperandIds_; void setArgumentId(ArgumentKind kind, OperandId id) { diff --git a/js/src/util/StructuredSpewer.h b/js/src/util/StructuredSpewer.h index 8ccd29db34dd..f8b6321d9813 100644 --- a/js/src/util/StructuredSpewer.h +++ b/js/src/util/StructuredSpewer.h @@ -187,8 +187,8 @@ class StructuredSpewer { // Globally selected channel. StructuredSpewFilter selectedChannel_; - using NameArray = - mozilla::EnumeratedArray; + using NameArray = mozilla::EnumeratedArray; // Channel Names static NameArray const names_; diff --git a/js/src/vm/GlobalObject.h b/js/src/vm/GlobalObject.h index 0b08babf25fb..a2963363851e 100644 --- a/js/src/vm/GlobalObject.h +++ b/js/src/vm/GlobalObject.h @@ -129,8 +129,8 @@ class GlobalObjectData { HeapPtr constructor; HeapPtr prototype; }; - using CtorArray = - mozilla::EnumeratedArray; + using CtorArray = mozilla::EnumeratedArray; CtorArray builtinConstructors; // Built-in prototypes for this global. Note that this is different from the @@ -154,8 +154,8 @@ class GlobalObjectData { Limit }; - using ProtoArray = - mozilla::EnumeratedArray, ProtoKind::Limit>; + using ProtoArray = mozilla::EnumeratedArray, + size_t(ProtoKind::Limit)>; ProtoArray builtinProtos; HeapPtr emptyGlobalScope; @@ -197,7 +197,7 @@ class GlobalObjectData { // AllocKind. using PlainObjectShapeArray = mozilla::EnumeratedArray, - PlainObjectSlotsKind::Limit>; + size_t(PlainObjectSlotsKind::Limit)>; PlainObjectShapeArray plainObjectShapesWithDefaultProto; // Shape for JSFunction with %Function.prototype% as proto, for both diff --git a/js/src/vm/HelperThreadState.h b/js/src/vm/HelperThreadState.h index 56107f01560c..68312189672f 100644 --- a/js/src/vm/HelperThreadState.h +++ b/js/src/vm/HelperThreadState.h @@ -115,7 +115,8 @@ class GlobalHelperThreadState { PromiseHelperTaskVector; // Count of running task by each threadType. - mozilla::EnumeratedArray + mozilla::EnumeratedArray runningTaskCount; size_t totalCountRunningTasks; diff --git a/js/src/vm/Runtime.h b/js/src/vm/Runtime.h index e12b3bac11fd..57d4fb141189 100644 --- a/js/src/vm/Runtime.h +++ b/js/src/vm/Runtime.h @@ -528,7 +528,7 @@ struct JSRuntime { // Heap GC roots for PersistentRooted pointers. js::MainThreadData, - JS::RootKind::Limit>> + size_t(JS::RootKind::Limit)>> heapRoots; void tracePersistentRoots(JSTracer* trc); diff --git a/js/src/wasm/WasmBuiltinModule.h b/js/src/wasm/WasmBuiltinModule.h index 5b0b254162fb..8646e789e628 100644 --- a/js/src/wasm/WasmBuiltinModule.h +++ b/js/src/wasm/WasmBuiltinModule.h @@ -102,7 +102,7 @@ class BuiltinModuleFunc { class BuiltinModuleFuncs { using Storage = mozilla::EnumeratedArray; + size_t(BuiltinModuleFuncId::Limit)>; Storage funcs_; static BuiltinModuleFuncs* singleton_; diff --git a/js/src/wasm/WasmBuiltins.cpp b/js/src/wasm/WasmBuiltins.cpp index 1795b3c595be..7b03494bcd00 100644 --- a/js/src/wasm/WasmBuiltins.cpp +++ b/js/src/wasm/WasmBuiltins.cpp @@ -1839,7 +1839,7 @@ using TypedNativeToCodeRangeMap = HashMap; using SymbolicAddressToCodeRangeArray = - EnumeratedArray; + EnumeratedArray; struct BuiltinThunks { uint8_t* codeBase; diff --git a/js/src/wasm/WasmCode.h b/js/src/wasm/WasmCode.h index cacaaf3b334d..e03a2f596ebc 100644 --- a/js/src/wasm/WasmCode.h +++ b/js/src/wasm/WasmCode.h @@ -117,8 +117,8 @@ struct LinkData : LinkDataCacheablePod { }; using InternalLinkVector = Vector; - struct SymbolicLinkArray - : EnumeratedArray { + struct SymbolicLinkArray : EnumeratedArray { size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; }; diff --git a/js/src/wasm/WasmCodegenTypes.h b/js/src/wasm/WasmCodegenTypes.h index 4a72433bbce7..590572ae8a89 100644 --- a/js/src/wasm/WasmCodegenTypes.h +++ b/js/src/wasm/WasmCodegenTypes.h @@ -273,7 +273,7 @@ WASM_DECLARE_CACHEABLE_POD(TrapSite); WASM_DECLARE_POD_VECTOR(TrapSite, TrapSiteVector) struct TrapSiteVectorArray - : EnumeratedArray { + : EnumeratedArray { bool empty() const; void clear(); void swap(TrapSiteVectorArray& rhs); diff --git a/js/src/wasm/WasmGenerator.cpp b/js/src/wasm/WasmGenerator.cpp index b36bb6caba23..338812e1d6be 100644 --- a/js/src/wasm/WasmGenerator.cpp +++ b/js/src/wasm/WasmGenerator.cpp @@ -440,7 +440,7 @@ static bool InRange(uint32_t caller, uint32_t callee) { using OffsetMap = HashMap, SystemAllocPolicy>; using TrapMaybeOffsetArray = - EnumeratedArray, Trap::Limit>; + EnumeratedArray, size_t(Trap::Limit)>; bool ModuleGenerator::linkCallSites() { AutoCreatedBy acb(masm_, "linkCallSites"); diff --git a/layout/base/LayoutTelemetryTools.h b/layout/base/LayoutTelemetryTools.h index 3dbdc248f0e5..43eee799239f 100644 --- a/layout/base/LayoutTelemetryTools.h +++ b/layout/base/LayoutTelemetryTools.h @@ -38,9 +38,9 @@ enum class LayoutSubsystem : uint8_t { }; using LayoutSubsystemDurations = - EnumeratedArray; + EnumeratedArray; using LayoutFlushCount = - EnumeratedArray; + EnumeratedArray; struct Data { Data(); diff --git a/layout/base/nsPresContext.h b/layout/base/nsPresContext.h index 0d2dbba3d0f1..563dac0eaea5 100644 --- a/layout/base/nsPresContext.h +++ b/layout/base/nsPresContext.h @@ -1397,7 +1397,8 @@ class nsPresContext : public nsISupports, public mozilla::SupportsWeakPtr { #ifdef DEBUG private: friend struct nsAutoLayoutPhase; - mozilla::EnumeratedArray + mozilla::EnumeratedArray mLayoutPhaseCount; public: diff --git a/layout/generic/nsGridContainerFrame.h b/layout/generic/nsGridContainerFrame.h index cead30441a9a..cf3a3b57762d 100644 --- a/layout/generic/nsGridContainerFrame.h +++ b/layout/generic/nsGridContainerFrame.h @@ -106,12 +106,10 @@ class nsGridContainerFrame final : public nsContainerFrame, using NamedArea = mozilla::StyleNamedArea; template - using PerBaseline = mozilla::EnumeratedArray; + using PerBaseline = mozilla::EnumeratedArray; template - using PerLogicalAxis = - mozilla::EnumeratedArray; + using PerLogicalAxis = mozilla::EnumeratedArray; // nsIFrame overrides void Reflow(nsPresContext* aPresContext, ReflowOutput& aDesiredSize, diff --git a/layout/generic/nsTextPaintStyle.cpp b/layout/generic/nsTextPaintStyle.cpp index 448aa64fba5c..e97a059b0796 100644 --- a/layout/generic/nsTextPaintStyle.cpp +++ b/layout/generic/nsTextPaintStyle.cpp @@ -418,7 +418,7 @@ struct StyleIDs { LookAndFeel::FloatID mLineRelativeSize; }; EnumeratedArray + size_t(nsTextPaintStyle::SelectionStyleIndex::Count)> SelectionStyleIDs = { StyleIDs{LookAndFeel::ColorID::IMERawInputForeground, LookAndFeel::ColorID::IMERawInputBackground, diff --git a/layout/generic/nsTextPaintStyle.h b/layout/generic/nsTextPaintStyle.h index 2920e6b10623..adf28fdad8f7 100644 --- a/layout/generic/nsTextPaintStyle.h +++ b/layout/generic/nsTextPaintStyle.h @@ -146,7 +146,7 @@ class MOZ_STACK_CLASS nsTextPaintStyle { }; mozilla::EnumeratedArray, - SelectionStyleIndex::Count> + size_t(SelectionStyleIndex::Count)> mSelectionStyle; // Color initializations diff --git a/layout/style/ServoStyleSet.h b/layout/style/ServoStyleSet.h index 6bcfd17c9d1a..eee6cba0f780 100644 --- a/layout/style/ServoStyleSet.h +++ b/layout/style/ServoStyleSet.h @@ -687,7 +687,7 @@ class ServoStyleSet { // Stores pointers to our cached ComputedStyles for non-inheriting anonymous // boxes. EnumeratedArray, - nsCSSAnonBoxes::NonInheriting::_Count> + size_t(nsCSSAnonBoxes::NonInheriting::_Count)> mNonInheritingComputedStyles; public: diff --git a/mfbt/ArrayUtils.h b/mfbt/ArrayUtils.h index 11e12cff759e..78eec5dabadf 100644 --- a/mfbt/ArrayUtils.h +++ b/mfbt/ArrayUtils.h @@ -27,7 +27,7 @@ namespace mozilla { template class Array; -template +template class EnumeratedArray; /* @@ -64,9 +64,9 @@ constexpr size_t ArrayLength(const Array& aArr) { return N; } -template +template constexpr size_t ArrayLength(const EnumeratedArray& aArr) { - return size_t(N); + return N; } /* diff --git a/mfbt/EnumTypeTraits.h b/mfbt/EnumTypeTraits.h index 528e1db8a794..09ead3d0e96a 100644 --- a/mfbt/EnumTypeTraits.h +++ b/mfbt/EnumTypeTraits.h @@ -61,29 +61,6 @@ struct EnumTypeFitsWithin "must provide an integral type"); }; -/* - * Provides information about highest enum member value. - * Each specialization of struct MaxEnumValue should define - * "static constexpr unsigned int value". - * - * example: - * - * enum ExampleEnum - * { - * CAT = 0, - * DOG, - * HAMSTER - * }; - * - * template <> - * struct MaxEnumValue - * { - * static constexpr unsigned int value = static_cast(HAMSTER); - * }; - */ -template -struct MaxEnumValue; // no need to define the primary template - /** * Get the underlying value of an enum, but typesafe. * @@ -108,6 +85,65 @@ inline constexpr auto UnderlyingValue(const T v) { return static_cast::type>(v); } +/* + * Specialize either MaxContiguousEnumValue or MaxEnumValue to provide the + * highest enum member value for an enum class. Note that specializing + * MaxContiguousEnumValue will make MaxEnumValue just take its value from the + * MaxContiguousEnumValue specialization. + * + * Specialize MinContiguousEnumValue and MaxContiguousEnumValue to provide both + * lowest and highest enum member values for an enum class with contiguous + * values. + * + * Each specialization of these structs should define "static constexpr" member + * variable named "value". + * + * example: + * + * enum ExampleEnum + * { + * CAT = 0, + * DOG, + * HAMSTER + * }; + * + * template <> + * struct MaxEnumValue + * { + * static constexpr ExampleEnumvalue = HAMSTER; + * }; + */ + +template +struct MinContiguousEnumValue { + static constexpr T value = static_cast(0); +}; + +template +struct MaxContiguousEnumValue; + +template +struct MaxEnumValue { + static constexpr auto value = MaxContiguousEnumValue::value; +}; + +// Provides the min and max values for a contiguous enum (requires at least +// MaxContiguousEnumValue to be defined). +template +struct ContiguousEnumValues { + static constexpr auto min = MinContiguousEnumValue::value; + static constexpr auto max = MaxContiguousEnumValue::value; +}; + +// Provides the total number of values for a contiguous enum (requires at least +// MaxContiguousEnumValue to be defined). +template +struct ContiguousEnumSize { + static constexpr size_t value = + UnderlyingValue(ContiguousEnumValues::max) + 1 - + UnderlyingValue(ContiguousEnumValues::min); +}; + } // namespace mozilla #endif /* mozilla_EnumTypeTraits_h */ diff --git a/mfbt/EnumeratedArray.h b/mfbt/EnumeratedArray.h index 124199006efa..ba902d94b9ff 100644 --- a/mfbt/EnumeratedArray.h +++ b/mfbt/EnumeratedArray.h @@ -12,6 +12,7 @@ #include #include "mozilla/Array.h" +#include "EnumTypeTraits.h" namespace mozilla { @@ -38,14 +39,18 @@ namespace mozilla { * headCount[AnimalSpecies::Cow] = 17; * headCount[AnimalSpecies::Sheep] = 30; * + * If the enum class has contiguous values and provides a specialization of + * mozilla::MaxContiguousEnumValue then the size will be calculated as the max + * value + 1. */ -template +template ::value> class EnumeratedArray { - public: - static const size_t kSize = size_t(SizeAsEnumValue); - private: - typedef Array ArrayType; + static_assert(UnderlyingValue(MinContiguousEnumValue::value) == 0, + "All indexes would need to be corrected if min != 0"); + + using ArrayType = Array; ArrayType mArray; @@ -56,16 +61,16 @@ class EnumeratedArray { MOZ_IMPLICIT constexpr EnumeratedArray(Args&&... aArgs) : mArray{std::forward(aArgs)...} {} - ValueType& operator[](IndexType aIndex) { return mArray[size_t(aIndex)]; } + ValueType& operator[](Enum aIndex) { return mArray[size_t(aIndex)]; } - const ValueType& operator[](IndexType aIndex) const { + const ValueType& operator[](Enum aIndex) const { return mArray[size_t(aIndex)]; } - typedef typename ArrayType::iterator iterator; - typedef typename ArrayType::const_iterator const_iterator; - typedef typename ArrayType::reverse_iterator reverse_iterator; - typedef typename ArrayType::const_reverse_iterator const_reverse_iterator; + using iterator = typename ArrayType::iterator; + using const_iterator = typename ArrayType::const_iterator; + using reverse_iterator = typename ArrayType::reverse_iterator; + using const_reverse_iterator = typename ArrayType::const_reverse_iterator; // Methods for range-based for loops. iterator begin() { return mArray.begin(); } diff --git a/mfbt/tests/TestEnumeratedArray.cpp b/mfbt/tests/TestEnumeratedArray.cpp index f0a57d57d529..d5ec9ebbb313 100644 --- a/mfbt/tests/TestEnumeratedArray.cpp +++ b/mfbt/tests/TestEnumeratedArray.cpp @@ -4,17 +4,25 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "mozilla/ArrayUtils.h" #include "mozilla/EnumeratedArray.h" +#include "mozilla/EnumTypeTraits.h" using mozilla::EnumeratedArray; -enum class AnimalSpecies { Cow, Sheep, Pig, Count }; +enum class AnimalSpecies { Cow, Sheep, Pig }; -using TestArray = EnumeratedArray; +template <> +struct mozilla::MaxContiguousEnumValue { + static constexpr AnimalSpecies value = AnimalSpecies::Pig; +}; + +using TestArray = EnumeratedArray; void TestInitialValueByConstructor() { // Style 1 TestArray headCount(1, 2, 3); + MOZ_RELEASE_ASSERT(mozilla::ArrayLength(headCount) == 3); MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Cow] == 1); MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Sheep] == 2); MOZ_RELEASE_ASSERT(headCount[AnimalSpecies::Pig] == 3); diff --git a/toolkit/components/formautofill/FormAutofillNative.cpp b/toolkit/components/formautofill/FormAutofillNative.cpp index 80b93a7e48f6..08c462aa4407 100644 --- a/toolkit/components/formautofill/FormAutofillNative.cpp +++ b/toolkit/components/formautofill/FormAutofillNative.cpp @@ -200,14 +200,16 @@ enum class CCExpYearParams : uint8_t { }; struct AutofillParams { - EnumeratedArray + EnumeratedArray mCCNumberParams; - EnumeratedArray mCCNameParams; - EnumeratedArray mCCTypeParams; - EnumeratedArray mCCExpParams; - EnumeratedArray + EnumeratedArray + mCCNameParams; + EnumeratedArray + mCCTypeParams; + EnumeratedArray mCCExpParams; + EnumeratedArray mCCExpMonthParams; - EnumeratedArray + EnumeratedArray mCCExpYearParams; }; @@ -667,11 +669,11 @@ class FormAutofillImpl { // Array contains regular expressions to match the corresponding // field. Ex, CC number, CC type, etc. using RegexStringArray = - EnumeratedArray; + EnumeratedArray; RegexStringArray mRuleMap; // Array that holds RegexWrapper that created by regex::ffi::regex_new - using RegexWrapperArray = EnumeratedArray; + using RegexWrapperArray = EnumeratedArray; RegexWrapperArray mRegexes; }; diff --git a/toolkit/crashreporter/nsExceptionHandler.h b/toolkit/crashreporter/nsExceptionHandler.h index 81aa9b14b5e0..e9e9d87f2c1c 100644 --- a/toolkit/crashreporter/nsExceptionHandler.h +++ b/toolkit/crashreporter/nsExceptionHandler.h @@ -143,7 +143,8 @@ void GetAnnotation(uint32_t childPid, Annotation annotation, nsACString& outStr); // Functions for working with minidumps and .extras -typedef mozilla::EnumeratedArray +typedef mozilla::EnumeratedArray AnnotationTable; void DeleteMinidumpFilesForID( const nsAString& aId, diff --git a/uriloader/exthandler/unix/nsOSHelperAppService.cpp b/uriloader/exthandler/unix/nsOSHelperAppService.cpp index 3535ded72879..fcc2e9f5aba3 100644 --- a/uriloader/exthandler/unix/nsOSHelperAppService.cpp +++ b/uriloader/exthandler/unix/nsOSHelperAppService.cpp @@ -176,7 +176,7 @@ struct FileLocationCache { void Clear() { *this = {}; } }; - EnumeratedArray mEntries; + EnumeratedArray mEntries; static const char* PrefFor(FileKind aKind) { switch (aKind) { diff --git a/widget/nsPrinterBase.cpp b/widget/nsPrinterBase.cpp index dcd8e45f3772..0c8f71d574c6 100644 --- a/widget/nsPrinterBase.cpp +++ b/widget/nsPrinterBase.cpp @@ -126,7 +126,7 @@ nsresult nsPrinterBase::AsyncPromiseAttributeGetter( MOZ_ASSERT(NS_IsMainThread()); static constexpr EnumeratedArray + size_t(AsyncAttribute::Last)> attributeKeys{"SupportsDuplex"_ns, "SupportsColor"_ns, "SupportsMonochrome"_ns, "SupportsCollation"_ns, "PrinterInfo"_ns}; diff --git a/widget/nsPrinterBase.h b/widget/nsPrinterBase.h index 9f8be7891328..8ce1f8a90da8 100644 --- a/widget/nsPrinterBase.h +++ b/widget/nsPrinterBase.h @@ -105,7 +105,7 @@ class nsPrinterBase : public nsIPrinter { private: mozilla::EnumeratedArray, - AsyncAttribute::Last> + size_t(AsyncAttribute::Last)> mAsyncAttributePromises; // List of built-in, commonly used paper sizes. const RefPtr mCommonPaperInfo; diff --git a/widget/nsXPLookAndFeel.cpp b/widget/nsXPLookAndFeel.cpp index 5741eea47410..9eabbd84b63c 100644 --- a/widget/nsXPLookAndFeel.cpp +++ b/widget/nsXPLookAndFeel.cpp @@ -57,7 +57,7 @@ using FontID = mozilla::LookAndFeel::FontID; template class EnumeratedCache { - mozilla::EnumeratedArray mEntries; + mozilla::EnumeratedArray mEntries; std::bitset mValidity; public: diff --git a/widget/windows/nsWindow.h b/widget/windows/nsWindow.h index 3a67f1ce8e5a..3a521fb97832 100644 --- a/widget/windows/nsWindow.h +++ b/widget/windows/nsWindow.h @@ -888,7 +888,7 @@ class nsWindow final : public nsBaseWidget { // Client rect for minimize, maximize and close buttons. mozilla::EnumeratedArray + size_t(WindowButtonType::Count)> mWindowBtnRect; mozilla::DataMutex mDesktopId; diff --git a/xpcom/base/EnumeratedArrayCycleCollection.h b/xpcom/base/EnumeratedArrayCycleCollection.h index 9bf60fc13ecb..863b990de7c0 100644 --- a/xpcom/base/EnumeratedArrayCycleCollection.h +++ b/xpcom/base/EnumeratedArrayCycleCollection.h @@ -10,21 +10,21 @@ #include "mozilla/EnumeratedArray.h" #include "nsCycleCollectionTraversalCallback.h" -template +template inline void ImplCycleCollectionUnlink( - mozilla::EnumeratedArray& aField) { - for (size_t i = 0; i < size_t(SizeAsEnumValue); ++i) { + mozilla::EnumeratedArray& aField) { + for (size_t i = 0; i < Size; ++i) { aField[IndexType(i)] = nullptr; } } -template +template inline void ImplCycleCollectionTraverse( nsCycleCollectionTraversalCallback& aCallback, - mozilla::EnumeratedArray& aField, + mozilla::EnumeratedArray& aField, const char* aName, uint32_t aFlags = 0) { aFlags |= CycleCollectionEdgeNameArrayFlag; - for (size_t i = 0; i < size_t(SizeAsEnumValue); ++i) { + for (size_t i = 0; i < Size; ++i) { ImplCycleCollectionTraverse(aCallback, aField[IndexType(i)], aName, aFlags); } }