Ejemplo n.º 1
0
        internal override void GenerateMethodBody(TypeCompilationState compilationState, DiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics);

            var paramAccess = F.Parameter(Parameters[0]);

            BoundExpression expression;

            if (ContainingType.IsStructType())
            {
                // For structs:
                //
                //      return param is ContainingType i ? this.Equals(in i) : false;
                expression = F.Conditional(
                    F.Is(paramAccess, ContainingType),
                    F.Call(
                        F.This(),
                        _typedRecordEquals,
                        ImmutableArray.Create <RefKind>(RefKind.In),
                        ImmutableArray.Create <BoundExpression>(F.Convert(ContainingType, paramAccess))),
                    F.Literal(false),
                    F.SpecialType(SpecialType.System_Boolean));
            }
            else
            {
                // For classes:
                //      return this.Equals(param as ContainingType);
                expression = F.Call(F.This(), _typedRecordEquals, F.As(paramAccess, ContainingType));
            }

            F.CloseMethod(F.Block(ImmutableArray.Create <BoundStatement>(F.Return(expression))));
        }
Ejemplo n.º 2
0
        public override void Bind(BindContext context)
        {
            if (IsBound)
            {
                return;
            }

            base.Bind(context);

            var fieldChangeAttribute = GetAttribute <FieldChangeCallbackAttribute>();

            if (fieldChangeAttribute != null)
            {
                string targetPropertyName = fieldChangeAttribute.CallbackPropertyName;

                PropertySymbol targetProperty = ContainingType.GetMember <PropertySymbol>(targetPropertyName, context);

                if (targetProperty == null)
                {
                    throw new CompilerException($"Target property '{targetPropertyName}' for FieldChangeCallback was not found");
                }

                if (targetProperty.Type != Type)
                {
                    throw new CompilerException($"Target property type '{targetProperty.Type}' did not match field type");
                }

                targetProperty.MarkFieldCallback(this);
            }
        }
Ejemplo n.º 3
0
        internal override void GenerateMethodBody(TypeCompilationState compilationState, BindingDiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics);

            if (ParameterCount != _properties.Length)
            {
                // There is a mismatch, an error was reported elsewhere
                F.CloseMethod(F.ThrowNull());
                return;
            }

            var statementsBuilder = ArrayBuilder <BoundStatement> .GetInstance(_properties.Length + 1);

            for (int i = 0; i < _properties.Length; i++)
            {
                var parameter = Parameters[i];
                var property  = _properties[i];

                if (!parameter.Type.Equals(property.Type, TypeCompareKind.AllIgnoreOptions))
                {
                    // There is a mismatch, an error was reported elsewhere
                    statementsBuilder.Free();
                    F.CloseMethod(F.ThrowNull());
                    return;
                }

                // parameter_i = property_i;
                statementsBuilder.Add(F.Assignment(F.Parameter(parameter), F.Property(F.This(), property)));
            }

            statementsBuilder.Add(F.Return());
            F.CloseMethod(F.Block(statementsBuilder.ToImmutableAndFree()));
        }
Ejemplo n.º 4
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMethod(trapFile);
            PopulateModifiers(trapFile);

            var returnType = Type.Create(Context, Symbol.ReturnType);

            trapFile.operators(this,
                               Symbol.Name,
                               OperatorSymbol(Context, Symbol),
                               ContainingType,
                               returnType.TypeRef,
                               (UserOperator)OriginalDefinition);

            foreach (var l in Locations)
            {
                trapFile.operator_location(this, l);
            }

            if (IsSourceDeclaration)
            {
                var declSyntaxReferences = Symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).ToArray();
                foreach (var declaration in declSyntaxReferences.OfType <OperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.ReturnType, this, returnType);
                }
                foreach (var declaration in declSyntaxReferences.OfType <ConversionOperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.Type, this, returnType);
                }
            }

            ContainingType.PopulateGenerics();
        }
