2121import os .path
2222import re
2323import sys
24+ from typing import Dict , List , Tuple
2425
2526# Find modules relative to the directory containing this script.
2627# This is needed for hermetic Bazel builds, where the Table files are bundled
4546
4647MODE = 'new'
4748
48- def convert_min_required_version (version : str | None ) -> str :
49+ def convert_min_required_version (version ): # (version: str | None) -> str
4950 """Converts the minimal required SPIR-V version encoded in the grammar to
5051 the symbol in SPIRV-Tools."""
5152 if version is None :
@@ -55,7 +56,7 @@ def convert_min_required_version(version: str | None) -> str:
5556 return 'SPV_SPIRV_VERSION_WORD({})' .format (version .replace ('.' , ',' ))
5657
5758
58- def convert_max_required_version (version : str | None ) -> str :
59+ def convert_max_required_version (version ): # (version: str | None) -> str
5960 """Converts the maximum required SPIR-V version encoded in the grammar to
6061 the symbol in SPIRV-Tools."""
6162 if version is None :
@@ -130,7 +131,7 @@ def ctype(kind: str, quantifier: str) -> str:
130131 re .sub (r'([a-z])([A-Z])' , r'\1_\2' , kind ).upper ())
131132
132133
133- def convert_operand_kind (obj : dict [str , str ]) -> str :
134+ def convert_operand_kind (obj : Dict [str , str ]) -> str :
134135 """Returns the corresponding operand type used in spirv-tools for the given
135136 operand kind and quantifier used in the JSON grammar.
136137
@@ -155,12 +156,12 @@ class Grammar():
155156 and enum tables.
156157 Assumes an index range is emitted by printing an IndexRange object.
157158 """
158- def __init__ (self , extensions : list [str ], operand_kinds :list [dict ]) -> None :
159+ def __init__ (self , extensions : List [str ], operand_kinds :List [dict ]) -> None :
159160 self .context = Context ()
160161 self .extensions = extensions
161162 self .operand_kinds = sorted (operand_kinds , key = lambda ok : convert_operand_kind (ok ))
162- self .header_decls : list [str ] = [self .IndexRangeDecls ()]
163- self .body_decls : list [str ] = []
163+ self .header_decls : List [str ] = [self .IndexRangeDecls ()]
164+ self .body_decls : List [str ] = []
164165
165166 if len (self .operand_kinds ) == 0 :
166167 raise Exception ("operand_kinds should be a non-empty list" )
@@ -272,21 +273,21 @@ def ComputeOperandTables(self) -> None:
272273};
273274""" )
274275
275- def ShouldEmit (operand_kind_json : dict [str ,any ]):
276+ def ShouldEmit (operand_kind_json : Dict [str ,any ]):
276277 """ Returns true if we should emit a table for the given
277278 operand kind.
278279 """
279280 category = operand_kind_json .get ('category' )
280281 return category in ['ValueEnum' , 'BitEnum' ]
281282
282283 # Populate kOperandNames
283- operand_names : list [ tuple [IndexRange ,int ]] = []
284- name_range_for_kind : dict [str ,IndexRange ] = {}
284+ operand_names : List [ Tuple [IndexRange ,int ]] = []
285+ name_range_for_kind : Dict [str ,IndexRange ] = {}
285286 for operand_kind_json in self .operand_kinds :
286287 kind_key : str = convert_operand_kind (operand_kind_json )
287288 if ShouldEmit (operand_kind_json ):
288289 operands = [Operand (o ) for o in operand_kind_json ['enumerants' ]]
289- tuples : list [ tuple [str ,int ,str ]] = []
290+ tuples : List [ Tuple [str ,int ,str ]] = []
290291 for o in operands :
291292 tuples .append ((o .enumerant , o .value , kind_key ))
292293 for a in o .aliases :
@@ -297,13 +298,13 @@ def ShouldEmit(operand_kind_json: dict[str,any]):
297298 operand_names .extend (ir_tuples )
298299 else :
299300 pass
300- operand_name_strings : list [str ] = []
301+ operand_name_strings : List [str ] = []
301302 for i in range (0 , len (operand_names )):
302303 ir , value , kind_key = operand_names [i ]
303304 operand_name_strings .append ('{{{}, {}}}, // {} {} in {}' .format (
304305 str (ir ),value ,i ,self .context .GetString (ir ),kind_key ))
305306
306- parts : list [str ] = []
307+ parts : List [str ] = []
307308 parts .append ("""// Operand names and values, ordered by (operand kind, name)
308309// The fields in order are:
309310// name, either the primary name or an alias, indexing into kStrings
@@ -332,13 +333,13 @@ def ShouldEmit(operand_kind_json: dict[str,any]):
332333 self .body_decls .extend (parts )
333334
334335 # Populate kOperandsByValue
335- operands_by_value : list [str ] = []
336- operands_by_value_by_kind : dict [str ,IndexRange ] = {}
336+ operands_by_value : List [str ] = []
337+ operands_by_value_by_kind : Dict [str ,IndexRange ] = {}
337338 for operand_kind_json in self .operand_kinds :
338339 kind_key : str = convert_operand_kind (operand_kind_json )
339340 if ShouldEmit (operand_kind_json ):
340341 operands = [Operand (o ) for o in operand_kind_json ['enumerants' ]]
341- operand_descs : list [str ] = []
342+ operand_descs : List [str ] = []
342343 for o in sorted (operands , key = lambda o : o .value ):
343344 suboperands = [convert_operand_kind (p ) for p in o .parameters ]
344345 desc = [
@@ -433,19 +434,19 @@ def ComputeInstructionTables(self, insts) -> None:
433434""" )
434435
435436 # Create the sorted list of opcode strings, without the 'Op' prefix.
436- opcode_name_entries : list [str ] = []
437- name_value_pairs : list [ tuple [str ,int ]] = []
437+ opcode_name_entries : List [str ] = []
438+ name_value_pairs : List [ Tuple [str ,int ]] = []
438439 for i in insts :
439440 name_value_pairs .append ((i ['opname' ][2 :], i ['opcode' ]))
440441 for a in i .get ('aliases' ,[]):
441442 name_value_pairs .append ((a [2 :], i ['opcode' ]))
442443 name_value_pairs = sorted (name_value_pairs )
443- inst_name_strings : list [str ] = []
444+ inst_name_strings : List [str ] = []
444445 for i in range (0 , len (name_value_pairs )):
445446 name , value = name_value_pairs [i ]
446447 ir = self .context .AddString (name )
447448 inst_name_strings .append ('{{{}, {}}}, // {} {}' .format (str (ir ),value ,i ,name ))
448- parts : list [str ] = []
449+ parts : List [str ] = []
449450 parts .append ("""// Opcode strings (without the 'Op' prefix) and opcode values, ordered by name.
450451// The fields in order are:
451452// name, either the primary name or an alias, indexing into kStrings
@@ -456,9 +457,9 @@ def ComputeInstructionTables(self, insts) -> None:
456457 self .body_decls .extend (parts )
457458
458459 # Create the array of InstructionDesc
459- lines : list [str ] = []
460+ lines : List [str ] = []
460461 for inst in insts :
461- parts : list [str ] = []
462+ parts : List [str ] = []
462463
463464 opname : str = inst ['opname' ]
464465
@@ -530,15 +531,15 @@ def c_str(s: str):
530531 """
531532 return '"{}\\ 0"' .format (json .dumps (s ).strip ('"' ))
532533
533- parts : list [str ] = []
534+ parts : List [str ] = []
534535 parts .append ("// Array of characters, referenced by IndexRanges elsewhere." )
535536 parts .append ("// Each IndexRange denotes a string." )
536537 parts .append ('static const char kStrings[] =' );
537538 parts .extend ([' {} // {}' .format (c_str (s ), str (self .context .strings [s ])) for s in self .context .string_buffer ])
538539 parts .append (';\n ' );
539540 self .body_decls .extend (parts );
540541
541- parts : list [str ] = []
542+ parts : List [str ] = []
542543 parts .append ("""// Array of IndexRanges, where each represents a string by referencing
543544// the kStrings table.
544545// This array contains all sequences of alias strings used in the grammar.
@@ -588,7 +589,7 @@ def c_str(s: str):
588589 parts .append ('};\n ' );
589590 self .body_decls .extend (parts );
590591
591- parts : list [str ] = []
592+ parts : List [str ] = []
592593 parts .append ("// Returns the name of an extension, as an index into kStrings" )
593594 parts .append ("IndexRange ExtensionToIndexRange(Extension extension) {\n switch(extension) {" )
594595 for e in self .extensions :
0 commit comments