/// <returns>A SwumDataRecord containing <paramref name="swumNode"/> and various data extracted from it.</returns>
        protected SwumDataRecord ProcessSwumNode(FieldDeclarationNode swumNode)
        {
            var record = new SwumDataRecord();

            record.SwumNode = swumNode;
            return(record);
        }
        protected void AddSwumForFieldDefinitions(XElement file, string fileName)
        {
            //compute SWUM on each field
            foreach (var fieldDecl in (from declStmt in file.Descendants(SRC.DeclarationStatement)
                                       where !declStmt.Ancestors().Any(n => functionTypes.Contains(n.Name))
                                       select declStmt.Element(SRC.Declaration)))
            {
                int declPos = 1;
                foreach (var nameElement in fieldDecl.Elements(SRC.Name))
                {
                    string fieldName = nameElement.Elements(SRC.Name).Any() ? nameElement.Elements(SRC.Name).Last().Value : nameElement.Value;

                    FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl));
                    builder.ApplyRules(fdn);
                    //var signature = string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos);
                    var signature = nameElement.GetXPath(false);
                    var swumData  = ProcessSwumNode(fdn);
                    swumData.FileNames.Add(fileName);
                    lock (signaturesToSwum)
                    {
                        signaturesToSwum[signature] = swumData;
                    }
                    declPos++;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// If <paramref name="node"/> is a <see cref="FieldDeclarationNode"/>, this method selects
        /// all <see cref="VariableDeclaratorNode"/> children of <paramref name="node"/>. Otherwise,
        /// this method returns a collection containing <paramref name="node"/> itself.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <returns>
        /// If <paramref name="node"/> is a <see cref="FieldDeclarationNode"/>, this method returns
        /// <see cref="FieldDeclarationNode.VariableDeclarators"/>. Otherwise, this method returns
        /// a collection containing <paramref name="node"/> itself.
        /// </returns>
        private static IEnumerable <ParseTreeNode> SelectDeclaratorsFromFields(ParseTreeNode node)
        {
            FieldDeclarationNode fieldDeclarationNode = node as FieldDeclarationNode;

            if (fieldDeclarationNode == null)
            {
                return(Enumerable.Repeat(node, 1));
            }

            return(fieldDeclarationNode.VariableDeclarators);
        }
Beispiel #4
0
        public void TestConstructSwum()
        {
            string testSrcML = "<class>class <name>foo</name> <block>{<private type=\"default\"><decl_stmt><decl><type><name>int</name></type> <name>a</name></decl>;</decl_stmt></private>}</block>;</class>";
            XElement xml = XElement.Parse(string.Format(srcMLFormat, testSrcML), LoadOptions.PreserveWhitespace);
            FieldContext fc = ContextBuilder.BuildFieldContext(xml.Descendants(SRC.Declaration).First());

            FieldDeclarationNode fdn = new FieldDeclarationNode("a", fc);
            FieldRule rule = new FieldRule(posData, tagger, splitter);
            rule.ConstructSwum(fdn);
            Console.WriteLine(fdn.ToString());
        }
        bool IParseNodeValidator.Validate(ParseNode node, CompilerOptions options, IErrorHandler errorHandler)
        {
            FieldDeclarationNode fieldNode = (FieldDeclarationNode)node;

            if (fieldNode.Initializers.Count > 1)
            {
                errorHandler.ReportError("Field declarations are limited to a single field per declaration.",
                                         fieldNode.Token.Location);
                return(false);
            }

            return(true);
        }
Beispiel #6
0
        bool IParseNodeValidator.Validate(ParseNode node, CompilerOptions options, IErrorHandler errorHandler)
        {
            FieldDeclarationNode fieldNode = (FieldDeclarationNode)node;

            if (fieldNode.Initializers.Count > 1)
            {
                errorHandler.ReportNodeValidationError(DSharpStringResources.DUPLICATE_FIELD_DECLARATION, fieldNode);

                return(false);
            }

            return(true);
        }
Beispiel #7
0
        public SymbolImplementation BuildField(FieldSymbol fieldSymbol)
        {
            rootScope    = new SymbolScope((ISymbolTable)fieldSymbol.Parent);
            currentScope = rootScope;

            Expression initializerExpression = null;

            FieldDeclarationNode fieldDeclarationNode = (FieldDeclarationNode)fieldSymbol.ParseContext;

            Debug.Assert(fieldDeclarationNode != null);

            VariableInitializerNode initializerNode = (VariableInitializerNode)fieldDeclarationNode.Initializers[0];

            if (initializerNode.Value != null)
            {
                ExpressionBuilder expressionBuilder = new ExpressionBuilder(this, fieldSymbol, errorHandler, options);
                initializerExpression = expressionBuilder.BuildExpression(initializerNode.Value);

                if (initializerExpression is MemberExpression)
                {
                    initializerExpression =
                        expressionBuilder.TransformMemberExpression((MemberExpression)initializerExpression);
                }
            }
            else
            {
                TypeSymbol fieldType = fieldSymbol.AssociatedType;
                SymbolSet  symbolSet = fieldSymbol.SymbolSet;

                if (GetDefaultValue(fieldType, symbolSet) is object defaultValue)
                {
                    initializerExpression =
                        new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                              defaultValue);
                    fieldSymbol.SetImplementationState(hasInitializer: true);
                }
            }

            if (initializerExpression != null)
            {
                List <Statement> statements = new List <Statement>();
                statements.Add(new ExpressionStatement(initializerExpression, /* isFragment */ true));

                return(new SymbolImplementation(statements, null, "this"));
            }

            return(null);
        }
