public void Run(AstNode compilationUnit)
        {
            if (!context.Settings.UsingDeclarations)
            {
                return;
            }

            // First determine all the namespaces that need to be imported:
            compilationUnit.AcceptVisitor(this, null);

            importedNamespaces.Add("System");             // always import System, even when not necessary

            // Now add using declarations for those namespaces:
            foreach (string ns in importedNamespaces.OrderByDescending(n => n))
            {
                // we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards
                // (always inserting at the start of the list)
                string[] parts  = ns.Split('.');
                AstType  nsType = new SimpleType(parts[0]);
                for (int i = 1; i < parts.Length; i++)
                {
                    nsType = new MemberType {
                        Target = nsType, MemberName = parts[i]
                    };
                }
                compilationUnit.InsertChildAfter(null, new UsingDeclaration {
                    Import = nsType
                }, CompilationUnit.MemberRole);
            }

            // TODO: verify that the SimpleTypes refer to the correct type (no ambiguities)
        }
Example #2
0
        internal static void SetModifiers(AstNode node, Modifiers newValue)
        {
            Modifiers oldValue     = GetModifiers(node);
            AstNode   insertionPos = node.GetChildrenByRole(Attribute.AttributeRole).LastOrDefault();

            foreach (Modifiers m in VBModifierToken.AllModifiers)
            {
                if ((m & newValue) != 0)
                {
                    if ((m & oldValue) == 0)
                    {
                        // Modifier was added
                        var newToken = new VBModifierToken(TextLocation.Empty, m);
                        node.InsertChildAfter(insertionPos, newToken, ModifierRole);
                        insertionPos = newToken;
                    }
                    else
                    {
                        // Modifier already exists
                        insertionPos = node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m);
                    }
                }
                else
                {
                    if ((m & oldValue) != 0)
                    {
                        // Modifier was removed
                        node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m).Remove();
                    }
                }
            }
        }
        internal static void SetModifiers(AstNode node, LambdaExpressionModifiers newValue)
        {
            LambdaExpressionModifiers oldValue = GetModifiers(node);
            AstNode insertionPos = null;

            foreach (Modifiers m in VBModifierToken.AllModifiers)
            {
                if ((m & (Modifiers)newValue) != 0)
                {
                    if ((m & (Modifiers)oldValue) == 0)
                    {
                        // Modifier was added
                        var newToken = new VBModifierToken(AstLocation.Empty, m);
                        node.InsertChildAfter(insertionPos, newToken, ModifierRole);
                        insertionPos = newToken;
                    }
                    else
                    {
                        // Modifier already exists
                        insertionPos = node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m);
                    }
                }
                else
                {
                    if ((m & (Modifiers)oldValue) != 0)
                    {
                        // Modifier was removed
                        node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m).Remove();
                    }
                }
            }
        }
        public void Run(AstNode compilationUnit)
        {
            // First determine all the namespaces that need to be imported:
            compilationUnit.AcceptVisitor(new FindRequiredImports(this), null);

            importedNamespaces.Add("System");             // always import System, even when not necessary

            if (context.Settings.UsingDeclarations)
            {
                // Now add using declarations for those namespaces:
                foreach (string ns in GetNamespacesInReverseOrder())
                {
                    // we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards
                    // (always inserting at the start of the list)
                    string[] parts  = ns.Split('.');
                    AstType  nsType = new SimpleType(parts[0]).WithAnnotation(TextTokenType.NamespacePart);
                    for (int i = 1; i < parts.Length; i++)
                    {
                        nsType = new MemberType {
                            Target = nsType, MemberNameToken = Identifier.Create(parts[i]).WithAnnotation(TextTokenType.NamespacePart)
                        }.WithAnnotation(TextTokenType.NamespacePart);
                    }
                    compilationUnit.InsertChildAfter(null, new UsingDeclaration {
                        Import = nsType
                    }, SyntaxTree.MemberRole);
                }
            }

            if (!context.Settings.FullyQualifyAmbiguousTypeNames)
            {
                return;
            }

            if (context.CurrentModule != null)
            {
                FindAmbiguousTypeNames(context.CurrentModule.Types, internalsVisible: true);
                var asmDict = new Dictionary <AssemblyDef, List <AssemblyDef> >(AssemblyEqualityComparer.Instance);
                foreach (var r in context.CurrentModule.GetAssemblyRefs())
                {
                    AssemblyDef d = context.CurrentModule.Context.AssemblyResolver.Resolve(r, context.CurrentModule);
                    if (d == null)
                    {
                        continue;
                    }
                    List <AssemblyDef> list;
                    if (!asmDict.TryGetValue(d, out list))
                    {
                        asmDict.Add(d, list = new List <AssemblyDef>());
                    }
                    list.Add(d);
                }
                foreach (var list in asmDict.Values)
                {
                    FindAmbiguousTypeNames(GetTypes(list), internalsVisible: false);
                }
            }

            // verify that the SimpleTypes refer to the correct type (no ambiguities)
            compilationUnit.AcceptVisitor(new FullyQualifyAmbiguousTypeNamesVisitor(this), null);
        }
        public void Run(AstNode rootNode, TransformContext context)
        {
            // First determine all the namespaces that need to be imported:
            var requiredImports = new FindRequiredImports(context);

            rootNode.AcceptVisitor(requiredImports);

            var usingScope = new UsingScope();

            rootNode.AddAnnotation(usingScope);

            if (context.Settings.UsingDeclarations)
            {
                var insertionPoint = rootNode.Children.LastOrDefault(n => n is PreProcessorDirective p && p.Type == PreProcessorDirectiveType.Define);

                // Now add using declarations for those namespaces:
                foreach (string ns in requiredImports.ImportedNamespaces.OrderByDescending(n => n))
                {
                    Debug.Assert(context.RequiredNamespacesSuperset.Contains(ns), $"Should not insert using declaration for namespace that is missing from the superset: {ns}");
                    // we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards
                    // (always inserting at the start of the list)
                    string[] parts  = ns.Split('.');
                    AstType  nsType = new SimpleType(parts[0]);
                    for (int i = 1; i < parts.Length; i++)
                    {
                        nsType = new MemberType {
                            Target = nsType, MemberName = parts[i]
                        };
                    }
                    if (context.Settings.FullyQualifyAmbiguousTypeNames)
                    {
                        var reference = nsType.ToTypeReference(NameLookupMode.TypeInUsingDeclaration) as TypeOrNamespaceReference;
                        if (reference != null)
                        {
                            usingScope.Usings.Add(reference);
                        }
                    }
                    rootNode.InsertChildAfter(insertionPoint, new UsingDeclaration {
                        Import = nsType
                    }, SyntaxTree.MemberRole);
                }
            }

            if (!context.Settings.FullyQualifyAmbiguousTypeNames)
            {
                return;
            }

            // verify that the SimpleTypes refer to the correct type (no ambiguities)
            rootNode.AcceptVisitor(new FullyQualifyAmbiguousTypeNamesVisitor(context, usingScope));
        }
