/*-------------------- Constructors ---------------------------------*/ internal ReferenceScope(string name) : base(name) { Contract.Requires(name != null); defaultClass = new ClassRef(this, "", ""); defaultClass.MakeSpecial(); }
/// <summary> /// Add a class to this Scope. If the class already exists, /// throw an exception. /// </summary> /// <param name="nsName">name space name</param> /// <param name="name">class name</param> /// <returns>a descriptor for this class in another module</returns> public virtual ClassRef AddClass(string nsName, string name) { Contract.Requires(nsName != null); Contract.Requires(name != null); ClassRef aClass = GetClass(nsName, name); if (aClass != null) { if ((aClass is SystemClass) && (!((SystemClass)aClass).added)) { ((SystemClass)aClass).added = true; } else { throw new DescriptorException("Class " + aClass.NameString()); } } else { aClass = new ClassRef(this, nsName, name); classes.Add(aClass); } // FIXME Contract.Ensures(aClass != null); return(aClass); }
internal static void Read(PEReader buff, TableRow[] typeRefs, bool resolve) { for (uint i = 0; i < typeRefs.Length; i++) { uint resScopeIx = buff.GetCodedIndex(CIx.ResolutionScope); string name = buff.GetString(); string nameSpace = buff.GetString(); if (buff.CodedTable(CIx.ResolutionScope, resScopeIx) == MDTable.TypeRef) { typeRefs[i] = new NestedClassRef(resScopeIx, nameSpace, name); } else { typeRefs[i] = new ClassRef(resScopeIx, nameSpace, name); } typeRefs[i].Row = i + 1; } if (resolve) { foreach (TableRow typeRef in typeRefs) { ((ClassRef)typeRef).ResolveParent(buff, false); } } }
internal override void ResolveParent(PEReader buff, bool isExtern) { if (parent != null) { return; } CIx cIx = CIx.ResolutionScope; if (isExtern) { cIx = CIx.Implementation; } parent = (ClassRef)buff.GetCodedElement(cIx, resScopeIx); parent.ResolveParent(buff, isExtern); parent = (ClassRef)buff.GetCodedElement(cIx, resScopeIx); if (parent == null) { return; } NestedClassRef existing = parent.GetNestedClass(Name); if (existing == null) { scope = parent.GetScope(); parent.AddToClassList(this); } else if (isExtern) { buff.InsertInTable(MDTable.ExportedType, Row, existing); } else { buff.InsertInTable(MDTable.TypeRef, Row, existing); } }
internal MSCorLib() : base("mscorlib") { classes.Add(new SystemClass(this, PrimitiveType.Void)); classes.Add(new SystemClass(this, PrimitiveType.Boolean)); classes.Add(new SystemClass(this, PrimitiveType.Char)); classes.Add(new SystemClass(this, PrimitiveType.Int8)); classes.Add(new SystemClass(this, PrimitiveType.UInt8)); classes.Add(new SystemClass(this, PrimitiveType.Int16)); classes.Add(new SystemClass(this, PrimitiveType.UInt16)); classes.Add(new SystemClass(this, PrimitiveType.Int32)); classes.Add(new SystemClass(this, PrimitiveType.UInt32)); classes.Add(new SystemClass(this, PrimitiveType.Int64)); classes.Add(new SystemClass(this, PrimitiveType.UInt64)); classes.Add(new SystemClass(this, PrimitiveType.Float32)); classes.Add(new SystemClass(this, PrimitiveType.Float64)); classes.Add(new SystemClass(this, PrimitiveType.IntPtr)); classes.Add(new SystemClass(this, PrimitiveType.UIntPtr)); classes.Add(new SystemClass(this, PrimitiveType.String)); classes.Add(new SystemClass(this, PrimitiveType.TypedRef)); ObjectClass = new SystemClass(this, PrimitiveType.Object); classes.Add(ObjectClass); valueType = new ClassRef(this, "System", "ValueType"); valueType.MakeValueClass(); classes.Add(valueType); }
/// <summary> /// Create the FieldRef equivalent to this FieldDef. If one does not /// exist then create it. /// </summary> /// <returns>FieldRef for this FieldDef</returns> public FieldRef MakeRefOf() { if (refOf != null) { return(refOf); } ClassRef parRef = ((ClassDef)parent).MakeRefOf(); refOf = parRef.GetField(name); if (refOf == null) { Type refType; if (type is ClassDef) { refType = ((ClassDef)type).MakeRefOf(); } else { refType = type; } refOf = new FieldRef(parRef, name, refType); refOf.defOf = this; } return(refOf); }
/// <summary> /// Add a value class to this scope. If the class already exists, /// throw an exception. /// </summary> /// <param name="nsName">name space name</param> /// <param name="name">class name</param> /// <returns></returns> public virtual ClassRef AddValueClass(string nsName, string name) { Contract.Requires(nsName != null); Contract.Requires(name != null); ClassRef aClass = AddClass(nsName, name); aClass.MakeValueClass(); return(aClass); }
/*------------------------- internal functions --------------------------*/ /* internal void AddMember(Member memb) { * if (memb is Method) { * Method existing = GetMethod(memb.Name(),((Method)memb).GetParTypes()); * if (existing == null) * methods.Add(memb); * } else { * Field existing = GetField(memb.Name()); * if (existing == null) * fields.Add(memb); * } * } */ internal void AddToExportedClassList(ClassRef exClass) { Contract.Requires(exClass != null); if (exportedClasses.Contains(exClass)) { return; } exportedClasses.Add(exClass); }
internal static ClassRef ReadClass(PEReader buff, ReferenceScope resScope) { uint resScopeIx = buff.GetCodedIndex(CIx.ResolutionScope); string name = buff.GetString(); string nameSpace = buff.GetString(); ClassRef newClass = (ClassRef)resScope.GetExistingClass(nameSpace, name); if (newClass == null) { newClass = new ClassRef(resScope, nameSpace, name); } return(newClass); }
internal static void GetClassRefs(PEReader buff, TableRow[] eClasses) { for (uint i = 0; i < eClasses.Length; i++) { uint junk = buff.ReadUInt32(); junk = buff.ReadUInt32(); string name = buff.GetString(); string nameSpace = buff.GetString(); uint implIx = buff.GetCodedIndex(CIx.Implementation); eClasses[i] = new ClassRef(implIx, nameSpace, name); eClasses[i].Row = i + 1; } }
/// <summary> /// Add a class to this Scope. If this class already exists, throw /// an exception /// </summary> /// <param name="newClass">The class to be added</param> public void AddClass(ClassRef newClass) { Contract.Requires(newClass != null); ClassRef aClass = (ClassRef)GetClass(newClass.NameSpace, newClass.Name, true); if (aClass != null) { throw new DescriptorException("Class " + newClass.NameString()); } if (Diag.DiagOn) { Console.WriteLine("Adding class " + newClass.Name + " to ResolutionScope " + name); } classes.Add(newClass); // Change Refs to Defs here newClass.SetScope(this); }
/// <summary> /// Get the MethodRef equivalent to this MethodDef. If one /// does not exist, then create it. /// </summary> /// <returns>MethodRef for this MethodDef</returns> public MethodRef MakeRefOf() { if (refOf != null) { return(refOf); } ClassRef parRef = ((ClassDef)parent).MakeRefOf(); refOf = parRef.GetMethod(name, sig.parTypes); if (refOf == null) { Type rType = sig.MakeRefRetType(); Type[] pTypes = sig.MakeRefParTypes(); refOf = new MethodRef(parRef, name, rType, pTypes); refOf.defOf = this; refOf.AddCallConv(this.GetCallConv()); } return(refOf); }
/// <summary> /// Makes the assembly debuggable by attaching the DebuggableAttribute /// to the Assembly. Call immediately before calling WritePEFile. /// </summary> /// <param name="allowDebug">set true to enable debugging, false otherwise</param> /// <param name="suppressOpt">set true to disable optimizations that affect debugging</param> public void MakeDebuggable(bool allowDebug, bool suppressOpt) { Type[] twoBools = new Type[] { PrimitiveType.Boolean, PrimitiveType.Boolean }; ClassRef debugRef = MSCorLib.mscorlib.GetClass("System.Diagnostics", "DebuggableAttribute"); if (debugRef == null) { debugRef = MSCorLib.mscorlib.AddClass("System.Diagnostics", "DebuggableAttribute"); } MethodRef dCtor = debugRef.GetMethod(".ctor", twoBools); if (dCtor == null) { dCtor = debugRef.AddMethod(".ctor", PrimitiveType.Void, twoBools); dCtor.AddCallConv(CallConv.Instance); } Constant[] dbgArgs = new Constant[] { new BoolConst(allowDebug), new BoolConst(suppressOpt) }; thisAssembly.AddCustomAttribute(dCtor, dbgArgs); }
internal static ClassRef ReadDef(PEReader buff, ReferenceScope resScope, uint index) { uint junk = buff.ReadUInt32(); string cName = buff.GetString(); string nsName = buff.GetString(); ClassRef newClass = (ClassRef)resScope.GetExistingClass(nsName, cName); if (newClass == null) { newClass = new ClassRef(resScope, nsName, cName); resScope.AddToClassList(newClass); } newClass.readAsDef = true; newClass.Row = index; junk = buff.GetCodedIndex(CIx.TypeDefOrRef); newClass.fieldIx = buff.GetIndex(MDTable.Field); newClass.methodIx = buff.GetIndex(MDTable.Method); return(newClass); }
/*------------------------- public set and get methods --------------------------*/ /// <summary> /// Add a class which is declared public in this external module of /// THIS assembly. This class will be exported from this assembly. /// The ilasm syntax for this is .extern class /// </summary> /// <param name="attrSet">attributes of the class to be exported</param> /// <param name="nsName">name space name</param> /// <param name="name">external class name</param> /// <param name="declFile">the file where the class is declared</param> /// <param name="isValueClass">is this class a value type?</param> /// <returns>a descriptor for this external class</returns> public ClassRef AddExternClass(TypeAttr attrSet, string nsName, string name, bool isValueClass, PEFile pefile) { Contract.Requires(nsName != null); Contract.Requires(name != null); Contract.Requires(pefile != null); ClassRef cRef = new ClassRef(this, nsName, name); if (isValueClass) { cRef.MakeValueClass(); } ExternClass eClass = new ExternClass(attrSet, nsName, name, modFile); exportedClasses.Add(eClass); cRef.SetExternClass(eClass); classes.Add(cRef); return(cRef); }
internal void CalcElemSize() { elemSize[(int)MDTable.Assembly] = Assembly.Size(this); elemSize[(int)MDTable.AssemblyOS] = 12; elemSize[(int)MDTable.AssemblyProcessor] = 4; elemSize[(int)MDTable.AssemblyRefOS] = 12 + TableIndexSize(MDTable.AssemblyRef); elemSize[(int)MDTable.AssemblyRefProcessor] = 4 + TableIndexSize(MDTable.AssemblyRef); elemSize[(int)MDTable.Module] = Module.Size(this); elemSize[(int)MDTable.TypeRef] = ClassRef.Size(this); elemSize[(int)MDTable.TypeDef] = ClassDef.Size(this); elemSize[(int)MDTable.Field] = FieldDef.Size(this); elemSize[(int)MDTable.Method] = MethodDef.Size(this); elemSize[(int)MDTable.Param] = Param.Size(this); elemSize[(int)MDTable.InterfaceImpl] = InterfaceImpl.Size(this); elemSize[(int)MDTable.MemberRef] = FieldRef.Size(this); elemSize[(int)MDTable.Constant] = ConstantElem.Size(this); elemSize[(int)MDTable.CustomAttribute] = CustomAttribute.Size(this); elemSize[(int)MDTable.FieldMarshal] = FieldMarshal.Size(this); elemSize[(int)MDTable.DeclSecurity] = DeclSecurity.Size(this); elemSize[(int)MDTable.ClassLayout] = ClassLayout.Size(this); elemSize[(int)MDTable.FieldLayout] = FieldLayout.Size(this); elemSize[(int)MDTable.StandAloneSig] = Signature.Size(this); elemSize[(int)MDTable.EventMap] = MapElem.Size(this, MDTable.EventMap); elemSize[(int)MDTable.Event] = Event.Size(this); elemSize[(int)MDTable.PropertyMap] = MapElem.Size(this, MDTable.PropertyMap); elemSize[(int)MDTable.Property] = Property.Size(this); elemSize[(int)MDTable.MethodSemantics] = MethodSemantics.Size(this); elemSize[(int)MDTable.MethodImpl] = MethodImpl.Size(this); elemSize[(int)MDTable.ModuleRef] = ModuleRef.Size(this); elemSize[(int)MDTable.TypeSpec] = TypeSpec.Size(this); elemSize[(int)MDTable.ImplMap] = ImplMap.Size(this); elemSize[(int)MDTable.FieldRVA] = FieldRVA.Size(this); elemSize[(int)MDTable.Assembly] = Assembly.Size(this); elemSize[(int)MDTable.AssemblyRef] = AssemblyRef.Size(this); elemSize[(int)MDTable.File] = FileRef.Size(this); elemSize[(int)MDTable.ExportedType] = ExternClass.Size(this); elemSize[(int)MDTable.ManifestResource] = ManifestResource.Size(this); elemSize[(int)MDTable.NestedClass] = MapElem.Size(this, MDTable.NestedClass); elemSize[(int)MDTable.GenericParam] = GenericParam.Size(this); elemSize[(int)MDTable.GenericParamConstraint] = GenericParamConstraint.Size(this); elemSize[(int)MDTable.MethodSpec] = MethodSpec.Size(this); }
internal static void GetFieldRefs(PEReader buff, uint num, ClassRef parent) { for (int i = 0; i < num; i++) { uint flags = buff.ReadUInt16(); string name = buff.GetString(); uint sigIx = buff.GetBlobIx(); if ((flags & (uint)FieldAttr.Public) == (uint)FieldAttr.Public) { if (parent.GetField(name) == null) { //Console.WriteLine(parent.NameString()); buff.currentClassScope = parent; FieldRef fRef = new FieldRef(parent, name, buff.GetFieldType(sigIx)); buff.currentClassScope = null; parent.AddToFieldList(fRef); } } } }
internal virtual void ResolveParent(PEReader buff, bool isExtern) { CIx cIx = CIx.ResolutionScope; if (isExtern) { cIx = CIx.Implementation; } if (scope != null) { return; } MetaDataElement parentScope = buff.GetCodedElement(cIx, resScopeIx); if (parentScope is Module) { // special code for glitch in Everett ilasm ClassDef newDef = new ClassDef((PEFile)parentScope, 0, NameSpace, Name); ((Module)parentScope).AddToClassList(newDef); buff.InsertInTable(MDTable.TypeRef, Row, newDef); } else { scope = (ReferenceScope)buff.GetCodedElement(cIx, resScopeIx); ClassRef existing = (ClassRef)scope.GetExistingClass(NameSpace, Name); if (existing == null) { scope.AddToClassList(this); } else { if (isExtern) { buff.InsertInTable(MDTable.ExportedType, Row, existing); } else { buff.InsertInTable(MDTable.TypeRef, Row, existing); } } } }
internal static void GetMethodRefs(PEReader buff, uint num, ClassRef parent) { for (int i = 0; i < num; i++) { uint rva = buff.ReadUInt32(); ushort implFlags = buff.ReadUInt16(); ushort methFlags = buff.ReadUInt16(); string name = buff.GetString(); uint sigIx = buff.GetBlobIx(); uint parIx = buff.GetIndex(MDTable.Param); if (IsPublicOrProtected(methFlags)) { MethodRef mRef = new MethodRef(parIx, name, sigIx); // changed mRef.SetParent(parent); //Console.WriteLine(parent.NameString()); MethSig mSig = buff.ReadMethSig(mRef, name, sigIx); //mSig.name = name; mRef.SetSig(mSig); // changed parent.AddToMethodList(mRef); } } }
internal void SetDefaultClass(ClassRef dClass) { Contract.Requires(dClass != null); defaultClass = dClass; }
internal bool isDefaultClass(ClassRef aClass) { Contract.Requires(aClass != null); return aClass == defaultClass; }
/// <summary> /// Add a class to this Scope. If the class already exists, /// throw an exception. /// </summary> /// <param name="nsName">name space name</param> /// <param name="name">class name</param> /// <returns>a descriptor for this class in another module</returns> public virtual ClassRef AddClass(string nsName, string name) { Contract.Requires(nsName != null); Contract.Requires(name != null); ClassRef aClass = GetClass(nsName, name); if (aClass != null) { if ((aClass is SystemClass) && (!((SystemClass)aClass).added)) ((SystemClass)aClass).added = true; else throw new DescriptorException("Class " + aClass.NameString()); } else { aClass = new ClassRef(this, nsName, name); classes.Add(aClass); } // FIXME Contract.Ensures(aClass != null); return aClass; }
/// <summary> /// Add a class to this Scope. If this class already exists, throw /// an exception /// </summary> /// <param name="newClass">The class to be added</param> public void AddClass(ClassRef newClass) { Contract.Requires(newClass != null); ClassRef aClass = (ClassRef)GetClass(newClass.NameSpace, newClass.Name, true); if (aClass != null) throw new DescriptorException("Class " + newClass.NameString()); if (Diag.DiagOn) Console.WriteLine("Adding class " + newClass.Name + " to ResolutionScope " + name); classes.Add(newClass); // Change Refs to Defs here newClass.SetScope(this); }
/*------------------------- internal functions --------------------------*/ /* internal void AddMember(Member memb) { if (memb is Method) { Method existing = GetMethod(memb.Name(),((Method)memb).GetParTypes()); if (existing == null) methods.Add(memb); } else { Field existing = GetField(memb.Name()); if (existing == null) fields.Add(memb); } } */ internal void AddToExportedClassList(ClassRef exClass) { Contract.Requires(exClass != null); if (exportedClasses.Contains(exClass)) return; exportedClasses.Add(exClass); }
/*------------------------- public set and get methods --------------------------*/ /// <summary> /// Add a class which is declared public in this external module of /// THIS assembly. This class will be exported from this assembly. /// The ilasm syntax for this is .extern class /// </summary> /// <param name="attrSet">attributes of the class to be exported</param> /// <param name="nsName">name space name</param> /// <param name="name">external class name</param> /// <param name="declFile">the file where the class is declared</param> /// <param name="isValueClass">is this class a value type?</param> /// <returns>a descriptor for this external class</returns> public ClassRef AddExternClass(TypeAttr attrSet, string nsName, string name, bool isValueClass, PEFile pefile) { Contract.Requires(nsName != null); Contract.Requires(name != null); Contract.Requires(pefile != null); ClassRef cRef = new ClassRef(this, nsName, name); if (isValueClass) cRef.MakeValueClass(); ExternClass eClass = new ExternClass(attrSet, nsName, name, modFile); exportedClasses.Add(eClass); cRef.SetExternClass(eClass); classes.Add(cRef); return cRef; }
internal void SetParent(ClassRef paren) { parent = paren; }
internal bool isDefaultClass(ClassRef aClass) { Contract.Requires(aClass != null); return(aClass == defaultClass); }
/// <summary> /// Make a ClassRef for this ClassDef /// </summary> /// <returns>ClassRef equivalent to this ClassDef</returns> public virtual ClassRef MakeRefOf() { if (refOf == null) { Assembly assem = scope.GetThisAssembly(); ReferenceScope scopeRef; if (assem != null) scopeRef = assem.MakeRefOf(); else scopeRef = scope.MakeRefOf(); refOf = scopeRef.GetClass(Name); if (refOf == null) { refOf = new ClassRef(scopeRef, NameSpace, Name); scopeRef.AddToClassList(refOf); } refOf.defOf = this; } return refOf; }
/*-------------------- Constructors ---------------------------------*/ internal NestedClassRef(ClassRef parent, string name) : base(parent.GetScope(), "", name) { this.parent = parent; }
internal override void ResolveParent(PEReader buff, bool isExtern) { if (parent != null) return; CIx cIx = CIx.ResolutionScope; if (isExtern) cIx = CIx.Implementation; parent = (ClassRef)buff.GetCodedElement(cIx, resScopeIx); parent.ResolveParent(buff, isExtern); parent = (ClassRef)buff.GetCodedElement(cIx, resScopeIx); if (parent == null) return; NestedClassRef existing = parent.GetNestedClass(Name); if (existing == null) { scope = parent.GetScope(); parent.AddToClassList(this); } else if (isExtern) buff.InsertInTable(MDTable.ExportedType, Row, existing); else buff.InsertInTable(MDTable.TypeRef, Row, existing); }
internal static void Read(PEReader buff, TableRow[] typeRefs, bool resolve) { for (uint i = 0; i < typeRefs.Length; i++) { uint resScopeIx = buff.GetCodedIndex(CIx.ResolutionScope); string name = buff.GetString(); string nameSpace = buff.GetString(); if (buff.CodedTable(CIx.ResolutionScope, resScopeIx) == MDTable.TypeRef) typeRefs[i] = new NestedClassRef(resScopeIx, nameSpace, name); else typeRefs[i] = new ClassRef(resScopeIx, nameSpace, name); typeRefs[i].Row = i + 1; } if (resolve) { foreach (TableRow typeRef in typeRefs) { ((ClassRef)typeRef).ResolveParent(buff, false); } } }
internal static ClassRef ReadClass(PEReader buff, ReferenceScope resScope) { uint resScopeIx = buff.GetCodedIndex(CIx.ResolutionScope); string name = buff.GetString(); string nameSpace = buff.GetString(); ClassRef newClass = (ClassRef)resScope.GetExistingClass(nameSpace, name); if (newClass == null) newClass = new ClassRef(resScope, nameSpace, name); return newClass; }
internal static void GetClassRefs(PEReader buff, TableRow[] typeRefs, ReferenceScope paren, uint[] parIxs) { Contract.Requires(buff != null); Contract.Requires(typeRefs != null); Contract.Requires(paren != null); Contract.Requires(parIxs != null); int num = typeRefs.Length; uint[] fieldStart = new uint[num + 1], methStart = new uint[num + 1], extends = new uint[num + 1]; for (int i = 0; i < num; i++) { uint flags = buff.ReadUInt32(); string name = buff.GetString(); string nameSpace = buff.GetString(); extends[i] = buff.GetCodedIndex(CIx.TypeDefOrRef); fieldStart[i] = buff.GetIndex(MDTable.Field); methStart[i] = buff.GetIndex(MDTable.Method); //Console.WriteLine("flags = " + Hex.Int(flags)); if (i == 0) // ASSERT first entry is always <Module> typeRefs[i] = paren.GetDefaultClass(); else if (isPublic(flags)) { if (parIxs[i] != 0) { typeRefs[i] = new NestedClassRef(paren, nameSpace, name); } else { typeRefs[i] = paren.GetExistingClass(nameSpace, name); if (typeRefs[i] == null) { typeRefs[i] = new ClassRef(paren, nameSpace, name); paren.AddToClassList((ClassRef)typeRefs[i]); } } } } fieldStart[num] = buff.GetTableSize(MDTable.Field) + 1; methStart[num] = buff.GetTableSize(MDTable.Method) + 1; // Find Nested Classes for (int i = 0; i < typeRefs.Length; i++) { if ((typeRefs[i] != null) && (typeRefs[i] is NestedClassRef)) { NestedClassRef nRef = (NestedClassRef)typeRefs[i]; ClassRef nPar = (ClassRef)typeRefs[parIxs[i] - 1]; if (nPar == null) { // parent is private, so ignore typeRefs[i] = null; } else { nRef.SetParent(nPar); nPar.AddToClassList(nRef); } } if (typeRefs[i] != null) { if (buff.GetCodedElement(CIx.TypeDefOrRef, extends[i]) == MSCorLib.mscorlib.ValueType()) ((ClassRef)typeRefs[i]).MakeValueClass(); buff.SetElementPosition(MDTable.Field, fieldStart[i]); FieldDef.GetFieldRefs(buff, fieldStart[i + 1] - fieldStart[i], (ClassRef)typeRefs[i]); buff.SetElementPosition(MDTable.Method, methStart[i]); MethodDef.GetMethodRefs(buff, methStart[i + 1] - methStart[i], (ClassRef)typeRefs[i]); } } }
internal static ClassRef ReadDef(PEReader buff, ReferenceScope resScope, uint index) { uint junk = buff.ReadUInt32(); string cName = buff.GetString(); string nsName = buff.GetString(); ClassRef newClass = (ClassRef)resScope.GetExistingClass(nsName, cName); if (newClass == null) { newClass = new ClassRef(resScope, nsName, cName); resScope.AddToClassList(newClass); } newClass.readAsDef = true; newClass.Row = index; junk = buff.GetCodedIndex(CIx.TypeDefOrRef); newClass.fieldIx = buff.GetIndex(MDTable.Field); newClass.methodIx = buff.GetIndex(MDTable.Method); return newClass; }