/*! * \brief LocalsTreeItem::LocalsTreeItem * \param localItemData - a list of items.\n * 0 -> name\n * 1 -> displayName\n * 2 -> type\n * 3 -> value * \param localItemData * \param pLocalsTreeModel * \param pLocalsTreeItem */ LocalsTreeItem::LocalsTreeItem(const QVector<QVariant> &localItemData, LocalsTreeModel *pLocalsTreeModel, LocalsTreeItem *pLocalsTreeItem) : QObject(pLocalsTreeModel) { mpLocalsTreeModel = pLocalsTreeModel; mpParentLocalsTreeItem = pLocalsTreeItem; mpModelicaValue = 0; setName(localItemData[0].toString()); setDisplayName(localItemData[1].toString()); setNameStructure(""); setType(localItemData[2].toString()); /* if the item is a root item then its just a header */ if (!mpParentLocalsTreeItem) { setDisplayName(getName()); setDisplayType(getType()); setDisplayValue(localItemData[3].toString()); } else if (mpParentLocalsTreeItem == mpLocalsTreeModel->getRootLocalsTreeItem()) { /* if the item is a top level item then we need to fetch the type and value. */ setDisplayType(""); retrieveType(); setDisplayValue(""); retrieveValue(); } else { /* child node */ setDisplayName(getDisplayName()); setDisplayType(getType()); setDisplayValue(""); retrieveValue(); } setValueChanged(false); setExpanded(false); }
void declareIdList(AST_NODE* declarationNode, SymbolAttributeKind isVariableOrTypeAttribute, int ignoreArrayFirstDimSize) { AST_NODE *idNode = declarationNode->rightSibling; while (idNode) { if (detectSymbol(idNode->semantic_value.identifierSemanticValue.identifierName)) { //symbol name redefine printErrorMsg(idNode, SYMBOL_REDECLARE); } else { SymbolAttribute* varAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute)); TypeDescriptor* typeDesc = (TypeDescriptor*)malloc(sizeof(TypeDescriptor)); varAttr->attributeKind = VARIABLE_ATTRIBUTE; varAttr->attr.typeDescriptor = typeDesc; typeDesc->properties.dataType = retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName); if (typeDesc->properties.dataType == VOID_TYPE) { printErrorMsg(idNode, VOID_VARIABLE); } else { //printf("get variable %s\n", idNode->semantic_value.identifierSemanticValue.identifierName); //printf("type %d\n", retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName)); processDeclDimList(idNode, typeDesc, False); enterSymbol(idNode->semantic_value.identifierSemanticValue.identifierName, varAttr); } } idNode = idNode->rightSibling; } }
void processVariableLValue(AST_NODE* idNode) { SymbolTableEntry *entry = retrieveSymbol(idNode->semantic_value.identifierSemanticValue.identifierName); DATA_TYPE type = retrieveType(idNode->semantic_value.identifierSemanticValue.identifierName); if (entry == NULL) { printErrorMsg(idNode, SYMBOL_UNDECLARED); } else if (entry->attribute->attributeKind == FUNCTION_SIGNATURE) { printErrorMsg(idNode, IS_FUNCTION_NOT_VARIABLE); } else if (entry->attribute->attributeKind == TYPE_ATTRIBUTE) { printErrorMsg(idNode, IS_TYPE_NOT_VARIABLE); } else if (type == VOID_TYPE) { printErrorMsg(idNode, NOT_ASSIGNABLE); } }
void checkIDNode(AST_NODE *idNode){ SymbolTableEntry *entry = retrieveSymbol(idNode->semantic_value.identifierSemanticValue.identifierName); idNode->dataType = retrieveType(idNode->semantic_value.identifierSemanticValue.identifierName); if (entry == NULL) { printErrorMsg(idNode, SYMBOL_UNDECLARED); } else if (entry->attribute->attributeKind == FUNCTION_SIGNATURE) { printErrorMsg(idNode, IS_FUNCTION_NOT_VARIABLE); } else if (entry->attribute->attributeKind == TYPE_ATTRIBUTE) { printErrorMsg(idNode, IS_TYPE_NOT_VARIABLE); } else if (idNode->semantic_value.identifierSemanticValue.kind == ARRAY_ID && entry->attribute->attr.typeDescriptor->kind == SCALAR_TYPE_DESCRIPTOR) { printErrorMsg(idNode, NOT_ARRAY); } else if (idNode->semantic_value.identifierSemanticValue.kind == ARRAY_ID) { checkSubscript(idNode); } }
void processTypeNode(AST_NODE* idNodeAsType) { AST_NODE* idNode = idNodeAsType->rightSibling; SymbolAttribute* typeAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute)); typeAttr->attributeKind = TYPE_ATTRIBUTE; typeAttr->attr.typeDescriptor = (TypeDescriptor*)malloc(sizeof(TypeDescriptor)); typeAttr->attr.typeDescriptor->properties.dataType = retrieveType(idNodeAsType->semantic_value.identifierSemanticValue.identifierName); while (idNode) { if (detectSymbol(idNode->semantic_value.identifierSemanticValue.identifierName)) { //typename redefine printErrorMsg(idNode, SYMBOL_REDECLARE); } else { //push into symbol table enterSymbol(idNode->semantic_value.identifierSemanticValue.identifierName, typeAttr); } //rightSibling until NULL idNode = idNode->rightSibling; } }
void checkFunctionCall(AST_NODE* functionCallNode) { AST_NODE *idNode = functionCallNode->child; AST_NODE *firstParamNode = idNode->rightSibling; AST_NODE *paramNode = NULL; int actualParamNum = 0; int paramNum = 0; functionCallNode->dataType = ERROR_TYPE; if (strcmp(idNode->semantic_value.identifierSemanticValue.identifierName, "write") == 0) { checkWriteFunction(functionCallNode); } else if(strcmp(idNode->semantic_value.identifierSemanticValue.identifierName, "read") == 0 || strcmp(idNode->semantic_value.identifierSemanticValue.identifierName, "fread") == 0) { } else { SymbolTableEntry *entry = retrieveSymbol(idNode->semantic_value.identifierSemanticValue.identifierName); functionCallNode->dataType = retrieveType(idNode->semantic_value.identifierSemanticValue.identifierName); if (entry == NULL) { printErrorMsg(idNode, SYMBOL_UNDECLARED); } else if (entry->attribute->attributeKind != FUNCTION_SIGNATURE) { printErrorMsg(idNode, NOT_FUNCTION_NAME); } else { actualParamNum = entry->attribute->attr.functionSignature->parametersCount; if (actualParamNum > 0 && firstParamNode->nodeType == NUL_NODE) { printErrorMsg(idNode, TOO_FEW_ARGUMENTS); } else { firstParamNode = firstParamNode->child; paramNode = firstParamNode; while (firstParamNode) { firstParamNode = firstParamNode->rightSibling; ++paramNum; } if (paramNum < actualParamNum) { printErrorMsg(idNode, TOO_FEW_ARGUMENTS); } else if (paramNum > actualParamNum) { printErrorMsg(idNode, TOO_MANY_ARGUMENTS); } else { checkParameterPassing(entry->attribute->attr.functionSignature->parameterList, paramNode); } } } } }
void checkReturnStmt(AST_NODE* returnNode) { AST_NODE *exprNode = returnNode->child; switch(exprNode->nodeType){ case IDENTIFIER_NODE: { SymbolTableEntry *entry = retrieveSymbol(exprNode->semantic_value.identifierSemanticValue.identifierName); if (entry == NULL) { printErrorMsg(exprNode, SYMBOL_UNDECLARED); } else if (exprNode->semantic_value.identifierSemanticValue.kind == ARRAY_ID) { checkSubscript(exprNode); } else if (exprNode->semantic_value.identifierSemanticValue.kind == NORMAL_ID && entry->attribute->attr.typeDescriptor->kind == ARRAY_TYPE_DESCRIPTOR) { printErrorMsg(exprNode, RETURN_ARRAY); } break; } case EXPR_NODE: case CONST_VALUE_NODE: { processExprRelatedNode(exprNode); //get function node AST_NODE *funNode = returnNode; while (funNode->nodeType != DECLARATION_NODE || funNode->semantic_value.declSemanticValue.kind != FUNCTION_DECL) { funNode = funNode->parent; } if (exprNode->dataType != retrieveType(funNode->child->semantic_value.identifierSemanticValue.identifierName)) { printErrorMsg(exprNode, RETURN_TYPE_UNMATCH); } break; } default: { assert(0); } } }
void declareFunction(AST_NODE* declarationNode) { //get node AST_NODE* idNode = declarationNode->rightSibling; AST_NODE* paramListNode = idNode->rightSibling; AST_NODE* blockNode = paramListNode->rightSibling; SymbolAttribute *funcAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute)); FunctionSignature* funcSig = (FunctionSignature*)malloc(sizeof(FunctionSignature)); Parameter *param = NULL; //generate attribute funcAttr->attributeKind = FUNCTION_SIGNATURE; funcAttr->attr.functionSignature = funcSig; //generate signature funcSig->parametersCount = 0; funcSig->parameterList = NULL; funcSig->returnType = retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName); //printf("get function %s\n", idNode->semantic_value.identifierSemanticValue.identifierName); //printf("return type %d\n", retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName)); //parse parameter node AST_NODE *paramNode = paramListNode->child; while(paramNode) { //push into Parameter node AST_NODE *varTypeNode = paramNode->child; AST_NODE *varidNode = varTypeNode->rightSibling; Parameter *nextParam = param; param = (Parameter*)malloc(sizeof(Parameter)); param->type = (TypeDescriptor*)malloc(sizeof(TypeDescriptor)); param->parameterName = (char*)malloc(strlen(varidNode->semantic_value.identifierSemanticValue.identifierName+1)); strcpy(param->parameterName, varidNode->semantic_value.identifierSemanticValue.identifierName); param->next = nextParam; ++(funcSig->parametersCount); param->type->properties.dataType = retrieveType(varTypeNode->semantic_value.identifierSemanticValue.identifierName); processDeclDimList(varidNode, param->type, True); paramNode = paramNode->rightSibling; } funcSig->parameterList = param; if (detectSymbol(idNode->semantic_value.identifierSemanticValue.identifierName)) { //function name redefine printErrorMsg(idNode, SYMBOL_REDECLARE); } else { //push into symbol table enterSymbol(idNode->semantic_value.identifierSemanticValue.identifierName, funcAttr); } openScope(); //generate param symbol SymbolAttribute *paramAttr = NULL; TypeDescriptor *paramType = NULL; Parameter *paramVar = param; while(paramVar){ SymbolAttribute *paramAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute)); TypeDescriptor *paramType = (TypeDescriptor*)malloc(sizeof(TypeDescriptor)); paramAttr->attributeKind = VARIABLE_ATTRIBUTE; paramAttr->attr.typeDescriptor = paramType; memcpy(paramType, param->type, sizeof(TypeDescriptor)); if (detectSymbol(paramVar->parameterName)) { //function name redefine printErrorMsg(paramListNode, SYMBOL_REDECLARE); } else { //push into symbol table enterSymbol(paramVar->parameterName, paramAttr); } paramVar = paramVar->next; } processBlockNode(blockNode); closeScope(); }