private void FillEnums() { for (short i = 1; i < this.data.Smoke->numMethodMaps; i++) { Smoke.MethodMap *map = this.data.Smoke->methodMaps + i; if (map->method > 0) { Smoke.Method *meth = this.data.Smoke->methods + map->method; if ((meth->flags & (ushort)Smoke.MethodFlags.mf_enum) > 0) { this.eg.DefineMember(meth); } } else if (map->method < 0) { for (short *overload = this.data.Smoke->ambiguousMethodList + (-map->method); *overload > 0; overload++) { Smoke.Method *meth = this.data.Smoke->methods + *overload; if ((meth->flags & (ushort)Smoke.MethodFlags.mf_enum) > 0) { this.eg.DefineMember(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, this.data.Smoke->methods + *(overload + 1))) { nextDiffersByConst = true; } } if (nextDiffersByConst) { overload++; } } } } }
/* * 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(); }
public void Run() { for (short classId = 1; classId <= data.Smoke->numClasses; classId++) { Smoke.Class *klass = data.Smoke->classes + classId; if (klass->external) { continue; } List <Property> props = new List <Property>(); if (!GetProperties(data.Smoke, classId, (name, originalType, typeName, writable, isEnum) => props.Add(new Property(name, originalType, typeName, writable, isEnum)))) { continue; } CodeTypeDeclaration type = data.SmokeTypeMap[(IntPtr)klass]; string className = ByteArrayManager.GetString(klass->className); foreach (Property prop in props) { CodeMemberProperty cmp = new CodeMemberProperty(); try { bool isRef; short id = data.Smoke->IDType(prop.Type); if (id > 0) { cmp.Type = translator.CppToCSharp(data.Smoke->types + id, out isRef); } else { if (!prop.Type.Contains("::")) { id = data.Smoke->IDType(className + "::" + prop.Type); if (id > 0) { cmp.Type = translator.CppToCSharp(data.Smoke->types + id, out isRef); } else { cmp.Type = translator.CppToCSharp(prop.Type, out isRef); } } cmp.Type = translator.CppToCSharp(prop.Type, out isRef); } } catch (NotSupportedException) { Debug.Print(" |--Won't wrap Property {0}::{1}", className, prop.Name); continue; } if (documentation.ContainsKey(type)) { IList <string> docs = documentation[type]; for (int i = 0; i < docs.Count; i++) { Match match = Regex.Match(docs[i], prop.Name + " : (const )?" + prop.OriginalType + @"\n(?<docs>This.*?)\nAccess functions:", RegexOptions.Singleline); if (match.Success) { Util.FormatComment(match.Groups["docs"].Value, cmp, i > 0); break; } } } cmp.Name = prop.Name; // capitalize the first letter StringBuilder builder = new StringBuilder(cmp.Name); builder[0] = char.ToUpper(builder[0]); string capitalized = builder.ToString(); // If the new name clashes with a name of a type declaration, keep the lower-case name (or even make the name lower-case). var typesWithSameName = from member in data.GetAccessibleMembers(data.Smoke->classes + classId) where (member.Type == MemberTypes.NestedType || member.Type == MemberTypes.Method) && member.Name == capitalized select member; if (typesWithSameName.Any()) { Debug.Print( " |--Conflicting names: property/(type or method): {0} in class {1} - keeping original property name", capitalized, className); if (capitalized == cmp.Name) { builder[0] = char.ToLower(builder[0]); cmp.Name = builder.ToString(); // lower case the property if necessary } } else { cmp.Name = capitalized; } cmp.HasGet = true; cmp.HasSet = prop.IsWritable; cmp.Attributes = MemberAttributes.Public | MemberAttributes.New | MemberAttributes.Final; cmp.CustomAttributes.Add(new CodeAttributeDeclaration("Q_PROPERTY", new CodeAttributeArgument( new CodePrimitiveExpression(prop.OriginalType)), new CodeAttributeArgument( new CodePrimitiveExpression(prop.Name)))); // ===== get-method ===== short getterMapId = FindQPropertyGetAccessorMethodMapId(classId, prop, capitalized); if (getterMapId == 0) { Debug.Print(" |--Missing 'get' method for property {0}::{1} - using QObject.Property()", className, prop.Name); cmp.GetStatements.Add(new CodeMethodReturnStatement(new CodeCastExpression(cmp.Type, new CodeMethodInvokeExpression( new CodeThisReferenceExpression(), "Property", new CodePrimitiveExpression(prop.Name))))); } else { Smoke.MethodMap *map = data.Smoke->methodMaps + getterMapId; short getterId = map->method; if (getterId < 0) { // simply choose the first (i.e. non-const) version if there are alternatives getterId = data.Smoke->ambiguousMethodList[-getterId]; } Smoke.Method *getter = data.Smoke->methods + getterId; if (getter->classId != classId) { // The actual get method is defined in a parent class - don't create a property for it. continue; } if ((getter->flags & (uint)Smoke.MethodFlags.mf_virtual) == 0 && (getter->flags & (uint)Smoke.MethodFlags.mf_purevirtual) == 0) { cmp.GetStatements.Add(new CodeMethodReturnStatement(new CodeCastExpression(cmp.Type, new CodeMethodInvokeExpression( SmokeSupport.interceptor_Invoke, new CodePrimitiveExpression( ByteArrayManager.GetString( data.Smoke->methodNames[getter->name ])), new CodePrimitiveExpression( data.Smoke->GetMethodSignature(getter)), new CodeTypeOfExpression(cmp.Type), new CodePrimitiveExpression(false))))); } else { cmp.HasGet = false; if (!cmp.HasSet) { // the get accessor is virtual and there's no set accessor => continue continue; } } } // ===== set-method ===== if (!prop.IsWritable) { // not writable? => continue type.Members.Add(cmp); continue; } char mungedSuffix; short setterMethId = FindQPropertySetAccessorMethodId(classId, prop, capitalized, out mungedSuffix); if (setterMethId == 0) { Debug.Print(" |--Missing 'set' method for property {0}::{1} - using QObject.SetProperty()", className, prop.Name); cmp.SetStatements.Add(new CodeExpressionStatement( new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), "SetProperty", new CodePrimitiveExpression(prop.Name), new CodeArgumentReferenceExpression("value")))); } else { Smoke.Method *setter = data.Smoke->methods + setterMethId; if (setter->classId != classId) { // defined in parent class, continue type.Members.Add(cmp); continue; } string setterName = ByteArrayManager.GetString(data.Smoke->methodNames[setter->name]); if (!cmp.HasGet) { // so the 'get' method is virtual - generating a property for only the 'set' method is a bad idea MethodsGenerator mg = new MethodsGenerator(data, translator, type, klass); mg.GenerateMethod(setterMethId, setterName + mungedSuffix); continue; } cmp.SetStatements.Add(new CodeExpressionStatement( new CodeMethodInvokeExpression(SmokeSupport.interceptor_Invoke, new CodePrimitiveExpression(setterName + mungedSuffix), new CodePrimitiveExpression( this.data.Smoke->GetMethodSignature(setterMethId)), new CodeTypeOfExpression(typeof(void)), new CodePrimitiveExpression(false), new CodeTypeOfExpression(cmp.Type), new CodeArgumentReferenceExpression("value")))); } type.Members.Add(cmp); } } }