public override void Visit (EnumMember em) { EnumMemberDeclaration newField = new EnumMemberDeclaration (); AddAttributeSection (newField, em); newField.AddChild (Identifier.Create (em.Name, Convert (em.Location)), AstNode.Roles.Identifier); if (em.Initializer != null) { newField.AddChild (new CSharpTokenNode (Convert (em.Initializer.Location), 1), EnumMemberDeclaration.Roles.Assign); newField.AddChild ((Expression)em.Initializer.Accept (this), EnumMemberDeclaration.InitializerRole); } typeStack.Peek ().AddChild (newField, TypeDeclaration.MemberRole); }
public override void Visit(EnumMember em) { var newField = new EnumMemberDeclaration(); AddAttributeSection(newField, em); newField.AddChild(Identifier.Create(em.Name, Convert(em.Location)), Roles.Identifier); if (em.Initializer != null) { newField.AddChild(new CSharpTokenNode(Convert(em.Initializer.Location), Roles.Assign), Roles.Assign); newField.AddChild((Expression)em.Initializer.Accept(this), EnumMemberDeclaration.InitializerRole); } //Console.WriteLine (newField.StartLocation +"-" + newField.EndLocation); typeStack.Peek().AddChild(newField, Roles.TypeMemberRole); }
public TypeDeclaration CreateType(TypeDefinition typeDef) { TypeDefinition oldCurrentType = context.CurrentType; context.CurrentType = typeDef; TypeDeclaration astType = new TypeDeclaration(); ConvertAttributes(astType, typeDef); astType.AddAnnotation(typeDef); astType.Modifiers = ConvertModifiers(typeDef); astType.Name = CleanName(typeDef.Name); if (typeDef.IsEnum) { // NB: Enum is value type astType.ClassType = ClassType.Enum; astType.Modifiers &= ~Modifiers.Sealed; } else if (typeDef.IsValueType) { astType.ClassType = ClassType.Struct; astType.Modifiers &= ~Modifiers.Sealed; } else if (typeDef.IsInterface) { astType.ClassType = ClassType.Interface; astType.Modifiers &= ~Modifiers.Abstract; } else { astType.ClassType = ClassType.Class; } IEnumerable<GenericParameter> genericParameters = typeDef.GenericParameters; if (typeDef.DeclaringType != null && typeDef.DeclaringType.HasGenericParameters) genericParameters = genericParameters.Skip(typeDef.DeclaringType.GenericParameters.Count); astType.TypeParameters.AddRange(MakeTypeParameters(genericParameters)); astType.Constraints.AddRange(MakeConstraints(genericParameters)); // Nested types foreach(TypeDefinition nestedTypeDef in typeDef.NestedTypes) { if (MemberIsHidden(nestedTypeDef, context.Settings)) continue; astType.AddChild(CreateType(nestedTypeDef), TypeDeclaration.MemberRole); } if (typeDef.IsEnum) { long expectedEnumMemberValue = 0; bool forcePrintingInitializers = IsFlagsEnum(typeDef); foreach (FieldDefinition field in typeDef.Fields) { if (field.IsRuntimeSpecialName) { // the value__ field if (field.FieldType != typeDef.Module.TypeSystem.Int32) { astType.AddChild(ConvertType(field.FieldType), TypeDeclaration.BaseTypeRole); } } else { EnumMemberDeclaration enumMember = new EnumMemberDeclaration(); enumMember.Name = CleanName(field.Name); long memberValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false); if (forcePrintingInitializers || memberValue != expectedEnumMemberValue) { enumMember.AddChild(new PrimitiveExpression(field.Constant), EnumMemberDeclaration.InitializerRole); } expectedEnumMemberValue = memberValue + 1; astType.AddChild(enumMember, TypeDeclaration.MemberRole); } } } else { // Base type if (typeDef.BaseType != null && !typeDef.IsValueType && typeDef.BaseType.FullName != "System.Object") { astType.AddChild(ConvertType(typeDef.BaseType), TypeDeclaration.BaseTypeRole); } foreach (var i in typeDef.Interfaces) astType.AddChild(ConvertType(i), TypeDeclaration.BaseTypeRole); AddTypeMembers(astType, typeDef); } context.CurrentType = oldCurrentType; return astType; }
public override void Visit (EnumMember em) { EnumMemberDeclaration newField = new EnumMemberDeclaration (); VariableInitializer variable = new VariableInitializer (); variable.AddChild (new Identifier (em.Name, Convert (em.Location)), AstNode.Roles.Identifier); if (em.Initializer != null) { var initializer = (AstNode)em.Initializer.Accept (this); if (initializer != null) variable.AddChild (initializer, AstNode.Roles.Initializer); } newField.AddChild (variable, AstNode.Roles.Initializer); typeStack.Peek ().AddChild (newField, TypeDeclaration.Roles.Member); }
public override void Visit (EnumMember em) { EnumMemberDeclaration newField = new EnumMemberDeclaration (); VariableInitializer variable = new VariableInitializer (); variable.AddChild (new Identifier (em.Name, Convert (em.Location)), AstNode.Roles.Identifier); if (em.Initializer != null) { variable.AddChild ((MonoDevelop.CSharp.Ast.Expression)em.Initializer.Accept (this), VariableInitializer.Roles.Expression); } newField.AddChild (variable, AstNode.Roles.Variable); typeStack.Peek ().AddChild (newField, TypeDeclaration.MemberRole); }
public override void Visit (EnumMember em) { EnumMemberDeclaration newField = new EnumMemberDeclaration (); // TODO: attributes, 'new' modifier newField.AddChild (new Identifier (em.Name, Convert (em.Location)), AstNode.Roles.Identifier); if (em.Initializer != null) { newField.AddChild ((Expression)em.Initializer.Accept (this), EnumMemberDeclaration.InitializerRole); } typeStack.Peek ().AddChild (newField, TypeDeclaration.MemberRole); }
/// <summary> /// Creates the AST for a type definition. /// </summary> /// <param name="typeDef"></param> /// <returns>TypeDeclaration or DelegateDeclaration.</returns> public AttributedNode CreateType(TypeDefinition typeDef) { // create CSharp code mappings - used for debugger if (this.CodeMappings == null) this.CodeMappings = new Tuple<string, List<MemberMapping>>(typeDef.FullName, new List<MemberMapping>()); // create type TypeDefinition oldCurrentType = context.CurrentType; context.CurrentType = typeDef; TypeDeclaration astType = new TypeDeclaration(); ConvertAttributes(astType, typeDef); astType.AddAnnotation(typeDef); astType.Modifiers = ConvertModifiers(typeDef); astType.Name = CleanName(typeDef.Name); if (typeDef.IsEnum) { // NB: Enum is value type astType.ClassType = ClassType.Enum; astType.Modifiers &= ~Modifiers.Sealed; } else if (typeDef.IsValueType) { astType.ClassType = ClassType.Struct; astType.Modifiers &= ~Modifiers.Sealed; } else if (typeDef.IsInterface) { astType.ClassType = ClassType.Interface; astType.Modifiers &= ~Modifiers.Abstract; } else { astType.ClassType = ClassType.Class; } IEnumerable<GenericParameter> genericParameters = typeDef.GenericParameters; if (typeDef.DeclaringType != null && typeDef.DeclaringType.HasGenericParameters) genericParameters = genericParameters.Skip(typeDef.DeclaringType.GenericParameters.Count); astType.TypeParameters.AddRange(MakeTypeParameters(genericParameters)); astType.Constraints.AddRange(MakeConstraints(genericParameters)); // Nested types foreach (TypeDefinition nestedTypeDef in typeDef.NestedTypes) { if (MemberIsHidden(nestedTypeDef, context.Settings)) continue; astType.AddChild(CreateType(nestedTypeDef), TypeDeclaration.MemberRole); } AttributedNode result = astType; if (typeDef.IsEnum) { long expectedEnumMemberValue = 0; bool forcePrintingInitializers = IsFlagsEnum(typeDef); foreach (FieldDefinition field in typeDef.Fields) { if (field.IsRuntimeSpecialName) { // the value__ field if (field.FieldType != typeDef.Module.TypeSystem.Int32) { astType.AddChild(ConvertType(field.FieldType), TypeDeclaration.BaseTypeRole); } } else { EnumMemberDeclaration enumMember = new EnumMemberDeclaration(); enumMember.Name = CleanName(field.Name); long memberValue = (long)CSharpPrimitiveCast.Cast(TypeCode.Int64, field.Constant, false); if (forcePrintingInitializers || memberValue != expectedEnumMemberValue) { enumMember.AddChild(new PrimitiveExpression(field.Constant), EnumMemberDeclaration.InitializerRole); } expectedEnumMemberValue = memberValue + 1; astType.AddChild(enumMember, TypeDeclaration.MemberRole); } } } else if (typeDef.BaseType != null && typeDef.BaseType.FullName == "System.MulticastDelegate") { DelegateDeclaration dd = new DelegateDeclaration(); dd.Modifiers = astType.Modifiers & ~Modifiers.Sealed; dd.Name = astType.Name; dd.AddAnnotation(typeDef); astType.Attributes.MoveTo(dd.Attributes); astType.TypeParameters.MoveTo(dd.TypeParameters); astType.Constraints.MoveTo(dd.Constraints); foreach (var m in typeDef.Methods) { if (m.Name == "Invoke") { dd.ReturnType = ConvertType(m.ReturnType, m.MethodReturnType); dd.Parameters.AddRange(MakeParameters(m)); ConvertAttributes(dd, m.MethodReturnType, m.Module); } } result = dd; } else { // Base type if (typeDef.BaseType != null && !typeDef.IsValueType && typeDef.BaseType.FullName != "System.Object") { astType.AddChild(ConvertType(typeDef.BaseType), TypeDeclaration.BaseTypeRole); } foreach (var i in typeDef.Interfaces) astType.AddChild(ConvertType(i), TypeDeclaration.BaseTypeRole); AddTypeMembers(astType, typeDef); if (astType.Members.OfType<IndexerDeclaration>().Any(idx => idx.PrivateImplementationType.IsNull)) { // Remove the [DefaultMember] attribute if the class contains indexers foreach (AttributeSection section in astType.Attributes) { foreach (Ast.Attribute attr in section.Attributes) { TypeReference tr = attr.Type.Annotation<TypeReference>(); if (tr != null && tr.Name == "DefaultMemberAttribute" && tr.Namespace == "System.Reflection") { attr.Remove(); } } if (section.Attributes.Count == 0) section.Remove(); } } } context.CurrentType = oldCurrentType; return result; }