PUMA Library Reference Manual
Loading...
Searching...
No Matches
Puma::Semantic Class Reference

#include <Puma/Semantic.h>

+ Inheritance diagram for Puma::Semantic:

Description

Semantic analysis base class.

Implements the basic semantic analysis tasks. To be derived to implement language specific semantic analyses.

There are two kinds of semantic analyses in Puma. The first is the semantic analysis performed in parallel with the syntactic analysis (see Syntax) of the source code to differentiate ambigous syntactic constructs, resolve names, and so on. The other is an additional semantic analysis of the syntax tree (mainly of expressions) based on a tree visitor (see CVisitor) to resolve function calls, calculate the value of constant expressions, resolve the type of an expression, and so on. This class performs the first kind of semantic analysis.

The semantic object is organized as a multi-level stack. If a grammar rule is parsed then a new stack level is created. The semantic objects created while parsing the rule are pushed on this level of the stack. When parsing the grammar rule is finished, then the current stack level is discarded. If the grammar rule could not be parsed successfully then the semantic objects pushed on the stack are destroyed before the current stack level is discarded.

Public Member Functions

virtual void init (CSemDatabase &db, Unit &file)
 Initialize the semantic analysis. More...
 
virtual void configure (Config &c)
 Configure the semantic analysis. More...
 
void error_sink (ErrorSink &e)
 Set the error output stream object. More...
 
void undo (CTree *tree)
 Undo the declaration analysis of the given syntax tree node. More...
 
CTreePrivateName ()
 Create a new anonymous (private) name. More...
 
void save_state ()
 Save the current semantic analysis state. More...
 
void forget_state ()
 Discard the saved semantic analysis state. More...
 
void restore_state ()
 Restore the saved semantic analysis state. More...
 
CFileInfogetFile ()
 Get the root node of the semantic tree. More...
 
void enter_local_scope ()
 Enter a local scope. More...
 
virtual void leave_scopes ()
 Leave all scopes entered up to the current scope. More...
 
virtual void enter_param_decl_clause ()
 Enter a function parameter declaration clause. More...
 
virtual void leave_param_decl_clause ()
 Leave a function parameter declaration clause. More...
 
bool in_param_decl_clause ()
 Check if in the declaration of a function parameter clause. More...
 
void finish_decl ()
 Finish the analysis of the current declaration. More...
 
void finish_declarator ()
 Finish the analysis of the current declarator. More...
 
- Public Member Functions inherited from Puma::PtrStack< CObjectInfo >
 PtrStack (long size=8192, long incr=8192)
 Construct a new layered stack. More...
 
virtual ~PtrStack ()
 Destructor. More...
 
void Push (const CObjectInfo *item)
 Add a new item on the top of the current layer. More...
 
void Pop ()
 Remove the top item from the current layer. More...
 
CObjectInfoTop () const
 Get the top item from the current layer. More...
 
long Length () const
 Get the number of items added to the current layer. More...
 
CObjectInfoGet (long n) const
 Get the n-th item on the current layer. More...
 
void New ()
 Create a new layer. More...
 
void Forget ()
 Remove the top layer and add its items to the underlying layer. More...
 
void Reject ()
 Remove the top layer. More...
 
void Destroy ()
 Remove the top layer and delete its items. More...
 
long Stacks () const
 Get the number of layers. More...
 
void Reset ()
 Remove all layers. More...
 

Protected Member Functions

 Semantic ()
 Constructor. More...
 
virtual ~Semantic ()
 Destructor. More...
 
void common_settings (CObjectInfo *info, CTree *tree)
 Apply command settings on the given semantic object. More...
 
void common_settings (CObjectInfo *info, CTree *tree, CSemDeclarator *d)
 Apply command settings on the given semantic object. More...
 
virtual void Delete ()
 Delete the top item on the semantic stack. More...
 
void Push (CObjectInfo *info)
 Push the given semantic information object on the semantic stack. More...
 
bool in_decl () const
 Check if currently analysing a declaration. More...
 
void decl_begin (CSemDeclSpecs *ds)
 Begin analysing a declaration. More...
 
void decl_end ()
 Finish analysing a declaration. More...
 
void decl_specs_begin (CT_DeclSpecSeq *dss)
 Begin analysing a declaration specifier sequence. More...
 
void decl_specs_end ()
 Finish analysing a declaration specifier sequence. More...
 
CSemDeclSpecssem_decl_specs () const
 Get the semantic information for the currently analysed declaration specifier sequence. More...
 
