Project

General

Profile

7 krennw
/**
*
* OOAS Compiler - C++ AST
*
* Copyright 2015, AIT Austrian Institute of Technology.
* All rights reserved.
*
* SEE THE "LICENSE" FILE FOR THE TERMS UNDER WHICH THIS FILE IS PROVIDED.
*
* If you modify the file please update the list of contributors below to in-
* clude your name. Please also stick to the coding convention of using TABs
* to do the basic (block-level) indentation and spaces for anything after
* that. (Enable the display of special chars and it should be pretty obvious
* what this means.) Also, remove all trailing whitespace.
*
* Contributors:
* Willibald Krenn (AIT)
* Stephan Zimmerer (AIT)
* Christoph Czurda (AIT)
*
*/
2 krennw


/*
* AstFactory.cpp
*
* Created on: Dec 23, 2013
* Author: willibald
*/

#include "AstFactory.hpp"
#include "Ast.hpp"
#include <base/Debug.hpp>
#include <base/Exceptions.hpp>
#include <ast/identifiers/Identifier.hpp>
#include <ast/identifiers/TypeIdentifier.hpp>
#include <ast/identifiers/EnumIdentifier.hpp>
#include <ast/identifiers/AttributeIdentifier.hpp>
#include <ast/identifiers/LocalVariableIdentifier.hpp>
#include <ast/identifiers/ParameterIdentifier.hpp>
#include <ast/identifiers/ExpressionVariableIdentifier.hpp>
#include <ast/identifiers/MethodIdentifier.hpp>
#include <ast/identifiers/NamedActionIdentifier.hpp>
#include <ast/identifiers/Module.hpp>
#include <ast/identifiers/IdentifierBlock.hpp>
#include <ast/identifiers/ConstantIdentifier.hpp>
#include <ast/types/IntType.hpp>
#include <ast/types/BoolType.hpp>
#include <ast/types/EnumType.hpp>
#include <ast/types/ValuedEnumType.hpp>
#include <ast/types/ListType.hpp>
#include <ast/types/TupleType.hpp>
#include <ast/types/NullType.hpp> // not sure we need this one
#include <ast/types/FunctionType.hpp>
#include <ast/types/ActionSystemType.hpp>
#include <ast/types/PointerType.hpp>
#include <ast/statements/GuardedCommand.hpp>
#include <ast/statements/Call.hpp>
#include <ast/statements/Assignment.hpp>
#include <ast/statements/SeqBlock.hpp>
#include <ast/statements/NondetBlock.hpp>
#include <ast/statements/PrioBlock.hpp>
#include <ast/statements/Skip.hpp>
#include <ast/statements/Break.hpp>
#include <ast/statements/Abort.hpp>
#include <ast/expressions/TernaryOperator.hpp>
#include <ast/expressions/BinaryOperator.hpp>
#include <ast/expressions/UnaryOperator.hpp>
#include <ast/expressions/ForallQuantifier.hpp>
#include <ast/expressions/ExistsQuantifier.hpp>
#include <ast/expressions/ListConstructor.hpp>
#include <ast/expressions/SetConstructor.hpp>
#include <ast/expressions/TupleConstructor.hpp>
#include <ast/expressions/ObjectConstructor.hpp>
#include <ast/expressions/ValueExpression.hpp>
#include <ast/expressions/IdentifierExpression.hpp>
#include <ast/expressions/TypeExpression.hpp>
#include <ast/expressions/TupleMapAccessExpression.hpp>
#include <ast/expressions/CallExpression.hpp>
#include <ast/expressions/AccessExpression.hpp>
#include <ast/identifiers/MainModule.hpp>


