Пример #1
0
 public CSProperty(CSType type, CSMethodKind kind,
                   CSVisibility getVis, CSCodeBlock getter,
                   CSVisibility setVis, CSCodeBlock setter, CSParameterList parms)
     : this(type, kind, new CSIdentifier("this"), getVis, getter, setVis, setter,
            Exceptions.ThrowOnNull(parms, nameof(parms)))
 {
 }
 public CSDelegateTypeDecl(CSVisibility vis, CSType type, CSIdentifier name, CSParameterList parms)
 {
     Visibility = vis;
     Type       = type != null ? type : CSSimpleType.Void;
     Name       = Exceptions.ThrowOnNull(name, "name");
     Parameters = parms;
 }
Пример #3
0
 public CSEnum(CSVisibility vis, CSIdentifier name, CSType optionalType)
 {
     Values       = new List <CSBinding> ();
     Name         = Exceptions.ThrowOnNull(name, "name");
     OptionalType = optionalType;
     Visibility   = vis;
 }
Пример #4
0
 public CSProperty(CSType type, CSMethodKind kind, CSIdentifier name,
                   CSVisibility getVis, IEnumerable <ICodeElement> getter,
                   CSVisibility setVis, IEnumerable <ICodeElement> setter)
     : this(type, kind, name,
            getVis, getter != null ? new CSCodeBlock(getter) : null,
            setVis, setter != null ? new CSCodeBlock(setter) : null)
 {
 }
 void DeclInitExposure(CSVisibility vis)
 {
     using (Stream stm = Utils.BasicClass("None", "AClass", null, cl => {
         cl.Fields.Add(CSFieldDeclaration.FieldLine(CSSimpleType.Byte, "b", CSConstant.Val((byte)0), CSVisibility.Public));
         return(cl);
     })) {
         Utils.CompileAStream(stm);
     }
 }
Пример #6
0
        public static CSMethod PInvoke(CSVisibility vis, CSType type, string name, CSBaseExpression dllName, string externName, CSParameterList parms)
        {
            CSMethod method = new CSMethod(vis, CSMethodKind.StaticExtern, Exceptions.ThrowOnNull(type, "type"),
                                           new CSIdentifier(name), parms, null);

            CSAttribute.DllImport(dllName, externName).AttachBefore(method);

            return(method);
        }
Пример #7
0
        public CSMethod(CSVisibility vis, CSMethodKind kind, CSType type, CSIdentifier name,
                        CSParameterList parms, CSBaseExpression[] baseOrThisCallParms, bool callsBase, CSCodeBlock body, bool isSealed = false)
        {
            GenericParameters  = new CSGenericTypeDeclarationCollection();
            GenericConstraints = new CSGenericConstraintCollection();
            Visibility         = vis;
            Kind       = kind;
            Type       = type;       // no throw on null - could be constructor
            Name       = Exceptions.ThrowOnNull(name, nameof(name));
            Parameters = Exceptions.ThrowOnNull(parms, nameof(parms));
            CallsBase  = callsBase;
            BaseOrThisCallParameters = baseOrThisCallParms;

            Body     = body;         // can be null
            IsSealed = isSealed;

            LineCodeElementCollection <ICodeElement> lc = new LineCodeElementCollection <ICodeElement> (new ICodeElement [0], false, true);

            if (vis != CSVisibility.None)
            {
                lc.And(new SimpleElememt(VisibilityToString(vis))).And(SimpleElememt.Spacer);
            }

            if (isSealed)
            {
                lc.And(new SimpleElememt("sealed")).And(SimpleElememt.Spacer);
            }

            lc.And(new SimpleElememt(MethodKindToString(kind))).And(SimpleElememt.Spacer);

            if (type != null)
            {
                lc.And(type).And(SimpleElememt.Spacer);
            }

            lc.And(name).And(GenericParameters).And(new SimpleElememt("(")).And(parms).And(new SimpleElememt(")")).And(GenericConstraints);
            if (body == null)
            {
                if (!(kind == CSMethodKind.StaticExtern || kind == CSMethodKind.Interface))
                {
                    throw new ArgumentException("Method body is only optional when method kind kind is either StaticExtern or Interface",
                                                nameof(body));
                }
                lc.Add(new SimpleElememt(";"));
            }
            Add(lc);
            if (BaseOrThisCallParameters != null)
            {
                Add(new CSFunctionCall(CallsBase ? ": base" : ": this", false, BaseOrThisCallParameters));
            }
            if (body != null)
            {
                Add(body);
            }
        }