CT_DeclSpecSeqdecl_specs () const
 Get the currently analysed declaration specifier sequence. More...
 
virtual void enter_scope (CStructure *scope)
 Enter the given scope. More...
 
virtual void reenter_scope (CStructure *scope)
 Reenter the given scope. More...
 
- Protected Member Functions inherited from Puma::PtrStack< CObjectInfo >
virtual void Remove ()
 Remove the top item of the current layer. More...
 

Protected Attributes

CSemDatabase_db
 Database of all semantic objects of the semantic tree. More...
 
CFileInfo_file
 Top level semantic object representing the file scope. More...
 
ErrorSink_err
 Error output stream. More...
 
long _Anonymous
 Counter for generating anonymous (private) names. More...
 
CEnumInfocurrent_enum
 The current enumeration, if inside the definition of an enumeration. More...
 
CStructurecurrent_fct
 The current function, if inside the definition of a function. More...
 
CStructurecurrent_scope
 The current scope. More...
 
BCStack< CSemDeclSpecs *, 256 > _sem_decl_specs
 Stack for the type analysis of declarations. More...
 
BCStack< CT_DeclSpecSeq *, 256 > _decl_specs
 Stack for collecting declaration specifiers. More...
 
BCStack< bool, 256 > _in_param_decl_clause
 Stack for the state of analysing a function parameter list. More...
 
BCStack< bool, 256 > _in_decl
 Stack for the state of analysing a declaration. More...
 

Constructor & Destructor Documentation

◆ Semantic()

Puma::Semantic::Semantic ( )
inlineprotected

Constructor.

◆ ~Semantic()

virtual Puma::Semantic::~Semantic ( )
protectedvirtual

Destructor.

Member Function Documentation

◆ common_settings() [1/2]

void Puma::Semantic::common_settings ( CObjectInfo info,
CTree tree 
)
protected

Apply command settings on the given semantic object.

That is setting the semantic database, the source file, the start token of the analysed syntactic construct, and the syntax tree node.

Parameters
infoThe semantic information object.
treeThe analysed syntax tree.

◆ common_settings() [2/2]

void Puma::Semantic::common_settings ( CObjectInfo info,
CTree tree,
CSemDeclarator d 
)
protected

Apply command settings on the given semantic object.

That is setting the semantic database, the source file, the start token of the analysed syntactic construct, the syntax tree node, the type, and the name.

Parameters
infoThe semantic information object.
treeThe analysed syntax tree.
dDeclarator information object.

◆ configure()

virtual void Puma::Semantic::configure ( Config c)
inlinevirtual

Configure the semantic analysis.

Parameters
cThe configuration settings.

Reimplemented in Puma::CSemantic.

◆ decl_begin()

void Puma::Semantic::decl_begin ( CSemDeclSpecs ds)
protected

Begin analysing a declaration.

Parameters
dsThe declaration specifiers.

◆ decl_end()

void Puma::Semantic::decl_end ( )
protected

Finish analysing a declaration.

◆ decl_specs()

CT_DeclSpecSeq * Puma::Semantic::decl_specs ( ) const
inlineprotected

Get the currently analysed declaration specifier sequence.

◆ decl_specs_begin()

void Puma::Semantic::decl_specs_begin ( CT_DeclSpecSeq dss)
inlineprotected

Begin analysing a declaration specifier sequence.

Parameters
dssEmpty object for the declaration specifiers.

◆ decl_specs_end()

void Puma::Semantic::decl_specs_end ( )
inlineprotected

Finish analysing a declaration specifier sequence.

◆ Delete()

virtual void Puma::Semantic::Delete ( )
protectedvirtual

Delete the top item on the semantic stack.

To be implemented by derived classes.

Reimplemented from Puma::PtrStack< CObjectInfo >.

Reimplemented in Puma::CCSemantic, and Puma::CSemantic.

◆ enter_local_scope()

void Puma::Semantic::enter_local_scope ( )

Enter a local scope.

Creates a new CLocalScope and pushes it on the scope stack.

◆ enter_param_decl_clause()

virtual void Puma::Semantic::enter_param_decl_clause ( )
virtual

Enter a function parameter declaration clause.

Reimplemented in Puma::CCSemantic.

◆ enter_scope()

virtual void Puma::Semantic::enter_scope ( CStructure scope)
protectedvirtual

Enter the given scope.

