Jump to: | OMake Home • Guide Home • Guide (single-page) • Contents (short) • Contents (long) | |
Index: | All • Variables • Functions • Objects • Targets • Options |
The standard library file parse/C/Parse.om
implements a
parser for the C language. The following code fragment gives
an example of a function to parse a file.
open parse/C/Parse parse-file(filename) = prog = $(parser.parse-file prog, $(filename)) ...
The value prog
that is returned is derived from the
object Prog
16.1.3, which splits
the program into the following parts: 1) an array of definitions,
2) a table of struct
definitions, 3) a table of
enum
definitions, and 4) a table of typedef
s.
Each of the programs parts is defined through the following objects in the form of an abstract syntax tree (AST), with methods for performing some operations like resolving type definitions, printing out the tree, etc.
The AST is defined through the following classes, where we use
the notation C/Parse::<object-name>
to represent an
object in the C AST.
Extends: Map
13.1.2
This object represents a table that maps type names to
Type
16.1.68 values.
The base class from which all parser objects are derived.
Fields:
loc : Location
13.1.18 the location of the item.
Every parser item has a location, which is initialized to the value
of parse-loc
at the time the item is created.
Note: if you are defining new items manually, you must define
parse-loc
manually. For example, here is the definition of
the method Type.reference()
.
Type. = class Type ... reference() = parse-loc = $(this.loc) return $(TypePoiunter.make $(this))
Extends: Base
16.1.2.
The Prog
object is used to represent the abstract
syntax tree for a C program. The program has four parts.
defs : Definition Array
16.1.87
is the list of declarations and definitions
in the program.
typedefs : TypeTable
16.1.1
is a table that maps type names to their definitions.
structs : TypeTable
16.1.1
is a table of structure definitions.
tagged-unions : TypeTable
16.1.1
is a table that maps “tagged” unions to their definitions.
A tagged union is a C union in which one integer field, the “tag”,
specifies the variant.
enums : TypeTable
16.1.1
is a table that maps enumeration names to their definitions.
Extends: Base
16.1.2.
The Op
object represents an operator.
Fields:
op : String
is the name of the operator
Extends: C/Parse::Op
16.1.4.
Unary operators.
Extends: C/Parse::Op
16.1.4.
Extends: C/Parse::Op
16.1.4.
Ternary operators (in C, this is only exp ? exp : exp
).
Extends: Base
16.1.2.
The base class for expressions.
Extends: C/Parse::Exp
16.1.8.
A LiteralExp
represents a constant.
Fields:
val : String
the string representation of the constant.
Extends: C/Parse::LiteralExp
16.1.9.
A character constant. The field val
is the constant with
quotations, for example '\n'
.
Extends: C/Parse::LiteralExp
16.1.9.
An integer constant. The field val
is the constant in source
form, with any radix prefix and/or precision suffix, for example
0xabcdL
.
Extends: C/Parse::LiteralExp
16.1.9.
An floating-pointer constant. The field val
is the constant in source
form, for example 31.415926e-1
.
Extends: C/Parse::LiteralExp
16.1.9.
A string constant. The field val
is the constant in source
form, with quotes, for example "Hello world\n"
.
Extends: C/Parse::LiteralExp
16.1.9.
An identifier (a type or variable name). The field val
is the
name of the identifier.
Extends: C/Parse::Exp
16.1.8.
This is an expression with an operator an one subexpression.
Fields:
Extends: C/Parse::Exp1
16.1.15.
A pre-operation, such as --i
.
Extends: C/Parse::Exp1
16.1.15.
A post-operation, such as i--
.
Extends: C/Parse::Exp
16.1.8.
An expression with an operator and two subexpressions,
for example, 1 - 2
.
Fields:
Extends: C/Parse::Exp2
16.1.18.
This represents an assignment operation. The operator
can be either a simple assignment x = 1
, or
involve computation x *= 2
.
Extends: C/Parse::Exp
16.1.8.
This represents an expression with two operators and three
subexpressions. In plain C, there is only one expression
of this form, exp ? exp : exp
. The first operator
is ?
and the second is :
.
Fields:
Extends: C/Parse::Exp
16.1.8.
A parenthesized expression.
Fields:
exp : Exp
16.1.8 the subexpression.
Extends: C/Parse::Exp
16.1.8.
(GCC-specific) A compound statement expression,
for example ({ x = 1; y = 2; })
.
Fields:
stmt : Exp
16.1.8 the statement,
represented as an expression.
Extends: C/Parse::Exp
16.1.8.
A subscripting operation arg1[arg2]
.
Fields:
arg1, arg2 : Exp
16.1.8 the subexpressions.
Extends: C/Parse::Exp
16.1.8.
A function application f(arg1, ..., argN)
.
Fields:
var : String
the function.
args : Exp Array
16.1.8 the arguments to the function.
Extends: C/Parse::Exp
16.1.8.
A type cast (type) exp
.
Fields:
Extends: C/Parse::Exp
16.1.8.
A sizeof
expression, for example sizeof(int)
or sizeof(1 - 2)
.
Fields:
Extends: Base
16.1.2.
An initial value.
Extends: C/Parse::Initializer
16.1.27.
An expression initializer. In plain C, the expression must be constant,
for example the expression 1 - 2
in int x = 1 - 2
.
Fields:
exp : Exp
16.1.8 the expression.
Extends: C/Parse::Initializer
16.1.27.
An array initializer, for example int x[] = { 1, 2, 3 };
.
Fields:
exp_list : Initializer Array
16.1.27 the list of initial values.
Extends: C/Parse::Initializer
16.1.27.
A structure field initializer (GCC-specific). The following definition
contains field initializers of the form <identifier> : <exp>
.
struct foo { int x, y; }; struct foo z = { y: 1; x: 1 + 2; };
Fields:
name : String
the identifier.
exp : Initializer
16.1.27 the initializer.
Extends: Base
16.1.2.
A statement.
Extends: C/Parse::Stmt
16.1.31.
An empty statement, ;
.
Extends: C/Parse::Stmt
16.1.31.
An expression statement, for example 1;
.
Fields:
exp : Exp
16.1.8 the expression
Extends: C/Parse::Stmt
16.1.31.
Labels and cases are modeled as statements.
The DefaultStmt
represents the syntax default:
.
Fields:
exp : Exp
16.1.8 the expression
Extends: C/Parse::Stmt
16.1.31.
Labels and cases are modeled as statements.
The CaseStmt
represents the syntax case <exp>:
.
Fields:
exp : Exp
16.1.8 the expression.
Extends: C/Parse::Stmt
16.1.31.
Labels and cases are modeled as statements.
The LabelStmt
represents the syntax <label>:
.
Fields:
name : String
the label.
Extends: Stmt
16.1.31.
A goto <label>;
statement.
Fields:
name : String
the label.
Extends: Stmt
16.1.31.
A statement continue;
.
Extends: Stmt
16.1.31.
A statement break;
.
Extends: Stmt
16.1.31.
A return statement, for example return 1;
.
Fields:
exp : Exp
16.1.8 the expression to return.
If the statement has no return value return;
, then exp
is an EmptyStmt
16.1.32.
Extends: Stmt
16.1.31.
A compound statement, for example { x = 1; y = 2; }
.
Fields:
stmts : Stmt Array
16.1.31 the list of statements in the block.
Extends: Stmt
16.1.31.
A while
statement while(test) body
.
Fields:
Extends: Stmt
16.1.31.
A do
statement do body while(exp)
.
Fields:
Extends: Stmt
16.1.31.
A for
statement for(init; test; post) body
.
Any of the files may be empty.
Fields:
Extends: Stmt
16.1.31.
An if
statement if(test) stmt1 [else stmt2];
.
Use $(defined stmt2)
to determine if stmt2
is defined.
Fields:
Extends: Stmt
16.1.31.
A switch
statement switch(exp) body
. The body
is a compound statement—a list of statements with the case
labels inline.
Fields:
Extends: Base
16.1.2.
An argument to an assembly directive.
Syntax: : mode (arg)
.
Fields:
mode : String
.
arg : Exp
16.1.8.
Extends: Stmt
16.1.31.
An assembly directive.
asm (exp args);
Fields:
id : String
the keyword, usually either asm
or __asm__
.
exp : Exp
16.1.8 the assembly expression.
args : AsmArg Array
16.1.47 the assembly arguments.
Extends: Base
16.1.2.
A type modifier.
Extends: TypeModBase
16.1.49.
Fields:
name : String
the modifier.
There are three kinds of named type modifiers:
TypeClass
, TypeMod
, and TypeQual
.
Extends: TypeModNamed
16.1.50.
For plain C, one of the words auto
, extern
,
inline
, register
, static
, volatile
,
__volatile
, __inline
, __inline__
.
Extends: TypeModNamed
16.1.50.
For plain C, one of the words long
, short
.
Extends: TypeModNamed
16.1.50.
For plain C, one of the words const
, signed
,
unsigned
, __const
, __restrict
,
__signed
.
Extends: Base
16.1.2.
A variable.
Methods:
is-core()
: this variable is a simple identifier.
is-none()
: this is a null variable (used in parameter declarations).
to-fun()
: translate the variable to a type.
to-string() : String
the string representation.
to-identifier() : String
the identifier name.
to-id() : String
the identifier name.
to-var() : Var
16.1.54 translate to a variable.
replace-var(x : String) : VarId
replace the identifier with a new name x
.
replace-fun(x : String) : VarId
replace the identifier with a new name x
.
to-type(ty : Type) : Type
16.1.68 produce a type from the declaration.
Extends: Var
16.1.54.
A phony identifier, used as a placeholder in a parameter list.
Extends: Var
16.1.54.
A normal identifier.
Fields:
id : String
the identifier.
Extends: Var
16.1.54.
A variable with an initializer.
Fields:
Extends: Var
16.1.54.
A qualified variable, for example const x
.
Fields:
var : Var
16.1.54 the variable.
qualifiers : TypeMod Array
the qualifiers.
Extends: Var
16.1.54.
A variable with the GCC __attribute__
extension.
Fields:
Extends: Var
16.1.54.
A pointer, for example *x
.
Fields:
var : Var
16.1.54 the variable.
Extends: Var
16.1.54.
A reference, for example &x
.
Fields:
var : Var
16.1.54 the variable.
Extends: Var
16.1.54.
A reference, for example x[exp]
.
Fields:
Extends: Var
16.1.54.
A named function var(param1, ..., param2)
.
Fields:
Extends: Var
16.1.54.
A variable with a field specifier, like var : bits
.
Fields:
Extends: Base
16.1.2.
A declaration.
Methods:
is-elide() : Bool
the declaration is an elision (...
).
to-string() : String
the string representation of the declaration.
to-identifier() : String
the identifier associated with the declaration.
to-var() : Var
the variable associated with the declaration.
replace-var(v) : Decl
replace the variable with a new one v
.
to-type() : Type
get the type for the declaration.
Extends: Decl
16.1.65.
The elision ...
.
Extends: Decl
16.1.65.
A type variable declaration, like int x
(the type is int
and the variable is x
).
Fields:
Extends: Base
16.1.2.
A type.
Methods:
is-void() : Bool
is this the void
type?
is-scalar() : Bool
is the type a scalar type (a number
character, or pointer)?
is-struct-or-enum() : Bool
is the type a struct
or enum
type.
storage-info() : String
one of the following values:
false
the type is a char
or numeric type.
nonscalar
the type is not a scalar type.
pointer
the type is a pointer.
ref
the type is a reference type.
no-fields() : Type
16.1.68 remove any
fields from a struct
, union
, or enum
type.unqualified() : Type
16.1.68 remove
any type qualifiers.to-extern() : Type
16.1.68 remove
type qualifiers that are not useful for extern
declarations.to-pointer() : Type
16.1.68 convert
reference and array types to pointer types.dereference() : Type
16.1.68 dereference
a pointer type (remove one level of indirection).reference() : Type
16.1.68 add
a level of indirection.resolve() : Type
16.1.68 resolve
type names to get complete type definitions.simplify() : Type
16.1.68 resolve
the type definition and convert any outermost array or reference
to a pointer type (useful for parameters).to-string() : String
16.1.68
produce the string representation of the type.to-ml-string() : String
16.1.68
produce an string representation of the type in OCaml form.to-name() : String
16.1.68
get a canonical name for the type if it has one.
Extends: Type
16.1.68
A type name.
Fields:
id : String
the type name.
Extends: Type
16.1.68
A fake type, for elisions ...
.
Extends: Type
16.1.68
A verb+struct+ or union
type. A struct type may be complete with fields,
or the fields may be omitted.
Fields:
kind : String
one of struct
or union
.
name : String
the name of the struct. In anonymous structs,
as in struct { int x; } foo;
, the name will be fabricated.fields : VarDecl Array
16.1.67 an
optional list of field declarations.
Methods:
is-struct-or-enum() : Bool
returns true
.
no-fields() : TypeStruct
16.1.71
returns the type without any field declarations.no-subfields() : TypeStruct
16.1.71
returns the type without any nested field definitions.to-name() : String
get a canonical type name for the struct.resolve() : TypeStruct
16.1.71
if the fields are defined, resolve their definitions.find-fields() : TypeStruct
16.1.71
resolve and field definitions (from typedefs) and return a complete
definition if possible.
Extends: Base
16.1.2.
A field in a enum
type. The field has a name
and an optional value.
Fields:
name : String
the name of the constant.
val : Exp option
16.1.8
the value of the field, if there is an explicit value.
Extends: Type
16.1.68
A enum
definition. A TypeEnum
is like
a TypeStruct
16.1.71,
but the field definitions are of type
EnumFieldDecl
16.1.72.
Extends: Type
16.1.68.
A type with one subterm.
Fields:
ty : Type
16.1.68 the subterm.
Extends: Type
16.1.74.
A pointer type.
Extends: Type
16.1.75.
A pointer type *ty
.
Extends: Type
16.1.75.
A reference type &ty
.
Extends: Type
16.1.75.
An array type ty[exp]
.
Fields:
exp : Exp
16.1.8 the dimension of the array.
Extends: Type
16.1.74.
A type with some attributes.
Extends: Type
16.1.79.
A type with a name (for named parameters like int *p
).
Fields:
var : Var
16.1.54 the variable.
Extends: Type
16.1.79.
A type with qualifiers (like static
, long
, etc.).
Fields:
qualifiers : TypeMod Array
16.1.52 the qualifiers.
Extends: TypeQualified
16.1.81.
A type with qualifiers (like static
, long
, etc.).
Extends: TypeAttr
16.1.79.
A type with a GCC __attribute_
+.
attribute : Exp
16.1.8 the attribute.
Extends: TypeAttr
16.1.79.
A type with a bit length. The type should be an integer type.
bits : Exp
16.1.8 the bit-field length.
Extends: TypeAttr
16.1.79.
A type with an initializer.
exp : Exp
16.1.8 the initial value.
Extends: Type
16.1.68.
A function type. This isn't a pointer type; normally C function
types are defined as a TypePointer
16.1.76
to a TypeFun
.
Extends: Base
16.1.2.
A variable definition.
Extends: Definition
16.1.87.
A simple definition without a variable.
This rather silly case is for type-only definitions, like int;
.
Fields:
type : Type
16.1.68 the type.
Extends: Definition
16.1.87.
Extends: TypeVar
16.1.80.
A varable definition, like int x;
.
Fields:
Extends: VarDefCore
16.1.89.
A parameter definition.
Extends: VarDefCore
16.1.89.
A variable definition, like int x;
.
Extends: VarDefCore
16.1.89.
A typedef
, like typedef int x
.
Typedefs basically have the same information as a variable definition.
Extends: Base
16.1.2.
A __dll_callback
definition.
Callbacks are used by the DLL generator to declare functions
that are callback. Syntactically, a callback definition is
like a function declaration, but it uses the __dll_callback
keyword.
__dll_callback int my_callback(int arg);
Fields:
def : Definition
16.1.87 the definition.
Extends: Definition
16.1.87.
A function definition with a body.
Fields:
Extends: Definition
16.1.87.
A pre-processor directive. The parser interpretes these directives literally.
Fields:
line : String
the directive (uninterpreted).
Extends: Base
16.1.2.
A GCC extension, defined with __extension__ ...
.
Fields:
item : Exp
16.1.8 the extension.
The item
is usually an Exp
, but for forward compatibility
it may be a value of any type.
Jump to: | OMake Home • Guide Home • Guide (single-page) • Contents (short) • Contents (long) | |
Index: | All • Variables • Functions • Objects • Targets • Options |