Пример #1
0
        public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node)
        {
            if (node.IsUnSafe())
            {
                this.AppendCompileIssue(node, IssueType.Error, IssueId.UnsafeNotSupport);
                return node;
            }

            var ctorInfos = this._semanticModel.GetDeclaredSymbol(node).InstanceConstructors;
            if (ctorInfos.Count() > 1)
            {
                this.AppendCompileIssue(node, IssueType.Error, IssueId.CtorOverload);
                return node;
            }

            bool isStaticType = false;
            if (node.IsPartial())
                this.AppendCompileIssue(node, IssueType.Error, IssueId.PartialNotSupport);
            if (node.IsStaticType())
                isStaticType = true;

            List<MemberDeclarationSyntax> static_members = new List<MemberDeclarationSyntax>();

            var classInfo = this._semanticModel.GetDeclaredSymbol(node);
            _currentClass = classInfo.GetTypeSymbolName();

            int total = 0;//Note:Total member count, calc in below loop.
            foreach (var m in node.Members)
            {
                if (m.Kind() == SyntaxKind.PropertyDeclaration)
                {
                    this.AppendCompileIssue(m, IssueType.Error, IssueId.PropertyDeclareNotSupport);
                    continue;
                }

                if (m.IsValidMemberKind() && (m.IsStaticMember() || _semanticModel.IsGlobalVariable(m)))
                    continue;

                if (m.Kind() == SyntaxKind.FieldDeclaration && _semanticModel.IsNoCompile((m as FieldDeclarationSyntax)))
                    continue;

                if (m.Kind() == SyntaxKind.MethodDeclaration && _semanticModel.IsNoCompile((m as MethodDeclarationSyntax)))
                    continue;

                total++;
            }

            int memberCount = 0;

            string strBaseClass;
            if (node.BaseList != null)
            {
                var baseInfo = classInfo.BaseType;
                if (!baseInfo.IsScriptSymbol())
                {
                    this.AppendCompileIssue(node, IssueType.Error, IssueId.UseNonScript, baseInfo);
                    return node;
                }

                if (baseInfo.ContainingNamespace.Name == nameof(System) && baseInfo.Name == nameof(System.Object))
                    strBaseClass = _template.RootType;
                else
                    strBaseClass = baseInfo.GetTypeSymbolName();
            }
            else
            {
                strBaseClass = _template.RootType;
            }

            string strInterfaces = this.MakeInterfacesList(node);

            BasicClassTemplate classTemplate = _template.CreateClassTemplate(isStaticType);

            classTemplate.Assign(BasicClassTemplate.CLASS, _currentClass).Assign("baseclass", strBaseClass).Assign("interfaces", strInterfaces);
            _output.Write(node.Identifier, classTemplate.GetBeginString());

            if (!isStaticType)
            {
                if (_indentType)
                    _output.IncreaseIndent();
            }

            foreach (var member in node.Members)
            {
                if (member.Kind() == SyntaxKind.PropertyDeclaration)
                {
                    //Note:Must skip for the member count reason.
                    continue;
                }

                if (member.Kind() == SyntaxKind.ClassDeclaration)
                {
                    this.AppendCompileIssue(member, IssueType.Error, IssueId.InnerClassNotSupport);
                    continue;
                }

                if (member.Kind() == SyntaxKind.ConstructorDeclaration)
                {
                    var ctorSyntax = member as ConstructorDeclarationSyntax;
                    if (ctorSyntax.IsStaticMember())
                        this.AppendCompileIssue(node, IssueType.Error, IssueId.StaticConstructor);
                }

                if (member.Kind() == SyntaxKind.FieldDeclaration)
                {
                    var field = member as FieldDeclarationSyntax;
                    if (_semanticModel.IsGlobalVariable(field) || _semanticModel.IsNoCompile(field))
                        continue;
                }

                if (member.Kind() == SyntaxKind.MethodDeclaration)
                {
                    var method = member as MethodDeclarationSyntax;
                    if (_semanticModel.IsNoCompile(method))
                        continue;
                }

                if ((!isStaticType) && member.IsStaticMember())//Note:IsValid will be true, other case is handled above
                {
                    //Note: Check if is EvalAtCompile field, no need to parse it here. Don't add to static_memebers.
                    if (member.Kind() != SyntaxKind.FieldDeclaration || !(_semanticModel.IsEvalCandidate(member)))
                    {
                        static_members.Add(member);
                    }
                }
                else
                {
                    _isStaticMember = member.IsStaticMember();
                    if (isStaticType)
                    {
                        _output.Write(member, _currentClass + ".");
                    }

                    this.Visit(member);
                    memberCount++;

                    if (isStaticType)
                        _output.TrivialWriteLine(';');
                    else if (memberCount != total)
                    {
                        _output.TrivialWriteLine(_template.MemberSeparator);
                        if (member.Kind() == SyntaxKind.MethodDeclaration || member.Kind() == SyntaxKind.ConstructorDeclaration || member.Kind() == SyntaxKind.PropertyDeclaration)
                        {
                            _output.TrivialWriteLine();
                        }
                    }
                    _isStaticMember = false;
                }
            }

            if (!isStaticType)
            {
                if (_indentType)
                    _output.DecreaseIndent();
            }
            _output.TrivialWriteLine();

            classTemplate.Assign(BasicClassTemplate.CLASS, _currentClass).Assign(BasicClassTemplate.BASECLASS, strBaseClass).Assign(BasicClassTemplate.INTERFACE, strInterfaces);
            _output.TrivialWrite(classTemplate.GetEndString());

            _output.TrivialWriteLine();//Note: Empty line after class declare.

            if (!isStaticType && static_members.Count > 0)
            {
                _isStaticMember = true;
                foreach (var member in static_members)
                {
                    _output.Write(member, _currentClass + ".");
                    Visit(member);
                    _output.TrivialWriteLine(';');
                }

                _output.TrivialWriteLine();
                _isStaticMember = false;
            }

            return node;
        }