Ejemplo n.º 5
0
        public override Id MakeId(bool inContext)
        {
            if (IsPrimitiveType)
            {
                return(PrimitiveTypeId);
            }

            var name = cx.GetId(td.Name);

            Id l;

            if (ContainingType != null)
            {
                l = ContainingType.GetId(inContext) + cx.Dot;
            }
            else
            {
                l = AssemblyPrefix;

                var ns = Namespace;
                if (!ns.IsGlobalNamespace)
                {
                    l = l + ns.ShortId + cx.Dot;
                }
            }

            return(l + name);
        }
Ejemplo n.º 6
0
        public override Id MakeId(bool inContext)
        {
            if (IsPrimitiveType)
            {
                return(PrimitiveTypeId);
            }

            var ct = ContainingType;
            Id  l  = null;

            if (ct != null)
            {
                l = ContainingType.GetId(inContext);
            }
            else
            {
                if (tr.ResolutionScope.Kind == HandleKind.AssemblyReference)
                {
                    l = AssemblyPrefix;
                }

                if (!Namespace.IsGlobalNamespace)
                {
                    l += Namespace.ShortId;
                }
            }

            return(l + cx.Dot + cx.GetId(tr.Name));
        }
Ejemplo n.º 7
0
        internal override void GenerateMethodBody(TypeCompilationState compilationState, DiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, this.SyntaxNode, compilationState, diagnostics);

            try
            {
                MethodSymbol? equalityComparer_GetHashCode = null;
                MethodSymbol? equalityComparer_get_Default = null;
                BoundExpression currentHashValue;

                if (ContainingType.BaseTypeNoUseSiteDiagnostics.IsObjectType())
                {
                    if (_equalityContract.IsStatic)
                    {
                        F.CloseMethod(F.ThrowNull());
                        return;
                    }

                    // There are no base record types.
                    // Get hash code of the equality contract and combine it with hash codes for field values.
                    ensureEqualityComparerHelpers(F, ref equalityComparer_GetHashCode, ref equalityComparer_get_Default);
                    currentHashValue = MethodBodySynthesizer.GenerateGetHashCode(equalityComparer_GetHashCode!, equalityComparer_get_Default!, F.Property(F.This(), _equalityContract), F);
                }
                else
                {
                    // There are base record types.
                    // Get base.GetHashCode() and combine it with hash codes for field values.
                    var overridden = OverriddenMethod;

                    if (overridden is null || overridden.ReturnType.SpecialType != SpecialType.System_Int32)
                    {
                        // There was a problem with overriding, an error was reported elsewhere
                        F.CloseMethod(F.ThrowNull());
                        return;
                    }

                    currentHashValue = F.Call(F.Base(overridden.ContainingType), overridden);
                }

                //  bound HASH_FACTOR
                BoundLiteral? boundHashFactor = null;

                foreach (var f in ContainingType.GetFieldsToEmit())
                {
                    if (!f.IsStatic)
                    {
                        ensureEqualityComparerHelpers(F, ref equalityComparer_GetHashCode, ref equalityComparer_get_Default);
                        currentHashValue = MethodBodySynthesizer.GenerateHashCombine(currentHashValue, equalityComparer_GetHashCode!, equalityComparer_get_Default!, ref boundHashFactor,
                                                                                     F.Field(F.This(), f),
                                                                                     F);
                    }
                }

                F.CloseMethod(F.Block(F.Return(currentHashValue)));
            }
            catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
            {
                diagnostics.Add(ex.Diagnostic);
                F.CloseMethod(F.ThrowNull());
            }
Ejemplo n.º 8
0
        internal override void GenerateMethodBody(TypeCompilationState compilationState, BindingDiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, this.SyntaxNode, compilationState, diagnostics);

            try
            {
                var paramAccess = F.Parameter(Parameters[0]);

                BoundExpression expression;
                if (ContainingType.IsStructType())
                {
                    throw ExceptionUtilities.Unreachable;
                }
                else
                {
                    if (_typedRecordEquals.ReturnType.SpecialType != SpecialType.System_Boolean)
                    {
                        // There is a signature mismatch, an error was reported elsewhere
                        F.CloseMethod(F.ThrowNull());
                        return;
                    }

                    // For classes:
                    //      return this.Equals(param as ContainingType);
                    expression = F.Call(F.This(), _typedRecordEquals, F.As(paramAccess, ContainingType));
                }

                F.CloseMethod(F.Block(ImmutableArray.Create <BoundStatement>(F.Return(expression))));
            }
            catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
            {
                diagnostics.Add(ex.Diagnostic);
                F.CloseMethod(F.ThrowNull());
            }
        }
