See also:
#include <cfront/init.h>
void cfront_initialize (void)
Initializes the internal state of cfront and cpp to reasonable
defaults.
#include <cfront/init.h>
void cfront_stdclib_support(void)
This function will initialize the include path and initialize
type sizes to those specified in limits.h. The include path is
initialized to the following, in order:
#include <cfront/init.h>
void ast_cross_link (<node type>,
void *ast_node,
functionDefinition function,
statement stat,
Scope scope);
Annotates an AST element with all kinds of useful information. For
example, links
IdExpr to the Id_Decl that
declares the name, This is actually a wrapper macro around the function _ast_cross_link. It can perform the cross link decoration on any node in the AST. You should provide the type of the node as the first argument node type, it should always be a super type, for example statement not compound_statement. The node itself should be passed as the second argument. Since this is a generic decoration you have to supply some extra information, the function argument should contain the functionDefinition ast_node is in, if it's not in a functionDefinition just pass NULL. Similarly, the stat argument should contain the statement ast_node is in or NULL. The argument scope should contain the scope the ast_node is in.
Example
void
compound_prepend_statement (compound_statement parent, statement s)
{
compound_statement_body (parent) = Create_List_statement(compound_statement_body (parent), s);
ast_cross_link (statement, s, statement_function (parent), parent, statement_scope (parent);
}
#include <cfront/init.h>
void cfront_purge_namespaces (void);
Cleans the symbol table of old symbols.
The symbol table can become cluttered with old symbols due to rewrites and redecoration of the AST. This can sometime lead to problems in decorating the AST, typically links to elements that no longer exist. This function will remove the symbols obtained from earlier decorations from the symbol table.
#include <cfront/init.h>
void cfront_set_symtab_bottom (void);
Symbols defined before the call to this function will not be removed by
cfront_purge_namespaces.
#include <cfront/platform.h>
int bits_per_char;
The type char is the smallest type in C, the size of all other
types are in terms of chars. As its name suggests, with
bits_per_char you specify the number of bits in a
character, usually this will be 8. Though the ANSI-C standard
specifies that a character should be able to contain all ASCII
characters (7 bits), cfront is able to cope with character sizes of
smaller than 7. Sizes smaller than 1 give undefined behaviour.
Bool char_is_unsigned;
The signedness of char is unspecified by the ANSI-C
standard. Use this variable to specify this.
Bool unsigned_int_fits_long;
TO DO: Why does this exist? It should be obvious from the sizes of the
types.
Bool divide_rounds_to_0;
This variable specifies whether divide rounds down or to zero. This
should be set according to the behaviour of the divide machine
operation of the compilation target.
Bool signed_modulo;
If signed_modulo is set to TRUE, the modulo operator (%) can
return negative values.
Bool unsigned_bitfields;
The signedness of bitfields in structs is unspecified by the
ANSI-C standard. Use this
variable to specify this. If this variable is TRUE a bitfield of the
form int foo:1 can contain the value 0 and 1, if
unsigned_bitfields is FALSE it can contain 0 and -1.
Bool bitfields_allocated_up;
Bit fields can be allocated in an element (eg. int) starting at the
least significant bit (bit 0) and up, bitfields_allocated_up =
TRUE, or from the most significant bit down,
bitfields_allocated_up = FALSE.
Bool shr_preserves_sign;
Whether the shift right operator (>>) preserves the sign of the
left hand side is not specified by the ANSI-C standard. Use this
variable to specify this.
Bool little_endian;
This variable sets the endianness of the compilation target. A little
endian processor stores the value 0x12AB34CD in the following byte
order: 0xCD, 0x34, 0xAB, 0x12. A big endian processor stores this
value as follows: 0x12, 0xAB, 0x34, 0xCD. Both have their merits.
Bool variable_length_arrays_allowed;
This is a gcc extension. Setting this variable to TRUE allows
non-constant array size in array declarations with automatic storage
(local array variables), for example:
void foo(int c)
{
char array[c];
...
}
#include <cfront/platform.h>
void gcc_extensions(void);
Call this function after initialization to provide support for the gcc
extensions, __builtin_va_list, __builtin_va_arg, __builtin_va_end,
__builtin_va_start, __builtin_stdarg_start. These extensions are used
in, for example, the GNU libc.
common members | ||
List_externalDeclaration | decls | All elements in the source file |
There are several traversals on the root of the AST. The most used are cfront_decorate, which annotates the entire AST with useful information, and cfront_give_types which computes the type for all expressions and declarators.
#include <cfront/cfront.h>
cfront Create_cfront
(SrcInfo src_info,
List_externalDeclaration
decls);
Create a cfront AST node. The generic members will be
initialized to their default values.
#include <cfront/init.h>
cfront cfront_Parse (const char *result);
Parses the C file specified by filename and decorates the
resulting AST. if filename is NULL input is read from
stdin. The result is the AST constructed from the input. Any source
errors are recorded by egg. The global variable
cfront_root will also contain the AST.
#include <cfront/init.h>
void cfront_decorate (cfront ast);
This will concatenate sequences of strings into one string, call the
user defined attribute parsers, perform
ast_cross_link, and evaluates all
IntExprs.
#include <cfront/typing.h>
Bool cfront_give_types (cfront root)
Adds types to all declarators and expressions in root. This
will simultaneously do the type checking, any typing errors are
recorded by egg.
common members | ||
statement | body | the compound statement that is the implementation of the function |
List_expression | callers | all expressions (in this file) of the kind CallExprs that call this function |
externalDeclaration | cur_external | the externalDeclaration that contains this functionDefinition |
function_header | head | the declaration specifiers and declarator of the function |
Bool | is_leaf | holds if this function does not call other functions |
List_declaration | old_par_decls | the declarations of parameters for K&R style function definitions |
Type | stack_frame | a type constructed by [Some function] to represent the stack frame of this function |
Input examples:
<functionDefinition>
<head>void main (int argc, char **argv)</head>
<body>
{
printf ("Hello world.\n");
}
</body>
</functionDefinition>
Old style:
<functionDefinition>
<head>void main (argc, argv)</head>
<old_par_decls>
int argc;
char **argv;
</old_par_decls>
<body>
{
printf ("Hello world.\n");
}
</body>
</functionDefinition>
#include <cfront/function.h>
declarator functionDefinition_decl
(functionDefinition function);
Returns the declarator of the header of function. This is not
necessarily a FuncDecl, for example if the
result type is a pointer type, this will be a PointerDecl.
#include <cfront/function.h>
Type
functionDefinition_type (functionDefinition
function);
Returns the type of function.
common members | ||
function_header_tag | tag | Sub type specification |
declarator | decl | the declarator of the functionDefinition |
Type | type | the type of the function, this is the same as the type of the Id_Decl in the tree of decl |
After type information has been computed using cfront_give_types, the member type will contain the type of the function.
Input examples:
<function_header>
<spec>int</spec>
<decl>main (int argc, char **argv)</decl>
</function_header>
{
printf ("Hello world.\n");
}
declaration_specifiers |
specs | list of declaration
specifiers for the functionDefinition |
declarator | pre | The name part of the function declarator. For normal function declarations this will be an Id_Decl, for function pointers this will be a pointer_declarator. |
parameter_list | pars | The function arguments. |
gnu_attr | attrs | GNU attributes that apply to the function. |
compound_statement | body | The body of the function, taken from the functionDefinition, if it's present in the AST. |
#include <cfront/function.h>
Bool function_has_result
(FuncDecl function);
Returns TRUE if the result type of function is not void.
#include <cfront/function.h>
void
function_prepend_parameter
(FuncDecl func,
declaration par);
Add the parameter declaration par
at the front of the parameter declaration list of func.
#include <cfront/function.h>
void
function_append_parameter
(FuncDecl func,
declaration par);
Add the parameter declaration par
at the end of the parameter declaration list of func.
common members | ||
List_declaration | pars | The parameters of a function declaration |
common members | ||
declaration_specifiers | specs | list of declaration specifiers |
List_declarator | decls | |
externalDeclaration | cur_external | optional. The PlainDef that contains this declaration |
common members | ||
declaration_specifier_tag | tag | Sub type specification. |
Type | type | Partial type for the declarators of the declaration. This member is used during typing and is not much use anywhere else. |
declaration_specifier | definition | tag==StructDecl_kind : the StructDef that defines the members tag==UnionDecl_kind : the UnionDef that defines the members otherwise : NULL |
It may seem a little bit awkward, but struct and union definitions are also part of the declaration specifiers. This also makes it clear that the declarator part of a declaration can be empty, since a structure definition does not have to coincide with a variable declaration.
Ident | id | The tag name of the struct |
List_declaration | members | The member declarations of the struct |
Ident | id | The tag name of the union |
List_declaration | members | The member declarations of the union |
Ident | id | The tag name of the struct |
Ident | id | The tag name of the union |
Ident | id | The tag name of the enum |
List_declarator | enums | The declarations of the names defined in this enum |
You may have noticed that the grammar for enumerator_list is a bit strange, it specifies a list of optional enumerators. This is done this way because we cannot, in a non-conflicting way, write down that the list may or may not be terminated by a comma. Whether the enumeration is syntactically correct is checked in a later traversal.
functionDefinition | cur_func | The function that contains this statement |
statement | parent | optional. The statement that contains this statement |
expression | guard | To do: uninitialized member |
statement | original | The statement that was replaced by this statement. Filled by replace_stat |
statement | previous | To do: obsolete member |
<type> | data | The element in this entry |
List_<type> | next | The tail of the list |