Dehydra API: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(function body)
Line 20: Line 20:
</dl>
</dl>


=== process_function(func, body) ===
=== process_function(decl, body) ===


Dehydra calls this for each top-level function or class member function. func is a JS object with the function name, type, location, and isStatic property. body is an array of objects representing the statements in the body AST.
Dehydra calls this for each top-level function or class member function.  
 
<dl>
<dt>.decl
<dd>The function declaration.
<dl>
<dt>.name
<dd>The function name.
<dt>.type
<dd>The function type, including parameter types.
<dt>.loc
<dd>Source location of the function
<dt>.isStatic
<dd>True if the function is static
<dt>.memberOf
<dd>Class containing the function, if any.
</dl>
 
<dt>.body[_]
<dd>The function body is an array with an element for each statement.
<dl>
<dt>.loc
<dd>Source location of the statement
<dt>.statements
<dd>Substatements--each element represents a value referenced in the statement.
</dl>
 
</dl>


=== process_var(decl) ===
=== process_var(decl) ===

Revision as of 22:17, 28 February 2008

This is an attempt to document the functions for writing scripts for Dehydra GCC. It is incomplete, as is dehydra-gcc itself, so things are in constant flux.

Callback Functions

Dehydra works by calling functions in your Javascript for elements of the source code file. You are not required to have any of these functions in your script: if a function is not there, Dehydra will know and will not try to call it. Here are the functions Dehydra will call:

process_class(cls)

Dehydra calls this for each class/struct. process_class is called after process_function is called for all the member functions. cls is a JS object representing the class, with the following properties:

.kind
"class" or "struct"
.bases
An array representing the base classes of this class.
.members
An array of member variables and functions.

process_function(decl, body)

Dehydra calls this for each top-level function or class member function.

.decl
The function declaration.
.name
The function name.
.type
The function type, including parameter types.
.loc
Source location of the function
.isStatic
True if the function is static
.memberOf
Class containing the function, if any.
.body[_]
The function body is an array with an element for each statement.
.loc
Source location of the statement
.statements
Substatements--each element represents a value referenced in the statement.

process_var(decl)

decl is a JS object representing a variable declaration outside of a function body.

process_tree(func, body)

func is a function declaration. body is a JS object representing the CFG for a function body. NOT YET FULLY IMPLEMENTED

input_end()

Called once at the end of the C++ source file.

Builtin functions

The following functions are provided by dehydra and may be called by the user:

warning(msg, ...)

Print one or more warning messages using the GCC warning mechanism. If -Werror is specified this will cause compilation to fail.

error(msg, ...)

Print one or more error messages using the GCC error mechanism. This will cause compilation to fail.

print(msg)

Print a string to stdout (or stderr if the compiler is piping output). If the current callback is associated with a particular location, the location will be printed first.

_print(msg, ...)

Like print() but the location will not be printed.

read_file(filename)

Read a file a return it as a string.

write_file(filename, data)

Write a string to a file.

Variable Objects

Dehydra presents AST nodes as variables. Variables can have flags such as isReturn. Variable type is indicated by the .type attribute.

Methods

Methods have .fieldOf to indicate the variable being acted upon and .methodOf is the type object of the class implementing the method.

Constructors

Like a method, differentiated by presence of .isConstructor attribute. Constructors generally have a .fieldOf attribute unless they are constructing the return value and .isReturn attribute is present.

Type objects

Dehydra provides detailed type objects. A Dehydra type object can represent a primitive type (with name attribute), a class type (with name, members attributes, etc.), a pointer type (with isPointer=true and a type attribute), a reference type (with isReference=true and a type attribute), or a function pointer type (with parameters attribute, etc).