Beispiel #1
0
        public override void EnterClass_(XSharpParser.Class_Context context)
        {
            base.EnterClass_(context);
            XCodeTypeDeclaration newClass = new XCodeTypeDeclaration(context.Id.GetCleanText());

            newClass.IsClass = true;
            bool nested = CurrentType != null;

            pushCurrentType(newClass);
            addAttributes(newClass, context.Attributes);
            writeTrivia(newClass, context);

            ContextToTypeAttributes(newClass, context.Modifiers);
            ContextToTypeModifiers(newClass, context.Modifiers);

            // INHERIT from ?
            if (context.BaseType != null)
            {
                string baseName = context.BaseType.GetCleanText();
                var    baseType = BuildTypeReference(baseName);
                SaveSourceCode(baseType, context.BaseType);
                newClass.BaseTypes.Add(baseType);
            }
            // IMPLEMENTS ?
            addInterfaces(newClass, context._Implements);
            //
            // Add the variables from this class to the Members collection and lookup table
            ClearMembers();
            addFields(newClass, context);
            if (nested)
            {
                SaveSourceCode(newClass, context);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Merge both CodeCompileUnit. The main type (class) will come from designerCompileUnit
        /// </summary>
        /// <param name="compileUnit"></param>
        /// <param name="designerCompileUnit"></param>
        /// <returns></returns>
        internal static XMergedCodeCompileUnit MergeCodeCompileUnit(XCodeCompileUnit compileUnit, XCodeCompileUnit designerCompileUnit)
        {
            // Create the merged CodeCompileUnit
            var mergedCodeCompileUnit = new XMergedCodeCompileUnit();

            CodeNamespace       designerNamespace;
            CodeTypeDeclaration designerClass = FindDesignerClass(designerCompileUnit, out designerNamespace);

            if (designerClass != null)
            {
                // Do the same with the form
                CodeNamespace       nameSpace;
                CodeTypeDeclaration formClass;
                HasPartialClass(compileUnit, out nameSpace, out formClass);
                // and merge only if ...
                if ((string.Compare(designerNamespace.Name, nameSpace.Name, true) == 0) &&
                    (string.Compare(designerClass.Name, formClass.Name, true) == 0))
                {
                    // Ok, same Namespace & same Class : Merge !

                    // So, the "main" class is...
                    XCodeTypeDeclaration mergedType = new XCodeTypeDeclaration(formClass.Name);
                    // And does inherit from
                    mergedType.BaseTypes.AddRange(formClass.BaseTypes);
                    mergedType.IsPartial      = true;
                    mergedType.TypeAttributes = formClass.TypeAttributes;
                    // Now, read members from each side, and put a stamp on each
                    foreach (CodeTypeMember member in designerClass.Members)
                    {
                        member.SetFromDesigner(true);
                        mergedType.Members.Add(member);
                    }
                    foreach (CodeTypeMember member in formClass.Members)
                    {
                        member.SetFromDesigner(false);
                        mergedType.Members.Add(member);
                    }
                    // A class is always in a NameSpace
                    XCodeNamespace mergedNamespace = new XCodeNamespace(nameSpace.Name);
                    mergedNamespace.Types.Add(mergedType);
                    // Now, add it to the CompileUnit
                    mergedCodeCompileUnit.Namespaces.Clear();
                    mergedCodeCompileUnit.Namespaces.Add(mergedNamespace);
                    //
                }
                else
                {
                    // Something went wrong, return the designer CodeCompileUnit
                    mergedCodeCompileUnit = new XMergedCodeCompileUnit(designerCompileUnit);
                }
            }
            mergedCodeCompileUnit.FormUnit     = compileUnit;
            mergedCodeCompileUnit.FileName     = compileUnit.FileName;
            mergedCodeCompileUnit.DesignerUnit = designerCompileUnit;
            return(mergedCodeCompileUnit);
        }
Beispiel #3
0
 public void addInterfaces(XCodeTypeDeclaration newClass, IList <XSharpParser.DatatypeContext> parents)
 {
     if ((parents != null) && (parents.Count > 0))
     {
         foreach (var typecontext in parents)
         {
             var ifName   = typecontext.GetCleanText();
             var baseType = BuildTypeReference(ifName);
             SaveSourceCode(baseType, typecontext);
             newClass.BaseTypes.Add(baseType);
         }
     }
 }
Beispiel #4
0
        public override void EnterEnum_([NotNull] XSharpParser.Enum_Context context)
        {
            base.EnterEnum_(context);
            XCodeTypeDeclaration newClass = new XCodeTypeDeclaration(context.Id.GetCleanText());

            newClass.IsEnum = true;

            pushCurrentType(newClass);
            addAttributes(newClass, context.Attributes);
            writeTrivia(newClass, context);
            ContextToTypeModifiers(newClass, context.Modifiers);
            ContextToTypeAttributes(newClass, context.Modifiers);
            ClearMembers();
            addFields(newClass, context);
            SaveSourceCode(newClass, context);
        }
Beispiel #5
0
 private void addFields(XCodeTypeDeclaration newClass, XSharpParserRuleContext context)
 {
     if (FieldList.ContainsKey(context))
     {
         var fields = FieldList[context];
         foreach (var f in fields)
         {
             newClass.Members.Add(f);
             var xtype = findType(f.Type.BaseType);
             if (xtype != null)
             {
                 addClassMember(new XMemberType(f.Name, MemberTypes.Field, false, xtype.FullName));
             }
             else
             {
                 addClassMember(new XMemberType(f.Name, MemberTypes.Field, false, f.Type.BaseType));
             }
         }
     }
 }
Beispiel #6
0
        public override void EnterStructure_(XSharpParser.Structure_Context context)
        {
            base.EnterStructure_(context);
            XCodeTypeDeclaration newClass = new XCodeTypeDeclaration(context.Id.GetCleanText());

            newClass.IsStruct = true;

            pushCurrentType(newClass);
            addAttributes(newClass, context.Attributes);
            writeTrivia(newClass, context);
            //
            ContextToTypeAttributes(newClass, context.Modifiers);
            ContextToTypeModifiers(newClass, context.Modifiers);

            // Interfaces
            addInterfaces(newClass, context._Implements);
            //
            // Add the variables from this class to the Members collection and lookup table
            ClearMembers();
            addFields(newClass, context);
            SaveSourceCode(newClass, context);
        }
Beispiel #7
0
        private static XCodeTypeDeclaration FindFirstClass(CodeCompileUnit ccu, out XCodeNamespace namespaceName)
        {
            namespaceName = null;
            XCodeTypeDeclaration rstClass = null;

            if (ccu != null)
            {
                foreach (XCodeNamespace namespace2 in ccu.Namespaces)
                {
                    foreach (XCodeTypeDeclaration declaration in namespace2.Types)
                    {
                        //  The first Type == The first Class declaration
                        if (declaration.IsClass)
                        {
                            namespaceName = namespace2;
                            rstClass      = declaration;
                            break;
                        }
                    }
                }
            }
            return(rstClass);
        }
Beispiel #8
0
        public override void EnterStructure_(XSharpParser.Structure_Context context)
        {
            XCodeTypeDeclaration newClass = new XCodeTypeDeclaration(context.Id.GetCleanText());

            // Set as Current working Class
            CurrentClass = newClass;
            if (context.Attributes != null)
            {
                newClass.CustomAttributes = GenerateAttributes(context.Attributes);
            }
            // and push into the Namespace
            CurrentNamespace.Types.Add(newClass);
            // That's a Class
            newClass.IsStruct = true;
            writeTrivia(newClass, context);
            //
            if (context.Modifiers == null)
            {
                newClass.TypeAttributes = System.Reflection.TypeAttributes.Public;
            }
            else
            {
                // Modifiers
                foreach (var t in context.Modifiers._Tokens)
                {
                    switch (t.Type)
                    {
                    case XSharpParser.PARTIAL:
                        newClass.IsPartial = true;
                        break;

                    case XSharpParser.SEALED:
                        newClass.Attributes |= MemberAttributes.Final;
                        break;

                    case XSharpParser.ABSTRACT:
                        newClass.Attributes |= MemberAttributes.Abstract;
                        break;

                    case XSharpParser.INTERNAL:
                        newClass.Attributes |= MemberAttributes.Assembly;
                        break;

                    case XSharpParser.PUBLIC:
                        newClass.Attributes |= MemberAttributes.Public;
                        break;
                    }
                }
                // What Visibility ?
                newClass.TypeAttributes = ContextToStructureModifiers(context.Modifiers);
            }

            // IMPLEMENTS ?
            if ((context._Implements != null) && (context._Implements.Count > 0))
            {
                foreach (var interfaces in context._Implements)
                {
                    var ifName = interfaces.GetCleanText();
                    newClass.BaseTypes.Add(BuildTypeReference(ifName));
                }
            }
            //
            // Add the variables from this class to the Members collection and lookup table
            ClearMembers();
            if (FieldList.ContainsKey(context))
            {
                var fields = FieldList[context];
                foreach (var f in fields)
                {
                    newClass.Members.Add(f);
                    addClassMember(new XMemberType(f.Name, MemberTypes.Field, false, findType(f.Type.BaseType), f.Type.BaseType));
                }
            }
            var token      = context.Stop as XSharpToken;
            var tokenIndex = token.OriginalTokenIndex;
            var line       = token.Line;

            for (;;)
            {
                if (tokenIndex >= _tokens.Count - 1)
                {
                    break;
                }
                tokenIndex++;
                if (_tokens[tokenIndex].Line > line)
                {
                    break;
                }
            }
        }