bool Context<T>::isValueAvailable(int idx) { auto var = ensureVariable(idx); if (var->variableType() == VariableType::NDARRAY) { return var->getNDArray() != nullptr; } else if (var->variableType() == VariableType::ARRAY_LIST) { return var->getNDArrayList() != nullptr; } return false; }
void SetFieldValueAST::generateCode(CodeGenerator& codeGen, GeneratedFunction& func) { auto& checker = codeGen.typeChecker(); std::shared_ptr<ClassType> objRefType; if (auto varRef = std::dynamic_pointer_cast<VariableReferenceExpressionAST>(mObjectRefExpression)) { auto varSymbol = std::dynamic_pointer_cast<VariableSymbol>(mSymbolTable->find(varRef->name())); objRefType = std::dynamic_pointer_cast<ClassType>(checker.findType(varSymbol->variableType())); } else if (auto arrayRef = std::dynamic_pointer_cast<ArrayAccessAST>(mObjectRefExpression)) { objRefType = std::dynamic_pointer_cast<ClassType>(arrayRef->expressionType(checker)); } auto memberName = getMemberName(); if (auto arrayMember = std::dynamic_pointer_cast<ArrayAccessAST>(mMemberExpression)) { mObjectRefExpression->generateCode(codeGen, func); func.addInstruction("LDFIELD " + objRefType->vmClassName() + "::" + memberName); arrayMember->accessExpression()->generateCode(codeGen, func); mRightHandSide->generateCode(codeGen, func); func.addInstruction("STELEM " + mRightHandSide->expressionType(codeGen.typeChecker())->vmType()); } else { mObjectRefExpression->generateCode(codeGen, func); mRightHandSide->generateCode(codeGen, func); func.addInstruction("STFIELD " + objRefType->vmClassName() + "::" + memberName); } }
void MemberAccessAST::typeCheck(TypeChecker& checker) { mAccessExpression->typeCheck(checker); if (auto varRef = std::dynamic_pointer_cast<VariableReferenceExpressionAST>(mAccessExpression)) { auto varSymbol = std::dynamic_pointer_cast<VariableSymbol>(mSymbolTable->find(varRef->name())); auto varRefType = checker.findType(varSymbol->variableType()); std::string memberName = getMemberName(); std::string objName = varRefType->name(); if (std::dynamic_pointer_cast<ArrayType>(varRefType)) { objName = "Array"; } if (!checker.objectExists(objName)) { checker.typeError(varRefType->name() + " is not an object type."); } auto& object = checker.getObject(objName); if (!object.fieldExists(memberName)) { checker.typeError("There exists no field '" + memberName + "' in the type '" + varRefType->name() + "'."); } if (auto arrayMember = std::dynamic_pointer_cast<ArrayAccessAST>(mMemberExpression)) { arrayMember->accessExpression()->typeCheck(checker); } } }
void MemberCallExpressionAST::typeCheck(TypeChecker& checker) { mAccessExpression->typeCheck(checker); if (auto varRef = std::dynamic_pointer_cast<VariableReferenceExpressionAST>(mAccessExpression)) { auto varSymbol = std::dynamic_pointer_cast<VariableSymbol>(mSymbolTable->find(varRef->name())); auto varRefType = checker.findType(varSymbol->variableType()); std::string objName = varRefType->name(); if (!checker.objectExists(objName)) { checker.typeError(varRefType->name() + " is not an object type."); } auto classSymbol = std::dynamic_pointer_cast<ClassSymbol>(Helpers::findSymbolInNamespace(mSymbolTable, objName)); mMemberCallExpression->setCallTable(classSymbol->symbolTable()); mMemberCallExpression->generateSymbols(checker.binder(), mSymbolTable); mMemberCallExpression->typeCheck(checker); } else { auto varRefType = mAccessExpression->expressionType(checker); std::string objName = varRefType->name(); if (!checker.objectExists(objName)) { checker.typeError(varRefType->name() + " is not an object type."); } auto classSymbol = std::dynamic_pointer_cast<ClassSymbol>(Helpers::findSymbolInNamespace(mSymbolTable, objName)); mMemberCallExpression->setCallTable(classSymbol->symbolTable()); mMemberCallExpression->generateSymbols(checker.binder(), mSymbolTable); mMemberCallExpression->typeCheck(checker); } }
const Object& SetFieldValueAST::getObject(const TypeChecker& checker) const { std::shared_ptr<Type> objRefType; if (auto varRef = std::dynamic_pointer_cast<VariableReferenceExpressionAST>(mObjectRefExpression)) { auto varSymbol = std::dynamic_pointer_cast<VariableSymbol>(mSymbolTable->find(varRef->name())); objRefType = checker.findType(varSymbol->variableType()); } else if (auto arrayRef = std::dynamic_pointer_cast<ArrayAccessAST>(mObjectRefExpression)) { objRefType = arrayRef->expressionType(checker); } return checker.getObject(objRefType->name()); }
const Object& MemberAccessAST::getObject(const TypeChecker& checker) const { std::shared_ptr<Type> varRefType = nullptr; if (auto varRef = std::dynamic_pointer_cast<VariableReferenceExpressionAST>(mAccessExpression)) { auto varSymbol = std::dynamic_pointer_cast<VariableSymbol>(mSymbolTable->find(varRef->name())); varRefType = checker.findType(varSymbol->variableType()); } else { varRefType = mAccessExpression->expressionType(checker); } std::string objName = varRefType->name(); if (std::dynamic_pointer_cast<ArrayType>(varRefType)) { objName = "Array"; } return checker.getObject(objName); }
void SetFieldValueAST::typeCheck(TypeChecker& checker) { mObjectRefExpression->typeCheck(checker); std::shared_ptr<Type> objRefType; if (auto varRef = std::dynamic_pointer_cast<VariableReferenceExpressionAST>(mObjectRefExpression)) { auto varSymbol = std::dynamic_pointer_cast<VariableSymbol>(mSymbolTable->find(varRef->name())); objRefType = checker.findType(varSymbol->variableType()); } else if (auto arrayRef = std::dynamic_pointer_cast<ArrayAccessAST>(mObjectRefExpression)) { objRefType = arrayRef->expressionType(checker); } else { checker.typeError("Not implemented"); } auto memberName = getMemberName(); std::string objName = objRefType->name(); if (!checker.objectExists(objName)) { checker.typeError(objRefType->name() + " is not an object type."); } auto& object = checker.getObject(objName); if (!object.fieldExists(memberName)) { checker.typeError("There exists no field '" + memberName + "' in the type '" + objRefType->name() + "'."); } mRightHandSide->typeCheck(checker); //Check rhs std::shared_ptr<Type> fieldType; if (std::dynamic_pointer_cast<ArrayAccessAST>(mMemberExpression)) { fieldType = std::dynamic_pointer_cast<ArrayType>(object.getField(memberName).type())->elementType(); } else { fieldType = object.getField(memberName).type(); } checker.assertSameType( *fieldType, *mRightHandSide->expressionType(checker), asString()); }
TIntermSymbol *MakeNewTemporary(const TString &name, TBasicType type) { TType variableType(type, EbpHigh, EvqInternal); return new TIntermSymbol(-1, name, variableType); }
ProblemInstance::ProblemInstance (RPHandle hndl, SharedPtr<ReaderPluginFunctionTable> functions) : handle(hndl), ftable(functions) { int i; m_variables = ftable->variables(handle); m_constraints = ftable->constraints(handle); m_objectives = ftable->objectives(handle); m_variableTypes = new char[variables()+2*constraints()+2*objectives()]; m_functionTypes = &m_variableTypes[variables()]; m_constraintTypes = &m_functionTypes[constraints()+objectives()]; m_objectiveTypes = &m_constraintTypes[constraints()]; for (i = 0; i < variables(); ++i) { m_variableTypes[i] = ftable->variableType(handle, i); } for (i = 0; i < constraints(); ++i) { m_functionTypes[i] = ftable->functionType(handle, 'c', i); } for (i = 0; i < objectives(); ++i) { m_functionTypes[constraints()+i] = ftable->functionType(handle, 'o', i); } for (i = 0; i < constraints(); ++i) { m_constraintTypes[i] = ftable->constraintType(handle, i); } for (i = 0; i < objectives(); ++i) { m_objectiveTypes[i] = ftable->objectiveType(handle, i); } int* tmp = new int[std::max(objectives()+constraints()+2, variables())]; m_presenceIndexes = new unsigned[2*variables()+constraints()+objectives()]; for (i = 0; i < variables(); ++i) { ftable->variablePresence(handle, i, tmp, &tmp[constraints()]); m_presences.reserve(m_presences.size()+2+tmp[0]+tmp[constraints()]); varConstrPresenceIndex(i) = m_presences.size(); m_presences.insert(m_presences.end(), tmp, &tmp[tmp[0]+1]); varObjPresenceIndex(i) = m_presences.size(); m_presences.insert(m_presences.end(), &tmp[constraints()], &tmp[constraints()+tmp[constraints()]+1]); } m_variableCounts = new int[4*(constraints()+objectives())]; std::fill(m_variableCounts, &m_variableCounts[4*(constraints()+objectives())], 0); for (i = 0; i < constraints(); ++i) { int vars = ftable->constraintVariables(handle, i, tmp); m_presences.reserve(m_presences.size()+1+vars); constraintPresenceIndex(i) = m_presences.size(); m_presences.push_back(vars); m_presences.insert(m_presences.end(), tmp, &tmp[vars]); unsigned offset = 4*i; m_variableCounts[offset] = vars; for (int j = 0; j < vars; ++j) { switch (variableType(tmp[j])) { case 'r': ++m_variableCounts[offset+1]; break; case 'b': ++m_variableCounts[offset+2]; break; case 'i': ++m_variableCounts[offset+3]; break; } } } for (i = 0; i < objectives(); ++i) { int vars = ftable->objectiveVariables(handle, i, tmp); m_presences.reserve(m_presences.size()+1+vars); objectivePresenceIndex(i) = m_presences.size(); m_presences.push_back(vars); m_presences.insert(m_presences.end(), tmp, &tmp[vars]); unsigned offset = 4*(constraints()+i); m_variableCounts[offset] = vars; for (int j = 0; j < vars; ++j) { switch (variableType(tmp[j])) { case 'r': ++m_variableCounts[offset+1]; break; case 'b': ++m_variableCounts[offset+2]; break; case 'i': ++m_variableCounts[offset+3]; break; } } } delete [] tmp; m_varBounds = new Real[2*(variables()+constraints())]; m_constrBounds = &m_varBounds[2*variables()]; for (i = 0; i < variables(); ++i) { ftable->variableBounds(handle, i, &m_varBounds[2*i], &m_varBounds[2*i+1]); } for (i = 0; i < constraints(); ++i) { ftable->constraintBounds(handle, i, &m_constrBounds[2*i], &m_constrBounds[2*i+1]); switch (constraintType(i)) { case 'l': m_constrBounds[2*i] = -std::numeric_limits<Real>::infinity(); break; case 'g': m_constrBounds[2*i+1] = std::numeric_limits<Real>::infinity(); break; case 'e': m_constrBounds[2*i] = m_constrBounds[2*i+1]; break; case 'u': m_constrBounds[2*i] = -std::numeric_limits<Real>::infinity(); m_constrBounds[2*i+1] = std::numeric_limits<Real>::infinity(); break; default: break; } } latestErrorType = 0; }