public class

TypeStringParser

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

Class Overview

A parser for C declaration, such as types, prototypes and routine signatures.

Examples:

 ITypeManager typeman = TypeManager.createForIntel386();
 IStructureType tHouse = typeman.createStructure("House");
 typeman.addStructureField(tHouse, "field0", typeman.getType("int"));
 
 TypeStringParser parser = new TypeStringParser(typeman);
 parser.parseType("int a;");         // name is optional; semicolon is also optional
 parser.parseType("int");            // name optional; semicolon optional
 parser.parseType("House*");
 parser.parseType("void (*f)()");    // for function pointers, a name (which will be discarded) is mandatory; semicolon is optional
 parser.parseType("char* __cdecl f(int, double);");
 
 // the format for anonymous prototypes is the following (no name; again, final semi-colon optional)
 // anonymous prototypes follow a JEB-custom format: their calling convention precedes the return type, and is stored in angled brackets
 // prototype attributes are not supported
 parser.parsePrototype("void()");
 parser.parsePrototype("int();");
 parser.parsePrototype("int(void)");
 parser.parsePrototype("<__cdecl> int(int)");
 parser.parsePrototype("<__cdecl> int(int, int)");
 parser.parsePrototype("<__cdecl> int(int, char*, ...)");
 
 // signatures: standard C signatures with support for MSVC- and GCC-style attributes
 parser.parseSignature("void __cdecl f()");
 parser.parseSignature("void f(void)");
 parser.parseSignature("void f(int a)");
 parser.parseSignature("void f(unsigned, unsigned int *)");
 parser.parseSignature("void f(int a, void __cdecl (*pf[4])(int a))");
 parser.parseSignature("void f(int a, ...)");
 parser.parseSignature("void f(int a[3])");
 parser.parseSignature("void __attribute__((noreturn)) exit(int code)");  //GCC no-return attribute
 parser.parseSignature("void __cdecl __declspec(noreturn) exit(int)");  //MSVC no-return attribute
 
 

Summary

Constants
int PROTO
int SIG
int TYPE
Public Constructors
TypeStringParser(ITypeManager typeman)
Create a type parser.
Public Methods
static String massageSingleDeclaration(String declstring, boolean discardCppTemplates)
IPrototypeItem parsePrototype(String s)
This method is deprecated. It is recommended to use #parseSignature(String) instead.
IPrototypeItem parseSignature(String s, boolean discardCppTemplates)
Parse a standard C function signature.
INativeType parseType(String s)
Parse a type.@return
static String removeCppTemplates(String s)
static void verify(String declstring, boolean discardCppTemplates)
This static method can be used to verify that the provided argument looks like a valid C declaration.
[Expand]
Inherited Methods
From class java.lang.Object

Constants

public static final int PROTO

Constant Value: 1 (0x00000001)

public static final int SIG

Constant Value: 2 (0x00000002)

public static final int TYPE

Constant Value: 0 (0x00000000)

Public Constructors

public TypeStringParser (ITypeManager typeman)

Create a type parser.

Parameters
typeman the type manager to be used as a source of existing types and recipient of newly-parsed types

Public Methods

public static String massageSingleDeclaration (String declstring, boolean discardCppTemplates)

public IPrototypeItem parsePrototype (String s)

This method is deprecated.
It is recommended to use #parseSignature(String) instead.

Parse an anonymous prototype written using the following style:

 <calling-convention> returnType(paramType1 _)
 <calling-convention> returnType(paramType1, paramType2)
 <calling-convention> returnType(paramType1, paramType2, ...)
 

Parameters
s a JEB-style prototype string
Returns
  • a prototype object

public IPrototypeItem parseSignature (String s, boolean discardCppTemplates)

Parse a standard C function signature.

Parameters
s a signature a C declaration
discardCppTemplates if true, templated types "<...>" will be erased, thereby making the type potentially parsable as a C type
Returns
  • a prototype item also holding the routine name and parameter names

public INativeType parseType (String s)

Parse a type.@return

public static String removeCppTemplates (String s)

public static void verify (String declstring, boolean discardCppTemplates)

This static method can be used to verify that the provided argument looks like a valid C declaration.

Parameters
declstring a C declaration
discardCppTemplates if true, templated types "<...>" will be erased, thereby making the type potentially parsable as a C type
Throws
TypeStringParseException a parsing error occurred