Beispiel #1
0
    private static void ReorderField(ModelDefinition definition)
    {
        if (typeof(IEntity).IsAssignableFrom(definition.ModelType))
        {
            FieldDefinition?idField          = null;
            FieldDefinition?collationIdField = null;

            List <FieldDefinition?> fields = new(definition.FieldDefinitions.Count)
            {
                null, null                 //set null for keys space
            };
            foreach (FieldDefinition fieldDefinition in definition.FieldDefinitions)
            {
                switch (fieldDefinition.Name)
                {
                case nameof(IEntity.Id):
                    idField = fieldDefinition;
                    break;

                case nameof(IEntity.CollationId):
                    collationIdField = fieldDefinition;
                    break;

                default:
                    fields.Add(fieldDefinition);
                    break;
                }
            }

            fields[0] = idField;
            fields[1] = collationIdField;

            definition.FieldDefinitions = fields;
        }
        else if (typeof(IGuidEntity).IsAssignableFrom(definition.ModelType))
        {
            FieldDefinition?idField = null;

            List <FieldDefinition?> fields = new(definition.FieldDefinitions.Count)
            {
                null,                 //set null for keys space
            };
            foreach (FieldDefinition fieldDefinition in definition.FieldDefinitions)
            {
                switch (fieldDefinition.Name)
                {
                case nameof(IGuidEntity.Id):
                    idField = fieldDefinition;
                    break;

                default:
                    fields.Add(fieldDefinition);
                    break;
                }
            }

            fields[0] = idField;
            definition.FieldDefinitions = fields;
        }
    }
Beispiel #2
0
        private void ScanLdfld(
            Instruction operation,
            Stack <StackSlot> currentStack,
            MethodDefinition thisMethod,
            MethodBody methodBody)
        {
            Code code = operation.OpCode.Code;

            if (code == Code.Ldfld || code == Code.Ldflda)
            {
                PopUnknown(currentStack, 1, methodBody, operation.Offset);
            }

            bool isByRef = code == Code.Ldflda || code == Code.Ldsflda;

            FieldDefinition?field = _context.TryResolve((FieldReference)operation.Operand);

            if (field != null)
            {
                StackSlot slot = new StackSlot(GetFieldValue(thisMethod, field), isByRef);
                currentStack.Push(slot);
                return;
            }

            PushUnknown(currentStack);
        }
Beispiel #3
0
    private void CollectFieldsAndFragmentNames(
        TypeDefinition parentType,
        SelectionSet selectionSet,
        Dictionary <string, List <FieldDefPair> > nodeAndDefs,
        Dictionary <string, bool> fragments)
    {
        var selections = selectionSet.ToList();

        for (var i = 0; i < selections.Count; i++)
        {
            var selection = selections[i];

            if (selection is FieldSelection field)
            {
                var             fieldName = field.Name;
                FieldDefinition?fieldDef  = null;
                if (parentType is not null && (IsObjectDefinition(parentType) || IsInterfaceType(parentType)))
                {
                    fieldDef = _context.Schema.GetField(parentType.Name, fieldName);
                }

                var responseName = field.AliasOrName;

                if (!nodeAndDefs.ContainsKey(responseName))
                {
                    nodeAndDefs[responseName] = new List <FieldDefPair>();
                }

                nodeAndDefs[responseName].Add(new FieldDefPair
                {
                    ParentType = parentType,
                    Field      = field,
                    FieldDef   = fieldDef
                });
            }
            else if (selection is FragmentSpread fragmentSpread)
            {
                fragments[fragmentSpread.FragmentName] = true;
            }
            else if (selection is InlineFragment inlineFragment)
            {
                var typeCondition = inlineFragment.TypeCondition;

                if (typeCondition is not null)
                {
                    var inlineFragmentType =
                        _context.Schema.GetNamedType(typeCondition.Name) ?? parentType;

                    CollectFieldsAndFragmentNames(
                        inlineFragmentType,
                        inlineFragment.SelectionSet,
                        nodeAndDefs,
                        fragments);
                }
            }
        }
    }
Beispiel #4
0
 void VerifyInitializerField(FieldDefinition src, FieldDefinition?linked)
 {
     VerifyFieldKept(src, linked);
     verifiedGeneratedFields.Add(linked !.FullName);
     linkedMembers.Remove(linked.FullName);
     VerifyTypeDefinitionKept(src.FieldType.Resolve(), linked.FieldType.Resolve());
     linkedMembers.Remove(linked.FieldType.FullName);
     linkedMembers.Remove(linked.DeclaringType.FullName);
     verifiedGeneratedTypes.Add(linked.DeclaringType.FullName);
 }
        public static bool RequiresReflectionMethodBodyScannerForAccess(LinkContext context, FieldReference field)
        {
            FieldDefinition?fieldDefinition = context.TryResolve(field);

            if (fieldDefinition == null)
            {
                return(false);
            }

            return(context.Annotations.FlowAnnotations.RequiresDataFlowAnalysis(fieldDefinition));
        }