Ejemplo n.º 9
0
        internal override void GenerateMethodBody(TypeCompilationState compilationState, BindingDiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, this.SyntaxNode, compilationState, diagnostics);

            try
            {
                ParameterSymbol parameter = Parameters[0];

                if (parameter.Type.IsErrorType())
                {
                    F.CloseMethod(F.ThrowNull());
                    return;
                }

                var retExpr = F.Call(
                    F.This(),
                    ContainingType.GetMembersUnordered().OfType <SynthesizedRecordObjEquals>().Single(),
                    F.Convert(F.SpecialType(SpecialType.System_Object), F.Parameter(parameter)));

                F.CloseMethod(F.Block(F.Return(retExpr)));
            }
            catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
            {
                diagnostics.Add(ex.Diagnostic);
                F.CloseMethod(F.ThrowNull());
            }
        }
Ejemplo n.º 10
0
        public override void Populate()
        {
            PopulateMethod();
            ExtractModifiers();

            var returnType = Type.Create(Context, symbol.ReturnType);

            Context.Emit(Tuples.operators(this,
                                          symbol.Name,
                                          OperatorSymbol(Context, symbol.Name),
                                          ContainingType,
                                          returnType.TypeRef,
                                          (UserOperator)OriginalDefinition));

            foreach (var l in Locations)
            {
                Context.Emit(Tuples.operator_location(this, l));
            }

            if (IsSourceDeclaration)
            {
                var declSyntaxReferences = symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).ToArray();
                foreach (var declaration in declSyntaxReferences.OfType <OperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.ReturnType, this, returnType);
                }
                foreach (var declaration in declSyntaxReferences.OfType <ConversionOperatorDeclarationSyntax>())
                {
                    TypeMention.Create(Context, declaration.Type, this, returnType);
                }
            }

            ContainingType.ExtractGenerics();
        }
Ejemplo n.º 11
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMethod(trapFile);
            PopulateModifiers(trapFile);
            ContainingType.PopulateGenerics();

            var returnType = Type.Create(Context, symbol.ReturnType);

            trapFile.methods(this, Name, ContainingType, returnType.TypeRef, OriginalDefinition);

            if (IsSourceDeclaration)
            {
                foreach (var declaration in symbol.DeclaringSyntaxReferences.Select(s => s.GetSyntax()).OfType <MethodDeclarationSyntax>())
                {
                    Context.BindComments(this, declaration.Identifier.GetLocation());
                    TypeMention.Create(Context, declaration.ReturnType, this, returnType);
                }
            }

            foreach (var l in Locations)
            {
                trapFile.method_location(this, l);
            }

            PopulateGenerics(trapFile);
            Overrides(trapFile);
            ExtractRefReturn(trapFile, symbol, this);
            ExtractCompilerGenerated(trapFile);
        }
Ejemplo n.º 12
0
            public override object EnsureObject(Context ctx, object instance)
            {
                var runtime_fields = ContainingType.EnsureRuntimeFields(instance);

                // (instance)._runtime_fields[_name]
                return(runtime_fields.EnsureItemObject(_name));
            }
Ejemplo n.º 13
0
        private TypeWithAnnotations ComputeReturnType(DiagnosticBag diagnostics)
        {
            if (this.MethodKind == MethodKind.PropertyGet)
            {
                var type = _property.TypeWithAnnotations;
                if (!ContainingType.IsInterfaceType() && type.Type.IsStatic)
                {
                    // '{0}': static types cannot be used as return types
                    diagnostics.Add(ErrorCode.ERR_ReturnTypeIsStaticClass, this.locations[0], type.Type);
                }

                return(type);
            }
            else
            {
                var binder = GetBinder();
                var type   = TypeWithAnnotations.Create(binder.GetSpecialType(SpecialType.System_Void, diagnostics, this.GetSyntax()));

                if (IsInitOnly)
                {
                    var isInitOnlyType = Binder.GetWellKnownType(this.DeclaringCompilation,
                                                                 WellKnownType.System_Runtime_CompilerServices_IsExternalInit, diagnostics, this.locations[0]);

                    var modifiers = ImmutableArray.Create <CustomModifier>(
                        CSharpCustomModifier.CreateRequired(isInitOnlyType));
                    type = type.WithModifiers(modifiers);
                }

                return(type);
            }
        }
