1# Copyright (c) Barefoot Networks, Inc. 2# Licensed under the Apache License, Version 2.0 (the "License") 3 4from p4_hlir.hlir import p4_header 5from ebpfStructType import * 6 7class EbpfTypeFactory(object): 8 def __init__(self, config): 9 self.type_map = {} 10 self.config = config 11 12 def build(self, hlirType, asMetadata): 13 name = hlirType.name 14 if hlirType.name in self.type_map: 15 retval = self.type_map[name] 16 if ((not asMetadata and isinstance(retval, EbpfMetadataType)) or 17 (asMetadata and isinstance(retval, EbpfHeaderType))): 18 raise CompilationException( 19 True, "Same type used both as a header and metadata {0}", 20 hlirType) 21 22 if isinstance(hlirType, p4_header): 23 if asMetadata: 24 type = EbpfMetadataType(hlirType, self.config) 25 else: 26 type = EbpfHeaderType(hlirType, self.config) 27 else: 28 raise CompilationException(True, "Unexpected type {0}", hlirType) 29 self.registerType(name, type) 30 return type 31 32 def registerType(self, name, ebpfType): 33 self.type_map[name] = ebpfType 34