internal void GenerateSource(SourceStreamWriter sw, RegistryContext ctx)
		{
			if (sw == null)
				throw new ArgumentNullException("sw");
			if (ctx == null)
				throw new ArgumentNullException("ctx");

			bool bitmask = Enums.TrueForAll(delegate(Enumerant item) {
				Enumerant actualEnumerant = ctx.Registry.GetEnumerant(item.Name);

				return (actualEnumerant == null || actualEnumerant.ParentEnumerantBlock.Type == "bitmask");
			});

			// Collect group enumerants by their value
			Dictionary<string, List<Enumerant>> groupEnums = new Dictionary<string, List<Enumerant>>();

			// ...include all enums defined in this group
			foreach (Enumerant item in Enums) {
				Enumerant itemValue = ctx.Registry.GetEnumerant(item.Name);

				if (itemValue != null) {
					if (!groupEnums.ContainsKey(itemValue.Value))
						groupEnums.Add(itemValue.Value, new List<Enumerant>());
					groupEnums[itemValue.Value].Add(itemValue);
				}
			}

			// Modify canonical enumeration (value/block/group) definition
			CommandFlagsDatabase.EnumerantItem enumerantExtension = CommandFlagsDatabase.FindEnumerant(Name);

			if (enumerantExtension != null) {
				// ...override group information
				if (enumerantExtension.Type != null) {
					switch (enumerantExtension.Type) {
						case "bitmask":
							bitmask = true;
							break;
					}
				}

				// ...include all enums to be added by additional configuration
				foreach (string addedEnum in enumerantExtension.AddEnumerants) {
					Enumerant addedEnumValue = ctx.Registry.GetEnumerant(addedEnum);

					if (addedEnumValue != null) {
						if (!groupEnums.ContainsKey(addedEnumValue.Value))
							groupEnums.Add(addedEnumValue.Value, new List<Enumerant>());

						// Note: since specification can be updated while the CommandFlags.xml is not in synch, the specification
						// may defined missed enumerant values. In this case do not add enumerant value
						if (groupEnums[addedEnumValue.Value].Contains(addedEnumValue) == false)
							groupEnums[addedEnumValue.Value].Add(addedEnumValue);
					}
				}
			}

			// Make enumerants distinct (discard duplicated enumerants, mainly from extensions _ARB, _EXT, ...)
			List<Enumerant> uniqueEnums = new List<Enumerant>();

			foreach (KeyValuePair<string, List<Enumerant>> pair in groupEnums) {
				if (pair.Value.Count > 1) {
					Enumerant shorterNameEnum = null;

					foreach (Enumerant item in pair.Value) {
						if ((shorterNameEnum == null) || (shorterNameEnum.Name.Length > item.Name.Length))
							shorterNameEnum = item;
					}

					uniqueEnums.Add(shorterNameEnum);
				} else
					uniqueEnums.Add(pair.Value[0]);
			}

			sw.WriteLine("/// <summary>");
			sw.WriteLine("/// Strongly typed enumeration {0}.", Name);
			sw.WriteLine("/// </summary>");
			if (bitmask)
				sw.WriteLine("[Flags()]");
			sw.WriteLine("public enum {0}{1}", Name, bitmask ? " : uint" : String.Empty);
			sw.WriteLine("{");
			sw.Indent();
			foreach (Enumerant enumerant in uniqueEnums) {
				List<Enumerant> allEnums = groupEnums[enumerant.Value];
				string bindingName = enumerant.EnumAlias == null ? enumerant.ImplementationName : enumerant.EnumAlias.ImplementationName;
				string camelCase = SpecificationStyle.GetCamelCase(bindingName);

				sw.WriteLine("/// <summary>");
				if (allEnums.Count > 1) {
					StringBuilder sb = new StringBuilder();

					sb.Append("Strongly typed for value ");
					for (int i = 0; i < allEnums.Count; i++) {
						sb.Append(allEnums[i].Name);
						if (i < allEnums.Count - 1)
							sb.Append(", ");
					}
					sb.Append(".");

					foreach (string docLine in RegistryDocumentation.SplitDocumentationLines(sb.ToString()))
						sw.WriteLine("/// {0}", docLine);
				} else
					sw.WriteLine("/// Strongly typed for value {0}.", enumerant.Name);
				sw.WriteLine("/// </summary>");
				sw.WriteLine("{0} = Gl.{1},", camelCase, bindingName);
				sw.WriteLine();
			}
			sw.Unindent();
			sw.WriteLine("}");
		}
