public class

TypeUtil

extends Object
java.lang.Object
   ↳ com.pnfsoftware.jeb.core.units.code.asm.type.TypeUtil

Class Overview

Utility routines for native types.

Summary

Constants
String CPP_PACKAGE_SEPARATOR
int TYPESOURCE_ALL
int TYPESOURCE_INUSE
int TYPESOURCE_TYPELIBS
Fields
public static final Pattern CPP_TEMPLATE_REGEXP
public static final Set<String> keywords
Public Constructors
TypeUtil()
Public Methods
static boolean areBitfielTypesEquivalent(INativeType a, INativeType b)
static INativeType buildArrayType(ITypeManager typeman, String lowestElementSignature, int... dimensions)
Convenience routine to create an array type, possibly multi-dimensional.
static String buildFullyQualifiedTypeNameFromElements(List<String> elements)
static INativeType buildQuick(ITypeManager typeman, String signature)
static IPrototypeItem buildQuickPrototype(ITypeManager typeman, String protoString)
Generate a simple prototype item from a prototype string.
static INativeType buildQuickType(ITypeManager typeman, String signature)
Create an array or reference type using an existing base type.
static boolean checkAliasedType(INativeType t, INativeType expectedType)
Check if a type aliases another type.
static List<INativeType> collectTypes(IPrototypeItem proto)
static boolean containsCppSeparator(String name)
Fast check, equivalent to string.contains(CPP_PACKAGE_SEPARATOR).
static INativeType findShorterForm(INativeType _t)
Attempt to retrieve a type equivalent to the provided input type, but whose string representation is shorter.
static List<INativeType> findType(Collection<? extends INativeType> types, String pattern, boolean earlyExitOnFirstMatch)
Find a type by pattern (partial name or signature, original or effective).
static String generateRoutineSignature(String name, IPrototypeItem proto)
Generate a routine signature given a prototype and a routine name.
static INativeType getBaseType(INativeType type)
Provide the base type of a type.
static INativeType getFirstSimpleType(INativeType type)
Get the first simple type embedded in the given type.
static TypeLayoutInfo getLayoutInfo(INativeType t)
static <T extends INativeType> T getNonAlias(INativeType t, Class<T> expected)
static INativeType getNonAlias(INativeType t)
static IStructureTypeField getStructureField(INativeType t, String fieldName)
Given a structure type (or an alias to one), retrieve the field with the provided name.
static IStructureTypeField getStructureField(INativeType t, int fieldStartOffset)
Given a structure type (or an alias to one), retrieve the field at the provided offset.
static boolean isAlias(INativeType t)
static boolean isBitfieldCompatible(INativeType t)
static boolean isCharacter(INativeType t)
static boolean isCompositeType(INativeType t)
Determine if the (unaliased) type is an array or a structure type.
static boolean isFloat(INativeType t)
static boolean isFunctionPointer(INativeType t)
Determine if the provided type is a function pointer, that is, a reference (aliased or not) to a prototype item.
static boolean isFunctionPointer(INativeType t, IPrototypeItem[] aproto)
Determine if the provided type is a function pointer, that is, a reference (aliased or not) to a prototype item.
static boolean isInteger(INativeType t)
static boolean isKeyword(String s)
static boolean isPointer(INativeType t)
Determine if the (unaliased) type is a reference type.
static boolean isPointerToPointer(INativeType t)
Determine if the (unaliased) type is a reference to a reference type.
static boolean isPrimitive(INativeType t)
static boolean isPrototype(INativeType t)
static boolean isReference(INativeType t)
static boolean isReservedLiteral(String s)
static boolean isSignedInteger(INativeType t)
static boolean isSimple(INativeType t)
static boolean isUnsignedInteger(INativeType t)
static boolean isValidFullyQualifiedName(String basesig, List<String> elements, String[] aname)
Parse and validate the elements of a normalized signature (aka fully-qualified name).
static boolean isValidPackageName(String s)
static boolean isValidTypeName(String s)
static boolean isVoid(INativeType t)
static boolean isVoidPointer(INativeType t)
static String normalizeSignature(boolean alreadyPreNormalized, String signature, List<String> elements, int[] counts)
Normalize a type signature (including reference types).
static String preNormalizeSignature(String signature)
static String processSignature(String signature, int[] counts)
Process non array or single-dimension arrays of reference or non-reference types.
static List<INativeType> retrieveAvailableTypes(INativeCodeUnit<?> unit, int sourceFlags)
static List<INativeType> retrieveAvailableTypes(INativeCodeUnit<?> unit, int sourceFlags, ISimpleFilter<INativeType> filter)
static boolean same(INativeType a, INativeType b)
static List<String> splitCppName(String fullName)
Split a C++ name using CPP_PACKAGE_SEPARATOR as separator.
static List<String> splitCppParameters(String parameters)
Split a list of C++ parameters separated by comma.
static boolean structureInStructure(IStructureType structType, INativeType type)
Detect structure cycles: Determine if the second parameter type is or contains the structure type provided as the first parameter.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final String CPP_PACKAGE_SEPARATOR

