示例#1
0
 /// <summary>
 /// Class struct generator
 /// </summary>
 /// <param name="context">The walker context</param>
 /// <param name="classCode">Class code</param>
 public ClassStructGenerator(WalkerContext context, ClassCodeData classCode)
 {
     m_context   = context;
     m_classCode = classCode;
 }
示例#2
0
        /// <summary>
        /// Creates class initializers, class structs and .cctors
        /// </summary>
        /// <param name="node">The class declaration</param>
        public override void Generate(ClassDeclarationSyntax node)
        {
            // Temporarily hold all the fields/properties so we can put them in the initialization method
            ClassCodeData classCode = new ClassCodeData();

            // Get fields and properties of the current class
            List <VariableDeclarationSyntax> fields     = null;
            List <PropertyDeclarationSyntax> properties = null;

            GetFieldsAndProperties(node, out fields, out properties);

            // Mark the base class as an extending class
            if (node.BaseList != null)
            {
                IEnumerable <SyntaxNode> children = node.BaseList.ChildNodes();
                foreach (SimpleBaseTypeSyntax child in children)
                {
                    ITypeSymbol typeSymbol = m_context.Model.GetTypeInfo(child.ChildNodes().First()).Type;
                    string      str        = string.Format("{0}_{1}", m_context.ConvertNameSpace(typeSymbol.ContainingNamespace), typeSymbol.Name);
                    m_context.TypeIsExtending[str] = true;
                }
            }

            // Fields
            foreach (VariableDeclarationSyntax fieldNodeChild in fields)
            {
                bool isStatic = false;
                bool isConst  = false;

                // Check if this field is a static or a const field by looping through its tokens
                IEnumerable <SyntaxToken> fieldNodeTokens = fieldNodeChild.Parent.ChildTokens();
                foreach (SyntaxToken token in fieldNodeTokens)
                {
                    SyntaxKind tokenKind = token.Kind();
                    if (tokenKind == SyntaxKind.StaticKeyword)
                    {
                        isStatic = true;
                    }
                    else if (tokenKind == SyntaxKind.ConstKeyword)
                    {
                        isConst = true;
                    }
                }

                // Loop through the variable declarators in the field
                foreach (VariableDeclaratorSyntax variable in fieldNodeChild.Variables)
                {
                    string identifier = variable.Identifier.ToString();

                    // Constant value definition
                    if (isConst)
                    {
                        m_context.Writer.CurrentDestination = WriterDestination.Defines;
                        m_context.Writer.Append(string.Format("#define const_{0}_{1}", m_context.TypeConvert.CurrentClassNameFormatted, identifier));

                        m_context.Writer.Append(" (");
                        m_context.Writer.CurrentDestination = WriterDestination.TempBuffer;
                        m_context.Generators.Expression.Generate(variable.Initializer.Value);
                        m_context.Writer.CurrentDestination = WriterDestination.Defines;
                        m_context.Writer.Append(m_context.Writer.FlushTempBuffer());
                        m_context.Writer.AppendLine(")");
                    }
                    // Regular field
                    else
                    {
                        Dictionary <string, EqualsValueClauseSyntax> dictValues = isStatic ? classCode.staticFields : classCode.nonStaticFields;
                        Dictionary <string, TypeSyntax> dictTypes = isStatic ? classCode.staticFieldTypes : classCode.nonStaticFieldTypes;

                        dictTypes.Add(identifier, fieldNodeChild.Type);
                        if (variable.Initializer != null)
                        {
                            dictValues.Add(identifier, variable.Initializer);
                        }
                    }
                }
            }

            // Properties
            foreach (PropertyDeclarationSyntax property in properties)
            {
                string identifier = property.Identifier.ToString();

                // Check if the property is static by looping through its tokens
                bool isStatic = false;
                IEnumerable <SyntaxToken> tokens = property.ChildTokens();
                foreach (SyntaxToken token in tokens)
                {
                    if (token.Kind() == SyntaxKind.StaticKeyword)
                    {
                        isStatic = true;
                        break;
                    }
                }

                Dictionary <string, EqualsValueClauseSyntax> dictValues = isStatic ? classCode.propertyInitialValuesStatic : classCode.propertyInitialValuesNonStatic;
                Dictionary <string, TypeSyntax> dictTypes = isStatic ? classCode.propertyTypesStatic : classCode.propertyTypesNonStatic;

                dictTypes.Add(identifier, property.Type);
                if (property.Initializer != null)
                {
                    dictValues.Add(identifier, property.Initializer);
                }
            }

            // Other generators
            ClassStructGenerator       structGen       = new ClassStructGenerator(m_context, classCode);
            ClassStaticStructGenerator staticStructGen = new ClassStaticStructGenerator(m_context, classCode);
            ClassInitGenerator         classInitGen    = new ClassInitGenerator(m_context, classCode);
            ClassCctorGenerator        classCctorGen   = new ClassCctorGenerator(m_context, classCode);

            m_context.Writer.CurrentDestination = WriterDestination.ClassStructs;
            structGen.Generate(node);
            m_context.Writer.CurrentDestination = WriterDestination.ClassStructs;
            staticStructGen.Generate(node);
            classInitGen.Generate(node);
            classCctorGen.Generate(node);
        }