Example #2
0
		/// <summary>
		/// Generate the command implementation (pinned variant).
		/// </summary>
		/// <param name="sw">
		/// The <see cref="SourceStreamWriter"/> used to write the source code.
		/// </param>
		/// <param name="ctx">
		/// The <see cref="RegistryContext"/> defining the OpenGL registry information.
		/// </param>
		/// <param name="commandParams">
		/// A <see cref="T:List{CommandParameter}"/> determining the method overload.
		/// </param>
		private void GenerateImplementation_Pinned(SourceStreamWriter sw, RegistryContext ctx, List<CommandParameter> commandParams)
		{
			// Signature
			GenerateImplementation_Signature(sw, ctx, commandParams);

			// Implementation block
			sw.WriteLine("{");
			sw.Indent();

			#region Pinned Object Block (Open)

			foreach (CommandParameter param in commandParams)
				param.WritePinnedVariable(sw, ctx, this);

			sw.WriteLine("try {");
			sw.Indent();

			#endregion

			#region Implementation Call

			sw.WriteIdentation();
			if (HasReturnValue)
				sw.Write("return (");

			sw.Write("{0}(", GetImplementationName(ctx));

			#region Parameters

			for (int i = 0; i < commandParams.Count; i++) {
				CommandParameter param = commandParams[i];

				param.WriteDelegateParam(sw, ctx, this);

				if (i != commandParams.Count - 1)
					sw.Write(", ");
			}

			#endregion

			sw.Write(")");

			if (HasReturnValue)
				sw.Write(")");
			sw.Write(";");
			sw.WriteLine();

			#endregion

			#region Pinned Object Block (Close)

			sw.Unindent();
			sw.WriteLine("} finally {");
			sw.Indent();

			foreach (CommandParameter param in commandParams)
				param.WriteUnpinCommand(sw, ctx, this);

			sw.Unindent();
			sw.WriteLine("}");

			#endregion

			sw.Unindent();
			sw.WriteLine("}");
		}
Example #3
0
		/// <summary>
		/// Generate the command implementation (generate one object variant).
		/// </summary>
		/// <param name="sw">
		/// The <see cref="SourceStreamWriter"/> used to write the source code.
		/// </param>
		/// <param name="ctx">
		/// The <see cref="RegistryContext"/> defining the OpenGL registry information.
		/// </param>
		/// <param name="commandParams">
		/// A <see cref="T:List{CommandParameter}"/> determining the method overload.
		/// </param>
		private void GenerateImplementation_GenOneObject(SourceStreamWriter sw, RegistryContext ctx)
		{
			List<CommandParameter> commandParams = new List<CommandParameter>();
			string implementationName = GetImplementationName(ctx);

			if      (implementationName.EndsWith("ies"))
				implementationName = implementationName.Substring(0, implementationName.Length - 3) + "y";
			else if (implementationName.EndsWith("s"))
				implementationName = implementationName.Substring(0, implementationName.Length - 1);

			foreach (CommandParameter commandParameter in Parameters)
				commandParams.Add(new CommandParameterArray(commandParameter, ctx, this));

			List<CommandParameterArray> arrayParameters = new List<CommandParameterArray>();
			List<CommandParameter> signatureParams = commandParams.FindAll(delegate(CommandParameter item) {
				bool compatible = CommandParameterArray.IsCompatible(item, ctx, this);
				bool arrayLengthParam = CommandParameterArray.IsArrayLengthParameter(item, ctx, this);

				if (compatible)
					arrayParameters.Add((CommandParameterArray)item);

				return (!compatible && !arrayLengthParam);
			});

			Debug.Assert(arrayParameters.Count == 1);
			CommandParameterArray returnParameter = arrayParameters[0];
			string returnParameterType = returnParameter.GetImplementationType(ctx, this);

			// Remove []
			returnParameterType = returnParameterType.Substring(0, returnParameterType.Length - 2);

			// Signature
			GenerateImplementation_Signature(sw, ctx, signatureParams, implementationName, returnParameterType);

			// Implementation block
			sw.WriteLine("{");
			sw.Indent();

			#region Local Variables

			sw.WriteLine("{0}[] {1} = new {0}[1];", returnParameterType, ReturnVariableName);

			#endregion

			#region Implementation Call

			sw.WriteIdentation();
			sw.Write("{0}(", GetImplementationName(ctx));

			#region Parameters

			for (int i = 0; i < commandParams.Count; i++) {
				CommandParameter param = commandParams[i];

				if        (CommandParameterArray.IsArrayLengthParameter(param, ctx, this)) {
					continue;
				} else if (CommandParameterArray.IsCompatible(param, ctx, this))
					sw.Write(ReturnVariableName);
				else
					param.WriteDelegateParam(sw, ctx, this);

				if (i != commandParams.Count - 1)
					sw.Write(", ");
			}

			#endregion

			sw.Write(");");
			sw.WriteLine();

			sw.WriteLine("return ({0}[0]);", ReturnVariableName);

			#endregion

			sw.Unindent();
			sw.WriteLine("}");
		}