Constant Value: "::"

public static final int TYPESOURCE_ALL

Constant Value: 3 (0x00000003)

public static final int TYPESOURCE_INUSE

Constant Value: 1 (0x00000001)

public static final int TYPESOURCE_TYPELIBS

Constant Value: 2 (0x00000002)

Fields

public static final Pattern CPP_TEMPLATE_REGEXP

public static final Set<String> keywords

Public Constructors

public TypeUtil ()

Public Methods

public static boolean areBitfielTypesEquivalent (INativeType a, INativeType b)

public static INativeType buildArrayType (ITypeManager typeman, String lowestElementSignature, int... dimensions)

Convenience routine to create an array type, possibly multi-dimensional.

Parameters
lowestElementSignature finest element type (ideally, should be a non-array)
dimensions highest dimensions first, eg, (2, 3, 4) -> type[2][3][4]
Returns
  • the type

public static String buildFullyQualifiedTypeNameFromElements (List<String> elements)

public static INativeType buildQuick (ITypeManager typeman, String signature)

public static IPrototypeItem buildQuickPrototype (ITypeManager typeman, String protoString)

Generate a simple prototype item from a prototype string. Complex prototypes cannot be parsed by this routine. Prototypes attributes other than var-arg (via `...`) are not supported either. Refer to TypeStringParser for a full-blown C type and prototype parser.

Format:

 [<calling-convention>]returnType([param1Type[, param2Type[, ...]]])
 
Examples:
 void()
 int()
 unsigned int(char)
 <cdecl>char(int, int, int)
 

Parameters
protoString see format above
Returns
  • a prototype item, null on error
See Also

public static INativeType buildQuickType (ITypeManager typeman, String signature)

Create an array or reference type using an existing base type. This method is for convenience only, and by no means exhaustive. Proper type building requires adequate parsing; refer to TLGen. (TODO: bindings to support string type parsing provided by TLGen).

Parameters
signature accepted signatures: abc, abc*, abc**, abc[6], abc*[10], abc**[20]; other type signatures are not accepted (eg, abc[2][4], abc[4]*, abc**[12], etc.)
Returns
  • the type item, null on error

public static boolean checkAliasedType (INativeType t, INativeType expectedType)

Check if a type aliases another type.

Parameters
t the type to be checked
expectedType the other type, supposedly aliased by the first parameter
Returns
  • true or false

public static List<INativeType> collectTypes (IPrototypeItem proto)

public static boolean containsCppSeparator (String name)

Fast check, equivalent to string.contains(CPP_PACKAGE_SEPARATOR).

Parameters
name Potential CPP name with pacakges
Returns
  • true if string contains a CPP package separator. See splitCppName(String) to retrieve CPP elements.

public static INativeType findShorterForm (INativeType _t)

Attempt to retrieve a type equivalent to the provided input type, but whose string representation is shorter.

Returns
  • shorter form of the type (e.g. an existing alias), or the same input type if nothing was found

public static List<INativeType> findType (Collection<? extends INativeType> types, String pattern, boolean earlyExitOnFirstMatch)

Find a type by pattern (partial name or signature, original or effective). Does not include primitives.

Parameters
types input types
pattern type "pattern" (partial name or signature)
earlyExitOnFirstMatch exit on first match: the returned list will contain one element
Returns
  • the list of candidates, possibly empty

public static String generateRoutineSignature (String name, IPrototypeItem proto)

Generate a routine signature given a prototype and a routine name.

public static INativeType getBaseType (INativeType type)

Provide the base type of a type. The base type of a pointer type is its non-pointer type; the base type of an array type is its dimension-less type. Primitive, class, structure, union, enumeration and alias types, as well as prototypes, are all considered base types.

Returns
  • the base type of type

public static INativeType getFirstSimpleType (INativeType type)

Get the first simple type embedded in the given type.

public static TypeLayoutInfo getLayoutInfo (INativeType t)

public static T getNonAlias (INativeType t, Class<T> expected)

