private void GenerateWorkForCtors(IClosureVisitor visitor) { foreach (INamedTypeDefinition type in _depot.TypesClosure.Values) { GenerateWorkForCtors(type, visitor); } }
private void GenerateWorkForVirtuals(IClosureVisitor visitor) { INamedTypeDefinition[] defs = new INamedTypeDefinition[_depot.TypesClosure.Values.Count]; _depot.TypesClosure.Values.CopyTo(defs, 0); foreach (INamedTypeDefinition type in defs) { GenerateWorkForVirtuals(type, visitor); } }
private void ProcessWorkList(IClosureVisitor visitor) { // // walk the closure of each item in the list // int loopIterations = 0; while (HasWorkToDo()) { IReference node = Dequeue(); visitor.VisitNode(node); loopIterations++; } }
// // Some types may have base types with no default ctors // and no ctors themselves. In those cases, the compiler // cannot instantiate the type without an explicit ctor // that calls one of the included base type ctors. // private void GenerateWorkForCtors(INamedTypeDefinition type, IClosureVisitor visitor) { if (TypeHelper.BaseClass(type) == null) return; List<IMethodDefinition> ctors = GetCtors(type, true); if (ctors.Count != 0) return; List<IMethodDefinition> baseCtors = GetCtors(Util.CanonicalizeType(TypeHelper.BaseClass(type)), true); if (baseCtors.Count == 0) return; int nDefaultCtors = 0; foreach (IMethodDefinition ctor in baseCtors) { if (Util.ParameterCount(ctor) == 0) { nDefaultCtors++; } } if (nDefaultCtors != 0) return; // TODO: Shouldn't this be part of implclosure? ctors = GetCtors(type, false); foreach (IMethodDefinition baseCtor in baseCtors) { foreach (IMethodDefinition ctor in ctors) { if (MethodCallsMethod(ctor, baseCtor)) { AddMemberReference(ctor); return; // @TODO: we may need to add more than just the first one we find.. } } } // at this point, no included ctor in the base type is // being called by any of the ctors in the derived type // so we have to get a little more creative if (ctors.Count > 0) { IMethodDefinition fallback = FindCalledBaseCtor(ctors[0]); if (null != fallback) { AddMemberReference(ctors[0]); AddMemberReference(fallback); } } }
private void GenerateWorkForVirtuals(INamedTypeDefinition type, IClosureVisitor visitor) { // TODO: can we use INamedTypeDefinition.ExplicitImplementationOverrides instead? //foreach (IMethodImplementation methodImpl in type.ExplicitImplementationOverrides) //{ // ITypeReference declType = methodImpl.ImplementedMethod.ContainingType; // if (_depot.TypesClosure.ContainsKey(declType.ResolvedType.InternedKey)) // { // AddMemberReference(methodImpl); // } //} // TODO: Events? foreach (ITypeDefinitionMember member in type.Members) { IMethodDefinition method = member as IMethodDefinition; IPropertyDefinition property = member as IPropertyDefinition; IEventDefinition eventDef = member as IEventDefinition; if (((method == null) || !method.IsVirtual) && ((property == null) || !Util.IsPropertyVirtual(property)) && ((eventDef == null) || !Util.IsEventVirtual(eventDef))) { continue; } // // If this or any related member on a base type or interface is in the closure, // we must ensure that all related members are also in the closure. // bool includeRelatedMembers = false; List<ITypeDefinitionMember> relatedMembers = Util.FindRelatedMembers(member, delegate (INamedTypeReference myType) { return _depot.TypesClosure.ContainsKey(myType.InternedKey) || !CanInclude(myType); } ); foreach (ITypeDefinitionMember m in relatedMembers) { ITypeDefinitionMember specializedMember = Util.CanonicalizeMember(m); if (_depot.ContainsMember(specializedMember) || !CanInclude(Util.CanonicalizeType(specializedMember.ContainingType))) includeRelatedMembers = true; } if (includeRelatedMembers) { foreach (ITypeDefinitionMember m in relatedMembers) { INamedTypeDefinition canonicalDeclaringType = Util.CanonicalizeType(m.ContainingType); if (CanInclude(canonicalDeclaringType)) { // TODO: Won't AddMemberReference add the type definition anyway? // Since these members could have resolved to another assembly, check whether we can include them. if (!_depot.TypesClosure.ContainsKey(canonicalDeclaringType.InternedKey)) Console.Error.WriteLine("ERROR: declaring type {0} of {1} not present in closure", canonicalDeclaringType, m); AddMemberReference(Util.CanonicalizeMember(m)); } } } } }
private void Validate(IClosureVisitor visitor) { //Queue<IReference> workList = visitor.WorkList; //_assembliesClosure = visitor.AssembliesClosure; //_typesClosure = visitor.TypesClosure; //_membersClosure = visitor.MembersClosure; //_methodsClosure = visitor.MethodsClosure; // // seed the work list and closures with our roots // // NOTE: the pattern here is that whenever you add a node // to the work list, it must also be added to the appropriate // closure at the same time. Adding to the work list will // cause the node to be visited, at which time, the visitor // is supposed to find all *other* references that that node // draws into the closure and adds them to the worklist and // closure at that time. foreach (IAssembly assembly in _rootAssemblies.Keys) { AddAssemblyReference(assembly); } foreach (INamedTypeDefinition type in _rootTypes.Keys) { AddTypeReference(type); } foreach (IAliasForType typeForwarder in _rootTypeForwarders.Keys) { AddTypeForwarderReference(typeForwarder); } foreach (ITypeDefinitionMember member in _rootMembers.Keys) { AddMemberReference(member); } int loopIterations = 0; while (HasWorkToDo()) { //Console.WriteLine("iterations: {0}, queue length: {1}", loopIterations, workList.Count); loopIterations++; ProcessWorkList(visitor); GenerateWorkForVirtuals(visitor); } // TODO: we should not need this if we have authored model.xml correctly. // e.g. we should have included MulticaseDelegate..ctor(object, string) and // CodeAccessSecurityAttribute..ctor(SecurityAction) GenerateWorkForCtors(visitor); while (HasWorkToDo()) { //Console.WriteLine("iterations: {0}, queue length: {1}", loopIterations, workList.Count); loopIterations++; ProcessWorkList(visitor); GenerateWorkForCtors(visitor); } }
// // Some types may have base types with no default ctors // and no ctors themselves. In those cases, the compiler // cannot instantiate the type without an explicit ctor // that calls one of the included base type ctors. // private void GenerateWorkForCtors(INamedTypeDefinition type, IClosureVisitor visitor) { if (TypeHelper.BaseClass(type) == null) { return; } List <IMethodDefinition> ctors = GetCtors(type, true); if (ctors.Count != 0) { return; } List <IMethodDefinition> baseCtors = GetCtors(Util.CanonicalizeType(TypeHelper.BaseClass(type)), true); if (baseCtors.Count == 0) { return; } int nDefaultCtors = 0; foreach (IMethodDefinition ctor in baseCtors) { if (Util.ParameterCount(ctor) == 0) { nDefaultCtors++; } } if (nDefaultCtors != 0) { return; } // TODO: Shouldn't this be part of implclosure? ctors = GetCtors(type, false); foreach (IMethodDefinition baseCtor in baseCtors) { foreach (IMethodDefinition ctor in ctors) { if (MethodCallsMethod(ctor, baseCtor)) { AddMemberReference(ctor); return; // @TODO: we may need to add more than just the first one we find.. } } } // at this point, no included ctor in the base type is // being called by any of the ctors in the derived type // so we have to get a little more creative if (ctors.Count > 0) { IMethodDefinition fallback = FindCalledBaseCtor(ctors[0]); if (null != fallback) { AddMemberReference(ctors[0]); AddMemberReference(fallback); } } }
private void GenerateWorkForVirtuals(INamedTypeDefinition type, IClosureVisitor visitor) { // TODO: can we use INamedTypeDefinition.ExplicitImplementationOverrides instead? //foreach (IMethodImplementation methodImpl in type.ExplicitImplementationOverrides) //{ // ITypeReference declType = methodImpl.ImplementedMethod.ContainingType; // if (_depot.TypesClosure.ContainsKey(declType.ResolvedType.InternedKey)) // { // AddMemberReference(methodImpl); // } //} // TODO: Events? foreach (ITypeDefinitionMember member in type.Members) { IMethodDefinition method = member as IMethodDefinition; IPropertyDefinition property = member as IPropertyDefinition; IEventDefinition eventDef = member as IEventDefinition; if (((method == null) || !method.IsVirtual) && ((property == null) || !Util.IsPropertyVirtual(property)) && ((eventDef == null) || !Util.IsEventVirtual(eventDef))) { continue; } // // If this or any related member on a base type or interface is in the closure, // we must ensure that all related members are also in the closure. // bool includeRelatedMembers = false; List <ITypeDefinitionMember> relatedMembers = Util.FindRelatedMembers(member, delegate(INamedTypeReference myType) { return(_depot.TypesClosure.ContainsKey(myType.InternedKey) || !CanInclude(myType)); } ); foreach (ITypeDefinitionMember m in relatedMembers) { ITypeDefinitionMember specializedMember = Util.CanonicalizeMember(m); if (_depot.ContainsMember(specializedMember) || !CanInclude(Util.CanonicalizeType(specializedMember.ContainingType))) { includeRelatedMembers = true; } } if (includeRelatedMembers) { foreach (ITypeDefinitionMember m in relatedMembers) { INamedTypeDefinition canonicalDeclaringType = Util.CanonicalizeType(m.ContainingType); if (CanInclude(canonicalDeclaringType)) { // TODO: Won't AddMemberReference add the type definition anyway? // Since these members could have resolved to another assembly, check whether we can include them. if (!_depot.TypesClosure.ContainsKey(canonicalDeclaringType.InternedKey)) { Console.Error.WriteLine("ERROR: declaring type {0} of {1} not present in closure", canonicalDeclaringType, m); } AddMemberReference(Util.CanonicalizeMember(m)); } } } } }