Example #4
0
        /// <summary>
        /// Generate the command implementation (fixed variant).
        /// </summary>
        /// <param name="sw">
        /// The <see cref="SourceStreamWriter"/> used to write the source code.
        /// </param>
        /// <param name="ctx">
        /// The <see cref="RegistryContext"/> defining the OpenGL registry information.
        /// </param>
        /// <param name="commandParams">
        /// A <see cref="T:List{CommandParameter}"/> determining the method overload.
        /// </param>
        private void GenerateImplementation_Default(SourceStreamWriter sw, RegistryContext ctx, List <CommandParameter> commandParams)
        {
            List <Command> aliases      = new List <Command>();
            Command        aliasCommand = this;

            // The implementation returned type
            string returnType = aliasCommand.GetImplementationReturnType(ctx);
            // Is fixed implementation
            bool fixedImplementation = IsFixedImplementation(ctx, commandParams);

            aliases.Add(this);
            aliases.AddRange(Aliases);

            // Signature
            GenerateImplementation_Signature(sw, ctx, commandParams);

            // Implementation block
            sw.WriteLine("{");
            sw.Indent();

            #region Debug Assertions

            // Debug assertions
            foreach (CommandParameter param in commandParams)
            {
                param.WriteDebugAssertion(sw, ctx, this);
            }

            #endregion

            #region Local Variables

            // Local variable: returned value
            if (HasReturnValue)
            {
                sw.WriteLine("{0} {1};", DelegateReturnType, ReturnVariableName);
                sw.WriteLine();
            }

            #endregion

            #region Unsafe Block (Open)

            if (fixedImplementation)
            {
                sw.WriteLine("unsafe {");                                                                               // (1)
                sw.Indent();

                foreach (CommandParameter param in commandParams)
                {
                    param.WriteFixedStatement(sw, ctx, this);
                }

                sw.WriteLine("{");                                                                                              // (2)
                sw.Indent();
            }

            #endregion

            sw.WriteLine("Debug.Assert(Delegates.{0} != null, \"{0} not implemented\");", aliasCommand.DelegateName);

            #region Delegate Call

            sw.WriteIdentation();

            // Local return value
            if (HasReturnValue)
            {
                sw.Write("{0} = ", ReturnVariableName);
            }

            sw.Write("Delegates.{0}(", aliasCommand.DelegateName);

            #region Parameters

            for (int i = 0; i < commandParams.Count; i++)
            {
                CommandParameter param = commandParams[i];

                param.WriteDelegateParam(sw, ctx, this);

                if (i != Parameters.Count - 1)
                {
                    sw.Write(", ");
                }
            }

            #endregion

            sw.Write(")");

            sw.Write(";");
            sw.WriteLine();

            #endregion

            #region Call Log

            sw.WriteIdentation(); sw.Write("LogFunction(");

            #region Call Log - Format String

            sw.Write("\"{0}(", aliasCommand.ImportName);
            for (int i = 0; i < commandParams.Count; i++)
            {
                commandParams[i].WriteCallLogFormatParam(sw, ctx, this, i);
                if (i < commandParams.Count - 1)
                {
                    sw.Write(", ");
                }
            }
            sw.Write(")");

            if (HasReturnValue)
            {
                sw.Write(" = {{{0}}}", commandParams.Count);
            }

            sw.Write("\"");

            #endregion

            #region Call Log - Format Arguments

            if ((commandParams.Count > 0) || HasReturnValue)
            {
                sw.Write(", ");

                for (int i = 0; i < commandParams.Count; i++)
                {
                    commandParams[i].WriteCallLogArgParam(sw, ctx, this);
                    if (i < commandParams.Count - 1)
                    {
                        sw.Write(", ");
                    }
                }
            }

            // Return value
            if (HasReturnValue)
            {
                if (commandParams.Count > 0)
                {
                    sw.Write(", ");
                }
                CommandParameter.WriteCallLogArgParam(sw, GetReturnValueExpression(ctx), returnType);
            }

            #endregion

            sw.Write(");");
            sw.WriteLine();

            #endregion

            #region Unsafe Block (Close)

            if (fixedImplementation)
            {
                sw.Unindent();
                sw.WriteLine("}");                                                                                              // (2) CLOSED
                sw.Unindent();
                sw.WriteLine("}");                                                                                              // (1) CLOSED
            }

            #endregion

            // Check call errors
            if ((Flags & CommandFlags.NoGetError) == 0)
            {
                string returnValue = "null";

                // Optionally pass the returned value to error checking method
                if (HasReturnValue)
                {
                    returnValue = ReturnVariableName;
                }

                sw.WriteLine("DebugCheckErrors({0});", returnValue);
            }

            // Returns value
            if (HasReturnValue)
            {
                sw.WriteLine();
                sw.WriteLine("return ({0});", GetReturnValueExpression(ctx));
            }

            sw.Unindent();
            sw.WriteLine("}");
        }