Example #6
0
        public void Run(AstNode compilationUnit)
        {
            // First determine all the namespaces that need to be imported:
            compilationUnit.AcceptVisitor(new FindRequiredImports(this), null);

            importedNamespaces.Add("System");             // always import System, even when not necessary

            if (context.Settings.UsingDeclarations)
            {
                // Now add using declarations for those namespaces:
                foreach (string ns in importedNamespaces.OrderByDescending(n => n))
                {
                    // we go backwards (OrderByDescending) through the list of namespaces because we insert them backwards
                    // (always inserting at the start of the list)
                    string[] parts  = ns.Split('.');
                    AstType  nsType = new SimpleType(parts[0]);
                    for (int i = 1; i < parts.Length; i++)
                    {
                        nsType = new MemberType {
                            Target = nsType, MemberName = parts[i]
                        };
                    }
                    compilationUnit.InsertChildAfter(null, new UsingDeclaration {
                        Import = nsType
                    }, SyntaxTree.MemberRole);
                }
            }

            if (!context.Settings.FullyQualifyAmbiguousTypeNames)
            {
                return;
            }

            FindAmbiguousTypeNames(context.CurrentModule, internalsVisible: true);
            if (context.CurrentModule is ModuleDefMD)
            {
                foreach (var r in ((ModuleDefMD)context.CurrentModule).GetAssemblyRefs())
                {
                    var d = context.CurrentModule.Context.AssemblyResolver.Resolve(r, context.CurrentModule);
                    if (d != null)
                    {
                        FindAmbiguousTypeNames(d.ManifestModule, internalsVisible: false);
                    }
                }
            }

            // verify that the SimpleTypes refer to the correct type (no ambiguities)
            compilationUnit.AcceptVisitor(new FullyQualifyAmbiguousTypeNamesVisitor(this), null);
        }
		internal static void SetModifiers(AstNode node, Modifiers newValue)
		{
			Modifiers oldValue = GetModifiers(node);
			AstNode insertionPos = node.GetChildrenByRole(AttributeRole).LastOrDefault();
			foreach (Modifiers m in CSharpModifierToken.AllModifiers) {
				if ((m & newValue) != 0) {
					if ((m & oldValue) == 0) {
						// Modifier was added
						node.InsertChildAfter(insertionPos, new CSharpModifierToken(AstLocation.Empty, m), ModifierRole);
					} else {
						// Modifier already exists
						insertionPos = node.GetChildrenByRole(ModifierRole).First(t => t.Modifier == m);
					}
				} else {
					if ((m & oldValue) != 0) {
						// Modifier was removed
						node.GetChildrenByRole (ModifierRole).First(t => t.Modifier == m).Remove();
					}
				}
			}
		}
