public ConvertType ( String type, bool anyType ) : String | ||
type | String | |
anyType | bool | |
return | String |
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 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 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 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 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 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; }