Example #5
0
		/// <summary>
		/// Generate the command implementation (fixed variant).
		/// </summary>
		/// <param name="sw">
		/// The <see cref="SourceStreamWriter"/> used to write the source code.
		/// </param>
		/// <param name="ctx">
		/// The <see cref="RegistryContext"/> defining the OpenGL registry information.
		/// </param>
		/// <param name="commandParams">
		/// A <see cref="T:List{CommandParameter}"/> determining the method overload.
		/// </param>
		private void GenerateImplementation_Default(SourceStreamWriter sw, RegistryContext ctx, List<CommandParameter> commandParams)
		{
			List<Command> aliases = new List<Command>();
			Command aliasCommand = this;

			// The implementation returned type
			string returnType = aliasCommand.GetImplementationReturnType(ctx);
			// The delegate returned type
			string delegateReturnType = aliasCommand.DelegateReturnType;

			bool fixedImplementation = IsFixedImplementation(ctx, commandParams);
			// At least one parameter is an array with length correlated with another parameter
			bool isArrayImplementation = commandParams.FindIndex(delegate(CommandParameter item) { return (item is CommandParameterArray); }) >= 0;
			// Returned value must be marshalled as string
			bool marshalReturnedString = aliasCommand.HasReturnValue && (returnType.ToLower() == "string") && (delegateReturnType.ToLower() != "string");
			// Returned value must be marshalled as structure
			bool marshalReturnedStruct = aliasCommand.HasReturnValue && (DelegateReturnType == "IntPtr") && (GetImplementationReturnType(ctx) != "IntPtr");

			aliases.Add(this);
			aliases.AddRange(Aliases);

			// Signature
			GenerateImplementation_Signature(sw, ctx, commandParams);

			// Implementation block
			sw.WriteLine("{");
			sw.Indent();

			#region Debug Assertions

			// Debug assertions
			foreach (CommandParameter param in commandParams)
				param.WriteDebugAssertion(sw, ctx, this);

			#endregion

			#region Local Variables

			// Local variable: returned value
			if (HasReturnValue) {
				sw.WriteLine("{0} {1};", DelegateReturnType, ReturnVariableName);
				sw.WriteLine();
			}

			#endregion

			#region Unsafe Block (Open)

			if (fixedImplementation) {
				sw.WriteLine("unsafe {");								// (1)
				sw.Indent();

				foreach (CommandParameter param in commandParams)
					param.WriteFixedStatement(sw, ctx, this);

				sw.WriteLine("{");										// (2)
				sw.Indent();
			}

			#endregion

			sw.WriteLine("Debug.Assert(Delegates.{0} != null, \"{0} not implemented\");", aliasCommand.DelegateName);

			#region Delegate Call

			sw.WriteIdentation();

			// Local return value
			if (HasReturnValue) 
				sw.Write("{0} = ", ReturnVariableName);

			sw.Write("Delegates.{0}(", aliasCommand.DelegateName);

			#region Parameters

			for (int i = 0; i < commandParams.Count; i++) {
				CommandParameter param = commandParams[i];

				param.WriteDelegateParam(sw, ctx, this);

				if (i != Parameters.Count - 1)
					sw.Write(", ");
			}

			#endregion

			sw.Write(")");

			sw.Write(";");
			sw.WriteLine();

			#endregion

			#region Call Log

			sw.WriteIdentation(); sw.Write("CallLog(");

			#region Call Log - Format String

			sw.Write("\"{0}(", aliasCommand.ImportName);
			for (int i = 0; i < commandParams.Count; i++)
			{
				commandParams[i].WriteCallLogFormatParam(sw, ctx, this, i);
				if (i < commandParams.Count - 1)
					sw.Write(", ");
			}
			sw.Write(")");

			if (HasReturnValue)
				sw.Write(" = {{{0}}}", commandParams.Count);

			sw.Write("\"");

			#endregion

			#region Call Log - Format Arguments

			if ((commandParams.Count > 0) || HasReturnValue) {
				sw.Write(", ");

				for (int i = 0; i < commandParams.Count; i++) {
					commandParams[i].WriteCallLogArgParam(sw, ctx, this);
					if (i < commandParams.Count - 1)
						sw.Write(", ");
				}
			}

			// Return value
			if (HasReturnValue)
			{
				if (commandParams.Count > 0)
					sw.Write(", ");
				CommandParameter.WriteCallLogArgParam(sw, ReturnVariableName, returnType);
			}

			#endregion

			sw.Write(");");
			sw.WriteLine();

			#endregion

			#region Unsafe Block (Close)

			if (fixedImplementation)
			{
				sw.Unindent();
				sw.WriteLine("}");										// (2) CLOSED
				sw.Unindent();
				sw.WriteLine("}");										// (1) CLOSED
			}

			#endregion

			// Check call errors
			if ((Flags & CommandFlags.NoGetError) == 0)
				sw.WriteLine("DebugCheckErrors();");

			// Returns value
			if (HasReturnValue) {
				sw.WriteLine();

				if      (marshalReturnedString)
					sw.WriteLine("return (Marshal.PtrToStringAnsi({0}));", ReturnVariableName);
				else if (marshalReturnedStruct)
					sw.WriteLine("return (({1})Marshal.PtrToStructure({0}, typeof({1})));", ReturnVariableName, GetImplementationReturnType(ctx));
				else if (returnType != delegateReturnType)
					sw.WriteLine("return (({1}){0});", ReturnVariableName, GetImplementationReturnType(ctx));
				else
					sw.WriteLine("return ({0});", ReturnVariableName);
			}

			sw.Unindent();
			sw.WriteLine("}");
		}
