public SBExpressionData CastProcedureAssignmentArgumentIfNeeded(TypeReference type, SBExpressionData expression) { var t = type.Type; if (!expression.IsError() && expression.DataType.Type != t && expression.DataType.Type == typeof(IProcedureReference) && t.IsConstructedGenericType && t.GenericTypeArguments.Length == 1 && t.GetGenericTypeDefinition() == typeof(IProcedureReference <>)) { var getCastMethodTyped = s_CastProcedureReference.MakeGenericMethod(t.GenericTypeArguments[0]); var casted = Expression.Call( getCastMethodTyped, m_currentProcedure.ContextReferenceInternal, expression.ExpressionCode); return(new SBExpressionData(casted)); } else { return(expression); } }
private SBExpressionData ResolveIdentifierForGetOperation(string input, bool inFunctionScope, TypeReference targetType = null) { SBExpressionData result = this.ResolveQualifiedIdentifier(input, inFunctionScope); if (result.IsError()) { return(result); } if (result != null) { Expression expression = result.ExpressionCode; var isContainer = typeof(IValueContainer).IsAssignableFrom(expression.Type); if (isContainer && (targetType == null || !targetType.Type.IsAssignableFrom(typeof(IValueContainer)))) { var datatype = expression.Type.GenericTypeArguments[0]; var getValue = Expression.Call( expression, expression.Type.GetMethod("GetTypedValue"), Expression.Constant(null, typeof(StepBro.Core.Logging.ILogger))); result = new SBExpressionData( HomeType.Immediate, SBExpressionType.Expression, (TypeReference)datatype, getValue); } return(result); } else { throw new NotImplementedException("Must be handled somewhere else!!"); } }
public override void ExitVariableInitializerExpression([NotNull] SBP.VariableInitializerExpressionContext context) { var stack = m_expressionData.PopStackLevel(); m_variableInitializer = this.ResolveForGetOperation(stack.Pop()); if (m_variableInitializer.IsError()) { m_errors.UnresolvedIdentifier(m_variableInitializer.Token.Line, m_variableInitializer.Token.Column, m_variableInitializer.Value as string); } }
public override void ExitVariableDeclarator([NotNull] SBP.VariableDeclaratorContext context) { if (m_variableType != null) { //m_variableInitializer = ResolveIfIdentifier(m_variableInitializer, m_inFunctionScope); if (m_variableInitializer.IsError()) { if (m_variableType.Type != typeof(VarSpecifiedType)) { object def = null; if (m_variableType.Type.IsValueType) { def = Activator.CreateInstance(m_variableType.Type); } m_variableInitializer = SBExpressionData.Constant(m_variableType, def); } else { // Just set the type to 'object' then. m_variableInitializer = SBExpressionData.Constant(TypeReference.TypeObject, null); } } if (m_variableInitializer.IsConstant && m_variableInitializer.Value == null) { // Convert the null value to the type of the variable if (m_variableType.Type == typeof(string)) { m_variableInitializer = SBExpressionData.Constant(TypeReference.TypeString, null); } } else if (m_variableInitializer.IsAwaitExpression) { m_variableInitializer = new SBExpressionData( this.MakeAwaitOperation( m_variableInitializer.ExpressionCode, context, true, m_variableType.Type)); } if (m_variableType.Type != typeof(VarSpecifiedType)) { if (m_variableInitializer.IsValueType && m_variableInitializer.IsConstant && m_variableInitializer.Value == null) { m_variableInitializer.NarrowGetValueType(m_variableType); } else if (m_variableType != m_variableInitializer.DataType && !m_variableType.Type.IsAssignableFrom(m_variableInitializer.DataType.Type)) { m_variableInitializer = this.CastProcedureAssignmentArgumentIfNeeded(m_variableType, m_variableInitializer); if (m_variableInitializer.DataType.Type != m_variableType.Type) { if (m_variableInitializer.DataType.Type == typeof(object) && m_variableInitializer.SuggestsAutomaticTypeConversion) { m_variableInitializer = new SBExpressionData( Expression.Convert(m_variableInitializer.ExpressionCode, m_variableType.Type)); } else { m_variableInitializer.NarrowGetValueType(); if (m_variableInitializer.DataType.Type != m_variableType.Type) { //m_errors.SymanticError(context.Start.Line, context.Start.Column, false, ""); throw new ParsingErrorException(context.Start.Line, "Data Type", "Convertion of variable initializer is not implemented."); } } } } } m_variables.Add( new NamedData <SBExpressionData>( m_variableName, m_variableInitializer)); m_variableName = null; m_variableInitializer = null; } }