AddNestedClass() public method

Add a nested class to this class
public AddNestedClass ( TypeAttr attrSet, string name ) : NestedClassDef
attrSet TypeAttr attributes for this nested class
name string nested class name
return NestedClassDef
Beispiel #1
0
        //////////////////////////////////////////////////////////////////////////
        // Methods
        //////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Define a new class to emit for this assembly. This class
        /// becomes the 'current' class, where subsequent EmitMethod
        /// and EmitField calls will popuate this class.
        /// </summary>
        public void emitClass(string baseQName, string qname, string[] interfaces, TypeAttr attr)
        {
            string[] s = FanUtil.splitQName(qname);
            className = qname;

            // first check if this type was already stubbed out
            classDef = (ClassDef)types[qname];
            if (classDef == null)
            {
                // if not, we need to create it
                if (qname.IndexOf("/") != -1)
                {
                    // Nested class
                    PERWAPI.ClassDef cdef = (PERWAPI.ClassDef)findType(s[0]);
                    classDef = cdef.AddNestedClass(attr, s[1]);
                }
                else
                {
                    // Normal class
                    classDef = peFile.AddClass(attr, s[0], s[1]);
                }
            }
            else
            {
                // if stubbed out, make sure we define the type correctly
                classDef.SetAttribute(attr);
            }

            // base class
            if (baseQName == null)
            {
                classDef.SuperType = null;
            }
            else
            {
                classDef.SuperType = findType(baseQName) as PERWAPI.Class;
            }

            // interfaces
            for (int i = 0; i < interfaces.Length; i++)
            {
                classDef.AddImplementedInterface(findType(interfaces[i]) as PERWAPI.Class);
            }

            types[qname] = classDef;
        }
Beispiel #2
0
        //////////////////////////////////////////////////////////////////////////
        // Util
        //////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Find the Type instance for this fully qualified type name.
        /// </summary>
        internal PERWAPI.Type findType(string qname)
        {
            // Always convert voids to native
            if (qname == "Fan.Sys.Void")
            {
                qname = "System.Void";
            }

            PERWAPI.Type type = (PERWAPI.Type)types[qname];
            if (type == null)
            {
                string aname = FanUtil.getPodName(qname);
                if (aname == null)
                {
                    aname = "mscorlib";
                }
                if (qname.StartsWith("Fanx."))
                {
                    aname = "sys";                       // hack for support classes
                }
                if (qname.EndsWith("Peer"))
                {
                    aname += "Native_";                  // TODO
                }
                // first check if this is a type in this pod that
                // hasn't been defined yet
                if (aname == assemblyName)
                {
                    // stub out type - fill get filled in later (we hope)
                    string[] sn   = FanUtil.splitQName(qname);
                    ClassDef stub = null;
                    if (qname.IndexOf("/") != -1)
                    {
                        // Nested class
                        PERWAPI.ClassDef cdef = (PERWAPI.ClassDef)findType(sn[0]);
                        stub = cdef.AddNestedClass(PERWAPI.TypeAttr.NestedPublic, sn[1]);
                    }
                    else
                    {
                        // Normal class
                        stub = peFile.AddClass(PERWAPI.TypeAttr.Public, sn[0], sn[1]);
                    }
                    types[qname] = stub;
                    return(stub);
                }

                AssemblyRef aref = (AssemblyRef)assemblies[aname];
                if (aref == null)
                {
                    aref = peFile.MakeExternAssembly(aname);
                    assemblies[aname] = aref;
                }

                string[] s = FanUtil.splitQName(qname);
                if (qname.IndexOf("/") != -1)
                {
                    // Nested class
                    PERWAPI.ClassRef cref = (PERWAPI.ClassRef)findType(s[0]);
                    type = cref.AddNestedClass(s[1]);
                }

                /*
                 * else if (qname.IndexOf("<") != -1)
                 * {
                 * // Generic type
                 * //if (type == null) type = aref.AddClass(s[0], s[1]);
                 * PERWAPI.ClassRef cref = (PERWAPI.ClassRef)findType(s[0]);
                 * cref.SetGenericParams(new GenericParam[] { cref.GetGenericParam(0) });
                 * type = cref;
                 * }
                 */
                else
                {
                    // Normal class, get/add type
                    type = aref.GetClass(s[0], s[1]);
                    if (type == null)
                    {
                        type = aref.AddClass(s[0], s[1]);
                    }
                }
                types[qname] = type;
            }
            return(type);
        }
 internal ClassDef CreateNestedClass(ClassDef parent, string name, PERWAPI.Class superType) {
     if (parent == null) {
         if (Assembly.GetClass(name) != null)
             return Assembly.GetClass(name);
         return Assembly.AddClass(TypeAttr.Public | TypeAttr.BeforeFieldInit, null, name, superType);
     } else {
         if (parent.GetNestedClass(name) != null)
             return parent.GetNestedClass(name);
         return parent.AddNestedClass(TypeAttr.NestedPublic | TypeAttr.BeforeFieldInit, name, superType);
     }
 }