Class TypeUtil
java.lang.Object
com.pnfsoftware.jeb.core.units.code.asm.type.TypeUtil
Utility routines for native types.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringC++ package separator.static final PatternC++ template detection pattern.Reserved native type keywords.static final intRetrieve all available types.static final intRetrieve types currently in use.static final intRetrieve types from loaded type libraries. -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanDetermine if two bitfield types are equivalent.static INativeTypebuildArrayType(ITypeManager typeman, String lowestElementSignature, int... dimensions) Convenience routine to create an array type, possibly multi-dimensional.static StringbuildFullyQualifiedTypeNameFromElements(List<String> elements) Build a fully qualified type name from name elements.static INativeTypebuildQuick(ITypeManager typeman, String signature) Build a type or prototype from a simple signature.static IPrototypeItembuildQuickPrototype(ITypeManager typeman, String protoString) Generate a simple prototype item from a prototype string.static INativeTypebuildQuickType(ITypeManager typeman, String signature) Create an array or reference type using an existing base type.static booleancheckAliasedType(INativeType t, INativeType expectedType) Check if a type aliases another type.static voidClear internal caches.static List<INativeType> collectTypes(IPrototypeItem proto) Collect the native types referenced by a prototype.static booleancontainsCppSeparator(String name) Fast check, equivalent tostring.contains(CPP_PACKAGE_SEPARATOR).static INativeTypeAttempt 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 StringgenerateRoutineSignature(String name, IPrototypeItem proto) Generate a routine signature given a prototype and a routine name.static INativeTypegetBaseType(INativeType type) Provide the base type of a type.static INativeTypeGet the first simple type embedded in the given type.static TypeLayoutInfoGet type layout information.static INativeTypeRetrieve the non-aliased type.static <T extends INativeType>
TgetNonAlias(INativeType t, Class<T> expected) Retrieve the non-aliased type and cast it to an expected type.static IStructureTypeFieldgetStructureField(INativeType t, int fieldStartOffset) Given a structure type (or an alias to one), retrieve the field at the provided offset.static IStructureTypeFieldgetStructureField(INativeType t, String fieldName) Given a structure type (or an alias to one), retrieve the field with the provided name.static booleanDetermine if a type is an alias.static booleanDetermine if a type can be used for bitfields.static booleanDetermine if a type is a character type.static booleanDetermine if the (unaliased) type is an array or a structure type.static booleanDetermine if a type is a floating-point type.static booleanDetermine if the provided type is a function pointer, that is, a reference (aliased or not) to a prototype item.static booleanisFunctionPointer(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 booleanDetermine if a type is an integer type.static booleanDetermine whether a string is a reserved keyword.static booleanDetermine if the (unaliased) type is a reference type.static booleanDetermine if the (unaliased) type is a reference to a reference type.static booleanDetermine if a type is primitive.static booleanDetermine if a type is a prototype.static booleanDetermine if a type is a reference.static booleanDetermine whether a string is a reserved literal.static booleanDetermine if a type is a signed integer type.static booleanDetermine if a type is a simple type.static booleanDetermine if a type is an unsigned integer type.static booleanisValidFullyQualifiedName(String basesig, List<String> elements, String[] aname) Parse and validate the elements of a normalized signature (aka fully-qualified name).static booleanDetermine whether a package name is valid.static booleanDetermine whether a type name is valid.static booleanDetermine if a type is void.static booleanDetermine if a type is a pointer to void.static StringnormalizeSignature(boolean alreadyPreNormalized, String signature, List<String> elements, int[] counts) Normalize a type signature (including reference types).static StringpreNormalizeSignature(String signature) Normalize a signature for quick parsing.static StringprocessSignature(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) Retrieve available types.static List<INativeType> retrieveAvailableTypes(INativeCodeUnit<?> unit, int sourceFlags, ISimpleFilter<INativeType> filter) Retrieve available types.static booleansame(INativeType a, INativeType b) Determine whether two types are equal after alias resolution.splitCppName(String fullName) Split a C++ name usingCPP_PACKAGE_SEPARATORas separator.splitCppParameters(String parameters) Split a list of C++ parameters separated by comma.static booleanstructureInStructure(IStructureType structType, INativeType type) Detect structure cycles: Determine if the second parameter type is or contains the structure type provided as the first parameter.
-
Field Details
-
CPP_PACKAGE_SEPARATOR
C++ package separator.- See Also:
-
CPP_TEMPLATE_REGEXP
C++ template detection pattern. -
keywords
Reserved native type keywords. -
TYPESOURCE_INUSE
public static final int TYPESOURCE_INUSERetrieve types currently in use.- See Also:
-
TYPESOURCE_TYPELIBS
public static final int TYPESOURCE_TYPELIBSRetrieve types from loaded type libraries.- See Also:
-
TYPESOURCE_ALL
public static final int TYPESOURCE_ALLRetrieve all available types.- See Also:
-
-
Method Details
-
isKeyword
Determine whether a string is a reserved keyword.- Parameters:
s- string to test- Returns:
- true if the string is a keyword
-
isReservedLiteral
Determine whether a string is a reserved literal.- Parameters:
s- string to test- Returns:
- true if the string is reserved
-
isValidPackageName
Determine whether a package name is valid.- Parameters:
s- package name- Returns:
- true if the package name is valid
-
isValidTypeName
Determine whether a type name is valid.- Parameters:
s- type name- Returns:
- true if the type name is valid
-
containsCppSeparator
Fast check, equivalent tostring.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.
-
isValidFullyQualifiedName
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-lesselements- optional (output elements)aname- optional (output name)- Returns:
- true if the name is valid
-
splitCppName
Split a C++ name usingCPP_PACKAGE_SEPARATORas separator.For example,
'std<int>::pkg:titi'will be split into[std<int>, pkg, titi]- Parameters:
fullName- full C++ name- Returns:
- list of names, never null
-
splitCppParameters
Split a list of C++ parameters separated by comma. Start/End Parentheses are optional.For example,
'std<int>::pkg:titi, long'will be split into[std<int>::pkg:titi, long]- Parameters:
parameters- C++ parameter list- Returns:
- list of parameters, never null
-
processSignature
Process non array or single-dimension arrays of reference or non-reference types.- Parameters:
signature- a pre-normalized signature of the typecounts- 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
-
normalizeSignature
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 "::" (not '.' or '/'). Remove header and trailer whitespace. Remove header "::" if any. Validate signature elements.
- Parameters:
alreadyPreNormalized- true if the signature has beenpre-normalizedsignature- a non-canonical type signatureelements- optional output list to hold the signature elements of the base type signaturecounts- optional output two-element array, will hold the reference count and the dimension count of the provided signature; refer toprocessSignature(String, int[])- Returns:
- the normalized signature, null on error
-
preNormalizeSignature
Normalize a signature for quick parsing.- Parameters:
signature- signature to normalize- Returns:
- never return null
-
buildFullyQualifiedTypeNameFromElements
Build a fully qualified type name from name elements.- Parameters:
elements- name elements- Returns:
- fully qualified type name
-
buildQuick
Build a type or prototype from a simple signature.- Parameters:
typeman- type managersignature- type or prototype signature- Returns:
- native type, or null
-
buildQuickType
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 toTLGen. (TODO: bindings to support string type parsing provided byTLGen).- Parameters:
typeman- type managersignature- 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
-
buildArrayType
public static INativeType buildArrayType(ITypeManager typeman, String lowestElementSignature, int... dimensions) Convenience routine to create an array type, possibly multi-dimensional.- Parameters:
typeman- type managerlowestElementSignature- finest element type (ideally, should be a non-array)dimensions- highest dimensions first, eg, (2, 3, 4) -> type[2][3][4]- Returns:
- the type
-
isPrimitive
Determine if a type is primitive.- Parameters:
t- type- Returns:
- true if primitive
-
isReference
Determine if a type is a reference.- Parameters:
t- type- Returns:
- true if reference
-
isPrototype
Determine if a type is a prototype.- Parameters:
t- type- Returns:
- true if prototype
-
isSimple
Determine if a type is a simple type.- Parameters:
t- type- Returns:
- true if primitive, reference, or prototype
-
isAlias
Determine if a type is an alias.- Parameters:
t- type- Returns:
- true if alias
-
getNonAlias
Retrieve the non-aliased type.- Parameters:
t- type- Returns:
- non-aliased type
-
getNonAlias
Retrieve the non-aliased type and cast it to an expected type.- Type Parameters:
T- expected type- Parameters:
t- typeexpected- expected class- Returns:
- non-aliased type
-
checkAliasedType
Check if a type aliases another type.- Parameters:
t- the type to be checkedexpectedType- the other type, supposedly aliased by the first parameter- Returns:
- true or false
-
isBitfieldCompatible
Determine if a type can be used for bitfields.- Parameters:
t- type- Returns:
- true if bitfield-compatible
-
areBitfielTypesEquivalent
Determine if two bitfield types are equivalent.- Parameters:
a- first typeb- second type- Returns:
- true if equivalent
-
isPointer
Determine if the (unaliased) type is a reference type.- Parameters:
t- type- Returns:
- true if the type is a pointer
-
isPointerToPointer
Determine if the (unaliased) type is a reference to a reference type.- Parameters:
t- type- Returns:
- true if the type is a pointer to pointer
-
isCompositeType
Determine if the (unaliased) type is an array or a structure type.- Parameters:
t- type- Returns:
- true if the type is composite
-
isFunctionPointer
Determine if the provided type is a function pointer, that is, a reference (aliased or not) to a prototype item.- Parameters:
t- type- Returns:
- true if the type is a function pointer
-
isFunctionPointer
Determine if the provided type is a function pointer, that is, a reference (aliased or not) to a prototype item.- Parameters:
t- input typeaproto- optional 1-element array receiving the prototype object on success- Returns:
- success indicator
-
isVoid
Determine if a type is void.- Parameters:
t- type- Returns:
- true if void
-
isVoidPointer
Determine if a type is a pointer to void.- Parameters:
t- type- Returns:
- true if pointer to void
-
isCharacter
Determine if a type is a character type.- Parameters:
t- type- Returns:
- true if character
-
isInteger
Determine if a type is an integer type.- Parameters:
t- type- Returns:
- true if integer
-
isSignedInteger
Determine if a type is a signed integer type.- Parameters:
t- type- Returns:
- true if signed integer
-
isUnsignedInteger
Determine if a type is an unsigned integer type.- Parameters:
t- type- Returns:
- true if unsigned integer
-
isFloat
Determine if a type is a floating-point type.- Parameters:
t- type- Returns:
- true if floating-point
-
collectTypes
Collect the native types referenced by a prototype.- Parameters:
proto- prototype- Returns:
- return and parameter types
-
findType
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 typespattern- 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
-
structureInStructure
Detect structure cycles: Determine if the second parameter type is or contains the structure type provided as the first parameter.- Parameters:
structType- the input structuretype- 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
-
getFirstSimpleType
Get the first simple type embedded in the given type.- Parameters:
type- type- Returns:
- first simple type, or null
-
getBaseType
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.- Parameters:
type- type- Returns:
- the base type of type
-
getStructureField
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 structurefieldName- a field name- Returns:
- null if not found or on error
-
getStructureField
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 structurefieldStartOffset- a field offset- Returns:
- null if not found or on error
-
buildQuickPrototype
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 toTypeStringParserfor 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:
typeman- type managerprotoString- see format above- Returns:
- a prototype item, null on error
- See Also:
-
retrieveAvailableTypes
Retrieve available types.- Parameters:
unit- native code unitsourceFlags- type source flags- Returns:
- available types
-
retrieveAvailableTypes
public static List<INativeType> retrieveAvailableTypes(INativeCodeUnit<?> unit, int sourceFlags, ISimpleFilter<INativeType> filter) Retrieve available types.- Parameters:
unit- native code unitsourceFlags- type source flagsfilter- optional type filter- Returns:
- available types
-
generateRoutineSignature
Generate a routine signature given a prototype and a routine name.- Parameters:
name- routine nameproto- routine prototype- Returns:
- routine signature
-
same
Determine whether two types are equal after alias resolution.- Parameters:
a- first typeb- second type- Returns:
- true if the types are the same
-
getLayoutInfo
Get type layout information.- Parameters:
t- type- Returns:
- layout information
-
clearCache
public static void clearCache()Clear internal caches. -
findShorterForm
Attempt to retrieve a type equivalent to the provided input type, but whose string representation is shorter.- Parameters:
_t- an input type- Returns:
- shorter form of the type (e.g. an existing alias), or the same input type if nothing was found
-