Intel® Implicit SPMD Program Compiler (Intel® ISPC)  1.13.0
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 116 of file sym.h.

Member Typedef Documentation

◆ FunctionMapType

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 267 of file sym.h.

◆ SymbolMapType

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 257 of file sym.h.

◆ TypeMapType

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

Type definitions can't currently be scoped.

Definition at line 272 of file sym.h.

Constructor & Destructor Documentation

◆ SymbolTable()

SymbolTable::SymbolTable ( )

Definition at line 59 of file sym.cpp.

◆ ~SymbolTable()

SymbolTable::~SymbolTable ( )

Definition at line 61 of file sym.cpp.

References Assert.

Member Function Documentation

◆ AddFunction()

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 126 of file sym.cpp.

References Assert, Symbol::name, and Symbol::type.

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

◆ AddType()

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 165 of file sym.cpp.

References Error(), Symbol::name, and Symbol::type.

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

◆ AddVariable()

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 85 of file sym.cpp.

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

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

◆ ClosestEnumTypeMatch()

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

Definition at line 238 of file sym.cpp.

◆ 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 236 of file sym.cpp.

◆ closestTypeMatch()

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

Definition at line 242 of file sym.cpp.

References StringEditDistance().

◆ ClosestVariableOrFunctionMatch()

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 199 of file sym.cpp.

References Symbol::name, and StringEditDistance().

◆ ContainsType()

bool SymbolTable::ContainsType ( const Type type) const

Look for a type given a pointer.

Returns
True if found, False otherwise.

Definition at line 188 of file sym.cpp.

Referenced by DeclSpecs::DeclSpecs().

◆ GetMatchingFunctions()

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 277 of file sym.h.

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

◆ GetMatchingVariables()

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 291 of file sym.h.

◆ LookupFunction() [1/2]

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 139 of file sym.cpp.

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

◆ LookupFunction() [2/2]

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 153 of file sym.cpp.

References Type::Equal().

◆ LookupType()

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 180 of file sym.cpp.

◆ LookupVariable()

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 112 of file sym.cpp.

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

◆ PopScope()

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 79 of file sym.cpp.

References Assert.

◆ Print()

void SymbolTable::Print ( )

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

Definition at line 272 of file sym.cpp.

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

◆ PushScope()

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 67 of file sym.cpp.

◆ RandomSymbol()

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 315 of file sym.cpp.

References Assert, and ispcRand().

◆ RandomType()

const Type * SymbolTable::RandomType ( )

Returns a random type from the symbol table.

Definition at line 328 of file sym.cpp.

References Assert.

Member Data Documentation

◆ freeSymbolMaps

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

Definition at line 260 of file sym.h.

◆ functions

FunctionMapType SymbolTable::functions
private

Definition at line 268 of file sym.h.

◆ types

TypeMapType SymbolTable::types
private

Definition at line 273 of file sym.h.

◆ variables

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

Definition at line 258 of file sym.h.


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