<s> from p4_hlir . hlir import p4_header_instance <EOL> from ebpfType import EbpfType <EOL> from compilationException import CompilationException <EOL> from programSerializer import ProgramSerializer <EOL> import typeFactory <EOL> class EbpfInstanceBase ( object ) : <EOL> def __init__ ( self ) : <EOL> pass <EOL> class SimpleInstance ( EbpfInstanceBase ) : <EOL> def __init__ ( self , hlirInstance , factory , isMetadata ) : <EOL> super ( SimpleInstance , self ) . __init__ ( ) <EOL> self . hlirInstance = hlirInstance <EOL> self . name = hlirInstance . base_name <EOL> self . type = factory . build ( hlirInstance . header_type , isMetadata ) <EOL> def declare ( self , serializer ) : <EOL> assert isinstance ( serializer , ProgramSerializer ) <EOL> self . type . declare ( serializer , self . name , False ) <EOL> class EbpfHeader ( SimpleInstance ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hlirHeaderInstance , factory ) : <EOL> super ( EbpfHeader , self ) . __init__ ( hlirHeaderInstance , factory , False ) <EOL> if hlirHeaderInstance . metadata : <EOL> raise CompilationException ( True , "<STR_LIT>" ) <EOL> if hlirHeaderInstance . index is not None : <EOL> self . name += "<STR_LIT:_>" + str ( hlirHeaderInstance . index ) <EOL> class EbpfMetadata ( SimpleInstance ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hlirMetadataInstance , factory ) : <EOL> super ( EbpfMetadata , self ) . __init__ ( hlirMetadataInstance , factory , True ) <EOL> if not hlirMetadataInstance . metadata : <EOL> raise CompilationException ( <EOL> True , "<STR_LIT>" , <EOL> hlirMetadataInstance ) <EOL> if hlirMetadataInstance . index is not None : <EOL> raise CompilationException ( <EOL> True , "<STR_LIT>" , self . hlirInstance ) <EOL> if hasattr ( hlirMetadataInstance , "<STR_LIT>" ) : <EOL> self . initializer = hlirMetadataInstance . initializer <EOL> else : <EOL> self . initializer = None <EOL> def emitInitializer ( self , serializer ) : <EOL> assert isinstance ( serializer , ProgramSerializer ) <EOL> if self . initializer is None : <EOL> self . type . emitInitializer ( serializer ) <EOL> else : <EOL> for key in self . initializer . keys ( ) : <EOL> serializer . appendFormat ( <EOL> "<STR_LIT>" , key , self . initializer [ key ] ) <EOL> class EbpfHeaderStack ( EbpfInstanceBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , hlirInstance , indexVar , factory ) : <EOL> super ( EbpfHeaderStack , self ) . __init__ ( ) <EOL> assert isinstance ( indexVar , str ) <EOL> assert isinstance ( factory , typeFactory . EbpfTypeFactory ) <EOL> assert isinstance ( hlirInstance , p4_header_instance ) <EOL> self . indexVar = indexVar <EOL> self . name = hlirInstance . base_name <EOL> self . basetype = factory . build ( hlirInstance . header_type , False ) <EOL> assert isinstance ( self . basetype , EbpfType ) <EOL> self . arraySize = hlirInstance . max_index + <NUM_LIT:1> <EOL> self . hlirInstance = hlirInstance <EOL> def declare ( self , serializer ) : <EOL> assert isinstance ( serializer , ProgramSerializer ) <EOL> self . basetype . declareArray ( serializer , self . name , self . arraySize ) </s>
