Beispiel #1
0
        private void GenerateClassMethods(Class @class, Class realOwner)
        {
            if (@class.IsValueType)
            {
                foreach (var @base in @class.Bases.Where(b => b.IsClass && !b.Class.Ignore))
                {
                    GenerateClassMethods(@base.Class, realOwner);
                }
            }

            foreach (var method in @class.Methods.Where(m => @class == realOwner || !m.IsOperator))
            {
                if (ASTUtils.CheckIgnoreMethod(method) || CLIHeaders.FunctionIgnored(method))
                {
                    continue;
                }

                // C++/CLI does not allow special member funtions for value types.
                if (@class.IsValueType && method.IsCopyConstructor)
                {
                    continue;
                }

                // Do not generate constructors or destructors from base classes.
                var declaringClass = method.Namespace as Class;
                if (declaringClass != realOwner && (method.IsConstructor || method.IsDestructor))
                {
                    continue;
                }

                GenerateMethod(method, realOwner);
            }
        }
Beispiel #2
0
        public void GenerateFunction(Function function, DeclarationContext @namespace)
        {
            if (!function.IsGenerated || CLIHeaders.FunctionIgnored(function))
            {
                return;
            }

            GenerateDeclarationCommon(function);

            var classSig = string.Format("{0}::{1}", QualifiedIdentifier(@namespace),
                                         TranslationUnit.FileNameWithoutExtension);

            Write("{0} {1}::{2}(", function.ReturnType, classSig,
                  function.Name);

            for (var i = 0; i < function.Parameters.Count; ++i)
            {
                var param = function.Parameters[i];
                Write("{0}", CTypePrinter.VisitParameter(param));
                if (i < function.Parameters.Count - 1)
                {
                    Write(", ");
                }
            }

            WriteLine(")");
            WriteOpenBraceAndIndent();

            GenerateFunctionCall(function);

            UnindentAndWriteCloseBrace();
        }
Beispiel #3
0
        public override List<Template> Generate(IEnumerable<TranslationUnit> units)
        {
            var outputs = new List<Template>();

            var header = new CLIHeaders(Driver, units);
            outputs.Add(header);

            var source = new CLISources(Driver, units);
            outputs.Add(source);

            return outputs;
        }
Beispiel #4
0
        public override List <CodeGenerator> Generate(IEnumerable <TranslationUnit> units)
        {
            var outputs = new List <CodeGenerator>();

            var header = new CLIHeaders(Context, units);

            outputs.Add(header);

            var source = new CLISources(Context, units);

            outputs.Add(source);

            return(outputs);
        }
Beispiel #5
0
        private void GenerateClassProperties(Class @class, Class realOwner)
        {
            if (@class.IsValueType)
            {
                foreach (var @base in @class.Bases.Where(b => b.IsClass && b.Class.IsDeclared))
                {
                    GenerateClassProperties(@base.Class, realOwner);
                }
            }

            foreach (var property in @class.Properties.Where(
                         p => !ASTUtils.CheckIgnoreProperty(p) && !p.IsInRefTypeAndBackedByValueClassField() &&
                         !CLIHeaders.TypeIgnored(p.Type)))
            {
                GenerateProperty(property, realOwner);
            }
        }
Beispiel #6
0
        private void GenerateStructMarshaling(Class @class, string nativeVar)
        {
            foreach (var @base in @class.Bases.Where(b => b.IsClass && b.Class.IsDeclared))
            {
                GenerateStructMarshaling(@base.Class, nativeVar);
            }

            int paramIndex = 0;

            foreach (var property in @class.Properties.Where(
                         p => !ASTUtils.CheckIgnoreProperty(p) && !CLIHeaders.TypeIgnored(p.Type)))
            {
                if (property.Field == null)
                {
                    continue;
                }

                var nativeField = string.Format("{0}{1}",
                                                nativeVar, property.Field.OriginalName);

                var ctx = new MarshalContext(Context)
                {
                    ArgName        = property.Name,
                    ReturnVarName  = nativeField,
                    ReturnType     = property.QualifiedType,
                    Declaration    = property.Field,
                    ParameterIndex = paramIndex++
                };

                var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
                property.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                {
                    Write(marshal.Context.Before);
                }

                WriteLine("{0} = {1};", property.Field.Name, marshal.Context.Return);
            }
        }
Beispiel #7
0
        public void GenerateMethod(Method method, Class @class)
        {
            if (CLIHeaders.FunctionIgnored(method))
            {
                return;
            }
            PushBlock(BlockKind.Method, method);

            if (method.IsConstructor || method.IsDestructor ||
                method.OperatorKind == CXXOperatorKind.Conversion ||
                method.OperatorKind == CXXOperatorKind.ExplicitConversion)
            {
                Write("{0}::{1}(", QualifiedIdentifier(@class), GetMethodName(method));
            }
            else
            {
                Write("{0} {1}::{2}(", method.ReturnType, QualifiedIdentifier(@class),
                      method.Name);
            }

            GenerateMethodParameters(method);

            WriteLine(")");

            if (method.IsConstructor)
            {
                GenerateClassConstructorBase(@class, method: method);
            }

            WriteOpenBraceAndIndent();

            PushBlock(BlockKind.MethodBody, method);

            if (method.IsConstructor && @class.IsRefType)
            {
                PushBlock(BlockKind.ConstructorBody, @class);

                WriteLine("{0} = true;", Helpers.OwnsNativeInstanceIdentifier);
            }

            if (method.IsProxy)
            {
                goto SkipImpl;
            }

            if (@class.IsRefType)
            {
                if (method.IsConstructor)
                {
                    if ([email protected])
                    {
                        var @params = GenerateFunctionParamsMarshal(method.Parameters, method);
                        Write($@"NativePtr = new {typePrinter.PrintTag(@class)}::{
                            @class.QualifiedOriginalName}(");
                        GenerateFunctionParams(@params);
                        WriteLine(");");
                    }

                    PopBlock();
                }
                else
                {
                    GenerateFunctionCall(method, @class);
                }
            }
            else if (@class.IsValueType)
            {
                if (!method.IsConstructor)
                {
                    GenerateFunctionCall(method, @class);
                }
                else
                {
                    GenerateValueTypeConstructorCall(method, @class);
                }
            }

SkipImpl:

            PopBlock();

            UnindentAndWriteCloseBrace();

            if (method.OperatorKind == CXXOperatorKind.EqualEqual)
            {
                GenerateEquals(method, @class);
            }

            PopBlock(NewLineKind.Always);
        }