Exemplo n.º 1
0
 /// <summary>
 /// Matches an 'ldflda' instruction accessing a tuple element.
 ///
 /// E.g. matches:
 /// <c>ldflda Item1(ldflda Rest(target))</c>
 /// </summary>
 public static bool MatchTupleFieldAccess(LdFlda inst, out IType tupleType, out ILInstruction target, out int position)
 {
     tupleType = inst.Field.DeclaringType;
     target    = inst.Target;
     if (!inst.Field.Name.StartsWith("Item", StringComparison.Ordinal))
     {
         position = 0;
         return(false);
     }
     if (!int.TryParse(inst.Field.Name.Substring(4), out position))
     {
         return(false);
     }
     if (!TupleType.IsTupleCompatible(tupleType, out _))
     {
         return(false);
     }
     while (target is LdFlda ldflda && ldflda.Field.Name == "Rest" && TupleType.IsTupleCompatible(ldflda.Field.DeclaringType, out _))
     {
         tupleType = ldflda.Field.DeclaringType;
         target    = ldflda.Target;
         position += TupleType.RestPosition - 1;
     }
     return(true);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Matches 'newobj TupleType(...)'.
        /// Takes care of flattening long tuples.
        /// </summary>
        public static bool MatchTupleConstruction(NewObj newobj, out ILInstruction[] arguments)
        {
            arguments = null;
            if (newobj == null)
            {
                return(false);
            }
            if (!TupleType.IsTupleCompatible(newobj.Method.DeclaringType, out int elementCount))
            {
                return(false);
            }
            arguments = new ILInstruction[elementCount];
            int outIndex = 0;

            while (elementCount >= TupleType.RestPosition)
            {
                if (newobj.Arguments.Count != TupleType.RestPosition)
                {
                    return(false);
                }
                for (int pos = 1; pos < TupleType.RestPosition; pos++)
                {
                    arguments[outIndex++] = newobj.Arguments[pos - 1];
                }
                elementCount -= TupleType.RestPosition - 1;
                Debug.Assert(outIndex + elementCount == arguments.Length);
                newobj = newobj.Arguments.Last() as NewObj;
                if (newobj == null)
                {
                    return(false);
                }
                if (!TupleType.IsTupleCompatible(newobj.Method.DeclaringType, out int restElementCount))
                {
                    return(false);
                }
                if (restElementCount != elementCount)
                {
                    return(false);
                }
            }
            Debug.Assert(outIndex + elementCount == arguments.Length);
            if (newobj.Arguments.Count != elementCount)
            {
                return(false);
            }
            for (int i = 0; i < elementCount; i++)
            {
                arguments[outIndex++] = newobj.Arguments[i];
            }
            return(true);
        }