Ejemplo n.º 14
0
        public override MultiValue VisitFieldReference(IFieldReferenceOperation fieldRef, StateValue state)
        {
            var field = fieldRef.Field;

            switch (field.Name)
            {
            case "EmptyTypes" when field.ContainingType.IsTypeOf("System", "Type"): {
                return(ArrayValue.Create(0));
            }

            case "Empty" when field.ContainingType.IsTypeOf("System", "String"): {
                return(new KnownStringValue(string.Empty));
            }
            }

            if (TryGetConstantValue(fieldRef, out var constValue))
            {
                return(constValue);
            }

            if (fieldRef.Field.Type.IsTypeInterestingForDataflow())
            {
                return(new FieldValue(fieldRef.Field));
            }

            return(TopValue);
        }
        internal override void GenerateMethodBody(
            TypeCompilationState compilationState,
            BindingDiagnosticBag diagnostics
            )
        {
            var F = new SyntheticBoundNodeFactory(
                this,
                ContainingType.GetNonNullSyntaxNode(),
                compilationState,
                diagnostics
                );

            try
            {
                // => (object)left == right || ((object)left != null && left.Equals(right));
                MethodSymbol?equals = null;
                foreach (var member in ContainingType.GetMembers(WellKnownMemberNames.ObjectEquals))
                {
                    if (
                        member is MethodSymbol candidate &&
                        candidate.ParameterCount == 1 &&
                        candidate.Parameters[0].RefKind == RefKind.None &&
                        candidate.ReturnType.SpecialType == SpecialType.System_Boolean &&
                        !candidate.IsStatic &&
                        candidate.Parameters[0].Type.Equals(
                            ContainingType,
                            TypeCompareKind.AllIgnoreOptions
                            )
                        )
                    {
                        equals = candidate;
                        break;
                    }
                }

                if (equals is null)
                {
                    // Unable to locate expected method, an error was reported elsewhere
                    F.CloseMethod(F.ThrowNull());
                    return;
                }

                var left  = F.Parameter(Parameters[0]);
                var right = F.Parameter(Parameters[1]);

                BoundExpression objectEqual  = F.ObjectEqual(left, right);
                BoundExpression recordEquals = F.LogicalAnd(
                    F.ObjectNotEqual(left, F.Null(F.SpecialType(SpecialType.System_Object))),
                    F.Call(left, equals, right)
                    );

                F.CloseMethod(F.Block(F.Return(F.LogicalOr(objectEqual, recordEquals))));
            }
            catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
            {
                diagnostics.Add(ex.Diagnostic);
                F.CloseMethod(F.ThrowNull());
            }
        }
Ejemplo n.º 16
0
        public override void Populate()
        {
            PopulateMethod();
            ExtractModifiers();
            ContainingType.ExtractGenerics();

            Context.Emit(Tuples.destructors(this, string.Format("~{0}", symbol.ContainingType.Name), ContainingType, OriginalDefinition(Context, this, symbol)));
            Context.Emit(Tuples.destructor_location(this, Location));
        }
Ejemplo n.º 17
0
        public static RestoredAssetsRoot Create(string prefixName, ContainingType type)
        {
            var instance = ScriptableObject.CreateInstance <RestoredAssetsRoot>();

            instance.Type = type;
            instance.name = FindName(prefixName, type);

            return(instance);
        }
Ejemplo n.º 18
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMethod(trapFile);
            PopulateModifiers(trapFile);
            ContainingType.PopulateGenerics();

            trapFile.destructors(this, string.Format("~{0}", symbol.ContainingType.Name), ContainingType, OriginalDefinition(Context, this, symbol));
            trapFile.destructor_location(this, Location);
        }
