Exemple #1
0
        private FunctionType GetNewFunctionType(ITypedDecl decl, QualifiedType type)
        {
            var functionType = new FunctionType();
            var method       = decl as Method;

            if (method != null && method.FunctionType == type)
            {
                functionType.Parameters.AddRange(
                    method.GatherInternalParams(Context.ParserOptions.IsItaniumLikeAbi, true));
                functionType.CallingConvention = method.CallingConvention;
                functionType.IsDependent       = method.IsDependent;
                functionType.ReturnType        = method.ReturnType;
            }
            else
            {
                var funcTypeParam = (FunctionType)decl.Type.Desugar().GetFinalPointee().Desugar();
                functionType = new FunctionType(funcTypeParam);
            }

            for (int i = 0; i < functionType.Parameters.Count; i++)
            {
                functionType.Parameters[i].Name = $"_{i}";
            }

            return(functionType);
        }
Exemple #2
0
        private QualifiedType CheckForDelegate(QualifiedType type, ITypedDecl decl)
        {
            if (type.Type is TypedefType)
            {
                return(type);
            }

            var desugared = type.Type.Desugar();

            if (desugared.IsDependent)
            {
                return(type);
            }

            Type pointee = desugared.GetPointee() ?? desugared;

            if (pointee is TypedefType)
            {
                return(type);
            }

            var functionType = pointee.Desugar() as FunctionType;

            if (functionType == null)
            {
                return(type);
            }

            TypedefDecl @delegate = GetDelegate(type, decl);

            return(new QualifiedType(new TypedefType {
                Declaration = @delegate
            }));
        }
 private static string GetOriginalParameterType(ITypedDecl parameter)
 {
     Class decl;
     return parameter.Type.Desugar().SkipPointerRefs().Desugar().TryGetClass(out decl)
         ? decl.QualifiedOriginalName
         : parameter.Type.ToString();
 }
Exemple #4
0
        private TypedefDecl GetDelegate(QualifiedType type, ITypedDecl typedDecl)
        {
            var          decl            = (Declaration)typedDecl;
            string       delegateName    = null;
            FunctionType newFunctionType = GetNewFunctionType(typedDecl, type);

            if (Options.GenerateRawCBindings && decl.Namespace is Function)
            {
                delegateName = $"{decl.Namespace.Name}_delegate";
            }

            else
            {
                delegateName = GetDelegateName(newFunctionType, TypePrinter);
            }
            var access = typedDecl is Method ? AccessSpecifier.Private : AccessSpecifier.Public;

            Module module           = decl.TranslationUnit.Module;
            var    existingDelegate = delegates.Find(t => Match(t, delegateName, module));

            if (existingDelegate != null)
            {
                // Ensure a delegate used for a virtual method and a type is public
                if (existingDelegate.Access == AccessSpecifier.Private &&
                    access == AccessSpecifier.Public)
                {
                    existingDelegate.Access = access;
                }

                // Check if there is an existing delegate with a different calling convention
                if (((FunctionType)existingDelegate.Type.GetPointee()).CallingConvention ==
                    newFunctionType.CallingConvention)
                {
                    return(existingDelegate);
                }

                // Add a new delegate with the calling convention appended to its name
                delegateName    += '_' + newFunctionType.CallingConvention.ToString();
                existingDelegate = delegates.Find(t => Match(t, delegateName, module));
                if (existingDelegate != null)
                {
                    return(existingDelegate);
                }
            }

            var namespaceDelegates = GetDeclContextForDelegates(decl.Namespace);
            var delegateType       = new QualifiedType(new PointerType(new QualifiedType(newFunctionType)));

            existingDelegate = new TypedefDecl
            {
                Access        = access,
                Name          = delegateName,
                Namespace     = namespaceDelegates,
                QualifiedType = delegateType,
                IsSynthetized = true
            };
            delegates.Add(existingDelegate);

            return(existingDelegate);
        }
Exemple #5
0
        private static string GetOriginalParameterType(ITypedDecl parameter)
        {
            Class decl;

            return(parameter.Type.Desugar().SkipPointerRefs().Desugar().TryGetClass(out decl)
                ? decl.QualifiedOriginalName
                : parameter.Type.ToString());
        }
Exemple #6
0
        static Class GetClassFromTypedef(ITypedDecl typedef)
        {
            var type = typedef.Type.Desugar() as TagType;

            if (type == null)
            {
                return(null);
            }

            var @class = type.Declaration as Class;

            return(@class.IsIncomplete ?
                   (@class.CompleteDeclaration as Class) : @class);
        }
