public static string ToTypescript(this GenericNameSyntax syntaxItem) { if (syntaxItem.Identifier.Text == "Dictionary") { return(syntaxItem.TypeArgumentList.ToTypescript(true)); } if (syntaxItem.Identifier.Text == "List") { return(syntaxItem.TypeArgumentList.ToTypescript() + "[]"); } if (syntaxItem.Identifier.Text == "Expression") { return("any"); } if (syntaxItem.IsSpecifiedKnownType()) { return("any"); } if (syntaxItem.IsKnownType()) { return($"{syntaxItem.Identifier.Text}<{syntaxItem.TypeArgumentList.ToTypescript()}>"); } if (syntaxItem.IsExcluded()) { return("any"); } Log.Warn($"unknown Generic: {syntaxItem} mapped to any"); return("any"); }
static MockTransform() { var mockTemplate = CSharpSyntaxTree.ParseText("private Mock<IFoo> thing;"); var root = mockTemplate.GetRoot(); genericMock = root.DescendantNodes().OfType <GenericNameSyntax>().FirstOrDefault(); }
// public override SyntaxNode Visit(SyntaxNode node) { // if (node == null) { // Console.WriteLine("Warning: visiting a node which is null..."); // return null; // } // Console.WriteLine("visiting: " + node.ToString()); // Console.WriteLine("type: " + node.GetType().ToString()); // base.Visit(node); // return node; // } public override SyntaxNode VisitGenericName(GenericNameSyntax node) { if (node == null) { Console.WriteLine("Warning: visiting a node which is null..."); return(null); } Console.WriteLine("visiting generic name: " + node.ToFullString()); Console.WriteLine("identifier: " + node.Identifier); Console.WriteLine("type arguments:"); foreach (var typeArg in node.TypeArgumentList.Arguments) { Console.WriteLine(" - " + typeArg.ToFullString()); } if (node.TypeArgumentList.Arguments.Count != 1) { Console.WriteLine(string.Format("Error: Unexpected TypeArgument count {0} for ScalarType, there should be only one.", node.TypeArgumentList.Arguments.Count)); return(node); } ScalarType scalarType; try { scalarType = (ScalarType)Enum.Parse(typeof(ScalarType), node.TypeArgumentList.Arguments[0].ToFullString()); } catch (Exception e) { Console.WriteLine(string.Format("Error: Type argument {0} not recognized as valid ScalarType. Reason:\n{1}", node.TypeArgumentList.Arguments[0], e.Message)); return(node); } var replacementNode = _typeMap[scalarType]; base.VisitGenericName(node); return(replacementNode); }
private Doc PrintGenericNameSyntax(GenericNameSyntax node) { return(Group( this.PrintSyntaxToken(node.Identifier), this.Print(node.TypeArgumentList) )); }
private static bool TryMatchGenericSyntaxNodeWithGivenSymbol(GenericNameSyntax genericName, ISymbol previewReturnTypeSymbol, [NotNullWhen(true)] out SyntaxNode?syntaxNode) { if (IsSyntaxToken(genericName.Identifier, previewReturnTypeSymbol)) { syntaxNode = genericName; return(true); } TypeArgumentListSyntax typeArgumentList = genericName.TypeArgumentList; foreach (TypeSyntax typeArgument in typeArgumentList.Arguments) { TypeSyntax typeArgumentElementType = GetElementTypeForNullableAndArrayTypeNodes(typeArgument); if (typeArgumentElementType is GenericNameSyntax innerGenericName) { if (TryMatchGenericSyntaxNodeWithGivenSymbol(innerGenericName, previewReturnTypeSymbol, out syntaxNode)) { return(true); } } if (IsIdentifierNameSyntax(typeArgumentElementType, previewReturnTypeSymbol)) { syntaxNode = typeArgumentElementType; return(true); } } syntaxNode = null; return(false); }
public override void VisitGenericName(GenericNameSyntax genericNode) { _cancellationToken.ThrowIfCancellationRequested(); SymbolInfo symbolInfo = _syntaxContext.SemanticModel.GetSymbolInfo(genericNode, _cancellationToken); if (!(symbolInfo.Symbol is ITypeSymbol typeSymbol)) { _cancellationToken.ThrowIfCancellationRequested(); base.VisitGenericName(genericNode); return; } if (genericNode.IsUnboundGenericName) { typeSymbol = typeSymbol.OriginalDefinition; } if (!ParametersCounter.CountParametersInTypeSymbolForGenericNode(typeSymbol, _cancellationToken)) { return; } _cancellationToken.ThrowIfCancellationRequested(); base.VisitGenericName(genericNode); }
public static string GenericName(GenericNameSyntax name) { //TODO: replace the screwed up Action<>/Func<> conversions w/ DNSwift implementations switch (name.Identifier.Text) { case "Action": //Action<string, int> converts to (String, Int) -> Void return(": (" + SyntaxNode(name.TypeArgumentList) + ") -> Void"); case "Func": //Func<string, int, string> converts to (String, Int) -> String var output = ": ("; //The last generic argument in Func<> is used as a return type var allButLastArguments = name.TypeArgumentList.Arguments.Take(name.TypeArgumentList.Arguments.Count - 1); output += string.Join(", ", allButLastArguments.Select(SyntaxNode)); return(output + ") -> " + SyntaxNode(name.TypeArgumentList.Arguments.Last())); case "Unwrapped": return(SyntaxNode(name.TypeArgumentList.Arguments.First()).TrimEnd('!') + "!"); case "Optional": return(SyntaxNode(name.TypeArgumentList.Arguments.First()).TrimEnd('!') + "?"); case "AmbiguousWrapping": return(SyntaxNode(name.TypeArgumentList.Arguments.First()).TrimEnd('!')); } //Something<another, thing> converts to Something<another, thing> :D return(Type(name.Identifier.Text) + SyntaxNode(name.TypeArgumentList)); }
public override IDeclarationUse <SyntaxNode> VisitGenericName(GenericNameSyntax node) { var identifier = node.Identifier.Text; var tparams = new List <IDeclarationUse <SyntaxNode> >(); foreach (var item in node.TypeArgumentList.Arguments) { tparams.Add(item.Accept(this)); } var genericDeclaration = this.resolver.Resolve(identifier, tparams, this.genericDeclaration); if (genericDeclaration != null) { return(new GenericDeclarationUse( new ParserSyntaxNodeProvider <GenericNameSyntax>(node), genericDeclaration, tparams)); } return(new UnknownGenericDeclarationUse( new ParserSyntaxNodeProvider <GenericNameSyntax>(node), new UnknownDeclaration(identifier), tparams)); }
protected override void GenerateInternal(Module module) { m_selectClassName = ClassNameBase + "Select"; m_whereClassName = ClassNameBase + "Where"; m_orderByClassName = ClassNameBase + "OrderBy"; var parameters = module.Parameters.ToList(); var sortParameters = RemoveAndReturnByNames(parameters, "sort", "dir"); var methodParameters = parameters.RemoveAndReturn(p => p.Required); // don't belong anywhere, are used in a special way RemoveAndReturnByNames(parameters, "continue", "offset", "limit", "prop"); var whereParameters = parameters; var selectClass = GenerateSelect(module.PropertyGroups, module.Name == "revisions"); var whereClass = GenerateWhere(whereParameters); var orderByClass = GenerateOrderBy(sortParameters, module.PropertyGroups.SelectMany(g => g.Properties)); var codeUnit = SyntaxEx.CompilationUnit( SyntaxEx.NamespaceDeclaration(Wiki.EntitiesNamespace, selectClass, whereClass, orderByClass), "System", "System.Globalization", "System.Xml.Linq", "LinqToWiki", "LinqToWiki.Collections", "LinqToWiki.Internals"); Wiki.Files.Add(ClassNameBase, codeUnit); string queryTypeName = "WikiQuery"; var queryTypeGenericParameters = new List<string> { m_whereClassName, m_selectClassName }; if (orderByClass != null) { queryTypeName += "Sortable"; queryTypeGenericParameters.Insert(1, m_orderByClassName); } if (module.Generator) { queryTypeName += "Generator"; queryTypeGenericParameters.Insert(0, Wiki.Names.Page); } m_queryType = SyntaxEx.GenericName(queryTypeName, queryTypeGenericParameters); SortType? sortType = null; var dirParameter = sortParameters.SingleOrDefault(p => p.Name == "dir"); if (dirParameter != null) { var type = (EnumParameterType)dirParameter.Type; if (type.Values.Any(x => x == "ascending")) sortType = SortType.Ascending; else if (type.Values.Any(x => x == "newer")) sortType = SortType.Newer; } GenerateMethod(module, methodParameters, m_selectClassName, m_selectProps, MethodClassName, false, sortType); }
public override SyntaxNode VisitGenericName(GenericNameSyntax node) { return RewriteSyntax(node); }
public void VisitGenericName(GenericNameSyntax node) { if (node == null) throw new ArgumentNullException("node"); node.Validate(); ExpressionStart(node); _writer.WriteIdentifier(node.Identifier); node.TypeArgumentList.Accept(this); ExpressionEnd(node); }
private BoundExpression BindGenericName(GenericNameSyntax node) { // TODO: I think this method can share code with BindIdentifier. (petergo) Debug.Assert(node != null); // For each instance type T, starting with the instance type of the immediately enclosing // type declaration and continuing with the instance type of each enclosing class or struct declaration (if any): var typeArguments = node.Arguments.Select(x => context.BindType(x)).ToArray(); foreach (NamedTypeSymbol t in this.containingMethod.ContainingType.TypeAndOuterTypes()) { // If a member lookup of I in T with K type arguments produces a match: var lookupResult = MemberLookup(t, node.PlainName, node.Arity, false); if (lookupResult.IsViable) { Debug.Assert(lookupResult.Symbols.Any()); // If T is the instance type of the immediately enclosing class or struct type and the lookup identifies // one or more methods, the result is a method group with an associated instance expression of this. if (t == this.containingMethod.ContainingType) { if (lookupResult.Symbols.OfType<MethodSymbol>().Any()) { return new BoundMethodGroup(node, typeArguments, new BoundThisReference(null, t), lookupResult.Symbols.OfType<MethodSymbol>().ToList()); } } // UNDONE: Otherwise, the result is the same as a member access of the form T.I<A1, ..., AK>. In this case, it is a // UNDONE: compile-time error for the simple-name to refer to an instance member. } } #if false // Otherwise, for each namespace N, starting with the namespace in which the simple-name occurs, // continuing with each enclosing namespace (if any), and ending with the global namespace, // the following steps are evaluated until an entity is located: foreach(var ns in containingMethod.ContainingNamespaces()) { // If N contains an accessible type having name I and K type parameters, then: var childType = ns.GetMembers(node.PlainName).OfType<TypeSymbol>().FirstOrDefault(); // UNDONE: Check accessibility // UNDONE: Check arity if (childType != null) { // The namespace-or-type-name refers to the type constructed with the given type arguments. return new TypeExpression(node, childType); } // UNDONE: Otherwise, if the location where the simple-name occurs is enclosed by a namespace declaration for N: // UNDONE: Otherwise, if the namespaces imported by the using-namespace-directives of the namespace declaration // UNDONE: contain more than one type having name I and K type parameters, then the simple-name is ambiguous and // UNDONE: an error occurs. } #endif // UNDONE: Until we get the lookup and error reporting logic above correct we can simply use the context lookup: var result = context.LookupType(node.PlainName, node.Arity, null, ConsList<Symbol>.Empty); if (result.IsViable) { var type = result.Symbols.OfType<NamedTypeSymbol>().FirstOrDefault(); if (type != null) { return new BoundTypeExpression(node, type.Construct(typeArguments)); } } // UNDONE: Otherwise, the simple-name is undefined and a compile-time error occurs. return null; }