Beispiel #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)))
 {
 }
Beispiel #2
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)
 {
 }
        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);
            }
        }
        public CSProperty CompileProperty(string propertyName, CSUsingPackages packs, SwiftType swiftPropertyType, bool hasGetter, bool hasSetter,
                                          CSMethodKind methodKind)
        {
            propertyName = typeMap.SanitizeIdentifier(propertyName);
            NetTypeBundle propertyType = typeMap.MapType(swiftPropertyType, false);

            if (!(swiftPropertyType is SwiftGenericArgReferenceType))
            {
                AddUsingBlock(packs, propertyType);
            }
            ICodeElement [] uselessLine = new ICodeElement [] { CSReturn.ReturnLine(new CSIdentifier("useless")) };

            CSCodeBlock getterBlock = null;

            if (hasGetter)
            {
                getterBlock = new CSCodeBlock(uselessLine);
            }
            CSCodeBlock setterBlock = null;

            if (hasSetter)
            {
                setterBlock = new CSCodeBlock(uselessLine);
            }

            CSProperty theProp = new CSProperty(propertyType.ToCSType(packs), methodKind,
                                                new CSIdentifier(propertyName), CSVisibility.Public, getterBlock, CSVisibility.Public, setterBlock);

            if (getterBlock != null)
            {
                getterBlock.Clear();
            }
            if (setterBlock != null)
            {
                setterBlock.Clear();
            }

            return(theProp);
        }
        public static string MethodKindToString(CSMethodKind kind)
        {
            switch (kind)
            {
            case CSMethodKind.None:
            case CSMethodKind.Interface:
                return("");

            case CSMethodKind.Extern:
                return("extern");

            case CSMethodKind.New:
                return("new");

            case CSMethodKind.Override:
                return("override");

            case CSMethodKind.Static:
                return("static");

            case CSMethodKind.StaticExtern:
                return("static extern");

            case CSMethodKind.StaticNew:
                return("static new");

            case CSMethodKind.Virtual:
                return("virtual");

            case CSMethodKind.Abstract:
                return("abstract");

            default:
                throw new ArgumentOutOfRangeException(nameof(kind));
            }
        }
Beispiel #6
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);
        }
Beispiel #7
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)
 {
 }
Beispiel #8
0
        public static CSProperty PublicGetBacking(CSType type, CSIdentifier name, CSIdentifier backingFieldName,
                                                  bool includeBackingFieldDeclaration = false, CSMethodKind methodKind = CSMethodKind.None)
        {
            LineCodeElementCollection <ICodeElement> getCode =
                new LineCodeElementCollection <ICodeElement> (
                    new ICodeElement [] {
                CSReturn.ReturnLine(Exceptions.ThrowOnNull(backingFieldName, nameof(backingFieldName)))
            }, false, true);
            CSProperty prop = new CSProperty(type, methodKind, Exceptions.ThrowOnNull(name, nameof(name)),
                                             CSVisibility.Public, new CSCodeBlock(getCode),
                                             CSVisibility.Public, null);

            if (includeBackingFieldDeclaration)
            {
                prop.Insert(0, CSFieldDeclaration.FieldLine(type, backingFieldName));
            }
            return(prop);
        }
 public static CSMethod PublicMethod(CSMethodKind kind, CSType type, string name, CSParameterList parms, CSCodeBlock body)
 {
     return(new CSMethod(CSVisibility.Public, kind, type, new CSIdentifier(name), parms, Exceptions.ThrowOnNull(body, "body")));
 }
 public CSMethod(CSVisibility vis, CSMethodKind kind, CSType type, CSIdentifier name, CSParameterList parms, CSCodeBlock body)
     : this(vis, kind, type, name, parms, null, false, body)
 {
 }
        public CSProperty CompileProperty(CSUsingPackages packs, string propertyName,
                                          FunctionDeclaration getter, FunctionDeclaration setter, CSMethodKind methodKind = CSMethodKind.None)
        {
            var           swiftPropertyType = GetPropertyType(getter, setter);
            NetTypeBundle propertyType      = null;

            if (TypeMapper.IsCompoundProtocolListType(swiftPropertyType))
            {
                propertyType = new NetTypeBundle("System", "object", false, false, EntityType.ProtocolList);
            }
            else
            {
                propertyType = typeMap.MapType(getter, swiftPropertyType, false, true);
            }
            propertyName = propertyName ?? typeMap.SanitizeIdentifier(getter != null ? getter.PropertyName : setter.PropertyName);
            bool isSubscript = getter != null ? getter.IsSubscript :
                               setter.IsSubscript;

            if (!getter.IsTypeSpecGeneric(swiftPropertyType))
            {
                AddUsingBlock(packs, propertyType);
            }

            var uselessLine = new ICodeElement [] { CSReturn.ReturnLine(new CSIdentifier("useless")) };

            CSCodeBlock getterBlock = null;

            if (getter != null)
            {
                getterBlock = new CSCodeBlock(uselessLine);
            }
            CSCodeBlock setterBlock = null;

            if (setter != null)
            {
                setterBlock = new CSCodeBlock(uselessLine);
            }

            CSProperty theProp = null;

            if (isSubscript)
            {
                List <ParameterItem> swiftParms = null;
                if (getter != null)
                {
                    swiftParms = getter.ParameterLists [1];
                }
                else
                {
                    swiftParms = setter.ParameterLists [1].Skip(1).ToList();
                }
                var args = typeMap.MapParameterList(getter, swiftParms, false, false, null, null);
                args.ForEach(a => AddUsingBlock(packs, a.Type));

                var csParams =
                    new CSParameterList(
                        args.Select(a =>
                                    new CSParameter(a.Type.ToCSType(packs),
                                                    new CSIdentifier(a.Name), a.Type.IsReference ? CSParameterKind.Ref : CSParameterKind.None, null)));
                theProp = new CSProperty(propertyType.ToCSType(packs), methodKind, CSVisibility.Public, getterBlock,
                                         CSVisibility.Public, setterBlock, csParams);
            }
            else
            {
                theProp = new CSProperty(propertyType.ToCSType(packs), methodKind,
                                         new CSIdentifier(propertyName), CSVisibility.Public, getterBlock, CSVisibility.Public, setterBlock);
            }
            if (getterBlock != null)
            {
                getterBlock.Clear();
            }
            if (setterBlock != null)
            {
                setterBlock.Clear();
            }

            return(theProp);
        }