Example #8
0
			void AddConstraints(AstNode parent, TypeParameters d)
			{
				if (d == null)
					return;
				for (int i = 0; i < d.Count; i++) {
					var typeParameter = d [i];
					if (typeParameter == null)
						continue;
					var c = typeParameter.Constraints;
					if (c == null)
						continue;
					var location = LocationsBag.GetLocations(c);
					var constraint = new Constraint();
					constraint.AddChild(new CSharpTokenNode(Convert(c.Location), Roles.WhereKeyword), Roles.WhereKeyword);
					constraint.AddChild(new SimpleType(Identifier.Create(c.TypeParameter.Value, Convert(c.TypeParameter.Location))), Roles.ConstraintTypeParameter);
					if (location != null)
						constraint.AddChild(new CSharpTokenNode(Convert(location [0]), Roles.Colon), Roles.Colon);
					var commaLocs = LocationsBag.GetLocations(c.ConstraintExpressions);
					int curComma = 0;
					if (c.ConstraintExpressions != null) {
						foreach (var expr in c.ConstraintExpressions) {
							constraint.AddChild(ConvertToType(expr), Roles.BaseType);
							var sce = expr as SpecialContraintExpr;
							if (sce != null) {
								switch (sce.Constraint) {
									case SpecialConstraint.Class:
										break;
									case SpecialConstraint.Struct:
										break;
									case SpecialConstraint.Constructor:
										var bl = LocationsBag.GetLocations(expr);
										if (bl != null) {
											constraint.AddChild(new CSharpTokenNode(Convert(bl [0]), Roles.LPar), Roles.LPar);
											constraint.AddChild(new CSharpTokenNode(Convert(bl [1]), Roles.RPar), Roles.RPar);
										}
										break;
								}
							}

							if (commaLocs != null && curComma < commaLocs.Count)
								constraint.AddChild(new CSharpTokenNode(Convert(commaLocs [curComma++]), Roles.Comma), Roles.Comma);
						}
					}
					
					// We need to sort the constraints by position; as they might be in a different order than the type parameters
					AstNode prevSibling = parent.LastChild;
					while (prevSibling.StartLocation > constraint.StartLocation && prevSibling.PrevSibling != null)
						prevSibling = prevSibling.PrevSibling;
					parent.InsertChildAfter(prevSibling, constraint, Roles.Constraint);
				}
			}