Intel SPMD Program Compiler  1.9.1
Public Member Functions | Private Types | Private Member Functions | Private Attributes | List of all members
SymbolTable Class Reference

Symbol table that holds all known symbols during parsing and compilation. More...

#include <sym.h>

Collaboration diagram for SymbolTable:
Collaboration graph
[legend]

Public Member Functions

 SymbolTable ()
 
 ~SymbolTable ()
 
void PushScope ()
 
void PopScope ()
 
bool AddVariable (Symbol *symbol)
 
SymbolLookupVariable (const char *name)
 
bool AddFunction (Symbol *symbol)
 
bool LookupFunction (const char *name, std::vector< Symbol * > *matches=NULL)
 
SymbolLookupFunction (const char *name, const FunctionType *type)
 
template<typename Predicate >
void GetMatchingFunctions (Predicate pred, std::vector< Symbol * > *matches) const
 
template<typename Predicate >
void GetMatchingVariables (Predicate pred, std::vector< Symbol * > *matches) const
 
bool AddType (const char *name, const Type *type, SourcePos pos)
 
const TypeLookupType (const char *name) const
 
bool ContainsType (const Type *type) const
 
std::vector< std::string > ClosestVariableOrFunctionMatch (const char *name) const
 
std::vector< std::string > ClosestTypeMatch (const char *name) const
 
std::vector< std::string > ClosestEnumTypeMatch (const char *name) const
 
void Print ()
 
SymbolRandomSymbol ()
 
const TypeRandomType ()
 

Private Types

typedef std::map< std::string, Symbol * > SymbolMapType
 
typedef std::map< std::string, std::vector< Symbol * > > FunctionMapType
 
typedef std::map< std::string, const Type * > TypeMapType
 

Private Member Functions

std::vector< std::string > closestTypeMatch (const char *str, bool structsVsEnums) const
 

Private Attributes

std::vector< SymbolMapType * > variables
 
std::vector< SymbolMapType * > freeSymbolMaps
 
FunctionMapType functions
 
TypeMapType types
 

Detailed Description

Symbol table that holds all known symbols during parsing and compilation.

A single instance of a SymbolTable is stored in the Module class (Module::symbolTable); it is created in the Module::Module() constructor. It is then accessed via the global variable Module *m throughout the ispc implementation.

Definition at line 119 of file sym.h.

Member Typedef Documentation

typedef std::map<std::string, std::vector<Symbol *> > SymbolTable::FunctionMapType
private

Function declarations are not scoped. (C99, for example, allows an implementation to maintain function declarations in a single namespace.) A STL vector is used to store the function symbols for a given name since, due to function overloading, a name can have multiple function symbols associated with it.

Definition at line 276 of file sym.h.

typedef std::map<std::string, Symbol *> SymbolTable::SymbolMapType
private

This member variable holds one SymbolMap for each of the current active scopes as the program is being parsed. New maps are added and removed from the end of the main vector, so searches for symbols start looking at the end of variables and work backwards.

Definition at line 266 of file sym.h.

typedef std::map<std::string, const Type *> SymbolTable::TypeMapType
private

Type definitions can't currently be scoped.

Definition at line 281 of file sym.h.

Constructor & Destructor Documentation

SymbolTable::SymbolTable ( )

Definition at line 62 of file sym.cpp.

References PushScope().

SymbolTable::~SymbolTable ( )

Definition at line 67 of file sym.cpp.

References Assert, PopScope(), and variables.

Member Function Documentation

bool SymbolTable::AddFunction ( Symbol symbol)

Adds the given function symbol to the symbol table.

Parameters
symbolThe function symbol to be added.
Returns
true if the symbol has been added. False if another function symbol with the same name and function signature is already present in the symbol table.

Definition at line 147 of file sym.cpp.

References Assert, functions, LookupFunction(), Symbol::name, and Symbol::type.

Referenced by Module::AddFunctionDeclaration(), lCreateISPCSymbol(), and lCreateSymbol().

bool SymbolTable::AddType ( const char *  name,
const Type type,
SourcePos  pos 
)

Adds the named type to the symbol table. This is used for both struct definitions (where struct Foo causes type Foo to be added to the symbol table) as well as for typedefs. For structs with forward declarations ("struct Foo;") and are thus UndefinedStructTypes, this method replaces these with an actual struct definition if one is provided.

Parameters
nameName of the type to be added
typeType that name represents
posPosition in source file where the type was named
Returns
true if the named type was successfully added. False if a type with the same name has already been defined.

Definition at line 192 of file sym.cpp.

References Error(), LookupType(), and types.

Referenced by Module::AddTypeDef(), and lDeclareSizeAndPtrIntTypes().

bool SymbolTable::AddVariable ( Symbol symbol)

Adds the given variable symbol to the symbol table.

Parameters
symbolThe symbol to be added
Returns
true if successful; false if the provided symbol clashes with a symbol defined at the same scope. (Symbols may shaodow symbols in outer scopes; a warning is issued in this case, but this method still returns true.)

Definition at line 98 of file sym.cpp.

References Assert, Error(), Symbol::name, Symbol::pos, variables, and Warning().

Referenced by Module::AddGlobalVariable(), Declaration::GetVariableDeclarations(), lDefineConstantInt(), lDefineConstantIntFunc(), and lDefineProgramIndex().

std::vector< std::string > SymbolTable::ClosestEnumTypeMatch ( const char *  name) const

Definition at line 275 of file sym.cpp.

References closestTypeMatch().

std::vector< std::string > SymbolTable::ClosestTypeMatch ( const char *  name) const

This method returns zero or more strings with the names of types in the symbol table that nearly (but not exactly) match the given name.

Definition at line 269 of file sym.cpp.

References closestTypeMatch().

