Ghidra Decompiler Analysis Engine
Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends | List of all members
Datatype Class Referenceabstract

The base datatype class for the decompiler. More...

#include <type.hh>

Inheritance diagram for Datatype:
Inheritance graph
[legend]

Public Member Functions

 Datatype (const Datatype &op)
 Construct the base data-type copying low-level properties of another.
 
 Datatype (int4 s, type_metatype m)
 Construct the base data-type providing size and meta-type.
 
 Datatype (int4 s, type_metatype m, const string &n)
 Construct the base data-type providing size, meta-type, and name.
 
bool hasSameVariableBase (const Datatype *ct) const
 Are these the same variable length data-type. More...
 
virtual void printRaw (ostream &s) const
 Print a description of the type to stream. More...
 
virtual DatatypegetSubType (uintb off, uintb *newoff) const
 Recover component data-type one-level down. More...
 
virtual DatatypenearestArrayedComponentForward (uintb off, uintb *newoff, int4 *elSize) const
 
virtual DatatypenearestArrayedComponentBackward (uintb off, uintb *newoff, int4 *elSize) const
 
virtual int4 compare (const Datatype &op, int4 level) const
 Compare for functional equivalence. More...
 
virtual int4 compareDependency (const Datatype &op) const
 Compare for storage in tree structure. More...
 
virtual Datatypeclone (void) const =0
 Clone the data-type.
 
virtual void saveXml (ostream &s) const
 Serialize the data-type to XML. More...
 
int4 typeOrderBool (const Datatype &op) const
 Order this with -op-, treating bool data-type as special. More...
 
void saveXmlBasic (ostream &s) const
 Save basic data-type properties. More...
 
void saveXmlRef (ostream &s) const
 Write an XML reference of this to stream. More...
 
bool isPtrsubMatching (uintb offset) const
 Is this data-type suitable as input to a CPUI_PTRSUB op. More...
 

Protected Types

enum  {
  coretype = 1, chartype = 2, enumtype = 4, poweroftwo = 8,
  utf16 = 16, utf32 = 32, opaque_string = 64, variable_length = 128
}
 Boolean properties of datatypes. More...
 

Protected Member Functions

void restoreXmlBasic (const Element *el)
 Recover basic data-type properties. More...
 
virtual void restoreXml (const Element *el, TypeFactory &typegrp)
 Restore data-type from XML. More...
 

Static Protected Member Functions

static uint8 hashName (const string &nm)
 Produce a data-type id by hashing the type name. More...
 
static uint8 hashSize (uint8 id, int4 size)
 Reversibly hash size into id. More...
 

Protected Attributes

int4 size
 Size (of variable holding a value of this type)
 
string name
 Name of type.
 
type_metatype metatype
 Meta-type - type disregarding size.
 
uint4 flags
 Boolean properties of the type.
 
uint8 id
 A unique id for the type (or 0 if an id is not assigned)
 

Friends

class TypeFactory
 
struct DatatypeCompare
 

Detailed Description

The base datatype class for the decompiler.

Used for symbols, function prototypes, type propagation etc.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protected

Boolean properties of datatypes.

Enumerator
coretype 

This is a basic type which will never be redefined.

chartype 

ASCII character data.

enumtype 

An enumeration type (as well as an integer)

poweroftwo 

An enumeration type where all values are of 2^^n form.

utf16 

16-bit wide chars in unicode UTF16

utf32 

32-bit wide chars in unicode UTF32

opaque_string 

Structure that should be treated as a string.

variable_length 

May be other structures with same name different lengths.

Member Function Documentation

◆ compare()

int4 Datatype::compare ( const Datatype op,
int4  level 
) const
virtual

Compare for functional equivalence.

0 (equality) means the data-types are functionally equivalent (even if names differ) Smaller types come earlier. More specific types come earlier.

Parameters
opis the data-type to compare with this
levelis maximum level to descend when recursively comparing
Returns
negative, 0, positive depending on ordering of types

Reimplemented in TypeSpacebase, TypeCode, TypeStruct, TypeEnum, TypeArray, and TypePointer.

◆ compareDependency()

int4 Datatype::compareDependency ( const Datatype op) const
virtual

Compare for storage in tree structure.

Ordering of data-types for the main TypeFactory container. Comparison only goes down one-level in the component structure, before just comparing pointers.

Parameters
opis the data-type to compare with this
Returns
negative, 0, positive depending on ordering of types

Reimplemented in TypeSpacebase, TypeCode, TypeStruct, TypeEnum, TypeArray, and TypePointer.

◆ getSubType()

Datatype * Datatype::getSubType ( uintb  off,
uintb *  newoff 
) const
virtual

Recover component data-type one-level down.

Given an offset into this data-type, return the component data-type at that offset. Also, pass back a "renormalized" offset suitable for recursize getSubType() calls: i.e. if the original offset hits the exact start of the sub-type, 0 is passed back. If there is no valid component data-type at the offset, return NULL and pass back the original offset

Parameters
offis the offset into this data-type
newoffis a pointer to the passed-back offset
Returns
a pointer to the component data-type or NULL

