C-Breeze
C Compiler Infrastructure

[ Project home page]

Node Class Reference
[The AST nodes]

AST node base class. More...

#include <ast.h>

Inheritance diagram for Node:

defNode exprNode stmtNode textNode typeNode unitNode declNode procNode binaryNode callNode castNode commaNode indexNode initializerNode metaexprNode operandNode ternaryNode unaryNode attribNode blockNode exprstmtNode jumpNode loopNode metastmtNode selectionNode targetNode threeAddrNode arrayNode funcNode primNode ptrNode sueNode suespecNode tdefNode List of all members.

Garbage collection.

bool mark
static node_list nodes
static map< Node *, bool > deleted_nodes

Memory leak detection.

int _uid
static int _count
static int _t_count [50]
static int _uid_count

Public Member Functions

 Node (NodeType typ, const Coord coord, bool parenthesized=false)
 Construct a new node.
 Node (const Node &other)
 Copy constructor.
virtual ~Node ()
 Destructor.
virtual typeNodetype () const
 Return the node data type.
virtual typeNodebase_type (bool TdefIndir) const
 Return the base data type of a node.
typeNodedatatype () const
 Call base_type() with the argument true.
typeNodedatatype_superior () const
 Call base_type() with the argument false.
virtual void dataflow (FlowVal *v, FlowProblem &fp)=0
 Run the dataflow analyzer.
virtual Nodeclone () const =0
 Clone the input node.
virtual void output (output_context &ct, Node *par)=0
 Generate C code.
Accessors
Methods to get and set fields in the class.

NodeType typ () const
 Get the node type.
Coord coord () const
 Get the source location.
void coord (const Coord coord)
 Set the source location.
bool parenthesized () const
 Get the parenthesized boolean.
void parenthesized (bool paren)
 Set the parenthesized boolean.
annote_listannotations ()
 Get the annotations list.
FlowValgen () const
 Get the "gen" flow value.
void gen (FlowVal *g)
 Set the "gen" flow value.
FlowValkill () const
 Get the "kill" flow value.
void kill (FlowVal *k)
 Set the "kill" flow value.
AST traversal
These methods provide a uniform way to traverse the AST without explicitly testing the node types. They are all variations of the visitor design pattern. The input object is an instance of a user-defined subclass of Visitor, Walker or Changer. By overriding virtual methods, the user can define what to do at each kind of node that is encountered. See the C-Breeze documentation for more details.

virtual void visit (Visitor *the_visitor)=0
 Dispatch a Visitor.
virtual void walk (Walker &the_walker)=0
 Dispatch a Walker.
virtual Nodechange (Changer &the_changer, bool redispatch=false)=0
 Dispatch a Changer.

Static Public Member Functions

static void report ()
 Report node count statistics.

Private Attributes

NodeType _typ
 The kind of AST node.
Coord _coord
 The source location.
bool _parenthesized
 Whether this Node was enclosed in parenthases in the souce code.
annote_list _annotations
 Annotations list.
FlowVal_gen
 "Gen" flow value.
FlowVal_kill
 "Kill" flow value.

Detailed Description

AST node base class.

Each AST node corresponds to a C construction, such as a unary expression or a while loop. Subclasses are used to represent specific constructions.

To determine what type of AST node is contained in a Node, use the typ() method.

Each node has a source location, known as its "coord." This location specifies what part of the source C code a Node corresponds to.

A Node can have zero or more children. Each child is a Node that is "contained" within the construction represented by the Node. For example, a binary expression node has two children: one for its right operand, and one for its left operand. The Walker and Changer classes may be used to perform operations on a Node and all of its ancestors. See AST traversal for more information.

Definition at line 74 of file ast.h.


Constructor & Destructor Documentation

Node::Node NodeType  typ,
const Coord  coord,
bool  parenthesized = false
 

Construct a new node.

The new Node's annotation list is initialized to be empty; its GEN and KILL values are set to NULL.

Parameters:
typ the kind of node.
coord the location of the construct in the source file.
parenthesized true if the construct is parenthesized.

Node::Node const Node other  ) 
 

Copy constructor.