Beispiel #8
0
 /// <summary>
 /// Constructs the Software Word Use Model on the given node, using this Rule.
 /// </summary>
 /// <param name="node">The node to construct the SWUM on.</param>
 public override void ConstructSwum(ProgramElementNode node)
 {
     if (node is FieldDeclarationNode)
     {
         FieldDeclarationNode fdn = node as FieldDeclarationNode;
         fdn.Parse(this.Splitter);
         this.PosTagger.TagNounPhrase(fdn.ParsedName);
         fdn.AssignStructuralInformation(this.Splitter, this.PosTagger);
         //TODO: set fdn.Type.IsPrimitive
         fdn.SwumRuleUsed = this;
     }
     else
     {
         //TODO: return some sort of error indicator?
         Console.Error.WriteLine("FieldRule.ConstructSwum expected a FieldDeclarationNode, received a {0}", node.GetType());
     }
 }
Beispiel #9
0
        private FieldSymbol BuildField(FieldDeclarationNode fieldNode, TypeSymbol typeSymbol)
        {
            TypeSymbol fieldType = typeSymbol.SymbolSet.ResolveType(fieldNode.Type, _symbolTable, typeSymbol);

            Debug.Assert(fieldType != null);

            if (fieldType != null)
            {
                FieldSymbol symbol = new FieldSymbol(fieldNode.Name, typeSymbol, fieldType);
                BuildMemberDetails(symbol, typeSymbol, fieldNode, fieldNode.Attributes);

                if (fieldNode.Initializers.Count != 0)
                {
                    VariableInitializerNode initializer = (VariableInitializerNode)fieldNode.Initializers[0];

                    if ((initializer.Value != null) && (initializer.Value.NodeType != ParseNodeType.Literal))
                    {
                        symbol.SetImplementationState(/* hasInitializer */ true);
                    }
                }

                if (fieldNode.NodeType == ParseNodeType.ConstFieldDeclaration)
                {
                    Debug.Assert(fieldNode.Initializers.Count == 1);

                    VariableInitializerNode initializer = (VariableInitializerNode)fieldNode.Initializers[0];
                    if ((initializer.Value != null) && (initializer.Value.NodeType == ParseNodeType.Literal))
                    {
                        symbol.SetConstant();
                        symbol.Value = ((LiteralToken)initializer.Value.Token).LiteralValue;
                    }

                    // TODO: Handle other constant cases that can be evaluated at compile
                    //       time (eg. combining enum flags)
                }

                return(symbol);
            }

            return(null);
        }