Beispiel #6
0
        void VerifyFieldKept(FieldDefinition src, FieldDefinition?linked)
        {
            if (linked == null)
            {
                Assert.True(false, $"Field `{src}' should have been kept");
                return;
            }

            if (!object.Equals(src.Constant, linked.Constant))
            {
                Assert.True(false, $"Field '{src}' value doesn's match. Expected {src.Constant}, actual {linked.Constant}");
            }

            VerifyPseudoAttributes(src, linked);
            VerifyCustomAttributes(src, linked);
        }
Beispiel #7
0
        void VerifyField(FieldDefinition src, FieldDefinition?linked)
        {
            bool expectedKept = ShouldBeKept(src);

            if (!expectedKept)
            {
                if (linked != null)
                {
                    Assert.True(false, $"Field `{src}' should have been removed");
                }

                return;
            }

            VerifyFieldKept(src, linked);
        }
        public static ICollection <NavigationPropertyWeavingContext> GetNavigationPropertyCandidates(this ClassWeavingContext context)
        {
            return(context.TypeDefinition.Properties.Select(propertyDefinition =>
            {
                if (propertyDefinition.SetMethod != null)
                {
                    return null;
                }

                bool isReadOnlyCollectionType = propertyDefinition.PropertyType.GetElementType().Resolve() ==
                                                context.References.ReadOnlyCollectionType.Resolve();
                bool isReadOnlyCollectionInterface = propertyDefinition.PropertyType.GetElementType().Resolve() ==
                                                     context.References.ReadOnlyCollectionInterface.Resolve();

                if (!isReadOnlyCollectionType && !isReadOnlyCollectionInterface)
                {
                    return null;
                }

                GenericInstanceType instance = (GenericInstanceType)propertyDefinition.PropertyType;
                TypeDefinition?genericArgument = instance.GenericArguments.FirstOrDefault()?.Resolve();

                if (genericArgument == null || !genericArgument.IsClass || genericArgument.IsString() || genericArgument.IsEnum ||
                    genericArgument.IsValueType || genericArgument.IsPrimitive)
                {
                    return null;
                }

                string propertyName = propertyDefinition.Name;

                FieldDefinition?fieldDefinition = context.TypeDefinition.Fields.SingleOrDefault(fieldDefinition =>
                                                                                                string.Equals(fieldDefinition.Name, propertyName, StringComparison.OrdinalIgnoreCase) ||
                                                                                                string.Equals(fieldDefinition.Name, $"_{propertyName}", StringComparison.OrdinalIgnoreCase) ||
                                                                                                string.Equals(fieldDefinition.Name, $"m_{propertyName}", StringComparison.OrdinalIgnoreCase));

                if (fieldDefinition == null)
                {
                    return null;
                }

                return new NavigationPropertyWeavingContext(context, propertyDefinition, fieldDefinition);
            })
                   .Where(x => x != null)
                   .ToList() !);
        }
Beispiel #9
0
        private void ScanStfld(
            Instruction operation,
            Stack <StackSlot> currentStack,
            MethodDefinition thisMethod,
            MethodBody methodBody)
        {
            StackSlot valueToStoreSlot = PopUnknown(currentStack, 1, methodBody, operation.Offset);

            if (operation.OpCode.Code == Code.Stfld)
            {
                PopUnknown(currentStack, 1, methodBody, operation.Offset);
            }

            FieldDefinition?field = _context.TryResolve((FieldReference)operation.Operand);

            if (field != null)
            {
                HandleStoreField(thisMethod, field, operation, valueToStoreSlot.Value);
            }
        }
Beispiel #10
0
        private TypeDefinition Add()
        {
            var typeDefinition = new TypeDefinition(
                NameSpace,
                TypeName,
                TypeAttributes.SequentialLayout | TypeAttributes.NotPublic | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit,
                valueTypeHelper.ValueType)
            {
                CustomAttributes =
                {
                    new CustomAttribute(readOnlyAttributeHelper.Ctor),
                },
            };

            module.Types.Add(typeDefinition);

            key = new FieldDefinition("Key", ThisFieldAttributes, typeHelper.Type);
            typeDefinition.Fields.Add(key);

            value = new FieldDefinition("Value", ThisFieldAttributes, module.TypeSystem.UInt64);
            typeDefinition.Fields.Add(value);

            return(typeDefinition);
        }
Beispiel #11
0
 private static ApiLevel getApiLevel(FieldDefinition?f) => f is null ? ApiLevel.None : f.IsPrivate ? ApiLevel.Private : f.IsAssembly || f.IsFamilyAndAssembly ? ApiLevel.Internal : ApiLevel.Public;
Beispiel #12
0
 public bool TryGetOrAdd(byte[] data, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out FieldDefinition?field)