Construct a copy of the given node, with the same type, coordinate, and parenthasization. Annotations and the GEN and KILL value are not copied: the annotation list is initialized to be empty, and the GEN and KILL values are set to NULL.

Parameters:
other the node to copy.

virtual Node::~Node  )  [virtual]
 

Destructor.

Warning:
Do not use the destructors to delete AST nodes. Instead, rely on the node garbage collector.
Todo:
Fix the AST node garbage collector.


Member Function Documentation

annote_list& Node::annotations  )  [inline]
 

Get the annotations list.

This method returns a modifiable reference to the list of annotations on the node. Users can add new annotations, search for annotations, or remove annotations. Users are responsible for ensuring that every element of the annotation list points to a valid annotation. In particular, no element should be NULL.

See also:
Annote class

Definition at line 274 of file ast.h.

References _annotations.

virtual typeNode* Node::base_type bool  TdefIndir  )  const [virtual]
 

Return the base data type of a node.

This method differs from the Node::type() method in two respects. First, it follows some of the simple type inferences. For example, calling it on an idNode will return the type of its declaration. Second, the boolean argument indicates whether or not to follow typedef links.

Parameters:
TdefIndir pass true to follow typedefs to their definitions.

Reimplemented in declNode, procNode, typeNode, tdefNode, exprNode, idNode, commaNode, callNode, blockNode, and exprstmtNode.

virtual Node* Node::change Changer the_changer,
bool  redispatch = false
[pure virtual]
 

Dispatch a Changer.

This abstract method works much the walker, but allows the tree to be changed.

Parameters:
the_changer the specific Changer object to use.
redispatch pass true to revisit parts of the tree that have changed.

Implemented in unitNode, declNode, subdeclNode, procNode, primNode, tdefNode, ptrNode, arrayNode, funcNode, structNode, unionNode, enumNode, suespecNode, constNode, idNode, binaryNode, unaryNode, castNode, commaNode, ternaryNode, callNode, initializerNode, blockNode, basicblockNode, exprstmtNode, labelNode, caseNode, ifNode, switchNode, whileNode, doNode, forNode, gotoNode, continueNode, breakNode, returnNode, attribNode, operandNode, conditiongotoNode, threeAddrNode, textNode, metaexprNode, and metastmtNode.

virtual Node* Node::clone  )  const [pure virtual]
 

Clone the input node.

This is not a "deep" clone, so be careful. For a deep clone, use the ref_clone_changer class.

Returns:
a shallow copy of the node (no subtrees are copied).

Implemented in unitNode, declNode, subdeclNode, procNode, primNode, tdefNode, ptrNode, arrayNode, funcNode, structNode, unionNode, enumNode, suespecNode, constNode, idNode, binaryNode, unaryNode, castNode, commaNode, ternaryNode, callNode, initializerNode, blockNode, basicblockNode, exprstmtNode, labelNode, caseNode, ifNode, switchNode, whileNode, doNode, forNode, gotoNode, continueNode, breakNode, returnNode, attribNode, operandNode, conditiongotoNode, threeAddrNode, textNode, metaexprNode, and metastmtNode.

Referenced by ref_clone_changer::at_node().

void Node::coord const Coord  coord  )  [inline]
 

Set the source location.

This location should indicate the position in the source text that this Node represents, or Coord::Unknown if it does not represent any node in the source text.

It is not common to set the source location of a node. Currently, only the compiler error messages actually make use of it.

Parameters:
coord the new location.

Definition at line 243 of file ast.h.

References _coord.

Coord Node::coord  )  const [inline]
 

Get the source location.

The Coord class holds a location in the input source (file, line, position). During parsing, each AST node created records the position of the source text that it represents. However, subsequent phases may create or change nodes, so no guarantee is given about how the source location corresponds to the original source text.

Returns:
the Coord structure indicating where the node came from in the source file.

Definition at line 230 of file ast.h.

References _coord.

virtual void Node::dataflow FlowVal v,
FlowProblem fp
[pure virtual]
 

Run the dataflow analyzer.

Each subclass overrides this method to define it's semantics for dataflow analysis. It alters the input flow value to reflect the effect of the node within the given flow problem, calling dataflow() on it's subtrees as necessary. See the dataflow analysis documentation for more information.

