Decaf Compiler
symbol.h
Go to the documentation of this file.
1 
9 #ifndef __ANALYSIS_H
10 #define __ANALYSIS_H
11 
12 #include "ast.h"
13 #include "visitor.h"
14 
18 void type_attr_print(void* value, FILE* output);
19 
23 void symtable_attr_print(void* value, FILE* output);
24 
30 typedef struct Symbol
31 {
35  enum {
36  SCALAR_SYMBOL,
37  ARRAY_SYMBOL,
38  FUNCTION_SYMBOL
39  } symbol_type;
40 
45 
50 
54  int length;
55 
60 
64  enum {
65  UNKNOWN_LOC,
66  STATIC_VAR,
67  STACK_PARAM,
68  STACK_LOCAL
69  } location;
70 
74  int offset;
75 
79  struct Symbol* next;
80 
81 } Symbol;
82 
89 Symbol* Symbol_new (const char* name, DecafType type);
90 
98 Symbol* Symbol_new_array (const char* name, DecafType type, int length);
99 
107 Symbol* Symbol_new_function (const char* name, DecafType return_type, ParameterList* parameters);
108 
112 void Symbol_print (Symbol* symbol, FILE* output);
113 
117 void Symbol_free (Symbol* symbol);
118 
120 
121 
128 typedef struct SymbolTable
129 {
134 
139 
140 } SymbolTable;
141 
146 
153 
160 void SymbolTable_insert (SymbolTable* table, Symbol* symbol);
161 
171 Symbol* SymbolTable_lookup (SymbolTable* table, const char* name);
172 
176 void SymbolTable_free (SymbolTable* table);
177 
191 Symbol* lookup_symbol(ASTNode* node, const char* name);
192 
199 
206 NodeVisitor* PrintSymbolsVisitor_new (FILE* output);
207 
211 typedef struct AnalysisError
212 {
214  struct AnalysisError* next;
215 } AnalysisError;
216 
218 
219 
222 void ErrorList_printf (ErrorList* list, const char* format, ...);
223 
224 #endif
AnalysisError::message
char message[256]
Error message.
Definition: symbol.h:213
visitor.h
AST visitors.
AnalysisError
struct AnalysisError AnalysisError
Static analysis error structure.
ASTNode
Main AST node structure.
Definition: ast.h:447
Symbol_print
void Symbol_print(Symbol *symbol, FILE *output)
Print simplified string representation.
Definition: symbol.c:74
PrintSymbolsVisitor_new
NodeVisitor * PrintSymbolsVisitor_new(FILE *output)
Create a new visitor that prints symbol tables.
Definition: symbol.c:324
Symbol::location
enum Symbol::@5 location
Memory access location (initialized during code generation)
ast.h
AST nodes and attributes.
symtable_attr_print
void symtable_attr_print(void *value, FILE *output)
Function that prints a symbol table attribute using DOT formatting.
Definition: symbol.c:9
NodeVisitor
Node visitor structure.
Definition: visitor.h:27
Symbol
struct Symbol Symbol
Single Decaf symbol.
SymbolList
Linked list of struct Symbol* elements.
Definition: symbol.h:119
MAX_ERROR_LEN
#define MAX_ERROR_LEN
Maximum length (in characters) of any error message.
Definition: common.h:52
DecafType
DecafType
Valid Decaf types.
Definition: common.h:66
BuildSymbolTablesVisitor_new
NodeVisitor * BuildSymbolTablesVisitor_new()
Create a new visitor that builds symbol tables.
Definition: symbol.c:251
Symbol::next
struct Symbol * next
Next symbol (if stored in a list)
Definition: symbol.h:79
Symbol::parameters
ParameterList * parameters
List of parameters (for function symbols only)
Definition: symbol.h:59
ErrorList
Linked list of AnalysisError* elements.
Definition: symbol.h:217
SymbolTable_lookup
Symbol * SymbolTable_lookup(SymbolTable *table, const char *name)
Retrieve a symbol from a table.
Definition: symbol.c:136
DECL_LIST_TYPE
#define DECL_LIST_TYPE(NAME, ELEMTYPE)
Declare a singly-linked list structure of the given type.
Definition: common.h:122
Symbol_new
Symbol * Symbol_new(const char *name, DecafType type)
Create a new scalar symbol.
Definition: symbol.c:26
SymbolTable_new_child
SymbolTable * SymbolTable_new_child(SymbolTable *parent)
Create a new symbol table with a parent link.
Definition: symbol.c:124
Symbol_free
void Symbol_free(Symbol *symbol)
Deallocate a symbol.
Definition: symbol.c:107
ParameterList
Linked list of struct Parameter* elements.
Definition: ast.h:121
SymbolTable
Stores symbol info for a single lexical scope.
Definition: symbol.h:128
Symbol_new_function
Symbol * Symbol_new_function(const char *name, DecafType return_type, ParameterList *parameters)
Create a new function symbol.
Definition: symbol.c:56
Symbol::symbol_type
enum Symbol::@4 symbol_type
Kind of symbol (scalar, array, or function)
AnalysisError::next
struct AnalysisError * next
Next message (if stored in a list)
Definition: symbol.h:214
Symbol::name
char name[256]
Name of symbol in code.
Definition: symbol.h:44
ErrorList_printf
void ErrorList_printf(ErrorList *list, const char *format,...)
Add an error message to a list of errors using printf syntax.
Definition: symbol.c:341
SymbolTable::parent
struct SymbolTable * parent
Link to parent table.
Definition: symbol.h:138
type_attr_print
void type_attr_print(void *value, FILE *output)
Simple "print" function that prints an attribute value as a Decaf type.
Definition: symbol.c:4
SymbolTable_free
void SymbolTable_free(SymbolTable *table)
Deallocate a symbol table.
Definition: symbol.c:149
AnalysisError
Static analysis error structure.
Definition: symbol.h:211
SymbolTable_insert
void SymbolTable_insert(SymbolTable *table, Symbol *symbol)
Add a symbol to a table.
Definition: symbol.c:131
MAX_ID_LEN
#define MAX_ID_LEN
Maximum length (in characters) of any identifier.
Definition: common.h:57
SymbolTable::local_symbols
SymbolList * local_symbols
List of symbols defined in the scope associated with this table.
Definition: symbol.h:133
SymbolTable
struct SymbolTable SymbolTable
Stores symbol info for a single lexical scope.
Symbol_new_array
Symbol * Symbol_new_array(const char *name, DecafType type, int length)
Create a new array symbol.
Definition: symbol.c:41
SymbolTable_new
SymbolTable * SymbolTable_new()
Create a new symbol table with no parent link.
Definition: symbol.c:115
Symbol::offset
int offset
Memory offset (initialized during code generation)
Definition: symbol.h:74
Symbol::type
DecafType type
Variable or function return type.
Definition: symbol.h:49
Symbol
Single Decaf symbol.
Definition: symbol.h:30
Symbol::length
int length
Length (for array symbols only)
Definition: symbol.h:54
lookup_symbol
Symbol * lookup_symbol(ASTNode *node, const char *name)
Look up a symbol in an AST.
Definition: symbol.c:155