From 981e0c082d8b55e65b7bd9ef07c92ed761b88ab7 Mon Sep 17 00:00:00 2001
From: TSRBerry <20988865+TSRBerry@users.noreply.github.com>
Date: Wed, 28 Jun 2023 18:54:20 +0200
Subject: [Spv.Generator] Address dotnet-format issues (#5394)

* dotnet format style --severity info

Some changes were manually reverted.

* Restore a few unused methods and variables

* Silence dotnet format IDE0052 warnings

* Address or silence dotnet format IDE1006 warnings

* Address or silence dotnet format CA1069 warnings

* Address review comments

* Address most dotnet format whitespace warnings

* Run dotnet format after rebase and remove unused usings

- analyzers
- style
- whitespace

* Add comments to disabled warnings

* Simplify properties and array initialization, Use const when possible, Remove trailing commas

* Address IDE0251 warnings

* Revert "Simplify properties and array initialization, Use const when possible, Remove trailing commas"

This reverts commit 9462e4136c0a2100dc28b20cf9542e06790aa67e.

* dotnet format whitespace after rebase

* Rename Operand.cs to IOperand.cs

* Update src/Spv.Generator/Module.cs

Co-authored-by: Ac_K <Acoustik666@gmail.com>

* Remove NotNullWhen attribute and use conditional access to avoid NRE

* Fix duplicated enum values

* Remove unread member

---------

Co-authored-by: Ac_K <Acoustik666@gmail.com>
---
 src/Spv.Generator/Module.cs | 191 ++++++++++++++++++++++----------------------
 1 file changed, 95 insertions(+), 96 deletions(-)

(limited to 'src/Spv.Generator/Module.cs')

diff --git a/src/Spv.Generator/Module.cs b/src/Spv.Generator/Module.cs
index 34ad6036..5e1a9123 100644
--- a/src/Spv.Generator/Module.cs
+++ b/src/Spv.Generator/Module.cs
@@ -15,30 +15,30 @@ namespace Spv.Generator
         private uint _bound;
 
         // Follow spec order here while keeping it as simple as possible.
-        private List<Capability> _capabilities;
-        private List<string> _extensions;
-        private Dictionary<DeterministicStringKey, Instruction> _extInstImports;
+        private readonly List<Capability> _capabilities;
+        private readonly List<string> _extensions;
+        private readonly Dictionary<DeterministicStringKey, Instruction> _extInstImports;
         private AddressingModel _addressingModel;
         private MemoryModel _memoryModel;
 
-        private List<Instruction> _entrypoints;
-        private List<Instruction> _executionModes;
-        private List<Instruction> _debug;
-        private List<Instruction> _annotations;
+        private readonly List<Instruction> _entrypoints;
+        private readonly List<Instruction> _executionModes;
+        private readonly List<Instruction> _debug;
+        private readonly List<Instruction> _annotations;
 
         // In the declaration block.
-        private Dictionary<TypeDeclarationKey, Instruction> _typeDeclarations;
+        private readonly Dictionary<TypeDeclarationKey, Instruction> _typeDeclarations;
         // In the declaration block.
-        private List<Instruction> _globals;
+        private readonly List<Instruction> _globals;
         // In the declaration block.
-        private Dictionary<ConstantKey, Instruction> _constants;
+        private readonly Dictionary<ConstantKey, Instruction> _constants;
         // In the declaration block, for function that aren't defined in the module.
-        private List<Instruction> _functionsDeclarations;
+        private readonly List<Instruction> _functionsDeclarations;
 
-        private List<Instruction> _functionsDefinitions;
+        private readonly List<Instruction> _functionsDefinitions;
 
-        private GeneratorPool<Instruction> _instPool;
-        private GeneratorPool<LiteralInteger> _integerPool;
+        private readonly GeneratorPool<Instruction> _instPool;
+        private readonly GeneratorPool<LiteralInteger> _integerPool;
 
         public Module(uint version, GeneratorPool<Instruction> instPool = null, GeneratorPool<LiteralInteger> integerPool = null)
         {
@@ -143,7 +143,7 @@ namespace Spv.Generator
             _entrypoints.Add(entryPoint);
         }
 
-        public void AddExecutionMode(Instruction function, ExecutionMode mode, params Operand[] parameters)
+        public void AddExecutionMode(Instruction function, ExecutionMode mode, params IOperand[] parameters)
         {
             Debug.Assert(function.Opcode == Op.OpFunction);
 
@@ -225,7 +225,7 @@ namespace Spv.Generator
             _constants.Add(key, constant);
         }
 
-        public Instruction ExtInst(Instruction resultType, Instruction set, LiteralInteger instruction, params Operand[] parameters)
+        public Instruction ExtInst(Instruction resultType, Instruction set, LiteralInteger instruction, params IOperand[] parameters)
         {
             Instruction result = NewInstruction(Op.OpExtInst, GetNewId(), resultType);
 
@@ -262,104 +262,103 @@ namespace Spv.Generator
             // Estimate the size needed for the generated code, to avoid expanding the MemoryStream.
             int sizeEstimate = 1024 + _functionsDefinitions.Count * 32;
 
-            using (MemoryStream stream = new MemoryStream(sizeEstimate))
-            {
-                BinaryWriter writer = new BinaryWriter(stream, System.Text.Encoding.ASCII);
+            using MemoryStream stream = new(sizeEstimate);
 
-                // Header
-                writer.Write(MagicNumber);
-                writer.Write(_version);
-                writer.Write(GeneratorId);
-                writer.Write(_bound);
-                writer.Write(0u);
+            BinaryWriter writer = new(stream, System.Text.Encoding.ASCII);
 
-                // 1.
-                foreach (Capability capability in _capabilities)
-                {
-                    Instruction capabilityInstruction = NewInstruction(Op.OpCapability);
+            // Header
+            writer.Write(MagicNumber);
+            writer.Write(_version);
+            writer.Write(GeneratorId);
+            writer.Write(_bound);
+            writer.Write(0u);
 
-                    capabilityInstruction.AddOperand(capability);
-                    capabilityInstruction.Write(writer);
-                }
+            // 1.
+            foreach (Capability capability in _capabilities)
+            {
+                Instruction capabilityInstruction = NewInstruction(Op.OpCapability);
 
-                // 2.
-                foreach (string extension in _extensions)
-                {
-                    Instruction extensionInstruction = NewInstruction(Op.OpExtension);
+                capabilityInstruction.AddOperand(capability);
+                capabilityInstruction.Write(writer);
+            }
 
-                    extensionInstruction.AddOperand(extension);
-                    extensionInstruction.Write(writer);
-                }
+            // 2.
+            foreach (string extension in _extensions)
+            {
+                Instruction extensionInstruction = NewInstruction(Op.OpExtension);
 
-                // 3.
-                foreach (Instruction extInstImport in _extInstImports.Values)
-                {
-                    extInstImport.Write(writer);
-                }
+                extensionInstruction.AddOperand(extension);
+                extensionInstruction.Write(writer);
+            }
 
-                // 4.
-                Instruction memoryModelInstruction = NewInstruction(Op.OpMemoryModel);
-                memoryModelInstruction.AddOperand(_addressingModel);
-                memoryModelInstruction.AddOperand(_memoryModel);
-                memoryModelInstruction.Write(writer);
+            // 3.
+            foreach (Instruction extInstImport in _extInstImports.Values)
+            {
+                extInstImport.Write(writer);
+            }
 
-                // 5.
-                foreach (Instruction entrypoint in _entrypoints)
-                {
-                    entrypoint.Write(writer);
-                }
+            // 4.
+            Instruction memoryModelInstruction = NewInstruction(Op.OpMemoryModel);
+            memoryModelInstruction.AddOperand(_addressingModel);
+            memoryModelInstruction.AddOperand(_memoryModel);
+            memoryModelInstruction.Write(writer);
 
-                // 6.
-                foreach (Instruction executionMode in _executionModes)
-                {
-                    executionMode.Write(writer);
-                }
+            // 5.
+            foreach (Instruction entrypoint in _entrypoints)
+            {
+                entrypoint.Write(writer);
+            }
 
-                // 7.
-                // TODO: Order debug information correctly.
-                foreach (Instruction debug in _debug)
-                {
-                    debug.Write(writer);
-                }
+            // 6.
+            foreach (Instruction executionMode in _executionModes)
+            {
+                executionMode.Write(writer);
+            }
 
-                // 8.
-                foreach (Instruction annotation in _annotations)
-                {
-                    annotation.Write(writer);
-                }
+            // 7.
+            // TODO: Order debug information correctly.
+            foreach (Instruction debug in _debug)
+            {
+                debug.Write(writer);
+            }
 
-                // Ensure that everything is in the right order in the declarations section.
-                List<Instruction> declarations = new List<Instruction>();
-                declarations.AddRange(_typeDeclarations.Values);
-                declarations.AddRange(_globals);
-                declarations.AddRange(_constants.Values);
-                declarations.Sort((Instruction x, Instruction y) => x.Id.CompareTo(y.Id));
+            // 8.
+            foreach (Instruction annotation in _annotations)
+            {
+                annotation.Write(writer);
+            }
 
-                // 9.
-                foreach (Instruction declaration in declarations)
-                {
-                    declaration.Write(writer);
-                }
+            // Ensure that everything is in the right order in the declarations section.
+            List<Instruction> declarations = new();
+            declarations.AddRange(_typeDeclarations.Values);
+            declarations.AddRange(_globals);
+            declarations.AddRange(_constants.Values);
+            declarations.Sort((Instruction x, Instruction y) => x.Id.CompareTo(y.Id));
 
-                // 10.
-                foreach (Instruction functionDeclaration in _functionsDeclarations)
-                {
-                    functionDeclaration.Write(writer);
-                }
+            // 9.
+            foreach (Instruction declaration in declarations)
+            {
+                declaration.Write(writer);
+            }
 
-                // 11.
-                foreach (Instruction functionDefinition in _functionsDefinitions)
-                {
-                    functionDefinition.Write(writer);
-                }
+            // 10.
+            foreach (Instruction functionDeclaration in _functionsDeclarations)
+            {
+                functionDeclaration.Write(writer);
+            }
 
-                _instPool.Clear();
-                _integerPool.Clear();
+            // 11.
+            foreach (Instruction functionDefinition in _functionsDefinitions)
+            {
+                functionDefinition.Write(writer);
+            }
 
-                LiteralInteger.UnregisterPool();
+            _instPool.Clear();
+            _integerPool.Clear();
 
-                return stream.ToArray();
-            }
+            LiteralInteger.UnregisterPool();
+
+            return stream.ToArray();
         }
     }
 }
-- 
cgit v1.2.3-70-g09d2