Beispiel #1
0
        void AddTypeAndNamespaceFromSyntax(TypeSyntax typeSyntax, bool convertedType)
        {
            if (convertedType)
            {
                throw new NotImplementedException();
            }

            {
                TypeInfo    typeInfo            = currentFileModel.semanticModel.GetTypeInfo(typeSyntax);
                ITypeSymbol typeSymbol          = typeInfo.Type;
                ITypeSymbol convertedTypeSymbol = typeInfo.ConvertedType;
                if (typeSymbol != null)
                {
                    AddTypeAndNamespace(typeSymbol);
                    return;
                }
            }

            // the type syntax was not a type
            SymbolInfo symbolInfo = currentFileModel.semanticModel.GetSymbolInfo(typeSyntax);

            if (symbolInfo.Symbol == null)
            {
                throw new InvalidOperationException();
            }
            if (CSharpToD.generateDebug)
            {
                writer.Write("// '{0}' is not a type, it is a(n) '{1}'",
                             typeSyntax.GetText().ToString().Trim().Replace("\n", "").Replace("\r\n", ""),
                             symbolInfo.Symbol.Kind);
            }
        }
Beispiel #2
0
            public Field(TypeSyntax type, ISymbol symbol, MemberDeclarationSyntax declaration, AttributeSyntax fieldAttribute)
            {
                string typeName = type.GetText().ToString().Split('.').Last().TrimEnd();

                TypeName    = typeName;
                ActualType  = typeName;
                IsArray     = TypeName.EndsWith("[]") || TypeName.StartsWith("List<");
                Name        = symbol.Name;
                Symbol      = symbol;
                Declaration = declaration;
                Index       = int.Parse(fieldAttribute.ArgumentList.Arguments.First().GetText().ToString());

                OriginalType = null;
                CountType    = null;
                IsAbsolute   = false;
                IsVarLength  = false;
                FixedLength  = -1;
                IsGeneric    = symbol.ContainingType.TypeParameters.Any(genericParameter => genericParameter.Name == typeName);
                var attributes = declaration.AttributeLists.SelectMany(list => list.Attributes);

                foreach (var attribute in attributes)
                {
                    switch (attribute.Name.GetText().ToString())
                    {
                    case absoluteAttribute:
                        IsAbsolute = true;
                        break;

                    case varLengthAttribute:
                        IsVarLength = true;
                        break;

                    case fixedLengthAttribute:
                        FixedLength = int.Parse(attribute.ArgumentList.Arguments.First().GetText().ToString());
                        break;

                    case actualTypeAttribute:
                        TypeName     = GetAttributeTypeArgument(attribute);
                        OriginalType = TypeName;
                        break;

                    case countType:
                        CountType = GetAttributeTypeArgument(attribute);
                        break;
                    }
                }

                if (!IsArray)
                {
                    if (IsVarLength)
                    {
                        TypeName += "_Var";
                    }
                    if (IsAbsolute)
                    {
                        TypeName += "_Abs";
                    }
                }
            }
        private void CheckIfTypeIsAllowed(TypeSyntax type)
        {
            var typeName = type.GetText().ToString().Trim(); // TODO: handle primite types aliases, String and string for example

            if (!TypeAllowed(typeName))
            {
                throw new Exception("[" + typeName + "] " + ExceptionMessage); // TODO: Make these errors explicit
            }
        }
Beispiel #4
0
        private string ParseFulltype(TypeSyntax syntax)
        {
            if (!(syntax is QualifiedNameSyntax))
            {
                throw new InvalidOperationException("All types must be fully qualified (including namespace)");
            }

            return($"{syntax.GetText()}");
        }
        public IEnumerable <CodeIssue> GetIssues(IDocument document, CommonSyntaxNode node, CancellationToken cancellationToken)
        {
            ISemanticModel model = document.GetSemanticModel(cancellationToken);

            ObjectCreationExpressionSyntax expression = (ObjectCreationExpressionSyntax)node;

            // Note: Do NOT use HasDiagnostics, as this expression obviously DOES produce errors (type is not known yet)

            TypeSyntax typeSyntax = expression.Type;

            CommonTypeInfo typeInfo = model.GetTypeInfo(typeSyntax, cancellationToken);

            TypeSymbol typeSymbol = (TypeSymbol)typeInfo.Type;

            if (typeSymbol == null)
            {
                yield break;
            }

            // Check if this is an unrecognized symbol
            if (typeSymbol.Kind == SymbolKind.ErrorType)
            {
                string className = string.Empty;

                // TODO: extract container info and propose solutions
                // Considers:
                // new A.B.C() -> namespace A.B { class C {} }

                // Extract top-level name
                // Considers:
                // new A.B.C<int,float>() -> C
                if (typeSyntax.Kind == SyntaxKind.IdentifierName)
                {
                    className = typeSyntax.GetText();
                }
                else if (typeSyntax.Kind == SyntaxKind.GenericName)
                {
                    GenericNameSyntax genericName = (GenericNameSyntax)typeSyntax;
                    className = genericName.Identifier.GetText();
                }
                else if (typeSyntax.Kind == SyntaxKind.QualifiedName)
                {
                    QualifiedNameSyntax qualifiedName = (QualifiedNameSyntax)typeSyntax;
                    className = qualifiedName.Right.GetText();
                }

                yield return(new CodeIssue(CodeIssue.Severity.Info, expression.Span, String.Format("Generate class `{0}'", className), new GenerateClassFromUsageAction(document, expression, className)));
            }

            yield break;
        }
        public static string GetTypeName(this TypeSyntax member)
        {
            return(member.GetText().ToString().Trim());

            //switch (member)
            //{
            //    case PredefinedTypeSyntax predefined:
            //        return predefined.Keyword.Text;
            //        break;
            //    case IdentifierNameSyntax name:
            //        return name.Identifier.Text;
            //        break;
            //    case GenericNameSyntax generic:
            //        return generic.GetText().ToString().Trim();
            //        break;
            //    todo add array handling
            //    default:
            //        throw new ArgumentException($"Unknown TypeSyntax node type : {member.Kind().ToString()}");
            //}
        }
        private bool ValidateReturnType(TypeSyntax returnTypeSyntax)
        {
            var returnType = returnTypeSyntax.GetText().ToString().Trim();

            return(returnType != "dynamic");
        }
Beispiel #8
0
 public static string GetTypeName(this TypeSyntax value)
 {// kind of tricky way to get type name
     return(value.GetText().ToString().Trim(' '));
 }
 private string GetGenericTypeName()
 {
     return("T" + _type.GetText().ToString().Trim().UppercaseFirst());
 }