Ejemplo n.º 19
0
        internal override void GenerateMethodBody(TypeCompilationState compilationState, BindingDiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics);

            try
            {
                ImmutableArray <Symbol> printableMembers = ContainingType.GetMembers().WhereAsArray(m => isPrintable(m));

                if (ReturnType.IsErrorType() ||
                    printableMembers.Any(static m => m.GetTypeOrReturnType().Type.IsErrorType()))
Ejemplo n.º 20
0
        protected void TypeChecks(TypeSymbol type, BindingDiagnosticBag diagnostics)
        {
            if (type.HasFileLocalTypes() && !ContainingType.HasFileLocalTypes())
            {
                diagnostics.Add(ErrorCode.ERR_FileTypeDisallowedInSignature, this.ErrorLocation, type, ContainingType);
            }
            else if (type.IsStatic)
            {
                // Cannot declare a variable of static type '{0}'
                diagnostics.Add(ErrorCode.ERR_VarDeclIsStaticClass, this.ErrorLocation, type);
            }
            else if (type.IsVoidType())
            {
                diagnostics.Add(ErrorCode.ERR_FieldCantHaveVoidType, TypeSyntax?.Location ?? this.Locations[0]);
            }
            else if (type.IsRestrictedType(ignoreSpanLikeTypes: true))
            {
                diagnostics.Add(ErrorCode.ERR_FieldCantBeRefAny, TypeSyntax?.Location ?? this.Locations[0], type);
            }
            else if (type.IsRefLikeType && (this.IsStatic || !containingType.IsRefLikeType))
            {
                diagnostics.Add(ErrorCode.ERR_FieldAutoPropCantBeByRefLike, TypeSyntax?.Location ?? this.Locations[0], type);
            }
            else if (IsConst && !type.CanBeConst())
            {
                SyntaxToken constToken = default(SyntaxToken);
                foreach (var modifier in ModifiersTokenList)
                {
                    if (modifier.Kind() == SyntaxKind.ConstKeyword)
                    {
                        constToken = modifier;
                        break;
                    }
                }
                Debug.Assert(constToken.Kind() == SyntaxKind.ConstKeyword);

                diagnostics.Add(ErrorCode.ERR_BadConstType, constToken.GetLocation(), type);
            }
            else if (IsVolatile && !type.IsValidVolatileFieldType())
            {
                // '{0}': a volatile field cannot be of the type '{1}'
                diagnostics.Add(ErrorCode.ERR_VolatileStruct, this.ErrorLocation, this, type);
            }

            CompoundUseSiteInfo <AssemblySymbol> useSiteInfo = new CompoundUseSiteInfo <AssemblySymbol>(diagnostics, ContainingAssembly);

            if (!this.IsNoMoreVisibleThan(type, ref useSiteInfo))
            {
                // Inconsistent accessibility: field type '{1}' is less accessible than field '{0}'
                diagnostics.Add(ErrorCode.ERR_BadVisFieldType, this.ErrorLocation, this, type);
            }

            diagnostics.Add(this.ErrorLocation, useSiteInfo);
        }
Ejemplo n.º 21
0
            public override IPhpArray EnsureArray(Context ctx, object instance)
            {
                var runtime_fields = ContainingType.EnsureRuntimeFields(instance);

                if (runtime_fields == null)
                {
                    throw new NotSupportedException();
                }

                // (instance)._runtime_fields[_name]
                return(runtime_fields.EnsureItemArray(_name));
            }
Ejemplo n.º 22
0
        public override void Populate(TextWriter trapFile)
        {
            PopulateMethod(trapFile);
            PopulateModifiers(trapFile);
            ContainingType.PopulateGenerics();

            var prop = PropertySymbol;

            if (prop == null)
            {
                Context.ModelError(symbol, "Unhandled accessor associated symbol");
                return;
            }

            var      parent = Property.Create(Context, prop);
            int      kind;
            Accessor unboundAccessor;

            if (SymbolEqualityComparer.Default.Equals(symbol, prop.GetMethod))
            {
                kind            = 1;
                unboundAccessor = Create(Context, prop.OriginalDefinition.GetMethod);
            }
            else if (SymbolEqualityComparer.Default.Equals(symbol, prop.SetMethod))
            {
                kind            = 2;
                unboundAccessor = Create(Context, prop.OriginalDefinition.SetMethod);
            }
            else
            {
                Context.ModelError(symbol, "Unhandled accessor kind");
                return;
            }

            trapFile.accessors(this, kind, symbol.Name, parent, unboundAccessor);

            foreach (var l in Locations)
            {
                trapFile.accessor_location(this, l);
            }

            Overrides(trapFile);

            if (symbol.FromSource() && Block == null)
            {
                trapFile.compiler_generated(this);
            }

            if (symbol.IsInitOnly)
            {
                trapFile.init_only_accessors(this);
            }
        }
Ejemplo n.º 23
0
            public override void SetValue(Context ctx, object instance, PhpValue value)
            {
                var runtime_fields = ContainingType.EnsureRuntimeFields(instance);

                if (value.IsAlias)
                {
                    runtime_fields[_name] = value;
                }
                else
                {
                    runtime_fields.SetItemValue(_name, value);
                }
            }
        internal override void GenerateMethodBody(TypeCompilationState compilationState, BindingDiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics);

            if (ParameterCount != _positionalMembers.Length)
            {
                // There is a mismatch, an error was reported elsewhere
                F.CloseMethod(F.ThrowNull());
                return;
            }

            var statementsBuilder = ArrayBuilder <BoundStatement> .GetInstance(_positionalMembers.Length + 1);

            for (int i = 0; i < _positionalMembers.Length; i++)
            {
                var parameter        = Parameters[i];
                var positionalMember = _positionalMembers[i];

                var type = positionalMember switch
                {
                    PropertySymbol property => property.Type,
                    FieldSymbol field => field.Type,
                    _ => throw ExceptionUtilities.Unreachable
                };

                if (!parameter.Type.Equals(type, TypeCompareKind.AllIgnoreOptions))
                {
                    // There is a mismatch, an error was reported elsewhere
                    statementsBuilder.Free();
                    F.CloseMethod(F.ThrowNull());
                    return;
                }

                switch (positionalMember)
                {
                case PropertySymbol property:
                    // parameter_i = property_i;
                    statementsBuilder.Add(F.Assignment(F.Parameter(parameter), F.Property(F.This(), property)));
                    break;

                case FieldSymbol field:
                    // parameter_i = field_i;
                    statementsBuilder.Add(F.Assignment(F.Parameter(parameter), F.Field(F.This(), field)));
                    break;
                }
            }

            statementsBuilder.Add(F.Return());
            F.CloseMethod(F.Block(statementsBuilder.ToImmutableAndFree()));
        }
    }
