Decaf Compiler
visitor.h
Go to the documentation of this file.
1 
11 #ifndef __VISITOR_H
12 #define __VISITOR_H
13 
14 #include "ast.h"
15 
16 
17 /*
18  * AST TRAVERSAL (VISITOR PATTERN)
19  */
20 
27 typedef struct NodeVisitor
28 {
32  void *data;
33 
38 
39  /*
40  * Traversal routines; each of these is called at the appropriate time as
41  * the visitor traverses the AST.
42  */
43 
44  #ifndef SKIP_IN_DOXYGEN
45  void (* previsit_default) (struct NodeVisitor* visitor, ASTNode* node);
46  void (*postvisit_default) (struct NodeVisitor* visitor, ASTNode* node);
47  void (* previsit_program) (struct NodeVisitor* visitor, ASTNode* node);
48  void (*postvisit_program) (struct NodeVisitor* visitor, ASTNode* node);
49  void (* previsit_vardecl) (struct NodeVisitor* visitor, ASTNode* node);
50  void (*postvisit_vardecl) (struct NodeVisitor* visitor, ASTNode* node);
51  void (* previsit_funcdecl) (struct NodeVisitor* visitor, ASTNode* node);
52  void (*postvisit_funcdecl) (struct NodeVisitor* visitor, ASTNode* node);
53  void (* previsit_block) (struct NodeVisitor* visitor, ASTNode* node);
54  void (*postvisit_block) (struct NodeVisitor* visitor, ASTNode* node);
55  void (* previsit_assignment) (struct NodeVisitor* visitor, ASTNode* node);
56  void (*postvisit_assignment) (struct NodeVisitor* visitor, ASTNode* node);
57  void (* previsit_conditional) (struct NodeVisitor* visitor, ASTNode* node);
58  void (*postvisit_conditional) (struct NodeVisitor* visitor, ASTNode* node);
59  void (* previsit_whileloop) (struct NodeVisitor* visitor, ASTNode* node);
60  void (*postvisit_whileloop) (struct NodeVisitor* visitor, ASTNode* node);
61  void (* previsit_return) (struct NodeVisitor* visitor, ASTNode* node);
62  void (*postvisit_return) (struct NodeVisitor* visitor, ASTNode* node);
63  void (* previsit_break) (struct NodeVisitor* visitor, ASTNode* node);
64  void (*postvisit_break) (struct NodeVisitor* visitor, ASTNode* node);
65  void (* previsit_continue) (struct NodeVisitor* visitor, ASTNode* node);
66  void (*postvisit_continue) (struct NodeVisitor* visitor, ASTNode* node);
67  void (* previsit_binaryop) (struct NodeVisitor* visitor, ASTNode* node);
68  void (* invisit_binaryop) (struct NodeVisitor* visitor, ASTNode* node);
69  void (*postvisit_binaryop) (struct NodeVisitor* visitor, ASTNode* node);
70  void (* previsit_unaryop) (struct NodeVisitor* visitor, ASTNode* node);
71  void (*postvisit_unaryop) (struct NodeVisitor* visitor, ASTNode* node);
72  void (* previsit_location) (struct NodeVisitor* visitor, ASTNode* node);
73  void (*postvisit_location) (struct NodeVisitor* visitor, ASTNode* node);
74  void (* previsit_funccall) (struct NodeVisitor* visitor, ASTNode* node);
75  void (*postvisit_funccall) (struct NodeVisitor* visitor, ASTNode* node);
76  void (* previsit_literal) (struct NodeVisitor* visitor, ASTNode* node);
77  void (*postvisit_literal) (struct NodeVisitor* visitor, ASTNode* node);
78  #endif
79 
80 } NodeVisitor;
81 
88 
96 void NodeVisitor_traverse (NodeVisitor* visitor, ASTNode* node);
97 
110 void NodeVisitor_traverse_and_free (NodeVisitor* visitor, ASTNode* node);
111 
117 void NodeVisitor_free (NodeVisitor* visitor);
118 
119 
120 /*
121  * VISITORS
122  */
123 
130 NodeVisitor* PrintVisitor_new (FILE* output);
131 
144 NodeVisitor* GenerateASTGraph_new (FILE* output);
145 
154 
163 
164 #endif
NodeVisitor_traverse
void NodeVisitor_traverse(NodeVisitor *visitor, ASTNode *node)
Perform an AST traversal using the given visitor.
Definition: visitor.c:60
ASTNode
Main AST node structure.
Definition: ast.h:449
SetParentVisitor_new
NodeVisitor * SetParentVisitor_new()
Create a new visitor that sets up parent pointers as attributes.
Definition: visitor.c:550
CalcDepthVisitor_new
NodeVisitor * CalcDepthVisitor_new()
Create a new visitor that calculates node depths as attributes.
Definition: visitor.c:584
PrintVisitor_new
NodeVisitor * PrintVisitor_new(FILE *output)
Create a new AST debug print visitor.
Definition: visitor.c:329
ast.h
AST nodes and attributes.
NodeVisitor
Node visitor structure.
Definition: visitor.h:27
NodeVisitor_free
void NodeVisitor_free(NodeVisitor *visitor)
Deallocate a visitor structure.
Definition: visitor.c:188
NodeVisitor::data
void * data
Visitor-specific state information.
Definition: visitor.h:32
NodeVisitor
struct NodeVisitor NodeVisitor
Node visitor structure.
NodeVisitor_traverse_and_free
void NodeVisitor_traverse_and_free(NodeVisitor *visitor, ASTNode *node)
Perform an AST traversal using the given visitor and then deallocate the visitor.
Definition: visitor.c:182
GenerateASTGraph_new
NodeVisitor * GenerateASTGraph_new(FILE *output)
Create a new AST debug graph output visitor.
Definition: visitor.c:455
NodeVisitor::dtor
Destructor dtor
Pointer to destructor function (used to deallocate the data member)
Definition: visitor.h:37
NodeVisitor_new
NodeVisitor * NodeVisitor_new()
Allocate a new generic visitor structure.
Definition: visitor.c:13
Destructor
void(* Destructor)(void *)
Function pointer used to store references to custom "free" routines.
Definition: ast.h:23