Exemplo n.º 1
0
        public override void ExitPointerType(GoParser.PointerTypeContext context)
        {
            string name = context.GetText();

            if (!Types.TryGetValue(context.type_(), out TypeInfo typeInfo))
            {
                AddWarning(context, $"Failed to find pointer type info for \"{name}\"");
                return;
            }

            if (name.StartsWith("**"))
            {
                int count = 0;

                while (name[count] == '*')
                {
                    count++;
                }

                count = count - (typeInfo.IsByRefPointer ? 1 : 0);

                string prefix = string.Join("", Enumerable.Range(0, count).Select(i => "ptr<"));
                string suffix = new string('>', count);

                typeInfo = ConvertByRefToBasicPointer(typeInfo);

                Types[context.Parent.Parent] = new TypeInfo
                {
                    Name           = name,
                    TypeName       = $"{prefix}{typeInfo.TypeName}{suffix}",
                    FullTypeName   = $"{prefix}{typeInfo.FullTypeName}{suffix}",
                    IsPointer      = true,
                    IsByRefPointer = false,
                    TypeClass      = TypeClass.Simple
                };
            }
            else if (name.StartsWith("*(*") && name.EndsWith(")"))
            {
                typeInfo = ConvertByRefToNativePointer(typeInfo);

                Types[context.Parent.Parent] = new TypeInfo
                {
                    Name           = name,
                    TypeName       = $"*({typeInfo.TypeName})",
                    FullTypeName   = $"*({typeInfo.FullTypeName})",
                    IsPointer      = true,
                    IsByRefPointer = false,
                    TypeClass      = TypeClass.Simple
                };

                UsesUnsafePointers = true;
            }
            else
            {
                Types[context.Parent.Parent] = new TypeInfo
                {
                    Name           = name,
                    TypeName       = $"ref {typeInfo.TypeName}",
                    FullTypeName   = $"ref {typeInfo.FullTypeName}",
                    IsPointer      = true,
                    IsByRefPointer = true,
                    TypeClass      = TypeClass.Simple
                };
            }
        }
Exemplo n.º 2
0
        public override void ExitPointerType(GoParser.PointerTypeContext context)
        {
            string name = context.GetText();

            if (!Types.TryGetValue(context.type_(), out TypeInfo typeInfo))
            {
                AddWarning(context, $"Failed to find pointer type info for \"{name}\"");
                return;
            }

            if (typeInfo.TypeClass == TypeClass.Function)
            {
                typeInfo = typeInfo.Clone();
                typeInfo.IsDerefPointer      = true;
                Types[context.Parent.Parent] = typeInfo;
                return;
            }

            typeInfo = ConvertByRefToBasicPointer(typeInfo);

            Types[context.Parent.Parent] = new PointerTypeInfo
            {
                Name           = name,
                TypeName       = $"ptr<{typeInfo.TypeName}>",
                FullTypeName   = $"ptr<{typeInfo.FullTypeName}>",
                TypeClass      = TypeClass.Simple,
                TargetTypeInfo = typeInfo
            };

            // Native unsafe pointer option
            //if (name.StartsWith("*(*") && name.EndsWith(")"))
            //{
            //    typeInfo = ConvertByRefToNativePointer(typeInfo);

            //    Types[context.Parent.Parent] = new TypeInfo
            //    {
            //        Name = name,
            //        TypeName = $"*({typeInfo.TypeName})",
            //        FullTypeName = $"*({typeInfo.FullTypeName})",
            //        IsPointer = true,
            //        IsByRefPointer = false,
            //        TypeClass = TypeClass.Simple
            //    };

            //    UsesUnsafePointers = true;
            //}


            // ByRef pointer option
            //else
            //{
            //    Types[context.Parent.Parent] = new TypeInfo
            //    {
            //        Name = name,
            //        TypeName = $"ref {typeInfo.TypeName}",
            //        FullTypeName = $"ref {typeInfo.FullTypeName}",
            //        IsPointer = true,
            //        IsByRefPointer = true,
            //        TypeClass = TypeClass.Simple
            //    };
            //}
        }