Ejemplo n.º 25
0
            public override PhpValue GetValue(Context ctx, object instance)
            {
                var runtime_fields = ContainingType.GetRuntimeFields(instance);

                // (instance)._runtime_fields[_name]
                if (runtime_fields != null && runtime_fields.TryGetValue(_name, out var value))
                {
                    return(value);
                }
                else
                {
                    PhpException.UndefinedProperty(ContainingType.Name, _name.ToString());
                    return(PhpValue.Null);
                }
            }
Ejemplo n.º 26
0
        private FieldSymbol GetAutoField(BindContext context)
        {
            PropertySymbol containingProperty = (PropertySymbol)context.GetSymbol(RoslynSymbol.AssociatedSymbol);

            foreach (var field in ContainingType.GetMembers <FieldSymbol>(context))
            {
                if (field.RoslynSymbol.AssociatedSymbol != null &&
                    field.RoslynSymbol.AssociatedSymbol.Equals(containingProperty.RoslynSymbol))
                {
                    return(field);
                }
            }

            return(null);
        }
Ejemplo n.º 27
0
        public override void Populate()
        {
            PopulateMethod();
            ExtractModifiers();
            ContainingType.ExtractGenerics();

            var prop = PropertySymbol;

            if (prop == null)
            {
                Context.ModelError(symbol, "Unhandled accessor associated symbol");
                return;
            }

            var      parent = Property.Create(Context, prop);
            int      kind;
            Accessor unboundAccessor;

            if (symbol.Equals(prop.GetMethod))
            {
                kind            = 1;
                unboundAccessor = Create(Context, prop.OriginalDefinition.GetMethod);
            }
            else if (symbol.Equals(prop.SetMethod))
            {
                kind            = 2;
                unboundAccessor = Create(Context, prop.OriginalDefinition.SetMethod);
            }
            else
            {
                Context.ModelError(symbol, "Undhandled accessor kind");
                return;
            }

            Context.Emit(Tuples.accessors(this, kind, symbol.Name, parent, unboundAccessor));

            foreach (var l in Locations)
            {
                Context.Emit(Tuples.accessor_location(this, l));
            }

            Overrides();

            if (symbol.FromSource() && Block == null)
            {
                Context.Emit(Tuples.compiler_generated(this));
            }
        }
        internal override void GenerateMethodBody(TypeCompilationState compilationState, DiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics);

            try
            {
                // => !(r1 == r2);
                F.CloseMethod(F.Block(F.Return(F.Not(F.Call(receiver: null, ContainingType.GetMembers(WellKnownMemberNames.EqualityOperatorName).OfType <SynthesizedRecordEqualityOperator>().Single(),
                                                            F.Parameter(Parameters[0]), F.Parameter(Parameters[1]))))));
            }
            catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
            {
                diagnostics.Add(ex.Diagnostic);
                F.CloseMethod(F.ThrowNull());
            }
        }
        internal override void GenerateMethodBody(TypeCompilationState compilationState, DiagnosticBag diagnostics)
        {
            var F = new SyntheticBoundNodeFactory(this, ContainingType.GetNonNullSyntaxNode(), compilationState, diagnostics);

            try
            {
                MethodSymbol equalityComparer_GetHashCode = F.WellKnownMethod(WellKnownMember.System_Collections_Generic_EqualityComparer_T__GetHashCode, isOptional: false) !;
                MethodSymbol equalityComparer_get_Default = F.WellKnownMethod(WellKnownMember.System_Collections_Generic_EqualityComparer_T__get_Default, isOptional: false) !;

                BoundExpression currentHashValue;

                if (ContainingType.BaseTypeNoUseSiteDiagnostics.IsObjectType())
                {
                    // There are no base record types.
                    // Get hash code of the equality contract and combine it with hash codes for field values.
                    currentHashValue = MethodBodySynthesizer.GenerateGetHashCode(equalityComparer_GetHashCode, equalityComparer_get_Default, F.Property(F.This(), _equalityContract), F);
                }
                else
                {
                    // There are base record types.
                    // Get base.GetHashCode() and combine it with hash codes for field values.
                    var overridden = OverriddenMethod;
                    currentHashValue = F.Call(F.Base(overridden.ContainingType), overridden);
                }

                //  bound HASH_FACTOR
                BoundLiteral?boundHashFactor = null;

                foreach (var f in ContainingType.GetFieldsToEmit())
                {
                    if (!f.IsStatic)
                    {
                        currentHashValue = MethodBodySynthesizer.GenerateHashCombine(currentHashValue, equalityComparer_GetHashCode, equalityComparer_get_Default, ref boundHashFactor,
                                                                                     F.Field(F.This(), f),
                                                                                     F);
                    }
                }

                F.CloseMethod(F.Block(F.Return(currentHashValue)));
            }
            catch (SyntheticBoundNodeFactory.MissingPredefinedMember ex)
            {
                diagnostics.Add(ex.Diagnostic);
                F.CloseMethod(F.ThrowNull());
            }
        }
Ejemplo n.º 30
0
            public override void SetValue(Context ctx, object instance, PhpValue value)
            {
                var runtime_fields = ContainingType.EnsureRuntimeFields(instance);

                if (runtime_fields == null)
                {
                    throw new NotSupportedException();
                }

                if (value.IsAlias)
                {
                    runtime_fields[_name] = value;
                }
                else
                {
                    runtime_fields.SetItemValue(_name, value);
                }
            }