Exemple #1
0
        public override void ExitFileVariableWithPropertyBlock([NotNull] SBP.FileVariableWithPropertyBlockContext context)
        {
            var type = this.PopType("ExitFileVariableWithPropertyBlock");
            var name = m_varName;

            var element = new FileElement(this.TopElement, m_elementStartLine, ScriptData.FileElementType.FileVariable, m_varName);

            element.ReturnTypeData = type;
            element.Modifiers      = m_modifiers;
            this.TopElement.Childs.Add(element);
        }
Exemple #2
0
        public override void ExitFileVariableWithPropertyBlock([NotNull] SBP.FileVariableWithPropertyBlockContext context)
        {
            TypeReference type = m_variableType;

            if (type == null)
            {
                return;
            }
            var props = m_lastElementPropertyBlock;
            VariableContainerAction createAction = null;
            VariableContainerAction initAction   = null;
            VariableContainerAction resetAction  = null;

            if (m_variableType.Type.IsValueType || m_variableType.Type == typeof(string))
            {
                var code = Expression.Constant(Activator.CreateInstance(type.Type), type.Type);
                createAction = CreateVariableContainerValueAssignAction(code);
                resetAction  = createAction;
            }
            else
            {
                #region Creator Action
                var ctor = type.Type.GetConstructor(new Type[] { });
                if (ctor != null)
                {
                    var code = Expression.New(ctor);
                    createAction = CreateVariableContainerValueAssignAction(code);
                }
                else
                {
                    var createMethods =
                        type.Type.GetMethods().Where(
                            m => String.Equals(m.Name, "Create", StringComparison.InvariantCulture) && m.IsStatic).ToArray();
                    throw new NotImplementedException();
                }
                #endregion
                #region Reset Action
                var resettable = type.Type.GetInterface(nameof(IResettable));
                if (resettable != null)
                {
                    LabelTarget returnLabel = Expression.Label(typeof(bool));

                    var parameterContainer = Expression.Parameter(typeof(IValueContainerOwnerAccess), "container");
                    var parameterLogger    = Expression.Parameter(typeof(ILogger), "logger");

                    var callSetValue = Expression.Call(
                        typeof(ExecutionHelperMethods).GetMethod(nameof(ExecutionHelperMethods.ResetFileVariable), new Type[] { typeof(IValueContainerOwnerAccess), typeof(ILogger) }),
                        parameterContainer,
                        parameterLogger);

                    var lambdaExpr = Expression.Lambda(
                        typeof(VariableContainerAction),
                        Expression.Block(
                            callSetValue,
                            Expression.Label(returnLabel, Expression.Constant(true))),
                        parameterContainer,
                        parameterLogger);

                    var @delegate = lambdaExpr.Compile();
                    resetAction = (VariableContainerAction)@delegate;
                }
                #endregion
            }

            PropertyBlock customProperties = null;
            if (props != null && props.Count > 0)
            {
                initAction = this.CreateVariableContainerObjectInitAction(type.Type, props, m_errors, context.Start);
                if (props.Count(e => e.Tag == null) > 0)
                {
                    customProperties = new PropertyBlock(context.Start.Line);
                    customProperties.AddRange(props.Where(e => e.Tag == null));
                }
            }

            var codeHash = context.GetText().GetHashCode();
            var id       = m_file.CreateOrGetFileVariable(
                m_currentNamespace, m_fileElementModifier, m_variableName, type, true,
                context.Start.Line, context.Start.Column, codeHash,
                resetter: resetAction,
                creator: createAction,
                initializer: initAction,
                customSetupData: customProperties);
            m_file.SetFileVariableModifier(id, m_fileElementModifier);

            m_variableName = null;
        }
Exemple #3
0
 public override void EnterFileVariableWithPropertyBlock([NotNull] SBP.FileVariableWithPropertyBlockContext context)
 {
     m_elementStartLine = context.Start.Line;
 }
Exemple #4
0
 public override void EnterFileVariableWithPropertyBlock([NotNull] SBP.FileVariableWithPropertyBlockContext context)
 {
     m_variableModifier = VariableModifier.Static;
 }