Пример #8
0
        static LineCodeElementCollection <ICodeElement> MakeEtter(CSVisibility vis, string getset,
                                                                  bool unifiedVis, bool moreRestrictiveVis)
        {
            LineCodeElementCollection <ICodeElement> getLine = new LineCodeElementCollection <ICodeElement> (null, false, true);

            if (!unifiedVis && vis != CSVisibility.None && moreRestrictiveVis)
            {
                getLine.And(new SimpleElememt(CSMethod.VisibilityToString(vis))).And(SimpleElememt.Spacer);
            }
            return(getLine.And(new SimpleElememt(getset, false)));
        }
Пример #9
0
 public CSInterface(CSVisibility vis, CSIdentifier name,
                    IEnumerable <CSMethod> methods = null)
 {
     Visibility         = vis;
     Name               = Exceptions.ThrowOnNull(name, "name");
     Inheritance        = new CSInheritance();
     Methods            = new List <CSMethod> ();
     Properties         = new List <CSProperty> ();
     GenericParams      = new CSGenericTypeDeclarationCollection();
     GenericConstraints = new CSGenericConstraintCollection();
     if (methods != null)
     {
         Methods.AddRange(methods);
     }
 }
Пример #10
0
 public CSFieldDeclaration(CSType type, IEnumerable <CSBinding> bindings, CSVisibility vis = CSVisibility.None, bool isStatic = false, bool isReadonly = false)
     : base(type, bindings)
 {
     Visibilty = vis;
     IsStatic  = isStatic;
     if (isReadonly)
     {
         this.Insert(0, new SimpleElememt("readonly"));
         this.Insert(1, SimpleElememt.Spacer);
     }
     if (isStatic)
     {
         this.Insert(0, new SimpleElememt("static"));
         this.Insert(1, SimpleElememt.Spacer);
     }
     if (vis != CSVisibility.None)
     {
         this.Insert(0, new SimpleElememt(CSMethod.VisibilityToString(vis)));
         this.Insert(1, SimpleElememt.Spacer);
     }
 }
Пример #11
0
        public static string VisibilityToString(CSVisibility visibility)
        {
            switch (visibility)
            {
            case CSVisibility.None:
                return("");

            case CSVisibility.Internal:
                return("internal");

            case CSVisibility.Private:
                return("private");

            case CSVisibility.Public:
                return("public");

            case CSVisibility.Protected:
                return("protected");

            default:
                throw new ArgumentOutOfRangeException("vis");
            }
        }
Пример #12
0
        public CSClass(CSVisibility vis, CSIdentifier name, IEnumerable <CSMethod> methods = null,
                       bool isStatic = false, bool isSealed = false)
        {
            Visibility         = vis;
            IsStatic           = isStatic;
            IsSealed           = isSealed;
            Name               = Exceptions.ThrowOnNull(name, "name");
            Inheritance        = new CSInheritance();
            Delegates          = new List <CSDelegateTypeDecl> ();
            Fields             = new List <ICodeElement> ();
            Constructors       = new List <CSMethod> ();
            Methods            = new List <CSMethod> ();
            Properties         = new List <CSProperty> ();
            InnerClasses       = new CSClasses();
            InnerEnums         = new List <CSEnum> ();
            StaticConstructor  = new CSCodeBlock();
            GenericParams      = new CSGenericTypeDeclarationCollection();
            GenericConstraints = new CSGenericConstraintCollection();

            if (methods != null)
            {
                Methods.AddRange(methods);
            }
        }