namespace Ast {
namespace Factory {

ActionSystemInstance* createActionSystemInstance(CallContext &context) {
return context.m_ast->createActionSystemInstance();
}

ExpressionList* createExpressionList(CallContext &context) {
return context.m_ast->createExpressionList();
}

IdentifierList* createIdentifierList(CallContext &context) {
return context.m_ast->createIdentifierList();
}

StatementList* createStatementList(CallContext &context) {
return context.m_ast->createStatementList();
}


Type* createType(CallContext &context, TypeKind t) {
return context.m_ast->createType(t);
}

Statement* createStatement(CallContext &context, StatementKind t) {
return context.m_ast->createStatement(t);
}

Identifier* createIdentifier(CallContext &context, IdentifierKind t) {
return context.m_ast->createIdentifier(t);
}

Expression* createExpression(CallContext &context, ExpressionKind t) {
return context.m_ast->createExpression(t);
}


/**
* Creates and registers a symbol table object
*/
SymbolTable* createSymbolTable(CallContext &context) {
return context.m_ast->createSymbolTable();
}

/**
* Creates and registers a parameter list object
*/
ParameterList* createParameterList(CallContext &context) {
return context.m_ast->createParameterList();
}

TypeList* createTypeList(CallContext &context) {
return context.m_ast->createTypeList();
}

ActionSystemInstanceList* createActionSystemInstanceList(CallContext &context) {
return context.m_ast->createActionSystemInstanceList();
}

bool addSymbolToTable(void* symbolTable, void* identifier) {
SymbolTable* table = (SymbolTable*) symbolTable;
Identifier* id = (Identifier*) identifier;
return table->addIdentifier(id);
}

bool addParameterToList(void* parameterList, void* parameterIdentifier) {
((ParameterList*)parameterList)->push_back((ParameterIdentifier*)parameterIdentifier);
return true;
}

bool addTypeToList(void* typeList, void* type) {
((TypeList*)typeList)->push_back((Type*)type);
return true;
}

bool addActionSystemInstanceToList(void* objectsList, void* instanceRef) {
((ActionSystemInstanceList*)objectsList)->push_back((ActionSystemInstance*)instanceRef);
return true;
}

bool addIdentifierToBlock(void* idBlock, void* idRef){
((IdentifierBlock*)idBlock)->addElement((Identifier*)idRef);
return true;
}

bool addIdentifierToList(void* idList, void* idRef) {
((IdentifierList*)idList) -> push_back((Identifier*)idRef);
return true;
}

bool addExpressionToList(void* exprList, void* exprRef) {
((ExpressionList*)exprList) -> push_back((Expression*)exprRef);
return true;
}

bool addStatementToList(void* stmtList, void* stmtRef) {
((StatementList*)stmtList) -> push_back((Statement*)stmtRef);
return true;
}


/**
* Initializers for Identifiers
*/
bool initIdentifier(void* id, std::int32_t line, std::int32_t col, const char* text,
void* scopeRef, void* typeRef)
{
((Identifier*)id)->init(line,col,text,(IScope*)scopeRef,(Type*)typeRef);
return true;
}
bool initEnumIdentifier(void* enumId, std::int32_t line, std::int32_t col, const char* text,
void* scopeRef, void* typeRef, bool haveValue, std::int32_t value)
{
((EnumIdentifier*)enumId) -> init(line, col, text, (IScope*) scopeRef, (Type*) typeRef, haveValue, value);
return true;
}
bool initConstIdentifier(void* constId, std::int32_t line, std::int32_t col, const char* text,
void* scopeRef, void* typeRef, void* valueRef)
{
((ConstantIdentifier*)constId) -> init(line,col,text,(IScope*)scopeRef, (Type*)typeRef, (Expression*)valueRef);
return true;
}
bool initAttrIdentifier(void* attrId, std::int32_t line, std::int32_t col, const char* text,
void* scopeRef, void* typeRef, void* initRef,bool isStatic, bool isControllable, bool isObservable)
{
((AttributeIdentifier*)attrId) -> init(line, col, text, (IScope*) scopeRef, (Type*)typeRef,
(Expression*) initRef, isStatic, isControllable, isObservable);
return true;
}
bool initExprVarIdentifier(void* exprVarId, std::int32_t line, std::int32_t col, const char* text,
void* scopeRef, void* typeRef, bool initialized)
{
((ExpressionVariableIdentifier*) exprVarId) -> init(line, col, text, (IScope*)scopeRef,
(Type*)typeRef, initialized);
return true;
}
bool initTypeIdentifier(void* typeId, std::int32_t line, std::int32_t col, const char* text,
void* scopeRef, void* typeRef, const char* prefix)
{
((TypeIdentifier*)typeId)->init(line,col,text,(IScope*)scopeRef,(Type*)typeRef,prefix);
return true;
}
bool initMethodIdentifier(void* methodId, std::int32_t line, std::int32_t col, const char* text,
void* scopeRef, void* typeRef, const char* prefix, void* parameterListRef, void* symbolTableRef,
void* bodyRef)
{
((MethodIdentifier*)methodId) -> init(line,col,text,(IScope*)scopeRef, (Type*)typeRef,
prefix, (ParameterList*) parameterListRef, (SymbolTable*) symbolTableRef, (Block*) bodyRef);
return true;
}

bool initModule(void* moduleId, int line, int col, const char* text, void* scopeRef,
void* typeRef, const char* prefix, void* symTabRef)
{
((Module*)moduleId) ->init(line, col, text, (IScope*)scopeRef,
(Type*) typeRef, prefix, (SymbolTable*)symTabRef);
return true;
}

bool initMainModule(void* moduleId, int line, int col, const char* text, void* scopeRef,
void* typeRef, const char* prefix, void* symTabRef, void* idListRef)
{
((MainModule*)moduleId) ->init(line, col, text, (IScope*)scopeRef,
(Type*) typeRef, prefix, (SymbolTable*)symTabRef, (IdentifierBlock*) idListRef);
return true;
}


/**
* Initializers for Types
*/
bool initIntType(void* typeId, void* identifierRef, bool anonymousType, std::int32_t low,
std::int32_t high)
{
((IntType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType, low, high);
return true;
}
bool initBoolType(void* typeId, void* identifierRef, bool anonymousType)
{
((BoolType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType);
return true;
}
bool initValuedEnumType(void* typeId, void* identifierRef, bool anonymousType, void* symTabRef,
void* intTypeRef)
{
((ValuedEnumType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType,
(SymbolTable*) symTabRef, (IntType*) intTypeRef);
return true;
}
bool initEnumType(void* typeId,void* identifierRef, bool anonymousType, void* symTabRef)
{
((EnumType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType,
(SymbolTable*) symTabRef);
return true;
}
bool initListType(void* typeId, void* identifierRef, bool anonymousType, void* innerTypeRef,
std::uint32_t maxNumberOfElements)
{
((ListType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType,
(Type*) innerTypeRef, maxNumberOfElements);
return true;
}
bool initTupleType(void* typeId, void* identifierRef, bool anonymousType, void* typeListRef)
{
((TupleType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType,
(TypeList*) typeListRef);
return true;
}
bool initFunctionType(void* typeId, void* identifierRef, bool anonymousType, void* paramTypeListRef,
void* returnTypeRef, std::int32_t functionKind, bool isPure)
{
((FunctionType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType,
(TypeList*) paramTypeListRef, (Type*)returnTypeRef, (FunctionKind) functionKind, isPure);
return true;
}
bool initActionSystemInstance(void* instance, void* typeRef, const char* name,
std::int32_t numOfCreatedObjs, void* parentInstanceRef)
{
((ActionSystemInstance*)instance) -> init( (ActionSystemType*) typeRef, name,
numOfCreatedObjs, (ActionSystemInstance*) parentInstanceRef);
return true;
}
bool initActionSystemType(void* typeId, void* identifierRef, bool anonymousType, void* baseTypeRef,
void* parentScopeRef, void* doOdBlockRef, void* symTab, void* objectsListRef,
void* derivedObjectsListRef, bool autoConstruction, bool isInSystemDescription)
{
((ActionSystemType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType,
(ActionSystemType*) baseTypeRef, (IScope*) parentScopeRef, (Block*) doOdBlockRef,
(SymbolTable*) symTab, (ActionSystemInstanceList*) objectsListRef,
(ActionSystemInstanceList*) derivedObjectsListRef, autoConstruction, isInSystemDescription);
return true;
}
bool initNullType(void* typeId, void* identifierRef, bool anonymousType)
{
((NullType*)typeId) -> init( (TypeIdentifier*) identifierRef, anonymousType);
return true;
}

bool initSkip(void* stmnt, std::int32_t line, std::int32_t col)
{
((Skip*)stmnt) -> init(line, col);
return true;
}

bool initBreak(void* stmnt, std::int32_t line, std::int32_t col)
{
((Break*)stmnt) -> init(line, col);
return true;
}

bool initAbort(void* stmnt, std::int32_t line, std::int32_t col)
{
((Abort*)stmnt) -> init(line, col);
return true;
}

bool initNondetBlock(void* block, std::int32_t line, std::int32_t col,
void* symTabRef, void* stmtListRef, void* scopeRef)
{
((NondetBlock*)block) -> init(line, col, (SymbolTable*) symTabRef,
(StatementList*) stmtListRef, (IScope*)scopeRef);
return true;
}

bool initSeqBlock(void* block, std::int32_t line, std::int32_t col,
void* symTabRef, void* stmtListRef, void* scopeRef, void* filterExprRef)
{
((SeqBlock*)block) -> init(line, col, (SymbolTable*) symTabRef,
(StatementList*) stmtListRef, (IScope*)scopeRef, (Expression*) filterExprRef);
return true;
}

bool initPrioBlock(void* block, std::int32_t line, std::int32_t col,
void* symTabRef, void* stmtListRef, void* scopeRef)
{
((PrioBlock*)block) -> init(line, col, (SymbolTable*) symTabRef,
(StatementList*) stmtListRef, (IScope*)scopeRef);
return true;
}

bool initGuardedCommand(void* stmt,std::int32_t line,std::int32_t pos,
void* scopeRef,void* guardExprRef,void* bodyRef)
{
((GuardedCommand*)stmt) -> init(line, pos, (IScope*) scopeRef, (Expression*) guardExprRef,
(Block*)bodyRef);
return true;
}

bool initAssignment(void* stmt,std::int32_t line,std::int32_t pos,void* scopeRef,
void* nonDetExprRef, void* placeExprListRef, void* valueExprListRef, void* symTabRef)
{
((Assignment*)stmt) -> init(line, pos, (IScope*) scopeRef,
(Expression*) nonDetExprRef, (ExpressionList*) placeExprListRef,
(ExpressionList*) valueExprListRef, (SymbolTable*)symTabRef);
return true;
}

bool initCall(void* stmt,std::int32_t line,std::int32_t pos,void* callExprRef)
{
((Call*)stmt) -> init(line,pos, (Expression*) callExprRef);
return true;
}



bool initTypeExpression(void* expr, std::int32_t line, std::int32_t pos,
void* typeRef, void* callTargetsIdentifierListRef, void* symbTabRef)
{
((TypeExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef);
return true;
}

bool initIdentifierExpression(void* expr, std::int32_t line, std::int32_t pos,
void* typeRef, void* callTargetsIdentifierListRef, void* symbTabRef, void* identifierRef,
bool isSelf)
{
((IdentifierExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Identifier*) identifierRef, isSelf);
return true;
}

bool initUnaryExpression(void* expr, std::int32_t line, std::int32_t pos,
void* typeRef, void* callTargetsIdentifierListRef, void* symbTabRef,
void* child)
{
((UnaryOperator*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Expression*) child);
return true;
}

bool initBinaryExpression(void* expr, std::int32_t line, std::int32_t pos,
void* typeRef, void* callTargetsIdentifierListRef, void* symbTabRef,
void* left, void* right)
{
((BinaryOperator*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Expression*) left, (Expression*)right);
return true;
}

bool initTernaryExpression(void* expr, std::int32_t line, std::int32_t pos,
void* typeRef, void* callTargetsIdentifierListRef, void* symbTabRef,
void* left, void* mid, void* right, void* defScopeRef)
{
((TernaryOperator*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Expression*) left,
(Expression*)mid, (Expression*)right, (Scope*) defScopeRef);
return true;
}

bool initIntValueExpression(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef, std::int32_t value)
{
((IntValueExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, value);
return true;
}

bool initBoolValueExpression(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,bool value)
{
((BoolValueExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, value);
return true;
}

bool initRefValueExpression(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef, void* value)
{
((RefValueExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, value);
return true;
}


bool initListConstructor(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* exprList, void* symTab, void* parentScope,
void* comprehension, bool hasComprehension)
{
((ListConstructor*)expr) ->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (ExpressionList*) exprList,
(SymbolTable*)symTab, (IScope*) parentScope,
(Expression*) comprehension, hasComprehension);
return true;
}

bool initSetConstructor(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* exprList, void* symTab, void* parentScope,
void* comprehension, bool hasComprehension)
{
((SetConstructor*)expr) ->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (ExpressionList*) exprList,
(SymbolTable*)symTab, (IScope*) parentScope,
(Expression*) comprehension, hasComprehension);
return true;
}

bool initTupleConstructor(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* exprList, void* tupleTypeId, bool isMatcher)
{
((TupleConstructor*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (ExpressionList*) exprList,
(Identifier*) tupleTypeId, isMatcher);
return true;
}

bool initAccessExpression(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* left, void* right)
{
((AccessExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Expression*) left, (Expression*) right);
return true;
}

bool initTupleMapAccessExpression(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* child, void* argument)
{
((TupleMapAccessExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Expression*) child, (Expression*) argument);
return true;
}

bool initCallExpression(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* child, void* argumentList, void* scopeRef)
{
((CallExpression*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Expression*) child, (ExpressionList*) argumentList,
(IScope*)scopeRef);
return true;
}

bool initQuantifierExpression(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* child, void* symTabRef, void* scopeRef)
{
((Quantifier*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef, (Expression*) child,
(SymbolTable*) symTabRef, (IScope*) scopeRef);
return true;
}

bool initObjectConstructor(void* expr, std::int32_t line, std::int32_t pos, void* typeRef,
void* callTargetsIdentifierListRef, void* symbTabRef,
void* instanceList, std::uint32_t currentInstance, const char* name)
{
((ObjectConstructor*)expr)->init(line,pos,(Type*)typeRef,
(IdentifierList*)callTargetsIdentifierListRef,
(SymbolTable*)symbTabRef,(ActionSystemInstanceList*) instanceList,
currentInstance, name);

return true;
}


}} // namespaces