Parameters:
v the flow value to pass through the node.
fp the flow problem (specifying the transfer functions).

Implemented in unitNode, declNode, procNode, typeNode, constNode, idNode, binaryNode, unaryNode, castNode, commaNode, ternaryNode, callNode, initializerNode, blockNode, exprstmtNode, labelNode, caseNode, ifNode, switchNode, whileNode, doNode, forNode, gotoNode, continueNode, breakNode, returnNode, attribNode, operandNode, conditiongotoNode, threeAddrNode, textNode, metaexprNode, and metastmtNode.

typeNode* Node::datatype  )  const
 

Call base_type() with the argument true.

typeNode* Node::datatype_superior  )  const
 

Call base_type() with the argument false.

void Node::gen FlowVal g  )  [inline]
 

Set the "gen" flow value.

This value is used in dataflow analyses to store information that is generated at this node. Note that each node has exactly one "gen" flow value.

In order to set the flow value to be empty, call this method with a value of NULL.

Parameters:
g the new gen flow value.

Definition at line 297 of file ast.h.

References _gen.

FlowVal* Node::gen  )  const [inline]
 

Get the "gen" flow value.

This value is used in dataflow analyses to store information that is generated at this node. Note that each node has exactly one "gen" flow value.

Returns:
the "gen" flow value.

Definition at line 284 of file ast.h.

References _gen.

void Node::kill FlowVal k  )  [inline]
 

Set the "kill" flow value.

This value is used in dataflow analyses to store information that is killed at this node. Note that each node has exactly one "kill" flow value.

To set the flow value to be empty, call this method with a value of NULL.

Parameters:
k the new kill flow value.

Definition at line 320 of file ast.h.

References _kill.

FlowVal* Node::kill  )  const [inline]
 

Get the "kill" flow value.

This value is used in dataflow analyses to store information that is killed at this node. Note that each node has exactly one "kill" flow value.

Returns:
the "kill" flow value.

Definition at line 307 of file ast.h.

References _kill.

virtual void Node::output output_context ct,
Node par
[pure virtual]
 

Generate C code.

Each subclass overrides this method to define how to produce the output C code. To use this method, pass an output_context and a null parent.

Parameters:
ct the output context which describes the formatting.
par the parent node (or null, if at the top of a subtree).

Implemented in unitNode, declNode, subdeclNode, procNode, typeNode, exprNode, stmtNode, and textNode.

void Node::parenthesized bool  paren  )  [inline]
 

Set the parenthesized boolean.

This boolean determines whether this expression will be parenthesized in the output. Note that that the parentheses will always be added when they are needed to disambiguate the output. This variable only controls the use of "un-necessary" parentheses.

Parameters:
paren the new value of the parenthesized boolean.

Definition at line 262 of file ast.h.

References _parenthesized.

bool Node::parenthesized  )  const [inline]
 

Get the parenthesized boolean.

Returns:
true if the construct corresponding to this Node was parenthesized in the source.

Definition at line 250 of file ast.h.

References _parenthesized.

static void Node::report  )  [static]
 

Report node count statistics.

The code can be configured to gather statistics about node usage according to type. This method prints the current state of that accounting information to standard out.

NodeType Node::typ  )  const [inline]
 

Get the node type.

This has nothing to do with types in the source code. It indicates what kind of AST node this is (e.g., a unary expression node or a while node).

Returns:
the node type.

Definition at line 216 of file ast.h.

References _typ.

Referenced by gcWalker::delete_nodes(), and NodeInfo::nodeIndex().

virtual typeNode* Node::type  )  const [inline, virtual]
 

Return the node data type.

This method returns the C data type of a node. Several different kinds of nodes contain typeNode pointers. This method is just a unified way to access them. It does not attempt to compute the type, it just returns whatever typeNode pointer resides on the node. The subclasses that have types return them, and the others return null.

Returns:
the typeNode pointer on the node.

Reimplemented in declNode, typeNode, exprNode, and operandNode.

Definition at line 342 of file ast.h.

virtual void Node::visit Visitor the_visitor  )  [pure virtual]
 

Dispatch a Visitor.

This abstract method is the entry point for the visitor design pattern. Each node subclass defines a visit() method that calls the appropriate at_ method in the visitor. For more information see the Visitor documentation.

