예제 #1
0
 public PredefinedType(BuiltinTypeSpec type)
 {
     this.kind = type.Kind;
     this.name = type.Name;
     this.ns   = type.Namespace;
     this.type = type;
 }
예제 #2
0
        public BuiltinTypes()
        {
            Object    = new BuiltinTypeSpec(MemberKind.Class, "System", "Object", BuiltinTypeSpec.Type.Object);
            ValueType = new BuiltinTypeSpec(MemberKind.Class, "System", "ValueType", BuiltinTypeSpec.Type.ValueType);
            Attribute = new BuiltinTypeSpec(MemberKind.Class, "System", "Attribute", BuiltinTypeSpec.Type.Attribute);

            Int    = new BuiltinTypeSpec(MemberKind.Struct, "System", "Int32", BuiltinTypeSpec.Type.Int);
            Long   = new BuiltinTypeSpec(MemberKind.Struct, "System", "Int64", BuiltinTypeSpec.Type.Long);
            UInt   = new BuiltinTypeSpec(MemberKind.Struct, "System", "UInt32", BuiltinTypeSpec.Type.UInt);
            ULong  = new BuiltinTypeSpec(MemberKind.Struct, "System", "UInt64", BuiltinTypeSpec.Type.ULong);
            Byte   = new BuiltinTypeSpec(MemberKind.Struct, "System", "Byte", BuiltinTypeSpec.Type.Byte);
            SByte  = new BuiltinTypeSpec(MemberKind.Struct, "System", "SByte", BuiltinTypeSpec.Type.SByte);
            Short  = new BuiltinTypeSpec(MemberKind.Struct, "System", "Int16", BuiltinTypeSpec.Type.Short);
            UShort = new BuiltinTypeSpec(MemberKind.Struct, "System", "UInt16", BuiltinTypeSpec.Type.UShort);

            IEnumerator = new BuiltinTypeSpec(MemberKind.Interface, "System.Collections", "IEnumerator", BuiltinTypeSpec.Type.IEnumerator);
            IEnumerable = new BuiltinTypeSpec(MemberKind.Interface, "System.Collections", "IEnumerable", BuiltinTypeSpec.Type.IEnumerable);
            IDisposable = new BuiltinTypeSpec(MemberKind.Interface, "System", "IDisposable", BuiltinTypeSpec.Type.IDisposable);

            Char    = new BuiltinTypeSpec(MemberKind.Struct, "System", "Char", BuiltinTypeSpec.Type.Char);
            String  = new BuiltinTypeSpec(MemberKind.Class, "System", "String", BuiltinTypeSpec.Type.String);
            Float   = new BuiltinTypeSpec(MemberKind.Struct, "System", "Single", BuiltinTypeSpec.Type.Float);
            Double  = new BuiltinTypeSpec(MemberKind.Struct, "System", "Double", BuiltinTypeSpec.Type.Double);
            Decimal = new BuiltinTypeSpec(MemberKind.Struct, "System", "Decimal", BuiltinTypeSpec.Type.Decimal);
            Bool    = new BuiltinTypeSpec(MemberKind.Struct, "System", "Boolean", BuiltinTypeSpec.Type.Bool);
            IntPtr  = new BuiltinTypeSpec(MemberKind.Struct, "System", "IntPtr", BuiltinTypeSpec.Type.IntPtr);
            UIntPtr = new BuiltinTypeSpec(MemberKind.Struct, "System", "UIntPtr", BuiltinTypeSpec.Type.UIntPtr);

            MulticastDelegate = new BuiltinTypeSpec(MemberKind.Class, "System", "MulticastDelegate", BuiltinTypeSpec.Type.MulticastDelegate);
            Delegate          = new BuiltinTypeSpec(MemberKind.Class, "System", "Delegate", BuiltinTypeSpec.Type.Delegate);
            Enum               = new BuiltinTypeSpec(MemberKind.Class, "System", "Enum", BuiltinTypeSpec.Type.Enum);
            Array              = new BuiltinTypeSpec(MemberKind.Class, "System", "Array", BuiltinTypeSpec.Type.Array);
            Void               = new BuiltinTypeSpec(MemberKind.Void, "System", "Void", BuiltinTypeSpec.Type.Other);
            Type               = new BuiltinTypeSpec(MemberKind.Class, "System", "Type", BuiltinTypeSpec.Type.Type);
            Exception          = new BuiltinTypeSpec(MemberKind.Class, "System", "Exception", BuiltinTypeSpec.Type.Exception);
            RuntimeFieldHandle = new BuiltinTypeSpec(MemberKind.Struct, "System", "RuntimeFieldHandle", BuiltinTypeSpec.Type.Other);
            RuntimeTypeHandle  = new BuiltinTypeSpec(MemberKind.Struct, "System", "RuntimeTypeHandle", BuiltinTypeSpec.Type.Other);

            // TODO: Maybe I should promote it to different kind for faster compares
            Dynamic = new BuiltinTypeSpec("dynamic", BuiltinTypeSpec.Type.Dynamic);

            OperatorsBinaryStandard = Binary.CreateStandardOperatorsTable(this);
            OperatorsBinaryEquality = Binary.CreateEqualityOperatorsTable(this);
            OperatorsBinaryUnsafe   = Binary.CreatePointerOperatorsTable(this);
            OperatorsUnary          = Unary.CreatePredefinedOperatorsTable(this);
            OperatorsUnaryMutator   = UnaryMutator.CreatePredefinedOperatorsTable(this);

            BinaryPromotionsTypes = ConstantFold.CreateBinaryPromotionsTypes(this);
            SwitchUserTypes       = Switch.CreateSwitchUserTypes(this);

            types = new BuiltinTypeSpec[] {
                Object, ValueType, Attribute,
                Int, UInt, Long, ULong, Float, Double, Char, Short, Decimal, Bool, SByte, Byte, UShort, String,
                Enum, Delegate, MulticastDelegate, Void, Array, Type, IEnumerator, IEnumerable, IDisposable,
                IntPtr, UIntPtr, RuntimeFieldHandle, RuntimeTypeHandle, Exception
            };
        }