Beispiel #10
0
        public SymbolImplementation BuildField(FieldSymbol fieldSymbol)
        {
            rootScope    = new SymbolScope((ISymbolTable)fieldSymbol.Parent);
            currentScope = rootScope;

            Expression initializerExpression = null;

            FieldDeclarationNode fieldDeclarationNode = (FieldDeclarationNode)fieldSymbol.ParseContext;

            Debug.Assert(fieldDeclarationNode != null);

            VariableInitializerNode initializerNode = (VariableInitializerNode)fieldDeclarationNode.Initializers[0];

            if (initializerNode.Value != null)
            {
                ExpressionBuilder expressionBuilder = new ExpressionBuilder(this, fieldSymbol, errorHandler, options);
                initializerExpression = expressionBuilder.BuildExpression(initializerNode.Value);

                if (initializerExpression is MemberExpression)
                {
                    initializerExpression =
                        expressionBuilder.TransformMemberExpression((MemberExpression)initializerExpression);
                }
            }
            else
            {
                object defaultValue = null;

                TypeSymbol fieldType = fieldSymbol.AssociatedType;
                SymbolSet  symbolSet = fieldSymbol.SymbolSet;

                if (fieldType.Type == SymbolType.Enumeration)
                {
                    // The default for named values is null, so this only applies to
                    // regular enum types

                    EnumerationSymbol enumType = (EnumerationSymbol)fieldType;

                    if (enumType.UseNamedValues == false)
                    {
                        defaultValue = 0;
                    }
                }
                else if (fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Integer) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedInteger) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Long) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedLong) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Short) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.UnsignedShort) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Byte) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.SignedByte) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Double) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Single) ||
                         fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Decimal))
                {
                    defaultValue = 0;
                }
                else if (fieldType == symbolSet.ResolveIntrinsicType(IntrinsicType.Boolean))
                {
                    defaultValue = false;
                }

                if (defaultValue != null)
                {
                    initializerExpression =
                        new LiteralExpression(symbolSet.ResolveIntrinsicType(IntrinsicType.Object),
                                              defaultValue);
                    fieldSymbol.SetImplementationState(/* hasInitializer */ true);
                }
            }

            if (initializerExpression != null)
            {
                List <Statement> statements = new List <Statement>();
                statements.Add(new ExpressionStatement(initializerExpression, /* isFragment */ true));

                return(new SymbolImplementation(statements, null, "this"));
            }

            return(null);
        }
Beispiel #11
0
        public override void Execute()
        {
            if (Pause)
            {
                Console.WriteLine("Ready to begin (press Enter)");
                Console.ReadLine();
            }

            Console.WriteLine("Using srcML file {0}", this.File);

            var builder = new UnigramSwumBuilder();

            if (!string.IsNullOrWhiteSpace(CountFile))
            {
                Console.WriteLine("Initializing SamuraiIdSplitter using word count file {0}", this.CountFile);
                builder.Splitter = new SamuraiIdSplitter(CountFile);
            }
            Console.WriteLine("SwumBuilder initialized");

            int methodCount = 0, fieldCount = 0;

            {
                SrcMLFile testFile      = new SrcMLFile(this.File);
                var       functionTypes = new XName[] { SRC.Function, SRC.Constructor, SRC.Destructor };
                foreach (XElement file in testFile.FileUnits)
                {
                    string fileName = file.Attribute("filename").Value;
                    Console.WriteLine("File {0}:", fileName);

                    //compute SWUM on each function
                    foreach (var func in (from func in file.Descendants()
                                          where functionTypes.Contains(func.Name) && !func.Ancestors(SRC.Declaration).Any()
                                          select func))
                    {
                        var nameElement = SrcMLElement.GetNameForMethod(func);
                        if (nameElement != null)
                        {
                            string funcName      = nameElement.Value;
                            string funcSignature = SrcMLElement.GetMethodSignature(func);
                            if (PrintSwum)
                            {
                                Console.WriteLine("<{0}> {1}", func.Name.LocalName, funcSignature);
                            }

                            MethodDeclarationNode mdn = new MethodDeclarationNode(funcName, ContextBuilder.BuildMethodContext(func));
                            builder.ApplyRules(mdn);
                            methodSwum[string.Format("{0}:{1}", fileName, funcSignature)] = mdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(mdn.ToString() + Environment.NewLine);
                            }

                            methodCount++;
                        }
                    }

                    //compute SWUM on each field
                    foreach (var fieldDecl in (from declStmt in file.Descendants(SRC.DeclarationStatement)
                                               where !declStmt.Ancestors().Any(n => functionTypes.Contains(n.Name))
                                               select declStmt.Element(SRC.Declaration)))
                    {
                        int declPos = 1;
                        foreach (var nameElement in fieldDecl.Elements(SRC.Name))
                        {
                            string fieldName = nameElement.Elements(SRC.Name).Any() ? nameElement.Elements(SRC.Name).Last().Value : nameElement.Value;
                            if (PrintSwum)
                            {
                                Console.WriteLine("Field: {0}, Name: {1}", fieldDecl.Value, fieldName);
                            }

                            FieldDeclarationNode fdn = new FieldDeclarationNode(fieldName, ContextBuilder.BuildFieldContext(fieldDecl));
                            builder.ApplyRules(fdn);
                            fieldSwum[string.Format("{0}:{1}:{2}", fileName, fieldDecl.Value, declPos)] = fdn;
                            if (PrintSwum)
                            {
                                Console.WriteLine(fdn.ToString() + Environment.NewLine);
                            }

                            fieldCount++;
                            declPos++;
                        }
                    }
                }
            }

            GC.Collect();

            Console.WriteLine("{0} functions analyzed", methodCount);
            Console.WriteLine("{0} functions in dictionary", methodSwum.Count);
            Console.WriteLine("{0} fields analyzed", fieldCount);
            Console.WriteLine("{0} fields in dictionary", fieldSwum.Count);

            if (Pause)
            {
                Console.WriteLine("Finished building SWUM (press Enter)");
                Console.ReadLine();
            }
        }
