ModifierEnum ConvertModifier(AST.Modifiers m, ModifierEnum defaultVisibility) { ModifierEnum r = (ModifierEnum)m; if ((r & ModifierEnum.VisibilityMask) == ModifierEnum.None) return r | defaultVisibility; else return r; }
ModifierEnum ConvertTypeModifier(AST.Modifiers m) { if (this.IsVisualBasic) return ConvertModifier(m, ModifierEnum.Public); if (currentClass.Count > 0) return ConvertModifier(m, ModifierEnum.Private); else return ConvertModifier(m, ModifierEnum.Internal); }
ModifierEnum ConvertModifier(AST.Modifiers m) { if (this.IsVisualBasic) return ConvertModifier(m, ModifierEnum.Public); else if (currentClass.Count > 0 && currentClass.Peek().ClassType == ClassType.Interface) return ConvertModifier(m, ModifierEnum.Public); else return ConvertModifier(m, ModifierEnum.Private); }
IReturnType CreateReturnType(NRefactoryAST.TypeReference reference) { return CreateReturnType(reference, null, TypeVisitor.ReturnTypeOptions.None); }
IReturnType CreateReturnType(NRefactoryAST.TypeReference reference, IMethod method, TypeVisitor.ReturnTypeOptions options) { return CreateReturnType(reference, method, GetCurrentClass(), cu, options); }
public override object VisitEventDeclaration(NRefactoryAST.EventDeclaration eventDeclaration, object data) { DomRegion region = GetRegion(eventDeclaration.StartLocation, eventDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(eventDeclaration.BodyStart, eventDeclaration.BodyEnd); DefaultClass c = GetCurrentClass(); IReturnType type; if (eventDeclaration.TypeReference.IsNull) { DefaultClass del = new DefaultClass(cu, ClassType.Delegate, ConvertModifier(eventDeclaration.Modifier), region, c); del.Modifiers |= ModifierEnum.Synthetic; CreateDelegate(del, eventDeclaration.Name + "EventHandler", new NRefactoryAST.TypeReference("System.Void", true), new NRefactoryAST.TemplateDefinition[0], eventDeclaration.Parameters); type = del.DefaultReturnType; } else { type = CreateReturnType(eventDeclaration.TypeReference); } DefaultEvent e = new DefaultEvent(eventDeclaration.Name, type, ConvertModifier(eventDeclaration.Modifier), region, bodyRegion, c); ConvertAttributes(eventDeclaration, e); c.Events.Add(e); e.Documentation = GetDocumentation(region.BeginLine, eventDeclaration.Attributes); if (eventDeclaration.HasAddRegion) { e.AddMethod = new DefaultMethod(e.DeclaringType, "add_" + e.Name) { Parameters = { new DefaultParameter("value", e.ReturnType, DomRegion.Empty) }, Region = GetRegion(eventDeclaration.AddRegion.StartLocation, eventDeclaration.AddRegion.EndLocation), BodyRegion = GetRegion(eventDeclaration.AddRegion.Block.StartLocation, eventDeclaration.AddRegion.Block.EndLocation) }; } if (eventDeclaration.HasRemoveRegion) { e.RemoveMethod = new DefaultMethod(e.DeclaringType, "remove_" + e.Name) { Parameters = { new DefaultParameter("value", e.ReturnType, DomRegion.Empty) }, Region = GetRegion(eventDeclaration.RemoveRegion.StartLocation, eventDeclaration.RemoveRegion.EndLocation), BodyRegion = GetRegion(eventDeclaration.RemoveRegion.Block.StartLocation, eventDeclaration.RemoveRegion.Block.EndLocation) }; } return null; }
public override object VisitPropertyDeclaration(NRefactoryAST.PropertyDeclaration propertyDeclaration, object data) { DomRegion region = GetRegion(propertyDeclaration.StartLocation, propertyDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(propertyDeclaration.BodyStart, propertyDeclaration.BodyEnd); IReturnType type = CreateReturnType(propertyDeclaration.TypeReference); DefaultClass c = GetCurrentClass(); DefaultProperty property = new DefaultProperty(propertyDeclaration.Name, type, ConvertModifier(propertyDeclaration.Modifier), region, bodyRegion, GetCurrentClass()); if (propertyDeclaration.HasGetRegion) { property.GetterRegion = GetRegion(propertyDeclaration.GetRegion.StartLocation, propertyDeclaration.GetRegion.EndLocation); property.CanGet = true; property.GetterModifiers = ConvertModifier(propertyDeclaration.GetRegion.Modifier, ModifierEnum.None); } if (propertyDeclaration.HasSetRegion) { property.SetterRegion = GetRegion(propertyDeclaration.SetRegion.StartLocation, propertyDeclaration.SetRegion.EndLocation); property.CanSet = true; property.SetterModifiers = ConvertModifier(propertyDeclaration.SetRegion.Modifier, ModifierEnum.None); } property.Documentation = GetDocumentation(region.BeginLine, propertyDeclaration.Attributes); ConvertAttributes(propertyDeclaration, property); c.Properties.Add(property); mapProperty(propertyDeclaration, property); return null; }
public override object VisitDestructorDeclaration(NRefactoryAST.DestructorDeclaration destructorDeclaration, object data) { DomRegion region = GetRegion(destructorDeclaration.StartLocation, destructorDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(destructorDeclaration.EndLocation, destructorDeclaration.Body != null ? destructorDeclaration.Body.EndLocation : RefParser.Location.Empty); DefaultClass c = GetCurrentClass(); Destructor destructor = new Destructor(region, bodyRegion, c); ConvertAttributes(destructorDeclaration, destructor); c.Methods.Add(destructor); return null; }
public override object VisitOperatorDeclaration(NRefactoryAST.OperatorDeclaration operatorDeclaration, object data) { DefaultClass c = GetCurrentClass(); DomRegion region = GetRegion(operatorDeclaration.StartLocation, operatorDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(operatorDeclaration.EndLocation, operatorDeclaration.Body != null ? operatorDeclaration.Body.EndLocation : RefParser.Location.Empty); DefaultMethod method = new DefaultMethod(operatorDeclaration.Name, CreateReturnType(operatorDeclaration.TypeReference), ConvertModifier(operatorDeclaration.Modifier), region, bodyRegion, c); ConvertAttributes(operatorDeclaration, method); if(operatorDeclaration.Parameters != null) { foreach (NRefactoryAST.ParameterDeclarationExpression par in operatorDeclaration.Parameters) { method.Parameters.Add(CreateParameter(par, method)); } } c.Methods.Add(method); return null; }
ClassType TranslateClassType(NRefactoryAST.ClassType type) { switch (type) { case NRefactoryAST.ClassType.Enum: return ClassType.Enum; case NRefactoryAST.ClassType.Interface: return ClassType.Interface; case NRefactoryAST.ClassType.Struct: return ClassType.Struct; case NRefactoryAST.ClassType.Module: return ClassType.Module; default: return ClassType.Class; } }
public override object VisitNamespaceDeclaration(NRefactoryAST.NamespaceDeclaration namespaceDeclaration, object data) { DefaultUsingScope oldNamespace = currentNamespace; foreach (string name in namespaceDeclaration.Name.Split('.')) { currentNamespace = new DefaultUsingScope { Parent = currentNamespace, NamespaceName = PrependCurrentNamespace(name), }; currentNamespace.Parent.ChildScopes.Add(currentNamespace); } object ret = namespaceDeclaration.AcceptChildren(this, data); currentNamespace = oldNamespace; return ret; }
public override object VisitAttributeSection(NRefactoryAST.AttributeSection attributeSection, object data) { if (GetCurrentClass() == null) { ClassFinder cf = new ClassFinder(new DefaultClass(cu, "DummyClass"), attributeSection.StartLocation.Line, attributeSection.StartLocation.Column); cu.Attributes.AddRange(VisitAttributes(new[] { attributeSection }, cf)); } return null; }
static object ConvertAttributeArgument(NRefactoryAST.Expression expression) { NRefactoryAST.PrimitiveExpression pe = expression as NRefactoryAST.PrimitiveExpression; if (pe != null) return pe.Value; else return null; }
void ConvertAttributes(NRefactoryAST.AttributedNode from, AbstractEntity to) { if (from.Attributes.Count == 0) { to.Attributes = DefaultAttribute.EmptyAttributeList; } else { ICSharpCode.NRefactory.Location location = from.Attributes[0].StartLocation; ClassFinder context; if (to is IClass) { context = new ClassFinder((IClass)to, location.Line, location.Column); } else { context = new ClassFinder(to.DeclaringType, location.Line, location.Column); } to.Attributes = VisitAttributes(from.Attributes, context); } }
public override object VisitUsing(NRefactoryAST.Using u, object data) { Debug.Assert(data is DefaultUsing); DefaultUsing us = (DefaultUsing)data; if (u.IsAlias) { IReturnType rt = CreateReturnType(u.Alias); if (rt != null) { us.AddAlias(u.Name, rt); } } else { us.Usings.Add(u.Name); } return data; }
public override object VisitMethodDeclaration(NRefactoryAST.MethodDeclaration methodDeclaration, object data) { DomRegion region = GetRegion(methodDeclaration.StartLocation, methodDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(methodDeclaration.EndLocation, methodDeclaration.Body != null ? methodDeclaration.Body.EndLocation : RefParser.Location.Empty); DefaultClass currentClass = GetCurrentClass(); DefaultMethod method = new DefaultMethod(methodDeclaration.Name, null, ConvertModifier(methodDeclaration.Modifier), region, bodyRegion, currentClass); method.IsExtensionMethod = methodDeclaration.IsExtensionMethod; method.Documentation = GetDocumentation(region.BeginLine, methodDeclaration.Attributes); ConvertTemplates(methodDeclaration.Templates, method); method.ReturnType = CreateReturnType(methodDeclaration.TypeReference, method, TypeVisitor.ReturnTypeOptions.None); ConvertAttributes(methodDeclaration, method); if (methodDeclaration.Parameters.Count > 0) { foreach (NRefactoryAST.ParameterDeclarationExpression par in methodDeclaration.Parameters) { method.Parameters.Add(CreateParameter(par, method)); } } else { method.Parameters = DefaultParameter.EmptyParameterList; } if (methodDeclaration.HandlesClause.Count > 0) { foreach (string handlesClause in methodDeclaration.HandlesClause) { if (handlesClause.ToLowerInvariant().StartsWith("me.")) method.HandlesClauses.Add(handlesClause.Substring(3)); else if (handlesClause.ToLowerInvariant().StartsWith("mybase.")) method.HandlesClauses.Add(handlesClause.Substring(7)); else method.HandlesClauses.Add(handlesClause); } } else { method.HandlesClauses = EmptyList<string>.Instance; } currentClass.Methods.Add(method); mapMethod(methodDeclaration, method); return null; }
public override object VisitDeclareDeclaration(NRefactoryAST.DeclareDeclaration declareDeclaration, object data) { DefaultClass currentClass = GetCurrentClass(); DomRegion region = GetRegion(declareDeclaration.StartLocation, declareDeclaration.EndLocation); DefaultMethod method = new DefaultMethod(declareDeclaration.Name, null, ConvertModifier(declareDeclaration.Modifier), region, DomRegion.Empty, currentClass); method.Documentation = GetDocumentation(region.BeginLine, declareDeclaration.Attributes); method.Modifiers |= ModifierEnum.Extern | ModifierEnum.Static; method.ReturnType = CreateReturnType(declareDeclaration.TypeReference, method, TypeVisitor.ReturnTypeOptions.None); ConvertAttributes(declareDeclaration, method); foreach (NRefactoryAST.ParameterDeclarationExpression par in declareDeclaration.Parameters) { method.Parameters.Add(CreateParameter(par, method)); } currentClass.Methods.Add(method); return null; }
public override object VisitTypeDeclaration(NRefactoryAST.TypeDeclaration typeDeclaration, object data) { DomRegion region = GetRegion(typeDeclaration.StartLocation, typeDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(typeDeclaration.BodyStartLocation, typeDeclaration.EndLocation); DefaultClass c = new DefaultClass(cu, TranslateClassType(typeDeclaration.Type), ConvertTypeModifier(typeDeclaration.Modifier), region, GetCurrentClass()); if (c.IsStatic) { // static classes are also abstract and sealed at the same time c.Modifiers |= ModifierEnum.Abstract | ModifierEnum.Sealed; } c.BodyRegion = bodyRegion; ConvertAttributes(typeDeclaration, c); c.Documentation = GetDocumentation(region.BeginLine, typeDeclaration.Attributes); DefaultClass outerClass = GetCurrentClass(); if (outerClass != null) { outerClass.InnerClasses.Add(c); c.FullyQualifiedName = outerClass.FullyQualifiedName + '.' + typeDeclaration.Name; } else { c.FullyQualifiedName = PrependCurrentNamespace(typeDeclaration.Name); cu.Classes.Add(c); } c.UsingScope = currentNamespace; currentClass.Push(c); ConvertTemplates(outerClass, typeDeclaration.Templates, c); // resolve constrains in context of the class // templates must be converted before base types because base types may refer to generic types if (c.ClassType != ClassType.Enum && typeDeclaration.BaseTypes != null) { foreach (NRefactoryAST.TypeReference type in typeDeclaration.BaseTypes) { IReturnType rt = CreateReturnType(type, null, TypeVisitor.ReturnTypeOptions.BaseTypeReference); if (rt != null) { c.BaseTypes.Add(rt); } } } object ret = typeDeclaration.AcceptChildren(this, data); currentClass.Pop(); if (c.ClassType == ClassType.Module) { foreach (DefaultField f in c.Fields) { f.Modifiers |= ModifierEnum.Static; } foreach (DefaultMethod m in c.Methods) { m.Modifiers |= ModifierEnum.Static; } foreach (DefaultProperty p in c.Properties) { p.Modifiers |= ModifierEnum.Static; } foreach (DefaultEvent e in c.Events) { e.Modifiers |= ModifierEnum.Static; } } mapType(typeDeclaration, c); return ret; }
public override object VisitConstructorDeclaration(NRefactoryAST.ConstructorDeclaration constructorDeclaration, object data) { DomRegion region = GetRegion(constructorDeclaration.StartLocation, constructorDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(constructorDeclaration.EndLocation, constructorDeclaration.Body != null ? constructorDeclaration.Body.EndLocation : RefParser.Location.Empty); DefaultClass c = GetCurrentClass(); Constructor constructor = new Constructor(ConvertModifier(constructorDeclaration.Modifier), region, bodyRegion, GetCurrentClass()); constructor.Documentation = GetDocumentation(region.BeginLine, constructorDeclaration.Attributes); ConvertAttributes(constructorDeclaration, constructor); if (constructorDeclaration.Parameters != null) { foreach (NRefactoryAST.ParameterDeclarationExpression par in constructorDeclaration.Parameters) { constructor.Parameters.Add(CreateParameter(par)); } } c.Methods.Add(constructor); mapConstructor(constructorDeclaration, constructor); return null; }
void ConvertConstraints(NRefactoryAST.TemplateDefinition template, DefaultTypeParameter typeParameter) { foreach (NRefactoryAST.TypeReference typeRef in template.Bases) { if (typeRef == NRefactoryAST.TypeReference.NewConstraint) { typeParameter.HasConstructableConstraint = true; } else if (typeRef == NRefactoryAST.TypeReference.ClassConstraint) { typeParameter.HasReferenceTypeConstraint = true; } else if (typeRef == NRefactoryAST.TypeReference.StructConstraint) { typeParameter.HasValueTypeConstraint = true; } else { IReturnType rt = CreateReturnType(typeRef, typeParameter.Method, TypeVisitor.ReturnTypeOptions.None); if (rt != null) { typeParameter.Constraints.Add(rt); } } } }
public override object VisitFieldDeclaration(NRefactoryAST.FieldDeclaration fieldDeclaration, object data) { DomRegion region = GetRegion(fieldDeclaration.StartLocation, fieldDeclaration.EndLocation); DefaultClass c = GetCurrentClass(); ModifierEnum modifier = ConvertModifier(fieldDeclaration.Modifier, (c.ClassType == ClassType.Struct && this.IsVisualBasic) ? ModifierEnum.Public : ModifierEnum.Private); string doku = GetDocumentation(region.BeginLine, fieldDeclaration.Attributes); if (currentClass.Count > 0) { for (int i = 0; i < fieldDeclaration.Fields.Count; ++i) { NRefactoryAST.VariableDeclaration field = (NRefactoryAST.VariableDeclaration)fieldDeclaration.Fields[i]; IReturnType retType; if (c.ClassType == ClassType.Enum) { retType = c.DefaultReturnType; } else { retType = CreateReturnType(fieldDeclaration.GetTypeForField(i)); if (!field.FixedArrayInitialization.IsNull) retType = new ArrayReturnType(cu.ProjectContent, retType, 1); } DefaultField f = new DefaultField(retType, field.Name, modifier, region, c); ConvertAttributes(fieldDeclaration, f); f.Documentation = doku; if (c.ClassType == ClassType.Enum) { f.Modifiers = ModifierEnum.Const | ModifierEnum.Public; } c.Fields.Add(f); mapField(fieldDeclaration, f); } } return null; }
public override object VisitDelegateDeclaration(NRefactoryAST.DelegateDeclaration delegateDeclaration, object data) { DomRegion region = GetRegion(delegateDeclaration.StartLocation, delegateDeclaration.EndLocation); DefaultClass c = new DefaultClass(cu, ClassType.Delegate, ConvertTypeModifier(delegateDeclaration.Modifier), region, GetCurrentClass()); c.Documentation = GetDocumentation(region.BeginLine, delegateDeclaration.Attributes); ConvertAttributes(delegateDeclaration, c); CreateDelegate(c, delegateDeclaration.Name, delegateDeclaration.ReturnType, delegateDeclaration.Templates, delegateDeclaration.Parameters); return c; }
public override object VisitIndexerDeclaration(NRefactoryAST.IndexerDeclaration indexerDeclaration, object data) { DomRegion region = GetRegion(indexerDeclaration.StartLocation, indexerDeclaration.EndLocation); DomRegion bodyRegion = GetRegion(indexerDeclaration.BodyStart, indexerDeclaration.BodyEnd); DefaultProperty i = new DefaultProperty("Item", CreateReturnType(indexerDeclaration.TypeReference), ConvertModifier(indexerDeclaration.Modifier), region, bodyRegion, GetCurrentClass()); i.IsIndexer = true; if (indexerDeclaration.HasGetRegion) { i.GetterRegion = GetRegion(indexerDeclaration.GetRegion.StartLocation, indexerDeclaration.GetRegion.EndLocation); i.CanGet = true; i.GetterModifiers = ConvertModifier(indexerDeclaration.GetRegion.Modifier, ModifierEnum.None); } if (indexerDeclaration.HasSetRegion) { i.SetterRegion = GetRegion(indexerDeclaration.SetRegion.StartLocation, indexerDeclaration.SetRegion.EndLocation); i.CanSet = true; i.SetterModifiers = ConvertModifier(indexerDeclaration.SetRegion.Modifier, ModifierEnum.None); } i.Documentation = GetDocumentation(region.BeginLine, indexerDeclaration.Attributes); ConvertAttributes(indexerDeclaration, i); if (indexerDeclaration.Parameters != null) { foreach (NRefactoryAST.ParameterDeclarationExpression par in indexerDeclaration.Parameters) { i.Parameters.Add(CreateParameter(par)); } } // If an IndexerNameAttribute is specified, use the specified name // for the indexer instead of the default name. IAttribute indexerNameAttribute = i.Attributes.LastOrDefault(this.IsIndexerNameAttribute); if (indexerNameAttribute != null && indexerNameAttribute.PositionalArguments.Count > 0) { string name = indexerNameAttribute.PositionalArguments[0] as string; if (!String.IsNullOrEmpty(name)) { i.FullyQualifiedName = String.Concat(i.DeclaringType.FullyQualifiedName, ".", name); } } DefaultClass c = GetCurrentClass(); c.Properties.Add(i); return null; }
void CreateDelegate(DefaultClass c, string name, NRefactoryAST.TypeReference returnType, IList<NRefactoryAST.TemplateDefinition> templates, IList<NRefactoryAST.ParameterDeclarationExpression> parameters) { c.BaseTypes.Add(c.ProjectContent.SystemTypes.MulticastDelegate); DefaultClass outerClass = GetCurrentClass(); if (outerClass != null) { outerClass.InnerClasses.Add(c); c.FullyQualifiedName = outerClass.FullyQualifiedName + '.' + name; } else { c.FullyQualifiedName = PrependCurrentNamespace(name); cu.Classes.Add(c); } c.UsingScope = currentNamespace; currentClass.Push(c); // necessary for CreateReturnType ConvertTemplates(outerClass, templates, c); List<IParameter> p = new List<IParameter>(); if (parameters != null) { foreach (NRefactoryAST.ParameterDeclarationExpression param in parameters) { p.Add(CreateParameter(param)); } } AnonymousMethodReturnType.AddDefaultDelegateMethod(c, CreateReturnType(returnType), p); currentClass.Pop(); }
// DC //DC /*public void loadCode(string code) { code = (code.fileExists()) ? code.fileContents() : code; var parser = code.csharpAst(); loadCompilationUnit(parser.CompilationUnit); }*/ //DC public void loadCompilationUnit(NRefactoryAST.CompilationUnit compilationUnit) { cu = new DefaultCompilationUnit(defaultProjectContent); compilationUnit.AcceptVisitor(this, null); }
IParameter CreateParameter(NRefactoryAST.ParameterDeclarationExpression par) { return CreateParameter(par, null); }
static IReturnType CreateReturnType(NRefactoryAST.TypeReference reference, IMethod method, IClass currentClass, ICompilationUnit cu, TypeVisitor.ReturnTypeOptions options) { if (currentClass == null) { return TypeVisitor.CreateReturnType(reference, new DefaultClass(cu, "___DummyClass"), method, 1, 1, cu.ProjectContent, options | TypeVisitor.ReturnTypeOptions.Lazy); } else { return TypeVisitor.CreateReturnType(reference, currentClass, method, currentClass.Region.BeginLine + 1, 1, cu.ProjectContent, options | TypeVisitor.ReturnTypeOptions.Lazy); } }
IParameter CreateParameter(NRefactoryAST.ParameterDeclarationExpression par, IMethod method) { return CreateParameter(par, method, GetCurrentClass(), cu); }
//DC public void mapCompilationUnit(NRefactoryAST.CompilationUnit compilationUnit, ICompilationUnit iCompilationUnit) { CompilationUnitToICompilationUnit.Add(compilationUnit, iCompilationUnit); }
internal static IParameter CreateParameter(NRefactoryAST.ParameterDeclarationExpression par, IMethod method, IClass currentClass, ICompilationUnit cu) { IReturnType parType = CreateReturnType(par.TypeReference, method, currentClass, cu, TypeVisitor.ReturnTypeOptions.None); DefaultParameter p = new DefaultParameter(par.ParameterName, parType, GetRegion(par.StartLocation, par.EndLocation)); p.Modifiers = (ParameterModifiers)par.ParamModifier; return p; }