Cfront reference manual

See also:

Initialization and general decorations

cfront_initialize

#include <cfront/init.h>

void cfront_initialize (void)
Initializes the internal state of cfront and cpp to reasonable defaults.

cfront_stdclib_support

#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:
  1. $CPATH
  2. $C_INCLUDE_PATH
  3. $CPLUS_INCLUDE_PATH
  4. /usr/local/include
  5. /usr/include

ast_cross_link

#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, statements to the compound statement they are in, function calls to the function they are calling if it exists, and so on.

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

cfront_purge_namespaces

#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.

cfront_set_symtab_bottom

#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.

Platform specific behaviour

Global variables

Use of these variables require the following line: #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];
  ...
}

gcc_extensions

#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.

AST root

This section describes the types and functions related to the root of the cfront AST.

cfront

Grammar: Members: cfront is the root node of the AST. As can be seen from the grammar, all elements are externalDeclarations, be they functions, global variables, typedefs, or something else. A scope is opened for the children of cfront, so when you have several cfront ASTs they will not interfere with each other. There are four ways to create a cfront node, the usual way with cfront_Parse, by copying COPY_cfront, using the "constructor", Create_cfront, and by parsing from a string, Parse_cfront.

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.

Create_cfront

#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.

cfront_Parse

#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.

cfront_decorate

#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.

cfront_give_types

#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.

externalDeclaration

Used by: cfront
Grammar: An externalDeclaration is an element of the AST root, cfront. One rarely has to do anything with this. As the name suggests function definitions are represented by the FuncDef sub-type. All declarations are represented by the PlainDef sub-type. A typedef is a declaration with typedef storage-class, a struct, union, or enum definition is a declaration without a declarator.

Function

This section describes all types and functions related to examining and modifying function declarations and definitions.

functionDefinition

Used by: externalDeclaration
Grammar: Members: A functionDefinition represents a function, including its name, result type, argument types, and the actual implementation. For "new style" function definitions the first three are specified by a function_header. In an "old style", or K&R style, function definition the header does not specify the argument types, instead they are declared in the old_par_decls. The body of a functionDefinition contains the implementation and is guaranteed to be a compound_statement.

Input examples:

functionDefinition_decl

#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.

functionDefinition_type

#include <cfront/function.h>

Type functionDefinition_type (functionDefinition function);
Returns the type of function.

function_header

Used by: functionDefinition
Grammar: Sub types: Members: A function_header describes the name, result, and arguments of a functionDefinition. The way in which the type of the function is defined is exactly as is done in variable declarations. The declarator decl will contain an Id_Decl that is the name of the function, obtainable with get_Id_Decl, and also a FuncDecl that will contain the declarations for the arguments.

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_functiondef

Super type: function_header
Members: To do: can this sub type go away?

declarator : FuncDecl

Super type: declarator
Grammar: Members: The FuncDecl is the declarator that actually declares a function. It specifies the name and the argument types of the function. It's used in both functionDefinition and function declaration. The FuncDecl is the most natural element to use when you wish to inspect functions.

function_has_result

#include <cfront/function.h>

Bool function_has_result (FuncDecl function);
Returns TRUE if the result type of function is not void.

function_prepend_parameter

#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.

function_append_parameter

#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.

parameter_list

Used by: declarator : FuncDecl
Grammar: Members: The parameter_id_list is mapped onto the parameter_list where the declarations have no specifiers and the Par_Id_Decl as declarator.

Variable and typedef

declaration

Used by: declarator : FuncDecl, externalDeclaration : PlainDef
Grammar: Members: A declaration is used to declare variables, types, and functions. One declaration can declare several names. A declaration has two parts, the declaration specifiers, and the declarators. The specifier part can play several roles:
  1. storage class specifier
  2. type qualifier
  3. base type specification
  4. struct and union definition
The declarator part contains the names of the variable, function, or type, in the form of an Id_Decl, aswell as more typing, eg. for pointer, array, and function.

declaration_specifier

Used by: declaration, function_header : spec_functiondef
Grammar: Sub types: Members: Declaration specifiers are used for several things. Most obvious is the use as a type specifier, eg. int, or unsigned long. These are a TypeId, and a Sign followed by a TypeId. The keywords extern, static, auto, register, and (officially for convenience) typedef, are known as storage class specifiers. The keywords const and volatile are known as type qualifiers.

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.

declaration_specifier : StructDef

Super type: declaration_specifier
Grammar: Members: A StructDef, or structure definition, is used to declare a tag as a structure containing the specified members. The structure definition is refered to by a structure declaration, or StructDecl, with the same name.

declaration_specifier : UnionDef

