public PropertyDeclarationSyntax GeneratePropertyForName(FieldInfo fieldInfo, string fieldName, string clsName = "")
        {
            var identifier = SyntaxFactory.Identifier(fieldName);

            SyntaxList <AccessorDeclarationSyntax> accessors = new SyntaxList <AccessorDeclarationSyntax>()
            {
            };

            string cvname = FieldGenerator.GetPrefixedName(fieldName);

            string hookexpr = HookTemplates.FieldUpdateHook(fieldName, clsName, "value");

            accessors = accessors.Add(SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration, GetAccessorBody(cvname)));
            accessors = accessors.Add(SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration, SetAccessorBody(cvname, hookexpr)));

            var accessList = SyntaxFactory.AccessorList(accessors);

            var outProp = SyntaxFactory.PropertyDeclaration(
                new SyntaxList <AttributeListSyntax>()
            {
            },
                fieldInfo.Modifiers,
                fieldInfo.Type,
                null,
                identifier,
                accessList
                );

            return(outProp);
        }
예제 #2
0
        public CodeRunBlockRenderingInfo CodeRunBlockRenderInfoForMethod(ClassInfo classInfo, int id)
        {
            var info       = new CodeRunBlockRenderingInfo();
            var methodInfo = classInfo.GetCodeRunnerInfo(id);

            // Initialize render block dictionaries
            foreach (int blockid in methodInfo.blockInfo.Keys)
            {
                info.renderingInfo[blockid] = new List <string>()
                {
                };
            }

            // Add OnMethodHook
            if (shouldHookOnMethod())
            {
                if (!info.renderingInfo.ContainsKey(0))
                {
                    info.renderingInfo[0] = new List <string>()
                    {
                    }
                }
                ;

                string methodStr = HookTemplates.MethodEnterHook(classInfo.RelativeFilePath, methodInfo.Name, methodInfo.IsStatic);
                info.renderingInfo[0].Add(methodStr);
            }


            // Add Field Initial Value Update Hooks
            if (methodInfo.IsConstructor && shouldHookFieldInits())
            {
                if (!info.renderingInfo.ContainsKey(0))
                {
                    info.renderingInfo[0] = new List <string>()
                    {
                    }
                }
                ;

                foreach (var finfo in classInfo.FieldInfos.Values)
                {
                    foreach (var fname in finfo.Names)
                    {
                        string hookexpr = HookTemplates.FieldUpdateHook(fname, classInfo.Name, FieldGenerator.GetPrefixedName(fname));
                        info.renderingInfo[0].Add(hookexpr);
                    }
                }
            }

            var localVarsList = new List <string>()
            {
            };

            // Hook local var declarations for method argument
            if (shouldHookLocalVarChange())
            {
                if (!info.renderingInfo.ContainsKey(0))
                {
                    info.renderingInfo[0] = new List <string>()
                    {
                    }
                }
                ;

                foreach (var arg in methodInfo.Arguments)
                {
                    if (arg.Name != "")
                    {
                        localVarsList.Add(arg.Name);
                        string localHook = HookTemplates.LocalVarUpdateHook(arg.Name, "");
                        info.renderingInfo[0].Add(localHook);
                    }
                }
            }


            // Add LineExecHooks
            foreach (int blockid in methodInfo.blockInfo.Keys)
            {
                var statInfos = methodInfo.blockInfo[blockid].StatementInfos;
                for (int i = 0; i < statInfos.Count; i++)
                {
                    var statInfo = statInfos[i];

                    if (shouldHookLineExec())
                    {
                        string likeHook = HookTemplates.LineExecHook(statInfo.LineNo, "");
                        info.renderingInfo[blockid].Add(likeHook);
                    }

                    // Add the cue for the original statement
                    info.renderingInfo[blockid].Add(i.ToString());

                    if (shouldHookLocalVarChange())
                    {
                        bool addLocalVarUpdater = false;

                        if (statInfo.IsLocalVarDeclaration)
                        {
                            localVarsList.AddRange(statInfo.LocalVarNames);
                            addLocalVarUpdater = true;
                        }

                        if (statInfo.IsLocalVarStateChanger)
                        {
                            if (localVarsList.Contains(statInfo.LocalVarNames[0]))
                            {
                                addLocalVarUpdater = true;
                            }
                        }

                        if (addLocalVarUpdater)
                        {
                            foreach (string varName in statInfo.LocalVarNames)
                            {
                                string localHook = HookTemplates.LocalVarUpdateHook(varName, "");
                                info.renderingInfo[blockid].Add(localHook);
                            }
                        }
                    }
                }

                // Add line hook for close brace of the block
                int closeLineNo = methodInfo.blockInfo[blockid].CloseBraceLineNo;
                if (closeLineNo != -1 && shouldHookLineExec())
                {
                    string closeBraceStr = HookTemplates.LineExecHook(closeLineNo, "");
                    info.renderingInfo[blockid].Add(closeBraceStr);
                }
            }

            return(info);
        }