(self, token, parent=None)
return out
Accepts a token returned by a parser, returns a subclass of CDeclBase.
Accepts a token returned by a parser, returns a subclass of CDeclBase.
Accepts a token returned by a parser returns a subclass of CDeclBase .
def get_type_from_token(self, token, parent=None):
    """Accepts a token returned by a parser, returns a subclass of CDeclBase."""

    comment = codegen_util.mangle_comment(token.comment)
    is_const = token.is_const == "const"

    # An anonymous union declaration
    if token.anonymous_union:
      if not parent and parent.name:
        raise Error(
            "Anonymous unions must be members of a named struct or union.")

      # Generate a name based on the name of the parent.
      name = codegen_util.mangle_varname(parent.name + "_anon_union")

      members = codegen_util.UniqueOrderedDict()
      sub_structs = codegen_util.UniqueOrderedDict()
      out = c_declarations.AnonymousUnion(
          name, members, sub_structs, comment, parent)

      # Add members
      for sub_token in token.members:

        # Recurse into nested structs
        member = self.get_type_from_token(sub_token, parent=out)
        out.members[member.name] = member

        # Nested sub-structures need special treatment
        if isinstance(member, c_declarations.Struct):
          out.sub_structs[member.name] = member

      # Add to dict of unions
      self.types_dict[out.ctypes_typename] = out

    # A struct declaration
    elif token.members:

      name = token.name

      # If the name is empty, see if there is a type declaration that matches
      # this struct's typename
      if not name:
        for k, v in self.typedefs_dict.items():
          if v == token.typename:
            name = k

      # Anonymous structs need a dummy typename
      typename = token.typename
      if not typename:
        if parent:
          typename = token.name
        else:
          raise Error(
              "Anonymous structs that aren't members of a named struct are not "
              "supported (name = '{token.name}').".format(token=token))

      # Mangle the name if it contains any protected keywords
      name = codegen_util.mangle_varname(name)

      members = codegen_util.UniqueOrderedDict()
      sub_structs = codegen_util.UniqueOrderedDict()
      out = c_declarations.Struct(name, typename, members, sub_structs, comment,
                                  parent, is_const)

      # Map the old typename to the mangled typename in typedefs_dict
      self.typedefs_dict[typename] = out.ctypes_typename

      # Add members
      for sub_token in token.members:

        # Recurse into nested structs
        member = self.get_type_from_token(sub_token, parent=out)
        out.members[member.name] = member

        # Nested sub-structures need special treatment
        if isinstance(member, c_declarations.Struct):
          out.sub_structs[member.name] = member

      # Add to dict of structs
      self.types_dict[out.ctypes_typename] = out

    else:

      name = codegen_util.mangle_varname(token.name)
      typename = self.resolve_typename(token.typename)

      # 1D array with size defined at compile time
      if token.size:
        shape = self.get_shape_tuple(token.size)
        if typename in {header_parsing.NONE, header_parsing.CTYPES_CHAR}:
          out = c_declarations.StaticPtrArray(
              name, typename, shape, comment, parent, is_const)
        else:
          out = c_declarations.StaticNDArray(
              name, typename, shape, comment, parent, is_const)

      elif token.ptr:

        # Pointer to a numpy-compatible type, could be an array or a scalar
        if typename in header_parsing.CTYPES_TO_NUMPY:

          # Multidimensional array (one or more dimensions might be undefined)
          if name in self.hints_dict:

            # Dynamically-sized dimensions have string identifiers
            shape = self.hints_dict[name]
            if any(isinstance(d, str) for d in shape):
              out = c_declarations.DynamicNDArray(name, typename, shape,
                                                  comment, parent, is_const)
            else:
              out = c_declarations.StaticNDArray(name, typename, shape, comment,
                                                 parent, is_const)

          # This must be a pointer to a scalar primitive
          else:
            out = c_declarations.ScalarPrimitivePtr(name, typename, comment,
                                                    parent, is_const)

        # Pointer to struct or other arbitrary type
        else:
          out = c_declarations.ScalarPrimitivePtr(name, typename, comment,
                                                  parent, is_const)

      # A struct we've already encountered
      elif typename in self.types_dict:
        s = self.types_dict[typename]
        if isinstance(s, c_declarations.FunctionPtrTypedef):
          out = c_declarations.FunctionPtr(
              name, token.name, s.typename, comment)
        else:
          out = c_declarations.Struct(name, s.typename, s.members,
                                      s.sub_structs, comment, parent)

      # Presumably this is a scalar primitive
      else:
        out = c_declarations.ScalarPrimitive(name, typename, comment, parent,
                                             is_const)

    return out