Reimplemented in TypeSpacebase, TypeCode, TypeStruct, and TypeArray.

◆ hashName()

uint8 Datatype::hashName ( const string &  nm)
staticprotected

Produce a data-type id by hashing the type name.

If a type id is explicitly provided for a data-type, this routine is used to produce an id based on a hash of the name. IDs produced this way will have their sign-bit set to distinguish it from other IDs.

Parameters
nmis the type name to be hashed

◆ hashSize()

uint8 Datatype::hashSize ( uint8  id,
int4  size 
)
staticprotected

Reversibly hash size into id.

This allows IDs for variable length structures to be uniquefied based on size. A base ID is given and a size of the specific instance. A unique ID is returned. The hashing is reversible by feeding the output ID back into this function with the same size.

Parameters
idis the given ID to (de)uniquify
sizeis the instance size of the structure
Returns
the (de)uniquified id

◆ hasSameVariableBase()

bool Datatype::hasSameVariableBase ( const Datatype ct) const

Are these the same variable length data-type.

If this and the other given data-type are both variable length and come from the the same base data-type, return true.

Parameters
ctis the other given data-type to compare with this
Returns
true if they are the same variable length data-type.

◆ isPtrsubMatching()

bool Datatype::isPtrsubMatching ( uintb  offset) const

Is this data-type suitable as input to a CPUI_PTRSUB op.

A CPUI_PTRSUB must act on a pointer data-type where the given offset addresses a component. Perform this check.

Parameters
offsetis the given offset
Returns
true if this is a suitable PTRSUB data-type

◆ nearestArrayedComponentBackward()

Datatype * Datatype::nearestArrayedComponentBackward ( uintb  off,
uintb *  newoff,
int4 *  elSize 
) const
virtual

Find the first component data-type before the given offset that is (or contains) an array, and pass back the difference between the component's start and the given offset. Return the component data-type or null if no array is found.

Parameters
offis the given offset into this data-type
newoffis used to pass back the offset difference
elSizeis used to pass back the array element size
Returns
the component data-type or null

Reimplemented in TypeSpacebase, and TypeStruct.

◆ nearestArrayedComponentForward()

Datatype * Datatype::nearestArrayedComponentForward ( uintb  off,
uintb *  newoff,
int4 *  elSize 
) const
virtual

Find the first component data-type after the given offset that is (or contains) an array, and pass back the difference between the component's start and the given offset. Return the component data-type or null if no array is found.

Parameters
offis the given offset into this data-type
newoffis used to pass back the offset difference
elSizeis used to pass back the array element size
Returns
the component data-type or null

Reimplemented in TypeSpacebase, and TypeStruct.

◆ printRaw()

void Datatype::printRaw ( ostream &  s) const
virtual

Print a description of the type to stream.

Print a raw description of the type to stream. Intended for debugging. Not intended to produce parsable C.

Parameters
sis the output stream

Reimplemented in TypeCode, TypeArray, and TypePointer.

◆ restoreXml()

void Datatype::restoreXml ( const Element el,
TypeFactory typegrp 
)
protectedvirtual

Restore data-type from XML.

Restore a Datatype object from an XML element

Parameters
elis the XML element
typegrpis the underlying TypeFactory that will hold the new object

Reimplemented in TypeCode, TypeStruct, TypeEnum, TypeArray, TypePointer, and TypeUnicode.

◆ restoreXmlBasic()

void Datatype::restoreXmlBasic ( const Element el)
protected

Recover basic data-type properties.

Restore the basic properties (name,size,id) of a data-type from an XML element Properties are read from the attributes of the element

Parameters
elis the XML element

◆ saveXml()

void Datatype::saveXml ( ostream &  s) const
virtual

Serialize the data-type to XML.

Write out a formal description of the data-type as an XML <type> tag. For composite data-types, the description goes down one level, describing the component types only by reference.

Parameters
sis the stream to write to

Reimplemented in TypeSpacebase, TypeCode, TypeStruct, TypeEnum, TypeArray, TypePointer, TypeVoid, TypeUnicode, and TypeChar.

◆ saveXmlBasic()

void Datatype::saveXmlBasic ( ostream &  s) const

Save basic data-type properties.

Write out basic data-type properties (name,size,id) as XML attributes. This routine presumes the initial tag is already written to the stream.

Parameters
sis the stream to write to

◆ saveXmlRef()

void Datatype::saveXmlRef ( ostream &  s) const

Write an XML reference of this to stream.

Write a simple reference to this data-type as an XML <typeref> tag, which only encodes the name and id.

Parameters
sis the stream to write to

◆ typeOrderBool()

int4 Datatype::typeOrderBool ( const Datatype op) const
inline

Order this with -op-, treating bool data-type as special.

Order data-types, with special handling of the bool data-type. Data-types are compared using the normal ordering, but bool is ordered after all other data-types. A return value of 0 indicates the data-types are the same, -1 indicates that this is prefered (ordered earlier), and 1 indicates this is ordered later.

Parameters
opis the other data-type to compare with this
Returns
-1, 0, or 1

The documentation for this class was generated from the following files: