Ejemplo n.º 1
0
    public ParameterSyntax GenerateManagedParameter(CsParameter csElement)
    {
        var        param = Parameter(Identifier(csElement.Name));
        TypeSyntax type;

        if (csElement.IsOut && csElement.IsFast)
        {
            type = ParseTypeName(csElement.PublicType.GetNativeImplementationQualifiedName());
        }
        else
        {
            type = ParseTypeName(csElement.PublicType.QualifiedName);

            if (csElement.IsOut)
            {
                param = param.AddModifiers(Token(SyntaxKind.OutKeyword));
            }
            else if (csElement.IsRef || csElement.IsRefIn)
            {
                param = param.AddModifiers(Token(SyntaxKind.RefKeyword));
            }
        }

        return(param.WithType(type));
    }
        public ParameterSyntax GenerateManagedParameter(CsParameter csElement)
        {
            var param = Parameter(Identifier(csElement.Name));

            if (csElement.IsFastOut)
            {
                var iface = (CsInterface)csElement.PublicType;
                param = param.WithType(ParseTypeName(iface.GetNativeImplementationOrThis().QualifiedName));
            }
            else
            {
                param = param.WithType(ParseTypeName(csElement.PublicType.QualifiedName));

                if (csElement.IsOut)
                {
                    param = param.AddModifiers(Token(SyntaxKind.OutKeyword));
                }
                else if (csElement.IsRef || csElement.IsRefIn)
                {
                    param = param.AddModifiers(Token(SyntaxKind.RefKeyword));
                }
            }

            return(param);
        }
Ejemplo n.º 3
0
        public FixedStatementSyntax GeneratePin(CsParameter csElement)
        {
            if (csElement.IsFixed && !csElement.IsUsedAsReturnType)
            {
                return(FixedStatement(
                           VariableDeclaration(
                               VoidPtrType,
                               SingletonSeparatedList(
                                   VariableDeclarator(
                                       GetMarshalStorageLocationIdentifier(csElement),
                                       null,
                                       EqualsValueClause(
                                           PrefixUnaryExpression(
                                               SyntaxKind.AddressOfExpression, IdentifierName(csElement.Name)
                                               )
                                           )
                                       )
                                   )
                               ),
                           EmptyStatement()
                           ));
            }

            return(null);
        }