Super type: declaration_specifier
Grammar: Members: A UnionDef, or union definition, is used to declare a tag as a union containing the specified members. The union definition is refered to by a union declaration, or UnionDecl, with the same name.

declaration_specifier : StructDecl

Super type:declaration_specifier
Grammar: Members: A StructDecl, or structure declaration, is used to specify a structure type. The StructDecl is also use for forward declarations of a structure definition. If the StructDecl is part of a pointer declaration the structure may not be defined in the module.

declaration_specifier : UnionDecl

Super type:declaration_specifier
Grammar: Members: A UnionDecl, or union declaration, is used to specify a union type. The UnionDecl is also use for forward declarations of a union definition. If the UnionDecl is part of a pointer declaration the union may not be defined in the module.

declaration_specifier : Enum

Super type: declaration_specifier
Grammar: Members: An Enum is used for both enum declaration and definition.

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.

declarator : enumerator

Super type: declarator
Used by: declaration_specifier : enum_specifier
Grammar: An enumerator declares an element of an Enum. It is only allowed inside an enum.

storage_class_specifier

Used by: declaration_specifier
Grammar: With a storage class the programmer can specify several things.
  1. Whether a variable has internal linkage, static, or external linkage, extern
  2. Whether a variable has static storage, eg. global variables or local static variables, or automatic storage, eg. local variables or parameters (this is what the keyword auto is for)
  3. Whether a variable needs "fast allocation", register
  4. We can even specify whether it is a variable we're declaring or a type, typedef

type_qualifier

Used by: declaration_specifier, declarator : pointer_declarator
Grammar:

declarator

Used by: declaration, function_header
Grammar: Sub types:

declarator : abstract_declarator

Grammar:

declarator : Id_Decl

Super type: declarator
Grammar:

declarator : pointer_declarator

Super type: declarator
Grammar:

declarator : ArrayDecl

Super type: declarator
Grammar:

declarator : initDeclarator

Super type: declarator
Used by: declaration
Grammar:

declarator : structDeclarator

Super type: declarator
Used by: struct_declaration
Grammar:

declarator : Par_Id_Decl

Super type: declarator
Used by: parameter_id_list
Grammar:

Statement

statement

Grammar: Members:

statement : LabeledStat

Super type: statement
Grammar:

statement : CaseStat

Super type:statement
Grammar:

statement : DefaultStat

Super type: statement
Grammar:

statement : ExprStat

Super type: statement
Grammar:

statement : compound_statement

Super type: statement
Used by: functionDefinition
Grammar:

statement : IfStat

Super type: statement
Grammar:

statement : SwitchStat

Super type:statement
Grammar:

statement : WhileStat

Super type: statement
Grammar:

statement : DoStat

Super type: statement
Grammar:

statement : ForStat

Super type:statement
Grammar:

statement : GotoStat

Super type: statement
Grammar:

statement : ContinueStat

Super type: statement
Grammar:

statement : BreakStat

Super type: statement
Grammar:

statement : ReturnStat

Super type: statement
Grammar:

statement : PragmaStat

Super type: statement
Grammar:

Expression

expression

Used by: Grammar: Sub types:

expression : bin_expression

Super type: expression
Grammar:

expression : CallExpr

Super type: expression
Grammar:

expression : CastExpr

Super type: expression
Grammar:

expression : CharExpr

Super type: expression
Grammar:

expression : comma_expression

Super type: expression
Grammar:

expression : CompoundInit

Grammar:

expression : CompoundLit

Super type: expression
Grammar: This is a GNU extension

expression : CondExpr

Super type: expression
Grammar:

expression : DerefMemberExpr

Grammar:

expression : FloatExpr

Super type: expression
Grammar:

expression : IdExpr

Super type: expression
Grammar:

expression : IndexExpr

Super type: expression
Grammar:

expression : LabelExpr

Super type: expression
Grammar: This is a GNU extension.

expression : LabeledInit

Super type: expression
Grammar: GNU extension

expression : MemberExpr

Super type: expression
Grammar:

expression : mon_expression

Super type: expression
Grammar:

expression : NumExpr

Super type: expression
Grammar:

expression : ParExpr

Super type: expression
Grammar:

expression : SizeofExpr

Super type: expression
Grammar:

expression : SizeofType

Super type: expression
Grammar:

expression : StatExpr

Super type: expression
Grammar: This is a GNU extension.

expression : StringExpr

Super type: expression
Grammar:

expression : TypeExpr

Super type: expression
Grammar: Required for gcc 3+ __builtin_va_arg.

expression : WideCharExpr

Super type: expression

Generic AST functions

Common members

To do

List_<type>

All List_<type> types are based on the Front_List type.
<type> data The element in this entry
List_<type> next The tail of the list

Complete grammar

CFront is hosted by
SourceForge.net Logo