예제 #3
0
		void EmitFieldSize (int buffer_size)
		{
			int type_size = BuiltinTypeSpec.GetSize (MemberType);

			if (buffer_size > int.MaxValue / type_size) {
				Report.Error (1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
					GetSignatureForError (), buffer_size.ToString (), TypeManager.CSharpName (MemberType));
				return;
			}

			AttributeEncoder encoder;

			var ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve (Location);
			if (ctor == null)
				return;

			var field_size = Module.PredefinedMembers.StructLayoutSize.Resolve (Location);
			var field_charset = Module.PredefinedMembers.StructLayoutCharSet.Resolve (Location);
			if (field_size == null || field_charset == null)
				return;

			var char_set = CharSet ?? Module.DefaultCharSet ?? 0;

			encoder = new AttributeEncoder ();
			encoder.Encode ((short)LayoutKind.Sequential);
			encoder.EncodeNamedArguments (
				new [] { field_size, field_charset },
				new Constant [] { 
					new IntConstant (Compiler.BuiltinTypes, buffer_size * type_size, Location),
					new IntConstant (Compiler.BuiltinTypes, (int) char_set, Location)
				}
			);

			fixed_buffer_type.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());

			//
			// Don't emit FixedBufferAttribute attribute for private types
			//
			if ((ModFlags & Modifiers.PRIVATE) != 0)
				return;

			ctor = Module.PredefinedMembers.FixedBufferAttributeCtor.Resolve (Location);
			if (ctor == null)
				return;

			encoder = new AttributeEncoder ();
			encoder.EncodeTypeName (MemberType);
			encoder.Encode (buffer_size);
			encoder.EncodeEmptyNamedArguments ();

			FieldBuilder.SetCustomAttribute ((ConstructorInfo) ctor.GetMetaInfo (), encoder.ToArray ());
		}
예제 #4
0
        public void SetBuiltinType(BuiltinTypeSpec pts)
        {
            var found = types[pts.Name];

            cached_types.Remove(pts.Name);
            if (found.Count == 1)
            {
                types[pts.Name][0] = pts;
            }
            else
            {
                throw new NotImplementedException();
            }
        }
예제 #5
0
        public void ReplaceTypeWithPredefined(TypeSpec ts, BuiltinTypeSpec pts)
        {
            var found = types [ts.Name];

            cached_types.Remove(ts.Name);
            if (found.Count == 1)
            {
                types[ts.Name][0] = pts;
            }
            else
            {
                throw new NotImplementedException();
            }
        }
예제 #6
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);
                int index = Parent.PartialContainer.Fields.IndexOf(this);
                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;
                    Parent.PartialContainer.Fields.Insert(++index, 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);
        }
