예제 #1
0
        public override bool Define()
        {
            if (!base.Define())
            {
                return(false);
            }

            if (!BuiltinTypeSpec.IsPrimitiveType(MemberType))
            {
                Report.Error(1663, Location,
                             "`{0}': Fixed size buffers type must be one of the following: bool, byte, short, int, long, char, sbyte, ushort, uint, ulong, float or double",
                             GetSignatureForError());
            }
            else if (declarators != null)
            {
                var t = new TypeExpression(MemberType, TypeExpression.Location);
                foreach (var d in declarators)
                {
                    var f = new FixedField(Parent, t, ModFlags, new MemberName(d.Name.Value, d.Name.Location), OptAttributes);
                    f.initializer = d.Initializer;
                    ((ConstInitializer)f.initializer).Name = d.Name.Value;
                    f.Define();
                    Parent.PartialContainer.Members.Add(f);
                }
            }

            // Create nested fixed buffer container
            string name = String.Format("<{0}>__FixedBuffer{1}", Name, GlobalCounter++);

            fixed_buffer_type = Parent.TypeBuilder.DefineNestedType(name,
                                                                    TypeAttributes.NestedPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                                                                    Compiler.BuiltinTypes.ValueType.GetMetaInfo());

            var ffield = fixed_buffer_type.DefineField(FixedElementName, MemberType.GetMetaInfo(), FieldAttributes.Public);

            FieldBuilder = Parent.TypeBuilder.DefineField(Name, fixed_buffer_type, ModifiersExtensions.FieldAttr(ModFlags));

            var element_spec = new FieldSpec(null, this, MemberType, ffield, ModFlags);

            spec = new FixedFieldSpec(Parent.Definition, this, FieldBuilder, element_spec, ModFlags);

            Parent.MemberCache.AddMember(spec);
            return(true);
        }
예제 #2
0
        protected override Expression DoResolve(ResolveContext ec)
        {
            if (ec.Target == Target.JavaScript)
            {
                type   = ec.BuiltinTypes.Dynamic;
                eclass = ExprClass.Value;
                return(this);
            }

            if (Expr is AsArrayInitializer)
            {
                return(Expr.Resolve(ec));
            }

            New newExpr = null;

            if (Expr is Invocation)
            {
                var inv = Expr as Invocation;

                //
                // Special case for PlayScript scalar types with 1 argument -
                // just do an assignment. This is required for cosntructs like
                //
                //	var num:Number = new Number(1.0);
                //
                // since the underlying C# types are primitives and don't have
                // constructors which take arugments.
                //
                var sn = inv.Exp as SimpleName;
                if (sn != null && IsPlayScriptScalarClass(sn.Name) && inv.Arguments != null && inv.Arguments.Count == 1)
                {
                    Argument arg = inv.Arguments [0].Clone(new CloneContext());
                    arg.Resolve(ec);
                    if (arg.Expr.Type != null)
                    {
                        if (BuiltinTypeSpec.IsPrimitiveType(arg.Expr.Type) || arg.Expr.Type.BuiltinType == BuiltinTypeSpec.Type.String)
                        {
                            return(arg.Expr);
                        }
                    }
                    // TODO: ActionScript does actually allow this, but its runtime
                    // rules are hard to implement at compile time, and this should
                    // be a rare use case, so I am leaving it as a compiler error for
                    // now.
                    ec.Report.Error(7112, loc, "The type `{0}' does not contain a constructor that takes non-scalar arguments", sn.Name);
                    return(null);
                }

                newExpr = new New(inv.Exp, inv.Arguments, loc);
            }
            else if (Expr is ElementAccess)
            {
                if (loc.SourceFile != null && !loc.SourceFile.PsExtended)
                {
                    ec.Report.Error(7103, loc, "Native arrays are only suppored in ASX.'");
                    return(null);
                }
                var elemAcc  = Expr as ElementAccess;
                var exprList = new List <Expression>();
                foreach (var arg in elemAcc.Arguments)
                {
                    exprList.Add(arg.Expr);
                }
                // TODO: Handle jagged arrays
                var arrayCreate = new ArrayCreation((FullNamedExpression)elemAcc.Expr, exprList,
                                                    new ComposedTypeSpecifier(exprList.Count, loc), null, loc);
                return(arrayCreate.Resolve(ec));
            }
            else
            {
                var resolveExpr = Expr.Resolve(ec);
                if (resolveExpr == null)
                {
                    return(null);
                }
                if (resolveExpr is TypeOf)
                {
                    newExpr = new New(((TypeOf)resolveExpr).TypeExpression, new Arguments(0), loc);
                }
                else
                {
                    newExpr = new New(resolveExpr, new Arguments(0), loc);
                }
            }

            return(newExpr.Resolve(ec));
        }