Parameters:
the_visitor the specific Visitor object to use.

Implemented in unitNode, declNode, subdeclNode, procNode, primNode, tdefNode, ptrNode, arrayNode, funcNode, structNode, unionNode, enumNode, suespecNode, constNode, idNode, binaryNode, unaryNode, castNode, commaNode, ternaryNode, callNode, initializerNode, blockNode, basicblockNode, exprstmtNode, labelNode, caseNode, ifNode, switchNode, whileNode, doNode, forNode, gotoNode, continueNode, breakNode, returnNode, attribNode, operandNode, conditiongotoNode, threeAddrNode, textNode, metaexprNode, and metastmtNode.

virtual void Node::walk Walker the_walker  )  [pure virtual]
 

Dispatch a Walker.

This abstract method works much like the visitor, but instead walks the entire underlying subtree calling the appropriate at_ method at each node. For more information see the Walker documentation.

Parameters:
the_walker the specific Walker object to use.

Implemented in unitNode, declNode, subdeclNode, procNode, primNode, tdefNode, ptrNode, arrayNode, funcNode, structNode, unionNode, enumNode, suespecNode, constNode, idNode, binaryNode, unaryNode, castNode, commaNode, ternaryNode, callNode, initializerNode, blockNode, basicblockNode, exprstmtNode, labelNode, caseNode, ifNode, switchNode, whileNode, doNode, forNode, gotoNode, continueNode, breakNode, returnNode, attribNode, operandNode, conditiongotoNode, threeAddrNode, textNode, metaexprNode, and metastmtNode.

Referenced by gcWalker::at_binary(), sue_complete_walker::check(), and df_number_walker::number().


Member Data Documentation

annote_list Node::_annotations [private]
 

Annotations list.

This is a user-accessible list of Annotation objects that allows general information to be attached to any node. Every element in the list should point to a valid Annotation; in particular, the list should contain no pointers to NULL values.

Definition at line 112 of file ast.h.

Referenced by annotations().

Coord Node::_coord [private]
 

The source location.

For nodes created during parsing, this indicates (roughly) where in the source code the node came from. For nodes created by other processing, you can either pass an existing Coord, or use Coord::Unknown.

Definition at line 93 of file ast.h.

Referenced by coord().

int Node::_count [static, private]
 

Definition at line 141 of file ast.h.

FlowVal* Node::_gen [private]
 

"Gen" flow value.

For dataflow analysis, this field can hold a flow value that stores information generated at this node. When not in use, its value is NULL.

Definition at line 120 of file ast.h.

Referenced by gen().

FlowVal* Node::_kill [private]
 

"Kill" flow value.

For dataflow analysis, this field can hold a flow value that stores information killed at this node. When not in use, its value is NULL.

Definition at line 128 of file ast.h.

Referenced by kill().

bool Node::_parenthesized [private]
 

Whether this Node was enclosed in parenthases in the souce code.

This value is used to decide whether to insert extra parenthases when generating C code from the AST. Note that parenthases will always be included when they are necessary to disambiguate an expression, regardless of the value of this variable.

Definition at line 103 of file ast.h.

Referenced by parenthesized().

int Node::_t_count[50] [static, private]
 

Definition at line 144 of file ast.h.

NodeType Node::_typ [private]
 

The kind of AST node.

This should be set by the constructor for the specific node subclass being created.

Definition at line 84 of file ast.h.

Referenced by typ().

int Node::_uid [private]
 

Definition at line 138 of file ast.h.

int Node::_uid_count [static, private]
 

Definition at line 147 of file ast.h.

map<Node *, bool> Node::deleted_nodes [static]
 

Definition at line 165 of file ast.h.

Referenced by gcWalker::delete_nodes().

bool Node::mark
 

Definition at line 168 of file ast.h.

Referenced by gcWalker::at_binary(), and gcWalker::delete_nodes().

node_list Node::nodes [static]
 

Definition at line 162 of file ast.h.

Referenced by gcWalker::delete_nodes(), and gcWalker::gcWalker().


The documentation for this class was generated from the following file:

Generated on February 1, 2006
Back to the C-Breeze home page