Пример #13
0
 public CSFieldDeclaration(CSType type, string name, ICSExpression value = null, CSVisibility vis = CSVisibility.None, bool isSatic = false, bool isReadonly = false)
     : this(type, new CSIdentifier(name), value, vis, isSatic, isReadonly)
 {
 }
        public CSDelegateTypeDecl CompileToDelegateDeclaration(FunctionDeclaration func, CSUsingPackages packs,
                                                               string mangledName, string delegateName, bool objectsAreIntPtrs, CSVisibility vis, bool isSwiftProtocol)
        {
            bool returnIsGeneric = func.IsTypeSpecGeneric(func.ReturnTypeSpec);
            var  args            = typeMap.MapParameterList(func, func.ParameterLists.Last(), objectsAreIntPtrs, true, null, null);

            RemapSwiftClosureRepresensation(args);
            var returnType = returnIsGeneric ? null : typeMap.MapType(func, func.ReturnTypeSpec, objectsAreIntPtrs, true);

            delegateName = delegateName ?? typeMap.SanitizeIdentifier(func.Name);

            args.ForEach(a => AddUsingBlock(packs, a.Type));

            if (returnType != null && !returnIsGeneric)
            {
                AddUsingBlock(packs, returnType);
            }

            CSType csReturnType = returnType == null || returnType.IsVoid ? CSSimpleType.Void : returnType.ToCSType(packs);
            var    csParams     = new CSParameterList();

            for (int i = 0; i < args.Count; i++)
            {
                var         arg          = args [i];
                var         argIsGeneric = func.IsTypeSpecGeneric(func.ParameterLists.Last() [i].TypeSpec);
                CSParameter csParam      = null;
                var         parmType     = func.ParameterLists.Last() [i].TypeSpec;
                if (arg.Type.Entity == EntityType.Tuple || (!argIsGeneric && IsObjCStruct(parmType)))
                {
                    csParam = new CSParameter(CSSimpleType.IntPtr, new CSIdentifier(arg.Name), CSParameterKind.None, null);
                }
                else
                {
                    csParam = new CSParameter(arg.Type.ToCSType(packs), new CSIdentifier(arg.Name),
                                              arg.Type.IsReference ? CSParameterKind.Ref : CSParameterKind.None, null);
                }
                csParams.Add(csParam);
            }

            if (isSwiftProtocol)
            {
                packs.AddIfNotPresent(typeof(SwiftExistentialContainer1));
                csParams.Insert(0, new CSParameter(new CSSimpleType(typeof(SwiftExistentialContainer1)), new CSIdentifier("self"), CSParameterKind.Ref));
            }
            else
            {
                csParams.Insert(0, new CSParameter(CSSimpleType.IntPtr, new CSIdentifier("self")));
            }

            var retvalName = "xam_retval";
            var retvalID   = new CSIdentifier(retvalName);

            if (func.HasThrows || returnIsGeneric || !returnType.IsVoid)               // && func.Signature.ReturnType.IsStruct || func.Signature.ReturnType.IsEnum) {
            {
                if (func.HasThrows)
                {
                    csParams.Insert(0, new CSParameter(CSSimpleType.IntPtr, retvalName, CSParameterKind.None));
                    csReturnType = CSSimpleType.Void;
                }
                else
                {
                    if (!returnIsGeneric)
                    {
                        if (!(func.ReturnTypeSpec is ClosureTypeSpec))
                        {
                            Entity ent = typeMap.GetEntityForTypeSpec(func.ReturnTypeSpec);
                            if (ent == null && !(func.ReturnTypeSpec is ProtocolListTypeSpec))
                            {
                                throw ErrorHelper.CreateError(ReflectorError.kCompilerBase + 8, $"Unable to find entity for class {csReturnType.ToString ()}.");
                            }

                            if (ent != null && (ent.IsStructOrEnum || ent.EntityType == EntityType.Protocol))
                            {
                                csParams.Insert(0, new CSParameter(CSSimpleType.IntPtr, retvalID, CSParameterKind.None));
                                csReturnType = CSSimpleType.Void;
                            }
                            else if (func.ReturnTypeSpec is ProtocolListTypeSpec pl)
                            {
                                csParams.Insert(0, new CSParameter(new CSSimpleType($"SwiftExistentialContainer{pl.Protocols.Count}"), retvalID, CSParameterKind.Ref));
                                csReturnType = CSSimpleType.Void;
                            }
                        }
                    }
                    else
                    {
                        csParams.Insert(0, new CSParameter(CSSimpleType.IntPtr, retvalID, CSParameterKind.None));
                    }
                }
            }

            return(new CSDelegateTypeDecl(vis, csReturnType, new CSIdentifier(delegateName), csParams));
        }