def get_type_from_token ( self , token , parent = None ) : comment = codegen_util . mangle_comment ( token . comment ) is_const = token . is_const == "const" # An anonymous union declaration if token . anonymous_union : if not parent and parent . name : raise Error ( "Anonymous unions must be members of a named struct or union." ) # Generate a name based on the name of the parent. name = codegen_util . mangle_varname ( parent . name + "_anon_union" ) members = codegen_util . UniqueOrderedDict ( ) sub_structs = codegen_util . UniqueOrderedDict ( ) out = c_declarations . AnonymousUnion ( name , members , sub_structs , comment , parent ) # Add members for sub_token in token . members : # Recurse into nested structs member = self . get_type_from_token ( sub_token , parent = out ) out . members [ member . name ] = member # Nested sub-structures need special treatment if isinstance ( member , c_declarations . Struct ) : out . sub_structs [ member . name ] = member # Add to dict of unions self . types_dict [ out . ctypes_typename ] = out # A struct declaration elif token . members : name = token . name # If the name is empty, see if there is a type declaration that matches # this struct's typename if not name : for k , v in self . typedefs_dict . items ( ) : if v == token . typename : name = k # Anonymous structs need a dummy typename typename = token . typename if not typename : if parent : typename = token . name else : raise Error ( "Anonymous structs that aren't members of a named struct are not " "supported (name = '{token.name}')." . format ( token = token ) ) # Mangle the name if it contains any protected keywords name = codegen_util . mangle_varname ( name ) members = codegen_util . UniqueOrderedDict ( ) sub_structs = codegen_util . UniqueOrderedDict ( ) out = c_declarations . Struct ( name , typename , members , sub_structs , comment , parent , is_const ) # Map the old typename to the mangled typename in typedefs_dict self . typedefs_dict [ typename ] = out . ctypes_typename # Add members for sub_token in token . members : # Recurse into nested structs member = self . get_type_from_token ( sub_token , parent = out ) out . members [ member . name ] = member # Nested sub-structures need special treatment if isinstance ( member , c_declarations . Struct ) : out . sub_structs [ member . name ] = member # Add to dict of structs self . types_dict [ out . ctypes_typename ] = out else : name = codegen_util . mangle_varname ( token . name ) typename = self . resolve_typename ( token . typename ) # 1D array with size defined at compile time if token . size : shape = self . get_shape_tuple ( token . size ) if typename in { header_parsing . NONE , header_parsing . CTYPES_CHAR } : out = c_declarations . StaticPtrArray ( name , typename , shape , comment , parent , is_const ) else : out = c_declarations . StaticNDArray ( name , typename , shape , comment , parent , is_const ) elif token . ptr : # Pointer to a numpy-compatible type, could be an array or a scalar if typename in header_parsing . CTYPES_TO_NUMPY : # Multidimensional array (one or more dimensions might be undefined) if name in self . hints_dict : # Dynamically-sized dimensions have string identifiers shape = self . hints_dict [ name ] if any ( isinstance ( d , str ) for d in shape ) : out = c_declarations . DynamicNDArray ( name , typename , shape , comment , parent , is_const ) else : out = c_declarations . StaticNDArray ( name , typename , shape , comment , parent , is_const ) # This must be a pointer to a scalar primitive else : out = c_declarations . ScalarPrimitivePtr ( name , typename , comment , parent , is_const ) # Pointer to struct or other arbitrary type else : out = c_declarations . ScalarPrimitivePtr ( name , typename , comment , parent , is_const ) # A struct we've already encountered elif typename in self . types_dict : s = self . types_dict [ typename ] if isinstance ( s , c_declarations . FunctionPtrTypedef ) : out = c_declarations . FunctionPtr ( name , token . name , s . typename , comment ) else : out = c_declarations . Struct ( name , s . typename , s . members , s . sub_structs , comment , parent ) # Presumably this is a scalar primitive else : out = c_declarations . ScalarPrimitive ( name , typename , comment , parent , is_const ) return out