Example #6
0
        /// <summary>
        /// Generate the command implementation (generate one object variant).
        /// </summary>
        /// <param name="sw">
        /// The <see cref="SourceStreamWriter"/> used to write the source code.
        /// </param>
        /// <param name="ctx">
        /// The <see cref="RegistryContext"/> defining the OpenGL registry information.
        /// </param>
        /// <param name="commandParams">
        /// A <see cref="T:List{CommandParameter}"/> determining the method overload.
        /// </param>
        private void GenerateImplementation_GenOneObject(SourceStreamWriter sw, RegistryContext ctx)
        {
            List <CommandParameter> commandParams = new List <CommandParameter>();
            string implementationName             = GetImplementationName(ctx);

            if (implementationName.EndsWith("ies"))
            {
                implementationName = implementationName.Substring(0, implementationName.Length - 3) + "y";
            }
            else if (implementationName.EndsWith("s"))
            {
                implementationName = implementationName.Substring(0, implementationName.Length - 1);
            }

            foreach (CommandParameter commandParameter in Parameters)
            {
                commandParams.Add(new CommandParameterArrayLength(commandParameter, ctx, this));
            }

            List <CommandParameterArrayLength> arrayParameters = new List <CommandParameterArrayLength>();
            List <CommandParameter>            signatureParams = commandParams.FindAll(delegate(CommandParameter item) {
                bool compatible       = CommandParameterArrayLength.IsCompatible(ctx, this, item);
                bool arrayLengthParam = CommandParameterArrayLength.IsArrayLengthParameter(item, ctx, this);

                if (compatible)
                {
                    arrayParameters.Add((CommandParameterArrayLength)item);
                }

                return(!compatible && !arrayLengthParam);
            });

            Debug.Assert(arrayParameters.Count == 1);
            CommandParameterArrayLength returnParameter = arrayParameters[0];
            string returnParameterType = returnParameter.GetImplementationType(ctx, this);

            // Remove []
            returnParameterType = returnParameterType.Substring(0, returnParameterType.Length - 2);

            // Signature
            GenerateImplementation_Signature(sw, ctx, signatureParams, implementationName, returnParameterType);

            // Implementation block
            sw.WriteLine("{");
            sw.Indent();

            #region Local Variables

            sw.WriteLine("{0}[] {1} = new {0}[1];", returnParameterType, ReturnVariableName);

            #endregion

            #region Implementation Call

            sw.WriteIdentation();
            sw.Write("{0}(", GetImplementationName(ctx));

            #region Parameters

            for (int i = 0; i < commandParams.Count; i++)
            {
                CommandParameter param = commandParams[i];

                if (CommandParameterArrayLength.IsArrayLengthParameter(param, ctx, this))
                {
                    continue;
                }
                else if (CommandParameterArrayLength.IsCompatible(ctx, this, param))
                {
                    sw.Write(ReturnVariableName);
                }
                else
                {
                    param.WriteDelegateParam(sw, ctx, this);
                }

                if (i != commandParams.Count - 1)
                {
                    sw.Write(", ");
                }
            }

            #endregion

            sw.Write(");");
            sw.WriteLine();

            sw.WriteLine("return ({0}[0]);", ReturnVariableName);

            #endregion

            sw.Unindent();
            sw.WriteLine("}");
        }
Example #7
0
        /// <summary>
        /// Generate the command implementation (pinned variant).
        /// </summary>
        /// <param name="sw">
        /// The <see cref="SourceStreamWriter"/> used to write the source code.
        /// </param>
        /// <param name="ctx">
        /// The <see cref="RegistryContext"/> defining the OpenGL registry information.
        /// </param>
        /// <param name="commandParams">
        /// A <see cref="T:List{CommandParameter}"/> determining the method overload.
        /// </param>
        private void GenerateImplementation_Pinned(SourceStreamWriter sw, RegistryContext ctx, List <CommandParameter> commandParams)
        {
            // Signature
            GenerateImplementation_Signature(sw, ctx, commandParams);

            // Implementation block
            sw.WriteLine("{");
            sw.Indent();

            #region Pinned Object Block (Open)

            foreach (CommandParameter param in commandParams)
            {
                param.WritePinnedVariable(sw, ctx, this);
            }

            sw.WriteLine("try {");
            sw.Indent();

            #endregion

            #region Implementation Call

            sw.WriteIdentation();
            if (HasReturnValue)
            {
                sw.Write("return (");
            }

            sw.Write("{0}(", GetImplementationName(ctx));

            #region Parameters

            for (int i = 0; i < commandParams.Count; i++)
            {
                CommandParameter param = commandParams[i];

                param.WriteDelegateParam(sw, ctx, this);

                if (i != commandParams.Count - 1)
                {
                    sw.Write(", ");
                }
            }

            #endregion

            sw.Write(")");

            if (HasReturnValue)
            {
                sw.Write(")");
            }
            sw.Write(";");
            sw.WriteLine();

            #endregion

            #region Pinned Object Block (Close)

            sw.Unindent();
            sw.WriteLine("} finally {");
            sw.Indent();

            foreach (CommandParameter param in commandParams)
            {
                param.WriteUnpinCommand(sw, ctx, this);
            }

            sw.Unindent();
            sw.WriteLine("}");

            #endregion

            sw.Unindent();
            sw.WriteLine("}");
        }