Sets the parent of the entered scope to the current scope.

Parameters
scopeThe scope to enter.

Reimplemented in Puma::CCSemantic, and Puma::CSemantic.

◆ error_sink()

void Puma::Semantic::error_sink ( ErrorSink e)
inline

Set the error output stream object.

Parameters
eThe error output stream.

◆ finish_decl()

void Puma::Semantic::finish_decl ( )
inline

Finish the analysis of the current declaration.

◆ finish_declarator()

void Puma::Semantic::finish_declarator ( )

Finish the analysis of the current declarator.

◆ forget_state()

void Puma::Semantic::forget_state ( )
inline

Discard the saved semantic analysis state.

◆ getFile()

CFileInfo * Puma::Semantic::getFile ( )
inline

Get the root node of the semantic tree.

◆ in_decl()

bool Puma::Semantic::in_decl ( ) const
inlineprotected

Check if currently analysing a declaration.

◆ in_param_decl_clause()

bool Puma::Semantic::in_param_decl_clause ( )

Check if in the declaration of a function parameter clause.

◆ init()

virtual void Puma::Semantic::init ( CSemDatabase db,
Unit file 
)
virtual

Initialize the semantic analysis.

Parameters
dbThe semantic object database.
fileThe input file.

Reimplemented in Puma::CCSemantic, and Puma::CSemantic.

◆ leave_param_decl_clause()

virtual void Puma::Semantic::leave_param_decl_clause ( )
virtual

Leave a function parameter declaration clause.

Reimplemented in Puma::CCSemantic.

◆ leave_scopes()

virtual void Puma::Semantic::leave_scopes ( )
virtual

Leave all scopes entered up to the current scope.

Make the parent scope of the current scope to the current scope.

Reimplemented in Puma::CSemantic.

◆ PrivateName()

CTree * Puma::Semantic::PrivateName ( )

Create a new anonymous (private) name.

Returns
A newly allocated node of type CT_PrivateName. Has to be destroyed by the caller.

◆ Push()

void Puma::Semantic::Push ( CObjectInfo info)
protected

Push the given semantic information object on the semantic stack.

Parameters
infoThe semantic object.

◆ reenter_scope()

virtual void Puma::Semantic::reenter_scope ( CStructure scope)
protectedvirtual

Reenter the given scope.

Does not reset the parent of entered scope.

Parameters
scopeThe scope to reenter.

Reimplemented in Puma::CSemantic.

◆ restore_state()

void Puma::Semantic::restore_state ( )
inline

Restore the saved semantic analysis state.

◆ save_state()

void Puma::Semantic::save_state ( )
inline

Save the current semantic analysis state.

◆ sem_decl_specs()

CSemDeclSpecs * Puma::Semantic::sem_decl_specs ( ) const
inlineprotected

Get the semantic information for the currently analysed declaration specifier sequence.

◆ undo()

void Puma::Semantic::undo ( CTree tree)

Undo the declaration analysis of the given syntax tree node.

Parameters
treeThe syntax tree node.

Member Data Documentation

◆ _Anonymous

long Puma::Semantic::_Anonymous
protected

Counter for generating anonymous (private) names.

◆ _db

CSemDatabase* Puma::Semantic::_db
protected

Database of all semantic objects of the semantic tree.

◆ _decl_specs

BCStack<CT_DeclSpecSeq*, 256> Puma::Semantic::_decl_specs
protected

Stack for collecting declaration specifiers.

◆ _err

ErrorSink* Puma::Semantic::_err
protected

Error output stream.

◆ _file

CFileInfo* Puma::Semantic::_file
protected

Top level semantic object representing the file scope.

◆ _in_decl

BCStack<bool, 256> Puma::Semantic::_in_decl
protected

Stack for the state of analysing a declaration.

◆ _in_param_decl_clause

BCStack<bool, 256> Puma::Semantic::_in_param_decl_clause
protected

Stack for the state of analysing a function parameter list.

◆ _sem_decl_specs

BCStack<CSemDeclSpecs*, 256> Puma::Semantic::_sem_decl_specs
protected

Stack for the type analysis of declarations.

◆ current_enum

CEnumInfo* Puma::Semantic::current_enum
protected

The current enumeration, if inside the definition of an enumeration.

◆ current_fct

CStructure* Puma::Semantic::current_fct
protected

The current function, if inside the definition of a function.

◆ current_scope

CStructure* Puma::Semantic::current_scope
protected

The current scope.