Decaf Compiler
Classes | Typedefs | Functions
symbol.h File Reference

Symbols, symbol tables, and static analysis errors. More...

#include "ast.h"
#include "visitor.h"

Go to the source code of this file.

Classes

struct  Symbol
 Single Decaf symbol. More...
 
struct  SymbolList
 Linked list of struct Symbol* elements. More...
 
struct  SymbolTable
 Stores symbol info for a single lexical scope. More...
 
struct  AnalysisError
 Static analysis error structure. More...
 
struct  ErrorList
 Linked list of AnalysisError* elements. More...
 

Typedefs

typedef struct Symbol Symbol
 Single Decaf symbol. More...
 
typedef struct SymbolList SymbolList
 Linked list of struct Symbol* elements.
 
typedef struct SymbolTable SymbolTable
 Stores symbol info for a single lexical scope. More...
 
typedef struct AnalysisError AnalysisError
 Static analysis error structure.
 
typedef struct ErrorList ErrorList
 Linked list of AnalysisError* elements.
 

Functions

void type_attr_print (void *value, FILE *output)
 Simple "print" function that prints an attribute value as a Decaf type.
 
void symtable_attr_print (void *value, FILE *output)
 Function that prints a symbol table attribute using DOT formatting.
 
SymbolSymbol_new (const char *name, DecafType type)
 Create a new scalar symbol. More...
 
SymbolSymbol_new_array (const char *name, DecafType type, int length)
 Create a new array symbol. More...
 
SymbolSymbol_new_function (const char *name, DecafType return_type, ParameterList *parameters)
 Create a new function symbol. More...
 
void Symbol_print (Symbol *symbol, FILE *output)
 Print simplified string representation.
 
void Symbol_free (Symbol *symbol)
 Deallocate a symbol.
 
SymbolListSymbolList_new ()
 Allocate and initialize a new, empty list.
 
void SymbolList_add (SymbolList *list, struct Symbol *item)
 Add an item to the end of a list.
 
int SymbolList_size (SymbolList *list)
 Look up the size of a list.
 
bool SymbolList_is_empty (SymbolList *list)
 Test a list to see if it is empty.
 
void SymbolList_free (SymbolList *list)
 Deallocate a list and any contained items.
 
SymbolTableSymbolTable_new ()
 Create a new symbol table with no parent link.
 
SymbolTableSymbolTable_new_child (SymbolTable *parent)
 Create a new symbol table with a parent link. More...
 
void SymbolTable_insert (SymbolTable *table, Symbol *symbol)
 Add a symbol to a table. More...
 
SymbolSymbolTable_lookup (SymbolTable *table, const char *name)
 Retrieve a symbol from a table. More...
 
void SymbolTable_free (SymbolTable *table)
 Deallocate a symbol table.
 
Symbollookup_symbol (ASTNode *node, const char *name)
 Look up a symbol in an AST. More...
 
NodeVisitorBuildSymbolTablesVisitor_new ()
 Create a new visitor that builds symbol tables. More...
 
NodeVisitorPrintSymbolsVisitor_new (FILE *output)
 Create a new visitor that prints symbol tables. More...
 
ErrorListErrorList_new ()
 Allocate and initialize a new, empty list.
 
void ErrorList_add (ErrorList *list, AnalysisError *item)
 Add an item to the end of a list.
 
int ErrorList_size (ErrorList *list)
 Look up the size of a list.
 
bool ErrorList_is_empty (ErrorList *list)
 Test a list to see if it is empty.
 
void ErrorList_free (ErrorList *list)
 Deallocate a list and any contained items.
 
void ErrorList_printf (ErrorList *list, const char *format,...)
 Add an error message to a list of errors using printf syntax.
 

Detailed Description

Symbols, symbol tables, and static analysis errors.

This module provides declarations for the symbol table and static analysis framework. The declarations in this file will be will be critical for Project 3 (static analysis) and 4 (code generation).

Typedef Documentation

◆ Symbol

typedef struct Symbol Symbol

Single Decaf symbol.

Might represent a scalar variable, an array, or a formal function parameter.

◆ SymbolTable

typedef struct SymbolTable SymbolTable

Stores symbol info for a single lexical scope.

Symbol tables are generated using a simple AST traversal algorithm, and are used during code generation to look up type and location information for individual symbols.

Function Documentation

◆ BuildSymbolTablesVisitor_new()

NodeVisitor* BuildSymbolTablesVisitor_new ( )

Create a new visitor that builds symbol tables.

Returns
Pointer to visitor structure

◆ lookup_symbol()

Symbol* lookup_symbol ( ASTNode node,
const char *  name 
)

Look up a symbol in an AST.

The search has two phases: 1) searching AST nodes for a symbol table as a "symbolTable" attribute and following "parent" attributes as necessary (requires the links as set up by a SetParentVisitor), and 2) searching symbol tables for the given symbol name and following parent pointers as necessary.

Parameters
nodeAST node to begin the search at
nameName of symbol to find
Returns
The Symbol if found, otherwise NULL

◆ PrintSymbolsVisitor_new()

NodeVisitor* PrintSymbolsVisitor_new ( FILE *  output)

Create a new visitor that prints symbol tables.

Parameters
outputFile stream for the print output
Returns
Pointer to visitor structure

◆ Symbol_new()

Symbol* Symbol_new ( const char *  name,
DecafType  type 
)

Create a new scalar symbol.

Parameters
nameName in source code
typeData type

◆ Symbol_new_array()

Symbol* Symbol_new_array ( const char *  name,
DecafType  type,
int  length 
)

Create a new array symbol.

Parameters
nameName in source code
typeData type
lengthArray length

◆ Symbol_new_function()

Symbol* Symbol_new_function ( const char *  name,
DecafType  return_type,
ParameterList parameters 
)

Create a new function symbol.

Parameters
nameName in source code
return_typeFunction return type
parametersList of formal parameter names and data types

◆ SymbolTable_insert()

void SymbolTable_insert ( SymbolTable table,
Symbol symbol 
)

Add a symbol to a table.

Parameters
tableSymbol table to insert the symbol into
symbolSymbol to insert

◆ SymbolTable_lookup()

Symbol* SymbolTable_lookup ( SymbolTable table,
const char *  name 
)

Retrieve a symbol from a table.

Looks through parent tables if the symbol is not found in the local table.

Parameters
tableSymbol table to search
nameName of symbol to find
Returns
The Symbol if found, otherwise NULL

◆ SymbolTable_new_child()

SymbolTable* SymbolTable_new_child ( SymbolTable parent)

Create a new symbol table with a parent link.

Parameters
parentPointer to parent table