Exemple #7
0
        private TypedefDecl GetDelegate(QualifiedType type, ITypedDecl decl)
        {
            FunctionType newFunctionType = GetNewFunctionType(decl, type);

            var delegateName     = GetDelegateName(newFunctionType);
            var access           = decl is Method ? AccessSpecifier.Private : AccessSpecifier.Public;
            var existingDelegate = delegates.SingleOrDefault(t => t.Name == delegateName);

            if (existingDelegate != null)
            {
                // Ensure a delegate used for a virtual method and a type is public
                if (existingDelegate.Access == AccessSpecifier.Private &&
                    access == AccessSpecifier.Public)
                {
                    existingDelegate.Access = access;
                }

                // Check if there is an existing delegate with a different calling convention
                if (((FunctionType)existingDelegate.Type.GetPointee()).CallingConvention ==
                    newFunctionType.CallingConvention)
                {
                    return(existingDelegate);
                }

                // Add a new delegate with the calling convention appended to its name
                delegateName    += '_' + newFunctionType.CallingConvention.ToString();
                existingDelegate = delegates.SingleOrDefault(t => t.Name == delegateName);
                if (existingDelegate != null)
                {
                    return(existingDelegate);
                }
            }

            var namespaceDelegates = GetDeclContextForDelegates(((Declaration)decl).Namespace);
            var delegateType       = new QualifiedType(new PointerType(new QualifiedType(newFunctionType)));

            existingDelegate = new TypedefDecl
            {
                Access        = access,
                Name          = delegateName,
                Namespace     = namespaceDelegates,
                QualifiedType = delegateType,
                IsSynthetized = true
            };
            delegates.Add(existingDelegate);

            return(existingDelegate);
        }
Exemple #8
0
 public static void CSharpMarshalToManaged(this ITypedDecl decl,
                                           CSharpMarshalNativeToManagedPrinter printer)
 {
     CSharpMarshalToManaged(decl.QualifiedType, printer);
 }
Exemple #9
0
 /// <summary>
 /// Checks if a given type is invalid, which can happen for a number of
 /// reasons: incomplete definitions, being explicitly ignored, or also
 /// by being a type we do not know how to handle.
 /// </summary>
 private bool HasInvalidType(ITypedDecl decl, out string msg)
 {
     return(HasInvalidType(decl.Type, (Declaration)decl, out msg));
 }
        static Class GetClassFromTypedef(ITypedDecl typedef)
        {
            var type = typedef.Type.Desugar() as TagType;
            if (type == null)
                return null;

            var @class = type.Declaration as Class;

            return @class.IsIncomplete ?
                (@class.CompleteDeclaration as Class) : @class; 
        }
 private static string GetOriginalParameterType(ITypedDecl parameter)
 {
     Declaration decl;
     return parameter.Type.IsTagDecl(out decl) ? decl.QualifiedOriginalName : parameter.Type.ToString();
 }
Exemple #12
0
        public override bool VisitFieldDecl(Field field)
        {
            if (field.Ignore)
            {
                return(false);
            }

            TypeMap typeMap;

            if (!typeMapDatabase.FindTypeMap(field.Type, out typeMap))
            {
                return(false);
            }

            if (!(typeMap is EventMap))
            {
                return(false);
            }

            field.ExplicityIgnored = true;

            ITypedDecl decl = field;

            var typedef = field.Type as TypedefType;

            if (typedef != null)
            {
                return(false);
            }

            var template = decl.Type as TemplateSpecializationType;
            var @params  = template.Arguments.Select(param =>
                                                     new Parameter()
            {
                QualifiedType = param.Type
            }).ToList();

            if (template.Template.TemplatedDecl.Name.StartsWith("Delegate"))
            {
                return(false);
            }

            // Clean up the event name.
            var names = StringHelpers.SplitCamelCase(field.Name);

            if (names.Length > 1 &&
                names[0].Equals("On", StringComparison.InvariantCultureIgnoreCase))
            {
                names = names.Skip(1).ToArray();
            }

            var @event = new Event()
            {
                Name          = string.Join(string.Empty, names),
                OriginalName  = field.OriginalName,
                Namespace     = field.Namespace,
                QualifiedType = field.QualifiedType,
                Parameters    = @params
            };

            field.Class.Events.Add(@event);

            return(true);
        }