예제 #7
0
파일: class.cs 프로젝트: fvalette/mono
		//
		// Replaces normal spec with predefined one when compiling corlib
		// and this type container defines predefined type
		//
		public void SetPredefinedSpec (BuiltinTypeSpec spec)
		{
			// When compiling build-in types we start with two
			// version of same type. One is of BuiltinTypeSpec and
			// second one is ordinary TypeSpec. The unification
			// happens at later stage when we know which type
			// really matches the builtin type signature. However
			// that means TypeSpec create during CreateType of this
			// type has to be replaced with builtin one
			// 
			spec.SetMetaInfo (TypeBuilder);
			spec.MemberCache = this.spec.MemberCache;
			spec.DeclaringType = this.spec.DeclaringType;

			this.spec = spec;
			current_type = null;
		}
예제 #8
0
 public void SetBuiltinType(BuiltinTypeSpec pts)
 {
     var found = types[pts.Name];
     cached_types.Remove (pts.Name);
     if (found.Count == 1) {
         types[pts.Name][0] = pts;
     } else {
         throw new NotImplementedException ();
     }
 }
예제 #9
0
 public PredefinedMember(ModuleContainer module, BuiltinTypeSpec type, string name, params TypeSpec[] types)
     : this(module, type, MemberFilter.Method(name, 0, ParametersCompiled.CreateFullyResolved(types), null))
 {
 }
예제 #10
0
파일: field.cs 프로젝트: allisterb/Escher
        void EmitFieldSize(int buffer_size)
        {
            int type_size = BuiltinTypeSpec.GetSize(MemberType);

            if (buffer_size > int.MaxValue / type_size)
            {
                Report.Error(1664, Location, "Fixed size buffer `{0}' of length `{1}' and type `{2}' exceeded 2^31 limit",
                             GetSignatureForError(), buffer_size.ToString(), MemberType.GetSignatureForError());
                return;
            }

            AttributeEncoder encoder;
            MethodSpec       ctor;

            var char_set = CharSetValue ?? Module.DefaultCharSet ?? 0;

#if STATIC
            //
            // Set struct layout without resolving StructLayoutAttribute which is not always available
            //

            TypeAttributes attribs = TypeAttributes.SequentialLayout;
            switch (char_set)
            {
            case CharSet.None:
            case CharSet.Ansi:
                attribs |= TypeAttributes.AnsiClass;
                break;

            case CharSet.Auto:
                attribs |= TypeAttributes.AutoClass;
                break;

            case CharSet.Unicode:
                attribs |= TypeAttributes.UnicodeClass;
                break;
            }

            fixed_buffer_type.__SetAttributes(fixed_buffer_type.Attributes | attribs);
            fixed_buffer_type.__SetLayout(0, buffer_size * type_size);
#else
            ctor = Module.PredefinedMembers.StructLayoutAttributeCtor.Resolve(Location);
            if (ctor == null)
            {
                return;
            }

            var field_size    = Module.PredefinedMembers.StructLayoutSize.Resolve(Location);
            var field_charset = Module.PredefinedMembers.StructLayoutCharSet.Resolve(Location);
            if (field_size == null || field_charset == null)
            {
                return;
            }

            encoder = new AttributeEncoder();
            encoder.Encode((short)LayoutKind.Sequential);
            encoder.EncodeNamedArguments(
                new [] { field_size, field_charset },
                new Constant [] {
                new IntConstant(Compiler.BuiltinTypes, buffer_size * type_size, Location),
                new IntConstant(Compiler.BuiltinTypes, (int)char_set, Location)
            }
                );

            fixed_buffer_type.SetCustomAttribute((ConstructorInfo)ctor.GetMetaInfo(), encoder.ToArray(out _));
#endif
            //
            // Don't emit FixedBufferAttribute attribute for private types
            //
            if ((ModFlags & Modifiers.PRIVATE) != 0)
            {
                return;
            }

            ctor = Module.PredefinedMembers.FixedBufferAttributeCtor.Resolve(Location);
            if (ctor == null)
            {
                return;
            }

            encoder = new AttributeEncoder();
            encoder.EncodeTypeName(MemberType);
            encoder.Encode(buffer_size);
            encoder.EncodeEmptyNamedArguments();

            FieldBuilder.SetCustomAttribute((ConstructorInfo)ctor.GetMetaInfo(), encoder.ToArray(out var references));
            Module.AddAssemblyReferences(references);
        }
예제 #11
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));
        }
예제 #12
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));
            }
        }
예제 #13
0
		public void ReplaceTypeWithPredefined (TypeSpec ts, BuiltinTypeSpec pts)
		{
			var found = types [ts.Name];
			cached_types.Remove (ts.Name);
			if (found.Count == 1) {
				types[ts.Name][0] = pts;
			} else {
				throw new NotImplementedException ();
			}
		}