예제 #3
0
        protected override Expression DoResolve(ResolveContext rc)
        {
//			BEN: This won't work because the returned type won't pass Mono's type checkers.
//			if (rc.Target == Target.JavaScript) {
//				this.type = rc.Module.PredefinedTypes.AsArray.Resolve();
//				this.eclass = ExprClass.Value;
//				foreach (var elem in Elements)
//					elem.Resolve (rc);
//				return this;
//			}

            // Attempt to build simple const initializer
            bool     is_const_init = false;
            TypeSpec const_type    = null;

            if (elements.Count > 0)
            {
                is_const_init = true;
                const_type    = vectorType != null?vectorType.ResolveAsType(rc) : null;

                foreach (var elem in elements)
                {
                    if (elem == null)
                    {
                        is_const_init = false;
                        break;
                    }
                    if (!(elem is Constant) && !(elem is Unary && ((Unary)elem).Expr is Constant))
                    {
                        is_const_init = false;
                        break;
                    }
                    TypeSpec elemType = elem.Type;
                    if (vectorType == null)
                    {
                        if (elemType == null)
                        {
                            is_const_init = false;
                            break;
                        }
                        if (const_type == null)
                        {
                            const_type = BuiltinTypeSpec.IsPrimitiveType(elemType) ? elemType : rc.BuiltinTypes.Object;
                        }
                        if (const_type != elemType)
                        {
                            if (((const_type == rc.BuiltinTypes.Int || const_type == rc.BuiltinTypes.UInt) && elemType == rc.BuiltinTypes.Double) ||
                                (const_type == rc.BuiltinTypes.Double && (elemType == rc.BuiltinTypes.Int || elemType == rc.BuiltinTypes.UInt)))
                            {
                                const_type = rc.BuiltinTypes.Double;
                            }
                            else
                            {
                                const_type = rc.BuiltinTypes.Object;
                            }
                        }
                    }
                }
            }

            TypeExpression type;

            if (vectorType != null)               // For new <Type> [ initializer ] expressions..
            {
                var elemTypeSpec = vectorType.ResolveAsType(rc);
                if (elemTypeSpec != null)
                {
                    type = new TypeExpression(
                        rc.Module.PredefinedTypes.AsVector.Resolve().MakeGenericType(rc, new [] { elemTypeSpec }), Location);
                }
                else
                {
                    type = new TypeExpression(rc.Module.PredefinedTypes.AsArray.Resolve(), Location);
                }
            }
            else
            {
                type = new TypeExpression(rc.Module.PredefinedTypes.AsArray.Resolve(), Location);
            }

            TypeSpec typeSpec = type.ResolveAsType(rc.MemberContext);

            if (typeSpec.IsArray)
            {
                ArrayCreation arrayCreate = (ArrayCreation) new ArrayCreation(type, this).Resolve(rc);
                return(arrayCreate);
            }
            else if (is_const_init)
            {
                // If all elements in the initializer list are simple constants, we just pass the elements in a .NET array to the
                // PS Array initializer.
                var newArgs = new Arguments(1);
                newArgs.Add(new Argument(new ArrayCreation(new TypeExpression(const_type, loc), this, loc)));
                return(new New(type, newArgs, loc).Resolve(rc));
            }
            else
            {
                var initElems = new List <Expression>();
                foreach (var e in elements)
                {
                    initElems.Add(new CollectionElementInitializer(e));
                }
                return(new NewInitialize(type, null,
                                         new CollectionOrObjectInitializers(initElems, Location), Location).Resolve(rc));
            }
        }