PUMA Library Reference Manual
|
#include <Puma/Semantic.h>
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. | |
virtual void | configure (Config &c) |
Configure the semantic analysis. | |
void | error_sink (ErrorSink &e) |
Set the error output stream object. | |
void | undo (CTree *tree) |
Undo the declaration analysis of the given syntax tree node. | |
CTree * | PrivateName () |
Create a new anonymous (private) name. | |
void | save_state () |
Save the current semantic analysis state. | |
void | forget_state () |
Discard the saved semantic analysis state. | |
void | restore_state () |
Restore the saved semantic analysis state. | |
CFileInfo * | getFile () |
Get the root node of the semantic tree. | |
void | enter_local_scope () |
Enter a local scope. | |
virtual void | leave_scopes () |
Leave all scopes entered up to the current scope. | |
virtual void | enter_param_decl_clause () |
Enter a function parameter declaration clause. | |
virtual void | leave_param_decl_clause () |
Leave a function parameter declaration clause. | |
bool | in_param_decl_clause () |
Check if in the declaration of a function parameter clause. | |
void | finish_decl () |
Finish the analysis of the current declaration. | |
void | finish_declarator () |
Finish the analysis of the current declarator. | |
Public Member Functions inherited from Puma::PtrStack< CObjectInfo > | |
PtrStack (long size=8192, long incr=8192) | |
Construct a new layered stack. | |
virtual | ~PtrStack () |
Destructor. | |
void | Push (const CObjectInfo *item) |
Add a new item on the top of the current layer. | |
void | Pop () |
Remove the top item from the current layer. | |
CObjectInfo * | Top () const |
Get the top item from the current layer. | |
long | Length () const |
Get the number of items added to the current layer. | |
CObjectInfo * | Get (long n) const |
Get the n-th item on the current layer. | |
void | New () |
Create a new layer. | |
void | Forget () |
Remove the top layer and add its items to the underlying layer. | |
void | Reject () |
Remove the top layer. | |
void | Destroy () |
Remove the top layer and delete its items. | |
long | Stacks () const |
Get the number of layers. | |
void | Reset () |
Remove all layers. | |
Protected Member Functions | |
Semantic () | |
Constructor. | |
virtual | ~Semantic () |
Destructor. | |
void | common_settings (CObjectInfo *info, CTree *tree) |
Apply command settings on the given semantic object. | |
void | common_settings (CObjectInfo *info, CTree *tree, CSemDeclarator *d) |
Apply command settings on the given semantic object. | |
virtual void | Delete () |
Delete the top item on the semantic stack. | |
void | Push (CObjectInfo *info) |
Push the given semantic information object on the semantic stack. | |
bool | in_decl () const |
Check if currently analysing a declaration. | |
void | decl_begin (CSemDeclSpecs *ds) |
Begin analysing a declaration. | |
void | decl_end () |
Finish analysing a declaration. | |
void | decl_specs_begin (CT_DeclSpecSeq *dss) |
Begin analysing a declaration specifier sequence. | |
void | decl_specs_end () |
Finish analysing a declaration specifier sequence. | |
CSemDeclSpecs * | sem_decl_specs () const |
Get the semantic information for the currently analysed declaration specifier sequence. | |
CT_DeclSpecSeq * | decl_specs () const |
Get the currently analysed declaration specifier sequence. | |
virtual void | enter_scope (CStructure *scope) |
Enter the given scope. | |
virtual void | reenter_scope (CStructure *scope) |
Reenter the given scope. | |
Protected Member Functions inherited from Puma::PtrStack< CObjectInfo > | |
virtual void | Remove () |
Remove the top item of the current layer. | |
Protected Attributes | |
CSemDatabase * | _db |
Database of all semantic objects of the semantic tree. | |
CFileInfo * | _file |
Top level semantic object representing the file scope. | |
ErrorSink * | _err |
Error output stream. | |
long | _Anonymous |
Counter for generating anonymous (private) names. | |
CEnumInfo * | current_enum |
The current enumeration, if inside the definition of an enumeration. | |
CStructure * | current_fct |
The current function, if inside the definition of a function. | |
CStructure * | current_scope |
The current scope. | |
BCStack< CSemDeclSpecs *, 256 > | _sem_decl_specs |
Stack for the type analysis of declarations. | |
BCStack< CT_DeclSpecSeq *, 256 > | _decl_specs |
Stack for collecting declaration specifiers. | |
BCStack< bool, 256 > | _in_param_decl_clause |
Stack for the state of analysing a function parameter list. | |
BCStack< bool, 256 > | _in_decl |
Stack for the state of analysing a declaration. | |
|
inlineprotected |
Constructor.
|
protectedvirtual |
Destructor.
|
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.
info | The semantic information object. |
tree | The analysed syntax 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, the syntax tree node, the type, and the name.
info | The semantic information object. |
tree | The analysed syntax tree. |
d | Declarator information object. |
|
inlinevirtual |
Configure the semantic analysis.
c | The configuration settings. |
Reimplemented in Puma::CSemantic.
|
protected |
Begin analysing a declaration.
ds | The declaration specifiers. |
|
protected |
Finish analysing a declaration.
|
inlineprotected |
Get the currently analysed declaration specifier sequence.
|
inlineprotected |
Begin analysing a declaration specifier sequence.
dss | Empty object for the declaration specifiers. |
|
inlineprotected |
Finish analysing a declaration specifier sequence.
|
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.
void Puma::Semantic::enter_local_scope | ( | ) |
Enter a local scope.
Creates a new CLocalScope and pushes it on the scope stack.
|
virtual |
Enter a function parameter declaration clause.
Reimplemented in Puma::CCSemantic.
|
protectedvirtual |
Enter the given scope.
Sets the parent of the entered scope to the current scope.
scope | The scope to enter. |
Reimplemented in Puma::CCSemantic, and Puma::CSemantic.
|
inline |
Set the error output stream object.
e | The error output stream. |
|
inline |
Finish the analysis of the current declaration.
void Puma::Semantic::finish_declarator | ( | ) |
Finish the analysis of the current declarator.
|
inline |
Discard the saved semantic analysis state.
|
inline |
Get the root node of the semantic tree.
|
inlineprotected |
Check if currently analysing a declaration.
bool Puma::Semantic::in_param_decl_clause | ( | ) |
Check if in the declaration of a function parameter clause.
|
virtual |
Initialize the semantic analysis.
db | The semantic object database. |
file | The input file. |
Reimplemented in Puma::CCSemantic, and Puma::CSemantic.
|
virtual |
Leave a function parameter declaration clause.
Reimplemented in Puma::CCSemantic.
|
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.
CTree * Puma::Semantic::PrivateName | ( | ) |
Create a new anonymous (private) name.
|
protected |
Push the given semantic information object on the semantic stack.
info | The semantic object. |
|
protectedvirtual |
Reenter the given scope.
Does not reset the parent of entered scope.
scope | The scope to reenter. |
Reimplemented in Puma::CSemantic.
|
inline |
Restore the saved semantic analysis state.
|
inline |
Save the current semantic analysis state.
|
inlineprotected |
Get the semantic information for the currently analysed declaration specifier sequence.
void Puma::Semantic::undo | ( | CTree * | tree | ) |
Undo the declaration analysis of the given syntax tree node.
tree | The syntax tree node. |
|
protected |
Counter for generating anonymous (private) names.
|
protected |
Database of all semantic objects of the semantic tree.
|
protected |
Stack for collecting declaration specifiers.
|
protected |
Error output stream.
|
protected |
Top level semantic object representing the file scope.
|
protected |
Stack for the state of analysing a declaration.
|
protected |
Stack for the state of analysing a function parameter list.
|
protected |
Stack for the type analysis of declarations.
|
protected |
The current enumeration, if inside the definition of an enumeration.
|
protected |
The current function, if inside the definition of a function.
|
protected |
The current scope.