public virtual string GetAutoPropertyBackingFieldName(IProperty property)
 {
     return(_prev.GetAutoPropertyBackingFieldName(property));
 }
Beispiel #2
0
        public override void VisitPropertyDeclaration(PropertyDeclaration propertyDeclaration)
        {
            var resolveResult = _resolver.Resolve(propertyDeclaration);

            if (!(resolveResult is MemberResolveResult))
            {
                _errorReporter.Region = propertyDeclaration.GetRegion();
                _errorReporter.InternalError("Property declaration " + propertyDeclaration.Name + " does not resolve to a member.");
                return;
            }

            var property = ((MemberResolveResult)resolveResult).Member as IProperty;

            if (property == null)
            {
                _errorReporter.Region = propertyDeclaration.GetRegion();
                _errorReporter.InternalError("Property declaration " + propertyDeclaration.Name + " does not resolve to a property (resolves to " + resolveResult.ToString() + ")");
                return;
            }

            var jsClass = GetJsClass(property.DeclaringTypeDefinition);

            if (jsClass == null)
            {
                return;
            }

            var impl = _metadataImporter.GetPropertySemantics(property);

            switch (impl.Type)
            {
            case PropertyScriptSemantics.ImplType.GetAndSetMethods: {
                if (!property.IsAbstract && propertyDeclaration.Getter.Body.IsNull && propertyDeclaration.Setter.Body.IsNull)
                {
                    // Auto-property
                    var fieldName = _metadataImporter.GetAutoPropertyBackingFieldName(property);
                    if (_metadataImporter.ShouldGenerateAutoPropertyBackingField(property))
                    {
                        AddDefaultFieldInitializerToType(jsClass, fieldName, property, property.IsStatic);
                    }
                    CompileAndAddAutoPropertyMethodsToType(jsClass, property, impl, fieldName);
                }
                else
                {
                    if (!propertyDeclaration.Getter.IsNull)
                    {
                        MaybeCompileAndAddMethodToType(jsClass, propertyDeclaration.Getter, propertyDeclaration.Getter.Body, property.Getter, impl.GetMethod);
                    }

                    if (!propertyDeclaration.Setter.IsNull)
                    {
                        MaybeCompileAndAddMethodToType(jsClass, propertyDeclaration.Setter, propertyDeclaration.Setter.Body, property.Setter, impl.SetMethod);
                    }
                }
                break;
            }

            case PropertyScriptSemantics.ImplType.Field: {
                AddDefaultFieldInitializerToType(jsClass, impl.FieldName, property, property.IsStatic);
                break;
            }

            case PropertyScriptSemantics.ImplType.NotUsableFromScript: {
                break;
            }

            default: {
                throw new InvalidOperationException("Invalid property implementation " + impl.Type);
            }
            }
        }
        private JsExpression GenerateStructCloneMethod(ITypeDefinition type, string typevarName, bool hasCreateInstance)
        {
            var stmts = new List <JsStatement>()
            {
                JsStatement.Var("r", hasCreateInstance ? (JsExpression)JsExpression.Invocation(JsExpression.Member(JsExpression.Identifier(typevarName), "createInstance")) : JsExpression.New(JsExpression.Identifier(typevarName)))
            };
            var o = JsExpression.Identifier("o");
            var r = JsExpression.Identifier("r");

            foreach (var f in type.Fields.Where(f => !f.IsStatic))
            {
                var sem = _metadataImporter.GetFieldSemantics(f);
                if (sem.Type == FieldScriptSemantics.ImplType.Field)
                {
                    var          def   = f.ReturnType.GetDefinition();
                    JsExpression value = JsExpression.Member(o, sem.Name);
                    if (def != null && def.Kind == TypeKind.Struct && _metadataImporter.GetTypeSemantics(def).Type == TypeScriptSemantics.ImplType.MutableValueType)
                    {
                        value = _runtimeLibrary.CloneValueType(value, f.ReturnType, new DefaultRuntimeContext(type, _metadataImporter, _errorReporter, _namer));
                    }
                    stmts.Add(JsExpression.Assign(JsExpression.Member(r, sem.Name), value));
                }
            }

            foreach (var p in type.Properties.Where(p => !p.IsStatic))
            {
                var sem = _metadataImporter.GetPropertySemantics(p);

                if ((sem.Type == PropertyScriptSemantics.ImplType.GetAndSetMethods && MetadataUtils.IsAutoProperty(p) == true) || sem.Type == PropertyScriptSemantics.ImplType.Field)
                {
                    var          def       = p.ReturnType.GetDefinition();
                    var          fieldName = sem.Type == PropertyScriptSemantics.ImplType.GetAndSetMethods ? _metadataImporter.GetAutoPropertyBackingFieldName(p) : sem.FieldName;
                    JsExpression value     = JsExpression.Member(o, fieldName);
                    if (def != null && def.Kind == TypeKind.Struct && _metadataImporter.GetTypeSemantics(def).Type == TypeScriptSemantics.ImplType.MutableValueType)
                    {
                        value = _runtimeLibrary.CloneValueType(value, p.ReturnType, new DefaultRuntimeContext(type, _metadataImporter, _errorReporter, _namer));
                    }
                    stmts.Add(JsExpression.Assign(JsExpression.Member(r, fieldName), value));
                }
            }

            foreach (var e in type.Events.Where(e => !e.IsStatic && MetadataUtils.IsAutoEvent(e) == true))
            {
                var sem = _metadataImporter.GetEventSemantics(e);

                if (sem.Type == EventScriptSemantics.ImplType.AddAndRemoveMethods)
                {
                    var          def       = e.ReturnType.GetDefinition();
                    var          fieldName = _metadataImporter.GetAutoEventBackingFieldName(e);
                    JsExpression value     = JsExpression.Member(o, fieldName);
                    if (def != null && def.Kind == TypeKind.Struct && _metadataImporter.GetTypeSemantics(def).Type == TypeScriptSemantics.ImplType.MutableValueType)
                    {
                        value = _runtimeLibrary.CloneValueType(value, e.ReturnType, new DefaultRuntimeContext(type, _metadataImporter, _errorReporter, _namer));
                    }
                    stmts.Add(JsExpression.Assign(JsExpression.Member(r, fieldName), value));
                }
            }
            stmts.Add(JsStatement.Return(r));
            return(JsExpression.FunctionDefinition(new[] { "o" }, JsStatement.Block(stmts)));
        }