Example #8
0
		internal void GenerateSource(SourceStreamWriter sw, RegistryContext ctx)
		{
			if (sw == null)
				throw new ArgumentNullException("sw");
			if (ctx == null)
				throw new ArgumentNullException("ctx");

			bool bitmask = Enums.TrueForAll(delegate(Enumerant item) {
				Enumerant actualEnumerant = ctx.Registry.GetGlEnumerant(item.Name);

				return (actualEnumerant == null || actualEnumerant.ParentEnumerantBlock.Type == "bitmask");
			});

			// Collect group enumerants by their value
			Dictionary<string, List<Enumerant>> groupEnums = new Dictionary<string, List<Enumerant>>();

			foreach (Enumerant item in Enums) {
				Enumerant itemValue = ctx.Registry.GetGlEnumerant(item.Name);

				if (itemValue != null) {
					if (!groupEnums.ContainsKey(itemValue.Value))
						groupEnums.Add(itemValue.Value, new List<Enumerant>());

					groupEnums[itemValue.Value].Add(itemValue);
				}
			}

			// Make enumerants distinct (discard duplicated enumerants, mainly from extensions)
			List<Enumerant> uniqueEnums = new List<Enumerant>();

			foreach (KeyValuePair<string, List<Enumerant>> pair in groupEnums) {
				if (pair.Value.Count > 1) {
					Enumerant shortedNameEnum = null;

					foreach (Enumerant item in pair.Value) {
						if ((shortedNameEnum == null) || (shortedNameEnum.Name.Length > item.Name.Length))
							shortedNameEnum = item;
					}

					uniqueEnums.Add(shortedNameEnum);
				} else
					uniqueEnums.Add(pair.Value[0]);
			}

			sw.WriteLine("/// <summary>");
			sw.WriteLine("/// Strongly typed enumeration {0}.", Name);
			sw.WriteLine("/// </summary>");
			if (bitmask)
				sw.WriteLine("[Flags()]");
			sw.WriteLine("public enum {0}{1}", Name, bitmask ? " : uint" : String.Empty);
			sw.WriteLine("{");
			sw.Indent();
			foreach (Enumerant enumerant in uniqueEnums) {
				List<Enumerant> allEnums = groupEnums[enumerant.Value];
				string bindingName = enumerant.EnumAlias == null ? enumerant.ImplementationName : enumerant.EnumAlias.ImplementationName;
				string camelCase = SpecificationStyle.GetCamelCase(bindingName);

				sw.WriteLine("/// <summary>");
				if (allEnums.Count > 1) {
					StringBuilder sb = new StringBuilder();

					sb.Append("Strongly typed for value ");
					for (int i = 0; i < allEnums.Count; i++) {
						sb.Append(allEnums[i].Name);
						if (i < allEnums.Count - 1)
							sb.Append(", ");
					}
					sb.Append(".");

					foreach (string docLine in RegistryDocumentation.SplitDocumentationLines(sb.ToString()))
						sw.WriteLine("/// {0}", docLine);
				} else
					sw.WriteLine("/// Strongly typed for value {0}.", enumerant.Name);
				sw.WriteLine("/// </summary>");
				sw.WriteLine("{0} = Gl.{1},", camelCase, bindingName);
				sw.WriteLine();
			}
			sw.Unindent();
			sw.WriteLine("}");
		}
