private void GenerateInternalImplementationMethods() { for (short i = 1; i <= data.Smoke->numClasses; i++) { Smoke.Class *klass = data.Smoke->classes + i; if (klass->external) { continue; } CodeTypeDeclaration type = data.SmokeTypeMap[(IntPtr)klass]; CodeTypeDeclaration implType; if (!data.InternalTypeMap.TryGetValue(type, out implType)) { continue; } MethodsGenerator methgen = new MethodsGenerator(data, translator, implType, klass); methgen.InternalImplementation = true; foreach (KeyValuePair <Smoke.ModuleIndex, string> pair in Util.GetAbstractMethods(data.Smoke, i)) { methgen.GenerateMethod(pair.Key.smoke, pair.Key.index, pair.Value); } methgen.GenerateProperties(); } }
/* * Adds the methods to the classes created by Run() */ private void GenerateMethods() { short currentClassId = 0; Smoke.Class * klass = (Smoke.Class *)IntPtr.Zero; MethodsGenerator methgen = null; AttributeGenerator attrgen = null; CodeTypeDeclaration type = null; List <Smoke.ModuleIndex> alreadyImplemented = new List <Smoke.ModuleIndex>(); this.FillEnums(); for (short i = 1; i < data.Smoke->numMethodMaps; i++) { Smoke.MethodMap *map = data.Smoke->methodMaps + i; if (currentClassId != map->classId) { // we encountered a new class if (attrgen != null) { // generate inherited methods this.GenerateInheritedMethods(klass, methgen, attrgen, alreadyImplemented); // generate all scheduled attributes attrgen.Run(); if (PostMembersHooks != null) { PostMembersHooks(data.Smoke, klass, type); } methgen.GenerateProperties(); } currentClassId = map->classId; klass = data.Smoke->classes + currentClassId; type = data.SmokeTypeMap[(IntPtr)klass]; alreadyImplemented.Clear(); attrgen = new AttributeGenerator(data, translator, type); methgen = new MethodsGenerator(data, translator, type, klass); } string mungedName = ByteArrayManager.GetString(data.Smoke->methodNames[map->name]); if (map->method > 0) { Smoke.Method *meth = data.Smoke->methods + map->method; if ((meth->flags & (ushort)Smoke.MethodFlags.mf_enum) > 0) { continue; } if ((meth->flags & (ushort)Smoke.MethodFlags.mf_property) > 0 && // non-virtual properties are excluded (meth->flags & (ushort)Smoke.MethodFlags.mf_virtual) == 0 && (meth->flags & (ushort)Smoke.MethodFlags.mf_purevirtual) == 0) { continue; } if ((meth->flags & (ushort)Smoke.MethodFlags.mf_attribute) > 0) { attrgen.ScheduleAttributeAccessor(meth); continue; } methgen.GenerateMethod(map->method, mungedName); alreadyImplemented.Add(new Smoke.ModuleIndex(data.Smoke, map->method)); } else if (map->method < 0) { for (short *overload = data.Smoke->ambiguousMethodList + (-map->method); *overload > 0; overload++) { Smoke.Method *meth = data.Smoke->methods + *overload; if ((meth->flags & (ushort)Smoke.MethodFlags.mf_enum) > 0) { continue; } if ((meth->flags & (ushort)Smoke.MethodFlags.mf_property) > 0 && // non-virtual properties are excluded (meth->flags & (ushort)Smoke.MethodFlags.mf_virtual) == 0 && (meth->flags & (ushort)Smoke.MethodFlags.mf_purevirtual) == 0) { continue; } if ((meth->flags & (ushort)Smoke.MethodFlags.mf_attribute) > 0) { attrgen.ScheduleAttributeAccessor(meth); continue; } // if the methods differ only by constness, we will generate special code bool nextDiffersByConst = false; if (*(overload + 1) > 0) { if (SmokeMethodEqualityComparer.EqualExceptConstness(meth, data.Smoke->methods + *(overload + 1))) { nextDiffersByConst = true; } } methgen.GenerateMethod(*overload, mungedName); alreadyImplemented.Add(new Smoke.ModuleIndex(data.Smoke, *overload)); if (nextDiffersByConst) { overload++; } } } } // Generate the last scheduled attributes attrgen.Run(); // Generate remaining inherited methods this.GenerateInheritedMethods(klass, methgen, attrgen, alreadyImplemented); if (PostMembersHooks != null) { PostMembersHooks(data.Smoke, klass, type); } methgen.GenerateProperties(); AddMissingOperators(); }
private void GenerateInternalImplementationMethods() { for (short i = 1; i <= data.Smoke->numClasses; i++) { Smoke.Class* klass = data.Smoke->classes + i; if (klass->external) { continue; } CodeTypeDeclaration type = data.SmokeTypeMap[(IntPtr) klass]; CodeTypeDeclaration implType; if (!data.InternalTypeMap.TryGetValue(type, out implType)) { continue; } MethodsGenerator methgen = new MethodsGenerator(data, translator, implType, klass); methgen.InternalImplementation = true; foreach (KeyValuePair<Smoke.ModuleIndex, string> pair in Util.GetAbstractMethods(data.Smoke, i)) { methgen.GenerateMethod(pair.Key.smoke, pair.Key.index, pair.Value); } methgen.GenerateProperties(); } }
public void Run() { HashSet<short> interfaceClasses = GetClassList(); // Make the interfaces known first, otherwise Translator won't work correctly. foreach (short idx in interfaceClasses) { Smoke.Class* klass = data.Smoke->classes + idx; string className = ByteArrayManager.GetString(klass->className); int colon = className.LastIndexOf("::", StringComparison.Ordinal); string prefix = (colon != -1) ? className.Substring(0, colon) : string.Empty; string name = (colon != -1) ? className.Substring(colon + 2) : className; CodeTypeDeclaration ifaceDecl = new CodeTypeDeclaration('I' + name); ifaceDecl.IsInterface = true; CodeAttributeDeclaration attr = new CodeAttributeDeclaration("SmokeClass", new CodeAttributeArgument( new CodePrimitiveExpression(className))); ifaceDecl.CustomAttributes.Add(attr); data.GetTypeCollection(prefix).Add(ifaceDecl); data.InterfaceTypeMap[className] = ifaceDecl; } // Now generate the methods. foreach (short idx in interfaceClasses) { Smoke.Class* klass = data.Smoke->classes + idx; string className = ByteArrayManager.GetString(klass->className); CodeTypeDeclaration ifaceDecl = data.InterfaceTypeMap[className]; short* parent = data.Smoke->inheritanceList + klass->parents; while (*parent > 0) { ifaceDecl.BaseTypes.Add(translator.CppToCSharp(data.Smoke->classes + *parent, ifaceDecl)); parent++; } MethodsGenerator mg = new MethodsGenerator(data, translator, ifaceDecl, klass); AttributeGenerator ag = new AttributeGenerator(data, translator, ifaceDecl); List<IntPtr> methods = new List<IntPtr>(); ///TODO: replace this algorithm, it's highly inefficient for (short i = 0; i <= data.Smoke->numMethods && data.Smoke->methods[i].classId <= idx; i++) { Smoke.Method* meth = data.Smoke->methods + i; if (meth->classId != idx) continue; string methName = ByteArrayManager.GetString(data.Smoke->methodNames[meth->name]); // we don't want anything except protected, const or empty flags if ((meth->flags & (ushort) Smoke.MethodFlags.mf_enum) > 0 || (meth->flags & (ushort) Smoke.MethodFlags.mf_ctor) > 0 || (meth->flags & (ushort) Smoke.MethodFlags.mf_copyctor) > 0 || (meth->flags & (ushort) Smoke.MethodFlags.mf_dtor) > 0 || (meth->flags & (ushort) Smoke.MethodFlags.mf_static) > 0 || (meth->flags & (ushort) Smoke.MethodFlags.mf_internal) > 0 || (meth->flags & (ushort) Smoke.MethodFlags.mf_protected) > 0 || methName.StartsWith("operator")) { continue; } if ((meth->flags & (ushort) Smoke.MethodFlags.mf_attribute) > 0) { ag.ScheduleAttributeAccessor(meth); continue; } methods.Add((IntPtr) meth); } methods.Sort(CompareSmokeMethods); foreach (Smoke.Method* method in methods) { CodeMemberMethod cmm = mg.GenerateBasicMethodDefinition(data.Smoke, method); if (cmm != null && !ifaceDecl.HasMethod(cmm)) { ifaceDecl.Members.Add(cmm); } } mg.GenerateProperties(); foreach (CodeMemberProperty prop in ag.GenerateBasicAttributeDefinitions()) { ifaceDecl.Members.Add(prop); } } }
/* * Adds the methods to the classes created by Run() */ private void GenerateMethods(IList<IntPtr> excludedMethods) { List<Smoke.ModuleIndex> alreadyImplemented = new List<Smoke.ModuleIndex>(); this.FillEnums(); Dictionary<short, List<Smoke.MethodMap>> dictionary = new Dictionary<short, List<Smoke.MethodMap>>(); List<short> classes = new List<short>(); for (short i = 1; i < data.Smoke->numMethodMaps; i++) { Smoke.MethodMap* map = data.Smoke->methodMaps + i; if (!dictionary.ContainsKey(map->classId)) { dictionary.Add(map->classId, new List<Smoke.MethodMap>()); } dictionary[map->classId].Add(*map); if (!classes.Contains(map->classId)) { classes.Add(map->classId); } } foreach (KeyValuePair<short, List<Smoke.MethodMap>> pair in dictionary.OrderBy(k => { int chainLength = 0; Smoke.Class* klass = data.Smoke->classes + k.Key; short* parent = data.Smoke->inheritanceList + klass->parents; while (*parent > 0) { ++chainLength; klass = data.Smoke->classes + *parent; parent = data.Smoke->inheritanceList + klass->parents; } return chainLength; })) { Smoke.Class* klass = data.Smoke->classes + pair.Key; CodeTypeDeclaration type = data.SmokeTypeMap[(IntPtr) klass]; alreadyImplemented.Clear(); AttributeGenerator attrgen = new AttributeGenerator(data, translator, type); MethodsGenerator methgen = new MethodsGenerator(data, translator, type, klass); foreach (Smoke.MethodMap map in pair.Value) { string mungedName = ByteArrayManager.GetString(data.Smoke->methodNames[map.name]); if (map.method > 0) { Smoke.Method* meth = data.Smoke->methods + map.method; if (excludedMethods.Contains((IntPtr) meth) || (meth->flags & (ushort) Smoke.MethodFlags.mf_enum) > 0) { continue; } if ((meth->flags & (ushort) Smoke.MethodFlags.mf_property) > 0 // non-virtual properties are excluded && (meth->flags & (ushort) Smoke.MethodFlags.mf_virtual) == 0 && (meth->flags & (ushort) Smoke.MethodFlags.mf_purevirtual) == 0) { continue; } if ((meth->flags & (ushort) Smoke.MethodFlags.mf_attribute) > 0) { attrgen.ScheduleAttributeAccessor(meth); continue; } methgen.GenerateMethod(map.method, mungedName); alreadyImplemented.Add(new Smoke.ModuleIndex(data.Smoke, map.method)); } else if (map.method < 0) { for (short* overload = data.Smoke->ambiguousMethodList + (-map.method); *overload > 0; overload++) { Smoke.Method* meth = data.Smoke->methods + *overload; if (excludedMethods.Contains((IntPtr) meth) || (meth->flags & (ushort) Smoke.MethodFlags.mf_enum) > 0) { continue; } if ((meth->flags & (ushort) Smoke.MethodFlags.mf_property) > 0 // non-virtual properties are excluded && (meth->flags & (ushort) Smoke.MethodFlags.mf_virtual) == 0 && (meth->flags & (ushort) Smoke.MethodFlags.mf_purevirtual) == 0) { continue; } if ((meth->flags & (ushort) Smoke.MethodFlags.mf_attribute) > 0) { attrgen.ScheduleAttributeAccessor(meth); continue; } // if the methods differ only by constness, we will generate special code bool nextDiffersByConst = false; if (*(overload + 1) > 0) { if (SmokeMethodEqualityComparer.EqualExceptConstness(meth, data.Smoke->methods + *(overload + 1))) nextDiffersByConst = true; } methgen.GenerateMethod(*overload, mungedName); alreadyImplemented.Add(new Smoke.ModuleIndex(data.Smoke, *overload)); if (nextDiffersByConst) overload++; } } } // generate inherited methods this.GenerateInheritedMethods(klass, methgen, attrgen, alreadyImplemented); // generate all scheduled attributes attrgen.Run(); methgen.GenerateProperties(); } foreach (short @class in classes) { Smoke.Class* klass = data.Smoke->classes + @class; CodeTypeDeclaration type = data.SmokeTypeMap[(IntPtr) klass]; if (PostMembersHooks != null) { PostMembersHooks(this.data.Smoke, klass, type); } } AddMissingOperators(); }
public void Run() { HashSet <short> interfaceClasses = GetClassList(); // Make the interfaces known first, otherwise Translator won't work correctly. foreach (short idx in interfaceClasses) { Smoke.Class *klass = data.Smoke->classes + idx; string className = ByteArrayManager.GetString(klass->className); int colon = className.LastIndexOf("::", StringComparison.Ordinal); string prefix = (colon != -1) ? className.Substring(0, colon) : string.Empty; string name = (colon != -1) ? className.Substring(colon + 2) : className; CodeTypeDeclaration ifaceDecl = new CodeTypeDeclaration('I' + name); ifaceDecl.IsInterface = true; CodeAttributeDeclaration attr = new CodeAttributeDeclaration("SmokeClass", new CodeAttributeArgument( new CodePrimitiveExpression(className))); ifaceDecl.CustomAttributes.Add(attr); data.GetTypeCollection(prefix).Add(ifaceDecl); data.InterfaceTypeMap[className] = ifaceDecl; } // Now generate the methods. foreach (short idx in interfaceClasses) { Smoke.Class * klass = data.Smoke->classes + idx; string className = ByteArrayManager.GetString(klass->className); CodeTypeDeclaration ifaceDecl = data.InterfaceTypeMap[className]; short *parent = data.Smoke->inheritanceList + klass->parents; while (*parent > 0) { ifaceDecl.BaseTypes.Add(translator.CppToCSharp(data.Smoke->classes + *parent)); parent++; } MethodsGenerator mg = new MethodsGenerator(data, translator, ifaceDecl, klass); AttributeGenerator ag = new AttributeGenerator(data, translator, ifaceDecl); List <IntPtr> methods = new List <IntPtr>(); ///TODO: replace this algorithm, it's highly inefficient for (short i = 0; i <= data.Smoke->numMethods && data.Smoke->methods[i].classId <= idx; i++) { Smoke.Method *meth = data.Smoke->methods + i; if (meth->classId != idx) { continue; } string methName = ByteArrayManager.GetString(data.Smoke->methodNames[meth->name]); // we don't want anything except protected, const or empty flags if ((meth->flags & (ushort)Smoke.MethodFlags.mf_enum) > 0 || (meth->flags & (ushort)Smoke.MethodFlags.mf_ctor) > 0 || (meth->flags & (ushort)Smoke.MethodFlags.mf_copyctor) > 0 || (meth->flags & (ushort)Smoke.MethodFlags.mf_dtor) > 0 || (meth->flags & (ushort)Smoke.MethodFlags.mf_static) > 0 || (meth->flags & (ushort)Smoke.MethodFlags.mf_internal) > 0 || (meth->flags & (ushort)Smoke.MethodFlags.mf_protected) > 0 || methName.StartsWith("operator")) { continue; } if ((meth->flags & (ushort)Smoke.MethodFlags.mf_attribute) > 0) { ag.ScheduleAttributeAccessor(meth); continue; } methods.Add((IntPtr)meth); } methods.Sort(CompareSmokeMethods); foreach (Smoke.Method *method in methods) { CodeMemberMethod cmm = mg.GenerateBasicMethodDefinition(data.Smoke, method); if (cmm != null && !ifaceDecl.HasMethod(cmm)) { ifaceDecl.Members.Add(cmm); } } mg.GenerateProperties(); foreach (CodeMemberProperty prop in ag.GenerateBasicAttributeDefinitions()) { ifaceDecl.Members.Add(prop); } } }