private static Field GetInstanceField(TypeNode originalReturnType, Field possiblyGenericField, TypeNode instanceDeclaringType)
        {
            Contract.Requires(instanceDeclaringType != null);

            if (instanceDeclaringType.Template == null)
            {
                return(possiblyGenericField);
            }

            var declaringTemplate = instanceDeclaringType;

            while (declaringTemplate.Template != null)
            {
                declaringTemplate = declaringTemplate.Template;
            }

            Contract.Assume(declaringTemplate == possiblyGenericField.DeclaringType);

            return(Rewriter.GetFieldInstanceReference(possiblyGenericField, instanceDeclaringType));

#if false
            Field f = instanceDeclaringType.GetField(possiblyGenericField.Name);
            if (f != null)
            {
                // already instantiated
                return(f);
            }
            // pseudo instance
            Field instance = new Field(instanceDeclaringType, possiblyGenericField.Attributes, possiblyGenericField.Flags, possiblyGenericField.Name, originalReturnType, null);
            instanceDeclaringType.Members.Add(instance);
            return(instance);
#endif
        }
        private Field GetReturnValueClosureField(TypeNode declaringType, TypeNode resultType, FieldFlags flags, int uniqueKey)
        {
            Contract.Requires(declaringType != null);

            Contract.Assume(declaringType.Template == null);
            Identifier name = Identifier.For("_result" + uniqueKey.ToString()); // unique name for this field

            Field f = declaringType.GetField(name);

            if (f != null)
            {
                return(f);
            }

            f = new Field(declaringType,
                          null,
                          flags,
                          name,
                          resultType,
                          null);

            declaringType.Members.Add(f);
            // remember we added it so we can make it part of initializations
            if (f.IsStatic)
            {
                topLevelStaticResultField = f;
            }
            else
            {
                topLevelClosureResultField = f;
            }

            return(f);
        }