Example #9
0
		/// <summary>
		/// Generate the command implementation (fixed variant).
		/// </summary>
		/// <param name="sw">
		/// The <see cref="SourceStreamWriter"/> used to write the source code.
		/// </param>
		/// <param name="ctx">
		/// The <see cref="RegistryContext"/> defining the OpenGL registry information.
		/// </param>
		/// <param name="commandParams">
		/// A <see cref="T:List{CommandParameter}"/> determining the method overload.
		/// </param>
		private void GenerateImplementation_Default(SourceStreamWriter sw, RegistryContext ctx, List<CommandParameter> commandParams)
		{
			List<Command> aliases = new List<Command>();
			Command aliasCommand = this;

			// The implementation returned type
			string returnType = aliasCommand.GetImplementationReturnType(ctx);
			// Is fixed implementation
			bool fixedImplementation = IsFixedImplementation(ctx, commandParams);

			aliases.Add(this);
			aliases.AddRange(Aliases);

			// Signature
			GenerateImplementation_Signature(sw, ctx, commandParams);

			// Implementation block
			sw.WriteLine("{");
			sw.Indent();

			#region Debug Assertions

			// Debug assertions
			foreach (CommandParameter param in commandParams)
				param.WriteDebugAssertion(sw, ctx, this);

			#endregion

			#region Local Variables

			// Local variable: returned value
			if (HasReturnValue) {
				sw.WriteLine("{0} {1};", DelegateReturnType, ReturnVariableName);
				sw.WriteLine();
			}

			#endregion

			#region Unsafe Block (Open)

			if (fixedImplementation) {
				sw.WriteLine("unsafe {");								// (1)
				sw.Indent();

				foreach (CommandParameter param in commandParams)
					param.WriteFixedStatement(sw, ctx, this);

				sw.WriteLine("{");										// (2)
				sw.Indent();
			}

			#endregion

			sw.WriteLine("Debug.Assert(Delegates.{0} != null, \"{0} not implemented\");", aliasCommand.DelegateName);

			#region Delegate Call

			sw.WriteIdentation();

			// Local return value
			if (HasReturnValue) 
				sw.Write("{0} = ", ReturnVariableName);

			sw.Write("Delegates.{0}(", aliasCommand.DelegateName);

			#region Parameters

			for (int i = 0; i < commandParams.Count; i++) {
				CommandParameter param = commandParams[i];

				param.WriteDelegateParam(sw, ctx, this);

				if (i != Parameters.Count - 1)
					sw.Write(", ");
			}

			#endregion

			sw.Write(")");

			sw.Write(";");
			sw.WriteLine();

			#endregion

			#region Call Log

			sw.WriteIdentation(); sw.Write("LogFunction(");

			#region Call Log - Format String

			sw.Write("\"{0}(", aliasCommand.ImportName);
			for (int i = 0; i < commandParams.Count; i++)
			{
				commandParams[i].WriteCallLogFormatParam(sw, ctx, this, i);
				if (i < commandParams.Count - 1)
					sw.Write(", ");
			}
			sw.Write(")");

			if (HasReturnValue)
				sw.Write(" = {{{0}}}", commandParams.Count);

			sw.Write("\"");

			#endregion

			#region Call Log - Format Arguments

			if ((commandParams.Count > 0) || HasReturnValue) {
				sw.Write(", ");

				for (int i = 0; i < commandParams.Count; i++) {
					commandParams[i].WriteCallLogArgParam(sw, ctx, this);
					if (i < commandParams.Count - 1)
						sw.Write(", ");
				}
			}

			// Return value
			if (HasReturnValue)
			{
				if (commandParams.Count > 0)
					sw.Write(", ");
				CommandParameter.WriteCallLogArgParam(sw, GetReturnValueExpression(ctx), returnType);
			}

			#endregion

			sw.Write(");");
			sw.WriteLine();

			#endregion

			#region Unsafe Block (Close)

			if (fixedImplementation)
			{
				sw.Unindent();
				sw.WriteLine("}");										// (2) CLOSED
				sw.Unindent();
				sw.WriteLine("}");										// (1) CLOSED
			}

			#endregion

			// Check call errors
			if ((Flags & CommandFlags.NoGetError) == 0) {
				string returnValue = "null";

				// Optionally pass the returned value to error checking method
				if (HasReturnValue)
					returnValue = ReturnVariableName;

				sw.WriteLine("DebugCheckErrors({0});", returnValue);
			}

			// Returns value
			if (HasReturnValue) {
				sw.WriteLine();
				sw.WriteLine("return ({0});", GetReturnValueExpression(ctx));
			}

			sw.Unindent();
			sw.WriteLine("}");
		}
