Decaf Compiler
Loading...
Searching...
No Matches
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
27typedef 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
81
88
96void NodeVisitor_traverse (NodeVisitor* visitor, ASTNode* node);
97
111
117void NodeVisitor_free (NodeVisitor* visitor);
118
119
120/*
121 * VISITORS
122 */
123
130NodeVisitor* PrintVisitor_new (FILE* output);
131
144NodeVisitor* GenerateASTGraph_new (FILE* output);
145
154
163
164#endif
AST nodes and attributes.
void(* Destructor)(void *)
Function pointer used to store references to custom "free" routines.
Definition ast.h:23
Main AST node structure.
Definition ast.h:471
Node visitor structure.
Definition visitor.h:28
void * data
Visitor-specific state information.
Definition visitor.h:32
Destructor dtor
Pointer to destructor function (used to deallocate the data member)
Definition visitor.h:37
void NodeVisitor_free(NodeVisitor *visitor)
Deallocate a visitor structure.
Definition visitor.c:188
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
NodeVisitor * NodeVisitor_new(void)
Allocate a new generic visitor structure.
Definition visitor.c:13
NodeVisitor * CalcDepthVisitor_new(void)
Create a new visitor that calculates node depths as attributes.
Definition visitor.c:584
NodeVisitor * SetParentVisitor_new(void)
Create a new visitor that sets up parent pointers as attributes.
Definition visitor.c:550
NodeVisitor * GenerateASTGraph_new(FILE *output)
Create a new AST debug graph output visitor.
Definition visitor.c:455
NodeVisitor * PrintVisitor_new(FILE *output)
Create a new AST debug print visitor.
Definition visitor.c:329
void NodeVisitor_traverse(NodeVisitor *visitor, ASTNode *node)
Perform an AST traversal using the given visitor.
Definition visitor.c:60