std::vector< std::string > SymbolTable::closestTypeMatch ( const char *  str,
bool  structsVsEnums 
) const
private

Definition at line 281 of file sym.cpp.

References StringEditDistance(), and types.

Referenced by ClosestEnumTypeMatch(), and ClosestTypeMatch().

std::vector< std::string > SymbolTable::ClosestVariableOrFunctionMatch ( const char *  name) const

This method returns zero or more strings with the names of symbols in the symbol table that nearly (but not exactly) match the given name. This is useful for issuing informative error methods when misspelled identifiers are found a programs.

Parameters
nameString to compare variable and function symbol names against.
Returns
vector of zero or more strings that approximately match name.

Definition at line 230 of file sym.cpp.

References functions, Symbol::name, StringEditDistance(), and variables.

bool SymbolTable::ContainsType ( const Type type) const

Look for a type given a pointer.

Returns
True if found, False otherwise.

Definition at line 218 of file sym.cpp.

References types.

Referenced by DeclSpecs::DeclSpecs().

template<typename Predicate >
void SymbolTable::GetMatchingFunctions ( Predicate  pred,
std::vector< Symbol * > *  matches 
) const

Returns all of the functions in the symbol table that match the given predicate.

Parameters
predA unary predicate that returns true or false, given a Symbol pointer, based on whether the symbol should be included in the returned set of matches. It can either be a function, with signature bool pred(const Symbol *s), or a unary predicate object with an bool operator()(const Symbol *) method.
matchesPointer to a vector in which to return the matching symbols.

Definition at line 287 of file sym.h.

References functions.

Referenced by lGetExportedFunctions(), Module::writeDevStub(), Module::writeDispatchHeader(), Module::writeHeader(), and Module::writeHostStub().

template<typename Predicate >
void SymbolTable::GetMatchingVariables ( Predicate  pred,
std::vector< Symbol * > *  matches 
) const

Returns all of the variable symbols in the symbol table that match the given predicate. The predicate is defined as in the GetMatchingFunctions() method.

Definition at line 303 of file sym.h.

References variables.

bool SymbolTable::LookupFunction ( const char *  name,
std::vector< Symbol * > *  matches = NULL 
)

Looks for the function or functions with the given name in the symbol name. If a function has been overloaded and multiple definitions are present for a given function name, all of them will be returned in the provided vector and it's up the the caller to resolve which one (if any) to use. Returns true if any matches were found.

Definition at line 162 of file sym.cpp.

References functions.

Referenced by AddFunction(), Module::AddFunctionDeclaration(), Module::AddFunctionDefinition(), Module::AddGlobalVariable(), FunctionEmitContext::All(), FunctionEmitContext::Any(), FunctionEmitContext::LaneMask(), lCreateBinaryOperatorCall(), FunctionEmitContext::LoadInst(), FunctionEmitContext::None(), and BinaryExpr::Optimize().

Symbol * SymbolTable::LookupFunction ( const char *  name,
const FunctionType type 
)

Looks for a function with the given name and type in the symbol table.

Returns
pointer to matching Symbol; NULL if none is found.

Definition at line 178 of file sym.cpp.

References Type::Equal(), and functions.

const Type * SymbolTable::LookupType ( const char *  name) const

Looks for a type of the given name in the symbol table.

Returns
Pointer to the Type, if found; otherwise NULL is returned.

Definition at line 209 of file sym.cpp.

References types.

Referenced by AddType().

Symbol * SymbolTable::LookupVariable ( const char *  name)

Looks for a variable with the given name in the symbol table. This method searches outward from the innermost scope to the outermost, returning the first match found.

Parameters
nameThe name of the variable to be searched for.
Returns
A pointer to the Symbol, if a match is found. NULL if no Symbol with the given name is in the symbol table.

Definition at line 131 of file sym.cpp.

References variables.

Referenced by Module::AddFunctionDeclaration(), Module::AddGlobalVariable(), ForeachUniqueStmt::ForeachUniqueStmt(), Function::Function(), and lExtractOrCheckGlobals().

void SymbolTable::PopScope ( )

For each scope started by a call to SymbolTable::PushScope(), there must be a matching call to SymbolTable::PopScope() at the end of that scope.

Definition at line 90 of file sym.cpp.

References Assert, freeSymbolMaps, and variables.

Referenced by ~SymbolTable().

void SymbolTable::Print ( )

Prints out the entire contents of the symbol table to standard error. (Debugging method).

Definition at line 313 of file sym.cpp.

References functions, Type::GetString(), Symbol::name, Symbol::type, types, and variables.

void SymbolTable::PushScope ( )

The parser calls this method when it enters a new scope in the program; this allows us to track variables that shadows others in outer scopes with same name as well as to efficiently discard all of the variables declared in a particular scope when we exit that scope.

Definition at line 75 of file sym.cpp.

References freeSymbolMaps, and variables.

Referenced by SymbolTable().

Symbol * SymbolTable::RandomSymbol ( )

Returns a random symbol from the symbol table. (It is not guaranteed that it is equally likely to return all symbols).

Definition at line 361 of file sym.cpp.

References Assert, ispcRand(), and variables.

const Type * SymbolTable::RandomType ( )

Returns a random type from the symbol table.

Definition at line 376 of file sym.cpp.

References Assert, and types.

Member Data Documentation

std::vector<SymbolMapType *> SymbolTable::freeSymbolMaps
private

Definition at line 269 of file sym.h.

Referenced by PopScope(), and PushScope().

FunctionMapType SymbolTable::functions
private
TypeMapType SymbolTable::types
private

Definition at line 282 of file sym.h.

Referenced by AddType(), closestTypeMatch(), ContainsType(), LookupType(), Print(), and RandomType().

std::vector<SymbolMapType *> SymbolTable::variables
private

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