示例#1
0
 static bool IsNestable(BaseDeclaration bdl)
 {
     return(bdl is ClassDeclaration || bdl is StructDeclaration ||
            bdl is EnumDeclaration
            );
 }
示例#2
0
 SLType MapType(BaseDeclaration declContext, SLImportModules modules, TupleTypeSpec spec)
 {
     return(new SLTupleType(spec.Elements.Select(p => new SLNameTypePair((string)null, MapType(declContext, modules, p, false))).ToList()));
 }
示例#3
0
 SLType MapType(BaseDeclaration declContext, SLImportModules modules, ProtocolListTypeSpec spec)
 {
     return(new SLProtocolListType(spec.Protocols.Keys.Select(proto => MapType(declContext, modules, proto))));
 }
示例#4
0
        SLType MapType(BaseDeclaration declContext, SLImportModules modules, ClosureTypeSpec spec, bool isForReturn)
        {
            if (spec.Throws)
            {
                throw ErrorHelper.CreateError(ReflectorError.kTypeMapBase + 16, $"In {declContext.ToFullyQualifiedName ()}, closure type {spec.ToString ()} throws, which is not supported yet.");
            }
            var argumentTypes = new List <SLType> ();

            if (spec.Arguments is TupleTypeSpec)
            {
                argumentTypes.AddRange(((TupleTypeSpec)spec.Arguments).Elements.Select(arg => MapType(declContext, modules, arg, false)));
            }
            else
            {
                argumentTypes.Add(MapType(declContext, modules, spec.Arguments, false));
            }


            SLFuncType funcType = null;

            if (isForOverride)
            {
                var arguments = new SLTupleType(argumentTypes.Select(at => new SLNameTypePair((string)null, at)).ToList());
                if (spec.ReturnType.IsEmptyTuple)
                {
                    // Action ->
                    funcType = new SLFuncType(arguments ?? new SLTupleType(), new SLTupleType());
                }
                else
                {
                    // Func
                    SLType slRetType = MapType(declContext, modules, spec.ReturnType, true);
                    funcType = new SLFuncType(arguments ?? new SLTupleType(), slRetType);
                }
                if (spec.IsEscaping)
                {
                    SLAttribute.Escaping().AttachBefore(funcType);
                }
            }
            else if (isForReturn)
            {
                var arguments = argumentTypes.Select(at => new SLNameTypePair("_", at)).ToList();

                if (spec.ReturnType.IsEmptyTuple)
                {
                    // Action ->
                    // (UnsafeMutablePointer<(argumentTypes)>) -> ()
                    var pointerToArgTuple = arguments.Count != 0 ?
                                            new SLBoundGenericType("UnsafeMutablePointer", new SLTupleType(arguments))
                                                                         : null;
                    if (pointerToArgTuple != null)
                    {
                        funcType = new SLFuncType(new SLTupleType(new SLNameTypePair("_", pointerToArgTuple)),
                                                  new SLTupleType());
                    }
                    else                         // should never happen?
                    {
                        funcType = new SLFuncType(new SLTupleType(), new SLTupleType());
                    }
                }
                else
                {
                    // Func
                    // (UnsafeMutablePointer<returnType>,UnsafeMutablePointer<(argumentTypes)>) -> ()
                    SLType slRetType         = MapType(declContext, modules, spec.ReturnType, true);
                    var    pointerToArgTuple = arguments.Count != 0 ?
                                               new SLBoundGenericType("UnsafeMutablePointer", new SLTupleType(arguments))
                                                                         : null;
                    if (pointerToArgTuple != null)
                    {
                        funcType = new SLFuncType(new SLTupleType(new SLNameTypePair("_", new SLBoundGenericType("UnsafeMutablePointer", slRetType)),
                                                                  new SLNameTypePair("_", pointerToArgTuple)),
                                                  new SLTupleType());
                    }
                    else
                    {
                        funcType = new SLFuncType(new SLTupleType(new SLNameTypePair("_", new SLBoundGenericType("UnsafeMutablePointer", slRetType))),
                                                  new SLTupleType());
                    }
                }
            }
            else
            {
                var arguments        = argumentTypes.Select(at => new SLNameTypePair("_", at)).ToList();
                var opaquePointerArg = new SLNameTypePair("_", SLSimpleType.OpaquePointer);

                if (spec.ReturnType.IsEmptyTuple)
                {
                    // Action ->
                    // (UnsafeMutablePointer<(argumentTypes)>, OpaquePointer) -> ()
                    var pointerToArgTuple = arguments.Count != 0 ?
                                            new SLBoundGenericType("UnsafeMutablePointer", new SLTupleType(arguments))
                                                                         : null;
                    if (pointerToArgTuple != null)
                    {
                        funcType = new SLFuncType(new SLTupleType(new SLNameTypePair("_", pointerToArgTuple), opaquePointerArg),
                                                  new SLTupleType());
                    }
                    else                         // should never happen?
                    {
                        funcType = new SLFuncType(new SLTupleType(opaquePointerArg), new SLTupleType());
                    }
                }
                else
                {
                    // Func
                    // (UnsafeMutablePointer<returnType>,UnsafeMutablePointer<(argumentTypes)>) -> ()
                    SLType slRetType         = MapType(declContext, modules, spec.ReturnType, true);
                    var    pointerToArgTuple = arguments.Count != 0 ?
                                               new SLBoundGenericType("UnsafeMutablePointer", new SLTupleType(arguments))
                                                                         : null;
                    if (pointerToArgTuple != null)
                    {
                        funcType = new SLFuncType(new SLTupleType(new SLNameTypePair("_", new SLBoundGenericType("UnsafeMutablePointer", slRetType)),
                                                                  new SLNameTypePair("_", pointerToArgTuple),
                                                                  opaquePointerArg),
                                                  new SLTupleType());
                    }
                    else
                    {
                        funcType = new SLFuncType(new SLTupleType(new SLNameTypePair("_", new SLBoundGenericType("UnsafeMutablePointer", slRetType)),
                                                                  opaquePointerArg),
                                                  new SLTupleType());
                    }
                }

                if (!isForReturn)
                {
                    funcType.Attributes.Add(new SLAttribute("escaping", null));
                }
            }


            return(funcType);
        }