Ejemplo n.º 4
0
        public override IEnumerable <MemberDeclarationSyntax> GenerateCode(CsCallable csElement)
        {
            // method signature
            var parameters = csElement.PublicParameters.Select(
                param => GetMarshaller(param)
                .GenerateManagedParameter(param)
                .WithDefault(
                    param.DefaultValue == null
                                 ? default
                                 : EqualsValueClause(ParseExpression(param.DefaultValue))
                    )
                );

            var methodDeclaration = AddDocumentationTrivia(
                MethodDeclaration(
                    ParseTypeName(csElement.PublicReturnTypeQualifiedName),
                    csElement.Name
                    )
                .WithModifiers(csElement.VisibilityTokenList.Add(Token(SyntaxKind.UnsafeKeyword)))
                .WithParameterList(ParameterList(SeparatedList(parameters))),
                csElement
                );

            if (csElement.SignatureOnly)
            {
                yield return(methodDeclaration
                             .WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
                             .WithModifiers(TokenList()));

                yield break;
            }

            StatementSyntaxList statements = new(Generators.Marshalling);

            foreach (var param in csElement.Parameters)
            {
                var relations = param.Relations;

                if (relations.Count != 0 || param.UsedAsReturn)
                {
                    statements.Add(GenerateManagedHiddenMarshallableProlog(param));
                }

                foreach (var relation in relations)
                {
                    if (!ValidRelationInScenario(relation))
                    {
                        Logger.Error(
                            LoggingCodes.InvalidRelationInScenario,
                            $"The relation \"{relation}\" is invalid in a method/function."
                            );
                        continue;
                    }

                    CsParameter relatedParameter = null;

                    if (relation is LengthRelation {
                        Identifier: { Length: > 0 } relatedMarshallableName
                    })
 public FixedStatementSyntax GeneratePin(CsParameter csElement)
 {
     return(FixedStatement(VariableDeclaration(PointerType(PredefinedType(Token(SyntaxKind.VoidKeyword))),
                                               SingletonSeparatedList(
                                                   VariableDeclarator(Identifier(csElement.IntermediateMarshalName)).WithInitializer(EqualsValueClause(
                                                                                                                                         GetMarshalStorageLocation(csElement)
                                                                                                                                         )))), EmptyStatement()));
 }
Ejemplo n.º 6
0
        private (InteropType type, bool isLocal) GetInteropTypeForParameter(CsParameter param)
        {
            InteropType interopType;
            var         isLocal    = false;
            var         publicName = param.PublicType.QualifiedName;

            if (publicName == globalNamespace.GetTypeName(WellKnownName.PointerSize))
            {
                interopType = typeof(void *);
            }
            else if (param.HasPointer)
            {
                interopType = typeof(void *);
            }
            else if (param.MarshalType is CsFundamentalType marshalFundamental)
            {
                var type = marshalFundamental.Type;
                if (type == typeof(IntPtr))
                {
                    type = typeof(void *);
                }
                interopType = type;
            }
            else if (param.PublicType is CsFundamentalType publicFundamental)
            {
                var type = publicFundamental.Type;
                if (type == typeof(IntPtr))
                {
                    type = typeof(void *);
                }
                interopType = type;
            }
            else if (param.PublicType is CsStruct csStruct)
            {
                // If parameter is a struct, then a LocalInterop is needed
                if (csStruct.HasMarshalType)
                {
                    interopType = $"{csStruct.QualifiedName}.__Native";
                }
                else
                {
                    interopType = csStruct.QualifiedName;
                }
                isLocal = true;
            }
            else if (param.PublicType is CsEnum csEnum)
            {
                interopType = csEnum.UnderlyingType.Type;
            }
            else
            {
                interopType = null;
            }

            return(interopType, isLocal);
        }
Ejemplo n.º 7
0
        public ArgumentSyntax GenerateManagedArgument(CsParameter csElement)
        {
            var arg = Argument(IdentifierName(csElement.Name));

            if (csElement.IsOut)
            {
                arg = arg.WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword));
            }

            return(arg);
        }
 public FixedStatementSyntax GeneratePin(CsParameter csElement) => FixedStatement(
     VariableDeclaration(
         GetMarshalTypeSyntax(csElement),
         SingletonSeparatedList(
             VariableDeclarator(GetMarshalStorageLocationIdentifier(csElement)).WithInitializer(
                 EqualsValueClause(IdentifierName(csElement.Name))
                 )
             )
         ),
     EmptyStatement()
     );
Ejemplo n.º 9
0
        public ParameterSyntax GenerateManagedParameter(CsParameter csElement)
        {
            var param = Parameter(Identifier(csElement.Name))
                        .WithType(StringType);

            if (csElement.IsOut)
            {
                param = param.AddModifiers(Token(SyntaxKind.OutKeyword));
            }

            return(param);
        }
Ejemplo n.º 10
0
        protected ParameterSyntax GenerateManagedArrayParameter(CsParameter csElement)
        {
            var param = Parameter(Identifier(csElement.Name))
                        .WithType(ArrayType(ParseTypeName(csElement.PublicType.QualifiedName), SingletonList(ArrayRankSpecifier())));

            if (csElement.HasParams)
            {
                param = param.AddModifiers(Token(SyntaxKind.ParamsKeyword));
            }

            return(param);
        }
 public FixedStatementSyntax GeneratePin(CsParameter csElement)
 {
     if (csElement.IsFixed && !csElement.IsUsedAsReturnType)
     {
         return(FixedStatement(VariableDeclaration(PointerType(PredefinedType(Token(SyntaxKind.VoidKeyword))),
                                                   SingletonSeparatedList(
                                                       VariableDeclarator(GetMarshalStorageLocationIdentifier(csElement)).WithInitializer(EqualsValueClause(
                                                                                                                                              PrefixUnaryExpression(SyntaxKind.AddressOfExpression,
                                                                                                                                                                    IdentifierName(csElement.Name))
                                                                                                                                              )))), EmptyStatement()));
     }
     return(null);
 }
 public FixedStatementSyntax GeneratePin(CsParameter csElement) => FixedStatement(
     VariableDeclaration(
         VoidPtrType,
         SingletonSeparatedList(
             VariableDeclarator(
                 Identifier(csElement.IntermediateMarshalName),
                 null,
                 EqualsValueClause(GetMarshalStorageLocation(csElement))
                 )
             )
         ),
     EmptyStatement()
     );
Ejemplo n.º 13
0
        protected ArgumentSyntax GenerateManagedValueTypeArgument(CsParameter csElement)
        {
            var arg = Argument(IdentifierName(csElement.Name));

            if (csElement.IsOut)
            {
                return(arg.WithRefOrOutKeyword(Token(SyntaxKind.OutKeyword)));
            }
            else if (csElement.PassedByManagedReference)
            {
                return(arg.WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword)));
            }

            return(arg);
        }
        private InteropType GetInteropTypeForParameter(CsParameter param)
        {
            if (param.HasPointer)
            {
                return(TypeRegistry.VoidPtr);
            }

            if (param.PublicType.IsWellKnownType(Provider, WellKnownName.PointerSize))
            {
                return(TypeRegistry.VoidPtr);
            }

            if (param.MarshalType is CsFundamentalType marshalFundamental)
            {
                return marshalFundamental switch
                       {
                           { IsIntPtr : true } => TypeRegistry.VoidPtr,
Ejemplo n.º 15
0
        public FixedStatementSyntax GeneratePin(CsParameter csElement)
        {
            if (csElement.IsWideChar)
            {
                return(FixedStatement(
                           VariableDeclaration(
                               PointerType(PredefinedType(Token(SyntaxKind.CharKeyword))),
                               SingletonSeparatedList(
                                   VariableDeclarator(
                                       GetMarshalStorageLocationIdentifier(csElement),
                                       null,
                                       EqualsValueClause(IdentifierName(csElement.Name))
                                       )
                                   )
                               ),
                           EmptyStatement()
                           ));
            }

            return(null);
        }
Ejemplo n.º 16
0
        protected ParameterSyntax GenerateManagedValueTypeParameter(CsParameter csElement)
        {
            var param = Parameter(Identifier(csElement.Name));

            if (csElement.IsOut)
            {
                param = param.AddModifiers(Token(SyntaxKind.OutKeyword));
            }
            else if (csElement.PassedByManagedReference)
            {
                param = param.AddModifiers(Token(SyntaxKind.RefKeyword));
            }

            var type = ParseTypeName(csElement.PublicType.QualifiedName);

            if (csElement.IsNullableStruct)
            {
                type = NullableType(type);
            }

            return(param.WithType(type));
        }
 public ParameterSyntax GenerateManagedParameter(CsParameter csElement) =>
 GenerateManagedArrayParameter(csElement);
 public ArgumentSyntax GenerateManagedArgument(CsParameter csElement) =>
 Argument(IdentifierName(csElement.Name));
 public FixedStatementSyntax GeneratePin(CsParameter csElement)
 {
     throw new InvalidOperationException();
 }
 public ParameterSyntax GenerateManagedParameter(CsParameter csElement)
 {
     return(GenerateManagedArrayParameter(csElement));
 }
Ejemplo n.º 21
0
 public FixedStatementSyntax GeneratePin(CsParameter csElement)
 {
     return(null);
 }
Ejemplo n.º 22
0
 public ParameterSyntax GenerateManagedParameter(CsParameter csElement)
 {
     return(GenerateManagedValueTypeParameter(csElement));
 }
Ejemplo n.º 23
0
 public ArgumentSyntax GenerateManagedArgument(CsParameter csElement)
 {
     return(GenerateManagedValueTypeArgument(csElement));
 }
Ejemplo n.º 24
0
 public FixedStatementSyntax GeneratePin(CsParameter csElement) => null;
Ejemplo n.º 25
0
 public ParameterSyntax GenerateManagedParameter(CsParameter csElement) =>
 GenerateManagedValueTypeParameter(csElement);
Ejemplo n.º 26
0
 public ArgumentSyntax GenerateManagedArgument(CsParameter csElement) =>
 GenerateManagedValueTypeArgument(csElement);
Ejemplo n.º 27
0
 public ParameterSyntax GenerateManagedParameter(CsParameter csElement) => Parameter(Identifier(csElement.Name))
 .WithType(ParseTypeName(csElement.PublicType.QualifiedName));
Ejemplo n.º 28
0
 public ParameterSyntax GenerateManagedParameter(CsParameter csElement) => throw new NotSupportedException();
Ejemplo n.º 29
0
 public ArgumentSyntax GenerateManagedArgument(CsParameter csElement) => throw new NotSupportedException();
Ejemplo n.º 30
0
 public FixedStatementSyntax GeneratePin(CsParameter csElement) => throw new NotSupportedException();