Example #10
0
        internal void GenerateSource(SourceStreamWriter sw, RegistryContext ctx)
        {
            if (sw == null)
            {
                throw new ArgumentNullException("sw");
            }
            if (ctx == null)
            {
                throw new ArgumentNullException("ctx");
            }

            bool bitmask = Enums.TrueForAll(delegate(Enumerant item) {
                Enumerant actualEnumerant = ctx.Registry.GetEnumerant(item.Name);

                return(actualEnumerant == null || actualEnumerant.ParentEnumerantBlock.Type == "bitmask");
            });

            // Collect group enumerants by their value
            Dictionary <string, List <Enumerant> > groupEnums = new Dictionary <string, List <Enumerant> >();

            // ...include all enums defined in this group
            foreach (Enumerant item in Enums)
            {
                Enumerant itemValue = ctx.Registry.GetEnumerant(item.Name);

                if (itemValue != null)
                {
                    if (!groupEnums.ContainsKey(itemValue.Value))
                    {
                        groupEnums.Add(itemValue.Value, new List <Enumerant>());
                    }
                    groupEnums[itemValue.Value].Add(itemValue);
                }
            }

            // Modify canonical enumeration (value/block/group) definition
            CommandFlagsDatabase.EnumerantItem enumerantExtension = CommandFlagsDatabase.FindEnumerant(Name);

            if (enumerantExtension != null)
            {
                // ...override group information
                if (enumerantExtension.Type != null)
                {
                    switch (enumerantExtension.Type)
                    {
                    case "bitmask":
                        bitmask = true;
                        break;
                    }
                }

                // ...include all enums to be added by additional configuration
                foreach (string addedEnum in enumerantExtension.AddEnumerants)
                {
                    Enumerant addedEnumValue = ctx.Registry.GetEnumerant(addedEnum);

                    if (addedEnumValue != null)
                    {
                        if (!groupEnums.ContainsKey(addedEnumValue.Value))
                        {
                            groupEnums.Add(addedEnumValue.Value, new List <Enumerant>());
                        }

                        // Note: since specification can be updated while the CommandFlags.xml is not in synch, the specification
                        // may defined missed enumerant values. In this case do not add enumerant value
                        if (groupEnums[addedEnumValue.Value].Contains(addedEnumValue) == false)
                        {
                            groupEnums[addedEnumValue.Value].Add(addedEnumValue);
                        }
                    }
                }
            }

            // Make enumerants distinct (discard duplicated enumerants, mainly from extensions _ARB, _EXT, ...)
            List <Enumerant> uniqueEnums = new List <Enumerant>();

            foreach (KeyValuePair <string, List <Enumerant> > pair in groupEnums)
            {
                if (pair.Value.Count > 1)
                {
                    List <Enumerant> uniqueNames = new List <Enumerant>();

                    foreach (Enumerant item in pair.Value)
                    {
                        if (item.Alias != null)
                        {
                            continue;
                        }
                        if (item.EnumAlias != null)
                        {
                            continue;
                        }
                        if (uniqueNames.FindIndex(delegate(Enumerant item1) { return(item.Name.StartsWith(item1.Name)); }) >= 0)
                        {
                            continue;
                        }

                        if (uniqueNames.FindIndex(delegate(Enumerant item1) { return(item1.Name.StartsWith(item.Name)); }) >= 0)
                        {
                            uniqueNames.RemoveAll(delegate(Enumerant item1) { return(item1.Name.StartsWith(item.Name)); });
                        }

                        uniqueNames.Add(item);
                    }

                    uniqueEnums.AddRange(uniqueNames);
                }
                else
                {
                    uniqueEnums.AddRange(pair.Value);
                }
            }

            sw.WriteLine("/// <summary>");
            sw.WriteLine("/// Strongly typed enumeration {0}.", Name);
            sw.WriteLine("/// </summary>");
            if (bitmask)
            {
                sw.WriteLine("[Flags()]");
            }
            sw.WriteLine("public enum {0}{1}", Name, bitmask ? " : uint" : String.Empty);
            sw.WriteLine("{");
            sw.Indent();
            foreach (Enumerant enumerant in uniqueEnums)
            {
                List <Enumerant> allEnums    = groupEnums[enumerant.Value];
                string           bindingName = enumerant.EnumAlias == null ? enumerant.ImplementationName : enumerant.EnumAlias.ImplementationName;
                string           camelCase   = SpecificationStyle.GetCamelCase(bindingName);

                sw.WriteLine("/// <summary>");
                if (allEnums.Count > 1)
                {
                    StringBuilder sb = new StringBuilder();

                    sb.Append("Strongly typed for value ");
                    for (int i = 0; i < allEnums.Count; i++)
                    {
                        sb.Append(allEnums[i].Name);
                        if (i < allEnums.Count - 1)
                        {
                            sb.Append(", ");
                        }
                    }
                    sb.Append(".");

                    foreach (string docLine in RegistryDocumentation.SplitDocumentationLines(sb.ToString()))
                    {
                        sw.WriteLine("/// {0}", docLine);
                    }
                }
                else
                {
                    sw.WriteLine("/// Strongly typed for value {0}.", enumerant.Name);
                }
                sw.WriteLine("/// </summary>");

                Enumerant enumvalue       = ctx.Registry.GetEnumerant(ctx.Class.ToUpperInvariant() + "_" + bindingName);
                string    classDefaultApi = ctx.Class.ToLower();

                if (enumvalue != null)
                {
                    // RequiredByFeature
                    foreach (IFeature feature in enumvalue.RequiredBy)
                    {
                        sw.WriteLine(feature.GetRequiredByFeature(classDefaultApi));
                    }
                    // RequiredByFeature (from aliases) Note: not sure that Profile is considered here
                    foreach (Enumerant aliasOf in enumvalue.AliasOf)
                    {
                        foreach (IFeature feature in aliasOf.RequiredBy)
                        {
                            sw.WriteLine(feature.GetRequiredByFeature(classDefaultApi));
                        }
                    }
                    // RemovedByFeature
                    foreach (IFeature feature in enumvalue.RemovedBy)
                    {
                        sw.WriteLine(feature.GetRemovedByFeature(classDefaultApi));
                    }
                }

                sw.WriteLine("{0} = {1}.{2},", camelCase, ctx.Class, bindingName);
                sw.WriteLine();
            }
            sw.Unindent();
            sw.WriteLine("}");
        }