Beispiel #12
0
 private void WriteFieldDeclaration(FieldDeclarationNode fieldDeclarationNode)
 {
     WriteDeclaration(fieldDeclarationNode.Declaration);
     _builder.Append(fieldDeclarationNode.PeriodToken);
 }
 /// <inheritdoc/>
 public override void VisitFieldDeclarationNode(FieldDeclarationNode node)
 {
 }
Beispiel #14
0
    private static SUnit TranslateAssignment(Statement statement)
    {
        // action = "Assign"
        // define left-hand-side (lhs)
        // theme = right hand side

        var fieldRule = SetupFieldRule();

        //        var equalsSign = statement.GetDescendants<OperatorUse>()
        //                                .Where(o => o.Text.Equals("=")).First();

        //        var lhs = equalsSign.GetSiblingsBeforeSelf<VariableUse>().First();

        var assignExpression = (VariableDeclaration)statement.GetExpressions().First();
        var lhs = assignExpression.Name;


        var lhsFieldContext = new FieldContext(assignExpression.VariableType.ToString(), false, "");
        var lhsDecNode      = new FieldDeclarationNode(lhs.ToString(), lhsFieldContext);

        fieldRule.InClass(lhsDecNode);
        fieldRule.ConstructSwum(lhsDecNode);

        var        rhsString = "";
        var        rhsAction = "";
        var        rhsTheme  = "";
        Expression rhs       = new Expression();

        if (assignExpression.Initializer != null)
        {
            rhs = assignExpression.Initializer;
        }

        if (rhs is VariableUse)
        {
            var rhsFieldContext = new FieldContext(rhs.ResolveType().First().ToString(), false, "");
            var rhsDecNode      = new FieldDeclarationNode(rhs.ToString(), lhsFieldContext);
            fieldRule.InClass(rhsDecNode);
            fieldRule.ConstructSwum(rhsDecNode);
            rhsAction = "Assign";
            rhsString = rhsDecNode.ToPlainString();
        }
        else if (rhs is MethodCall)
        {
            string type = rhs.ResolveType().ToString();

            MethodContext         mc  = new MethodContext(type);
            MethodDeclarationNode mdn = new MethodDeclarationNode(rhs.ToString(), mc);

            var swumRule = SetupBaseVerbRule();
            swumRule.InClass(mdn);
            swumRule.ConstructSwum(mdn);

            rhsAction = mdn.Action.ToPlainString();
            rhsTheme  = mdn.Action.ToPlainString();
            rhsString = mdn.ToPlainString();
        }
        else
        {
            rhsString = rhs.ToString();
        }


        var sunit = new SUnit();

        sunit.type   = SUnitType.Assignment;
        sunit.action = rhsString;
        //sunit.lhs = lhsDecNode.ToPlainString();
        sunit.lhs   = lhs.ToString();
        sunit.theme = rhsString;

        return(sunit);
    }