public static INativeType getNonAlias (INativeType t)

public static IStructureTypeField getStructureField (INativeType t, String fieldName)

Given a structure type (or an alias to one), retrieve the field with the provided name.

Parameters
t a type whose non-alias should be a structure
fieldName a field name
Returns
  • null if not found or on error

public static IStructureTypeField getStructureField (INativeType t, int fieldStartOffset)

Given a structure type (or an alias to one), retrieve the field at the provided offset.

Parameters
t a type whose non-alias should be a structure
fieldStartOffset a field offset
Returns
  • null if not found or on error

public static boolean isAlias (INativeType t)

public static boolean isBitfieldCompatible (INativeType t)

public static boolean isCharacter (INativeType t)

public static boolean isCompositeType (INativeType t)

Determine if the (unaliased) type is an array or a structure type.

public static boolean isFloat (INativeType t)

public static boolean isFunctionPointer (INativeType t)

Determine if the provided type is a function pointer, that is, a reference (aliased or not) to a prototype item.

public static boolean isFunctionPointer (INativeType t, IPrototypeItem[] aproto)

Determine if the provided type is a function pointer, that is, a reference (aliased or not) to a prototype item.

Parameters
t input type
aproto optional 1-element array receiving the prototype object on success
Returns
  • success indicator

public static boolean isInteger (INativeType t)

public static boolean isKeyword (String s)

public static boolean isPointer (INativeType t)

Determine if the (unaliased) type is a reference type.

public static boolean isPointerToPointer (INativeType t)

Determine if the (unaliased) type is a reference to a reference type.

public static boolean isPrimitive (INativeType t)

public static boolean isPrototype (INativeType t)

public static boolean isReference (INativeType t)

public static boolean isReservedLiteral (String s)

public static boolean isSignedInteger (INativeType t)

public static boolean isSimple (INativeType t)

public static boolean isUnsignedInteger (INativeType t)

public static boolean isValidFullyQualifiedName (String basesig, List<String> elements, String[] aname)

Parse and validate the elements of a normalized signature (aka fully-qualified name).

Parameters
basesig normalized signature, dimension-less, reference-less
elements optional (output elements)
aname optional (output name)

public static boolean isValidPackageName (String s)

public static boolean isValidTypeName (String s)

public static boolean isVoid (INativeType t)

public static boolean isVoidPointer (INativeType t)

public static String normalizeSignature (boolean alreadyPreNormalized, String signature, List<String> elements, int[] counts)

Normalize a type signature (including reference types).

What it does: Make sure the type separator is {@value #CPP_PACKAGE_SEPARATOR} (not '.' or '/'). Remove header and trailer whitespace. Remove header {@value #CPP_PACKAGE_SEPARATOR} if any. Validate signature elements.

Parameters
alreadyPreNormalized true if the signature has been pre-normalized
signature a non-canonical type signature
elements optional output list to hold the signature elements of the base type signature
counts optional output two-element array, will hold the reference count and the dimension count of the provided signature; refer to processSignature(String, int[])
Returns
  • the normalized signature, null on error

public static String preNormalizeSignature (String signature)

Returns
  • never return null

public static String processSignature (String signature, int[] counts)

Process non array or single-dimension arrays of reference or non-reference types.

Parameters
signature a pre-normalized signature of the type
counts optional output array, contains the reference count (0 if none) and the single-dimension array count (-1 if none)
Returns
  • the base signature (reference-less, dimension-less) of the base type; null on error

public static List<INativeType> retrieveAvailableTypes (INativeCodeUnit<?> unit, int sourceFlags)

public static List<INativeType> retrieveAvailableTypes (INativeCodeUnit<?> unit, int sourceFlags, ISimpleFilter<INativeType> filter)

public static boolean same (INativeType a, INativeType b)

public static List<String> splitCppName (String fullName)

Split a C++ name using CPP_PACKAGE_SEPARATOR as separator.

For example, 'std::pkg:titi' will be split into [std, pkg, titi]

Returns
  • list of names, never null

public static List<String> splitCppParameters (String parameters)

Split a list of C++ parameters separated by comma. Start/End Parentheses are optional.

For example, 'std::pkg:titi, long' will be split into ['std::pkg:titi, long]

Returns
  • list of parameters, never null

public static boolean structureInStructure (IStructureType structType, INativeType type)

Detect structure cycles: Determine if the second parameter type is or contains the structure type provided as the first parameter.

Parameters
structType the input structure
type the type to be vetted against the provided structure
Returns
  • true if a cycle was detected (the test type contains the structure type); false otherwise