Exemple #1
0
        public CCodeInterfaceCastOperatorDeclaration(INamedTypeSymbol type, INamedTypeSymbol interfaceSymbol)
            : base(new InterfaceCastOperatorMethod(type))
        {
            this.type            = type;
            this.interfaceSymbol = interfaceSymbol;

            MethodBodyOpt = new MethodBody(Method)
            {
                Statements =
                {
                    new ReturnStatement
                    {
                        ExpressionOpt = new ObjectCreationExpression
                        {
                            NewOperator     = true,
                            AllocatorPrefix = true,
                            Type            = new NamedTypeImpl {
                                Name = CCodeInterfaceWrapperClass.GetName(this.type, this.interfaceSymbol), TypeKind = TypeKind.Class
                            },
                            Arguments ={ new ThisReference                 {
                              Type = type
                          } }
                        }
                    }
                }
            };
        }
Exemple #2
0
        public void WriteMethodNameNoTemplate(IMethodSymbol methodSymbol, IMethodSymbol methodSymbolForName = null, bool interfaceWrapperMethodSpecialCase = false)
        {
            // name
            ////if (methodSymbol.MethodKind == MethodKind.Destructor)
            ////{
            ////    this.TextSpan("~");
            ////    this.WriteTypeName((methodSymbolForName ?? methodSymbol).ContainingType, false, false);
            ////    return;
            ////}

            var symbol = methodSymbolForName ?? methodSymbol;

            if (methodSymbol.IsExternDeclaration())
            {
                this.WriteNameEnsureCompatible(symbol, true);
                return;
            }

            var explicitInterfaceImplementation =
                symbol.ExplicitInterfaceImplementations != null
                    ? symbol.ExplicitInterfaceImplementations.FirstOrDefault()
                    : null;

            if (methodSymbol.ContainingType != null &&
                methodSymbol.ContainingType.TypeKind == TypeKind.Interface)
            {
                this.TextSpan(methodSymbol.ContainingType.GetTypeFullName());
                if (interfaceWrapperMethodSpecialCase && explicitInterfaceImplementation == null)
                {
                    if (methodSymbol.ContainingType.IsGenericType &&
                        methodSymbol.ContainingType.GetTemplateArguments().Select(t => t as INamedTypeSymbol).All(t => t != null && !t.IsGenericType))
                    {
                        var sb = new StringBuilder();
                        CCodeInterfaceWrapperClass.GetGenericArgumentsRecursive(sb, methodSymbol.ContainingType);
                        this.TextSpan(sb.ToString());
                    }
                }

                this.TextSpan("_");
            }

            if (explicitInterfaceImplementation != null)
            {
                this.TextSpan(explicitInterfaceImplementation.ContainingType.GetTypeFullName());

                if (explicitInterfaceImplementation.ContainingType.IsGenericType &&
                    explicitInterfaceImplementation.ContainingType.GetTemplateArguments().Select(t => t as INamedTypeSymbol).All(t => t != null && !t.IsGenericType))
                {
                    var sb = new StringBuilder();
                    CCodeInterfaceWrapperClass.GetGenericArgumentsRecursive(sb, explicitInterfaceImplementation.ContainingType);
                    this.TextSpan(sb.ToString());
                }

                var dotIndex = symbol.MetadataName.LastIndexOf('.');
                if (dotIndex > 0)
                {
                    var name = symbol.MetadataName.Substring(dotIndex);
                    this.TextSpan(name.CleanUpName().EnsureCompatible());
                }
                else
                {
                    this.TextSpan("_");
                    this.WriteNameEnsureCompatible(symbol, symbol.MethodKind == MethodKind.BuiltinOperator && symbol.ContainingType == null);
                }
            }
            else
            {
                this.WriteNameEnsureCompatible(symbol, symbol.MethodKind == MethodKind.BuiltinOperator && symbol.ContainingType == null);
            }

            if (methodSymbol.MetadataName == "op_Explicit")
            {
                this.TextSpan("_");
                this.WriteTypeSuffix(methodSymbol.ReturnType);
            }
            else if (methodSymbol.IsStatic && methodSymbol.MetadataName == "op_Implicit")
            {
                this.TextSpan("_");

                var effectiveType           = methodSymbol.ReturnType;
                var substitutedMethodSymbol = methodSymbol as SubstitutedMethodSymbol;
                if (substitutedMethodSymbol != null &&
                    substitutedMethodSymbol.UnderlyingMethod.ReturnType.TypeKind == TypeKind.TypeParameter)
                {
                    effectiveType = substitutedMethodSymbol.UnderlyingMethod.ReturnType;
                }

                this.WriteTypeSuffix(effectiveType);
            }

            // write suffixes for ref & out parameters
            if (!string.IsNullOrWhiteSpace(methodSymbol.Name ?? methodSymbol.MetadataName) && methodSymbol.MethodKind != MethodKind.Constructor)
            {
                foreach (var parameter in methodSymbol.Parameters.Where(p => p.RefKind != RefKind.None))
                {
                    this.TextSpan("_");
                    this.TextSpan(parameter.RefKind.ToString());
                }
            }

            if (methodSymbol.IsGenericMethod && methodSymbol.Arity > 0)
            {
                this.TextSpan("T");
                this.TextSpan(methodSymbol.Arity.ToString());
            }
        }