Пример #15
0
 public CSEnum(CSVisibility vis, string name, CSType optionalType)
     : this(vis, new CSIdentifier(name), optionalType)
 {
 }
Пример #16
0
        CSProperty(CSType type, CSMethodKind kind, CSIdentifier name,
                   CSVisibility getVis, CSCodeBlock getter,
                   CSVisibility setVis, CSCodeBlock setter, CSParameterList parms)
        {
            bool unifiedVis = getVis == setVis;

            IndexerParameters = parms;

            LineCodeElementCollection <ICodeElement> decl = new LineCodeElementCollection <ICodeElement> (null, false, true);

            GetterVisibility = getVis;
            SetterVisibility = setVis;
            CSVisibility bestVis = (CSVisibility)Math.Min((int)getVis, (int)setVis);

            decl.And(new SimpleElememt(CSMethod.VisibilityToString(bestVis))).And(SimpleElememt.Spacer);
            if (kind != CSMethodKind.None)
            {
                decl.And(new SimpleElememt(CSMethod.MethodKindToString(kind))).And(SimpleElememt.Spacer);
            }

            PropType = type;
            Name     = name;

            decl.And(Exceptions.ThrowOnNull(type, "type")).And(SimpleElememt.Spacer)
            .And(Exceptions.ThrowOnNull(name, nameof(name)));
            if (parms != null)
            {
                decl.And(new SimpleElememt("[", true)).And(parms).And(new SimpleElememt("]"));
            }
            Add(decl);


            CSCodeBlock cb = new CSCodeBlock(null);

            if (getter != null)
            {
                Getter = getter;
                LineCodeElementCollection <ICodeElement> getLine = MakeEtter(getVis, "get", unifiedVis, getVis > setVis);
                cb.Add(getLine);
                if (getter.Count() == 0)
                {
                    getLine.Add(new SimpleElememt(";"));
                }
                else
                {
                    cb.Add(getter);
                }
            }
            if (setter != null)
            {
                Setter = setter;
                LineCodeElementCollection <ICodeElement> setLine = MakeEtter(setVis, "set", unifiedVis, setVis > getVis);
                cb.Add(setLine);
                if (setter.Count() == 0)
                {
                    setLine.Add(new SimpleElememt(";"));
                }
                else
                {
                    cb.Add(setter);
                }
            }

            Add(cb);
        }
Пример #17
0
 public CSProperty(CSType type, CSMethodKind kind, CSIdentifier name,
                   CSVisibility getVis, CSCodeBlock getter,
                   CSVisibility setVis, CSCodeBlock setter)
     : this(type, kind, name, getVis, getter, setVis, setter, null)
 {
 }
Пример #18
0
 public CSStruct(CSVisibility vis, CSIdentifier name, IEnumerable <CSMethod> methods = null,
                 bool isStatic = false, bool isSealed = false)
     : base(vis, name, methods, isStatic, isSealed)
 {
 }
Пример #19
0
 public CSFieldDeclaration(CSType type, CSIdentifier name, ICSExpression value = null, CSVisibility vis = CSVisibility.None, bool isStatic = false, bool isReadOnly = false)
     : this(type, new CSBinding [] { new CSBinding(name, value) }, vis, isStatic, isReadOnly)
 {
 }
Пример #20
0
 public CSMethod(CSVisibility vis, CSMethodKind kind, CSType type, CSIdentifier name, CSParameterList parms, CSCodeBlock body)
     : this(vis, kind, type, name, parms, null, false, body)
 {
 }
Пример #21
0
 public CSClass(CSVisibility vis, string name,
                IEnumerable <CSMethod> members = null, bool isStatic = false, bool isSealed = false)
     : this(vis, new CSIdentifier(name), members, isStatic, isSealed)
 {
 }
Пример #22
0
 public static CSLine FieldLine(CSType type, CSIdentifier name, ICSExpression value = null, CSVisibility vis = CSVisibility.None, bool isStatic = false)
 {
     return(new CSLine(new CSFieldDeclaration(type, name, value, vis, isStatic)));
 }
Пример #23
0
 public CSInterface(CSVisibility vis, string name, IEnumerable <CSMethod> methods = null)
     : this(vis, new CSIdentifier(name), methods)
 {
 }