private CodeTypeDeclaration GenerateClass(GenerationContext context, @class cls) { bool isApplication = GenerationContext.IsApplicationClass(cls); CodeTypeDeclaration typeDeclaration = new CodeTypeDeclaration(); typeDeclaration.IsClass = true; // Set the name and the base class name String className = context.ConvertType(cls.name, false); String baseClassName; if (isApplication) { baseClassName = "SBApplication"; } else { baseClassName = String.IsNullOrEmpty(cls.inherits) ? "SBObject" : context.ConvertType(cls.inherits, false); } typeDeclaration.Name = className; typeDeclaration.BaseTypes.Add(baseClassName); // Add default constructor CodeConstructor defaultConstructor1 = new CodeConstructor(); defaultConstructor1.Attributes = MemberAttributes.Public; typeDeclaration.Members.Add(defaultConstructor1); // Add default constructor with pointer CodeConstructor defaultConstructor2 = new CodeConstructor(); defaultConstructor2.Attributes = MemberAttributes.Public; defaultConstructor2.Parameters.Add(new CodeParameterDeclarationExpression(typeof (IntPtr), "pointer")); defaultConstructor2.BaseConstructorArgs.Add(new CodeArgumentReferenceExpression("pointer")); typeDeclaration.Members.Add(defaultConstructor2); // Generate elements typeDeclaration.Members.AddRange(GenerateElements(context, cls).ToArray()); // Generate properties typeDeclaration.Members.AddRange(GenerateProperties(context, cls).ToArray()); // Generate commands typeDeclaration.Members.AddRange(GenerateCommands(context, cls).ToArray()); return typeDeclaration; }
private static IEnumerable<CodeMemberProperty> GenerateProperties(GenerationContext context, @class cls) { foreach (property item in context.GetPropertiesFor(cls).OrderBy(i => i.name)) { yield return GenerateProperty(context, cls, item); } }
private static CodeMemberProperty GenerateProperty(GenerationContext context, @class cls, property property) { String propertyType = context.ConvertType(property.type, true); String propertyName = NamingHelper.GenerateDotNetName(String.Empty, property.name); // Define various references CodeTypeReference typeReference = new CodeTypeReference(propertyType); CodeThisReferenceExpression thisReferenceExpression = new CodeThisReferenceExpression(); CodeTypeReferenceExpression typeReferenceExpression = new CodeTypeReferenceExpression("ObjectiveCRuntime"); // Define the property CodeMemberProperty memberProperty = new CodeMemberProperty(); memberProperty.Attributes = MemberAttributes.Public; memberProperty.Name = propertyName; memberProperty.Type = typeReference; // Generate getter switch (property.access) { case "r": case "rw": { String selector = NamingHelper.GenerateObjCName(property.name); CodeMethodReferenceExpression methodReferenceExpression = new CodeMethodReferenceExpression(typeReferenceExpression, "SendMessage"); methodReferenceExpression.TypeArguments.Add(typeReference); CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression(methodReferenceExpression, thisReferenceExpression, new CodePrimitiveExpression(selector)); CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(invokeExpression); memberProperty.GetStatements.Add(returnStatement); break; } default: break; } // Generate setter switch (property.access) { case "rw": case "w": { String selector = "set" + propertyName + ":"; CodeMethodReferenceExpression methodReferenceExpression = new CodeMethodReferenceExpression(typeReferenceExpression, "SendMessage"); CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression(methodReferenceExpression, thisReferenceExpression, new CodePrimitiveExpression(selector), new CodeVariableReferenceExpression("value")); CodeExpressionStatement expressionStatement = new CodeExpressionStatement(invokeExpression); memberProperty.SetStatements.Add(expressionStatement); break; } default: break; } return memberProperty; }
private static CodeMemberProperty GenerateElement(GenerationContext context, @class cls, element element) { String value; @class typeCls = context.Classes.FirstOrDefault(c => String.Equals(c.name, element.type)); if (typeCls != null) { // Make sure that we use the correct plural value = typeCls.plural ?? typeCls.name + "s"; } else { // Use the default name value = element.type; } String elementName = NamingHelper.GenerateDotNetName(String.Empty, value); String selector = NamingHelper.GenerateObjCName(value); // Define various references CodeTypeReference typeReference = new CodeTypeReference("SBElementArray"); CodeThisReferenceExpression thisReferenceExpression = new CodeThisReferenceExpression(); CodeTypeReferenceExpression typeReferenceExpression = new CodeTypeReferenceExpression("ObjectiveCRuntime"); // Define the property CodeMemberProperty memberProperty = new CodeMemberProperty(); memberProperty.Attributes = MemberAttributes.Public; memberProperty.Name = elementName; memberProperty.Type = typeReference; // Generate the getter CodeMethodReferenceExpression methodReferenceExpression = new CodeMethodReferenceExpression(typeReferenceExpression, "SendMessage"); methodReferenceExpression.TypeArguments.Add(typeReference); CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression(methodReferenceExpression, thisReferenceExpression, new CodePrimitiveExpression(selector)); CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(invokeExpression); memberProperty.GetStatements.Add(returnStatement); return memberProperty; }
private static IEnumerable<CodeMemberProperty> GenerateElements(GenerationContext context, @class cls) { foreach (element item in context.GetElementsFor(cls).OrderBy(i => i.type)) { yield return GenerateElement(context, cls, item); } }
private static CodeMemberMethod GenerateCommand(GenerationContext context, @class cls, command command) { bool isApplication = GenerationContext.IsApplicationClass(cls); String returnType = context.ConvertType(GenerationContext.GetType(command.result) ?? "void", true); bool hasReturnType = returnType != "void"; bool useDirectParameter = isApplication && command.directparameter != null && command.directparameter.type != "specifier"; String methodName = NamingHelper.GenerateDotNetMethodsName(command); // Define various references CodeTypeReference typeReference = new CodeTypeReference(returnType); CodeThisReferenceExpression thisReferenceExpression = new CodeThisReferenceExpression(); CodeTypeReferenceExpression typeReferenceExpression = new CodeTypeReferenceExpression("ObjectiveCRuntime"); CodeMethodReferenceExpression methodReferenceExpression = new CodeMethodReferenceExpression(typeReferenceExpression, "SendMessage"); if (hasReturnType) { methodReferenceExpression.TypeArguments.Add(typeReference); } // Define the method CodeMemberMethod memberMethod = new CodeMemberMethod(); memberMethod.Attributes = MemberAttributes.Public; memberMethod.Name = methodName; if (hasReturnType) { memberMethod.ReturnType = typeReference; } // Gather all the expressions needed for the invocation. List<CodeExpression> expressions = new List<CodeExpression>(); expressions.Add(thisReferenceExpression); expressions.Add(new CodePrimitiveExpression(NamingHelper.GenerateObjCSelector(command, isApplication))); // If the command is for the application and the direct parameter is not an object specifier, add it to the signature if (useDirectParameter) { CodeParameterDeclarationExpression parameterDeclarationExpression = new CodeParameterDeclarationExpression(); parameterDeclarationExpression.Type = new CodeTypeReference(context.ConvertType(GenerationContext.GetType(command.directparameter), true)); parameterDeclarationExpression.Name = "x"; memberMethod.Parameters.Add(parameterDeclarationExpression); expressions.Add(new CodeVariableReferenceExpression(parameterDeclarationExpression.Name)); } // Add all the parameters if (command.parameter != null) { foreach (parameter parameter in command.parameter) { CodeParameterDeclarationExpression parameterDeclarationExpression = new CodeParameterDeclarationExpression(); parameterDeclarationExpression.Type = new CodeTypeReference(context.ConvertType(GenerationContext.GetType(parameter), true)); parameterDeclarationExpression.Name = GenerationContext.ConvertParameterName(NamingHelper.GenerateObjCName(parameter.name)); memberMethod.Parameters.Add(parameterDeclarationExpression); expressions.Add(new CodeVariableReferenceExpression(parameterDeclarationExpression.Name)); } } // Generate the runtime invocation CodeMethodInvokeExpression invokeExpression = new CodeMethodInvokeExpression(methodReferenceExpression, expressions.ToArray()); CodeStatement expressionStatement; if (hasReturnType) { expressionStatement = new CodeMethodReturnStatement(invokeExpression); } else { expressionStatement = new CodeExpressionStatement(invokeExpression); } memberMethod.Statements.Add(expressionStatement); return memberMethod; }
private static IEnumerable<CodeMemberMethod> GenerateCommands(GenerationContext context, @class cls) { foreach (command item in context.GetCommandsFor(cls).OrderBy(i => i.name)) { yield return GenerateCommand(context, cls, item); } }
/// <summary> /// Gets the properties for the given class. /// </summary> public IEnumerable<property> GetPropertiesFor(@class cls) { List<property> items = new List<property>(); // Add class items if any if (cls.property != null) { items.AddRange(cls.property); } // Add extension items if any IEnumerable<property> extensionItems = this.GetClassExtensionFor(cls).Where(c => c.property != null).SelectMany(c => c.property); if (extensionItems != null) { items.AddRange(extensionItems); } foreach (property item in items) { if ("class;description;version".Contains(item.name)) { continue; } if (item.hidden) { continue; } yield return item; } }
/// <summary> /// Gets the elements for the given class. /// </summary> public IEnumerable<element> GetElementsFor(@class cls) { List<element> items = new List<element>(); // Add class items if any if (cls.element != null) { items.AddRange(cls.element); } // Add extension items if any IEnumerable<element> extensionItems = this.GetClassExtensionFor(cls).Where(c => c.element != null).SelectMany(c => c.element); if (extensionItems != null) { items.AddRange(extensionItems); } // Retrieve the base class if any @class baseClass = null; String type = cls.inherits; if (type != null) { baseClass = (from c in this.Classes where (c.id != null && String.Equals(c.id, type)) || (c.id == null && String.Equals(c.name, type)) select c).FirstOrDefault(); } // Filter out elements to avoid override if (baseClass != null && baseClass.element != null) { IEnumerable<element> baseElements = this.GetElementsFor(baseClass); foreach (element item in items) { if (item.hidden) { continue; } element baseItem = baseElements.FirstOrDefault(e => String.Equals(e.type, item.type)); if (baseItem != null) { continue; } yield return item; } } else { foreach (element item in items) { if (item.hidden) { continue; } yield return item; } } }
/// <summary> /// Gets the commands for the given class. /// </summary> public IEnumerable<command> GetCommandsFor(@class cls) { bool isApplication = IsApplicationClass(cls); bool isObject = String.IsNullOrEmpty(cls.inherits) && !isApplication; foreach (command item in this.Commands) { if (item.hidden) { continue; } if (item.parameter != null && item.parameter.Any(p => (p.name == "each" || p.name == "new"))) { continue; } if (item.parameter != null && item.parameter.Any(p => p.type == "any")) { continue; } if (item.result != null && item.result.type == "any") { continue; } if (isApplication) { if (item.directparameter != null) { String type = GetType(item.directparameter); if (type == "specifier") { if (!item.directparameter.optional) { continue; } } if (this.Classes.Any(c => String.Equals(c.name, type))) { continue; } } yield return item; } else { if (item.directparameter == null) { continue; } String type = GetType(item.directparameter); if (type == "specifier" && isObject) { yield return item; } if (type == cls.name) { yield return item; } } } }
/// <summary> /// Gets the class extension for the given class. /// </summary> public IEnumerable<classextension> GetClassExtensionFor(@class cls) { return this.ClassExtensions.Where(classExtension => String.Equals(classExtension.extends, cls.name)); }
/// <summary> /// Determines whether the specified class is the application class. /// </summary> /// <param name="cls">The CLS.</param> /// <returns> /// <c>true</c> if the specified class is the application class; otherwise, <c>false</c>. /// </returns> public static bool IsApplicationClass(@class cls) { return cls.name == "application"; }